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"
95 #include "bgpd/bgp_orr.h"
96 #include "bgp_trace.h"
98 DEFINE_MTYPE_STATIC(BGPD
, PEER_TX_SHUTDOWN_MSG
, "Peer shutdown message (TX)");
99 DEFINE_MTYPE_STATIC(BGPD
, BGP_EVPN_INFO
, "BGP EVPN instance information");
100 DEFINE_QOBJ_TYPE(bgp_master
);
101 DEFINE_QOBJ_TYPE(bgp
);
102 DEFINE_QOBJ_TYPE(peer
);
103 DEFINE_HOOK(bgp_inst_delete
, (struct bgp
*bgp
), (bgp
));
105 /* BGP process wide configuration. */
106 static struct bgp_master bgp_master
;
108 /* BGP process wide configuration pointer to export. */
109 struct bgp_master
*bm
;
111 /* BGP community-list. */
112 struct community_list_handler
*bgp_clist
;
114 unsigned int multipath_num
= MULTIPATH_NUM
;
116 /* Number of bgp instances configured for suppress fib config */
117 unsigned int bgp_suppress_fib_count
;
119 static void bgp_if_finish(struct bgp
*bgp
);
120 static void peer_drop_dynamic_neighbor(struct peer
*peer
);
122 extern struct zclient
*zclient
;
124 /* handle main socket creation or deletion */
125 static int bgp_check_main_socket(bool create
, struct bgp
*bgp
)
127 static int bgp_server_main_created
;
128 struct listnode
*node
;
132 if (bgp_server_main_created
)
134 if (list_isempty(bm
->addresses
)) {
135 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
136 return BGP_ERR_INVALID_VALUE
;
138 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
139 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
140 return BGP_ERR_INVALID_VALUE
;
142 bgp_server_main_created
= 1;
145 if (!bgp_server_main_created
)
148 bgp_server_main_created
= 0;
152 void bgp_session_reset(struct peer
*peer
)
154 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
155 && !(CHECK_FLAG(peer
->doppelganger
->flags
, PEER_FLAG_CONFIG_NODE
)))
156 peer_delete(peer
->doppelganger
);
158 BGP_EVENT_ADD(peer
, BGP_Stop
);
162 * During session reset, we may delete the doppelganger peer, which would
163 * be the next node to the current node. If the session reset was invoked
164 * during walk of peer list, we would end up accessing the freed next
165 * node. This function moves the next node along.
167 static void bgp_session_reset_safe(struct peer
*peer
, struct listnode
**nnode
)
172 n
= (nnode
) ? *nnode
: NULL
;
173 npeer
= (n
) ? listgetdata(n
) : NULL
;
175 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
176 && !(CHECK_FLAG(peer
->doppelganger
->flags
,
177 PEER_FLAG_CONFIG_NODE
))) {
178 if (peer
->doppelganger
== npeer
)
179 /* nnode and *nnode are confirmed to be non-NULL here */
180 *nnode
= (*nnode
)->next
;
181 peer_delete(peer
->doppelganger
);
184 BGP_EVENT_ADD(peer
, BGP_Stop
);
187 /* BGP global flag manipulation. */
188 int bgp_option_set(int flag
)
192 case BGP_OPT_NO_LISTEN
:
193 case BGP_OPT_NO_ZEBRA
:
194 SET_FLAG(bm
->options
, flag
);
197 return BGP_ERR_INVALID_FLAG
;
202 int bgp_option_unset(int flag
)
206 case BGP_OPT_NO_ZEBRA
:
208 UNSET_FLAG(bm
->options
, flag
);
211 return BGP_ERR_INVALID_FLAG
;
216 int bgp_option_check(int flag
)
218 return CHECK_FLAG(bm
->options
, flag
);
221 /* set the bgp no-rib option during runtime and remove installed routes */
222 void bgp_option_norib_set_runtime(void)
225 struct listnode
*node
;
229 if (bgp_option_check(BGP_OPT_NO_FIB
))
232 bgp_option_set(BGP_OPT_NO_FIB
);
234 zlog_info("Disabled BGP route installation to RIB (Zebra)");
236 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
237 FOREACH_AFI_SAFI (afi
, safi
) {
239 * Stop a crash, more work is needed
240 * here to properly add/remove these types of
243 if (!bgp_fibupd_safi(safi
))
246 bgp_zebra_withdraw_table_all_subtypes(bgp
, afi
, safi
);
250 zlog_info("All routes have been withdrawn from RIB (Zebra)");
253 /* unset the bgp no-rib option during runtime and announce routes to Zebra */
254 void bgp_option_norib_unset_runtime(void)
257 struct listnode
*node
;
261 if (!bgp_option_check(BGP_OPT_NO_FIB
))
264 bgp_option_unset(BGP_OPT_NO_FIB
);
266 zlog_info("Enabled BGP route installation to RIB (Zebra)");
268 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
269 FOREACH_AFI_SAFI (afi
, safi
) {
271 * Stop a crash, more work is needed
272 * here to properly add/remove these types
273 * of routes from zebra
275 if (!bgp_fibupd_safi(safi
))
278 bgp_zebra_announce_table_all_subtypes(bgp
, afi
, safi
);
282 zlog_info("All routes have been installed in RIB (Zebra)");
285 /* Internal function to set BGP structure configureation flag. */
286 static void bgp_config_set(struct bgp
*bgp
, int config
)
288 SET_FLAG(bgp
->config
, config
);
291 static void bgp_config_unset(struct bgp
*bgp
, int config
)
293 UNSET_FLAG(bgp
->config
, config
);
296 static int bgp_config_check(struct bgp
*bgp
, int config
)
298 return CHECK_FLAG(bgp
->config
, config
);
301 /* Set BGP router identifier; distinguish between explicit config and other
304 static int bgp_router_id_set(struct bgp
*bgp
, const struct in_addr
*id
,
308 struct listnode
*node
, *nnode
;
310 if (IPV4_ADDR_SAME(&bgp
->router_id
, id
))
313 /* EVPN uses router id in RD, withdraw them */
314 if (is_evpn_enabled())
315 bgp_evpn_handle_router_id_update(bgp
, true);
317 vpn_handle_router_id_update(bgp
, true, is_config
);
319 IPV4_ADDR_COPY(&bgp
->router_id
, id
);
321 /* Set all peer's local identifier with this value. */
322 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
323 IPV4_ADDR_COPY(&peer
->local_id
, id
);
325 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
326 peer
->last_reset
= PEER_DOWN_RID_CHANGE
;
327 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
328 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
332 /* EVPN uses router id in RD, update them */
333 if (is_evpn_enabled())
334 bgp_evpn_handle_router_id_update(bgp
, false);
336 vpn_handle_router_id_update(bgp
, false, is_config
);
341 void bgp_router_id_zebra_bump(vrf_id_t vrf_id
, const struct prefix
*router_id
)
343 struct listnode
*node
, *nnode
;
345 struct in_addr
*addr
= NULL
;
347 if (router_id
!= NULL
)
348 addr
= (struct in_addr
*)&(router_id
->u
.prefix4
);
350 if (vrf_id
== VRF_DEFAULT
) {
351 /* Router-id change for default VRF has to also update all
353 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
354 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
358 bgp
->router_id_zebra
= *addr
;
360 addr
= &bgp
->router_id_zebra
;
362 if (!bgp
->router_id_static
.s_addr
) {
363 /* Router ID is updated if there are no active
366 if (bgp
->established_peers
== 0) {
367 if (BGP_DEBUG(zebra
, ZEBRA
))
369 "RID change : vrf %s(%u), RTR ID %pI4",
373 * if old router-id was 0x0, set flag
374 * to use this new value
376 bgp_router_id_set(bgp
, addr
,
377 (bgp
->router_id
.s_addr
385 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
388 bgp
->router_id_zebra
= *addr
;
390 addr
= &bgp
->router_id_zebra
;
392 if (!bgp
->router_id_static
.s_addr
) {
393 /* Router ID is updated if there are no active
396 if (bgp
->established_peers
== 0) {
397 if (BGP_DEBUG(zebra
, ZEBRA
))
399 "RID change : vrf %s(%u), RTR ID %pI4",
403 * if old router-id was 0x0, set flag
404 * to use this new value
406 bgp_router_id_set(bgp
, addr
,
407 (bgp
->router_id
.s_addr
418 void bgp_router_id_static_set(struct bgp
*bgp
, struct in_addr id
)
420 bgp
->router_id_static
= id
;
421 bgp_router_id_set(bgp
,
422 id
.s_addr
!= INADDR_ANY
? &id
: &bgp
->router_id_zebra
,
423 true /* is config */);
426 void bm_wait_for_fib_set(bool set
)
428 bool send_msg
= false;
430 if (bm
->wait_for_fib
== set
)
433 bm
->wait_for_fib
= set
;
435 if (bgp_suppress_fib_count
== 0)
437 bgp_suppress_fib_count
++;
439 bgp_suppress_fib_count
--;
440 if (bgp_suppress_fib_count
== 0)
444 if (send_msg
&& zclient
)
445 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
449 /* Set the suppress fib pending for the bgp configuration */
450 void bgp_suppress_fib_pending_set(struct bgp
*bgp
, bool set
)
452 bool send_msg
= false;
454 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
458 SET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
459 /* Send msg to zebra for the first instance of bgp enabled
462 if (bgp_suppress_fib_count
== 0)
464 bgp_suppress_fib_count
++;
466 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
467 bgp_suppress_fib_count
--;
469 /* Send msg to zebra if there are no instances enabled
472 if (bgp_suppress_fib_count
== 0)
475 /* Send route notify request to RIB */
477 if (BGP_DEBUG(zebra
, ZEBRA
))
478 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
481 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
486 /* BGP's cluster-id control. */
487 void bgp_cluster_id_set(struct bgp
*bgp
, struct in_addr
*cluster_id
)
490 struct listnode
*node
, *nnode
;
492 if (bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
)
493 && IPV4_ADDR_SAME(&bgp
->cluster_id
, cluster_id
))
496 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
497 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
499 /* Clear all IBGP peer. */
500 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
501 if (peer
->sort
!= BGP_PEER_IBGP
)
504 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
505 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
506 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
507 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
512 void bgp_cluster_id_unset(struct bgp
*bgp
)
515 struct listnode
*node
, *nnode
;
517 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
520 bgp
->cluster_id
.s_addr
= 0;
521 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
523 /* Clear all IBGP peer. */
524 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
525 if (peer
->sort
!= BGP_PEER_IBGP
)
528 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
529 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
530 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
531 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
536 /* BGP timer configuration. */
537 void bgp_timers_set(struct bgp
*bgp
, uint32_t keepalive
, uint32_t holdtime
,
538 uint32_t connect_retry
, uint32_t delayopen
)
540 bgp
->default_keepalive
=
541 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
542 bgp
->default_holdtime
= holdtime
;
543 bgp
->default_connect_retry
= connect_retry
;
544 bgp
->default_delayopen
= delayopen
;
547 /* mostly for completeness - CLI uses its own defaults */
548 void bgp_timers_unset(struct bgp
*bgp
)
550 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
551 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
552 bgp
->default_connect_retry
= BGP_DEFAULT_CONNECT_RETRY
;
553 bgp
->default_delayopen
= BGP_DEFAULT_DELAYOPEN
;
556 void bgp_tcp_keepalive_set(struct bgp
*bgp
, uint16_t keepalive_idle
,
557 uint16_t keepalive_intvl
, uint16_t keepalive_probes
)
559 bgp
->tcp_keepalive_idle
= keepalive_idle
;
560 bgp
->tcp_keepalive_intvl
= keepalive_intvl
;
561 bgp
->tcp_keepalive_probes
= keepalive_probes
;
564 void bgp_tcp_keepalive_unset(struct bgp
*bgp
)
566 bgp
->tcp_keepalive_idle
= 0;
567 bgp
->tcp_keepalive_intvl
= 0;
568 bgp
->tcp_keepalive_probes
= 0;
571 /* BGP confederation configuration. */
572 void bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
575 struct listnode
*node
, *nnode
;
581 /* Remember - were we doing confederation before? */
582 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
584 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
586 /* If we were doing confederation already, this is just an external
587 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
588 were not doing confederation before, reset all EBGP sessions. */
589 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
590 enum bgp_peer_sort ptype
= peer_sort(peer
);
592 /* We're looking for peers who's AS is not local or part of our
594 if (already_confed
) {
595 if (ptype
== BGP_PEER_EBGP
) {
597 if (BGP_IS_VALID_STATE_FOR_NOTIF(
600 PEER_DOWN_CONFED_ID_CHANGE
;
602 peer
, BGP_NOTIFY_CEASE
,
603 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
605 bgp_session_reset_safe(peer
, &nnode
);
608 /* Not doign confederation before, so reset every
611 if (ptype
!= BGP_PEER_IBGP
) {
612 /* Reset the local_as to be our EBGP one */
613 if (ptype
== BGP_PEER_EBGP
)
615 if (BGP_IS_VALID_STATE_FOR_NOTIF(
618 PEER_DOWN_CONFED_ID_CHANGE
;
620 peer
, BGP_NOTIFY_CEASE
,
621 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
623 bgp_session_reset_safe(peer
, &nnode
);
630 void bgp_confederation_id_unset(struct bgp
*bgp
)
633 struct listnode
*node
, *nnode
;
636 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
638 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
639 /* We're looking for peers who's AS is not local */
640 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
641 peer
->local_as
= bgp
->as
;
642 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
643 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
644 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
645 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
649 bgp_session_reset_safe(peer
, &nnode
);
654 /* Is an AS part of the confed or not? */
655 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
662 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
663 if (bgp
->confed_peers
[i
] == as
)
669 /* Add an AS to the confederation set. */
670 void bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
673 struct listnode
*node
, *nnode
;
678 if (bgp_confederation_peers_check(bgp
, as
))
682 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
683 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
685 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
686 bgp
->confed_peers_cnt
++;
688 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
689 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
690 if (peer
->as
== as
) {
691 peer
->local_as
= bgp
->as
;
692 (void)peer_sort(peer
);
693 if (BGP_IS_VALID_STATE_FOR_NOTIF(
696 PEER_DOWN_CONFED_PEER_CHANGE
;
698 peer
, BGP_NOTIFY_CEASE
,
699 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
701 bgp_session_reset_safe(peer
, &nnode
);
707 /* Delete an AS from the confederation set. */
708 void bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
713 struct listnode
*node
, *nnode
;
718 if (!bgp_confederation_peers_check(bgp
, as
))
721 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
722 if (bgp
->confed_peers
[i
] == as
)
723 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
724 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
726 bgp
->confed_peers_cnt
--;
728 if (bgp
->confed_peers_cnt
== 0) {
729 if (bgp
->confed_peers
)
730 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
731 bgp
->confed_peers
= NULL
;
734 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
735 bgp
->confed_peers_cnt
* sizeof(as_t
));
737 /* Now reset any peer who's remote AS has just been removed from the
739 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
740 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
741 if (peer
->as
== as
) {
742 peer
->local_as
= bgp
->confed_id
;
743 (void)peer_sort(peer
);
744 if (BGP_IS_VALID_STATE_FOR_NOTIF(
747 PEER_DOWN_CONFED_PEER_CHANGE
;
749 peer
, BGP_NOTIFY_CEASE
,
750 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
752 bgp_session_reset_safe(peer
, &nnode
);
758 /* Local preference configuration. */
759 void bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
764 bgp
->default_local_pref
= local_pref
;
767 void bgp_default_local_preference_unset(struct bgp
*bgp
)
772 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
775 /* Local preference configuration. */
776 void bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
,
782 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
785 void bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
789 bgp
->default_subgroup_pkt_queue_max
=
790 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
793 /* Listen limit configuration. */
794 void bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
799 bgp
->dynamic_neighbors_limit
= listen_limit
;
802 void bgp_listen_limit_unset(struct bgp
*bgp
)
807 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
810 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
811 afi_t
*afi
, safi_t
*safi
)
813 /* Map from IANA values to internal values, return error if
814 * values are unrecognized.
816 *afi
= afi_iana2int(pkt_afi
);
817 *safi
= safi_iana2int(pkt_safi
);
818 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
824 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
825 iana_safi_t
*pkt_safi
)
827 /* Map from internal values to IANA values, return error if
828 * internal values are bad (unexpected).
830 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
832 *pkt_afi
= afi_int2iana(afi
);
833 *pkt_safi
= safi_int2iana(safi
);
837 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
846 afid
= afindex(afi
, safi
);
847 if (afid
>= BGP_AF_MAX
)
851 assert(peer
->peer_af_array
[afid
] == NULL
);
853 /* Allocate new peer af */
854 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
856 peer
->peer_af_array
[afid
] = af
;
861 bgp
->af_peer_count
[afi
][safi
]++;
866 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
873 afid
= afindex(afi
, safi
);
874 if (afid
>= BGP_AF_MAX
)
877 return peer
->peer_af_array
[afid
];
880 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
889 afid
= afindex(afi
, safi
);
890 if (afid
>= BGP_AF_MAX
)
893 af
= peer
->peer_af_array
[afid
];
898 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
900 bgp_stop_announce_route_timer(af
);
902 if (PAF_SUBGRP(af
)) {
903 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
904 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
905 af
->subgroup
->update_group
->id
,
906 af
->subgroup
->id
, peer
->host
);
910 update_subgroup_remove_peer(af
->subgroup
, af
);
912 if (bgp
->af_peer_count
[afi
][safi
])
913 bgp
->af_peer_count
[afi
][safi
]--;
915 peer
->peer_af_array
[afid
] = NULL
;
916 XFREE(MTYPE_BGP_PEER_AF
, af
);
920 /* Peer comparison function for sorting. */
921 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
923 if (p1
->group
&& !p2
->group
)
926 if (!p1
->group
&& p2
->group
)
929 if (p1
->group
== p2
->group
) {
930 if (p1
->conf_if
&& !p2
->conf_if
)
933 if (!p1
->conf_if
&& p2
->conf_if
)
936 if (p1
->conf_if
&& p2
->conf_if
)
937 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
939 return strcmp(p1
->group
->name
, p2
->group
->name
);
941 return sockunion_cmp(&p1
->su
, &p2
->su
);
944 static unsigned int peer_hash_key_make(const void *p
)
946 const struct peer
*peer
= p
;
947 return sockunion_hash(&peer
->su
);
950 static bool peer_hash_same(const void *p1
, const void *p2
)
952 const struct peer
*peer1
= p1
;
953 const struct peer
*peer2
= p2
;
955 return (sockunion_same(&peer1
->su
, &peer2
->su
)
956 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
957 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
960 void peer_flag_inherit(struct peer
*peer
, uint64_t flag
)
964 /* Skip if peer is not a peer-group member. */
965 if (!peer_group_active(peer
))
968 /* Unset override flag to signal inheritance from peer-group. */
969 UNSET_FLAG(peer
->flags_override
, flag
);
972 * Inherit flag state from peer-group. If the flag of the peer-group is
973 * not being inverted, the peer must inherit the inverse of the current
974 * peer-group flag state.
976 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
977 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
978 && CHECK_FLAG(peer
->flags_invert
, flag
))
979 COND_FLAG(peer
->flags
, flag
, !group_val
);
981 COND_FLAG(peer
->flags
, flag
, group_val
);
984 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
986 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
989 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
994 /* Skip if peer is not a peer-group member. */
995 if (!peer_group_active(peer
))
998 /* Unset override flag to signal inheritance from peer-group. */
999 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
1002 * Inherit flag state from peer-group. If the flag of the peer-group is
1003 * not being inverted, the peer must inherit the inverse of the current
1004 * peer-group flag state.
1006 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
1007 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
1008 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
1009 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
1011 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
1014 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
1015 static inline enum bgp_peer_sort
peer_calc_sort(struct peer
*peer
)
1022 if (peer
->change_local_as
)
1023 local_as
= peer
->change_local_as
;
1025 local_as
= peer
->local_as
;
1028 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1029 if (peer
->as_type
== AS_INTERNAL
)
1030 return BGP_PEER_IBGP
;
1032 else if (peer
->as_type
== AS_EXTERNAL
)
1033 return BGP_PEER_EBGP
;
1035 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1037 return (local_as
== peer
->as
? BGP_PEER_IBGP
1044 assert(peer
->group
);
1045 peer1
= listnode_head(peer
->group
->peer
);
1050 return BGP_PEER_INTERNAL
;
1054 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1056 return BGP_PEER_INTERNAL
;
1058 if (local_as
== peer
->as
) {
1059 if (bgp
->as
== bgp
->confed_id
) {
1060 if (local_as
== bgp
->as
)
1061 return BGP_PEER_IBGP
;
1063 return BGP_PEER_EBGP
;
1065 if (local_as
== bgp
->confed_id
)
1066 return BGP_PEER_EBGP
;
1068 return BGP_PEER_IBGP
;
1072 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1073 return BGP_PEER_CONFED
;
1075 return BGP_PEER_EBGP
;
1077 if (peer
->as_type
== AS_UNSPECIFIED
) {
1078 /* check if in peer-group with AS information */
1080 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1081 if (peer
->group
->conf
->as_type
1083 if (local_as
== peer
->group
->conf
->as
)
1084 return BGP_PEER_IBGP
;
1086 return BGP_PEER_EBGP
;
1087 } else if (peer
->group
->conf
->as_type
1089 return BGP_PEER_IBGP
;
1091 return BGP_PEER_EBGP
;
1093 /* no AS information anywhere, let caller know */
1094 return BGP_PEER_UNSPECIFIED
;
1095 } else if (peer
->as_type
!= AS_SPECIFIED
)
1096 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1099 return (local_as
== 0 ? BGP_PEER_INTERNAL
1100 : local_as
== peer
->as
? BGP_PEER_IBGP
1105 /* Calculate and cache the peer "sort" */
1106 enum bgp_peer_sort
peer_sort(struct peer
*peer
)
1108 peer
->sort
= peer_calc_sort(peer
);
1112 enum bgp_peer_sort
peer_sort_lookup(struct peer
*peer
)
1117 static void peer_free(struct peer
*peer
)
1122 assert(peer
->status
== Deleted
);
1126 /* this /ought/ to have been done already through bgp_stop earlier,
1127 * but just to be sure..
1129 bgp_timer_set(peer
);
1130 bgp_reads_off(peer
);
1131 bgp_writes_off(peer
);
1132 thread_cancel_event_ready(bm
->master
, peer
);
1133 FOREACH_AFI_SAFI (afi
, safi
)
1134 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
1135 assert(!peer
->t_write
);
1136 assert(!peer
->t_read
);
1137 BGP_EVENT_FLUSH(peer
);
1139 pthread_mutex_destroy(&peer
->io_mtx
);
1141 /* Free connected nexthop, if present */
1142 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1143 && !peer_dynamic_neighbor(peer
))
1144 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1147 FOREACH_AFI_SAFI (afi
, safi
) {
1148 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1149 XFREE(MTYPE_BGP_FILTER_NAME
,
1150 peer
->filter
[afi
][safi
].advmap
.aname
);
1151 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1152 XFREE(MTYPE_BGP_FILTER_NAME
,
1153 peer
->filter
[afi
][safi
].advmap
.cname
);
1156 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1158 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1159 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1160 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1161 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1163 /* Update source configuration. */
1164 if (peer
->update_source
) {
1165 sockunion_free(peer
->update_source
);
1166 peer
->update_source
= NULL
;
1169 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1171 XFREE(MTYPE_BGP_NOTIFICATION
, peer
->notify
.data
);
1172 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1174 if (peer
->clear_node_queue
)
1175 work_queue_free_and_null(&peer
->clear_node_queue
);
1177 bgp_sync_delete(peer
);
1179 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1181 /* Remove BFD configuration. */
1182 if (peer
->bfd_config
)
1183 bgp_peer_remove_bfd_config(peer
);
1185 FOREACH_AFI_SAFI (afi
, safi
)
1186 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1188 bgp_unlock(peer
->bgp
);
1190 memset(peer
, 0, sizeof(struct peer
));
1192 XFREE(MTYPE_BGP_PEER
, peer
);
1195 /* increase reference count on a struct peer */
1196 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1198 frrtrace(2, frr_bgp
, bgp_peer_lock
, peer
, name
);
1199 assert(peer
&& (peer
->lock
>= 0));
1206 /* decrease reference count on a struct peer
1207 * struct peer is freed and NULL returned if last reference
1209 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1211 frrtrace(2, frr_bgp
, bgp_peer_unlock
, peer
, name
);
1212 assert(peer
&& (peer
->lock
> 0));
1216 if (peer
->lock
== 0) {
1223 /* BGP GR changes */
1225 int bgp_global_gr_init(struct bgp
*bgp
)
1227 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1228 zlog_debug("%s called ..", __func__
);
1230 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1231 /* GLOBAL_HELPER Mode */
1234 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1235 GLOBAL_GR
, GLOBAL_INVALID
,
1236 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1237 GLOBAL_DISABLE
, GLOBAL_INVALID
1239 /* GLOBAL_GR Mode */
1242 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1243 GLOBAL_GR
, GLOBAL_HELPER
,
1244 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1245 GLOBAL_DISABLE
, GLOBAL_INVALID
1247 /* GLOBAL_DISABLE Mode */
1250 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1251 GLOBAL_GR
, GLOBAL_INVALID
,
1252 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1253 GLOBAL_INVALID
, GLOBAL_HELPER
1255 /* GLOBAL_INVALID Mode */
1258 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1259 GLOBAL_INVALID
, GLOBAL_INVALID
,
1260 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1261 GLOBAL_INVALID
, GLOBAL_INVALID
1264 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1265 sizeof(local_GLOBAL_GR_FSM
));
1267 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1268 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1270 return BGP_GR_SUCCESS
;
1273 int bgp_peer_gr_init(struct peer
*peer
)
1275 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1276 zlog_debug("%s called ..", __func__
);
1278 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1279 [BGP_PEER_GR_EVENT_CMD
] = {
1281 /* PEER_HELPER Mode */
1282 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1283 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1284 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1285 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1286 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1287 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1288 bgp_peer_gr_action
}
1292 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1293 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1294 bgp_peer_gr_action
},
1295 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1296 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1297 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1298 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1301 /* PEER_DISABLE Mode */
1302 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1303 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1304 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1305 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1306 bgp_peer_gr_action
},
1307 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1308 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1311 /* PEER_INVALID Mode */
1312 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1313 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1314 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1315 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1316 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1317 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1320 /* PEER_GLOBAL_INHERIT Mode */
1321 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1322 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1323 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1324 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1325 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1326 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1329 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1330 sizeof(local_Peer_GR_FSM
));
1331 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1332 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1334 return BGP_GR_SUCCESS
;
1337 static void bgp_srv6_init(struct bgp
*bgp
)
1339 bgp
->srv6_enabled
= false;
1340 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1341 bgp
->srv6_locator_chunks
= list_new();
1342 bgp
->srv6_functions
= list_new();
1345 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1347 if (bgp
->srv6_locator_chunks
)
1348 list_delete(&bgp
->srv6_locator_chunks
);
1349 if (bgp
->srv6_functions
)
1350 list_delete(&bgp
->srv6_functions
);
1353 /* Allocate new peer object, implicitely locked. */
1354 struct peer
*peer_new(struct bgp
*bgp
)
1361 /* bgp argument is absolutely required */
1364 /* Allocate new peer. */
1365 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1367 /* Set default value. */
1369 peer
->v_start
= BGP_INIT_START_TIMER
;
1370 peer
->v_connect
= bgp
->default_connect_retry
;
1371 peer
->status
= Idle
;
1372 peer
->ostatus
= Idle
;
1373 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1374 peer
->bgp
= bgp_lock(bgp
);
1375 peer
= peer_lock(peer
); /* initial reference */
1376 peer
->local_role
= ROLE_UNDEFINED
;
1377 peer
->remote_role
= ROLE_UNDEFINED
;
1378 peer
->password
= NULL
;
1379 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1381 /* Set default flags. */
1382 FOREACH_AFI_SAFI (afi
, safi
) {
1383 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1384 SET_FLAG(peer
->af_flags
[afi
][safi
],
1385 PEER_FLAG_SEND_EXT_COMMUNITY
);
1386 SET_FLAG(peer
->af_flags
[afi
][safi
],
1387 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1389 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1390 PEER_FLAG_SEND_COMMUNITY
);
1391 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1392 PEER_FLAG_SEND_EXT_COMMUNITY
);
1393 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1394 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1395 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1396 peer
->soo
[afi
][safi
] = NULL
;
1399 /* set nexthop-unchanged for l2vpn evpn by default */
1400 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1401 PEER_FLAG_NEXTHOP_UNCHANGED
);
1403 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1405 /* Initialize per peer bgp GR FSM */
1406 bgp_peer_gr_init(peer
);
1408 /* Create buffers. */
1409 peer
->ibuf
= stream_fifo_new();
1410 peer
->obuf
= stream_fifo_new();
1411 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1413 /* We use a larger buffer for peer->obuf_work in the event that:
1414 * - We RX a BGP_UPDATE where the attributes alone are just
1415 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1416 * - The user configures an outbound route-map that does many as-path
1417 * prepends or adds many communities. At most they can have
1418 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1419 * large they can make the attributes.
1421 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1422 * bounds checking for every single attribute as we construct an
1426 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1428 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1430 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1432 bgp_sync_init(peer
);
1434 /* Get service port number. */
1435 sp
= getservbyname("bgp", "tcp");
1436 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1438 QOBJ_REG(peer
, peer
);
1443 * This function is invoked when a duplicate peer structure associated with
1444 * a neighbor is being deleted. If this about-to-be-deleted structure is
1445 * the one with all the config, then we have to copy over the info.
1447 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1449 struct peer_af
*paf
;
1457 /* The following function is used by both peer group config copy to
1458 * individual peer and when we transfer config
1460 if (peer_src
->change_local_as
)
1461 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1463 /* peer flags apply */
1464 peer_dst
->flags
= peer_src
->flags
;
1466 * The doppelganger *must* not have a config node stored
1468 UNSET_FLAG(peer_dst
->flags
, PEER_FLAG_CONFIG_NODE
);
1469 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1470 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1472 peer_dst
->local_as
= peer_src
->local_as
;
1473 peer_dst
->port
= peer_src
->port
;
1474 /* copy tcp_mss value */
1475 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1476 (void)peer_sort(peer_dst
);
1477 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1478 peer_dst
->local_role
= peer_src
->local_role
;
1480 peer_dst
->max_packet_size
= peer_src
->max_packet_size
;
1483 peer_dst
->holdtime
= peer_src
->holdtime
;
1484 peer_dst
->keepalive
= peer_src
->keepalive
;
1485 peer_dst
->connect
= peer_src
->connect
;
1486 peer_dst
->delayopen
= peer_src
->delayopen
;
1487 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1488 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1489 peer_dst
->routeadv
= peer_src
->routeadv
;
1490 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1491 peer_dst
->v_delayopen
= peer_src
->v_delayopen
;
1493 /* password apply */
1494 if (peer_src
->password
&& !peer_dst
->password
)
1495 peer_dst
->password
=
1496 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1498 FOREACH_AFI_SAFI (afi
, safi
) {
1499 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1500 peer_dst
->af_flags
[afi
][safi
] = peer_src
->af_flags
[afi
][safi
];
1501 peer_dst
->allowas_in
[afi
][safi
] =
1502 peer_src
->allowas_in
[afi
][safi
];
1503 peer_dst
->weight
[afi
][safi
] = peer_src
->weight
[afi
][safi
];
1504 peer_dst
->addpath_type
[afi
][safi
] =
1505 peer_src
->addpath_type
[afi
][safi
];
1508 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1509 paf
= peer_src
->peer_af_array
[afidx
];
1511 if (!peer_af_find(peer_dst
, paf
->afi
, paf
->safi
))
1512 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1516 /* update-source apply */
1517 if (peer_src
->update_source
) {
1518 if (peer_dst
->update_source
)
1519 sockunion_free(peer_dst
->update_source
);
1520 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1521 peer_dst
->update_source
=
1522 sockunion_dup(peer_src
->update_source
);
1523 } else if (peer_src
->update_if
) {
1524 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1525 if (peer_dst
->update_source
) {
1526 sockunion_free(peer_dst
->update_source
);
1527 peer_dst
->update_source
= NULL
;
1529 peer_dst
->update_if
=
1530 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1533 if (peer_src
->ifname
) {
1534 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1537 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1541 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1542 struct interface
*ifp
)
1544 struct connected
*ifc
;
1547 struct listnode
*node
;
1549 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1550 * IPv4 address of the other end.
1552 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1553 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1554 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
1555 if (p
.prefixlen
== 30) {
1556 peer
->su
.sa
.sa_family
= AF_INET
;
1557 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1559 peer
->su
.sin
.sin_addr
.s_addr
=
1561 else if (addr
% 4 == 2)
1562 peer
->su
.sin
.sin_addr
.s_addr
=
1564 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1565 peer
->su
.sin
.sin_len
=
1566 sizeof(struct sockaddr_in
);
1567 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1569 } else if (p
.prefixlen
== 31) {
1570 peer
->su
.sa
.sa_family
= AF_INET
;
1571 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1573 peer
->su
.sin
.sin_addr
.s_addr
=
1576 peer
->su
.sin
.sin_addr
.s_addr
=
1578 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1579 peer
->su
.sin
.sin_len
=
1580 sizeof(struct sockaddr_in
);
1581 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1583 } else if (bgp_debug_neighbor_events(peer
))
1585 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1593 static bool bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1594 struct interface
*ifp
)
1596 struct nbr_connected
*ifc_nbr
;
1598 /* Have we learnt the peer's IPv6 link-local address? */
1599 if (ifp
->nbr_connected
1600 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1601 peer
->su
.sa
.sa_family
= AF_INET6
;
1602 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1603 sizeof(struct in6_addr
));
1605 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1607 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1615 * Set or reset the peer address socketunion structure based on the
1616 * learnt/derived peer address. If the address has changed, update the
1617 * password on the listen socket, if needed.
1619 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1621 struct interface
*ifp
;
1623 int peer_addr_updated
= 0;
1624 struct listnode
*node
;
1625 union sockunion old_su
;
1628 * This function is only ever needed when FRR an interface
1629 * based peering, so this simple test will tell us if
1630 * we are in an interface based configuration or not
1637 prev_family
= peer
->su
.sa
.sa_family
;
1638 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1640 /* If BGP unnumbered is not "v6only", we first see if we can
1642 * peer's IPv4 address.
1644 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1646 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1648 /* If "v6only" or we can't derive peer's IPv4 address, see if
1650 * learnt the peer's IPv6 link-local address. This is from the
1652 * IPv6 address in router advertisement.
1654 if (!peer_addr_updated
)
1656 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1658 /* If we could derive the peer address, we may need to install the
1660 * configured for the peer, if any, on the listen socket. Otherwise,
1662 * that peer's address is not available and uninstall the password, if
1665 if (peer_addr_updated
) {
1666 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1667 && prev_family
== AF_UNSPEC
)
1670 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1671 && prev_family
!= AF_UNSPEC
)
1672 bgp_md5_unset(peer
);
1673 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1674 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1678 * If they are the same, nothing to do here, move along
1680 if (!sockunion_same(&old_su
, &peer
->su
)) {
1681 union sockunion new_su
= peer
->su
;
1682 struct bgp
*bgp
= peer
->bgp
;
1685 * Our peer structure is stored in the bgp->peerhash
1686 * release it before we modify anything in both the
1687 * hash and the list. But *only* if the peer
1688 * is in the bgp->peerhash as that on deletion
1689 * we call bgp_stop which calls this function :(
1690 * so on deletion let's remove from the list first
1691 * and then do the deletion preventing this from
1692 * being added back on the list below when we
1693 * fail to remove it up here.
1697 * listnode_lookup just scans the list
1698 * for the peer structure so it's safe
1699 * to use without modifying the su
1701 node
= listnode_lookup(bgp
->peer
, peer
);
1704 * Let's reset the peer->su release and
1705 * reset it and put it back. We have to
1706 * do this because hash_release will
1707 * scan through looking for a matching
1711 hash_release(peer
->bgp
->peerhash
, peer
);
1712 listnode_delete(peer
->bgp
->peer
, peer
);
1715 (void)hash_get(peer
->bgp
->peerhash
, peer
,
1717 listnode_add_sort(peer
->bgp
->peer
, peer
);
1722 void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1724 struct bgp_dest
*dest
, *ndest
;
1725 struct bgp_table
*table
;
1727 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1728 dest
= bgp_route_next(dest
)) {
1729 table
= bgp_dest_get_bgp_table_info(dest
);
1730 if (table
!= NULL
) {
1731 /* Special handling for 2-level routing
1733 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1734 || safi
== SAFI_EVPN
) {
1735 for (ndest
= bgp_table_top(table
); ndest
;
1736 ndest
= bgp_route_next(ndest
))
1737 bgp_process(bgp
, ndest
, afi
, safi
);
1739 bgp_process(bgp
, dest
, afi
, safi
);
1744 /* Force a bestpath recalculation for all prefixes. This is used
1745 * when 'bgp bestpath' commands are entered.
1747 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1752 FOREACH_AFI_SAFI (afi
, safi
) {
1753 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1758 * Create new BGP peer.
1760 * conf_if and su are mutually exclusive if configuring from the cli.
1761 * If we are handing a doppelganger, then we *must* pass in both
1762 * the original peer's su and conf_if, so that we can appropriately
1763 * track the bgp->peerhash( ie we don't want to remove the current
1764 * one from the config ).
1766 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1767 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1768 int as_type
, struct peer_group
*group
,
1773 char buf
[SU_ADDRSTRLEN
];
1777 peer
= peer_new(bgp
);
1779 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1783 bgp_peer_conf_if_to_su_update(peer
);
1784 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1785 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1788 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1789 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1790 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1792 peer
->local_as
= local_as
;
1793 peer
->as
= remote_as
;
1794 peer
->as_type
= as_type
;
1795 peer
->local_id
= bgp
->router_id
;
1796 peer
->v_holdtime
= bgp
->default_holdtime
;
1797 peer
->v_keepalive
= bgp
->default_keepalive
;
1798 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1799 ? BGP_DEFAULT_IBGP_ROUTEADV
1800 : BGP_DEFAULT_EBGP_ROUTEADV
;
1801 if (bgp_config_inprocess())
1802 peer
->shut_during_cfg
= true;
1804 peer
= peer_lock(peer
); /* bgp peer list reference */
1805 peer
->group
= group
;
1806 listnode_add_sort(bgp
->peer
, peer
);
1809 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1811 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1813 /* Adjust update-group coalesce timer heuristics for # peers. */
1814 if (bgp
->heuristic_coalesce
) {
1815 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1817 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1818 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1821 active
= peer_active(peer
);
1823 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1824 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1826 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1829 /* Last read and reset time set */
1830 peer
->readtime
= peer
->resettime
= monotime(NULL
);
1832 /* Default TTL set. */
1833 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1835 /* Default configured keepalives count for shutdown rtt command */
1836 peer
->rtt_keepalive_conf
= 1;
1838 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1839 * neighbor for the corresponding address family. IPv4 Unicast is
1840 * the only address family enabled by default without expliict
1843 FOREACH_AFI_SAFI (afi
, safi
) {
1844 if (bgp
->default_af
[afi
][safi
]) {
1845 peer
->afc
[afi
][safi
] = 1;
1846 peer_af_create(peer
, afi
, safi
);
1850 /* auto shutdown if configured */
1851 if (bgp
->autoshutdown
)
1852 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1853 /* Set up peer's events and timers. */
1854 else if (!active
&& peer_active(peer
))
1855 bgp_timer_set(peer
);
1857 bgp_peer_gr_flags_update(peer
);
1858 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1863 /* Make accept BGP peer. This function is only called from the test code */
1864 struct peer
*peer_create_accept(struct bgp
*bgp
)
1868 peer
= peer_new(bgp
);
1870 peer
= peer_lock(peer
); /* bgp peer list reference */
1871 listnode_add_sort(bgp
->peer
, peer
);
1872 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1878 * Return true if we have a peer configured to use this afi/safi
1880 bool bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1882 struct listnode
*node
;
1885 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1886 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1889 if (peer
->afc
[afi
][safi
])
1896 /* Change peer's AS number. */
1897 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
)
1901 enum bgp_peer_sort origtype
, newtype
;
1904 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1905 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1906 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1907 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1908 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1910 bgp_session_reset(peer
);
1912 origtype
= peer_sort_lookup(peer
);
1914 peer
->as_type
= as_specified
;
1916 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1917 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1918 && peer
->bgp
->as
!= as
)
1919 peer
->local_as
= peer
->bgp
->confed_id
;
1921 peer
->local_as
= peer
->bgp
->as
;
1923 newtype
= peer_sort(peer
);
1924 /* Advertisement-interval reset */
1925 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1926 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1927 ? BGP_DEFAULT_IBGP_ROUTEADV
1928 : BGP_DEFAULT_EBGP_ROUTEADV
;
1932 if (newtype
== BGP_PEER_IBGP
)
1934 else if (origtype
== BGP_PEER_IBGP
)
1935 peer
->ttl
= BGP_DEFAULT_TTL
;
1937 /* reflector-client reset */
1938 if (newtype
!= BGP_PEER_IBGP
) {
1940 FOREACH_AFI_SAFI (afi
, safi
)
1941 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
1942 PEER_FLAG_ORR_GROUP
);
1944 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1945 PEER_FLAG_REFLECTOR_CLIENT
);
1946 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1947 PEER_FLAG_REFLECTOR_CLIENT
);
1948 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1949 PEER_FLAG_REFLECTOR_CLIENT
);
1950 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1951 PEER_FLAG_REFLECTOR_CLIENT
);
1952 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1953 PEER_FLAG_REFLECTOR_CLIENT
);
1954 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1955 PEER_FLAG_REFLECTOR_CLIENT
);
1956 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1957 PEER_FLAG_REFLECTOR_CLIENT
);
1958 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1959 PEER_FLAG_REFLECTOR_CLIENT
);
1960 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1961 PEER_FLAG_REFLECTOR_CLIENT
);
1962 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1963 PEER_FLAG_REFLECTOR_CLIENT
);
1964 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1965 PEER_FLAG_REFLECTOR_CLIENT
);
1966 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1967 PEER_FLAG_REFLECTOR_CLIENT
);
1968 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1969 PEER_FLAG_REFLECTOR_CLIENT
);
1973 /* If peer does not exist, create new one. If peer already exists,
1974 set AS number to the peer. */
1975 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1976 as_t
*as
, int as_type
)
1982 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1984 peer
= peer_lookup(bgp
, su
);
1987 /* Not allowed for a dynamic peer. */
1988 if (peer_dynamic_neighbor(peer
)) {
1990 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1993 /* When this peer is a member of peer-group. */
1995 /* peer-group already has AS number/internal/external */
1996 if (peer
->group
->conf
->as
1997 || peer
->group
->conf
->as_type
) {
1998 /* Return peer group's AS number. */
1999 *as
= peer
->group
->conf
->as
;
2000 return BGP_ERR_PEER_GROUP_MEMBER
;
2003 enum bgp_peer_sort peer_sort_type
=
2004 peer_sort(peer
->group
->conf
);
2006 /* Explicit AS numbers used, compare AS numbers */
2007 if (as_type
== AS_SPECIFIED
) {
2008 if (((peer_sort_type
== BGP_PEER_IBGP
)
2009 && (bgp
->as
!= *as
))
2010 || ((peer_sort_type
== BGP_PEER_EBGP
)
2011 && (bgp
->as
== *as
))) {
2013 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2016 /* internal/external used, compare as-types */
2017 if (((peer_sort_type
== BGP_PEER_IBGP
)
2018 && (as_type
!= AS_INTERNAL
))
2019 || ((peer_sort_type
== BGP_PEER_EBGP
)
2020 && (as_type
!= AS_EXTERNAL
))) {
2022 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2027 /* Existing peer's AS number change. */
2028 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2029 || (peer
->as_type
!= as_type
))
2030 peer_as_change(peer
, *as
, as_type
);
2033 return BGP_ERR_NO_INTERFACE_CONFIG
;
2035 /* If the peer is not part of our confederation, and its not an
2036 iBGP peer then spoof the source AS */
2037 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
2038 && !bgp_confederation_peers_check(bgp
, *as
)
2040 local_as
= bgp
->confed_id
;
2044 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
,
2051 const char *bgp_get_name_by_role(uint8_t role
)
2056 case ROLE_RS_SERVER
:
2058 case ROLE_RS_CLIENT
:
2064 case ROLE_UNDEFINED
:
2070 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
2071 struct peer
*peer
, afi_t afi
,
2075 int out
= FILTER_OUT
;
2077 uint64_t pflags_ovrd
;
2078 uint8_t *pfilter_ovrd
;
2082 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2083 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2085 /* peer af_flags apply */
2086 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2087 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2088 ^ peer
->af_flags_invert
[afi
][safi
];
2089 flags_tmp
&= ~pflags_ovrd
;
2091 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2092 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2093 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2094 conf
->af_flags_invert
[afi
][safi
]);
2096 /* maximum-prefix */
2097 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2098 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2099 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2100 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2103 /* maximum-prefix-out */
2104 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX_OUT
))
2105 PEER_ATTR_INHERIT(peer
, group
, pmax_out
[afi
][safi
]);
2108 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2109 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2112 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_SOO
))
2113 PEER_ATTR_INHERIT(peer
, group
, soo
[afi
][safi
]);
2116 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2117 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2119 /* default-originate route-map */
2120 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2121 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2122 MTYPE_ROUTE_MAP_NAME
);
2123 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2126 /* inbound filter apply */
2127 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2128 PEER_STR_ATTR_INHERIT(peer
, group
,
2129 filter
[afi
][safi
].dlist
[in
].name
,
2130 MTYPE_BGP_FILTER_NAME
);
2131 PEER_ATTR_INHERIT(peer
, group
,
2132 filter
[afi
][safi
].dlist
[in
].alist
);
2135 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2136 PEER_STR_ATTR_INHERIT(peer
, group
,
2137 filter
[afi
][safi
].plist
[in
].name
,
2138 MTYPE_BGP_FILTER_NAME
);
2139 PEER_ATTR_INHERIT(peer
, group
,
2140 filter
[afi
][safi
].plist
[in
].plist
);
2143 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2144 PEER_STR_ATTR_INHERIT(peer
, group
,
2145 filter
[afi
][safi
].aslist
[in
].name
,
2146 MTYPE_BGP_FILTER_NAME
);
2147 PEER_ATTR_INHERIT(peer
, group
,
2148 filter
[afi
][safi
].aslist
[in
].aslist
);
2151 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2152 PEER_STR_ATTR_INHERIT(peer
, group
,
2153 filter
[afi
][safi
].map
[in
].name
,
2154 MTYPE_BGP_FILTER_NAME
);
2155 PEER_ATTR_INHERIT(peer
, group
,
2156 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2159 /* outbound filter apply */
2160 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2161 PEER_STR_ATTR_INHERIT(peer
, group
,
2162 filter
[afi
][safi
].dlist
[out
].name
,
2163 MTYPE_BGP_FILTER_NAME
);
2164 PEER_ATTR_INHERIT(peer
, group
,
2165 filter
[afi
][safi
].dlist
[out
].alist
);
2168 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2169 PEER_STR_ATTR_INHERIT(peer
, group
,
2170 filter
[afi
][safi
].plist
[out
].name
,
2171 MTYPE_BGP_FILTER_NAME
);
2172 PEER_ATTR_INHERIT(peer
, group
,
2173 filter
[afi
][safi
].plist
[out
].plist
);
2176 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2177 PEER_STR_ATTR_INHERIT(peer
, group
,
2178 filter
[afi
][safi
].aslist
[out
].name
,
2179 MTYPE_BGP_FILTER_NAME
);
2180 PEER_ATTR_INHERIT(peer
, group
,
2181 filter
[afi
][safi
].aslist
[out
].aslist
);
2184 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2185 PEER_STR_ATTR_INHERIT(peer
, group
,
2186 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2187 MTYPE_BGP_FILTER_NAME
);
2188 PEER_ATTR_INHERIT(peer
, group
,
2189 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2192 /* nondirectional filter apply */
2193 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2194 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2195 MTYPE_BGP_FILTER_NAME
);
2196 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2199 /* Conditional Advertisements */
2200 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ADVERTISE_MAP
)) {
2201 PEER_STR_ATTR_INHERIT(peer
, group
,
2202 filter
[afi
][safi
].advmap
.aname
,
2203 MTYPE_BGP_FILTER_NAME
);
2204 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.amap
);
2205 PEER_STR_ATTR_INHERIT(peer
, group
,
2206 filter
[afi
][safi
].advmap
.cname
,
2207 MTYPE_BGP_FILTER_NAME
);
2208 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.cmap
);
2209 PEER_ATTR_INHERIT(peer
, group
,
2210 filter
[afi
][safi
].advmap
.condition
);
2213 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2214 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2215 bgp_addpath_type_changed(conf
->bgp
);
2219 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2224 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2225 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2226 __func__
, peer
->host
);
2230 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2232 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2233 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2234 return BGP_ERR_PEER_SAFI_CONFLICT
;
2236 /* Nothing to do if we've already activated this peer */
2237 if (peer
->afc
[afi
][safi
])
2240 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2243 active
= peer_active(peer
);
2244 peer
->afc
[afi
][safi
] = 1;
2247 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2249 if (!active
&& peer_active(peer
)) {
2250 bgp_timer_set(peer
);
2252 if (peer_established(peer
)) {
2253 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2254 peer
->afc_adv
[afi
][safi
] = 1;
2255 bgp_capability_send(peer
, afi
, safi
,
2257 CAPABILITY_ACTION_SET
);
2258 if (peer
->afc_recv
[afi
][safi
]) {
2259 peer
->afc_nego
[afi
][safi
] = 1;
2260 bgp_announce_route(peer
, afi
, safi
,
2264 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2265 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2266 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2269 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2270 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2271 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2272 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2275 * If we are turning on a AFI/SAFI locally and we've
2276 * started bringing a peer up, we need to tell
2277 * the other peer to restart because we might loose
2278 * configuration here because when the doppelganger
2279 * gets to a established state due to how
2280 * we resolve we could just overwrite the afi/safi
2283 other
= peer
->doppelganger
;
2285 && (other
->status
== OpenSent
2286 || other
->status
== OpenConfirm
)) {
2287 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2288 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2289 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2296 /* Activate the peer or peer group for specified AFI and SAFI. */
2297 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2300 struct peer_group
*group
;
2301 struct listnode
*node
, *nnode
;
2302 struct peer
*tmp_peer
;
2305 /* Nothing to do if we've already activated this peer */
2306 if (peer
->afc
[afi
][safi
])
2311 /* This is a peer-group so activate all of the members of the
2312 * peer-group as well */
2313 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2315 /* Do not activate a peer for both SAFI_UNICAST and
2316 * SAFI_LABELED_UNICAST */
2317 if ((safi
== SAFI_UNICAST
2318 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2319 || (safi
== SAFI_LABELED_UNICAST
2320 && peer
->afc
[afi
][SAFI_UNICAST
]))
2321 return BGP_ERR_PEER_SAFI_CONFLICT
;
2323 peer
->afc
[afi
][safi
] = 1;
2324 group
= peer
->group
;
2326 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2327 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2330 ret
|= peer_activate_af(peer
, afi
, safi
);
2333 /* If this is the first peer to be activated for this
2334 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2335 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2336 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2338 if (BGP_DEBUG(zebra
, ZEBRA
))
2340 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2342 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2343 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2346 if (safi
== SAFI_FLOWSPEC
) {
2347 /* connect to table manager */
2348 bgp_zebra_init_tm_connect(bgp
);
2353 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2356 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2357 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2358 __func__
, peer
->host
);
2362 /* Nothing to do if we've already deactivated this peer */
2363 if (!peer
->afc
[afi
][safi
])
2366 /* De-activate the address family configuration. */
2367 peer
->afc
[afi
][safi
] = 0;
2369 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2370 flog_err(EC_BGP_PEER_DELETE
,
2371 "couldn't delete af structure for peer %s(%s, %s)",
2372 peer
->host
, afi2str(afi
), safi2str(safi
));
2376 if (peer_established(peer
)) {
2377 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2378 peer
->afc_adv
[afi
][safi
] = 0;
2379 peer
->afc_nego
[afi
][safi
] = 0;
2381 if (peer_active_nego(peer
)) {
2382 bgp_capability_send(peer
, afi
, safi
,
2384 CAPABILITY_ACTION_UNSET
);
2385 bgp_clear_route(peer
, afi
, safi
);
2386 peer
->pcount
[afi
][safi
] = 0;
2388 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2389 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2390 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2393 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2394 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2395 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2402 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2405 struct peer_group
*group
;
2406 struct peer
*tmp_peer
;
2407 struct listnode
*node
, *nnode
;
2410 /* Nothing to do if we've already de-activated this peer */
2411 if (!peer
->afc
[afi
][safi
])
2414 /* This is a peer-group so de-activate all of the members of the
2415 * peer-group as well */
2416 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2417 peer
->afc
[afi
][safi
] = 0;
2418 group
= peer
->group
;
2420 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2421 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2424 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2429 /* If this is the last peer to be deactivated for this
2430 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2431 if (safi
== SAFI_LABELED_UNICAST
2432 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2433 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2435 if (BGP_DEBUG(zebra
, ZEBRA
))
2437 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2439 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2440 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2445 void peer_nsf_stop(struct peer
*peer
)
2450 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2451 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2453 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2454 peer
->nsf
[afi
][safi
] = 0;
2455 THREAD_OFF(peer
->t_llgr_stale
[afi
][safi
]);
2458 if (peer
->t_gr_restart
) {
2459 THREAD_OFF(peer
->t_gr_restart
);
2460 if (bgp_debug_neighbor_events(peer
))
2461 zlog_debug("%pBP graceful restart timer stopped", peer
);
2463 if (peer
->t_gr_stale
) {
2464 THREAD_OFF(peer
->t_gr_stale
);
2465 if (bgp_debug_neighbor_events(peer
))
2467 "%pBP graceful restart stalepath timer stopped",
2470 bgp_clear_route_all(peer
);
2473 /* Delete peer from confguration.
2475 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2476 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2478 * This function /should/ take care to be idempotent, to guard against
2479 * it being called multiple times through stray events that come in
2480 * that happen to result in this function being called again. That
2481 * said, getting here for a "Deleted" peer is a bug in the neighbour
2484 int peer_delete(struct peer
*peer
)
2490 struct bgp_filter
*filter
;
2491 struct listnode
*pn
;
2494 assert(peer
->status
!= Deleted
);
2497 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2499 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2501 bgp_keepalives_off(peer
);
2502 bgp_reads_off(peer
);
2503 bgp_writes_off(peer
);
2504 thread_cancel_event_ready(bm
->master
, peer
);
2505 FOREACH_AFI_SAFI (afi
, safi
)
2506 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
2507 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2508 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2509 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2511 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2512 peer_nsf_stop(peer
);
2514 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2516 /* Remove BFD settings. */
2517 if (peer
->bfd_config
)
2518 bgp_peer_remove_bfd_config(peer
);
2520 /* If this peer belongs to peer group, clear up the
2523 if (peer_dynamic_neighbor(peer
))
2524 peer_drop_dynamic_neighbor(peer
);
2526 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2528 peer
); /* group->peer list reference */
2529 list_delete_node(peer
->group
->peer
, pn
);
2534 /* Withdraw all information from routing table. We can not use
2535 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2536 * executed after peer structure is deleted.
2538 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2540 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2542 if (peer
->doppelganger
) {
2543 peer
->doppelganger
->doppelganger
= NULL
;
2544 peer
->doppelganger
= NULL
;
2547 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2548 bgp_fsm_change_status(peer
, Deleted
);
2550 /* Remove from NHT */
2551 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2552 bgp_unlink_nexthop_by_peer(peer
);
2554 /* Password configuration */
2555 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2556 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2557 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2558 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2559 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2560 bgp_md5_unset(peer
);
2563 bgp_timer_set(peer
); /* stops all timers for Deleted */
2565 /* Delete from all peer list. */
2566 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2567 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2569 * Removing from the list node first because
2570 * peer_unlock *can* call peer_delete( I know,
2571 * I know ). So let's remove it and in
2572 * the su recalculate function we'll ensure
2573 * it's in there or not.
2575 list_delete_node(bgp
->peer
, pn
);
2576 hash_release(bgp
->peerhash
, peer
);
2577 peer_unlock(peer
); /* bgp peer list reference */
2582 stream_fifo_free(peer
->ibuf
);
2587 stream_fifo_free(peer
->obuf
);
2591 if (peer
->ibuf_work
) {
2592 ringbuf_del(peer
->ibuf_work
);
2593 peer
->ibuf_work
= NULL
;
2596 if (peer
->obuf_work
) {
2597 stream_free(peer
->obuf_work
);
2598 peer
->obuf_work
= NULL
;
2601 if (peer
->scratch
) {
2602 stream_free(peer
->scratch
);
2603 peer
->scratch
= NULL
;
2606 /* Local and remote addresses. */
2607 if (peer
->su_local
) {
2608 sockunion_free(peer
->su_local
);
2609 peer
->su_local
= NULL
;
2612 if (peer
->su_remote
) {
2613 sockunion_free(peer
->su_remote
);
2614 peer
->su_remote
= NULL
;
2617 /* Free filter related memory. */
2618 FOREACH_AFI_SAFI (afi
, safi
) {
2619 filter
= &peer
->filter
[afi
][safi
];
2621 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2622 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2623 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2624 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2627 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2628 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2631 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2632 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2633 ecommunity_free(&peer
->soo
[afi
][safi
]);
2636 FOREACH_AFI_SAFI (afi
, safi
)
2637 peer_af_delete(peer
, afi
, safi
);
2639 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2640 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2642 peer_unlock(peer
); /* initial reference */
2647 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2649 return strcmp(g1
->name
, g2
->name
);
2652 /* Peer group cofiguration. */
2653 static struct peer_group
*peer_group_new(void)
2655 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2658 static void peer_group_free(struct peer_group
*group
)
2660 XFREE(MTYPE_PEER_GROUP
, group
);
2663 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2665 struct peer_group
*group
;
2666 struct listnode
*node
, *nnode
;
2668 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2669 if (strcmp(group
->name
, name
) == 0)
2675 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2677 struct peer_group
*group
;
2681 group
= peer_group_lookup(bgp
, name
);
2685 group
= peer_group_new();
2687 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2688 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2689 group
->peer
= list_new();
2690 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2691 group
->listen_range
[afi
] = list_new();
2692 group
->conf
= peer_new(bgp
);
2693 FOREACH_AFI_SAFI (afi
, safi
) {
2694 if (bgp
->default_af
[afi
][safi
])
2695 group
->conf
->afc
[afi
][safi
] = 1;
2697 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2698 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2699 group
->conf
->group
= group
;
2700 group
->conf
->as
= 0;
2701 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2702 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2703 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2704 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2705 listnode_add_sort(bgp
->group
, group
);
2710 static void peer_group2peer_config_copy(struct peer_group
*group
,
2715 bool config_node
= !!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2721 peer
->as
= conf
->as
;
2724 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2725 peer
->change_local_as
= conf
->change_local_as
;
2727 /* If peer-group has configured TTL then override it */
2728 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2729 peer
->ttl
= conf
->ttl
;
2732 peer
->gtsm_hops
= conf
->gtsm_hops
;
2734 /* peer flags apply */
2735 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2736 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2737 flags_tmp
&= ~peer
->flags_override
;
2739 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2740 SET_FLAG(peer
->flags
, flags_tmp
);
2741 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2744 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2746 /* peer timers apply */
2747 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2748 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2749 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2752 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2753 PEER_ATTR_INHERIT(peer
, group
, connect
);
2754 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2755 peer
->v_connect
= conf
->connect
;
2757 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2760 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2761 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2762 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2763 peer
->v_delayopen
= conf
->delayopen
;
2765 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2768 /* advertisement-interval apply */
2769 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2770 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2771 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2772 peer
->v_routeadv
= conf
->routeadv
;
2774 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2775 ? BGP_DEFAULT_IBGP_ROUTEADV
2776 : BGP_DEFAULT_EBGP_ROUTEADV
;
2779 /* capability extended-nexthop apply */
2780 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2781 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2782 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2784 /* password apply */
2785 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2786 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2787 MTYPE_PEER_PASSWORD
);
2789 if (!BGP_PEER_SU_UNSPEC(peer
))
2792 /* update-source apply */
2793 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2794 if (conf
->update_source
) {
2795 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2796 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2797 } else if (conf
->update_if
) {
2798 sockunion_free(peer
->update_source
);
2799 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2800 MTYPE_PEER_UPDATE_SOURCE
);
2805 PEER_ATTR_INHERIT(peer
, group
, local_role
);
2807 /* Update GR flags for the peer. */
2808 bgp_peer_gr_flags_update(peer
);
2810 /* Apply BFD settings from group to peer if it exists. */
2811 if (conf
->bfd_config
) {
2812 bgp_peer_configure_bfd(peer
, false);
2813 bgp_peer_config_apply(peer
, group
);
2817 /* Peer group's remote AS configuration. */
2818 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2821 struct peer_group
*group
;
2823 struct listnode
*node
, *nnode
;
2825 group
= peer_group_lookup(bgp
, group_name
);
2829 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2833 /* When we setup peer-group AS number all peer group member's AS
2834 number must be updated to same number. */
2835 peer_as_change(group
->conf
, *as
, as_type
);
2837 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2838 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2839 || (peer
->as_type
!= as_type
))
2840 peer_as_change(peer
, *as
, as_type
);
2846 void peer_notify_unconfig(struct peer
*peer
)
2848 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2849 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2850 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2853 static void peer_notify_shutdown(struct peer
*peer
)
2855 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
2856 if (bgp_debug_neighbor_events(peer
))
2858 "%pBP configured Graceful-Restart, skipping shutdown notification",
2863 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2864 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2865 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
2868 void peer_group_notify_unconfig(struct peer_group
*group
)
2870 struct peer
*peer
, *other
;
2871 struct listnode
*node
, *nnode
;
2873 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2874 other
= peer
->doppelganger
;
2875 if (other
&& other
->status
!= Deleted
) {
2876 other
->group
= NULL
;
2877 peer_notify_unconfig(other
);
2879 peer_notify_unconfig(peer
);
2883 int peer_group_delete(struct peer_group
*group
)
2887 struct prefix
*prefix
;
2889 struct listnode
*node
, *nnode
;
2894 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2895 other
= peer
->doppelganger
;
2897 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2898 bgp_zebra_terminate_radv(bgp
, peer
);
2901 if (other
&& other
->status
!= Deleted
) {
2902 other
->group
= NULL
;
2906 list_delete(&group
->peer
);
2908 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2909 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2911 prefix_free(&prefix
);
2913 list_delete(&group
->listen_range
[afi
]);
2916 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2919 if (group
->conf
->bfd_config
)
2920 bgp_peer_remove_bfd_config(group
->conf
);
2922 group
->conf
->group
= NULL
;
2923 peer_delete(group
->conf
);
2925 /* Delete from all peer_group list. */
2926 listnode_delete(bgp
->group
, group
);
2928 peer_group_free(group
);
2933 int peer_group_remote_as_delete(struct peer_group
*group
)
2935 struct peer
*peer
, *other
;
2936 struct listnode
*node
, *nnode
;
2938 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2939 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2942 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2943 other
= peer
->doppelganger
;
2945 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2946 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2950 if (other
&& other
->status
!= Deleted
) {
2951 other
->group
= NULL
;
2955 list_delete_all_node(group
->peer
);
2957 group
->conf
->as
= 0;
2958 group
->conf
->as_type
= AS_UNSPECIFIED
;
2963 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2965 struct prefix
*prefix
;
2966 struct listnode
*node
, *nnode
;
2969 afi
= family2afi(range
->family
);
2971 /* Group needs remote AS configured. */
2972 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2973 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2975 /* Ensure no duplicates. Currently we don't care about overlaps. */
2976 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2977 if (prefix_same(range
, prefix
))
2981 prefix
= prefix_new();
2982 prefix_copy(prefix
, range
);
2983 listnode_add(group
->listen_range
[afi
], prefix
);
2985 /* Update passwords for new ranges */
2986 if (group
->conf
->password
)
2987 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
2992 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2994 struct prefix
*prefix
, prefix2
;
2995 struct listnode
*node
, *nnode
;
2999 afi
= family2afi(range
->family
);
3001 /* Identify the listen range. */
3002 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
3003 if (prefix_same(range
, prefix
))
3008 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
3010 /* Dispose off any dynamic neighbors that exist due to this listen range
3012 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
3013 if (!peer_dynamic_neighbor(peer
))
3016 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
3017 && prefix_match(prefix
, &prefix2
)) {
3018 if (bgp_debug_neighbor_events(peer
))
3020 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
3021 peer
->host
, group
->name
, prefix
);
3026 /* Get rid of the listen range */
3027 listnode_delete(group
->listen_range
[afi
], prefix
);
3029 /* Remove passwords for deleted ranges */
3030 if (group
->conf
->password
)
3031 bgp_md5_unset_prefix(group
->bgp
, prefix
);
3036 /* Bind specified peer to peer group. */
3037 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
3038 struct peer_group
*group
, as_t
*as
)
3040 int first_member
= 0;
3043 enum bgp_peer_sort ptype
, gtype
;
3045 /* Lookup the peer. */
3047 peer
= peer_lookup(bgp
, su
);
3049 /* The peer exist, bind it to the peer-group */
3051 /* When the peer already belongs to a peer-group, check the
3053 if (peer_group_active(peer
)) {
3055 /* The peer is already bound to the peer-group,
3058 if (strcmp(peer
->group
->name
, group
->name
) == 0)
3061 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
3064 /* The peer has not specified a remote-as, inherit it from the
3066 if (peer
->as_type
== AS_UNSPECIFIED
) {
3067 peer
->as_type
= group
->conf
->as_type
;
3068 peer
->as
= group
->conf
->as
;
3069 peer
->sort
= group
->conf
->sort
;
3072 ptype
= peer_sort(peer
);
3073 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
3074 gtype
= peer_sort(group
->conf
);
3075 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
3078 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
3081 if (gtype
== BGP_PEER_INTERNAL
)
3085 peer_group2peer_config_copy(group
, peer
);
3087 FOREACH_AFI_SAFI (afi
, safi
) {
3088 if (group
->conf
->afc
[afi
][safi
]) {
3089 peer
->afc
[afi
][safi
] = 1;
3091 if (peer_af_find(peer
, afi
, safi
)
3092 || peer_af_create(peer
, afi
, safi
)) {
3093 peer_group2peer_config_copy_af(
3094 group
, peer
, afi
, safi
);
3096 } else if (peer
->afc
[afi
][safi
])
3097 peer_deactivate(peer
, afi
, safi
);
3101 assert(group
&& peer
->group
== group
);
3103 listnode_delete(bgp
->peer
, peer
);
3105 peer
->group
= group
;
3106 listnode_add_sort(bgp
->peer
, peer
);
3108 peer
= peer_lock(peer
); /* group->peer list reference */
3109 listnode_add(group
->peer
, peer
);
3113 gtype
= peer_sort(group
->conf
);
3114 /* Advertisement-interval reset */
3115 if (!CHECK_FLAG(group
->conf
->flags
,
3116 PEER_FLAG_ROUTEADV
)) {
3117 group
->conf
->v_routeadv
=
3118 (gtype
== BGP_PEER_IBGP
)
3119 ? BGP_DEFAULT_IBGP_ROUTEADV
3120 : BGP_DEFAULT_EBGP_ROUTEADV
;
3123 /* ebgp-multihop reset */
3124 if (gtype
== BGP_PEER_IBGP
)
3125 group
->conf
->ttl
= MAXTTL
;
3128 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3130 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3131 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3132 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3133 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3135 bgp_session_reset(peer
);
3139 /* Create a new peer. */
3141 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3142 && (!group
->conf
->as
)) {
3143 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3146 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3147 group
->conf
->as_type
, group
, true);
3149 peer
= peer_lock(peer
); /* group->peer list reference */
3150 listnode_add(group
->peer
, peer
);
3152 peer_group2peer_config_copy(group
, peer
);
3154 /* If the peer-group is active for this afi/safi then activate
3156 FOREACH_AFI_SAFI (afi
, safi
) {
3157 if (group
->conf
->afc
[afi
][safi
]) {
3158 peer
->afc
[afi
][safi
] = 1;
3160 if (!peer_af_find(peer
, afi
, safi
))
3161 peer_af_create(peer
, afi
, safi
);
3163 peer_group2peer_config_copy_af(group
, peer
, afi
,
3165 } else if (peer
->afc
[afi
][safi
])
3166 peer_deactivate(peer
, afi
, safi
);
3169 /* Set up peer's events and timers. */
3170 if (peer_active(peer
))
3171 bgp_timer_set(peer
);
3177 static void bgp_startup_timer_expire(struct thread
*thread
)
3181 bgp
= THREAD_ARG(thread
);
3182 bgp
->t_startup
= NULL
;
3186 * On shutdown we call the cleanup function which
3187 * does a free of the link list nodes, free up
3188 * the data we are pointing at too.
3190 static void bgp_vrf_string_name_delete(void *data
)
3194 XFREE(MTYPE_TMP
, vname
);
3197 /* BGP instance creation by `router bgp' commands. */
3198 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3199 enum bgp_instance_type inst_type
)
3205 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3207 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3208 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3209 zlog_debug("Creating Default VRF, AS %u", *as
);
3211 zlog_debug("Creating %s %s, AS %u",
3212 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3218 /* Default the EVPN VRF to the default one */
3219 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3226 bgp
->allow_martian
= false;
3227 bgp_process_queue_init(bgp
);
3228 bgp
->heuristic_coalesce
= true;
3229 bgp
->inst_type
= inst_type
;
3230 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3232 bgp
->peer_self
= peer_new(bgp
);
3233 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3234 bgp
->peer_self
->host
=
3235 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3236 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3237 if (cmd_hostname_get())
3238 bgp
->peer_self
->hostname
=
3239 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3241 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3242 if (cmd_domainname_get())
3243 bgp
->peer_self
->domainname
=
3244 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3245 bgp
->peer
= list_new();
3246 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3247 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3249 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3251 bgp
->group
= list_new();
3252 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3254 FOREACH_AFI_SAFI (afi
, safi
) {
3255 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3256 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3257 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3259 /* Enable maximum-paths */
3260 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3262 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3264 /* Initialize graceful restart info */
3265 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3266 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3267 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3268 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3269 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3272 bgp
->v_update_delay
= bm
->v_update_delay
;
3273 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3274 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3275 bgp
->default_subgroup_pkt_queue_max
=
3276 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3277 bgp_tcp_keepalive_unset(bgp
);
3278 bgp_timers_unset(bgp
);
3279 bgp
->default_min_holdtime
= 0;
3280 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3281 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3282 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3283 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3284 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3285 bgp
->dynamic_neighbors_count
= 0;
3286 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3287 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3288 bgp
->reject_as_sets
= false;
3289 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3290 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3291 bgp
->fast_convergence
= false;
3293 bgp
->llgr_stale_time
= BGP_DEFAULT_LLGR_STALE_TIME
;
3295 #ifdef ENABLE_BGP_VNC
3296 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3297 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3299 assert(bgp
->rfapi_cfg
);
3301 #endif /* ENABLE_BGP_VNC */
3303 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3304 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3305 bgp
->vpn_policy
[afi
].afi
= afi
;
3306 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3307 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3310 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3311 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3312 bgp_vrf_string_name_delete
;
3313 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3314 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3315 bgp_vrf_string_name_delete
;
3316 SET_FLAG(bgp
->af_flags
[afi
][SAFI_MPLS_VPN
],
3317 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL
);
3320 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3322 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3323 bgp
->restart_time
, &bgp
->t_startup
);
3325 /* printable name we can use in debug messages */
3326 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3327 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3337 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3339 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3340 snprintf(bgp
->name_pretty
, len
, "%s %s",
3341 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3347 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3348 memory_order_relaxed
);
3349 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3350 memory_order_relaxed
);
3351 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3352 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3356 update_bgp_group_init(bgp
);
3358 /* assign a unique rd id for auto derivation of vrf's RD */
3359 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3361 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3362 sizeof(struct bgp_evpn_info
));
3364 bgp_evpn_vrf_es_init(bgp
);
3368 /*initilize global GR FSM */
3369 bgp_global_gr_init(bgp
);
3371 memset(&bgp
->ebgprequirespolicywarning
, 0,
3372 sizeof(bgp
->ebgprequirespolicywarning
));
3377 /* Return the "default VRF" instance of BGP. */
3378 struct bgp
*bgp_get_default(void)
3381 struct listnode
*node
, *nnode
;
3383 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3384 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3389 /* Lookup BGP entry. */
3390 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3393 struct listnode
*node
, *nnode
;
3395 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3397 && ((bgp
->name
== NULL
&& name
== NULL
)
3398 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3403 /* Lookup BGP structure by view name. */
3404 struct bgp
*bgp_lookup_by_name(const char *name
)
3407 struct listnode
*node
, *nnode
;
3409 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3410 if ((bgp
->name
== NULL
&& name
== NULL
)
3411 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3416 /* Lookup BGP instance based on VRF id. */
3417 /* Note: Only to be used for incoming messages from Zebra. */
3418 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3422 /* Lookup VRF (in tree) and follow link. */
3423 vrf
= vrf_lookup_by_id(vrf_id
);
3426 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3429 /* Sets the BGP instance where EVPN is enabled */
3430 void bgp_set_evpn(struct bgp
*bgp
)
3432 if (bm
->bgp_evpn
== bgp
)
3435 /* First, release the reference count we hold on the instance */
3437 bgp_unlock(bm
->bgp_evpn
);
3441 /* Increase the reference count on this new VRF */
3443 bgp_lock(bm
->bgp_evpn
);
3446 /* Returns the BGP instance where EVPN is enabled, if any */
3447 struct bgp
*bgp_get_evpn(void)
3449 return bm
->bgp_evpn
;
3452 /* handle socket creation or deletion, if necessary
3453 * this is called for all new BGP instances
3455 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3458 struct listnode
*node
;
3461 /* Create BGP server socket, if listen mode not disabled */
3462 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3464 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3466 * suppress vrf socket
3469 bgp_close_vrf_socket(bgp
);
3473 return BGP_ERR_INVALID_VALUE
;
3475 * if vrf_id did not change
3477 if (vrf
->vrf_id
== old_vrf_id
)
3479 if (old_vrf_id
!= VRF_UNKNOWN
) {
3480 /* look for old socket. close it. */
3481 bgp_close_vrf_socket(bgp
);
3483 /* if backend is not yet identified ( VRF_UNKNOWN) then
3484 * creation will be done later
3486 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3488 if (list_isempty(bm
->addresses
)) {
3489 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3490 return BGP_ERR_INVALID_VALUE
;
3492 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3493 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3494 return BGP_ERR_INVALID_VALUE
;
3498 return bgp_check_main_socket(create
, bgp
);
3501 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3502 enum bgp_instance_type inst_type
)
3506 /* Multiple instance check. */
3508 bgp
= bgp_lookup_by_name(name
);
3510 bgp
= bgp_get_default();
3514 if (bgp
->as
!= *as
) {
3516 return BGP_ERR_AS_MISMATCH
;
3518 if (bgp
->inst_type
!= inst_type
)
3519 return BGP_ERR_INSTANCE_MISMATCH
;
3527 /* Called from VTY commands. */
3528 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3529 enum bgp_instance_type inst_type
)
3532 struct vrf
*vrf
= NULL
;
3535 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3536 if (ret
|| *bgp_val
)
3539 bgp
= bgp_create(as
, name
, inst_type
);
3542 * view instances will never work inside of a vrf
3543 * as such they must always be in the VRF_DEFAULT
3544 * Also we must set this to something useful because
3545 * of the vrf socket code needing an actual useful
3546 * default value to send to the underlying OS.
3548 * This code is currently ignoring vrf based
3549 * code using the -Z option( and that is probably
3550 * best addressed elsewhere in the code )
3552 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3553 bgp
->vrf_id
= VRF_DEFAULT
;
3555 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3556 bgp_address_init(bgp
);
3557 bgp_tip_hash_init(bgp
);
3561 bgp
->t_rmap_def_originate_eval
= NULL
;
3563 /* If Default instance or VRF, link to the VRF structure, if present. */
3564 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3565 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3566 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3568 bgp_vrf_link(bgp
, vrf
);
3570 /* BGP server socket already processed if BGP instance
3571 * already part of the list
3573 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3574 listnode_add(bm
->bgp
, bgp
);
3576 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3577 if (BGP_DEBUG(zebra
, ZEBRA
))
3578 zlog_debug("%s: Registering BGP instance %s to zebra",
3580 bgp_zebra_instance_register(bgp
);
3586 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3587 vrf_id_t vrf_id
, bool set
)
3591 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3594 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3598 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3600 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3604 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3608 struct list
*red_list
;
3609 struct listnode
*node
;
3610 struct bgp_redist
*red
;
3612 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3613 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3615 red_list
= bgp
->redist
[afi
][i
];
3619 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3620 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3627 * Make BGP instance "up". Applies only to VRFs (non-default) and
3628 * implies the VRF has been learnt from Zebra.
3630 void bgp_instance_up(struct bgp
*bgp
)
3633 struct listnode
*node
, *next
;
3635 bgp_set_redist_vrf_bitmaps(bgp
, true);
3637 /* Register with zebra. */
3638 bgp_zebra_instance_register(bgp
);
3640 /* Kick off any peers that may have been configured. */
3641 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3642 if (!BGP_PEER_START_SUPPRESSED(peer
))
3643 BGP_EVENT_ADD(peer
, BGP_Start
);
3646 /* Process any networks that have been configured. */
3647 bgp_static_add(bgp
);
3651 * Make BGP instance "down". Applies only to VRFs (non-default) and
3652 * implies the VRF has been deleted by Zebra.
3654 void bgp_instance_down(struct bgp
*bgp
)
3657 struct listnode
*node
;
3658 struct listnode
*next
;
3661 if (bgp
->t_rmap_def_originate_eval
) {
3662 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3663 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3667 /* Bring down peers, so corresponding routes are purged. */
3668 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3669 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3670 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3671 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3673 bgp_session_reset(peer
);
3676 /* Purge network and redistributed routes. */
3677 bgp_purge_static_redist_routes(bgp
);
3679 /* Cleanup registered nexthops (flags) */
3680 bgp_cleanup_nexthops(bgp
);
3682 bgp_zebra_instance_deregister(bgp
);
3684 bgp_set_redist_vrf_bitmaps(bgp
, false);
3687 /* Delete BGP instance. */
3688 int bgp_delete(struct bgp
*bgp
)
3691 struct peer_group
*group
;
3692 struct listnode
*node
, *next
;
3697 struct graceful_restart_info
*gr_info
;
3701 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3703 /* make sure we withdraw any exported routes */
3704 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3706 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3709 bgp_vpn_leak_unimport(bgp
);
3711 hook_call(bgp_inst_delete
, bgp
);
3713 FOREACH_AFI_SAFI (afi
, safi
)
3714 THREAD_OFF(bgp
->t_revalidate
[afi
][safi
]);
3716 THREAD_OFF(bgp
->t_condition_check
);
3717 THREAD_OFF(bgp
->t_startup
);
3718 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3719 THREAD_OFF(bgp
->t_update_delay
);
3720 THREAD_OFF(bgp
->t_establish_wait
);
3722 /* Set flag indicating bgp instance delete in progress */
3723 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3725 /* Delete the graceful restart info */
3726 FOREACH_AFI_SAFI (afi
, safi
) {
3729 gr_info
= &bgp
->gr_info
[afi
][safi
];
3732 t
= gr_info
->t_select_deferral
;
3734 void *info
= THREAD_ARG(t
);
3736 XFREE(MTYPE_TMP
, info
);
3738 THREAD_OFF(gr_info
->t_select_deferral
);
3740 t
= gr_info
->t_route_select
;
3742 void *info
= THREAD_ARG(t
);
3744 XFREE(MTYPE_TMP
, info
);
3746 THREAD_OFF(gr_info
->t_route_select
);
3749 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3750 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3751 zlog_debug("Deleting Default VRF");
3753 zlog_debug("Deleting %s %s",
3754 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3760 /* unmap from RT list */
3761 bgp_evpn_vrf_delete(bgp
);
3763 /* unmap bgp vrf label */
3764 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3765 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3768 if (bgp
->t_rmap_def_originate_eval
) {
3769 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3770 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3774 /* Inform peers we're going down. */
3775 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3776 peer_notify_shutdown(peer
);
3778 /* Delete static routes (networks). */
3779 bgp_static_delete(bgp
);
3781 /* Unset redistribution. */
3782 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3783 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3784 if (i
!= ZEBRA_ROUTE_BGP
)
3785 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3787 /* Free peers and peer-groups. */
3788 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3789 peer_group_delete(group
);
3791 while (listcount(bgp
->peer
)) {
3792 peer
= listnode_head(bgp
->peer
);
3796 if (bgp
->peer_self
) {
3797 peer_delete(bgp
->peer_self
);
3798 bgp
->peer_self
= NULL
;
3801 update_bgp_group_free(bgp
);
3803 /* TODO - Other memory may need to be freed - e.g., NHT */
3805 #ifdef ENABLE_BGP_VNC
3808 bgp_cleanup_routes(bgp
);
3810 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3811 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3814 &bgp
->vpn_policy
[afi
]
3815 .import_redirect_rtlist
);
3816 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3819 /* Free any memory allocated to holding routemap references */
3820 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3821 for (enum vpn_policy_direction dir
= 0;
3822 dir
< BGP_VPN_POLICY_DIR_MAX
; ++dir
) {
3823 if (bgp
->vpn_policy
[afi
].rmap_name
[dir
])
3824 XFREE(MTYPE_ROUTE_MAP_NAME
,
3825 bgp
->vpn_policy
[afi
].rmap_name
[dir
]);
3826 bgp
->vpn_policy
[afi
].rmap
[dir
] = NULL
;
3830 /* Deregister from Zebra, if needed */
3831 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3832 if (BGP_DEBUG(zebra
, ZEBRA
))
3834 "%s: deregistering this bgp %s instance from zebra",
3835 __func__
, bgp
->name
);
3836 bgp_zebra_instance_deregister(bgp
);
3839 /* Remove visibility via the master list - there may however still be
3840 * routes to be processed still referencing the struct bgp.
3842 listnode_delete(bm
->bgp
, bgp
);
3844 /* Free interfaces in this instance. */
3847 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3848 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3850 bgp_vrf_unlink(bgp
, vrf
);
3852 /* Update EVPN VRF pointer */
3853 if (bm
->bgp_evpn
== bgp
) {
3854 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3857 bgp_set_evpn(bgp_get_default());
3860 if (bgp
->process_queue
)
3861 work_queue_free_and_null(&bgp
->process_queue
);
3863 thread_master_free_unused(bm
->master
);
3864 bgp_unlock(bgp
); /* initial reference */
3869 void bgp_free(struct bgp
*bgp
)
3873 struct bgp_table
*table
;
3874 struct bgp_dest
*dest
;
3875 struct bgp_rmap
*rmap
;
3879 list_delete(&bgp
->group
);
3880 list_delete(&bgp
->peer
);
3882 if (bgp
->peerhash
) {
3883 hash_free(bgp
->peerhash
);
3884 bgp
->peerhash
= NULL
;
3887 FOREACH_AFI_SAFI (afi
, safi
) {
3888 /* Special handling for 2-level routing tables. */
3889 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3890 || safi
== SAFI_EVPN
) {
3891 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3892 dest
= bgp_route_next(dest
)) {
3893 table
= bgp_dest_get_bgp_table_info(dest
);
3894 bgp_table_finish(&table
);
3897 if (bgp
->route
[afi
][safi
])
3898 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3899 if (bgp
->aggregate
[afi
][safi
])
3900 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3901 if (bgp
->rib
[afi
][safi
])
3902 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3903 rmap
= &bgp
->table_map
[afi
][safi
];
3904 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3907 bgp_scan_finish(bgp
);
3908 bgp_address_destroy(bgp
);
3909 bgp_tip_hash_destroy(bgp
);
3911 /* release the auto RD id */
3912 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3914 bgp_evpn_cleanup(bgp
);
3915 bgp_pbr_cleanup(bgp
);
3916 bgp_srv6_cleanup(bgp
);
3917 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3919 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3920 enum vpn_policy_direction dir
;
3922 if (bgp
->vpn_policy
[afi
].import_vrf
)
3923 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3924 if (bgp
->vpn_policy
[afi
].export_vrf
)
3925 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3927 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3928 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3929 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3930 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3931 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3932 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3935 bgp_orr_cleanup(bgp
);
3937 XFREE(MTYPE_BGP
, bgp
->name
);
3938 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3939 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3941 XFREE(MTYPE_BGP
, bgp
);
3944 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3947 struct listnode
*node
, *nnode
;
3953 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3954 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3955 && !CHECK_FLAG(peer
->sflags
,
3956 PEER_STATUS_ACCEPT_PEER
))
3958 } else if (bm
->bgp
!= NULL
) {
3959 struct listnode
*bgpnode
, *nbgpnode
;
3961 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3962 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3964 && !strcmp(peer
->conf_if
, conf_if
)
3965 && !CHECK_FLAG(peer
->sflags
,
3966 PEER_STATUS_ACCEPT_PEER
))
3972 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3975 struct listnode
*node
, *nnode
;
3981 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3982 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3983 && !CHECK_FLAG(peer
->sflags
,
3984 PEER_STATUS_ACCEPT_PEER
))
3986 } else if (bm
->bgp
!= NULL
) {
3987 struct listnode
*bgpnode
, *nbgpnode
;
3989 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3990 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3992 && !strcmp(peer
->hostname
, hostname
)
3993 && !CHECK_FLAG(peer
->sflags
,
3994 PEER_STATUS_ACCEPT_PEER
))
4000 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
4002 struct peer
*peer
= NULL
;
4003 struct peer tmp_peer
;
4005 memset(&tmp_peer
, 0, sizeof(struct peer
));
4008 * We do not want to find the doppelganger peer so search for the peer
4010 * the hash that has PEER_FLAG_CONFIG_NODE
4012 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
4017 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4018 } else if (bm
->bgp
!= NULL
) {
4019 struct listnode
*bgpnode
, *nbgpnode
;
4021 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
4022 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4031 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
4032 union sockunion
*su
,
4033 struct peer_group
*group
)
4039 /* Create peer first; we've already checked group config is valid. */
4040 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
4041 group
->conf
->as_type
, group
, true);
4046 peer
= peer_lock(peer
);
4047 listnode_add(group
->peer
, peer
);
4049 peer_group2peer_config_copy(group
, peer
);
4052 * Bind peer for all AFs configured for the group. We don't call
4053 * peer_group_bind as that is sub-optimal and does some stuff we don't
4056 FOREACH_AFI_SAFI (afi
, safi
) {
4057 if (!group
->conf
->afc
[afi
][safi
])
4059 peer
->afc
[afi
][safi
] = 1;
4061 if (!peer_af_find(peer
, afi
, safi
))
4062 peer_af_create(peer
, afi
, safi
);
4064 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
4067 /* Mark as dynamic, but also as a "config node" for other things to
4069 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
4075 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
4076 struct prefix
*prefix
)
4078 struct listnode
*node
, *nnode
;
4079 struct prefix
*range
;
4082 afi
= family2afi(prefix
->family
);
4084 if (group
->listen_range
[afi
])
4085 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
4087 if (prefix_match(range
, prefix
))
4094 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
4095 struct prefix
**listen_range
)
4097 struct prefix
*range
= NULL
;
4098 struct peer_group
*group
= NULL
;
4099 struct listnode
*node
, *nnode
;
4101 *listen_range
= NULL
;
4103 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4104 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4107 } else if (bm
->bgp
!= NULL
) {
4108 struct listnode
*bgpnode
, *nbgpnode
;
4110 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4111 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4112 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4118 *listen_range
= range
;
4119 return (group
&& range
) ? group
: NULL
;
4122 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
4124 struct peer_group
*group
;
4127 struct prefix prefix
;
4128 struct prefix
*listen_range
;
4131 if (!sockunion2hostprefix(su
, &prefix
))
4134 /* See if incoming connection matches a configured listen range. */
4135 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
4146 if (bgp_debug_neighbor_events(NULL
))
4148 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4149 &prefix
, group
->name
, listen_range
);
4151 /* Are we within the listen limit? */
4152 dncount
= gbgp
->dynamic_neighbors_count
;
4154 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4155 if (bgp_debug_neighbor_events(NULL
))
4157 "Dynamic Neighbor %pFX rejected - at limit %d",
4158 &prefix
, gbgp
->dynamic_neighbors_limit
);
4162 /* Ensure group is not disabled. */
4163 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4164 if (bgp_debug_neighbor_events(NULL
))
4166 "Dynamic Neighbor %pFX rejected - group %s disabled",
4167 &prefix
, group
->name
);
4171 /* Check that at least one AF is activated for the group. */
4172 if (!peer_group_af_configured(group
)) {
4173 if (bgp_debug_neighbor_events(NULL
))
4175 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4176 &prefix
, group
->name
);
4180 /* Create dynamic peer and bind to associated group. */
4181 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4184 gbgp
->dynamic_neighbors_count
= ++dncount
;
4186 if (bgp_debug_neighbor_events(peer
))
4187 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4188 peer
->host
, group
->name
, dncount
);
4193 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4196 if (peer
->group
->bgp
) {
4197 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4199 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4201 if (bgp_debug_neighbor_events(peer
))
4202 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4203 peer
->group
->name
, dncount
);
4206 /* If peer is configured at least one address family return 1. */
4207 bool peer_active(struct peer
*peer
)
4209 if (BGP_PEER_SU_UNSPEC(peer
))
4211 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4212 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4213 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4214 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4215 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4216 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4217 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4218 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4219 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4220 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4221 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4226 /* If peer is negotiated at least one address family return 1. */
4227 bool peer_active_nego(struct peer
*peer
)
4229 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4230 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4231 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4232 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4233 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4234 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4235 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4236 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4237 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4238 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4239 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4240 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4241 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4246 /* If peer received at least one address family MP, return true */
4247 bool peer_afc_received(struct peer
*peer
)
4252 FOREACH_AFI_SAFI (afi
, safi
)
4253 if (peer
->afc_recv
[afi
][safi
])
4259 /* If peer advertised at least one address family MP, return true */
4260 bool peer_afc_advertised(struct peer
*peer
)
4265 FOREACH_AFI_SAFI (afi
, safi
)
4266 if (peer
->afc_adv
[afi
][safi
])
4272 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4273 enum peer_change_type type
)
4275 struct peer_af
*paf
;
4277 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4280 if (!peer_established(peer
))
4283 if (type
== peer_change_reset
) {
4284 /* If we're resetting session, we've to delete both peer struct
4286 if ((peer
->doppelganger
)
4287 && (peer
->doppelganger
->status
!= Deleted
)
4288 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4289 PEER_FLAG_CONFIG_NODE
)))
4290 peer_delete(peer
->doppelganger
);
4292 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4293 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4294 } else if (type
== peer_change_reset_in
) {
4295 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4296 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4297 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4298 BGP_ROUTE_REFRESH_NORMAL
);
4300 if ((peer
->doppelganger
)
4301 && (peer
->doppelganger
->status
!= Deleted
)
4302 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4303 PEER_FLAG_CONFIG_NODE
)))
4304 peer_delete(peer
->doppelganger
);
4306 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4307 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4309 } else if (type
== peer_change_reset_out
) {
4310 paf
= peer_af_find(peer
, afi
, safi
);
4311 if (paf
&& paf
->subgroup
)
4312 SET_FLAG(paf
->subgroup
->sflags
,
4313 SUBGRP_STATUS_FORCE_UPDATES
);
4315 update_group_adjust_peer(paf
);
4316 bgp_announce_route(peer
, afi
, safi
, false);
4320 struct peer_flag_action
{
4324 /* This flag can be set for peer-group member. */
4325 uint8_t not_for_member
;
4327 /* Action when the flag is changed. */
4328 enum peer_change_type type
;
4331 static const struct peer_flag_action peer_flag_action_list
[] = {
4332 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4333 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4334 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4335 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4336 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4337 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4338 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4339 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4340 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4341 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4342 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4343 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4344 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4345 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4346 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4347 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4348 {PEER_FLAG_LOCAL_AS
, 0, peer_change_reset
},
4349 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_reset
},
4350 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_reset
},
4351 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4352 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4353 {PEER_FLAG_EXTENDED_OPT_PARAMS
, 0, peer_change_reset
},
4354 {PEER_FLAG_ROLE_STRICT_MODE
, 0, peer_change_reset
},
4355 {PEER_FLAG_ROLE
, 0, peer_change_reset
},
4356 {PEER_FLAG_PORT
, 0, peer_change_reset
},
4357 {PEER_FLAG_AIGP
, 0, peer_change_none
},
4358 {PEER_FLAG_GRACEFUL_SHUTDOWN
, 0, peer_change_none
},
4361 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4362 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4363 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4364 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4365 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4366 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4367 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4368 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4369 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4370 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4371 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4372 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4373 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4374 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4375 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4376 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4377 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4378 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4379 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4380 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4381 {PEER_FLAG_MAX_PREFIX_OUT
, 0, peer_change_none
},
4382 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4383 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4384 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4385 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4386 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4387 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4388 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4389 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4390 {PEER_FLAG_SOO
, 0, peer_change_reset
},
4391 {PEER_FLAG_ACCEPT_OWN
, 0, peer_change_reset
},
4394 /* Proper action set. */
4395 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4396 int size
, struct peer_flag_action
*action
,
4403 const struct peer_flag_action
*match
= NULL
;
4405 /* Check peer's frag action. */
4406 for (i
= 0; i
< size
; i
++) {
4407 match
= &action_list
[i
];
4409 if (match
->flag
== 0)
4412 if (match
->flag
& flag
) {
4415 if (match
->type
== peer_change_reset_in
)
4417 if (match
->type
== peer_change_reset_out
)
4419 if (match
->type
== peer_change_reset
) {
4423 if (match
->not_for_member
)
4424 action
->not_for_member
= 1;
4428 /* Set peer clear type. */
4429 if (reset_in
&& reset_out
)
4430 action
->type
= peer_change_reset
;
4432 action
->type
= peer_change_reset_in
;
4434 action
->type
= peer_change_reset_out
;
4436 action
->type
= peer_change_none
;
4441 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4443 if (flag
== PEER_FLAG_SHUTDOWN
) {
4444 if (CHECK_FLAG(peer
->flags
, flag
)) {
4445 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4446 peer_nsf_stop(peer
);
4448 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4450 if (peer
->t_pmax_restart
) {
4451 THREAD_OFF(peer
->t_pmax_restart
);
4452 if (bgp_debug_neighbor_events(peer
))
4454 "%pBP Maximum-prefix restart timer canceled",
4458 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4459 char *msg
= peer
->tx_shutdown_message
;
4461 uint8_t msgbuf
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4463 if (!msg
&& peer_group_active(peer
))
4464 msg
= peer
->group
->conf
4465 ->tx_shutdown_message
;
4466 msglen
= msg
? strlen(msg
) : 0;
4467 if (msglen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4468 msglen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4472 memcpy(msgbuf
+ 1, msg
, msglen
);
4474 bgp_notify_send_with_data(
4475 peer
, BGP_NOTIFY_CEASE
,
4476 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4477 msgbuf
, msglen
+ 1);
4480 peer
, BGP_NOTIFY_CEASE
,
4481 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4483 bgp_session_reset(peer
);
4485 peer
->v_start
= BGP_INIT_START_TIMER
;
4486 BGP_EVENT_ADD(peer
, BGP_Stop
);
4488 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4489 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4490 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4491 else if (flag
== PEER_FLAG_PASSIVE
)
4492 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4493 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4494 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4496 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4497 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4499 bgp_session_reset(peer
);
4502 /* Enable global administrative shutdown of all peers of BGP instance */
4503 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4506 struct listnode
*node
;
4507 /* length(1) + message(N) */
4508 uint8_t data
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4510 /* do nothing if already shut down */
4511 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4514 /* informational log message */
4515 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4518 /* iterate through peers of BGP instance */
4519 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4520 /* continue, if peer is already in administrative shutdown. */
4521 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4524 /* send a RFC 4486 notification message if necessary */
4525 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4527 size_t datalen
= strlen(msg
);
4529 if (datalen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4530 datalen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4533 memcpy(data
+ 1, msg
, datalen
);
4535 bgp_notify_send_with_data(
4536 peer
, BGP_NOTIFY_CEASE
,
4537 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
, data
,
4541 peer
, BGP_NOTIFY_CEASE
,
4542 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4546 /* reset start timer to initial value */
4547 peer
->v_start
= BGP_INIT_START_TIMER
;
4549 /* trigger a RFC 4271 ManualStop event */
4550 BGP_EVENT_ADD(peer
, BGP_Stop
);
4553 /* set the BGP instances shutdown flag */
4554 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4557 /* Disable global administrative shutdown of all peers of BGP instance */
4558 void bgp_shutdown_disable(struct bgp
*bgp
)
4560 /* do nothing if not shut down. */
4561 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4564 /* informational log message */
4565 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4568 /* clear the BGP instances shutdown flag */
4569 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4572 /* Change specified peer flag. */
4573 static int peer_flag_modify(struct peer
*peer
, uint64_t flag
, int set
)
4577 bool invert
, member_invert
;
4578 struct peer
*member
;
4579 struct listnode
*node
, *nnode
;
4580 struct peer_flag_action action
;
4582 memset(&action
, 0, sizeof(struct peer_flag_action
));
4583 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4585 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4586 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4589 /* Abort if no flag action exists. */
4591 return BGP_ERR_INVALID_FLAG
;
4593 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4594 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4595 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4596 return BGP_ERR_PEER_FLAG_CONFLICT
;
4598 /* Handle flag updates where desired state matches current state. */
4599 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4600 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4601 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4605 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4606 COND_FLAG(peer
->flags_override
, flag
, invert
);
4611 /* Inherit from peer-group or set/unset flags accordingly. */
4612 if (peer_group_active(peer
) && set
== invert
)
4613 peer_flag_inherit(peer
, flag
);
4615 COND_FLAG(peer
->flags
, flag
, set
);
4617 /* Check if handling a regular peer. */
4618 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4619 /* Update flag override state accordingly. */
4620 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4623 * For the extended next-hop encoding flag we need to turn RAs
4624 * on if flag is being set, but only turn RAs off if the flag
4625 * is being unset on this peer and if this peer is a member of a
4626 * peer-group, the peer-group also doesn't have the flag set.
4628 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4630 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4631 } else if (peer_group_active(peer
)) {
4632 if (!CHECK_FLAG(peer
->group
->conf
->flags
,
4635 bgp_zebra_terminate_radv(peer
->bgp
,
4638 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4641 /* Execute flag action on peer. */
4642 if (action
.type
== peer_change_reset
)
4643 peer_flag_modify_action(peer
, flag
);
4645 /* Skip peer-group mechanics for regular peers. */
4650 * Update peer-group members, unless they are explicitly overriding
4651 * peer-group configuration.
4653 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4654 /* Skip peers with overridden configuration. */
4655 if (CHECK_FLAG(member
->flags_override
, flag
))
4658 /* Check if only member without group is inverted. */
4660 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4662 /* Skip peers with equivalent configuration. */
4663 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4666 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4669 /* Update flag on peer-group member. */
4670 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4672 if (flag
== PEER_FLAG_CAPABILITY_ENHE
&& !member
->conf_if
)
4673 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4674 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4676 /* Execute flag action on peer-group member. */
4677 if (action
.type
== peer_change_reset
)
4678 peer_flag_modify_action(member
, flag
);
4684 int peer_flag_set(struct peer
*peer
, uint64_t flag
)
4686 return peer_flag_modify(peer
, flag
, 1);
4689 int peer_flag_unset(struct peer
*peer
, uint64_t flag
)
4691 return peer_flag_modify(peer
, flag
, 0);
4694 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4695 uint64_t flag
, bool set
)
4699 bool invert
, member_invert
;
4700 struct peer
*member
;
4701 struct listnode
*node
, *nnode
;
4702 struct peer_flag_action action
;
4703 enum bgp_peer_sort ptype
;
4705 memset(&action
, 0, sizeof(struct peer_flag_action
));
4706 size
= sizeof(peer_af_flag_action_list
)
4707 / sizeof(struct peer_flag_action
);
4709 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4710 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4713 /* Abort if flag action exists. */
4715 return BGP_ERR_INVALID_FLAG
;
4717 ptype
= peer_sort(peer
);
4718 /* Special check for reflector client. */
4719 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4720 return BGP_ERR_NOT_INTERNAL_PEER
;
4722 /* Do not remove reflector client when ORR is configured on this peer */
4723 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& !set
&&
4724 peer_orr_rrclient_check(peer
, afi
, safi
))
4725 return BGP_ERR_PEER_ORR_CONFIGURED
;
4727 /* Special check for remove-private-AS. */
4728 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4729 return BGP_ERR_REMOVE_PRIVATE_AS
;
4731 /* as-override is not allowed for IBGP peers */
4732 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4733 return BGP_ERR_AS_OVERRIDE
;
4735 /* Handle flag updates where desired state matches current state. */
4736 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4737 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4738 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4743 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4744 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4751 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4752 * if we are setting/unsetting flags which conflict with this flag
4753 * handle accordingly
4755 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4759 * if we are setting NEXTHOP_SELF, we need to unset the
4760 * NEXTHOP_UNCHANGED flag
4762 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4763 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4764 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4765 PEER_FLAG_NEXTHOP_UNCHANGED
);
4769 * if we are unsetting NEXTHOP_SELF, we need to set the
4770 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4772 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4773 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4774 SET_FLAG(peer
->af_flags
[afi
][safi
],
4775 PEER_FLAG_NEXTHOP_UNCHANGED
);
4780 * If the peer is a route server client let's not
4781 * muck with the nexthop on the way out the door
4783 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4785 SET_FLAG(peer
->af_flags
[afi
][safi
],
4786 PEER_FLAG_NEXTHOP_UNCHANGED
);
4788 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4789 PEER_FLAG_NEXTHOP_UNCHANGED
);
4792 /* Inherit from peer-group or set/unset flags accordingly. */
4793 if (peer_group_active(peer
) && set
== invert
)
4794 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4796 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4798 /* Execute action when peer is established. */
4799 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4800 && peer_established(peer
)) {
4801 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4802 bgp_clear_adj_in(peer
, afi
, safi
);
4804 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4805 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4806 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4807 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4808 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4809 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4810 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4811 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4813 peer_change_action(peer
, afi
, safi
, action
.type
);
4817 /* Check if handling a regular peer. */
4818 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4819 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4823 * Update peer-group members, unless they are explicitly
4824 * overriding peer-group configuration.
4826 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4828 /* Skip peers with overridden configuration. */
4829 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4833 /* Check if only member without group is inverted. */
4835 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4839 /* Skip peers with equivalent configuration. */
4840 if (set
!= member_invert
4841 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4844 if (set
== member_invert
4845 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4848 /* Update flag on peer-group member. */
4849 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4850 set
!= member_invert
);
4852 /* Execute flag action on peer-group member. */
4853 if (peer_established(member
)) {
4854 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4855 bgp_clear_adj_in(member
, afi
, safi
);
4857 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4858 member
->last_reset
=
4859 PEER_DOWN_RR_CLIENT_CHANGE
;
4861 == PEER_FLAG_RSERVER_CLIENT
)
4862 member
->last_reset
=
4863 PEER_DOWN_RS_CLIENT_CHANGE
;
4865 == PEER_FLAG_ORF_PREFIX_SM
)
4866 member
->last_reset
=
4867 PEER_DOWN_CAPABILITY_CHANGE
;
4869 == PEER_FLAG_ORF_PREFIX_RM
)
4870 member
->last_reset
=
4871 PEER_DOWN_CAPABILITY_CHANGE
;
4873 peer_change_action(member
, afi
, safi
,
4883 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4885 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4888 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4890 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4894 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4896 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4897 peer
->tx_shutdown_message
=
4898 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4901 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4903 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4907 /* EBGP multihop configuration. */
4908 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4910 struct peer_group
*group
;
4911 struct listnode
*node
, *nnode
;
4914 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4917 /* is there anything to do? */
4918 if (peer
->ttl
== ttl
)
4921 /* see comment in peer_ttl_security_hops_set() */
4922 if (ttl
!= MAXTTL
) {
4923 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4924 group
= peer
->group
;
4925 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4926 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4928 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4930 if (peer1
->sort
== BGP_PEER_IBGP
)
4933 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4934 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4937 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4938 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4944 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4945 if (peer
->sort
!= BGP_PEER_IBGP
) {
4946 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4947 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4948 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4950 bgp_session_reset(peer
);
4952 /* Reconfigure BFD peer with new TTL. */
4953 if (peer
->bfd_config
)
4954 bgp_peer_bfd_update_source(peer
);
4957 group
= peer
->group
;
4958 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4959 if (peer
->sort
== BGP_PEER_IBGP
)
4962 peer
->ttl
= group
->conf
->ttl
;
4964 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4965 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4966 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4968 bgp_session_reset(peer
);
4970 /* Reconfigure BFD peer with new TTL. */
4971 if (peer
->bfd_config
)
4972 bgp_peer_bfd_update_source(peer
);
4978 int peer_ebgp_multihop_unset(struct peer
*peer
)
4980 struct peer_group
*group
;
4981 struct listnode
*node
, *nnode
;
4984 if (peer
->sort
== BGP_PEER_IBGP
)
4987 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
4988 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4990 if (peer_group_active(peer
))
4991 ttl
= peer
->group
->conf
->ttl
;
4993 ttl
= BGP_DEFAULT_TTL
;
4995 if (ttl
== peer
->ttl
)
5000 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5001 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5002 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5003 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5005 bgp_session_reset(peer
);
5007 /* Reconfigure BFD peer with new TTL. */
5008 if (peer
->bfd_config
)
5009 bgp_peer_bfd_update_source(peer
);
5011 group
= peer
->group
;
5012 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5013 if (peer
->sort
== BGP_PEER_IBGP
)
5016 peer
->ttl
= BGP_DEFAULT_TTL
;
5018 if (peer
->fd
>= 0) {
5019 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5021 peer
, BGP_NOTIFY_CEASE
,
5022 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5024 bgp_session_reset(peer
);
5027 /* Reconfigure BFD peer with new TTL. */
5028 if (peer
->bfd_config
)
5029 bgp_peer_bfd_update_source(peer
);
5035 /* Set Open Policy Role and check its correctness */
5036 int peer_role_set(struct peer
*peer
, uint8_t role
, bool strict_mode
)
5038 struct peer
*member
;
5039 struct listnode
*node
, *nnode
;
5041 peer_flag_set(peer
, PEER_FLAG_ROLE
);
5043 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5044 if (peer
->sort
!= BGP_PEER_EBGP
)
5045 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5047 if (peer
->local_role
== role
) {
5048 if (CHECK_FLAG(peer
->flags
,
5049 PEER_FLAG_ROLE_STRICT_MODE
) &&
5051 /* TODO: Is session restart needed if it was
5054 UNSET_FLAG(peer
->flags
,
5055 PEER_FLAG_ROLE_STRICT_MODE
);
5056 if (!CHECK_FLAG(peer
->flags
,
5057 PEER_FLAG_ROLE_STRICT_MODE
) &&
5059 SET_FLAG(peer
->flags
,
5060 PEER_FLAG_ROLE_STRICT_MODE
);
5061 /* Restart session to throw Role Mismatch
5064 if (peer
->remote_role
== ROLE_UNDEFINED
)
5065 bgp_session_reset(peer
);
5068 peer
->local_role
= role
;
5070 SET_FLAG(peer
->flags
,
5071 PEER_FLAG_ROLE_STRICT_MODE
);
5073 UNSET_FLAG(peer
->flags
,
5074 PEER_FLAG_ROLE_STRICT_MODE
);
5075 bgp_session_reset(peer
);
5081 peer
->local_role
= role
;
5082 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5083 if (member
->sort
!= BGP_PEER_EBGP
)
5084 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5086 if (member
->local_role
== role
) {
5087 if (CHECK_FLAG(member
->flags
,
5088 PEER_FLAG_ROLE_STRICT_MODE
) &&
5090 /* TODO: Is session restart needed if it was
5093 UNSET_FLAG(member
->flags
,
5094 PEER_FLAG_ROLE_STRICT_MODE
);
5095 if (!CHECK_FLAG(member
->flags
,
5096 PEER_FLAG_ROLE_STRICT_MODE
) &&
5098 SET_FLAG(peer
->flags
,
5099 PEER_FLAG_ROLE_STRICT_MODE
);
5100 SET_FLAG(member
->flags
,
5101 PEER_FLAG_ROLE_STRICT_MODE
);
5102 /* Restart session to throw Role Mismatch
5105 if (member
->remote_role
== ROLE_UNDEFINED
)
5106 bgp_session_reset(member
);
5109 member
->local_role
= role
;
5112 SET_FLAG(peer
->flags
,
5113 PEER_FLAG_ROLE_STRICT_MODE
);
5114 SET_FLAG(member
->flags
,
5115 PEER_FLAG_ROLE_STRICT_MODE
);
5117 UNSET_FLAG(member
->flags
,
5118 PEER_FLAG_ROLE_STRICT_MODE
);
5120 bgp_session_reset(member
);
5127 int peer_role_unset(struct peer
*peer
)
5129 struct peer
*member
;
5130 struct listnode
*node
, *nnode
;
5132 peer_flag_unset(peer
, PEER_FLAG_ROLE
);
5134 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5135 return peer_role_set(peer
, ROLE_UNDEFINED
, 0);
5137 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
))
5138 peer_role_set(member
, ROLE_UNDEFINED
, 0);
5143 /* Neighbor description. */
5144 void peer_description_set(struct peer
*peer
, const char *desc
)
5146 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5148 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
5151 void peer_description_unset(struct peer
*peer
)
5153 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5156 /* Neighbor update-source. */
5157 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
5159 struct peer
*member
;
5160 struct listnode
*node
, *nnode
;
5162 /* Set flag and configuration on peer. */
5163 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5164 if (peer
->update_if
) {
5165 if (strcmp(peer
->update_if
, ifname
) == 0)
5167 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5169 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5170 sockunion_free(peer
->update_source
);
5171 peer
->update_source
= NULL
;
5173 /* Check if handling a regular peer. */
5174 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5175 /* Send notification or reset peer depending on state. */
5176 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5177 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5178 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5179 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5181 bgp_session_reset(peer
);
5183 /* Apply new source configuration to BFD session. */
5184 if (peer
->bfd_config
)
5185 bgp_peer_bfd_update_source(peer
);
5187 /* Skip peer-group mechanics for regular peers. */
5192 * Set flag and configuration on all peer-group members, unless they are
5193 * explicitly overriding peer-group configuration.
5195 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5196 /* Skip peers with overridden configuration. */
5197 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5200 /* Skip peers with the same configuration. */
5201 if (member
->update_if
) {
5202 if (strcmp(member
->update_if
, ifname
) == 0)
5204 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5207 /* Set flag and configuration on peer-group member. */
5208 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5209 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5210 sockunion_free(member
->update_source
);
5211 member
->update_source
= NULL
;
5213 /* Send notification or reset peer depending on state. */
5214 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5215 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5216 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5217 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5219 bgp_session_reset(member
);
5221 /* Apply new source configuration to BFD session. */
5222 if (member
->bfd_config
)
5223 bgp_peer_bfd_update_source(member
);
5229 void peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
5231 struct peer
*member
;
5232 struct listnode
*node
, *nnode
;
5234 /* Set flag and configuration on peer. */
5235 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5236 if (peer
->update_source
) {
5237 if (sockunion_cmp(peer
->update_source
, su
) == 0)
5239 sockunion_free(peer
->update_source
);
5241 peer
->update_source
= sockunion_dup(su
);
5242 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5244 /* Check if handling a regular peer. */
5245 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5246 /* Send notification or reset peer depending on state. */
5247 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5248 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5249 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5250 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5252 bgp_session_reset(peer
);
5254 /* Apply new source configuration to BFD session. */
5255 if (peer
->bfd_config
)
5256 bgp_peer_bfd_update_source(peer
);
5258 /* Skip peer-group mechanics for regular peers. */
5263 * Set flag and configuration on all peer-group members, unless they are
5264 * explicitly overriding peer-group configuration.
5266 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5267 /* Skip peers with overridden configuration. */
5268 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5271 /* Skip peers with the same configuration. */
5272 if (member
->update_source
) {
5273 if (sockunion_cmp(member
->update_source
, su
) == 0)
5275 sockunion_free(member
->update_source
);
5278 /* Set flag and configuration on peer-group member. */
5279 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5280 member
->update_source
= sockunion_dup(su
);
5281 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5283 /* Send notification or reset peer depending on state. */
5284 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5285 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5286 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5287 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5289 bgp_session_reset(member
);
5291 /* Apply new source configuration to BFD session. */
5292 if (member
->bfd_config
)
5293 bgp_peer_bfd_update_source(member
);
5297 void peer_update_source_unset(struct peer
*peer
)
5299 struct peer
*member
;
5300 struct listnode
*node
, *nnode
;
5302 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
5305 /* Inherit configuration from peer-group if peer is member. */
5306 if (peer_group_active(peer
)) {
5307 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
5308 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
5309 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
5310 MTYPE_PEER_UPDATE_SOURCE
);
5312 /* Otherwise remove flag and configuration from peer. */
5313 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
5314 sockunion_free(peer
->update_source
);
5315 peer
->update_source
= NULL
;
5316 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5319 /* Check if handling a regular peer. */
5320 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5321 /* Send notification or reset peer depending on state. */
5322 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5323 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5324 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5325 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5327 bgp_session_reset(peer
);
5329 /* Apply new source configuration to BFD session. */
5330 if (peer
->bfd_config
)
5331 bgp_peer_bfd_update_source(peer
);
5333 /* Skip peer-group mechanics for regular peers. */
5338 * Set flag and configuration on all peer-group members, unless they are
5339 * explicitly overriding peer-group configuration.
5341 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5342 /* Skip peers with overridden configuration. */
5343 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5346 /* Skip peers with the same configuration. */
5347 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5348 && !member
->update_source
&& !member
->update_if
)
5351 /* Remove flag and configuration on peer-group member. */
5352 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5353 sockunion_free(member
->update_source
);
5354 member
->update_source
= NULL
;
5355 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5357 /* Send notification or reset peer depending on state. */
5358 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5359 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5360 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5361 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5363 bgp_session_reset(member
);
5365 /* Apply new source configuration to BFD session. */
5366 if (member
->bfd_config
)
5367 bgp_peer_bfd_update_source(member
);
5371 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5372 const char *rmap
, struct route_map
*route_map
)
5374 struct peer
*member
;
5375 struct listnode
*node
, *nnode
;
5376 struct update_subgroup
*subgrp
;
5378 /* Set flag and configuration on peer. */
5379 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5381 subgrp
= peer_subgroup(peer
, afi
, safi
);
5384 if (!peer
->default_rmap
[afi
][safi
].name
5385 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5386 if (peer
->default_rmap
[afi
][safi
].name
)
5387 XFREE(MTYPE_ROUTE_MAP_NAME
,
5388 peer
->default_rmap
[afi
][safi
].name
);
5391 * When there is a change in route-map policy,
5392 * this flow gets triggered. Since, the default
5393 * route is already originated, the flag is set.
5394 * The flag should be unset here,
5395 * to trigger the flow of sending update message.
5398 UNSET_FLAG(subgrp
->sflags
,
5399 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5401 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5402 peer
->default_rmap
[afi
][safi
].name
=
5403 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5404 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5405 route_map_counter_increment(route_map
);
5408 if (peer
->default_rmap
[afi
][safi
].name
)
5409 XFREE(MTYPE_ROUTE_MAP_NAME
,
5410 peer
->default_rmap
[afi
][safi
].name
);
5413 * This is triggered in case of route-map deletion.
5414 * The flag needs to be unset, to trigger the flow
5415 * of sending an update message.
5418 UNSET_FLAG(subgrp
->sflags
,
5419 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5421 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5422 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5423 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5426 /* Check if handling a regular peer. */
5427 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5428 /* Update peer route announcements. */
5429 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5430 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5431 bgp_default_originate(peer
, afi
, safi
, 0);
5432 bgp_announce_route(peer
, afi
, safi
, false);
5435 /* Skip peer-group mechanics for regular peers. */
5440 * Set flag and configuration on all peer-group members, unless they are
5441 * explicitly overriding peer-group configuration.
5443 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5444 /* Skip peers with overridden configuration. */
5445 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5446 PEER_FLAG_DEFAULT_ORIGINATE
))
5449 /* Set flag and configuration on peer-group member. */
5450 SET_FLAG(member
->af_flags
[afi
][safi
],
5451 PEER_FLAG_DEFAULT_ORIGINATE
);
5453 if (member
->default_rmap
[afi
][safi
].name
)
5454 XFREE(MTYPE_ROUTE_MAP_NAME
,
5455 member
->default_rmap
[afi
][safi
].name
);
5456 route_map_counter_decrement(
5457 member
->default_rmap
[afi
][safi
].map
);
5458 member
->default_rmap
[afi
][safi
].name
=
5459 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5460 member
->default_rmap
[afi
][safi
].map
= route_map
;
5461 route_map_counter_increment(route_map
);
5464 /* Update peer route announcements. */
5465 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5466 update_group_adjust_peer(
5467 peer_af_find(member
, afi
, safi
));
5468 bgp_default_originate(member
, afi
, safi
, 0);
5469 bgp_announce_route(member
, afi
, safi
, false);
5476 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5478 struct peer
*member
;
5479 struct listnode
*node
, *nnode
;
5481 /* Inherit configuration from peer-group if peer is member. */
5482 if (peer_group_active(peer
)) {
5483 peer_af_flag_inherit(peer
, afi
, safi
,
5484 PEER_FLAG_DEFAULT_ORIGINATE
);
5485 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5486 default_rmap
[afi
][safi
].name
,
5487 MTYPE_ROUTE_MAP_NAME
);
5488 PEER_ATTR_INHERIT(peer
, peer
->group
,
5489 default_rmap
[afi
][safi
].map
);
5491 /* Otherwise remove flag and configuration from peer. */
5492 peer_af_flag_unset(peer
, afi
, safi
,
5493 PEER_FLAG_DEFAULT_ORIGINATE
);
5494 if (peer
->default_rmap
[afi
][safi
].name
)
5495 XFREE(MTYPE_ROUTE_MAP_NAME
,
5496 peer
->default_rmap
[afi
][safi
].name
);
5497 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5498 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5499 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5502 /* Check if handling a regular peer. */
5503 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5504 /* Update peer route announcements. */
5505 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5506 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5507 bgp_default_originate(peer
, afi
, safi
, 1);
5508 bgp_announce_route(peer
, afi
, safi
, false);
5511 /* Skip peer-group mechanics for regular peers. */
5516 * Remove flag and configuration from all peer-group members, unless
5517 * they are explicitly overriding peer-group configuration.
5519 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5520 /* Skip peers with overridden configuration. */
5521 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5522 PEER_FLAG_DEFAULT_ORIGINATE
))
5525 /* Remove flag and configuration on peer-group member. */
5526 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5527 PEER_FLAG_DEFAULT_ORIGINATE
);
5528 if (member
->default_rmap
[afi
][safi
].name
)
5529 XFREE(MTYPE_ROUTE_MAP_NAME
,
5530 member
->default_rmap
[afi
][safi
].name
);
5531 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5532 member
->default_rmap
[afi
][safi
].name
= NULL
;
5533 member
->default_rmap
[afi
][safi
].map
= NULL
;
5535 /* Update peer route announcements. */
5536 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5537 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5538 bgp_default_originate(member
, afi
, safi
, 1);
5539 bgp_announce_route(member
, afi
, safi
, false);
5546 void peer_port_set(struct peer
*peer
, uint16_t port
)
5549 peer_flag_set(peer
, PEER_FLAG_PORT
);
5552 void peer_port_unset(struct peer
*peer
)
5554 peer
->port
= BGP_PORT_DEFAULT
;
5555 peer_flag_unset(peer
, PEER_FLAG_PORT
);
5558 /* Set the TCP-MSS value in the peer structure,
5559 * This gets applied only after connection reset
5560 * So this value will be used in bgp_connect.
5562 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5564 peer
->tcp_mss
= tcp_mss
;
5565 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5568 /* Reset the TCP-MSS value in the peer structure,
5569 * This gets applied only after connection reset
5570 * So this value will be used in bgp_connect.
5572 void peer_tcp_mss_unset(struct peer
*peer
)
5574 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5579 * Helper function that is called after the name of the policy
5580 * being used by a peer has changed (AF specific). Automatically
5581 * initiates inbound or outbound processing as needed.
5583 void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5587 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5588 if (peer_established(peer
))
5589 bgp_announce_route(peer
, afi
, safi
, false);
5591 if (!peer_established(peer
))
5594 if (bgp_soft_reconfig_in(peer
, afi
, safi
))
5597 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
) ||
5598 CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
)) {
5599 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5600 PEER_CAP_ORF_PREFIX_SM_ADV
) &&
5601 (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5602 PEER_CAP_ORF_PREFIX_RM_RCV
) ||
5603 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5604 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
)))
5605 peer_clear_soft(peer
, afi
, safi
,
5606 BGP_CLEAR_SOFT_IN_ORF_PREFIX
);
5608 bgp_route_refresh_send(
5609 peer
, afi
, safi
, 0, 0, 0,
5610 BGP_ROUTE_REFRESH_NORMAL
);
5616 /* neighbor weight. */
5617 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5619 struct peer
*member
;
5620 struct listnode
*node
, *nnode
;
5622 /* Set flag and configuration on peer. */
5623 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5624 if (peer
->weight
[afi
][safi
] != weight
) {
5625 peer
->weight
[afi
][safi
] = weight
;
5626 peer_on_policy_change(peer
, afi
, safi
, 0);
5629 /* Skip peer-group mechanics for regular peers. */
5630 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5634 * Set flag and configuration on all peer-group members, unless they are
5635 * explicitly overriding peer-group configuration.
5637 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5638 /* Skip peers with overridden configuration. */
5639 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5643 /* Set flag and configuration on peer-group member. */
5644 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5645 if (member
->weight
[afi
][safi
] != weight
) {
5646 member
->weight
[afi
][safi
] = weight
;
5647 peer_on_policy_change(member
, afi
, safi
, 0);
5654 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5656 struct peer
*member
;
5657 struct listnode
*node
, *nnode
;
5659 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5662 /* Inherit configuration from peer-group if peer is member. */
5663 if (peer_group_active(peer
)) {
5664 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5665 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5667 peer_on_policy_change(peer
, afi
, safi
, 0);
5671 /* Remove flag and configuration from peer. */
5672 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5673 peer
->weight
[afi
][safi
] = 0;
5674 peer_on_policy_change(peer
, afi
, safi
, 0);
5676 /* Skip peer-group mechanics for regular peers. */
5677 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5681 * Remove flag and configuration from all peer-group members, unless
5682 * they are explicitly overriding peer-group configuration.
5684 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5685 /* Skip peers with overridden configuration. */
5686 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5690 /* Skip peers where flag is already disabled. */
5691 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5694 /* Remove flag and configuration on peer-group member. */
5695 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5696 member
->weight
[afi
][safi
] = 0;
5697 peer_on_policy_change(member
, afi
, safi
, 0);
5703 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5705 struct peer
*member
;
5706 struct listnode
*node
, *nnode
;
5708 if (keepalive
> UINT16_MAX
)
5709 return BGP_ERR_INVALID_VALUE
;
5711 if (holdtime
> UINT16_MAX
)
5712 return BGP_ERR_INVALID_VALUE
;
5714 if (holdtime
< 3 && holdtime
!= 0)
5715 return BGP_ERR_INVALID_VALUE
;
5717 /* Set flag and configuration on peer. */
5718 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5719 peer
->holdtime
= holdtime
;
5720 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5722 /* Skip peer-group mechanics for regular peers. */
5723 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5727 * Set flag and configuration on all peer-group members, unless they are
5728 * explicitly overriding peer-group configuration.
5730 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5731 /* Skip peers with overridden configuration. */
5732 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5735 /* Set flag and configuration on peer-group member. */
5736 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5737 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5738 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5744 int peer_timers_unset(struct peer
*peer
)
5746 struct peer
*member
;
5747 struct listnode
*node
, *nnode
;
5749 /* Inherit configuration from peer-group if peer is member. */
5750 if (peer_group_active(peer
)) {
5751 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5752 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5753 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5755 /* Otherwise remove flag and configuration from peer. */
5756 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5758 peer
->keepalive
= 0;
5761 /* Skip peer-group mechanics for regular peers. */
5762 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5766 * Remove flag and configuration from all peer-group members, unless
5767 * they are explicitly overriding peer-group configuration.
5769 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5770 /* Skip peers with overridden configuration. */
5771 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5774 /* Remove flag and configuration on peer-group member. */
5775 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5776 member
->holdtime
= 0;
5777 member
->keepalive
= 0;
5783 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5785 struct peer
*member
;
5786 struct listnode
*node
, *nnode
;
5788 if (connect
> UINT16_MAX
)
5789 return BGP_ERR_INVALID_VALUE
;
5791 /* Set flag and configuration on peer. */
5792 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5793 peer
->connect
= connect
;
5794 peer
->v_connect
= connect
;
5796 /* Skip peer-group mechanics for regular peers. */
5797 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5798 if (!peer_established(peer
)) {
5799 if (peer_active(peer
))
5800 BGP_EVENT_ADD(peer
, BGP_Stop
);
5801 BGP_EVENT_ADD(peer
, BGP_Start
);
5806 * Set flag and configuration on all peer-group members, unless they are
5807 * explicitly overriding peer-group configuration.
5809 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5810 /* Skip peers with overridden configuration. */
5811 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5814 /* Set flag and configuration on peer-group member. */
5815 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5816 member
->connect
= connect
;
5817 member
->v_connect
= connect
;
5819 if (!peer_established(member
)) {
5820 if (peer_active(member
))
5821 BGP_EVENT_ADD(member
, BGP_Stop
);
5822 BGP_EVENT_ADD(member
, BGP_Start
);
5829 int peer_timers_connect_unset(struct peer
*peer
)
5831 struct peer
*member
;
5832 struct listnode
*node
, *nnode
;
5834 /* Inherit configuration from peer-group if peer is member. */
5835 if (peer_group_active(peer
)) {
5836 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5837 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5839 /* Otherwise remove flag and configuration from peer. */
5840 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5844 /* Set timer with fallback to default value. */
5846 peer
->v_connect
= peer
->connect
;
5848 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5850 /* Skip peer-group mechanics for regular peers. */
5851 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5852 if (!peer_established(peer
)) {
5853 if (peer_active(peer
))
5854 BGP_EVENT_ADD(peer
, BGP_Stop
);
5855 BGP_EVENT_ADD(peer
, BGP_Start
);
5860 * Remove flag and configuration from all peer-group members, unless
5861 * they are explicitly overriding peer-group configuration.
5863 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5864 /* Skip peers with overridden configuration. */
5865 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5868 /* Remove flag and configuration on peer-group member. */
5869 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5870 member
->connect
= 0;
5871 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5873 if (!peer_established(member
)) {
5874 if (peer_active(member
))
5875 BGP_EVENT_ADD(member
, BGP_Stop
);
5876 BGP_EVENT_ADD(member
, BGP_Start
);
5883 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5885 struct peer
*member
;
5886 struct listnode
*node
, *nnode
;
5889 return BGP_ERR_INVALID_VALUE
;
5891 /* Set flag and configuration on peer. */
5892 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5893 peer
->routeadv
= routeadv
;
5894 peer
->v_routeadv
= routeadv
;
5896 /* Check if handling a regular peer. */
5897 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5898 /* Update peer route announcements. */
5899 update_group_adjust_peer_afs(peer
);
5900 if (peer_established(peer
))
5901 bgp_announce_route_all(peer
);
5903 /* Skip peer-group mechanics for regular peers. */
5908 * Set flag and configuration on all peer-group members, unless they are
5909 * explicitly overriding peer-group configuration.
5911 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5912 /* Skip peers with overridden configuration. */
5913 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5916 /* Set flag and configuration on peer-group member. */
5917 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5918 member
->routeadv
= routeadv
;
5919 member
->v_routeadv
= routeadv
;
5921 /* Update peer route announcements. */
5922 update_group_adjust_peer_afs(member
);
5923 if (peer_established(member
))
5924 bgp_announce_route_all(member
);
5930 int peer_advertise_interval_unset(struct peer
*peer
)
5932 struct peer
*member
;
5933 struct listnode
*node
, *nnode
;
5935 /* Inherit configuration from peer-group if peer is member. */
5936 if (peer_group_active(peer
)) {
5937 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5938 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5940 /* Otherwise remove flag and configuration from peer. */
5941 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5945 /* Set timer with fallback to default value. */
5947 peer
->v_routeadv
= peer
->routeadv
;
5949 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5950 ? BGP_DEFAULT_IBGP_ROUTEADV
5951 : BGP_DEFAULT_EBGP_ROUTEADV
;
5953 /* Check if handling a regular peer. */
5954 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5955 /* Update peer route announcements. */
5956 update_group_adjust_peer_afs(peer
);
5957 if (peer_established(peer
))
5958 bgp_announce_route_all(peer
);
5960 /* Skip peer-group mechanics for regular peers. */
5965 * Remove flag and configuration from all peer-group members, unless
5966 * they are explicitly overriding peer-group configuration.
5968 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5969 /* Skip peers with overridden configuration. */
5970 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5973 /* Remove flag and configuration on peer-group member. */
5974 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5975 member
->routeadv
= 0;
5976 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
5977 ? BGP_DEFAULT_IBGP_ROUTEADV
5978 : BGP_DEFAULT_EBGP_ROUTEADV
;
5980 /* Update peer route announcements. */
5981 update_group_adjust_peer_afs(member
);
5982 if (peer_established(member
))
5983 bgp_announce_route_all(member
);
5989 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
5992 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
5994 struct peer
*member
;
5995 struct listnode
*node
;
5997 /* Set peers session attribute flag and timer interval. */
5998 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5999 peer
->delayopen
= delayopen
;
6000 peer
->v_delayopen
= delayopen
;
6002 /* Skip group mechanics for regular peers. */
6003 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6006 /* Set flag and configuration on all peer-group members, unless they are
6007 * explicitly overriding peer-group configuration.
6009 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6010 /* Skip peers with overridden configuration. */
6011 if (CHECK_FLAG(member
->flags_override
,
6012 PEER_FLAG_TIMER_DELAYOPEN
))
6015 /* Set session attribute flag and timer intervals on peer-group
6018 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6019 member
->delayopen
= delayopen
;
6020 member
->v_delayopen
= delayopen
;
6026 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
6027 * DelayOpenTimer interval to the default value.
6029 int peer_timers_delayopen_unset(struct peer
*peer
)
6031 struct peer
*member
;
6032 struct listnode
*node
;
6034 /* Inherit configuration from peer-group if peer is member. */
6035 if (peer_group_active(peer
)) {
6036 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6037 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
6039 /* Otherwise remove session attribute flag and set timer
6040 * interval to default value.
6042 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6043 peer
->delayopen
= peer
->bgp
->default_delayopen
;
6046 /* Set timer value to zero */
6047 peer
->v_delayopen
= 0;
6049 /* Skip peer-group mechanics for regular peers. */
6050 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6053 /* Remove flag and configuration from all peer-group members, unless
6054 * they are explicitly overriding peer-group configuration.
6056 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6057 /* Skip peers with overridden configuration. */
6058 if (CHECK_FLAG(member
->flags_override
,
6059 PEER_FLAG_TIMER_DELAYOPEN
))
6062 /* Remove session attribute flag, reset the timer interval to
6063 * the default value and set the timer value to zero.
6065 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6066 member
->delayopen
= peer
->bgp
->default_delayopen
;
6067 member
->v_delayopen
= 0;
6073 /* neighbor interface */
6074 void peer_interface_set(struct peer
*peer
, const char *str
)
6076 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6077 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
6080 void peer_interface_unset(struct peer
*peer
)
6082 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6086 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6087 int allow_num
, int origin
)
6089 struct peer
*member
;
6090 struct listnode
*node
, *nnode
;
6092 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
6093 return BGP_ERR_INVALID_VALUE
;
6095 /* Set flag and configuration on peer. */
6096 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6098 if (peer
->allowas_in
[afi
][safi
] != 0
6099 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6100 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6101 peer_af_flag_set(peer
, afi
, safi
,
6102 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6103 peer
->allowas_in
[afi
][safi
] = 0;
6104 peer_on_policy_change(peer
, afi
, safi
, 0);
6107 if (peer
->allowas_in
[afi
][safi
] != allow_num
6108 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6109 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6111 peer_af_flag_unset(peer
, afi
, safi
,
6112 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6113 peer
->allowas_in
[afi
][safi
] = allow_num
;
6114 peer_on_policy_change(peer
, afi
, safi
, 0);
6118 /* Skip peer-group mechanics for regular peers. */
6119 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6123 * Set flag and configuration on all peer-group members, unless
6124 * they are explicitly overriding peer-group configuration.
6126 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6127 /* Skip peers with overridden configuration. */
6128 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6129 PEER_FLAG_ALLOWAS_IN
))
6132 /* Set flag and configuration on peer-group member. */
6133 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6135 if (member
->allowas_in
[afi
][safi
] != 0
6136 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
6137 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6138 SET_FLAG(member
->af_flags
[afi
][safi
],
6139 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6140 member
->allowas_in
[afi
][safi
] = 0;
6141 peer_on_policy_change(peer
, afi
, safi
, 0);
6144 if (member
->allowas_in
[afi
][safi
] != allow_num
6145 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
6146 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6147 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6148 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6149 member
->allowas_in
[afi
][safi
] = allow_num
;
6150 peer_on_policy_change(peer
, afi
, safi
, 0);
6158 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6160 struct peer
*member
;
6161 struct listnode
*node
, *nnode
;
6163 /* Skip peer if flag is already disabled. */
6164 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
6167 /* Inherit configuration from peer-group if peer is member. */
6168 if (peer_group_active(peer
)) {
6169 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6170 peer_af_flag_inherit(peer
, afi
, safi
,
6171 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6172 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
6173 peer_on_policy_change(peer
, afi
, safi
, 0);
6178 /* Remove flag and configuration from peer. */
6179 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6180 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6181 peer
->allowas_in
[afi
][safi
] = 0;
6182 peer_on_policy_change(peer
, afi
, safi
, 0);
6184 /* Skip peer-group mechanics if handling a regular peer. */
6185 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6189 * Remove flags and configuration from all peer-group members, unless
6190 * they are explicitly overriding peer-group configuration.
6192 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6193 /* Skip peers with overridden configuration. */
6194 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6195 PEER_FLAG_ALLOWAS_IN
))
6198 /* Remove flags and configuration on peer-group member. */
6199 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6200 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6201 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6202 member
->allowas_in
[afi
][safi
] = 0;
6203 peer_on_policy_change(member
, afi
, safi
, 0);
6209 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
6212 bool old_no_prepend
, old_replace_as
;
6213 struct bgp
*bgp
= peer
->bgp
;
6214 struct peer
*member
;
6215 struct listnode
*node
, *nnode
;
6218 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
6220 /* Save previous flag states. */
6222 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6224 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6226 /* Set flag and configuration on peer. */
6227 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
6228 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
6229 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
6231 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
6232 && old_replace_as
== replace_as
)
6234 peer
->change_local_as
= as
;
6235 (void)peer_sort(peer
);
6237 /* Check if handling a regular peer. */
6238 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6242 * Set flag and configuration on all peer-group members, unless they are
6243 * explicitly overriding peer-group configuration.
6245 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6246 /* Skip peers with overridden configuration. */
6247 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6250 /* Skip peers with the same configuration. */
6251 old_no_prepend
= CHECK_FLAG(member
->flags
,
6252 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6253 old_replace_as
= CHECK_FLAG(member
->flags
,
6254 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6255 if (member
->change_local_as
== as
6256 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
6257 && old_no_prepend
== no_prepend
6258 && old_replace_as
== replace_as
)
6261 /* Set flag and configuration on peer-group member. */
6262 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6263 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
6265 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
6267 member
->change_local_as
= as
;
6273 int peer_local_as_unset(struct peer
*peer
)
6275 struct peer
*member
;
6276 struct listnode
*node
, *nnode
;
6278 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
6281 /* Inherit configuration from peer-group if peer is member. */
6282 if (peer_group_active(peer
)) {
6283 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
6284 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6285 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6286 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
6288 /* Otherwise remove flag and configuration from peer. */
6289 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
6290 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6291 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6292 peer
->change_local_as
= 0;
6295 /* Check if handling a regular peer. */
6296 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6297 /* Send notification or stop peer depending on state. */
6298 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6299 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6300 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6301 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6303 BGP_EVENT_ADD(peer
, BGP_Stop
);
6305 /* Skip peer-group mechanics for regular peers. */
6310 * Remove flag and configuration from all peer-group members, unless
6311 * they are explicitly overriding peer-group configuration.
6313 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6314 /* Skip peers with overridden configuration. */
6315 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6318 /* Remove flag and configuration on peer-group member. */
6319 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6320 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6321 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6322 member
->change_local_as
= 0;
6324 /* Send notification or stop peer depending on state. */
6325 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6326 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6327 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6328 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6330 bgp_session_reset(member
);
6336 /* Set password for authenticating with the peer. */
6337 int peer_password_set(struct peer
*peer
, const char *password
)
6339 struct peer
*member
;
6340 struct listnode
*node
, *nnode
;
6341 int len
= password
? strlen(password
) : 0;
6342 int ret
= BGP_SUCCESS
;
6344 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
6345 return BGP_ERR_INVALID_VALUE
;
6347 /* Set flag and configuration on peer. */
6348 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
6349 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
6351 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6352 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6354 /* Check if handling a regular peer. */
6355 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6356 /* Send notification or reset peer depending on state. */
6357 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6358 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6359 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6361 bgp_session_reset(peer
);
6364 * Attempt to install password on socket and skip peer-group
6367 if (BGP_PEER_SU_UNSPEC(peer
))
6369 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6370 : BGP_ERR_TCPSIG_FAILED
;
6374 * Set flag and configuration on all peer-group members, unless they are
6375 * explicitly overriding peer-group configuration.
6377 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6378 /* Skip peers with overridden configuration. */
6379 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6382 /* Skip peers with the same password. */
6383 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6386 /* Set flag and configuration on peer-group member. */
6387 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6388 if (member
->password
)
6389 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6390 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6392 /* Send notification or reset peer depending on state. */
6393 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6394 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6395 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6397 bgp_session_reset(member
);
6399 /* Attempt to install password on socket. */
6400 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6401 ret
= BGP_ERR_TCPSIG_FAILED
;
6404 /* Set flag and configuration on all peer-group listen ranges */
6405 struct listnode
*ln
;
6408 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6409 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6410 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6411 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6416 int peer_password_unset(struct peer
*peer
)
6418 struct peer
*member
;
6419 struct listnode
*node
, *nnode
;
6421 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6424 /* Inherit configuration from peer-group if peer is member. */
6425 if (peer_group_active(peer
)) {
6426 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6427 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6428 MTYPE_PEER_PASSWORD
);
6430 /* Otherwise remove flag and configuration from peer. */
6431 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6432 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6435 /* Check if handling a regular peer. */
6436 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6437 /* Send notification or reset peer depending on state. */
6438 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6439 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6440 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6442 bgp_session_reset(peer
);
6444 /* Attempt to uninstall password on socket. */
6445 if (!BGP_PEER_SU_UNSPEC(peer
))
6446 bgp_md5_unset(peer
);
6447 /* Skip peer-group mechanics for regular peers. */
6452 * Remove flag and configuration from all peer-group members, unless
6453 * they are explicitly overriding peer-group configuration.
6455 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6456 /* Skip peers with overridden configuration. */
6457 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6460 /* Remove flag and configuration on peer-group member. */
6461 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6462 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6464 /* Send notification or reset peer depending on state. */
6465 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6466 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6467 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6469 bgp_session_reset(member
);
6471 /* Attempt to uninstall password on socket. */
6472 if (!BGP_PEER_SU_UNSPEC(member
))
6473 bgp_md5_unset(member
);
6476 /* Set flag and configuration on all peer-group listen ranges */
6477 struct listnode
*ln
;
6480 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6481 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6482 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6483 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6489 /* Set distribute list to the peer. */
6490 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6493 struct peer
*member
;
6494 struct bgp_filter
*filter
;
6495 struct listnode
*node
, *nnode
;
6497 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6498 return BGP_ERR_INVALID_VALUE
;
6500 /* Set configuration on peer. */
6501 filter
= &peer
->filter
[afi
][safi
];
6502 if (filter
->plist
[direct
].name
)
6503 return BGP_ERR_PEER_FILTER_CONFLICT
;
6504 if (filter
->dlist
[direct
].name
)
6505 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6506 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6507 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6509 /* Check if handling a regular peer. */
6510 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6511 /* Set override-flag and process peer route updates. */
6512 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6513 PEER_FT_DISTRIBUTE_LIST
);
6514 peer_on_policy_change(peer
, afi
, safi
,
6515 (direct
== FILTER_OUT
) ? 1 : 0);
6517 /* Skip peer-group mechanics for regular peers. */
6522 * Set configuration on all peer-group members, un less they are
6523 * explicitly overriding peer-group configuration.
6525 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6526 /* Skip peers with overridden configuration. */
6527 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6528 PEER_FT_DISTRIBUTE_LIST
))
6531 /* Set configuration on peer-group member. */
6532 filter
= &member
->filter
[afi
][safi
];
6533 if (filter
->dlist
[direct
].name
)
6534 XFREE(MTYPE_BGP_FILTER_NAME
,
6535 filter
->dlist
[direct
].name
);
6536 filter
->dlist
[direct
].name
=
6537 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6538 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6540 /* Process peer route updates. */
6541 peer_on_policy_change(member
, afi
, safi
,
6542 (direct
== FILTER_OUT
) ? 1 : 0);
6548 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6550 struct peer
*member
;
6551 struct bgp_filter
*filter
;
6552 struct listnode
*node
, *nnode
;
6554 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6555 return BGP_ERR_INVALID_VALUE
;
6557 /* Unset override-flag unconditionally. */
6558 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6559 PEER_FT_DISTRIBUTE_LIST
);
6561 /* Inherit configuration from peer-group if peer is member. */
6562 if (peer_group_active(peer
)) {
6563 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6564 filter
[afi
][safi
].dlist
[direct
].name
,
6565 MTYPE_BGP_FILTER_NAME
);
6566 PEER_ATTR_INHERIT(peer
, peer
->group
,
6567 filter
[afi
][safi
].dlist
[direct
].alist
);
6569 /* Otherwise remove configuration from peer. */
6570 filter
= &peer
->filter
[afi
][safi
];
6571 if (filter
->dlist
[direct
].name
)
6572 XFREE(MTYPE_BGP_FILTER_NAME
,
6573 filter
->dlist
[direct
].name
);
6574 filter
->dlist
[direct
].name
= NULL
;
6575 filter
->dlist
[direct
].alist
= NULL
;
6578 /* Check if handling a regular peer. */
6579 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6580 /* Process peer route updates. */
6581 peer_on_policy_change(peer
, afi
, safi
,
6582 (direct
== FILTER_OUT
) ? 1 : 0);
6584 /* Skip peer-group mechanics for regular peers. */
6589 * Remove configuration on all peer-group members, unless they are
6590 * explicitly overriding peer-group configuration.
6592 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6593 /* Skip peers with overridden configuration. */
6594 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6595 PEER_FT_DISTRIBUTE_LIST
))
6598 /* Remove configuration on peer-group member. */
6599 filter
= &member
->filter
[afi
][safi
];
6600 if (filter
->dlist
[direct
].name
)
6601 XFREE(MTYPE_BGP_FILTER_NAME
,
6602 filter
->dlist
[direct
].name
);
6603 filter
->dlist
[direct
].name
= NULL
;
6604 filter
->dlist
[direct
].alist
= NULL
;
6606 /* Process peer route updates. */
6607 peer_on_policy_change(member
, afi
, safi
,
6608 (direct
== FILTER_OUT
) ? 1 : 0);
6614 /* Update distribute list. */
6615 static void peer_distribute_update(struct access_list
*access
)
6620 struct listnode
*mnode
, *mnnode
;
6621 struct listnode
*node
, *nnode
;
6624 struct peer_group
*group
;
6625 struct bgp_filter
*filter
;
6627 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6629 update_group_policy_update(bgp
,
6630 BGP_POLICY_DISTRIBUTE_LIST
,
6631 access
->name
, true, 0);
6632 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6633 FOREACH_AFI_SAFI (afi
, safi
) {
6634 filter
= &peer
->filter
[afi
][safi
];
6636 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6638 if (filter
->dlist
[direct
].name
)
6639 filter
->dlist
[direct
]
6640 .alist
= access_list_lookup(
6642 filter
->dlist
[direct
]
6645 filter
->dlist
[direct
].alist
=
6650 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6651 FOREACH_AFI_SAFI (afi
, safi
) {
6652 filter
= &group
->conf
->filter
[afi
][safi
];
6654 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6656 if (filter
->dlist
[direct
].name
)
6657 filter
->dlist
[direct
]
6658 .alist
= access_list_lookup(
6660 filter
->dlist
[direct
]
6663 filter
->dlist
[direct
].alist
=
6668 #ifdef ENABLE_BGP_VNC
6669 vnc_prefix_list_update(bgp
);
6674 /* Set prefix list to the peer. */
6675 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6678 struct peer
*member
;
6679 struct bgp_filter
*filter
;
6680 struct listnode
*node
, *nnode
;
6682 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6683 return BGP_ERR_INVALID_VALUE
;
6685 /* Set configuration on peer. */
6686 filter
= &peer
->filter
[afi
][safi
];
6687 if (filter
->dlist
[direct
].name
)
6688 return BGP_ERR_PEER_FILTER_CONFLICT
;
6689 if (filter
->plist
[direct
].name
)
6690 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6691 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6692 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6694 /* Check if handling a regular peer. */
6695 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6696 /* Set override-flag and process peer route updates. */
6697 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6698 PEER_FT_PREFIX_LIST
);
6699 peer_on_policy_change(peer
, afi
, safi
,
6700 (direct
== FILTER_OUT
) ? 1 : 0);
6702 /* Skip peer-group mechanics for regular peers. */
6707 * Set configuration on all peer-group members, unless they are
6708 * explicitly overriding peer-group configuration.
6710 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6711 /* Skip peers with overridden configuration. */
6712 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6713 PEER_FT_PREFIX_LIST
))
6716 /* Set configuration on peer-group member. */
6717 filter
= &member
->filter
[afi
][safi
];
6718 if (filter
->plist
[direct
].name
)
6719 XFREE(MTYPE_BGP_FILTER_NAME
,
6720 filter
->plist
[direct
].name
);
6721 filter
->plist
[direct
].name
=
6722 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6723 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6725 /* Process peer route updates. */
6726 peer_on_policy_change(member
, afi
, safi
,
6727 (direct
== FILTER_OUT
) ? 1 : 0);
6733 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6736 struct peer
*member
;
6737 struct bgp_filter
*filter
;
6738 struct listnode
*node
, *nnode
;
6740 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6741 return BGP_ERR_INVALID_VALUE
;
6743 /* Unset override-flag unconditionally. */
6744 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6745 PEER_FT_PREFIX_LIST
);
6747 /* Inherit configuration from peer-group if peer is member. */
6748 if (peer_group_active(peer
)) {
6749 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6750 filter
[afi
][safi
].plist
[direct
].name
,
6751 MTYPE_BGP_FILTER_NAME
);
6752 PEER_ATTR_INHERIT(peer
, peer
->group
,
6753 filter
[afi
][safi
].plist
[direct
].plist
);
6755 /* Otherwise remove configuration from peer. */
6756 filter
= &peer
->filter
[afi
][safi
];
6757 if (filter
->plist
[direct
].name
)
6758 XFREE(MTYPE_BGP_FILTER_NAME
,
6759 filter
->plist
[direct
].name
);
6760 filter
->plist
[direct
].name
= NULL
;
6761 filter
->plist
[direct
].plist
= NULL
;
6764 /* Check if handling a regular peer. */
6765 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6766 /* Process peer route updates. */
6767 peer_on_policy_change(peer
, afi
, safi
,
6768 (direct
== FILTER_OUT
) ? 1 : 0);
6770 /* Skip peer-group mechanics for regular peers. */
6775 * Remove configuration on all peer-group members, unless they are
6776 * explicitly overriding peer-group configuration.
6778 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6779 /* Skip peers with overridden configuration. */
6780 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6781 PEER_FT_PREFIX_LIST
))
6784 /* Remove configuration on peer-group member. */
6785 filter
= &member
->filter
[afi
][safi
];
6786 if (filter
->plist
[direct
].name
)
6787 XFREE(MTYPE_BGP_FILTER_NAME
,
6788 filter
->plist
[direct
].name
);
6789 filter
->plist
[direct
].name
= NULL
;
6790 filter
->plist
[direct
].plist
= NULL
;
6792 /* Process peer route updates. */
6793 peer_on_policy_change(member
, afi
, safi
,
6794 (direct
== FILTER_OUT
) ? 1 : 0);
6800 /* Update prefix-list list. */
6801 static void peer_prefix_list_update(struct prefix_list
*plist
)
6803 struct listnode
*mnode
, *mnnode
;
6804 struct listnode
*node
, *nnode
;
6807 struct peer_group
*group
;
6808 struct bgp_filter
*filter
;
6813 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6816 * Update the prefix-list on update groups.
6818 update_group_policy_update(
6819 bgp
, BGP_POLICY_PREFIX_LIST
,
6820 plist
? prefix_list_name(plist
) : NULL
, true, 0);
6822 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6823 FOREACH_AFI_SAFI (afi
, safi
) {
6824 filter
= &peer
->filter
[afi
][safi
];
6826 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6828 if (filter
->plist
[direct
].name
)
6829 filter
->plist
[direct
]
6830 .plist
= prefix_list_lookup(
6832 filter
->plist
[direct
]
6835 filter
->plist
[direct
].plist
=
6839 /* If we touch prefix-list, we need to process
6840 * new updates. This is important for ORF to
6841 * work correctly as well.
6843 if (peer
->afc_nego
[afi
][safi
])
6844 peer_on_policy_change(peer
, afi
, safi
,
6848 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6849 FOREACH_AFI_SAFI (afi
, safi
) {
6850 filter
= &group
->conf
->filter
[afi
][safi
];
6852 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6854 if (filter
->plist
[direct
].name
)
6855 filter
->plist
[direct
]
6856 .plist
= prefix_list_lookup(
6858 filter
->plist
[direct
]
6861 filter
->plist
[direct
].plist
=
6869 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6872 struct peer
*member
;
6873 struct bgp_filter
*filter
;
6874 struct listnode
*node
, *nnode
;
6876 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6877 return BGP_ERR_INVALID_VALUE
;
6879 /* Set configuration on peer. */
6880 filter
= &peer
->filter
[afi
][safi
];
6881 if (filter
->aslist
[direct
].name
)
6882 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6883 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6884 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6886 /* Check if handling a regular peer. */
6887 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6888 /* Set override-flag and process peer route updates. */
6889 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6890 PEER_FT_FILTER_LIST
);
6891 peer_on_policy_change(peer
, afi
, safi
,
6892 (direct
== FILTER_OUT
) ? 1 : 0);
6894 /* Skip peer-group mechanics for regular peers. */
6899 * Set configuration on all peer-group members, unless they are
6900 * explicitly overriding peer-group configuration.
6902 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6903 /* Skip peers with overridden configuration. */
6904 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6905 PEER_FT_FILTER_LIST
))
6908 /* Set configuration on peer-group member. */
6909 filter
= &member
->filter
[afi
][safi
];
6910 if (filter
->aslist
[direct
].name
)
6911 XFREE(MTYPE_BGP_FILTER_NAME
,
6912 filter
->aslist
[direct
].name
);
6913 filter
->aslist
[direct
].name
=
6914 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6915 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6917 /* Process peer route updates. */
6918 peer_on_policy_change(member
, afi
, safi
,
6919 (direct
== FILTER_OUT
) ? 1 : 0);
6925 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6927 struct peer
*member
;
6928 struct bgp_filter
*filter
;
6929 struct listnode
*node
, *nnode
;
6931 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6932 return BGP_ERR_INVALID_VALUE
;
6934 /* Unset override-flag unconditionally. */
6935 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6936 PEER_FT_FILTER_LIST
);
6938 /* Inherit configuration from peer-group if peer is member. */
6939 if (peer_group_active(peer
)) {
6940 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6941 filter
[afi
][safi
].aslist
[direct
].name
,
6942 MTYPE_BGP_FILTER_NAME
);
6943 PEER_ATTR_INHERIT(peer
, peer
->group
,
6944 filter
[afi
][safi
].aslist
[direct
].aslist
);
6946 /* Otherwise remove configuration from peer. */
6947 filter
= &peer
->filter
[afi
][safi
];
6948 if (filter
->aslist
[direct
].name
)
6949 XFREE(MTYPE_BGP_FILTER_NAME
,
6950 filter
->aslist
[direct
].name
);
6951 filter
->aslist
[direct
].name
= NULL
;
6952 filter
->aslist
[direct
].aslist
= NULL
;
6955 /* Check if handling a regular peer. */
6956 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6957 /* Process peer route updates. */
6958 peer_on_policy_change(peer
, afi
, safi
,
6959 (direct
== FILTER_OUT
) ? 1 : 0);
6961 /* Skip peer-group mechanics for regular peers. */
6966 * Remove configuration on all peer-group members, unless they are
6967 * explicitly overriding peer-group configuration.
6969 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6970 /* Skip peers with overridden configuration. */
6971 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6972 PEER_FT_FILTER_LIST
))
6975 /* Remove configuration on peer-group member. */
6976 filter
= &member
->filter
[afi
][safi
];
6977 if (filter
->aslist
[direct
].name
)
6978 XFREE(MTYPE_BGP_FILTER_NAME
,
6979 filter
->aslist
[direct
].name
);
6980 filter
->aslist
[direct
].name
= NULL
;
6981 filter
->aslist
[direct
].aslist
= NULL
;
6983 /* Process peer route updates. */
6984 peer_on_policy_change(member
, afi
, safi
,
6985 (direct
== FILTER_OUT
) ? 1 : 0);
6991 static void peer_aslist_update(const char *aslist_name
)
6996 struct listnode
*mnode
, *mnnode
;
6997 struct listnode
*node
, *nnode
;
7000 struct peer_group
*group
;
7001 struct bgp_filter
*filter
;
7003 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
7004 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
7005 aslist_name
, true, 0);
7007 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7008 FOREACH_AFI_SAFI (afi
, safi
) {
7009 filter
= &peer
->filter
[afi
][safi
];
7011 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7013 if (filter
->aslist
[direct
].name
)
7014 filter
->aslist
[direct
]
7015 .aslist
= as_list_lookup(
7016 filter
->aslist
[direct
]
7019 filter
->aslist
[direct
].aslist
=
7024 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
7025 FOREACH_AFI_SAFI (afi
, safi
) {
7026 filter
= &group
->conf
->filter
[afi
][safi
];
7028 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7030 if (filter
->aslist
[direct
].name
)
7031 filter
->aslist
[direct
]
7032 .aslist
= as_list_lookup(
7033 filter
->aslist
[direct
]
7036 filter
->aslist
[direct
].aslist
=
7044 static void peer_aslist_add(char *aslist_name
)
7046 peer_aslist_update(aslist_name
);
7047 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
7050 static void peer_aslist_del(const char *aslist_name
)
7052 peer_aslist_update(aslist_name
);
7053 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
7057 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
7058 const char *name
, struct route_map
*route_map
)
7060 struct peer
*member
;
7061 struct bgp_filter
*filter
;
7062 struct listnode
*node
, *nnode
;
7064 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7065 return BGP_ERR_INVALID_VALUE
;
7067 /* Set configuration on peer. */
7068 filter
= &peer
->filter
[afi
][safi
];
7069 if (filter
->map
[direct
].name
) {
7070 /* If the neighbor is configured with the same route-map
7071 * again then, ignore the duplicate configuration.
7073 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
7076 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7078 route_map_counter_decrement(filter
->map
[direct
].map
);
7079 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7080 filter
->map
[direct
].map
= route_map
;
7081 route_map_counter_increment(route_map
);
7083 /* Check if handling a regular peer. */
7084 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7085 /* Set override-flag and process peer route updates. */
7086 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
7088 peer_on_policy_change(peer
, afi
, safi
,
7089 (direct
== RMAP_OUT
) ? 1 : 0);
7091 /* Skip peer-group mechanics for regular peers. */
7096 * Set configuration on all peer-group members, unless they are
7097 * explicitly overriding peer-group configuration.
7099 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7100 /* Skip peers with overridden configuration. */
7101 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7105 /* Set configuration on peer-group member. */
7106 filter
= &member
->filter
[afi
][safi
];
7107 if (filter
->map
[direct
].name
)
7108 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7109 route_map_counter_decrement(filter
->map
[direct
].map
);
7110 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7111 filter
->map
[direct
].map
= route_map
;
7112 route_map_counter_increment(route_map
);
7114 /* Process peer route updates. */
7115 peer_on_policy_change(member
, afi
, safi
,
7116 (direct
== RMAP_OUT
) ? 1 : 0);
7121 /* Unset route-map from the peer. */
7122 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
7124 struct peer
*member
;
7125 struct bgp_filter
*filter
;
7126 struct listnode
*node
, *nnode
;
7128 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7129 return BGP_ERR_INVALID_VALUE
;
7131 /* Unset override-flag unconditionally. */
7132 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
7134 /* Inherit configuration from peer-group if peer is member. */
7135 if (peer_group_active(peer
)) {
7136 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7137 filter
[afi
][safi
].map
[direct
].name
,
7138 MTYPE_BGP_FILTER_NAME
);
7139 PEER_ATTR_INHERIT(peer
, peer
->group
,
7140 filter
[afi
][safi
].map
[direct
].map
);
7142 /* Otherwise remove configuration from peer. */
7143 filter
= &peer
->filter
[afi
][safi
];
7144 if (filter
->map
[direct
].name
)
7145 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7146 route_map_counter_decrement(filter
->map
[direct
].map
);
7147 filter
->map
[direct
].name
= NULL
;
7148 filter
->map
[direct
].map
= NULL
;
7151 /* Check if handling a regular peer. */
7152 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7153 /* Process peer route updates. */
7154 peer_on_policy_change(peer
, afi
, safi
,
7155 (direct
== RMAP_OUT
) ? 1 : 0);
7157 /* Skip peer-group mechanics for regular peers. */
7162 * Remove configuration on all peer-group members, unless they are
7163 * explicitly overriding peer-group configuration.
7165 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7166 /* Skip peers with overridden configuration. */
7167 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7171 /* Remove configuration on peer-group member. */
7172 filter
= &member
->filter
[afi
][safi
];
7173 if (filter
->map
[direct
].name
)
7174 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7175 route_map_counter_decrement(filter
->map
[direct
].map
);
7176 filter
->map
[direct
].name
= NULL
;
7177 filter
->map
[direct
].map
= NULL
;
7179 /* Process peer route updates. */
7180 peer_on_policy_change(member
, afi
, safi
,
7181 (direct
== RMAP_OUT
) ? 1 : 0);
7187 /* Set unsuppress-map to the peer. */
7188 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7189 const char *name
, struct route_map
*route_map
)
7191 struct peer
*member
;
7192 struct bgp_filter
*filter
;
7193 struct listnode
*node
, *nnode
;
7195 /* Set configuration on peer. */
7196 filter
= &peer
->filter
[afi
][safi
];
7197 if (filter
->usmap
.name
)
7198 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7199 route_map_counter_decrement(filter
->usmap
.map
);
7200 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7201 filter
->usmap
.map
= route_map
;
7202 route_map_counter_increment(route_map
);
7204 /* Check if handling a regular peer. */
7205 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7206 /* Set override-flag and process peer route updates. */
7207 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
7208 PEER_FT_UNSUPPRESS_MAP
);
7209 peer_on_policy_change(peer
, afi
, safi
, 1);
7211 /* Skip peer-group mechanics for regular peers. */
7216 * Set configuration on all peer-group members, unless they are
7217 * explicitly overriding peer-group configuration.
7219 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7220 /* Skip peers with overridden configuration. */
7221 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7222 PEER_FT_UNSUPPRESS_MAP
))
7225 /* Set configuration on peer-group member. */
7226 filter
= &member
->filter
[afi
][safi
];
7227 if (filter
->usmap
.name
)
7228 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7229 route_map_counter_decrement(filter
->usmap
.map
);
7230 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7231 filter
->usmap
.map
= route_map
;
7232 route_map_counter_increment(route_map
);
7234 /* Process peer route updates. */
7235 peer_on_policy_change(member
, afi
, safi
, 1);
7241 /* Unset route-map from the peer. */
7242 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7244 struct peer
*member
;
7245 struct bgp_filter
*filter
;
7246 struct listnode
*node
, *nnode
;
7248 /* Unset override-flag unconditionally. */
7249 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
7251 /* Inherit configuration from peer-group if peer is member. */
7252 if (peer_group_active(peer
)) {
7253 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7254 filter
[afi
][safi
].usmap
.name
,
7255 MTYPE_BGP_FILTER_NAME
);
7256 PEER_ATTR_INHERIT(peer
, peer
->group
,
7257 filter
[afi
][safi
].usmap
.map
);
7259 /* Otherwise remove configuration from peer. */
7260 filter
= &peer
->filter
[afi
][safi
];
7261 if (filter
->usmap
.name
)
7262 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7263 route_map_counter_decrement(filter
->usmap
.map
);
7264 filter
->usmap
.name
= NULL
;
7265 filter
->usmap
.map
= NULL
;
7268 /* Check if handling a regular peer. */
7269 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7270 /* Process peer route updates. */
7271 peer_on_policy_change(peer
, afi
, safi
, 1);
7273 /* Skip peer-group mechanics for regular peers. */
7278 * Remove configuration on all peer-group members, unless they are
7279 * explicitly overriding peer-group configuration.
7281 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7282 /* Skip peers with overridden configuration. */
7283 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7284 PEER_FT_UNSUPPRESS_MAP
))
7287 /* Remove configuration on peer-group member. */
7288 filter
= &member
->filter
[afi
][safi
];
7289 if (filter
->usmap
.name
)
7290 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7291 route_map_counter_decrement(filter
->usmap
.map
);
7292 filter
->usmap
.name
= NULL
;
7293 filter
->usmap
.map
= NULL
;
7295 /* Process peer route updates. */
7296 peer_on_policy_change(member
, afi
, safi
, 1);
7302 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7304 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7307 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7308 if (peer
->t_pmax_restart
) {
7309 THREAD_OFF(peer
->t_pmax_restart
);
7310 if (bgp_debug_neighbor_events(peer
))
7312 "%pBP Maximum-prefix restart timer cancelled",
7315 BGP_EVENT_ADD(peer
, BGP_Start
);
7319 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7320 uint32_t max
, uint8_t threshold
, int warning
,
7321 uint16_t restart
, bool force
)
7323 struct peer
*member
;
7324 struct listnode
*node
, *nnode
;
7326 /* Set flags and configuration on peer. */
7327 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7330 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7332 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7335 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7337 peer_af_flag_unset(peer
, afi
, safi
,
7338 PEER_FLAG_MAX_PREFIX_WARNING
);
7340 peer
->pmax
[afi
][safi
] = max
;
7341 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7342 peer
->pmax_restart
[afi
][safi
] = restart
;
7344 /* Check if handling a regular peer. */
7345 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7346 /* Re-check if peer violates maximum-prefix. */
7347 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7348 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7350 /* Skip peer-group mechanics for regular peers. */
7355 * Set flags and configuration on all peer-group members, unless they
7356 * are explicitly overriding peer-group configuration.
7358 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7359 /* Skip peers with overridden configuration. */
7360 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7361 PEER_FLAG_MAX_PREFIX
))
7364 /* Set flag and configuration on peer-group member. */
7365 member
->pmax
[afi
][safi
] = max
;
7366 member
->pmax_threshold
[afi
][safi
] = threshold
;
7367 member
->pmax_restart
[afi
][safi
] = restart
;
7370 SET_FLAG(member
->af_flags
[afi
][safi
],
7371 PEER_FLAG_MAX_PREFIX_FORCE
);
7373 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7374 PEER_FLAG_MAX_PREFIX_FORCE
);
7377 SET_FLAG(member
->af_flags
[afi
][safi
],
7378 PEER_FLAG_MAX_PREFIX_WARNING
);
7380 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7381 PEER_FLAG_MAX_PREFIX_WARNING
);
7383 /* Re-check if peer violates maximum-prefix. */
7384 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7385 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7391 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7393 /* Inherit configuration from peer-group if peer is member. */
7394 if (peer_group_active(peer
)) {
7395 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7396 peer_af_flag_inherit(peer
, afi
, safi
,
7397 PEER_FLAG_MAX_PREFIX_FORCE
);
7398 peer_af_flag_inherit(peer
, afi
, safi
,
7399 PEER_FLAG_MAX_PREFIX_WARNING
);
7400 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7401 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7402 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7407 /* Remove flags and configuration from peer. */
7408 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7409 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7410 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7411 peer
->pmax
[afi
][safi
] = 0;
7412 peer
->pmax_threshold
[afi
][safi
] = 0;
7413 peer
->pmax_restart
[afi
][safi
] = 0;
7416 * Remove flags and configuration from all peer-group members, unless
7417 * they are explicitly overriding peer-group configuration.
7419 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7420 struct peer
*member
;
7421 struct listnode
*node
;
7423 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7424 /* Skip peers with overridden configuration. */
7425 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7426 PEER_FLAG_MAX_PREFIX
))
7429 /* Remove flag and configuration on peer-group member.
7431 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7432 PEER_FLAG_MAX_PREFIX
);
7433 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7434 PEER_FLAG_MAX_PREFIX_FORCE
);
7435 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7436 PEER_FLAG_MAX_PREFIX_WARNING
);
7437 member
->pmax
[afi
][safi
] = 0;
7438 member
->pmax_threshold
[afi
][safi
] = 0;
7439 member
->pmax_restart
[afi
][safi
] = 0;
7441 peer_maximum_prefix_clear_overflow(member
);
7444 peer_maximum_prefix_clear_overflow(peer
);
7450 void peer_maximum_prefix_out_refresh_routes(struct peer
*peer
, afi_t afi
,
7453 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
7455 if (peer_established(peer
))
7456 bgp_announce_route(peer
, afi
, safi
, false);
7459 int peer_maximum_prefix_out_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7462 struct peer
*member
;
7463 struct listnode
*node
, *nnode
;
7465 /* Set flag on peer and peer-group member if any */
7466 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7467 /* Set configuration on peer. */
7468 peer
->pmax_out
[afi
][safi
] = max
;
7470 /* Check if handling a regular peer. */
7471 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7472 /* Skip peer-group mechanics for regular peers. */
7473 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7478 * Set flag and configuration on all peer-group members, unless they
7479 * are explicitly overriding peer-group configuration.
7481 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7482 /* Skip peers with overridden configuration. */
7483 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7484 PEER_FLAG_MAX_PREFIX_OUT
))
7487 /* Set configuration on peer-group member. */
7488 member
->pmax_out
[afi
][safi
] = max
;
7490 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7495 int peer_maximum_prefix_out_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7497 struct peer
*member
;
7498 struct listnode
*node
;
7499 /* Inherit configuration from peer-group if peer is member. */
7500 if (peer_group_active(peer
)) {
7501 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7502 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_out
[afi
][safi
]);
7504 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7508 /* Remove flag and configuration from peer. */
7509 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7510 peer
->pmax_out
[afi
][safi
] = 0;
7512 /* Check if handling a regular peer. */
7513 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7514 /* Skip peer-group mechanics for regular peers. */
7515 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7520 * Remove flag and configuration from all peer-group members, unless
7521 * they are explicitly overriding peer-group configuration.
7523 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7524 /* Skip peers with overridden configuration. */
7525 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7526 PEER_FLAG_MAX_PREFIX_OUT
))
7529 /* Remove flag and configuration on peer-group member.
7531 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7532 PEER_FLAG_MAX_PREFIX_OUT
);
7533 member
->pmax_out
[afi
][safi
] = 0;
7535 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7540 int is_ebgp_multihop_configured(struct peer
*peer
)
7542 struct peer_group
*group
;
7543 struct listnode
*node
, *nnode
;
7546 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7547 group
= peer
->group
;
7548 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7549 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7552 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7553 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7554 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7558 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7559 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7565 /* Set # of hops between us and BGP peer. */
7566 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7568 struct peer_group
*group
;
7570 struct listnode
*node
, *nnode
;
7573 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7576 /* We cannot configure ttl-security hops when ebgp-multihop is already
7577 set. For non peer-groups, the check is simple. For peer-groups,
7579 slightly messy, because we need to check both the peer-group
7581 and all peer-group members for any trace of ebgp-multihop
7583 before actually applying the ttl-security rules. Cisco really made a
7584 mess of this configuration parameter, and OpenBGPD got it right.
7587 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7588 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7589 if (is_ebgp_multihop_configured(peer
))
7590 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7592 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7593 peer
->gtsm_hops
= gtsm_hops
;
7595 /* Calling ebgp multihop also resets the session.
7596 * On restart, NHT will get setup correctly as will the
7597 * min & max ttls on the socket. The return value is
7600 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7605 group
= peer
->group
;
7606 group
->conf
->gtsm_hops
= gtsm_hops
;
7607 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7609 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7611 /* Calling ebgp multihop also resets the
7613 * On restart, NHT will get setup correctly as
7615 * min & max ttls on the socket. The return
7619 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7623 /* Post the first gtsm setup or if its ibgp, maxttl setting
7625 * necessary, just set the minttl.
7627 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7628 peer
->gtsm_hops
= gtsm_hops
;
7631 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7632 MAXTTL
+ 1 - gtsm_hops
);
7633 if ((peer
->status
< Established
) && peer
->doppelganger
7634 && (peer
->doppelganger
->fd
>= 0))
7635 sockopt_minttl(peer
->su
.sa
.sa_family
,
7636 peer
->doppelganger
->fd
,
7637 MAXTTL
+ 1 - gtsm_hops
);
7639 group
= peer
->group
;
7640 group
->conf
->gtsm_hops
= gtsm_hops
;
7641 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7643 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7645 /* Change setting of existing peer
7646 * established then change value (may break
7648 * not established yet (teardown session and
7650 * no session then do nothing (will get
7651 * handled by next connection)
7655 != BGP_GTSM_HOPS_DISABLED
)
7657 gpeer
->su
.sa
.sa_family
,
7659 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7660 if ((gpeer
->status
< Established
)
7661 && gpeer
->doppelganger
7662 && (gpeer
->doppelganger
->fd
>= 0))
7663 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7664 gpeer
->doppelganger
->fd
,
7665 MAXTTL
+ 1 - gtsm_hops
);
7673 int peer_ttl_security_hops_unset(struct peer
*peer
)
7675 struct peer_group
*group
;
7676 struct listnode
*node
, *nnode
;
7679 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7681 /* if a peer-group member, then reset to peer-group default rather than
7683 if (peer_group_active(peer
))
7684 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7686 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7688 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7689 /* Invoking ebgp_multihop_set will set the TTL back to the
7691 * value as well as restting the NHT and such. The session is
7694 if (peer
->sort
== BGP_PEER_EBGP
)
7695 ret
= peer_ebgp_multihop_unset(peer
);
7698 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7701 if ((peer
->status
< Established
) && peer
->doppelganger
7702 && (peer
->doppelganger
->fd
>= 0))
7703 sockopt_minttl(peer
->su
.sa
.sa_family
,
7704 peer
->doppelganger
->fd
, 0);
7707 group
= peer
->group
;
7708 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7709 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7710 if (peer
->sort
== BGP_PEER_EBGP
)
7711 ret
= peer_ebgp_multihop_unset(peer
);
7714 sockopt_minttl(peer
->su
.sa
.sa_family
,
7717 if ((peer
->status
< Established
)
7718 && peer
->doppelganger
7719 && (peer
->doppelganger
->fd
>= 0))
7720 sockopt_minttl(peer
->su
.sa
.sa_family
,
7721 peer
->doppelganger
->fd
,
7730 static void peer_reset_message_stats(struct peer
*peer
)
7733 atomic_store_explicit(&peer
->open_in
, 0, memory_order_relaxed
);
7734 atomic_store_explicit(&peer
->open_out
, 0, memory_order_relaxed
);
7735 atomic_store_explicit(&peer
->update_in
, 0,
7736 memory_order_relaxed
);
7737 atomic_store_explicit(&peer
->update_out
, 0,
7738 memory_order_relaxed
);
7739 atomic_store_explicit(&peer
->keepalive_in
, 0,
7740 memory_order_relaxed
);
7741 atomic_store_explicit(&peer
->keepalive_out
, 0,
7742 memory_order_relaxed
);
7743 atomic_store_explicit(&peer
->notify_in
, 0,
7744 memory_order_relaxed
);
7745 atomic_store_explicit(&peer
->notify_out
, 0,
7746 memory_order_relaxed
);
7747 atomic_store_explicit(&peer
->refresh_in
, 0,
7748 memory_order_relaxed
);
7749 atomic_store_explicit(&peer
->refresh_out
, 0,
7750 memory_order_relaxed
);
7751 atomic_store_explicit(&peer
->dynamic_cap_in
, 0,
7752 memory_order_relaxed
);
7753 atomic_store_explicit(&peer
->dynamic_cap_out
, 0,
7754 memory_order_relaxed
);
7759 * If peer clear is invoked in a loop for all peers on the BGP instance,
7760 * it may end up freeing the doppelganger, and if this was the next node
7761 * to the current node, we would end up accessing the freed next node.
7762 * Pass along additional parameter which can be updated if next node
7763 * is freed; only required when walking the peer list on BGP instance.
7765 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7767 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7768 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7769 if (peer_maximum_prefix_clear_overflow(peer
))
7772 peer
->v_start
= BGP_INIT_START_TIMER
;
7773 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7774 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7775 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7777 bgp_session_reset_safe(peer
, nnode
);
7782 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7783 enum bgp_clear_type stype
)
7785 struct peer_af
*paf
;
7787 if (!peer_established(peer
))
7790 if (!peer
->afc
[afi
][safi
])
7791 return BGP_ERR_AF_UNCONFIGURED
;
7793 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7795 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7796 /* Clear the "neighbor x.x.x.x default-originate" flag */
7797 paf
= peer_af_find(peer
, afi
, safi
);
7798 if (paf
&& paf
->subgroup
7799 && CHECK_FLAG(paf
->subgroup
->sflags
,
7800 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7801 UNSET_FLAG(paf
->subgroup
->sflags
,
7802 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7804 bgp_announce_route(peer
, afi
, safi
, false);
7807 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7808 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7809 PEER_CAP_ORF_PREFIX_SM_ADV
)
7810 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7811 PEER_CAP_ORF_PREFIX_RM_RCV
)
7812 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7813 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7814 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7815 uint8_t prefix_type
;
7817 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7818 PEER_CAP_ORF_PREFIX_RM_RCV
))
7819 prefix_type
= ORF_TYPE_PREFIX
;
7821 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7823 if (filter
->plist
[FILTER_IN
].plist
) {
7824 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7825 PEER_STATUS_ORF_PREFIX_SEND
))
7826 bgp_route_refresh_send(
7827 peer
, afi
, safi
, prefix_type
,
7829 BGP_ROUTE_REFRESH_NORMAL
);
7830 bgp_route_refresh_send(
7831 peer
, afi
, safi
, prefix_type
,
7832 REFRESH_IMMEDIATE
, 0,
7833 BGP_ROUTE_REFRESH_NORMAL
);
7835 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7836 PEER_STATUS_ORF_PREFIX_SEND
))
7837 bgp_route_refresh_send(
7838 peer
, afi
, safi
, prefix_type
,
7839 REFRESH_IMMEDIATE
, 1,
7840 BGP_ROUTE_REFRESH_NORMAL
);
7842 bgp_route_refresh_send(
7843 peer
, afi
, safi
, 0, 0, 0,
7844 BGP_ROUTE_REFRESH_NORMAL
);
7850 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7851 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7852 /* If neighbor has soft reconfiguration inbound flag.
7853 Use Adj-RIB-In database. */
7854 if (!bgp_soft_reconfig_in(peer
, afi
, safi
)) {
7855 /* If neighbor has route refresh capability, send route
7857 message to the peer. */
7858 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7859 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7860 bgp_route_refresh_send(
7861 peer
, afi
, safi
, 0, 0, 0,
7862 BGP_ROUTE_REFRESH_NORMAL
);
7864 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7868 if (stype
== BGP_CLEAR_MESSAGE_STATS
)
7869 peer_reset_message_stats(peer
);
7874 /* Display peer uptime.*/
7875 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7878 time_t uptime1
, epoch_tbuf
;
7881 /* If there is no connection has been done before print `never'. */
7884 json_object_string_add(json
, "peerUptime", "never");
7885 json_object_int_add(json
, "peerUptimeMsec", 0);
7887 snprintf(buf
, len
, "never");
7891 /* Get current time. */
7892 uptime1
= monotime(NULL
);
7894 gmtime_r(&uptime1
, &tm
);
7896 if (uptime1
< ONE_DAY_SECOND
)
7897 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7899 else if (uptime1
< ONE_WEEK_SECOND
)
7900 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7902 else if (uptime1
< ONE_YEAR_SECOND
)
7903 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7904 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7906 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7908 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7911 epoch_tbuf
= time(NULL
) - uptime1
;
7912 json_object_string_add(json
, "peerUptime", buf
);
7913 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7914 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7921 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7922 struct list
*addresses
)
7926 memset(&bgp_master
, 0, sizeof(bgp_master
));
7929 bm
->bgp
= list_new();
7930 bm
->listen_sockets
= list_new();
7931 bm
->port
= BGP_PORT_DEFAULT
;
7932 bm
->addresses
= addresses
;
7933 bm
->master
= master
;
7934 bm
->start_time
= monotime(NULL
);
7935 bm
->t_rmap_update
= NULL
;
7936 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7937 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7938 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7939 bm
->terminating
= false;
7940 bm
->socket_buffer
= buffer_size
;
7941 bm
->wait_for_fib
= false;
7942 bm
->tcp_dscp
= IPTOS_PREC_INTERNETCONTROL
;
7943 bm
->inq_limit
= BM_DEFAULT_INQ_LIMIT
;
7946 /* init the rd id space.
7947 assign 0th index in the bitfield,
7948 so that we start with id 1
7950 bf_init(bm
->rd_idspace
, UINT16_MAX
);
7951 bf_assign_zero_index(bm
->rd_idspace
);
7953 /* mpls label dynamic allocation pool */
7954 bgp_lp_init(bm
->master
, &bm
->labelpool
);
7958 QOBJ_REG(bm
, bgp_master
);
7962 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7963 * instance delete (non-default only) or BGP exit.
7965 static void bgp_if_finish(struct bgp
*bgp
)
7968 struct interface
*ifp
;
7970 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
7972 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
7975 FOR_ALL_INTERFACES (vrf
, ifp
) {
7976 struct listnode
*c_node
, *c_nnode
;
7977 struct connected
*c
;
7979 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
7980 bgp_connected_delete(bgp
, c
);
7984 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
7986 struct vrf
*vrf
= NULL
;
7987 struct listnode
*next
;
7990 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
7991 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
7993 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
7994 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
7997 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
8001 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
8003 struct listnode
*next
, *next2
;
8004 struct bgp
*bgp
, *bgp2
;
8007 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8009 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
8010 if (bgp2
->as
== bgp
->as
)
8018 snprintf(buf
, sizeof(buf
), "%u", bgp
->as
);
8019 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
8023 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
8024 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
8025 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
8026 {.completions
= NULL
},
8029 struct frr_pthread
*bgp_pth_io
;
8030 struct frr_pthread
*bgp_pth_ka
;
8032 static void bgp_pthreads_init(void)
8034 assert(!bgp_pth_io
);
8035 assert(!bgp_pth_ka
);
8037 struct frr_pthread_attr io
= {
8038 .start
= frr_pthread_attr_default
.start
,
8039 .stop
= frr_pthread_attr_default
.stop
,
8041 struct frr_pthread_attr ka
= {
8042 .start
= bgp_keepalives_start
,
8043 .stop
= bgp_keepalives_stop
,
8045 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
8046 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
8049 void bgp_pthreads_run(void)
8051 frr_pthread_run(bgp_pth_io
, NULL
);
8052 frr_pthread_run(bgp_pth_ka
, NULL
);
8054 /* Wait until threads are ready. */
8055 frr_pthread_wait_running(bgp_pth_io
);
8056 frr_pthread_wait_running(bgp_pth_ka
);
8059 void bgp_pthreads_finish(void)
8061 frr_pthread_stop_all();
8064 static int peer_unshut_after_cfg(struct bgp
*bgp
)
8066 struct listnode
*node
;
8069 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
8070 if (!peer
->shut_during_cfg
)
8073 if (bgp_debug_neighbor_events(peer
))
8074 zlog_debug("%s: released from config-pending hold",
8077 peer
->shut_during_cfg
= false;
8078 if (peer_active(peer
) && peer
->status
!= Established
) {
8079 if (peer
->status
!= Idle
)
8080 BGP_EVENT_ADD(peer
, BGP_Stop
);
8081 BGP_EVENT_ADD(peer
, BGP_Start
);
8088 void bgp_init(unsigned short instance
)
8090 hook_register(bgp_config_end
, peer_unshut_after_cfg
);
8092 /* allocates some vital data structures used by peer commands in
8095 /* pre-init pthreads */
8096 bgp_pthreads_init();
8099 bgp_zebra_init(bm
->master
, instance
);
8101 #ifdef ENABLE_BGP_VNC
8102 vnc_zebra_init(bm
->master
);
8105 /* BGP VTY commands installation. */
8111 bgp_community_alias_init();
8114 bgp_route_map_init();
8115 bgp_scan_vty_init();
8117 #ifdef ENABLE_BGP_VNC
8120 bgp_ethernetvpn_init();
8121 bgp_flowspec_vty_init();
8123 /* Access list initialize. */
8125 access_list_add_hook(peer_distribute_update
);
8126 access_list_delete_hook(peer_distribute_update
);
8128 /* Filter list initialize. */
8130 as_list_add_hook(peer_aslist_add
);
8131 as_list_delete_hook(peer_aslist_del
);
8133 /* Prefix list initialize.*/
8135 prefix_list_add_hook(peer_prefix_list_update
);
8136 prefix_list_delete_hook(peer_prefix_list_update
);
8138 /* Community list initialize. */
8139 bgp_clist
= community_list_init();
8142 bgp_bfd_init(bm
->master
);
8146 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
8149 void bgp_terminate(void)
8153 struct listnode
*node
, *nnode
;
8154 struct listnode
*mnode
, *mnnode
;
8158 /* Close the listener sockets first as this prevents peers from
8160 * to reconnect on receiving the peer unconfig message. In the presence
8161 * of a large number of peers this will ensure that no peer is left with
8162 * a dangling connection
8166 /* reverse bgp_master_init */
8167 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
8168 bgp_close_vrf_socket(bgp
);
8169 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8170 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
8171 if (bgp_debug_neighbor_events(peer
))
8173 "%pBP configured Graceful-Restart, skipping unconfig notification",
8177 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
8178 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
8179 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
8183 if (bm
->listen_sockets
)
8184 list_delete(&bm
->listen_sockets
);
8186 THREAD_OFF(bm
->t_rmap_update
);
8191 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
8192 const char *ip_str
, bool use_json
)
8198 /* Get peer sockunion. */
8199 ret
= str2sockunion(ip_str
, &su
);
8201 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
8203 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
8207 json_object
*json_no
= NULL
;
8208 json_no
= json_object_new_object();
8209 json_object_string_add(
8211 "malformedAddressOrName",
8213 vty_json(vty
, json_no
);
8216 "%% Malformed address or name: %s\n",
8224 /* Peer structure lookup. */
8225 peer
= peer_lookup(bgp
, &su
);
8228 json_object
*json_no
= NULL
;
8229 json_no
= json_object_new_object();
8230 json_object_string_add(json_no
, "warning",
8231 "No such neighbor in this view/vrf");
8232 vty_json(vty
, json_no
);
8234 vty_out(vty
, "No such neighbor in this view/vrf\n");
8241 void bgp_gr_apply_running_config(void)
8243 struct peer
*peer
= NULL
;
8244 struct bgp
*bgp
= NULL
;
8245 struct listnode
*node
, *nnode
;
8246 bool gr_router_detected
= false;
8248 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
8249 zlog_debug("[BGP_GR] %s called !", __func__
);
8251 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
8252 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8253 bgp_peer_gr_flags_update(peer
);
8254 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
8255 gr_router_detected
= true;
8258 if (gr_router_detected
8259 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
8260 bgp_zebra_send_capabilities(bgp
, true);
8261 } else if (!gr_router_detected
8262 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
8263 bgp_zebra_send_capabilities(bgp
, false);
8266 gr_router_detected
= false;
8270 printfrr_ext_autoreg_p("BP", printfrr_bp
);
8271 static ssize_t
printfrr_bp(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
8274 const struct peer
*peer
= ptr
;
8277 return bputs(buf
, "(null)");
8279 return bprintfrr(buf
, "%s(%s)", peer
->host
,
8280 peer
->hostname
? peer
->hostname
: "Unknown");