1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP-4, BGP-4+ daemon program
3 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
14 #include "sockunion.h"
23 #include "workqueue.h"
31 #include "lib/sockopt.h"
32 #include "frr_pthread.h"
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_aspath.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_dump.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_errors.h"
42 #include "bgpd/bgp_community.h"
43 #include "bgpd/bgp_community_alias.h"
44 #include "bgpd/bgp_conditional_adv.h"
45 #include "bgpd/bgp_attr.h"
46 #include "bgpd/bgp_regex.h"
47 #include "bgpd/bgp_clist.h"
48 #include "bgpd/bgp_fsm.h"
49 #include "bgpd/bgp_packet.h"
50 #include "bgpd/bgp_zebra.h"
51 #include "bgpd/bgp_open.h"
52 #include "bgpd/bgp_filter.h"
53 #include "bgpd/bgp_nexthop.h"
54 #include "bgpd/bgp_damp.h"
55 #include "bgpd/bgp_mplsvpn.h"
57 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
58 #include "bgpd/rfapi/rfapi_backend.h"
60 #include "bgpd/bgp_evpn.h"
61 #include "bgpd/bgp_advertise.h"
62 #include "bgpd/bgp_network.h"
63 #include "bgpd/bgp_vty.h"
64 #include "bgpd/bgp_mpath.h"
65 #include "bgpd/bgp_nht.h"
66 #include "bgpd/bgp_updgrp.h"
67 #include "bgpd/bgp_bfd.h"
68 #include "bgpd/bgp_memory.h"
69 #include "bgpd/bgp_evpn_vty.h"
70 #include "bgpd/bgp_keepalives.h"
71 #include "bgpd/bgp_io.h"
72 #include "bgpd/bgp_ecommunity.h"
73 #include "bgpd/bgp_flowspec.h"
74 #include "bgpd/bgp_labelpool.h"
75 #include "bgpd/bgp_pbr.h"
76 #include "bgpd/bgp_addpath.h"
77 #include "bgpd/bgp_evpn_private.h"
78 #include "bgpd/bgp_evpn_mh.h"
79 #include "bgpd/bgp_mac.h"
80 #include "bgp_trace.h"
82 DEFINE_MTYPE_STATIC(BGPD
, PEER_TX_SHUTDOWN_MSG
, "Peer shutdown message (TX)");
83 DEFINE_MTYPE_STATIC(BGPD
, BGP_EVPN_INFO
, "BGP EVPN instance information");
84 DEFINE_QOBJ_TYPE(bgp_master
);
85 DEFINE_QOBJ_TYPE(bgp
);
86 DEFINE_QOBJ_TYPE(peer
);
87 DEFINE_HOOK(bgp_inst_delete
, (struct bgp
*bgp
), (bgp
));
89 /* BGP process wide configuration. */
90 static struct bgp_master bgp_master
;
92 /* BGP process wide configuration pointer to export. */
93 struct bgp_master
*bm
;
95 /* BGP community-list. */
96 struct community_list_handler
*bgp_clist
;
98 unsigned int multipath_num
= MULTIPATH_NUM
;
100 /* Number of bgp instances configured for suppress fib config */
101 unsigned int bgp_suppress_fib_count
;
103 static void bgp_if_finish(struct bgp
*bgp
);
104 static void peer_drop_dynamic_neighbor(struct peer
*peer
);
106 extern struct zclient
*zclient
;
108 /* handle main socket creation or deletion */
109 static int bgp_check_main_socket(bool create
, struct bgp
*bgp
)
111 static int bgp_server_main_created
;
112 struct listnode
*node
;
116 if (bgp_server_main_created
)
118 if (list_isempty(bm
->addresses
)) {
119 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
120 return BGP_ERR_INVALID_VALUE
;
122 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
123 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
124 return BGP_ERR_INVALID_VALUE
;
126 bgp_server_main_created
= 1;
129 if (!bgp_server_main_created
)
132 bgp_server_main_created
= 0;
136 void bgp_session_reset(struct peer
*peer
)
138 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
139 && !(CHECK_FLAG(peer
->doppelganger
->flags
, PEER_FLAG_CONFIG_NODE
)))
140 peer_delete(peer
->doppelganger
);
142 BGP_EVENT_ADD(peer
, BGP_Stop
);
146 * During session reset, we may delete the doppelganger peer, which would
147 * be the next node to the current node. If the session reset was invoked
148 * during walk of peer list, we would end up accessing the freed next
149 * node. This function moves the next node along.
151 static void bgp_session_reset_safe(struct peer
*peer
, struct listnode
**nnode
)
156 n
= (nnode
) ? *nnode
: NULL
;
157 npeer
= (n
) ? listgetdata(n
) : NULL
;
159 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
160 && !(CHECK_FLAG(peer
->doppelganger
->flags
,
161 PEER_FLAG_CONFIG_NODE
))) {
162 if (peer
->doppelganger
== npeer
)
163 /* nnode and *nnode are confirmed to be non-NULL here */
164 *nnode
= (*nnode
)->next
;
165 peer_delete(peer
->doppelganger
);
168 BGP_EVENT_ADD(peer
, BGP_Stop
);
171 /* BGP global flag manipulation. */
172 int bgp_option_set(int flag
)
176 case BGP_OPT_NO_LISTEN
:
177 case BGP_OPT_NO_ZEBRA
:
178 SET_FLAG(bm
->options
, flag
);
181 return BGP_ERR_INVALID_FLAG
;
186 int bgp_option_unset(int flag
)
190 case BGP_OPT_NO_ZEBRA
:
192 UNSET_FLAG(bm
->options
, flag
);
195 return BGP_ERR_INVALID_FLAG
;
200 int bgp_option_check(int flag
)
202 return CHECK_FLAG(bm
->options
, flag
);
205 /* set the bgp no-rib option during runtime and remove installed routes */
206 void bgp_option_norib_set_runtime(void)
209 struct listnode
*node
;
213 if (bgp_option_check(BGP_OPT_NO_FIB
))
216 bgp_option_set(BGP_OPT_NO_FIB
);
218 zlog_info("Disabled BGP route installation to RIB (Zebra)");
220 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
221 FOREACH_AFI_SAFI (afi
, safi
) {
223 * Stop a crash, more work is needed
224 * here to properly add/remove these types of
227 if (!bgp_fibupd_safi(safi
))
230 bgp_zebra_withdraw_table_all_subtypes(bgp
, afi
, safi
);
234 zlog_info("All routes have been withdrawn from RIB (Zebra)");
237 /* unset the bgp no-rib option during runtime and announce routes to Zebra */
238 void bgp_option_norib_unset_runtime(void)
241 struct listnode
*node
;
245 if (!bgp_option_check(BGP_OPT_NO_FIB
))
248 bgp_option_unset(BGP_OPT_NO_FIB
);
250 zlog_info("Enabled BGP route installation to RIB (Zebra)");
252 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
253 FOREACH_AFI_SAFI (afi
, safi
) {
255 * Stop a crash, more work is needed
256 * here to properly add/remove these types
257 * of routes from zebra
259 if (!bgp_fibupd_safi(safi
))
262 bgp_zebra_announce_table_all_subtypes(bgp
, afi
, safi
);
266 zlog_info("All routes have been installed in RIB (Zebra)");
269 /* Internal function to set BGP structure configureation flag. */
270 static void bgp_config_set(struct bgp
*bgp
, int config
)
272 SET_FLAG(bgp
->config
, config
);
275 static void bgp_config_unset(struct bgp
*bgp
, int config
)
277 UNSET_FLAG(bgp
->config
, config
);
280 static int bgp_config_check(struct bgp
*bgp
, int config
)
282 return CHECK_FLAG(bgp
->config
, config
);
285 /* Set BGP router identifier; distinguish between explicit config and other
288 static int bgp_router_id_set(struct bgp
*bgp
, const struct in_addr
*id
,
292 struct listnode
*node
, *nnode
;
294 if (IPV4_ADDR_SAME(&bgp
->router_id
, id
))
297 /* EVPN uses router id in RD, withdraw them */
298 if (is_evpn_enabled())
299 bgp_evpn_handle_router_id_update(bgp
, true);
301 vpn_handle_router_id_update(bgp
, true, is_config
);
303 IPV4_ADDR_COPY(&bgp
->router_id
, id
);
305 /* Set all peer's local identifier with this value. */
306 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
307 IPV4_ADDR_COPY(&peer
->local_id
, id
);
309 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
310 peer
->last_reset
= PEER_DOWN_RID_CHANGE
;
311 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
312 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
316 /* EVPN uses router id in RD, update them */
317 if (is_evpn_enabled())
318 bgp_evpn_handle_router_id_update(bgp
, false);
320 vpn_handle_router_id_update(bgp
, false, is_config
);
325 void bgp_router_id_zebra_bump(vrf_id_t vrf_id
, const struct prefix
*router_id
)
327 struct listnode
*node
, *nnode
;
329 struct in_addr
*addr
= NULL
;
331 if (router_id
!= NULL
)
332 addr
= (struct in_addr
*)&(router_id
->u
.prefix4
);
334 if (vrf_id
== VRF_DEFAULT
) {
335 /* Router-id change for default VRF has to also update all
337 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
338 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
342 bgp
->router_id_zebra
= *addr
;
344 addr
= &bgp
->router_id_zebra
;
346 if (!bgp
->router_id_static
.s_addr
) {
347 /* Router ID is updated if there are no active
350 if (bgp
->established_peers
== 0) {
351 if (BGP_DEBUG(zebra
, ZEBRA
))
353 "RID change : vrf %s(%u), RTR ID %pI4",
357 * if old router-id was 0x0, set flag
358 * to use this new value
360 bgp_router_id_set(bgp
, addr
,
361 (bgp
->router_id
.s_addr
369 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
372 bgp
->router_id_zebra
= *addr
;
374 addr
= &bgp
->router_id_zebra
;
376 if (!bgp
->router_id_static
.s_addr
) {
377 /* Router ID is updated if there are no active
380 if (bgp
->established_peers
== 0) {
381 if (BGP_DEBUG(zebra
, ZEBRA
))
383 "RID change : vrf %s(%u), RTR ID %pI4",
387 * if old router-id was 0x0, set flag
388 * to use this new value
390 bgp_router_id_set(bgp
, addr
,
391 (bgp
->router_id
.s_addr
402 void bgp_router_id_static_set(struct bgp
*bgp
, struct in_addr id
)
404 bgp
->router_id_static
= id
;
405 bgp_router_id_set(bgp
,
406 id
.s_addr
!= INADDR_ANY
? &id
: &bgp
->router_id_zebra
,
407 true /* is config */);
410 void bm_wait_for_fib_set(bool set
)
412 bool send_msg
= false;
414 if (bm
->wait_for_fib
== set
)
417 bm
->wait_for_fib
= set
;
419 if (bgp_suppress_fib_count
== 0)
421 bgp_suppress_fib_count
++;
423 bgp_suppress_fib_count
--;
424 if (bgp_suppress_fib_count
== 0)
428 if (send_msg
&& zclient
)
429 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
433 /* Set the suppress fib pending for the bgp configuration */
434 void bgp_suppress_fib_pending_set(struct bgp
*bgp
, bool set
)
436 bool send_msg
= false;
438 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
442 SET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
443 /* Send msg to zebra for the first instance of bgp enabled
446 if (bgp_suppress_fib_count
== 0)
448 bgp_suppress_fib_count
++;
450 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
451 bgp_suppress_fib_count
--;
453 /* Send msg to zebra if there are no instances enabled
456 if (bgp_suppress_fib_count
== 0)
459 /* Send route notify request to RIB */
461 if (BGP_DEBUG(zebra
, ZEBRA
))
462 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
465 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
470 /* BGP's cluster-id control. */
471 void bgp_cluster_id_set(struct bgp
*bgp
, struct in_addr
*cluster_id
)
474 struct listnode
*node
, *nnode
;
476 if (bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
)
477 && IPV4_ADDR_SAME(&bgp
->cluster_id
, cluster_id
))
480 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
481 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
483 /* Clear all IBGP peer. */
484 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
485 if (peer
->sort
!= BGP_PEER_IBGP
)
488 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
489 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
490 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
491 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
496 void bgp_cluster_id_unset(struct bgp
*bgp
)
499 struct listnode
*node
, *nnode
;
501 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
504 bgp
->cluster_id
.s_addr
= 0;
505 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
507 /* Clear all IBGP peer. */
508 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
509 if (peer
->sort
!= BGP_PEER_IBGP
)
512 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
513 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
514 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
515 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
520 /* BGP timer configuration. */
521 void bgp_timers_set(struct bgp
*bgp
, uint32_t keepalive
, uint32_t holdtime
,
522 uint32_t connect_retry
, uint32_t delayopen
)
524 bgp
->default_keepalive
=
525 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
526 bgp
->default_holdtime
= holdtime
;
527 bgp
->default_connect_retry
= connect_retry
;
528 bgp
->default_delayopen
= delayopen
;
531 /* mostly for completeness - CLI uses its own defaults */
532 void bgp_timers_unset(struct bgp
*bgp
)
534 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
535 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
536 bgp
->default_connect_retry
= BGP_DEFAULT_CONNECT_RETRY
;
537 bgp
->default_delayopen
= BGP_DEFAULT_DELAYOPEN
;
540 void bgp_tcp_keepalive_set(struct bgp
*bgp
, uint16_t keepalive_idle
,
541 uint16_t keepalive_intvl
, uint16_t keepalive_probes
)
543 bgp
->tcp_keepalive_idle
= keepalive_idle
;
544 bgp
->tcp_keepalive_intvl
= keepalive_intvl
;
545 bgp
->tcp_keepalive_probes
= keepalive_probes
;
548 void bgp_tcp_keepalive_unset(struct bgp
*bgp
)
550 bgp
->tcp_keepalive_idle
= 0;
551 bgp
->tcp_keepalive_intvl
= 0;
552 bgp
->tcp_keepalive_probes
= 0;
555 /* BGP confederation configuration. */
556 void bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
559 struct listnode
*node
, *nnode
;
565 /* Remember - were we doing confederation before? */
566 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
568 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
570 /* If we were doing confederation already, this is just an external
571 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
572 were not doing confederation before, reset all EBGP sessions. */
573 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
574 enum bgp_peer_sort ptype
= peer_sort(peer
);
576 /* We're looking for peers who's AS is not local or part of our
578 if (already_confed
) {
579 if (ptype
== BGP_PEER_EBGP
) {
581 if (BGP_IS_VALID_STATE_FOR_NOTIF(
584 PEER_DOWN_CONFED_ID_CHANGE
;
586 peer
, BGP_NOTIFY_CEASE
,
587 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
589 bgp_session_reset_safe(peer
, &nnode
);
592 /* Not doign confederation before, so reset every
595 if (ptype
!= BGP_PEER_IBGP
) {
596 /* Reset the local_as to be our EBGP one */
597 if (ptype
== BGP_PEER_EBGP
)
599 if (BGP_IS_VALID_STATE_FOR_NOTIF(
602 PEER_DOWN_CONFED_ID_CHANGE
;
604 peer
, BGP_NOTIFY_CEASE
,
605 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
607 bgp_session_reset_safe(peer
, &nnode
);
614 void bgp_confederation_id_unset(struct bgp
*bgp
)
617 struct listnode
*node
, *nnode
;
620 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
622 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
623 /* We're looking for peers who's AS is not local */
624 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
625 peer
->local_as
= bgp
->as
;
626 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
627 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
628 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
629 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
633 bgp_session_reset_safe(peer
, &nnode
);
638 /* Is an AS part of the confed or not? */
639 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
646 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
647 if (bgp
->confed_peers
[i
] == as
)
653 /* Add an AS to the confederation set. */
654 void bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
657 struct listnode
*node
, *nnode
;
662 if (bgp_confederation_peers_check(bgp
, as
))
666 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
667 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
669 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
670 bgp
->confed_peers_cnt
++;
672 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
673 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
674 if (peer
->as
== as
) {
675 peer
->local_as
= bgp
->as
;
676 (void)peer_sort(peer
);
677 if (BGP_IS_VALID_STATE_FOR_NOTIF(
680 PEER_DOWN_CONFED_PEER_CHANGE
;
682 peer
, BGP_NOTIFY_CEASE
,
683 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
685 bgp_session_reset_safe(peer
, &nnode
);
691 /* Delete an AS from the confederation set. */
692 void bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
697 struct listnode
*node
, *nnode
;
702 if (!bgp_confederation_peers_check(bgp
, as
))
705 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
706 if (bgp
->confed_peers
[i
] == as
)
707 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
708 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
710 bgp
->confed_peers_cnt
--;
712 if (bgp
->confed_peers_cnt
== 0) {
713 if (bgp
->confed_peers
)
714 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
715 bgp
->confed_peers
= NULL
;
718 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
719 bgp
->confed_peers_cnt
* sizeof(as_t
));
721 /* Now reset any peer who's remote AS has just been removed from the
723 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
724 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
725 if (peer
->as
== as
) {
726 peer
->local_as
= bgp
->confed_id
;
727 (void)peer_sort(peer
);
728 if (BGP_IS_VALID_STATE_FOR_NOTIF(
731 PEER_DOWN_CONFED_PEER_CHANGE
;
733 peer
, BGP_NOTIFY_CEASE
,
734 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
736 bgp_session_reset_safe(peer
, &nnode
);
742 /* Local preference configuration. */
743 void bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
748 bgp
->default_local_pref
= local_pref
;
751 void bgp_default_local_preference_unset(struct bgp
*bgp
)
756 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
759 /* Local preference configuration. */
760 void bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
,
766 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
769 void bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
773 bgp
->default_subgroup_pkt_queue_max
=
774 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
777 /* Listen limit configuration. */
778 void bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
783 bgp
->dynamic_neighbors_limit
= listen_limit
;
786 void bgp_listen_limit_unset(struct bgp
*bgp
)
791 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
794 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
795 afi_t
*afi
, safi_t
*safi
)
797 /* Map from IANA values to internal values, return error if
798 * values are unrecognized.
800 *afi
= afi_iana2int(pkt_afi
);
801 *safi
= safi_iana2int(pkt_safi
);
802 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
808 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
809 iana_safi_t
*pkt_safi
)
811 /* Map from internal values to IANA values, return error if
812 * internal values are bad (unexpected).
814 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
816 *pkt_afi
= afi_int2iana(afi
);
817 *pkt_safi
= safi_int2iana(safi
);
821 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
830 afid
= afindex(afi
, safi
);
831 if (afid
>= BGP_AF_MAX
)
835 assert(peer
->peer_af_array
[afid
] == NULL
);
837 /* Allocate new peer af */
838 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
840 peer
->peer_af_array
[afid
] = af
;
845 bgp
->af_peer_count
[afi
][safi
]++;
850 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
857 afid
= afindex(afi
, safi
);
858 if (afid
>= BGP_AF_MAX
)
861 return peer
->peer_af_array
[afid
];
864 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
873 afid
= afindex(afi
, safi
);
874 if (afid
>= BGP_AF_MAX
)
877 af
= peer
->peer_af_array
[afid
];
882 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
884 bgp_stop_announce_route_timer(af
);
886 if (PAF_SUBGRP(af
)) {
887 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
888 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
889 af
->subgroup
->update_group
->id
,
890 af
->subgroup
->id
, peer
->host
);
894 update_subgroup_remove_peer(af
->subgroup
, af
);
896 if (bgp
->af_peer_count
[afi
][safi
])
897 bgp
->af_peer_count
[afi
][safi
]--;
899 peer
->peer_af_array
[afid
] = NULL
;
900 XFREE(MTYPE_BGP_PEER_AF
, af
);
904 /* Peer comparison function for sorting. */
905 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
907 if (p1
->group
&& !p2
->group
)
910 if (!p1
->group
&& p2
->group
)
913 if (p1
->group
== p2
->group
) {
914 if (p1
->conf_if
&& !p2
->conf_if
)
917 if (!p1
->conf_if
&& p2
->conf_if
)
920 if (p1
->conf_if
&& p2
->conf_if
)
921 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
923 return strcmp(p1
->group
->name
, p2
->group
->name
);
925 return sockunion_cmp(&p1
->su
, &p2
->su
);
928 static unsigned int peer_hash_key_make(const void *p
)
930 const struct peer
*peer
= p
;
931 return sockunion_hash(&peer
->su
);
934 static bool peer_hash_same(const void *p1
, const void *p2
)
936 const struct peer
*peer1
= p1
;
937 const struct peer
*peer2
= p2
;
939 return (sockunion_same(&peer1
->su
, &peer2
->su
)
940 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
941 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
944 void peer_flag_inherit(struct peer
*peer
, uint64_t flag
)
948 /* Skip if peer is not a peer-group member. */
949 if (!peer_group_active(peer
))
952 /* Unset override flag to signal inheritance from peer-group. */
953 UNSET_FLAG(peer
->flags_override
, flag
);
956 * Inherit flag state from peer-group. If the flag of the peer-group is
957 * not being inverted, the peer must inherit the inverse of the current
958 * peer-group flag state.
960 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
961 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
962 && CHECK_FLAG(peer
->flags_invert
, flag
))
963 COND_FLAG(peer
->flags
, flag
, !group_val
);
965 COND_FLAG(peer
->flags
, flag
, group_val
);
968 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
970 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
973 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
978 /* Skip if peer is not a peer-group member. */
979 if (!peer_group_active(peer
))
982 /* Unset override flag to signal inheritance from peer-group. */
983 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
986 * Inherit flag state from peer-group. If the flag of the peer-group is
987 * not being inverted, the peer must inherit the inverse of the current
988 * peer-group flag state.
990 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
991 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
992 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
993 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
995 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
998 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
999 static inline enum bgp_peer_sort
peer_calc_sort(struct peer
*peer
)
1006 if (peer
->change_local_as
)
1007 local_as
= peer
->change_local_as
;
1009 local_as
= peer
->local_as
;
1012 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1013 if (peer
->as_type
== AS_INTERNAL
)
1014 return BGP_PEER_IBGP
;
1016 else if (peer
->as_type
== AS_EXTERNAL
)
1017 return BGP_PEER_EBGP
;
1019 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1021 return (local_as
== peer
->as
? BGP_PEER_IBGP
1028 assert(peer
->group
);
1029 peer1
= listnode_head(peer
->group
->peer
);
1034 return BGP_PEER_INTERNAL
;
1038 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1040 return BGP_PEER_INTERNAL
;
1042 if (local_as
== peer
->as
) {
1043 if (bgp
->as
== bgp
->confed_id
) {
1044 if (local_as
== bgp
->as
)
1045 return BGP_PEER_IBGP
;
1047 return BGP_PEER_EBGP
;
1049 if (local_as
== bgp
->confed_id
)
1050 return BGP_PEER_EBGP
;
1052 return BGP_PEER_IBGP
;
1056 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1057 return BGP_PEER_CONFED
;
1059 return BGP_PEER_EBGP
;
1061 if (peer
->as_type
== AS_UNSPECIFIED
) {
1062 /* check if in peer-group with AS information */
1064 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1065 if (peer
->group
->conf
->as_type
1067 if (local_as
== peer
->group
->conf
->as
)
1068 return BGP_PEER_IBGP
;
1070 return BGP_PEER_EBGP
;
1071 } else if (peer
->group
->conf
->as_type
1073 return BGP_PEER_IBGP
;
1075 return BGP_PEER_EBGP
;
1077 /* no AS information anywhere, let caller know */
1078 return BGP_PEER_UNSPECIFIED
;
1079 } else if (peer
->as_type
!= AS_SPECIFIED
)
1080 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1083 return (local_as
== 0 ? BGP_PEER_INTERNAL
1084 : local_as
== peer
->as
? BGP_PEER_IBGP
1089 /* Calculate and cache the peer "sort" */
1090 enum bgp_peer_sort
peer_sort(struct peer
*peer
)
1092 peer
->sort
= peer_calc_sort(peer
);
1096 enum bgp_peer_sort
peer_sort_lookup(struct peer
*peer
)
1101 static void peer_free(struct peer
*peer
)
1106 assert(peer
->status
== Deleted
);
1110 /* this /ought/ to have been done already through bgp_stop earlier,
1111 * but just to be sure..
1113 bgp_timer_set(peer
);
1114 bgp_reads_off(peer
);
1115 bgp_writes_off(peer
);
1116 thread_cancel_event_ready(bm
->master
, peer
);
1117 FOREACH_AFI_SAFI (afi
, safi
)
1118 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
1119 assert(!peer
->t_write
);
1120 assert(!peer
->t_read
);
1121 BGP_EVENT_FLUSH(peer
);
1123 pthread_mutex_destroy(&peer
->io_mtx
);
1125 /* Free connected nexthop, if present */
1126 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1127 && !peer_dynamic_neighbor(peer
))
1128 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1131 FOREACH_AFI_SAFI (afi
, safi
) {
1132 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1133 XFREE(MTYPE_BGP_FILTER_NAME
,
1134 peer
->filter
[afi
][safi
].advmap
.aname
);
1135 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1136 XFREE(MTYPE_BGP_FILTER_NAME
,
1137 peer
->filter
[afi
][safi
].advmap
.cname
);
1140 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1142 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1143 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1144 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
1145 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1146 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1148 /* Update source configuration. */
1149 if (peer
->update_source
) {
1150 sockunion_free(peer
->update_source
);
1151 peer
->update_source
= NULL
;
1154 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1156 XFREE(MTYPE_BGP_NOTIFICATION
, peer
->notify
.data
);
1157 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1159 if (peer
->clear_node_queue
)
1160 work_queue_free_and_null(&peer
->clear_node_queue
);
1162 bgp_sync_delete(peer
);
1164 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1166 XFREE(MTYPE_BGP_SOFT_VERSION
, peer
->soft_version
);
1168 /* Remove BFD configuration. */
1169 if (peer
->bfd_config
)
1170 bgp_peer_remove_bfd_config(peer
);
1172 FOREACH_AFI_SAFI (afi
, safi
)
1173 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1175 bgp_unlock(peer
->bgp
);
1177 memset(peer
, 0, sizeof(struct peer
));
1179 XFREE(MTYPE_BGP_PEER
, peer
);
1182 /* increase reference count on a struct peer */
1183 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1185 frrtrace(2, frr_bgp
, bgp_peer_lock
, peer
, name
);
1186 assert(peer
&& (peer
->lock
>= 0));
1193 /* decrease reference count on a struct peer
1194 * struct peer is freed and NULL returned if last reference
1196 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1198 frrtrace(2, frr_bgp
, bgp_peer_unlock
, peer
, name
);
1199 assert(peer
&& (peer
->lock
> 0));
1203 if (peer
->lock
== 0) {
1210 /* BGP GR changes */
1212 int bgp_global_gr_init(struct bgp
*bgp
)
1214 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1215 zlog_debug("%s called ..", __func__
);
1217 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1218 /* GLOBAL_HELPER Mode */
1221 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1222 GLOBAL_GR
, GLOBAL_INVALID
,
1223 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1224 GLOBAL_DISABLE
, GLOBAL_INVALID
1226 /* GLOBAL_GR Mode */
1229 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1230 GLOBAL_GR
, GLOBAL_HELPER
,
1231 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1232 GLOBAL_DISABLE
, GLOBAL_INVALID
1234 /* GLOBAL_DISABLE Mode */
1237 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1238 GLOBAL_GR
, GLOBAL_INVALID
,
1239 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1240 GLOBAL_INVALID
, GLOBAL_HELPER
1242 /* GLOBAL_INVALID Mode */
1245 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1246 GLOBAL_INVALID
, GLOBAL_INVALID
,
1247 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1248 GLOBAL_INVALID
, GLOBAL_INVALID
1251 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1252 sizeof(local_GLOBAL_GR_FSM
));
1254 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1255 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1257 return BGP_GR_SUCCESS
;
1260 int bgp_peer_gr_init(struct peer
*peer
)
1262 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1263 zlog_debug("%s called ..", __func__
);
1265 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1266 [BGP_PEER_GR_EVENT_CMD
] = {
1268 /* PEER_HELPER Mode */
1269 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1270 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1271 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1272 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1273 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1274 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1275 bgp_peer_gr_action
}
1279 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1280 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1281 bgp_peer_gr_action
},
1282 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1283 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1284 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1285 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1288 /* PEER_DISABLE Mode */
1289 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1290 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1291 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1292 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1293 bgp_peer_gr_action
},
1294 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1295 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1298 /* PEER_INVALID Mode */
1299 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1300 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1301 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1302 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1303 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1304 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1307 /* PEER_GLOBAL_INHERIT Mode */
1308 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1309 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1310 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1311 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1312 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1313 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1316 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1317 sizeof(local_Peer_GR_FSM
));
1318 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1319 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1321 return BGP_GR_SUCCESS
;
1324 static void bgp_srv6_init(struct bgp
*bgp
)
1326 bgp
->srv6_enabled
= false;
1327 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1328 bgp
->srv6_locator_chunks
= list_new();
1329 bgp
->srv6_functions
= list_new();
1332 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1334 if (bgp
->srv6_locator_chunks
)
1335 list_delete(&bgp
->srv6_locator_chunks
);
1336 if (bgp
->srv6_functions
)
1337 list_delete(&bgp
->srv6_functions
);
1340 /* Allocate new peer object, implicitely locked. */
1341 struct peer
*peer_new(struct bgp
*bgp
)
1348 /* bgp argument is absolutely required */
1351 /* Allocate new peer. */
1352 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1354 /* Set default value. */
1356 peer
->v_start
= BGP_INIT_START_TIMER
;
1357 peer
->v_connect
= bgp
->default_connect_retry
;
1358 peer
->status
= Idle
;
1359 peer
->ostatus
= Idle
;
1360 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1361 peer
->bgp
= bgp_lock(bgp
);
1362 peer
= peer_lock(peer
); /* initial reference */
1363 peer
->local_role
= ROLE_UNDEFINED
;
1364 peer
->remote_role
= ROLE_UNDEFINED
;
1365 peer
->password
= NULL
;
1366 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1368 /* Set default flags. */
1369 FOREACH_AFI_SAFI (afi
, safi
) {
1370 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1371 SET_FLAG(peer
->af_flags
[afi
][safi
],
1372 PEER_FLAG_SEND_EXT_COMMUNITY
);
1373 SET_FLAG(peer
->af_flags
[afi
][safi
],
1374 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1376 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1377 PEER_FLAG_SEND_COMMUNITY
);
1378 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1379 PEER_FLAG_SEND_EXT_COMMUNITY
);
1380 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1381 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1382 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1383 peer
->soo
[afi
][safi
] = NULL
;
1386 /* set nexthop-unchanged for l2vpn evpn by default */
1387 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1388 PEER_FLAG_NEXTHOP_UNCHANGED
);
1390 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1392 /* Initialize per peer bgp GR FSM */
1393 bgp_peer_gr_init(peer
);
1395 /* Create buffers. */
1396 peer
->ibuf
= stream_fifo_new();
1397 peer
->obuf
= stream_fifo_new();
1398 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1400 /* We use a larger buffer for peer->obuf_work in the event that:
1401 * - We RX a BGP_UPDATE where the attributes alone are just
1402 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1403 * - The user configures an outbound route-map that does many as-path
1404 * prepends or adds many communities. At most they can have
1405 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1406 * large they can make the attributes.
1408 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1409 * bounds checking for every single attribute as we construct an
1413 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1415 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1417 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1419 bgp_sync_init(peer
);
1421 /* Get service port number. */
1422 sp
= getservbyname("bgp", "tcp");
1423 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1425 QOBJ_REG(peer
, peer
);
1430 * This function is invoked when a duplicate peer structure associated with
1431 * a neighbor is being deleted. If this about-to-be-deleted structure is
1432 * the one with all the config, then we have to copy over the info.
1434 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1436 struct peer_af
*paf
;
1444 /* The following function is used by both peer group config copy to
1445 * individual peer and when we transfer config
1447 if (peer_src
->change_local_as
)
1448 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1450 /* peer flags apply */
1451 peer_dst
->flags
= peer_src
->flags
;
1453 * The doppelganger *must* not have a config node stored
1455 UNSET_FLAG(peer_dst
->flags
, PEER_FLAG_CONFIG_NODE
);
1456 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1457 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1459 peer_dst
->local_as
= peer_src
->local_as
;
1460 peer_dst
->port
= peer_src
->port
;
1461 /* copy tcp_mss value */
1462 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1463 (void)peer_sort(peer_dst
);
1464 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1465 peer_dst
->local_role
= peer_src
->local_role
;
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;
1611 struct listnode
*node
;
1612 union sockunion old_su
;
1615 * This function is only ever needed when FRR an interface
1616 * based peering, so this simple test will tell us if
1617 * we are in an interface based configuration or not
1624 prev_family
= peer
->su
.sa
.sa_family
;
1625 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1627 /* If BGP unnumbered is not "v6only", we first see if we can
1629 * peer's IPv4 address.
1631 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1633 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1635 /* If "v6only" or we can't derive peer's IPv4 address, see if
1637 * learnt the peer's IPv6 link-local address. This is from the
1639 * IPv6 address in router advertisement.
1641 if (!peer_addr_updated
)
1643 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1645 /* If we could derive the peer address, we may need to install the
1647 * configured for the peer, if any, on the listen socket. Otherwise,
1649 * that peer's address is not available and uninstall the password, if
1652 if (peer_addr_updated
) {
1653 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1654 && prev_family
== AF_UNSPEC
)
1657 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1658 && prev_family
!= AF_UNSPEC
)
1659 bgp_md5_unset(peer
);
1660 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1661 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1665 * If they are the same, nothing to do here, move along
1667 if (!sockunion_same(&old_su
, &peer
->su
)) {
1668 union sockunion new_su
= peer
->su
;
1669 struct bgp
*bgp
= peer
->bgp
;
1672 * Our peer structure is stored in the bgp->peerhash
1673 * release it before we modify anything in both the
1674 * hash and the list. But *only* if the peer
1675 * is in the bgp->peerhash as that on deletion
1676 * we call bgp_stop which calls this function :(
1677 * so on deletion let's remove from the list first
1678 * and then do the deletion preventing this from
1679 * being added back on the list below when we
1680 * fail to remove it up here.
1684 * listnode_lookup just scans the list
1685 * for the peer structure so it's safe
1686 * to use without modifying the su
1688 node
= listnode_lookup(bgp
->peer
, peer
);
1691 * Let's reset the peer->su release and
1692 * reset it and put it back. We have to
1693 * do this because hash_release will
1694 * scan through looking for a matching
1698 hash_release(peer
->bgp
->peerhash
, peer
);
1699 listnode_delete(peer
->bgp
->peer
, peer
);
1702 (void)hash_get(peer
->bgp
->peerhash
, peer
,
1704 listnode_add_sort(peer
->bgp
->peer
, peer
);
1709 void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1711 struct bgp_dest
*dest
, *ndest
;
1712 struct bgp_table
*table
;
1714 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1715 dest
= bgp_route_next(dest
)) {
1716 table
= bgp_dest_get_bgp_table_info(dest
);
1717 if (table
!= NULL
) {
1718 /* Special handling for 2-level routing
1720 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1721 || safi
== SAFI_EVPN
) {
1722 for (ndest
= bgp_table_top(table
); ndest
;
1723 ndest
= bgp_route_next(ndest
))
1724 bgp_process(bgp
, ndest
, afi
, safi
);
1726 bgp_process(bgp
, dest
, afi
, safi
);
1731 /* Force a bestpath recalculation for all prefixes. This is used
1732 * when 'bgp bestpath' commands are entered.
1734 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1739 FOREACH_AFI_SAFI (afi
, safi
) {
1740 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1745 * Create new BGP peer.
1747 * conf_if and su are mutually exclusive if configuring from the cli.
1748 * If we are handing a doppelganger, then we *must* pass in both
1749 * the original peer's su and conf_if, so that we can appropriately
1750 * track the bgp->peerhash( ie we don't want to remove the current
1751 * one from the config ).
1753 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1754 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1755 int as_type
, struct peer_group
*group
,
1760 char buf
[SU_ADDRSTRLEN
];
1764 peer
= peer_new(bgp
);
1766 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1770 bgp_peer_conf_if_to_su_update(peer
);
1771 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1772 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1775 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1776 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1777 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1779 peer
->local_as
= local_as
;
1780 peer
->as
= remote_as
;
1781 peer
->as_type
= as_type
;
1782 peer
->local_id
= bgp
->router_id
;
1783 peer
->v_holdtime
= bgp
->default_holdtime
;
1784 peer
->v_keepalive
= bgp
->default_keepalive
;
1785 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1786 ? BGP_DEFAULT_IBGP_ROUTEADV
1787 : BGP_DEFAULT_EBGP_ROUTEADV
;
1788 if (bgp_config_inprocess())
1789 peer
->shut_during_cfg
= true;
1791 peer
= peer_lock(peer
); /* bgp peer list reference */
1792 peer
->group
= group
;
1793 listnode_add_sort(bgp
->peer
, peer
);
1796 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1798 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1800 /* Adjust update-group coalesce timer heuristics for # peers. */
1801 if (bgp
->heuristic_coalesce
) {
1802 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1804 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1805 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1808 active
= peer_active(peer
);
1810 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1811 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1813 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1816 /* Last read and reset time set */
1817 peer
->readtime
= peer
->resettime
= monotime(NULL
);
1819 /* Default TTL set. */
1820 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1822 /* Default configured keepalives count for shutdown rtt command */
1823 peer
->rtt_keepalive_conf
= 1;
1825 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1826 * neighbor for the corresponding address family. IPv4 Unicast is
1827 * the only address family enabled by default without expliict
1830 FOREACH_AFI_SAFI (afi
, safi
) {
1831 if (bgp
->default_af
[afi
][safi
]) {
1832 peer
->afc
[afi
][safi
] = 1;
1833 peer_af_create(peer
, afi
, safi
);
1837 /* auto shutdown if configured */
1838 if (bgp
->autoshutdown
)
1839 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1840 /* Set up peer's events and timers. */
1841 else if (!active
&& peer_active(peer
))
1842 bgp_timer_set(peer
);
1844 bgp_peer_gr_flags_update(peer
);
1845 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1850 /* Make accept BGP peer. This function is only called from the test code */
1851 struct peer
*peer_create_accept(struct bgp
*bgp
)
1855 peer
= peer_new(bgp
);
1857 peer
= peer_lock(peer
); /* bgp peer list reference */
1858 listnode_add_sort(bgp
->peer
, peer
);
1859 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1865 * Return true if we have a peer configured to use this afi/safi
1867 bool bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1869 struct listnode
*node
;
1872 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1873 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1876 if (peer
->afc
[afi
][safi
])
1883 /* Change peer's AS number. */
1884 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
)
1886 enum bgp_peer_sort origtype
, newtype
;
1889 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1890 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1891 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1892 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1893 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1895 bgp_session_reset(peer
);
1897 origtype
= peer_sort_lookup(peer
);
1899 peer
->as_type
= as_specified
;
1901 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1902 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1903 && peer
->bgp
->as
!= as
)
1904 peer
->local_as
= peer
->bgp
->confed_id
;
1906 peer
->local_as
= peer
->bgp
->as
;
1908 newtype
= peer_sort(peer
);
1909 /* Advertisement-interval reset */
1910 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1911 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1912 ? BGP_DEFAULT_IBGP_ROUTEADV
1913 : BGP_DEFAULT_EBGP_ROUTEADV
;
1917 if (newtype
== BGP_PEER_IBGP
)
1919 else if (origtype
== BGP_PEER_IBGP
)
1920 peer
->ttl
= BGP_DEFAULT_TTL
;
1922 /* reflector-client reset */
1923 if (newtype
!= BGP_PEER_IBGP
) {
1924 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1925 PEER_FLAG_REFLECTOR_CLIENT
);
1926 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1927 PEER_FLAG_REFLECTOR_CLIENT
);
1928 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1929 PEER_FLAG_REFLECTOR_CLIENT
);
1930 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1931 PEER_FLAG_REFLECTOR_CLIENT
);
1932 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1933 PEER_FLAG_REFLECTOR_CLIENT
);
1934 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1935 PEER_FLAG_REFLECTOR_CLIENT
);
1936 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1937 PEER_FLAG_REFLECTOR_CLIENT
);
1938 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1939 PEER_FLAG_REFLECTOR_CLIENT
);
1940 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1941 PEER_FLAG_REFLECTOR_CLIENT
);
1942 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1943 PEER_FLAG_REFLECTOR_CLIENT
);
1944 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1945 PEER_FLAG_REFLECTOR_CLIENT
);
1946 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1947 PEER_FLAG_REFLECTOR_CLIENT
);
1948 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1949 PEER_FLAG_REFLECTOR_CLIENT
);
1953 /* If peer does not exist, create new one. If peer already exists,
1954 set AS number to the peer. */
1955 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1956 as_t
*as
, int as_type
)
1962 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1964 peer
= peer_lookup(bgp
, su
);
1967 /* Not allowed for a dynamic peer. */
1968 if (peer_dynamic_neighbor(peer
)) {
1970 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1973 /* When this peer is a member of peer-group. */
1975 /* peer-group already has AS number/internal/external */
1976 if (peer
->group
->conf
->as
1977 || peer
->group
->conf
->as_type
) {
1978 /* Return peer group's AS number. */
1979 *as
= peer
->group
->conf
->as
;
1980 return BGP_ERR_PEER_GROUP_MEMBER
;
1983 enum bgp_peer_sort peer_sort_type
=
1984 peer_sort(peer
->group
->conf
);
1986 /* Explicit AS numbers used, compare AS numbers */
1987 if (as_type
== AS_SPECIFIED
) {
1988 if (((peer_sort_type
== BGP_PEER_IBGP
)
1989 && (bgp
->as
!= *as
))
1990 || ((peer_sort_type
== BGP_PEER_EBGP
)
1991 && (bgp
->as
== *as
))) {
1993 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1996 /* internal/external used, compare as-types */
1997 if (((peer_sort_type
== BGP_PEER_IBGP
)
1998 && (as_type
!= AS_INTERNAL
))
1999 || ((peer_sort_type
== BGP_PEER_EBGP
)
2000 && (as_type
!= AS_EXTERNAL
))) {
2002 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2007 /* Existing peer's AS number change. */
2008 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2009 || (peer
->as_type
!= as_type
))
2010 peer_as_change(peer
, *as
, as_type
);
2013 return BGP_ERR_NO_INTERFACE_CONFIG
;
2015 /* If the peer is not part of our confederation, and its not an
2016 iBGP peer then spoof the source AS */
2017 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
2018 && !bgp_confederation_peers_check(bgp
, *as
)
2020 local_as
= bgp
->confed_id
;
2024 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
,
2031 const char *bgp_get_name_by_role(uint8_t role
)
2036 case ROLE_RS_SERVER
:
2038 case ROLE_RS_CLIENT
:
2044 case ROLE_UNDEFINED
:
2050 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
2051 struct peer
*peer
, afi_t afi
,
2055 int out
= FILTER_OUT
;
2057 uint64_t pflags_ovrd
;
2058 uint8_t *pfilter_ovrd
;
2062 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2063 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2065 /* peer af_flags apply */
2066 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2067 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2068 ^ peer
->af_flags_invert
[afi
][safi
];
2069 flags_tmp
&= ~pflags_ovrd
;
2071 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2072 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2073 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2074 conf
->af_flags_invert
[afi
][safi
]);
2076 /* maximum-prefix */
2077 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2078 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2079 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2080 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2083 /* maximum-prefix-out */
2084 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX_OUT
))
2085 PEER_ATTR_INHERIT(peer
, group
, pmax_out
[afi
][safi
]);
2088 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2089 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2092 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_SOO
))
2093 PEER_ATTR_INHERIT(peer
, group
, soo
[afi
][safi
]);
2096 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2097 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2099 /* default-originate route-map */
2100 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2101 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2102 MTYPE_ROUTE_MAP_NAME
);
2103 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2106 /* inbound filter apply */
2107 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2108 PEER_STR_ATTR_INHERIT(peer
, group
,
2109 filter
[afi
][safi
].dlist
[in
].name
,
2110 MTYPE_BGP_FILTER_NAME
);
2111 PEER_ATTR_INHERIT(peer
, group
,
2112 filter
[afi
][safi
].dlist
[in
].alist
);
2115 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2116 PEER_STR_ATTR_INHERIT(peer
, group
,
2117 filter
[afi
][safi
].plist
[in
].name
,
2118 MTYPE_BGP_FILTER_NAME
);
2119 PEER_ATTR_INHERIT(peer
, group
,
2120 filter
[afi
][safi
].plist
[in
].plist
);
2123 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2124 PEER_STR_ATTR_INHERIT(peer
, group
,
2125 filter
[afi
][safi
].aslist
[in
].name
,
2126 MTYPE_BGP_FILTER_NAME
);
2127 PEER_ATTR_INHERIT(peer
, group
,
2128 filter
[afi
][safi
].aslist
[in
].aslist
);
2131 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2132 PEER_STR_ATTR_INHERIT(peer
, group
,
2133 filter
[afi
][safi
].map
[in
].name
,
2134 MTYPE_BGP_FILTER_NAME
);
2135 PEER_ATTR_INHERIT(peer
, group
,
2136 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2139 /* outbound filter apply */
2140 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2141 PEER_STR_ATTR_INHERIT(peer
, group
,
2142 filter
[afi
][safi
].dlist
[out
].name
,
2143 MTYPE_BGP_FILTER_NAME
);
2144 PEER_ATTR_INHERIT(peer
, group
,
2145 filter
[afi
][safi
].dlist
[out
].alist
);
2148 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2149 PEER_STR_ATTR_INHERIT(peer
, group
,
2150 filter
[afi
][safi
].plist
[out
].name
,
2151 MTYPE_BGP_FILTER_NAME
);
2152 PEER_ATTR_INHERIT(peer
, group
,
2153 filter
[afi
][safi
].plist
[out
].plist
);
2156 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2157 PEER_STR_ATTR_INHERIT(peer
, group
,
2158 filter
[afi
][safi
].aslist
[out
].name
,
2159 MTYPE_BGP_FILTER_NAME
);
2160 PEER_ATTR_INHERIT(peer
, group
,
2161 filter
[afi
][safi
].aslist
[out
].aslist
);
2164 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2165 PEER_STR_ATTR_INHERIT(peer
, group
,
2166 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2167 MTYPE_BGP_FILTER_NAME
);
2168 PEER_ATTR_INHERIT(peer
, group
,
2169 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2172 /* nondirectional filter apply */
2173 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2174 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2175 MTYPE_BGP_FILTER_NAME
);
2176 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2179 /* Conditional Advertisements */
2180 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ADVERTISE_MAP
)) {
2181 PEER_STR_ATTR_INHERIT(peer
, group
,
2182 filter
[afi
][safi
].advmap
.aname
,
2183 MTYPE_BGP_FILTER_NAME
);
2184 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.amap
);
2185 PEER_STR_ATTR_INHERIT(peer
, group
,
2186 filter
[afi
][safi
].advmap
.cname
,
2187 MTYPE_BGP_FILTER_NAME
);
2188 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.cmap
);
2189 PEER_ATTR_INHERIT(peer
, group
,
2190 filter
[afi
][safi
].advmap
.condition
);
2193 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2194 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2195 bgp_addpath_type_changed(conf
->bgp
);
2199 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2204 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2205 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2206 __func__
, peer
->host
);
2210 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2212 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2213 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2214 return BGP_ERR_PEER_SAFI_CONFLICT
;
2216 /* Nothing to do if we've already activated this peer */
2217 if (peer
->afc
[afi
][safi
])
2220 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2223 active
= peer_active(peer
);
2224 peer
->afc
[afi
][safi
] = 1;
2227 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2229 if (!active
&& peer_active(peer
)) {
2230 bgp_timer_set(peer
);
2232 if (peer_established(peer
)) {
2233 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2234 peer
->afc_adv
[afi
][safi
] = 1;
2235 bgp_capability_send(peer
, afi
, safi
,
2237 CAPABILITY_ACTION_SET
);
2238 if (peer
->afc_recv
[afi
][safi
]) {
2239 peer
->afc_nego
[afi
][safi
] = 1;
2240 bgp_announce_route(peer
, afi
, safi
,
2244 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2245 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2246 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2249 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2250 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2251 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2252 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2255 * If we are turning on a AFI/SAFI locally and we've
2256 * started bringing a peer up, we need to tell
2257 * the other peer to restart because we might loose
2258 * configuration here because when the doppelganger
2259 * gets to a established state due to how
2260 * we resolve we could just overwrite the afi/safi
2263 other
= peer
->doppelganger
;
2265 && (other
->status
== OpenSent
2266 || other
->status
== OpenConfirm
)) {
2267 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2268 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2269 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2276 /* Activate the peer or peer group for specified AFI and SAFI. */
2277 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2280 struct peer_group
*group
;
2281 struct listnode
*node
, *nnode
;
2282 struct peer
*tmp_peer
;
2285 /* Nothing to do if we've already activated this peer */
2286 if (peer
->afc
[afi
][safi
])
2291 /* This is a peer-group so activate all of the members of the
2292 * peer-group as well */
2293 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2295 /* Do not activate a peer for both SAFI_UNICAST and
2296 * SAFI_LABELED_UNICAST */
2297 if ((safi
== SAFI_UNICAST
2298 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2299 || (safi
== SAFI_LABELED_UNICAST
2300 && peer
->afc
[afi
][SAFI_UNICAST
]))
2301 return BGP_ERR_PEER_SAFI_CONFLICT
;
2303 peer
->afc
[afi
][safi
] = 1;
2304 group
= peer
->group
;
2306 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2307 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2310 ret
|= peer_activate_af(peer
, afi
, safi
);
2313 /* If this is the first peer to be activated for this
2314 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2315 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2316 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2318 if (BGP_DEBUG(zebra
, ZEBRA
))
2320 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2322 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2323 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2326 if (safi
== SAFI_FLOWSPEC
) {
2327 /* connect to table manager */
2328 bgp_zebra_init_tm_connect(bgp
);
2333 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2336 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2337 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2338 __func__
, peer
->host
);
2342 /* Nothing to do if we've already deactivated this peer */
2343 if (!peer
->afc
[afi
][safi
])
2346 /* De-activate the address family configuration. */
2347 peer
->afc
[afi
][safi
] = 0;
2349 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2350 flog_err(EC_BGP_PEER_DELETE
,
2351 "couldn't delete af structure for peer %s(%s, %s)",
2352 peer
->host
, afi2str(afi
), safi2str(safi
));
2356 if (peer_established(peer
)) {
2357 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2358 peer
->afc_adv
[afi
][safi
] = 0;
2359 peer
->afc_nego
[afi
][safi
] = 0;
2361 if (peer_active_nego(peer
)) {
2362 bgp_capability_send(peer
, afi
, safi
,
2364 CAPABILITY_ACTION_UNSET
);
2365 bgp_clear_route(peer
, afi
, safi
);
2366 peer
->pcount
[afi
][safi
] = 0;
2368 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2369 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2370 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2373 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2374 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2375 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2382 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2385 struct peer_group
*group
;
2386 struct peer
*tmp_peer
;
2387 struct listnode
*node
, *nnode
;
2390 /* Nothing to do if we've already de-activated this peer */
2391 if (!peer
->afc
[afi
][safi
])
2394 /* This is a peer-group so de-activate all of the members of the
2395 * peer-group as well */
2396 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2397 peer
->afc
[afi
][safi
] = 0;
2398 group
= peer
->group
;
2400 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2401 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2404 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2409 /* If this is the last peer to be deactivated for this
2410 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2411 if (safi
== SAFI_LABELED_UNICAST
2412 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2413 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2415 if (BGP_DEBUG(zebra
, ZEBRA
))
2417 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2419 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2420 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2425 void peer_nsf_stop(struct peer
*peer
)
2430 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2431 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2433 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2434 peer
->nsf
[afi
][safi
] = 0;
2435 THREAD_OFF(peer
->t_llgr_stale
[afi
][safi
]);
2438 if (peer
->t_gr_restart
) {
2439 THREAD_OFF(peer
->t_gr_restart
);
2440 if (bgp_debug_neighbor_events(peer
))
2441 zlog_debug("%pBP graceful restart timer stopped", peer
);
2443 if (peer
->t_gr_stale
) {
2444 THREAD_OFF(peer
->t_gr_stale
);
2445 if (bgp_debug_neighbor_events(peer
))
2447 "%pBP graceful restart stalepath timer stopped",
2450 bgp_clear_route_all(peer
);
2453 /* Delete peer from confguration.
2455 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2456 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2458 * This function /should/ take care to be idempotent, to guard against
2459 * it being called multiple times through stray events that come in
2460 * that happen to result in this function being called again. That
2461 * said, getting here for a "Deleted" peer is a bug in the neighbour
2464 int peer_delete(struct peer
*peer
)
2470 struct bgp_filter
*filter
;
2471 struct listnode
*pn
;
2474 assert(peer
->status
!= Deleted
);
2477 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2479 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2481 bgp_keepalives_off(peer
);
2482 bgp_reads_off(peer
);
2483 bgp_writes_off(peer
);
2484 thread_cancel_event_ready(bm
->master
, peer
);
2485 FOREACH_AFI_SAFI (afi
, safi
)
2486 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
2487 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2488 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2489 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2491 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2492 peer_nsf_stop(peer
);
2494 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2496 /* Remove BFD settings. */
2497 if (peer
->bfd_config
)
2498 bgp_peer_remove_bfd_config(peer
);
2500 /* If this peer belongs to peer group, clear up the
2503 if (peer_dynamic_neighbor(peer
))
2504 peer_drop_dynamic_neighbor(peer
);
2506 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2508 peer
); /* group->peer list reference */
2509 list_delete_node(peer
->group
->peer
, pn
);
2514 /* Withdraw all information from routing table. We can not use
2515 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2516 * executed after peer structure is deleted.
2518 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2520 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2522 if (peer
->doppelganger
) {
2523 peer
->doppelganger
->doppelganger
= NULL
;
2524 peer
->doppelganger
= NULL
;
2527 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2528 bgp_fsm_change_status(peer
, Deleted
);
2530 /* Remove from NHT */
2531 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2532 bgp_unlink_nexthop_by_peer(peer
);
2534 /* Password configuration */
2535 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2536 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2537 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2538 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2539 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2540 bgp_md5_unset(peer
);
2543 bgp_timer_set(peer
); /* stops all timers for Deleted */
2545 /* Delete from all peer list. */
2546 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2547 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2549 * Removing from the list node first because
2550 * peer_unlock *can* call peer_delete( I know,
2551 * I know ). So let's remove it and in
2552 * the su recalculate function we'll ensure
2553 * it's in there or not.
2555 list_delete_node(bgp
->peer
, pn
);
2556 hash_release(bgp
->peerhash
, peer
);
2557 peer_unlock(peer
); /* bgp peer list reference */
2562 stream_fifo_free(peer
->ibuf
);
2567 stream_fifo_free(peer
->obuf
);
2571 if (peer
->ibuf_work
) {
2572 ringbuf_del(peer
->ibuf_work
);
2573 peer
->ibuf_work
= NULL
;
2576 if (peer
->obuf_work
) {
2577 stream_free(peer
->obuf_work
);
2578 peer
->obuf_work
= NULL
;
2581 if (peer
->scratch
) {
2582 stream_free(peer
->scratch
);
2583 peer
->scratch
= NULL
;
2586 /* Local and remote addresses. */
2587 if (peer
->su_local
) {
2588 sockunion_free(peer
->su_local
);
2589 peer
->su_local
= NULL
;
2592 if (peer
->su_remote
) {
2593 sockunion_free(peer
->su_remote
);
2594 peer
->su_remote
= NULL
;
2597 /* Free filter related memory. */
2598 FOREACH_AFI_SAFI (afi
, safi
) {
2599 filter
= &peer
->filter
[afi
][safi
];
2601 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2602 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2603 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2604 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2607 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2608 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2611 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2612 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2613 ecommunity_free(&peer
->soo
[afi
][safi
]);
2616 FOREACH_AFI_SAFI (afi
, safi
)
2617 peer_af_delete(peer
, afi
, safi
);
2619 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2620 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2621 XFREE(MTYPE_BGP_SOFT_VERSION
, peer
->soft_version
);
2623 peer_unlock(peer
); /* initial reference */
2628 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2630 return strcmp(g1
->name
, g2
->name
);
2633 /* Peer group cofiguration. */
2634 static struct peer_group
*peer_group_new(void)
2636 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2639 static void peer_group_free(struct peer_group
*group
)
2641 XFREE(MTYPE_PEER_GROUP
, group
);
2644 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2646 struct peer_group
*group
;
2647 struct listnode
*node
, *nnode
;
2649 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2650 if (strcmp(group
->name
, name
) == 0)
2656 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2658 struct peer_group
*group
;
2662 group
= peer_group_lookup(bgp
, name
);
2666 group
= peer_group_new();
2668 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2669 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2670 group
->peer
= list_new();
2671 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2672 group
->listen_range
[afi
] = list_new();
2673 group
->conf
= peer_new(bgp
);
2674 FOREACH_AFI_SAFI (afi
, safi
) {
2675 if (bgp
->default_af
[afi
][safi
])
2676 group
->conf
->afc
[afi
][safi
] = 1;
2678 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2679 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2680 group
->conf
->group
= group
;
2681 group
->conf
->as
= 0;
2682 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2683 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2684 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2685 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2686 listnode_add_sort(bgp
->group
, group
);
2691 static void peer_group2peer_config_copy(struct peer_group
*group
,
2696 bool config_node
= !!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2702 peer
->as
= conf
->as
;
2705 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2706 peer
->change_local_as
= conf
->change_local_as
;
2708 /* If peer-group has configured TTL then override it */
2709 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2710 peer
->ttl
= conf
->ttl
;
2713 peer
->gtsm_hops
= conf
->gtsm_hops
;
2715 /* peer flags apply */
2716 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2717 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2718 flags_tmp
&= ~peer
->flags_override
;
2720 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2721 SET_FLAG(peer
->flags
, flags_tmp
);
2722 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2725 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2727 /* peer timers apply */
2728 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2729 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2730 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2733 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2734 PEER_ATTR_INHERIT(peer
, group
, connect
);
2735 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2736 peer
->v_connect
= conf
->connect
;
2738 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2741 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2742 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2743 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2744 peer
->v_delayopen
= conf
->delayopen
;
2746 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2749 /* advertisement-interval apply */
2750 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2751 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2752 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2753 peer
->v_routeadv
= conf
->routeadv
;
2755 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2756 ? BGP_DEFAULT_IBGP_ROUTEADV
2757 : BGP_DEFAULT_EBGP_ROUTEADV
;
2760 /* capability extended-nexthop apply */
2761 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2762 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2763 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2765 /* capability software-version apply */
2766 if (!CHECK_FLAG(peer
->flags_override
,
2767 PEER_FLAG_CAPABILITY_SOFT_VERSION
))
2768 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_SOFT_VERSION
))
2769 SET_FLAG(peer
->flags
,
2770 PEER_FLAG_CAPABILITY_SOFT_VERSION
);
2772 /* password apply */
2773 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2774 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2775 MTYPE_PEER_PASSWORD
);
2777 if (!BGP_PEER_SU_UNSPEC(peer
))
2780 /* update-source apply */
2781 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2782 if (conf
->update_source
) {
2783 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2784 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2785 } else if (conf
->update_if
) {
2786 sockunion_free(peer
->update_source
);
2787 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2788 MTYPE_PEER_UPDATE_SOURCE
);
2793 PEER_ATTR_INHERIT(peer
, group
, local_role
);
2795 /* Update GR flags for the peer. */
2796 bgp_peer_gr_flags_update(peer
);
2798 /* Apply BFD settings from group to peer if it exists. */
2799 if (conf
->bfd_config
) {
2800 bgp_peer_configure_bfd(peer
, false);
2801 bgp_peer_config_apply(peer
, group
);
2805 /* Peer group's remote AS configuration. */
2806 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2809 struct peer_group
*group
;
2811 struct listnode
*node
, *nnode
;
2813 group
= peer_group_lookup(bgp
, group_name
);
2817 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2821 /* When we setup peer-group AS number all peer group member's AS
2822 number must be updated to same number. */
2823 peer_as_change(group
->conf
, *as
, as_type
);
2825 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2826 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2827 || (peer
->as_type
!= as_type
))
2828 peer_as_change(peer
, *as
, as_type
);
2834 void peer_notify_unconfig(struct peer
*peer
)
2836 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2837 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2838 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2841 static void peer_notify_shutdown(struct peer
*peer
)
2843 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
2844 if (bgp_debug_neighbor_events(peer
))
2846 "%pBP configured Graceful-Restart, skipping shutdown notification",
2851 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2852 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2853 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
2856 void peer_group_notify_unconfig(struct peer_group
*group
)
2858 struct peer
*peer
, *other
;
2859 struct listnode
*node
, *nnode
;
2861 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2862 other
= peer
->doppelganger
;
2863 if (other
&& other
->status
!= Deleted
) {
2864 other
->group
= NULL
;
2865 peer_notify_unconfig(other
);
2867 peer_notify_unconfig(peer
);
2871 int peer_group_delete(struct peer_group
*group
)
2875 struct prefix
*prefix
;
2877 struct listnode
*node
, *nnode
;
2882 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2883 other
= peer
->doppelganger
;
2885 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2886 bgp_zebra_terminate_radv(bgp
, peer
);
2889 if (other
&& other
->status
!= Deleted
) {
2890 other
->group
= NULL
;
2894 list_delete(&group
->peer
);
2896 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2897 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2899 prefix_free(&prefix
);
2901 list_delete(&group
->listen_range
[afi
]);
2904 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2907 if (group
->conf
->bfd_config
)
2908 bgp_peer_remove_bfd_config(group
->conf
);
2910 group
->conf
->group
= NULL
;
2911 peer_delete(group
->conf
);
2913 /* Delete from all peer_group list. */
2914 listnode_delete(bgp
->group
, group
);
2916 peer_group_free(group
);
2921 int peer_group_remote_as_delete(struct peer_group
*group
)
2923 struct peer
*peer
, *other
;
2924 struct listnode
*node
, *nnode
;
2926 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2927 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2930 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2931 other
= peer
->doppelganger
;
2933 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2934 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2938 if (other
&& other
->status
!= Deleted
) {
2939 other
->group
= NULL
;
2943 list_delete_all_node(group
->peer
);
2945 group
->conf
->as
= 0;
2946 group
->conf
->as_type
= AS_UNSPECIFIED
;
2951 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2953 struct prefix
*prefix
;
2954 struct listnode
*node
, *nnode
;
2957 afi
= family2afi(range
->family
);
2959 /* Group needs remote AS configured. */
2960 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2961 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2963 /* Ensure no duplicates. Currently we don't care about overlaps. */
2964 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2965 if (prefix_same(range
, prefix
))
2969 prefix
= prefix_new();
2970 prefix_copy(prefix
, range
);
2971 listnode_add(group
->listen_range
[afi
], prefix
);
2973 /* Update passwords for new ranges */
2974 if (group
->conf
->password
)
2975 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
2980 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2982 struct prefix
*prefix
, prefix2
;
2983 struct listnode
*node
, *nnode
;
2987 afi
= family2afi(range
->family
);
2989 /* Identify the listen range. */
2990 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2991 if (prefix_same(range
, prefix
))
2996 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
2998 /* Dispose off any dynamic neighbors that exist due to this listen range
3000 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
3001 if (!peer_dynamic_neighbor(peer
))
3004 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
3005 && prefix_match(prefix
, &prefix2
)) {
3006 if (bgp_debug_neighbor_events(peer
))
3008 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
3009 peer
->host
, group
->name
, prefix
);
3014 /* Get rid of the listen range */
3015 listnode_delete(group
->listen_range
[afi
], prefix
);
3017 /* Remove passwords for deleted ranges */
3018 if (group
->conf
->password
)
3019 bgp_md5_unset_prefix(group
->bgp
, prefix
);
3024 /* Bind specified peer to peer group. */
3025 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
3026 struct peer_group
*group
, as_t
*as
)
3028 int first_member
= 0;
3031 enum bgp_peer_sort ptype
, gtype
;
3033 /* Lookup the peer. */
3035 peer
= peer_lookup(bgp
, su
);
3037 /* The peer exist, bind it to the peer-group */
3039 /* When the peer already belongs to a peer-group, check the
3041 if (peer_group_active(peer
)) {
3043 /* The peer is already bound to the peer-group,
3046 if (strcmp(peer
->group
->name
, group
->name
) == 0)
3049 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
3052 /* The peer has not specified a remote-as, inherit it from the
3054 if (peer
->as_type
== AS_UNSPECIFIED
) {
3055 peer
->as_type
= group
->conf
->as_type
;
3056 peer
->as
= group
->conf
->as
;
3057 peer
->sort
= group
->conf
->sort
;
3060 ptype
= peer_sort(peer
);
3061 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
3062 gtype
= peer_sort(group
->conf
);
3063 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
3066 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
3069 if (gtype
== BGP_PEER_INTERNAL
)
3073 peer_group2peer_config_copy(group
, peer
);
3075 FOREACH_AFI_SAFI (afi
, safi
) {
3076 if (group
->conf
->afc
[afi
][safi
]) {
3077 peer
->afc
[afi
][safi
] = 1;
3079 if (peer_af_find(peer
, afi
, safi
)
3080 || peer_af_create(peer
, afi
, safi
)) {
3081 peer_group2peer_config_copy_af(
3082 group
, peer
, afi
, safi
);
3084 } else if (peer
->afc
[afi
][safi
])
3085 peer_deactivate(peer
, afi
, safi
);
3089 assert(group
&& peer
->group
== group
);
3091 listnode_delete(bgp
->peer
, peer
);
3093 peer
->group
= group
;
3094 listnode_add_sort(bgp
->peer
, peer
);
3096 peer
= peer_lock(peer
); /* group->peer list reference */
3097 listnode_add(group
->peer
, peer
);
3101 gtype
= peer_sort(group
->conf
);
3102 /* Advertisement-interval reset */
3103 if (!CHECK_FLAG(group
->conf
->flags
,
3104 PEER_FLAG_ROUTEADV
)) {
3105 group
->conf
->v_routeadv
=
3106 (gtype
== BGP_PEER_IBGP
)
3107 ? BGP_DEFAULT_IBGP_ROUTEADV
3108 : BGP_DEFAULT_EBGP_ROUTEADV
;
3111 /* ebgp-multihop reset */
3112 if (gtype
== BGP_PEER_IBGP
)
3113 group
->conf
->ttl
= MAXTTL
;
3116 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3118 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3119 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3120 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3121 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3123 bgp_session_reset(peer
);
3127 /* Create a new peer. */
3129 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3130 && (!group
->conf
->as
)) {
3131 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3134 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3135 group
->conf
->as_type
, group
, true);
3137 peer
= peer_lock(peer
); /* group->peer list reference */
3138 listnode_add(group
->peer
, peer
);
3140 peer_group2peer_config_copy(group
, peer
);
3142 /* If the peer-group is active for this afi/safi then activate
3144 FOREACH_AFI_SAFI (afi
, safi
) {
3145 if (group
->conf
->afc
[afi
][safi
]) {
3146 peer
->afc
[afi
][safi
] = 1;
3148 if (!peer_af_find(peer
, afi
, safi
))
3149 peer_af_create(peer
, afi
, safi
);
3151 peer_group2peer_config_copy_af(group
, peer
, afi
,
3153 } else if (peer
->afc
[afi
][safi
])
3154 peer_deactivate(peer
, afi
, safi
);
3157 /* Set up peer's events and timers. */
3158 if (peer_active(peer
))
3159 bgp_timer_set(peer
);
3165 static void bgp_startup_timer_expire(struct thread
*thread
)
3169 bgp
= THREAD_ARG(thread
);
3170 bgp
->t_startup
= NULL
;
3174 * On shutdown we call the cleanup function which
3175 * does a free of the link list nodes, free up
3176 * the data we are pointing at too.
3178 static void bgp_vrf_string_name_delete(void *data
)
3182 XFREE(MTYPE_TMP
, vname
);
3185 /* BGP instance creation by `router bgp' commands. */
3186 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3187 enum bgp_instance_type inst_type
)
3193 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3195 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3196 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3197 zlog_debug("Creating Default VRF, AS %u", *as
);
3199 zlog_debug("Creating %s %s, AS %u",
3200 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3206 /* Default the EVPN VRF to the default one */
3207 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3214 bgp
->allow_martian
= false;
3215 bgp_process_queue_init(bgp
);
3216 bgp
->heuristic_coalesce
= true;
3217 bgp
->inst_type
= inst_type
;
3218 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3220 bgp
->peer_self
= peer_new(bgp
);
3221 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3222 bgp
->peer_self
->host
=
3223 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3224 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3225 if (cmd_hostname_get())
3226 bgp
->peer_self
->hostname
=
3227 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3229 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3230 if (cmd_domainname_get())
3231 bgp
->peer_self
->domainname
=
3232 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3233 bgp
->peer
= list_new();
3234 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3235 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3237 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3239 bgp
->group
= list_new();
3240 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3242 FOREACH_AFI_SAFI (afi
, safi
) {
3243 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3244 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3245 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3247 /* Enable maximum-paths */
3248 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3250 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3252 /* Initialize graceful restart info */
3253 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3254 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3255 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3256 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3257 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3260 bgp
->v_update_delay
= bm
->v_update_delay
;
3261 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3262 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3263 bgp
->default_subgroup_pkt_queue_max
=
3264 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3265 bgp_tcp_keepalive_unset(bgp
);
3266 bgp_timers_unset(bgp
);
3267 bgp
->default_min_holdtime
= 0;
3268 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3269 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3270 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3271 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3272 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3273 bgp
->dynamic_neighbors_count
= 0;
3274 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3275 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3276 bgp
->reject_as_sets
= false;
3277 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3278 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3279 bgp
->fast_convergence
= false;
3281 bgp
->llgr_stale_time
= BGP_DEFAULT_LLGR_STALE_TIME
;
3283 #ifdef ENABLE_BGP_VNC
3284 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3285 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3287 assert(bgp
->rfapi_cfg
);
3289 #endif /* ENABLE_BGP_VNC */
3291 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3292 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3293 bgp
->vpn_policy
[afi
].afi
= afi
;
3294 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3295 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3298 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3299 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3300 bgp_vrf_string_name_delete
;
3301 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3302 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3303 bgp_vrf_string_name_delete
;
3304 SET_FLAG(bgp
->af_flags
[afi
][SAFI_MPLS_VPN
],
3305 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL
);
3308 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3310 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3311 bgp
->restart_time
, &bgp
->t_startup
);
3313 /* printable name we can use in debug messages */
3314 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3315 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3325 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3327 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3328 snprintf(bgp
->name_pretty
, len
, "%s %s",
3329 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3335 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3336 memory_order_relaxed
);
3337 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3338 memory_order_relaxed
);
3339 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3340 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3344 update_bgp_group_init(bgp
);
3346 /* assign a unique rd id for auto derivation of vrf's RD */
3347 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3349 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3350 sizeof(struct bgp_evpn_info
));
3352 bgp_evpn_vrf_es_init(bgp
);
3356 /*initilize global GR FSM */
3357 bgp_global_gr_init(bgp
);
3359 memset(&bgp
->ebgprequirespolicywarning
, 0,
3360 sizeof(bgp
->ebgprequirespolicywarning
));
3365 /* Return the "default VRF" instance of BGP. */
3366 struct bgp
*bgp_get_default(void)
3369 struct listnode
*node
, *nnode
;
3371 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3372 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3377 /* Lookup BGP entry. */
3378 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3381 struct listnode
*node
, *nnode
;
3383 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3385 && ((bgp
->name
== NULL
&& name
== NULL
)
3386 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3391 /* Lookup BGP structure by view name. */
3392 struct bgp
*bgp_lookup_by_name(const char *name
)
3395 struct listnode
*node
, *nnode
;
3397 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3398 if ((bgp
->name
== NULL
&& name
== NULL
)
3399 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3404 /* Lookup BGP instance based on VRF id. */
3405 /* Note: Only to be used for incoming messages from Zebra. */
3406 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3410 /* Lookup VRF (in tree) and follow link. */
3411 vrf
= vrf_lookup_by_id(vrf_id
);
3414 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3417 /* Sets the BGP instance where EVPN is enabled */
3418 void bgp_set_evpn(struct bgp
*bgp
)
3420 if (bm
->bgp_evpn
== bgp
)
3423 /* First, release the reference count we hold on the instance */
3425 bgp_unlock(bm
->bgp_evpn
);
3429 /* Increase the reference count on this new VRF */
3431 bgp_lock(bm
->bgp_evpn
);
3434 /* Returns the BGP instance where EVPN is enabled, if any */
3435 struct bgp
*bgp_get_evpn(void)
3437 return bm
->bgp_evpn
;
3440 /* handle socket creation or deletion, if necessary
3441 * this is called for all new BGP instances
3443 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3446 struct listnode
*node
;
3449 /* Create BGP server socket, if listen mode not disabled */
3450 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3452 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3454 * suppress vrf socket
3457 bgp_close_vrf_socket(bgp
);
3461 return BGP_ERR_INVALID_VALUE
;
3463 * if vrf_id did not change
3465 if (vrf
->vrf_id
== old_vrf_id
)
3467 if (old_vrf_id
!= VRF_UNKNOWN
) {
3468 /* look for old socket. close it. */
3469 bgp_close_vrf_socket(bgp
);
3471 /* if backend is not yet identified ( VRF_UNKNOWN) then
3472 * creation will be done later
3474 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3476 if (list_isempty(bm
->addresses
)) {
3477 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3478 return BGP_ERR_INVALID_VALUE
;
3480 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3481 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3482 return BGP_ERR_INVALID_VALUE
;
3486 return bgp_check_main_socket(create
, bgp
);
3489 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3490 enum bgp_instance_type inst_type
)
3494 /* Multiple instance check. */
3496 bgp
= bgp_lookup_by_name(name
);
3498 bgp
= bgp_get_default();
3502 if (bgp
->as
!= *as
) {
3504 return BGP_ERR_AS_MISMATCH
;
3506 if (bgp
->inst_type
!= inst_type
)
3507 return BGP_ERR_INSTANCE_MISMATCH
;
3515 /* Called from VTY commands. */
3516 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3517 enum bgp_instance_type inst_type
)
3520 struct vrf
*vrf
= NULL
;
3523 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3524 if (ret
|| *bgp_val
)
3527 bgp
= bgp_create(as
, name
, inst_type
);
3530 * view instances will never work inside of a vrf
3531 * as such they must always be in the VRF_DEFAULT
3532 * Also we must set this to something useful because
3533 * of the vrf socket code needing an actual useful
3534 * default value to send to the underlying OS.
3536 * This code is currently ignoring vrf based
3537 * code using the -Z option( and that is probably
3538 * best addressed elsewhere in the code )
3540 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3541 bgp
->vrf_id
= VRF_DEFAULT
;
3543 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3544 bgp_address_init(bgp
);
3545 bgp_tip_hash_init(bgp
);
3549 bgp
->t_rmap_def_originate_eval
= NULL
;
3551 /* If Default instance or VRF, link to the VRF structure, if present. */
3552 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3553 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3554 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3556 bgp_vrf_link(bgp
, vrf
);
3558 /* BGP server socket already processed if BGP instance
3559 * already part of the list
3561 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3562 listnode_add(bm
->bgp
, bgp
);
3564 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3565 if (BGP_DEBUG(zebra
, ZEBRA
))
3566 zlog_debug("%s: Registering BGP instance %s to zebra",
3567 __func__
, bgp
->name_pretty
);
3568 bgp_zebra_instance_register(bgp
);
3574 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3575 vrf_id_t vrf_id
, bool set
)
3579 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3582 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3586 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3588 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3592 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3596 struct list
*red_list
;
3597 struct listnode
*node
;
3598 struct bgp_redist
*red
;
3600 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3601 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3603 red_list
= bgp
->redist
[afi
][i
];
3607 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3608 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3615 * Make BGP instance "up". Applies only to VRFs (non-default) and
3616 * implies the VRF has been learnt from Zebra.
3618 void bgp_instance_up(struct bgp
*bgp
)
3621 struct listnode
*node
, *next
;
3623 bgp_set_redist_vrf_bitmaps(bgp
, true);
3625 /* Register with zebra. */
3626 bgp_zebra_instance_register(bgp
);
3628 /* Kick off any peers that may have been configured. */
3629 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3630 if (!BGP_PEER_START_SUPPRESSED(peer
))
3631 BGP_EVENT_ADD(peer
, BGP_Start
);
3634 /* Process any networks that have been configured. */
3635 bgp_static_add(bgp
);
3639 * Make BGP instance "down". Applies only to VRFs (non-default) and
3640 * implies the VRF has been deleted by Zebra.
3642 void bgp_instance_down(struct bgp
*bgp
)
3645 struct listnode
*node
;
3646 struct listnode
*next
;
3649 if (bgp
->t_rmap_def_originate_eval
) {
3650 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3651 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3655 /* Bring down peers, so corresponding routes are purged. */
3656 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3657 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3658 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3659 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3661 bgp_session_reset(peer
);
3664 /* Purge network and redistributed routes. */
3665 bgp_purge_static_redist_routes(bgp
);
3667 /* Cleanup registered nexthops (flags) */
3668 bgp_cleanup_nexthops(bgp
);
3670 bgp_zebra_instance_deregister(bgp
);
3672 bgp_set_redist_vrf_bitmaps(bgp
, false);
3675 /* Delete BGP instance. */
3676 int bgp_delete(struct bgp
*bgp
)
3679 struct peer_group
*group
;
3680 struct listnode
*node
, *next
;
3685 struct graceful_restart_info
*gr_info
;
3689 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3691 /* make sure we withdraw any exported routes */
3692 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3694 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3697 bgp_vpn_leak_unimport(bgp
);
3699 hook_call(bgp_inst_delete
, bgp
);
3701 FOREACH_AFI_SAFI (afi
, safi
)
3702 THREAD_OFF(bgp
->t_revalidate
[afi
][safi
]);
3704 THREAD_OFF(bgp
->t_condition_check
);
3705 THREAD_OFF(bgp
->t_startup
);
3706 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3707 THREAD_OFF(bgp
->t_update_delay
);
3708 THREAD_OFF(bgp
->t_establish_wait
);
3710 /* Set flag indicating bgp instance delete in progress */
3711 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3713 /* Delete the graceful restart info */
3714 FOREACH_AFI_SAFI (afi
, safi
) {
3717 gr_info
= &bgp
->gr_info
[afi
][safi
];
3720 t
= gr_info
->t_select_deferral
;
3722 void *info
= THREAD_ARG(t
);
3724 XFREE(MTYPE_TMP
, info
);
3726 THREAD_OFF(gr_info
->t_select_deferral
);
3728 t
= gr_info
->t_route_select
;
3730 void *info
= THREAD_ARG(t
);
3732 XFREE(MTYPE_TMP
, info
);
3734 THREAD_OFF(gr_info
->t_route_select
);
3737 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3738 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3739 zlog_debug("Deleting Default VRF");
3741 zlog_debug("Deleting %s %s",
3742 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3748 /* unmap from RT list */
3749 bgp_evpn_vrf_delete(bgp
);
3751 /* unmap bgp vrf label */
3752 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3753 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3756 if (bgp
->t_rmap_def_originate_eval
) {
3757 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3758 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3762 /* Inform peers we're going down. */
3763 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3764 peer_notify_shutdown(peer
);
3766 /* Delete static routes (networks). */
3767 bgp_static_delete(bgp
);
3769 /* Unset redistribution. */
3770 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3771 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3772 if (i
!= ZEBRA_ROUTE_BGP
)
3773 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3775 /* Free peers and peer-groups. */
3776 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3777 peer_group_delete(group
);
3779 while (listcount(bgp
->peer
)) {
3780 peer
= listnode_head(bgp
->peer
);
3784 if (bgp
->peer_self
) {
3785 peer_delete(bgp
->peer_self
);
3786 bgp
->peer_self
= NULL
;
3789 update_bgp_group_free(bgp
);
3791 /* TODO - Other memory may need to be freed - e.g., NHT */
3793 #ifdef ENABLE_BGP_VNC
3796 bgp_cleanup_routes(bgp
);
3798 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3799 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3802 &bgp
->vpn_policy
[afi
]
3803 .import_redirect_rtlist
);
3804 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3807 /* Free any memory allocated to holding routemap references */
3808 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3809 for (enum vpn_policy_direction dir
= 0;
3810 dir
< BGP_VPN_POLICY_DIR_MAX
; ++dir
) {
3811 if (bgp
->vpn_policy
[afi
].rmap_name
[dir
])
3812 XFREE(MTYPE_ROUTE_MAP_NAME
,
3813 bgp
->vpn_policy
[afi
].rmap_name
[dir
]);
3814 bgp
->vpn_policy
[afi
].rmap
[dir
] = NULL
;
3818 /* Deregister from Zebra, if needed */
3819 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3820 if (BGP_DEBUG(zebra
, ZEBRA
))
3822 "%s: deregistering this bgp %s instance from zebra",
3823 __func__
, bgp
->name
);
3824 bgp_zebra_instance_deregister(bgp
);
3827 /* Remove visibility via the master list - there may however still be
3828 * routes to be processed still referencing the struct bgp.
3830 listnode_delete(bm
->bgp
, bgp
);
3832 /* Free interfaces in this instance. */
3835 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3836 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3838 bgp_vrf_unlink(bgp
, vrf
);
3840 /* Update EVPN VRF pointer */
3841 if (bm
->bgp_evpn
== bgp
) {
3842 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3845 bgp_set_evpn(bgp_get_default());
3848 if (bgp
->process_queue
)
3849 work_queue_free_and_null(&bgp
->process_queue
);
3851 thread_master_free_unused(bm
->master
);
3852 bgp_unlock(bgp
); /* initial reference */
3857 void bgp_free(struct bgp
*bgp
)
3861 struct bgp_table
*table
;
3862 struct bgp_dest
*dest
;
3863 struct bgp_rmap
*rmap
;
3867 list_delete(&bgp
->group
);
3868 list_delete(&bgp
->peer
);
3870 if (bgp
->peerhash
) {
3871 hash_free(bgp
->peerhash
);
3872 bgp
->peerhash
= NULL
;
3875 FOREACH_AFI_SAFI (afi
, safi
) {
3876 /* Special handling for 2-level routing tables. */
3877 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3878 || safi
== SAFI_EVPN
) {
3879 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3880 dest
= bgp_route_next(dest
)) {
3881 table
= bgp_dest_get_bgp_table_info(dest
);
3882 bgp_table_finish(&table
);
3885 if (bgp
->route
[afi
][safi
])
3886 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3887 if (bgp
->aggregate
[afi
][safi
])
3888 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3889 if (bgp
->rib
[afi
][safi
])
3890 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3891 rmap
= &bgp
->table_map
[afi
][safi
];
3892 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3895 bgp_scan_finish(bgp
);
3896 bgp_address_destroy(bgp
);
3897 bgp_tip_hash_destroy(bgp
);
3899 /* release the auto RD id */
3900 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3902 bgp_evpn_cleanup(bgp
);
3903 bgp_pbr_cleanup(bgp
);
3904 bgp_srv6_cleanup(bgp
);
3905 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3907 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3908 enum vpn_policy_direction dir
;
3910 if (bgp
->vpn_policy
[afi
].import_vrf
)
3911 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3912 if (bgp
->vpn_policy
[afi
].export_vrf
)
3913 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3915 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3916 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3917 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3918 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3919 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3920 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3923 XFREE(MTYPE_BGP
, bgp
->name
);
3924 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3925 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3927 XFREE(MTYPE_BGP
, bgp
);
3930 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3933 struct listnode
*node
, *nnode
;
3939 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3940 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3941 && !CHECK_FLAG(peer
->sflags
,
3942 PEER_STATUS_ACCEPT_PEER
))
3944 } else if (bm
->bgp
!= NULL
) {
3945 struct listnode
*bgpnode
, *nbgpnode
;
3947 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3948 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3950 && !strcmp(peer
->conf_if
, conf_if
)
3951 && !CHECK_FLAG(peer
->sflags
,
3952 PEER_STATUS_ACCEPT_PEER
))
3958 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3961 struct listnode
*node
, *nnode
;
3967 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3968 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3969 && !CHECK_FLAG(peer
->sflags
,
3970 PEER_STATUS_ACCEPT_PEER
))
3972 } else if (bm
->bgp
!= NULL
) {
3973 struct listnode
*bgpnode
, *nbgpnode
;
3975 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3976 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3978 && !strcmp(peer
->hostname
, hostname
)
3979 && !CHECK_FLAG(peer
->sflags
,
3980 PEER_STATUS_ACCEPT_PEER
))
3986 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
3988 struct peer
*peer
= NULL
;
3989 struct peer tmp_peer
;
3991 memset(&tmp_peer
, 0, sizeof(struct peer
));
3994 * We do not want to find the doppelganger peer so search for the peer
3996 * the hash that has PEER_FLAG_CONFIG_NODE
3998 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
4003 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4004 } else if (bm
->bgp
!= NULL
) {
4005 struct listnode
*bgpnode
, *nbgpnode
;
4007 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
4008 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4017 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
4018 union sockunion
*su
,
4019 struct peer_group
*group
)
4025 /* Create peer first; we've already checked group config is valid. */
4026 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
4027 group
->conf
->as_type
, group
, true);
4032 peer
= peer_lock(peer
);
4033 listnode_add(group
->peer
, peer
);
4035 peer_group2peer_config_copy(group
, peer
);
4038 * Bind peer for all AFs configured for the group. We don't call
4039 * peer_group_bind as that is sub-optimal and does some stuff we don't
4042 FOREACH_AFI_SAFI (afi
, safi
) {
4043 if (!group
->conf
->afc
[afi
][safi
])
4045 peer
->afc
[afi
][safi
] = 1;
4047 if (!peer_af_find(peer
, afi
, safi
))
4048 peer_af_create(peer
, afi
, safi
);
4050 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
4053 /* Mark as dynamic, but also as a "config node" for other things to
4055 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
4061 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
4062 struct prefix
*prefix
)
4064 struct listnode
*node
, *nnode
;
4065 struct prefix
*range
;
4068 afi
= family2afi(prefix
->family
);
4070 if (group
->listen_range
[afi
])
4071 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
4073 if (prefix_match(range
, prefix
))
4080 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
4081 struct prefix
**listen_range
)
4083 struct prefix
*range
= NULL
;
4084 struct peer_group
*group
= NULL
;
4085 struct listnode
*node
, *nnode
;
4087 *listen_range
= NULL
;
4089 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4090 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4093 } else if (bm
->bgp
!= NULL
) {
4094 struct listnode
*bgpnode
, *nbgpnode
;
4096 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4097 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4098 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4104 *listen_range
= range
;
4105 return (group
&& range
) ? group
: NULL
;
4108 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
4110 struct peer_group
*group
;
4113 struct prefix prefix
;
4114 struct prefix
*listen_range
;
4117 if (!sockunion2hostprefix(su
, &prefix
))
4120 /* See if incoming connection matches a configured listen range. */
4121 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
4132 if (bgp_debug_neighbor_events(NULL
))
4134 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4135 &prefix
, group
->name
, listen_range
);
4137 /* Are we within the listen limit? */
4138 dncount
= gbgp
->dynamic_neighbors_count
;
4140 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4141 if (bgp_debug_neighbor_events(NULL
))
4143 "Dynamic Neighbor %pFX rejected - at limit %d",
4144 &prefix
, gbgp
->dynamic_neighbors_limit
);
4148 /* Ensure group is not disabled. */
4149 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4150 if (bgp_debug_neighbor_events(NULL
))
4152 "Dynamic Neighbor %pFX rejected - group %s disabled",
4153 &prefix
, group
->name
);
4157 /* Check that at least one AF is activated for the group. */
4158 if (!peer_group_af_configured(group
)) {
4159 if (bgp_debug_neighbor_events(NULL
))
4161 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4162 &prefix
, group
->name
);
4166 /* Create dynamic peer and bind to associated group. */
4167 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4170 gbgp
->dynamic_neighbors_count
= ++dncount
;
4172 if (bgp_debug_neighbor_events(peer
))
4173 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4174 peer
->host
, group
->name
, dncount
);
4179 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4182 if (peer
->group
->bgp
) {
4183 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4185 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4187 if (bgp_debug_neighbor_events(peer
))
4188 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4189 peer
->group
->name
, dncount
);
4192 bool bgp_path_attribute_discard(struct peer
*peer
, char *buf
, size_t size
)
4199 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4200 if (peer
->discard_attrs
[i
])
4201 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4202 (strlen(buf
) > 0) ? " " : "", i
);
4205 if (strlen(buf
) > 0)
4211 bool bgp_path_attribute_treat_as_withdraw(struct peer
*peer
, char *buf
,
4219 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4220 if (peer
->withdraw_attrs
[i
])
4221 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4222 (strlen(buf
) > 0) ? " " : "", i
);
4225 if (strlen(buf
) > 0)
4231 /* If peer is configured at least one address family return 1. */
4232 bool peer_active(struct peer
*peer
)
4234 if (BGP_PEER_SU_UNSPEC(peer
))
4236 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4237 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4238 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4239 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4240 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4241 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4242 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4243 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4244 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4245 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4246 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4251 /* If peer is negotiated at least one address family return 1. */
4252 bool peer_active_nego(struct peer
*peer
)
4254 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4255 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4256 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4257 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4258 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4259 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4260 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4261 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4262 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4263 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4264 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4265 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4266 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4271 /* If peer received at least one address family MP, return true */
4272 bool peer_afc_received(struct peer
*peer
)
4277 FOREACH_AFI_SAFI (afi
, safi
)
4278 if (peer
->afc_recv
[afi
][safi
])
4284 /* If peer advertised at least one address family MP, return true */
4285 bool peer_afc_advertised(struct peer
*peer
)
4290 FOREACH_AFI_SAFI (afi
, safi
)
4291 if (peer
->afc_adv
[afi
][safi
])
4297 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4298 enum peer_change_type type
)
4300 struct peer_af
*paf
;
4302 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4305 if (!peer_established(peer
))
4308 if (type
== peer_change_reset
) {
4309 /* If we're resetting session, we've to delete both peer struct
4311 if ((peer
->doppelganger
)
4312 && (peer
->doppelganger
->status
!= Deleted
)
4313 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4314 PEER_FLAG_CONFIG_NODE
)))
4315 peer_delete(peer
->doppelganger
);
4317 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4318 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4319 } else if (type
== peer_change_reset_in
) {
4320 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4321 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4322 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4323 BGP_ROUTE_REFRESH_NORMAL
);
4325 if ((peer
->doppelganger
)
4326 && (peer
->doppelganger
->status
!= Deleted
)
4327 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4328 PEER_FLAG_CONFIG_NODE
)))
4329 peer_delete(peer
->doppelganger
);
4331 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4332 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4334 } else if (type
== peer_change_reset_out
) {
4335 paf
= peer_af_find(peer
, afi
, safi
);
4336 if (paf
&& paf
->subgroup
)
4337 SET_FLAG(paf
->subgroup
->sflags
,
4338 SUBGRP_STATUS_FORCE_UPDATES
);
4340 update_group_adjust_peer(paf
);
4341 bgp_announce_route(peer
, afi
, safi
, false);
4345 struct peer_flag_action
{
4349 /* This flag can be set for peer-group member. */
4350 uint8_t not_for_member
;
4352 /* Action when the flag is changed. */
4353 enum peer_change_type type
;
4356 static const struct peer_flag_action peer_flag_action_list
[] = {
4357 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4358 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4359 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4360 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4361 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4362 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4363 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4364 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4365 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4366 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4367 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4368 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4369 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4370 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4371 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4372 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4373 {PEER_FLAG_LOCAL_AS
, 0, peer_change_reset
},
4374 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_reset
},
4375 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_reset
},
4376 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4377 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4378 {PEER_FLAG_EXTENDED_OPT_PARAMS
, 0, peer_change_reset
},
4379 {PEER_FLAG_ROLE_STRICT_MODE
, 0, peer_change_reset
},
4380 {PEER_FLAG_ROLE
, 0, peer_change_reset
},
4381 {PEER_FLAG_PORT
, 0, peer_change_reset
},
4382 {PEER_FLAG_AIGP
, 0, peer_change_none
},
4383 {PEER_FLAG_GRACEFUL_SHUTDOWN
, 0, peer_change_none
},
4384 {PEER_FLAG_CAPABILITY_SOFT_VERSION
, 0, peer_change_reset
},
4387 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4388 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4389 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4390 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4391 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4392 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4393 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4394 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4395 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4396 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4397 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4398 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4399 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4400 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4401 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4402 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4403 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4404 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4405 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4406 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4407 {PEER_FLAG_MAX_PREFIX_OUT
, 0, peer_change_none
},
4408 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4409 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4410 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4411 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4412 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4413 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4414 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4415 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4416 {PEER_FLAG_SOO
, 0, peer_change_reset
},
4417 {PEER_FLAG_ACCEPT_OWN
, 0, peer_change_reset
},
4420 /* Proper action set. */
4421 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4422 int size
, struct peer_flag_action
*action
,
4429 const struct peer_flag_action
*match
= NULL
;
4431 /* Check peer's frag action. */
4432 for (i
= 0; i
< size
; i
++) {
4433 match
= &action_list
[i
];
4435 if (match
->flag
== 0)
4438 if (match
->flag
& flag
) {
4441 if (match
->type
== peer_change_reset_in
)
4443 if (match
->type
== peer_change_reset_out
)
4445 if (match
->type
== peer_change_reset
) {
4449 if (match
->not_for_member
)
4450 action
->not_for_member
= 1;
4454 /* Set peer clear type. */
4455 if (reset_in
&& reset_out
)
4456 action
->type
= peer_change_reset
;
4458 action
->type
= peer_change_reset_in
;
4460 action
->type
= peer_change_reset_out
;
4462 action
->type
= peer_change_none
;
4467 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4469 if (flag
== PEER_FLAG_SHUTDOWN
) {
4470 if (CHECK_FLAG(peer
->flags
, flag
)) {
4471 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4472 peer_nsf_stop(peer
);
4474 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4476 if (peer
->t_pmax_restart
) {
4477 THREAD_OFF(peer
->t_pmax_restart
);
4478 if (bgp_debug_neighbor_events(peer
))
4480 "%pBP Maximum-prefix restart timer canceled",
4484 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4485 char *msg
= peer
->tx_shutdown_message
;
4487 uint8_t msgbuf
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4489 if (!msg
&& peer_group_active(peer
))
4490 msg
= peer
->group
->conf
4491 ->tx_shutdown_message
;
4492 msglen
= msg
? strlen(msg
) : 0;
4493 if (msglen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4494 msglen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4498 memcpy(msgbuf
+ 1, msg
, msglen
);
4500 bgp_notify_send_with_data(
4501 peer
, BGP_NOTIFY_CEASE
,
4502 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4503 msgbuf
, msglen
+ 1);
4506 peer
, BGP_NOTIFY_CEASE
,
4507 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4509 bgp_session_reset(peer
);
4511 peer
->v_start
= BGP_INIT_START_TIMER
;
4512 BGP_EVENT_ADD(peer
, BGP_Stop
);
4514 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4515 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4516 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4517 else if (flag
== PEER_FLAG_PASSIVE
)
4518 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4519 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4520 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4522 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4523 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4525 bgp_session_reset(peer
);
4528 /* Enable global administrative shutdown of all peers of BGP instance */
4529 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4532 struct listnode
*node
;
4533 /* length(1) + message(N) */
4534 uint8_t data
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4536 /* do nothing if already shut down */
4537 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4540 /* informational log message */
4541 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4544 /* iterate through peers of BGP instance */
4545 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4546 /* continue, if peer is already in administrative shutdown. */
4547 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4550 /* send a RFC 4486 notification message if necessary */
4551 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4553 size_t datalen
= strlen(msg
);
4555 if (datalen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4556 datalen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4559 memcpy(data
+ 1, msg
, datalen
);
4561 bgp_notify_send_with_data(
4562 peer
, BGP_NOTIFY_CEASE
,
4563 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
, data
,
4567 peer
, BGP_NOTIFY_CEASE
,
4568 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4572 /* reset start timer to initial value */
4573 peer
->v_start
= BGP_INIT_START_TIMER
;
4575 /* trigger a RFC 4271 ManualStop event */
4576 BGP_EVENT_ADD(peer
, BGP_Stop
);
4579 /* set the BGP instances shutdown flag */
4580 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4583 /* Disable global administrative shutdown of all peers of BGP instance */
4584 void bgp_shutdown_disable(struct bgp
*bgp
)
4586 /* do nothing if not shut down. */
4587 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4590 /* informational log message */
4591 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4594 /* clear the BGP instances shutdown flag */
4595 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4598 /* Change specified peer flag. */
4599 static int peer_flag_modify(struct peer
*peer
, uint64_t flag
, int set
)
4603 bool invert
, member_invert
;
4604 struct peer
*member
;
4605 struct listnode
*node
, *nnode
;
4606 struct peer_flag_action action
;
4608 memset(&action
, 0, sizeof(struct peer_flag_action
));
4609 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4611 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4612 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4615 /* Abort if no flag action exists. */
4617 return BGP_ERR_INVALID_FLAG
;
4619 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4620 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4621 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4622 return BGP_ERR_PEER_FLAG_CONFLICT
;
4624 /* Handle flag updates where desired state matches current state. */
4625 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4626 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4627 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4631 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4632 COND_FLAG(peer
->flags_override
, flag
, invert
);
4637 /* Inherit from peer-group or set/unset flags accordingly. */
4638 if (peer_group_active(peer
) && set
== invert
)
4639 peer_flag_inherit(peer
, flag
);
4641 COND_FLAG(peer
->flags
, flag
, set
);
4643 /* Check if handling a regular peer. */
4644 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4645 /* Update flag override state accordingly. */
4646 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4649 * For the extended next-hop encoding flag we need to turn RAs
4650 * on if flag is being set, but only turn RAs off if the flag
4651 * is being unset on this peer and if this peer is a member of a
4652 * peer-group, the peer-group also doesn't have the flag set.
4654 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4656 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4657 } else if (peer_group_active(peer
)) {
4658 if (!CHECK_FLAG(peer
->group
->conf
->flags
,
4661 bgp_zebra_terminate_radv(peer
->bgp
,
4664 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4667 /* Execute flag action on peer. */
4668 if (action
.type
== peer_change_reset
)
4669 peer_flag_modify_action(peer
, flag
);
4671 /* Skip peer-group mechanics for regular peers. */
4676 * Update peer-group members, unless they are explicitly overriding
4677 * peer-group configuration.
4679 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4680 /* Skip peers with overridden configuration. */
4681 if (CHECK_FLAG(member
->flags_override
, flag
))
4684 /* Check if only member without group is inverted. */
4686 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4688 /* Skip peers with equivalent configuration. */
4689 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4692 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4695 /* Update flag on peer-group member. */
4696 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4698 if (flag
== PEER_FLAG_CAPABILITY_ENHE
&& !member
->conf_if
)
4699 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4700 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4702 /* Execute flag action on peer-group member. */
4703 if (action
.type
== peer_change_reset
)
4704 peer_flag_modify_action(member
, flag
);
4710 int peer_flag_set(struct peer
*peer
, uint64_t flag
)
4712 return peer_flag_modify(peer
, flag
, 1);
4715 int peer_flag_unset(struct peer
*peer
, uint64_t flag
)
4717 return peer_flag_modify(peer
, flag
, 0);
4720 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4721 uint64_t flag
, bool set
)
4725 bool invert
, member_invert
;
4726 struct peer
*member
;
4727 struct listnode
*node
, *nnode
;
4728 struct peer_flag_action action
;
4729 enum bgp_peer_sort ptype
;
4731 memset(&action
, 0, sizeof(struct peer_flag_action
));
4732 size
= sizeof(peer_af_flag_action_list
)
4733 / sizeof(struct peer_flag_action
);
4735 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4736 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4739 /* Abort if flag action exists. */
4741 return BGP_ERR_INVALID_FLAG
;
4743 ptype
= peer_sort(peer
);
4744 /* Special check for reflector client. */
4745 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4746 return BGP_ERR_NOT_INTERNAL_PEER
;
4748 /* Special check for remove-private-AS. */
4749 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4750 return BGP_ERR_REMOVE_PRIVATE_AS
;
4752 /* as-override is not allowed for IBGP peers */
4753 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4754 return BGP_ERR_AS_OVERRIDE
;
4756 /* Handle flag updates where desired state matches current state. */
4757 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4758 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4759 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4764 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4765 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4772 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4773 * if we are setting/unsetting flags which conflict with this flag
4774 * handle accordingly
4776 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4780 * if we are setting NEXTHOP_SELF, we need to unset the
4781 * NEXTHOP_UNCHANGED flag
4783 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4784 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4785 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4786 PEER_FLAG_NEXTHOP_UNCHANGED
);
4790 * if we are unsetting NEXTHOP_SELF, we need to set the
4791 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4793 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4794 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4795 SET_FLAG(peer
->af_flags
[afi
][safi
],
4796 PEER_FLAG_NEXTHOP_UNCHANGED
);
4801 * If the peer is a route server client let's not
4802 * muck with the nexthop on the way out the door
4804 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4806 SET_FLAG(peer
->af_flags
[afi
][safi
],
4807 PEER_FLAG_NEXTHOP_UNCHANGED
);
4809 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4810 PEER_FLAG_NEXTHOP_UNCHANGED
);
4813 /* Inherit from peer-group or set/unset flags accordingly. */
4814 if (peer_group_active(peer
) && set
== invert
)
4815 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4817 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4819 /* Execute action when peer is established. */
4820 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4821 && peer_established(peer
)) {
4822 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4823 bgp_clear_adj_in(peer
, afi
, safi
);
4825 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4826 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4827 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4828 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4829 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4830 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4831 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4832 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4834 peer_change_action(peer
, afi
, safi
, action
.type
);
4838 /* Check if handling a regular peer. */
4839 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4840 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4844 * Update peer-group members, unless they are explicitly
4845 * overriding peer-group configuration.
4847 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4849 /* Skip peers with overridden configuration. */
4850 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4854 /* Check if only member without group is inverted. */
4856 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4860 /* Skip peers with equivalent configuration. */
4861 if (set
!= member_invert
4862 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4865 if (set
== member_invert
4866 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4869 /* Update flag on peer-group member. */
4870 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4871 set
!= member_invert
);
4873 /* Execute flag action on peer-group member. */
4874 if (peer_established(member
)) {
4875 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4876 bgp_clear_adj_in(member
, afi
, safi
);
4878 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4879 member
->last_reset
=
4880 PEER_DOWN_RR_CLIENT_CHANGE
;
4882 == PEER_FLAG_RSERVER_CLIENT
)
4883 member
->last_reset
=
4884 PEER_DOWN_RS_CLIENT_CHANGE
;
4886 == PEER_FLAG_ORF_PREFIX_SM
)
4887 member
->last_reset
=
4888 PEER_DOWN_CAPABILITY_CHANGE
;
4890 == PEER_FLAG_ORF_PREFIX_RM
)
4891 member
->last_reset
=
4892 PEER_DOWN_CAPABILITY_CHANGE
;
4894 peer_change_action(member
, afi
, safi
,
4904 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4906 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4909 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4911 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4915 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4917 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4918 peer
->tx_shutdown_message
=
4919 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4922 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4924 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4928 /* EBGP multihop configuration. */
4929 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4931 struct peer_group
*group
;
4932 struct listnode
*node
, *nnode
;
4935 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4938 /* is there anything to do? */
4939 if (peer
->ttl
== ttl
)
4942 /* see comment in peer_ttl_security_hops_set() */
4943 if (ttl
!= MAXTTL
) {
4944 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4945 group
= peer
->group
;
4946 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4947 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4949 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4951 if (peer1
->sort
== BGP_PEER_IBGP
)
4954 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4955 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4958 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4959 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4965 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4966 if (peer
->sort
!= BGP_PEER_IBGP
) {
4967 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4968 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4969 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4971 bgp_session_reset(peer
);
4973 /* Reconfigure BFD peer with new TTL. */
4974 if (peer
->bfd_config
)
4975 bgp_peer_bfd_update_source(peer
);
4978 group
= peer
->group
;
4979 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4980 if (peer
->sort
== BGP_PEER_IBGP
)
4983 peer
->ttl
= group
->conf
->ttl
;
4985 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4986 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4987 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4989 bgp_session_reset(peer
);
4991 /* Reconfigure BFD peer with new TTL. */
4992 if (peer
->bfd_config
)
4993 bgp_peer_bfd_update_source(peer
);
4999 int peer_ebgp_multihop_unset(struct peer
*peer
)
5001 struct peer_group
*group
;
5002 struct listnode
*node
, *nnode
;
5005 if (peer
->sort
== BGP_PEER_IBGP
)
5008 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
5009 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
5011 if (peer_group_active(peer
))
5012 ttl
= peer
->group
->conf
->ttl
;
5014 ttl
= BGP_DEFAULT_TTL
;
5016 if (ttl
== peer
->ttl
)
5021 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5022 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5023 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5024 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5026 bgp_session_reset(peer
);
5028 /* Reconfigure BFD peer with new TTL. */
5029 if (peer
->bfd_config
)
5030 bgp_peer_bfd_update_source(peer
);
5032 group
= peer
->group
;
5033 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5034 if (peer
->sort
== BGP_PEER_IBGP
)
5037 peer
->ttl
= BGP_DEFAULT_TTL
;
5039 if (peer
->fd
>= 0) {
5040 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5042 peer
, BGP_NOTIFY_CEASE
,
5043 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5045 bgp_session_reset(peer
);
5048 /* Reconfigure BFD peer with new TTL. */
5049 if (peer
->bfd_config
)
5050 bgp_peer_bfd_update_source(peer
);
5056 /* Set Open Policy Role and check its correctness */
5057 int peer_role_set(struct peer
*peer
, uint8_t role
, bool strict_mode
)
5059 struct peer
*member
;
5060 struct listnode
*node
, *nnode
;
5062 peer_flag_set(peer
, PEER_FLAG_ROLE
);
5064 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5065 if (peer
->sort
!= BGP_PEER_EBGP
)
5066 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5068 if (peer
->local_role
== role
) {
5069 if (CHECK_FLAG(peer
->flags
,
5070 PEER_FLAG_ROLE_STRICT_MODE
) &&
5072 /* TODO: Is session restart needed if it was
5075 UNSET_FLAG(peer
->flags
,
5076 PEER_FLAG_ROLE_STRICT_MODE
);
5077 if (!CHECK_FLAG(peer
->flags
,
5078 PEER_FLAG_ROLE_STRICT_MODE
) &&
5080 SET_FLAG(peer
->flags
,
5081 PEER_FLAG_ROLE_STRICT_MODE
);
5082 /* Restart session to throw Role Mismatch
5085 if (peer
->remote_role
== ROLE_UNDEFINED
)
5086 bgp_session_reset(peer
);
5089 peer
->local_role
= role
;
5091 SET_FLAG(peer
->flags
,
5092 PEER_FLAG_ROLE_STRICT_MODE
);
5094 UNSET_FLAG(peer
->flags
,
5095 PEER_FLAG_ROLE_STRICT_MODE
);
5096 bgp_session_reset(peer
);
5102 peer
->local_role
= role
;
5103 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5104 if (member
->sort
!= BGP_PEER_EBGP
)
5105 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5107 if (member
->local_role
== role
) {
5108 if (CHECK_FLAG(member
->flags
,
5109 PEER_FLAG_ROLE_STRICT_MODE
) &&
5111 /* TODO: Is session restart needed if it was
5114 UNSET_FLAG(member
->flags
,
5115 PEER_FLAG_ROLE_STRICT_MODE
);
5116 if (!CHECK_FLAG(member
->flags
,
5117 PEER_FLAG_ROLE_STRICT_MODE
) &&
5119 SET_FLAG(peer
->flags
,
5120 PEER_FLAG_ROLE_STRICT_MODE
);
5121 SET_FLAG(member
->flags
,
5122 PEER_FLAG_ROLE_STRICT_MODE
);
5123 /* Restart session to throw Role Mismatch
5126 if (member
->remote_role
== ROLE_UNDEFINED
)
5127 bgp_session_reset(member
);
5130 member
->local_role
= role
;
5133 SET_FLAG(peer
->flags
,
5134 PEER_FLAG_ROLE_STRICT_MODE
);
5135 SET_FLAG(member
->flags
,
5136 PEER_FLAG_ROLE_STRICT_MODE
);
5138 UNSET_FLAG(member
->flags
,
5139 PEER_FLAG_ROLE_STRICT_MODE
);
5141 bgp_session_reset(member
);
5148 int peer_role_unset(struct peer
*peer
)
5150 struct peer
*member
;
5151 struct listnode
*node
, *nnode
;
5153 peer_flag_unset(peer
, PEER_FLAG_ROLE
);
5155 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5156 return peer_role_set(peer
, ROLE_UNDEFINED
, 0);
5158 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
))
5159 peer_role_set(member
, ROLE_UNDEFINED
, 0);
5164 /* Neighbor description. */
5165 void peer_description_set(struct peer
*peer
, const char *desc
)
5167 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5169 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
5172 void peer_description_unset(struct peer
*peer
)
5174 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5177 /* Neighbor update-source. */
5178 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
5180 struct peer
*member
;
5181 struct listnode
*node
, *nnode
;
5183 /* Set flag and configuration on peer. */
5184 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5185 if (peer
->update_if
) {
5186 if (strcmp(peer
->update_if
, ifname
) == 0)
5188 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5190 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5191 sockunion_free(peer
->update_source
);
5192 peer
->update_source
= NULL
;
5194 /* Check if handling a regular peer. */
5195 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5196 /* Send notification or reset peer depending on state. */
5197 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5198 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5199 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5200 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5202 bgp_session_reset(peer
);
5204 /* Apply new source configuration to BFD session. */
5205 if (peer
->bfd_config
)
5206 bgp_peer_bfd_update_source(peer
);
5208 /* Skip peer-group mechanics for regular peers. */
5213 * Set flag and configuration on all peer-group members, unless they are
5214 * explicitly overriding peer-group configuration.
5216 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5217 /* Skip peers with overridden configuration. */
5218 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5221 /* Skip peers with the same configuration. */
5222 if (member
->update_if
) {
5223 if (strcmp(member
->update_if
, ifname
) == 0)
5225 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5228 /* Set flag and configuration on peer-group member. */
5229 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5230 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5231 sockunion_free(member
->update_source
);
5232 member
->update_source
= NULL
;
5234 /* Send notification or reset peer depending on state. */
5235 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5236 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5237 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5238 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5240 bgp_session_reset(member
);
5242 /* Apply new source configuration to BFD session. */
5243 if (member
->bfd_config
)
5244 bgp_peer_bfd_update_source(member
);
5250 void peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
5252 struct peer
*member
;
5253 struct listnode
*node
, *nnode
;
5255 /* Set flag and configuration on peer. */
5256 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5257 if (peer
->update_source
) {
5258 if (sockunion_cmp(peer
->update_source
, su
) == 0)
5260 sockunion_free(peer
->update_source
);
5262 peer
->update_source
= sockunion_dup(su
);
5263 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5265 /* Check if handling a regular peer. */
5266 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5267 /* Send notification or reset peer depending on state. */
5268 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5269 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5270 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5271 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5273 bgp_session_reset(peer
);
5275 /* Apply new source configuration to BFD session. */
5276 if (peer
->bfd_config
)
5277 bgp_peer_bfd_update_source(peer
);
5279 /* Skip peer-group mechanics for regular peers. */
5284 * Set flag and configuration on all peer-group members, unless they are
5285 * explicitly overriding peer-group configuration.
5287 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5288 /* Skip peers with overridden configuration. */
5289 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5292 /* Skip peers with the same configuration. */
5293 if (member
->update_source
) {
5294 if (sockunion_cmp(member
->update_source
, su
) == 0)
5296 sockunion_free(member
->update_source
);
5299 /* Set flag and configuration on peer-group member. */
5300 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5301 member
->update_source
= sockunion_dup(su
);
5302 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5304 /* Send notification or reset peer depending on state. */
5305 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5306 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5307 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5308 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5310 bgp_session_reset(member
);
5312 /* Apply new source configuration to BFD session. */
5313 if (member
->bfd_config
)
5314 bgp_peer_bfd_update_source(member
);
5318 void peer_update_source_unset(struct peer
*peer
)
5320 struct peer
*member
;
5321 struct listnode
*node
, *nnode
;
5323 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
5326 /* Inherit configuration from peer-group if peer is member. */
5327 if (peer_group_active(peer
)) {
5328 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
5329 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
5330 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
5331 MTYPE_PEER_UPDATE_SOURCE
);
5333 /* Otherwise remove flag and configuration from peer. */
5334 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
5335 sockunion_free(peer
->update_source
);
5336 peer
->update_source
= NULL
;
5337 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5340 /* Check if handling a regular peer. */
5341 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5342 /* Send notification or reset peer depending on state. */
5343 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5344 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5345 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5346 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5348 bgp_session_reset(peer
);
5350 /* Apply new source configuration to BFD session. */
5351 if (peer
->bfd_config
)
5352 bgp_peer_bfd_update_source(peer
);
5354 /* Skip peer-group mechanics for regular peers. */
5359 * Set flag and configuration on all peer-group members, unless they are
5360 * explicitly overriding peer-group configuration.
5362 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5363 /* Skip peers with overridden configuration. */
5364 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5367 /* Skip peers with the same configuration. */
5368 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5369 && !member
->update_source
&& !member
->update_if
)
5372 /* Remove flag and configuration on peer-group member. */
5373 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5374 sockunion_free(member
->update_source
);
5375 member
->update_source
= NULL
;
5376 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5378 /* Send notification or reset peer depending on state. */
5379 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5380 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5381 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5382 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5384 bgp_session_reset(member
);
5386 /* Apply new source configuration to BFD session. */
5387 if (member
->bfd_config
)
5388 bgp_peer_bfd_update_source(member
);
5392 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5393 const char *rmap
, struct route_map
*route_map
)
5395 struct peer
*member
;
5396 struct listnode
*node
, *nnode
;
5397 struct update_subgroup
*subgrp
;
5399 /* Set flag and configuration on peer. */
5400 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5402 subgrp
= peer_subgroup(peer
, afi
, safi
);
5405 if (!peer
->default_rmap
[afi
][safi
].name
5406 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5407 if (peer
->default_rmap
[afi
][safi
].name
)
5408 XFREE(MTYPE_ROUTE_MAP_NAME
,
5409 peer
->default_rmap
[afi
][safi
].name
);
5412 * When there is a change in route-map policy,
5413 * this flow gets triggered. Since, the default
5414 * route is already originated, the flag is set.
5415 * The flag should be unset here,
5416 * to trigger the flow of sending update message.
5419 UNSET_FLAG(subgrp
->sflags
,
5420 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5422 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5423 peer
->default_rmap
[afi
][safi
].name
=
5424 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5425 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5426 route_map_counter_increment(route_map
);
5429 if (peer
->default_rmap
[afi
][safi
].name
)
5430 XFREE(MTYPE_ROUTE_MAP_NAME
,
5431 peer
->default_rmap
[afi
][safi
].name
);
5434 * This is triggered in case of route-map deletion.
5435 * The flag needs to be unset, to trigger the flow
5436 * of sending an update message.
5439 UNSET_FLAG(subgrp
->sflags
,
5440 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5442 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5443 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5444 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5447 /* Check if handling a regular peer. */
5448 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5449 /* Update peer route announcements. */
5450 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5451 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5452 bgp_default_originate(peer
, afi
, safi
, 0);
5453 bgp_announce_route(peer
, afi
, safi
, false);
5456 /* Skip peer-group mechanics for regular peers. */
5461 * Set flag and configuration on all peer-group members, unless they are
5462 * explicitly overriding peer-group configuration.
5464 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5465 /* Skip peers with overridden configuration. */
5466 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5467 PEER_FLAG_DEFAULT_ORIGINATE
))
5470 /* Set flag and configuration on peer-group member. */
5471 SET_FLAG(member
->af_flags
[afi
][safi
],
5472 PEER_FLAG_DEFAULT_ORIGINATE
);
5474 if (member
->default_rmap
[afi
][safi
].name
)
5475 XFREE(MTYPE_ROUTE_MAP_NAME
,
5476 member
->default_rmap
[afi
][safi
].name
);
5477 route_map_counter_decrement(
5478 member
->default_rmap
[afi
][safi
].map
);
5479 member
->default_rmap
[afi
][safi
].name
=
5480 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5481 member
->default_rmap
[afi
][safi
].map
= route_map
;
5482 route_map_counter_increment(route_map
);
5485 /* Update peer route announcements. */
5486 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5487 update_group_adjust_peer(
5488 peer_af_find(member
, afi
, safi
));
5489 bgp_default_originate(member
, afi
, safi
, 0);
5490 bgp_announce_route(member
, afi
, safi
, false);
5497 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5499 struct peer
*member
;
5500 struct listnode
*node
, *nnode
;
5502 /* Inherit configuration from peer-group if peer is member. */
5503 if (peer_group_active(peer
)) {
5504 peer_af_flag_inherit(peer
, afi
, safi
,
5505 PEER_FLAG_DEFAULT_ORIGINATE
);
5506 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5507 default_rmap
[afi
][safi
].name
,
5508 MTYPE_ROUTE_MAP_NAME
);
5509 PEER_ATTR_INHERIT(peer
, peer
->group
,
5510 default_rmap
[afi
][safi
].map
);
5512 /* Otherwise remove flag and configuration from peer. */
5513 peer_af_flag_unset(peer
, afi
, safi
,
5514 PEER_FLAG_DEFAULT_ORIGINATE
);
5515 if (peer
->default_rmap
[afi
][safi
].name
)
5516 XFREE(MTYPE_ROUTE_MAP_NAME
,
5517 peer
->default_rmap
[afi
][safi
].name
);
5518 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5519 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5520 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5523 /* Check if handling a regular peer. */
5524 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5525 /* Update peer route announcements. */
5526 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5527 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5528 bgp_default_originate(peer
, afi
, safi
, 1);
5529 bgp_announce_route(peer
, afi
, safi
, false);
5532 /* Skip peer-group mechanics for regular peers. */
5537 * Remove flag and configuration from all peer-group members, unless
5538 * they are explicitly overriding peer-group configuration.
5540 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5541 /* Skip peers with overridden configuration. */
5542 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5543 PEER_FLAG_DEFAULT_ORIGINATE
))
5546 /* Remove flag and configuration on peer-group member. */
5547 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5548 PEER_FLAG_DEFAULT_ORIGINATE
);
5549 if (member
->default_rmap
[afi
][safi
].name
)
5550 XFREE(MTYPE_ROUTE_MAP_NAME
,
5551 member
->default_rmap
[afi
][safi
].name
);
5552 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5553 member
->default_rmap
[afi
][safi
].name
= NULL
;
5554 member
->default_rmap
[afi
][safi
].map
= NULL
;
5556 /* Update peer route announcements. */
5557 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5558 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5559 bgp_default_originate(member
, afi
, safi
, 1);
5560 bgp_announce_route(member
, afi
, safi
, false);
5567 void peer_port_set(struct peer
*peer
, uint16_t port
)
5570 peer_flag_set(peer
, PEER_FLAG_PORT
);
5573 void peer_port_unset(struct peer
*peer
)
5575 peer
->port
= BGP_PORT_DEFAULT
;
5576 peer_flag_unset(peer
, PEER_FLAG_PORT
);
5579 /* Set the TCP-MSS value in the peer structure,
5580 * This gets applied only after connection reset
5581 * So this value will be used in bgp_connect.
5583 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5585 peer
->tcp_mss
= tcp_mss
;
5586 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5589 /* Reset the TCP-MSS value in the peer structure,
5590 * This gets applied only after connection reset
5591 * So this value will be used in bgp_connect.
5593 void peer_tcp_mss_unset(struct peer
*peer
)
5595 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5600 * Helper function that is called after the name of the policy
5601 * being used by a peer has changed (AF specific). Automatically
5602 * initiates inbound or outbound processing as needed.
5604 void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5608 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5609 if (peer_established(peer
))
5610 bgp_announce_route(peer
, afi
, safi
, false);
5612 if (!peer_established(peer
))
5615 if (bgp_soft_reconfig_in(peer
, afi
, safi
))
5618 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
) ||
5619 CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
)) {
5620 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5621 PEER_CAP_ORF_PREFIX_SM_ADV
) &&
5622 (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5623 PEER_CAP_ORF_PREFIX_RM_RCV
) ||
5624 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5625 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
)))
5626 peer_clear_soft(peer
, afi
, safi
,
5627 BGP_CLEAR_SOFT_IN_ORF_PREFIX
);
5629 bgp_route_refresh_send(
5630 peer
, afi
, safi
, 0, 0, 0,
5631 BGP_ROUTE_REFRESH_NORMAL
);
5637 /* neighbor weight. */
5638 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5640 struct peer
*member
;
5641 struct listnode
*node
, *nnode
;
5643 /* Set flag and configuration on peer. */
5644 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5645 if (peer
->weight
[afi
][safi
] != weight
) {
5646 peer
->weight
[afi
][safi
] = weight
;
5647 peer_on_policy_change(peer
, afi
, safi
, 0);
5650 /* Skip peer-group mechanics for regular peers. */
5651 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5655 * Set flag and configuration on all peer-group members, unless they are
5656 * explicitly overriding peer-group configuration.
5658 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5659 /* Skip peers with overridden configuration. */
5660 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5664 /* Set flag and configuration on peer-group member. */
5665 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5666 if (member
->weight
[afi
][safi
] != weight
) {
5667 member
->weight
[afi
][safi
] = weight
;
5668 peer_on_policy_change(member
, afi
, safi
, 0);
5675 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5677 struct peer
*member
;
5678 struct listnode
*node
, *nnode
;
5680 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5683 /* Inherit configuration from peer-group if peer is member. */
5684 if (peer_group_active(peer
)) {
5685 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5686 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5688 peer_on_policy_change(peer
, afi
, safi
, 0);
5692 /* Remove flag and configuration from peer. */
5693 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5694 peer
->weight
[afi
][safi
] = 0;
5695 peer_on_policy_change(peer
, afi
, safi
, 0);
5697 /* Skip peer-group mechanics for regular peers. */
5698 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5702 * Remove flag and configuration from all peer-group members, unless
5703 * they are explicitly overriding peer-group configuration.
5705 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5706 /* Skip peers with overridden configuration. */
5707 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5711 /* Skip peers where flag is already disabled. */
5712 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5715 /* Remove flag and configuration on peer-group member. */
5716 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5717 member
->weight
[afi
][safi
] = 0;
5718 peer_on_policy_change(member
, afi
, safi
, 0);
5724 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5726 struct peer
*member
;
5727 struct listnode
*node
, *nnode
;
5729 if (keepalive
> UINT16_MAX
)
5730 return BGP_ERR_INVALID_VALUE
;
5732 if (holdtime
> UINT16_MAX
)
5733 return BGP_ERR_INVALID_VALUE
;
5735 if (holdtime
< 3 && holdtime
!= 0)
5736 return BGP_ERR_INVALID_VALUE
;
5738 /* Set flag and configuration on peer. */
5739 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5740 peer
->holdtime
= holdtime
;
5741 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5743 /* Skip peer-group mechanics for regular peers. */
5744 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5748 * Set flag and configuration on all peer-group members, unless they are
5749 * explicitly overriding peer-group configuration.
5751 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5752 /* Skip peers with overridden configuration. */
5753 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5756 /* Set flag and configuration on peer-group member. */
5757 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5758 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5759 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5765 int peer_timers_unset(struct peer
*peer
)
5767 struct peer
*member
;
5768 struct listnode
*node
, *nnode
;
5770 /* Inherit configuration from peer-group if peer is member. */
5771 if (peer_group_active(peer
)) {
5772 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5773 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5774 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5776 /* Otherwise remove flag and configuration from peer. */
5777 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5779 peer
->keepalive
= 0;
5782 /* Skip peer-group mechanics for regular peers. */
5783 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5787 * Remove flag and configuration from all peer-group members, unless
5788 * they are explicitly overriding peer-group configuration.
5790 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5791 /* Skip peers with overridden configuration. */
5792 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5795 /* Remove flag and configuration on peer-group member. */
5796 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5797 member
->holdtime
= 0;
5798 member
->keepalive
= 0;
5804 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5806 struct peer
*member
;
5807 struct listnode
*node
, *nnode
;
5809 if (connect
> UINT16_MAX
)
5810 return BGP_ERR_INVALID_VALUE
;
5812 /* Set flag and configuration on peer. */
5813 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5814 peer
->connect
= connect
;
5815 peer
->v_connect
= connect
;
5817 /* Skip peer-group mechanics for regular peers. */
5818 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5819 if (!peer_established(peer
)) {
5820 if (peer_active(peer
))
5821 BGP_EVENT_ADD(peer
, BGP_Stop
);
5822 BGP_EVENT_ADD(peer
, BGP_Start
);
5827 * Set flag and configuration on all peer-group members, unless they are
5828 * explicitly overriding peer-group configuration.
5830 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5831 /* Skip peers with overridden configuration. */
5832 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5835 /* Set flag and configuration on peer-group member. */
5836 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5837 member
->connect
= connect
;
5838 member
->v_connect
= connect
;
5840 if (!peer_established(member
)) {
5841 if (peer_active(member
))
5842 BGP_EVENT_ADD(member
, BGP_Stop
);
5843 BGP_EVENT_ADD(member
, BGP_Start
);
5850 int peer_timers_connect_unset(struct peer
*peer
)
5852 struct peer
*member
;
5853 struct listnode
*node
, *nnode
;
5855 /* Inherit configuration from peer-group if peer is member. */
5856 if (peer_group_active(peer
)) {
5857 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5858 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5860 /* Otherwise remove flag and configuration from peer. */
5861 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5865 /* Set timer with fallback to default value. */
5867 peer
->v_connect
= peer
->connect
;
5869 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5871 /* Skip peer-group mechanics for regular peers. */
5872 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5873 if (!peer_established(peer
)) {
5874 if (peer_active(peer
))
5875 BGP_EVENT_ADD(peer
, BGP_Stop
);
5876 BGP_EVENT_ADD(peer
, BGP_Start
);
5881 * Remove flag and configuration from all peer-group members, unless
5882 * they are explicitly overriding peer-group configuration.
5884 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5885 /* Skip peers with overridden configuration. */
5886 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5889 /* Remove flag and configuration on peer-group member. */
5890 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5891 member
->connect
= 0;
5892 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5894 if (!peer_established(member
)) {
5895 if (peer_active(member
))
5896 BGP_EVENT_ADD(member
, BGP_Stop
);
5897 BGP_EVENT_ADD(member
, BGP_Start
);
5904 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5906 struct peer
*member
;
5907 struct listnode
*node
, *nnode
;
5910 return BGP_ERR_INVALID_VALUE
;
5912 /* Set flag and configuration on peer. */
5913 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5914 peer
->routeadv
= routeadv
;
5915 peer
->v_routeadv
= routeadv
;
5917 /* Check if handling a regular peer. */
5918 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5919 /* Update peer route announcements. */
5920 update_group_adjust_peer_afs(peer
);
5921 if (peer_established(peer
))
5922 bgp_announce_route_all(peer
);
5924 /* Skip peer-group mechanics for regular peers. */
5929 * Set flag and configuration on all peer-group members, unless they are
5930 * explicitly overriding peer-group configuration.
5932 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5933 /* Skip peers with overridden configuration. */
5934 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5937 /* Set flag and configuration on peer-group member. */
5938 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5939 member
->routeadv
= routeadv
;
5940 member
->v_routeadv
= routeadv
;
5942 /* Update peer route announcements. */
5943 update_group_adjust_peer_afs(member
);
5944 if (peer_established(member
))
5945 bgp_announce_route_all(member
);
5951 int peer_advertise_interval_unset(struct peer
*peer
)
5953 struct peer
*member
;
5954 struct listnode
*node
, *nnode
;
5956 /* Inherit configuration from peer-group if peer is member. */
5957 if (peer_group_active(peer
)) {
5958 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5959 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5961 /* Otherwise remove flag and configuration from peer. */
5962 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5966 /* Set timer with fallback to default value. */
5968 peer
->v_routeadv
= peer
->routeadv
;
5970 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5971 ? BGP_DEFAULT_IBGP_ROUTEADV
5972 : BGP_DEFAULT_EBGP_ROUTEADV
;
5974 /* Check if handling a regular peer. */
5975 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5976 /* Update peer route announcements. */
5977 update_group_adjust_peer_afs(peer
);
5978 if (peer_established(peer
))
5979 bgp_announce_route_all(peer
);
5981 /* Skip peer-group mechanics for regular peers. */
5986 * Remove flag and configuration from all peer-group members, unless
5987 * they are explicitly overriding peer-group configuration.
5989 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5990 /* Skip peers with overridden configuration. */
5991 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5994 /* Remove flag and configuration on peer-group member. */
5995 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5996 member
->routeadv
= 0;
5997 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
5998 ? BGP_DEFAULT_IBGP_ROUTEADV
5999 : BGP_DEFAULT_EBGP_ROUTEADV
;
6001 /* Update peer route announcements. */
6002 update_group_adjust_peer_afs(member
);
6003 if (peer_established(member
))
6004 bgp_announce_route_all(member
);
6010 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
6013 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
6015 struct peer
*member
;
6016 struct listnode
*node
;
6018 /* Set peers session attribute flag and timer interval. */
6019 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6020 peer
->delayopen
= delayopen
;
6021 peer
->v_delayopen
= delayopen
;
6023 /* Skip group mechanics for regular peers. */
6024 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6027 /* Set flag and configuration on all peer-group members, unless they are
6028 * explicitly overriding peer-group configuration.
6030 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6031 /* Skip peers with overridden configuration. */
6032 if (CHECK_FLAG(member
->flags_override
,
6033 PEER_FLAG_TIMER_DELAYOPEN
))
6036 /* Set session attribute flag and timer intervals on peer-group
6039 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6040 member
->delayopen
= delayopen
;
6041 member
->v_delayopen
= delayopen
;
6047 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
6048 * DelayOpenTimer interval to the default value.
6050 int peer_timers_delayopen_unset(struct peer
*peer
)
6052 struct peer
*member
;
6053 struct listnode
*node
;
6055 /* Inherit configuration from peer-group if peer is member. */
6056 if (peer_group_active(peer
)) {
6057 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6058 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
6060 /* Otherwise remove session attribute flag and set timer
6061 * interval to default value.
6063 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6064 peer
->delayopen
= peer
->bgp
->default_delayopen
;
6067 /* Set timer value to zero */
6068 peer
->v_delayopen
= 0;
6070 /* Skip peer-group mechanics for regular peers. */
6071 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6074 /* Remove flag and configuration from all peer-group members, unless
6075 * they are explicitly overriding peer-group configuration.
6077 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6078 /* Skip peers with overridden configuration. */
6079 if (CHECK_FLAG(member
->flags_override
,
6080 PEER_FLAG_TIMER_DELAYOPEN
))
6083 /* Remove session attribute flag, reset the timer interval to
6084 * the default value and set the timer value to zero.
6086 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6087 member
->delayopen
= peer
->bgp
->default_delayopen
;
6088 member
->v_delayopen
= 0;
6094 /* neighbor interface */
6095 void peer_interface_set(struct peer
*peer
, const char *str
)
6097 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6098 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
6101 void peer_interface_unset(struct peer
*peer
)
6103 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6107 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6108 int allow_num
, int origin
)
6110 struct peer
*member
;
6111 struct listnode
*node
, *nnode
;
6113 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
6114 return BGP_ERR_INVALID_VALUE
;
6116 /* Set flag and configuration on peer. */
6117 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6119 if (peer
->allowas_in
[afi
][safi
] != 0
6120 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6121 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6122 peer_af_flag_set(peer
, afi
, safi
,
6123 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6124 peer
->allowas_in
[afi
][safi
] = 0;
6125 peer_on_policy_change(peer
, afi
, safi
, 0);
6128 if (peer
->allowas_in
[afi
][safi
] != allow_num
6129 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6130 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6132 peer_af_flag_unset(peer
, afi
, safi
,
6133 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6134 peer
->allowas_in
[afi
][safi
] = allow_num
;
6135 peer_on_policy_change(peer
, afi
, safi
, 0);
6139 /* Skip peer-group mechanics for regular peers. */
6140 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6144 * Set flag and configuration on all peer-group members, unless
6145 * they are explicitly overriding peer-group configuration.
6147 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6148 /* Skip peers with overridden configuration. */
6149 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6150 PEER_FLAG_ALLOWAS_IN
))
6153 /* Set flag and configuration on peer-group member. */
6154 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6156 if (member
->allowas_in
[afi
][safi
] != 0
6157 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
6158 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6159 SET_FLAG(member
->af_flags
[afi
][safi
],
6160 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6161 member
->allowas_in
[afi
][safi
] = 0;
6162 peer_on_policy_change(peer
, afi
, safi
, 0);
6165 if (member
->allowas_in
[afi
][safi
] != allow_num
6166 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
6167 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6168 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6169 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6170 member
->allowas_in
[afi
][safi
] = allow_num
;
6171 peer_on_policy_change(peer
, afi
, safi
, 0);
6179 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6181 struct peer
*member
;
6182 struct listnode
*node
, *nnode
;
6184 /* Skip peer if flag is already disabled. */
6185 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
6188 /* Inherit configuration from peer-group if peer is member. */
6189 if (peer_group_active(peer
)) {
6190 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6191 peer_af_flag_inherit(peer
, afi
, safi
,
6192 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6193 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
6194 peer_on_policy_change(peer
, afi
, safi
, 0);
6199 /* Remove flag and configuration from peer. */
6200 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6201 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6202 peer
->allowas_in
[afi
][safi
] = 0;
6203 peer_on_policy_change(peer
, afi
, safi
, 0);
6205 /* Skip peer-group mechanics if handling a regular peer. */
6206 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6210 * Remove flags and configuration from all peer-group members, unless
6211 * they are explicitly overriding peer-group configuration.
6213 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6214 /* Skip peers with overridden configuration. */
6215 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6216 PEER_FLAG_ALLOWAS_IN
))
6219 /* Remove flags and configuration on peer-group member. */
6220 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6221 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6222 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6223 member
->allowas_in
[afi
][safi
] = 0;
6224 peer_on_policy_change(member
, afi
, safi
, 0);
6230 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
6233 bool old_no_prepend
, old_replace_as
;
6234 struct bgp
*bgp
= peer
->bgp
;
6235 struct peer
*member
;
6236 struct listnode
*node
, *nnode
;
6239 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
6241 /* Save previous flag states. */
6243 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6245 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6247 /* Set flag and configuration on peer. */
6248 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
6249 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
6250 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
6252 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
6253 && old_replace_as
== replace_as
)
6255 peer
->change_local_as
= as
;
6256 (void)peer_sort(peer
);
6258 /* Check if handling a regular peer. */
6259 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6263 * Set flag and configuration on all peer-group members, unless they are
6264 * explicitly overriding peer-group configuration.
6266 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6267 /* Skip peers with overridden configuration. */
6268 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6271 /* Skip peers with the same configuration. */
6272 old_no_prepend
= CHECK_FLAG(member
->flags
,
6273 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6274 old_replace_as
= CHECK_FLAG(member
->flags
,
6275 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6276 if (member
->change_local_as
== as
6277 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
6278 && old_no_prepend
== no_prepend
6279 && old_replace_as
== replace_as
)
6282 /* Set flag and configuration on peer-group member. */
6283 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6284 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
6286 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
6288 member
->change_local_as
= as
;
6294 int peer_local_as_unset(struct peer
*peer
)
6296 struct peer
*member
;
6297 struct listnode
*node
, *nnode
;
6299 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
6302 /* Inherit configuration from peer-group if peer is member. */
6303 if (peer_group_active(peer
)) {
6304 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
6305 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6306 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6307 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
6309 /* Otherwise remove flag and configuration from peer. */
6310 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
6311 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6312 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6313 peer
->change_local_as
= 0;
6316 /* Check if handling a regular peer. */
6317 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6318 /* Send notification or stop peer depending on state. */
6319 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6320 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6321 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6322 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6324 BGP_EVENT_ADD(peer
, BGP_Stop
);
6326 /* Skip peer-group mechanics for regular peers. */
6331 * Remove flag and configuration from all peer-group members, unless
6332 * they are explicitly overriding peer-group configuration.
6334 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6335 /* Skip peers with overridden configuration. */
6336 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6339 /* Remove flag and configuration on peer-group member. */
6340 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6341 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6342 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6343 member
->change_local_as
= 0;
6345 /* Send notification or stop peer depending on state. */
6346 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6347 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6348 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6349 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6351 bgp_session_reset(member
);
6357 /* Set password for authenticating with the peer. */
6358 int peer_password_set(struct peer
*peer
, const char *password
)
6360 struct peer
*member
;
6361 struct listnode
*node
, *nnode
;
6362 int len
= password
? strlen(password
) : 0;
6363 int ret
= BGP_SUCCESS
;
6365 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
6366 return BGP_ERR_INVALID_VALUE
;
6368 /* Set flag and configuration on peer. */
6369 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
6370 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
6372 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6373 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6375 /* Check if handling a regular peer. */
6376 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6377 /* Send notification or reset peer depending on state. */
6378 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6379 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6380 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6382 bgp_session_reset(peer
);
6385 * Attempt to install password on socket and skip peer-group
6388 if (BGP_PEER_SU_UNSPEC(peer
))
6390 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6391 : BGP_ERR_TCPSIG_FAILED
;
6395 * Set flag and configuration on all peer-group members, unless they are
6396 * explicitly overriding peer-group configuration.
6398 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6399 /* Skip peers with overridden configuration. */
6400 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6403 /* Skip peers with the same password. */
6404 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6407 /* Set flag and configuration on peer-group member. */
6408 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6409 if (member
->password
)
6410 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6411 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6413 /* Send notification or reset peer depending on state. */
6414 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6415 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6416 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6418 bgp_session_reset(member
);
6420 /* Attempt to install password on socket. */
6421 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6422 ret
= BGP_ERR_TCPSIG_FAILED
;
6425 /* Set flag and configuration on all peer-group listen ranges */
6426 struct listnode
*ln
;
6429 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6430 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6431 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6432 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6437 int peer_password_unset(struct peer
*peer
)
6439 struct peer
*member
;
6440 struct listnode
*node
, *nnode
;
6442 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6445 /* Inherit configuration from peer-group if peer is member. */
6446 if (peer_group_active(peer
)) {
6447 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6448 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6449 MTYPE_PEER_PASSWORD
);
6451 /* Otherwise remove flag and configuration from peer. */
6452 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6453 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6456 /* Check if handling a regular peer. */
6457 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6458 /* Send notification or reset peer depending on state. */
6459 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6460 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6461 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6463 bgp_session_reset(peer
);
6465 /* Attempt to uninstall password on socket. */
6466 if (!BGP_PEER_SU_UNSPEC(peer
))
6467 bgp_md5_unset(peer
);
6468 /* Skip peer-group mechanics for regular peers. */
6473 * Remove flag and configuration from all peer-group members, unless
6474 * they are explicitly overriding peer-group configuration.
6476 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6477 /* Skip peers with overridden configuration. */
6478 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6481 /* Remove flag and configuration on peer-group member. */
6482 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6483 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6485 /* Send notification or reset peer depending on state. */
6486 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6487 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6488 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6490 bgp_session_reset(member
);
6492 /* Attempt to uninstall password on socket. */
6493 if (!BGP_PEER_SU_UNSPEC(member
))
6494 bgp_md5_unset(member
);
6497 /* Set flag and configuration on all peer-group listen ranges */
6498 struct listnode
*ln
;
6501 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6502 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6503 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6504 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6510 /* Set distribute list to the peer. */
6511 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6514 struct peer
*member
;
6515 struct bgp_filter
*filter
;
6516 struct listnode
*node
, *nnode
;
6518 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6519 return BGP_ERR_INVALID_VALUE
;
6521 /* Set configuration on peer. */
6522 filter
= &peer
->filter
[afi
][safi
];
6523 if (filter
->plist
[direct
].name
)
6524 return BGP_ERR_PEER_FILTER_CONFLICT
;
6525 if (filter
->dlist
[direct
].name
)
6526 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6527 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6528 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6530 /* Check if handling a regular peer. */
6531 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6532 /* Set override-flag and process peer route updates. */
6533 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6534 PEER_FT_DISTRIBUTE_LIST
);
6535 peer_on_policy_change(peer
, afi
, safi
,
6536 (direct
== FILTER_OUT
) ? 1 : 0);
6538 /* Skip peer-group mechanics for regular peers. */
6543 * Set configuration on all peer-group members, un less they are
6544 * explicitly overriding peer-group configuration.
6546 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6547 /* Skip peers with overridden configuration. */
6548 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6549 PEER_FT_DISTRIBUTE_LIST
))
6552 /* Set configuration on peer-group member. */
6553 filter
= &member
->filter
[afi
][safi
];
6554 if (filter
->dlist
[direct
].name
)
6555 XFREE(MTYPE_BGP_FILTER_NAME
,
6556 filter
->dlist
[direct
].name
);
6557 filter
->dlist
[direct
].name
=
6558 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6559 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6561 /* Process peer route updates. */
6562 peer_on_policy_change(member
, afi
, safi
,
6563 (direct
== FILTER_OUT
) ? 1 : 0);
6569 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6571 struct peer
*member
;
6572 struct bgp_filter
*filter
;
6573 struct listnode
*node
, *nnode
;
6575 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6576 return BGP_ERR_INVALID_VALUE
;
6578 /* Unset override-flag unconditionally. */
6579 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6580 PEER_FT_DISTRIBUTE_LIST
);
6582 /* Inherit configuration from peer-group if peer is member. */
6583 if (peer_group_active(peer
)) {
6584 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6585 filter
[afi
][safi
].dlist
[direct
].name
,
6586 MTYPE_BGP_FILTER_NAME
);
6587 PEER_ATTR_INHERIT(peer
, peer
->group
,
6588 filter
[afi
][safi
].dlist
[direct
].alist
);
6590 /* Otherwise remove configuration from peer. */
6591 filter
= &peer
->filter
[afi
][safi
];
6592 if (filter
->dlist
[direct
].name
)
6593 XFREE(MTYPE_BGP_FILTER_NAME
,
6594 filter
->dlist
[direct
].name
);
6595 filter
->dlist
[direct
].name
= NULL
;
6596 filter
->dlist
[direct
].alist
= NULL
;
6599 /* Check if handling a regular peer. */
6600 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6601 /* Process peer route updates. */
6602 peer_on_policy_change(peer
, afi
, safi
,
6603 (direct
== FILTER_OUT
) ? 1 : 0);
6605 /* Skip peer-group mechanics for regular peers. */
6610 * Remove configuration on all peer-group members, unless they are
6611 * explicitly overriding peer-group configuration.
6613 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6614 /* Skip peers with overridden configuration. */
6615 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6616 PEER_FT_DISTRIBUTE_LIST
))
6619 /* Remove configuration on peer-group member. */
6620 filter
= &member
->filter
[afi
][safi
];
6621 if (filter
->dlist
[direct
].name
)
6622 XFREE(MTYPE_BGP_FILTER_NAME
,
6623 filter
->dlist
[direct
].name
);
6624 filter
->dlist
[direct
].name
= NULL
;
6625 filter
->dlist
[direct
].alist
= NULL
;
6627 /* Process peer route updates. */
6628 peer_on_policy_change(member
, afi
, safi
,
6629 (direct
== FILTER_OUT
) ? 1 : 0);
6635 /* Update distribute list. */
6636 static void peer_distribute_update(struct access_list
*access
)
6641 struct listnode
*mnode
, *mnnode
;
6642 struct listnode
*node
, *nnode
;
6645 struct peer_group
*group
;
6646 struct bgp_filter
*filter
;
6648 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6650 update_group_policy_update(bgp
,
6651 BGP_POLICY_DISTRIBUTE_LIST
,
6652 access
->name
, true, 0);
6653 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6654 FOREACH_AFI_SAFI (afi
, safi
) {
6655 filter
= &peer
->filter
[afi
][safi
];
6657 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6659 if (filter
->dlist
[direct
].name
)
6660 filter
->dlist
[direct
]
6661 .alist
= access_list_lookup(
6663 filter
->dlist
[direct
]
6666 filter
->dlist
[direct
].alist
=
6671 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6672 FOREACH_AFI_SAFI (afi
, safi
) {
6673 filter
= &group
->conf
->filter
[afi
][safi
];
6675 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6677 if (filter
->dlist
[direct
].name
)
6678 filter
->dlist
[direct
]
6679 .alist
= access_list_lookup(
6681 filter
->dlist
[direct
]
6684 filter
->dlist
[direct
].alist
=
6689 #ifdef ENABLE_BGP_VNC
6690 vnc_prefix_list_update(bgp
);
6695 /* Set prefix list to the peer. */
6696 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6699 struct peer
*member
;
6700 struct bgp_filter
*filter
;
6701 struct listnode
*node
, *nnode
;
6703 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6704 return BGP_ERR_INVALID_VALUE
;
6706 /* Set configuration on peer. */
6707 filter
= &peer
->filter
[afi
][safi
];
6708 if (filter
->dlist
[direct
].name
)
6709 return BGP_ERR_PEER_FILTER_CONFLICT
;
6710 if (filter
->plist
[direct
].name
)
6711 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6712 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6713 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6715 /* Check if handling a regular peer. */
6716 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6717 /* Set override-flag and process peer route updates. */
6718 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6719 PEER_FT_PREFIX_LIST
);
6720 peer_on_policy_change(peer
, afi
, safi
,
6721 (direct
== FILTER_OUT
) ? 1 : 0);
6723 /* Skip peer-group mechanics for regular peers. */
6728 * Set configuration on all peer-group members, unless they are
6729 * explicitly overriding peer-group configuration.
6731 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6732 /* Skip peers with overridden configuration. */
6733 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6734 PEER_FT_PREFIX_LIST
))
6737 /* Set configuration on peer-group member. */
6738 filter
= &member
->filter
[afi
][safi
];
6739 if (filter
->plist
[direct
].name
)
6740 XFREE(MTYPE_BGP_FILTER_NAME
,
6741 filter
->plist
[direct
].name
);
6742 filter
->plist
[direct
].name
=
6743 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6744 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6746 /* Process peer route updates. */
6747 peer_on_policy_change(member
, afi
, safi
,
6748 (direct
== FILTER_OUT
) ? 1 : 0);
6754 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6757 struct peer
*member
;
6758 struct bgp_filter
*filter
;
6759 struct listnode
*node
, *nnode
;
6761 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6762 return BGP_ERR_INVALID_VALUE
;
6764 /* Unset override-flag unconditionally. */
6765 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6766 PEER_FT_PREFIX_LIST
);
6768 /* Inherit configuration from peer-group if peer is member. */
6769 if (peer_group_active(peer
)) {
6770 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6771 filter
[afi
][safi
].plist
[direct
].name
,
6772 MTYPE_BGP_FILTER_NAME
);
6773 PEER_ATTR_INHERIT(peer
, peer
->group
,
6774 filter
[afi
][safi
].plist
[direct
].plist
);
6776 /* Otherwise remove configuration from peer. */
6777 filter
= &peer
->filter
[afi
][safi
];
6778 if (filter
->plist
[direct
].name
)
6779 XFREE(MTYPE_BGP_FILTER_NAME
,
6780 filter
->plist
[direct
].name
);
6781 filter
->plist
[direct
].name
= NULL
;
6782 filter
->plist
[direct
].plist
= NULL
;
6785 /* Check if handling a regular peer. */
6786 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6787 /* Process peer route updates. */
6788 peer_on_policy_change(peer
, afi
, safi
,
6789 (direct
== FILTER_OUT
) ? 1 : 0);
6791 /* Skip peer-group mechanics for regular peers. */
6796 * Remove configuration on all peer-group members, unless they are
6797 * explicitly overriding peer-group configuration.
6799 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6800 /* Skip peers with overridden configuration. */
6801 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6802 PEER_FT_PREFIX_LIST
))
6805 /* Remove configuration on peer-group member. */
6806 filter
= &member
->filter
[afi
][safi
];
6807 if (filter
->plist
[direct
].name
)
6808 XFREE(MTYPE_BGP_FILTER_NAME
,
6809 filter
->plist
[direct
].name
);
6810 filter
->plist
[direct
].name
= NULL
;
6811 filter
->plist
[direct
].plist
= NULL
;
6813 /* Process peer route updates. */
6814 peer_on_policy_change(member
, afi
, safi
,
6815 (direct
== FILTER_OUT
) ? 1 : 0);
6821 /* Update prefix-list list. */
6822 static void peer_prefix_list_update(struct prefix_list
*plist
)
6824 struct listnode
*mnode
, *mnnode
;
6825 struct listnode
*node
, *nnode
;
6828 struct peer_group
*group
;
6829 struct bgp_filter
*filter
;
6834 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6837 * Update the prefix-list on update groups.
6839 update_group_policy_update(
6840 bgp
, BGP_POLICY_PREFIX_LIST
,
6841 plist
? prefix_list_name(plist
) : NULL
, true, 0);
6843 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6844 FOREACH_AFI_SAFI (afi
, safi
) {
6845 filter
= &peer
->filter
[afi
][safi
];
6847 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6849 if (filter
->plist
[direct
].name
)
6850 filter
->plist
[direct
]
6851 .plist
= prefix_list_lookup(
6853 filter
->plist
[direct
]
6856 filter
->plist
[direct
].plist
=
6860 /* If we touch prefix-list, we need to process
6861 * new updates. This is important for ORF to
6862 * work correctly as well.
6864 if (peer
->afc_nego
[afi
][safi
])
6865 peer_on_policy_change(peer
, afi
, safi
,
6869 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6870 FOREACH_AFI_SAFI (afi
, safi
) {
6871 filter
= &group
->conf
->filter
[afi
][safi
];
6873 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6875 if (filter
->plist
[direct
].name
)
6876 filter
->plist
[direct
]
6877 .plist
= prefix_list_lookup(
6879 filter
->plist
[direct
]
6882 filter
->plist
[direct
].plist
=
6890 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6893 struct peer
*member
;
6894 struct bgp_filter
*filter
;
6895 struct listnode
*node
, *nnode
;
6897 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6898 return BGP_ERR_INVALID_VALUE
;
6900 /* Set configuration on peer. */
6901 filter
= &peer
->filter
[afi
][safi
];
6902 if (filter
->aslist
[direct
].name
)
6903 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6904 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6905 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6907 /* Check if handling a regular peer. */
6908 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6909 /* Set override-flag and process peer route updates. */
6910 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6911 PEER_FT_FILTER_LIST
);
6912 peer_on_policy_change(peer
, afi
, safi
,
6913 (direct
== FILTER_OUT
) ? 1 : 0);
6915 /* Skip peer-group mechanics for regular peers. */
6920 * Set configuration on all peer-group members, unless they are
6921 * explicitly overriding peer-group configuration.
6923 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6924 /* Skip peers with overridden configuration. */
6925 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6926 PEER_FT_FILTER_LIST
))
6929 /* Set configuration on peer-group member. */
6930 filter
= &member
->filter
[afi
][safi
];
6931 if (filter
->aslist
[direct
].name
)
6932 XFREE(MTYPE_BGP_FILTER_NAME
,
6933 filter
->aslist
[direct
].name
);
6934 filter
->aslist
[direct
].name
=
6935 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6936 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6938 /* Process peer route updates. */
6939 peer_on_policy_change(member
, afi
, safi
,
6940 (direct
== FILTER_OUT
) ? 1 : 0);
6946 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6948 struct peer
*member
;
6949 struct bgp_filter
*filter
;
6950 struct listnode
*node
, *nnode
;
6952 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6953 return BGP_ERR_INVALID_VALUE
;
6955 /* Unset override-flag unconditionally. */
6956 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6957 PEER_FT_FILTER_LIST
);
6959 /* Inherit configuration from peer-group if peer is member. */
6960 if (peer_group_active(peer
)) {
6961 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6962 filter
[afi
][safi
].aslist
[direct
].name
,
6963 MTYPE_BGP_FILTER_NAME
);
6964 PEER_ATTR_INHERIT(peer
, peer
->group
,
6965 filter
[afi
][safi
].aslist
[direct
].aslist
);
6967 /* Otherwise remove configuration from peer. */
6968 filter
= &peer
->filter
[afi
][safi
];
6969 if (filter
->aslist
[direct
].name
)
6970 XFREE(MTYPE_BGP_FILTER_NAME
,
6971 filter
->aslist
[direct
].name
);
6972 filter
->aslist
[direct
].name
= NULL
;
6973 filter
->aslist
[direct
].aslist
= NULL
;
6976 /* Check if handling a regular peer. */
6977 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6978 /* Process peer route updates. */
6979 peer_on_policy_change(peer
, afi
, safi
,
6980 (direct
== FILTER_OUT
) ? 1 : 0);
6982 /* Skip peer-group mechanics for regular peers. */
6987 * Remove configuration on all peer-group members, unless they are
6988 * explicitly overriding peer-group configuration.
6990 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6991 /* Skip peers with overridden configuration. */
6992 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6993 PEER_FT_FILTER_LIST
))
6996 /* Remove configuration on peer-group member. */
6997 filter
= &member
->filter
[afi
][safi
];
6998 if (filter
->aslist
[direct
].name
)
6999 XFREE(MTYPE_BGP_FILTER_NAME
,
7000 filter
->aslist
[direct
].name
);
7001 filter
->aslist
[direct
].name
= NULL
;
7002 filter
->aslist
[direct
].aslist
= NULL
;
7004 /* Process peer route updates. */
7005 peer_on_policy_change(member
, afi
, safi
,
7006 (direct
== FILTER_OUT
) ? 1 : 0);
7012 static void peer_aslist_update(const char *aslist_name
)
7017 struct listnode
*mnode
, *mnnode
;
7018 struct listnode
*node
, *nnode
;
7021 struct peer_group
*group
;
7022 struct bgp_filter
*filter
;
7024 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
7025 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
7026 aslist_name
, true, 0);
7028 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7029 FOREACH_AFI_SAFI (afi
, safi
) {
7030 filter
= &peer
->filter
[afi
][safi
];
7032 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7034 if (filter
->aslist
[direct
].name
)
7035 filter
->aslist
[direct
]
7036 .aslist
= as_list_lookup(
7037 filter
->aslist
[direct
]
7040 filter
->aslist
[direct
].aslist
=
7045 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
7046 FOREACH_AFI_SAFI (afi
, safi
) {
7047 filter
= &group
->conf
->filter
[afi
][safi
];
7049 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7051 if (filter
->aslist
[direct
].name
)
7052 filter
->aslist
[direct
]
7053 .aslist
= as_list_lookup(
7054 filter
->aslist
[direct
]
7057 filter
->aslist
[direct
].aslist
=
7065 static void peer_aslist_add(char *aslist_name
)
7067 peer_aslist_update(aslist_name
);
7068 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
7071 static void peer_aslist_del(const char *aslist_name
)
7073 peer_aslist_update(aslist_name
);
7074 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
7078 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
7079 const char *name
, struct route_map
*route_map
)
7081 struct peer
*member
;
7082 struct bgp_filter
*filter
;
7083 struct listnode
*node
, *nnode
;
7085 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7086 return BGP_ERR_INVALID_VALUE
;
7088 /* Set configuration on peer. */
7089 filter
= &peer
->filter
[afi
][safi
];
7090 if (filter
->map
[direct
].name
) {
7091 /* If the neighbor is configured with the same route-map
7092 * again then, ignore the duplicate configuration.
7094 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
7097 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7099 route_map_counter_decrement(filter
->map
[direct
].map
);
7100 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7101 filter
->map
[direct
].map
= route_map
;
7102 route_map_counter_increment(route_map
);
7104 /* Check if handling a regular peer. */
7105 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7106 /* Set override-flag and process peer route updates. */
7107 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
7109 peer_on_policy_change(peer
, afi
, safi
,
7110 (direct
== RMAP_OUT
) ? 1 : 0);
7112 /* Skip peer-group mechanics for regular peers. */
7117 * Set configuration on all peer-group members, unless they are
7118 * explicitly overriding peer-group configuration.
7120 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7121 /* Skip peers with overridden configuration. */
7122 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7126 /* Set configuration on peer-group member. */
7127 filter
= &member
->filter
[afi
][safi
];
7128 if (filter
->map
[direct
].name
)
7129 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7130 route_map_counter_decrement(filter
->map
[direct
].map
);
7131 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7132 filter
->map
[direct
].map
= route_map
;
7133 route_map_counter_increment(route_map
);
7135 /* Process peer route updates. */
7136 peer_on_policy_change(member
, afi
, safi
,
7137 (direct
== RMAP_OUT
) ? 1 : 0);
7142 /* Unset route-map from the peer. */
7143 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
7145 struct peer
*member
;
7146 struct bgp_filter
*filter
;
7147 struct listnode
*node
, *nnode
;
7149 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7150 return BGP_ERR_INVALID_VALUE
;
7152 /* Unset override-flag unconditionally. */
7153 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
7155 /* Inherit configuration from peer-group if peer is member. */
7156 if (peer_group_active(peer
)) {
7157 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7158 filter
[afi
][safi
].map
[direct
].name
,
7159 MTYPE_BGP_FILTER_NAME
);
7160 PEER_ATTR_INHERIT(peer
, peer
->group
,
7161 filter
[afi
][safi
].map
[direct
].map
);
7163 /* Otherwise remove configuration from peer. */
7164 filter
= &peer
->filter
[afi
][safi
];
7165 if (filter
->map
[direct
].name
)
7166 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7167 route_map_counter_decrement(filter
->map
[direct
].map
);
7168 filter
->map
[direct
].name
= NULL
;
7169 filter
->map
[direct
].map
= NULL
;
7172 /* Check if handling a regular peer. */
7173 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7174 /* Process peer route updates. */
7175 peer_on_policy_change(peer
, afi
, safi
,
7176 (direct
== RMAP_OUT
) ? 1 : 0);
7178 /* Skip peer-group mechanics for regular peers. */
7183 * Remove configuration on all peer-group members, unless they are
7184 * explicitly overriding peer-group configuration.
7186 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7187 /* Skip peers with overridden configuration. */
7188 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7192 /* Remove configuration on peer-group member. */
7193 filter
= &member
->filter
[afi
][safi
];
7194 if (filter
->map
[direct
].name
)
7195 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7196 route_map_counter_decrement(filter
->map
[direct
].map
);
7197 filter
->map
[direct
].name
= NULL
;
7198 filter
->map
[direct
].map
= NULL
;
7200 /* Process peer route updates. */
7201 peer_on_policy_change(member
, afi
, safi
,
7202 (direct
== RMAP_OUT
) ? 1 : 0);
7208 /* Set unsuppress-map to the peer. */
7209 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7210 const char *name
, struct route_map
*route_map
)
7212 struct peer
*member
;
7213 struct bgp_filter
*filter
;
7214 struct listnode
*node
, *nnode
;
7216 /* Set configuration on peer. */
7217 filter
= &peer
->filter
[afi
][safi
];
7218 if (filter
->usmap
.name
)
7219 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7220 route_map_counter_decrement(filter
->usmap
.map
);
7221 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7222 filter
->usmap
.map
= route_map
;
7223 route_map_counter_increment(route_map
);
7225 /* Check if handling a regular peer. */
7226 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7227 /* Set override-flag and process peer route updates. */
7228 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
7229 PEER_FT_UNSUPPRESS_MAP
);
7230 peer_on_policy_change(peer
, afi
, safi
, 1);
7232 /* Skip peer-group mechanics for regular peers. */
7237 * Set configuration on all peer-group members, unless they are
7238 * explicitly overriding peer-group configuration.
7240 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7241 /* Skip peers with overridden configuration. */
7242 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7243 PEER_FT_UNSUPPRESS_MAP
))
7246 /* Set configuration on peer-group member. */
7247 filter
= &member
->filter
[afi
][safi
];
7248 if (filter
->usmap
.name
)
7249 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7250 route_map_counter_decrement(filter
->usmap
.map
);
7251 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7252 filter
->usmap
.map
= route_map
;
7253 route_map_counter_increment(route_map
);
7255 /* Process peer route updates. */
7256 peer_on_policy_change(member
, afi
, safi
, 1);
7262 /* Unset route-map from the peer. */
7263 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7265 struct peer
*member
;
7266 struct bgp_filter
*filter
;
7267 struct listnode
*node
, *nnode
;
7269 /* Unset override-flag unconditionally. */
7270 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
7272 /* Inherit configuration from peer-group if peer is member. */
7273 if (peer_group_active(peer
)) {
7274 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7275 filter
[afi
][safi
].usmap
.name
,
7276 MTYPE_BGP_FILTER_NAME
);
7277 PEER_ATTR_INHERIT(peer
, peer
->group
,
7278 filter
[afi
][safi
].usmap
.map
);
7280 /* Otherwise remove configuration from peer. */
7281 filter
= &peer
->filter
[afi
][safi
];
7282 if (filter
->usmap
.name
)
7283 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7284 route_map_counter_decrement(filter
->usmap
.map
);
7285 filter
->usmap
.name
= NULL
;
7286 filter
->usmap
.map
= NULL
;
7289 /* Check if handling a regular peer. */
7290 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7291 /* Process peer route updates. */
7292 peer_on_policy_change(peer
, afi
, safi
, 1);
7294 /* Skip peer-group mechanics for regular peers. */
7299 * Remove configuration on all peer-group members, unless they are
7300 * explicitly overriding peer-group configuration.
7302 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7303 /* Skip peers with overridden configuration. */
7304 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7305 PEER_FT_UNSUPPRESS_MAP
))
7308 /* Remove configuration on peer-group member. */
7309 filter
= &member
->filter
[afi
][safi
];
7310 if (filter
->usmap
.name
)
7311 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7312 route_map_counter_decrement(filter
->usmap
.map
);
7313 filter
->usmap
.name
= NULL
;
7314 filter
->usmap
.map
= NULL
;
7316 /* Process peer route updates. */
7317 peer_on_policy_change(member
, afi
, safi
, 1);
7323 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7325 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7328 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7329 if (peer
->t_pmax_restart
) {
7330 THREAD_OFF(peer
->t_pmax_restart
);
7331 if (bgp_debug_neighbor_events(peer
))
7333 "%pBP Maximum-prefix restart timer cancelled",
7336 BGP_EVENT_ADD(peer
, BGP_Start
);
7340 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7341 uint32_t max
, uint8_t threshold
, int warning
,
7342 uint16_t restart
, bool force
)
7344 struct peer
*member
;
7345 struct listnode
*node
, *nnode
;
7347 /* Set flags and configuration on peer. */
7348 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7351 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7353 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7356 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7358 peer_af_flag_unset(peer
, afi
, safi
,
7359 PEER_FLAG_MAX_PREFIX_WARNING
);
7361 peer
->pmax
[afi
][safi
] = max
;
7362 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7363 peer
->pmax_restart
[afi
][safi
] = restart
;
7365 /* Check if handling a regular peer. */
7366 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7367 /* Re-check if peer violates maximum-prefix. */
7368 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7369 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7371 /* Skip peer-group mechanics for regular peers. */
7376 * Set flags and configuration on all peer-group members, unless they
7377 * are explicitly overriding peer-group configuration.
7379 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7380 /* Skip peers with overridden configuration. */
7381 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7382 PEER_FLAG_MAX_PREFIX
))
7385 /* Set flag and configuration on peer-group member. */
7386 member
->pmax
[afi
][safi
] = max
;
7387 member
->pmax_threshold
[afi
][safi
] = threshold
;
7388 member
->pmax_restart
[afi
][safi
] = restart
;
7391 SET_FLAG(member
->af_flags
[afi
][safi
],
7392 PEER_FLAG_MAX_PREFIX_FORCE
);
7394 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7395 PEER_FLAG_MAX_PREFIX_FORCE
);
7398 SET_FLAG(member
->af_flags
[afi
][safi
],
7399 PEER_FLAG_MAX_PREFIX_WARNING
);
7401 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7402 PEER_FLAG_MAX_PREFIX_WARNING
);
7404 /* Re-check if peer violates maximum-prefix. */
7405 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7406 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7412 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7414 /* Inherit configuration from peer-group if peer is member. */
7415 if (peer_group_active(peer
)) {
7416 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7417 peer_af_flag_inherit(peer
, afi
, safi
,
7418 PEER_FLAG_MAX_PREFIX_FORCE
);
7419 peer_af_flag_inherit(peer
, afi
, safi
,
7420 PEER_FLAG_MAX_PREFIX_WARNING
);
7421 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7422 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7423 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7428 /* Remove flags and configuration from peer. */
7429 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7430 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7431 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7432 peer
->pmax
[afi
][safi
] = 0;
7433 peer
->pmax_threshold
[afi
][safi
] = 0;
7434 peer
->pmax_restart
[afi
][safi
] = 0;
7437 * Remove flags and configuration from all peer-group members, unless
7438 * they are explicitly overriding peer-group configuration.
7440 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7441 struct peer
*member
;
7442 struct listnode
*node
;
7444 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7445 /* Skip peers with overridden configuration. */
7446 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7447 PEER_FLAG_MAX_PREFIX
))
7450 /* Remove flag and configuration on peer-group member.
7452 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7453 PEER_FLAG_MAX_PREFIX
);
7454 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7455 PEER_FLAG_MAX_PREFIX_FORCE
);
7456 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7457 PEER_FLAG_MAX_PREFIX_WARNING
);
7458 member
->pmax
[afi
][safi
] = 0;
7459 member
->pmax_threshold
[afi
][safi
] = 0;
7460 member
->pmax_restart
[afi
][safi
] = 0;
7462 peer_maximum_prefix_clear_overflow(member
);
7465 peer_maximum_prefix_clear_overflow(peer
);
7471 void peer_maximum_prefix_out_refresh_routes(struct peer
*peer
, afi_t afi
,
7474 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
7476 if (peer_established(peer
))
7477 bgp_announce_route(peer
, afi
, safi
, false);
7480 int peer_maximum_prefix_out_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7483 struct peer
*member
;
7484 struct listnode
*node
, *nnode
;
7486 /* Set flag on peer and peer-group member if any */
7487 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7488 /* Set configuration on peer. */
7489 peer
->pmax_out
[afi
][safi
] = max
;
7491 /* Check if handling a regular peer. */
7492 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7493 /* Skip peer-group mechanics for regular peers. */
7494 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7499 * Set flag and configuration on all peer-group members, unless they
7500 * are explicitly overriding peer-group configuration.
7502 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7503 /* Skip peers with overridden configuration. */
7504 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7505 PEER_FLAG_MAX_PREFIX_OUT
))
7508 /* Set configuration on peer-group member. */
7509 member
->pmax_out
[afi
][safi
] = max
;
7511 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7516 int peer_maximum_prefix_out_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7518 struct peer
*member
;
7519 struct listnode
*node
;
7520 /* Inherit configuration from peer-group if peer is member. */
7521 if (peer_group_active(peer
)) {
7522 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7523 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_out
[afi
][safi
]);
7525 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7529 /* Remove flag and configuration from peer. */
7530 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7531 peer
->pmax_out
[afi
][safi
] = 0;
7533 /* Check if handling a regular peer. */
7534 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7535 /* Skip peer-group mechanics for regular peers. */
7536 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7541 * Remove flag and configuration from all peer-group members, unless
7542 * they are explicitly overriding peer-group configuration.
7544 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7545 /* Skip peers with overridden configuration. */
7546 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7547 PEER_FLAG_MAX_PREFIX_OUT
))
7550 /* Remove flag and configuration on peer-group member.
7552 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7553 PEER_FLAG_MAX_PREFIX_OUT
);
7554 member
->pmax_out
[afi
][safi
] = 0;
7556 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7561 int is_ebgp_multihop_configured(struct peer
*peer
)
7563 struct peer_group
*group
;
7564 struct listnode
*node
, *nnode
;
7567 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7568 group
= peer
->group
;
7569 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7570 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7573 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7574 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7575 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7579 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7580 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7586 /* Set # of hops between us and BGP peer. */
7587 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7589 struct peer_group
*group
;
7591 struct listnode
*node
, *nnode
;
7594 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7597 /* We cannot configure ttl-security hops when ebgp-multihop is already
7598 set. For non peer-groups, the check is simple. For peer-groups,
7600 slightly messy, because we need to check both the peer-group
7602 and all peer-group members for any trace of ebgp-multihop
7604 before actually applying the ttl-security rules. Cisco really made a
7605 mess of this configuration parameter, and OpenBGPD got it right.
7608 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7609 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7610 if (is_ebgp_multihop_configured(peer
))
7611 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7613 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7614 peer
->gtsm_hops
= gtsm_hops
;
7616 /* Calling ebgp multihop also resets the session.
7617 * On restart, NHT will get setup correctly as will the
7618 * min & max ttls on the socket. The return value is
7621 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7626 group
= peer
->group
;
7627 group
->conf
->gtsm_hops
= gtsm_hops
;
7628 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7630 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7632 /* Calling ebgp multihop also resets the
7634 * On restart, NHT will get setup correctly as
7636 * min & max ttls on the socket. The return
7640 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7644 /* Post the first gtsm setup or if its ibgp, maxttl setting
7646 * necessary, just set the minttl.
7648 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7649 peer
->gtsm_hops
= gtsm_hops
;
7652 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7653 MAXTTL
+ 1 - gtsm_hops
);
7654 if ((peer
->status
< Established
) && peer
->doppelganger
7655 && (peer
->doppelganger
->fd
>= 0))
7656 sockopt_minttl(peer
->su
.sa
.sa_family
,
7657 peer
->doppelganger
->fd
,
7658 MAXTTL
+ 1 - gtsm_hops
);
7660 group
= peer
->group
;
7661 group
->conf
->gtsm_hops
= gtsm_hops
;
7662 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7664 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7666 /* Change setting of existing peer
7667 * established then change value (may break
7669 * not established yet (teardown session and
7671 * no session then do nothing (will get
7672 * handled by next connection)
7676 != BGP_GTSM_HOPS_DISABLED
)
7678 gpeer
->su
.sa
.sa_family
,
7680 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7681 if ((gpeer
->status
< Established
)
7682 && gpeer
->doppelganger
7683 && (gpeer
->doppelganger
->fd
>= 0))
7684 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7685 gpeer
->doppelganger
->fd
,
7686 MAXTTL
+ 1 - gtsm_hops
);
7694 int peer_ttl_security_hops_unset(struct peer
*peer
)
7696 struct peer_group
*group
;
7697 struct listnode
*node
, *nnode
;
7700 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7702 /* if a peer-group member, then reset to peer-group default rather than
7704 if (peer_group_active(peer
))
7705 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7707 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7709 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7710 /* Invoking ebgp_multihop_set will set the TTL back to the
7712 * value as well as restting the NHT and such. The session is
7715 if (peer
->sort
== BGP_PEER_EBGP
)
7716 ret
= peer_ebgp_multihop_unset(peer
);
7719 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7722 if ((peer
->status
< Established
) && peer
->doppelganger
7723 && (peer
->doppelganger
->fd
>= 0))
7724 sockopt_minttl(peer
->su
.sa
.sa_family
,
7725 peer
->doppelganger
->fd
, 0);
7728 group
= peer
->group
;
7729 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7730 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7731 if (peer
->sort
== BGP_PEER_EBGP
)
7732 ret
= peer_ebgp_multihop_unset(peer
);
7735 sockopt_minttl(peer
->su
.sa
.sa_family
,
7738 if ((peer
->status
< Established
)
7739 && peer
->doppelganger
7740 && (peer
->doppelganger
->fd
>= 0))
7741 sockopt_minttl(peer
->su
.sa
.sa_family
,
7742 peer
->doppelganger
->fd
,
7751 static void peer_reset_message_stats(struct peer
*peer
)
7754 atomic_store_explicit(&peer
->open_in
, 0, memory_order_relaxed
);
7755 atomic_store_explicit(&peer
->open_out
, 0, memory_order_relaxed
);
7756 atomic_store_explicit(&peer
->update_in
, 0,
7757 memory_order_relaxed
);
7758 atomic_store_explicit(&peer
->update_out
, 0,
7759 memory_order_relaxed
);
7760 atomic_store_explicit(&peer
->keepalive_in
, 0,
7761 memory_order_relaxed
);
7762 atomic_store_explicit(&peer
->keepalive_out
, 0,
7763 memory_order_relaxed
);
7764 atomic_store_explicit(&peer
->notify_in
, 0,
7765 memory_order_relaxed
);
7766 atomic_store_explicit(&peer
->notify_out
, 0,
7767 memory_order_relaxed
);
7768 atomic_store_explicit(&peer
->refresh_in
, 0,
7769 memory_order_relaxed
);
7770 atomic_store_explicit(&peer
->refresh_out
, 0,
7771 memory_order_relaxed
);
7772 atomic_store_explicit(&peer
->dynamic_cap_in
, 0,
7773 memory_order_relaxed
);
7774 atomic_store_explicit(&peer
->dynamic_cap_out
, 0,
7775 memory_order_relaxed
);
7780 * If peer clear is invoked in a loop for all peers on the BGP instance,
7781 * it may end up freeing the doppelganger, and if this was the next node
7782 * to the current node, we would end up accessing the freed next node.
7783 * Pass along additional parameter which can be updated if next node
7784 * is freed; only required when walking the peer list on BGP instance.
7786 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7788 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7789 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7790 if (peer_maximum_prefix_clear_overflow(peer
))
7793 peer
->v_start
= BGP_INIT_START_TIMER
;
7794 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7795 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7796 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7798 bgp_session_reset_safe(peer
, nnode
);
7803 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7804 enum bgp_clear_type stype
)
7806 struct peer_af
*paf
;
7808 if (!peer_established(peer
))
7811 if (!peer
->afc
[afi
][safi
])
7812 return BGP_ERR_AF_UNCONFIGURED
;
7814 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7816 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7817 /* Clear the "neighbor x.x.x.x default-originate" flag */
7818 paf
= peer_af_find(peer
, afi
, safi
);
7819 if (paf
&& paf
->subgroup
7820 && CHECK_FLAG(paf
->subgroup
->sflags
,
7821 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7822 UNSET_FLAG(paf
->subgroup
->sflags
,
7823 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7825 bgp_announce_route(peer
, afi
, safi
, false);
7828 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7829 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7830 PEER_CAP_ORF_PREFIX_SM_ADV
)
7831 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7832 PEER_CAP_ORF_PREFIX_RM_RCV
)
7833 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7834 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7835 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7836 uint8_t prefix_type
;
7838 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7839 PEER_CAP_ORF_PREFIX_RM_RCV
))
7840 prefix_type
= ORF_TYPE_PREFIX
;
7842 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7844 if (filter
->plist
[FILTER_IN
].plist
) {
7845 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7846 PEER_STATUS_ORF_PREFIX_SEND
))
7847 bgp_route_refresh_send(
7848 peer
, afi
, safi
, prefix_type
,
7850 BGP_ROUTE_REFRESH_NORMAL
);
7851 bgp_route_refresh_send(
7852 peer
, afi
, safi
, prefix_type
,
7853 REFRESH_IMMEDIATE
, 0,
7854 BGP_ROUTE_REFRESH_NORMAL
);
7856 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7857 PEER_STATUS_ORF_PREFIX_SEND
))
7858 bgp_route_refresh_send(
7859 peer
, afi
, safi
, prefix_type
,
7860 REFRESH_IMMEDIATE
, 1,
7861 BGP_ROUTE_REFRESH_NORMAL
);
7863 bgp_route_refresh_send(
7864 peer
, afi
, safi
, 0, 0, 0,
7865 BGP_ROUTE_REFRESH_NORMAL
);
7871 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7872 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7873 /* If neighbor has soft reconfiguration inbound flag.
7874 Use Adj-RIB-In database. */
7875 if (!bgp_soft_reconfig_in(peer
, afi
, safi
)) {
7876 /* If neighbor has route refresh capability, send route
7878 message to the peer. */
7879 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7880 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7881 bgp_route_refresh_send(
7882 peer
, afi
, safi
, 0, 0, 0,
7883 BGP_ROUTE_REFRESH_NORMAL
);
7885 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7889 if (stype
== BGP_CLEAR_MESSAGE_STATS
)
7890 peer_reset_message_stats(peer
);
7895 /* Display peer uptime.*/
7896 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7899 time_t uptime1
, epoch_tbuf
;
7902 /* If there is no connection has been done before print `never'. */
7905 json_object_string_add(json
, "peerUptime", "never");
7906 json_object_int_add(json
, "peerUptimeMsec", 0);
7908 snprintf(buf
, len
, "never");
7912 /* Get current time. */
7913 uptime1
= monotime(NULL
);
7915 gmtime_r(&uptime1
, &tm
);
7917 if (uptime1
< ONE_DAY_SECOND
)
7918 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7920 else if (uptime1
< ONE_WEEK_SECOND
)
7921 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7923 else if (uptime1
< ONE_YEAR_SECOND
)
7924 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7925 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7927 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7929 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7932 epoch_tbuf
= time(NULL
) - uptime1
;
7933 json_object_string_add(json
, "peerUptime", buf
);
7934 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7935 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7942 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7943 struct list
*addresses
)
7947 memset(&bgp_master
, 0, sizeof(bgp_master
));
7950 bm
->bgp
= list_new();
7951 bm
->listen_sockets
= list_new();
7952 bm
->port
= BGP_PORT_DEFAULT
;
7953 bm
->addresses
= addresses
;
7954 bm
->master
= master
;
7955 bm
->start_time
= monotime(NULL
);
7956 bm
->t_rmap_update
= NULL
;
7957 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7958 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7959 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7960 bm
->terminating
= false;
7961 bm
->socket_buffer
= buffer_size
;
7962 bm
->wait_for_fib
= false;
7963 bm
->tcp_dscp
= IPTOS_PREC_INTERNETCONTROL
;
7964 bm
->inq_limit
= BM_DEFAULT_Q_LIMIT
;
7965 bm
->outq_limit
= BM_DEFAULT_Q_LIMIT
;
7968 /* init the rd id space.
7969 assign 0th index in the bitfield,
7970 so that we start with id 1
7972 bf_init(bm
->rd_idspace
, UINT16_MAX
);
7973 bf_assign_zero_index(bm
->rd_idspace
);
7975 /* mpls label dynamic allocation pool */
7976 bgp_lp_init(bm
->master
, &bm
->labelpool
);
7980 QOBJ_REG(bm
, bgp_master
);
7984 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7985 * instance delete (non-default only) or BGP exit.
7987 static void bgp_if_finish(struct bgp
*bgp
)
7990 struct interface
*ifp
;
7992 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
7994 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
7997 FOR_ALL_INTERFACES (vrf
, ifp
) {
7998 struct listnode
*c_node
, *c_nnode
;
7999 struct connected
*c
;
8001 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
8002 bgp_connected_delete(bgp
, c
);
8006 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
8008 struct vrf
*vrf
= NULL
;
8009 struct listnode
*next
;
8012 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
8013 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
8015 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8016 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
8019 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
8023 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
8025 struct listnode
*next
, *next2
;
8026 struct bgp
*bgp
, *bgp2
;
8029 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8031 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
8032 if (bgp2
->as
== bgp
->as
)
8040 snprintf(buf
, sizeof(buf
), "%u", bgp
->as
);
8041 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
8045 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
8046 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
8047 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
8048 {.completions
= NULL
},
8051 struct frr_pthread
*bgp_pth_io
;
8052 struct frr_pthread
*bgp_pth_ka
;
8054 static void bgp_pthreads_init(void)
8056 assert(!bgp_pth_io
);
8057 assert(!bgp_pth_ka
);
8059 struct frr_pthread_attr io
= {
8060 .start
= frr_pthread_attr_default
.start
,
8061 .stop
= frr_pthread_attr_default
.stop
,
8063 struct frr_pthread_attr ka
= {
8064 .start
= bgp_keepalives_start
,
8065 .stop
= bgp_keepalives_stop
,
8067 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
8068 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
8071 void bgp_pthreads_run(void)
8073 frr_pthread_run(bgp_pth_io
, NULL
);
8074 frr_pthread_run(bgp_pth_ka
, NULL
);
8076 /* Wait until threads are ready. */
8077 frr_pthread_wait_running(bgp_pth_io
);
8078 frr_pthread_wait_running(bgp_pth_ka
);
8081 void bgp_pthreads_finish(void)
8083 frr_pthread_stop_all();
8086 static int peer_unshut_after_cfg(struct bgp
*bgp
)
8088 struct listnode
*node
;
8091 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
8092 if (!peer
->shut_during_cfg
)
8095 if (bgp_debug_neighbor_events(peer
))
8096 zlog_debug("%s: released from config-pending hold",
8099 peer
->shut_during_cfg
= false;
8100 if (peer_active(peer
) && peer
->status
!= Established
) {
8101 if (peer
->status
!= Idle
)
8102 BGP_EVENT_ADD(peer
, BGP_Stop
);
8103 BGP_EVENT_ADD(peer
, BGP_Start
);
8110 void bgp_init(unsigned short instance
)
8112 hook_register(bgp_config_end
, peer_unshut_after_cfg
);
8114 /* allocates some vital data structures used by peer commands in
8117 /* pre-init pthreads */
8118 bgp_pthreads_init();
8121 bgp_zebra_init(bm
->master
, instance
);
8123 #ifdef ENABLE_BGP_VNC
8124 vnc_zebra_init(bm
->master
);
8127 /* BGP VTY commands installation. */
8133 bgp_community_alias_init();
8136 bgp_route_map_init();
8137 bgp_scan_vty_init();
8139 #ifdef ENABLE_BGP_VNC
8142 bgp_ethernetvpn_init();
8143 bgp_flowspec_vty_init();
8145 /* Access list initialize. */
8147 access_list_add_hook(peer_distribute_update
);
8148 access_list_delete_hook(peer_distribute_update
);
8150 /* Filter list initialize. */
8152 as_list_add_hook(peer_aslist_add
);
8153 as_list_delete_hook(peer_aslist_del
);
8155 /* Prefix list initialize.*/
8157 prefix_list_add_hook(peer_prefix_list_update
);
8158 prefix_list_delete_hook(peer_prefix_list_update
);
8160 /* Community list initialize. */
8161 bgp_clist
= community_list_init();
8164 bgp_bfd_init(bm
->master
);
8168 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
8171 void bgp_terminate(void)
8175 struct listnode
*node
, *nnode
;
8176 struct listnode
*mnode
, *mnnode
;
8180 /* Close the listener sockets first as this prevents peers from
8182 * to reconnect on receiving the peer unconfig message. In the presence
8183 * of a large number of peers this will ensure that no peer is left with
8184 * a dangling connection
8188 /* reverse bgp_master_init */
8189 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
8190 bgp_close_vrf_socket(bgp
);
8191 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8192 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
8193 if (bgp_debug_neighbor_events(peer
))
8195 "%pBP configured Graceful-Restart, skipping unconfig notification",
8199 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
8200 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
8201 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
8205 if (bm
->listen_sockets
)
8206 list_delete(&bm
->listen_sockets
);
8208 THREAD_OFF(bm
->t_rmap_update
);
8213 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
8214 const char *ip_str
, bool use_json
)
8220 /* Get peer sockunion. */
8221 ret
= str2sockunion(ip_str
, &su
);
8223 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
8225 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
8229 json_object
*json_no
= NULL
;
8230 json_no
= json_object_new_object();
8231 json_object_string_add(
8233 "malformedAddressOrName",
8235 vty_json(vty
, json_no
);
8238 "%% Malformed address or name: %s\n",
8246 /* Peer structure lookup. */
8247 peer
= peer_lookup(bgp
, &su
);
8250 json_object
*json_no
= NULL
;
8251 json_no
= json_object_new_object();
8252 json_object_string_add(json_no
, "warning",
8253 "No such neighbor in this view/vrf");
8254 vty_json(vty
, json_no
);
8256 vty_out(vty
, "No such neighbor in this view/vrf\n");
8263 void bgp_gr_apply_running_config(void)
8265 struct peer
*peer
= NULL
;
8266 struct bgp
*bgp
= NULL
;
8267 struct listnode
*node
, *nnode
;
8268 bool gr_router_detected
= false;
8270 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
8271 zlog_debug("[BGP_GR] %s called !", __func__
);
8273 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
8274 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8275 bgp_peer_gr_flags_update(peer
);
8276 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
8277 gr_router_detected
= true;
8280 if (gr_router_detected
8281 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
8282 bgp_zebra_send_capabilities(bgp
, true);
8283 } else if (!gr_router_detected
8284 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
8285 bgp_zebra_send_capabilities(bgp
, false);
8288 gr_router_detected
= false;
8292 printfrr_ext_autoreg_p("BP", printfrr_bp
);
8293 static ssize_t
printfrr_bp(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
8296 const struct peer
*peer
= ptr
;
8299 return bputs(buf
, "(null)");
8301 return bprintfrr(buf
, "%s(%s)", peer
->host
,
8302 peer
->hostname
? peer
->hostname
: "Unknown");