1 /* BGP-4, BGP-4+ daemon program
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "sockunion.h"
38 #include "workqueue.h"
46 #include "lib/sockopt.h"
47 #include "frr_pthread.h"
50 #include "bgpd/bgpd.h"
51 #include "bgpd/bgp_table.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_route.h"
54 #include "bgpd/bgp_dump.h"
55 #include "bgpd/bgp_debug.h"
56 #include "bgpd/bgp_errors.h"
57 #include "bgpd/bgp_community.h"
58 #include "bgpd/bgp_community_alias.h"
59 #include "bgpd/bgp_conditional_adv.h"
60 #include "bgpd/bgp_attr.h"
61 #include "bgpd/bgp_regex.h"
62 #include "bgpd/bgp_clist.h"
63 #include "bgpd/bgp_fsm.h"
64 #include "bgpd/bgp_packet.h"
65 #include "bgpd/bgp_zebra.h"
66 #include "bgpd/bgp_open.h"
67 #include "bgpd/bgp_filter.h"
68 #include "bgpd/bgp_nexthop.h"
69 #include "bgpd/bgp_damp.h"
70 #include "bgpd/bgp_mplsvpn.h"
72 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
73 #include "bgpd/rfapi/rfapi_backend.h"
75 #include "bgpd/bgp_evpn.h"
76 #include "bgpd/bgp_advertise.h"
77 #include "bgpd/bgp_network.h"
78 #include "bgpd/bgp_vty.h"
79 #include "bgpd/bgp_mpath.h"
80 #include "bgpd/bgp_nht.h"
81 #include "bgpd/bgp_updgrp.h"
82 #include "bgpd/bgp_bfd.h"
83 #include "bgpd/bgp_memory.h"
84 #include "bgpd/bgp_evpn_vty.h"
85 #include "bgpd/bgp_keepalives.h"
86 #include "bgpd/bgp_io.h"
87 #include "bgpd/bgp_ecommunity.h"
88 #include "bgpd/bgp_flowspec.h"
89 #include "bgpd/bgp_labelpool.h"
90 #include "bgpd/bgp_pbr.h"
91 #include "bgpd/bgp_addpath.h"
92 #include "bgpd/bgp_evpn_private.h"
93 #include "bgpd/bgp_evpn_mh.h"
94 #include "bgpd/bgp_mac.h"
96 DEFINE_MTYPE_STATIC(BGPD
, PEER_TX_SHUTDOWN_MSG
, "Peer shutdown message (TX)");
97 DEFINE_MTYPE_STATIC(BGPD
, BGP_EVPN_INFO
, "BGP EVPN instance information");
98 DEFINE_QOBJ_TYPE(bgp_master
);
99 DEFINE_QOBJ_TYPE(bgp
);
100 DEFINE_QOBJ_TYPE(peer
);
101 DEFINE_HOOK(bgp_inst_delete
, (struct bgp
*bgp
), (bgp
));
103 /* BGP process wide configuration. */
104 static struct bgp_master bgp_master
;
106 /* BGP process wide configuration pointer to export. */
107 struct bgp_master
*bm
;
109 /* BGP community-list. */
110 struct community_list_handler
*bgp_clist
;
112 unsigned int multipath_num
= MULTIPATH_NUM
;
114 /* Number of bgp instances configured for suppress fib config */
115 unsigned int bgp_suppress_fib_count
;
117 static void bgp_if_finish(struct bgp
*bgp
);
118 static void peer_drop_dynamic_neighbor(struct peer
*peer
);
120 extern struct zclient
*zclient
;
122 /* handle main socket creation or deletion */
123 static int bgp_check_main_socket(bool create
, struct bgp
*bgp
)
125 static int bgp_server_main_created
;
126 struct listnode
*node
;
130 if (bgp_server_main_created
)
132 if (list_isempty(bm
->addresses
)) {
133 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
134 return BGP_ERR_INVALID_VALUE
;
136 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
137 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
138 return BGP_ERR_INVALID_VALUE
;
140 bgp_server_main_created
= 1;
143 if (!bgp_server_main_created
)
146 bgp_server_main_created
= 0;
150 void bgp_session_reset(struct peer
*peer
)
152 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
153 && !(CHECK_FLAG(peer
->doppelganger
->flags
, PEER_FLAG_CONFIG_NODE
)))
154 peer_delete(peer
->doppelganger
);
156 BGP_EVENT_ADD(peer
, BGP_Stop
);
160 * During session reset, we may delete the doppelganger peer, which would
161 * be the next node to the current node. If the session reset was invoked
162 * during walk of peer list, we would end up accessing the freed next
163 * node. This function moves the next node along.
165 static void bgp_session_reset_safe(struct peer
*peer
, struct listnode
**nnode
)
170 n
= (nnode
) ? *nnode
: NULL
;
171 npeer
= (n
) ? listgetdata(n
) : NULL
;
173 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
174 && !(CHECK_FLAG(peer
->doppelganger
->flags
,
175 PEER_FLAG_CONFIG_NODE
))) {
176 if (peer
->doppelganger
== npeer
)
177 /* nnode and *nnode are confirmed to be non-NULL here */
178 *nnode
= (*nnode
)->next
;
179 peer_delete(peer
->doppelganger
);
182 BGP_EVENT_ADD(peer
, BGP_Stop
);
185 /* BGP global flag manipulation. */
186 int bgp_option_set(int flag
)
190 case BGP_OPT_NO_LISTEN
:
191 case BGP_OPT_NO_ZEBRA
:
192 SET_FLAG(bm
->options
, flag
);
195 return BGP_ERR_INVALID_FLAG
;
200 int bgp_option_unset(int flag
)
204 case BGP_OPT_NO_ZEBRA
:
206 UNSET_FLAG(bm
->options
, flag
);
209 return BGP_ERR_INVALID_FLAG
;
214 int bgp_option_check(int flag
)
216 return CHECK_FLAG(bm
->options
, flag
);
219 /* set the bgp no-rib option during runtime and remove installed routes */
220 void bgp_option_norib_set_runtime(void)
223 struct listnode
*node
;
227 if (bgp_option_check(BGP_OPT_NO_FIB
))
230 bgp_option_set(BGP_OPT_NO_FIB
);
232 zlog_info("Disabled BGP route installation to RIB (Zebra)");
234 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
235 FOREACH_AFI_SAFI (afi
, safi
) {
237 * Stop a crash, more work is needed
238 * here to properly add/remove these types of
241 if (!bgp_fibupd_safi(safi
))
244 bgp_zebra_withdraw_table_all_subtypes(bgp
, afi
, safi
);
248 zlog_info("All routes have been withdrawn from RIB (Zebra)");
251 /* unset the bgp no-rib option during runtime and announce routes to Zebra */
252 void bgp_option_norib_unset_runtime(void)
255 struct listnode
*node
;
259 if (!bgp_option_check(BGP_OPT_NO_FIB
))
262 bgp_option_unset(BGP_OPT_NO_FIB
);
264 zlog_info("Enabled BGP route installation to RIB (Zebra)");
266 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
267 FOREACH_AFI_SAFI (afi
, safi
) {
269 * Stop a crash, more work is needed
270 * here to properly add/remove these types
271 * of routes from zebra
273 if (!bgp_fibupd_safi(safi
))
276 bgp_zebra_announce_table_all_subtypes(bgp
, afi
, safi
);
280 zlog_info("All routes have been installed in RIB (Zebra)");
283 /* Internal function to set BGP structure configureation flag. */
284 static void bgp_config_set(struct bgp
*bgp
, int config
)
286 SET_FLAG(bgp
->config
, config
);
289 static void bgp_config_unset(struct bgp
*bgp
, int config
)
291 UNSET_FLAG(bgp
->config
, config
);
294 static int bgp_config_check(struct bgp
*bgp
, int config
)
296 return CHECK_FLAG(bgp
->config
, config
);
299 /* Set BGP router identifier; distinguish between explicit config and other
302 static int bgp_router_id_set(struct bgp
*bgp
, const struct in_addr
*id
,
306 struct listnode
*node
, *nnode
;
308 if (IPV4_ADDR_SAME(&bgp
->router_id
, id
))
311 /* EVPN uses router id in RD, withdraw them */
312 if (is_evpn_enabled())
313 bgp_evpn_handle_router_id_update(bgp
, true);
315 vpn_handle_router_id_update(bgp
, true, is_config
);
317 IPV4_ADDR_COPY(&bgp
->router_id
, id
);
319 /* Set all peer's local identifier with this value. */
320 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
321 IPV4_ADDR_COPY(&peer
->local_id
, id
);
323 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
324 peer
->last_reset
= PEER_DOWN_RID_CHANGE
;
325 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
326 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
330 /* EVPN uses router id in RD, update them */
331 if (is_evpn_enabled())
332 bgp_evpn_handle_router_id_update(bgp
, false);
334 vpn_handle_router_id_update(bgp
, false, is_config
);
339 void bgp_router_id_zebra_bump(vrf_id_t vrf_id
, const struct prefix
*router_id
)
341 struct listnode
*node
, *nnode
;
343 struct in_addr
*addr
= NULL
;
345 if (router_id
!= NULL
)
346 addr
= (struct in_addr
*)&(router_id
->u
.prefix4
);
348 if (vrf_id
== VRF_DEFAULT
) {
349 /* Router-id change for default VRF has to also update all
351 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
352 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
356 bgp
->router_id_zebra
= *addr
;
358 addr
= &bgp
->router_id_zebra
;
360 if (!bgp
->router_id_static
.s_addr
) {
361 /* Router ID is updated if there are no active
364 if (bgp
->established_peers
== 0) {
365 if (BGP_DEBUG(zebra
, ZEBRA
))
367 "RID change : vrf %s(%u), RTR ID %pI4",
371 * if old router-id was 0x0, set flag
372 * to use this new value
374 bgp_router_id_set(bgp
, addr
,
375 (bgp
->router_id
.s_addr
383 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
386 bgp
->router_id_zebra
= *addr
;
388 addr
= &bgp
->router_id_zebra
;
390 if (!bgp
->router_id_static
.s_addr
) {
391 /* Router ID is updated if there are no active
394 if (bgp
->established_peers
== 0) {
395 if (BGP_DEBUG(zebra
, ZEBRA
))
397 "RID change : vrf %s(%u), RTR ID %pI4",
401 * if old router-id was 0x0, set flag
402 * to use this new value
404 bgp_router_id_set(bgp
, addr
,
405 (bgp
->router_id
.s_addr
416 void bgp_router_id_static_set(struct bgp
*bgp
, struct in_addr id
)
418 bgp
->router_id_static
= id
;
419 bgp_router_id_set(bgp
,
420 id
.s_addr
!= INADDR_ANY
? &id
: &bgp
->router_id_zebra
,
421 true /* is config */);
424 void bm_wait_for_fib_set(bool set
)
426 bool send_msg
= false;
428 if (bm
->wait_for_fib
== set
)
431 bm
->wait_for_fib
= set
;
433 if (bgp_suppress_fib_count
== 0)
435 bgp_suppress_fib_count
++;
437 bgp_suppress_fib_count
--;
438 if (bgp_suppress_fib_count
== 0)
442 if (send_msg
&& zclient
)
443 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
447 /* Set the suppress fib pending for the bgp configuration */
448 void bgp_suppress_fib_pending_set(struct bgp
*bgp
, bool set
)
450 bool send_msg
= false;
452 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
456 SET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
457 /* Send msg to zebra for the first instance of bgp enabled
460 if (bgp_suppress_fib_count
== 0)
462 bgp_suppress_fib_count
++;
464 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
465 bgp_suppress_fib_count
--;
467 /* Send msg to zebra if there are no instances enabled
470 if (bgp_suppress_fib_count
== 0)
473 /* Send route notify request to RIB */
475 if (BGP_DEBUG(zebra
, ZEBRA
))
476 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
479 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
484 /* BGP's cluster-id control. */
485 void bgp_cluster_id_set(struct bgp
*bgp
, struct in_addr
*cluster_id
)
488 struct listnode
*node
, *nnode
;
490 if (bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
)
491 && IPV4_ADDR_SAME(&bgp
->cluster_id
, cluster_id
))
494 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
495 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
497 /* Clear all IBGP peer. */
498 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
499 if (peer
->sort
!= BGP_PEER_IBGP
)
502 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
503 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
504 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
505 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
510 void bgp_cluster_id_unset(struct bgp
*bgp
)
513 struct listnode
*node
, *nnode
;
515 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
518 bgp
->cluster_id
.s_addr
= 0;
519 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
521 /* Clear all IBGP peer. */
522 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
523 if (peer
->sort
!= BGP_PEER_IBGP
)
526 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
527 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
528 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
529 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
534 /* BGP timer configuration. */
535 void bgp_timers_set(struct bgp
*bgp
, uint32_t keepalive
, uint32_t holdtime
,
536 uint32_t connect_retry
, uint32_t delayopen
)
538 bgp
->default_keepalive
=
539 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
540 bgp
->default_holdtime
= holdtime
;
541 bgp
->default_connect_retry
= connect_retry
;
542 bgp
->default_delayopen
= delayopen
;
545 /* mostly for completeness - CLI uses its own defaults */
546 void bgp_timers_unset(struct bgp
*bgp
)
548 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
549 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
550 bgp
->default_connect_retry
= BGP_DEFAULT_CONNECT_RETRY
;
551 bgp
->default_delayopen
= BGP_DEFAULT_DELAYOPEN
;
554 /* BGP confederation configuration. */
555 void bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
558 struct listnode
*node
, *nnode
;
564 /* Remember - were we doing confederation before? */
565 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
567 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
569 /* If we were doing confederation already, this is just an external
570 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
571 were not doing confederation before, reset all EBGP sessions. */
572 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
573 enum bgp_peer_sort ptype
= peer_sort(peer
);
575 /* We're looking for peers who's AS is not local or part of our
577 if (already_confed
) {
578 if (ptype
== BGP_PEER_EBGP
) {
580 if (BGP_IS_VALID_STATE_FOR_NOTIF(
583 PEER_DOWN_CONFED_ID_CHANGE
;
585 peer
, BGP_NOTIFY_CEASE
,
586 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
588 bgp_session_reset_safe(peer
, &nnode
);
591 /* Not doign confederation before, so reset every
594 if (ptype
!= BGP_PEER_IBGP
) {
595 /* Reset the local_as to be our EBGP one */
596 if (ptype
== BGP_PEER_EBGP
)
598 if (BGP_IS_VALID_STATE_FOR_NOTIF(
601 PEER_DOWN_CONFED_ID_CHANGE
;
603 peer
, BGP_NOTIFY_CEASE
,
604 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
606 bgp_session_reset_safe(peer
, &nnode
);
613 void bgp_confederation_id_unset(struct bgp
*bgp
)
616 struct listnode
*node
, *nnode
;
619 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
621 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
622 /* We're looking for peers who's AS is not local */
623 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
624 peer
->local_as
= bgp
->as
;
625 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
626 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
627 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
628 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
632 bgp_session_reset_safe(peer
, &nnode
);
637 /* Is an AS part of the confed or not? */
638 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
645 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
646 if (bgp
->confed_peers
[i
] == as
)
652 /* Add an AS to the confederation set. */
653 void bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
656 struct listnode
*node
, *nnode
;
661 if (bgp_confederation_peers_check(bgp
, as
))
665 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
666 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
668 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
669 bgp
->confed_peers_cnt
++;
671 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
672 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
673 if (peer
->as
== as
) {
674 (void)peer_sort(peer
);
675 peer
->local_as
= bgp
->as
;
676 if (BGP_IS_VALID_STATE_FOR_NOTIF(
679 PEER_DOWN_CONFED_PEER_CHANGE
;
681 peer
, BGP_NOTIFY_CEASE
,
682 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
684 bgp_session_reset_safe(peer
, &nnode
);
690 /* Delete an AS from the confederation set. */
691 void bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
696 struct listnode
*node
, *nnode
;
701 if (!bgp_confederation_peers_check(bgp
, as
))
704 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
705 if (bgp
->confed_peers
[i
] == as
)
706 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
707 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
709 bgp
->confed_peers_cnt
--;
711 if (bgp
->confed_peers_cnt
== 0) {
712 if (bgp
->confed_peers
)
713 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
714 bgp
->confed_peers
= NULL
;
717 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
718 bgp
->confed_peers_cnt
* sizeof(as_t
));
720 /* Now reset any peer who's remote AS has just been removed from the
722 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
723 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
724 if (peer
->as
== as
) {
725 (void)peer_sort(peer
);
726 peer
->local_as
= bgp
->confed_id
;
727 if (BGP_IS_VALID_STATE_FOR_NOTIF(
730 PEER_DOWN_CONFED_PEER_CHANGE
;
732 peer
, BGP_NOTIFY_CEASE
,
733 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
735 bgp_session_reset_safe(peer
, &nnode
);
741 /* Local preference configuration. */
742 void bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
747 bgp
->default_local_pref
= local_pref
;
750 void bgp_default_local_preference_unset(struct bgp
*bgp
)
755 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
758 /* Local preference configuration. */
759 void bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
,
765 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
768 void bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
772 bgp
->default_subgroup_pkt_queue_max
=
773 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
776 /* Listen limit configuration. */
777 void bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
782 bgp
->dynamic_neighbors_limit
= listen_limit
;
785 void bgp_listen_limit_unset(struct bgp
*bgp
)
790 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
793 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
794 afi_t
*afi
, safi_t
*safi
)
796 /* Map from IANA values to internal values, return error if
797 * values are unrecognized.
799 *afi
= afi_iana2int(pkt_afi
);
800 *safi
= safi_iana2int(pkt_safi
);
801 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
807 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
808 iana_safi_t
*pkt_safi
)
810 /* Map from internal values to IANA values, return error if
811 * internal values are bad (unexpected).
813 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
815 *pkt_afi
= afi_int2iana(afi
);
816 *pkt_safi
= safi_int2iana(safi
);
820 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
829 afid
= afindex(afi
, safi
);
830 if (afid
>= BGP_AF_MAX
)
834 assert(peer
->peer_af_array
[afid
] == NULL
);
836 /* Allocate new peer af */
837 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
839 peer
->peer_af_array
[afid
] = af
;
844 bgp
->af_peer_count
[afi
][safi
]++;
849 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
856 afid
= afindex(afi
, safi
);
857 if (afid
>= BGP_AF_MAX
)
860 return peer
->peer_af_array
[afid
];
863 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
872 afid
= afindex(afi
, safi
);
873 if (afid
>= BGP_AF_MAX
)
876 af
= peer
->peer_af_array
[afid
];
881 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
883 bgp_stop_announce_route_timer(af
);
885 if (PAF_SUBGRP(af
)) {
886 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
887 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
888 af
->subgroup
->update_group
->id
,
889 af
->subgroup
->id
, peer
->host
);
893 update_subgroup_remove_peer(af
->subgroup
, af
);
895 if (bgp
->af_peer_count
[afi
][safi
])
896 bgp
->af_peer_count
[afi
][safi
]--;
898 peer
->peer_af_array
[afid
] = NULL
;
899 XFREE(MTYPE_BGP_PEER_AF
, af
);
903 /* Peer comparison function for sorting. */
904 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
906 if (p1
->group
&& !p2
->group
)
909 if (!p1
->group
&& p2
->group
)
912 if (p1
->group
== p2
->group
) {
913 if (p1
->conf_if
&& !p2
->conf_if
)
916 if (!p1
->conf_if
&& p2
->conf_if
)
919 if (p1
->conf_if
&& p2
->conf_if
)
920 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
922 return strcmp(p1
->group
->name
, p2
->group
->name
);
924 return sockunion_cmp(&p1
->su
, &p2
->su
);
927 static unsigned int peer_hash_key_make(const void *p
)
929 const struct peer
*peer
= p
;
930 return sockunion_hash(&peer
->su
);
933 static bool peer_hash_same(const void *p1
, const void *p2
)
935 const struct peer
*peer1
= p1
;
936 const struct peer
*peer2
= p2
;
937 return (sockunion_same(&peer1
->su
, &peer2
->su
)
938 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
939 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
942 void peer_flag_inherit(struct peer
*peer
, uint64_t flag
)
946 /* Skip if peer is not a peer-group member. */
947 if (!peer_group_active(peer
))
950 /* Unset override flag to signal inheritance from peer-group. */
951 UNSET_FLAG(peer
->flags_override
, flag
);
954 * Inherit flag state from peer-group. If the flag of the peer-group is
955 * not being inverted, the peer must inherit the inverse of the current
956 * peer-group flag state.
958 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
959 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
960 && CHECK_FLAG(peer
->flags_invert
, flag
))
961 COND_FLAG(peer
->flags
, flag
, !group_val
);
963 COND_FLAG(peer
->flags
, flag
, group_val
);
966 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
968 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
971 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
976 /* Skip if peer is not a peer-group member. */
977 if (!peer_group_active(peer
))
980 /* Unset override flag to signal inheritance from peer-group. */
981 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
984 * Inherit flag state from peer-group. If the flag of the peer-group is
985 * not being inverted, the peer must inherit the inverse of the current
986 * peer-group flag state.
988 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
989 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
990 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
991 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
993 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
996 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
997 static inline enum bgp_peer_sort
peer_calc_sort(struct peer
*peer
)
1004 if (peer
->change_local_as
)
1005 local_as
= peer
->change_local_as
;
1007 local_as
= peer
->local_as
;
1010 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1011 if (peer
->as_type
== AS_INTERNAL
)
1012 return BGP_PEER_IBGP
;
1014 else if (peer
->as_type
== AS_EXTERNAL
)
1015 return BGP_PEER_EBGP
;
1017 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1019 return (local_as
== peer
->as
? BGP_PEER_IBGP
1026 assert(peer
->group
);
1027 peer1
= listnode_head(peer
->group
->peer
);
1032 return BGP_PEER_INTERNAL
;
1036 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1038 return BGP_PEER_INTERNAL
;
1040 if (local_as
== peer
->as
) {
1041 if (bgp
->as
== bgp
->confed_id
) {
1042 if (local_as
== bgp
->as
)
1043 return BGP_PEER_IBGP
;
1045 return BGP_PEER_EBGP
;
1047 if (local_as
== bgp
->confed_id
)
1048 return BGP_PEER_EBGP
;
1050 return BGP_PEER_IBGP
;
1054 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1055 return BGP_PEER_CONFED
;
1057 return BGP_PEER_EBGP
;
1059 if (peer
->as_type
== AS_UNSPECIFIED
) {
1060 /* check if in peer-group with AS information */
1062 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1063 if (peer
->group
->conf
->as_type
1065 if (local_as
== peer
->group
->conf
->as
)
1066 return BGP_PEER_IBGP
;
1068 return BGP_PEER_EBGP
;
1069 } else if (peer
->group
->conf
->as_type
1071 return BGP_PEER_IBGP
;
1073 return BGP_PEER_EBGP
;
1075 /* no AS information anywhere, let caller know */
1076 return BGP_PEER_UNSPECIFIED
;
1077 } else if (peer
->as_type
!= AS_SPECIFIED
)
1078 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1081 return (local_as
== 0 ? BGP_PEER_INTERNAL
1082 : local_as
== peer
->as
? BGP_PEER_IBGP
1087 /* Calculate and cache the peer "sort" */
1088 enum bgp_peer_sort
peer_sort(struct peer
*peer
)
1090 peer
->sort
= peer_calc_sort(peer
);
1094 enum bgp_peer_sort
peer_sort_lookup(struct peer
*peer
)
1099 static void peer_free(struct peer
*peer
)
1104 assert(peer
->status
== Deleted
);
1108 /* this /ought/ to have been done already through bgp_stop earlier,
1109 * but just to be sure..
1111 bgp_timer_set(peer
);
1112 bgp_reads_off(peer
);
1113 bgp_writes_off(peer
);
1114 assert(!peer
->t_write
);
1115 assert(!peer
->t_read
);
1116 BGP_EVENT_FLUSH(peer
);
1118 pthread_mutex_destroy(&peer
->io_mtx
);
1120 /* Free connected nexthop, if present */
1121 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1122 && !peer_dynamic_neighbor(peer
))
1123 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1126 FOREACH_AFI_SAFI (afi
, safi
) {
1127 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1128 XFREE(MTYPE_BGP_FILTER_NAME
,
1129 peer
->filter
[afi
][safi
].advmap
.aname
);
1130 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1131 XFREE(MTYPE_BGP_FILTER_NAME
,
1132 peer
->filter
[afi
][safi
].advmap
.cname
);
1135 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1137 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1138 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1139 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1140 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1142 /* Update source configuration. */
1143 if (peer
->update_source
) {
1144 sockunion_free(peer
->update_source
);
1145 peer
->update_source
= NULL
;
1148 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1150 XFREE(MTYPE_BGP_NOTIFICATION
, peer
->notify
.data
);
1151 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1153 if (peer
->clear_node_queue
)
1154 work_queue_free_and_null(&peer
->clear_node_queue
);
1156 bgp_sync_delete(peer
);
1158 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1160 /* Remove BFD configuration. */
1161 if (peer
->bfd_config
)
1162 bgp_peer_remove_bfd_config(peer
);
1164 FOREACH_AFI_SAFI (afi
, safi
)
1165 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1167 bgp_unlock(peer
->bgp
);
1169 memset(peer
, 0, sizeof(struct peer
));
1171 XFREE(MTYPE_BGP_PEER
, peer
);
1174 /* increase reference count on a struct peer */
1175 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1177 assert(peer
&& (peer
->lock
>= 0));
1184 /* decrease reference count on a struct peer
1185 * struct peer is freed and NULL returned if last reference
1187 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1189 assert(peer
&& (peer
->lock
> 0));
1193 if (peer
->lock
== 0) {
1200 /* BGP GR changes */
1202 int bgp_global_gr_init(struct bgp
*bgp
)
1204 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1205 zlog_debug("%s called ..", __func__
);
1207 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1208 /* GLOBAL_HELPER Mode */
1211 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1212 GLOBAL_GR
, GLOBAL_INVALID
,
1213 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1214 GLOBAL_DISABLE
, GLOBAL_INVALID
1216 /* GLOBAL_GR Mode */
1219 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1220 GLOBAL_GR
, GLOBAL_HELPER
,
1221 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1222 GLOBAL_DISABLE
, GLOBAL_INVALID
1224 /* GLOBAL_DISABLE Mode */
1227 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1228 GLOBAL_GR
, GLOBAL_INVALID
,
1229 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1230 GLOBAL_INVALID
, GLOBAL_HELPER
1232 /* GLOBAL_INVALID Mode */
1235 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1236 GLOBAL_INVALID
, GLOBAL_INVALID
,
1237 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1238 GLOBAL_INVALID
, GLOBAL_INVALID
1241 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1242 sizeof(local_GLOBAL_GR_FSM
));
1244 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1245 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1247 return BGP_GR_SUCCESS
;
1250 int bgp_peer_gr_init(struct peer
*peer
)
1252 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1253 zlog_debug("%s called ..", __func__
);
1255 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1256 [BGP_PEER_GR_EVENT_CMD
] = {
1258 /* PEER_HELPER Mode */
1259 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1260 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1261 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1262 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1263 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1264 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1265 bgp_peer_gr_action
}
1269 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1270 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1271 bgp_peer_gr_action
},
1272 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1273 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1274 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1275 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1278 /* PEER_DISABLE Mode */
1279 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1280 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1281 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1282 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1283 bgp_peer_gr_action
},
1284 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1285 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1288 /* PEER_INVALID Mode */
1289 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1290 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1291 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1292 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1293 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1294 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1297 /* PEER_GLOBAL_INHERIT Mode */
1298 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1299 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1300 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1301 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1302 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1303 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1306 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1307 sizeof(local_Peer_GR_FSM
));
1308 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1309 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1311 return BGP_GR_SUCCESS
;
1314 static void bgp_srv6_init(struct bgp
*bgp
)
1316 bgp
->srv6_enabled
= false;
1317 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1318 bgp
->srv6_locator_chunks
= list_new();
1319 bgp
->srv6_functions
= list_new();
1322 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1324 if (bgp
->srv6_locator_chunks
)
1325 list_delete(&bgp
->srv6_locator_chunks
);
1326 if (bgp
->srv6_functions
)
1327 list_delete(&bgp
->srv6_functions
);
1330 /* Allocate new peer object, implicitely locked. */
1331 struct peer
*peer_new(struct bgp
*bgp
)
1338 /* bgp argument is absolutely required */
1341 /* Allocate new peer. */
1342 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1344 /* Set default value. */
1346 peer
->v_start
= BGP_INIT_START_TIMER
;
1347 peer
->v_connect
= bgp
->default_connect_retry
;
1348 peer
->status
= Idle
;
1349 peer
->ostatus
= Idle
;
1350 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1351 peer
->bgp
= bgp_lock(bgp
);
1352 peer
= peer_lock(peer
); /* initial reference */
1353 peer
->local_role
= ROLE_UNDEFINED
;
1354 peer
->remote_role
= ROLE_UNDEFINED
;
1355 peer
->password
= NULL
;
1356 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1358 /* Set default flags. */
1359 FOREACH_AFI_SAFI (afi
, safi
) {
1360 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1361 SET_FLAG(peer
->af_flags
[afi
][safi
],
1362 PEER_FLAG_SEND_EXT_COMMUNITY
);
1363 SET_FLAG(peer
->af_flags
[afi
][safi
],
1364 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1366 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1367 PEER_FLAG_SEND_COMMUNITY
);
1368 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1369 PEER_FLAG_SEND_EXT_COMMUNITY
);
1370 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1371 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1372 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1373 peer
->soo
[afi
][safi
] = NULL
;
1376 /* set nexthop-unchanged for l2vpn evpn by default */
1377 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1378 PEER_FLAG_NEXTHOP_UNCHANGED
);
1380 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1382 /* Initialize per peer bgp GR FSM */
1383 bgp_peer_gr_init(peer
);
1385 /* Create buffers. */
1386 peer
->ibuf
= stream_fifo_new();
1387 peer
->obuf
= stream_fifo_new();
1388 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1390 /* We use a larger buffer for peer->obuf_work in the event that:
1391 * - We RX a BGP_UPDATE where the attributes alone are just
1392 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1393 * - The user configures an outbound route-map that does many as-path
1394 * prepends or adds many communities. At most they can have
1395 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1396 * large they can make the attributes.
1398 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1399 * bounds checking for every single attribute as we construct an
1403 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1405 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1407 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1409 bgp_sync_init(peer
);
1411 /* Get service port number. */
1412 sp
= getservbyname("bgp", "tcp");
1413 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1415 QOBJ_REG(peer
, peer
);
1420 * This function is invoked when a duplicate peer structure associated with
1421 * a neighbor is being deleted. If this about-to-be-deleted structure is
1422 * the one with all the config, then we have to copy over the info.
1424 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1426 struct peer_af
*paf
;
1434 /* The following function is used by both peer group config copy to
1435 * individual peer and when we transfer config
1437 if (peer_src
->change_local_as
)
1438 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1440 /* peer flags apply */
1441 peer_dst
->flags
= peer_src
->flags
;
1443 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1444 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1446 peer_dst
->local_as
= peer_src
->local_as
;
1447 peer_dst
->port
= peer_src
->port
;
1448 /* copy tcp_mss value */
1449 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1450 (void)peer_sort(peer_dst
);
1451 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1452 peer_dst
->local_role
= peer_src
->local_role
;
1454 peer_dst
->max_packet_size
= peer_src
->max_packet_size
;
1457 peer_dst
->holdtime
= peer_src
->holdtime
;
1458 peer_dst
->keepalive
= peer_src
->keepalive
;
1459 peer_dst
->connect
= peer_src
->connect
;
1460 peer_dst
->delayopen
= peer_src
->delayopen
;
1461 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1462 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1463 peer_dst
->routeadv
= peer_src
->routeadv
;
1464 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1465 peer_dst
->v_delayopen
= peer_src
->v_delayopen
;
1467 /* password apply */
1468 if (peer_src
->password
&& !peer_dst
->password
)
1469 peer_dst
->password
=
1470 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1472 FOREACH_AFI_SAFI (afi
, safi
) {
1473 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1474 peer_dst
->af_flags
[afi
][safi
] = peer_src
->af_flags
[afi
][safi
];
1475 peer_dst
->allowas_in
[afi
][safi
] =
1476 peer_src
->allowas_in
[afi
][safi
];
1477 peer_dst
->weight
[afi
][safi
] = peer_src
->weight
[afi
][safi
];
1478 peer_dst
->addpath_type
[afi
][safi
] =
1479 peer_src
->addpath_type
[afi
][safi
];
1482 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1483 paf
= peer_src
->peer_af_array
[afidx
];
1485 if (!peer_af_find(peer_dst
, paf
->afi
, paf
->safi
))
1486 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1490 /* update-source apply */
1491 if (peer_src
->update_source
) {
1492 if (peer_dst
->update_source
)
1493 sockunion_free(peer_dst
->update_source
);
1494 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1495 peer_dst
->update_source
=
1496 sockunion_dup(peer_src
->update_source
);
1497 } else if (peer_src
->update_if
) {
1498 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1499 if (peer_dst
->update_source
) {
1500 sockunion_free(peer_dst
->update_source
);
1501 peer_dst
->update_source
= NULL
;
1503 peer_dst
->update_if
=
1504 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1507 if (peer_src
->ifname
) {
1508 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1511 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1515 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1516 struct interface
*ifp
)
1518 struct connected
*ifc
;
1521 struct listnode
*node
;
1523 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1524 * IPv4 address of the other end.
1526 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1527 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1528 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
1529 if (p
.prefixlen
== 30) {
1530 peer
->su
.sa
.sa_family
= AF_INET
;
1531 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1533 peer
->su
.sin
.sin_addr
.s_addr
=
1535 else if (addr
% 4 == 2)
1536 peer
->su
.sin
.sin_addr
.s_addr
=
1538 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1539 peer
->su
.sin
.sin_len
=
1540 sizeof(struct sockaddr_in
);
1541 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1543 } else if (p
.prefixlen
== 31) {
1544 peer
->su
.sa
.sa_family
= AF_INET
;
1545 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1547 peer
->su
.sin
.sin_addr
.s_addr
=
1550 peer
->su
.sin
.sin_addr
.s_addr
=
1552 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1553 peer
->su
.sin
.sin_len
=
1554 sizeof(struct sockaddr_in
);
1555 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1557 } else if (bgp_debug_neighbor_events(peer
))
1559 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1567 static bool bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1568 struct interface
*ifp
)
1570 struct nbr_connected
*ifc_nbr
;
1572 /* Have we learnt the peer's IPv6 link-local address? */
1573 if (ifp
->nbr_connected
1574 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1575 peer
->su
.sa
.sa_family
= AF_INET6
;
1576 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1577 sizeof(struct in6_addr
));
1579 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1581 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1589 * Set or reset the peer address socketunion structure based on the
1590 * learnt/derived peer address. If the address has changed, update the
1591 * password on the listen socket, if needed.
1593 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1595 struct interface
*ifp
;
1597 int peer_addr_updated
= 0;
1603 * Our peer structure is stored in the bgp->peerhash
1604 * release it before we modify anything.
1606 hash_release(peer
->bgp
->peerhash
, peer
);
1608 prev_family
= peer
->su
.sa
.sa_family
;
1609 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1611 /* If BGP unnumbered is not "v6only", we first see if we can
1613 * peer's IPv4 address.
1615 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1617 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1619 /* If "v6only" or we can't derive peer's IPv4 address, see if
1621 * learnt the peer's IPv6 link-local address. This is from the
1623 * IPv6 address in router advertisement.
1625 if (!peer_addr_updated
)
1627 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1629 /* If we could derive the peer address, we may need to install the
1631 * configured for the peer, if any, on the listen socket. Otherwise,
1633 * that peer's address is not available and uninstall the password, if
1636 if (peer_addr_updated
) {
1637 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1638 && prev_family
== AF_UNSPEC
)
1641 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1642 && prev_family
!= AF_UNSPEC
)
1643 bgp_md5_unset(peer
);
1644 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1645 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1649 * Since our su changed we need to del/add peer to the peerhash
1651 (void)hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
1654 void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1656 struct bgp_dest
*dest
, *ndest
;
1657 struct bgp_table
*table
;
1659 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1660 dest
= bgp_route_next(dest
)) {
1661 table
= bgp_dest_get_bgp_table_info(dest
);
1662 if (table
!= NULL
) {
1663 /* Special handling for 2-level routing
1665 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1666 || safi
== SAFI_EVPN
) {
1667 for (ndest
= bgp_table_top(table
); ndest
;
1668 ndest
= bgp_route_next(ndest
))
1669 bgp_process(bgp
, ndest
, afi
, safi
);
1671 bgp_process(bgp
, dest
, afi
, safi
);
1676 /* Force a bestpath recalculation for all prefixes. This is used
1677 * when 'bgp bestpath' commands are entered.
1679 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1684 FOREACH_AFI_SAFI (afi
, safi
) {
1685 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1690 * Create new BGP peer.
1692 * conf_if and su are mutually exclusive if configuring from the cli.
1693 * If we are handing a doppelganger, then we *must* pass in both
1694 * the original peer's su and conf_if, so that we can appropriately
1695 * track the bgp->peerhash( ie we don't want to remove the current
1696 * one from the config ).
1698 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1699 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1700 int as_type
, struct peer_group
*group
)
1704 char buf
[SU_ADDRSTRLEN
];
1708 peer
= peer_new(bgp
);
1710 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1714 bgp_peer_conf_if_to_su_update(peer
);
1715 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1716 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1719 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1720 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1721 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1723 peer
->local_as
= local_as
;
1724 peer
->as
= remote_as
;
1725 peer
->as_type
= as_type
;
1726 peer
->local_id
= bgp
->router_id
;
1727 peer
->v_holdtime
= bgp
->default_holdtime
;
1728 peer
->v_keepalive
= bgp
->default_keepalive
;
1729 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1730 ? BGP_DEFAULT_IBGP_ROUTEADV
1731 : BGP_DEFAULT_EBGP_ROUTEADV
;
1732 if (bgp_config_inprocess())
1733 peer
->shut_during_cfg
= true;
1735 peer
= peer_lock(peer
); /* bgp peer list reference */
1736 peer
->group
= group
;
1737 listnode_add_sort(bgp
->peer
, peer
);
1738 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1740 /* Adjust update-group coalesce timer heuristics for # peers. */
1741 if (bgp
->heuristic_coalesce
) {
1742 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1744 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1745 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1748 active
= peer_active(peer
);
1750 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1751 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1753 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1756 /* Last read and reset time set */
1757 peer
->readtime
= peer
->resettime
= monotime(NULL
);
1759 /* Default TTL set. */
1760 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1762 /* Default configured keepalives count for shutdown rtt command */
1763 peer
->rtt_keepalive_conf
= 1;
1765 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1767 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1768 * neighbor for the corresponding address family. IPv4 Unicast is
1769 * the only address family enabled by default without expliict
1772 FOREACH_AFI_SAFI (afi
, safi
) {
1773 if (bgp
->default_af
[afi
][safi
]) {
1774 peer
->afc
[afi
][safi
] = 1;
1775 peer_af_create(peer
, afi
, safi
);
1779 /* auto shutdown if configured */
1780 if (bgp
->autoshutdown
)
1781 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1782 /* Set up peer's events and timers. */
1783 else if (!active
&& peer_active(peer
))
1784 bgp_timer_set(peer
);
1786 bgp_peer_gr_flags_update(peer
);
1787 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1792 /* Make accept BGP peer. This function is only called from the test code */
1793 struct peer
*peer_create_accept(struct bgp
*bgp
)
1797 peer
= peer_new(bgp
);
1799 peer
= peer_lock(peer
); /* bgp peer list reference */
1800 listnode_add_sort(bgp
->peer
, peer
);
1806 * Return true if we have a peer configured to use this afi/safi
1808 bool bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1810 struct listnode
*node
;
1813 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1814 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1817 if (peer
->afc
[afi
][safi
])
1824 /* Change peer's AS number. */
1825 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
)
1827 enum bgp_peer_sort origtype
, newtype
;
1830 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1831 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1832 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1833 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1834 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1836 bgp_session_reset(peer
);
1838 origtype
= peer_sort_lookup(peer
);
1840 peer
->as_type
= as_specified
;
1842 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1843 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1844 && peer
->bgp
->as
!= as
)
1845 peer
->local_as
= peer
->bgp
->confed_id
;
1847 peer
->local_as
= peer
->bgp
->as
;
1849 newtype
= peer_sort(peer
);
1850 /* Advertisement-interval reset */
1851 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1852 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1853 ? BGP_DEFAULT_IBGP_ROUTEADV
1854 : BGP_DEFAULT_EBGP_ROUTEADV
;
1858 if (newtype
== BGP_PEER_IBGP
)
1860 else if (origtype
== BGP_PEER_IBGP
)
1861 peer
->ttl
= BGP_DEFAULT_TTL
;
1863 /* reflector-client reset */
1864 if (newtype
!= BGP_PEER_IBGP
) {
1865 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1866 PEER_FLAG_REFLECTOR_CLIENT
);
1867 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1868 PEER_FLAG_REFLECTOR_CLIENT
);
1869 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1870 PEER_FLAG_REFLECTOR_CLIENT
);
1871 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1872 PEER_FLAG_REFLECTOR_CLIENT
);
1873 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1874 PEER_FLAG_REFLECTOR_CLIENT
);
1875 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1876 PEER_FLAG_REFLECTOR_CLIENT
);
1877 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1878 PEER_FLAG_REFLECTOR_CLIENT
);
1879 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1880 PEER_FLAG_REFLECTOR_CLIENT
);
1881 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1882 PEER_FLAG_REFLECTOR_CLIENT
);
1883 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1884 PEER_FLAG_REFLECTOR_CLIENT
);
1885 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1886 PEER_FLAG_REFLECTOR_CLIENT
);
1887 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1888 PEER_FLAG_REFLECTOR_CLIENT
);
1889 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1890 PEER_FLAG_REFLECTOR_CLIENT
);
1894 /* If peer does not exist, create new one. If peer already exists,
1895 set AS number to the peer. */
1896 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1897 as_t
*as
, int as_type
)
1903 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1905 peer
= peer_lookup(bgp
, su
);
1908 /* Not allowed for a dynamic peer. */
1909 if (peer_dynamic_neighbor(peer
)) {
1911 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1914 /* When this peer is a member of peer-group. */
1916 /* peer-group already has AS number/internal/external */
1917 if (peer
->group
->conf
->as
1918 || peer
->group
->conf
->as_type
) {
1919 /* Return peer group's AS number. */
1920 *as
= peer
->group
->conf
->as
;
1921 return BGP_ERR_PEER_GROUP_MEMBER
;
1924 enum bgp_peer_sort peer_sort_type
=
1925 peer_sort(peer
->group
->conf
);
1927 /* Explicit AS numbers used, compare AS numbers */
1928 if (as_type
== AS_SPECIFIED
) {
1929 if (((peer_sort_type
== BGP_PEER_IBGP
)
1930 && (bgp
->as
!= *as
))
1931 || ((peer_sort_type
== BGP_PEER_EBGP
)
1932 && (bgp
->as
== *as
))) {
1934 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1937 /* internal/external used, compare as-types */
1938 if (((peer_sort_type
== BGP_PEER_IBGP
)
1939 && (as_type
!= AS_INTERNAL
))
1940 || ((peer_sort_type
== BGP_PEER_EBGP
)
1941 && (as_type
!= AS_EXTERNAL
))) {
1943 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1948 /* Existing peer's AS number change. */
1949 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
1950 || (peer
->as_type
!= as_type
))
1951 peer_as_change(peer
, *as
, as_type
);
1954 return BGP_ERR_NO_INTERFACE_CONFIG
;
1956 /* If the peer is not part of our confederation, and its not an
1957 iBGP peer then spoof the source AS */
1958 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
1959 && !bgp_confederation_peers_check(bgp
, *as
)
1961 local_as
= bgp
->confed_id
;
1965 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
);
1971 const char *bgp_get_name_by_role(uint8_t role
)
1976 case ROLE_RS_SERVER
:
1978 case ROLE_RS_CLIENT
:
1984 case ROLE_UNDEFINED
:
1990 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
1991 struct peer
*peer
, afi_t afi
,
1995 int out
= FILTER_OUT
;
1997 uint64_t pflags_ovrd
;
1998 uint8_t *pfilter_ovrd
;
2002 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2003 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2005 /* peer af_flags apply */
2006 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2007 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2008 ^ peer
->af_flags_invert
[afi
][safi
];
2009 flags_tmp
&= ~pflags_ovrd
;
2011 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2012 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2013 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2014 conf
->af_flags_invert
[afi
][safi
]);
2016 /* maximum-prefix */
2017 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2018 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2019 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2020 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2023 /* maximum-prefix-out */
2024 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX_OUT
))
2025 PEER_ATTR_INHERIT(peer
, group
, pmax_out
[afi
][safi
]);
2028 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2029 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2032 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_SOO
))
2033 PEER_ATTR_INHERIT(peer
, group
, soo
[afi
][safi
]);
2036 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2037 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2039 /* default-originate route-map */
2040 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2041 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2042 MTYPE_ROUTE_MAP_NAME
);
2043 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2046 /* inbound filter apply */
2047 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2048 PEER_STR_ATTR_INHERIT(peer
, group
,
2049 filter
[afi
][safi
].dlist
[in
].name
,
2050 MTYPE_BGP_FILTER_NAME
);
2051 PEER_ATTR_INHERIT(peer
, group
,
2052 filter
[afi
][safi
].dlist
[in
].alist
);
2055 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2056 PEER_STR_ATTR_INHERIT(peer
, group
,
2057 filter
[afi
][safi
].plist
[in
].name
,
2058 MTYPE_BGP_FILTER_NAME
);
2059 PEER_ATTR_INHERIT(peer
, group
,
2060 filter
[afi
][safi
].plist
[in
].plist
);
2063 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2064 PEER_STR_ATTR_INHERIT(peer
, group
,
2065 filter
[afi
][safi
].aslist
[in
].name
,
2066 MTYPE_BGP_FILTER_NAME
);
2067 PEER_ATTR_INHERIT(peer
, group
,
2068 filter
[afi
][safi
].aslist
[in
].aslist
);
2071 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2072 PEER_STR_ATTR_INHERIT(peer
, group
,
2073 filter
[afi
][safi
].map
[in
].name
,
2074 MTYPE_BGP_FILTER_NAME
);
2075 PEER_ATTR_INHERIT(peer
, group
,
2076 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2079 /* outbound filter apply */
2080 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2081 PEER_STR_ATTR_INHERIT(peer
, group
,
2082 filter
[afi
][safi
].dlist
[out
].name
,
2083 MTYPE_BGP_FILTER_NAME
);
2084 PEER_ATTR_INHERIT(peer
, group
,
2085 filter
[afi
][safi
].dlist
[out
].alist
);
2088 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2089 PEER_STR_ATTR_INHERIT(peer
, group
,
2090 filter
[afi
][safi
].plist
[out
].name
,
2091 MTYPE_BGP_FILTER_NAME
);
2092 PEER_ATTR_INHERIT(peer
, group
,
2093 filter
[afi
][safi
].plist
[out
].plist
);
2096 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2097 PEER_STR_ATTR_INHERIT(peer
, group
,
2098 filter
[afi
][safi
].aslist
[out
].name
,
2099 MTYPE_BGP_FILTER_NAME
);
2100 PEER_ATTR_INHERIT(peer
, group
,
2101 filter
[afi
][safi
].aslist
[out
].aslist
);
2104 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2105 PEER_STR_ATTR_INHERIT(peer
, group
,
2106 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2107 MTYPE_BGP_FILTER_NAME
);
2108 PEER_ATTR_INHERIT(peer
, group
,
2109 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2112 /* nondirectional filter apply */
2113 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2114 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2115 MTYPE_BGP_FILTER_NAME
);
2116 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2119 /* Conditional Advertisements */
2120 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ADVERTISE_MAP
)) {
2121 PEER_STR_ATTR_INHERIT(peer
, group
,
2122 filter
[afi
][safi
].advmap
.aname
,
2123 MTYPE_BGP_FILTER_NAME
);
2124 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.amap
);
2125 PEER_STR_ATTR_INHERIT(peer
, group
,
2126 filter
[afi
][safi
].advmap
.cname
,
2127 MTYPE_BGP_FILTER_NAME
);
2128 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.cmap
);
2129 PEER_ATTR_INHERIT(peer
, group
,
2130 filter
[afi
][safi
].advmap
.condition
);
2133 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2134 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2135 bgp_addpath_type_changed(conf
->bgp
);
2139 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2144 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2145 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2146 __func__
, peer
->host
);
2150 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2152 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2153 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2154 return BGP_ERR_PEER_SAFI_CONFLICT
;
2156 /* Nothing to do if we've already activated this peer */
2157 if (peer
->afc
[afi
][safi
])
2160 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2163 active
= peer_active(peer
);
2164 peer
->afc
[afi
][safi
] = 1;
2167 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2169 if (!active
&& peer_active(peer
)) {
2170 bgp_timer_set(peer
);
2172 if (peer_established(peer
)) {
2173 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2174 peer
->afc_adv
[afi
][safi
] = 1;
2175 bgp_capability_send(peer
, afi
, safi
,
2177 CAPABILITY_ACTION_SET
);
2178 if (peer
->afc_recv
[afi
][safi
]) {
2179 peer
->afc_nego
[afi
][safi
] = 1;
2180 bgp_announce_route(peer
, afi
, safi
,
2184 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2185 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2186 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2189 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2190 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2191 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2192 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2195 * If we are turning on a AFI/SAFI locally and we've
2196 * started bringing a peer up, we need to tell
2197 * the other peer to restart because we might loose
2198 * configuration here because when the doppelganger
2199 * gets to a established state due to how
2200 * we resolve we could just overwrite the afi/safi
2203 other
= peer
->doppelganger
;
2205 && (other
->status
== OpenSent
2206 || other
->status
== OpenConfirm
)) {
2207 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2208 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2209 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2216 /* Activate the peer or peer group for specified AFI and SAFI. */
2217 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2220 struct peer_group
*group
;
2221 struct listnode
*node
, *nnode
;
2222 struct peer
*tmp_peer
;
2225 /* Nothing to do if we've already activated this peer */
2226 if (peer
->afc
[afi
][safi
])
2231 /* This is a peer-group so activate all of the members of the
2232 * peer-group as well */
2233 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2235 /* Do not activate a peer for both SAFI_UNICAST and
2236 * SAFI_LABELED_UNICAST */
2237 if ((safi
== SAFI_UNICAST
2238 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2239 || (safi
== SAFI_LABELED_UNICAST
2240 && peer
->afc
[afi
][SAFI_UNICAST
]))
2241 return BGP_ERR_PEER_SAFI_CONFLICT
;
2243 peer
->afc
[afi
][safi
] = 1;
2244 group
= peer
->group
;
2246 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2247 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2250 ret
|= peer_activate_af(peer
, afi
, safi
);
2253 /* If this is the first peer to be activated for this
2254 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2255 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2256 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2258 if (BGP_DEBUG(zebra
, ZEBRA
))
2260 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2262 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2263 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2266 if (safi
== SAFI_FLOWSPEC
) {
2267 /* connect to table manager */
2268 bgp_zebra_init_tm_connect(bgp
);
2273 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2276 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2277 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2278 __func__
, peer
->host
);
2282 /* Nothing to do if we've already deactivated this peer */
2283 if (!peer
->afc
[afi
][safi
])
2286 /* De-activate the address family configuration. */
2287 peer
->afc
[afi
][safi
] = 0;
2289 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2290 flog_err(EC_BGP_PEER_DELETE
,
2291 "couldn't delete af structure for peer %s(%s, %s)",
2292 peer
->host
, afi2str(afi
), safi2str(safi
));
2296 if (peer_established(peer
)) {
2297 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2298 peer
->afc_adv
[afi
][safi
] = 0;
2299 peer
->afc_nego
[afi
][safi
] = 0;
2301 if (peer_active_nego(peer
)) {
2302 bgp_capability_send(peer
, afi
, safi
,
2304 CAPABILITY_ACTION_UNSET
);
2305 bgp_clear_route(peer
, afi
, safi
);
2306 peer
->pcount
[afi
][safi
] = 0;
2308 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2309 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2310 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2313 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2314 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2315 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2322 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2325 struct peer_group
*group
;
2326 struct peer
*tmp_peer
;
2327 struct listnode
*node
, *nnode
;
2330 /* Nothing to do if we've already de-activated this peer */
2331 if (!peer
->afc
[afi
][safi
])
2334 /* This is a peer-group so de-activate all of the members of the
2335 * peer-group as well */
2336 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2337 peer
->afc
[afi
][safi
] = 0;
2338 group
= peer
->group
;
2340 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2341 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2344 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2349 /* If this is the last peer to be deactivated for this
2350 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2351 if (safi
== SAFI_LABELED_UNICAST
2352 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2353 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2355 if (BGP_DEBUG(zebra
, ZEBRA
))
2357 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2359 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2360 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2365 void peer_nsf_stop(struct peer
*peer
)
2370 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2371 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2373 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2374 peer
->nsf
[afi
][safi
] = 0;
2375 THREAD_OFF(peer
->t_llgr_stale
[afi
][safi
]);
2378 if (peer
->t_gr_restart
) {
2379 THREAD_OFF(peer
->t_gr_restart
);
2380 if (bgp_debug_neighbor_events(peer
))
2381 zlog_debug("%pBP graceful restart timer stopped", peer
);
2383 if (peer
->t_gr_stale
) {
2384 THREAD_OFF(peer
->t_gr_stale
);
2385 if (bgp_debug_neighbor_events(peer
))
2387 "%pBP graceful restart stalepath timer stopped",
2390 bgp_clear_route_all(peer
);
2393 /* Delete peer from confguration.
2395 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2396 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2398 * This function /should/ take care to be idempotent, to guard against
2399 * it being called multiple times through stray events that come in
2400 * that happen to result in this function being called again. That
2401 * said, getting here for a "Deleted" peer is a bug in the neighbour
2404 int peer_delete(struct peer
*peer
)
2410 struct bgp_filter
*filter
;
2411 struct listnode
*pn
;
2414 assert(peer
->status
!= Deleted
);
2417 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2419 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2421 bgp_keepalives_off(peer
);
2422 bgp_reads_off(peer
);
2423 bgp_writes_off(peer
);
2424 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2425 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2426 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2428 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2429 peer_nsf_stop(peer
);
2431 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2433 /* Remove BFD settings. */
2434 if (peer
->bfd_config
)
2435 bgp_peer_remove_bfd_config(peer
);
2437 /* If this peer belongs to peer group, clear up the
2440 if (peer_dynamic_neighbor(peer
))
2441 peer_drop_dynamic_neighbor(peer
);
2443 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2445 peer
); /* group->peer list reference */
2446 list_delete_node(peer
->group
->peer
, pn
);
2451 /* Withdraw all information from routing table. We can not use
2452 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2453 * executed after peer structure is deleted.
2455 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2457 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2459 if (peer
->doppelganger
) {
2460 peer
->doppelganger
->doppelganger
= NULL
;
2461 peer
->doppelganger
= NULL
;
2464 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2465 bgp_fsm_change_status(peer
, Deleted
);
2467 /* Remove from NHT */
2468 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2469 bgp_unlink_nexthop_by_peer(peer
);
2471 /* Password configuration */
2472 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2473 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2474 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2475 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2476 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2477 bgp_md5_unset(peer
);
2480 bgp_timer_set(peer
); /* stops all timers for Deleted */
2482 /* Delete from all peer list. */
2483 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2484 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2485 peer_unlock(peer
); /* bgp peer list reference */
2486 list_delete_node(bgp
->peer
, pn
);
2487 hash_release(bgp
->peerhash
, peer
);
2492 stream_fifo_free(peer
->ibuf
);
2497 stream_fifo_free(peer
->obuf
);
2501 if (peer
->ibuf_work
) {
2502 ringbuf_del(peer
->ibuf_work
);
2503 peer
->ibuf_work
= NULL
;
2506 if (peer
->obuf_work
) {
2507 stream_free(peer
->obuf_work
);
2508 peer
->obuf_work
= NULL
;
2511 if (peer
->scratch
) {
2512 stream_free(peer
->scratch
);
2513 peer
->scratch
= NULL
;
2516 /* Local and remote addresses. */
2517 if (peer
->su_local
) {
2518 sockunion_free(peer
->su_local
);
2519 peer
->su_local
= NULL
;
2522 if (peer
->su_remote
) {
2523 sockunion_free(peer
->su_remote
);
2524 peer
->su_remote
= NULL
;
2527 /* Free filter related memory. */
2528 FOREACH_AFI_SAFI (afi
, safi
) {
2529 filter
= &peer
->filter
[afi
][safi
];
2531 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2532 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2533 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2534 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2537 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2538 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2541 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2542 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2543 ecommunity_free(&peer
->soo
[afi
][safi
]);
2546 FOREACH_AFI_SAFI (afi
, safi
)
2547 peer_af_delete(peer
, afi
, safi
);
2549 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2550 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2552 peer_unlock(peer
); /* initial reference */
2557 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2559 return strcmp(g1
->name
, g2
->name
);
2562 /* Peer group cofiguration. */
2563 static struct peer_group
*peer_group_new(void)
2565 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2568 static void peer_group_free(struct peer_group
*group
)
2570 XFREE(MTYPE_PEER_GROUP
, group
);
2573 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2575 struct peer_group
*group
;
2576 struct listnode
*node
, *nnode
;
2578 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2579 if (strcmp(group
->name
, name
) == 0)
2585 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2587 struct peer_group
*group
;
2591 group
= peer_group_lookup(bgp
, name
);
2595 group
= peer_group_new();
2597 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2598 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2599 group
->peer
= list_new();
2600 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2601 group
->listen_range
[afi
] = list_new();
2602 group
->conf
= peer_new(bgp
);
2603 FOREACH_AFI_SAFI (afi
, safi
) {
2604 if (bgp
->default_af
[afi
][safi
])
2605 group
->conf
->afc
[afi
][safi
] = 1;
2607 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2608 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2609 group
->conf
->group
= group
;
2610 group
->conf
->as
= 0;
2611 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2612 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2613 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2614 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2615 listnode_add_sort(bgp
->group
, group
);
2620 static void peer_group2peer_config_copy(struct peer_group
*group
,
2630 peer
->as
= conf
->as
;
2633 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2634 peer
->change_local_as
= conf
->change_local_as
;
2636 /* If peer-group has configured TTL then override it */
2637 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2638 peer
->ttl
= conf
->ttl
;
2641 peer
->gtsm_hops
= conf
->gtsm_hops
;
2643 /* peer flags apply */
2644 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2645 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2646 flags_tmp
&= ~peer
->flags_override
;
2648 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2649 SET_FLAG(peer
->flags
, flags_tmp
);
2650 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2652 /* peer timers apply */
2653 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2654 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2655 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2658 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2659 PEER_ATTR_INHERIT(peer
, group
, connect
);
2660 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2661 peer
->v_connect
= conf
->connect
;
2663 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2666 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2667 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2668 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2669 peer
->v_delayopen
= conf
->delayopen
;
2671 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2674 /* advertisement-interval apply */
2675 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2676 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2677 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2678 peer
->v_routeadv
= conf
->routeadv
;
2680 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2681 ? BGP_DEFAULT_IBGP_ROUTEADV
2682 : BGP_DEFAULT_EBGP_ROUTEADV
;
2685 /* capability extended-nexthop apply */
2686 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2687 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2688 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2690 /* password apply */
2691 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2692 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2693 MTYPE_PEER_PASSWORD
);
2695 if (!BGP_PEER_SU_UNSPEC(peer
))
2698 /* update-source apply */
2699 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2700 if (conf
->update_source
) {
2701 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2702 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2703 } else if (conf
->update_if
) {
2704 sockunion_free(peer
->update_source
);
2705 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2706 MTYPE_PEER_UPDATE_SOURCE
);
2711 PEER_ATTR_INHERIT(peer
, group
, local_role
);
2713 /* Update GR flags for the peer. */
2714 bgp_peer_gr_flags_update(peer
);
2716 /* Apply BFD settings from group to peer if it exists. */
2717 if (conf
->bfd_config
) {
2718 bgp_peer_configure_bfd(peer
, false);
2719 bgp_peer_config_apply(peer
, group
);
2723 /* Peer group's remote AS configuration. */
2724 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2727 struct peer_group
*group
;
2729 struct listnode
*node
, *nnode
;
2731 group
= peer_group_lookup(bgp
, group_name
);
2735 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2739 /* When we setup peer-group AS number all peer group member's AS
2740 number must be updated to same number. */
2741 peer_as_change(group
->conf
, *as
, as_type
);
2743 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2744 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2745 || (peer
->as_type
!= as_type
))
2746 peer_as_change(peer
, *as
, as_type
);
2752 void peer_notify_unconfig(struct peer
*peer
)
2754 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2755 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2756 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2759 void peer_group_notify_unconfig(struct peer_group
*group
)
2761 struct peer
*peer
, *other
;
2762 struct listnode
*node
, *nnode
;
2764 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2765 other
= peer
->doppelganger
;
2766 if (other
&& other
->status
!= Deleted
) {
2767 other
->group
= NULL
;
2768 peer_notify_unconfig(other
);
2770 peer_notify_unconfig(peer
);
2774 int peer_group_delete(struct peer_group
*group
)
2778 struct prefix
*prefix
;
2780 struct listnode
*node
, *nnode
;
2785 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2786 other
= peer
->doppelganger
;
2788 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2789 bgp_zebra_terminate_radv(bgp
, peer
);
2792 if (other
&& other
->status
!= Deleted
) {
2793 other
->group
= NULL
;
2797 list_delete(&group
->peer
);
2799 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2800 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2802 prefix_free(&prefix
);
2804 list_delete(&group
->listen_range
[afi
]);
2807 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2810 if (group
->conf
->bfd_config
)
2811 bgp_peer_remove_bfd_config(group
->conf
);
2813 group
->conf
->group
= NULL
;
2814 peer_delete(group
->conf
);
2816 /* Delete from all peer_group list. */
2817 listnode_delete(bgp
->group
, group
);
2819 peer_group_free(group
);
2824 int peer_group_remote_as_delete(struct peer_group
*group
)
2826 struct peer
*peer
, *other
;
2827 struct listnode
*node
, *nnode
;
2829 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2830 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2833 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2834 other
= peer
->doppelganger
;
2836 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2837 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2841 if (other
&& other
->status
!= Deleted
) {
2842 other
->group
= NULL
;
2846 list_delete_all_node(group
->peer
);
2848 group
->conf
->as
= 0;
2849 group
->conf
->as_type
= AS_UNSPECIFIED
;
2854 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2856 struct prefix
*prefix
;
2857 struct listnode
*node
, *nnode
;
2860 afi
= family2afi(range
->family
);
2862 /* Group needs remote AS configured. */
2863 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2864 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2866 /* Ensure no duplicates. Currently we don't care about overlaps. */
2867 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2868 if (prefix_same(range
, prefix
))
2872 prefix
= prefix_new();
2873 prefix_copy(prefix
, range
);
2874 listnode_add(group
->listen_range
[afi
], prefix
);
2876 /* Update passwords for new ranges */
2877 if (group
->conf
->password
)
2878 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
2883 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2885 struct prefix
*prefix
, prefix2
;
2886 struct listnode
*node
, *nnode
;
2890 afi
= family2afi(range
->family
);
2892 /* Identify the listen range. */
2893 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2894 if (prefix_same(range
, prefix
))
2899 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
2901 /* Dispose off any dynamic neighbors that exist due to this listen range
2903 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2904 if (!peer_dynamic_neighbor(peer
))
2907 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
2908 && prefix_match(prefix
, &prefix2
)) {
2909 if (bgp_debug_neighbor_events(peer
))
2911 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
2912 peer
->host
, group
->name
, prefix
);
2917 /* Get rid of the listen range */
2918 listnode_delete(group
->listen_range
[afi
], prefix
);
2920 /* Remove passwords for deleted ranges */
2921 if (group
->conf
->password
)
2922 bgp_md5_unset_prefix(group
->bgp
, prefix
);
2927 /* Bind specified peer to peer group. */
2928 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
2929 struct peer_group
*group
, as_t
*as
)
2931 int first_member
= 0;
2934 enum bgp_peer_sort ptype
, gtype
;
2936 /* Lookup the peer. */
2938 peer
= peer_lookup(bgp
, su
);
2940 /* The peer exist, bind it to the peer-group */
2942 /* When the peer already belongs to a peer-group, check the
2944 if (peer_group_active(peer
)) {
2946 /* The peer is already bound to the peer-group,
2949 if (strcmp(peer
->group
->name
, group
->name
) == 0)
2952 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
2955 /* The peer has not specified a remote-as, inherit it from the
2957 if (peer
->as_type
== AS_UNSPECIFIED
) {
2958 peer
->as_type
= group
->conf
->as_type
;
2959 peer
->as
= group
->conf
->as
;
2960 peer
->sort
= group
->conf
->sort
;
2963 ptype
= peer_sort(peer
);
2964 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
2965 gtype
= peer_sort(group
->conf
);
2966 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
2969 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2972 if (gtype
== BGP_PEER_INTERNAL
)
2976 peer_group2peer_config_copy(group
, peer
);
2978 FOREACH_AFI_SAFI (afi
, safi
) {
2979 if (group
->conf
->afc
[afi
][safi
]) {
2980 peer
->afc
[afi
][safi
] = 1;
2982 if (peer_af_find(peer
, afi
, safi
)
2983 || peer_af_create(peer
, afi
, safi
)) {
2984 peer_group2peer_config_copy_af(
2985 group
, peer
, afi
, safi
);
2987 } else if (peer
->afc
[afi
][safi
])
2988 peer_deactivate(peer
, afi
, safi
);
2992 assert(group
&& peer
->group
== group
);
2994 listnode_delete(bgp
->peer
, peer
);
2996 peer
->group
= group
;
2997 listnode_add_sort(bgp
->peer
, peer
);
2999 peer
= peer_lock(peer
); /* group->peer list reference */
3000 listnode_add(group
->peer
, peer
);
3004 gtype
= peer_sort(group
->conf
);
3005 /* Advertisement-interval reset */
3006 if (!CHECK_FLAG(group
->conf
->flags
,
3007 PEER_FLAG_ROUTEADV
)) {
3008 group
->conf
->v_routeadv
=
3009 (gtype
== BGP_PEER_IBGP
)
3010 ? BGP_DEFAULT_IBGP_ROUTEADV
3011 : BGP_DEFAULT_EBGP_ROUTEADV
;
3014 /* ebgp-multihop reset */
3015 if (gtype
== BGP_PEER_IBGP
)
3016 group
->conf
->ttl
= MAXTTL
;
3019 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3021 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3022 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3023 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3024 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3026 bgp_session_reset(peer
);
3030 /* Create a new peer. */
3032 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3033 && (!group
->conf
->as
)) {
3034 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3037 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3038 group
->conf
->as_type
, group
);
3040 peer
= peer_lock(peer
); /* group->peer list reference */
3041 listnode_add(group
->peer
, peer
);
3043 peer_group2peer_config_copy(group
, peer
);
3045 /* If the peer-group is active for this afi/safi then activate
3047 FOREACH_AFI_SAFI (afi
, safi
) {
3048 if (group
->conf
->afc
[afi
][safi
]) {
3049 peer
->afc
[afi
][safi
] = 1;
3051 if (!peer_af_find(peer
, afi
, safi
))
3052 peer_af_create(peer
, afi
, safi
);
3054 peer_group2peer_config_copy_af(group
, peer
, afi
,
3056 } else if (peer
->afc
[afi
][safi
])
3057 peer_deactivate(peer
, afi
, safi
);
3060 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3062 /* Set up peer's events and timers. */
3063 if (peer_active(peer
))
3064 bgp_timer_set(peer
);
3070 static void bgp_startup_timer_expire(struct thread
*thread
)
3074 bgp
= THREAD_ARG(thread
);
3075 bgp
->t_startup
= NULL
;
3079 * On shutdown we call the cleanup function which
3080 * does a free of the link list nodes, free up
3081 * the data we are pointing at too.
3083 static void bgp_vrf_string_name_delete(void *data
)
3087 XFREE(MTYPE_TMP
, vname
);
3090 /* BGP instance creation by `router bgp' commands. */
3091 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3092 enum bgp_instance_type inst_type
)
3098 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3100 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3101 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3102 zlog_debug("Creating Default VRF, AS %u", *as
);
3104 zlog_debug("Creating %s %s, AS %u",
3105 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3111 /* Default the EVPN VRF to the default one */
3112 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3119 bgp
->allow_martian
= false;
3120 bgp_process_queue_init(bgp
);
3121 bgp
->heuristic_coalesce
= true;
3122 bgp
->inst_type
= inst_type
;
3123 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3125 bgp
->peer_self
= peer_new(bgp
);
3126 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3127 bgp
->peer_self
->host
=
3128 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3129 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3130 if (cmd_hostname_get())
3131 bgp
->peer_self
->hostname
=
3132 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3134 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3135 if (cmd_domainname_get())
3136 bgp
->peer_self
->domainname
=
3137 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3138 bgp
->peer
= list_new();
3139 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3140 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3142 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3144 bgp
->group
= list_new();
3145 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3147 FOREACH_AFI_SAFI (afi
, safi
) {
3148 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3149 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3150 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3152 /* Enable maximum-paths */
3153 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3155 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3157 /* Initialize graceful restart info */
3158 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3159 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3160 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3161 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3162 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3165 bgp
->v_update_delay
= bm
->v_update_delay
;
3166 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3167 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3168 bgp
->default_subgroup_pkt_queue_max
=
3169 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3170 bgp_timers_unset(bgp
);
3171 bgp
->default_min_holdtime
= 0;
3172 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3173 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3174 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3175 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3176 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3177 bgp
->dynamic_neighbors_count
= 0;
3178 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3179 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3180 bgp
->reject_as_sets
= false;
3181 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3182 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3183 bgp
->fast_convergence
= false;
3185 bgp
->llgr_stale_time
= BGP_DEFAULT_LLGR_STALE_TIME
;
3187 #ifdef ENABLE_BGP_VNC
3188 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3189 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3191 assert(bgp
->rfapi_cfg
);
3193 #endif /* ENABLE_BGP_VNC */
3195 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3196 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3197 bgp
->vpn_policy
[afi
].afi
= afi
;
3198 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3199 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3202 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3203 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3204 bgp_vrf_string_name_delete
;
3205 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3206 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3207 bgp_vrf_string_name_delete
;
3208 SET_FLAG(bgp
->af_flags
[afi
][SAFI_MPLS_VPN
],
3209 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL
);
3212 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3214 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3215 bgp
->restart_time
, &bgp
->t_startup
);
3217 /* printable name we can use in debug messages */
3218 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3219 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3229 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3231 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3232 snprintf(bgp
->name_pretty
, len
, "%s %s",
3233 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3239 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3240 memory_order_relaxed
);
3241 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3242 memory_order_relaxed
);
3243 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3244 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3248 update_bgp_group_init(bgp
);
3250 /* assign a unique rd id for auto derivation of vrf's RD */
3251 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3253 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3254 sizeof(struct bgp_evpn_info
));
3256 bgp_evpn_vrf_es_init(bgp
);
3260 /*initilize global GR FSM */
3261 bgp_global_gr_init(bgp
);
3263 memset(&bgp
->ebgprequirespolicywarning
, 0,
3264 sizeof(bgp
->ebgprequirespolicywarning
));
3269 /* Return the "default VRF" instance of BGP. */
3270 struct bgp
*bgp_get_default(void)
3273 struct listnode
*node
, *nnode
;
3275 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3276 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3281 /* Lookup BGP entry. */
3282 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3285 struct listnode
*node
, *nnode
;
3287 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3289 && ((bgp
->name
== NULL
&& name
== NULL
)
3290 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3295 /* Lookup BGP structure by view name. */
3296 struct bgp
*bgp_lookup_by_name(const char *name
)
3299 struct listnode
*node
, *nnode
;
3301 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3302 if ((bgp
->name
== NULL
&& name
== NULL
)
3303 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3308 /* Lookup BGP instance based on VRF id. */
3309 /* Note: Only to be used for incoming messages from Zebra. */
3310 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3314 /* Lookup VRF (in tree) and follow link. */
3315 vrf
= vrf_lookup_by_id(vrf_id
);
3318 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3321 /* Sets the BGP instance where EVPN is enabled */
3322 void bgp_set_evpn(struct bgp
*bgp
)
3324 if (bm
->bgp_evpn
== bgp
)
3327 /* First, release the reference count we hold on the instance */
3329 bgp_unlock(bm
->bgp_evpn
);
3333 /* Increase the reference count on this new VRF */
3335 bgp_lock(bm
->bgp_evpn
);
3338 /* Returns the BGP instance where EVPN is enabled, if any */
3339 struct bgp
*bgp_get_evpn(void)
3341 return bm
->bgp_evpn
;
3344 /* handle socket creation or deletion, if necessary
3345 * this is called for all new BGP instances
3347 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3350 struct listnode
*node
;
3353 /* Create BGP server socket, if listen mode not disabled */
3354 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3356 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3358 * suppress vrf socket
3361 bgp_close_vrf_socket(bgp
);
3365 return BGP_ERR_INVALID_VALUE
;
3367 * if vrf_id did not change
3369 if (vrf
->vrf_id
== old_vrf_id
)
3371 if (old_vrf_id
!= VRF_UNKNOWN
) {
3372 /* look for old socket. close it. */
3373 bgp_close_vrf_socket(bgp
);
3375 /* if backend is not yet identified ( VRF_UNKNOWN) then
3376 * creation will be done later
3378 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3380 if (list_isempty(bm
->addresses
)) {
3381 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3382 return BGP_ERR_INVALID_VALUE
;
3384 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3385 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3386 return BGP_ERR_INVALID_VALUE
;
3390 return bgp_check_main_socket(create
, bgp
);
3393 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3394 enum bgp_instance_type inst_type
)
3398 /* Multiple instance check. */
3400 bgp
= bgp_lookup_by_name(name
);
3402 bgp
= bgp_get_default();
3406 if (bgp
->as
!= *as
) {
3408 return BGP_ERR_AS_MISMATCH
;
3410 if (bgp
->inst_type
!= inst_type
)
3411 return BGP_ERR_INSTANCE_MISMATCH
;
3419 /* Called from VTY commands. */
3420 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3421 enum bgp_instance_type inst_type
)
3424 struct vrf
*vrf
= NULL
;
3427 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3428 if (ret
|| *bgp_val
)
3431 bgp
= bgp_create(as
, name
, inst_type
);
3434 * view instances will never work inside of a vrf
3435 * as such they must always be in the VRF_DEFAULT
3436 * Also we must set this to something useful because
3437 * of the vrf socket code needing an actual useful
3438 * default value to send to the underlying OS.
3440 * This code is currently ignoring vrf based
3441 * code using the -Z option( and that is probably
3442 * best addressed elsewhere in the code )
3444 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3445 bgp
->vrf_id
= VRF_DEFAULT
;
3447 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3448 bgp_address_init(bgp
);
3449 bgp_tip_hash_init(bgp
);
3453 bgp
->t_rmap_def_originate_eval
= NULL
;
3455 /* If Default instance or VRF, link to the VRF structure, if present. */
3456 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3457 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3458 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3460 bgp_vrf_link(bgp
, vrf
);
3462 /* BGP server socket already processed if BGP instance
3463 * already part of the list
3465 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3466 listnode_add(bm
->bgp
, bgp
);
3468 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3469 if (BGP_DEBUG(zebra
, ZEBRA
))
3470 zlog_debug("%s: Registering BGP instance %s to zebra",
3472 bgp_zebra_instance_register(bgp
);
3478 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3479 vrf_id_t vrf_id
, bool set
)
3483 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3486 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3490 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3492 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3496 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3500 struct list
*red_list
;
3501 struct listnode
*node
;
3502 struct bgp_redist
*red
;
3504 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3505 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3507 red_list
= bgp
->redist
[afi
][i
];
3511 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3512 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3519 * Make BGP instance "up". Applies only to VRFs (non-default) and
3520 * implies the VRF has been learnt from Zebra.
3522 void bgp_instance_up(struct bgp
*bgp
)
3525 struct listnode
*node
, *next
;
3527 bgp_set_redist_vrf_bitmaps(bgp
, true);
3529 /* Register with zebra. */
3530 bgp_zebra_instance_register(bgp
);
3532 /* Kick off any peers that may have been configured. */
3533 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3534 if (!BGP_PEER_START_SUPPRESSED(peer
))
3535 BGP_EVENT_ADD(peer
, BGP_Start
);
3538 /* Process any networks that have been configured. */
3539 bgp_static_add(bgp
);
3543 * Make BGP instance "down". Applies only to VRFs (non-default) and
3544 * implies the VRF has been deleted by Zebra.
3546 void bgp_instance_down(struct bgp
*bgp
)
3549 struct listnode
*node
;
3550 struct listnode
*next
;
3553 if (bgp
->t_rmap_def_originate_eval
) {
3554 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3555 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3559 /* Bring down peers, so corresponding routes are purged. */
3560 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3561 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3562 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3563 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3565 bgp_session_reset(peer
);
3568 /* Purge network and redistributed routes. */
3569 bgp_purge_static_redist_routes(bgp
);
3571 /* Cleanup registered nexthops (flags) */
3572 bgp_cleanup_nexthops(bgp
);
3574 bgp_zebra_instance_deregister(bgp
);
3576 bgp_set_redist_vrf_bitmaps(bgp
, false);
3579 /* Delete BGP instance. */
3580 int bgp_delete(struct bgp
*bgp
)
3583 struct peer_group
*group
;
3584 struct listnode
*node
, *next
;
3589 struct graceful_restart_info
*gr_info
;
3593 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3595 /* make sure we withdraw any exported routes */
3596 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3598 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3601 bgp_vpn_leak_unimport(bgp
);
3603 hook_call(bgp_inst_delete
, bgp
);
3605 THREAD_OFF(bgp
->t_condition_check
);
3606 THREAD_OFF(bgp
->t_startup
);
3607 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3608 THREAD_OFF(bgp
->t_update_delay
);
3609 THREAD_OFF(bgp
->t_establish_wait
);
3611 /* Set flag indicating bgp instance delete in progress */
3612 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3614 /* Delete the graceful restart info */
3615 FOREACH_AFI_SAFI (afi
, safi
) {
3618 gr_info
= &bgp
->gr_info
[afi
][safi
];
3621 t
= gr_info
->t_select_deferral
;
3623 void *info
= THREAD_ARG(t
);
3625 XFREE(MTYPE_TMP
, info
);
3627 THREAD_OFF(gr_info
->t_select_deferral
);
3629 t
= gr_info
->t_route_select
;
3631 void *info
= THREAD_ARG(t
);
3633 XFREE(MTYPE_TMP
, info
);
3635 THREAD_OFF(gr_info
->t_route_select
);
3638 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3639 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3640 zlog_debug("Deleting Default VRF");
3642 zlog_debug("Deleting %s %s",
3643 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3649 /* unmap from RT list */
3650 bgp_evpn_vrf_delete(bgp
);
3652 /* unmap bgp vrf label */
3653 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3654 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3657 if (bgp
->t_rmap_def_originate_eval
) {
3658 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3659 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3663 /* Inform peers we're going down. */
3664 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3665 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3666 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3667 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3670 /* Delete static routes (networks). */
3671 bgp_static_delete(bgp
);
3673 /* Unset redistribution. */
3674 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3675 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3676 if (i
!= ZEBRA_ROUTE_BGP
)
3677 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3679 /* Free peers and peer-groups. */
3680 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3681 peer_group_delete(group
);
3683 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3686 if (bgp
->peer_self
) {
3687 peer_delete(bgp
->peer_self
);
3688 bgp
->peer_self
= NULL
;
3691 update_bgp_group_free(bgp
);
3693 /* TODO - Other memory may need to be freed - e.g., NHT */
3695 #ifdef ENABLE_BGP_VNC
3698 bgp_cleanup_routes(bgp
);
3700 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3701 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3704 &bgp
->vpn_policy
[afi
]
3705 .import_redirect_rtlist
);
3706 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3709 /* Free any memory allocated to holding routemap references */
3710 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3711 for (enum vpn_policy_direction dir
= 0;
3712 dir
< BGP_VPN_POLICY_DIR_MAX
; ++dir
) {
3713 if (bgp
->vpn_policy
[afi
].rmap_name
[dir
])
3714 XFREE(MTYPE_ROUTE_MAP_NAME
,
3715 bgp
->vpn_policy
[afi
].rmap_name
[dir
]);
3716 bgp
->vpn_policy
[afi
].rmap
[dir
] = NULL
;
3720 /* Deregister from Zebra, if needed */
3721 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3722 if (BGP_DEBUG(zebra
, ZEBRA
))
3724 "%s: deregistering this bgp %s instance from zebra",
3725 __func__
, bgp
->name
);
3726 bgp_zebra_instance_deregister(bgp
);
3729 /* Remove visibility via the master list - there may however still be
3730 * routes to be processed still referencing the struct bgp.
3732 listnode_delete(bm
->bgp
, bgp
);
3734 /* Free interfaces in this instance. */
3737 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3738 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3740 bgp_vrf_unlink(bgp
, vrf
);
3742 /* Update EVPN VRF pointer */
3743 if (bm
->bgp_evpn
== bgp
) {
3744 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3747 bgp_set_evpn(bgp_get_default());
3750 if (bgp
->process_queue
)
3751 work_queue_free_and_null(&bgp
->process_queue
);
3753 thread_master_free_unused(bm
->master
);
3754 bgp_unlock(bgp
); /* initial reference */
3759 void bgp_free(struct bgp
*bgp
)
3763 struct bgp_table
*table
;
3764 struct bgp_dest
*dest
;
3765 struct bgp_rmap
*rmap
;
3769 list_delete(&bgp
->group
);
3770 list_delete(&bgp
->peer
);
3772 if (bgp
->peerhash
) {
3773 hash_free(bgp
->peerhash
);
3774 bgp
->peerhash
= NULL
;
3777 FOREACH_AFI_SAFI (afi
, safi
) {
3778 /* Special handling for 2-level routing tables. */
3779 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3780 || safi
== SAFI_EVPN
) {
3781 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3782 dest
= bgp_route_next(dest
)) {
3783 table
= bgp_dest_get_bgp_table_info(dest
);
3784 bgp_table_finish(&table
);
3787 if (bgp
->route
[afi
][safi
])
3788 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3789 if (bgp
->aggregate
[afi
][safi
])
3790 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3791 if (bgp
->rib
[afi
][safi
])
3792 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3793 rmap
= &bgp
->table_map
[afi
][safi
];
3794 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3797 bgp_scan_finish(bgp
);
3798 bgp_address_destroy(bgp
);
3799 bgp_tip_hash_destroy(bgp
);
3801 /* release the auto RD id */
3802 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3804 bgp_evpn_cleanup(bgp
);
3805 bgp_pbr_cleanup(bgp
);
3806 bgp_srv6_cleanup(bgp
);
3807 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3809 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3810 enum vpn_policy_direction dir
;
3812 if (bgp
->vpn_policy
[afi
].import_vrf
)
3813 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3814 if (bgp
->vpn_policy
[afi
].export_vrf
)
3815 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3817 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3818 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3819 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3820 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3821 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3822 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3825 XFREE(MTYPE_BGP
, bgp
->name
);
3826 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3827 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3829 XFREE(MTYPE_BGP
, bgp
);
3832 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3835 struct listnode
*node
, *nnode
;
3841 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3842 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3843 && !CHECK_FLAG(peer
->sflags
,
3844 PEER_STATUS_ACCEPT_PEER
))
3846 } else if (bm
->bgp
!= NULL
) {
3847 struct listnode
*bgpnode
, *nbgpnode
;
3849 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3850 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3852 && !strcmp(peer
->conf_if
, conf_if
)
3853 && !CHECK_FLAG(peer
->sflags
,
3854 PEER_STATUS_ACCEPT_PEER
))
3860 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3863 struct listnode
*node
, *nnode
;
3869 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3870 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3871 && !CHECK_FLAG(peer
->sflags
,
3872 PEER_STATUS_ACCEPT_PEER
))
3874 } else if (bm
->bgp
!= NULL
) {
3875 struct listnode
*bgpnode
, *nbgpnode
;
3877 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3878 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3880 && !strcmp(peer
->hostname
, hostname
)
3881 && !CHECK_FLAG(peer
->sflags
,
3882 PEER_STATUS_ACCEPT_PEER
))
3888 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
3890 struct peer
*peer
= NULL
;
3891 struct peer tmp_peer
;
3893 memset(&tmp_peer
, 0, sizeof(struct peer
));
3896 * We do not want to find the doppelganger peer so search for the peer
3898 * the hash that has PEER_FLAG_CONFIG_NODE
3900 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
3905 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
3906 } else if (bm
->bgp
!= NULL
) {
3907 struct listnode
*bgpnode
, *nbgpnode
;
3909 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
3910 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
3919 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
3920 union sockunion
*su
,
3921 struct peer_group
*group
)
3927 /* Create peer first; we've already checked group config is valid. */
3928 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3929 group
->conf
->as_type
, group
);
3934 peer
= peer_lock(peer
);
3935 listnode_add(group
->peer
, peer
);
3937 peer_group2peer_config_copy(group
, peer
);
3940 * Bind peer for all AFs configured for the group. We don't call
3941 * peer_group_bind as that is sub-optimal and does some stuff we don't
3944 FOREACH_AFI_SAFI (afi
, safi
) {
3945 if (!group
->conf
->afc
[afi
][safi
])
3947 peer
->afc
[afi
][safi
] = 1;
3949 if (!peer_af_find(peer
, afi
, safi
))
3950 peer_af_create(peer
, afi
, safi
);
3952 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
3955 /* Mark as dynamic, but also as a "config node" for other things to
3957 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
3958 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3964 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
3965 struct prefix
*prefix
)
3967 struct listnode
*node
, *nnode
;
3968 struct prefix
*range
;
3971 afi
= family2afi(prefix
->family
);
3973 if (group
->listen_range
[afi
])
3974 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
3976 if (prefix_match(range
, prefix
))
3983 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
3984 struct prefix
**listen_range
)
3986 struct prefix
*range
= NULL
;
3987 struct peer_group
*group
= NULL
;
3988 struct listnode
*node
, *nnode
;
3990 *listen_range
= NULL
;
3992 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
3993 if ((range
= peer_group_lookup_dynamic_neighbor_range(
3996 } else if (bm
->bgp
!= NULL
) {
3997 struct listnode
*bgpnode
, *nbgpnode
;
3999 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4000 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4001 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4007 *listen_range
= range
;
4008 return (group
&& range
) ? group
: NULL
;
4011 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
4013 struct peer_group
*group
;
4016 struct prefix prefix
;
4017 struct prefix
*listen_range
;
4020 if (!sockunion2hostprefix(su
, &prefix
))
4023 /* See if incoming connection matches a configured listen range. */
4024 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
4035 if (bgp_debug_neighbor_events(NULL
))
4037 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4038 &prefix
, group
->name
, listen_range
);
4040 /* Are we within the listen limit? */
4041 dncount
= gbgp
->dynamic_neighbors_count
;
4043 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4044 if (bgp_debug_neighbor_events(NULL
))
4046 "Dynamic Neighbor %pFX rejected - at limit %d",
4047 &prefix
, gbgp
->dynamic_neighbors_limit
);
4051 /* Ensure group is not disabled. */
4052 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4053 if (bgp_debug_neighbor_events(NULL
))
4055 "Dynamic Neighbor %pFX rejected - group %s disabled",
4056 &prefix
, group
->name
);
4060 /* Check that at least one AF is activated for the group. */
4061 if (!peer_group_af_configured(group
)) {
4062 if (bgp_debug_neighbor_events(NULL
))
4064 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4065 &prefix
, group
->name
);
4069 /* Create dynamic peer and bind to associated group. */
4070 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4073 gbgp
->dynamic_neighbors_count
= ++dncount
;
4075 if (bgp_debug_neighbor_events(peer
))
4076 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4077 peer
->host
, group
->name
, dncount
);
4082 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4085 if (peer
->group
->bgp
) {
4086 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4088 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4090 if (bgp_debug_neighbor_events(peer
))
4091 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4092 peer
->group
->name
, dncount
);
4095 /* If peer is configured at least one address family return 1. */
4096 bool peer_active(struct peer
*peer
)
4098 if (BGP_PEER_SU_UNSPEC(peer
))
4100 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4101 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4102 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4103 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4104 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4105 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4106 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4107 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4108 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4109 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4110 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4115 /* If peer is negotiated at least one address family return 1. */
4116 bool peer_active_nego(struct peer
*peer
)
4118 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4119 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4120 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4121 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4122 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4123 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4124 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4125 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4126 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4127 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4128 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4129 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4130 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4135 /* If peer received at least one address family MP, return true */
4136 bool peer_afc_received(struct peer
*peer
)
4141 FOREACH_AFI_SAFI (afi
, safi
)
4142 if (peer
->afc_recv
[afi
][safi
])
4148 /* If peer advertised at least one address family MP, return true */
4149 bool peer_afc_advertised(struct peer
*peer
)
4154 FOREACH_AFI_SAFI (afi
, safi
)
4155 if (peer
->afc_adv
[afi
][safi
])
4161 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4162 enum peer_change_type type
)
4164 struct peer_af
*paf
;
4166 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4169 if (!peer_established(peer
))
4172 if (type
== peer_change_reset
) {
4173 /* If we're resetting session, we've to delete both peer struct
4175 if ((peer
->doppelganger
)
4176 && (peer
->doppelganger
->status
!= Deleted
)
4177 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4178 PEER_FLAG_CONFIG_NODE
)))
4179 peer_delete(peer
->doppelganger
);
4181 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4182 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4183 } else if (type
== peer_change_reset_in
) {
4184 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4185 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4186 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4187 BGP_ROUTE_REFRESH_NORMAL
);
4189 if ((peer
->doppelganger
)
4190 && (peer
->doppelganger
->status
!= Deleted
)
4191 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4192 PEER_FLAG_CONFIG_NODE
)))
4193 peer_delete(peer
->doppelganger
);
4195 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4196 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4198 } else if (type
== peer_change_reset_out
) {
4199 paf
= peer_af_find(peer
, afi
, safi
);
4200 if (paf
&& paf
->subgroup
)
4201 SET_FLAG(paf
->subgroup
->sflags
,
4202 SUBGRP_STATUS_FORCE_UPDATES
);
4204 update_group_adjust_peer(paf
);
4205 bgp_announce_route(peer
, afi
, safi
, false);
4209 struct peer_flag_action
{
4213 /* This flag can be set for peer-group member. */
4214 uint8_t not_for_member
;
4216 /* Action when the flag is changed. */
4217 enum peer_change_type type
;
4220 static const struct peer_flag_action peer_flag_action_list
[] = {
4221 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4222 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4223 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4224 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4225 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4226 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4227 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4228 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4229 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4230 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4231 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4232 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4233 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4234 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4235 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4236 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4237 {PEER_FLAG_LOCAL_AS
, 0, peer_change_none
},
4238 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_none
},
4239 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_none
},
4240 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4241 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4242 {PEER_FLAG_EXTENDED_OPT_PARAMS
, 0, peer_change_reset
},
4243 {PEER_FLAG_ROLE_STRICT_MODE
, 0, peer_change_reset
},
4244 {PEER_FLAG_ROLE
, 0, peer_change_reset
},
4245 {PEER_FLAG_PORT
, 0, peer_change_reset
},
4248 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4249 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4250 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4251 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4252 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4253 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4254 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4255 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4256 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4257 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4258 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4259 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4260 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4261 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4262 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4263 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4264 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4265 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4266 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4267 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4268 {PEER_FLAG_MAX_PREFIX_OUT
, 0, peer_change_none
},
4269 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4270 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4271 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4272 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4273 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4274 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4275 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4276 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4277 {PEER_FLAG_SOO
, 0, peer_change_reset
},
4280 /* Proper action set. */
4281 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4282 int size
, struct peer_flag_action
*action
,
4289 const struct peer_flag_action
*match
= NULL
;
4291 /* Check peer's frag action. */
4292 for (i
= 0; i
< size
; i
++) {
4293 match
= &action_list
[i
];
4295 if (match
->flag
== 0)
4298 if (match
->flag
& flag
) {
4301 if (match
->type
== peer_change_reset_in
)
4303 if (match
->type
== peer_change_reset_out
)
4305 if (match
->type
== peer_change_reset
) {
4309 if (match
->not_for_member
)
4310 action
->not_for_member
= 1;
4314 /* Set peer clear type. */
4315 if (reset_in
&& reset_out
)
4316 action
->type
= peer_change_reset
;
4318 action
->type
= peer_change_reset_in
;
4320 action
->type
= peer_change_reset_out
;
4322 action
->type
= peer_change_none
;
4327 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4329 if (flag
== PEER_FLAG_SHUTDOWN
) {
4330 if (CHECK_FLAG(peer
->flags
, flag
)) {
4331 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4332 peer_nsf_stop(peer
);
4334 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4336 if (peer
->t_pmax_restart
) {
4337 THREAD_OFF(peer
->t_pmax_restart
);
4338 if (bgp_debug_neighbor_events(peer
))
4340 "%pBP Maximum-prefix restart timer canceled",
4344 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4345 char *msg
= peer
->tx_shutdown_message
;
4347 uint8_t msgbuf
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4349 if (!msg
&& peer_group_active(peer
))
4350 msg
= peer
->group
->conf
4351 ->tx_shutdown_message
;
4352 msglen
= msg
? strlen(msg
) : 0;
4353 if (msglen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4354 msglen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4358 memcpy(msgbuf
+ 1, msg
, msglen
);
4360 bgp_notify_send_with_data(
4361 peer
, BGP_NOTIFY_CEASE
,
4362 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4363 msgbuf
, msglen
+ 1);
4366 peer
, BGP_NOTIFY_CEASE
,
4367 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4369 bgp_session_reset(peer
);
4371 peer
->v_start
= BGP_INIT_START_TIMER
;
4372 BGP_EVENT_ADD(peer
, BGP_Stop
);
4374 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4375 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4376 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4377 else if (flag
== PEER_FLAG_PASSIVE
)
4378 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4379 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4380 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4382 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4383 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4385 bgp_session_reset(peer
);
4388 /* Enable global administrative shutdown of all peers of BGP instance */
4389 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4392 struct listnode
*node
;
4393 /* length(1) + message(N) */
4394 uint8_t data
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4396 /* do nothing if already shut down */
4397 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4400 /* informational log message */
4401 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4404 /* iterate through peers of BGP instance */
4405 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4406 /* continue, if peer is already in administrative shutdown. */
4407 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4410 /* send a RFC 4486 notification message if necessary */
4411 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4413 size_t datalen
= strlen(msg
);
4415 if (datalen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4416 datalen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4419 memcpy(data
+ 1, msg
, datalen
);
4421 bgp_notify_send_with_data(
4422 peer
, BGP_NOTIFY_CEASE
,
4423 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
, data
,
4427 peer
, BGP_NOTIFY_CEASE
,
4428 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4432 /* reset start timer to initial value */
4433 peer
->v_start
= BGP_INIT_START_TIMER
;
4435 /* trigger a RFC 4271 ManualStop event */
4436 BGP_EVENT_ADD(peer
, BGP_Stop
);
4439 /* set the BGP instances shutdown flag */
4440 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4443 /* Disable global administrative shutdown of all peers of BGP instance */
4444 void bgp_shutdown_disable(struct bgp
*bgp
)
4446 /* do nothing if not shut down. */
4447 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4450 /* informational log message */
4451 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4454 /* clear the BGP instances shutdown flag */
4455 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4458 /* Change specified peer flag. */
4459 static int peer_flag_modify(struct peer
*peer
, uint64_t flag
, int set
)
4463 bool invert
, member_invert
;
4464 struct peer
*member
;
4465 struct listnode
*node
, *nnode
;
4466 struct peer_flag_action action
;
4468 memset(&action
, 0, sizeof(struct peer_flag_action
));
4469 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4471 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4472 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4475 /* Abort if no flag action exists. */
4477 return BGP_ERR_INVALID_FLAG
;
4479 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4480 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4481 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4482 return BGP_ERR_PEER_FLAG_CONFLICT
;
4484 /* Handle flag updates where desired state matches current state. */
4485 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4486 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4487 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4491 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4492 COND_FLAG(peer
->flags_override
, flag
, invert
);
4497 /* Inherit from peer-group or set/unset flags accordingly. */
4498 if (peer_group_active(peer
) && set
== invert
)
4499 peer_flag_inherit(peer
, flag
);
4501 COND_FLAG(peer
->flags
, flag
, set
);
4503 /* Check if handling a regular peer. */
4504 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4505 /* Update flag override state accordingly. */
4506 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4509 * For the extended next-hop encoding flag we need to turn RAs
4510 * on if flag is being set, but only turn RAs off if the flag
4511 * is being unset on this peer and if this peer is a member of a
4512 * peer-group, the peer-group also doesn't have the flag set.
4514 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4516 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4517 } else if (peer_group_active(peer
)) {
4518 if (!CHECK_FLAG(peer
->group
->conf
->flags
,
4521 bgp_zebra_terminate_radv(peer
->bgp
,
4524 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4527 /* Execute flag action on peer. */
4528 if (action
.type
== peer_change_reset
)
4529 peer_flag_modify_action(peer
, flag
);
4531 /* Skip peer-group mechanics for regular peers. */
4536 * Update peer-group members, unless they are explicitly overriding
4537 * peer-group configuration.
4539 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4540 /* Skip peers with overridden configuration. */
4541 if (CHECK_FLAG(member
->flags_override
, flag
))
4544 /* Check if only member without group is inverted. */
4546 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4548 /* Skip peers with equivalent configuration. */
4549 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4552 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4555 /* Update flag on peer-group member. */
4556 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4558 if (flag
== PEER_FLAG_CAPABILITY_ENHE
&& !member
->conf_if
)
4559 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4560 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4562 /* Execute flag action on peer-group member. */
4563 if (action
.type
== peer_change_reset
)
4564 peer_flag_modify_action(member
, flag
);
4570 int peer_flag_set(struct peer
*peer
, uint64_t flag
)
4572 return peer_flag_modify(peer
, flag
, 1);
4575 int peer_flag_unset(struct peer
*peer
, uint64_t flag
)
4577 return peer_flag_modify(peer
, flag
, 0);
4580 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4581 uint64_t flag
, bool set
)
4585 bool invert
, member_invert
;
4586 struct peer
*member
;
4587 struct listnode
*node
, *nnode
;
4588 struct peer_flag_action action
;
4589 enum bgp_peer_sort ptype
;
4591 memset(&action
, 0, sizeof(struct peer_flag_action
));
4592 size
= sizeof(peer_af_flag_action_list
)
4593 / sizeof(struct peer_flag_action
);
4595 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4596 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4599 /* Abort if flag action exists. */
4601 return BGP_ERR_INVALID_FLAG
;
4603 ptype
= peer_sort(peer
);
4604 /* Special check for reflector client. */
4605 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4606 return BGP_ERR_NOT_INTERNAL_PEER
;
4608 /* Special check for remove-private-AS. */
4609 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4610 return BGP_ERR_REMOVE_PRIVATE_AS
;
4612 /* as-override is not allowed for IBGP peers */
4613 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4614 return BGP_ERR_AS_OVERRIDE
;
4616 /* Handle flag updates where desired state matches current state. */
4617 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4618 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4619 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4624 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4625 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4632 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4633 * if we are setting/unsetting flags which conflict with this flag
4634 * handle accordingly
4636 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4640 * if we are setting NEXTHOP_SELF, we need to unset the
4641 * NEXTHOP_UNCHANGED flag
4643 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4644 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4645 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4646 PEER_FLAG_NEXTHOP_UNCHANGED
);
4650 * if we are unsetting NEXTHOP_SELF, we need to set the
4651 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4653 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4654 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4655 SET_FLAG(peer
->af_flags
[afi
][safi
],
4656 PEER_FLAG_NEXTHOP_UNCHANGED
);
4661 * If the peer is a route server client let's not
4662 * muck with the nexthop on the way out the door
4664 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4666 SET_FLAG(peer
->af_flags
[afi
][safi
],
4667 PEER_FLAG_NEXTHOP_UNCHANGED
);
4669 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4670 PEER_FLAG_NEXTHOP_UNCHANGED
);
4673 /* Inherit from peer-group or set/unset flags accordingly. */
4674 if (peer_group_active(peer
) && set
== invert
)
4675 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4677 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4679 /* Execute action when peer is established. */
4680 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4681 && peer_established(peer
)) {
4682 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4683 bgp_clear_adj_in(peer
, afi
, safi
);
4685 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4686 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4687 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4688 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4689 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4690 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4691 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4692 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4694 peer_change_action(peer
, afi
, safi
, action
.type
);
4698 /* Check if handling a regular peer. */
4699 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4700 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4704 * Update peer-group members, unless they are explicitly
4705 * overriding peer-group configuration.
4707 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4709 /* Skip peers with overridden configuration. */
4710 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4714 /* Check if only member without group is inverted. */
4716 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4720 /* Skip peers with equivalent configuration. */
4721 if (set
!= member_invert
4722 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4725 if (set
== member_invert
4726 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4729 /* Update flag on peer-group member. */
4730 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4731 set
!= member_invert
);
4733 /* Execute flag action on peer-group member. */
4734 if (peer_established(member
)) {
4735 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4736 bgp_clear_adj_in(member
, afi
, safi
);
4738 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4739 member
->last_reset
=
4740 PEER_DOWN_RR_CLIENT_CHANGE
;
4742 == PEER_FLAG_RSERVER_CLIENT
)
4743 member
->last_reset
=
4744 PEER_DOWN_RS_CLIENT_CHANGE
;
4746 == PEER_FLAG_ORF_PREFIX_SM
)
4747 member
->last_reset
=
4748 PEER_DOWN_CAPABILITY_CHANGE
;
4750 == PEER_FLAG_ORF_PREFIX_RM
)
4751 member
->last_reset
=
4752 PEER_DOWN_CAPABILITY_CHANGE
;
4754 peer_change_action(member
, afi
, safi
,
4764 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4766 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4769 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4771 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4775 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4777 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4778 peer
->tx_shutdown_message
=
4779 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4782 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4784 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4788 /* EBGP multihop configuration. */
4789 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4791 struct peer_group
*group
;
4792 struct listnode
*node
, *nnode
;
4795 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4798 /* is there anything to do? */
4799 if (peer
->ttl
== ttl
)
4802 /* see comment in peer_ttl_security_hops_set() */
4803 if (ttl
!= MAXTTL
) {
4804 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4805 group
= peer
->group
;
4806 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4807 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4809 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4811 if (peer1
->sort
== BGP_PEER_IBGP
)
4814 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4815 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4818 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4819 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4825 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4826 if (peer
->sort
!= BGP_PEER_IBGP
) {
4827 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4828 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4829 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4831 bgp_session_reset(peer
);
4833 /* Reconfigure BFD peer with new TTL. */
4834 if (peer
->bfd_config
)
4835 bgp_peer_bfd_update_source(peer
);
4838 group
= peer
->group
;
4839 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4840 if (peer
->sort
== BGP_PEER_IBGP
)
4843 peer
->ttl
= group
->conf
->ttl
;
4845 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4846 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4847 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4849 bgp_session_reset(peer
);
4851 /* Reconfigure BFD peer with new TTL. */
4852 if (peer
->bfd_config
)
4853 bgp_peer_bfd_update_source(peer
);
4859 int peer_ebgp_multihop_unset(struct peer
*peer
)
4861 struct peer_group
*group
;
4862 struct listnode
*node
, *nnode
;
4865 if (peer
->sort
== BGP_PEER_IBGP
)
4868 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
4869 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4871 if (peer_group_active(peer
))
4872 ttl
= peer
->group
->conf
->ttl
;
4874 ttl
= BGP_DEFAULT_TTL
;
4876 if (ttl
== peer
->ttl
)
4881 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4882 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4883 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4884 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4886 bgp_session_reset(peer
);
4888 /* Reconfigure BFD peer with new TTL. */
4889 if (peer
->bfd_config
)
4890 bgp_peer_bfd_update_source(peer
);
4892 group
= peer
->group
;
4893 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4894 if (peer
->sort
== BGP_PEER_IBGP
)
4897 peer
->ttl
= BGP_DEFAULT_TTL
;
4899 if (peer
->fd
>= 0) {
4900 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4902 peer
, BGP_NOTIFY_CEASE
,
4903 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4905 bgp_session_reset(peer
);
4908 /* Reconfigure BFD peer with new TTL. */
4909 if (peer
->bfd_config
)
4910 bgp_peer_bfd_update_source(peer
);
4916 /* Set Open Policy Role and check its correctness */
4917 int peer_role_set(struct peer
*peer
, uint8_t role
, bool strict_mode
)
4919 struct peer
*member
;
4920 struct listnode
*node
, *nnode
;
4922 peer_flag_set(peer
, PEER_FLAG_ROLE
);
4924 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4925 if (peer
->sort
!= BGP_PEER_EBGP
)
4926 return BGP_ERR_INVALID_INTERNAL_ROLE
;
4928 if (peer
->local_role
== role
) {
4929 if (CHECK_FLAG(peer
->flags
,
4930 PEER_FLAG_ROLE_STRICT_MODE
) &&
4932 /* TODO: Is session restart needed if it was
4935 UNSET_FLAG(peer
->flags
,
4936 PEER_FLAG_ROLE_STRICT_MODE
);
4937 if (!CHECK_FLAG(peer
->flags
,
4938 PEER_FLAG_ROLE_STRICT_MODE
) &&
4940 SET_FLAG(peer
->flags
,
4941 PEER_FLAG_ROLE_STRICT_MODE
);
4942 /* Restart session to throw Role Mismatch
4945 if (peer
->remote_role
== ROLE_UNDEFINED
)
4946 bgp_session_reset(peer
);
4949 peer
->local_role
= role
;
4951 SET_FLAG(peer
->flags
,
4952 PEER_FLAG_ROLE_STRICT_MODE
);
4954 UNSET_FLAG(peer
->flags
,
4955 PEER_FLAG_ROLE_STRICT_MODE
);
4956 bgp_session_reset(peer
);
4962 peer
->local_role
= role
;
4963 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4964 if (member
->sort
!= BGP_PEER_EBGP
)
4965 return BGP_ERR_INVALID_INTERNAL_ROLE
;
4967 if (member
->local_role
== role
) {
4968 if (CHECK_FLAG(member
->flags
,
4969 PEER_FLAG_ROLE_STRICT_MODE
) &&
4971 /* TODO: Is session restart needed if it was
4974 UNSET_FLAG(member
->flags
,
4975 PEER_FLAG_ROLE_STRICT_MODE
);
4976 if (!CHECK_FLAG(member
->flags
,
4977 PEER_FLAG_ROLE_STRICT_MODE
) &&
4979 SET_FLAG(peer
->flags
,
4980 PEER_FLAG_ROLE_STRICT_MODE
);
4981 SET_FLAG(member
->flags
,
4982 PEER_FLAG_ROLE_STRICT_MODE
);
4983 /* Restart session to throw Role Mismatch
4986 if (member
->remote_role
== ROLE_UNDEFINED
)
4987 bgp_session_reset(member
);
4990 member
->local_role
= role
;
4993 SET_FLAG(peer
->flags
,
4994 PEER_FLAG_ROLE_STRICT_MODE
);
4995 SET_FLAG(member
->flags
,
4996 PEER_FLAG_ROLE_STRICT_MODE
);
4998 UNSET_FLAG(member
->flags
,
4999 PEER_FLAG_ROLE_STRICT_MODE
);
5001 bgp_session_reset(member
);
5008 int peer_role_unset(struct peer
*peer
)
5010 struct peer
*member
;
5011 struct listnode
*node
, *nnode
;
5013 peer_flag_unset(peer
, PEER_FLAG_ROLE
);
5015 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5016 return peer_role_set(peer
, ROLE_UNDEFINED
, 0);
5018 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
))
5019 peer_role_set(member
, ROLE_UNDEFINED
, 0);
5024 /* Neighbor description. */
5025 void peer_description_set(struct peer
*peer
, const char *desc
)
5027 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5029 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
5032 void peer_description_unset(struct peer
*peer
)
5034 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5037 /* Neighbor update-source. */
5038 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
5040 struct peer
*member
;
5041 struct listnode
*node
, *nnode
;
5043 /* Set flag and configuration on peer. */
5044 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5045 if (peer
->update_if
) {
5046 if (strcmp(peer
->update_if
, ifname
) == 0)
5048 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5050 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5051 sockunion_free(peer
->update_source
);
5052 peer
->update_source
= NULL
;
5054 /* Check if handling a regular peer. */
5055 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5056 /* Send notification or reset peer depending on state. */
5057 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5058 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5059 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5060 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5062 bgp_session_reset(peer
);
5064 /* Apply new source configuration to BFD session. */
5065 if (peer
->bfd_config
)
5066 bgp_peer_bfd_update_source(peer
);
5068 /* Skip peer-group mechanics for regular peers. */
5073 * Set flag and configuration on all peer-group members, unless they are
5074 * explicitly overriding peer-group configuration.
5076 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5077 /* Skip peers with overridden configuration. */
5078 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5081 /* Skip peers with the same configuration. */
5082 if (member
->update_if
) {
5083 if (strcmp(member
->update_if
, ifname
) == 0)
5085 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5088 /* Set flag and configuration on peer-group member. */
5089 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5090 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5091 sockunion_free(member
->update_source
);
5092 member
->update_source
= NULL
;
5094 /* Send notification or reset peer depending on state. */
5095 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5096 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5097 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5098 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5100 bgp_session_reset(member
);
5102 /* Apply new source configuration to BFD session. */
5103 if (member
->bfd_config
)
5104 bgp_peer_bfd_update_source(member
);
5110 void peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
5112 struct peer
*member
;
5113 struct listnode
*node
, *nnode
;
5115 /* Set flag and configuration on peer. */
5116 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5117 if (peer
->update_source
) {
5118 if (sockunion_cmp(peer
->update_source
, su
) == 0)
5120 sockunion_free(peer
->update_source
);
5122 peer
->update_source
= sockunion_dup(su
);
5123 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5125 /* Check if handling a regular peer. */
5126 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5127 /* Send notification or reset peer depending on state. */
5128 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5129 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5130 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5131 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5133 bgp_session_reset(peer
);
5135 /* Apply new source configuration to BFD session. */
5136 if (peer
->bfd_config
)
5137 bgp_peer_bfd_update_source(peer
);
5139 /* Skip peer-group mechanics for regular peers. */
5144 * Set flag and configuration on all peer-group members, unless they are
5145 * explicitly overriding peer-group configuration.
5147 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5148 /* Skip peers with overridden configuration. */
5149 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5152 /* Skip peers with the same configuration. */
5153 if (member
->update_source
) {
5154 if (sockunion_cmp(member
->update_source
, su
) == 0)
5156 sockunion_free(member
->update_source
);
5159 /* Set flag and configuration on peer-group member. */
5160 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5161 member
->update_source
= sockunion_dup(su
);
5162 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5164 /* Send notification or reset peer depending on state. */
5165 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5166 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5167 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5168 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5170 bgp_session_reset(member
);
5172 /* Apply new source configuration to BFD session. */
5173 if (member
->bfd_config
)
5174 bgp_peer_bfd_update_source(member
);
5178 void peer_update_source_unset(struct peer
*peer
)
5180 struct peer
*member
;
5181 struct listnode
*node
, *nnode
;
5183 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
5186 /* Inherit configuration from peer-group if peer is member. */
5187 if (peer_group_active(peer
)) {
5188 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
5189 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
5190 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
5191 MTYPE_PEER_UPDATE_SOURCE
);
5193 /* Otherwise remove flag and configuration from peer. */
5194 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
5195 sockunion_free(peer
->update_source
);
5196 peer
->update_source
= NULL
;
5197 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5200 /* Check if handling a regular peer. */
5201 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5202 /* Send notification or reset peer depending on state. */
5203 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5204 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5205 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5206 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5208 bgp_session_reset(peer
);
5210 /* Apply new source configuration to BFD session. */
5211 if (peer
->bfd_config
)
5212 bgp_peer_bfd_update_source(peer
);
5214 /* Skip peer-group mechanics for regular peers. */
5219 * Set flag and configuration on all peer-group members, unless they are
5220 * explicitly overriding peer-group configuration.
5222 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5223 /* Skip peers with overridden configuration. */
5224 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5227 /* Skip peers with the same configuration. */
5228 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5229 && !member
->update_source
&& !member
->update_if
)
5232 /* Remove flag and configuration on peer-group member. */
5233 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5234 sockunion_free(member
->update_source
);
5235 member
->update_source
= NULL
;
5236 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5238 /* Send notification or reset peer depending on state. */
5239 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5240 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5241 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5242 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5244 bgp_session_reset(member
);
5246 /* Apply new source configuration to BFD session. */
5247 if (member
->bfd_config
)
5248 bgp_peer_bfd_update_source(member
);
5252 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5253 const char *rmap
, struct route_map
*route_map
)
5255 struct peer
*member
;
5256 struct listnode
*node
, *nnode
;
5257 struct update_subgroup
*subgrp
;
5259 /* Set flag and configuration on peer. */
5260 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5262 subgrp
= peer_subgroup(peer
, afi
, safi
);
5265 if (!peer
->default_rmap
[afi
][safi
].name
5266 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5267 if (peer
->default_rmap
[afi
][safi
].name
)
5268 XFREE(MTYPE_ROUTE_MAP_NAME
,
5269 peer
->default_rmap
[afi
][safi
].name
);
5272 * When there is a change in route-map policy,
5273 * this flow gets triggered. Since, the default
5274 * route is already originated, the flag is set.
5275 * The flag should be unset here,
5276 * to trigger the flow of sending update message.
5279 UNSET_FLAG(subgrp
->sflags
,
5280 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5282 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5283 peer
->default_rmap
[afi
][safi
].name
=
5284 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5285 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5286 route_map_counter_increment(route_map
);
5289 if (peer
->default_rmap
[afi
][safi
].name
)
5290 XFREE(MTYPE_ROUTE_MAP_NAME
,
5291 peer
->default_rmap
[afi
][safi
].name
);
5294 * This is triggered in case of route-map deletion.
5295 * The flag needs to be unset, to trigger the flow
5296 * of sending an update message.
5299 UNSET_FLAG(subgrp
->sflags
,
5300 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5302 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5303 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5304 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5307 /* Check if handling a regular peer. */
5308 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5309 /* Update peer route announcements. */
5310 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5311 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5312 bgp_default_originate(peer
, afi
, safi
, 0);
5313 bgp_announce_route(peer
, afi
, safi
, false);
5316 /* Skip peer-group mechanics for regular peers. */
5321 * Set flag and configuration on all peer-group members, unless they are
5322 * explicitly overriding peer-group configuration.
5324 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5325 /* Skip peers with overridden configuration. */
5326 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5327 PEER_FLAG_DEFAULT_ORIGINATE
))
5330 /* Set flag and configuration on peer-group member. */
5331 SET_FLAG(member
->af_flags
[afi
][safi
],
5332 PEER_FLAG_DEFAULT_ORIGINATE
);
5334 if (member
->default_rmap
[afi
][safi
].name
)
5335 XFREE(MTYPE_ROUTE_MAP_NAME
,
5336 member
->default_rmap
[afi
][safi
].name
);
5337 route_map_counter_decrement(
5338 member
->default_rmap
[afi
][safi
].map
);
5339 member
->default_rmap
[afi
][safi
].name
=
5340 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5341 member
->default_rmap
[afi
][safi
].map
= route_map
;
5342 route_map_counter_increment(route_map
);
5345 /* Update peer route announcements. */
5346 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5347 update_group_adjust_peer(
5348 peer_af_find(member
, afi
, safi
));
5349 bgp_default_originate(member
, afi
, safi
, 0);
5350 bgp_announce_route(member
, afi
, safi
, false);
5357 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5359 struct peer
*member
;
5360 struct listnode
*node
, *nnode
;
5362 /* Inherit configuration from peer-group if peer is member. */
5363 if (peer_group_active(peer
)) {
5364 peer_af_flag_inherit(peer
, afi
, safi
,
5365 PEER_FLAG_DEFAULT_ORIGINATE
);
5366 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5367 default_rmap
[afi
][safi
].name
,
5368 MTYPE_ROUTE_MAP_NAME
);
5369 PEER_ATTR_INHERIT(peer
, peer
->group
,
5370 default_rmap
[afi
][safi
].map
);
5372 /* Otherwise remove flag and configuration from peer. */
5373 peer_af_flag_unset(peer
, afi
, safi
,
5374 PEER_FLAG_DEFAULT_ORIGINATE
);
5375 if (peer
->default_rmap
[afi
][safi
].name
)
5376 XFREE(MTYPE_ROUTE_MAP_NAME
,
5377 peer
->default_rmap
[afi
][safi
].name
);
5378 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5379 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5380 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5383 /* Check if handling a regular peer. */
5384 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5385 /* Update peer route announcements. */
5386 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5387 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5388 bgp_default_originate(peer
, afi
, safi
, 1);
5389 bgp_announce_route(peer
, afi
, safi
, false);
5392 /* Skip peer-group mechanics for regular peers. */
5397 * Remove flag and configuration from all peer-group members, unless
5398 * they are explicitly overriding peer-group configuration.
5400 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5401 /* Skip peers with overridden configuration. */
5402 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5403 PEER_FLAG_DEFAULT_ORIGINATE
))
5406 /* Remove flag and configuration on peer-group member. */
5407 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5408 PEER_FLAG_DEFAULT_ORIGINATE
);
5409 if (member
->default_rmap
[afi
][safi
].name
)
5410 XFREE(MTYPE_ROUTE_MAP_NAME
,
5411 member
->default_rmap
[afi
][safi
].name
);
5412 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5413 member
->default_rmap
[afi
][safi
].name
= NULL
;
5414 member
->default_rmap
[afi
][safi
].map
= NULL
;
5416 /* Update peer route announcements. */
5417 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5418 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5419 bgp_default_originate(member
, afi
, safi
, 1);
5420 bgp_announce_route(member
, afi
, safi
, false);
5427 void peer_port_set(struct peer
*peer
, uint16_t port
)
5430 peer_flag_set(peer
, PEER_FLAG_PORT
);
5433 void peer_port_unset(struct peer
*peer
)
5435 peer
->port
= BGP_PORT_DEFAULT
;
5436 peer_flag_unset(peer
, PEER_FLAG_PORT
);
5439 /* Set the TCP-MSS value in the peer structure,
5440 * This gets applied only after connection reset
5441 * So this value will be used in bgp_connect.
5443 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5445 peer
->tcp_mss
= tcp_mss
;
5446 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5449 /* Reset the TCP-MSS value in the peer structure,
5450 * This gets applied only after connection reset
5451 * So this value will be used in bgp_connect.
5453 void peer_tcp_mss_unset(struct peer
*peer
)
5455 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5460 * Helper function that is called after the name of the policy
5461 * being used by a peer has changed (AF specific). Automatically
5462 * initiates inbound or outbound processing as needed.
5464 void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5468 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5469 if (peer_established(peer
))
5470 bgp_announce_route(peer
, afi
, safi
, false);
5472 if (!peer_established(peer
))
5475 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5476 PEER_FLAG_SOFT_RECONFIG
)) {
5477 bgp_soft_reconfig_in(peer
, afi
, safi
);
5478 } else if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
) ||
5479 CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
)) {
5480 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5481 PEER_CAP_ORF_PREFIX_SM_ADV
) &&
5482 (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5483 PEER_CAP_ORF_PREFIX_RM_RCV
) ||
5484 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5485 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
)))
5486 peer_clear_soft(peer
, afi
, safi
,
5487 BGP_CLEAR_SOFT_IN_ORF_PREFIX
);
5489 bgp_route_refresh_send(
5490 peer
, afi
, safi
, 0, 0, 0,
5491 BGP_ROUTE_REFRESH_NORMAL
);
5497 /* neighbor weight. */
5498 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5500 struct peer
*member
;
5501 struct listnode
*node
, *nnode
;
5503 /* Set flag and configuration on peer. */
5504 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5505 if (peer
->weight
[afi
][safi
] != weight
) {
5506 peer
->weight
[afi
][safi
] = weight
;
5507 peer_on_policy_change(peer
, afi
, safi
, 0);
5510 /* Skip peer-group mechanics for regular peers. */
5511 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5515 * Set flag and configuration on all peer-group members, unless they are
5516 * explicitly overriding peer-group configuration.
5518 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5519 /* Skip peers with overridden configuration. */
5520 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5524 /* Set flag and configuration on peer-group member. */
5525 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5526 if (member
->weight
[afi
][safi
] != weight
) {
5527 member
->weight
[afi
][safi
] = weight
;
5528 peer_on_policy_change(member
, afi
, safi
, 0);
5535 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5537 struct peer
*member
;
5538 struct listnode
*node
, *nnode
;
5540 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5543 /* Inherit configuration from peer-group if peer is member. */
5544 if (peer_group_active(peer
)) {
5545 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5546 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5548 peer_on_policy_change(peer
, afi
, safi
, 0);
5552 /* Remove flag and configuration from peer. */
5553 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5554 peer
->weight
[afi
][safi
] = 0;
5555 peer_on_policy_change(peer
, afi
, safi
, 0);
5557 /* Skip peer-group mechanics for regular peers. */
5558 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5562 * Remove flag and configuration from all peer-group members, unless
5563 * they are explicitly overriding peer-group configuration.
5565 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5566 /* Skip peers with overridden configuration. */
5567 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5571 /* Skip peers where flag is already disabled. */
5572 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5575 /* Remove flag and configuration on peer-group member. */
5576 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5577 member
->weight
[afi
][safi
] = 0;
5578 peer_on_policy_change(member
, afi
, safi
, 0);
5584 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5586 struct peer
*member
;
5587 struct listnode
*node
, *nnode
;
5589 if (keepalive
> UINT16_MAX
)
5590 return BGP_ERR_INVALID_VALUE
;
5592 if (holdtime
> UINT16_MAX
)
5593 return BGP_ERR_INVALID_VALUE
;
5595 if (holdtime
< 3 && holdtime
!= 0)
5596 return BGP_ERR_INVALID_VALUE
;
5598 /* Set flag and configuration on peer. */
5599 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5600 peer
->holdtime
= holdtime
;
5601 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5603 /* Skip peer-group mechanics for regular peers. */
5604 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5608 * Set flag and configuration on all peer-group members, unless they are
5609 * explicitly overriding peer-group configuration.
5611 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5612 /* Skip peers with overridden configuration. */
5613 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5616 /* Set flag and configuration on peer-group member. */
5617 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5618 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5619 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5625 int peer_timers_unset(struct peer
*peer
)
5627 struct peer
*member
;
5628 struct listnode
*node
, *nnode
;
5630 /* Inherit configuration from peer-group if peer is member. */
5631 if (peer_group_active(peer
)) {
5632 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5633 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5634 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5636 /* Otherwise remove flag and configuration from peer. */
5637 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5639 peer
->keepalive
= 0;
5642 /* Skip peer-group mechanics for regular peers. */
5643 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5647 * Remove flag and configuration from all peer-group members, unless
5648 * they are explicitly overriding peer-group configuration.
5650 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5651 /* Skip peers with overridden configuration. */
5652 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5655 /* Remove flag and configuration on peer-group member. */
5656 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5657 member
->holdtime
= 0;
5658 member
->keepalive
= 0;
5664 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5666 struct peer
*member
;
5667 struct listnode
*node
, *nnode
;
5669 if (connect
> UINT16_MAX
)
5670 return BGP_ERR_INVALID_VALUE
;
5672 /* Set flag and configuration on peer. */
5673 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5674 peer
->connect
= connect
;
5675 peer
->v_connect
= connect
;
5677 /* Skip peer-group mechanics for regular peers. */
5678 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5679 if (!peer_established(peer
)) {
5680 if (peer_active(peer
))
5681 BGP_EVENT_ADD(peer
, BGP_Stop
);
5682 BGP_EVENT_ADD(peer
, BGP_Start
);
5687 * Set flag and configuration on all peer-group members, unless they are
5688 * explicitly overriding peer-group configuration.
5690 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5691 /* Skip peers with overridden configuration. */
5692 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5695 /* Set flag and configuration on peer-group member. */
5696 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5697 member
->connect
= connect
;
5698 member
->v_connect
= connect
;
5700 if (!peer_established(member
)) {
5701 if (peer_active(member
))
5702 BGP_EVENT_ADD(member
, BGP_Stop
);
5703 BGP_EVENT_ADD(member
, BGP_Start
);
5710 int peer_timers_connect_unset(struct peer
*peer
)
5712 struct peer
*member
;
5713 struct listnode
*node
, *nnode
;
5715 /* Inherit configuration from peer-group if peer is member. */
5716 if (peer_group_active(peer
)) {
5717 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5718 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5720 /* Otherwise remove flag and configuration from peer. */
5721 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5725 /* Set timer with fallback to default value. */
5727 peer
->v_connect
= peer
->connect
;
5729 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5731 /* Skip peer-group mechanics for regular peers. */
5732 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5733 if (!peer_established(peer
)) {
5734 if (peer_active(peer
))
5735 BGP_EVENT_ADD(peer
, BGP_Stop
);
5736 BGP_EVENT_ADD(peer
, BGP_Start
);
5741 * Remove flag and configuration from all peer-group members, unless
5742 * they are explicitly overriding peer-group configuration.
5744 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5745 /* Skip peers with overridden configuration. */
5746 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5749 /* Remove flag and configuration on peer-group member. */
5750 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5751 member
->connect
= 0;
5752 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5754 if (!peer_established(member
)) {
5755 if (peer_active(member
))
5756 BGP_EVENT_ADD(member
, BGP_Stop
);
5757 BGP_EVENT_ADD(member
, BGP_Start
);
5764 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5766 struct peer
*member
;
5767 struct listnode
*node
, *nnode
;
5770 return BGP_ERR_INVALID_VALUE
;
5772 /* Set flag and configuration on peer. */
5773 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5774 peer
->routeadv
= routeadv
;
5775 peer
->v_routeadv
= routeadv
;
5777 /* Check if handling a regular peer. */
5778 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5779 /* Update peer route announcements. */
5780 update_group_adjust_peer_afs(peer
);
5781 if (peer_established(peer
))
5782 bgp_announce_route_all(peer
);
5784 /* Skip peer-group mechanics for regular peers. */
5789 * Set flag and configuration on all peer-group members, unless they are
5790 * explicitly overriding peer-group configuration.
5792 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5793 /* Skip peers with overridden configuration. */
5794 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5797 /* Set flag and configuration on peer-group member. */
5798 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5799 member
->routeadv
= routeadv
;
5800 member
->v_routeadv
= routeadv
;
5802 /* Update peer route announcements. */
5803 update_group_adjust_peer_afs(member
);
5804 if (peer_established(member
))
5805 bgp_announce_route_all(member
);
5811 int peer_advertise_interval_unset(struct peer
*peer
)
5813 struct peer
*member
;
5814 struct listnode
*node
, *nnode
;
5816 /* Inherit configuration from peer-group if peer is member. */
5817 if (peer_group_active(peer
)) {
5818 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5819 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5821 /* Otherwise remove flag and configuration from peer. */
5822 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5826 /* Set timer with fallback to default value. */
5828 peer
->v_routeadv
= peer
->routeadv
;
5830 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5831 ? BGP_DEFAULT_IBGP_ROUTEADV
5832 : BGP_DEFAULT_EBGP_ROUTEADV
;
5834 /* Check if handling a regular peer. */
5835 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5836 /* Update peer route announcements. */
5837 update_group_adjust_peer_afs(peer
);
5838 if (peer_established(peer
))
5839 bgp_announce_route_all(peer
);
5841 /* Skip peer-group mechanics for regular peers. */
5846 * Remove flag and configuration from all peer-group members, unless
5847 * they are explicitly overriding peer-group configuration.
5849 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5850 /* Skip peers with overridden configuration. */
5851 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5854 /* Remove flag and configuration on peer-group member. */
5855 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5856 member
->routeadv
= 0;
5857 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
5858 ? BGP_DEFAULT_IBGP_ROUTEADV
5859 : BGP_DEFAULT_EBGP_ROUTEADV
;
5861 /* Update peer route announcements. */
5862 update_group_adjust_peer_afs(member
);
5863 if (peer_established(member
))
5864 bgp_announce_route_all(member
);
5870 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
5873 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
5875 struct peer
*member
;
5876 struct listnode
*node
;
5878 /* Set peers session attribute flag and timer interval. */
5879 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5880 peer
->delayopen
= delayopen
;
5881 peer
->v_delayopen
= delayopen
;
5883 /* Skip group mechanics for regular peers. */
5884 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5887 /* Set flag and configuration on all peer-group members, unless they are
5888 * explicitly overriding peer-group configuration.
5890 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
5891 /* Skip peers with overridden configuration. */
5892 if (CHECK_FLAG(member
->flags_override
,
5893 PEER_FLAG_TIMER_DELAYOPEN
))
5896 /* Set session attribute flag and timer intervals on peer-group
5899 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
5900 member
->delayopen
= delayopen
;
5901 member
->v_delayopen
= delayopen
;
5907 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
5908 * DelayOpenTimer interval to the default value.
5910 int peer_timers_delayopen_unset(struct peer
*peer
)
5912 struct peer
*member
;
5913 struct listnode
*node
;
5915 /* Inherit configuration from peer-group if peer is member. */
5916 if (peer_group_active(peer
)) {
5917 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5918 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
5920 /* Otherwise remove session attribute flag and set timer
5921 * interval to default value.
5923 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5924 peer
->delayopen
= peer
->bgp
->default_delayopen
;
5927 /* Set timer value to zero */
5928 peer
->v_delayopen
= 0;
5930 /* Skip peer-group mechanics for regular peers. */
5931 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5934 /* Remove flag and configuration from all peer-group members, unless
5935 * they are explicitly overriding peer-group configuration.
5937 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
5938 /* Skip peers with overridden configuration. */
5939 if (CHECK_FLAG(member
->flags_override
,
5940 PEER_FLAG_TIMER_DELAYOPEN
))
5943 /* Remove session attribute flag, reset the timer interval to
5944 * the default value and set the timer value to zero.
5946 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
5947 member
->delayopen
= peer
->bgp
->default_delayopen
;
5948 member
->v_delayopen
= 0;
5954 /* neighbor interface */
5955 void peer_interface_set(struct peer
*peer
, const char *str
)
5957 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
5958 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
5961 void peer_interface_unset(struct peer
*peer
)
5963 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
5967 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5968 int allow_num
, int origin
)
5970 struct peer
*member
;
5971 struct listnode
*node
, *nnode
;
5973 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
5974 return BGP_ERR_INVALID_VALUE
;
5976 /* Set flag and configuration on peer. */
5977 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
5979 if (peer
->allowas_in
[afi
][safi
] != 0
5980 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5981 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5982 peer_af_flag_set(peer
, afi
, safi
,
5983 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5984 peer
->allowas_in
[afi
][safi
] = 0;
5985 peer_on_policy_change(peer
, afi
, safi
, 0);
5988 if (peer
->allowas_in
[afi
][safi
] != allow_num
5989 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5990 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5992 peer_af_flag_unset(peer
, afi
, safi
,
5993 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5994 peer
->allowas_in
[afi
][safi
] = allow_num
;
5995 peer_on_policy_change(peer
, afi
, safi
, 0);
5999 /* Skip peer-group mechanics for regular peers. */
6000 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6004 * Set flag and configuration on all peer-group members, unless
6005 * they are explicitly overriding peer-group configuration.
6007 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6008 /* Skip peers with overridden configuration. */
6009 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6010 PEER_FLAG_ALLOWAS_IN
))
6013 /* Set flag and configuration on peer-group member. */
6014 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6016 if (member
->allowas_in
[afi
][safi
] != 0
6017 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
6018 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6019 SET_FLAG(member
->af_flags
[afi
][safi
],
6020 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6021 member
->allowas_in
[afi
][safi
] = 0;
6022 peer_on_policy_change(peer
, afi
, safi
, 0);
6025 if (member
->allowas_in
[afi
][safi
] != allow_num
6026 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
6027 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6028 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6029 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6030 member
->allowas_in
[afi
][safi
] = allow_num
;
6031 peer_on_policy_change(peer
, afi
, safi
, 0);
6039 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6041 struct peer
*member
;
6042 struct listnode
*node
, *nnode
;
6044 /* Skip peer if flag is already disabled. */
6045 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
6048 /* Inherit configuration from peer-group if peer is member. */
6049 if (peer_group_active(peer
)) {
6050 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6051 peer_af_flag_inherit(peer
, afi
, safi
,
6052 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6053 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
6054 peer_on_policy_change(peer
, afi
, safi
, 0);
6059 /* Remove flag and configuration from peer. */
6060 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6061 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6062 peer
->allowas_in
[afi
][safi
] = 0;
6063 peer_on_policy_change(peer
, afi
, safi
, 0);
6065 /* Skip peer-group mechanics if handling a regular peer. */
6066 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6070 * Remove flags and configuration from all peer-group members, unless
6071 * they are explicitly overriding peer-group configuration.
6073 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6074 /* Skip peers with overridden configuration. */
6075 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6076 PEER_FLAG_ALLOWAS_IN
))
6079 /* Remove flags and configuration on peer-group member. */
6080 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6081 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6082 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6083 member
->allowas_in
[afi
][safi
] = 0;
6084 peer_on_policy_change(member
, afi
, safi
, 0);
6090 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
6093 bool old_no_prepend
, old_replace_as
;
6094 struct bgp
*bgp
= peer
->bgp
;
6095 struct peer
*member
;
6096 struct listnode
*node
, *nnode
;
6099 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
6101 /* Save previous flag states. */
6103 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6105 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6107 /* Set flag and configuration on peer. */
6108 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
6109 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
6110 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
6112 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
6113 && old_replace_as
== replace_as
)
6115 peer
->change_local_as
= as
;
6116 (void)peer_sort(peer
);
6118 /* Check if handling a regular peer. */
6119 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6120 /* Send notification or reset peer depending on state. */
6121 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6122 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6123 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6124 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6126 bgp_session_reset(peer
);
6128 /* Skip peer-group mechanics for regular peers. */
6133 * Set flag and configuration on all peer-group members, unless they are
6134 * explicitly overriding peer-group configuration.
6136 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6137 /* Skip peers with overridden configuration. */
6138 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6141 /* Skip peers with the same configuration. */
6142 old_no_prepend
= CHECK_FLAG(member
->flags
,
6143 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6144 old_replace_as
= CHECK_FLAG(member
->flags
,
6145 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6146 if (member
->change_local_as
== as
6147 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
6148 && old_no_prepend
== no_prepend
6149 && old_replace_as
== replace_as
)
6152 /* Set flag and configuration on peer-group member. */
6153 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6154 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
6156 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
6158 member
->change_local_as
= as
;
6160 /* Send notification or stop peer depending on state. */
6161 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6162 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6163 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6164 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6166 BGP_EVENT_ADD(member
, BGP_Stop
);
6172 int peer_local_as_unset(struct peer
*peer
)
6174 struct peer
*member
;
6175 struct listnode
*node
, *nnode
;
6177 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
6180 /* Inherit configuration from peer-group if peer is member. */
6181 if (peer_group_active(peer
)) {
6182 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
6183 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6184 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6185 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
6187 /* Otherwise remove flag and configuration from peer. */
6188 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
6189 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6190 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6191 peer
->change_local_as
= 0;
6194 /* Check if handling a regular peer. */
6195 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6196 /* Send notification or stop peer depending on state. */
6197 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6198 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6199 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6200 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6202 BGP_EVENT_ADD(peer
, BGP_Stop
);
6204 /* Skip peer-group mechanics for regular peers. */
6209 * Remove flag and configuration from all peer-group members, unless
6210 * they are explicitly overriding peer-group configuration.
6212 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6213 /* Skip peers with overridden configuration. */
6214 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6217 /* Remove flag and configuration on peer-group member. */
6218 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6219 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6220 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6221 member
->change_local_as
= 0;
6223 /* Send notification or stop peer depending on state. */
6224 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6225 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6226 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6227 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6229 bgp_session_reset(member
);
6235 /* Set password for authenticating with the peer. */
6236 int peer_password_set(struct peer
*peer
, const char *password
)
6238 struct peer
*member
;
6239 struct listnode
*node
, *nnode
;
6240 int len
= password
? strlen(password
) : 0;
6241 int ret
= BGP_SUCCESS
;
6243 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
6244 return BGP_ERR_INVALID_VALUE
;
6246 /* Set flag and configuration on peer. */
6247 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
6248 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
6250 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6251 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6253 /* Check if handling a regular peer. */
6254 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6255 /* Send notification or reset peer depending on state. */
6256 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6257 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6258 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6260 bgp_session_reset(peer
);
6263 * Attempt to install password on socket and skip peer-group
6266 if (BGP_PEER_SU_UNSPEC(peer
))
6268 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6269 : BGP_ERR_TCPSIG_FAILED
;
6273 * Set flag and configuration on all peer-group members, unless they are
6274 * explicitly overriding peer-group configuration.
6276 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6277 /* Skip peers with overridden configuration. */
6278 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6281 /* Skip peers with the same password. */
6282 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6285 /* Set flag and configuration on peer-group member. */
6286 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6287 if (member
->password
)
6288 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6289 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6291 /* Send notification or reset peer depending on state. */
6292 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6293 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6294 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6296 bgp_session_reset(member
);
6298 /* Attempt to install password on socket. */
6299 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6300 ret
= BGP_ERR_TCPSIG_FAILED
;
6303 /* Set flag and configuration on all peer-group listen ranges */
6304 struct listnode
*ln
;
6307 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6308 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6309 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6310 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6315 int peer_password_unset(struct peer
*peer
)
6317 struct peer
*member
;
6318 struct listnode
*node
, *nnode
;
6320 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6323 /* Inherit configuration from peer-group if peer is member. */
6324 if (peer_group_active(peer
)) {
6325 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6326 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6327 MTYPE_PEER_PASSWORD
);
6329 /* Otherwise remove flag and configuration from peer. */
6330 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6331 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6334 /* Check if handling a regular peer. */
6335 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6336 /* Send notification or reset peer depending on state. */
6337 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6338 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6339 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6341 bgp_session_reset(peer
);
6343 /* Attempt to uninstall password on socket. */
6344 if (!BGP_PEER_SU_UNSPEC(peer
))
6345 bgp_md5_unset(peer
);
6346 /* Skip peer-group mechanics for regular peers. */
6351 * Remove flag and configuration from all peer-group members, unless
6352 * they are explicitly overriding peer-group configuration.
6354 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6355 /* Skip peers with overridden configuration. */
6356 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6359 /* Remove flag and configuration on peer-group member. */
6360 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6361 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6363 /* Send notification or reset peer depending on state. */
6364 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6365 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6366 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6368 bgp_session_reset(member
);
6370 /* Attempt to uninstall password on socket. */
6371 if (!BGP_PEER_SU_UNSPEC(member
))
6372 bgp_md5_unset(member
);
6375 /* Set flag and configuration on all peer-group listen ranges */
6376 struct listnode
*ln
;
6379 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6380 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6381 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6382 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6388 /* Set distribute list to the peer. */
6389 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6392 struct peer
*member
;
6393 struct bgp_filter
*filter
;
6394 struct listnode
*node
, *nnode
;
6396 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6397 return BGP_ERR_INVALID_VALUE
;
6399 /* Set configuration on peer. */
6400 filter
= &peer
->filter
[afi
][safi
];
6401 if (filter
->plist
[direct
].name
)
6402 return BGP_ERR_PEER_FILTER_CONFLICT
;
6403 if (filter
->dlist
[direct
].name
)
6404 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6405 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6406 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6408 /* Check if handling a regular peer. */
6409 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6410 /* Set override-flag and process peer route updates. */
6411 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6412 PEER_FT_DISTRIBUTE_LIST
);
6413 peer_on_policy_change(peer
, afi
, safi
,
6414 (direct
== FILTER_OUT
) ? 1 : 0);
6416 /* Skip peer-group mechanics for regular peers. */
6421 * Set configuration on all peer-group members, un less they are
6422 * explicitly overriding peer-group configuration.
6424 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6425 /* Skip peers with overridden configuration. */
6426 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6427 PEER_FT_DISTRIBUTE_LIST
))
6430 /* Set configuration on peer-group member. */
6431 filter
= &member
->filter
[afi
][safi
];
6432 if (filter
->dlist
[direct
].name
)
6433 XFREE(MTYPE_BGP_FILTER_NAME
,
6434 filter
->dlist
[direct
].name
);
6435 filter
->dlist
[direct
].name
=
6436 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6437 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6439 /* Process peer route updates. */
6440 peer_on_policy_change(member
, afi
, safi
,
6441 (direct
== FILTER_OUT
) ? 1 : 0);
6447 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6449 struct peer
*member
;
6450 struct bgp_filter
*filter
;
6451 struct listnode
*node
, *nnode
;
6453 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6454 return BGP_ERR_INVALID_VALUE
;
6456 /* Unset override-flag unconditionally. */
6457 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6458 PEER_FT_DISTRIBUTE_LIST
);
6460 /* Inherit configuration from peer-group if peer is member. */
6461 if (peer_group_active(peer
)) {
6462 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6463 filter
[afi
][safi
].dlist
[direct
].name
,
6464 MTYPE_BGP_FILTER_NAME
);
6465 PEER_ATTR_INHERIT(peer
, peer
->group
,
6466 filter
[afi
][safi
].dlist
[direct
].alist
);
6468 /* Otherwise remove configuration from peer. */
6469 filter
= &peer
->filter
[afi
][safi
];
6470 if (filter
->dlist
[direct
].name
)
6471 XFREE(MTYPE_BGP_FILTER_NAME
,
6472 filter
->dlist
[direct
].name
);
6473 filter
->dlist
[direct
].name
= NULL
;
6474 filter
->dlist
[direct
].alist
= NULL
;
6477 /* Check if handling a regular peer. */
6478 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6479 /* Process peer route updates. */
6480 peer_on_policy_change(peer
, afi
, safi
,
6481 (direct
== FILTER_OUT
) ? 1 : 0);
6483 /* Skip peer-group mechanics for regular peers. */
6488 * Remove configuration on all peer-group members, unless they are
6489 * explicitly overriding peer-group configuration.
6491 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6492 /* Skip peers with overridden configuration. */
6493 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6494 PEER_FT_DISTRIBUTE_LIST
))
6497 /* Remove configuration on peer-group member. */
6498 filter
= &member
->filter
[afi
][safi
];
6499 if (filter
->dlist
[direct
].name
)
6500 XFREE(MTYPE_BGP_FILTER_NAME
,
6501 filter
->dlist
[direct
].name
);
6502 filter
->dlist
[direct
].name
= NULL
;
6503 filter
->dlist
[direct
].alist
= NULL
;
6505 /* Process peer route updates. */
6506 peer_on_policy_change(member
, afi
, safi
,
6507 (direct
== FILTER_OUT
) ? 1 : 0);
6513 /* Update distribute list. */
6514 static void peer_distribute_update(struct access_list
*access
)
6519 struct listnode
*mnode
, *mnnode
;
6520 struct listnode
*node
, *nnode
;
6523 struct peer_group
*group
;
6524 struct bgp_filter
*filter
;
6526 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6528 update_group_policy_update(bgp
,
6529 BGP_POLICY_DISTRIBUTE_LIST
,
6530 access
->name
, true, 0);
6531 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6532 FOREACH_AFI_SAFI (afi
, safi
) {
6533 filter
= &peer
->filter
[afi
][safi
];
6535 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6537 if (filter
->dlist
[direct
].name
)
6538 filter
->dlist
[direct
]
6539 .alist
= access_list_lookup(
6541 filter
->dlist
[direct
]
6544 filter
->dlist
[direct
].alist
=
6549 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6550 FOREACH_AFI_SAFI (afi
, safi
) {
6551 filter
= &group
->conf
->filter
[afi
][safi
];
6553 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6555 if (filter
->dlist
[direct
].name
)
6556 filter
->dlist
[direct
]
6557 .alist
= access_list_lookup(
6559 filter
->dlist
[direct
]
6562 filter
->dlist
[direct
].alist
=
6567 #ifdef ENABLE_BGP_VNC
6568 vnc_prefix_list_update(bgp
);
6573 /* Set prefix list to the peer. */
6574 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6577 struct peer
*member
;
6578 struct bgp_filter
*filter
;
6579 struct listnode
*node
, *nnode
;
6581 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6582 return BGP_ERR_INVALID_VALUE
;
6584 /* Set configuration on peer. */
6585 filter
= &peer
->filter
[afi
][safi
];
6586 if (filter
->dlist
[direct
].name
)
6587 return BGP_ERR_PEER_FILTER_CONFLICT
;
6588 if (filter
->plist
[direct
].name
)
6589 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6590 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6591 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6593 /* Check if handling a regular peer. */
6594 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6595 /* Set override-flag and process peer route updates. */
6596 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6597 PEER_FT_PREFIX_LIST
);
6598 peer_on_policy_change(peer
, afi
, safi
,
6599 (direct
== FILTER_OUT
) ? 1 : 0);
6601 /* Skip peer-group mechanics for regular peers. */
6606 * Set configuration on all peer-group members, unless they are
6607 * explicitly overriding peer-group configuration.
6609 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6610 /* Skip peers with overridden configuration. */
6611 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6612 PEER_FT_PREFIX_LIST
))
6615 /* Set configuration on peer-group member. */
6616 filter
= &member
->filter
[afi
][safi
];
6617 if (filter
->plist
[direct
].name
)
6618 XFREE(MTYPE_BGP_FILTER_NAME
,
6619 filter
->plist
[direct
].name
);
6620 filter
->plist
[direct
].name
=
6621 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6622 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6624 /* Process peer route updates. */
6625 peer_on_policy_change(member
, afi
, safi
,
6626 (direct
== FILTER_OUT
) ? 1 : 0);
6632 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6635 struct peer
*member
;
6636 struct bgp_filter
*filter
;
6637 struct listnode
*node
, *nnode
;
6639 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6640 return BGP_ERR_INVALID_VALUE
;
6642 /* Unset override-flag unconditionally. */
6643 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6644 PEER_FT_PREFIX_LIST
);
6646 /* Inherit configuration from peer-group if peer is member. */
6647 if (peer_group_active(peer
)) {
6648 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6649 filter
[afi
][safi
].plist
[direct
].name
,
6650 MTYPE_BGP_FILTER_NAME
);
6651 PEER_ATTR_INHERIT(peer
, peer
->group
,
6652 filter
[afi
][safi
].plist
[direct
].plist
);
6654 /* Otherwise remove configuration from peer. */
6655 filter
= &peer
->filter
[afi
][safi
];
6656 if (filter
->plist
[direct
].name
)
6657 XFREE(MTYPE_BGP_FILTER_NAME
,
6658 filter
->plist
[direct
].name
);
6659 filter
->plist
[direct
].name
= NULL
;
6660 filter
->plist
[direct
].plist
= NULL
;
6663 /* Check if handling a regular peer. */
6664 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6665 /* Process peer route updates. */
6666 peer_on_policy_change(peer
, afi
, safi
,
6667 (direct
== FILTER_OUT
) ? 1 : 0);
6669 /* Skip peer-group mechanics for regular peers. */
6674 * Remove configuration on all peer-group members, unless they are
6675 * explicitly overriding peer-group configuration.
6677 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6678 /* Skip peers with overridden configuration. */
6679 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6680 PEER_FT_PREFIX_LIST
))
6683 /* Remove configuration on peer-group member. */
6684 filter
= &member
->filter
[afi
][safi
];
6685 if (filter
->plist
[direct
].name
)
6686 XFREE(MTYPE_BGP_FILTER_NAME
,
6687 filter
->plist
[direct
].name
);
6688 filter
->plist
[direct
].name
= NULL
;
6689 filter
->plist
[direct
].plist
= NULL
;
6691 /* Process peer route updates. */
6692 peer_on_policy_change(member
, afi
, safi
,
6693 (direct
== FILTER_OUT
) ? 1 : 0);
6699 /* Update prefix-list list. */
6700 static void peer_prefix_list_update(struct prefix_list
*plist
)
6702 struct listnode
*mnode
, *mnnode
;
6703 struct listnode
*node
, *nnode
;
6706 struct peer_group
*group
;
6707 struct bgp_filter
*filter
;
6712 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6715 * Update the prefix-list on update groups.
6717 update_group_policy_update(
6718 bgp
, BGP_POLICY_PREFIX_LIST
,
6719 plist
? prefix_list_name(plist
) : NULL
, true, 0);
6721 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6722 FOREACH_AFI_SAFI (afi
, safi
) {
6723 filter
= &peer
->filter
[afi
][safi
];
6725 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6727 if (filter
->plist
[direct
].name
)
6728 filter
->plist
[direct
]
6729 .plist
= prefix_list_lookup(
6731 filter
->plist
[direct
]
6734 filter
->plist
[direct
].plist
=
6738 /* If we touch prefix-list, we need to process
6739 * new updates. This is important for ORF to
6740 * work correctly as well.
6742 if (peer
->afc_nego
[afi
][safi
])
6743 peer_on_policy_change(peer
, afi
, safi
,
6747 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6748 FOREACH_AFI_SAFI (afi
, safi
) {
6749 filter
= &group
->conf
->filter
[afi
][safi
];
6751 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6753 if (filter
->plist
[direct
].name
)
6754 filter
->plist
[direct
]
6755 .plist
= prefix_list_lookup(
6757 filter
->plist
[direct
]
6760 filter
->plist
[direct
].plist
=
6768 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6771 struct peer
*member
;
6772 struct bgp_filter
*filter
;
6773 struct listnode
*node
, *nnode
;
6775 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6776 return BGP_ERR_INVALID_VALUE
;
6778 /* Set configuration on peer. */
6779 filter
= &peer
->filter
[afi
][safi
];
6780 if (filter
->aslist
[direct
].name
)
6781 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6782 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6783 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6785 /* Check if handling a regular peer. */
6786 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6787 /* Set override-flag and process peer route updates. */
6788 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6789 PEER_FT_FILTER_LIST
);
6790 peer_on_policy_change(peer
, afi
, safi
,
6791 (direct
== FILTER_OUT
) ? 1 : 0);
6793 /* Skip peer-group mechanics for regular peers. */
6798 * Set configuration on all peer-group members, unless they are
6799 * explicitly overriding peer-group configuration.
6801 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6802 /* Skip peers with overridden configuration. */
6803 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6804 PEER_FT_FILTER_LIST
))
6807 /* Set configuration on peer-group member. */
6808 filter
= &member
->filter
[afi
][safi
];
6809 if (filter
->aslist
[direct
].name
)
6810 XFREE(MTYPE_BGP_FILTER_NAME
,
6811 filter
->aslist
[direct
].name
);
6812 filter
->aslist
[direct
].name
=
6813 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6814 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6816 /* Process peer route updates. */
6817 peer_on_policy_change(member
, afi
, safi
,
6818 (direct
== FILTER_OUT
) ? 1 : 0);
6824 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6826 struct peer
*member
;
6827 struct bgp_filter
*filter
;
6828 struct listnode
*node
, *nnode
;
6830 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6831 return BGP_ERR_INVALID_VALUE
;
6833 /* Unset override-flag unconditionally. */
6834 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6835 PEER_FT_FILTER_LIST
);
6837 /* Inherit configuration from peer-group if peer is member. */
6838 if (peer_group_active(peer
)) {
6839 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6840 filter
[afi
][safi
].aslist
[direct
].name
,
6841 MTYPE_BGP_FILTER_NAME
);
6842 PEER_ATTR_INHERIT(peer
, peer
->group
,
6843 filter
[afi
][safi
].aslist
[direct
].aslist
);
6845 /* Otherwise remove configuration from peer. */
6846 filter
= &peer
->filter
[afi
][safi
];
6847 if (filter
->aslist
[direct
].name
)
6848 XFREE(MTYPE_BGP_FILTER_NAME
,
6849 filter
->aslist
[direct
].name
);
6850 filter
->aslist
[direct
].name
= NULL
;
6851 filter
->aslist
[direct
].aslist
= NULL
;
6854 /* Check if handling a regular peer. */
6855 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6856 /* Process peer route updates. */
6857 peer_on_policy_change(peer
, afi
, safi
,
6858 (direct
== FILTER_OUT
) ? 1 : 0);
6860 /* Skip peer-group mechanics for regular peers. */
6865 * Remove configuration on all peer-group members, unless they are
6866 * explicitly overriding peer-group configuration.
6868 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6869 /* Skip peers with overridden configuration. */
6870 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6871 PEER_FT_FILTER_LIST
))
6874 /* Remove configuration on peer-group member. */
6875 filter
= &member
->filter
[afi
][safi
];
6876 if (filter
->aslist
[direct
].name
)
6877 XFREE(MTYPE_BGP_FILTER_NAME
,
6878 filter
->aslist
[direct
].name
);
6879 filter
->aslist
[direct
].name
= NULL
;
6880 filter
->aslist
[direct
].aslist
= NULL
;
6882 /* Process peer route updates. */
6883 peer_on_policy_change(member
, afi
, safi
,
6884 (direct
== FILTER_OUT
) ? 1 : 0);
6890 static void peer_aslist_update(const char *aslist_name
)
6895 struct listnode
*mnode
, *mnnode
;
6896 struct listnode
*node
, *nnode
;
6899 struct peer_group
*group
;
6900 struct bgp_filter
*filter
;
6902 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6903 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
6904 aslist_name
, true, 0);
6906 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6907 FOREACH_AFI_SAFI (afi
, safi
) {
6908 filter
= &peer
->filter
[afi
][safi
];
6910 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6912 if (filter
->aslist
[direct
].name
)
6913 filter
->aslist
[direct
]
6914 .aslist
= as_list_lookup(
6915 filter
->aslist
[direct
]
6918 filter
->aslist
[direct
].aslist
=
6923 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6924 FOREACH_AFI_SAFI (afi
, safi
) {
6925 filter
= &group
->conf
->filter
[afi
][safi
];
6927 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6929 if (filter
->aslist
[direct
].name
)
6930 filter
->aslist
[direct
]
6931 .aslist
= as_list_lookup(
6932 filter
->aslist
[direct
]
6935 filter
->aslist
[direct
].aslist
=
6943 static void peer_aslist_add(char *aslist_name
)
6945 peer_aslist_update(aslist_name
);
6946 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
6949 static void peer_aslist_del(const char *aslist_name
)
6951 peer_aslist_update(aslist_name
);
6952 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
6956 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6957 const char *name
, struct route_map
*route_map
)
6959 struct peer
*member
;
6960 struct bgp_filter
*filter
;
6961 struct listnode
*node
, *nnode
;
6963 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
6964 return BGP_ERR_INVALID_VALUE
;
6966 /* Set configuration on peer. */
6967 filter
= &peer
->filter
[afi
][safi
];
6968 if (filter
->map
[direct
].name
) {
6969 /* If the neighbor is configured with the same route-map
6970 * again then, ignore the duplicate configuration.
6972 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
6975 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6977 route_map_counter_decrement(filter
->map
[direct
].map
);
6978 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6979 filter
->map
[direct
].map
= route_map
;
6980 route_map_counter_increment(route_map
);
6982 /* Check if handling a regular peer. */
6983 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6984 /* Set override-flag and process peer route updates. */
6985 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6987 peer_on_policy_change(peer
, afi
, safi
,
6988 (direct
== RMAP_OUT
) ? 1 : 0);
6990 /* Skip peer-group mechanics for regular peers. */
6995 * Set configuration on all peer-group members, unless they are
6996 * explicitly overriding peer-group configuration.
6998 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6999 /* Skip peers with overridden configuration. */
7000 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7004 /* Set configuration on peer-group member. */
7005 filter
= &member
->filter
[afi
][safi
];
7006 if (filter
->map
[direct
].name
)
7007 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7008 route_map_counter_decrement(filter
->map
[direct
].map
);
7009 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7010 filter
->map
[direct
].map
= route_map
;
7011 route_map_counter_increment(route_map
);
7013 /* Process peer route updates. */
7014 peer_on_policy_change(member
, afi
, safi
,
7015 (direct
== RMAP_OUT
) ? 1 : 0);
7020 /* Unset route-map from the peer. */
7021 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
7023 struct peer
*member
;
7024 struct bgp_filter
*filter
;
7025 struct listnode
*node
, *nnode
;
7027 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7028 return BGP_ERR_INVALID_VALUE
;
7030 /* Unset override-flag unconditionally. */
7031 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
7033 /* Inherit configuration from peer-group if peer is member. */
7034 if (peer_group_active(peer
)) {
7035 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7036 filter
[afi
][safi
].map
[direct
].name
,
7037 MTYPE_BGP_FILTER_NAME
);
7038 PEER_ATTR_INHERIT(peer
, peer
->group
,
7039 filter
[afi
][safi
].map
[direct
].map
);
7041 /* Otherwise remove configuration from peer. */
7042 filter
= &peer
->filter
[afi
][safi
];
7043 if (filter
->map
[direct
].name
)
7044 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7045 route_map_counter_decrement(filter
->map
[direct
].map
);
7046 filter
->map
[direct
].name
= NULL
;
7047 filter
->map
[direct
].map
= NULL
;
7050 /* Check if handling a regular peer. */
7051 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7052 /* Process peer route updates. */
7053 peer_on_policy_change(peer
, afi
, safi
,
7054 (direct
== RMAP_OUT
) ? 1 : 0);
7056 /* Skip peer-group mechanics for regular peers. */
7061 * Remove configuration on all peer-group members, unless they are
7062 * explicitly overriding peer-group configuration.
7064 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7065 /* Skip peers with overridden configuration. */
7066 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7070 /* Remove configuration on peer-group member. */
7071 filter
= &member
->filter
[afi
][safi
];
7072 if (filter
->map
[direct
].name
)
7073 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7074 route_map_counter_decrement(filter
->map
[direct
].map
);
7075 filter
->map
[direct
].name
= NULL
;
7076 filter
->map
[direct
].map
= NULL
;
7078 /* Process peer route updates. */
7079 peer_on_policy_change(member
, afi
, safi
,
7080 (direct
== RMAP_OUT
) ? 1 : 0);
7086 /* Set unsuppress-map to the peer. */
7087 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7088 const char *name
, struct route_map
*route_map
)
7090 struct peer
*member
;
7091 struct bgp_filter
*filter
;
7092 struct listnode
*node
, *nnode
;
7094 /* Set configuration on peer. */
7095 filter
= &peer
->filter
[afi
][safi
];
7096 if (filter
->usmap
.name
)
7097 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7098 route_map_counter_decrement(filter
->usmap
.map
);
7099 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7100 filter
->usmap
.map
= route_map
;
7101 route_map_counter_increment(route_map
);
7103 /* Check if handling a regular peer. */
7104 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7105 /* Set override-flag and process peer route updates. */
7106 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
7107 PEER_FT_UNSUPPRESS_MAP
);
7108 peer_on_policy_change(peer
, afi
, safi
, 1);
7110 /* Skip peer-group mechanics for regular peers. */
7115 * Set configuration on all peer-group members, unless they are
7116 * explicitly overriding peer-group configuration.
7118 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7119 /* Skip peers with overridden configuration. */
7120 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7121 PEER_FT_UNSUPPRESS_MAP
))
7124 /* Set configuration on peer-group member. */
7125 filter
= &member
->filter
[afi
][safi
];
7126 if (filter
->usmap
.name
)
7127 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7128 route_map_counter_decrement(filter
->usmap
.map
);
7129 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7130 filter
->usmap
.map
= route_map
;
7131 route_map_counter_increment(route_map
);
7133 /* Process peer route updates. */
7134 peer_on_policy_change(member
, afi
, safi
, 1);
7140 /* Unset route-map from the peer. */
7141 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7143 struct peer
*member
;
7144 struct bgp_filter
*filter
;
7145 struct listnode
*node
, *nnode
;
7147 /* Unset override-flag unconditionally. */
7148 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
7150 /* Inherit configuration from peer-group if peer is member. */
7151 if (peer_group_active(peer
)) {
7152 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7153 filter
[afi
][safi
].usmap
.name
,
7154 MTYPE_BGP_FILTER_NAME
);
7155 PEER_ATTR_INHERIT(peer
, peer
->group
,
7156 filter
[afi
][safi
].usmap
.map
);
7158 /* Otherwise remove configuration from peer. */
7159 filter
= &peer
->filter
[afi
][safi
];
7160 if (filter
->usmap
.name
)
7161 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7162 route_map_counter_decrement(filter
->usmap
.map
);
7163 filter
->usmap
.name
= NULL
;
7164 filter
->usmap
.map
= NULL
;
7167 /* Check if handling a regular peer. */
7168 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7169 /* Process peer route updates. */
7170 peer_on_policy_change(peer
, afi
, safi
, 1);
7172 /* Skip peer-group mechanics for regular peers. */
7177 * Remove configuration on all peer-group members, unless they are
7178 * explicitly overriding peer-group configuration.
7180 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7181 /* Skip peers with overridden configuration. */
7182 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7183 PEER_FT_UNSUPPRESS_MAP
))
7186 /* Remove configuration on peer-group member. */
7187 filter
= &member
->filter
[afi
][safi
];
7188 if (filter
->usmap
.name
)
7189 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7190 route_map_counter_decrement(filter
->usmap
.map
);
7191 filter
->usmap
.name
= NULL
;
7192 filter
->usmap
.map
= NULL
;
7194 /* Process peer route updates. */
7195 peer_on_policy_change(member
, afi
, safi
, 1);
7201 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7203 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7206 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7207 if (peer
->t_pmax_restart
) {
7208 THREAD_OFF(peer
->t_pmax_restart
);
7209 if (bgp_debug_neighbor_events(peer
))
7211 "%pBP Maximum-prefix restart timer cancelled",
7214 BGP_EVENT_ADD(peer
, BGP_Start
);
7218 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7219 uint32_t max
, uint8_t threshold
, int warning
,
7220 uint16_t restart
, bool force
)
7222 struct peer
*member
;
7223 struct listnode
*node
, *nnode
;
7225 /* Set flags and configuration on peer. */
7226 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7229 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7231 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7234 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7236 peer_af_flag_unset(peer
, afi
, safi
,
7237 PEER_FLAG_MAX_PREFIX_WARNING
);
7239 peer
->pmax
[afi
][safi
] = max
;
7240 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7241 peer
->pmax_restart
[afi
][safi
] = restart
;
7243 /* Check if handling a regular peer. */
7244 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7245 /* Re-check if peer violates maximum-prefix. */
7246 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7247 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7249 /* Skip peer-group mechanics for regular peers. */
7254 * Set flags and configuration on all peer-group members, unless they
7255 * are explicitly overriding peer-group configuration.
7257 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7258 /* Skip peers with overridden configuration. */
7259 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7260 PEER_FLAG_MAX_PREFIX
))
7263 /* Set flag and configuration on peer-group member. */
7264 member
->pmax
[afi
][safi
] = max
;
7265 member
->pmax_threshold
[afi
][safi
] = threshold
;
7266 member
->pmax_restart
[afi
][safi
] = restart
;
7269 SET_FLAG(member
->af_flags
[afi
][safi
],
7270 PEER_FLAG_MAX_PREFIX_FORCE
);
7272 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7273 PEER_FLAG_MAX_PREFIX_FORCE
);
7276 SET_FLAG(member
->af_flags
[afi
][safi
],
7277 PEER_FLAG_MAX_PREFIX_WARNING
);
7279 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7280 PEER_FLAG_MAX_PREFIX_WARNING
);
7282 /* Re-check if peer violates maximum-prefix. */
7283 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7284 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7290 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7292 /* Inherit configuration from peer-group if peer is member. */
7293 if (peer_group_active(peer
)) {
7294 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7295 peer_af_flag_inherit(peer
, afi
, safi
,
7296 PEER_FLAG_MAX_PREFIX_FORCE
);
7297 peer_af_flag_inherit(peer
, afi
, safi
,
7298 PEER_FLAG_MAX_PREFIX_WARNING
);
7299 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7300 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7301 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7306 /* Remove flags and configuration from peer. */
7307 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7308 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7309 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7310 peer
->pmax
[afi
][safi
] = 0;
7311 peer
->pmax_threshold
[afi
][safi
] = 0;
7312 peer
->pmax_restart
[afi
][safi
] = 0;
7315 * Remove flags and configuration from all peer-group members, unless
7316 * they are explicitly overriding peer-group configuration.
7318 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7319 struct peer
*member
;
7320 struct listnode
*node
;
7322 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7323 /* Skip peers with overridden configuration. */
7324 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7325 PEER_FLAG_MAX_PREFIX
))
7328 /* Remove flag and configuration on peer-group member.
7330 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7331 PEER_FLAG_MAX_PREFIX
);
7332 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7333 PEER_FLAG_MAX_PREFIX_FORCE
);
7334 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7335 PEER_FLAG_MAX_PREFIX_WARNING
);
7336 member
->pmax
[afi
][safi
] = 0;
7337 member
->pmax_threshold
[afi
][safi
] = 0;
7338 member
->pmax_restart
[afi
][safi
] = 0;
7340 peer_maximum_prefix_clear_overflow(member
);
7343 peer_maximum_prefix_clear_overflow(peer
);
7349 void peer_maximum_prefix_out_refresh_routes(struct peer
*peer
, afi_t afi
,
7352 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
7354 if (peer_established(peer
))
7355 bgp_announce_route(peer
, afi
, safi
, false);
7358 int peer_maximum_prefix_out_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7361 struct peer
*member
;
7362 struct listnode
*node
, *nnode
;
7364 /* Set flag on peer and peer-group member if any */
7365 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7366 /* Set configuration on peer. */
7367 peer
->pmax_out
[afi
][safi
] = max
;
7369 /* Check if handling a regular peer. */
7370 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7371 /* Skip peer-group mechanics for regular peers. */
7372 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7377 * Set flag and configuration on all peer-group members, unless they
7378 * are explicitly overriding peer-group configuration.
7380 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7381 /* Skip peers with overridden configuration. */
7382 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7383 PEER_FLAG_MAX_PREFIX_OUT
))
7386 /* Set configuration on peer-group member. */
7387 member
->pmax_out
[afi
][safi
] = max
;
7389 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7394 int peer_maximum_prefix_out_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7396 struct peer
*member
;
7397 struct listnode
*node
;
7398 /* Inherit configuration from peer-group if peer is member. */
7399 if (peer_group_active(peer
)) {
7400 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7401 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_out
[afi
][safi
]);
7403 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7407 /* Remove flag and configuration from peer. */
7408 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7409 peer
->pmax_out
[afi
][safi
] = 0;
7411 /* Check if handling a regular peer. */
7412 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7413 /* Skip peer-group mechanics for regular peers. */
7414 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7419 * Remove flag and configuration from all peer-group members, unless
7420 * they are explicitly overriding peer-group configuration.
7422 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7423 /* Skip peers with overridden configuration. */
7424 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7425 PEER_FLAG_MAX_PREFIX_OUT
))
7428 /* Remove flag and configuration on peer-group member.
7430 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7431 PEER_FLAG_MAX_PREFIX_OUT
);
7432 member
->pmax_out
[afi
][safi
] = 0;
7434 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7439 int is_ebgp_multihop_configured(struct peer
*peer
)
7441 struct peer_group
*group
;
7442 struct listnode
*node
, *nnode
;
7445 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7446 group
= peer
->group
;
7447 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7448 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7451 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7452 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7453 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7457 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7458 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7464 /* Set # of hops between us and BGP peer. */
7465 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7467 struct peer_group
*group
;
7469 struct listnode
*node
, *nnode
;
7472 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7475 /* We cannot configure ttl-security hops when ebgp-multihop is already
7476 set. For non peer-groups, the check is simple. For peer-groups,
7478 slightly messy, because we need to check both the peer-group
7480 and all peer-group members for any trace of ebgp-multihop
7482 before actually applying the ttl-security rules. Cisco really made a
7483 mess of this configuration parameter, and OpenBGPD got it right.
7486 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7487 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7488 if (is_ebgp_multihop_configured(peer
))
7489 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7491 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7492 peer
->gtsm_hops
= gtsm_hops
;
7494 /* Calling ebgp multihop also resets the session.
7495 * On restart, NHT will get setup correctly as will the
7496 * min & max ttls on the socket. The return value is
7499 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7504 group
= peer
->group
;
7505 group
->conf
->gtsm_hops
= gtsm_hops
;
7506 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7508 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7510 /* Calling ebgp multihop also resets the
7512 * On restart, NHT will get setup correctly as
7514 * min & max ttls on the socket. The return
7518 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7522 /* Post the first gtsm setup or if its ibgp, maxttl setting
7524 * necessary, just set the minttl.
7526 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7527 peer
->gtsm_hops
= gtsm_hops
;
7530 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7531 MAXTTL
+ 1 - gtsm_hops
);
7532 if ((peer
->status
< Established
) && peer
->doppelganger
7533 && (peer
->doppelganger
->fd
>= 0))
7534 sockopt_minttl(peer
->su
.sa
.sa_family
,
7535 peer
->doppelganger
->fd
,
7536 MAXTTL
+ 1 - gtsm_hops
);
7538 group
= peer
->group
;
7539 group
->conf
->gtsm_hops
= gtsm_hops
;
7540 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7542 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7544 /* Change setting of existing peer
7545 * established then change value (may break
7547 * not established yet (teardown session and
7549 * no session then do nothing (will get
7550 * handled by next connection)
7554 != BGP_GTSM_HOPS_DISABLED
)
7556 gpeer
->su
.sa
.sa_family
,
7558 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7559 if ((gpeer
->status
< Established
)
7560 && gpeer
->doppelganger
7561 && (gpeer
->doppelganger
->fd
>= 0))
7562 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7563 gpeer
->doppelganger
->fd
,
7564 MAXTTL
+ 1 - gtsm_hops
);
7572 int peer_ttl_security_hops_unset(struct peer
*peer
)
7574 struct peer_group
*group
;
7575 struct listnode
*node
, *nnode
;
7578 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7580 /* if a peer-group member, then reset to peer-group default rather than
7582 if (peer_group_active(peer
))
7583 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7585 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7587 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7588 /* Invoking ebgp_multihop_set will set the TTL back to the
7590 * value as well as restting the NHT and such. The session is
7593 if (peer
->sort
== BGP_PEER_EBGP
)
7594 ret
= peer_ebgp_multihop_unset(peer
);
7597 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7600 if ((peer
->status
< Established
) && peer
->doppelganger
7601 && (peer
->doppelganger
->fd
>= 0))
7602 sockopt_minttl(peer
->su
.sa
.sa_family
,
7603 peer
->doppelganger
->fd
, 0);
7606 group
= peer
->group
;
7607 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7608 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7609 if (peer
->sort
== BGP_PEER_EBGP
)
7610 ret
= peer_ebgp_multihop_unset(peer
);
7613 sockopt_minttl(peer
->su
.sa
.sa_family
,
7616 if ((peer
->status
< Established
)
7617 && peer
->doppelganger
7618 && (peer
->doppelganger
->fd
>= 0))
7619 sockopt_minttl(peer
->su
.sa
.sa_family
,
7620 peer
->doppelganger
->fd
,
7629 static void peer_reset_message_stats(struct peer
*peer
)
7632 atomic_store_explicit(&peer
->open_in
, 0, memory_order_relaxed
);
7633 atomic_store_explicit(&peer
->open_out
, 0, memory_order_relaxed
);
7634 atomic_store_explicit(&peer
->update_in
, 0,
7635 memory_order_relaxed
);
7636 atomic_store_explicit(&peer
->update_out
, 0,
7637 memory_order_relaxed
);
7638 atomic_store_explicit(&peer
->keepalive_in
, 0,
7639 memory_order_relaxed
);
7640 atomic_store_explicit(&peer
->keepalive_out
, 0,
7641 memory_order_relaxed
);
7642 atomic_store_explicit(&peer
->notify_in
, 0,
7643 memory_order_relaxed
);
7644 atomic_store_explicit(&peer
->notify_out
, 0,
7645 memory_order_relaxed
);
7646 atomic_store_explicit(&peer
->refresh_in
, 0,
7647 memory_order_relaxed
);
7648 atomic_store_explicit(&peer
->refresh_out
, 0,
7649 memory_order_relaxed
);
7650 atomic_store_explicit(&peer
->dynamic_cap_in
, 0,
7651 memory_order_relaxed
);
7652 atomic_store_explicit(&peer
->dynamic_cap_out
, 0,
7653 memory_order_relaxed
);
7658 * If peer clear is invoked in a loop for all peers on the BGP instance,
7659 * it may end up freeing the doppelganger, and if this was the next node
7660 * to the current node, we would end up accessing the freed next node.
7661 * Pass along additional parameter which can be updated if next node
7662 * is freed; only required when walking the peer list on BGP instance.
7664 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7666 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7667 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7668 if (peer_maximum_prefix_clear_overflow(peer
))
7671 peer
->v_start
= BGP_INIT_START_TIMER
;
7672 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7673 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7674 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7676 bgp_session_reset_safe(peer
, nnode
);
7681 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7682 enum bgp_clear_type stype
)
7684 struct peer_af
*paf
;
7686 if (!peer_established(peer
))
7689 if (!peer
->afc
[afi
][safi
])
7690 return BGP_ERR_AF_UNCONFIGURED
;
7692 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7694 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7695 /* Clear the "neighbor x.x.x.x default-originate" flag */
7696 paf
= peer_af_find(peer
, afi
, safi
);
7697 if (paf
&& paf
->subgroup
7698 && CHECK_FLAG(paf
->subgroup
->sflags
,
7699 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7700 UNSET_FLAG(paf
->subgroup
->sflags
,
7701 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7703 bgp_announce_route(peer
, afi
, safi
, false);
7706 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7707 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7708 PEER_CAP_ORF_PREFIX_SM_ADV
)
7709 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7710 PEER_CAP_ORF_PREFIX_RM_RCV
)
7711 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7712 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7713 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7714 uint8_t prefix_type
;
7716 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7717 PEER_CAP_ORF_PREFIX_RM_RCV
))
7718 prefix_type
= ORF_TYPE_PREFIX
;
7720 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7722 if (filter
->plist
[FILTER_IN
].plist
) {
7723 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7724 PEER_STATUS_ORF_PREFIX_SEND
))
7725 bgp_route_refresh_send(
7726 peer
, afi
, safi
, prefix_type
,
7728 BGP_ROUTE_REFRESH_NORMAL
);
7729 bgp_route_refresh_send(
7730 peer
, afi
, safi
, prefix_type
,
7731 REFRESH_IMMEDIATE
, 0,
7732 BGP_ROUTE_REFRESH_NORMAL
);
7734 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7735 PEER_STATUS_ORF_PREFIX_SEND
))
7736 bgp_route_refresh_send(
7737 peer
, afi
, safi
, prefix_type
,
7738 REFRESH_IMMEDIATE
, 1,
7739 BGP_ROUTE_REFRESH_NORMAL
);
7741 bgp_route_refresh_send(
7742 peer
, afi
, safi
, 0, 0, 0,
7743 BGP_ROUTE_REFRESH_NORMAL
);
7749 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7750 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7751 /* If neighbor has soft reconfiguration inbound flag.
7752 Use Adj-RIB-In database. */
7753 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
7754 PEER_FLAG_SOFT_RECONFIG
))
7755 bgp_soft_reconfig_in(peer
, afi
, safi
);
7757 /* If neighbor has route refresh capability, send route
7759 message to the peer. */
7760 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7761 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7762 bgp_route_refresh_send(
7763 peer
, afi
, safi
, 0, 0, 0,
7764 BGP_ROUTE_REFRESH_NORMAL
);
7766 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7770 if (stype
== BGP_CLEAR_MESSAGE_STATS
)
7771 peer_reset_message_stats(peer
);
7776 /* Display peer uptime.*/
7777 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7780 time_t uptime1
, epoch_tbuf
;
7783 /* If there is no connection has been done before print `never'. */
7786 json_object_string_add(json
, "peerUptime", "never");
7787 json_object_int_add(json
, "peerUptimeMsec", 0);
7789 snprintf(buf
, len
, "never");
7793 /* Get current time. */
7794 uptime1
= monotime(NULL
);
7796 gmtime_r(&uptime1
, &tm
);
7798 if (uptime1
< ONE_DAY_SECOND
)
7799 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7801 else if (uptime1
< ONE_WEEK_SECOND
)
7802 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7804 else if (uptime1
< ONE_YEAR_SECOND
)
7805 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7806 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7808 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7810 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7813 epoch_tbuf
= time(NULL
) - uptime1
;
7814 json_object_string_add(json
, "peerUptime", buf
);
7815 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7816 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7823 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7824 struct list
*addresses
)
7828 memset(&bgp_master
, 0, sizeof(bgp_master
));
7831 bm
->bgp
= list_new();
7832 bm
->listen_sockets
= list_new();
7833 bm
->port
= BGP_PORT_DEFAULT
;
7834 bm
->addresses
= addresses
;
7835 bm
->master
= master
;
7836 bm
->start_time
= monotime(NULL
);
7837 bm
->t_rmap_update
= NULL
;
7838 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7839 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7840 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7841 bm
->terminating
= false;
7842 bm
->socket_buffer
= buffer_size
;
7843 bm
->wait_for_fib
= false;
7844 bm
->tcp_dscp
= IPTOS_PREC_INTERNETCONTROL
;
7847 /* init the rd id space.
7848 assign 0th index in the bitfield,
7849 so that we start with id 1
7851 bf_init(bm
->rd_idspace
, UINT16_MAX
);
7852 bf_assign_zero_index(bm
->rd_idspace
);
7854 /* mpls label dynamic allocation pool */
7855 bgp_lp_init(bm
->master
, &bm
->labelpool
);
7859 QOBJ_REG(bm
, bgp_master
);
7863 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7864 * instance delete (non-default only) or BGP exit.
7866 static void bgp_if_finish(struct bgp
*bgp
)
7869 struct interface
*ifp
;
7871 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
7873 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
7876 FOR_ALL_INTERFACES (vrf
, ifp
) {
7877 struct listnode
*c_node
, *c_nnode
;
7878 struct connected
*c
;
7880 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
7881 bgp_connected_delete(bgp
, c
);
7885 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
7887 struct vrf
*vrf
= NULL
;
7888 struct listnode
*next
;
7891 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
7892 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
7894 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
7895 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
7898 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
7902 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
7904 struct listnode
*next
, *next2
;
7905 struct bgp
*bgp
, *bgp2
;
7908 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
7910 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
7911 if (bgp2
->as
== bgp
->as
)
7919 snprintf(buf
, sizeof(buf
), "%u", bgp
->as
);
7920 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
7924 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
7925 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
7926 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
7927 {.completions
= NULL
},
7930 struct frr_pthread
*bgp_pth_io
;
7931 struct frr_pthread
*bgp_pth_ka
;
7933 static void bgp_pthreads_init(void)
7935 assert(!bgp_pth_io
);
7936 assert(!bgp_pth_ka
);
7938 struct frr_pthread_attr io
= {
7939 .start
= frr_pthread_attr_default
.start
,
7940 .stop
= frr_pthread_attr_default
.stop
,
7942 struct frr_pthread_attr ka
= {
7943 .start
= bgp_keepalives_start
,
7944 .stop
= bgp_keepalives_stop
,
7946 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
7947 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
7950 void bgp_pthreads_run(void)
7952 frr_pthread_run(bgp_pth_io
, NULL
);
7953 frr_pthread_run(bgp_pth_ka
, NULL
);
7955 /* Wait until threads are ready. */
7956 frr_pthread_wait_running(bgp_pth_io
);
7957 frr_pthread_wait_running(bgp_pth_ka
);
7960 void bgp_pthreads_finish(void)
7962 frr_pthread_stop_all();
7965 static int peer_unshut_after_cfg(struct bgp
*bgp
)
7967 struct listnode
*node
;
7970 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
7971 if (!peer
->shut_during_cfg
)
7974 if (bgp_debug_neighbor_events(peer
))
7975 zlog_debug("%s: released from config-pending hold",
7978 peer
->shut_during_cfg
= false;
7979 if (peer_active(peer
) && peer
->status
!= Established
) {
7980 if (peer
->status
!= Idle
)
7981 BGP_EVENT_ADD(peer
, BGP_Stop
);
7982 BGP_EVENT_ADD(peer
, BGP_Start
);
7989 void bgp_init(unsigned short instance
)
7991 hook_register(bgp_config_end
, peer_unshut_after_cfg
);
7993 /* allocates some vital data structures used by peer commands in
7996 /* pre-init pthreads */
7997 bgp_pthreads_init();
8000 bgp_zebra_init(bm
->master
, instance
);
8002 #ifdef ENABLE_BGP_VNC
8003 vnc_zebra_init(bm
->master
);
8006 /* BGP VTY commands installation. */
8012 bgp_community_alias_init();
8015 bgp_route_map_init();
8016 bgp_scan_vty_init();
8018 #ifdef ENABLE_BGP_VNC
8021 bgp_ethernetvpn_init();
8022 bgp_flowspec_vty_init();
8024 /* Access list initialize. */
8026 access_list_add_hook(peer_distribute_update
);
8027 access_list_delete_hook(peer_distribute_update
);
8029 /* Filter list initialize. */
8031 as_list_add_hook(peer_aslist_add
);
8032 as_list_delete_hook(peer_aslist_del
);
8034 /* Prefix list initialize.*/
8036 prefix_list_add_hook(peer_prefix_list_update
);
8037 prefix_list_delete_hook(peer_prefix_list_update
);
8039 /* Community list initialize. */
8040 bgp_clist
= community_list_init();
8043 bgp_bfd_init(bm
->master
);
8047 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
8050 void bgp_terminate(void)
8054 struct listnode
*node
, *nnode
;
8055 struct listnode
*mnode
, *mnnode
;
8059 /* Close the listener sockets first as this prevents peers from
8061 * to reconnect on receiving the peer unconfig message. In the presence
8062 * of a large number of peers this will ensure that no peer is left with
8063 * a dangling connection
8067 /* reverse bgp_master_init */
8068 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
8069 bgp_close_vrf_socket(bgp
);
8070 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
8071 if (peer_established(peer
) || peer
->status
== OpenSent
8072 || peer
->status
== OpenConfirm
)
8073 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
8074 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
8077 if (bm
->listen_sockets
)
8078 list_delete(&bm
->listen_sockets
);
8080 THREAD_OFF(bm
->t_rmap_update
);
8085 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
8086 const char *ip_str
, bool use_json
)
8092 /* Get peer sockunion. */
8093 ret
= str2sockunion(ip_str
, &su
);
8095 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
8097 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
8101 json_object
*json_no
= NULL
;
8102 json_no
= json_object_new_object();
8103 json_object_string_add(
8105 "malformedAddressOrName",
8107 vty_json(vty
, json_no
);
8110 "%% Malformed address or name: %s\n",
8118 /* Peer structure lookup. */
8119 peer
= peer_lookup(bgp
, &su
);
8122 json_object
*json_no
= NULL
;
8123 json_no
= json_object_new_object();
8124 json_object_string_add(json_no
, "warning",
8125 "No such neighbor in this view/vrf");
8126 vty_json(vty
, json_no
);
8128 vty_out(vty
, "No such neighbor in this view/vrf\n");
8135 void bgp_gr_apply_running_config(void)
8137 struct peer
*peer
= NULL
;
8138 struct bgp
*bgp
= NULL
;
8139 struct listnode
*node
, *nnode
;
8140 bool gr_router_detected
= false;
8142 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
8143 zlog_debug("[BGP_GR] %s called !", __func__
);
8145 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
8146 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8147 bgp_peer_gr_flags_update(peer
);
8148 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
8149 gr_router_detected
= true;
8152 if (gr_router_detected
8153 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
8154 bgp_zebra_send_capabilities(bgp
, true);
8155 } else if (!gr_router_detected
8156 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
8157 bgp_zebra_send_capabilities(bgp
, false);
8160 gr_router_detected
= false;
8164 printfrr_ext_autoreg_p("BP", printfrr_bp
);
8165 static ssize_t
printfrr_bp(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
8168 const struct peer
*peer
= ptr
;
8171 return bputs(buf
, "(null)");
8173 return bprintfrr(buf
, "%s(%s)", peer
->host
,
8174 peer
->hostname
? peer
->hostname
: "Unknown");