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 "bgp_trace.h"
97 DEFINE_MTYPE_STATIC(BGPD
, PEER_TX_SHUTDOWN_MSG
, "Peer shutdown message (TX)");
98 DEFINE_MTYPE_STATIC(BGPD
, BGP_EVPN_INFO
, "BGP EVPN instance information");
99 DEFINE_QOBJ_TYPE(bgp_master
);
100 DEFINE_QOBJ_TYPE(bgp
);
101 DEFINE_QOBJ_TYPE(peer
);
102 DEFINE_HOOK(bgp_inst_delete
, (struct bgp
*bgp
), (bgp
));
104 /* BGP process wide configuration. */
105 static struct bgp_master bgp_master
;
107 /* BGP process wide configuration pointer to export. */
108 struct bgp_master
*bm
;
110 /* BGP community-list. */
111 struct community_list_handler
*bgp_clist
;
113 unsigned int multipath_num
= MULTIPATH_NUM
;
115 /* Number of bgp instances configured for suppress fib config */
116 unsigned int bgp_suppress_fib_count
;
118 static void bgp_if_finish(struct bgp
*bgp
);
119 static void peer_drop_dynamic_neighbor(struct peer
*peer
);
121 extern struct zclient
*zclient
;
123 /* handle main socket creation or deletion */
124 static int bgp_check_main_socket(bool create
, struct bgp
*bgp
)
126 static int bgp_server_main_created
;
127 struct listnode
*node
;
131 if (bgp_server_main_created
)
133 if (list_isempty(bm
->addresses
)) {
134 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
135 return BGP_ERR_INVALID_VALUE
;
137 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
138 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
139 return BGP_ERR_INVALID_VALUE
;
141 bgp_server_main_created
= 1;
144 if (!bgp_server_main_created
)
147 bgp_server_main_created
= 0;
151 void bgp_session_reset(struct peer
*peer
)
153 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
154 && !(CHECK_FLAG(peer
->doppelganger
->flags
, PEER_FLAG_CONFIG_NODE
)))
155 peer_delete(peer
->doppelganger
);
157 BGP_EVENT_ADD(peer
, BGP_Stop
);
161 * During session reset, we may delete the doppelganger peer, which would
162 * be the next node to the current node. If the session reset was invoked
163 * during walk of peer list, we would end up accessing the freed next
164 * node. This function moves the next node along.
166 static void bgp_session_reset_safe(struct peer
*peer
, struct listnode
**nnode
)
171 n
= (nnode
) ? *nnode
: NULL
;
172 npeer
= (n
) ? listgetdata(n
) : NULL
;
174 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
175 && !(CHECK_FLAG(peer
->doppelganger
->flags
,
176 PEER_FLAG_CONFIG_NODE
))) {
177 if (peer
->doppelganger
== npeer
)
178 /* nnode and *nnode are confirmed to be non-NULL here */
179 *nnode
= (*nnode
)->next
;
180 peer_delete(peer
->doppelganger
);
183 BGP_EVENT_ADD(peer
, BGP_Stop
);
186 /* BGP global flag manipulation. */
187 int bgp_option_set(int flag
)
191 case BGP_OPT_NO_LISTEN
:
192 case BGP_OPT_NO_ZEBRA
:
193 SET_FLAG(bm
->options
, flag
);
196 return BGP_ERR_INVALID_FLAG
;
201 int bgp_option_unset(int flag
)
205 case BGP_OPT_NO_ZEBRA
:
207 UNSET_FLAG(bm
->options
, flag
);
210 return BGP_ERR_INVALID_FLAG
;
215 int bgp_option_check(int flag
)
217 return CHECK_FLAG(bm
->options
, flag
);
220 /* set the bgp no-rib option during runtime and remove installed routes */
221 void bgp_option_norib_set_runtime(void)
224 struct listnode
*node
;
228 if (bgp_option_check(BGP_OPT_NO_FIB
))
231 bgp_option_set(BGP_OPT_NO_FIB
);
233 zlog_info("Disabled BGP route installation to RIB (Zebra)");
235 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
236 FOREACH_AFI_SAFI (afi
, safi
) {
238 * Stop a crash, more work is needed
239 * here to properly add/remove these types of
242 if (!bgp_fibupd_safi(safi
))
245 bgp_zebra_withdraw_table_all_subtypes(bgp
, afi
, safi
);
249 zlog_info("All routes have been withdrawn from RIB (Zebra)");
252 /* unset the bgp no-rib option during runtime and announce routes to Zebra */
253 void bgp_option_norib_unset_runtime(void)
256 struct listnode
*node
;
260 if (!bgp_option_check(BGP_OPT_NO_FIB
))
263 bgp_option_unset(BGP_OPT_NO_FIB
);
265 zlog_info("Enabled BGP route installation to RIB (Zebra)");
267 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
268 FOREACH_AFI_SAFI (afi
, safi
) {
270 * Stop a crash, more work is needed
271 * here to properly add/remove these types
272 * of routes from zebra
274 if (!bgp_fibupd_safi(safi
))
277 bgp_zebra_announce_table_all_subtypes(bgp
, afi
, safi
);
281 zlog_info("All routes have been installed in RIB (Zebra)");
284 /* Internal function to set BGP structure configureation flag. */
285 static void bgp_config_set(struct bgp
*bgp
, int config
)
287 SET_FLAG(bgp
->config
, config
);
290 static void bgp_config_unset(struct bgp
*bgp
, int config
)
292 UNSET_FLAG(bgp
->config
, config
);
295 static int bgp_config_check(struct bgp
*bgp
, int config
)
297 return CHECK_FLAG(bgp
->config
, config
);
300 /* Set BGP router identifier; distinguish between explicit config and other
303 static int bgp_router_id_set(struct bgp
*bgp
, const struct in_addr
*id
,
307 struct listnode
*node
, *nnode
;
309 if (IPV4_ADDR_SAME(&bgp
->router_id
, id
))
312 /* EVPN uses router id in RD, withdraw them */
313 if (is_evpn_enabled())
314 bgp_evpn_handle_router_id_update(bgp
, true);
316 vpn_handle_router_id_update(bgp
, true, is_config
);
318 IPV4_ADDR_COPY(&bgp
->router_id
, id
);
320 /* Set all peer's local identifier with this value. */
321 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
322 IPV4_ADDR_COPY(&peer
->local_id
, id
);
324 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
325 peer
->last_reset
= PEER_DOWN_RID_CHANGE
;
326 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
327 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
331 /* EVPN uses router id in RD, update them */
332 if (is_evpn_enabled())
333 bgp_evpn_handle_router_id_update(bgp
, false);
335 vpn_handle_router_id_update(bgp
, false, is_config
);
340 void bgp_router_id_zebra_bump(vrf_id_t vrf_id
, const struct prefix
*router_id
)
342 struct listnode
*node
, *nnode
;
344 struct in_addr
*addr
= NULL
;
346 if (router_id
!= NULL
)
347 addr
= (struct in_addr
*)&(router_id
->u
.prefix4
);
349 if (vrf_id
== VRF_DEFAULT
) {
350 /* Router-id change for default VRF has to also update all
352 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
353 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
357 bgp
->router_id_zebra
= *addr
;
359 addr
= &bgp
->router_id_zebra
;
361 if (!bgp
->router_id_static
.s_addr
) {
362 /* Router ID is updated if there are no active
365 if (bgp
->established_peers
== 0) {
366 if (BGP_DEBUG(zebra
, ZEBRA
))
368 "RID change : vrf %s(%u), RTR ID %pI4",
372 * if old router-id was 0x0, set flag
373 * to use this new value
375 bgp_router_id_set(bgp
, addr
,
376 (bgp
->router_id
.s_addr
384 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
387 bgp
->router_id_zebra
= *addr
;
389 addr
= &bgp
->router_id_zebra
;
391 if (!bgp
->router_id_static
.s_addr
) {
392 /* Router ID is updated if there are no active
395 if (bgp
->established_peers
== 0) {
396 if (BGP_DEBUG(zebra
, ZEBRA
))
398 "RID change : vrf %s(%u), RTR ID %pI4",
402 * if old router-id was 0x0, set flag
403 * to use this new value
405 bgp_router_id_set(bgp
, addr
,
406 (bgp
->router_id
.s_addr
417 void bgp_router_id_static_set(struct bgp
*bgp
, struct in_addr id
)
419 bgp
->router_id_static
= id
;
420 bgp_router_id_set(bgp
,
421 id
.s_addr
!= INADDR_ANY
? &id
: &bgp
->router_id_zebra
,
422 true /* is config */);
425 void bm_wait_for_fib_set(bool set
)
427 bool send_msg
= false;
429 if (bm
->wait_for_fib
== set
)
432 bm
->wait_for_fib
= set
;
434 if (bgp_suppress_fib_count
== 0)
436 bgp_suppress_fib_count
++;
438 bgp_suppress_fib_count
--;
439 if (bgp_suppress_fib_count
== 0)
443 if (send_msg
&& zclient
)
444 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
448 /* Set the suppress fib pending for the bgp configuration */
449 void bgp_suppress_fib_pending_set(struct bgp
*bgp
, bool set
)
451 bool send_msg
= false;
453 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
457 SET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
458 /* Send msg to zebra for the first instance of bgp enabled
461 if (bgp_suppress_fib_count
== 0)
463 bgp_suppress_fib_count
++;
465 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
466 bgp_suppress_fib_count
--;
468 /* Send msg to zebra if there are no instances enabled
471 if (bgp_suppress_fib_count
== 0)
474 /* Send route notify request to RIB */
476 if (BGP_DEBUG(zebra
, ZEBRA
))
477 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
480 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
485 /* BGP's cluster-id control. */
486 void bgp_cluster_id_set(struct bgp
*bgp
, struct in_addr
*cluster_id
)
489 struct listnode
*node
, *nnode
;
491 if (bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
)
492 && IPV4_ADDR_SAME(&bgp
->cluster_id
, cluster_id
))
495 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
496 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
498 /* Clear all IBGP peer. */
499 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
500 if (peer
->sort
!= BGP_PEER_IBGP
)
503 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
504 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
505 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
506 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
511 void bgp_cluster_id_unset(struct bgp
*bgp
)
514 struct listnode
*node
, *nnode
;
516 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
519 bgp
->cluster_id
.s_addr
= 0;
520 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
522 /* Clear all IBGP peer. */
523 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
524 if (peer
->sort
!= BGP_PEER_IBGP
)
527 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
528 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
529 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
530 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
535 /* BGP timer configuration. */
536 void bgp_timers_set(struct bgp
*bgp
, uint32_t keepalive
, uint32_t holdtime
,
537 uint32_t connect_retry
, uint32_t delayopen
)
539 bgp
->default_keepalive
=
540 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
541 bgp
->default_holdtime
= holdtime
;
542 bgp
->default_connect_retry
= connect_retry
;
543 bgp
->default_delayopen
= delayopen
;
546 /* mostly for completeness - CLI uses its own defaults */
547 void bgp_timers_unset(struct bgp
*bgp
)
549 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
550 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
551 bgp
->default_connect_retry
= BGP_DEFAULT_CONNECT_RETRY
;
552 bgp
->default_delayopen
= BGP_DEFAULT_DELAYOPEN
;
555 void bgp_tcp_keepalive_set(struct bgp
*bgp
, uint16_t keepalive_idle
,
556 uint16_t keepalive_intvl
, uint16_t keepalive_probes
)
558 bgp
->tcp_keepalive_idle
= keepalive_idle
;
559 bgp
->tcp_keepalive_intvl
= keepalive_intvl
;
560 bgp
->tcp_keepalive_probes
= keepalive_probes
;
563 void bgp_tcp_keepalive_unset(struct bgp
*bgp
)
565 bgp
->tcp_keepalive_idle
= 0;
566 bgp
->tcp_keepalive_intvl
= 0;
567 bgp
->tcp_keepalive_probes
= 0;
570 /* BGP confederation configuration. */
571 void bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
574 struct listnode
*node
, *nnode
;
580 /* Remember - were we doing confederation before? */
581 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
583 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
585 /* If we were doing confederation already, this is just an external
586 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
587 were not doing confederation before, reset all EBGP sessions. */
588 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
589 enum bgp_peer_sort ptype
= peer_sort(peer
);
591 /* We're looking for peers who's AS is not local or part of our
593 if (already_confed
) {
594 if (ptype
== BGP_PEER_EBGP
) {
596 if (BGP_IS_VALID_STATE_FOR_NOTIF(
599 PEER_DOWN_CONFED_ID_CHANGE
;
601 peer
, BGP_NOTIFY_CEASE
,
602 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
604 bgp_session_reset_safe(peer
, &nnode
);
607 /* Not doign confederation before, so reset every
610 if (ptype
!= BGP_PEER_IBGP
) {
611 /* Reset the local_as to be our EBGP one */
612 if (ptype
== BGP_PEER_EBGP
)
614 if (BGP_IS_VALID_STATE_FOR_NOTIF(
617 PEER_DOWN_CONFED_ID_CHANGE
;
619 peer
, BGP_NOTIFY_CEASE
,
620 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
622 bgp_session_reset_safe(peer
, &nnode
);
629 void bgp_confederation_id_unset(struct bgp
*bgp
)
632 struct listnode
*node
, *nnode
;
635 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
637 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
638 /* We're looking for peers who's AS is not local */
639 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
640 peer
->local_as
= bgp
->as
;
641 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
642 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
643 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
644 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
648 bgp_session_reset_safe(peer
, &nnode
);
653 /* Is an AS part of the confed or not? */
654 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
661 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
662 if (bgp
->confed_peers
[i
] == as
)
668 /* Add an AS to the confederation set. */
669 void bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
672 struct listnode
*node
, *nnode
;
677 if (bgp_confederation_peers_check(bgp
, as
))
681 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
682 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
684 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
685 bgp
->confed_peers_cnt
++;
687 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
688 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
689 if (peer
->as
== as
) {
690 peer
->local_as
= bgp
->as
;
691 (void)peer_sort(peer
);
692 if (BGP_IS_VALID_STATE_FOR_NOTIF(
695 PEER_DOWN_CONFED_PEER_CHANGE
;
697 peer
, BGP_NOTIFY_CEASE
,
698 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
700 bgp_session_reset_safe(peer
, &nnode
);
706 /* Delete an AS from the confederation set. */
707 void bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
712 struct listnode
*node
, *nnode
;
717 if (!bgp_confederation_peers_check(bgp
, as
))
720 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
721 if (bgp
->confed_peers
[i
] == as
)
722 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
723 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
725 bgp
->confed_peers_cnt
--;
727 if (bgp
->confed_peers_cnt
== 0) {
728 if (bgp
->confed_peers
)
729 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
730 bgp
->confed_peers
= NULL
;
733 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
734 bgp
->confed_peers_cnt
* sizeof(as_t
));
736 /* Now reset any peer who's remote AS has just been removed from the
738 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
739 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
740 if (peer
->as
== as
) {
741 peer
->local_as
= bgp
->confed_id
;
742 (void)peer_sort(peer
);
743 if (BGP_IS_VALID_STATE_FOR_NOTIF(
746 PEER_DOWN_CONFED_PEER_CHANGE
;
748 peer
, BGP_NOTIFY_CEASE
,
749 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
751 bgp_session_reset_safe(peer
, &nnode
);
757 /* Local preference configuration. */
758 void bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
763 bgp
->default_local_pref
= local_pref
;
766 void bgp_default_local_preference_unset(struct bgp
*bgp
)
771 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
774 /* Local preference configuration. */
775 void bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
,
781 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
784 void bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
788 bgp
->default_subgroup_pkt_queue_max
=
789 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
792 /* Listen limit configuration. */
793 void bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
798 bgp
->dynamic_neighbors_limit
= listen_limit
;
801 void bgp_listen_limit_unset(struct bgp
*bgp
)
806 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
809 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
810 afi_t
*afi
, safi_t
*safi
)
812 /* Map from IANA values to internal values, return error if
813 * values are unrecognized.
815 *afi
= afi_iana2int(pkt_afi
);
816 *safi
= safi_iana2int(pkt_safi
);
817 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
823 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
824 iana_safi_t
*pkt_safi
)
826 /* Map from internal values to IANA values, return error if
827 * internal values are bad (unexpected).
829 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
831 *pkt_afi
= afi_int2iana(afi
);
832 *pkt_safi
= safi_int2iana(safi
);
836 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
845 afid
= afindex(afi
, safi
);
846 if (afid
>= BGP_AF_MAX
)
850 assert(peer
->peer_af_array
[afid
] == NULL
);
852 /* Allocate new peer af */
853 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
855 peer
->peer_af_array
[afid
] = af
;
860 bgp
->af_peer_count
[afi
][safi
]++;
865 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
872 afid
= afindex(afi
, safi
);
873 if (afid
>= BGP_AF_MAX
)
876 return peer
->peer_af_array
[afid
];
879 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
888 afid
= afindex(afi
, safi
);
889 if (afid
>= BGP_AF_MAX
)
892 af
= peer
->peer_af_array
[afid
];
897 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
899 bgp_stop_announce_route_timer(af
);
901 if (PAF_SUBGRP(af
)) {
902 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
903 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
904 af
->subgroup
->update_group
->id
,
905 af
->subgroup
->id
, peer
->host
);
909 update_subgroup_remove_peer(af
->subgroup
, af
);
911 if (bgp
->af_peer_count
[afi
][safi
])
912 bgp
->af_peer_count
[afi
][safi
]--;
914 peer
->peer_af_array
[afid
] = NULL
;
915 XFREE(MTYPE_BGP_PEER_AF
, af
);
919 /* Peer comparison function for sorting. */
920 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
922 if (p1
->group
&& !p2
->group
)
925 if (!p1
->group
&& p2
->group
)
928 if (p1
->group
== p2
->group
) {
929 if (p1
->conf_if
&& !p2
->conf_if
)
932 if (!p1
->conf_if
&& p2
->conf_if
)
935 if (p1
->conf_if
&& p2
->conf_if
)
936 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
938 return strcmp(p1
->group
->name
, p2
->group
->name
);
940 return sockunion_cmp(&p1
->su
, &p2
->su
);
943 static unsigned int peer_hash_key_make(const void *p
)
945 const struct peer
*peer
= p
;
946 return sockunion_hash(&peer
->su
);
949 static bool peer_hash_same(const void *p1
, const void *p2
)
951 const struct peer
*peer1
= p1
;
952 const struct peer
*peer2
= p2
;
954 return (sockunion_same(&peer1
->su
, &peer2
->su
)
955 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
956 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
959 void peer_flag_inherit(struct peer
*peer
, uint64_t flag
)
963 /* Skip if peer is not a peer-group member. */
964 if (!peer_group_active(peer
))
967 /* Unset override flag to signal inheritance from peer-group. */
968 UNSET_FLAG(peer
->flags_override
, flag
);
971 * Inherit flag state from peer-group. If the flag of the peer-group is
972 * not being inverted, the peer must inherit the inverse of the current
973 * peer-group flag state.
975 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
976 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
977 && CHECK_FLAG(peer
->flags_invert
, flag
))
978 COND_FLAG(peer
->flags
, flag
, !group_val
);
980 COND_FLAG(peer
->flags
, flag
, group_val
);
983 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
985 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
988 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
993 /* Skip if peer is not a peer-group member. */
994 if (!peer_group_active(peer
))
997 /* Unset override flag to signal inheritance from peer-group. */
998 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
1001 * Inherit flag state from peer-group. If the flag of the peer-group is
1002 * not being inverted, the peer must inherit the inverse of the current
1003 * peer-group flag state.
1005 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
1006 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
1007 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
1008 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
1010 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
1013 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
1014 static inline enum bgp_peer_sort
peer_calc_sort(struct peer
*peer
)
1021 if (peer
->change_local_as
)
1022 local_as
= peer
->change_local_as
;
1024 local_as
= peer
->local_as
;
1027 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1028 if (peer
->as_type
== AS_INTERNAL
)
1029 return BGP_PEER_IBGP
;
1031 else if (peer
->as_type
== AS_EXTERNAL
)
1032 return BGP_PEER_EBGP
;
1034 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1036 return (local_as
== peer
->as
? BGP_PEER_IBGP
1043 assert(peer
->group
);
1044 peer1
= listnode_head(peer
->group
->peer
);
1049 return BGP_PEER_INTERNAL
;
1053 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1055 return BGP_PEER_INTERNAL
;
1057 if (local_as
== peer
->as
) {
1058 if (bgp
->as
== bgp
->confed_id
) {
1059 if (local_as
== bgp
->as
)
1060 return BGP_PEER_IBGP
;
1062 return BGP_PEER_EBGP
;
1064 if (local_as
== bgp
->confed_id
)
1065 return BGP_PEER_EBGP
;
1067 return BGP_PEER_IBGP
;
1071 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1072 return BGP_PEER_CONFED
;
1074 return BGP_PEER_EBGP
;
1076 if (peer
->as_type
== AS_UNSPECIFIED
) {
1077 /* check if in peer-group with AS information */
1079 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1080 if (peer
->group
->conf
->as_type
1082 if (local_as
== peer
->group
->conf
->as
)
1083 return BGP_PEER_IBGP
;
1085 return BGP_PEER_EBGP
;
1086 } else if (peer
->group
->conf
->as_type
1088 return BGP_PEER_IBGP
;
1090 return BGP_PEER_EBGP
;
1092 /* no AS information anywhere, let caller know */
1093 return BGP_PEER_UNSPECIFIED
;
1094 } else if (peer
->as_type
!= AS_SPECIFIED
)
1095 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1098 return (local_as
== 0 ? BGP_PEER_INTERNAL
1099 : local_as
== peer
->as
? BGP_PEER_IBGP
1104 /* Calculate and cache the peer "sort" */
1105 enum bgp_peer_sort
peer_sort(struct peer
*peer
)
1107 peer
->sort
= peer_calc_sort(peer
);
1111 enum bgp_peer_sort
peer_sort_lookup(struct peer
*peer
)
1116 static void peer_free(struct peer
*peer
)
1121 assert(peer
->status
== Deleted
);
1125 /* this /ought/ to have been done already through bgp_stop earlier,
1126 * but just to be sure..
1128 bgp_timer_set(peer
);
1129 bgp_reads_off(peer
);
1130 bgp_writes_off(peer
);
1131 thread_cancel_event_ready(bm
->master
, peer
);
1132 FOREACH_AFI_SAFI (afi
, safi
)
1133 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
1134 assert(!peer
->t_write
);
1135 assert(!peer
->t_read
);
1136 BGP_EVENT_FLUSH(peer
);
1138 pthread_mutex_destroy(&peer
->io_mtx
);
1140 /* Free connected nexthop, if present */
1141 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1142 && !peer_dynamic_neighbor(peer
))
1143 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1146 FOREACH_AFI_SAFI (afi
, safi
) {
1147 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1148 XFREE(MTYPE_BGP_FILTER_NAME
,
1149 peer
->filter
[afi
][safi
].advmap
.aname
);
1150 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1151 XFREE(MTYPE_BGP_FILTER_NAME
,
1152 peer
->filter
[afi
][safi
].advmap
.cname
);
1155 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1157 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1158 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1159 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
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
)
1899 enum bgp_peer_sort origtype
, newtype
;
1902 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1903 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1904 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1905 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1906 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1908 bgp_session_reset(peer
);
1910 origtype
= peer_sort_lookup(peer
);
1912 peer
->as_type
= as_specified
;
1914 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1915 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1916 && peer
->bgp
->as
!= as
)
1917 peer
->local_as
= peer
->bgp
->confed_id
;
1919 peer
->local_as
= peer
->bgp
->as
;
1921 newtype
= peer_sort(peer
);
1922 /* Advertisement-interval reset */
1923 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1924 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1925 ? BGP_DEFAULT_IBGP_ROUTEADV
1926 : BGP_DEFAULT_EBGP_ROUTEADV
;
1930 if (newtype
== BGP_PEER_IBGP
)
1932 else if (origtype
== BGP_PEER_IBGP
)
1933 peer
->ttl
= BGP_DEFAULT_TTL
;
1935 /* reflector-client reset */
1936 if (newtype
!= BGP_PEER_IBGP
) {
1937 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1938 PEER_FLAG_REFLECTOR_CLIENT
);
1939 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1940 PEER_FLAG_REFLECTOR_CLIENT
);
1941 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1942 PEER_FLAG_REFLECTOR_CLIENT
);
1943 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1944 PEER_FLAG_REFLECTOR_CLIENT
);
1945 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1946 PEER_FLAG_REFLECTOR_CLIENT
);
1947 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1948 PEER_FLAG_REFLECTOR_CLIENT
);
1949 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1950 PEER_FLAG_REFLECTOR_CLIENT
);
1951 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1952 PEER_FLAG_REFLECTOR_CLIENT
);
1953 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1954 PEER_FLAG_REFLECTOR_CLIENT
);
1955 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1956 PEER_FLAG_REFLECTOR_CLIENT
);
1957 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1958 PEER_FLAG_REFLECTOR_CLIENT
);
1959 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1960 PEER_FLAG_REFLECTOR_CLIENT
);
1961 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1962 PEER_FLAG_REFLECTOR_CLIENT
);
1966 /* If peer does not exist, create new one. If peer already exists,
1967 set AS number to the peer. */
1968 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1969 as_t
*as
, int as_type
)
1975 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1977 peer
= peer_lookup(bgp
, su
);
1980 /* Not allowed for a dynamic peer. */
1981 if (peer_dynamic_neighbor(peer
)) {
1983 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1986 /* When this peer is a member of peer-group. */
1988 /* peer-group already has AS number/internal/external */
1989 if (peer
->group
->conf
->as
1990 || peer
->group
->conf
->as_type
) {
1991 /* Return peer group's AS number. */
1992 *as
= peer
->group
->conf
->as
;
1993 return BGP_ERR_PEER_GROUP_MEMBER
;
1996 enum bgp_peer_sort peer_sort_type
=
1997 peer_sort(peer
->group
->conf
);
1999 /* Explicit AS numbers used, compare AS numbers */
2000 if (as_type
== AS_SPECIFIED
) {
2001 if (((peer_sort_type
== BGP_PEER_IBGP
)
2002 && (bgp
->as
!= *as
))
2003 || ((peer_sort_type
== BGP_PEER_EBGP
)
2004 && (bgp
->as
== *as
))) {
2006 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2009 /* internal/external used, compare as-types */
2010 if (((peer_sort_type
== BGP_PEER_IBGP
)
2011 && (as_type
!= AS_INTERNAL
))
2012 || ((peer_sort_type
== BGP_PEER_EBGP
)
2013 && (as_type
!= AS_EXTERNAL
))) {
2015 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2020 /* Existing peer's AS number change. */
2021 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2022 || (peer
->as_type
!= as_type
))
2023 peer_as_change(peer
, *as
, as_type
);
2026 return BGP_ERR_NO_INTERFACE_CONFIG
;
2028 /* If the peer is not part of our confederation, and its not an
2029 iBGP peer then spoof the source AS */
2030 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
2031 && !bgp_confederation_peers_check(bgp
, *as
)
2033 local_as
= bgp
->confed_id
;
2037 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
,
2044 const char *bgp_get_name_by_role(uint8_t role
)
2049 case ROLE_RS_SERVER
:
2051 case ROLE_RS_CLIENT
:
2057 case ROLE_UNDEFINED
:
2063 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
2064 struct peer
*peer
, afi_t afi
,
2068 int out
= FILTER_OUT
;
2070 uint64_t pflags_ovrd
;
2071 uint8_t *pfilter_ovrd
;
2075 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2076 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2078 /* peer af_flags apply */
2079 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2080 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2081 ^ peer
->af_flags_invert
[afi
][safi
];
2082 flags_tmp
&= ~pflags_ovrd
;
2084 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2085 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2086 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2087 conf
->af_flags_invert
[afi
][safi
]);
2089 /* maximum-prefix */
2090 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2091 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2092 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2093 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2096 /* maximum-prefix-out */
2097 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX_OUT
))
2098 PEER_ATTR_INHERIT(peer
, group
, pmax_out
[afi
][safi
]);
2101 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2102 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2105 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_SOO
))
2106 PEER_ATTR_INHERIT(peer
, group
, soo
[afi
][safi
]);
2109 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2110 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2112 /* default-originate route-map */
2113 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2114 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2115 MTYPE_ROUTE_MAP_NAME
);
2116 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2119 /* inbound filter apply */
2120 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2121 PEER_STR_ATTR_INHERIT(peer
, group
,
2122 filter
[afi
][safi
].dlist
[in
].name
,
2123 MTYPE_BGP_FILTER_NAME
);
2124 PEER_ATTR_INHERIT(peer
, group
,
2125 filter
[afi
][safi
].dlist
[in
].alist
);
2128 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2129 PEER_STR_ATTR_INHERIT(peer
, group
,
2130 filter
[afi
][safi
].plist
[in
].name
,
2131 MTYPE_BGP_FILTER_NAME
);
2132 PEER_ATTR_INHERIT(peer
, group
,
2133 filter
[afi
][safi
].plist
[in
].plist
);
2136 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2137 PEER_STR_ATTR_INHERIT(peer
, group
,
2138 filter
[afi
][safi
].aslist
[in
].name
,
2139 MTYPE_BGP_FILTER_NAME
);
2140 PEER_ATTR_INHERIT(peer
, group
,
2141 filter
[afi
][safi
].aslist
[in
].aslist
);
2144 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2145 PEER_STR_ATTR_INHERIT(peer
, group
,
2146 filter
[afi
][safi
].map
[in
].name
,
2147 MTYPE_BGP_FILTER_NAME
);
2148 PEER_ATTR_INHERIT(peer
, group
,
2149 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2152 /* outbound filter apply */
2153 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2154 PEER_STR_ATTR_INHERIT(peer
, group
,
2155 filter
[afi
][safi
].dlist
[out
].name
,
2156 MTYPE_BGP_FILTER_NAME
);
2157 PEER_ATTR_INHERIT(peer
, group
,
2158 filter
[afi
][safi
].dlist
[out
].alist
);
2161 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2162 PEER_STR_ATTR_INHERIT(peer
, group
,
2163 filter
[afi
][safi
].plist
[out
].name
,
2164 MTYPE_BGP_FILTER_NAME
);
2165 PEER_ATTR_INHERIT(peer
, group
,
2166 filter
[afi
][safi
].plist
[out
].plist
);
2169 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2170 PEER_STR_ATTR_INHERIT(peer
, group
,
2171 filter
[afi
][safi
].aslist
[out
].name
,
2172 MTYPE_BGP_FILTER_NAME
);
2173 PEER_ATTR_INHERIT(peer
, group
,
2174 filter
[afi
][safi
].aslist
[out
].aslist
);
2177 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2178 PEER_STR_ATTR_INHERIT(peer
, group
,
2179 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2180 MTYPE_BGP_FILTER_NAME
);
2181 PEER_ATTR_INHERIT(peer
, group
,
2182 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2185 /* nondirectional filter apply */
2186 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2187 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2188 MTYPE_BGP_FILTER_NAME
);
2189 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2192 /* Conditional Advertisements */
2193 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ADVERTISE_MAP
)) {
2194 PEER_STR_ATTR_INHERIT(peer
, group
,
2195 filter
[afi
][safi
].advmap
.aname
,
2196 MTYPE_BGP_FILTER_NAME
);
2197 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.amap
);
2198 PEER_STR_ATTR_INHERIT(peer
, group
,
2199 filter
[afi
][safi
].advmap
.cname
,
2200 MTYPE_BGP_FILTER_NAME
);
2201 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.cmap
);
2202 PEER_ATTR_INHERIT(peer
, group
,
2203 filter
[afi
][safi
].advmap
.condition
);
2206 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2207 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2208 bgp_addpath_type_changed(conf
->bgp
);
2212 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2217 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2218 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2219 __func__
, peer
->host
);
2223 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2225 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2226 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2227 return BGP_ERR_PEER_SAFI_CONFLICT
;
2229 /* Nothing to do if we've already activated this peer */
2230 if (peer
->afc
[afi
][safi
])
2233 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2236 active
= peer_active(peer
);
2237 peer
->afc
[afi
][safi
] = 1;
2240 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2242 if (!active
&& peer_active(peer
)) {
2243 bgp_timer_set(peer
);
2245 if (peer_established(peer
)) {
2246 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2247 peer
->afc_adv
[afi
][safi
] = 1;
2248 bgp_capability_send(peer
, afi
, safi
,
2250 CAPABILITY_ACTION_SET
);
2251 if (peer
->afc_recv
[afi
][safi
]) {
2252 peer
->afc_nego
[afi
][safi
] = 1;
2253 bgp_announce_route(peer
, afi
, safi
,
2257 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2258 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2259 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2262 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2263 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2264 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2265 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2268 * If we are turning on a AFI/SAFI locally and we've
2269 * started bringing a peer up, we need to tell
2270 * the other peer to restart because we might loose
2271 * configuration here because when the doppelganger
2272 * gets to a established state due to how
2273 * we resolve we could just overwrite the afi/safi
2276 other
= peer
->doppelganger
;
2278 && (other
->status
== OpenSent
2279 || other
->status
== OpenConfirm
)) {
2280 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2281 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2282 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2289 /* Activate the peer or peer group for specified AFI and SAFI. */
2290 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2293 struct peer_group
*group
;
2294 struct listnode
*node
, *nnode
;
2295 struct peer
*tmp_peer
;
2298 /* Nothing to do if we've already activated this peer */
2299 if (peer
->afc
[afi
][safi
])
2304 /* This is a peer-group so activate all of the members of the
2305 * peer-group as well */
2306 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2308 /* Do not activate a peer for both SAFI_UNICAST and
2309 * SAFI_LABELED_UNICAST */
2310 if ((safi
== SAFI_UNICAST
2311 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2312 || (safi
== SAFI_LABELED_UNICAST
2313 && peer
->afc
[afi
][SAFI_UNICAST
]))
2314 return BGP_ERR_PEER_SAFI_CONFLICT
;
2316 peer
->afc
[afi
][safi
] = 1;
2317 group
= peer
->group
;
2319 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2320 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2323 ret
|= peer_activate_af(peer
, afi
, safi
);
2326 /* If this is the first peer to be activated for this
2327 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2328 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2329 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2331 if (BGP_DEBUG(zebra
, ZEBRA
))
2333 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2335 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2336 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2339 if (safi
== SAFI_FLOWSPEC
) {
2340 /* connect to table manager */
2341 bgp_zebra_init_tm_connect(bgp
);
2346 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2349 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2350 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2351 __func__
, peer
->host
);
2355 /* Nothing to do if we've already deactivated this peer */
2356 if (!peer
->afc
[afi
][safi
])
2359 /* De-activate the address family configuration. */
2360 peer
->afc
[afi
][safi
] = 0;
2362 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2363 flog_err(EC_BGP_PEER_DELETE
,
2364 "couldn't delete af structure for peer %s(%s, %s)",
2365 peer
->host
, afi2str(afi
), safi2str(safi
));
2369 if (peer_established(peer
)) {
2370 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2371 peer
->afc_adv
[afi
][safi
] = 0;
2372 peer
->afc_nego
[afi
][safi
] = 0;
2374 if (peer_active_nego(peer
)) {
2375 bgp_capability_send(peer
, afi
, safi
,
2377 CAPABILITY_ACTION_UNSET
);
2378 bgp_clear_route(peer
, afi
, safi
);
2379 peer
->pcount
[afi
][safi
] = 0;
2381 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2382 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2383 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2386 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2387 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2388 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2395 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2398 struct peer_group
*group
;
2399 struct peer
*tmp_peer
;
2400 struct listnode
*node
, *nnode
;
2403 /* Nothing to do if we've already de-activated this peer */
2404 if (!peer
->afc
[afi
][safi
])
2407 /* This is a peer-group so de-activate all of the members of the
2408 * peer-group as well */
2409 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2410 peer
->afc
[afi
][safi
] = 0;
2411 group
= peer
->group
;
2413 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2414 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2417 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2422 /* If this is the last peer to be deactivated for this
2423 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2424 if (safi
== SAFI_LABELED_UNICAST
2425 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2426 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2428 if (BGP_DEBUG(zebra
, ZEBRA
))
2430 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2432 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2433 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2438 void peer_nsf_stop(struct peer
*peer
)
2443 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2444 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2446 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2447 peer
->nsf
[afi
][safi
] = 0;
2448 THREAD_OFF(peer
->t_llgr_stale
[afi
][safi
]);
2451 if (peer
->t_gr_restart
) {
2452 THREAD_OFF(peer
->t_gr_restart
);
2453 if (bgp_debug_neighbor_events(peer
))
2454 zlog_debug("%pBP graceful restart timer stopped", peer
);
2456 if (peer
->t_gr_stale
) {
2457 THREAD_OFF(peer
->t_gr_stale
);
2458 if (bgp_debug_neighbor_events(peer
))
2460 "%pBP graceful restart stalepath timer stopped",
2463 bgp_clear_route_all(peer
);
2466 /* Delete peer from confguration.
2468 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2469 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2471 * This function /should/ take care to be idempotent, to guard against
2472 * it being called multiple times through stray events that come in
2473 * that happen to result in this function being called again. That
2474 * said, getting here for a "Deleted" peer is a bug in the neighbour
2477 int peer_delete(struct peer
*peer
)
2483 struct bgp_filter
*filter
;
2484 struct listnode
*pn
;
2487 assert(peer
->status
!= Deleted
);
2490 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2492 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2494 bgp_keepalives_off(peer
);
2495 bgp_reads_off(peer
);
2496 bgp_writes_off(peer
);
2497 thread_cancel_event_ready(bm
->master
, peer
);
2498 FOREACH_AFI_SAFI (afi
, safi
)
2499 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
2500 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2501 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2502 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2504 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2505 peer_nsf_stop(peer
);
2507 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2509 /* Remove BFD settings. */
2510 if (peer
->bfd_config
)
2511 bgp_peer_remove_bfd_config(peer
);
2513 /* If this peer belongs to peer group, clear up the
2516 if (peer_dynamic_neighbor(peer
))
2517 peer_drop_dynamic_neighbor(peer
);
2519 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2521 peer
); /* group->peer list reference */
2522 list_delete_node(peer
->group
->peer
, pn
);
2527 /* Withdraw all information from routing table. We can not use
2528 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2529 * executed after peer structure is deleted.
2531 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2533 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2535 if (peer
->doppelganger
) {
2536 peer
->doppelganger
->doppelganger
= NULL
;
2537 peer
->doppelganger
= NULL
;
2540 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2541 bgp_fsm_change_status(peer
, Deleted
);
2543 /* Remove from NHT */
2544 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2545 bgp_unlink_nexthop_by_peer(peer
);
2547 /* Password configuration */
2548 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2549 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2550 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2551 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2552 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2553 bgp_md5_unset(peer
);
2556 bgp_timer_set(peer
); /* stops all timers for Deleted */
2558 /* Delete from all peer list. */
2559 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2560 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2562 * Removing from the list node first because
2563 * peer_unlock *can* call peer_delete( I know,
2564 * I know ). So let's remove it and in
2565 * the su recalculate function we'll ensure
2566 * it's in there or not.
2568 list_delete_node(bgp
->peer
, pn
);
2569 hash_release(bgp
->peerhash
, peer
);
2570 peer_unlock(peer
); /* bgp peer list reference */
2575 stream_fifo_free(peer
->ibuf
);
2580 stream_fifo_free(peer
->obuf
);
2584 if (peer
->ibuf_work
) {
2585 ringbuf_del(peer
->ibuf_work
);
2586 peer
->ibuf_work
= NULL
;
2589 if (peer
->obuf_work
) {
2590 stream_free(peer
->obuf_work
);
2591 peer
->obuf_work
= NULL
;
2594 if (peer
->scratch
) {
2595 stream_free(peer
->scratch
);
2596 peer
->scratch
= NULL
;
2599 /* Local and remote addresses. */
2600 if (peer
->su_local
) {
2601 sockunion_free(peer
->su_local
);
2602 peer
->su_local
= NULL
;
2605 if (peer
->su_remote
) {
2606 sockunion_free(peer
->su_remote
);
2607 peer
->su_remote
= NULL
;
2610 /* Free filter related memory. */
2611 FOREACH_AFI_SAFI (afi
, safi
) {
2612 filter
= &peer
->filter
[afi
][safi
];
2614 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2615 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2616 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2617 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2620 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2621 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2624 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2625 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2626 ecommunity_free(&peer
->soo
[afi
][safi
]);
2629 FOREACH_AFI_SAFI (afi
, safi
)
2630 peer_af_delete(peer
, afi
, safi
);
2632 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2633 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2635 peer_unlock(peer
); /* initial reference */
2640 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2642 return strcmp(g1
->name
, g2
->name
);
2645 /* Peer group cofiguration. */
2646 static struct peer_group
*peer_group_new(void)
2648 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2651 static void peer_group_free(struct peer_group
*group
)
2653 XFREE(MTYPE_PEER_GROUP
, group
);
2656 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2658 struct peer_group
*group
;
2659 struct listnode
*node
, *nnode
;
2661 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2662 if (strcmp(group
->name
, name
) == 0)
2668 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2670 struct peer_group
*group
;
2674 group
= peer_group_lookup(bgp
, name
);
2678 group
= peer_group_new();
2680 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2681 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2682 group
->peer
= list_new();
2683 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2684 group
->listen_range
[afi
] = list_new();
2685 group
->conf
= peer_new(bgp
);
2686 FOREACH_AFI_SAFI (afi
, safi
) {
2687 if (bgp
->default_af
[afi
][safi
])
2688 group
->conf
->afc
[afi
][safi
] = 1;
2690 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2691 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2692 group
->conf
->group
= group
;
2693 group
->conf
->as
= 0;
2694 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2695 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2696 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2697 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2698 listnode_add_sort(bgp
->group
, group
);
2703 static void peer_group2peer_config_copy(struct peer_group
*group
,
2708 bool config_node
= !!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2714 peer
->as
= conf
->as
;
2717 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2718 peer
->change_local_as
= conf
->change_local_as
;
2720 /* If peer-group has configured TTL then override it */
2721 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2722 peer
->ttl
= conf
->ttl
;
2725 peer
->gtsm_hops
= conf
->gtsm_hops
;
2727 /* peer flags apply */
2728 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2729 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2730 flags_tmp
&= ~peer
->flags_override
;
2732 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2733 SET_FLAG(peer
->flags
, flags_tmp
);
2734 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2737 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2739 /* peer timers apply */
2740 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2741 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2742 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2745 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2746 PEER_ATTR_INHERIT(peer
, group
, connect
);
2747 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2748 peer
->v_connect
= conf
->connect
;
2750 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2753 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2754 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2755 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2756 peer
->v_delayopen
= conf
->delayopen
;
2758 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2761 /* advertisement-interval apply */
2762 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2763 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2764 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2765 peer
->v_routeadv
= conf
->routeadv
;
2767 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2768 ? BGP_DEFAULT_IBGP_ROUTEADV
2769 : BGP_DEFAULT_EBGP_ROUTEADV
;
2772 /* capability extended-nexthop apply */
2773 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2774 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2775 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2777 /* password apply */
2778 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2779 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2780 MTYPE_PEER_PASSWORD
);
2782 if (!BGP_PEER_SU_UNSPEC(peer
))
2785 /* update-source apply */
2786 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2787 if (conf
->update_source
) {
2788 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2789 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2790 } else if (conf
->update_if
) {
2791 sockunion_free(peer
->update_source
);
2792 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2793 MTYPE_PEER_UPDATE_SOURCE
);
2798 PEER_ATTR_INHERIT(peer
, group
, local_role
);
2800 /* Update GR flags for the peer. */
2801 bgp_peer_gr_flags_update(peer
);
2803 /* Apply BFD settings from group to peer if it exists. */
2804 if (conf
->bfd_config
) {
2805 bgp_peer_configure_bfd(peer
, false);
2806 bgp_peer_config_apply(peer
, group
);
2810 /* Peer group's remote AS configuration. */
2811 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2814 struct peer_group
*group
;
2816 struct listnode
*node
, *nnode
;
2818 group
= peer_group_lookup(bgp
, group_name
);
2822 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2826 /* When we setup peer-group AS number all peer group member's AS
2827 number must be updated to same number. */
2828 peer_as_change(group
->conf
, *as
, as_type
);
2830 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2831 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2832 || (peer
->as_type
!= as_type
))
2833 peer_as_change(peer
, *as
, as_type
);
2839 void peer_notify_unconfig(struct peer
*peer
)
2841 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2842 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2843 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2846 static void peer_notify_shutdown(struct peer
*peer
)
2848 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
2849 if (bgp_debug_neighbor_events(peer
))
2851 "%pBP configured Graceful-Restart, skipping shutdown notification",
2856 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2857 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2858 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
2861 void peer_group_notify_unconfig(struct peer_group
*group
)
2863 struct peer
*peer
, *other
;
2864 struct listnode
*node
, *nnode
;
2866 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2867 other
= peer
->doppelganger
;
2868 if (other
&& other
->status
!= Deleted
) {
2869 other
->group
= NULL
;
2870 peer_notify_unconfig(other
);
2872 peer_notify_unconfig(peer
);
2876 int peer_group_delete(struct peer_group
*group
)
2880 struct prefix
*prefix
;
2882 struct listnode
*node
, *nnode
;
2887 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2888 other
= peer
->doppelganger
;
2890 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2891 bgp_zebra_terminate_radv(bgp
, peer
);
2894 if (other
&& other
->status
!= Deleted
) {
2895 other
->group
= NULL
;
2899 list_delete(&group
->peer
);
2901 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2902 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2904 prefix_free(&prefix
);
2906 list_delete(&group
->listen_range
[afi
]);
2909 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2912 if (group
->conf
->bfd_config
)
2913 bgp_peer_remove_bfd_config(group
->conf
);
2915 group
->conf
->group
= NULL
;
2916 peer_delete(group
->conf
);
2918 /* Delete from all peer_group list. */
2919 listnode_delete(bgp
->group
, group
);
2921 peer_group_free(group
);
2926 int peer_group_remote_as_delete(struct peer_group
*group
)
2928 struct peer
*peer
, *other
;
2929 struct listnode
*node
, *nnode
;
2931 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2932 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2935 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2936 other
= peer
->doppelganger
;
2938 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2939 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2943 if (other
&& other
->status
!= Deleted
) {
2944 other
->group
= NULL
;
2948 list_delete_all_node(group
->peer
);
2950 group
->conf
->as
= 0;
2951 group
->conf
->as_type
= AS_UNSPECIFIED
;
2956 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2958 struct prefix
*prefix
;
2959 struct listnode
*node
, *nnode
;
2962 afi
= family2afi(range
->family
);
2964 /* Group needs remote AS configured. */
2965 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2966 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2968 /* Ensure no duplicates. Currently we don't care about overlaps. */
2969 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2970 if (prefix_same(range
, prefix
))
2974 prefix
= prefix_new();
2975 prefix_copy(prefix
, range
);
2976 listnode_add(group
->listen_range
[afi
], prefix
);
2978 /* Update passwords for new ranges */
2979 if (group
->conf
->password
)
2980 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
2985 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2987 struct prefix
*prefix
, prefix2
;
2988 struct listnode
*node
, *nnode
;
2992 afi
= family2afi(range
->family
);
2994 /* Identify the listen range. */
2995 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2996 if (prefix_same(range
, prefix
))
3001 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
3003 /* Dispose off any dynamic neighbors that exist due to this listen range
3005 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
3006 if (!peer_dynamic_neighbor(peer
))
3009 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
3010 && prefix_match(prefix
, &prefix2
)) {
3011 if (bgp_debug_neighbor_events(peer
))
3013 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
3014 peer
->host
, group
->name
, prefix
);
3019 /* Get rid of the listen range */
3020 listnode_delete(group
->listen_range
[afi
], prefix
);
3022 /* Remove passwords for deleted ranges */
3023 if (group
->conf
->password
)
3024 bgp_md5_unset_prefix(group
->bgp
, prefix
);
3029 /* Bind specified peer to peer group. */
3030 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
3031 struct peer_group
*group
, as_t
*as
)
3033 int first_member
= 0;
3036 enum bgp_peer_sort ptype
, gtype
;
3038 /* Lookup the peer. */
3040 peer
= peer_lookup(bgp
, su
);
3042 /* The peer exist, bind it to the peer-group */
3044 /* When the peer already belongs to a peer-group, check the
3046 if (peer_group_active(peer
)) {
3048 /* The peer is already bound to the peer-group,
3051 if (strcmp(peer
->group
->name
, group
->name
) == 0)
3054 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
3057 /* The peer has not specified a remote-as, inherit it from the
3059 if (peer
->as_type
== AS_UNSPECIFIED
) {
3060 peer
->as_type
= group
->conf
->as_type
;
3061 peer
->as
= group
->conf
->as
;
3062 peer
->sort
= group
->conf
->sort
;
3065 ptype
= peer_sort(peer
);
3066 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
3067 gtype
= peer_sort(group
->conf
);
3068 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
3071 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
3074 if (gtype
== BGP_PEER_INTERNAL
)
3078 peer_group2peer_config_copy(group
, peer
);
3080 FOREACH_AFI_SAFI (afi
, safi
) {
3081 if (group
->conf
->afc
[afi
][safi
]) {
3082 peer
->afc
[afi
][safi
] = 1;
3084 if (peer_af_find(peer
, afi
, safi
)
3085 || peer_af_create(peer
, afi
, safi
)) {
3086 peer_group2peer_config_copy_af(
3087 group
, peer
, afi
, safi
);
3089 } else if (peer
->afc
[afi
][safi
])
3090 peer_deactivate(peer
, afi
, safi
);
3094 assert(group
&& peer
->group
== group
);
3096 listnode_delete(bgp
->peer
, peer
);
3098 peer
->group
= group
;
3099 listnode_add_sort(bgp
->peer
, peer
);
3101 peer
= peer_lock(peer
); /* group->peer list reference */
3102 listnode_add(group
->peer
, peer
);
3106 gtype
= peer_sort(group
->conf
);
3107 /* Advertisement-interval reset */
3108 if (!CHECK_FLAG(group
->conf
->flags
,
3109 PEER_FLAG_ROUTEADV
)) {
3110 group
->conf
->v_routeadv
=
3111 (gtype
== BGP_PEER_IBGP
)
3112 ? BGP_DEFAULT_IBGP_ROUTEADV
3113 : BGP_DEFAULT_EBGP_ROUTEADV
;
3116 /* ebgp-multihop reset */
3117 if (gtype
== BGP_PEER_IBGP
)
3118 group
->conf
->ttl
= MAXTTL
;
3121 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3123 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3124 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3125 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3126 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3128 bgp_session_reset(peer
);
3132 /* Create a new peer. */
3134 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3135 && (!group
->conf
->as
)) {
3136 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3139 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3140 group
->conf
->as_type
, group
, true);
3142 peer
= peer_lock(peer
); /* group->peer list reference */
3143 listnode_add(group
->peer
, peer
);
3145 peer_group2peer_config_copy(group
, peer
);
3147 /* If the peer-group is active for this afi/safi then activate
3149 FOREACH_AFI_SAFI (afi
, safi
) {
3150 if (group
->conf
->afc
[afi
][safi
]) {
3151 peer
->afc
[afi
][safi
] = 1;
3153 if (!peer_af_find(peer
, afi
, safi
))
3154 peer_af_create(peer
, afi
, safi
);
3156 peer_group2peer_config_copy_af(group
, peer
, afi
,
3158 } else if (peer
->afc
[afi
][safi
])
3159 peer_deactivate(peer
, afi
, safi
);
3162 /* Set up peer's events and timers. */
3163 if (peer_active(peer
))
3164 bgp_timer_set(peer
);
3170 static void bgp_startup_timer_expire(struct thread
*thread
)
3174 bgp
= THREAD_ARG(thread
);
3175 bgp
->t_startup
= NULL
;
3179 * On shutdown we call the cleanup function which
3180 * does a free of the link list nodes, free up
3181 * the data we are pointing at too.
3183 static void bgp_vrf_string_name_delete(void *data
)
3187 XFREE(MTYPE_TMP
, vname
);
3190 /* BGP instance creation by `router bgp' commands. */
3191 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3192 enum bgp_instance_type inst_type
)
3198 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3200 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3201 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3202 zlog_debug("Creating Default VRF, AS %u", *as
);
3204 zlog_debug("Creating %s %s, AS %u",
3205 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3211 /* Default the EVPN VRF to the default one */
3212 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3219 bgp
->allow_martian
= false;
3220 bgp_process_queue_init(bgp
);
3221 bgp
->heuristic_coalesce
= true;
3222 bgp
->inst_type
= inst_type
;
3223 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3225 bgp
->peer_self
= peer_new(bgp
);
3226 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3227 bgp
->peer_self
->host
=
3228 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3229 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3230 if (cmd_hostname_get())
3231 bgp
->peer_self
->hostname
=
3232 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3234 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3235 if (cmd_domainname_get())
3236 bgp
->peer_self
->domainname
=
3237 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3238 bgp
->peer
= list_new();
3239 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3240 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3242 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3244 bgp
->group
= list_new();
3245 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3247 FOREACH_AFI_SAFI (afi
, safi
) {
3248 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3249 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3250 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3252 /* Enable maximum-paths */
3253 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3255 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3257 /* Initialize graceful restart info */
3258 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3259 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3260 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3261 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3262 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3265 bgp
->v_update_delay
= bm
->v_update_delay
;
3266 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3267 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3268 bgp
->default_subgroup_pkt_queue_max
=
3269 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3270 bgp_tcp_keepalive_unset(bgp
);
3271 bgp_timers_unset(bgp
);
3272 bgp
->default_min_holdtime
= 0;
3273 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3274 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3275 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3276 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3277 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3278 bgp
->dynamic_neighbors_count
= 0;
3279 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3280 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3281 bgp
->reject_as_sets
= false;
3282 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3283 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3284 bgp
->fast_convergence
= false;
3286 bgp
->llgr_stale_time
= BGP_DEFAULT_LLGR_STALE_TIME
;
3288 #ifdef ENABLE_BGP_VNC
3289 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3290 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3292 assert(bgp
->rfapi_cfg
);
3294 #endif /* ENABLE_BGP_VNC */
3296 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3297 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3298 bgp
->vpn_policy
[afi
].afi
= afi
;
3299 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3300 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3303 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3304 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3305 bgp_vrf_string_name_delete
;
3306 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3307 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3308 bgp_vrf_string_name_delete
;
3309 SET_FLAG(bgp
->af_flags
[afi
][SAFI_MPLS_VPN
],
3310 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL
);
3313 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3315 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3316 bgp
->restart_time
, &bgp
->t_startup
);
3318 /* printable name we can use in debug messages */
3319 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3320 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3330 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3332 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3333 snprintf(bgp
->name_pretty
, len
, "%s %s",
3334 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3340 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3341 memory_order_relaxed
);
3342 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3343 memory_order_relaxed
);
3344 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3345 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3349 update_bgp_group_init(bgp
);
3351 /* assign a unique rd id for auto derivation of vrf's RD */
3352 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3354 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3355 sizeof(struct bgp_evpn_info
));
3357 bgp_evpn_vrf_es_init(bgp
);
3361 /*initilize global GR FSM */
3362 bgp_global_gr_init(bgp
);
3364 memset(&bgp
->ebgprequirespolicywarning
, 0,
3365 sizeof(bgp
->ebgprequirespolicywarning
));
3370 /* Return the "default VRF" instance of BGP. */
3371 struct bgp
*bgp_get_default(void)
3374 struct listnode
*node
, *nnode
;
3376 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3377 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3382 /* Lookup BGP entry. */
3383 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3386 struct listnode
*node
, *nnode
;
3388 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3390 && ((bgp
->name
== NULL
&& name
== NULL
)
3391 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3396 /* Lookup BGP structure by view name. */
3397 struct bgp
*bgp_lookup_by_name(const char *name
)
3400 struct listnode
*node
, *nnode
;
3402 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3403 if ((bgp
->name
== NULL
&& name
== NULL
)
3404 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3409 /* Lookup BGP instance based on VRF id. */
3410 /* Note: Only to be used for incoming messages from Zebra. */
3411 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3415 /* Lookup VRF (in tree) and follow link. */
3416 vrf
= vrf_lookup_by_id(vrf_id
);
3419 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3422 /* Sets the BGP instance where EVPN is enabled */
3423 void bgp_set_evpn(struct bgp
*bgp
)
3425 if (bm
->bgp_evpn
== bgp
)
3428 /* First, release the reference count we hold on the instance */
3430 bgp_unlock(bm
->bgp_evpn
);
3434 /* Increase the reference count on this new VRF */
3436 bgp_lock(bm
->bgp_evpn
);
3439 /* Returns the BGP instance where EVPN is enabled, if any */
3440 struct bgp
*bgp_get_evpn(void)
3442 return bm
->bgp_evpn
;
3445 /* handle socket creation or deletion, if necessary
3446 * this is called for all new BGP instances
3448 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3451 struct listnode
*node
;
3454 /* Create BGP server socket, if listen mode not disabled */
3455 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3457 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3459 * suppress vrf socket
3462 bgp_close_vrf_socket(bgp
);
3466 return BGP_ERR_INVALID_VALUE
;
3468 * if vrf_id did not change
3470 if (vrf
->vrf_id
== old_vrf_id
)
3472 if (old_vrf_id
!= VRF_UNKNOWN
) {
3473 /* look for old socket. close it. */
3474 bgp_close_vrf_socket(bgp
);
3476 /* if backend is not yet identified ( VRF_UNKNOWN) then
3477 * creation will be done later
3479 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3481 if (list_isempty(bm
->addresses
)) {
3482 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3483 return BGP_ERR_INVALID_VALUE
;
3485 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3486 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3487 return BGP_ERR_INVALID_VALUE
;
3491 return bgp_check_main_socket(create
, bgp
);
3494 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3495 enum bgp_instance_type inst_type
)
3499 /* Multiple instance check. */
3501 bgp
= bgp_lookup_by_name(name
);
3503 bgp
= bgp_get_default();
3507 if (bgp
->as
!= *as
) {
3509 return BGP_ERR_AS_MISMATCH
;
3511 if (bgp
->inst_type
!= inst_type
)
3512 return BGP_ERR_INSTANCE_MISMATCH
;
3520 /* Called from VTY commands. */
3521 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3522 enum bgp_instance_type inst_type
)
3525 struct vrf
*vrf
= NULL
;
3528 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3529 if (ret
|| *bgp_val
)
3532 bgp
= bgp_create(as
, name
, inst_type
);
3535 * view instances will never work inside of a vrf
3536 * as such they must always be in the VRF_DEFAULT
3537 * Also we must set this to something useful because
3538 * of the vrf socket code needing an actual useful
3539 * default value to send to the underlying OS.
3541 * This code is currently ignoring vrf based
3542 * code using the -Z option( and that is probably
3543 * best addressed elsewhere in the code )
3545 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3546 bgp
->vrf_id
= VRF_DEFAULT
;
3548 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3549 bgp_address_init(bgp
);
3550 bgp_tip_hash_init(bgp
);
3554 bgp
->t_rmap_def_originate_eval
= NULL
;
3556 /* If Default instance or VRF, link to the VRF structure, if present. */
3557 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3558 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3559 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3561 bgp_vrf_link(bgp
, vrf
);
3563 /* BGP server socket already processed if BGP instance
3564 * already part of the list
3566 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3567 listnode_add(bm
->bgp
, bgp
);
3569 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3570 if (BGP_DEBUG(zebra
, ZEBRA
))
3571 zlog_debug("%s: Registering BGP instance %s to zebra",
3572 __func__
, bgp
->name_pretty
);
3573 bgp_zebra_instance_register(bgp
);
3579 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3580 vrf_id_t vrf_id
, bool set
)
3584 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3587 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3591 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3593 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3597 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3601 struct list
*red_list
;
3602 struct listnode
*node
;
3603 struct bgp_redist
*red
;
3605 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3606 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3608 red_list
= bgp
->redist
[afi
][i
];
3612 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3613 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3620 * Make BGP instance "up". Applies only to VRFs (non-default) and
3621 * implies the VRF has been learnt from Zebra.
3623 void bgp_instance_up(struct bgp
*bgp
)
3626 struct listnode
*node
, *next
;
3628 bgp_set_redist_vrf_bitmaps(bgp
, true);
3630 /* Register with zebra. */
3631 bgp_zebra_instance_register(bgp
);
3633 /* Kick off any peers that may have been configured. */
3634 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3635 if (!BGP_PEER_START_SUPPRESSED(peer
))
3636 BGP_EVENT_ADD(peer
, BGP_Start
);
3639 /* Process any networks that have been configured. */
3640 bgp_static_add(bgp
);
3644 * Make BGP instance "down". Applies only to VRFs (non-default) and
3645 * implies the VRF has been deleted by Zebra.
3647 void bgp_instance_down(struct bgp
*bgp
)
3650 struct listnode
*node
;
3651 struct listnode
*next
;
3654 if (bgp
->t_rmap_def_originate_eval
) {
3655 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3656 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3660 /* Bring down peers, so corresponding routes are purged. */
3661 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3662 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3663 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3664 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3666 bgp_session_reset(peer
);
3669 /* Purge network and redistributed routes. */
3670 bgp_purge_static_redist_routes(bgp
);
3672 /* Cleanup registered nexthops (flags) */
3673 bgp_cleanup_nexthops(bgp
);
3675 bgp_zebra_instance_deregister(bgp
);
3677 bgp_set_redist_vrf_bitmaps(bgp
, false);
3680 /* Delete BGP instance. */
3681 int bgp_delete(struct bgp
*bgp
)
3684 struct peer_group
*group
;
3685 struct listnode
*node
, *next
;
3690 struct graceful_restart_info
*gr_info
;
3694 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3696 /* make sure we withdraw any exported routes */
3697 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3699 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3702 bgp_vpn_leak_unimport(bgp
);
3704 hook_call(bgp_inst_delete
, bgp
);
3706 FOREACH_AFI_SAFI (afi
, safi
)
3707 THREAD_OFF(bgp
->t_revalidate
[afi
][safi
]);
3709 THREAD_OFF(bgp
->t_condition_check
);
3710 THREAD_OFF(bgp
->t_startup
);
3711 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3712 THREAD_OFF(bgp
->t_update_delay
);
3713 THREAD_OFF(bgp
->t_establish_wait
);
3715 /* Set flag indicating bgp instance delete in progress */
3716 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3718 /* Delete the graceful restart info */
3719 FOREACH_AFI_SAFI (afi
, safi
) {
3722 gr_info
= &bgp
->gr_info
[afi
][safi
];
3725 t
= gr_info
->t_select_deferral
;
3727 void *info
= THREAD_ARG(t
);
3729 XFREE(MTYPE_TMP
, info
);
3731 THREAD_OFF(gr_info
->t_select_deferral
);
3733 t
= gr_info
->t_route_select
;
3735 void *info
= THREAD_ARG(t
);
3737 XFREE(MTYPE_TMP
, info
);
3739 THREAD_OFF(gr_info
->t_route_select
);
3742 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3743 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3744 zlog_debug("Deleting Default VRF");
3746 zlog_debug("Deleting %s %s",
3747 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3753 /* unmap from RT list */
3754 bgp_evpn_vrf_delete(bgp
);
3756 /* unmap bgp vrf label */
3757 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3758 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3761 if (bgp
->t_rmap_def_originate_eval
) {
3762 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3763 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3767 /* Inform peers we're going down. */
3768 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3769 peer_notify_shutdown(peer
);
3771 /* Delete static routes (networks). */
3772 bgp_static_delete(bgp
);
3774 /* Unset redistribution. */
3775 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3776 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3777 if (i
!= ZEBRA_ROUTE_BGP
)
3778 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3780 /* Free peers and peer-groups. */
3781 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3782 peer_group_delete(group
);
3784 while (listcount(bgp
->peer
)) {
3785 peer
= listnode_head(bgp
->peer
);
3789 if (bgp
->peer_self
) {
3790 peer_delete(bgp
->peer_self
);
3791 bgp
->peer_self
= NULL
;
3794 update_bgp_group_free(bgp
);
3796 /* TODO - Other memory may need to be freed - e.g., NHT */
3798 #ifdef ENABLE_BGP_VNC
3801 bgp_cleanup_routes(bgp
);
3803 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3804 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3807 &bgp
->vpn_policy
[afi
]
3808 .import_redirect_rtlist
);
3809 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3812 /* Free any memory allocated to holding routemap references */
3813 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3814 for (enum vpn_policy_direction dir
= 0;
3815 dir
< BGP_VPN_POLICY_DIR_MAX
; ++dir
) {
3816 if (bgp
->vpn_policy
[afi
].rmap_name
[dir
])
3817 XFREE(MTYPE_ROUTE_MAP_NAME
,
3818 bgp
->vpn_policy
[afi
].rmap_name
[dir
]);
3819 bgp
->vpn_policy
[afi
].rmap
[dir
] = NULL
;
3823 /* Deregister from Zebra, if needed */
3824 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3825 if (BGP_DEBUG(zebra
, ZEBRA
))
3827 "%s: deregistering this bgp %s instance from zebra",
3828 __func__
, bgp
->name
);
3829 bgp_zebra_instance_deregister(bgp
);
3832 /* Remove visibility via the master list - there may however still be
3833 * routes to be processed still referencing the struct bgp.
3835 listnode_delete(bm
->bgp
, bgp
);
3837 /* Free interfaces in this instance. */
3840 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3841 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3843 bgp_vrf_unlink(bgp
, vrf
);
3845 /* Update EVPN VRF pointer */
3846 if (bm
->bgp_evpn
== bgp
) {
3847 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3850 bgp_set_evpn(bgp_get_default());
3853 if (bgp
->process_queue
)
3854 work_queue_free_and_null(&bgp
->process_queue
);
3856 thread_master_free_unused(bm
->master
);
3857 bgp_unlock(bgp
); /* initial reference */
3862 void bgp_free(struct bgp
*bgp
)
3866 struct bgp_table
*table
;
3867 struct bgp_dest
*dest
;
3868 struct bgp_rmap
*rmap
;
3872 list_delete(&bgp
->group
);
3873 list_delete(&bgp
->peer
);
3875 if (bgp
->peerhash
) {
3876 hash_free(bgp
->peerhash
);
3877 bgp
->peerhash
= NULL
;
3880 FOREACH_AFI_SAFI (afi
, safi
) {
3881 /* Special handling for 2-level routing tables. */
3882 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3883 || safi
== SAFI_EVPN
) {
3884 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3885 dest
= bgp_route_next(dest
)) {
3886 table
= bgp_dest_get_bgp_table_info(dest
);
3887 bgp_table_finish(&table
);
3890 if (bgp
->route
[afi
][safi
])
3891 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3892 if (bgp
->aggregate
[afi
][safi
])
3893 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3894 if (bgp
->rib
[afi
][safi
])
3895 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3896 rmap
= &bgp
->table_map
[afi
][safi
];
3897 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3900 bgp_scan_finish(bgp
);
3901 bgp_address_destroy(bgp
);
3902 bgp_tip_hash_destroy(bgp
);
3904 /* release the auto RD id */
3905 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3907 bgp_evpn_cleanup(bgp
);
3908 bgp_pbr_cleanup(bgp
);
3909 bgp_srv6_cleanup(bgp
);
3910 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3912 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3913 enum vpn_policy_direction dir
;
3915 if (bgp
->vpn_policy
[afi
].import_vrf
)
3916 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3917 if (bgp
->vpn_policy
[afi
].export_vrf
)
3918 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3920 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3921 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3922 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3923 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3924 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3925 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3928 XFREE(MTYPE_BGP
, bgp
->name
);
3929 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3930 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3932 XFREE(MTYPE_BGP
, bgp
);
3935 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3938 struct listnode
*node
, *nnode
;
3944 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3945 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3946 && !CHECK_FLAG(peer
->sflags
,
3947 PEER_STATUS_ACCEPT_PEER
))
3949 } else if (bm
->bgp
!= NULL
) {
3950 struct listnode
*bgpnode
, *nbgpnode
;
3952 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3953 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3955 && !strcmp(peer
->conf_if
, conf_if
)
3956 && !CHECK_FLAG(peer
->sflags
,
3957 PEER_STATUS_ACCEPT_PEER
))
3963 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3966 struct listnode
*node
, *nnode
;
3972 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3973 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3974 && !CHECK_FLAG(peer
->sflags
,
3975 PEER_STATUS_ACCEPT_PEER
))
3977 } else if (bm
->bgp
!= NULL
) {
3978 struct listnode
*bgpnode
, *nbgpnode
;
3980 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3981 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3983 && !strcmp(peer
->hostname
, hostname
)
3984 && !CHECK_FLAG(peer
->sflags
,
3985 PEER_STATUS_ACCEPT_PEER
))
3991 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
3993 struct peer
*peer
= NULL
;
3994 struct peer tmp_peer
;
3996 memset(&tmp_peer
, 0, sizeof(struct peer
));
3999 * We do not want to find the doppelganger peer so search for the peer
4001 * the hash that has PEER_FLAG_CONFIG_NODE
4003 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
4008 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4009 } else if (bm
->bgp
!= NULL
) {
4010 struct listnode
*bgpnode
, *nbgpnode
;
4012 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
4013 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4022 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
4023 union sockunion
*su
,
4024 struct peer_group
*group
)
4030 /* Create peer first; we've already checked group config is valid. */
4031 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
4032 group
->conf
->as_type
, group
, true);
4037 peer
= peer_lock(peer
);
4038 listnode_add(group
->peer
, peer
);
4040 peer_group2peer_config_copy(group
, peer
);
4043 * Bind peer for all AFs configured for the group. We don't call
4044 * peer_group_bind as that is sub-optimal and does some stuff we don't
4047 FOREACH_AFI_SAFI (afi
, safi
) {
4048 if (!group
->conf
->afc
[afi
][safi
])
4050 peer
->afc
[afi
][safi
] = 1;
4052 if (!peer_af_find(peer
, afi
, safi
))
4053 peer_af_create(peer
, afi
, safi
);
4055 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
4058 /* Mark as dynamic, but also as a "config node" for other things to
4060 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
4066 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
4067 struct prefix
*prefix
)
4069 struct listnode
*node
, *nnode
;
4070 struct prefix
*range
;
4073 afi
= family2afi(prefix
->family
);
4075 if (group
->listen_range
[afi
])
4076 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
4078 if (prefix_match(range
, prefix
))
4085 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
4086 struct prefix
**listen_range
)
4088 struct prefix
*range
= NULL
;
4089 struct peer_group
*group
= NULL
;
4090 struct listnode
*node
, *nnode
;
4092 *listen_range
= NULL
;
4094 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4095 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4098 } else if (bm
->bgp
!= NULL
) {
4099 struct listnode
*bgpnode
, *nbgpnode
;
4101 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4102 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4103 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4109 *listen_range
= range
;
4110 return (group
&& range
) ? group
: NULL
;
4113 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
4115 struct peer_group
*group
;
4118 struct prefix prefix
;
4119 struct prefix
*listen_range
;
4122 if (!sockunion2hostprefix(su
, &prefix
))
4125 /* See if incoming connection matches a configured listen range. */
4126 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
4137 if (bgp_debug_neighbor_events(NULL
))
4139 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4140 &prefix
, group
->name
, listen_range
);
4142 /* Are we within the listen limit? */
4143 dncount
= gbgp
->dynamic_neighbors_count
;
4145 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4146 if (bgp_debug_neighbor_events(NULL
))
4148 "Dynamic Neighbor %pFX rejected - at limit %d",
4149 &prefix
, gbgp
->dynamic_neighbors_limit
);
4153 /* Ensure group is not disabled. */
4154 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4155 if (bgp_debug_neighbor_events(NULL
))
4157 "Dynamic Neighbor %pFX rejected - group %s disabled",
4158 &prefix
, group
->name
);
4162 /* Check that at least one AF is activated for the group. */
4163 if (!peer_group_af_configured(group
)) {
4164 if (bgp_debug_neighbor_events(NULL
))
4166 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4167 &prefix
, group
->name
);
4171 /* Create dynamic peer and bind to associated group. */
4172 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4175 gbgp
->dynamic_neighbors_count
= ++dncount
;
4177 if (bgp_debug_neighbor_events(peer
))
4178 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4179 peer
->host
, group
->name
, dncount
);
4184 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4187 if (peer
->group
->bgp
) {
4188 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4190 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4192 if (bgp_debug_neighbor_events(peer
))
4193 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4194 peer
->group
->name
, dncount
);
4197 bool bgp_path_attribute_discard(struct peer
*peer
, char *buf
, size_t size
)
4204 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4205 if (peer
->discard_attrs
[i
])
4206 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4207 (strlen(buf
) > 0) ? " " : "", i
);
4210 if (strlen(buf
) > 0)
4216 bool bgp_path_attribute_treat_as_withdraw(struct peer
*peer
, char *buf
,
4224 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4225 if (peer
->withdraw_attrs
[i
])
4226 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4227 (strlen(buf
) > 0) ? " " : "", i
);
4230 if (strlen(buf
) > 0)
4236 /* If peer is configured at least one address family return 1. */
4237 bool peer_active(struct peer
*peer
)
4239 if (BGP_PEER_SU_UNSPEC(peer
))
4241 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4242 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4243 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4244 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4245 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4246 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4247 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4248 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4249 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4250 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4251 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4256 /* If peer is negotiated at least one address family return 1. */
4257 bool peer_active_nego(struct peer
*peer
)
4259 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4260 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4261 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4262 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4263 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4264 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4265 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4266 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4267 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4268 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4269 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4270 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4271 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4276 /* If peer received at least one address family MP, return true */
4277 bool peer_afc_received(struct peer
*peer
)
4282 FOREACH_AFI_SAFI (afi
, safi
)
4283 if (peer
->afc_recv
[afi
][safi
])
4289 /* If peer advertised at least one address family MP, return true */
4290 bool peer_afc_advertised(struct peer
*peer
)
4295 FOREACH_AFI_SAFI (afi
, safi
)
4296 if (peer
->afc_adv
[afi
][safi
])
4302 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4303 enum peer_change_type type
)
4305 struct peer_af
*paf
;
4307 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4310 if (!peer_established(peer
))
4313 if (type
== peer_change_reset
) {
4314 /* If we're resetting session, we've to delete both peer struct
4316 if ((peer
->doppelganger
)
4317 && (peer
->doppelganger
->status
!= Deleted
)
4318 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4319 PEER_FLAG_CONFIG_NODE
)))
4320 peer_delete(peer
->doppelganger
);
4322 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4323 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4324 } else if (type
== peer_change_reset_in
) {
4325 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4326 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4327 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4328 BGP_ROUTE_REFRESH_NORMAL
);
4330 if ((peer
->doppelganger
)
4331 && (peer
->doppelganger
->status
!= Deleted
)
4332 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4333 PEER_FLAG_CONFIG_NODE
)))
4334 peer_delete(peer
->doppelganger
);
4336 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4337 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4339 } else if (type
== peer_change_reset_out
) {
4340 paf
= peer_af_find(peer
, afi
, safi
);
4341 if (paf
&& paf
->subgroup
)
4342 SET_FLAG(paf
->subgroup
->sflags
,
4343 SUBGRP_STATUS_FORCE_UPDATES
);
4345 update_group_adjust_peer(paf
);
4346 bgp_announce_route(peer
, afi
, safi
, false);
4350 struct peer_flag_action
{
4354 /* This flag can be set for peer-group member. */
4355 uint8_t not_for_member
;
4357 /* Action when the flag is changed. */
4358 enum peer_change_type type
;
4361 static const struct peer_flag_action peer_flag_action_list
[] = {
4362 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4363 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4364 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4365 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4366 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4367 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4368 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4369 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4370 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4371 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4372 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4373 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4374 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4375 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4376 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4377 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4378 {PEER_FLAG_LOCAL_AS
, 0, peer_change_reset
},
4379 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_reset
},
4380 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_reset
},
4381 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4382 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4383 {PEER_FLAG_EXTENDED_OPT_PARAMS
, 0, peer_change_reset
},
4384 {PEER_FLAG_ROLE_STRICT_MODE
, 0, peer_change_reset
},
4385 {PEER_FLAG_ROLE
, 0, peer_change_reset
},
4386 {PEER_FLAG_PORT
, 0, peer_change_reset
},
4387 {PEER_FLAG_AIGP
, 0, peer_change_none
},
4388 {PEER_FLAG_GRACEFUL_SHUTDOWN
, 0, peer_change_none
},
4391 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4392 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4393 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4394 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4395 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4396 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4397 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4398 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4399 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4400 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4401 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4402 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4403 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4404 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4405 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4406 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4407 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4408 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4409 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4410 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4411 {PEER_FLAG_MAX_PREFIX_OUT
, 0, peer_change_none
},
4412 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4413 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4414 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4415 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4416 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4417 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4418 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4419 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4420 {PEER_FLAG_SOO
, 0, peer_change_reset
},
4421 {PEER_FLAG_ACCEPT_OWN
, 0, peer_change_reset
},
4424 /* Proper action set. */
4425 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4426 int size
, struct peer_flag_action
*action
,
4433 const struct peer_flag_action
*match
= NULL
;
4435 /* Check peer's frag action. */
4436 for (i
= 0; i
< size
; i
++) {
4437 match
= &action_list
[i
];
4439 if (match
->flag
== 0)
4442 if (match
->flag
& flag
) {
4445 if (match
->type
== peer_change_reset_in
)
4447 if (match
->type
== peer_change_reset_out
)
4449 if (match
->type
== peer_change_reset
) {
4453 if (match
->not_for_member
)
4454 action
->not_for_member
= 1;
4458 /* Set peer clear type. */
4459 if (reset_in
&& reset_out
)
4460 action
->type
= peer_change_reset
;
4462 action
->type
= peer_change_reset_in
;
4464 action
->type
= peer_change_reset_out
;
4466 action
->type
= peer_change_none
;
4471 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4473 if (flag
== PEER_FLAG_SHUTDOWN
) {
4474 if (CHECK_FLAG(peer
->flags
, flag
)) {
4475 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4476 peer_nsf_stop(peer
);
4478 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4480 if (peer
->t_pmax_restart
) {
4481 THREAD_OFF(peer
->t_pmax_restart
);
4482 if (bgp_debug_neighbor_events(peer
))
4484 "%pBP Maximum-prefix restart timer canceled",
4488 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4489 char *msg
= peer
->tx_shutdown_message
;
4491 uint8_t msgbuf
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4493 if (!msg
&& peer_group_active(peer
))
4494 msg
= peer
->group
->conf
4495 ->tx_shutdown_message
;
4496 msglen
= msg
? strlen(msg
) : 0;
4497 if (msglen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4498 msglen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4502 memcpy(msgbuf
+ 1, msg
, msglen
);
4504 bgp_notify_send_with_data(
4505 peer
, BGP_NOTIFY_CEASE
,
4506 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4507 msgbuf
, msglen
+ 1);
4510 peer
, BGP_NOTIFY_CEASE
,
4511 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4513 bgp_session_reset(peer
);
4515 peer
->v_start
= BGP_INIT_START_TIMER
;
4516 BGP_EVENT_ADD(peer
, BGP_Stop
);
4518 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4519 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4520 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4521 else if (flag
== PEER_FLAG_PASSIVE
)
4522 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4523 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4524 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4526 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4527 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4529 bgp_session_reset(peer
);
4532 /* Enable global administrative shutdown of all peers of BGP instance */
4533 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4536 struct listnode
*node
;
4537 /* length(1) + message(N) */
4538 uint8_t data
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4540 /* do nothing if already shut down */
4541 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4544 /* informational log message */
4545 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4548 /* iterate through peers of BGP instance */
4549 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4550 /* continue, if peer is already in administrative shutdown. */
4551 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4554 /* send a RFC 4486 notification message if necessary */
4555 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4557 size_t datalen
= strlen(msg
);
4559 if (datalen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4560 datalen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4563 memcpy(data
+ 1, msg
, datalen
);
4565 bgp_notify_send_with_data(
4566 peer
, BGP_NOTIFY_CEASE
,
4567 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
, data
,
4571 peer
, BGP_NOTIFY_CEASE
,
4572 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4576 /* reset start timer to initial value */
4577 peer
->v_start
= BGP_INIT_START_TIMER
;
4579 /* trigger a RFC 4271 ManualStop event */
4580 BGP_EVENT_ADD(peer
, BGP_Stop
);
4583 /* set the BGP instances shutdown flag */
4584 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4587 /* Disable global administrative shutdown of all peers of BGP instance */
4588 void bgp_shutdown_disable(struct bgp
*bgp
)
4590 /* do nothing if not shut down. */
4591 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4594 /* informational log message */
4595 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4598 /* clear the BGP instances shutdown flag */
4599 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4602 /* Change specified peer flag. */
4603 static int peer_flag_modify(struct peer
*peer
, uint64_t flag
, int set
)
4607 bool invert
, member_invert
;
4608 struct peer
*member
;
4609 struct listnode
*node
, *nnode
;
4610 struct peer_flag_action action
;
4612 memset(&action
, 0, sizeof(struct peer_flag_action
));
4613 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4615 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4616 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4619 /* Abort if no flag action exists. */
4621 return BGP_ERR_INVALID_FLAG
;
4623 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4624 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4625 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4626 return BGP_ERR_PEER_FLAG_CONFLICT
;
4628 /* Handle flag updates where desired state matches current state. */
4629 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4630 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4631 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4635 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4636 COND_FLAG(peer
->flags_override
, flag
, invert
);
4641 /* Inherit from peer-group or set/unset flags accordingly. */
4642 if (peer_group_active(peer
) && set
== invert
)
4643 peer_flag_inherit(peer
, flag
);
4645 COND_FLAG(peer
->flags
, flag
, set
);
4647 /* Check if handling a regular peer. */
4648 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4649 /* Update flag override state accordingly. */
4650 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4653 * For the extended next-hop encoding flag we need to turn RAs
4654 * on if flag is being set, but only turn RAs off if the flag
4655 * is being unset on this peer and if this peer is a member of a
4656 * peer-group, the peer-group also doesn't have the flag set.
4658 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4660 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4661 } else if (peer_group_active(peer
)) {
4662 if (!CHECK_FLAG(peer
->group
->conf
->flags
,
4665 bgp_zebra_terminate_radv(peer
->bgp
,
4668 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4671 /* Execute flag action on peer. */
4672 if (action
.type
== peer_change_reset
)
4673 peer_flag_modify_action(peer
, flag
);
4675 /* Skip peer-group mechanics for regular peers. */
4680 * Update peer-group members, unless they are explicitly overriding
4681 * peer-group configuration.
4683 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4684 /* Skip peers with overridden configuration. */
4685 if (CHECK_FLAG(member
->flags_override
, flag
))
4688 /* Check if only member without group is inverted. */
4690 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4692 /* Skip peers with equivalent configuration. */
4693 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4696 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4699 /* Update flag on peer-group member. */
4700 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4702 if (flag
== PEER_FLAG_CAPABILITY_ENHE
&& !member
->conf_if
)
4703 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4704 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4706 /* Execute flag action on peer-group member. */
4707 if (action
.type
== peer_change_reset
)
4708 peer_flag_modify_action(member
, flag
);
4714 int peer_flag_set(struct peer
*peer
, uint64_t flag
)
4716 return peer_flag_modify(peer
, flag
, 1);
4719 int peer_flag_unset(struct peer
*peer
, uint64_t flag
)
4721 return peer_flag_modify(peer
, flag
, 0);
4724 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4725 uint64_t flag
, bool set
)
4729 bool invert
, member_invert
;
4730 struct peer
*member
;
4731 struct listnode
*node
, *nnode
;
4732 struct peer_flag_action action
;
4733 enum bgp_peer_sort ptype
;
4735 memset(&action
, 0, sizeof(struct peer_flag_action
));
4736 size
= sizeof(peer_af_flag_action_list
)
4737 / sizeof(struct peer_flag_action
);
4739 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4740 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4743 /* Abort if flag action exists. */
4745 return BGP_ERR_INVALID_FLAG
;
4747 ptype
= peer_sort(peer
);
4748 /* Special check for reflector client. */
4749 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4750 return BGP_ERR_NOT_INTERNAL_PEER
;
4752 /* Special check for remove-private-AS. */
4753 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4754 return BGP_ERR_REMOVE_PRIVATE_AS
;
4756 /* as-override is not allowed for IBGP peers */
4757 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4758 return BGP_ERR_AS_OVERRIDE
;
4760 /* Handle flag updates where desired state matches current state. */
4761 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4762 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4763 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4768 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4769 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4776 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4777 * if we are setting/unsetting flags which conflict with this flag
4778 * handle accordingly
4780 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4784 * if we are setting NEXTHOP_SELF, we need to unset the
4785 * NEXTHOP_UNCHANGED flag
4787 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4788 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4789 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4790 PEER_FLAG_NEXTHOP_UNCHANGED
);
4794 * if we are unsetting NEXTHOP_SELF, we need to set the
4795 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4797 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4798 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4799 SET_FLAG(peer
->af_flags
[afi
][safi
],
4800 PEER_FLAG_NEXTHOP_UNCHANGED
);
4805 * If the peer is a route server client let's not
4806 * muck with the nexthop on the way out the door
4808 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4810 SET_FLAG(peer
->af_flags
[afi
][safi
],
4811 PEER_FLAG_NEXTHOP_UNCHANGED
);
4813 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4814 PEER_FLAG_NEXTHOP_UNCHANGED
);
4817 /* Inherit from peer-group or set/unset flags accordingly. */
4818 if (peer_group_active(peer
) && set
== invert
)
4819 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4821 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4823 /* Execute action when peer is established. */
4824 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4825 && peer_established(peer
)) {
4826 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4827 bgp_clear_adj_in(peer
, afi
, safi
);
4829 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4830 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4831 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4832 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4833 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4834 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4835 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4836 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4838 peer_change_action(peer
, afi
, safi
, action
.type
);
4842 /* Check if handling a regular peer. */
4843 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4844 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4848 * Update peer-group members, unless they are explicitly
4849 * overriding peer-group configuration.
4851 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4853 /* Skip peers with overridden configuration. */
4854 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4858 /* Check if only member without group is inverted. */
4860 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4864 /* Skip peers with equivalent configuration. */
4865 if (set
!= member_invert
4866 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4869 if (set
== member_invert
4870 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4873 /* Update flag on peer-group member. */
4874 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4875 set
!= member_invert
);
4877 /* Execute flag action on peer-group member. */
4878 if (peer_established(member
)) {
4879 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4880 bgp_clear_adj_in(member
, afi
, safi
);
4882 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4883 member
->last_reset
=
4884 PEER_DOWN_RR_CLIENT_CHANGE
;
4886 == PEER_FLAG_RSERVER_CLIENT
)
4887 member
->last_reset
=
4888 PEER_DOWN_RS_CLIENT_CHANGE
;
4890 == PEER_FLAG_ORF_PREFIX_SM
)
4891 member
->last_reset
=
4892 PEER_DOWN_CAPABILITY_CHANGE
;
4894 == PEER_FLAG_ORF_PREFIX_RM
)
4895 member
->last_reset
=
4896 PEER_DOWN_CAPABILITY_CHANGE
;
4898 peer_change_action(member
, afi
, safi
,
4908 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4910 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4913 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4915 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4919 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4921 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4922 peer
->tx_shutdown_message
=
4923 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4926 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4928 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4932 /* EBGP multihop configuration. */
4933 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4935 struct peer_group
*group
;
4936 struct listnode
*node
, *nnode
;
4939 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4942 /* is there anything to do? */
4943 if (peer
->ttl
== ttl
)
4946 /* see comment in peer_ttl_security_hops_set() */
4947 if (ttl
!= MAXTTL
) {
4948 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4949 group
= peer
->group
;
4950 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4951 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4953 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4955 if (peer1
->sort
== BGP_PEER_IBGP
)
4958 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4959 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4962 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4963 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4969 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4970 if (peer
->sort
!= BGP_PEER_IBGP
) {
4971 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4972 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4973 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4975 bgp_session_reset(peer
);
4977 /* Reconfigure BFD peer with new TTL. */
4978 if (peer
->bfd_config
)
4979 bgp_peer_bfd_update_source(peer
);
4982 group
= peer
->group
;
4983 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4984 if (peer
->sort
== BGP_PEER_IBGP
)
4987 peer
->ttl
= group
->conf
->ttl
;
4989 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4990 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4991 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4993 bgp_session_reset(peer
);
4995 /* Reconfigure BFD peer with new TTL. */
4996 if (peer
->bfd_config
)
4997 bgp_peer_bfd_update_source(peer
);
5003 int peer_ebgp_multihop_unset(struct peer
*peer
)
5005 struct peer_group
*group
;
5006 struct listnode
*node
, *nnode
;
5009 if (peer
->sort
== BGP_PEER_IBGP
)
5012 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
5013 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
5015 if (peer_group_active(peer
))
5016 ttl
= peer
->group
->conf
->ttl
;
5018 ttl
= BGP_DEFAULT_TTL
;
5020 if (ttl
== peer
->ttl
)
5025 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5026 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5027 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5028 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5030 bgp_session_reset(peer
);
5032 /* Reconfigure BFD peer with new TTL. */
5033 if (peer
->bfd_config
)
5034 bgp_peer_bfd_update_source(peer
);
5036 group
= peer
->group
;
5037 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5038 if (peer
->sort
== BGP_PEER_IBGP
)
5041 peer
->ttl
= BGP_DEFAULT_TTL
;
5043 if (peer
->fd
>= 0) {
5044 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5046 peer
, BGP_NOTIFY_CEASE
,
5047 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5049 bgp_session_reset(peer
);
5052 /* Reconfigure BFD peer with new TTL. */
5053 if (peer
->bfd_config
)
5054 bgp_peer_bfd_update_source(peer
);
5060 /* Set Open Policy Role and check its correctness */
5061 int peer_role_set(struct peer
*peer
, uint8_t role
, bool strict_mode
)
5063 struct peer
*member
;
5064 struct listnode
*node
, *nnode
;
5066 peer_flag_set(peer
, PEER_FLAG_ROLE
);
5068 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5069 if (peer
->sort
!= BGP_PEER_EBGP
)
5070 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5072 if (peer
->local_role
== role
) {
5073 if (CHECK_FLAG(peer
->flags
,
5074 PEER_FLAG_ROLE_STRICT_MODE
) &&
5076 /* TODO: Is session restart needed if it was
5079 UNSET_FLAG(peer
->flags
,
5080 PEER_FLAG_ROLE_STRICT_MODE
);
5081 if (!CHECK_FLAG(peer
->flags
,
5082 PEER_FLAG_ROLE_STRICT_MODE
) &&
5084 SET_FLAG(peer
->flags
,
5085 PEER_FLAG_ROLE_STRICT_MODE
);
5086 /* Restart session to throw Role Mismatch
5089 if (peer
->remote_role
== ROLE_UNDEFINED
)
5090 bgp_session_reset(peer
);
5093 peer
->local_role
= role
;
5095 SET_FLAG(peer
->flags
,
5096 PEER_FLAG_ROLE_STRICT_MODE
);
5098 UNSET_FLAG(peer
->flags
,
5099 PEER_FLAG_ROLE_STRICT_MODE
);
5100 bgp_session_reset(peer
);
5106 peer
->local_role
= role
;
5107 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5108 if (member
->sort
!= BGP_PEER_EBGP
)
5109 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5111 if (member
->local_role
== role
) {
5112 if (CHECK_FLAG(member
->flags
,
5113 PEER_FLAG_ROLE_STRICT_MODE
) &&
5115 /* TODO: Is session restart needed if it was
5118 UNSET_FLAG(member
->flags
,
5119 PEER_FLAG_ROLE_STRICT_MODE
);
5120 if (!CHECK_FLAG(member
->flags
,
5121 PEER_FLAG_ROLE_STRICT_MODE
) &&
5123 SET_FLAG(peer
->flags
,
5124 PEER_FLAG_ROLE_STRICT_MODE
);
5125 SET_FLAG(member
->flags
,
5126 PEER_FLAG_ROLE_STRICT_MODE
);
5127 /* Restart session to throw Role Mismatch
5130 if (member
->remote_role
== ROLE_UNDEFINED
)
5131 bgp_session_reset(member
);
5134 member
->local_role
= role
;
5137 SET_FLAG(peer
->flags
,
5138 PEER_FLAG_ROLE_STRICT_MODE
);
5139 SET_FLAG(member
->flags
,
5140 PEER_FLAG_ROLE_STRICT_MODE
);
5142 UNSET_FLAG(member
->flags
,
5143 PEER_FLAG_ROLE_STRICT_MODE
);
5145 bgp_session_reset(member
);
5152 int peer_role_unset(struct peer
*peer
)
5154 struct peer
*member
;
5155 struct listnode
*node
, *nnode
;
5157 peer_flag_unset(peer
, PEER_FLAG_ROLE
);
5159 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5160 return peer_role_set(peer
, ROLE_UNDEFINED
, 0);
5162 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
))
5163 peer_role_set(member
, ROLE_UNDEFINED
, 0);
5168 /* Neighbor description. */
5169 void peer_description_set(struct peer
*peer
, const char *desc
)
5171 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5173 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
5176 void peer_description_unset(struct peer
*peer
)
5178 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5181 /* Neighbor update-source. */
5182 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
5184 struct peer
*member
;
5185 struct listnode
*node
, *nnode
;
5187 /* Set flag and configuration on peer. */
5188 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5189 if (peer
->update_if
) {
5190 if (strcmp(peer
->update_if
, ifname
) == 0)
5192 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5194 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5195 sockunion_free(peer
->update_source
);
5196 peer
->update_source
= NULL
;
5198 /* Check if handling a regular peer. */
5199 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5200 /* Send notification or reset peer depending on state. */
5201 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5202 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5203 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5204 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5206 bgp_session_reset(peer
);
5208 /* Apply new source configuration to BFD session. */
5209 if (peer
->bfd_config
)
5210 bgp_peer_bfd_update_source(peer
);
5212 /* Skip peer-group mechanics for regular peers. */
5217 * Set flag and configuration on all peer-group members, unless they are
5218 * explicitly overriding peer-group configuration.
5220 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5221 /* Skip peers with overridden configuration. */
5222 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5225 /* Skip peers with the same configuration. */
5226 if (member
->update_if
) {
5227 if (strcmp(member
->update_if
, ifname
) == 0)
5229 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5232 /* Set flag and configuration on peer-group member. */
5233 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5234 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5235 sockunion_free(member
->update_source
);
5236 member
->update_source
= NULL
;
5238 /* Send notification or reset peer depending on state. */
5239 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5240 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5241 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5242 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5244 bgp_session_reset(member
);
5246 /* Apply new source configuration to BFD session. */
5247 if (member
->bfd_config
)
5248 bgp_peer_bfd_update_source(member
);
5254 void peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
5256 struct peer
*member
;
5257 struct listnode
*node
, *nnode
;
5259 /* Set flag and configuration on peer. */
5260 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5261 if (peer
->update_source
) {
5262 if (sockunion_cmp(peer
->update_source
, su
) == 0)
5264 sockunion_free(peer
->update_source
);
5266 peer
->update_source
= sockunion_dup(su
);
5267 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5269 /* Check if handling a regular peer. */
5270 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5271 /* Send notification or reset peer depending on state. */
5272 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5273 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5274 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5275 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5277 bgp_session_reset(peer
);
5279 /* Apply new source configuration to BFD session. */
5280 if (peer
->bfd_config
)
5281 bgp_peer_bfd_update_source(peer
);
5283 /* Skip peer-group mechanics for regular peers. */
5288 * Set flag and configuration on all peer-group members, unless they are
5289 * explicitly overriding peer-group configuration.
5291 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5292 /* Skip peers with overridden configuration. */
5293 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5296 /* Skip peers with the same configuration. */
5297 if (member
->update_source
) {
5298 if (sockunion_cmp(member
->update_source
, su
) == 0)
5300 sockunion_free(member
->update_source
);
5303 /* Set flag and configuration on peer-group member. */
5304 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5305 member
->update_source
= sockunion_dup(su
);
5306 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5308 /* Send notification or reset peer depending on state. */
5309 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5310 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5311 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5312 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5314 bgp_session_reset(member
);
5316 /* Apply new source configuration to BFD session. */
5317 if (member
->bfd_config
)
5318 bgp_peer_bfd_update_source(member
);
5322 void peer_update_source_unset(struct peer
*peer
)
5324 struct peer
*member
;
5325 struct listnode
*node
, *nnode
;
5327 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
5330 /* Inherit configuration from peer-group if peer is member. */
5331 if (peer_group_active(peer
)) {
5332 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
5333 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
5334 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
5335 MTYPE_PEER_UPDATE_SOURCE
);
5337 /* Otherwise remove flag and configuration from peer. */
5338 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
5339 sockunion_free(peer
->update_source
);
5340 peer
->update_source
= NULL
;
5341 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5344 /* Check if handling a regular peer. */
5345 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5346 /* Send notification or reset peer depending on state. */
5347 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5348 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5349 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5350 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5352 bgp_session_reset(peer
);
5354 /* Apply new source configuration to BFD session. */
5355 if (peer
->bfd_config
)
5356 bgp_peer_bfd_update_source(peer
);
5358 /* Skip peer-group mechanics for regular peers. */
5363 * Set flag and configuration on all peer-group members, unless they are
5364 * explicitly overriding peer-group configuration.
5366 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5367 /* Skip peers with overridden configuration. */
5368 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5371 /* Skip peers with the same configuration. */
5372 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5373 && !member
->update_source
&& !member
->update_if
)
5376 /* Remove flag and configuration on peer-group member. */
5377 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5378 sockunion_free(member
->update_source
);
5379 member
->update_source
= NULL
;
5380 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5382 /* Send notification or reset peer depending on state. */
5383 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5384 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5385 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5386 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5388 bgp_session_reset(member
);
5390 /* Apply new source configuration to BFD session. */
5391 if (member
->bfd_config
)
5392 bgp_peer_bfd_update_source(member
);
5396 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5397 const char *rmap
, struct route_map
*route_map
)
5399 struct peer
*member
;
5400 struct listnode
*node
, *nnode
;
5401 struct update_subgroup
*subgrp
;
5403 /* Set flag and configuration on peer. */
5404 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5406 subgrp
= peer_subgroup(peer
, afi
, safi
);
5409 if (!peer
->default_rmap
[afi
][safi
].name
5410 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5411 if (peer
->default_rmap
[afi
][safi
].name
)
5412 XFREE(MTYPE_ROUTE_MAP_NAME
,
5413 peer
->default_rmap
[afi
][safi
].name
);
5416 * When there is a change in route-map policy,
5417 * this flow gets triggered. Since, the default
5418 * route is already originated, the flag is set.
5419 * The flag should be unset here,
5420 * to trigger the flow of sending update message.
5423 UNSET_FLAG(subgrp
->sflags
,
5424 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5426 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5427 peer
->default_rmap
[afi
][safi
].name
=
5428 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5429 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5430 route_map_counter_increment(route_map
);
5433 if (peer
->default_rmap
[afi
][safi
].name
)
5434 XFREE(MTYPE_ROUTE_MAP_NAME
,
5435 peer
->default_rmap
[afi
][safi
].name
);
5438 * This is triggered in case of route-map deletion.
5439 * The flag needs to be unset, to trigger the flow
5440 * of sending an update message.
5443 UNSET_FLAG(subgrp
->sflags
,
5444 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5446 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5447 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5448 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5451 /* Check if handling a regular peer. */
5452 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5453 /* Update peer route announcements. */
5454 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5455 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5456 bgp_default_originate(peer
, afi
, safi
, 0);
5457 bgp_announce_route(peer
, afi
, safi
, false);
5460 /* Skip peer-group mechanics for regular peers. */
5465 * Set flag and configuration on all peer-group members, unless they are
5466 * explicitly overriding peer-group configuration.
5468 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5469 /* Skip peers with overridden configuration. */
5470 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5471 PEER_FLAG_DEFAULT_ORIGINATE
))
5474 /* Set flag and configuration on peer-group member. */
5475 SET_FLAG(member
->af_flags
[afi
][safi
],
5476 PEER_FLAG_DEFAULT_ORIGINATE
);
5478 if (member
->default_rmap
[afi
][safi
].name
)
5479 XFREE(MTYPE_ROUTE_MAP_NAME
,
5480 member
->default_rmap
[afi
][safi
].name
);
5481 route_map_counter_decrement(
5482 member
->default_rmap
[afi
][safi
].map
);
5483 member
->default_rmap
[afi
][safi
].name
=
5484 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5485 member
->default_rmap
[afi
][safi
].map
= route_map
;
5486 route_map_counter_increment(route_map
);
5489 /* Update peer route announcements. */
5490 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5491 update_group_adjust_peer(
5492 peer_af_find(member
, afi
, safi
));
5493 bgp_default_originate(member
, afi
, safi
, 0);
5494 bgp_announce_route(member
, afi
, safi
, false);
5501 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5503 struct peer
*member
;
5504 struct listnode
*node
, *nnode
;
5506 /* Inherit configuration from peer-group if peer is member. */
5507 if (peer_group_active(peer
)) {
5508 peer_af_flag_inherit(peer
, afi
, safi
,
5509 PEER_FLAG_DEFAULT_ORIGINATE
);
5510 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5511 default_rmap
[afi
][safi
].name
,
5512 MTYPE_ROUTE_MAP_NAME
);
5513 PEER_ATTR_INHERIT(peer
, peer
->group
,
5514 default_rmap
[afi
][safi
].map
);
5516 /* Otherwise remove flag and configuration from peer. */
5517 peer_af_flag_unset(peer
, afi
, safi
,
5518 PEER_FLAG_DEFAULT_ORIGINATE
);
5519 if (peer
->default_rmap
[afi
][safi
].name
)
5520 XFREE(MTYPE_ROUTE_MAP_NAME
,
5521 peer
->default_rmap
[afi
][safi
].name
);
5522 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5523 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5524 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5527 /* Check if handling a regular peer. */
5528 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5529 /* Update peer route announcements. */
5530 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5531 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5532 bgp_default_originate(peer
, afi
, safi
, 1);
5533 bgp_announce_route(peer
, afi
, safi
, false);
5536 /* Skip peer-group mechanics for regular peers. */
5541 * Remove flag and configuration from all peer-group members, unless
5542 * they are explicitly overriding peer-group configuration.
5544 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5545 /* Skip peers with overridden configuration. */
5546 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5547 PEER_FLAG_DEFAULT_ORIGINATE
))
5550 /* Remove flag and configuration on peer-group member. */
5551 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5552 PEER_FLAG_DEFAULT_ORIGINATE
);
5553 if (member
->default_rmap
[afi
][safi
].name
)
5554 XFREE(MTYPE_ROUTE_MAP_NAME
,
5555 member
->default_rmap
[afi
][safi
].name
);
5556 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5557 member
->default_rmap
[afi
][safi
].name
= NULL
;
5558 member
->default_rmap
[afi
][safi
].map
= NULL
;
5560 /* Update peer route announcements. */
5561 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5562 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5563 bgp_default_originate(member
, afi
, safi
, 1);
5564 bgp_announce_route(member
, afi
, safi
, false);
5571 void peer_port_set(struct peer
*peer
, uint16_t port
)
5574 peer_flag_set(peer
, PEER_FLAG_PORT
);
5577 void peer_port_unset(struct peer
*peer
)
5579 peer
->port
= BGP_PORT_DEFAULT
;
5580 peer_flag_unset(peer
, PEER_FLAG_PORT
);
5583 /* Set the TCP-MSS value in the peer structure,
5584 * This gets applied only after connection reset
5585 * So this value will be used in bgp_connect.
5587 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5589 peer
->tcp_mss
= tcp_mss
;
5590 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5593 /* Reset the TCP-MSS value in the peer structure,
5594 * This gets applied only after connection reset
5595 * So this value will be used in bgp_connect.
5597 void peer_tcp_mss_unset(struct peer
*peer
)
5599 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5604 * Helper function that is called after the name of the policy
5605 * being used by a peer has changed (AF specific). Automatically
5606 * initiates inbound or outbound processing as needed.
5608 void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5612 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5613 if (peer_established(peer
))
5614 bgp_announce_route(peer
, afi
, safi
, false);
5616 if (!peer_established(peer
))
5619 if (bgp_soft_reconfig_in(peer
, afi
, safi
))
5622 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
) ||
5623 CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
)) {
5624 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5625 PEER_CAP_ORF_PREFIX_SM_ADV
) &&
5626 (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5627 PEER_CAP_ORF_PREFIX_RM_RCV
) ||
5628 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5629 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
)))
5630 peer_clear_soft(peer
, afi
, safi
,
5631 BGP_CLEAR_SOFT_IN_ORF_PREFIX
);
5633 bgp_route_refresh_send(
5634 peer
, afi
, safi
, 0, 0, 0,
5635 BGP_ROUTE_REFRESH_NORMAL
);
5641 /* neighbor weight. */
5642 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5644 struct peer
*member
;
5645 struct listnode
*node
, *nnode
;
5647 /* Set flag and configuration on peer. */
5648 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5649 if (peer
->weight
[afi
][safi
] != weight
) {
5650 peer
->weight
[afi
][safi
] = weight
;
5651 peer_on_policy_change(peer
, afi
, safi
, 0);
5654 /* Skip peer-group mechanics for regular peers. */
5655 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5659 * Set flag and configuration on all peer-group members, unless they are
5660 * explicitly overriding peer-group configuration.
5662 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5663 /* Skip peers with overridden configuration. */
5664 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5668 /* Set flag and configuration on peer-group member. */
5669 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5670 if (member
->weight
[afi
][safi
] != weight
) {
5671 member
->weight
[afi
][safi
] = weight
;
5672 peer_on_policy_change(member
, afi
, safi
, 0);
5679 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5681 struct peer
*member
;
5682 struct listnode
*node
, *nnode
;
5684 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5687 /* Inherit configuration from peer-group if peer is member. */
5688 if (peer_group_active(peer
)) {
5689 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5690 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5692 peer_on_policy_change(peer
, afi
, safi
, 0);
5696 /* Remove flag and configuration from peer. */
5697 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5698 peer
->weight
[afi
][safi
] = 0;
5699 peer_on_policy_change(peer
, afi
, safi
, 0);
5701 /* Skip peer-group mechanics for regular peers. */
5702 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5706 * Remove flag and configuration from all peer-group members, unless
5707 * they are explicitly overriding peer-group configuration.
5709 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5710 /* Skip peers with overridden configuration. */
5711 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5715 /* Skip peers where flag is already disabled. */
5716 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5719 /* Remove flag and configuration on peer-group member. */
5720 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5721 member
->weight
[afi
][safi
] = 0;
5722 peer_on_policy_change(member
, afi
, safi
, 0);
5728 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5730 struct peer
*member
;
5731 struct listnode
*node
, *nnode
;
5733 if (keepalive
> UINT16_MAX
)
5734 return BGP_ERR_INVALID_VALUE
;
5736 if (holdtime
> UINT16_MAX
)
5737 return BGP_ERR_INVALID_VALUE
;
5739 if (holdtime
< 3 && holdtime
!= 0)
5740 return BGP_ERR_INVALID_VALUE
;
5742 /* Set flag and configuration on peer. */
5743 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5744 peer
->holdtime
= holdtime
;
5745 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5747 /* Skip peer-group mechanics for regular peers. */
5748 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5752 * Set flag and configuration on all peer-group members, unless they are
5753 * explicitly overriding peer-group configuration.
5755 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5756 /* Skip peers with overridden configuration. */
5757 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5760 /* Set flag and configuration on peer-group member. */
5761 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5762 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5763 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5769 int peer_timers_unset(struct peer
*peer
)
5771 struct peer
*member
;
5772 struct listnode
*node
, *nnode
;
5774 /* Inherit configuration from peer-group if peer is member. */
5775 if (peer_group_active(peer
)) {
5776 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5777 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5778 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5780 /* Otherwise remove flag and configuration from peer. */
5781 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5783 peer
->keepalive
= 0;
5786 /* Skip peer-group mechanics for regular peers. */
5787 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5791 * Remove flag and configuration from all peer-group members, unless
5792 * they are explicitly overriding peer-group configuration.
5794 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5795 /* Skip peers with overridden configuration. */
5796 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5799 /* Remove flag and configuration on peer-group member. */
5800 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5801 member
->holdtime
= 0;
5802 member
->keepalive
= 0;
5808 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5810 struct peer
*member
;
5811 struct listnode
*node
, *nnode
;
5813 if (connect
> UINT16_MAX
)
5814 return BGP_ERR_INVALID_VALUE
;
5816 /* Set flag and configuration on peer. */
5817 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5818 peer
->connect
= connect
;
5819 peer
->v_connect
= connect
;
5821 /* Skip peer-group mechanics for regular peers. */
5822 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5823 if (!peer_established(peer
)) {
5824 if (peer_active(peer
))
5825 BGP_EVENT_ADD(peer
, BGP_Stop
);
5826 BGP_EVENT_ADD(peer
, BGP_Start
);
5831 * Set flag and configuration on all peer-group members, unless they are
5832 * explicitly overriding peer-group configuration.
5834 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5835 /* Skip peers with overridden configuration. */
5836 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5839 /* Set flag and configuration on peer-group member. */
5840 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5841 member
->connect
= connect
;
5842 member
->v_connect
= connect
;
5844 if (!peer_established(member
)) {
5845 if (peer_active(member
))
5846 BGP_EVENT_ADD(member
, BGP_Stop
);
5847 BGP_EVENT_ADD(member
, BGP_Start
);
5854 int peer_timers_connect_unset(struct peer
*peer
)
5856 struct peer
*member
;
5857 struct listnode
*node
, *nnode
;
5859 /* Inherit configuration from peer-group if peer is member. */
5860 if (peer_group_active(peer
)) {
5861 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5862 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5864 /* Otherwise remove flag and configuration from peer. */
5865 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5869 /* Set timer with fallback to default value. */
5871 peer
->v_connect
= peer
->connect
;
5873 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5875 /* Skip peer-group mechanics for regular peers. */
5876 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5877 if (!peer_established(peer
)) {
5878 if (peer_active(peer
))
5879 BGP_EVENT_ADD(peer
, BGP_Stop
);
5880 BGP_EVENT_ADD(peer
, BGP_Start
);
5885 * Remove flag and configuration from all peer-group members, unless
5886 * they are explicitly overriding peer-group configuration.
5888 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5889 /* Skip peers with overridden configuration. */
5890 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5893 /* Remove flag and configuration on peer-group member. */
5894 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5895 member
->connect
= 0;
5896 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5898 if (!peer_established(member
)) {
5899 if (peer_active(member
))
5900 BGP_EVENT_ADD(member
, BGP_Stop
);
5901 BGP_EVENT_ADD(member
, BGP_Start
);
5908 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5910 struct peer
*member
;
5911 struct listnode
*node
, *nnode
;
5914 return BGP_ERR_INVALID_VALUE
;
5916 /* Set flag and configuration on peer. */
5917 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5918 peer
->routeadv
= routeadv
;
5919 peer
->v_routeadv
= routeadv
;
5921 /* Check if handling a regular peer. */
5922 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5923 /* Update peer route announcements. */
5924 update_group_adjust_peer_afs(peer
);
5925 if (peer_established(peer
))
5926 bgp_announce_route_all(peer
);
5928 /* Skip peer-group mechanics for regular peers. */
5933 * Set flag and configuration on all peer-group members, unless they are
5934 * explicitly overriding peer-group configuration.
5936 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5937 /* Skip peers with overridden configuration. */
5938 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5941 /* Set flag and configuration on peer-group member. */
5942 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5943 member
->routeadv
= routeadv
;
5944 member
->v_routeadv
= routeadv
;
5946 /* Update peer route announcements. */
5947 update_group_adjust_peer_afs(member
);
5948 if (peer_established(member
))
5949 bgp_announce_route_all(member
);
5955 int peer_advertise_interval_unset(struct peer
*peer
)
5957 struct peer
*member
;
5958 struct listnode
*node
, *nnode
;
5960 /* Inherit configuration from peer-group if peer is member. */
5961 if (peer_group_active(peer
)) {
5962 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5963 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5965 /* Otherwise remove flag and configuration from peer. */
5966 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5970 /* Set timer with fallback to default value. */
5972 peer
->v_routeadv
= peer
->routeadv
;
5974 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5975 ? BGP_DEFAULT_IBGP_ROUTEADV
5976 : BGP_DEFAULT_EBGP_ROUTEADV
;
5978 /* Check if handling a regular peer. */
5979 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5980 /* Update peer route announcements. */
5981 update_group_adjust_peer_afs(peer
);
5982 if (peer_established(peer
))
5983 bgp_announce_route_all(peer
);
5985 /* Skip peer-group mechanics for regular peers. */
5990 * Remove flag and configuration from all peer-group members, unless
5991 * they are explicitly overriding peer-group configuration.
5993 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5994 /* Skip peers with overridden configuration. */
5995 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5998 /* Remove flag and configuration on peer-group member. */
5999 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
6000 member
->routeadv
= 0;
6001 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
6002 ? BGP_DEFAULT_IBGP_ROUTEADV
6003 : BGP_DEFAULT_EBGP_ROUTEADV
;
6005 /* Update peer route announcements. */
6006 update_group_adjust_peer_afs(member
);
6007 if (peer_established(member
))
6008 bgp_announce_route_all(member
);
6014 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
6017 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
6019 struct peer
*member
;
6020 struct listnode
*node
;
6022 /* Set peers session attribute flag and timer interval. */
6023 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6024 peer
->delayopen
= delayopen
;
6025 peer
->v_delayopen
= delayopen
;
6027 /* Skip group mechanics for regular peers. */
6028 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6031 /* Set flag and configuration on all peer-group members, unless they are
6032 * explicitly overriding peer-group configuration.
6034 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6035 /* Skip peers with overridden configuration. */
6036 if (CHECK_FLAG(member
->flags_override
,
6037 PEER_FLAG_TIMER_DELAYOPEN
))
6040 /* Set session attribute flag and timer intervals on peer-group
6043 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6044 member
->delayopen
= delayopen
;
6045 member
->v_delayopen
= delayopen
;
6051 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
6052 * DelayOpenTimer interval to the default value.
6054 int peer_timers_delayopen_unset(struct peer
*peer
)
6056 struct peer
*member
;
6057 struct listnode
*node
;
6059 /* Inherit configuration from peer-group if peer is member. */
6060 if (peer_group_active(peer
)) {
6061 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6062 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
6064 /* Otherwise remove session attribute flag and set timer
6065 * interval to default value.
6067 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6068 peer
->delayopen
= peer
->bgp
->default_delayopen
;
6071 /* Set timer value to zero */
6072 peer
->v_delayopen
= 0;
6074 /* Skip peer-group mechanics for regular peers. */
6075 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6078 /* Remove flag and configuration from all peer-group members, unless
6079 * they are explicitly overriding peer-group configuration.
6081 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6082 /* Skip peers with overridden configuration. */
6083 if (CHECK_FLAG(member
->flags_override
,
6084 PEER_FLAG_TIMER_DELAYOPEN
))
6087 /* Remove session attribute flag, reset the timer interval to
6088 * the default value and set the timer value to zero.
6090 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6091 member
->delayopen
= peer
->bgp
->default_delayopen
;
6092 member
->v_delayopen
= 0;
6098 /* neighbor interface */
6099 void peer_interface_set(struct peer
*peer
, const char *str
)
6101 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6102 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
6105 void peer_interface_unset(struct peer
*peer
)
6107 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6111 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6112 int allow_num
, int origin
)
6114 struct peer
*member
;
6115 struct listnode
*node
, *nnode
;
6117 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
6118 return BGP_ERR_INVALID_VALUE
;
6120 /* Set flag and configuration on peer. */
6121 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6123 if (peer
->allowas_in
[afi
][safi
] != 0
6124 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6125 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6126 peer_af_flag_set(peer
, afi
, safi
,
6127 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6128 peer
->allowas_in
[afi
][safi
] = 0;
6129 peer_on_policy_change(peer
, afi
, safi
, 0);
6132 if (peer
->allowas_in
[afi
][safi
] != allow_num
6133 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6134 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6136 peer_af_flag_unset(peer
, afi
, safi
,
6137 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6138 peer
->allowas_in
[afi
][safi
] = allow_num
;
6139 peer_on_policy_change(peer
, afi
, safi
, 0);
6143 /* Skip peer-group mechanics for regular peers. */
6144 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6148 * Set flag and configuration on all peer-group members, unless
6149 * they are explicitly overriding peer-group configuration.
6151 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6152 /* Skip peers with overridden configuration. */
6153 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6154 PEER_FLAG_ALLOWAS_IN
))
6157 /* Set flag and configuration on peer-group member. */
6158 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6160 if (member
->allowas_in
[afi
][safi
] != 0
6161 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
6162 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6163 SET_FLAG(member
->af_flags
[afi
][safi
],
6164 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6165 member
->allowas_in
[afi
][safi
] = 0;
6166 peer_on_policy_change(peer
, afi
, safi
, 0);
6169 if (member
->allowas_in
[afi
][safi
] != allow_num
6170 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
6171 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6172 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6173 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6174 member
->allowas_in
[afi
][safi
] = allow_num
;
6175 peer_on_policy_change(peer
, afi
, safi
, 0);
6183 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6185 struct peer
*member
;
6186 struct listnode
*node
, *nnode
;
6188 /* Skip peer if flag is already disabled. */
6189 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
6192 /* Inherit configuration from peer-group if peer is member. */
6193 if (peer_group_active(peer
)) {
6194 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6195 peer_af_flag_inherit(peer
, afi
, safi
,
6196 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6197 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
6198 peer_on_policy_change(peer
, afi
, safi
, 0);
6203 /* Remove flag and configuration from peer. */
6204 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6205 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6206 peer
->allowas_in
[afi
][safi
] = 0;
6207 peer_on_policy_change(peer
, afi
, safi
, 0);
6209 /* Skip peer-group mechanics if handling a regular peer. */
6210 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6214 * Remove flags and configuration from all peer-group members, unless
6215 * they are explicitly overriding peer-group configuration.
6217 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6218 /* Skip peers with overridden configuration. */
6219 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6220 PEER_FLAG_ALLOWAS_IN
))
6223 /* Remove flags and configuration on peer-group member. */
6224 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6225 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6226 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6227 member
->allowas_in
[afi
][safi
] = 0;
6228 peer_on_policy_change(member
, afi
, safi
, 0);
6234 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
6237 bool old_no_prepend
, old_replace_as
;
6238 struct bgp
*bgp
= peer
->bgp
;
6239 struct peer
*member
;
6240 struct listnode
*node
, *nnode
;
6243 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
6245 /* Save previous flag states. */
6247 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6249 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6251 /* Set flag and configuration on peer. */
6252 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
6253 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
6254 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
6256 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
6257 && old_replace_as
== replace_as
)
6259 peer
->change_local_as
= as
;
6260 (void)peer_sort(peer
);
6262 /* Check if handling a regular peer. */
6263 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6267 * Set flag and configuration on all peer-group members, unless they are
6268 * explicitly overriding peer-group configuration.
6270 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6271 /* Skip peers with overridden configuration. */
6272 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6275 /* Skip peers with the same configuration. */
6276 old_no_prepend
= CHECK_FLAG(member
->flags
,
6277 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6278 old_replace_as
= CHECK_FLAG(member
->flags
,
6279 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6280 if (member
->change_local_as
== as
6281 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
6282 && old_no_prepend
== no_prepend
6283 && old_replace_as
== replace_as
)
6286 /* Set flag and configuration on peer-group member. */
6287 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6288 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
6290 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
6292 member
->change_local_as
= as
;
6298 int peer_local_as_unset(struct peer
*peer
)
6300 struct peer
*member
;
6301 struct listnode
*node
, *nnode
;
6303 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
6306 /* Inherit configuration from peer-group if peer is member. */
6307 if (peer_group_active(peer
)) {
6308 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
6309 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6310 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6311 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
6313 /* Otherwise remove flag and configuration from peer. */
6314 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
6315 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6316 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6317 peer
->change_local_as
= 0;
6320 /* Check if handling a regular peer. */
6321 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6322 /* Send notification or stop peer depending on state. */
6323 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6324 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6325 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6326 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6328 BGP_EVENT_ADD(peer
, BGP_Stop
);
6330 /* Skip peer-group mechanics for regular peers. */
6335 * Remove flag and configuration from all peer-group members, unless
6336 * they are explicitly overriding peer-group configuration.
6338 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6339 /* Skip peers with overridden configuration. */
6340 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6343 /* Remove flag and configuration on peer-group member. */
6344 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6345 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6346 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6347 member
->change_local_as
= 0;
6349 /* Send notification or stop peer depending on state. */
6350 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6351 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6352 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6353 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6355 bgp_session_reset(member
);
6361 /* Set password for authenticating with the peer. */
6362 int peer_password_set(struct peer
*peer
, const char *password
)
6364 struct peer
*member
;
6365 struct listnode
*node
, *nnode
;
6366 int len
= password
? strlen(password
) : 0;
6367 int ret
= BGP_SUCCESS
;
6369 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
6370 return BGP_ERR_INVALID_VALUE
;
6372 /* Set flag and configuration on peer. */
6373 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
6374 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
6376 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6377 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6379 /* Check if handling a regular peer. */
6380 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6381 /* Send notification or reset peer depending on state. */
6382 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6383 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6384 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6386 bgp_session_reset(peer
);
6389 * Attempt to install password on socket and skip peer-group
6392 if (BGP_PEER_SU_UNSPEC(peer
))
6394 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6395 : BGP_ERR_TCPSIG_FAILED
;
6399 * Set flag and configuration on all peer-group members, unless they are
6400 * explicitly overriding peer-group configuration.
6402 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6403 /* Skip peers with overridden configuration. */
6404 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6407 /* Skip peers with the same password. */
6408 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6411 /* Set flag and configuration on peer-group member. */
6412 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6413 if (member
->password
)
6414 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6415 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6417 /* Send notification or reset peer depending on state. */
6418 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6419 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6420 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6422 bgp_session_reset(member
);
6424 /* Attempt to install password on socket. */
6425 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6426 ret
= BGP_ERR_TCPSIG_FAILED
;
6429 /* Set flag and configuration on all peer-group listen ranges */
6430 struct listnode
*ln
;
6433 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6434 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6435 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6436 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6441 int peer_password_unset(struct peer
*peer
)
6443 struct peer
*member
;
6444 struct listnode
*node
, *nnode
;
6446 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6449 /* Inherit configuration from peer-group if peer is member. */
6450 if (peer_group_active(peer
)) {
6451 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6452 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6453 MTYPE_PEER_PASSWORD
);
6455 /* Otherwise remove flag and configuration from peer. */
6456 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6457 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6460 /* Check if handling a regular peer. */
6461 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6462 /* Send notification or reset peer depending on state. */
6463 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6464 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6465 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6467 bgp_session_reset(peer
);
6469 /* Attempt to uninstall password on socket. */
6470 if (!BGP_PEER_SU_UNSPEC(peer
))
6471 bgp_md5_unset(peer
);
6472 /* Skip peer-group mechanics for regular peers. */
6477 * Remove flag and configuration from all peer-group members, unless
6478 * they are explicitly overriding peer-group configuration.
6480 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6481 /* Skip peers with overridden configuration. */
6482 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6485 /* Remove flag and configuration on peer-group member. */
6486 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6487 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6489 /* Send notification or reset peer depending on state. */
6490 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6491 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6492 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6494 bgp_session_reset(member
);
6496 /* Attempt to uninstall password on socket. */
6497 if (!BGP_PEER_SU_UNSPEC(member
))
6498 bgp_md5_unset(member
);
6501 /* Set flag and configuration on all peer-group listen ranges */
6502 struct listnode
*ln
;
6505 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6506 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6507 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6508 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6514 /* Set distribute list to the peer. */
6515 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6518 struct peer
*member
;
6519 struct bgp_filter
*filter
;
6520 struct listnode
*node
, *nnode
;
6522 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6523 return BGP_ERR_INVALID_VALUE
;
6525 /* Set configuration on peer. */
6526 filter
= &peer
->filter
[afi
][safi
];
6527 if (filter
->plist
[direct
].name
)
6528 return BGP_ERR_PEER_FILTER_CONFLICT
;
6529 if (filter
->dlist
[direct
].name
)
6530 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6531 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6532 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6534 /* Check if handling a regular peer. */
6535 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6536 /* Set override-flag and process peer route updates. */
6537 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6538 PEER_FT_DISTRIBUTE_LIST
);
6539 peer_on_policy_change(peer
, afi
, safi
,
6540 (direct
== FILTER_OUT
) ? 1 : 0);
6542 /* Skip peer-group mechanics for regular peers. */
6547 * Set configuration on all peer-group members, un less they are
6548 * explicitly overriding peer-group configuration.
6550 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6551 /* Skip peers with overridden configuration. */
6552 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6553 PEER_FT_DISTRIBUTE_LIST
))
6556 /* Set configuration on peer-group member. */
6557 filter
= &member
->filter
[afi
][safi
];
6558 if (filter
->dlist
[direct
].name
)
6559 XFREE(MTYPE_BGP_FILTER_NAME
,
6560 filter
->dlist
[direct
].name
);
6561 filter
->dlist
[direct
].name
=
6562 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6563 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6565 /* Process peer route updates. */
6566 peer_on_policy_change(member
, afi
, safi
,
6567 (direct
== FILTER_OUT
) ? 1 : 0);
6573 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6575 struct peer
*member
;
6576 struct bgp_filter
*filter
;
6577 struct listnode
*node
, *nnode
;
6579 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6580 return BGP_ERR_INVALID_VALUE
;
6582 /* Unset override-flag unconditionally. */
6583 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6584 PEER_FT_DISTRIBUTE_LIST
);
6586 /* Inherit configuration from peer-group if peer is member. */
6587 if (peer_group_active(peer
)) {
6588 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6589 filter
[afi
][safi
].dlist
[direct
].name
,
6590 MTYPE_BGP_FILTER_NAME
);
6591 PEER_ATTR_INHERIT(peer
, peer
->group
,
6592 filter
[afi
][safi
].dlist
[direct
].alist
);
6594 /* Otherwise remove configuration from peer. */
6595 filter
= &peer
->filter
[afi
][safi
];
6596 if (filter
->dlist
[direct
].name
)
6597 XFREE(MTYPE_BGP_FILTER_NAME
,
6598 filter
->dlist
[direct
].name
);
6599 filter
->dlist
[direct
].name
= NULL
;
6600 filter
->dlist
[direct
].alist
= NULL
;
6603 /* Check if handling a regular peer. */
6604 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6605 /* Process peer route updates. */
6606 peer_on_policy_change(peer
, afi
, safi
,
6607 (direct
== FILTER_OUT
) ? 1 : 0);
6609 /* Skip peer-group mechanics for regular peers. */
6614 * Remove configuration on all peer-group members, unless they are
6615 * explicitly overriding peer-group configuration.
6617 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6618 /* Skip peers with overridden configuration. */
6619 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6620 PEER_FT_DISTRIBUTE_LIST
))
6623 /* Remove configuration on peer-group member. */
6624 filter
= &member
->filter
[afi
][safi
];
6625 if (filter
->dlist
[direct
].name
)
6626 XFREE(MTYPE_BGP_FILTER_NAME
,
6627 filter
->dlist
[direct
].name
);
6628 filter
->dlist
[direct
].name
= NULL
;
6629 filter
->dlist
[direct
].alist
= NULL
;
6631 /* Process peer route updates. */
6632 peer_on_policy_change(member
, afi
, safi
,
6633 (direct
== FILTER_OUT
) ? 1 : 0);
6639 /* Update distribute list. */
6640 static void peer_distribute_update(struct access_list
*access
)
6645 struct listnode
*mnode
, *mnnode
;
6646 struct listnode
*node
, *nnode
;
6649 struct peer_group
*group
;
6650 struct bgp_filter
*filter
;
6652 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6654 update_group_policy_update(bgp
,
6655 BGP_POLICY_DISTRIBUTE_LIST
,
6656 access
->name
, true, 0);
6657 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6658 FOREACH_AFI_SAFI (afi
, safi
) {
6659 filter
= &peer
->filter
[afi
][safi
];
6661 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6663 if (filter
->dlist
[direct
].name
)
6664 filter
->dlist
[direct
]
6665 .alist
= access_list_lookup(
6667 filter
->dlist
[direct
]
6670 filter
->dlist
[direct
].alist
=
6675 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6676 FOREACH_AFI_SAFI (afi
, safi
) {
6677 filter
= &group
->conf
->filter
[afi
][safi
];
6679 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6681 if (filter
->dlist
[direct
].name
)
6682 filter
->dlist
[direct
]
6683 .alist
= access_list_lookup(
6685 filter
->dlist
[direct
]
6688 filter
->dlist
[direct
].alist
=
6693 #ifdef ENABLE_BGP_VNC
6694 vnc_prefix_list_update(bgp
);
6699 /* Set prefix list to the peer. */
6700 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6703 struct peer
*member
;
6704 struct bgp_filter
*filter
;
6705 struct listnode
*node
, *nnode
;
6707 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6708 return BGP_ERR_INVALID_VALUE
;
6710 /* Set configuration on peer. */
6711 filter
= &peer
->filter
[afi
][safi
];
6712 if (filter
->dlist
[direct
].name
)
6713 return BGP_ERR_PEER_FILTER_CONFLICT
;
6714 if (filter
->plist
[direct
].name
)
6715 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6716 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6717 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6719 /* Check if handling a regular peer. */
6720 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6721 /* Set override-flag and process peer route updates. */
6722 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6723 PEER_FT_PREFIX_LIST
);
6724 peer_on_policy_change(peer
, afi
, safi
,
6725 (direct
== FILTER_OUT
) ? 1 : 0);
6727 /* Skip peer-group mechanics for regular peers. */
6732 * Set configuration on all peer-group members, unless they are
6733 * explicitly overriding peer-group configuration.
6735 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6736 /* Skip peers with overridden configuration. */
6737 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6738 PEER_FT_PREFIX_LIST
))
6741 /* Set configuration on peer-group member. */
6742 filter
= &member
->filter
[afi
][safi
];
6743 if (filter
->plist
[direct
].name
)
6744 XFREE(MTYPE_BGP_FILTER_NAME
,
6745 filter
->plist
[direct
].name
);
6746 filter
->plist
[direct
].name
=
6747 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6748 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6750 /* Process peer route updates. */
6751 peer_on_policy_change(member
, afi
, safi
,
6752 (direct
== FILTER_OUT
) ? 1 : 0);
6758 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6761 struct peer
*member
;
6762 struct bgp_filter
*filter
;
6763 struct listnode
*node
, *nnode
;
6765 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6766 return BGP_ERR_INVALID_VALUE
;
6768 /* Unset override-flag unconditionally. */
6769 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6770 PEER_FT_PREFIX_LIST
);
6772 /* Inherit configuration from peer-group if peer is member. */
6773 if (peer_group_active(peer
)) {
6774 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6775 filter
[afi
][safi
].plist
[direct
].name
,
6776 MTYPE_BGP_FILTER_NAME
);
6777 PEER_ATTR_INHERIT(peer
, peer
->group
,
6778 filter
[afi
][safi
].plist
[direct
].plist
);
6780 /* Otherwise remove configuration from peer. */
6781 filter
= &peer
->filter
[afi
][safi
];
6782 if (filter
->plist
[direct
].name
)
6783 XFREE(MTYPE_BGP_FILTER_NAME
,
6784 filter
->plist
[direct
].name
);
6785 filter
->plist
[direct
].name
= NULL
;
6786 filter
->plist
[direct
].plist
= NULL
;
6789 /* Check if handling a regular peer. */
6790 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6791 /* Process peer route updates. */
6792 peer_on_policy_change(peer
, afi
, safi
,
6793 (direct
== FILTER_OUT
) ? 1 : 0);
6795 /* Skip peer-group mechanics for regular peers. */
6800 * Remove configuration on all peer-group members, unless they are
6801 * explicitly overriding peer-group configuration.
6803 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6804 /* Skip peers with overridden configuration. */
6805 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6806 PEER_FT_PREFIX_LIST
))
6809 /* Remove configuration on peer-group member. */
6810 filter
= &member
->filter
[afi
][safi
];
6811 if (filter
->plist
[direct
].name
)
6812 XFREE(MTYPE_BGP_FILTER_NAME
,
6813 filter
->plist
[direct
].name
);
6814 filter
->plist
[direct
].name
= NULL
;
6815 filter
->plist
[direct
].plist
= NULL
;
6817 /* Process peer route updates. */
6818 peer_on_policy_change(member
, afi
, safi
,
6819 (direct
== FILTER_OUT
) ? 1 : 0);
6825 /* Update prefix-list list. */
6826 static void peer_prefix_list_update(struct prefix_list
*plist
)
6828 struct listnode
*mnode
, *mnnode
;
6829 struct listnode
*node
, *nnode
;
6832 struct peer_group
*group
;
6833 struct bgp_filter
*filter
;
6838 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6841 * Update the prefix-list on update groups.
6843 update_group_policy_update(
6844 bgp
, BGP_POLICY_PREFIX_LIST
,
6845 plist
? prefix_list_name(plist
) : NULL
, true, 0);
6847 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6848 FOREACH_AFI_SAFI (afi
, safi
) {
6849 filter
= &peer
->filter
[afi
][safi
];
6851 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6853 if (filter
->plist
[direct
].name
)
6854 filter
->plist
[direct
]
6855 .plist
= prefix_list_lookup(
6857 filter
->plist
[direct
]
6860 filter
->plist
[direct
].plist
=
6864 /* If we touch prefix-list, we need to process
6865 * new updates. This is important for ORF to
6866 * work correctly as well.
6868 if (peer
->afc_nego
[afi
][safi
])
6869 peer_on_policy_change(peer
, afi
, safi
,
6873 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6874 FOREACH_AFI_SAFI (afi
, safi
) {
6875 filter
= &group
->conf
->filter
[afi
][safi
];
6877 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6879 if (filter
->plist
[direct
].name
)
6880 filter
->plist
[direct
]
6881 .plist
= prefix_list_lookup(
6883 filter
->plist
[direct
]
6886 filter
->plist
[direct
].plist
=
6894 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6897 struct peer
*member
;
6898 struct bgp_filter
*filter
;
6899 struct listnode
*node
, *nnode
;
6901 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6902 return BGP_ERR_INVALID_VALUE
;
6904 /* Set configuration on peer. */
6905 filter
= &peer
->filter
[afi
][safi
];
6906 if (filter
->aslist
[direct
].name
)
6907 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6908 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6909 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6911 /* Check if handling a regular peer. */
6912 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6913 /* Set override-flag and process peer route updates. */
6914 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6915 PEER_FT_FILTER_LIST
);
6916 peer_on_policy_change(peer
, afi
, safi
,
6917 (direct
== FILTER_OUT
) ? 1 : 0);
6919 /* Skip peer-group mechanics for regular peers. */
6924 * Set configuration on all peer-group members, unless they are
6925 * explicitly overriding peer-group configuration.
6927 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6928 /* Skip peers with overridden configuration. */
6929 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6930 PEER_FT_FILTER_LIST
))
6933 /* Set configuration on peer-group member. */
6934 filter
= &member
->filter
[afi
][safi
];
6935 if (filter
->aslist
[direct
].name
)
6936 XFREE(MTYPE_BGP_FILTER_NAME
,
6937 filter
->aslist
[direct
].name
);
6938 filter
->aslist
[direct
].name
=
6939 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6940 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6942 /* Process peer route updates. */
6943 peer_on_policy_change(member
, afi
, safi
,
6944 (direct
== FILTER_OUT
) ? 1 : 0);
6950 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6952 struct peer
*member
;
6953 struct bgp_filter
*filter
;
6954 struct listnode
*node
, *nnode
;
6956 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6957 return BGP_ERR_INVALID_VALUE
;
6959 /* Unset override-flag unconditionally. */
6960 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6961 PEER_FT_FILTER_LIST
);
6963 /* Inherit configuration from peer-group if peer is member. */
6964 if (peer_group_active(peer
)) {
6965 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6966 filter
[afi
][safi
].aslist
[direct
].name
,
6967 MTYPE_BGP_FILTER_NAME
);
6968 PEER_ATTR_INHERIT(peer
, peer
->group
,
6969 filter
[afi
][safi
].aslist
[direct
].aslist
);
6971 /* Otherwise remove configuration from peer. */
6972 filter
= &peer
->filter
[afi
][safi
];
6973 if (filter
->aslist
[direct
].name
)
6974 XFREE(MTYPE_BGP_FILTER_NAME
,
6975 filter
->aslist
[direct
].name
);
6976 filter
->aslist
[direct
].name
= NULL
;
6977 filter
->aslist
[direct
].aslist
= NULL
;
6980 /* Check if handling a regular peer. */
6981 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6982 /* Process peer route updates. */
6983 peer_on_policy_change(peer
, afi
, safi
,
6984 (direct
== FILTER_OUT
) ? 1 : 0);
6986 /* Skip peer-group mechanics for regular peers. */
6991 * Remove configuration on all peer-group members, unless they are
6992 * explicitly overriding peer-group configuration.
6994 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6995 /* Skip peers with overridden configuration. */
6996 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6997 PEER_FT_FILTER_LIST
))
7000 /* Remove configuration on peer-group member. */
7001 filter
= &member
->filter
[afi
][safi
];
7002 if (filter
->aslist
[direct
].name
)
7003 XFREE(MTYPE_BGP_FILTER_NAME
,
7004 filter
->aslist
[direct
].name
);
7005 filter
->aslist
[direct
].name
= NULL
;
7006 filter
->aslist
[direct
].aslist
= NULL
;
7008 /* Process peer route updates. */
7009 peer_on_policy_change(member
, afi
, safi
,
7010 (direct
== FILTER_OUT
) ? 1 : 0);
7016 static void peer_aslist_update(const char *aslist_name
)
7021 struct listnode
*mnode
, *mnnode
;
7022 struct listnode
*node
, *nnode
;
7025 struct peer_group
*group
;
7026 struct bgp_filter
*filter
;
7028 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
7029 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
7030 aslist_name
, true, 0);
7032 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7033 FOREACH_AFI_SAFI (afi
, safi
) {
7034 filter
= &peer
->filter
[afi
][safi
];
7036 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7038 if (filter
->aslist
[direct
].name
)
7039 filter
->aslist
[direct
]
7040 .aslist
= as_list_lookup(
7041 filter
->aslist
[direct
]
7044 filter
->aslist
[direct
].aslist
=
7049 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
7050 FOREACH_AFI_SAFI (afi
, safi
) {
7051 filter
= &group
->conf
->filter
[afi
][safi
];
7053 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7055 if (filter
->aslist
[direct
].name
)
7056 filter
->aslist
[direct
]
7057 .aslist
= as_list_lookup(
7058 filter
->aslist
[direct
]
7061 filter
->aslist
[direct
].aslist
=
7069 static void peer_aslist_add(char *aslist_name
)
7071 peer_aslist_update(aslist_name
);
7072 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
7075 static void peer_aslist_del(const char *aslist_name
)
7077 peer_aslist_update(aslist_name
);
7078 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
7082 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
7083 const char *name
, struct route_map
*route_map
)
7085 struct peer
*member
;
7086 struct bgp_filter
*filter
;
7087 struct listnode
*node
, *nnode
;
7089 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7090 return BGP_ERR_INVALID_VALUE
;
7092 /* Set configuration on peer. */
7093 filter
= &peer
->filter
[afi
][safi
];
7094 if (filter
->map
[direct
].name
) {
7095 /* If the neighbor is configured with the same route-map
7096 * again then, ignore the duplicate configuration.
7098 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
7101 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7103 route_map_counter_decrement(filter
->map
[direct
].map
);
7104 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7105 filter
->map
[direct
].map
= route_map
;
7106 route_map_counter_increment(route_map
);
7108 /* Check if handling a regular peer. */
7109 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7110 /* Set override-flag and process peer route updates. */
7111 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
7113 peer_on_policy_change(peer
, afi
, safi
,
7114 (direct
== RMAP_OUT
) ? 1 : 0);
7116 /* Skip peer-group mechanics for regular peers. */
7121 * Set configuration on all peer-group members, unless they are
7122 * explicitly overriding peer-group configuration.
7124 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7125 /* Skip peers with overridden configuration. */
7126 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7130 /* Set configuration on peer-group member. */
7131 filter
= &member
->filter
[afi
][safi
];
7132 if (filter
->map
[direct
].name
)
7133 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7134 route_map_counter_decrement(filter
->map
[direct
].map
);
7135 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7136 filter
->map
[direct
].map
= route_map
;
7137 route_map_counter_increment(route_map
);
7139 /* Process peer route updates. */
7140 peer_on_policy_change(member
, afi
, safi
,
7141 (direct
== RMAP_OUT
) ? 1 : 0);
7146 /* Unset route-map from the peer. */
7147 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
7149 struct peer
*member
;
7150 struct bgp_filter
*filter
;
7151 struct listnode
*node
, *nnode
;
7153 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7154 return BGP_ERR_INVALID_VALUE
;
7156 /* Unset override-flag unconditionally. */
7157 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
7159 /* Inherit configuration from peer-group if peer is member. */
7160 if (peer_group_active(peer
)) {
7161 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7162 filter
[afi
][safi
].map
[direct
].name
,
7163 MTYPE_BGP_FILTER_NAME
);
7164 PEER_ATTR_INHERIT(peer
, peer
->group
,
7165 filter
[afi
][safi
].map
[direct
].map
);
7167 /* Otherwise remove configuration from peer. */
7168 filter
= &peer
->filter
[afi
][safi
];
7169 if (filter
->map
[direct
].name
)
7170 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7171 route_map_counter_decrement(filter
->map
[direct
].map
);
7172 filter
->map
[direct
].name
= NULL
;
7173 filter
->map
[direct
].map
= NULL
;
7176 /* Check if handling a regular peer. */
7177 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7178 /* Process peer route updates. */
7179 peer_on_policy_change(peer
, afi
, safi
,
7180 (direct
== RMAP_OUT
) ? 1 : 0);
7182 /* Skip peer-group mechanics for regular peers. */
7187 * Remove configuration on all peer-group members, unless they are
7188 * explicitly overriding peer-group configuration.
7190 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7191 /* Skip peers with overridden configuration. */
7192 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7196 /* Remove configuration on peer-group member. */
7197 filter
= &member
->filter
[afi
][safi
];
7198 if (filter
->map
[direct
].name
)
7199 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7200 route_map_counter_decrement(filter
->map
[direct
].map
);
7201 filter
->map
[direct
].name
= NULL
;
7202 filter
->map
[direct
].map
= NULL
;
7204 /* Process peer route updates. */
7205 peer_on_policy_change(member
, afi
, safi
,
7206 (direct
== RMAP_OUT
) ? 1 : 0);
7212 /* Set unsuppress-map to the peer. */
7213 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7214 const char *name
, struct route_map
*route_map
)
7216 struct peer
*member
;
7217 struct bgp_filter
*filter
;
7218 struct listnode
*node
, *nnode
;
7220 /* Set configuration on peer. */
7221 filter
= &peer
->filter
[afi
][safi
];
7222 if (filter
->usmap
.name
)
7223 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7224 route_map_counter_decrement(filter
->usmap
.map
);
7225 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7226 filter
->usmap
.map
= route_map
;
7227 route_map_counter_increment(route_map
);
7229 /* Check if handling a regular peer. */
7230 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7231 /* Set override-flag and process peer route updates. */
7232 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
7233 PEER_FT_UNSUPPRESS_MAP
);
7234 peer_on_policy_change(peer
, afi
, safi
, 1);
7236 /* Skip peer-group mechanics for regular peers. */
7241 * Set configuration on all peer-group members, unless they are
7242 * explicitly overriding peer-group configuration.
7244 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7245 /* Skip peers with overridden configuration. */
7246 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7247 PEER_FT_UNSUPPRESS_MAP
))
7250 /* Set configuration on peer-group member. */
7251 filter
= &member
->filter
[afi
][safi
];
7252 if (filter
->usmap
.name
)
7253 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7254 route_map_counter_decrement(filter
->usmap
.map
);
7255 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7256 filter
->usmap
.map
= route_map
;
7257 route_map_counter_increment(route_map
);
7259 /* Process peer route updates. */
7260 peer_on_policy_change(member
, afi
, safi
, 1);
7266 /* Unset route-map from the peer. */
7267 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7269 struct peer
*member
;
7270 struct bgp_filter
*filter
;
7271 struct listnode
*node
, *nnode
;
7273 /* Unset override-flag unconditionally. */
7274 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
7276 /* Inherit configuration from peer-group if peer is member. */
7277 if (peer_group_active(peer
)) {
7278 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7279 filter
[afi
][safi
].usmap
.name
,
7280 MTYPE_BGP_FILTER_NAME
);
7281 PEER_ATTR_INHERIT(peer
, peer
->group
,
7282 filter
[afi
][safi
].usmap
.map
);
7284 /* Otherwise remove configuration from peer. */
7285 filter
= &peer
->filter
[afi
][safi
];
7286 if (filter
->usmap
.name
)
7287 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7288 route_map_counter_decrement(filter
->usmap
.map
);
7289 filter
->usmap
.name
= NULL
;
7290 filter
->usmap
.map
= NULL
;
7293 /* Check if handling a regular peer. */
7294 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7295 /* Process peer route updates. */
7296 peer_on_policy_change(peer
, afi
, safi
, 1);
7298 /* Skip peer-group mechanics for regular peers. */
7303 * Remove configuration on all peer-group members, unless they are
7304 * explicitly overriding peer-group configuration.
7306 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7307 /* Skip peers with overridden configuration. */
7308 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7309 PEER_FT_UNSUPPRESS_MAP
))
7312 /* Remove configuration on peer-group member. */
7313 filter
= &member
->filter
[afi
][safi
];
7314 if (filter
->usmap
.name
)
7315 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7316 route_map_counter_decrement(filter
->usmap
.map
);
7317 filter
->usmap
.name
= NULL
;
7318 filter
->usmap
.map
= NULL
;
7320 /* Process peer route updates. */
7321 peer_on_policy_change(member
, afi
, safi
, 1);
7327 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7329 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7332 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7333 if (peer
->t_pmax_restart
) {
7334 THREAD_OFF(peer
->t_pmax_restart
);
7335 if (bgp_debug_neighbor_events(peer
))
7337 "%pBP Maximum-prefix restart timer cancelled",
7340 BGP_EVENT_ADD(peer
, BGP_Start
);
7344 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7345 uint32_t max
, uint8_t threshold
, int warning
,
7346 uint16_t restart
, bool force
)
7348 struct peer
*member
;
7349 struct listnode
*node
, *nnode
;
7351 /* Set flags and configuration on peer. */
7352 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7355 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7357 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7360 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7362 peer_af_flag_unset(peer
, afi
, safi
,
7363 PEER_FLAG_MAX_PREFIX_WARNING
);
7365 peer
->pmax
[afi
][safi
] = max
;
7366 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7367 peer
->pmax_restart
[afi
][safi
] = restart
;
7369 /* Check if handling a regular peer. */
7370 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7371 /* Re-check if peer violates maximum-prefix. */
7372 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7373 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7375 /* Skip peer-group mechanics for regular peers. */
7380 * Set flags and configuration on all peer-group members, unless they
7381 * are explicitly overriding peer-group configuration.
7383 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7384 /* Skip peers with overridden configuration. */
7385 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7386 PEER_FLAG_MAX_PREFIX
))
7389 /* Set flag and configuration on peer-group member. */
7390 member
->pmax
[afi
][safi
] = max
;
7391 member
->pmax_threshold
[afi
][safi
] = threshold
;
7392 member
->pmax_restart
[afi
][safi
] = restart
;
7395 SET_FLAG(member
->af_flags
[afi
][safi
],
7396 PEER_FLAG_MAX_PREFIX_FORCE
);
7398 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7399 PEER_FLAG_MAX_PREFIX_FORCE
);
7402 SET_FLAG(member
->af_flags
[afi
][safi
],
7403 PEER_FLAG_MAX_PREFIX_WARNING
);
7405 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7406 PEER_FLAG_MAX_PREFIX_WARNING
);
7408 /* Re-check if peer violates maximum-prefix. */
7409 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7410 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7416 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7418 /* Inherit configuration from peer-group if peer is member. */
7419 if (peer_group_active(peer
)) {
7420 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7421 peer_af_flag_inherit(peer
, afi
, safi
,
7422 PEER_FLAG_MAX_PREFIX_FORCE
);
7423 peer_af_flag_inherit(peer
, afi
, safi
,
7424 PEER_FLAG_MAX_PREFIX_WARNING
);
7425 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7426 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7427 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7432 /* Remove flags and configuration from peer. */
7433 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7434 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7435 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7436 peer
->pmax
[afi
][safi
] = 0;
7437 peer
->pmax_threshold
[afi
][safi
] = 0;
7438 peer
->pmax_restart
[afi
][safi
] = 0;
7441 * Remove flags and configuration from all peer-group members, unless
7442 * they are explicitly overriding peer-group configuration.
7444 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7445 struct peer
*member
;
7446 struct listnode
*node
;
7448 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7449 /* Skip peers with overridden configuration. */
7450 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7451 PEER_FLAG_MAX_PREFIX
))
7454 /* Remove flag and configuration on peer-group member.
7456 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7457 PEER_FLAG_MAX_PREFIX
);
7458 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7459 PEER_FLAG_MAX_PREFIX_FORCE
);
7460 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7461 PEER_FLAG_MAX_PREFIX_WARNING
);
7462 member
->pmax
[afi
][safi
] = 0;
7463 member
->pmax_threshold
[afi
][safi
] = 0;
7464 member
->pmax_restart
[afi
][safi
] = 0;
7466 peer_maximum_prefix_clear_overflow(member
);
7469 peer_maximum_prefix_clear_overflow(peer
);
7475 void peer_maximum_prefix_out_refresh_routes(struct peer
*peer
, afi_t afi
,
7478 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
7480 if (peer_established(peer
))
7481 bgp_announce_route(peer
, afi
, safi
, false);
7484 int peer_maximum_prefix_out_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7487 struct peer
*member
;
7488 struct listnode
*node
, *nnode
;
7490 /* Set flag on peer and peer-group member if any */
7491 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7492 /* Set configuration on peer. */
7493 peer
->pmax_out
[afi
][safi
] = max
;
7495 /* Check if handling a regular peer. */
7496 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7497 /* Skip peer-group mechanics for regular peers. */
7498 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7503 * Set flag and configuration on all peer-group members, unless they
7504 * are explicitly overriding peer-group configuration.
7506 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7507 /* Skip peers with overridden configuration. */
7508 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7509 PEER_FLAG_MAX_PREFIX_OUT
))
7512 /* Set configuration on peer-group member. */
7513 member
->pmax_out
[afi
][safi
] = max
;
7515 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7520 int peer_maximum_prefix_out_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7522 struct peer
*member
;
7523 struct listnode
*node
;
7524 /* Inherit configuration from peer-group if peer is member. */
7525 if (peer_group_active(peer
)) {
7526 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7527 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_out
[afi
][safi
]);
7529 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7533 /* Remove flag and configuration from peer. */
7534 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7535 peer
->pmax_out
[afi
][safi
] = 0;
7537 /* Check if handling a regular peer. */
7538 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7539 /* Skip peer-group mechanics for regular peers. */
7540 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7545 * Remove flag and configuration from all peer-group members, unless
7546 * they are explicitly overriding peer-group configuration.
7548 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7549 /* Skip peers with overridden configuration. */
7550 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7551 PEER_FLAG_MAX_PREFIX_OUT
))
7554 /* Remove flag and configuration on peer-group member.
7556 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7557 PEER_FLAG_MAX_PREFIX_OUT
);
7558 member
->pmax_out
[afi
][safi
] = 0;
7560 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7565 int is_ebgp_multihop_configured(struct peer
*peer
)
7567 struct peer_group
*group
;
7568 struct listnode
*node
, *nnode
;
7571 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7572 group
= peer
->group
;
7573 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7574 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7577 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7578 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7579 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7583 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7584 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7590 /* Set # of hops between us and BGP peer. */
7591 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7593 struct peer_group
*group
;
7595 struct listnode
*node
, *nnode
;
7598 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7601 /* We cannot configure ttl-security hops when ebgp-multihop is already
7602 set. For non peer-groups, the check is simple. For peer-groups,
7604 slightly messy, because we need to check both the peer-group
7606 and all peer-group members for any trace of ebgp-multihop
7608 before actually applying the ttl-security rules. Cisco really made a
7609 mess of this configuration parameter, and OpenBGPD got it right.
7612 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7613 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7614 if (is_ebgp_multihop_configured(peer
))
7615 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7617 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7618 peer
->gtsm_hops
= gtsm_hops
;
7620 /* Calling ebgp multihop also resets the session.
7621 * On restart, NHT will get setup correctly as will the
7622 * min & max ttls on the socket. The return value is
7625 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7630 group
= peer
->group
;
7631 group
->conf
->gtsm_hops
= gtsm_hops
;
7632 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7634 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7636 /* Calling ebgp multihop also resets the
7638 * On restart, NHT will get setup correctly as
7640 * min & max ttls on the socket. The return
7644 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7648 /* Post the first gtsm setup or if its ibgp, maxttl setting
7650 * necessary, just set the minttl.
7652 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7653 peer
->gtsm_hops
= gtsm_hops
;
7656 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7657 MAXTTL
+ 1 - gtsm_hops
);
7658 if ((peer
->status
< Established
) && peer
->doppelganger
7659 && (peer
->doppelganger
->fd
>= 0))
7660 sockopt_minttl(peer
->su
.sa
.sa_family
,
7661 peer
->doppelganger
->fd
,
7662 MAXTTL
+ 1 - gtsm_hops
);
7664 group
= peer
->group
;
7665 group
->conf
->gtsm_hops
= gtsm_hops
;
7666 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7668 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7670 /* Change setting of existing peer
7671 * established then change value (may break
7673 * not established yet (teardown session and
7675 * no session then do nothing (will get
7676 * handled by next connection)
7680 != BGP_GTSM_HOPS_DISABLED
)
7682 gpeer
->su
.sa
.sa_family
,
7684 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7685 if ((gpeer
->status
< Established
)
7686 && gpeer
->doppelganger
7687 && (gpeer
->doppelganger
->fd
>= 0))
7688 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7689 gpeer
->doppelganger
->fd
,
7690 MAXTTL
+ 1 - gtsm_hops
);
7698 int peer_ttl_security_hops_unset(struct peer
*peer
)
7700 struct peer_group
*group
;
7701 struct listnode
*node
, *nnode
;
7704 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7706 /* if a peer-group member, then reset to peer-group default rather than
7708 if (peer_group_active(peer
))
7709 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7711 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7713 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7714 /* Invoking ebgp_multihop_set will set the TTL back to the
7716 * value as well as restting the NHT and such. The session is
7719 if (peer
->sort
== BGP_PEER_EBGP
)
7720 ret
= peer_ebgp_multihop_unset(peer
);
7723 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7726 if ((peer
->status
< Established
) && peer
->doppelganger
7727 && (peer
->doppelganger
->fd
>= 0))
7728 sockopt_minttl(peer
->su
.sa
.sa_family
,
7729 peer
->doppelganger
->fd
, 0);
7732 group
= peer
->group
;
7733 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7734 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7735 if (peer
->sort
== BGP_PEER_EBGP
)
7736 ret
= peer_ebgp_multihop_unset(peer
);
7739 sockopt_minttl(peer
->su
.sa
.sa_family
,
7742 if ((peer
->status
< Established
)
7743 && peer
->doppelganger
7744 && (peer
->doppelganger
->fd
>= 0))
7745 sockopt_minttl(peer
->su
.sa
.sa_family
,
7746 peer
->doppelganger
->fd
,
7755 static void peer_reset_message_stats(struct peer
*peer
)
7758 atomic_store_explicit(&peer
->open_in
, 0, memory_order_relaxed
);
7759 atomic_store_explicit(&peer
->open_out
, 0, memory_order_relaxed
);
7760 atomic_store_explicit(&peer
->update_in
, 0,
7761 memory_order_relaxed
);
7762 atomic_store_explicit(&peer
->update_out
, 0,
7763 memory_order_relaxed
);
7764 atomic_store_explicit(&peer
->keepalive_in
, 0,
7765 memory_order_relaxed
);
7766 atomic_store_explicit(&peer
->keepalive_out
, 0,
7767 memory_order_relaxed
);
7768 atomic_store_explicit(&peer
->notify_in
, 0,
7769 memory_order_relaxed
);
7770 atomic_store_explicit(&peer
->notify_out
, 0,
7771 memory_order_relaxed
);
7772 atomic_store_explicit(&peer
->refresh_in
, 0,
7773 memory_order_relaxed
);
7774 atomic_store_explicit(&peer
->refresh_out
, 0,
7775 memory_order_relaxed
);
7776 atomic_store_explicit(&peer
->dynamic_cap_in
, 0,
7777 memory_order_relaxed
);
7778 atomic_store_explicit(&peer
->dynamic_cap_out
, 0,
7779 memory_order_relaxed
);
7784 * If peer clear is invoked in a loop for all peers on the BGP instance,
7785 * it may end up freeing the doppelganger, and if this was the next node
7786 * to the current node, we would end up accessing the freed next node.
7787 * Pass along additional parameter which can be updated if next node
7788 * is freed; only required when walking the peer list on BGP instance.
7790 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7792 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7793 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7794 if (peer_maximum_prefix_clear_overflow(peer
))
7797 peer
->v_start
= BGP_INIT_START_TIMER
;
7798 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7799 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7800 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7802 bgp_session_reset_safe(peer
, nnode
);
7807 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7808 enum bgp_clear_type stype
)
7810 struct peer_af
*paf
;
7812 if (!peer_established(peer
))
7815 if (!peer
->afc
[afi
][safi
])
7816 return BGP_ERR_AF_UNCONFIGURED
;
7818 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7820 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7821 /* Clear the "neighbor x.x.x.x default-originate" flag */
7822 paf
= peer_af_find(peer
, afi
, safi
);
7823 if (paf
&& paf
->subgroup
7824 && CHECK_FLAG(paf
->subgroup
->sflags
,
7825 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7826 UNSET_FLAG(paf
->subgroup
->sflags
,
7827 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7829 bgp_announce_route(peer
, afi
, safi
, false);
7832 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7833 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7834 PEER_CAP_ORF_PREFIX_SM_ADV
)
7835 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7836 PEER_CAP_ORF_PREFIX_RM_RCV
)
7837 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7838 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7839 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7840 uint8_t prefix_type
;
7842 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7843 PEER_CAP_ORF_PREFIX_RM_RCV
))
7844 prefix_type
= ORF_TYPE_PREFIX
;
7846 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7848 if (filter
->plist
[FILTER_IN
].plist
) {
7849 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7850 PEER_STATUS_ORF_PREFIX_SEND
))
7851 bgp_route_refresh_send(
7852 peer
, afi
, safi
, prefix_type
,
7854 BGP_ROUTE_REFRESH_NORMAL
);
7855 bgp_route_refresh_send(
7856 peer
, afi
, safi
, prefix_type
,
7857 REFRESH_IMMEDIATE
, 0,
7858 BGP_ROUTE_REFRESH_NORMAL
);
7860 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7861 PEER_STATUS_ORF_PREFIX_SEND
))
7862 bgp_route_refresh_send(
7863 peer
, afi
, safi
, prefix_type
,
7864 REFRESH_IMMEDIATE
, 1,
7865 BGP_ROUTE_REFRESH_NORMAL
);
7867 bgp_route_refresh_send(
7868 peer
, afi
, safi
, 0, 0, 0,
7869 BGP_ROUTE_REFRESH_NORMAL
);
7875 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7876 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7877 /* If neighbor has soft reconfiguration inbound flag.
7878 Use Adj-RIB-In database. */
7879 if (!bgp_soft_reconfig_in(peer
, afi
, safi
)) {
7880 /* If neighbor has route refresh capability, send route
7882 message to the peer. */
7883 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7884 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7885 bgp_route_refresh_send(
7886 peer
, afi
, safi
, 0, 0, 0,
7887 BGP_ROUTE_REFRESH_NORMAL
);
7889 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7893 if (stype
== BGP_CLEAR_MESSAGE_STATS
)
7894 peer_reset_message_stats(peer
);
7899 /* Display peer uptime.*/
7900 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7903 time_t uptime1
, epoch_tbuf
;
7906 /* If there is no connection has been done before print `never'. */
7909 json_object_string_add(json
, "peerUptime", "never");
7910 json_object_int_add(json
, "peerUptimeMsec", 0);
7912 snprintf(buf
, len
, "never");
7916 /* Get current time. */
7917 uptime1
= monotime(NULL
);
7919 gmtime_r(&uptime1
, &tm
);
7921 if (uptime1
< ONE_DAY_SECOND
)
7922 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7924 else if (uptime1
< ONE_WEEK_SECOND
)
7925 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7927 else if (uptime1
< ONE_YEAR_SECOND
)
7928 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7929 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7931 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7933 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7936 epoch_tbuf
= time(NULL
) - uptime1
;
7937 json_object_string_add(json
, "peerUptime", buf
);
7938 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7939 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7946 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7947 struct list
*addresses
)
7951 memset(&bgp_master
, 0, sizeof(bgp_master
));
7954 bm
->bgp
= list_new();
7955 bm
->listen_sockets
= list_new();
7956 bm
->port
= BGP_PORT_DEFAULT
;
7957 bm
->addresses
= addresses
;
7958 bm
->master
= master
;
7959 bm
->start_time
= monotime(NULL
);
7960 bm
->t_rmap_update
= NULL
;
7961 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7962 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7963 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7964 bm
->terminating
= false;
7965 bm
->socket_buffer
= buffer_size
;
7966 bm
->wait_for_fib
= false;
7967 bm
->tcp_dscp
= IPTOS_PREC_INTERNETCONTROL
;
7968 bm
->inq_limit
= BM_DEFAULT_Q_LIMIT
;
7969 bm
->outq_limit
= BM_DEFAULT_Q_LIMIT
;
7972 /* init the rd id space.
7973 assign 0th index in the bitfield,
7974 so that we start with id 1
7976 bf_init(bm
->rd_idspace
, UINT16_MAX
);
7977 bf_assign_zero_index(bm
->rd_idspace
);
7979 /* mpls label dynamic allocation pool */
7980 bgp_lp_init(bm
->master
, &bm
->labelpool
);
7984 QOBJ_REG(bm
, bgp_master
);
7988 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7989 * instance delete (non-default only) or BGP exit.
7991 static void bgp_if_finish(struct bgp
*bgp
)
7994 struct interface
*ifp
;
7996 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
7998 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
8001 FOR_ALL_INTERFACES (vrf
, ifp
) {
8002 struct listnode
*c_node
, *c_nnode
;
8003 struct connected
*c
;
8005 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
8006 bgp_connected_delete(bgp
, c
);
8010 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
8012 struct vrf
*vrf
= NULL
;
8013 struct listnode
*next
;
8016 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
8017 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
8019 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8020 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
8023 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
8027 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
8029 struct listnode
*next
, *next2
;
8030 struct bgp
*bgp
, *bgp2
;
8033 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8035 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
8036 if (bgp2
->as
== bgp
->as
)
8044 snprintf(buf
, sizeof(buf
), "%u", bgp
->as
);
8045 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
8049 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
8050 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
8051 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
8052 {.completions
= NULL
},
8055 struct frr_pthread
*bgp_pth_io
;
8056 struct frr_pthread
*bgp_pth_ka
;
8058 static void bgp_pthreads_init(void)
8060 assert(!bgp_pth_io
);
8061 assert(!bgp_pth_ka
);
8063 struct frr_pthread_attr io
= {
8064 .start
= frr_pthread_attr_default
.start
,
8065 .stop
= frr_pthread_attr_default
.stop
,
8067 struct frr_pthread_attr ka
= {
8068 .start
= bgp_keepalives_start
,
8069 .stop
= bgp_keepalives_stop
,
8071 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
8072 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
8075 void bgp_pthreads_run(void)
8077 frr_pthread_run(bgp_pth_io
, NULL
);
8078 frr_pthread_run(bgp_pth_ka
, NULL
);
8080 /* Wait until threads are ready. */
8081 frr_pthread_wait_running(bgp_pth_io
);
8082 frr_pthread_wait_running(bgp_pth_ka
);
8085 void bgp_pthreads_finish(void)
8087 frr_pthread_stop_all();
8090 static int peer_unshut_after_cfg(struct bgp
*bgp
)
8092 struct listnode
*node
;
8095 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
8096 if (!peer
->shut_during_cfg
)
8099 if (bgp_debug_neighbor_events(peer
))
8100 zlog_debug("%s: released from config-pending hold",
8103 peer
->shut_during_cfg
= false;
8104 if (peer_active(peer
) && peer
->status
!= Established
) {
8105 if (peer
->status
!= Idle
)
8106 BGP_EVENT_ADD(peer
, BGP_Stop
);
8107 BGP_EVENT_ADD(peer
, BGP_Start
);
8114 void bgp_init(unsigned short instance
)
8116 hook_register(bgp_config_end
, peer_unshut_after_cfg
);
8118 /* allocates some vital data structures used by peer commands in
8121 /* pre-init pthreads */
8122 bgp_pthreads_init();
8125 bgp_zebra_init(bm
->master
, instance
);
8127 #ifdef ENABLE_BGP_VNC
8128 vnc_zebra_init(bm
->master
);
8131 /* BGP VTY commands installation. */
8137 bgp_community_alias_init();
8140 bgp_route_map_init();
8141 bgp_scan_vty_init();
8143 #ifdef ENABLE_BGP_VNC
8146 bgp_ethernetvpn_init();
8147 bgp_flowspec_vty_init();
8149 /* Access list initialize. */
8151 access_list_add_hook(peer_distribute_update
);
8152 access_list_delete_hook(peer_distribute_update
);
8154 /* Filter list initialize. */
8156 as_list_add_hook(peer_aslist_add
);
8157 as_list_delete_hook(peer_aslist_del
);
8159 /* Prefix list initialize.*/
8161 prefix_list_add_hook(peer_prefix_list_update
);
8162 prefix_list_delete_hook(peer_prefix_list_update
);
8164 /* Community list initialize. */
8165 bgp_clist
= community_list_init();
8168 bgp_bfd_init(bm
->master
);
8172 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
8175 void bgp_terminate(void)
8179 struct listnode
*node
, *nnode
;
8180 struct listnode
*mnode
, *mnnode
;
8184 /* Close the listener sockets first as this prevents peers from
8186 * to reconnect on receiving the peer unconfig message. In the presence
8187 * of a large number of peers this will ensure that no peer is left with
8188 * a dangling connection
8192 /* reverse bgp_master_init */
8193 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
8194 bgp_close_vrf_socket(bgp
);
8195 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8196 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
8197 if (bgp_debug_neighbor_events(peer
))
8199 "%pBP configured Graceful-Restart, skipping unconfig notification",
8203 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
8204 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
8205 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
8209 if (bm
->listen_sockets
)
8210 list_delete(&bm
->listen_sockets
);
8212 THREAD_OFF(bm
->t_rmap_update
);
8217 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
8218 const char *ip_str
, bool use_json
)
8224 /* Get peer sockunion. */
8225 ret
= str2sockunion(ip_str
, &su
);
8227 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
8229 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
8233 json_object
*json_no
= NULL
;
8234 json_no
= json_object_new_object();
8235 json_object_string_add(
8237 "malformedAddressOrName",
8239 vty_json(vty
, json_no
);
8242 "%% Malformed address or name: %s\n",
8250 /* Peer structure lookup. */
8251 peer
= peer_lookup(bgp
, &su
);
8254 json_object
*json_no
= NULL
;
8255 json_no
= json_object_new_object();
8256 json_object_string_add(json_no
, "warning",
8257 "No such neighbor in this view/vrf");
8258 vty_json(vty
, json_no
);
8260 vty_out(vty
, "No such neighbor in this view/vrf\n");
8267 void bgp_gr_apply_running_config(void)
8269 struct peer
*peer
= NULL
;
8270 struct bgp
*bgp
= NULL
;
8271 struct listnode
*node
, *nnode
;
8272 bool gr_router_detected
= false;
8274 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
8275 zlog_debug("[BGP_GR] %s called !", __func__
);
8277 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
8278 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8279 bgp_peer_gr_flags_update(peer
);
8280 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
8281 gr_router_detected
= true;
8284 if (gr_router_detected
8285 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
8286 bgp_zebra_send_capabilities(bgp
, true);
8287 } else if (!gr_router_detected
8288 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
8289 bgp_zebra_send_capabilities(bgp
, false);
8292 gr_router_detected
= false;
8296 printfrr_ext_autoreg_p("BP", printfrr_bp
);
8297 static ssize_t
printfrr_bp(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
8300 const struct peer
*peer
= ptr
;
8303 return bputs(buf
, "(null)");
8305 return bprintfrr(buf
, "%s(%s)", peer
->host
,
8306 peer
->hostname
? peer
->hostname
: "Unknown");