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
, const char *as_str
)
574 struct listnode
*node
, *nnode
;
580 /* Remember - were we doing confederation before? */
581 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
583 if (bgp
->confed_id_pretty
)
584 XFREE(MTYPE_BGP
, bgp
->confed_id_pretty
);
585 bgp
->confed_id_pretty
= XSTRDUP(MTYPE_BGP
, as_str
);
586 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
588 /* If we were doing confederation already, this is just an external
589 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
590 were not doing confederation before, reset all EBGP sessions. */
591 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
592 enum bgp_peer_sort ptype
= peer_sort(peer
);
594 /* We're looking for peers who's AS is not local or part of our
596 if (already_confed
) {
597 if (ptype
== BGP_PEER_EBGP
) {
599 if (BGP_IS_VALID_STATE_FOR_NOTIF(
602 PEER_DOWN_CONFED_ID_CHANGE
;
604 peer
, BGP_NOTIFY_CEASE
,
605 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
607 bgp_session_reset_safe(peer
, &nnode
);
610 /* Not doign confederation before, so reset every
613 if (ptype
!= BGP_PEER_IBGP
) {
614 /* Reset the local_as to be our EBGP one */
615 if (ptype
== BGP_PEER_EBGP
)
617 if (BGP_IS_VALID_STATE_FOR_NOTIF(
620 PEER_DOWN_CONFED_ID_CHANGE
;
622 peer
, BGP_NOTIFY_CEASE
,
623 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
625 bgp_session_reset_safe(peer
, &nnode
);
632 void bgp_confederation_id_unset(struct bgp
*bgp
)
635 struct listnode
*node
, *nnode
;
638 XFREE(MTYPE_BGP
, bgp
->confed_id_pretty
);
639 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
641 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
642 /* We're looking for peers who's AS is not local */
643 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
644 peer
->local_as
= bgp
->as
;
645 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
646 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
647 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
648 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
652 bgp_session_reset_safe(peer
, &nnode
);
657 /* Is an AS part of the confed or not? */
658 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
665 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
666 if (bgp
->confed_peers
[i
].as
== as
)
672 /* Add an AS to the confederation set. */
673 void bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
, const char *as_str
)
676 struct listnode
*node
, *nnode
;
681 if (bgp_confederation_peers_check(bgp
, as
))
684 bgp
->confed_peers
= XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
685 (bgp
->confed_peers_cnt
+ 1) *
686 sizeof(struct as_confed
));
688 bgp
->confed_peers
[bgp
->confed_peers_cnt
].as
= as
;
689 bgp
->confed_peers
[bgp
->confed_peers_cnt
].as_pretty
=
690 XSTRDUP(MTYPE_BGP
, as_str
);
691 bgp
->confed_peers_cnt
++;
693 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
694 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
695 if (peer
->as
== as
) {
696 peer
->local_as
= bgp
->as
;
697 (void)peer_sort(peer
);
698 if (BGP_IS_VALID_STATE_FOR_NOTIF(
701 PEER_DOWN_CONFED_PEER_CHANGE
;
703 peer
, BGP_NOTIFY_CEASE
,
704 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
706 bgp_session_reset_safe(peer
, &nnode
);
712 /* Delete an AS from the confederation set. */
713 void bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
718 struct listnode
*node
, *nnode
;
723 if (!bgp_confederation_peers_check(bgp
, as
))
726 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
727 if (bgp
->confed_peers
[i
].as
== as
) {
728 XFREE(MTYPE_BGP
, bgp
->confed_peers
[i
].as_pretty
);
729 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++) {
730 bgp
->confed_peers
[j
- 1].as
=
731 bgp
->confed_peers
[j
].as
;
732 bgp
->confed_peers
[j
- 1].as_pretty
=
733 bgp
->confed_peers
[j
].as_pretty
;
737 bgp
->confed_peers_cnt
--;
739 if (bgp
->confed_peers_cnt
== 0) {
740 if (bgp
->confed_peers
)
741 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
742 bgp
->confed_peers
= NULL
;
744 bgp
->confed_peers
= XREALLOC(
745 MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
746 bgp
->confed_peers_cnt
* sizeof(struct as_confed
));
748 /* Now reset any peer who's remote AS has just been removed from the
750 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
751 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
752 if (peer
->as
== as
) {
753 peer
->local_as
= bgp
->confed_id
;
754 (void)peer_sort(peer
);
755 if (BGP_IS_VALID_STATE_FOR_NOTIF(
758 PEER_DOWN_CONFED_PEER_CHANGE
;
760 peer
, BGP_NOTIFY_CEASE
,
761 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
763 bgp_session_reset_safe(peer
, &nnode
);
769 /* Local preference configuration. */
770 void bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
775 bgp
->default_local_pref
= local_pref
;
778 void bgp_default_local_preference_unset(struct bgp
*bgp
)
783 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
786 /* Local preference configuration. */
787 void bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
,
793 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
796 void bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
800 bgp
->default_subgroup_pkt_queue_max
=
801 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
804 /* Listen limit configuration. */
805 void bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
810 bgp
->dynamic_neighbors_limit
= listen_limit
;
813 void bgp_listen_limit_unset(struct bgp
*bgp
)
818 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
821 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
822 afi_t
*afi
, safi_t
*safi
)
824 /* Map from IANA values to internal values, return error if
825 * values are unrecognized.
827 *afi
= afi_iana2int(pkt_afi
);
828 *safi
= safi_iana2int(pkt_safi
);
829 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
835 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
836 iana_safi_t
*pkt_safi
)
838 /* Map from internal values to IANA values, return error if
839 * internal values are bad (unexpected).
841 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
843 *pkt_afi
= afi_int2iana(afi
);
844 *pkt_safi
= safi_int2iana(safi
);
848 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
857 afid
= afindex(afi
, safi
);
858 if (afid
>= BGP_AF_MAX
)
862 assert(peer
->peer_af_array
[afid
] == NULL
);
864 /* Allocate new peer af */
865 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
867 peer
->peer_af_array
[afid
] = af
;
872 bgp
->af_peer_count
[afi
][safi
]++;
877 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
884 afid
= afindex(afi
, safi
);
885 if (afid
>= BGP_AF_MAX
)
888 return peer
->peer_af_array
[afid
];
891 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
900 afid
= afindex(afi
, safi
);
901 if (afid
>= BGP_AF_MAX
)
904 af
= peer
->peer_af_array
[afid
];
909 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
911 bgp_stop_announce_route_timer(af
);
913 if (PAF_SUBGRP(af
)) {
914 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
915 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
916 af
->subgroup
->update_group
->id
,
917 af
->subgroup
->id
, peer
->host
);
921 update_subgroup_remove_peer(af
->subgroup
, af
);
923 if (bgp
->af_peer_count
[afi
][safi
])
924 bgp
->af_peer_count
[afi
][safi
]--;
926 peer
->peer_af_array
[afid
] = NULL
;
927 XFREE(MTYPE_BGP_PEER_AF
, af
);
931 /* Peer comparison function for sorting. */
932 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
934 if (p1
->group
&& !p2
->group
)
937 if (!p1
->group
&& p2
->group
)
940 if (p1
->group
== p2
->group
) {
941 if (p1
->conf_if
&& !p2
->conf_if
)
944 if (!p1
->conf_if
&& p2
->conf_if
)
947 if (p1
->conf_if
&& p2
->conf_if
)
948 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
950 return strcmp(p1
->group
->name
, p2
->group
->name
);
952 return sockunion_cmp(&p1
->su
, &p2
->su
);
955 static unsigned int peer_hash_key_make(const void *p
)
957 const struct peer
*peer
= p
;
958 return sockunion_hash(&peer
->su
);
961 static bool peer_hash_same(const void *p1
, const void *p2
)
963 const struct peer
*peer1
= p1
;
964 const struct peer
*peer2
= p2
;
966 return (sockunion_same(&peer1
->su
, &peer2
->su
)
967 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
968 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
971 void peer_flag_inherit(struct peer
*peer
, uint64_t flag
)
975 /* Skip if peer is not a peer-group member. */
976 if (!peer_group_active(peer
))
979 /* Unset override flag to signal inheritance from peer-group. */
980 UNSET_FLAG(peer
->flags_override
, flag
);
983 * Inherit flag state from peer-group. If the flag of the peer-group is
984 * not being inverted, the peer must inherit the inverse of the current
985 * peer-group flag state.
987 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
988 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
989 && CHECK_FLAG(peer
->flags_invert
, flag
))
990 COND_FLAG(peer
->flags
, flag
, !group_val
);
992 COND_FLAG(peer
->flags
, flag
, group_val
);
995 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
997 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
1000 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
1005 /* Skip if peer is not a peer-group member. */
1006 if (!peer_group_active(peer
))
1009 /* Unset override flag to signal inheritance from peer-group. */
1010 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
1013 * Inherit flag state from peer-group. If the flag of the peer-group is
1014 * not being inverted, the peer must inherit the inverse of the current
1015 * peer-group flag state.
1017 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
1018 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
1019 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
1020 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
1022 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
1025 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
1026 static inline enum bgp_peer_sort
peer_calc_sort(struct peer
*peer
)
1033 if (peer
->change_local_as
)
1034 local_as
= peer
->change_local_as
;
1036 local_as
= peer
->local_as
;
1039 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1040 if (peer
->as_type
== AS_INTERNAL
)
1041 return BGP_PEER_IBGP
;
1043 else if (peer
->as_type
== AS_EXTERNAL
)
1044 return BGP_PEER_EBGP
;
1046 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1048 return (local_as
== peer
->as
? BGP_PEER_IBGP
1055 assert(peer
->group
);
1056 peer1
= listnode_head(peer
->group
->peer
);
1061 return BGP_PEER_INTERNAL
;
1065 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1067 return BGP_PEER_INTERNAL
;
1069 if (local_as
== peer
->as
) {
1070 if (bgp
->as
== bgp
->confed_id
) {
1071 if (local_as
== bgp
->as
)
1072 return BGP_PEER_IBGP
;
1074 return BGP_PEER_EBGP
;
1076 if (local_as
== bgp
->confed_id
)
1077 return BGP_PEER_EBGP
;
1079 return BGP_PEER_IBGP
;
1083 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1084 return BGP_PEER_CONFED
;
1086 return BGP_PEER_EBGP
;
1088 if (peer
->as_type
== AS_UNSPECIFIED
) {
1089 /* check if in peer-group with AS information */
1091 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1092 if (peer
->group
->conf
->as_type
1094 if (local_as
== peer
->group
->conf
->as
)
1095 return BGP_PEER_IBGP
;
1097 return BGP_PEER_EBGP
;
1098 } else if (peer
->group
->conf
->as_type
1100 return BGP_PEER_IBGP
;
1102 return BGP_PEER_EBGP
;
1104 /* no AS information anywhere, let caller know */
1105 return BGP_PEER_UNSPECIFIED
;
1106 } else if (peer
->as_type
!= AS_SPECIFIED
)
1107 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1110 return (local_as
== 0 ? BGP_PEER_INTERNAL
1111 : local_as
== peer
->as
? BGP_PEER_IBGP
1116 /* Calculate and cache the peer "sort" */
1117 enum bgp_peer_sort
peer_sort(struct peer
*peer
)
1119 peer
->sort
= peer_calc_sort(peer
);
1123 enum bgp_peer_sort
peer_sort_lookup(struct peer
*peer
)
1128 static void peer_free(struct peer
*peer
)
1133 assert(peer
->status
== Deleted
);
1137 /* this /ought/ to have been done already through bgp_stop earlier,
1138 * but just to be sure..
1140 bgp_timer_set(peer
);
1141 bgp_reads_off(peer
);
1142 bgp_writes_off(peer
);
1143 thread_cancel_event_ready(bm
->master
, peer
);
1144 FOREACH_AFI_SAFI (afi
, safi
)
1145 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
1146 assert(!peer
->t_write
);
1147 assert(!peer
->t_read
);
1148 BGP_EVENT_FLUSH(peer
);
1150 pthread_mutex_destroy(&peer
->io_mtx
);
1152 /* Free connected nexthop, if present */
1153 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1154 && !peer_dynamic_neighbor(peer
))
1155 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1158 FOREACH_AFI_SAFI (afi
, safi
) {
1159 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1160 XFREE(MTYPE_BGP_FILTER_NAME
,
1161 peer
->filter
[afi
][safi
].advmap
.aname
);
1162 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1163 XFREE(MTYPE_BGP_FILTER_NAME
,
1164 peer
->filter
[afi
][safi
].advmap
.cname
);
1167 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1169 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1170 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1171 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
1172 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1173 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1175 /* Update source configuration. */
1176 if (peer
->update_source
) {
1177 sockunion_free(peer
->update_source
);
1178 peer
->update_source
= NULL
;
1181 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1183 XFREE(MTYPE_BGP_NOTIFICATION
, peer
->notify
.data
);
1184 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1186 if (peer
->clear_node_queue
)
1187 work_queue_free_and_null(&peer
->clear_node_queue
);
1189 bgp_sync_delete(peer
);
1191 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1193 /* Remove BFD configuration. */
1194 if (peer
->bfd_config
)
1195 bgp_peer_remove_bfd_config(peer
);
1197 FOREACH_AFI_SAFI (afi
, safi
)
1198 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1200 if (peer
->change_local_as_pretty
)
1201 XFREE(MTYPE_BGP
, peer
->change_local_as_pretty
);
1202 if (peer
->as_pretty
)
1203 XFREE(MTYPE_BGP
, peer
->as_pretty
);
1205 bgp_unlock(peer
->bgp
);
1207 memset(peer
, 0, sizeof(struct peer
));
1209 XFREE(MTYPE_BGP_PEER
, peer
);
1212 /* increase reference count on a struct peer */
1213 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1215 frrtrace(2, frr_bgp
, bgp_peer_lock
, peer
, name
);
1216 assert(peer
&& (peer
->lock
>= 0));
1223 /* decrease reference count on a struct peer
1224 * struct peer is freed and NULL returned if last reference
1226 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1228 frrtrace(2, frr_bgp
, bgp_peer_unlock
, peer
, name
);
1229 assert(peer
&& (peer
->lock
> 0));
1233 if (peer
->lock
== 0) {
1240 /* BGP GR changes */
1242 int bgp_global_gr_init(struct bgp
*bgp
)
1244 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1245 zlog_debug("%s called ..", __func__
);
1247 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1248 /* GLOBAL_HELPER Mode */
1251 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1252 GLOBAL_GR
, GLOBAL_INVALID
,
1253 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1254 GLOBAL_DISABLE
, GLOBAL_INVALID
1256 /* GLOBAL_GR Mode */
1259 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1260 GLOBAL_GR
, GLOBAL_HELPER
,
1261 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1262 GLOBAL_DISABLE
, GLOBAL_INVALID
1264 /* GLOBAL_DISABLE Mode */
1267 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1268 GLOBAL_GR
, GLOBAL_INVALID
,
1269 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1270 GLOBAL_INVALID
, GLOBAL_HELPER
1272 /* GLOBAL_INVALID Mode */
1275 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1276 GLOBAL_INVALID
, GLOBAL_INVALID
,
1277 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1278 GLOBAL_INVALID
, GLOBAL_INVALID
1281 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1282 sizeof(local_GLOBAL_GR_FSM
));
1284 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1285 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1287 return BGP_GR_SUCCESS
;
1290 int bgp_peer_gr_init(struct peer
*peer
)
1292 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1293 zlog_debug("%s called ..", __func__
);
1295 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1296 [BGP_PEER_GR_EVENT_CMD
] = {
1298 /* PEER_HELPER Mode */
1299 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1300 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1301 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1302 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1303 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1304 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1305 bgp_peer_gr_action
}
1309 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1310 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1311 bgp_peer_gr_action
},
1312 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1313 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1314 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1315 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1318 /* PEER_DISABLE Mode */
1319 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1320 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1321 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1322 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1323 bgp_peer_gr_action
},
1324 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1325 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1328 /* PEER_INVALID Mode */
1329 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1330 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1331 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1332 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1333 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1334 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1337 /* PEER_GLOBAL_INHERIT Mode */
1338 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1339 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1340 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1341 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1342 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1343 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1346 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1347 sizeof(local_Peer_GR_FSM
));
1348 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1349 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1351 return BGP_GR_SUCCESS
;
1354 static void bgp_srv6_init(struct bgp
*bgp
)
1356 bgp
->srv6_enabled
= false;
1357 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1358 bgp
->srv6_locator_chunks
= list_new();
1359 bgp
->srv6_functions
= list_new();
1362 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1364 if (bgp
->srv6_locator_chunks
)
1365 list_delete(&bgp
->srv6_locator_chunks
);
1366 if (bgp
->srv6_functions
)
1367 list_delete(&bgp
->srv6_functions
);
1370 /* Allocate new peer object, implicitely locked. */
1371 struct peer
*peer_new(struct bgp
*bgp
)
1378 /* bgp argument is absolutely required */
1381 /* Allocate new peer. */
1382 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1384 /* Set default value. */
1386 peer
->v_start
= BGP_INIT_START_TIMER
;
1387 peer
->v_connect
= bgp
->default_connect_retry
;
1388 peer
->status
= Idle
;
1389 peer
->ostatus
= Idle
;
1390 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1391 peer
->bgp
= bgp_lock(bgp
);
1392 peer
= peer_lock(peer
); /* initial reference */
1393 peer
->local_role
= ROLE_UNDEFINED
;
1394 peer
->remote_role
= ROLE_UNDEFINED
;
1395 peer
->password
= NULL
;
1396 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1398 /* Set default flags. */
1399 FOREACH_AFI_SAFI (afi
, safi
) {
1400 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1401 SET_FLAG(peer
->af_flags
[afi
][safi
],
1402 PEER_FLAG_SEND_EXT_COMMUNITY
);
1403 SET_FLAG(peer
->af_flags
[afi
][safi
],
1404 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1406 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1407 PEER_FLAG_SEND_COMMUNITY
);
1408 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1409 PEER_FLAG_SEND_EXT_COMMUNITY
);
1410 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1411 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1412 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1413 peer
->soo
[afi
][safi
] = NULL
;
1416 /* set nexthop-unchanged for l2vpn evpn by default */
1417 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1418 PEER_FLAG_NEXTHOP_UNCHANGED
);
1420 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1422 /* Initialize per peer bgp GR FSM */
1423 bgp_peer_gr_init(peer
);
1425 /* Create buffers. */
1426 peer
->ibuf
= stream_fifo_new();
1427 peer
->obuf
= stream_fifo_new();
1428 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1430 /* We use a larger buffer for peer->obuf_work in the event that:
1431 * - We RX a BGP_UPDATE where the attributes alone are just
1432 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1433 * - The user configures an outbound route-map that does many as-path
1434 * prepends or adds many communities. At most they can have
1435 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1436 * large they can make the attributes.
1438 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1439 * bounds checking for every single attribute as we construct an
1443 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1445 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1447 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1449 bgp_sync_init(peer
);
1451 /* Get service port number. */
1452 sp
= getservbyname("bgp", "tcp");
1453 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1455 QOBJ_REG(peer
, peer
);
1460 * This function is invoked when a duplicate peer structure associated with
1461 * a neighbor is being deleted. If this about-to-be-deleted structure is
1462 * the one with all the config, then we have to copy over the info.
1464 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1466 struct peer_af
*paf
;
1474 /* The following function is used by both peer group config copy to
1475 * individual peer and when we transfer config
1477 if (peer_src
->change_local_as
)
1478 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1480 /* peer flags apply */
1481 peer_dst
->flags
= peer_src
->flags
;
1483 * The doppelganger *must* not have a config node stored
1485 UNSET_FLAG(peer_dst
->flags
, PEER_FLAG_CONFIG_NODE
);
1486 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1487 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1489 peer_dst
->local_as
= peer_src
->local_as
;
1490 peer_dst
->port
= peer_src
->port
;
1491 /* copy tcp_mss value */
1492 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1493 (void)peer_sort(peer_dst
);
1494 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1495 peer_dst
->local_role
= peer_src
->local_role
;
1497 peer_dst
->max_packet_size
= peer_src
->max_packet_size
;
1500 peer_dst
->holdtime
= peer_src
->holdtime
;
1501 peer_dst
->keepalive
= peer_src
->keepalive
;
1502 peer_dst
->connect
= peer_src
->connect
;
1503 peer_dst
->delayopen
= peer_src
->delayopen
;
1504 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1505 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1506 peer_dst
->routeadv
= peer_src
->routeadv
;
1507 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1508 peer_dst
->v_delayopen
= peer_src
->v_delayopen
;
1510 /* password apply */
1511 if (peer_src
->password
&& !peer_dst
->password
)
1512 peer_dst
->password
=
1513 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1515 FOREACH_AFI_SAFI (afi
, safi
) {
1516 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1517 peer_dst
->af_flags
[afi
][safi
] = peer_src
->af_flags
[afi
][safi
];
1518 peer_dst
->allowas_in
[afi
][safi
] =
1519 peer_src
->allowas_in
[afi
][safi
];
1520 peer_dst
->weight
[afi
][safi
] = peer_src
->weight
[afi
][safi
];
1521 peer_dst
->addpath_type
[afi
][safi
] =
1522 peer_src
->addpath_type
[afi
][safi
];
1525 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1526 paf
= peer_src
->peer_af_array
[afidx
];
1528 if (!peer_af_find(peer_dst
, paf
->afi
, paf
->safi
))
1529 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1533 /* update-source apply */
1534 if (peer_src
->update_source
) {
1535 if (peer_dst
->update_source
)
1536 sockunion_free(peer_dst
->update_source
);
1537 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1538 peer_dst
->update_source
=
1539 sockunion_dup(peer_src
->update_source
);
1540 } else if (peer_src
->update_if
) {
1541 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1542 if (peer_dst
->update_source
) {
1543 sockunion_free(peer_dst
->update_source
);
1544 peer_dst
->update_source
= NULL
;
1546 peer_dst
->update_if
=
1547 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1550 if (peer_src
->ifname
) {
1551 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1554 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1558 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1559 struct interface
*ifp
)
1561 struct connected
*ifc
;
1564 struct listnode
*node
;
1566 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1567 * IPv4 address of the other end.
1569 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1570 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1571 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
1572 if (p
.prefixlen
== 30) {
1573 peer
->su
.sa
.sa_family
= AF_INET
;
1574 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1576 peer
->su
.sin
.sin_addr
.s_addr
=
1578 else if (addr
% 4 == 2)
1579 peer
->su
.sin
.sin_addr
.s_addr
=
1581 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1582 peer
->su
.sin
.sin_len
=
1583 sizeof(struct sockaddr_in
);
1584 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1586 } else if (p
.prefixlen
== 31) {
1587 peer
->su
.sa
.sa_family
= AF_INET
;
1588 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1590 peer
->su
.sin
.sin_addr
.s_addr
=
1593 peer
->su
.sin
.sin_addr
.s_addr
=
1595 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1596 peer
->su
.sin
.sin_len
=
1597 sizeof(struct sockaddr_in
);
1598 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1600 } else if (bgp_debug_neighbor_events(peer
))
1602 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1610 static bool bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1611 struct interface
*ifp
)
1613 struct nbr_connected
*ifc_nbr
;
1615 /* Have we learnt the peer's IPv6 link-local address? */
1616 if (ifp
->nbr_connected
1617 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1618 peer
->su
.sa
.sa_family
= AF_INET6
;
1619 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1620 sizeof(struct in6_addr
));
1622 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1624 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1632 * Set or reset the peer address socketunion structure based on the
1633 * learnt/derived peer address. If the address has changed, update the
1634 * password on the listen socket, if needed.
1636 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1638 struct interface
*ifp
;
1640 int peer_addr_updated
= 0;
1641 struct listnode
*node
;
1642 union sockunion old_su
;
1645 * This function is only ever needed when FRR an interface
1646 * based peering, so this simple test will tell us if
1647 * we are in an interface based configuration or not
1654 prev_family
= peer
->su
.sa
.sa_family
;
1655 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1657 /* If BGP unnumbered is not "v6only", we first see if we can
1659 * peer's IPv4 address.
1661 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1663 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1665 /* If "v6only" or we can't derive peer's IPv4 address, see if
1667 * learnt the peer's IPv6 link-local address. This is from the
1669 * IPv6 address in router advertisement.
1671 if (!peer_addr_updated
)
1673 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1675 /* If we could derive the peer address, we may need to install the
1677 * configured for the peer, if any, on the listen socket. Otherwise,
1679 * that peer's address is not available and uninstall the password, if
1682 if (peer_addr_updated
) {
1683 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1684 && prev_family
== AF_UNSPEC
)
1687 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1688 && prev_family
!= AF_UNSPEC
)
1689 bgp_md5_unset(peer
);
1690 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1691 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1695 * If they are the same, nothing to do here, move along
1697 if (!sockunion_same(&old_su
, &peer
->su
)) {
1698 union sockunion new_su
= peer
->su
;
1699 struct bgp
*bgp
= peer
->bgp
;
1702 * Our peer structure is stored in the bgp->peerhash
1703 * release it before we modify anything in both the
1704 * hash and the list. But *only* if the peer
1705 * is in the bgp->peerhash as that on deletion
1706 * we call bgp_stop which calls this function :(
1707 * so on deletion let's remove from the list first
1708 * and then do the deletion preventing this from
1709 * being added back on the list below when we
1710 * fail to remove it up here.
1714 * listnode_lookup just scans the list
1715 * for the peer structure so it's safe
1716 * to use without modifying the su
1718 node
= listnode_lookup(bgp
->peer
, peer
);
1721 * Let's reset the peer->su release and
1722 * reset it and put it back. We have to
1723 * do this because hash_release will
1724 * scan through looking for a matching
1728 hash_release(peer
->bgp
->peerhash
, peer
);
1729 listnode_delete(peer
->bgp
->peer
, peer
);
1732 (void)hash_get(peer
->bgp
->peerhash
, peer
,
1734 listnode_add_sort(peer
->bgp
->peer
, peer
);
1739 void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1741 struct bgp_dest
*dest
, *ndest
;
1742 struct bgp_table
*table
;
1744 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1745 dest
= bgp_route_next(dest
)) {
1746 table
= bgp_dest_get_bgp_table_info(dest
);
1747 if (table
!= NULL
) {
1748 /* Special handling for 2-level routing
1750 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1751 || safi
== SAFI_EVPN
) {
1752 for (ndest
= bgp_table_top(table
); ndest
;
1753 ndest
= bgp_route_next(ndest
))
1754 bgp_process(bgp
, ndest
, afi
, safi
);
1756 bgp_process(bgp
, dest
, afi
, safi
);
1761 /* Force a bestpath recalculation for all prefixes. This is used
1762 * when 'bgp bestpath' commands are entered.
1764 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1769 FOREACH_AFI_SAFI (afi
, safi
) {
1770 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1775 * Create new BGP peer.
1777 * conf_if and su are mutually exclusive if configuring from the cli.
1778 * If we are handing a doppelganger, then we *must* pass in both
1779 * the original peer's su and conf_if, so that we can appropriately
1780 * track the bgp->peerhash( ie we don't want to remove the current
1781 * one from the config ).
1783 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1784 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1785 int as_type
, struct peer_group
*group
,
1786 bool config_node
, const char *as_str
)
1790 char buf
[SU_ADDRSTRLEN
];
1794 peer
= peer_new(bgp
);
1796 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1800 bgp_peer_conf_if_to_su_update(peer
);
1801 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1802 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1805 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1806 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1807 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1809 peer
->local_as
= local_as
;
1810 peer
->as
= remote_as
;
1811 /* internal and external values do not use as_pretty */
1812 if (as_str
&& asn_str2asn(as_str
, NULL
))
1813 peer
->as_pretty
= XSTRDUP(MTYPE_BGP
, as_str
);
1814 peer
->as_type
= as_type
;
1815 peer
->local_id
= bgp
->router_id
;
1816 peer
->v_holdtime
= bgp
->default_holdtime
;
1817 peer
->v_keepalive
= bgp
->default_keepalive
;
1818 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1819 ? BGP_DEFAULT_IBGP_ROUTEADV
1820 : BGP_DEFAULT_EBGP_ROUTEADV
;
1821 if (bgp_config_inprocess())
1822 peer
->shut_during_cfg
= true;
1824 peer
= peer_lock(peer
); /* bgp peer list reference */
1825 peer
->group
= group
;
1826 listnode_add_sort(bgp
->peer
, peer
);
1829 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1831 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1833 /* Adjust update-group coalesce timer heuristics for # peers. */
1834 if (bgp
->heuristic_coalesce
) {
1835 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1837 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1838 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1841 active
= peer_active(peer
);
1843 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1844 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1846 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1849 /* Last read and reset time set */
1850 peer
->readtime
= peer
->resettime
= monotime(NULL
);
1852 /* Default TTL set. */
1853 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1855 /* Default configured keepalives count for shutdown rtt command */
1856 peer
->rtt_keepalive_conf
= 1;
1858 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1859 * neighbor for the corresponding address family. IPv4 Unicast is
1860 * the only address family enabled by default without expliict
1863 FOREACH_AFI_SAFI (afi
, safi
) {
1864 if (bgp
->default_af
[afi
][safi
]) {
1865 peer
->afc
[afi
][safi
] = 1;
1866 peer_af_create(peer
, afi
, safi
);
1870 /* auto shutdown if configured */
1871 if (bgp
->autoshutdown
)
1872 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1873 /* Set up peer's events and timers. */
1874 else if (!active
&& peer_active(peer
))
1875 bgp_timer_set(peer
);
1877 bgp_peer_gr_flags_update(peer
);
1878 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1883 /* Make accept BGP peer. This function is only called from the test code */
1884 struct peer
*peer_create_accept(struct bgp
*bgp
)
1888 peer
= peer_new(bgp
);
1890 peer
= peer_lock(peer
); /* bgp peer list reference */
1891 listnode_add_sort(bgp
->peer
, peer
);
1892 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1898 * Return true if we have a peer configured to use this afi/safi
1900 bool bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1902 struct listnode
*node
;
1905 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1906 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1909 if (peer
->afc
[afi
][safi
])
1916 /* Change peer's AS number. */
1917 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
,
1920 enum bgp_peer_sort origtype
, newtype
;
1923 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1924 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1925 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1926 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1927 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1929 bgp_session_reset(peer
);
1931 origtype
= peer_sort_lookup(peer
);
1933 if (as_specified
== AS_SPECIFIED
&& as_str
) {
1934 if (peer
->as_pretty
)
1935 XFREE(MTYPE_BGP
, peer
->as_pretty
);
1936 peer
->as_pretty
= XSTRDUP(MTYPE_BGP
, as_str
);
1937 } else if (peer
->as_type
== AS_UNSPECIFIED
&& peer
->as_pretty
)
1938 XFREE(MTYPE_BGP
, peer
->as_pretty
);
1939 peer
->as_type
= as_specified
;
1941 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1942 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1943 && peer
->bgp
->as
!= as
)
1944 peer
->local_as
= peer
->bgp
->confed_id
;
1946 peer
->local_as
= peer
->bgp
->as
;
1948 newtype
= peer_sort(peer
);
1949 /* Advertisement-interval reset */
1950 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1951 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1952 ? BGP_DEFAULT_IBGP_ROUTEADV
1953 : BGP_DEFAULT_EBGP_ROUTEADV
;
1957 if (newtype
== BGP_PEER_IBGP
)
1959 else if (origtype
== BGP_PEER_IBGP
)
1960 peer
->ttl
= BGP_DEFAULT_TTL
;
1962 /* reflector-client reset */
1963 if (newtype
!= BGP_PEER_IBGP
) {
1964 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1965 PEER_FLAG_REFLECTOR_CLIENT
);
1966 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1967 PEER_FLAG_REFLECTOR_CLIENT
);
1968 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1969 PEER_FLAG_REFLECTOR_CLIENT
);
1970 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1971 PEER_FLAG_REFLECTOR_CLIENT
);
1972 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1973 PEER_FLAG_REFLECTOR_CLIENT
);
1974 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1975 PEER_FLAG_REFLECTOR_CLIENT
);
1976 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1977 PEER_FLAG_REFLECTOR_CLIENT
);
1978 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1979 PEER_FLAG_REFLECTOR_CLIENT
);
1980 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1981 PEER_FLAG_REFLECTOR_CLIENT
);
1982 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1983 PEER_FLAG_REFLECTOR_CLIENT
);
1984 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1985 PEER_FLAG_REFLECTOR_CLIENT
);
1986 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1987 PEER_FLAG_REFLECTOR_CLIENT
);
1988 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1989 PEER_FLAG_REFLECTOR_CLIENT
);
1993 /* If peer does not exist, create new one. If peer already exists,
1994 set AS number to the peer. */
1995 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1996 as_t
*as
, int as_type
, const char *as_str
)
2002 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
2004 peer
= peer_lookup(bgp
, su
);
2007 /* Not allowed for a dynamic peer. */
2008 if (peer_dynamic_neighbor(peer
)) {
2010 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
2013 /* When this peer is a member of peer-group. */
2015 /* peer-group already has AS number/internal/external */
2016 if (peer
->group
->conf
->as
2017 || peer
->group
->conf
->as_type
) {
2018 /* Return peer group's AS number. */
2019 *as
= peer
->group
->conf
->as
;
2020 return BGP_ERR_PEER_GROUP_MEMBER
;
2023 enum bgp_peer_sort peer_sort_type
=
2024 peer_sort(peer
->group
->conf
);
2026 /* Explicit AS numbers used, compare AS numbers */
2027 if (as_type
== AS_SPECIFIED
) {
2028 if (((peer_sort_type
== BGP_PEER_IBGP
)
2029 && (bgp
->as
!= *as
))
2030 || ((peer_sort_type
== BGP_PEER_EBGP
)
2031 && (bgp
->as
== *as
))) {
2033 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2036 /* internal/external used, compare as-types */
2037 if (((peer_sort_type
== BGP_PEER_IBGP
)
2038 && (as_type
!= AS_INTERNAL
))
2039 || ((peer_sort_type
== BGP_PEER_EBGP
)
2040 && (as_type
!= AS_EXTERNAL
))) {
2042 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2047 /* Existing peer's AS number change. */
2048 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2049 || (peer
->as_type
!= as_type
))
2050 peer_as_change(peer
, *as
, as_type
, as_str
);
2053 return BGP_ERR_NO_INTERFACE_CONFIG
;
2055 /* If the peer is not part of our confederation, and its not an
2056 iBGP peer then spoof the source AS */
2057 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
2058 && !bgp_confederation_peers_check(bgp
, *as
)
2060 local_as
= bgp
->confed_id
;
2064 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
,
2071 const char *bgp_get_name_by_role(uint8_t role
)
2076 case ROLE_RS_SERVER
:
2078 case ROLE_RS_CLIENT
:
2084 case ROLE_UNDEFINED
:
2090 enum asnotation_mode
bgp_get_asnotation(struct bgp
*bgp
)
2093 return ASNOTATION_PLAIN
;
2094 return bgp
->asnotation
;
2097 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
2098 struct peer
*peer
, afi_t afi
,
2102 int out
= FILTER_OUT
;
2104 uint64_t pflags_ovrd
;
2105 uint8_t *pfilter_ovrd
;
2109 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2110 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2112 /* peer af_flags apply */
2113 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2114 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2115 ^ peer
->af_flags_invert
[afi
][safi
];
2116 flags_tmp
&= ~pflags_ovrd
;
2118 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2119 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2120 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2121 conf
->af_flags_invert
[afi
][safi
]);
2123 /* maximum-prefix */
2124 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2125 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2126 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2127 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2130 /* maximum-prefix-out */
2131 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX_OUT
))
2132 PEER_ATTR_INHERIT(peer
, group
, pmax_out
[afi
][safi
]);
2135 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2136 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2139 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_SOO
))
2140 PEER_ATTR_INHERIT(peer
, group
, soo
[afi
][safi
]);
2143 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2144 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2146 /* default-originate route-map */
2147 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2148 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2149 MTYPE_ROUTE_MAP_NAME
);
2150 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2153 /* inbound filter apply */
2154 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2155 PEER_STR_ATTR_INHERIT(peer
, group
,
2156 filter
[afi
][safi
].dlist
[in
].name
,
2157 MTYPE_BGP_FILTER_NAME
);
2158 PEER_ATTR_INHERIT(peer
, group
,
2159 filter
[afi
][safi
].dlist
[in
].alist
);
2162 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2163 PEER_STR_ATTR_INHERIT(peer
, group
,
2164 filter
[afi
][safi
].plist
[in
].name
,
2165 MTYPE_BGP_FILTER_NAME
);
2166 PEER_ATTR_INHERIT(peer
, group
,
2167 filter
[afi
][safi
].plist
[in
].plist
);
2170 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2171 PEER_STR_ATTR_INHERIT(peer
, group
,
2172 filter
[afi
][safi
].aslist
[in
].name
,
2173 MTYPE_BGP_FILTER_NAME
);
2174 PEER_ATTR_INHERIT(peer
, group
,
2175 filter
[afi
][safi
].aslist
[in
].aslist
);
2178 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2179 PEER_STR_ATTR_INHERIT(peer
, group
,
2180 filter
[afi
][safi
].map
[in
].name
,
2181 MTYPE_BGP_FILTER_NAME
);
2182 PEER_ATTR_INHERIT(peer
, group
,
2183 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2186 /* outbound filter apply */
2187 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2188 PEER_STR_ATTR_INHERIT(peer
, group
,
2189 filter
[afi
][safi
].dlist
[out
].name
,
2190 MTYPE_BGP_FILTER_NAME
);
2191 PEER_ATTR_INHERIT(peer
, group
,
2192 filter
[afi
][safi
].dlist
[out
].alist
);
2195 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2196 PEER_STR_ATTR_INHERIT(peer
, group
,
2197 filter
[afi
][safi
].plist
[out
].name
,
2198 MTYPE_BGP_FILTER_NAME
);
2199 PEER_ATTR_INHERIT(peer
, group
,
2200 filter
[afi
][safi
].plist
[out
].plist
);
2203 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2204 PEER_STR_ATTR_INHERIT(peer
, group
,
2205 filter
[afi
][safi
].aslist
[out
].name
,
2206 MTYPE_BGP_FILTER_NAME
);
2207 PEER_ATTR_INHERIT(peer
, group
,
2208 filter
[afi
][safi
].aslist
[out
].aslist
);
2211 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2212 PEER_STR_ATTR_INHERIT(peer
, group
,
2213 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2214 MTYPE_BGP_FILTER_NAME
);
2215 PEER_ATTR_INHERIT(peer
, group
,
2216 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2219 /* nondirectional filter apply */
2220 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2221 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2222 MTYPE_BGP_FILTER_NAME
);
2223 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2226 /* Conditional Advertisements */
2227 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ADVERTISE_MAP
)) {
2228 PEER_STR_ATTR_INHERIT(peer
, group
,
2229 filter
[afi
][safi
].advmap
.aname
,
2230 MTYPE_BGP_FILTER_NAME
);
2231 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.amap
);
2232 PEER_STR_ATTR_INHERIT(peer
, group
,
2233 filter
[afi
][safi
].advmap
.cname
,
2234 MTYPE_BGP_FILTER_NAME
);
2235 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.cmap
);
2236 PEER_ATTR_INHERIT(peer
, group
,
2237 filter
[afi
][safi
].advmap
.condition
);
2240 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2241 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2242 bgp_addpath_type_changed(conf
->bgp
);
2246 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2251 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2252 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2253 __func__
, peer
->host
);
2257 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2259 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2260 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2261 return BGP_ERR_PEER_SAFI_CONFLICT
;
2263 /* Nothing to do if we've already activated this peer */
2264 if (peer
->afc
[afi
][safi
])
2267 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2270 active
= peer_active(peer
);
2271 peer
->afc
[afi
][safi
] = 1;
2274 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2276 if (!active
&& peer_active(peer
)) {
2277 bgp_timer_set(peer
);
2279 if (peer_established(peer
)) {
2280 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2281 peer
->afc_adv
[afi
][safi
] = 1;
2282 bgp_capability_send(peer
, afi
, safi
,
2284 CAPABILITY_ACTION_SET
);
2285 if (peer
->afc_recv
[afi
][safi
]) {
2286 peer
->afc_nego
[afi
][safi
] = 1;
2287 bgp_announce_route(peer
, afi
, safi
,
2291 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2292 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2293 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2296 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2297 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2298 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2299 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2302 * If we are turning on a AFI/SAFI locally and we've
2303 * started bringing a peer up, we need to tell
2304 * the other peer to restart because we might loose
2305 * configuration here because when the doppelganger
2306 * gets to a established state due to how
2307 * we resolve we could just overwrite the afi/safi
2310 other
= peer
->doppelganger
;
2312 && (other
->status
== OpenSent
2313 || other
->status
== OpenConfirm
)) {
2314 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2315 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2316 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2323 /* Activate the peer or peer group for specified AFI and SAFI. */
2324 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2327 struct peer_group
*group
;
2328 struct listnode
*node
, *nnode
;
2329 struct peer
*tmp_peer
;
2332 /* Nothing to do if we've already activated this peer */
2333 if (peer
->afc
[afi
][safi
])
2338 /* This is a peer-group so activate all of the members of the
2339 * peer-group as well */
2340 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2342 /* Do not activate a peer for both SAFI_UNICAST and
2343 * SAFI_LABELED_UNICAST */
2344 if ((safi
== SAFI_UNICAST
2345 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2346 || (safi
== SAFI_LABELED_UNICAST
2347 && peer
->afc
[afi
][SAFI_UNICAST
]))
2348 return BGP_ERR_PEER_SAFI_CONFLICT
;
2350 peer
->afc
[afi
][safi
] = 1;
2351 group
= peer
->group
;
2353 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2354 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2357 ret
|= peer_activate_af(peer
, afi
, safi
);
2360 /* If this is the first peer to be activated for this
2361 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2362 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2363 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2365 if (BGP_DEBUG(zebra
, ZEBRA
))
2367 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2369 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2370 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2373 if (safi
== SAFI_FLOWSPEC
) {
2374 /* connect to table manager */
2375 bgp_zebra_init_tm_connect(bgp
);
2380 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2383 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2384 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2385 __func__
, peer
->host
);
2389 /* Nothing to do if we've already deactivated this peer */
2390 if (!peer
->afc
[afi
][safi
])
2393 /* De-activate the address family configuration. */
2394 peer
->afc
[afi
][safi
] = 0;
2396 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2397 flog_err(EC_BGP_PEER_DELETE
,
2398 "couldn't delete af structure for peer %s(%s, %s)",
2399 peer
->host
, afi2str(afi
), safi2str(safi
));
2403 if (peer_established(peer
)) {
2404 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2405 peer
->afc_adv
[afi
][safi
] = 0;
2406 peer
->afc_nego
[afi
][safi
] = 0;
2408 if (peer_active_nego(peer
)) {
2409 bgp_capability_send(peer
, afi
, safi
,
2411 CAPABILITY_ACTION_UNSET
);
2412 bgp_clear_route(peer
, afi
, safi
);
2413 peer
->pcount
[afi
][safi
] = 0;
2415 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2416 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2417 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2420 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2421 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2422 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2429 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2432 struct peer_group
*group
;
2433 struct peer
*tmp_peer
;
2434 struct listnode
*node
, *nnode
;
2437 /* Nothing to do if we've already de-activated this peer */
2438 if (!peer
->afc
[afi
][safi
])
2441 /* This is a peer-group so de-activate all of the members of the
2442 * peer-group as well */
2443 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2444 peer
->afc
[afi
][safi
] = 0;
2445 group
= peer
->group
;
2447 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2448 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2451 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2456 /* If this is the last peer to be deactivated for this
2457 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2458 if (safi
== SAFI_LABELED_UNICAST
2459 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2460 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2462 if (BGP_DEBUG(zebra
, ZEBRA
))
2464 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2466 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2467 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2472 void peer_nsf_stop(struct peer
*peer
)
2477 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2478 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2480 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2481 peer
->nsf
[afi
][safi
] = 0;
2482 THREAD_OFF(peer
->t_llgr_stale
[afi
][safi
]);
2485 if (peer
->t_gr_restart
) {
2486 THREAD_OFF(peer
->t_gr_restart
);
2487 if (bgp_debug_neighbor_events(peer
))
2488 zlog_debug("%pBP graceful restart timer stopped", peer
);
2490 if (peer
->t_gr_stale
) {
2491 THREAD_OFF(peer
->t_gr_stale
);
2492 if (bgp_debug_neighbor_events(peer
))
2494 "%pBP graceful restart stalepath timer stopped",
2497 bgp_clear_route_all(peer
);
2500 /* Delete peer from confguration.
2502 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2503 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2505 * This function /should/ take care to be idempotent, to guard against
2506 * it being called multiple times through stray events that come in
2507 * that happen to result in this function being called again. That
2508 * said, getting here for a "Deleted" peer is a bug in the neighbour
2511 int peer_delete(struct peer
*peer
)
2517 struct bgp_filter
*filter
;
2518 struct listnode
*pn
;
2521 assert(peer
->status
!= Deleted
);
2524 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2526 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2528 bgp_keepalives_off(peer
);
2529 bgp_reads_off(peer
);
2530 bgp_writes_off(peer
);
2531 thread_cancel_event_ready(bm
->master
, peer
);
2532 FOREACH_AFI_SAFI (afi
, safi
)
2533 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
2534 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2535 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2536 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2538 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2539 peer_nsf_stop(peer
);
2541 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2543 /* Remove BFD settings. */
2544 if (peer
->bfd_config
)
2545 bgp_peer_remove_bfd_config(peer
);
2547 /* If this peer belongs to peer group, clear up the
2550 if (peer_dynamic_neighbor(peer
))
2551 peer_drop_dynamic_neighbor(peer
);
2553 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2555 peer
); /* group->peer list reference */
2556 list_delete_node(peer
->group
->peer
, pn
);
2561 /* Withdraw all information from routing table. We can not use
2562 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2563 * executed after peer structure is deleted.
2565 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2567 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2569 if (peer
->doppelganger
) {
2570 peer
->doppelganger
->doppelganger
= NULL
;
2571 peer
->doppelganger
= NULL
;
2574 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2575 bgp_fsm_change_status(peer
, Deleted
);
2577 /* Remove from NHT */
2578 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2579 bgp_unlink_nexthop_by_peer(peer
);
2581 /* Password configuration */
2582 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2583 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2584 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2585 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2586 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2587 bgp_md5_unset(peer
);
2590 bgp_timer_set(peer
); /* stops all timers for Deleted */
2592 /* Delete from all peer list. */
2593 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2594 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2596 * Removing from the list node first because
2597 * peer_unlock *can* call peer_delete( I know,
2598 * I know ). So let's remove it and in
2599 * the su recalculate function we'll ensure
2600 * it's in there or not.
2602 list_delete_node(bgp
->peer
, pn
);
2603 hash_release(bgp
->peerhash
, peer
);
2604 peer_unlock(peer
); /* bgp peer list reference */
2609 stream_fifo_free(peer
->ibuf
);
2614 stream_fifo_free(peer
->obuf
);
2618 if (peer
->ibuf_work
) {
2619 ringbuf_del(peer
->ibuf_work
);
2620 peer
->ibuf_work
= NULL
;
2623 if (peer
->obuf_work
) {
2624 stream_free(peer
->obuf_work
);
2625 peer
->obuf_work
= NULL
;
2628 if (peer
->scratch
) {
2629 stream_free(peer
->scratch
);
2630 peer
->scratch
= NULL
;
2633 /* Local and remote addresses. */
2634 if (peer
->su_local
) {
2635 sockunion_free(peer
->su_local
);
2636 peer
->su_local
= NULL
;
2639 if (peer
->su_remote
) {
2640 sockunion_free(peer
->su_remote
);
2641 peer
->su_remote
= NULL
;
2644 /* Free filter related memory. */
2645 FOREACH_AFI_SAFI (afi
, safi
) {
2646 filter
= &peer
->filter
[afi
][safi
];
2648 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2649 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2650 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2651 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2654 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2655 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2658 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2659 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2660 ecommunity_free(&peer
->soo
[afi
][safi
]);
2663 FOREACH_AFI_SAFI (afi
, safi
)
2664 peer_af_delete(peer
, afi
, safi
);
2666 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2667 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2669 peer_unlock(peer
); /* initial reference */
2674 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2676 return strcmp(g1
->name
, g2
->name
);
2679 /* Peer group cofiguration. */
2680 static struct peer_group
*peer_group_new(void)
2682 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2685 static void peer_group_free(struct peer_group
*group
)
2687 XFREE(MTYPE_PEER_GROUP
, group
);
2690 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2692 struct peer_group
*group
;
2693 struct listnode
*node
, *nnode
;
2695 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2696 if (strcmp(group
->name
, name
) == 0)
2702 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2704 struct peer_group
*group
;
2708 group
= peer_group_lookup(bgp
, name
);
2712 group
= peer_group_new();
2714 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2715 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2716 group
->peer
= list_new();
2717 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2718 group
->listen_range
[afi
] = list_new();
2719 group
->conf
= peer_new(bgp
);
2720 FOREACH_AFI_SAFI (afi
, safi
) {
2721 if (bgp
->default_af
[afi
][safi
])
2722 group
->conf
->afc
[afi
][safi
] = 1;
2724 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2725 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2726 group
->conf
->group
= group
;
2727 group
->conf
->as
= 0;
2728 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2729 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2730 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2731 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2732 listnode_add_sort(bgp
->group
, group
);
2737 static void peer_group2peer_config_copy(struct peer_group
*group
,
2742 bool config_node
= !!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2748 peer
->as
= conf
->as
;
2751 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2752 peer
->change_local_as
= conf
->change_local_as
;
2754 /* If peer-group has configured TTL then override it */
2755 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2756 peer
->ttl
= conf
->ttl
;
2759 peer
->gtsm_hops
= conf
->gtsm_hops
;
2761 /* peer flags apply */
2762 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2763 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2764 flags_tmp
&= ~peer
->flags_override
;
2766 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2767 SET_FLAG(peer
->flags
, flags_tmp
);
2768 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2771 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2773 /* peer timers apply */
2774 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2775 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2776 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2779 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2780 PEER_ATTR_INHERIT(peer
, group
, connect
);
2781 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2782 peer
->v_connect
= conf
->connect
;
2784 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2787 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2788 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2789 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2790 peer
->v_delayopen
= conf
->delayopen
;
2792 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2795 /* advertisement-interval apply */
2796 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2797 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2798 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2799 peer
->v_routeadv
= conf
->routeadv
;
2801 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2802 ? BGP_DEFAULT_IBGP_ROUTEADV
2803 : BGP_DEFAULT_EBGP_ROUTEADV
;
2806 /* capability extended-nexthop apply */
2807 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2808 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2809 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2811 /* password apply */
2812 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2813 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2814 MTYPE_PEER_PASSWORD
);
2816 if (!BGP_PEER_SU_UNSPEC(peer
))
2819 /* update-source apply */
2820 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2821 if (conf
->update_source
) {
2822 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2823 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2824 } else if (conf
->update_if
) {
2825 sockunion_free(peer
->update_source
);
2826 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2827 MTYPE_PEER_UPDATE_SOURCE
);
2832 PEER_ATTR_INHERIT(peer
, group
, local_role
);
2834 /* Update GR flags for the peer. */
2835 bgp_peer_gr_flags_update(peer
);
2837 /* Apply BFD settings from group to peer if it exists. */
2838 if (conf
->bfd_config
) {
2839 bgp_peer_configure_bfd(peer
, false);
2840 bgp_peer_config_apply(peer
, group
);
2844 /* Peer group's remote AS configuration. */
2845 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2846 int as_type
, const char *as_str
)
2848 struct peer_group
*group
;
2850 struct listnode
*node
, *nnode
;
2852 group
= peer_group_lookup(bgp
, group_name
);
2856 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2860 /* When we setup peer-group AS number all peer group member's AS
2861 number must be updated to same number. */
2862 peer_as_change(group
->conf
, *as
, as_type
, as_str
);
2864 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2865 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2866 || (peer
->as_type
!= as_type
))
2867 peer_as_change(peer
, *as
, as_type
, as_str
);
2873 void peer_notify_unconfig(struct peer
*peer
)
2875 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2876 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2877 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2880 static void peer_notify_shutdown(struct peer
*peer
)
2882 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
2883 if (bgp_debug_neighbor_events(peer
))
2885 "%pBP configured Graceful-Restart, skipping shutdown notification",
2890 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2891 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2892 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
2895 void peer_group_notify_unconfig(struct peer_group
*group
)
2897 struct peer
*peer
, *other
;
2898 struct listnode
*node
, *nnode
;
2900 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2901 other
= peer
->doppelganger
;
2902 if (other
&& other
->status
!= Deleted
) {
2903 other
->group
= NULL
;
2904 peer_notify_unconfig(other
);
2906 peer_notify_unconfig(peer
);
2910 int peer_group_delete(struct peer_group
*group
)
2914 struct prefix
*prefix
;
2916 struct listnode
*node
, *nnode
;
2921 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2922 other
= peer
->doppelganger
;
2924 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2925 bgp_zebra_terminate_radv(bgp
, peer
);
2928 if (other
&& other
->status
!= Deleted
) {
2929 other
->group
= NULL
;
2933 list_delete(&group
->peer
);
2935 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2936 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2938 prefix_free(&prefix
);
2940 list_delete(&group
->listen_range
[afi
]);
2943 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2946 if (group
->conf
->bfd_config
)
2947 bgp_peer_remove_bfd_config(group
->conf
);
2949 group
->conf
->group
= NULL
;
2950 peer_delete(group
->conf
);
2952 /* Delete from all peer_group list. */
2953 listnode_delete(bgp
->group
, group
);
2955 peer_group_free(group
);
2960 int peer_group_remote_as_delete(struct peer_group
*group
)
2962 struct peer
*peer
, *other
;
2963 struct listnode
*node
, *nnode
;
2965 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2966 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2969 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2970 other
= peer
->doppelganger
;
2972 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2973 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2977 if (other
&& other
->status
!= Deleted
) {
2978 other
->group
= NULL
;
2982 list_delete_all_node(group
->peer
);
2984 group
->conf
->as
= 0;
2985 group
->conf
->as_type
= AS_UNSPECIFIED
;
2990 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2992 struct prefix
*prefix
;
2993 struct listnode
*node
, *nnode
;
2996 afi
= family2afi(range
->family
);
2998 /* Group needs remote AS configured. */
2999 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
3000 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3002 /* Ensure no duplicates. Currently we don't care about overlaps. */
3003 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
3004 if (prefix_same(range
, prefix
))
3008 prefix
= prefix_new();
3009 prefix_copy(prefix
, range
);
3010 listnode_add(group
->listen_range
[afi
], prefix
);
3012 /* Update passwords for new ranges */
3013 if (group
->conf
->password
)
3014 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
3019 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
3021 struct prefix
*prefix
, prefix2
;
3022 struct listnode
*node
, *nnode
;
3026 afi
= family2afi(range
->family
);
3028 /* Identify the listen range. */
3029 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
3030 if (prefix_same(range
, prefix
))
3035 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
3037 /* Dispose off any dynamic neighbors that exist due to this listen range
3039 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
3040 if (!peer_dynamic_neighbor(peer
))
3043 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
3044 && prefix_match(prefix
, &prefix2
)) {
3045 if (bgp_debug_neighbor_events(peer
))
3047 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
3048 peer
->host
, group
->name
, prefix
);
3053 /* Get rid of the listen range */
3054 listnode_delete(group
->listen_range
[afi
], prefix
);
3056 /* Remove passwords for deleted ranges */
3057 if (group
->conf
->password
)
3058 bgp_md5_unset_prefix(group
->bgp
, prefix
);
3063 /* Bind specified peer to peer group. */
3064 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
3065 struct peer_group
*group
, as_t
*as
)
3067 int first_member
= 0;
3070 enum bgp_peer_sort ptype
, gtype
;
3072 /* Lookup the peer. */
3074 peer
= peer_lookup(bgp
, su
);
3076 /* The peer exist, bind it to the peer-group */
3078 /* When the peer already belongs to a peer-group, check the
3080 if (peer_group_active(peer
)) {
3082 /* The peer is already bound to the peer-group,
3085 if (strcmp(peer
->group
->name
, group
->name
) == 0)
3088 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
3091 /* The peer has not specified a remote-as, inherit it from the
3093 if (peer
->as_type
== AS_UNSPECIFIED
) {
3094 peer
->as_type
= group
->conf
->as_type
;
3095 peer
->as
= group
->conf
->as
;
3096 peer
->sort
= group
->conf
->sort
;
3099 ptype
= peer_sort(peer
);
3100 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
3101 gtype
= peer_sort(group
->conf
);
3102 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
3105 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
3108 if (gtype
== BGP_PEER_INTERNAL
)
3112 peer_group2peer_config_copy(group
, peer
);
3114 FOREACH_AFI_SAFI (afi
, safi
) {
3115 if (group
->conf
->afc
[afi
][safi
]) {
3116 peer
->afc
[afi
][safi
] = 1;
3118 if (peer_af_find(peer
, afi
, safi
)
3119 || peer_af_create(peer
, afi
, safi
)) {
3120 peer_group2peer_config_copy_af(
3121 group
, peer
, afi
, safi
);
3123 } else if (peer
->afc
[afi
][safi
])
3124 peer_deactivate(peer
, afi
, safi
);
3128 assert(group
&& peer
->group
== group
);
3130 listnode_delete(bgp
->peer
, peer
);
3132 peer
->group
= group
;
3133 listnode_add_sort(bgp
->peer
, peer
);
3135 peer
= peer_lock(peer
); /* group->peer list reference */
3136 listnode_add(group
->peer
, peer
);
3140 gtype
= peer_sort(group
->conf
);
3141 /* Advertisement-interval reset */
3142 if (!CHECK_FLAG(group
->conf
->flags
,
3143 PEER_FLAG_ROUTEADV
)) {
3144 group
->conf
->v_routeadv
=
3145 (gtype
== BGP_PEER_IBGP
)
3146 ? BGP_DEFAULT_IBGP_ROUTEADV
3147 : BGP_DEFAULT_EBGP_ROUTEADV
;
3150 /* ebgp-multihop reset */
3151 if (gtype
== BGP_PEER_IBGP
)
3152 group
->conf
->ttl
= MAXTTL
;
3155 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3157 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3158 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3159 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3160 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3162 bgp_session_reset(peer
);
3166 /* Create a new peer. */
3168 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3169 && (!group
->conf
->as
)) {
3170 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3173 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3174 group
->conf
->as_type
, group
, true, NULL
);
3176 peer
= peer_lock(peer
); /* group->peer list reference */
3177 listnode_add(group
->peer
, peer
);
3179 peer_group2peer_config_copy(group
, peer
);
3181 /* If the peer-group is active for this afi/safi then activate
3183 FOREACH_AFI_SAFI (afi
, safi
) {
3184 if (group
->conf
->afc
[afi
][safi
]) {
3185 peer
->afc
[afi
][safi
] = 1;
3187 if (!peer_af_find(peer
, afi
, safi
))
3188 peer_af_create(peer
, afi
, safi
);
3190 peer_group2peer_config_copy_af(group
, peer
, afi
,
3192 } else if (peer
->afc
[afi
][safi
])
3193 peer_deactivate(peer
, afi
, safi
);
3196 /* Set up peer's events and timers. */
3197 if (peer_active(peer
))
3198 bgp_timer_set(peer
);
3204 static void bgp_startup_timer_expire(struct thread
*thread
)
3208 bgp
= THREAD_ARG(thread
);
3209 bgp
->t_startup
= NULL
;
3213 * On shutdown we call the cleanup function which
3214 * does a free of the link list nodes, free up
3215 * the data we are pointing at too.
3217 static void bgp_vrf_string_name_delete(void *data
)
3221 XFREE(MTYPE_TMP
, vname
);
3224 /* BGP instance creation by `router bgp' commands. */
3225 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3226 enum bgp_instance_type inst_type
,
3227 const char *as_pretty
,
3228 enum asnotation_mode asnotation
)
3234 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3237 bgp
->as_pretty
= XSTRDUP(MTYPE_BGP
, as_pretty
);
3239 bgp
->as_pretty
= XSTRDUP(MTYPE_BGP
, asn_asn2asplain(*as
));
3241 if (asnotation
!= ASNOTATION_UNDEFINED
) {
3242 bgp
->asnotation
= asnotation
;
3243 SET_FLAG(bgp
->config
, BGP_CONFIG_ASNOTATION
);
3245 asn_str2asn_notation(bgp
->as_pretty
, NULL
, &bgp
->asnotation
);
3247 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3248 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3249 zlog_debug("Creating Default VRF, AS %s",
3252 zlog_debug("Creating %s %s, AS %s",
3253 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3256 name
, bgp
->as_pretty
);
3259 /* Default the EVPN VRF to the default one */
3260 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3267 bgp
->allow_martian
= false;
3268 bgp_process_queue_init(bgp
);
3269 bgp
->heuristic_coalesce
= true;
3270 bgp
->inst_type
= inst_type
;
3271 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3273 bgp
->peer_self
= peer_new(bgp
);
3274 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3275 bgp
->peer_self
->host
=
3276 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3277 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3278 if (cmd_hostname_get())
3279 bgp
->peer_self
->hostname
=
3280 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3282 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3283 if (cmd_domainname_get())
3284 bgp
->peer_self
->domainname
=
3285 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3286 bgp
->peer
= list_new();
3287 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3288 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3290 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3292 bgp
->group
= list_new();
3293 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3295 FOREACH_AFI_SAFI (afi
, safi
) {
3296 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3297 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3298 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3300 /* Enable maximum-paths */
3301 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3303 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3305 /* Initialize graceful restart info */
3306 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3307 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3308 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3309 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3310 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3313 bgp
->v_update_delay
= bm
->v_update_delay
;
3314 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3315 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3316 bgp
->default_subgroup_pkt_queue_max
=
3317 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3318 bgp_tcp_keepalive_unset(bgp
);
3319 bgp_timers_unset(bgp
);
3320 bgp
->default_min_holdtime
= 0;
3321 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3322 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3323 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3324 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3325 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3326 bgp
->dynamic_neighbors_count
= 0;
3327 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3328 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3329 bgp
->reject_as_sets
= false;
3330 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3331 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3332 bgp
->fast_convergence
= false;
3333 bgp
->llgr_stale_time
= BGP_DEFAULT_LLGR_STALE_TIME
;
3335 #ifdef ENABLE_BGP_VNC
3336 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3337 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3339 assert(bgp
->rfapi_cfg
);
3341 #endif /* ENABLE_BGP_VNC */
3343 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3344 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3345 bgp
->vpn_policy
[afi
].afi
= afi
;
3346 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3347 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3350 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3351 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3352 bgp_vrf_string_name_delete
;
3353 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3354 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3355 bgp_vrf_string_name_delete
;
3356 SET_FLAG(bgp
->af_flags
[afi
][SAFI_MPLS_VPN
],
3357 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL
);
3360 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3362 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3363 bgp
->restart_time
, &bgp
->t_startup
);
3365 /* printable name we can use in debug messages */
3366 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3367 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3377 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3379 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3380 snprintf(bgp
->name_pretty
, len
, "%s %s",
3381 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3387 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3388 memory_order_relaxed
);
3389 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3390 memory_order_relaxed
);
3391 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3392 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3396 update_bgp_group_init(bgp
);
3398 /* assign a unique rd id for auto derivation of vrf's RD */
3399 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3401 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3402 sizeof(struct bgp_evpn_info
));
3404 bgp_evpn_vrf_es_init(bgp
);
3408 /*initilize global GR FSM */
3409 bgp_global_gr_init(bgp
);
3411 memset(&bgp
->ebgprequirespolicywarning
, 0,
3412 sizeof(bgp
->ebgprequirespolicywarning
));
3417 /* Return the "default VRF" instance of BGP. */
3418 struct bgp
*bgp_get_default(void)
3421 struct listnode
*node
, *nnode
;
3423 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3424 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3429 /* Lookup BGP entry. */
3430 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3433 struct listnode
*node
, *nnode
;
3435 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3437 && ((bgp
->name
== NULL
&& name
== NULL
)
3438 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3443 /* Lookup BGP structure by view name. */
3444 struct bgp
*bgp_lookup_by_name(const char *name
)
3447 struct listnode
*node
, *nnode
;
3449 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3450 if ((bgp
->name
== NULL
&& name
== NULL
)
3451 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3456 /* Lookup BGP instance based on VRF id. */
3457 /* Note: Only to be used for incoming messages from Zebra. */
3458 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3462 /* Lookup VRF (in tree) and follow link. */
3463 vrf
= vrf_lookup_by_id(vrf_id
);
3466 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3469 /* Sets the BGP instance where EVPN is enabled */
3470 void bgp_set_evpn(struct bgp
*bgp
)
3472 if (bm
->bgp_evpn
== bgp
)
3475 /* First, release the reference count we hold on the instance */
3477 bgp_unlock(bm
->bgp_evpn
);
3481 /* Increase the reference count on this new VRF */
3483 bgp_lock(bm
->bgp_evpn
);
3486 /* Returns the BGP instance where EVPN is enabled, if any */
3487 struct bgp
*bgp_get_evpn(void)
3489 return bm
->bgp_evpn
;
3492 /* handle socket creation or deletion, if necessary
3493 * this is called for all new BGP instances
3495 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3498 struct listnode
*node
;
3501 /* Create BGP server socket, if listen mode not disabled */
3502 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3504 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3506 * suppress vrf socket
3509 bgp_close_vrf_socket(bgp
);
3513 return BGP_ERR_INVALID_VALUE
;
3515 * if vrf_id did not change
3517 if (vrf
->vrf_id
== old_vrf_id
)
3519 if (old_vrf_id
!= VRF_UNKNOWN
) {
3520 /* look for old socket. close it. */
3521 bgp_close_vrf_socket(bgp
);
3523 /* if backend is not yet identified ( VRF_UNKNOWN) then
3524 * creation will be done later
3526 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3528 if (list_isempty(bm
->addresses
)) {
3529 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3530 return BGP_ERR_INVALID_VALUE
;
3532 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3533 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3534 return BGP_ERR_INVALID_VALUE
;
3538 return bgp_check_main_socket(create
, bgp
);
3541 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3542 enum bgp_instance_type inst_type
)
3546 /* Multiple instance check. */
3548 bgp
= bgp_lookup_by_name(name
);
3550 bgp
= bgp_get_default();
3554 if (bgp
->as
!= *as
) {
3556 return BGP_ERR_AS_MISMATCH
;
3558 if (bgp
->inst_type
!= inst_type
)
3559 return BGP_ERR_INSTANCE_MISMATCH
;
3567 /* Called from VTY commands. */
3568 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3569 enum bgp_instance_type inst_type
, const char *as_pretty
,
3570 enum asnotation_mode asnotation
)
3573 struct vrf
*vrf
= NULL
;
3576 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3577 if (ret
|| *bgp_val
)
3580 bgp
= bgp_create(as
, name
, inst_type
, as_pretty
, asnotation
);
3583 * view instances will never work inside of a vrf
3584 * as such they must always be in the VRF_DEFAULT
3585 * Also we must set this to something useful because
3586 * of the vrf socket code needing an actual useful
3587 * default value to send to the underlying OS.
3589 * This code is currently ignoring vrf based
3590 * code using the -Z option( and that is probably
3591 * best addressed elsewhere in the code )
3593 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3594 bgp
->vrf_id
= VRF_DEFAULT
;
3596 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3597 bgp_address_init(bgp
);
3598 bgp_tip_hash_init(bgp
);
3602 bgp
->t_rmap_def_originate_eval
= NULL
;
3604 /* If Default instance or VRF, link to the VRF structure, if present. */
3605 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3606 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3607 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3609 bgp_vrf_link(bgp
, vrf
);
3611 /* BGP server socket already processed if BGP instance
3612 * already part of the list
3614 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3615 listnode_add(bm
->bgp
, bgp
);
3617 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3618 if (BGP_DEBUG(zebra
, ZEBRA
))
3619 zlog_debug("%s: Registering BGP instance %s to zebra",
3620 __func__
, bgp
->name_pretty
);
3621 bgp_zebra_instance_register(bgp
);
3627 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3628 vrf_id_t vrf_id
, bool set
)
3632 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3635 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3639 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3641 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3645 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3649 struct list
*red_list
;
3650 struct listnode
*node
;
3651 struct bgp_redist
*red
;
3653 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3654 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3656 red_list
= bgp
->redist
[afi
][i
];
3660 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3661 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3668 * Make BGP instance "up". Applies only to VRFs (non-default) and
3669 * implies the VRF has been learnt from Zebra.
3671 void bgp_instance_up(struct bgp
*bgp
)
3674 struct listnode
*node
, *next
;
3676 bgp_set_redist_vrf_bitmaps(bgp
, true);
3678 /* Register with zebra. */
3679 bgp_zebra_instance_register(bgp
);
3681 /* Kick off any peers that may have been configured. */
3682 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3683 if (!BGP_PEER_START_SUPPRESSED(peer
))
3684 BGP_EVENT_ADD(peer
, BGP_Start
);
3687 /* Process any networks that have been configured. */
3688 bgp_static_add(bgp
);
3692 * Make BGP instance "down". Applies only to VRFs (non-default) and
3693 * implies the VRF has been deleted by Zebra.
3695 void bgp_instance_down(struct bgp
*bgp
)
3698 struct listnode
*node
;
3699 struct listnode
*next
;
3702 if (bgp
->t_rmap_def_originate_eval
) {
3703 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3704 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3708 /* Bring down peers, so corresponding routes are purged. */
3709 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3710 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3711 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3712 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3714 bgp_session_reset(peer
);
3717 /* Purge network and redistributed routes. */
3718 bgp_purge_static_redist_routes(bgp
);
3720 /* Cleanup registered nexthops (flags) */
3721 bgp_cleanup_nexthops(bgp
);
3723 bgp_zebra_instance_deregister(bgp
);
3725 bgp_set_redist_vrf_bitmaps(bgp
, false);
3728 /* Delete BGP instance. */
3729 int bgp_delete(struct bgp
*bgp
)
3732 struct peer_group
*group
;
3733 struct listnode
*node
, *next
;
3738 struct graceful_restart_info
*gr_info
;
3742 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3744 /* make sure we withdraw any exported routes */
3745 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3747 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3750 bgp_vpn_leak_unimport(bgp
);
3752 hook_call(bgp_inst_delete
, bgp
);
3754 FOREACH_AFI_SAFI (afi
, safi
)
3755 THREAD_OFF(bgp
->t_revalidate
[afi
][safi
]);
3757 THREAD_OFF(bgp
->t_condition_check
);
3758 THREAD_OFF(bgp
->t_startup
);
3759 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3760 THREAD_OFF(bgp
->t_update_delay
);
3761 THREAD_OFF(bgp
->t_establish_wait
);
3763 /* Set flag indicating bgp instance delete in progress */
3764 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3766 /* Delete the graceful restart info */
3767 FOREACH_AFI_SAFI (afi
, safi
) {
3770 gr_info
= &bgp
->gr_info
[afi
][safi
];
3773 t
= gr_info
->t_select_deferral
;
3775 void *info
= THREAD_ARG(t
);
3777 XFREE(MTYPE_TMP
, info
);
3779 THREAD_OFF(gr_info
->t_select_deferral
);
3781 t
= gr_info
->t_route_select
;
3783 void *info
= THREAD_ARG(t
);
3785 XFREE(MTYPE_TMP
, info
);
3787 THREAD_OFF(gr_info
->t_route_select
);
3790 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3791 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3792 zlog_debug("Deleting Default VRF");
3794 zlog_debug("Deleting %s %s",
3795 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3801 /* unmap from RT list */
3802 bgp_evpn_vrf_delete(bgp
);
3804 /* unmap bgp vrf label */
3805 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3806 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3809 if (bgp
->t_rmap_def_originate_eval
) {
3810 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3811 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3815 /* Inform peers we're going down. */
3816 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3817 peer_notify_shutdown(peer
);
3819 /* Delete static routes (networks). */
3820 bgp_static_delete(bgp
);
3822 /* Unset redistribution. */
3823 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3824 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3825 if (i
!= ZEBRA_ROUTE_BGP
)
3826 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3828 /* Free peers and peer-groups. */
3829 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3830 peer_group_delete(group
);
3832 while (listcount(bgp
->peer
)) {
3833 peer
= listnode_head(bgp
->peer
);
3837 if (bgp
->peer_self
) {
3838 peer_delete(bgp
->peer_self
);
3839 bgp
->peer_self
= NULL
;
3842 update_bgp_group_free(bgp
);
3844 /* TODO - Other memory may need to be freed - e.g., NHT */
3846 #ifdef ENABLE_BGP_VNC
3849 bgp_cleanup_routes(bgp
);
3851 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3852 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3855 &bgp
->vpn_policy
[afi
]
3856 .import_redirect_rtlist
);
3857 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3860 /* Free any memory allocated to holding routemap references */
3861 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3862 for (enum vpn_policy_direction dir
= 0;
3863 dir
< BGP_VPN_POLICY_DIR_MAX
; ++dir
) {
3864 if (bgp
->vpn_policy
[afi
].rmap_name
[dir
])
3865 XFREE(MTYPE_ROUTE_MAP_NAME
,
3866 bgp
->vpn_policy
[afi
].rmap_name
[dir
]);
3867 bgp
->vpn_policy
[afi
].rmap
[dir
] = NULL
;
3871 /* Deregister from Zebra, if needed */
3872 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3873 if (BGP_DEBUG(zebra
, ZEBRA
))
3875 "%s: deregistering this bgp %s instance from zebra",
3876 __func__
, bgp
->name
);
3877 bgp_zebra_instance_deregister(bgp
);
3880 /* Remove visibility via the master list - there may however still be
3881 * routes to be processed still referencing the struct bgp.
3883 listnode_delete(bm
->bgp
, bgp
);
3885 /* Free interfaces in this instance. */
3888 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3889 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3891 bgp_vrf_unlink(bgp
, vrf
);
3893 /* Update EVPN VRF pointer */
3894 if (bm
->bgp_evpn
== bgp
) {
3895 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3898 bgp_set_evpn(bgp_get_default());
3901 if (bgp
->process_queue
)
3902 work_queue_free_and_null(&bgp
->process_queue
);
3904 thread_master_free_unused(bm
->master
);
3905 bgp_unlock(bgp
); /* initial reference */
3910 void bgp_free(struct bgp
*bgp
)
3914 struct bgp_table
*table
;
3915 struct bgp_dest
*dest
;
3916 struct bgp_rmap
*rmap
;
3920 list_delete(&bgp
->group
);
3921 list_delete(&bgp
->peer
);
3923 if (bgp
->peerhash
) {
3924 hash_free(bgp
->peerhash
);
3925 bgp
->peerhash
= NULL
;
3928 FOREACH_AFI_SAFI (afi
, safi
) {
3929 /* Special handling for 2-level routing tables. */
3930 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3931 || safi
== SAFI_EVPN
) {
3932 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3933 dest
= bgp_route_next(dest
)) {
3934 table
= bgp_dest_get_bgp_table_info(dest
);
3935 bgp_table_finish(&table
);
3938 if (bgp
->route
[afi
][safi
])
3939 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3940 if (bgp
->aggregate
[afi
][safi
])
3941 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3942 if (bgp
->rib
[afi
][safi
])
3943 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3944 rmap
= &bgp
->table_map
[afi
][safi
];
3945 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3948 bgp_scan_finish(bgp
);
3949 bgp_address_destroy(bgp
);
3950 bgp_tip_hash_destroy(bgp
);
3952 /* release the auto RD id */
3953 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3955 bgp_evpn_cleanup(bgp
);
3956 bgp_pbr_cleanup(bgp
);
3957 bgp_srv6_cleanup(bgp
);
3958 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3960 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3961 enum vpn_policy_direction dir
;
3963 if (bgp
->vpn_policy
[afi
].import_vrf
)
3964 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3965 if (bgp
->vpn_policy
[afi
].export_vrf
)
3966 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3968 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3969 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3970 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3971 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3972 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3973 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3976 bgp_confederation_id_unset(bgp
);
3978 XFREE(MTYPE_BGP
, bgp
->as_pretty
);
3979 XFREE(MTYPE_BGP
, bgp
->name
);
3980 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3981 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3983 XFREE(MTYPE_BGP
, bgp
);
3986 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3989 struct listnode
*node
, *nnode
;
3995 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3996 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3997 && !CHECK_FLAG(peer
->sflags
,
3998 PEER_STATUS_ACCEPT_PEER
))
4000 } else if (bm
->bgp
!= NULL
) {
4001 struct listnode
*bgpnode
, *nbgpnode
;
4003 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4004 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
4006 && !strcmp(peer
->conf_if
, conf_if
)
4007 && !CHECK_FLAG(peer
->sflags
,
4008 PEER_STATUS_ACCEPT_PEER
))
4014 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
4017 struct listnode
*node
, *nnode
;
4023 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
4024 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
4025 && !CHECK_FLAG(peer
->sflags
,
4026 PEER_STATUS_ACCEPT_PEER
))
4028 } else if (bm
->bgp
!= NULL
) {
4029 struct listnode
*bgpnode
, *nbgpnode
;
4031 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4032 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
4034 && !strcmp(peer
->hostname
, hostname
)
4035 && !CHECK_FLAG(peer
->sflags
,
4036 PEER_STATUS_ACCEPT_PEER
))
4042 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
4044 struct peer
*peer
= NULL
;
4045 struct peer tmp_peer
;
4047 memset(&tmp_peer
, 0, sizeof(struct peer
));
4050 * We do not want to find the doppelganger peer so search for the peer
4052 * the hash that has PEER_FLAG_CONFIG_NODE
4054 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
4059 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4060 } else if (bm
->bgp
!= NULL
) {
4061 struct listnode
*bgpnode
, *nbgpnode
;
4063 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
4064 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4073 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
4074 union sockunion
*su
,
4075 struct peer_group
*group
)
4081 /* Create peer first; we've already checked group config is valid. */
4082 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
4083 group
->conf
->as_type
, group
, true, NULL
);
4088 peer
= peer_lock(peer
);
4089 listnode_add(group
->peer
, peer
);
4091 peer_group2peer_config_copy(group
, peer
);
4094 * Bind peer for all AFs configured for the group. We don't call
4095 * peer_group_bind as that is sub-optimal and does some stuff we don't
4098 FOREACH_AFI_SAFI (afi
, safi
) {
4099 if (!group
->conf
->afc
[afi
][safi
])
4101 peer
->afc
[afi
][safi
] = 1;
4103 if (!peer_af_find(peer
, afi
, safi
))
4104 peer_af_create(peer
, afi
, safi
);
4106 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
4109 /* Mark as dynamic, but also as a "config node" for other things to
4111 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
4117 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
4118 struct prefix
*prefix
)
4120 struct listnode
*node
, *nnode
;
4121 struct prefix
*range
;
4124 afi
= family2afi(prefix
->family
);
4126 if (group
->listen_range
[afi
])
4127 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
4129 if (prefix_match(range
, prefix
))
4136 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
4137 struct prefix
**listen_range
)
4139 struct prefix
*range
= NULL
;
4140 struct peer_group
*group
= NULL
;
4141 struct listnode
*node
, *nnode
;
4143 *listen_range
= NULL
;
4145 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4146 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4149 } else if (bm
->bgp
!= NULL
) {
4150 struct listnode
*bgpnode
, *nbgpnode
;
4152 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4153 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4154 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4160 *listen_range
= range
;
4161 return (group
&& range
) ? group
: NULL
;
4164 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
4166 struct peer_group
*group
;
4169 struct prefix prefix
;
4170 struct prefix
*listen_range
;
4173 if (!sockunion2hostprefix(su
, &prefix
))
4176 /* See if incoming connection matches a configured listen range. */
4177 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
4188 if (bgp_debug_neighbor_events(NULL
))
4190 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4191 &prefix
, group
->name
, listen_range
);
4193 /* Are we within the listen limit? */
4194 dncount
= gbgp
->dynamic_neighbors_count
;
4196 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4197 if (bgp_debug_neighbor_events(NULL
))
4199 "Dynamic Neighbor %pFX rejected - at limit %d",
4200 &prefix
, gbgp
->dynamic_neighbors_limit
);
4204 /* Ensure group is not disabled. */
4205 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4206 if (bgp_debug_neighbor_events(NULL
))
4208 "Dynamic Neighbor %pFX rejected - group %s disabled",
4209 &prefix
, group
->name
);
4213 /* Check that at least one AF is activated for the group. */
4214 if (!peer_group_af_configured(group
)) {
4215 if (bgp_debug_neighbor_events(NULL
))
4217 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4218 &prefix
, group
->name
);
4222 /* Create dynamic peer and bind to associated group. */
4223 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4226 gbgp
->dynamic_neighbors_count
= ++dncount
;
4228 if (bgp_debug_neighbor_events(peer
))
4229 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4230 peer
->host
, group
->name
, dncount
);
4235 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4238 if (peer
->group
->bgp
) {
4239 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4241 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4243 if (bgp_debug_neighbor_events(peer
))
4244 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4245 peer
->group
->name
, dncount
);
4248 bool bgp_path_attribute_discard(struct peer
*peer
, char *buf
, size_t size
)
4255 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4256 if (peer
->discard_attrs
[i
])
4257 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4258 (strlen(buf
) > 0) ? " " : "", i
);
4261 if (strlen(buf
) > 0)
4267 /* If peer is configured at least one address family return 1. */
4268 bool peer_active(struct peer
*peer
)
4270 if (BGP_PEER_SU_UNSPEC(peer
))
4272 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4273 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4274 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4275 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4276 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4277 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4278 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4279 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4280 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4281 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4282 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4287 /* If peer is negotiated at least one address family return 1. */
4288 bool peer_active_nego(struct peer
*peer
)
4290 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4291 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4292 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4293 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4294 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4295 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4296 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4297 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4298 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4299 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4300 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4301 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4302 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4307 /* If peer received at least one address family MP, return true */
4308 bool peer_afc_received(struct peer
*peer
)
4313 FOREACH_AFI_SAFI (afi
, safi
)
4314 if (peer
->afc_recv
[afi
][safi
])
4320 /* If peer advertised at least one address family MP, return true */
4321 bool peer_afc_advertised(struct peer
*peer
)
4326 FOREACH_AFI_SAFI (afi
, safi
)
4327 if (peer
->afc_adv
[afi
][safi
])
4333 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4334 enum peer_change_type type
)
4336 struct peer_af
*paf
;
4338 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4341 if (!peer_established(peer
))
4344 if (type
== peer_change_reset
) {
4345 /* If we're resetting session, we've to delete both peer struct
4347 if ((peer
->doppelganger
)
4348 && (peer
->doppelganger
->status
!= Deleted
)
4349 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4350 PEER_FLAG_CONFIG_NODE
)))
4351 peer_delete(peer
->doppelganger
);
4353 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4354 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4355 } else if (type
== peer_change_reset_in
) {
4356 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4357 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4358 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4359 BGP_ROUTE_REFRESH_NORMAL
);
4361 if ((peer
->doppelganger
)
4362 && (peer
->doppelganger
->status
!= Deleted
)
4363 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4364 PEER_FLAG_CONFIG_NODE
)))
4365 peer_delete(peer
->doppelganger
);
4367 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4368 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4370 } else if (type
== peer_change_reset_out
) {
4371 paf
= peer_af_find(peer
, afi
, safi
);
4372 if (paf
&& paf
->subgroup
)
4373 SET_FLAG(paf
->subgroup
->sflags
,
4374 SUBGRP_STATUS_FORCE_UPDATES
);
4376 update_group_adjust_peer(paf
);
4377 bgp_announce_route(peer
, afi
, safi
, false);
4381 struct peer_flag_action
{
4385 /* This flag can be set for peer-group member. */
4386 uint8_t not_for_member
;
4388 /* Action when the flag is changed. */
4389 enum peer_change_type type
;
4392 static const struct peer_flag_action peer_flag_action_list
[] = {
4393 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4394 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4395 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4396 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4397 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4398 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4399 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4400 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4401 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4402 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4403 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4404 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4405 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4406 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4407 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4408 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4409 {PEER_FLAG_LOCAL_AS
, 0, peer_change_reset
},
4410 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_reset
},
4411 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_reset
},
4412 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4413 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4414 {PEER_FLAG_EXTENDED_OPT_PARAMS
, 0, peer_change_reset
},
4415 {PEER_FLAG_ROLE_STRICT_MODE
, 0, peer_change_reset
},
4416 {PEER_FLAG_ROLE
, 0, peer_change_reset
},
4417 {PEER_FLAG_PORT
, 0, peer_change_reset
},
4418 {PEER_FLAG_AIGP
, 0, peer_change_none
},
4419 {PEER_FLAG_GRACEFUL_SHUTDOWN
, 0, peer_change_none
},
4422 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4423 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4424 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4425 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4426 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4427 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4428 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4429 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4430 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4431 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4432 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4433 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4434 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4435 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4436 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4437 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4438 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4439 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4440 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4441 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4442 {PEER_FLAG_MAX_PREFIX_OUT
, 0, peer_change_none
},
4443 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4444 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4445 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4446 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4447 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4448 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4449 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4450 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4451 {PEER_FLAG_SOO
, 0, peer_change_reset
},
4452 {PEER_FLAG_ACCEPT_OWN
, 0, peer_change_reset
},
4455 /* Proper action set. */
4456 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4457 int size
, struct peer_flag_action
*action
,
4464 const struct peer_flag_action
*match
= NULL
;
4466 /* Check peer's frag action. */
4467 for (i
= 0; i
< size
; i
++) {
4468 match
= &action_list
[i
];
4470 if (match
->flag
== 0)
4473 if (match
->flag
& flag
) {
4476 if (match
->type
== peer_change_reset_in
)
4478 if (match
->type
== peer_change_reset_out
)
4480 if (match
->type
== peer_change_reset
) {
4484 if (match
->not_for_member
)
4485 action
->not_for_member
= 1;
4489 /* Set peer clear type. */
4490 if (reset_in
&& reset_out
)
4491 action
->type
= peer_change_reset
;
4493 action
->type
= peer_change_reset_in
;
4495 action
->type
= peer_change_reset_out
;
4497 action
->type
= peer_change_none
;
4502 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4504 if (flag
== PEER_FLAG_SHUTDOWN
) {
4505 if (CHECK_FLAG(peer
->flags
, flag
)) {
4506 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4507 peer_nsf_stop(peer
);
4509 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4511 if (peer
->t_pmax_restart
) {
4512 THREAD_OFF(peer
->t_pmax_restart
);
4513 if (bgp_debug_neighbor_events(peer
))
4515 "%pBP Maximum-prefix restart timer canceled",
4519 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4520 char *msg
= peer
->tx_shutdown_message
;
4522 uint8_t msgbuf
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4524 if (!msg
&& peer_group_active(peer
))
4525 msg
= peer
->group
->conf
4526 ->tx_shutdown_message
;
4527 msglen
= msg
? strlen(msg
) : 0;
4528 if (msglen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4529 msglen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4533 memcpy(msgbuf
+ 1, msg
, msglen
);
4535 bgp_notify_send_with_data(
4536 peer
, BGP_NOTIFY_CEASE
,
4537 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4538 msgbuf
, msglen
+ 1);
4541 peer
, BGP_NOTIFY_CEASE
,
4542 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4544 bgp_session_reset(peer
);
4546 peer
->v_start
= BGP_INIT_START_TIMER
;
4547 BGP_EVENT_ADD(peer
, BGP_Stop
);
4549 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4550 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4551 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4552 else if (flag
== PEER_FLAG_PASSIVE
)
4553 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4554 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4555 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4557 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4558 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4560 bgp_session_reset(peer
);
4563 /* Enable global administrative shutdown of all peers of BGP instance */
4564 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4567 struct listnode
*node
;
4568 /* length(1) + message(N) */
4569 uint8_t data
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4571 /* do nothing if already shut down */
4572 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4575 /* informational log message */
4576 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4579 /* iterate through peers of BGP instance */
4580 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4581 /* continue, if peer is already in administrative shutdown. */
4582 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4585 /* send a RFC 4486 notification message if necessary */
4586 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4588 size_t datalen
= strlen(msg
);
4590 if (datalen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4591 datalen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4594 memcpy(data
+ 1, msg
, datalen
);
4596 bgp_notify_send_with_data(
4597 peer
, BGP_NOTIFY_CEASE
,
4598 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
, data
,
4602 peer
, BGP_NOTIFY_CEASE
,
4603 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4607 /* reset start timer to initial value */
4608 peer
->v_start
= BGP_INIT_START_TIMER
;
4610 /* trigger a RFC 4271 ManualStop event */
4611 BGP_EVENT_ADD(peer
, BGP_Stop
);
4614 /* set the BGP instances shutdown flag */
4615 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4618 /* Disable global administrative shutdown of all peers of BGP instance */
4619 void bgp_shutdown_disable(struct bgp
*bgp
)
4621 /* do nothing if not shut down. */
4622 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4625 /* informational log message */
4626 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4629 /* clear the BGP instances shutdown flag */
4630 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4633 /* Change specified peer flag. */
4634 static int peer_flag_modify(struct peer
*peer
, uint64_t flag
, int set
)
4638 bool invert
, member_invert
;
4639 struct peer
*member
;
4640 struct listnode
*node
, *nnode
;
4641 struct peer_flag_action action
;
4643 memset(&action
, 0, sizeof(struct peer_flag_action
));
4644 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4646 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4647 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4650 /* Abort if no flag action exists. */
4652 return BGP_ERR_INVALID_FLAG
;
4654 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4655 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4656 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4657 return BGP_ERR_PEER_FLAG_CONFLICT
;
4659 /* Handle flag updates where desired state matches current state. */
4660 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4661 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4662 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4666 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4667 COND_FLAG(peer
->flags_override
, flag
, invert
);
4672 /* Inherit from peer-group or set/unset flags accordingly. */
4673 if (peer_group_active(peer
) && set
== invert
)
4674 peer_flag_inherit(peer
, flag
);
4676 COND_FLAG(peer
->flags
, flag
, set
);
4678 /* Check if handling a regular peer. */
4679 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4680 /* Update flag override state accordingly. */
4681 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4684 * For the extended next-hop encoding flag we need to turn RAs
4685 * on if flag is being set, but only turn RAs off if the flag
4686 * is being unset on this peer and if this peer is a member of a
4687 * peer-group, the peer-group also doesn't have the flag set.
4689 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4691 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4692 } else if (peer_group_active(peer
)) {
4693 if (!CHECK_FLAG(peer
->group
->conf
->flags
,
4696 bgp_zebra_terminate_radv(peer
->bgp
,
4699 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4702 /* Execute flag action on peer. */
4703 if (action
.type
== peer_change_reset
)
4704 peer_flag_modify_action(peer
, flag
);
4706 /* Skip peer-group mechanics for regular peers. */
4711 * Update peer-group members, unless they are explicitly overriding
4712 * peer-group configuration.
4714 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4715 /* Skip peers with overridden configuration. */
4716 if (CHECK_FLAG(member
->flags_override
, flag
))
4719 /* Check if only member without group is inverted. */
4721 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4723 /* Skip peers with equivalent configuration. */
4724 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4727 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4730 /* Update flag on peer-group member. */
4731 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4733 if (flag
== PEER_FLAG_CAPABILITY_ENHE
&& !member
->conf_if
)
4734 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4735 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4737 /* Execute flag action on peer-group member. */
4738 if (action
.type
== peer_change_reset
)
4739 peer_flag_modify_action(member
, flag
);
4745 int peer_flag_set(struct peer
*peer
, uint64_t flag
)
4747 return peer_flag_modify(peer
, flag
, 1);
4750 int peer_flag_unset(struct peer
*peer
, uint64_t flag
)
4752 return peer_flag_modify(peer
, flag
, 0);
4755 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4756 uint64_t flag
, bool set
)
4760 bool invert
, member_invert
;
4761 struct peer
*member
;
4762 struct listnode
*node
, *nnode
;
4763 struct peer_flag_action action
;
4764 enum bgp_peer_sort ptype
;
4766 memset(&action
, 0, sizeof(struct peer_flag_action
));
4767 size
= sizeof(peer_af_flag_action_list
)
4768 / sizeof(struct peer_flag_action
);
4770 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4771 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4774 /* Abort if flag action exists. */
4776 return BGP_ERR_INVALID_FLAG
;
4778 ptype
= peer_sort(peer
);
4779 /* Special check for reflector client. */
4780 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4781 return BGP_ERR_NOT_INTERNAL_PEER
;
4783 /* Special check for remove-private-AS. */
4784 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4785 return BGP_ERR_REMOVE_PRIVATE_AS
;
4787 /* as-override is not allowed for IBGP peers */
4788 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4789 return BGP_ERR_AS_OVERRIDE
;
4791 /* Handle flag updates where desired state matches current state. */
4792 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4793 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4794 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4799 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4800 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4807 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4808 * if we are setting/unsetting flags which conflict with this flag
4809 * handle accordingly
4811 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4815 * if we are setting NEXTHOP_SELF, we need to unset the
4816 * NEXTHOP_UNCHANGED flag
4818 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4819 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4820 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4821 PEER_FLAG_NEXTHOP_UNCHANGED
);
4825 * if we are unsetting NEXTHOP_SELF, we need to set the
4826 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4828 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4829 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4830 SET_FLAG(peer
->af_flags
[afi
][safi
],
4831 PEER_FLAG_NEXTHOP_UNCHANGED
);
4836 * If the peer is a route server client let's not
4837 * muck with the nexthop on the way out the door
4839 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4841 SET_FLAG(peer
->af_flags
[afi
][safi
],
4842 PEER_FLAG_NEXTHOP_UNCHANGED
);
4844 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4845 PEER_FLAG_NEXTHOP_UNCHANGED
);
4848 /* Inherit from peer-group or set/unset flags accordingly. */
4849 if (peer_group_active(peer
) && set
== invert
)
4850 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4852 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4854 /* Execute action when peer is established. */
4855 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4856 && peer_established(peer
)) {
4857 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4858 bgp_clear_adj_in(peer
, afi
, safi
);
4860 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4861 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4862 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4863 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4864 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4865 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4866 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4867 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4869 peer_change_action(peer
, afi
, safi
, action
.type
);
4873 /* Check if handling a regular peer. */
4874 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4875 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4879 * Update peer-group members, unless they are explicitly
4880 * overriding peer-group configuration.
4882 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4884 /* Skip peers with overridden configuration. */
4885 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4889 /* Check if only member without group is inverted. */
4891 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4895 /* Skip peers with equivalent configuration. */
4896 if (set
!= member_invert
4897 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4900 if (set
== member_invert
4901 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4904 /* Update flag on peer-group member. */
4905 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4906 set
!= member_invert
);
4908 /* Execute flag action on peer-group member. */
4909 if (peer_established(member
)) {
4910 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4911 bgp_clear_adj_in(member
, afi
, safi
);
4913 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4914 member
->last_reset
=
4915 PEER_DOWN_RR_CLIENT_CHANGE
;
4917 == PEER_FLAG_RSERVER_CLIENT
)
4918 member
->last_reset
=
4919 PEER_DOWN_RS_CLIENT_CHANGE
;
4921 == PEER_FLAG_ORF_PREFIX_SM
)
4922 member
->last_reset
=
4923 PEER_DOWN_CAPABILITY_CHANGE
;
4925 == PEER_FLAG_ORF_PREFIX_RM
)
4926 member
->last_reset
=
4927 PEER_DOWN_CAPABILITY_CHANGE
;
4929 peer_change_action(member
, afi
, safi
,
4939 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4941 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4944 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4946 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4950 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4952 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4953 peer
->tx_shutdown_message
=
4954 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4957 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4959 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4963 /* EBGP multihop configuration. */
4964 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4966 struct peer_group
*group
;
4967 struct listnode
*node
, *nnode
;
4970 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4973 /* is there anything to do? */
4974 if (peer
->ttl
== ttl
)
4977 /* see comment in peer_ttl_security_hops_set() */
4978 if (ttl
!= MAXTTL
) {
4979 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4980 group
= peer
->group
;
4981 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4982 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4984 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4986 if (peer1
->sort
== BGP_PEER_IBGP
)
4989 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4990 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4993 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4994 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
5000 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5001 if (peer
->sort
!= BGP_PEER_IBGP
) {
5002 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5003 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5004 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5006 bgp_session_reset(peer
);
5008 /* Reconfigure BFD peer with new TTL. */
5009 if (peer
->bfd_config
)
5010 bgp_peer_bfd_update_source(peer
);
5013 group
= peer
->group
;
5014 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5015 if (peer
->sort
== BGP_PEER_IBGP
)
5018 peer
->ttl
= group
->conf
->ttl
;
5020 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5021 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5022 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5024 bgp_session_reset(peer
);
5026 /* Reconfigure BFD peer with new TTL. */
5027 if (peer
->bfd_config
)
5028 bgp_peer_bfd_update_source(peer
);
5034 int peer_ebgp_multihop_unset(struct peer
*peer
)
5036 struct peer_group
*group
;
5037 struct listnode
*node
, *nnode
;
5040 if (peer
->sort
== BGP_PEER_IBGP
)
5043 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
5044 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
5046 if (peer_group_active(peer
))
5047 ttl
= peer
->group
->conf
->ttl
;
5049 ttl
= BGP_DEFAULT_TTL
;
5051 if (ttl
== peer
->ttl
)
5056 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5057 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5058 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5059 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5061 bgp_session_reset(peer
);
5063 /* Reconfigure BFD peer with new TTL. */
5064 if (peer
->bfd_config
)
5065 bgp_peer_bfd_update_source(peer
);
5067 group
= peer
->group
;
5068 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5069 if (peer
->sort
== BGP_PEER_IBGP
)
5072 peer
->ttl
= BGP_DEFAULT_TTL
;
5074 if (peer
->fd
>= 0) {
5075 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5077 peer
, BGP_NOTIFY_CEASE
,
5078 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5080 bgp_session_reset(peer
);
5083 /* Reconfigure BFD peer with new TTL. */
5084 if (peer
->bfd_config
)
5085 bgp_peer_bfd_update_source(peer
);
5091 /* Set Open Policy Role and check its correctness */
5092 int peer_role_set(struct peer
*peer
, uint8_t role
, bool strict_mode
)
5094 struct peer
*member
;
5095 struct listnode
*node
, *nnode
;
5097 peer_flag_set(peer
, PEER_FLAG_ROLE
);
5099 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5100 if (peer
->sort
!= BGP_PEER_EBGP
)
5101 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5103 if (peer
->local_role
== role
) {
5104 if (CHECK_FLAG(peer
->flags
,
5105 PEER_FLAG_ROLE_STRICT_MODE
) &&
5107 /* TODO: Is session restart needed if it was
5110 UNSET_FLAG(peer
->flags
,
5111 PEER_FLAG_ROLE_STRICT_MODE
);
5112 if (!CHECK_FLAG(peer
->flags
,
5113 PEER_FLAG_ROLE_STRICT_MODE
) &&
5115 SET_FLAG(peer
->flags
,
5116 PEER_FLAG_ROLE_STRICT_MODE
);
5117 /* Restart session to throw Role Mismatch
5120 if (peer
->remote_role
== ROLE_UNDEFINED
)
5121 bgp_session_reset(peer
);
5124 peer
->local_role
= role
;
5126 SET_FLAG(peer
->flags
,
5127 PEER_FLAG_ROLE_STRICT_MODE
);
5129 UNSET_FLAG(peer
->flags
,
5130 PEER_FLAG_ROLE_STRICT_MODE
);
5131 bgp_session_reset(peer
);
5137 peer
->local_role
= role
;
5138 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5139 if (member
->sort
!= BGP_PEER_EBGP
)
5140 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5142 if (member
->local_role
== role
) {
5143 if (CHECK_FLAG(member
->flags
,
5144 PEER_FLAG_ROLE_STRICT_MODE
) &&
5146 /* TODO: Is session restart needed if it was
5149 UNSET_FLAG(member
->flags
,
5150 PEER_FLAG_ROLE_STRICT_MODE
);
5151 if (!CHECK_FLAG(member
->flags
,
5152 PEER_FLAG_ROLE_STRICT_MODE
) &&
5154 SET_FLAG(peer
->flags
,
5155 PEER_FLAG_ROLE_STRICT_MODE
);
5156 SET_FLAG(member
->flags
,
5157 PEER_FLAG_ROLE_STRICT_MODE
);
5158 /* Restart session to throw Role Mismatch
5161 if (member
->remote_role
== ROLE_UNDEFINED
)
5162 bgp_session_reset(member
);
5165 member
->local_role
= role
;
5168 SET_FLAG(peer
->flags
,
5169 PEER_FLAG_ROLE_STRICT_MODE
);
5170 SET_FLAG(member
->flags
,
5171 PEER_FLAG_ROLE_STRICT_MODE
);
5173 UNSET_FLAG(member
->flags
,
5174 PEER_FLAG_ROLE_STRICT_MODE
);
5176 bgp_session_reset(member
);
5183 int peer_role_unset(struct peer
*peer
)
5185 struct peer
*member
;
5186 struct listnode
*node
, *nnode
;
5188 peer_flag_unset(peer
, PEER_FLAG_ROLE
);
5190 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5191 return peer_role_set(peer
, ROLE_UNDEFINED
, 0);
5193 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
))
5194 peer_role_set(member
, ROLE_UNDEFINED
, 0);
5199 /* Neighbor description. */
5200 void peer_description_set(struct peer
*peer
, const char *desc
)
5202 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5204 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
5207 void peer_description_unset(struct peer
*peer
)
5209 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5212 /* Neighbor update-source. */
5213 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
5215 struct peer
*member
;
5216 struct listnode
*node
, *nnode
;
5218 /* Set flag and configuration on peer. */
5219 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5220 if (peer
->update_if
) {
5221 if (strcmp(peer
->update_if
, ifname
) == 0)
5223 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5225 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5226 sockunion_free(peer
->update_source
);
5227 peer
->update_source
= NULL
;
5229 /* Check if handling a regular peer. */
5230 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5231 /* Send notification or reset peer depending on state. */
5232 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5233 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5234 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5235 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5237 bgp_session_reset(peer
);
5239 /* Apply new source configuration to BFD session. */
5240 if (peer
->bfd_config
)
5241 bgp_peer_bfd_update_source(peer
);
5243 /* Skip peer-group mechanics for regular peers. */
5248 * Set flag and configuration on all peer-group members, unless they are
5249 * explicitly overriding peer-group configuration.
5251 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5252 /* Skip peers with overridden configuration. */
5253 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5256 /* Skip peers with the same configuration. */
5257 if (member
->update_if
) {
5258 if (strcmp(member
->update_if
, ifname
) == 0)
5260 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5263 /* Set flag and configuration on peer-group member. */
5264 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5265 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5266 sockunion_free(member
->update_source
);
5267 member
->update_source
= NULL
;
5269 /* Send notification or reset peer depending on state. */
5270 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5271 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5272 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5273 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5275 bgp_session_reset(member
);
5277 /* Apply new source configuration to BFD session. */
5278 if (member
->bfd_config
)
5279 bgp_peer_bfd_update_source(member
);
5285 void peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
5287 struct peer
*member
;
5288 struct listnode
*node
, *nnode
;
5290 /* Set flag and configuration on peer. */
5291 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5292 if (peer
->update_source
) {
5293 if (sockunion_cmp(peer
->update_source
, su
) == 0)
5295 sockunion_free(peer
->update_source
);
5297 peer
->update_source
= sockunion_dup(su
);
5298 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5300 /* Check if handling a regular peer. */
5301 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5302 /* Send notification or reset peer depending on state. */
5303 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5304 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5305 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5306 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5308 bgp_session_reset(peer
);
5310 /* Apply new source configuration to BFD session. */
5311 if (peer
->bfd_config
)
5312 bgp_peer_bfd_update_source(peer
);
5314 /* Skip peer-group mechanics for regular peers. */
5319 * Set flag and configuration on all peer-group members, unless they are
5320 * explicitly overriding peer-group configuration.
5322 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5323 /* Skip peers with overridden configuration. */
5324 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5327 /* Skip peers with the same configuration. */
5328 if (member
->update_source
) {
5329 if (sockunion_cmp(member
->update_source
, su
) == 0)
5331 sockunion_free(member
->update_source
);
5334 /* Set flag and configuration on peer-group member. */
5335 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5336 member
->update_source
= sockunion_dup(su
);
5337 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5339 /* Send notification or reset peer depending on state. */
5340 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5341 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5342 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5343 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5345 bgp_session_reset(member
);
5347 /* Apply new source configuration to BFD session. */
5348 if (member
->bfd_config
)
5349 bgp_peer_bfd_update_source(member
);
5353 void peer_update_source_unset(struct peer
*peer
)
5355 struct peer
*member
;
5356 struct listnode
*node
, *nnode
;
5358 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
5361 /* Inherit configuration from peer-group if peer is member. */
5362 if (peer_group_active(peer
)) {
5363 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
5364 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
5365 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
5366 MTYPE_PEER_UPDATE_SOURCE
);
5368 /* Otherwise remove flag and configuration from peer. */
5369 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
5370 sockunion_free(peer
->update_source
);
5371 peer
->update_source
= NULL
;
5372 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5375 /* Check if handling a regular peer. */
5376 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5377 /* Send notification or reset peer depending on state. */
5378 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5379 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5380 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5381 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5383 bgp_session_reset(peer
);
5385 /* Apply new source configuration to BFD session. */
5386 if (peer
->bfd_config
)
5387 bgp_peer_bfd_update_source(peer
);
5389 /* Skip peer-group mechanics for regular peers. */
5394 * Set flag and configuration on all peer-group members, unless they are
5395 * explicitly overriding peer-group configuration.
5397 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5398 /* Skip peers with overridden configuration. */
5399 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5402 /* Skip peers with the same configuration. */
5403 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5404 && !member
->update_source
&& !member
->update_if
)
5407 /* Remove flag and configuration on peer-group member. */
5408 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5409 sockunion_free(member
->update_source
);
5410 member
->update_source
= NULL
;
5411 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5413 /* Send notification or reset peer depending on state. */
5414 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5415 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5416 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5417 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5419 bgp_session_reset(member
);
5421 /* Apply new source configuration to BFD session. */
5422 if (member
->bfd_config
)
5423 bgp_peer_bfd_update_source(member
);
5427 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5428 const char *rmap
, struct route_map
*route_map
)
5430 struct peer
*member
;
5431 struct listnode
*node
, *nnode
;
5432 struct update_subgroup
*subgrp
;
5434 /* Set flag and configuration on peer. */
5435 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5437 subgrp
= peer_subgroup(peer
, afi
, safi
);
5440 if (!peer
->default_rmap
[afi
][safi
].name
5441 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5442 if (peer
->default_rmap
[afi
][safi
].name
)
5443 XFREE(MTYPE_ROUTE_MAP_NAME
,
5444 peer
->default_rmap
[afi
][safi
].name
);
5447 * When there is a change in route-map policy,
5448 * this flow gets triggered. Since, the default
5449 * route is already originated, the flag is set.
5450 * The flag should be unset here,
5451 * to trigger the flow of sending update message.
5454 UNSET_FLAG(subgrp
->sflags
,
5455 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5457 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5458 peer
->default_rmap
[afi
][safi
].name
=
5459 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5460 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5461 route_map_counter_increment(route_map
);
5464 if (peer
->default_rmap
[afi
][safi
].name
)
5465 XFREE(MTYPE_ROUTE_MAP_NAME
,
5466 peer
->default_rmap
[afi
][safi
].name
);
5469 * This is triggered in case of route-map deletion.
5470 * The flag needs to be unset, to trigger the flow
5471 * of sending an update message.
5474 UNSET_FLAG(subgrp
->sflags
,
5475 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5477 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5478 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5479 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5482 /* Check if handling a regular peer. */
5483 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5484 /* Update peer route announcements. */
5485 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5486 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5487 bgp_default_originate(peer
, afi
, safi
, 0);
5488 bgp_announce_route(peer
, afi
, safi
, false);
5491 /* Skip peer-group mechanics for regular peers. */
5496 * Set flag and configuration on all peer-group members, unless they are
5497 * explicitly overriding peer-group configuration.
5499 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5500 /* Skip peers with overridden configuration. */
5501 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5502 PEER_FLAG_DEFAULT_ORIGINATE
))
5505 /* Set flag and configuration on peer-group member. */
5506 SET_FLAG(member
->af_flags
[afi
][safi
],
5507 PEER_FLAG_DEFAULT_ORIGINATE
);
5509 if (member
->default_rmap
[afi
][safi
].name
)
5510 XFREE(MTYPE_ROUTE_MAP_NAME
,
5511 member
->default_rmap
[afi
][safi
].name
);
5512 route_map_counter_decrement(
5513 member
->default_rmap
[afi
][safi
].map
);
5514 member
->default_rmap
[afi
][safi
].name
=
5515 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5516 member
->default_rmap
[afi
][safi
].map
= route_map
;
5517 route_map_counter_increment(route_map
);
5520 /* Update peer route announcements. */
5521 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5522 update_group_adjust_peer(
5523 peer_af_find(member
, afi
, safi
));
5524 bgp_default_originate(member
, afi
, safi
, 0);
5525 bgp_announce_route(member
, afi
, safi
, false);
5532 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5534 struct peer
*member
;
5535 struct listnode
*node
, *nnode
;
5537 /* Inherit configuration from peer-group if peer is member. */
5538 if (peer_group_active(peer
)) {
5539 peer_af_flag_inherit(peer
, afi
, safi
,
5540 PEER_FLAG_DEFAULT_ORIGINATE
);
5541 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5542 default_rmap
[afi
][safi
].name
,
5543 MTYPE_ROUTE_MAP_NAME
);
5544 PEER_ATTR_INHERIT(peer
, peer
->group
,
5545 default_rmap
[afi
][safi
].map
);
5547 /* Otherwise remove flag and configuration from peer. */
5548 peer_af_flag_unset(peer
, afi
, safi
,
5549 PEER_FLAG_DEFAULT_ORIGINATE
);
5550 if (peer
->default_rmap
[afi
][safi
].name
)
5551 XFREE(MTYPE_ROUTE_MAP_NAME
,
5552 peer
->default_rmap
[afi
][safi
].name
);
5553 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5554 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5555 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5558 /* Check if handling a regular peer. */
5559 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5560 /* Update peer route announcements. */
5561 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5562 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5563 bgp_default_originate(peer
, afi
, safi
, 1);
5564 bgp_announce_route(peer
, afi
, safi
, false);
5567 /* Skip peer-group mechanics for regular peers. */
5572 * Remove flag and configuration from all peer-group members, unless
5573 * they are explicitly overriding peer-group configuration.
5575 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5576 /* Skip peers with overridden configuration. */
5577 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5578 PEER_FLAG_DEFAULT_ORIGINATE
))
5581 /* Remove flag and configuration on peer-group member. */
5582 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5583 PEER_FLAG_DEFAULT_ORIGINATE
);
5584 if (member
->default_rmap
[afi
][safi
].name
)
5585 XFREE(MTYPE_ROUTE_MAP_NAME
,
5586 member
->default_rmap
[afi
][safi
].name
);
5587 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5588 member
->default_rmap
[afi
][safi
].name
= NULL
;
5589 member
->default_rmap
[afi
][safi
].map
= NULL
;
5591 /* Update peer route announcements. */
5592 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5593 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5594 bgp_default_originate(member
, afi
, safi
, 1);
5595 bgp_announce_route(member
, afi
, safi
, false);
5602 void peer_port_set(struct peer
*peer
, uint16_t port
)
5605 peer_flag_set(peer
, PEER_FLAG_PORT
);
5608 void peer_port_unset(struct peer
*peer
)
5610 peer
->port
= BGP_PORT_DEFAULT
;
5611 peer_flag_unset(peer
, PEER_FLAG_PORT
);
5614 /* Set the TCP-MSS value in the peer structure,
5615 * This gets applied only after connection reset
5616 * So this value will be used in bgp_connect.
5618 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5620 peer
->tcp_mss
= tcp_mss
;
5621 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5624 /* Reset the TCP-MSS value in the peer structure,
5625 * This gets applied only after connection reset
5626 * So this value will be used in bgp_connect.
5628 void peer_tcp_mss_unset(struct peer
*peer
)
5630 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5635 * Helper function that is called after the name of the policy
5636 * being used by a peer has changed (AF specific). Automatically
5637 * initiates inbound or outbound processing as needed.
5639 void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5643 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5644 if (peer_established(peer
))
5645 bgp_announce_route(peer
, afi
, safi
, false);
5647 if (!peer_established(peer
))
5650 if (bgp_soft_reconfig_in(peer
, afi
, safi
))
5653 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
) ||
5654 CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
)) {
5655 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5656 PEER_CAP_ORF_PREFIX_SM_ADV
) &&
5657 (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5658 PEER_CAP_ORF_PREFIX_RM_RCV
) ||
5659 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5660 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
)))
5661 peer_clear_soft(peer
, afi
, safi
,
5662 BGP_CLEAR_SOFT_IN_ORF_PREFIX
);
5664 bgp_route_refresh_send(
5665 peer
, afi
, safi
, 0, 0, 0,
5666 BGP_ROUTE_REFRESH_NORMAL
);
5672 /* neighbor weight. */
5673 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5675 struct peer
*member
;
5676 struct listnode
*node
, *nnode
;
5678 /* Set flag and configuration on peer. */
5679 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5680 if (peer
->weight
[afi
][safi
] != weight
) {
5681 peer
->weight
[afi
][safi
] = weight
;
5682 peer_on_policy_change(peer
, afi
, safi
, 0);
5685 /* Skip peer-group mechanics for regular peers. */
5686 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5690 * Set flag and configuration on all peer-group members, unless they are
5691 * explicitly overriding peer-group configuration.
5693 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5694 /* Skip peers with overridden configuration. */
5695 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5699 /* Set flag and configuration on peer-group member. */
5700 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5701 if (member
->weight
[afi
][safi
] != weight
) {
5702 member
->weight
[afi
][safi
] = weight
;
5703 peer_on_policy_change(member
, afi
, safi
, 0);
5710 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5712 struct peer
*member
;
5713 struct listnode
*node
, *nnode
;
5715 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5718 /* Inherit configuration from peer-group if peer is member. */
5719 if (peer_group_active(peer
)) {
5720 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5721 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5723 peer_on_policy_change(peer
, afi
, safi
, 0);
5727 /* Remove flag and configuration from peer. */
5728 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5729 peer
->weight
[afi
][safi
] = 0;
5730 peer_on_policy_change(peer
, afi
, safi
, 0);
5732 /* Skip peer-group mechanics for regular peers. */
5733 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5737 * Remove flag and configuration from all peer-group members, unless
5738 * they are explicitly overriding peer-group configuration.
5740 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5741 /* Skip peers with overridden configuration. */
5742 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5746 /* Skip peers where flag is already disabled. */
5747 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5750 /* Remove flag and configuration on peer-group member. */
5751 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5752 member
->weight
[afi
][safi
] = 0;
5753 peer_on_policy_change(member
, afi
, safi
, 0);
5759 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5761 struct peer
*member
;
5762 struct listnode
*node
, *nnode
;
5764 if (keepalive
> UINT16_MAX
)
5765 return BGP_ERR_INVALID_VALUE
;
5767 if (holdtime
> UINT16_MAX
)
5768 return BGP_ERR_INVALID_VALUE
;
5770 if (holdtime
< 3 && holdtime
!= 0)
5771 return BGP_ERR_INVALID_VALUE
;
5773 /* Set flag and configuration on peer. */
5774 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5775 peer
->holdtime
= holdtime
;
5776 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5778 /* Skip peer-group mechanics for regular peers. */
5779 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5783 * Set flag and configuration on all peer-group members, unless they are
5784 * explicitly overriding peer-group configuration.
5786 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5787 /* Skip peers with overridden configuration. */
5788 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5791 /* Set flag and configuration on peer-group member. */
5792 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5793 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5794 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5800 int peer_timers_unset(struct peer
*peer
)
5802 struct peer
*member
;
5803 struct listnode
*node
, *nnode
;
5805 /* Inherit configuration from peer-group if peer is member. */
5806 if (peer_group_active(peer
)) {
5807 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5808 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5809 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5811 /* Otherwise remove flag and configuration from peer. */
5812 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5814 peer
->keepalive
= 0;
5817 /* Skip peer-group mechanics for regular peers. */
5818 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5822 * Remove flag and configuration from all peer-group members, unless
5823 * they are explicitly overriding peer-group configuration.
5825 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5826 /* Skip peers with overridden configuration. */
5827 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5830 /* Remove flag and configuration on peer-group member. */
5831 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5832 member
->holdtime
= 0;
5833 member
->keepalive
= 0;
5839 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5841 struct peer
*member
;
5842 struct listnode
*node
, *nnode
;
5844 if (connect
> UINT16_MAX
)
5845 return BGP_ERR_INVALID_VALUE
;
5847 /* Set flag and configuration on peer. */
5848 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5849 peer
->connect
= connect
;
5850 peer
->v_connect
= connect
;
5852 /* Skip peer-group mechanics for regular peers. */
5853 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5854 if (!peer_established(peer
)) {
5855 if (peer_active(peer
))
5856 BGP_EVENT_ADD(peer
, BGP_Stop
);
5857 BGP_EVENT_ADD(peer
, BGP_Start
);
5862 * Set flag and configuration on all peer-group members, unless they are
5863 * explicitly overriding peer-group configuration.
5865 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5866 /* Skip peers with overridden configuration. */
5867 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5870 /* Set flag and configuration on peer-group member. */
5871 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5872 member
->connect
= connect
;
5873 member
->v_connect
= connect
;
5875 if (!peer_established(member
)) {
5876 if (peer_active(member
))
5877 BGP_EVENT_ADD(member
, BGP_Stop
);
5878 BGP_EVENT_ADD(member
, BGP_Start
);
5885 int peer_timers_connect_unset(struct peer
*peer
)
5887 struct peer
*member
;
5888 struct listnode
*node
, *nnode
;
5890 /* Inherit configuration from peer-group if peer is member. */
5891 if (peer_group_active(peer
)) {
5892 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5893 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5895 /* Otherwise remove flag and configuration from peer. */
5896 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5900 /* Set timer with fallback to default value. */
5902 peer
->v_connect
= peer
->connect
;
5904 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5906 /* Skip peer-group mechanics for regular peers. */
5907 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5908 if (!peer_established(peer
)) {
5909 if (peer_active(peer
))
5910 BGP_EVENT_ADD(peer
, BGP_Stop
);
5911 BGP_EVENT_ADD(peer
, BGP_Start
);
5916 * Remove flag and configuration from all peer-group members, unless
5917 * they are explicitly overriding peer-group configuration.
5919 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5920 /* Skip peers with overridden configuration. */
5921 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5924 /* Remove flag and configuration on peer-group member. */
5925 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5926 member
->connect
= 0;
5927 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5929 if (!peer_established(member
)) {
5930 if (peer_active(member
))
5931 BGP_EVENT_ADD(member
, BGP_Stop
);
5932 BGP_EVENT_ADD(member
, BGP_Start
);
5939 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5941 struct peer
*member
;
5942 struct listnode
*node
, *nnode
;
5945 return BGP_ERR_INVALID_VALUE
;
5947 /* Set flag and configuration on peer. */
5948 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5949 peer
->routeadv
= routeadv
;
5950 peer
->v_routeadv
= routeadv
;
5952 /* Check if handling a regular peer. */
5953 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5954 /* Update peer route announcements. */
5955 update_group_adjust_peer_afs(peer
);
5956 if (peer_established(peer
))
5957 bgp_announce_route_all(peer
);
5959 /* Skip peer-group mechanics for regular peers. */
5964 * Set flag and configuration on all peer-group members, unless they are
5965 * explicitly overriding peer-group configuration.
5967 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5968 /* Skip peers with overridden configuration. */
5969 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5972 /* Set flag and configuration on peer-group member. */
5973 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5974 member
->routeadv
= routeadv
;
5975 member
->v_routeadv
= routeadv
;
5977 /* Update peer route announcements. */
5978 update_group_adjust_peer_afs(member
);
5979 if (peer_established(member
))
5980 bgp_announce_route_all(member
);
5986 int peer_advertise_interval_unset(struct peer
*peer
)
5988 struct peer
*member
;
5989 struct listnode
*node
, *nnode
;
5991 /* Inherit configuration from peer-group if peer is member. */
5992 if (peer_group_active(peer
)) {
5993 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5994 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5996 /* Otherwise remove flag and configuration from peer. */
5997 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
6001 /* Set timer with fallback to default value. */
6003 peer
->v_routeadv
= peer
->routeadv
;
6005 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
6006 ? BGP_DEFAULT_IBGP_ROUTEADV
6007 : BGP_DEFAULT_EBGP_ROUTEADV
;
6009 /* Check if handling a regular peer. */
6010 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6011 /* Update peer route announcements. */
6012 update_group_adjust_peer_afs(peer
);
6013 if (peer_established(peer
))
6014 bgp_announce_route_all(peer
);
6016 /* Skip peer-group mechanics for regular peers. */
6021 * Remove flag and configuration from all peer-group members, unless
6022 * they are explicitly overriding peer-group configuration.
6024 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6025 /* Skip peers with overridden configuration. */
6026 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
6029 /* Remove flag and configuration on peer-group member. */
6030 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
6031 member
->routeadv
= 0;
6032 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
6033 ? BGP_DEFAULT_IBGP_ROUTEADV
6034 : BGP_DEFAULT_EBGP_ROUTEADV
;
6036 /* Update peer route announcements. */
6037 update_group_adjust_peer_afs(member
);
6038 if (peer_established(member
))
6039 bgp_announce_route_all(member
);
6045 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
6048 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
6050 struct peer
*member
;
6051 struct listnode
*node
;
6053 /* Set peers session attribute flag and timer interval. */
6054 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6055 peer
->delayopen
= delayopen
;
6056 peer
->v_delayopen
= delayopen
;
6058 /* Skip group mechanics for regular peers. */
6059 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6062 /* Set flag and configuration on all peer-group members, unless they are
6063 * explicitly overriding peer-group configuration.
6065 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6066 /* Skip peers with overridden configuration. */
6067 if (CHECK_FLAG(member
->flags_override
,
6068 PEER_FLAG_TIMER_DELAYOPEN
))
6071 /* Set session attribute flag and timer intervals on peer-group
6074 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6075 member
->delayopen
= delayopen
;
6076 member
->v_delayopen
= delayopen
;
6082 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
6083 * DelayOpenTimer interval to the default value.
6085 int peer_timers_delayopen_unset(struct peer
*peer
)
6087 struct peer
*member
;
6088 struct listnode
*node
;
6090 /* Inherit configuration from peer-group if peer is member. */
6091 if (peer_group_active(peer
)) {
6092 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6093 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
6095 /* Otherwise remove session attribute flag and set timer
6096 * interval to default value.
6098 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6099 peer
->delayopen
= peer
->bgp
->default_delayopen
;
6102 /* Set timer value to zero */
6103 peer
->v_delayopen
= 0;
6105 /* Skip peer-group mechanics for regular peers. */
6106 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6109 /* Remove flag and configuration from all peer-group members, unless
6110 * they are explicitly overriding peer-group configuration.
6112 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6113 /* Skip peers with overridden configuration. */
6114 if (CHECK_FLAG(member
->flags_override
,
6115 PEER_FLAG_TIMER_DELAYOPEN
))
6118 /* Remove session attribute flag, reset the timer interval to
6119 * the default value and set the timer value to zero.
6121 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6122 member
->delayopen
= peer
->bgp
->default_delayopen
;
6123 member
->v_delayopen
= 0;
6129 /* neighbor interface */
6130 void peer_interface_set(struct peer
*peer
, const char *str
)
6132 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6133 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
6136 void peer_interface_unset(struct peer
*peer
)
6138 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6142 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6143 int allow_num
, int origin
)
6145 struct peer
*member
;
6146 struct listnode
*node
, *nnode
;
6148 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
6149 return BGP_ERR_INVALID_VALUE
;
6151 /* Set flag and configuration on peer. */
6152 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6154 if (peer
->allowas_in
[afi
][safi
] != 0
6155 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6156 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6157 peer_af_flag_set(peer
, afi
, safi
,
6158 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6159 peer
->allowas_in
[afi
][safi
] = 0;
6160 peer_on_policy_change(peer
, afi
, safi
, 0);
6163 if (peer
->allowas_in
[afi
][safi
] != allow_num
6164 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6165 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6167 peer_af_flag_unset(peer
, afi
, safi
,
6168 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6169 peer
->allowas_in
[afi
][safi
] = allow_num
;
6170 peer_on_policy_change(peer
, afi
, safi
, 0);
6174 /* Skip peer-group mechanics for regular peers. */
6175 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6179 * Set flag and configuration on all peer-group members, unless
6180 * they are explicitly overriding peer-group configuration.
6182 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6183 /* Skip peers with overridden configuration. */
6184 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6185 PEER_FLAG_ALLOWAS_IN
))
6188 /* Set flag and configuration on peer-group member. */
6189 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6191 if (member
->allowas_in
[afi
][safi
] != 0
6192 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
6193 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6194 SET_FLAG(member
->af_flags
[afi
][safi
],
6195 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6196 member
->allowas_in
[afi
][safi
] = 0;
6197 peer_on_policy_change(peer
, afi
, safi
, 0);
6200 if (member
->allowas_in
[afi
][safi
] != allow_num
6201 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
6202 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6203 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6204 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6205 member
->allowas_in
[afi
][safi
] = allow_num
;
6206 peer_on_policy_change(peer
, afi
, safi
, 0);
6214 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6216 struct peer
*member
;
6217 struct listnode
*node
, *nnode
;
6219 /* Skip peer if flag is already disabled. */
6220 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
6223 /* Inherit configuration from peer-group if peer is member. */
6224 if (peer_group_active(peer
)) {
6225 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6226 peer_af_flag_inherit(peer
, afi
, safi
,
6227 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6228 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
6229 peer_on_policy_change(peer
, afi
, safi
, 0);
6234 /* Remove flag and configuration from peer. */
6235 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6236 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6237 peer
->allowas_in
[afi
][safi
] = 0;
6238 peer_on_policy_change(peer
, afi
, safi
, 0);
6240 /* Skip peer-group mechanics if handling a regular peer. */
6241 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6245 * Remove flags and configuration from all peer-group members, unless
6246 * they are explicitly overriding peer-group configuration.
6248 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6249 /* Skip peers with overridden configuration. */
6250 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6251 PEER_FLAG_ALLOWAS_IN
))
6254 /* Remove flags and configuration on peer-group member. */
6255 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6256 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6257 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6258 member
->allowas_in
[afi
][safi
] = 0;
6259 peer_on_policy_change(member
, afi
, safi
, 0);
6265 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
6266 bool replace_as
, const char *as_str
)
6268 bool old_no_prepend
, old_replace_as
;
6269 struct bgp
*bgp
= peer
->bgp
;
6270 struct peer
*member
;
6271 struct listnode
*node
, *nnode
;
6274 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
6276 /* Save previous flag states. */
6278 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6280 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6282 /* Set flag and configuration on peer. */
6283 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
6284 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
6285 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
6287 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
6288 && old_replace_as
== replace_as
)
6290 peer
->change_local_as
= as
;
6292 peer
->change_local_as_pretty
= XSTRDUP(MTYPE_BGP
, as_str
);
6294 (void)peer_sort(peer
);
6296 /* Check if handling a regular peer. */
6297 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6301 * Set flag and configuration on all peer-group members, unless they are
6302 * explicitly overriding peer-group configuration.
6304 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6305 /* Skip peers with overridden configuration. */
6306 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6309 /* Skip peers with the same configuration. */
6310 old_no_prepend
= CHECK_FLAG(member
->flags
,
6311 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6312 old_replace_as
= CHECK_FLAG(member
->flags
,
6313 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6314 if (member
->change_local_as
== as
6315 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
6316 && old_no_prepend
== no_prepend
6317 && old_replace_as
== replace_as
)
6320 /* Set flag and configuration on peer-group member. */
6321 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6322 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
6324 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
6326 member
->change_local_as
= as
;
6328 member
->change_local_as_pretty
=
6329 XSTRDUP(MTYPE_BGP
, as_str
);
6335 int peer_local_as_unset(struct peer
*peer
)
6337 struct peer
*member
;
6338 struct listnode
*node
, *nnode
;
6340 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
6343 /* Inherit configuration from peer-group if peer is member. */
6344 if (peer_group_active(peer
)) {
6345 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
6346 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6347 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6348 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
6350 /* Otherwise remove flag and configuration from peer. */
6351 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
6352 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6353 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6354 peer
->change_local_as
= 0;
6355 XFREE(MTYPE_BGP
, peer
->change_local_as_pretty
);
6358 /* Check if handling a regular peer. */
6359 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6360 /* Send notification or stop peer depending on state. */
6361 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6362 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6363 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6364 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6366 BGP_EVENT_ADD(peer
, BGP_Stop
);
6368 /* Skip peer-group mechanics for regular peers. */
6373 * Remove flag and configuration from all peer-group members, unless
6374 * they are explicitly overriding peer-group configuration.
6376 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6377 /* Skip peers with overridden configuration. */
6378 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6381 /* Remove flag and configuration on peer-group member. */
6382 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6383 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6384 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6385 member
->change_local_as
= 0;
6386 XFREE(MTYPE_BGP
, member
->change_local_as_pretty
);
6388 /* Send notification or stop peer depending on state. */
6389 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6390 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6391 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6392 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6394 bgp_session_reset(member
);
6400 /* Set password for authenticating with the peer. */
6401 int peer_password_set(struct peer
*peer
, const char *password
)
6403 struct peer
*member
;
6404 struct listnode
*node
, *nnode
;
6405 int len
= password
? strlen(password
) : 0;
6406 int ret
= BGP_SUCCESS
;
6408 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
6409 return BGP_ERR_INVALID_VALUE
;
6411 /* Set flag and configuration on peer. */
6412 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
6413 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
6415 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6416 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6418 /* Check if handling a regular peer. */
6419 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6420 /* Send notification or reset peer depending on state. */
6421 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6422 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6423 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6425 bgp_session_reset(peer
);
6428 * Attempt to install password on socket and skip peer-group
6431 if (BGP_PEER_SU_UNSPEC(peer
))
6433 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6434 : BGP_ERR_TCPSIG_FAILED
;
6438 * Set flag and configuration on all peer-group members, unless they are
6439 * explicitly overriding peer-group configuration.
6441 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6442 /* Skip peers with overridden configuration. */
6443 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6446 /* Skip peers with the same password. */
6447 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6450 /* Set flag and configuration on peer-group member. */
6451 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6452 if (member
->password
)
6453 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6454 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6456 /* Send notification or reset peer depending on state. */
6457 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6458 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6459 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6461 bgp_session_reset(member
);
6463 /* Attempt to install password on socket. */
6464 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6465 ret
= BGP_ERR_TCPSIG_FAILED
;
6468 /* Set flag and configuration on all peer-group listen ranges */
6469 struct listnode
*ln
;
6472 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6473 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6474 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6475 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6480 int peer_password_unset(struct peer
*peer
)
6482 struct peer
*member
;
6483 struct listnode
*node
, *nnode
;
6485 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6488 /* Inherit configuration from peer-group if peer is member. */
6489 if (peer_group_active(peer
)) {
6490 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6491 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6492 MTYPE_PEER_PASSWORD
);
6494 /* Otherwise remove flag and configuration from peer. */
6495 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6496 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6499 /* Check if handling a regular peer. */
6500 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6501 /* Send notification or reset peer depending on state. */
6502 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6503 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6504 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6506 bgp_session_reset(peer
);
6508 /* Attempt to uninstall password on socket. */
6509 if (!BGP_PEER_SU_UNSPEC(peer
))
6510 bgp_md5_unset(peer
);
6511 /* Skip peer-group mechanics for regular peers. */
6516 * Remove flag and configuration from all peer-group members, unless
6517 * they are explicitly overriding peer-group configuration.
6519 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6520 /* Skip peers with overridden configuration. */
6521 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6524 /* Remove flag and configuration on peer-group member. */
6525 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6526 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6528 /* Send notification or reset peer depending on state. */
6529 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6530 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6531 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6533 bgp_session_reset(member
);
6535 /* Attempt to uninstall password on socket. */
6536 if (!BGP_PEER_SU_UNSPEC(member
))
6537 bgp_md5_unset(member
);
6540 /* Set flag and configuration on all peer-group listen ranges */
6541 struct listnode
*ln
;
6544 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6545 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6546 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6547 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6553 /* Set distribute list to the peer. */
6554 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6557 struct peer
*member
;
6558 struct bgp_filter
*filter
;
6559 struct listnode
*node
, *nnode
;
6561 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6562 return BGP_ERR_INVALID_VALUE
;
6564 /* Set configuration on peer. */
6565 filter
= &peer
->filter
[afi
][safi
];
6566 if (filter
->plist
[direct
].name
)
6567 return BGP_ERR_PEER_FILTER_CONFLICT
;
6568 if (filter
->dlist
[direct
].name
)
6569 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6570 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6571 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6573 /* Check if handling a regular peer. */
6574 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6575 /* Set override-flag and process peer route updates. */
6576 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6577 PEER_FT_DISTRIBUTE_LIST
);
6578 peer_on_policy_change(peer
, afi
, safi
,
6579 (direct
== FILTER_OUT
) ? 1 : 0);
6581 /* Skip peer-group mechanics for regular peers. */
6586 * Set configuration on all peer-group members, un less they are
6587 * explicitly overriding peer-group configuration.
6589 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6590 /* Skip peers with overridden configuration. */
6591 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6592 PEER_FT_DISTRIBUTE_LIST
))
6595 /* Set configuration on peer-group member. */
6596 filter
= &member
->filter
[afi
][safi
];
6597 if (filter
->dlist
[direct
].name
)
6598 XFREE(MTYPE_BGP_FILTER_NAME
,
6599 filter
->dlist
[direct
].name
);
6600 filter
->dlist
[direct
].name
=
6601 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6602 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6604 /* Process peer route updates. */
6605 peer_on_policy_change(member
, afi
, safi
,
6606 (direct
== FILTER_OUT
) ? 1 : 0);
6612 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6614 struct peer
*member
;
6615 struct bgp_filter
*filter
;
6616 struct listnode
*node
, *nnode
;
6618 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6619 return BGP_ERR_INVALID_VALUE
;
6621 /* Unset override-flag unconditionally. */
6622 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6623 PEER_FT_DISTRIBUTE_LIST
);
6625 /* Inherit configuration from peer-group if peer is member. */
6626 if (peer_group_active(peer
)) {
6627 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6628 filter
[afi
][safi
].dlist
[direct
].name
,
6629 MTYPE_BGP_FILTER_NAME
);
6630 PEER_ATTR_INHERIT(peer
, peer
->group
,
6631 filter
[afi
][safi
].dlist
[direct
].alist
);
6633 /* Otherwise remove configuration from peer. */
6634 filter
= &peer
->filter
[afi
][safi
];
6635 if (filter
->dlist
[direct
].name
)
6636 XFREE(MTYPE_BGP_FILTER_NAME
,
6637 filter
->dlist
[direct
].name
);
6638 filter
->dlist
[direct
].name
= NULL
;
6639 filter
->dlist
[direct
].alist
= NULL
;
6642 /* Check if handling a regular peer. */
6643 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6644 /* Process peer route updates. */
6645 peer_on_policy_change(peer
, afi
, safi
,
6646 (direct
== FILTER_OUT
) ? 1 : 0);
6648 /* Skip peer-group mechanics for regular peers. */
6653 * Remove configuration on all peer-group members, unless they are
6654 * explicitly overriding peer-group configuration.
6656 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6657 /* Skip peers with overridden configuration. */
6658 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6659 PEER_FT_DISTRIBUTE_LIST
))
6662 /* Remove configuration on peer-group member. */
6663 filter
= &member
->filter
[afi
][safi
];
6664 if (filter
->dlist
[direct
].name
)
6665 XFREE(MTYPE_BGP_FILTER_NAME
,
6666 filter
->dlist
[direct
].name
);
6667 filter
->dlist
[direct
].name
= NULL
;
6668 filter
->dlist
[direct
].alist
= NULL
;
6670 /* Process peer route updates. */
6671 peer_on_policy_change(member
, afi
, safi
,
6672 (direct
== FILTER_OUT
) ? 1 : 0);
6678 /* Update distribute list. */
6679 static void peer_distribute_update(struct access_list
*access
)
6684 struct listnode
*mnode
, *mnnode
;
6685 struct listnode
*node
, *nnode
;
6688 struct peer_group
*group
;
6689 struct bgp_filter
*filter
;
6691 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6693 update_group_policy_update(bgp
,
6694 BGP_POLICY_DISTRIBUTE_LIST
,
6695 access
->name
, true, 0);
6696 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6697 FOREACH_AFI_SAFI (afi
, safi
) {
6698 filter
= &peer
->filter
[afi
][safi
];
6700 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6702 if (filter
->dlist
[direct
].name
)
6703 filter
->dlist
[direct
]
6704 .alist
= access_list_lookup(
6706 filter
->dlist
[direct
]
6709 filter
->dlist
[direct
].alist
=
6714 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6715 FOREACH_AFI_SAFI (afi
, safi
) {
6716 filter
= &group
->conf
->filter
[afi
][safi
];
6718 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6720 if (filter
->dlist
[direct
].name
)
6721 filter
->dlist
[direct
]
6722 .alist
= access_list_lookup(
6724 filter
->dlist
[direct
]
6727 filter
->dlist
[direct
].alist
=
6732 #ifdef ENABLE_BGP_VNC
6733 vnc_prefix_list_update(bgp
);
6738 /* Set prefix list to the peer. */
6739 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6742 struct peer
*member
;
6743 struct bgp_filter
*filter
;
6744 struct listnode
*node
, *nnode
;
6746 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6747 return BGP_ERR_INVALID_VALUE
;
6749 /* Set configuration on peer. */
6750 filter
= &peer
->filter
[afi
][safi
];
6751 if (filter
->dlist
[direct
].name
)
6752 return BGP_ERR_PEER_FILTER_CONFLICT
;
6753 if (filter
->plist
[direct
].name
)
6754 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6755 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6756 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6758 /* Check if handling a regular peer. */
6759 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6760 /* Set override-flag and process peer route updates. */
6761 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6762 PEER_FT_PREFIX_LIST
);
6763 peer_on_policy_change(peer
, afi
, safi
,
6764 (direct
== FILTER_OUT
) ? 1 : 0);
6766 /* Skip peer-group mechanics for regular peers. */
6771 * Set configuration on all peer-group members, unless they are
6772 * explicitly overriding peer-group configuration.
6774 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6775 /* Skip peers with overridden configuration. */
6776 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6777 PEER_FT_PREFIX_LIST
))
6780 /* Set configuration on peer-group member. */
6781 filter
= &member
->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
=
6786 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6787 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6789 /* Process peer route updates. */
6790 peer_on_policy_change(member
, afi
, safi
,
6791 (direct
== FILTER_OUT
) ? 1 : 0);
6797 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6800 struct peer
*member
;
6801 struct bgp_filter
*filter
;
6802 struct listnode
*node
, *nnode
;
6804 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6805 return BGP_ERR_INVALID_VALUE
;
6807 /* Unset override-flag unconditionally. */
6808 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6809 PEER_FT_PREFIX_LIST
);
6811 /* Inherit configuration from peer-group if peer is member. */
6812 if (peer_group_active(peer
)) {
6813 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6814 filter
[afi
][safi
].plist
[direct
].name
,
6815 MTYPE_BGP_FILTER_NAME
);
6816 PEER_ATTR_INHERIT(peer
, peer
->group
,
6817 filter
[afi
][safi
].plist
[direct
].plist
);
6819 /* Otherwise remove configuration from peer. */
6820 filter
= &peer
->filter
[afi
][safi
];
6821 if (filter
->plist
[direct
].name
)
6822 XFREE(MTYPE_BGP_FILTER_NAME
,
6823 filter
->plist
[direct
].name
);
6824 filter
->plist
[direct
].name
= NULL
;
6825 filter
->plist
[direct
].plist
= NULL
;
6828 /* Check if handling a regular peer. */
6829 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6830 /* Process peer route updates. */
6831 peer_on_policy_change(peer
, afi
, safi
,
6832 (direct
== FILTER_OUT
) ? 1 : 0);
6834 /* Skip peer-group mechanics for regular peers. */
6839 * Remove configuration on all peer-group members, unless they are
6840 * explicitly overriding peer-group configuration.
6842 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6843 /* Skip peers with overridden configuration. */
6844 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6845 PEER_FT_PREFIX_LIST
))
6848 /* Remove configuration on peer-group member. */
6849 filter
= &member
->filter
[afi
][safi
];
6850 if (filter
->plist
[direct
].name
)
6851 XFREE(MTYPE_BGP_FILTER_NAME
,
6852 filter
->plist
[direct
].name
);
6853 filter
->plist
[direct
].name
= NULL
;
6854 filter
->plist
[direct
].plist
= NULL
;
6856 /* Process peer route updates. */
6857 peer_on_policy_change(member
, afi
, safi
,
6858 (direct
== FILTER_OUT
) ? 1 : 0);
6864 /* Update prefix-list list. */
6865 static void peer_prefix_list_update(struct prefix_list
*plist
)
6867 struct listnode
*mnode
, *mnnode
;
6868 struct listnode
*node
, *nnode
;
6871 struct peer_group
*group
;
6872 struct bgp_filter
*filter
;
6877 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6880 * Update the prefix-list on update groups.
6882 update_group_policy_update(
6883 bgp
, BGP_POLICY_PREFIX_LIST
,
6884 plist
? prefix_list_name(plist
) : NULL
, true, 0);
6886 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6887 FOREACH_AFI_SAFI (afi
, safi
) {
6888 filter
= &peer
->filter
[afi
][safi
];
6890 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6892 if (filter
->plist
[direct
].name
)
6893 filter
->plist
[direct
]
6894 .plist
= prefix_list_lookup(
6896 filter
->plist
[direct
]
6899 filter
->plist
[direct
].plist
=
6903 /* If we touch prefix-list, we need to process
6904 * new updates. This is important for ORF to
6905 * work correctly as well.
6907 if (peer
->afc_nego
[afi
][safi
])
6908 peer_on_policy_change(peer
, afi
, safi
,
6912 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6913 FOREACH_AFI_SAFI (afi
, safi
) {
6914 filter
= &group
->conf
->filter
[afi
][safi
];
6916 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6918 if (filter
->plist
[direct
].name
)
6919 filter
->plist
[direct
]
6920 .plist
= prefix_list_lookup(
6922 filter
->plist
[direct
]
6925 filter
->plist
[direct
].plist
=
6933 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6936 struct peer
*member
;
6937 struct bgp_filter
*filter
;
6938 struct listnode
*node
, *nnode
;
6940 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6941 return BGP_ERR_INVALID_VALUE
;
6943 /* Set configuration on peer. */
6944 filter
= &peer
->filter
[afi
][safi
];
6945 if (filter
->aslist
[direct
].name
)
6946 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6947 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6948 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6950 /* Check if handling a regular peer. */
6951 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6952 /* Set override-flag and process peer route updates. */
6953 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6954 PEER_FT_FILTER_LIST
);
6955 peer_on_policy_change(peer
, afi
, safi
,
6956 (direct
== FILTER_OUT
) ? 1 : 0);
6958 /* Skip peer-group mechanics for regular peers. */
6963 * Set configuration on all peer-group members, unless they are
6964 * explicitly overriding peer-group configuration.
6966 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6967 /* Skip peers with overridden configuration. */
6968 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6969 PEER_FT_FILTER_LIST
))
6972 /* Set configuration on peer-group member. */
6973 filter
= &member
->filter
[afi
][safi
];
6974 if (filter
->aslist
[direct
].name
)
6975 XFREE(MTYPE_BGP_FILTER_NAME
,
6976 filter
->aslist
[direct
].name
);
6977 filter
->aslist
[direct
].name
=
6978 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6979 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6981 /* Process peer route updates. */
6982 peer_on_policy_change(member
, afi
, safi
,
6983 (direct
== FILTER_OUT
) ? 1 : 0);
6989 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6991 struct peer
*member
;
6992 struct bgp_filter
*filter
;
6993 struct listnode
*node
, *nnode
;
6995 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6996 return BGP_ERR_INVALID_VALUE
;
6998 /* Unset override-flag unconditionally. */
6999 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
7000 PEER_FT_FILTER_LIST
);
7002 /* Inherit configuration from peer-group if peer is member. */
7003 if (peer_group_active(peer
)) {
7004 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7005 filter
[afi
][safi
].aslist
[direct
].name
,
7006 MTYPE_BGP_FILTER_NAME
);
7007 PEER_ATTR_INHERIT(peer
, peer
->group
,
7008 filter
[afi
][safi
].aslist
[direct
].aslist
);
7010 /* Otherwise remove configuration from peer. */
7011 filter
= &peer
->filter
[afi
][safi
];
7012 if (filter
->aslist
[direct
].name
)
7013 XFREE(MTYPE_BGP_FILTER_NAME
,
7014 filter
->aslist
[direct
].name
);
7015 filter
->aslist
[direct
].name
= NULL
;
7016 filter
->aslist
[direct
].aslist
= NULL
;
7019 /* Check if handling a regular peer. */
7020 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7021 /* Process peer route updates. */
7022 peer_on_policy_change(peer
, afi
, safi
,
7023 (direct
== FILTER_OUT
) ? 1 : 0);
7025 /* Skip peer-group mechanics for regular peers. */
7030 * Remove configuration on all peer-group members, unless they are
7031 * explicitly overriding peer-group configuration.
7033 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7034 /* Skip peers with overridden configuration. */
7035 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7036 PEER_FT_FILTER_LIST
))
7039 /* Remove configuration on peer-group member. */
7040 filter
= &member
->filter
[afi
][safi
];
7041 if (filter
->aslist
[direct
].name
)
7042 XFREE(MTYPE_BGP_FILTER_NAME
,
7043 filter
->aslist
[direct
].name
);
7044 filter
->aslist
[direct
].name
= NULL
;
7045 filter
->aslist
[direct
].aslist
= NULL
;
7047 /* Process peer route updates. */
7048 peer_on_policy_change(member
, afi
, safi
,
7049 (direct
== FILTER_OUT
) ? 1 : 0);
7055 static void peer_aslist_update(const char *aslist_name
)
7060 struct listnode
*mnode
, *mnnode
;
7061 struct listnode
*node
, *nnode
;
7064 struct peer_group
*group
;
7065 struct bgp_filter
*filter
;
7067 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
7068 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
7069 aslist_name
, true, 0);
7071 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7072 FOREACH_AFI_SAFI (afi
, safi
) {
7073 filter
= &peer
->filter
[afi
][safi
];
7075 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7077 if (filter
->aslist
[direct
].name
)
7078 filter
->aslist
[direct
]
7079 .aslist
= as_list_lookup(
7080 filter
->aslist
[direct
]
7083 filter
->aslist
[direct
].aslist
=
7088 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
7089 FOREACH_AFI_SAFI (afi
, safi
) {
7090 filter
= &group
->conf
->filter
[afi
][safi
];
7092 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7094 if (filter
->aslist
[direct
].name
)
7095 filter
->aslist
[direct
]
7096 .aslist
= as_list_lookup(
7097 filter
->aslist
[direct
]
7100 filter
->aslist
[direct
].aslist
=
7108 static void peer_aslist_add(char *aslist_name
)
7110 peer_aslist_update(aslist_name
);
7111 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
7114 static void peer_aslist_del(const char *aslist_name
)
7116 peer_aslist_update(aslist_name
);
7117 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
7121 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
7122 const char *name
, struct route_map
*route_map
)
7124 struct peer
*member
;
7125 struct bgp_filter
*filter
;
7126 struct listnode
*node
, *nnode
;
7128 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7129 return BGP_ERR_INVALID_VALUE
;
7131 /* Set configuration on peer. */
7132 filter
= &peer
->filter
[afi
][safi
];
7133 if (filter
->map
[direct
].name
) {
7134 /* If the neighbor is configured with the same route-map
7135 * again then, ignore the duplicate configuration.
7137 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
7140 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7142 route_map_counter_decrement(filter
->map
[direct
].map
);
7143 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7144 filter
->map
[direct
].map
= route_map
;
7145 route_map_counter_increment(route_map
);
7147 /* Check if handling a regular peer. */
7148 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7149 /* Set override-flag and process peer route updates. */
7150 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
7152 peer_on_policy_change(peer
, afi
, safi
,
7153 (direct
== RMAP_OUT
) ? 1 : 0);
7155 /* Skip peer-group mechanics for regular peers. */
7160 * Set configuration on all peer-group members, unless they are
7161 * explicitly overriding peer-group configuration.
7163 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7164 /* Skip peers with overridden configuration. */
7165 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7169 /* Set configuration on peer-group member. */
7170 filter
= &member
->filter
[afi
][safi
];
7171 if (filter
->map
[direct
].name
)
7172 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7173 route_map_counter_decrement(filter
->map
[direct
].map
);
7174 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7175 filter
->map
[direct
].map
= route_map
;
7176 route_map_counter_increment(route_map
);
7178 /* Process peer route updates. */
7179 peer_on_policy_change(member
, afi
, safi
,
7180 (direct
== RMAP_OUT
) ? 1 : 0);
7185 /* Unset route-map from the peer. */
7186 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
7188 struct peer
*member
;
7189 struct bgp_filter
*filter
;
7190 struct listnode
*node
, *nnode
;
7192 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7193 return BGP_ERR_INVALID_VALUE
;
7195 /* Unset override-flag unconditionally. */
7196 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
7198 /* Inherit configuration from peer-group if peer is member. */
7199 if (peer_group_active(peer
)) {
7200 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7201 filter
[afi
][safi
].map
[direct
].name
,
7202 MTYPE_BGP_FILTER_NAME
);
7203 PEER_ATTR_INHERIT(peer
, peer
->group
,
7204 filter
[afi
][safi
].map
[direct
].map
);
7206 /* Otherwise remove configuration from peer. */
7207 filter
= &peer
->filter
[afi
][safi
];
7208 if (filter
->map
[direct
].name
)
7209 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7210 route_map_counter_decrement(filter
->map
[direct
].map
);
7211 filter
->map
[direct
].name
= NULL
;
7212 filter
->map
[direct
].map
= NULL
;
7215 /* Check if handling a regular peer. */
7216 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7217 /* Process peer route updates. */
7218 peer_on_policy_change(peer
, afi
, safi
,
7219 (direct
== RMAP_OUT
) ? 1 : 0);
7221 /* Skip peer-group mechanics for regular peers. */
7226 * Remove configuration on all peer-group members, unless they are
7227 * explicitly overriding peer-group configuration.
7229 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7230 /* Skip peers with overridden configuration. */
7231 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7235 /* Remove configuration on peer-group member. */
7236 filter
= &member
->filter
[afi
][safi
];
7237 if (filter
->map
[direct
].name
)
7238 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7239 route_map_counter_decrement(filter
->map
[direct
].map
);
7240 filter
->map
[direct
].name
= NULL
;
7241 filter
->map
[direct
].map
= NULL
;
7243 /* Process peer route updates. */
7244 peer_on_policy_change(member
, afi
, safi
,
7245 (direct
== RMAP_OUT
) ? 1 : 0);
7251 /* Set unsuppress-map to the peer. */
7252 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7253 const char *name
, struct route_map
*route_map
)
7255 struct peer
*member
;
7256 struct bgp_filter
*filter
;
7257 struct listnode
*node
, *nnode
;
7259 /* Set configuration on peer. */
7260 filter
= &peer
->filter
[afi
][safi
];
7261 if (filter
->usmap
.name
)
7262 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7263 route_map_counter_decrement(filter
->usmap
.map
);
7264 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7265 filter
->usmap
.map
= route_map
;
7266 route_map_counter_increment(route_map
);
7268 /* Check if handling a regular peer. */
7269 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7270 /* Set override-flag and process peer route updates. */
7271 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
7272 PEER_FT_UNSUPPRESS_MAP
);
7273 peer_on_policy_change(peer
, afi
, safi
, 1);
7275 /* Skip peer-group mechanics for regular peers. */
7280 * Set configuration on all peer-group members, unless they are
7281 * explicitly overriding peer-group configuration.
7283 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7284 /* Skip peers with overridden configuration. */
7285 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7286 PEER_FT_UNSUPPRESS_MAP
))
7289 /* Set configuration on peer-group member. */
7290 filter
= &member
->filter
[afi
][safi
];
7291 if (filter
->usmap
.name
)
7292 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7293 route_map_counter_decrement(filter
->usmap
.map
);
7294 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7295 filter
->usmap
.map
= route_map
;
7296 route_map_counter_increment(route_map
);
7298 /* Process peer route updates. */
7299 peer_on_policy_change(member
, afi
, safi
, 1);
7305 /* Unset route-map from the peer. */
7306 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7308 struct peer
*member
;
7309 struct bgp_filter
*filter
;
7310 struct listnode
*node
, *nnode
;
7312 /* Unset override-flag unconditionally. */
7313 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
7315 /* Inherit configuration from peer-group if peer is member. */
7316 if (peer_group_active(peer
)) {
7317 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7318 filter
[afi
][safi
].usmap
.name
,
7319 MTYPE_BGP_FILTER_NAME
);
7320 PEER_ATTR_INHERIT(peer
, peer
->group
,
7321 filter
[afi
][safi
].usmap
.map
);
7323 /* Otherwise remove configuration from peer. */
7324 filter
= &peer
->filter
[afi
][safi
];
7325 if (filter
->usmap
.name
)
7326 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7327 route_map_counter_decrement(filter
->usmap
.map
);
7328 filter
->usmap
.name
= NULL
;
7329 filter
->usmap
.map
= NULL
;
7332 /* Check if handling a regular peer. */
7333 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7334 /* Process peer route updates. */
7335 peer_on_policy_change(peer
, afi
, safi
, 1);
7337 /* Skip peer-group mechanics for regular peers. */
7342 * Remove configuration on all peer-group members, unless they are
7343 * explicitly overriding peer-group configuration.
7345 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7346 /* Skip peers with overridden configuration. */
7347 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7348 PEER_FT_UNSUPPRESS_MAP
))
7351 /* Remove configuration on peer-group member. */
7352 filter
= &member
->filter
[afi
][safi
];
7353 if (filter
->usmap
.name
)
7354 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7355 route_map_counter_decrement(filter
->usmap
.map
);
7356 filter
->usmap
.name
= NULL
;
7357 filter
->usmap
.map
= NULL
;
7359 /* Process peer route updates. */
7360 peer_on_policy_change(member
, afi
, safi
, 1);
7366 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7368 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7371 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7372 if (peer
->t_pmax_restart
) {
7373 THREAD_OFF(peer
->t_pmax_restart
);
7374 if (bgp_debug_neighbor_events(peer
))
7376 "%pBP Maximum-prefix restart timer cancelled",
7379 BGP_EVENT_ADD(peer
, BGP_Start
);
7383 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7384 uint32_t max
, uint8_t threshold
, int warning
,
7385 uint16_t restart
, bool force
)
7387 struct peer
*member
;
7388 struct listnode
*node
, *nnode
;
7390 /* Set flags and configuration on peer. */
7391 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7394 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7396 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7399 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7401 peer_af_flag_unset(peer
, afi
, safi
,
7402 PEER_FLAG_MAX_PREFIX_WARNING
);
7404 peer
->pmax
[afi
][safi
] = max
;
7405 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7406 peer
->pmax_restart
[afi
][safi
] = restart
;
7408 /* Check if handling a regular peer. */
7409 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7410 /* Re-check if peer violates maximum-prefix. */
7411 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7412 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7414 /* Skip peer-group mechanics for regular peers. */
7419 * Set flags and configuration on all peer-group members, unless they
7420 * are explicitly overriding peer-group configuration.
7422 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7423 /* Skip peers with overridden configuration. */
7424 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7425 PEER_FLAG_MAX_PREFIX
))
7428 /* Set flag and configuration on peer-group member. */
7429 member
->pmax
[afi
][safi
] = max
;
7430 member
->pmax_threshold
[afi
][safi
] = threshold
;
7431 member
->pmax_restart
[afi
][safi
] = restart
;
7434 SET_FLAG(member
->af_flags
[afi
][safi
],
7435 PEER_FLAG_MAX_PREFIX_FORCE
);
7437 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7438 PEER_FLAG_MAX_PREFIX_FORCE
);
7441 SET_FLAG(member
->af_flags
[afi
][safi
],
7442 PEER_FLAG_MAX_PREFIX_WARNING
);
7444 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7445 PEER_FLAG_MAX_PREFIX_WARNING
);
7447 /* Re-check if peer violates maximum-prefix. */
7448 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7449 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7455 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7457 /* Inherit configuration from peer-group if peer is member. */
7458 if (peer_group_active(peer
)) {
7459 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7460 peer_af_flag_inherit(peer
, afi
, safi
,
7461 PEER_FLAG_MAX_PREFIX_FORCE
);
7462 peer_af_flag_inherit(peer
, afi
, safi
,
7463 PEER_FLAG_MAX_PREFIX_WARNING
);
7464 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7465 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7466 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7471 /* Remove flags and configuration from peer. */
7472 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7473 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7474 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7475 peer
->pmax
[afi
][safi
] = 0;
7476 peer
->pmax_threshold
[afi
][safi
] = 0;
7477 peer
->pmax_restart
[afi
][safi
] = 0;
7480 * Remove flags and configuration from all peer-group members, unless
7481 * they are explicitly overriding peer-group configuration.
7483 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7484 struct peer
*member
;
7485 struct listnode
*node
;
7487 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7488 /* Skip peers with overridden configuration. */
7489 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7490 PEER_FLAG_MAX_PREFIX
))
7493 /* Remove flag and configuration on peer-group member.
7495 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7496 PEER_FLAG_MAX_PREFIX
);
7497 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7498 PEER_FLAG_MAX_PREFIX_FORCE
);
7499 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7500 PEER_FLAG_MAX_PREFIX_WARNING
);
7501 member
->pmax
[afi
][safi
] = 0;
7502 member
->pmax_threshold
[afi
][safi
] = 0;
7503 member
->pmax_restart
[afi
][safi
] = 0;
7505 peer_maximum_prefix_clear_overflow(member
);
7508 peer_maximum_prefix_clear_overflow(peer
);
7514 void peer_maximum_prefix_out_refresh_routes(struct peer
*peer
, afi_t afi
,
7517 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
7519 if (peer_established(peer
))
7520 bgp_announce_route(peer
, afi
, safi
, false);
7523 int peer_maximum_prefix_out_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7526 struct peer
*member
;
7527 struct listnode
*node
, *nnode
;
7529 /* Set flag on peer and peer-group member if any */
7530 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7531 /* Set configuration on peer. */
7532 peer
->pmax_out
[afi
][safi
] = max
;
7534 /* Check if handling a regular peer. */
7535 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7536 /* Skip peer-group mechanics for regular peers. */
7537 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7542 * Set flag and configuration on all peer-group members, unless they
7543 * are explicitly overriding peer-group configuration.
7545 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7546 /* Skip peers with overridden configuration. */
7547 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7548 PEER_FLAG_MAX_PREFIX_OUT
))
7551 /* Set configuration on peer-group member. */
7552 member
->pmax_out
[afi
][safi
] = max
;
7554 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7559 int peer_maximum_prefix_out_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7561 struct peer
*member
;
7562 struct listnode
*node
;
7563 /* Inherit configuration from peer-group if peer is member. */
7564 if (peer_group_active(peer
)) {
7565 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7566 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_out
[afi
][safi
]);
7568 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7572 /* Remove flag and configuration from peer. */
7573 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7574 peer
->pmax_out
[afi
][safi
] = 0;
7576 /* Check if handling a regular peer. */
7577 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7578 /* Skip peer-group mechanics for regular peers. */
7579 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7584 * Remove flag and configuration from all peer-group members, unless
7585 * they are explicitly overriding peer-group configuration.
7587 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7588 /* Skip peers with overridden configuration. */
7589 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7590 PEER_FLAG_MAX_PREFIX_OUT
))
7593 /* Remove flag and configuration on peer-group member.
7595 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7596 PEER_FLAG_MAX_PREFIX_OUT
);
7597 member
->pmax_out
[afi
][safi
] = 0;
7599 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7604 int is_ebgp_multihop_configured(struct peer
*peer
)
7606 struct peer_group
*group
;
7607 struct listnode
*node
, *nnode
;
7610 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7611 group
= peer
->group
;
7612 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7613 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7616 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7617 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7618 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7622 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7623 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7629 /* Set # of hops between us and BGP peer. */
7630 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7632 struct peer_group
*group
;
7634 struct listnode
*node
, *nnode
;
7637 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7640 /* We cannot configure ttl-security hops when ebgp-multihop is already
7641 set. For non peer-groups, the check is simple. For peer-groups,
7643 slightly messy, because we need to check both the peer-group
7645 and all peer-group members for any trace of ebgp-multihop
7647 before actually applying the ttl-security rules. Cisco really made a
7648 mess of this configuration parameter, and OpenBGPD got it right.
7651 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7652 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7653 if (is_ebgp_multihop_configured(peer
))
7654 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7656 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7657 peer
->gtsm_hops
= gtsm_hops
;
7659 /* Calling ebgp multihop also resets the session.
7660 * On restart, NHT will get setup correctly as will the
7661 * min & max ttls on the socket. The return value is
7664 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7669 group
= peer
->group
;
7670 group
->conf
->gtsm_hops
= gtsm_hops
;
7671 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7673 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7675 /* Calling ebgp multihop also resets the
7677 * On restart, NHT will get setup correctly as
7679 * min & max ttls on the socket. The return
7683 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7687 /* Post the first gtsm setup or if its ibgp, maxttl setting
7689 * necessary, just set the minttl.
7691 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7692 peer
->gtsm_hops
= gtsm_hops
;
7695 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7696 MAXTTL
+ 1 - gtsm_hops
);
7697 if ((peer
->status
< Established
) && peer
->doppelganger
7698 && (peer
->doppelganger
->fd
>= 0))
7699 sockopt_minttl(peer
->su
.sa
.sa_family
,
7700 peer
->doppelganger
->fd
,
7701 MAXTTL
+ 1 - gtsm_hops
);
7703 group
= peer
->group
;
7704 group
->conf
->gtsm_hops
= gtsm_hops
;
7705 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7707 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7709 /* Change setting of existing peer
7710 * established then change value (may break
7712 * not established yet (teardown session and
7714 * no session then do nothing (will get
7715 * handled by next connection)
7719 != BGP_GTSM_HOPS_DISABLED
)
7721 gpeer
->su
.sa
.sa_family
,
7723 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7724 if ((gpeer
->status
< Established
)
7725 && gpeer
->doppelganger
7726 && (gpeer
->doppelganger
->fd
>= 0))
7727 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7728 gpeer
->doppelganger
->fd
,
7729 MAXTTL
+ 1 - gtsm_hops
);
7737 int peer_ttl_security_hops_unset(struct peer
*peer
)
7739 struct peer_group
*group
;
7740 struct listnode
*node
, *nnode
;
7743 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7745 /* if a peer-group member, then reset to peer-group default rather than
7747 if (peer_group_active(peer
))
7748 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7750 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7752 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7753 /* Invoking ebgp_multihop_set will set the TTL back to the
7755 * value as well as restting the NHT and such. The session is
7758 if (peer
->sort
== BGP_PEER_EBGP
)
7759 ret
= peer_ebgp_multihop_unset(peer
);
7762 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7765 if ((peer
->status
< Established
) && peer
->doppelganger
7766 && (peer
->doppelganger
->fd
>= 0))
7767 sockopt_minttl(peer
->su
.sa
.sa_family
,
7768 peer
->doppelganger
->fd
, 0);
7771 group
= peer
->group
;
7772 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7773 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7774 if (peer
->sort
== BGP_PEER_EBGP
)
7775 ret
= peer_ebgp_multihop_unset(peer
);
7778 sockopt_minttl(peer
->su
.sa
.sa_family
,
7781 if ((peer
->status
< Established
)
7782 && peer
->doppelganger
7783 && (peer
->doppelganger
->fd
>= 0))
7784 sockopt_minttl(peer
->su
.sa
.sa_family
,
7785 peer
->doppelganger
->fd
,
7794 static void peer_reset_message_stats(struct peer
*peer
)
7797 atomic_store_explicit(&peer
->open_in
, 0, memory_order_relaxed
);
7798 atomic_store_explicit(&peer
->open_out
, 0, memory_order_relaxed
);
7799 atomic_store_explicit(&peer
->update_in
, 0,
7800 memory_order_relaxed
);
7801 atomic_store_explicit(&peer
->update_out
, 0,
7802 memory_order_relaxed
);
7803 atomic_store_explicit(&peer
->keepalive_in
, 0,
7804 memory_order_relaxed
);
7805 atomic_store_explicit(&peer
->keepalive_out
, 0,
7806 memory_order_relaxed
);
7807 atomic_store_explicit(&peer
->notify_in
, 0,
7808 memory_order_relaxed
);
7809 atomic_store_explicit(&peer
->notify_out
, 0,
7810 memory_order_relaxed
);
7811 atomic_store_explicit(&peer
->refresh_in
, 0,
7812 memory_order_relaxed
);
7813 atomic_store_explicit(&peer
->refresh_out
, 0,
7814 memory_order_relaxed
);
7815 atomic_store_explicit(&peer
->dynamic_cap_in
, 0,
7816 memory_order_relaxed
);
7817 atomic_store_explicit(&peer
->dynamic_cap_out
, 0,
7818 memory_order_relaxed
);
7823 * If peer clear is invoked in a loop for all peers on the BGP instance,
7824 * it may end up freeing the doppelganger, and if this was the next node
7825 * to the current node, we would end up accessing the freed next node.
7826 * Pass along additional parameter which can be updated if next node
7827 * is freed; only required when walking the peer list on BGP instance.
7829 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7831 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7832 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7833 if (peer_maximum_prefix_clear_overflow(peer
))
7836 peer
->v_start
= BGP_INIT_START_TIMER
;
7837 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7838 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7839 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7841 bgp_session_reset_safe(peer
, nnode
);
7846 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7847 enum bgp_clear_type stype
)
7849 struct peer_af
*paf
;
7851 if (!peer_established(peer
))
7854 if (!peer
->afc
[afi
][safi
])
7855 return BGP_ERR_AF_UNCONFIGURED
;
7857 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7859 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7860 /* Clear the "neighbor x.x.x.x default-originate" flag */
7861 paf
= peer_af_find(peer
, afi
, safi
);
7862 if (paf
&& paf
->subgroup
7863 && CHECK_FLAG(paf
->subgroup
->sflags
,
7864 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7865 UNSET_FLAG(paf
->subgroup
->sflags
,
7866 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7868 bgp_announce_route(peer
, afi
, safi
, false);
7871 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7872 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7873 PEER_CAP_ORF_PREFIX_SM_ADV
)
7874 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7875 PEER_CAP_ORF_PREFIX_RM_RCV
)
7876 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7877 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7878 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7879 uint8_t prefix_type
;
7881 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7882 PEER_CAP_ORF_PREFIX_RM_RCV
))
7883 prefix_type
= ORF_TYPE_PREFIX
;
7885 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7887 if (filter
->plist
[FILTER_IN
].plist
) {
7888 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7889 PEER_STATUS_ORF_PREFIX_SEND
))
7890 bgp_route_refresh_send(
7891 peer
, afi
, safi
, prefix_type
,
7893 BGP_ROUTE_REFRESH_NORMAL
);
7894 bgp_route_refresh_send(
7895 peer
, afi
, safi
, prefix_type
,
7896 REFRESH_IMMEDIATE
, 0,
7897 BGP_ROUTE_REFRESH_NORMAL
);
7899 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7900 PEER_STATUS_ORF_PREFIX_SEND
))
7901 bgp_route_refresh_send(
7902 peer
, afi
, safi
, prefix_type
,
7903 REFRESH_IMMEDIATE
, 1,
7904 BGP_ROUTE_REFRESH_NORMAL
);
7906 bgp_route_refresh_send(
7907 peer
, afi
, safi
, 0, 0, 0,
7908 BGP_ROUTE_REFRESH_NORMAL
);
7914 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7915 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7916 /* If neighbor has soft reconfiguration inbound flag.
7917 Use Adj-RIB-In database. */
7918 if (!bgp_soft_reconfig_in(peer
, afi
, safi
)) {
7919 /* If neighbor has route refresh capability, send route
7921 message to the peer. */
7922 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7923 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7924 bgp_route_refresh_send(
7925 peer
, afi
, safi
, 0, 0, 0,
7926 BGP_ROUTE_REFRESH_NORMAL
);
7928 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7932 if (stype
== BGP_CLEAR_MESSAGE_STATS
)
7933 peer_reset_message_stats(peer
);
7938 /* Display peer uptime.*/
7939 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7942 time_t uptime1
, epoch_tbuf
;
7945 /* If there is no connection has been done before print `never'. */
7948 json_object_string_add(json
, "peerUptime", "never");
7949 json_object_int_add(json
, "peerUptimeMsec", 0);
7951 snprintf(buf
, len
, "never");
7955 /* Get current time. */
7956 uptime1
= monotime(NULL
);
7958 gmtime_r(&uptime1
, &tm
);
7960 if (uptime1
< ONE_DAY_SECOND
)
7961 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7963 else if (uptime1
< ONE_WEEK_SECOND
)
7964 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7966 else if (uptime1
< ONE_YEAR_SECOND
)
7967 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7968 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7970 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7972 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7975 epoch_tbuf
= time(NULL
) - uptime1
;
7976 json_object_string_add(json
, "peerUptime", buf
);
7977 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7978 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7985 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7986 struct list
*addresses
)
7990 memset(&bgp_master
, 0, sizeof(bgp_master
));
7993 bm
->bgp
= list_new();
7994 bm
->listen_sockets
= list_new();
7995 bm
->port
= BGP_PORT_DEFAULT
;
7996 bm
->addresses
= addresses
;
7997 bm
->master
= master
;
7998 bm
->start_time
= monotime(NULL
);
7999 bm
->t_rmap_update
= NULL
;
8000 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
8001 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
8002 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
8003 bm
->terminating
= false;
8004 bm
->socket_buffer
= buffer_size
;
8005 bm
->wait_for_fib
= false;
8006 bm
->tcp_dscp
= IPTOS_PREC_INTERNETCONTROL
;
8007 bm
->inq_limit
= BM_DEFAULT_Q_LIMIT
;
8008 bm
->outq_limit
= BM_DEFAULT_Q_LIMIT
;
8011 /* init the rd id space.
8012 assign 0th index in the bitfield,
8013 so that we start with id 1
8015 bf_init(bm
->rd_idspace
, UINT16_MAX
);
8016 bf_assign_zero_index(bm
->rd_idspace
);
8018 /* mpls label dynamic allocation pool */
8019 bgp_lp_init(bm
->master
, &bm
->labelpool
);
8023 QOBJ_REG(bm
, bgp_master
);
8027 * Free up connected routes and interfaces for a BGP instance. Invoked upon
8028 * instance delete (non-default only) or BGP exit.
8030 static void bgp_if_finish(struct bgp
*bgp
)
8033 struct interface
*ifp
;
8035 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
8037 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
8040 FOR_ALL_INTERFACES (vrf
, ifp
) {
8041 struct listnode
*c_node
, *c_nnode
;
8042 struct connected
*c
;
8044 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
8045 bgp_connected_delete(bgp
, c
);
8049 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
8051 struct vrf
*vrf
= NULL
;
8052 struct listnode
*next
;
8055 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
8056 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
8058 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8059 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
8062 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
8066 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
8068 struct listnode
*next
, *next2
;
8069 struct bgp
*bgp
, *bgp2
;
8070 char buf
[ASN_STRING_MAX_SIZE
];
8072 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8074 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
8075 if (bgp2
->as
== bgp
->as
)
8083 snprintf(buf
, sizeof(buf
), "%s", bgp
->as_pretty
);
8084 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
8088 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
8089 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
8090 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
8091 {.completions
= NULL
},
8094 struct frr_pthread
*bgp_pth_io
;
8095 struct frr_pthread
*bgp_pth_ka
;
8097 static void bgp_pthreads_init(void)
8099 assert(!bgp_pth_io
);
8100 assert(!bgp_pth_ka
);
8102 struct frr_pthread_attr io
= {
8103 .start
= frr_pthread_attr_default
.start
,
8104 .stop
= frr_pthread_attr_default
.stop
,
8106 struct frr_pthread_attr ka
= {
8107 .start
= bgp_keepalives_start
,
8108 .stop
= bgp_keepalives_stop
,
8110 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
8111 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
8114 void bgp_pthreads_run(void)
8116 frr_pthread_run(bgp_pth_io
, NULL
);
8117 frr_pthread_run(bgp_pth_ka
, NULL
);
8119 /* Wait until threads are ready. */
8120 frr_pthread_wait_running(bgp_pth_io
);
8121 frr_pthread_wait_running(bgp_pth_ka
);
8124 void bgp_pthreads_finish(void)
8126 frr_pthread_stop_all();
8129 static int peer_unshut_after_cfg(struct bgp
*bgp
)
8131 struct listnode
*node
;
8134 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
8135 if (!peer
->shut_during_cfg
)
8138 if (bgp_debug_neighbor_events(peer
))
8139 zlog_debug("%s: released from config-pending hold",
8142 peer
->shut_during_cfg
= false;
8143 if (peer_active(peer
) && peer
->status
!= Established
) {
8144 if (peer
->status
!= Idle
)
8145 BGP_EVENT_ADD(peer
, BGP_Stop
);
8146 BGP_EVENT_ADD(peer
, BGP_Start
);
8153 void bgp_init(unsigned short instance
)
8155 hook_register(bgp_config_end
, peer_unshut_after_cfg
);
8157 /* allocates some vital data structures used by peer commands in
8160 /* pre-init pthreads */
8161 bgp_pthreads_init();
8164 bgp_zebra_init(bm
->master
, instance
);
8166 #ifdef ENABLE_BGP_VNC
8167 vnc_zebra_init(bm
->master
);
8170 /* BGP VTY commands installation. */
8176 bgp_community_alias_init();
8179 bgp_route_map_init();
8180 bgp_scan_vty_init();
8182 #ifdef ENABLE_BGP_VNC
8185 bgp_ethernetvpn_init();
8186 bgp_flowspec_vty_init();
8188 /* Access list initialize. */
8190 access_list_add_hook(peer_distribute_update
);
8191 access_list_delete_hook(peer_distribute_update
);
8193 /* Filter list initialize. */
8195 as_list_add_hook(peer_aslist_add
);
8196 as_list_delete_hook(peer_aslist_del
);
8198 /* Prefix list initialize.*/
8200 prefix_list_add_hook(peer_prefix_list_update
);
8201 prefix_list_delete_hook(peer_prefix_list_update
);
8203 /* Community list initialize. */
8204 bgp_clist
= community_list_init();
8207 bgp_bfd_init(bm
->master
);
8211 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
8214 void bgp_terminate(void)
8218 struct listnode
*node
, *nnode
;
8219 struct listnode
*mnode
, *mnnode
;
8223 /* Close the listener sockets first as this prevents peers from
8225 * to reconnect on receiving the peer unconfig message. In the presence
8226 * of a large number of peers this will ensure that no peer is left with
8227 * a dangling connection
8231 /* reverse bgp_master_init */
8232 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
8233 bgp_close_vrf_socket(bgp
);
8234 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8235 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
8236 if (bgp_debug_neighbor_events(peer
))
8238 "%pBP configured Graceful-Restart, skipping unconfig notification",
8242 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
8243 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
8244 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
8248 if (bm
->listen_sockets
)
8249 list_delete(&bm
->listen_sockets
);
8251 THREAD_OFF(bm
->t_rmap_update
);
8256 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
8257 const char *ip_str
, bool use_json
)
8263 /* Get peer sockunion. */
8264 ret
= str2sockunion(ip_str
, &su
);
8266 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
8268 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
8272 json_object
*json_no
= NULL
;
8273 json_no
= json_object_new_object();
8274 json_object_string_add(
8276 "malformedAddressOrName",
8278 vty_json(vty
, json_no
);
8281 "%% Malformed address or name: %s\n",
8289 /* Peer structure lookup. */
8290 peer
= peer_lookup(bgp
, &su
);
8293 json_object
*json_no
= NULL
;
8294 json_no
= json_object_new_object();
8295 json_object_string_add(json_no
, "warning",
8296 "No such neighbor in this view/vrf");
8297 vty_json(vty
, json_no
);
8299 vty_out(vty
, "No such neighbor in this view/vrf\n");
8306 void bgp_gr_apply_running_config(void)
8308 struct peer
*peer
= NULL
;
8309 struct bgp
*bgp
= NULL
;
8310 struct listnode
*node
, *nnode
;
8311 bool gr_router_detected
= false;
8313 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
8314 zlog_debug("[BGP_GR] %s called !", __func__
);
8316 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
8317 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8318 bgp_peer_gr_flags_update(peer
);
8319 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
8320 gr_router_detected
= true;
8323 if (gr_router_detected
8324 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
8325 bgp_zebra_send_capabilities(bgp
, true);
8326 } else if (!gr_router_detected
8327 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
8328 bgp_zebra_send_capabilities(bgp
, false);
8331 gr_router_detected
= false;
8335 printfrr_ext_autoreg_p("BP", printfrr_bp
);
8336 static ssize_t
printfrr_bp(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
8339 const struct peer
*peer
= ptr
;
8342 return bputs(buf
, "(null)");
8344 return bprintfrr(buf
, "%s(%s)", peer
->host
,
8345 peer
->hostname
? peer
->hostname
: "Unknown");