1 /* BGP-4, BGP-4+ daemon program
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "sockunion.h"
38 #include "workqueue.h"
46 #include "lib/sockopt.h"
47 #include "frr_pthread.h"
50 #include "bgpd/bgpd.h"
51 #include "bgpd/bgp_table.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_route.h"
54 #include "bgpd/bgp_dump.h"
55 #include "bgpd/bgp_debug.h"
56 #include "bgpd/bgp_errors.h"
57 #include "bgpd/bgp_community.h"
58 #include "bgpd/bgp_community_alias.h"
59 #include "bgpd/bgp_conditional_adv.h"
60 #include "bgpd/bgp_attr.h"
61 #include "bgpd/bgp_regex.h"
62 #include "bgpd/bgp_clist.h"
63 #include "bgpd/bgp_fsm.h"
64 #include "bgpd/bgp_packet.h"
65 #include "bgpd/bgp_zebra.h"
66 #include "bgpd/bgp_open.h"
67 #include "bgpd/bgp_filter.h"
68 #include "bgpd/bgp_nexthop.h"
69 #include "bgpd/bgp_damp.h"
70 #include "bgpd/bgp_mplsvpn.h"
72 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
73 #include "bgpd/rfapi/rfapi_backend.h"
75 #include "bgpd/bgp_evpn.h"
76 #include "bgpd/bgp_advertise.h"
77 #include "bgpd/bgp_network.h"
78 #include "bgpd/bgp_vty.h"
79 #include "bgpd/bgp_mpath.h"
80 #include "bgpd/bgp_nht.h"
81 #include "bgpd/bgp_updgrp.h"
82 #include "bgpd/bgp_bfd.h"
83 #include "bgpd/bgp_memory.h"
84 #include "bgpd/bgp_evpn_vty.h"
85 #include "bgpd/bgp_keepalives.h"
86 #include "bgpd/bgp_io.h"
87 #include "bgpd/bgp_ecommunity.h"
88 #include "bgpd/bgp_flowspec.h"
89 #include "bgpd/bgp_labelpool.h"
90 #include "bgpd/bgp_pbr.h"
91 #include "bgpd/bgp_addpath.h"
92 #include "bgpd/bgp_evpn_private.h"
93 #include "bgpd/bgp_evpn_mh.h"
94 #include "bgpd/bgp_mac.h"
95 #include "bgp_trace.h"
97 DEFINE_MTYPE_STATIC(BGPD
, PEER_TX_SHUTDOWN_MSG
, "Peer shutdown message (TX)");
98 DEFINE_MTYPE_STATIC(BGPD
, BGP_EVPN_INFO
, "BGP EVPN instance information");
99 DEFINE_QOBJ_TYPE(bgp_master
);
100 DEFINE_QOBJ_TYPE(bgp
);
101 DEFINE_QOBJ_TYPE(peer
);
102 DEFINE_HOOK(bgp_inst_delete
, (struct bgp
*bgp
), (bgp
));
104 /* BGP process wide configuration. */
105 static struct bgp_master bgp_master
;
107 /* BGP process wide configuration pointer to export. */
108 struct bgp_master
*bm
;
110 /* BGP community-list. */
111 struct community_list_handler
*bgp_clist
;
113 unsigned int multipath_num
= MULTIPATH_NUM
;
115 /* Number of bgp instances configured for suppress fib config */
116 unsigned int bgp_suppress_fib_count
;
118 static void bgp_if_finish(struct bgp
*bgp
);
119 static void peer_drop_dynamic_neighbor(struct peer
*peer
);
121 extern struct zclient
*zclient
;
123 /* handle main socket creation or deletion */
124 static int bgp_check_main_socket(bool create
, struct bgp
*bgp
)
126 static int bgp_server_main_created
;
127 struct listnode
*node
;
131 if (bgp_server_main_created
)
133 if (list_isempty(bm
->addresses
)) {
134 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
135 return BGP_ERR_INVALID_VALUE
;
137 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
138 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
139 return BGP_ERR_INVALID_VALUE
;
141 bgp_server_main_created
= 1;
144 if (!bgp_server_main_created
)
147 bgp_server_main_created
= 0;
151 void bgp_session_reset(struct peer
*peer
)
153 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
154 && !(CHECK_FLAG(peer
->doppelganger
->flags
, PEER_FLAG_CONFIG_NODE
)))
155 peer_delete(peer
->doppelganger
);
157 BGP_EVENT_ADD(peer
, BGP_Stop
);
161 * During session reset, we may delete the doppelganger peer, which would
162 * be the next node to the current node. If the session reset was invoked
163 * during walk of peer list, we would end up accessing the freed next
164 * node. This function moves the next node along.
166 static void bgp_session_reset_safe(struct peer
*peer
, struct listnode
**nnode
)
171 n
= (nnode
) ? *nnode
: NULL
;
172 npeer
= (n
) ? listgetdata(n
) : NULL
;
174 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
175 && !(CHECK_FLAG(peer
->doppelganger
->flags
,
176 PEER_FLAG_CONFIG_NODE
))) {
177 if (peer
->doppelganger
== npeer
)
178 /* nnode and *nnode are confirmed to be non-NULL here */
179 *nnode
= (*nnode
)->next
;
180 peer_delete(peer
->doppelganger
);
183 BGP_EVENT_ADD(peer
, BGP_Stop
);
186 /* BGP global flag manipulation. */
187 int bgp_option_set(int flag
)
191 case BGP_OPT_NO_LISTEN
:
192 case BGP_OPT_NO_ZEBRA
:
193 SET_FLAG(bm
->options
, flag
);
196 return BGP_ERR_INVALID_FLAG
;
201 int bgp_option_unset(int flag
)
205 case BGP_OPT_NO_ZEBRA
:
207 UNSET_FLAG(bm
->options
, flag
);
210 return BGP_ERR_INVALID_FLAG
;
215 int bgp_option_check(int flag
)
217 return CHECK_FLAG(bm
->options
, flag
);
220 /* set the bgp no-rib option during runtime and remove installed routes */
221 void bgp_option_norib_set_runtime(void)
224 struct listnode
*node
;
228 if (bgp_option_check(BGP_OPT_NO_FIB
))
231 bgp_option_set(BGP_OPT_NO_FIB
);
233 zlog_info("Disabled BGP route installation to RIB (Zebra)");
235 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
236 FOREACH_AFI_SAFI (afi
, safi
) {
238 * Stop a crash, more work is needed
239 * here to properly add/remove these types of
242 if (!bgp_fibupd_safi(safi
))
245 bgp_zebra_withdraw_table_all_subtypes(bgp
, afi
, safi
);
249 zlog_info("All routes have been withdrawn from RIB (Zebra)");
252 /* unset the bgp no-rib option during runtime and announce routes to Zebra */
253 void bgp_option_norib_unset_runtime(void)
256 struct listnode
*node
;
260 if (!bgp_option_check(BGP_OPT_NO_FIB
))
263 bgp_option_unset(BGP_OPT_NO_FIB
);
265 zlog_info("Enabled BGP route installation to RIB (Zebra)");
267 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
268 FOREACH_AFI_SAFI (afi
, safi
) {
270 * Stop a crash, more work is needed
271 * here to properly add/remove these types
272 * of routes from zebra
274 if (!bgp_fibupd_safi(safi
))
277 bgp_zebra_announce_table_all_subtypes(bgp
, afi
, safi
);
281 zlog_info("All routes have been installed in RIB (Zebra)");
284 /* Internal function to set BGP structure configureation flag. */
285 static void bgp_config_set(struct bgp
*bgp
, int config
)
287 SET_FLAG(bgp
->config
, config
);
290 static void bgp_config_unset(struct bgp
*bgp
, int config
)
292 UNSET_FLAG(bgp
->config
, config
);
295 static int bgp_config_check(struct bgp
*bgp
, int config
)
297 return CHECK_FLAG(bgp
->config
, config
);
300 /* Set BGP router identifier; distinguish between explicit config and other
303 static int bgp_router_id_set(struct bgp
*bgp
, const struct in_addr
*id
,
307 struct listnode
*node
, *nnode
;
309 if (IPV4_ADDR_SAME(&bgp
->router_id
, id
))
312 /* EVPN uses router id in RD, withdraw them */
313 if (is_evpn_enabled())
314 bgp_evpn_handle_router_id_update(bgp
, true);
316 vpn_handle_router_id_update(bgp
, true, is_config
);
318 IPV4_ADDR_COPY(&bgp
->router_id
, id
);
320 /* Set all peer's local identifier with this value. */
321 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
322 IPV4_ADDR_COPY(&peer
->local_id
, id
);
324 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
325 peer
->last_reset
= PEER_DOWN_RID_CHANGE
;
326 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
327 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
331 /* EVPN uses router id in RD, update them */
332 if (is_evpn_enabled())
333 bgp_evpn_handle_router_id_update(bgp
, false);
335 vpn_handle_router_id_update(bgp
, false, is_config
);
340 void bgp_router_id_zebra_bump(vrf_id_t vrf_id
, const struct prefix
*router_id
)
342 struct listnode
*node
, *nnode
;
344 struct in_addr
*addr
= NULL
;
346 if (router_id
!= NULL
)
347 addr
= (struct in_addr
*)&(router_id
->u
.prefix4
);
349 if (vrf_id
== VRF_DEFAULT
) {
350 /* Router-id change for default VRF has to also update all
352 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
353 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
357 bgp
->router_id_zebra
= *addr
;
359 addr
= &bgp
->router_id_zebra
;
361 if (!bgp
->router_id_static
.s_addr
) {
362 /* Router ID is updated if there are no active
365 if (bgp
->established_peers
== 0) {
366 if (BGP_DEBUG(zebra
, ZEBRA
))
368 "RID change : vrf %s(%u), RTR ID %pI4",
372 * if old router-id was 0x0, set flag
373 * to use this new value
375 bgp_router_id_set(bgp
, addr
,
376 (bgp
->router_id
.s_addr
384 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
387 bgp
->router_id_zebra
= *addr
;
389 addr
= &bgp
->router_id_zebra
;
391 if (!bgp
->router_id_static
.s_addr
) {
392 /* Router ID is updated if there are no active
395 if (bgp
->established_peers
== 0) {
396 if (BGP_DEBUG(zebra
, ZEBRA
))
398 "RID change : vrf %s(%u), RTR ID %pI4",
402 * if old router-id was 0x0, set flag
403 * to use this new value
405 bgp_router_id_set(bgp
, addr
,
406 (bgp
->router_id
.s_addr
417 void bgp_router_id_static_set(struct bgp
*bgp
, struct in_addr id
)
419 bgp
->router_id_static
= id
;
420 bgp_router_id_set(bgp
,
421 id
.s_addr
!= INADDR_ANY
? &id
: &bgp
->router_id_zebra
,
422 true /* is config */);
425 void bm_wait_for_fib_set(bool set
)
427 bool send_msg
= false;
429 if (bm
->wait_for_fib
== set
)
432 bm
->wait_for_fib
= set
;
434 if (bgp_suppress_fib_count
== 0)
436 bgp_suppress_fib_count
++;
438 bgp_suppress_fib_count
--;
439 if (bgp_suppress_fib_count
== 0)
443 if (send_msg
&& zclient
)
444 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
448 /* Set the suppress fib pending for the bgp configuration */
449 void bgp_suppress_fib_pending_set(struct bgp
*bgp
, bool set
)
451 bool send_msg
= false;
453 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
457 SET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
458 /* Send msg to zebra for the first instance of bgp enabled
461 if (bgp_suppress_fib_count
== 0)
463 bgp_suppress_fib_count
++;
465 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
466 bgp_suppress_fib_count
--;
468 /* Send msg to zebra if there are no instances enabled
471 if (bgp_suppress_fib_count
== 0)
474 /* Send route notify request to RIB */
476 if (BGP_DEBUG(zebra
, ZEBRA
))
477 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
480 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
485 /* BGP's cluster-id control. */
486 void bgp_cluster_id_set(struct bgp
*bgp
, struct in_addr
*cluster_id
)
489 struct listnode
*node
, *nnode
;
491 if (bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
)
492 && IPV4_ADDR_SAME(&bgp
->cluster_id
, cluster_id
))
495 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
496 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
498 /* Clear all IBGP peer. */
499 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
500 if (peer
->sort
!= BGP_PEER_IBGP
)
503 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
504 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
505 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
506 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
511 void bgp_cluster_id_unset(struct bgp
*bgp
)
514 struct listnode
*node
, *nnode
;
516 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
519 bgp
->cluster_id
.s_addr
= 0;
520 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
522 /* Clear all IBGP peer. */
523 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
524 if (peer
->sort
!= BGP_PEER_IBGP
)
527 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
528 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
529 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
530 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
535 /* BGP timer configuration. */
536 void bgp_timers_set(struct bgp
*bgp
, uint32_t keepalive
, uint32_t holdtime
,
537 uint32_t connect_retry
, uint32_t delayopen
)
539 bgp
->default_keepalive
=
540 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
541 bgp
->default_holdtime
= holdtime
;
542 bgp
->default_connect_retry
= connect_retry
;
543 bgp
->default_delayopen
= delayopen
;
546 /* mostly for completeness - CLI uses its own defaults */
547 void bgp_timers_unset(struct bgp
*bgp
)
549 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
550 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
551 bgp
->default_connect_retry
= BGP_DEFAULT_CONNECT_RETRY
;
552 bgp
->default_delayopen
= BGP_DEFAULT_DELAYOPEN
;
555 void bgp_tcp_keepalive_set(struct bgp
*bgp
, uint16_t keepalive_idle
,
556 uint16_t keepalive_intvl
, uint16_t keepalive_probes
)
558 bgp
->tcp_keepalive_idle
= keepalive_idle
;
559 bgp
->tcp_keepalive_intvl
= keepalive_intvl
;
560 bgp
->tcp_keepalive_probes
= keepalive_probes
;
563 void bgp_tcp_keepalive_unset(struct bgp
*bgp
)
565 bgp
->tcp_keepalive_idle
= 0;
566 bgp
->tcp_keepalive_intvl
= 0;
567 bgp
->tcp_keepalive_probes
= 0;
570 /* BGP confederation configuration. */
571 void bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
574 struct listnode
*node
, *nnode
;
580 /* Remember - were we doing confederation before? */
581 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
583 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
585 /* If we were doing confederation already, this is just an external
586 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
587 were not doing confederation before, reset all EBGP sessions. */
588 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
589 enum bgp_peer_sort ptype
= peer_sort(peer
);
591 /* We're looking for peers who's AS is not local or part of our
593 if (already_confed
) {
594 if (ptype
== BGP_PEER_EBGP
) {
596 if (BGP_IS_VALID_STATE_FOR_NOTIF(
599 PEER_DOWN_CONFED_ID_CHANGE
;
601 peer
, BGP_NOTIFY_CEASE
,
602 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
604 bgp_session_reset_safe(peer
, &nnode
);
607 /* Not doign confederation before, so reset every
610 if (ptype
!= BGP_PEER_IBGP
) {
611 /* Reset the local_as to be our EBGP one */
612 if (ptype
== BGP_PEER_EBGP
)
614 if (BGP_IS_VALID_STATE_FOR_NOTIF(
617 PEER_DOWN_CONFED_ID_CHANGE
;
619 peer
, BGP_NOTIFY_CEASE
,
620 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
622 bgp_session_reset_safe(peer
, &nnode
);
629 void bgp_confederation_id_unset(struct bgp
*bgp
)
632 struct listnode
*node
, *nnode
;
635 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
637 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
638 /* We're looking for peers who's AS is not local */
639 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
640 peer
->local_as
= bgp
->as
;
641 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
642 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
643 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
644 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
648 bgp_session_reset_safe(peer
, &nnode
);
653 /* Is an AS part of the confed or not? */
654 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
661 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
662 if (bgp
->confed_peers
[i
] == as
)
668 /* Add an AS to the confederation set. */
669 void bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
672 struct listnode
*node
, *nnode
;
677 if (bgp_confederation_peers_check(bgp
, as
))
681 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
682 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
684 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
685 bgp
->confed_peers_cnt
++;
687 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
688 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
689 if (peer
->as
== as
) {
690 peer
->local_as
= bgp
->as
;
691 (void)peer_sort(peer
);
692 if (BGP_IS_VALID_STATE_FOR_NOTIF(
695 PEER_DOWN_CONFED_PEER_CHANGE
;
697 peer
, BGP_NOTIFY_CEASE
,
698 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
700 bgp_session_reset_safe(peer
, &nnode
);
706 /* Delete an AS from the confederation set. */
707 void bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
712 struct listnode
*node
, *nnode
;
717 if (!bgp_confederation_peers_check(bgp
, as
))
720 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
721 if (bgp
->confed_peers
[i
] == as
)
722 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
723 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
725 bgp
->confed_peers_cnt
--;
727 if (bgp
->confed_peers_cnt
== 0) {
728 if (bgp
->confed_peers
)
729 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
730 bgp
->confed_peers
= NULL
;
733 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
734 bgp
->confed_peers_cnt
* sizeof(as_t
));
736 /* Now reset any peer who's remote AS has just been removed from the
738 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
739 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
740 if (peer
->as
== as
) {
741 peer
->local_as
= bgp
->confed_id
;
742 (void)peer_sort(peer
);
743 if (BGP_IS_VALID_STATE_FOR_NOTIF(
746 PEER_DOWN_CONFED_PEER_CHANGE
;
748 peer
, BGP_NOTIFY_CEASE
,
749 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
751 bgp_session_reset_safe(peer
, &nnode
);
757 /* Local preference configuration. */
758 void bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
763 bgp
->default_local_pref
= local_pref
;
766 void bgp_default_local_preference_unset(struct bgp
*bgp
)
771 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
774 /* Local preference configuration. */
775 void bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
,
781 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
784 void bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
788 bgp
->default_subgroup_pkt_queue_max
=
789 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
792 /* Listen limit configuration. */
793 void bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
798 bgp
->dynamic_neighbors_limit
= listen_limit
;
801 void bgp_listen_limit_unset(struct bgp
*bgp
)
806 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
809 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
810 afi_t
*afi
, safi_t
*safi
)
812 /* Map from IANA values to internal values, return error if
813 * values are unrecognized.
815 *afi
= afi_iana2int(pkt_afi
);
816 *safi
= safi_iana2int(pkt_safi
);
817 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
823 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
824 iana_safi_t
*pkt_safi
)
826 /* Map from internal values to IANA values, return error if
827 * internal values are bad (unexpected).
829 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
831 *pkt_afi
= afi_int2iana(afi
);
832 *pkt_safi
= safi_int2iana(safi
);
836 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
845 afid
= afindex(afi
, safi
);
846 if (afid
>= BGP_AF_MAX
)
850 assert(peer
->peer_af_array
[afid
] == NULL
);
852 /* Allocate new peer af */
853 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
855 peer
->peer_af_array
[afid
] = af
;
860 bgp
->af_peer_count
[afi
][safi
]++;
865 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
872 afid
= afindex(afi
, safi
);
873 if (afid
>= BGP_AF_MAX
)
876 return peer
->peer_af_array
[afid
];
879 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
888 afid
= afindex(afi
, safi
);
889 if (afid
>= BGP_AF_MAX
)
892 af
= peer
->peer_af_array
[afid
];
897 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
899 bgp_stop_announce_route_timer(af
);
901 if (PAF_SUBGRP(af
)) {
902 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
903 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
904 af
->subgroup
->update_group
->id
,
905 af
->subgroup
->id
, peer
->host
);
909 update_subgroup_remove_peer(af
->subgroup
, af
);
911 if (bgp
->af_peer_count
[afi
][safi
])
912 bgp
->af_peer_count
[afi
][safi
]--;
914 peer
->peer_af_array
[afid
] = NULL
;
915 XFREE(MTYPE_BGP_PEER_AF
, af
);
919 /* Peer comparison function for sorting. */
920 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
922 if (p1
->group
&& !p2
->group
)
925 if (!p1
->group
&& p2
->group
)
928 if (p1
->group
== p2
->group
) {
929 if (p1
->conf_if
&& !p2
->conf_if
)
932 if (!p1
->conf_if
&& p2
->conf_if
)
935 if (p1
->conf_if
&& p2
->conf_if
)
936 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
938 return strcmp(p1
->group
->name
, p2
->group
->name
);
940 return sockunion_cmp(&p1
->su
, &p2
->su
);
943 static unsigned int peer_hash_key_make(const void *p
)
945 const struct peer
*peer
= p
;
946 return sockunion_hash(&peer
->su
);
949 static bool peer_hash_same(const void *p1
, const void *p2
)
951 const struct peer
*peer1
= p1
;
952 const struct peer
*peer2
= p2
;
954 return (sockunion_same(&peer1
->su
, &peer2
->su
)
955 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
956 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
959 void peer_flag_inherit(struct peer
*peer
, uint64_t flag
)
963 /* Skip if peer is not a peer-group member. */
964 if (!peer_group_active(peer
))
967 /* Unset override flag to signal inheritance from peer-group. */
968 UNSET_FLAG(peer
->flags_override
, flag
);
971 * Inherit flag state from peer-group. If the flag of the peer-group is
972 * not being inverted, the peer must inherit the inverse of the current
973 * peer-group flag state.
975 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
976 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
977 && CHECK_FLAG(peer
->flags_invert
, flag
))
978 COND_FLAG(peer
->flags
, flag
, !group_val
);
980 COND_FLAG(peer
->flags
, flag
, group_val
);
983 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
985 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
988 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
993 /* Skip if peer is not a peer-group member. */
994 if (!peer_group_active(peer
))
997 /* Unset override flag to signal inheritance from peer-group. */
998 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
1001 * Inherit flag state from peer-group. If the flag of the peer-group is
1002 * not being inverted, the peer must inherit the inverse of the current
1003 * peer-group flag state.
1005 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
1006 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
1007 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
1008 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
1010 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
1013 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
1014 static inline enum bgp_peer_sort
peer_calc_sort(struct peer
*peer
)
1021 if (peer
->change_local_as
)
1022 local_as
= peer
->change_local_as
;
1024 local_as
= peer
->local_as
;
1027 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1028 if (peer
->as_type
== AS_INTERNAL
)
1029 return BGP_PEER_IBGP
;
1031 else if (peer
->as_type
== AS_EXTERNAL
)
1032 return BGP_PEER_EBGP
;
1034 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1036 return (local_as
== peer
->as
? BGP_PEER_IBGP
1043 assert(peer
->group
);
1044 peer1
= listnode_head(peer
->group
->peer
);
1049 return BGP_PEER_INTERNAL
;
1053 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1055 return BGP_PEER_INTERNAL
;
1057 if (local_as
== peer
->as
) {
1058 if (bgp
->as
== bgp
->confed_id
) {
1059 if (local_as
== bgp
->as
)
1060 return BGP_PEER_IBGP
;
1062 return BGP_PEER_EBGP
;
1064 if (local_as
== bgp
->confed_id
)
1065 return BGP_PEER_EBGP
;
1067 return BGP_PEER_IBGP
;
1071 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1072 return BGP_PEER_CONFED
;
1074 return BGP_PEER_EBGP
;
1076 if (peer
->as_type
== AS_UNSPECIFIED
) {
1077 /* check if in peer-group with AS information */
1079 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1080 if (peer
->group
->conf
->as_type
1082 if (local_as
== peer
->group
->conf
->as
)
1083 return BGP_PEER_IBGP
;
1085 return BGP_PEER_EBGP
;
1086 } else if (peer
->group
->conf
->as_type
1088 return BGP_PEER_IBGP
;
1090 return BGP_PEER_EBGP
;
1092 /* no AS information anywhere, let caller know */
1093 return BGP_PEER_UNSPECIFIED
;
1094 } else if (peer
->as_type
!= AS_SPECIFIED
)
1095 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1098 return (local_as
== 0 ? BGP_PEER_INTERNAL
1099 : local_as
== peer
->as
? BGP_PEER_IBGP
1104 /* Calculate and cache the peer "sort" */
1105 enum bgp_peer_sort
peer_sort(struct peer
*peer
)
1107 peer
->sort
= peer_calc_sort(peer
);
1111 enum bgp_peer_sort
peer_sort_lookup(struct peer
*peer
)
1116 static void peer_free(struct peer
*peer
)
1121 assert(peer
->status
== Deleted
);
1125 /* this /ought/ to have been done already through bgp_stop earlier,
1126 * but just to be sure..
1128 bgp_timer_set(peer
);
1129 bgp_reads_off(peer
);
1130 bgp_writes_off(peer
);
1131 thread_cancel_event_ready(bm
->master
, peer
);
1132 FOREACH_AFI_SAFI (afi
, safi
)
1133 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
1134 assert(!peer
->t_write
);
1135 assert(!peer
->t_read
);
1136 BGP_EVENT_FLUSH(peer
);
1138 pthread_mutex_destroy(&peer
->io_mtx
);
1140 /* Free connected nexthop, if present */
1141 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1142 && !peer_dynamic_neighbor(peer
))
1143 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1146 FOREACH_AFI_SAFI (afi
, safi
) {
1147 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1148 XFREE(MTYPE_BGP_FILTER_NAME
,
1149 peer
->filter
[afi
][safi
].advmap
.aname
);
1150 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1151 XFREE(MTYPE_BGP_FILTER_NAME
,
1152 peer
->filter
[afi
][safi
].advmap
.cname
);
1155 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1157 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1158 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1159 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
1160 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1161 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1163 /* Update source configuration. */
1164 if (peer
->update_source
) {
1165 sockunion_free(peer
->update_source
);
1166 peer
->update_source
= NULL
;
1169 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1171 XFREE(MTYPE_BGP_NOTIFICATION
, peer
->notify
.data
);
1172 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1174 if (peer
->clear_node_queue
)
1175 work_queue_free_and_null(&peer
->clear_node_queue
);
1177 bgp_sync_delete(peer
);
1179 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1181 /* Remove BFD configuration. */
1182 if (peer
->bfd_config
)
1183 bgp_peer_remove_bfd_config(peer
);
1185 FOREACH_AFI_SAFI (afi
, safi
)
1186 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1188 if (peer
->change_local_as_pretty
)
1189 XFREE(MTYPE_BGP
, peer
->change_local_as_pretty
);
1190 if (peer
->as_pretty
)
1191 XFREE(MTYPE_BGP
, peer
->as_pretty
);
1193 bgp_unlock(peer
->bgp
);
1195 memset(peer
, 0, sizeof(struct peer
));
1197 XFREE(MTYPE_BGP_PEER
, peer
);
1200 /* increase reference count on a struct peer */
1201 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1203 frrtrace(2, frr_bgp
, bgp_peer_lock
, peer
, name
);
1204 assert(peer
&& (peer
->lock
>= 0));
1211 /* decrease reference count on a struct peer
1212 * struct peer is freed and NULL returned if last reference
1214 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1216 frrtrace(2, frr_bgp
, bgp_peer_unlock
, peer
, name
);
1217 assert(peer
&& (peer
->lock
> 0));
1221 if (peer
->lock
== 0) {
1228 /* BGP GR changes */
1230 int bgp_global_gr_init(struct bgp
*bgp
)
1232 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1233 zlog_debug("%s called ..", __func__
);
1235 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1236 /* GLOBAL_HELPER Mode */
1239 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1240 GLOBAL_GR
, GLOBAL_INVALID
,
1241 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1242 GLOBAL_DISABLE
, GLOBAL_INVALID
1244 /* GLOBAL_GR Mode */
1247 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1248 GLOBAL_GR
, GLOBAL_HELPER
,
1249 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1250 GLOBAL_DISABLE
, GLOBAL_INVALID
1252 /* GLOBAL_DISABLE Mode */
1255 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1256 GLOBAL_GR
, GLOBAL_INVALID
,
1257 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1258 GLOBAL_INVALID
, GLOBAL_HELPER
1260 /* GLOBAL_INVALID Mode */
1263 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1264 GLOBAL_INVALID
, GLOBAL_INVALID
,
1265 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1266 GLOBAL_INVALID
, GLOBAL_INVALID
1269 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1270 sizeof(local_GLOBAL_GR_FSM
));
1272 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1273 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1275 return BGP_GR_SUCCESS
;
1278 int bgp_peer_gr_init(struct peer
*peer
)
1280 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1281 zlog_debug("%s called ..", __func__
);
1283 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1284 [BGP_PEER_GR_EVENT_CMD
] = {
1286 /* PEER_HELPER Mode */
1287 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1288 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1289 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1290 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1291 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1292 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1293 bgp_peer_gr_action
}
1297 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1298 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1299 bgp_peer_gr_action
},
1300 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1301 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1302 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1303 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1306 /* PEER_DISABLE Mode */
1307 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1308 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1309 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1310 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1311 bgp_peer_gr_action
},
1312 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1313 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1316 /* PEER_INVALID Mode */
1317 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1318 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1319 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1320 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1321 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1322 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1325 /* PEER_GLOBAL_INHERIT Mode */
1326 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1327 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1328 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1329 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1330 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1331 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1334 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1335 sizeof(local_Peer_GR_FSM
));
1336 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1337 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1339 return BGP_GR_SUCCESS
;
1342 static void bgp_srv6_init(struct bgp
*bgp
)
1344 bgp
->srv6_enabled
= false;
1345 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1346 bgp
->srv6_locator_chunks
= list_new();
1347 bgp
->srv6_functions
= list_new();
1350 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1352 if (bgp
->srv6_locator_chunks
)
1353 list_delete(&bgp
->srv6_locator_chunks
);
1354 if (bgp
->srv6_functions
)
1355 list_delete(&bgp
->srv6_functions
);
1358 /* Allocate new peer object, implicitely locked. */
1359 struct peer
*peer_new(struct bgp
*bgp
)
1366 /* bgp argument is absolutely required */
1369 /* Allocate new peer. */
1370 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1372 /* Set default value. */
1374 peer
->v_start
= BGP_INIT_START_TIMER
;
1375 peer
->v_connect
= bgp
->default_connect_retry
;
1376 peer
->status
= Idle
;
1377 peer
->ostatus
= Idle
;
1378 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1379 peer
->bgp
= bgp_lock(bgp
);
1380 peer
= peer_lock(peer
); /* initial reference */
1381 peer
->local_role
= ROLE_UNDEFINED
;
1382 peer
->remote_role
= ROLE_UNDEFINED
;
1383 peer
->password
= NULL
;
1384 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1386 /* Set default flags. */
1387 FOREACH_AFI_SAFI (afi
, safi
) {
1388 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1389 SET_FLAG(peer
->af_flags
[afi
][safi
],
1390 PEER_FLAG_SEND_EXT_COMMUNITY
);
1391 SET_FLAG(peer
->af_flags
[afi
][safi
],
1392 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1394 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1395 PEER_FLAG_SEND_COMMUNITY
);
1396 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1397 PEER_FLAG_SEND_EXT_COMMUNITY
);
1398 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1399 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1400 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1401 peer
->soo
[afi
][safi
] = NULL
;
1404 /* set nexthop-unchanged for l2vpn evpn by default */
1405 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1406 PEER_FLAG_NEXTHOP_UNCHANGED
);
1408 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1410 /* Initialize per peer bgp GR FSM */
1411 bgp_peer_gr_init(peer
);
1413 /* Create buffers. */
1414 peer
->ibuf
= stream_fifo_new();
1415 peer
->obuf
= stream_fifo_new();
1416 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1418 /* We use a larger buffer for peer->obuf_work in the event that:
1419 * - We RX a BGP_UPDATE where the attributes alone are just
1420 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1421 * - The user configures an outbound route-map that does many as-path
1422 * prepends or adds many communities. At most they can have
1423 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1424 * large they can make the attributes.
1426 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1427 * bounds checking for every single attribute as we construct an
1431 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1433 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1435 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1437 bgp_sync_init(peer
);
1439 /* Get service port number. */
1440 sp
= getservbyname("bgp", "tcp");
1441 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1443 QOBJ_REG(peer
, peer
);
1448 * This function is invoked when a duplicate peer structure associated with
1449 * a neighbor is being deleted. If this about-to-be-deleted structure is
1450 * the one with all the config, then we have to copy over the info.
1452 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1454 struct peer_af
*paf
;
1462 /* The following function is used by both peer group config copy to
1463 * individual peer and when we transfer config
1465 if (peer_src
->change_local_as
)
1466 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1468 /* peer flags apply */
1469 peer_dst
->flags
= peer_src
->flags
;
1471 * The doppelganger *must* not have a config node stored
1473 UNSET_FLAG(peer_dst
->flags
, PEER_FLAG_CONFIG_NODE
);
1474 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1475 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1477 peer_dst
->local_as
= peer_src
->local_as
;
1478 peer_dst
->port
= peer_src
->port
;
1479 /* copy tcp_mss value */
1480 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1481 (void)peer_sort(peer_dst
);
1482 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1483 peer_dst
->local_role
= peer_src
->local_role
;
1485 peer_dst
->max_packet_size
= peer_src
->max_packet_size
;
1488 peer_dst
->holdtime
= peer_src
->holdtime
;
1489 peer_dst
->keepalive
= peer_src
->keepalive
;
1490 peer_dst
->connect
= peer_src
->connect
;
1491 peer_dst
->delayopen
= peer_src
->delayopen
;
1492 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1493 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1494 peer_dst
->routeadv
= peer_src
->routeadv
;
1495 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1496 peer_dst
->v_delayopen
= peer_src
->v_delayopen
;
1498 /* password apply */
1499 if (peer_src
->password
&& !peer_dst
->password
)
1500 peer_dst
->password
=
1501 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1503 FOREACH_AFI_SAFI (afi
, safi
) {
1504 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1505 peer_dst
->af_flags
[afi
][safi
] = peer_src
->af_flags
[afi
][safi
];
1506 peer_dst
->allowas_in
[afi
][safi
] =
1507 peer_src
->allowas_in
[afi
][safi
];
1508 peer_dst
->weight
[afi
][safi
] = peer_src
->weight
[afi
][safi
];
1509 peer_dst
->addpath_type
[afi
][safi
] =
1510 peer_src
->addpath_type
[afi
][safi
];
1513 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1514 paf
= peer_src
->peer_af_array
[afidx
];
1516 if (!peer_af_find(peer_dst
, paf
->afi
, paf
->safi
))
1517 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1521 /* update-source apply */
1522 if (peer_src
->update_source
) {
1523 if (peer_dst
->update_source
)
1524 sockunion_free(peer_dst
->update_source
);
1525 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1526 peer_dst
->update_source
=
1527 sockunion_dup(peer_src
->update_source
);
1528 } else if (peer_src
->update_if
) {
1529 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1530 if (peer_dst
->update_source
) {
1531 sockunion_free(peer_dst
->update_source
);
1532 peer_dst
->update_source
= NULL
;
1534 peer_dst
->update_if
=
1535 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1538 if (peer_src
->ifname
) {
1539 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1542 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1546 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1547 struct interface
*ifp
)
1549 struct connected
*ifc
;
1552 struct listnode
*node
;
1554 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1555 * IPv4 address of the other end.
1557 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1558 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1559 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
1560 if (p
.prefixlen
== 30) {
1561 peer
->su
.sa
.sa_family
= AF_INET
;
1562 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1564 peer
->su
.sin
.sin_addr
.s_addr
=
1566 else if (addr
% 4 == 2)
1567 peer
->su
.sin
.sin_addr
.s_addr
=
1569 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1570 peer
->su
.sin
.sin_len
=
1571 sizeof(struct sockaddr_in
);
1572 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1574 } else if (p
.prefixlen
== 31) {
1575 peer
->su
.sa
.sa_family
= AF_INET
;
1576 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1578 peer
->su
.sin
.sin_addr
.s_addr
=
1581 peer
->su
.sin
.sin_addr
.s_addr
=
1583 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1584 peer
->su
.sin
.sin_len
=
1585 sizeof(struct sockaddr_in
);
1586 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1588 } else if (bgp_debug_neighbor_events(peer
))
1590 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1598 static bool bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1599 struct interface
*ifp
)
1601 struct nbr_connected
*ifc_nbr
;
1603 /* Have we learnt the peer's IPv6 link-local address? */
1604 if (ifp
->nbr_connected
1605 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1606 peer
->su
.sa
.sa_family
= AF_INET6
;
1607 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1608 sizeof(struct in6_addr
));
1610 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1612 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1620 * Set or reset the peer address socketunion structure based on the
1621 * learnt/derived peer address. If the address has changed, update the
1622 * password on the listen socket, if needed.
1624 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1626 struct interface
*ifp
;
1628 int peer_addr_updated
= 0;
1629 struct listnode
*node
;
1630 union sockunion old_su
;
1633 * This function is only ever needed when FRR an interface
1634 * based peering, so this simple test will tell us if
1635 * we are in an interface based configuration or not
1642 prev_family
= peer
->su
.sa
.sa_family
;
1643 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1645 /* If BGP unnumbered is not "v6only", we first see if we can
1647 * peer's IPv4 address.
1649 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1651 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1653 /* If "v6only" or we can't derive peer's IPv4 address, see if
1655 * learnt the peer's IPv6 link-local address. This is from the
1657 * IPv6 address in router advertisement.
1659 if (!peer_addr_updated
)
1661 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1663 /* If we could derive the peer address, we may need to install the
1665 * configured for the peer, if any, on the listen socket. Otherwise,
1667 * that peer's address is not available and uninstall the password, if
1670 if (peer_addr_updated
) {
1671 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1672 && prev_family
== AF_UNSPEC
)
1675 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1676 && prev_family
!= AF_UNSPEC
)
1677 bgp_md5_unset(peer
);
1678 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1679 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1683 * If they are the same, nothing to do here, move along
1685 if (!sockunion_same(&old_su
, &peer
->su
)) {
1686 union sockunion new_su
= peer
->su
;
1687 struct bgp
*bgp
= peer
->bgp
;
1690 * Our peer structure is stored in the bgp->peerhash
1691 * release it before we modify anything in both the
1692 * hash and the list. But *only* if the peer
1693 * is in the bgp->peerhash as that on deletion
1694 * we call bgp_stop which calls this function :(
1695 * so on deletion let's remove from the list first
1696 * and then do the deletion preventing this from
1697 * being added back on the list below when we
1698 * fail to remove it up here.
1702 * listnode_lookup just scans the list
1703 * for the peer structure so it's safe
1704 * to use without modifying the su
1706 node
= listnode_lookup(bgp
->peer
, peer
);
1709 * Let's reset the peer->su release and
1710 * reset it and put it back. We have to
1711 * do this because hash_release will
1712 * scan through looking for a matching
1716 hash_release(peer
->bgp
->peerhash
, peer
);
1717 listnode_delete(peer
->bgp
->peer
, peer
);
1720 (void)hash_get(peer
->bgp
->peerhash
, peer
,
1722 listnode_add_sort(peer
->bgp
->peer
, peer
);
1727 void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1729 struct bgp_dest
*dest
, *ndest
;
1730 struct bgp_table
*table
;
1732 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1733 dest
= bgp_route_next(dest
)) {
1734 table
= bgp_dest_get_bgp_table_info(dest
);
1735 if (table
!= NULL
) {
1736 /* Special handling for 2-level routing
1738 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1739 || safi
== SAFI_EVPN
) {
1740 for (ndest
= bgp_table_top(table
); ndest
;
1741 ndest
= bgp_route_next(ndest
))
1742 bgp_process(bgp
, ndest
, afi
, safi
);
1744 bgp_process(bgp
, dest
, afi
, safi
);
1749 /* Force a bestpath recalculation for all prefixes. This is used
1750 * when 'bgp bestpath' commands are entered.
1752 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1757 FOREACH_AFI_SAFI (afi
, safi
) {
1758 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1763 * Create new BGP peer.
1765 * conf_if and su are mutually exclusive if configuring from the cli.
1766 * If we are handing a doppelganger, then we *must* pass in both
1767 * the original peer's su and conf_if, so that we can appropriately
1768 * track the bgp->peerhash( ie we don't want to remove the current
1769 * one from the config ).
1771 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1772 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1773 int as_type
, struct peer_group
*group
,
1774 bool config_node
, const char *as_str
)
1778 char buf
[SU_ADDRSTRLEN
];
1782 peer
= peer_new(bgp
);
1784 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1788 bgp_peer_conf_if_to_su_update(peer
);
1789 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1790 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1793 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1794 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1795 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1797 peer
->local_as
= local_as
;
1798 peer
->as
= remote_as
;
1799 /* internal and external values do not use as_pretty */
1800 if (as_str
&& asn_str2asn(as_str
, NULL
))
1801 peer
->as_pretty
= XSTRDUP(MTYPE_BGP
, as_str
);
1802 peer
->as_type
= as_type
;
1803 peer
->local_id
= bgp
->router_id
;
1804 peer
->v_holdtime
= bgp
->default_holdtime
;
1805 peer
->v_keepalive
= bgp
->default_keepalive
;
1806 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1807 ? BGP_DEFAULT_IBGP_ROUTEADV
1808 : BGP_DEFAULT_EBGP_ROUTEADV
;
1809 if (bgp_config_inprocess())
1810 peer
->shut_during_cfg
= true;
1812 peer
= peer_lock(peer
); /* bgp peer list reference */
1813 peer
->group
= group
;
1814 listnode_add_sort(bgp
->peer
, peer
);
1817 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1819 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1821 /* Adjust update-group coalesce timer heuristics for # peers. */
1822 if (bgp
->heuristic_coalesce
) {
1823 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1825 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1826 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1829 active
= peer_active(peer
);
1831 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1832 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1834 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1837 /* Last read and reset time set */
1838 peer
->readtime
= peer
->resettime
= monotime(NULL
);
1840 /* Default TTL set. */
1841 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1843 /* Default configured keepalives count for shutdown rtt command */
1844 peer
->rtt_keepalive_conf
= 1;
1846 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1847 * neighbor for the corresponding address family. IPv4 Unicast is
1848 * the only address family enabled by default without expliict
1851 FOREACH_AFI_SAFI (afi
, safi
) {
1852 if (bgp
->default_af
[afi
][safi
]) {
1853 peer
->afc
[afi
][safi
] = 1;
1854 peer_af_create(peer
, afi
, safi
);
1858 /* auto shutdown if configured */
1859 if (bgp
->autoshutdown
)
1860 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1861 /* Set up peer's events and timers. */
1862 else if (!active
&& peer_active(peer
))
1863 bgp_timer_set(peer
);
1865 bgp_peer_gr_flags_update(peer
);
1866 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1871 /* Make accept BGP peer. This function is only called from the test code */
1872 struct peer
*peer_create_accept(struct bgp
*bgp
)
1876 peer
= peer_new(bgp
);
1878 peer
= peer_lock(peer
); /* bgp peer list reference */
1879 listnode_add_sort(bgp
->peer
, peer
);
1880 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1886 * Return true if we have a peer configured to use this afi/safi
1888 bool bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1890 struct listnode
*node
;
1893 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1894 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1897 if (peer
->afc
[afi
][safi
])
1904 /* Change peer's AS number. */
1905 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
,
1908 enum bgp_peer_sort origtype
, newtype
;
1911 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1912 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1913 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1914 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1915 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1917 bgp_session_reset(peer
);
1919 origtype
= peer_sort_lookup(peer
);
1921 if (as_specified
== AS_SPECIFIED
&& as_str
) {
1922 if (peer
->as_pretty
)
1923 XFREE(MTYPE_BGP
, peer
->as_pretty
);
1924 peer
->as_pretty
= XSTRDUP(MTYPE_BGP
, as_str
);
1925 } else if (peer
->as_type
== AS_UNSPECIFIED
&& peer
->as_pretty
)
1926 XFREE(MTYPE_BGP
, peer
->as_pretty
);
1927 peer
->as_type
= as_specified
;
1929 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1930 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1931 && peer
->bgp
->as
!= as
)
1932 peer
->local_as
= peer
->bgp
->confed_id
;
1934 peer
->local_as
= peer
->bgp
->as
;
1936 newtype
= peer_sort(peer
);
1937 /* Advertisement-interval reset */
1938 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1939 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1940 ? BGP_DEFAULT_IBGP_ROUTEADV
1941 : BGP_DEFAULT_EBGP_ROUTEADV
;
1945 if (newtype
== BGP_PEER_IBGP
)
1947 else if (origtype
== BGP_PEER_IBGP
)
1948 peer
->ttl
= BGP_DEFAULT_TTL
;
1950 /* reflector-client reset */
1951 if (newtype
!= BGP_PEER_IBGP
) {
1952 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1953 PEER_FLAG_REFLECTOR_CLIENT
);
1954 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1955 PEER_FLAG_REFLECTOR_CLIENT
);
1956 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1957 PEER_FLAG_REFLECTOR_CLIENT
);
1958 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1959 PEER_FLAG_REFLECTOR_CLIENT
);
1960 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1961 PEER_FLAG_REFLECTOR_CLIENT
);
1962 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1963 PEER_FLAG_REFLECTOR_CLIENT
);
1964 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1965 PEER_FLAG_REFLECTOR_CLIENT
);
1966 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1967 PEER_FLAG_REFLECTOR_CLIENT
);
1968 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1969 PEER_FLAG_REFLECTOR_CLIENT
);
1970 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1971 PEER_FLAG_REFLECTOR_CLIENT
);
1972 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1973 PEER_FLAG_REFLECTOR_CLIENT
);
1974 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1975 PEER_FLAG_REFLECTOR_CLIENT
);
1976 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1977 PEER_FLAG_REFLECTOR_CLIENT
);
1981 /* If peer does not exist, create new one. If peer already exists,
1982 set AS number to the peer. */
1983 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1984 as_t
*as
, int as_type
, const char *as_str
)
1990 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1992 peer
= peer_lookup(bgp
, su
);
1995 /* Not allowed for a dynamic peer. */
1996 if (peer_dynamic_neighbor(peer
)) {
1998 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
2001 /* When this peer is a member of peer-group. */
2003 /* peer-group already has AS number/internal/external */
2004 if (peer
->group
->conf
->as
2005 || peer
->group
->conf
->as_type
) {
2006 /* Return peer group's AS number. */
2007 *as
= peer
->group
->conf
->as
;
2008 return BGP_ERR_PEER_GROUP_MEMBER
;
2011 enum bgp_peer_sort peer_sort_type
=
2012 peer_sort(peer
->group
->conf
);
2014 /* Explicit AS numbers used, compare AS numbers */
2015 if (as_type
== AS_SPECIFIED
) {
2016 if (((peer_sort_type
== BGP_PEER_IBGP
)
2017 && (bgp
->as
!= *as
))
2018 || ((peer_sort_type
== BGP_PEER_EBGP
)
2019 && (bgp
->as
== *as
))) {
2021 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2024 /* internal/external used, compare as-types */
2025 if (((peer_sort_type
== BGP_PEER_IBGP
)
2026 && (as_type
!= AS_INTERNAL
))
2027 || ((peer_sort_type
== BGP_PEER_EBGP
)
2028 && (as_type
!= AS_EXTERNAL
))) {
2030 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2035 /* Existing peer's AS number change. */
2036 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2037 || (peer
->as_type
!= as_type
))
2038 peer_as_change(peer
, *as
, as_type
, as_str
);
2041 return BGP_ERR_NO_INTERFACE_CONFIG
;
2043 /* If the peer is not part of our confederation, and its not an
2044 iBGP peer then spoof the source AS */
2045 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
2046 && !bgp_confederation_peers_check(bgp
, *as
)
2048 local_as
= bgp
->confed_id
;
2052 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
,
2059 const char *bgp_get_name_by_role(uint8_t role
)
2064 case ROLE_RS_SERVER
:
2066 case ROLE_RS_CLIENT
:
2072 case ROLE_UNDEFINED
:
2078 enum asnotation_mode
bgp_get_asnotation(struct bgp
*bgp
)
2081 return ASNOTATION_PLAIN
;
2082 return bgp
->asnotation
;
2085 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
2086 struct peer
*peer
, afi_t afi
,
2090 int out
= FILTER_OUT
;
2092 uint64_t pflags_ovrd
;
2093 uint8_t *pfilter_ovrd
;
2097 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2098 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2100 /* peer af_flags apply */
2101 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2102 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2103 ^ peer
->af_flags_invert
[afi
][safi
];
2104 flags_tmp
&= ~pflags_ovrd
;
2106 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2107 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2108 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2109 conf
->af_flags_invert
[afi
][safi
]);
2111 /* maximum-prefix */
2112 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2113 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2114 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2115 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2118 /* maximum-prefix-out */
2119 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX_OUT
))
2120 PEER_ATTR_INHERIT(peer
, group
, pmax_out
[afi
][safi
]);
2123 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2124 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2127 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_SOO
))
2128 PEER_ATTR_INHERIT(peer
, group
, soo
[afi
][safi
]);
2131 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2132 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2134 /* default-originate route-map */
2135 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2136 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2137 MTYPE_ROUTE_MAP_NAME
);
2138 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2141 /* inbound filter apply */
2142 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2143 PEER_STR_ATTR_INHERIT(peer
, group
,
2144 filter
[afi
][safi
].dlist
[in
].name
,
2145 MTYPE_BGP_FILTER_NAME
);
2146 PEER_ATTR_INHERIT(peer
, group
,
2147 filter
[afi
][safi
].dlist
[in
].alist
);
2150 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2151 PEER_STR_ATTR_INHERIT(peer
, group
,
2152 filter
[afi
][safi
].plist
[in
].name
,
2153 MTYPE_BGP_FILTER_NAME
);
2154 PEER_ATTR_INHERIT(peer
, group
,
2155 filter
[afi
][safi
].plist
[in
].plist
);
2158 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2159 PEER_STR_ATTR_INHERIT(peer
, group
,
2160 filter
[afi
][safi
].aslist
[in
].name
,
2161 MTYPE_BGP_FILTER_NAME
);
2162 PEER_ATTR_INHERIT(peer
, group
,
2163 filter
[afi
][safi
].aslist
[in
].aslist
);
2166 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2167 PEER_STR_ATTR_INHERIT(peer
, group
,
2168 filter
[afi
][safi
].map
[in
].name
,
2169 MTYPE_BGP_FILTER_NAME
);
2170 PEER_ATTR_INHERIT(peer
, group
,
2171 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2174 /* outbound filter apply */
2175 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2176 PEER_STR_ATTR_INHERIT(peer
, group
,
2177 filter
[afi
][safi
].dlist
[out
].name
,
2178 MTYPE_BGP_FILTER_NAME
);
2179 PEER_ATTR_INHERIT(peer
, group
,
2180 filter
[afi
][safi
].dlist
[out
].alist
);
2183 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2184 PEER_STR_ATTR_INHERIT(peer
, group
,
2185 filter
[afi
][safi
].plist
[out
].name
,
2186 MTYPE_BGP_FILTER_NAME
);
2187 PEER_ATTR_INHERIT(peer
, group
,
2188 filter
[afi
][safi
].plist
[out
].plist
);
2191 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2192 PEER_STR_ATTR_INHERIT(peer
, group
,
2193 filter
[afi
][safi
].aslist
[out
].name
,
2194 MTYPE_BGP_FILTER_NAME
);
2195 PEER_ATTR_INHERIT(peer
, group
,
2196 filter
[afi
][safi
].aslist
[out
].aslist
);
2199 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2200 PEER_STR_ATTR_INHERIT(peer
, group
,
2201 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2202 MTYPE_BGP_FILTER_NAME
);
2203 PEER_ATTR_INHERIT(peer
, group
,
2204 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2207 /* nondirectional filter apply */
2208 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2209 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2210 MTYPE_BGP_FILTER_NAME
);
2211 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2214 /* Conditional Advertisements */
2215 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ADVERTISE_MAP
)) {
2216 PEER_STR_ATTR_INHERIT(peer
, group
,
2217 filter
[afi
][safi
].advmap
.aname
,
2218 MTYPE_BGP_FILTER_NAME
);
2219 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.amap
);
2220 PEER_STR_ATTR_INHERIT(peer
, group
,
2221 filter
[afi
][safi
].advmap
.cname
,
2222 MTYPE_BGP_FILTER_NAME
);
2223 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.cmap
);
2224 PEER_ATTR_INHERIT(peer
, group
,
2225 filter
[afi
][safi
].advmap
.condition
);
2228 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2229 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2230 bgp_addpath_type_changed(conf
->bgp
);
2234 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2239 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2240 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2241 __func__
, peer
->host
);
2245 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2247 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2248 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2249 return BGP_ERR_PEER_SAFI_CONFLICT
;
2251 /* Nothing to do if we've already activated this peer */
2252 if (peer
->afc
[afi
][safi
])
2255 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2258 active
= peer_active(peer
);
2259 peer
->afc
[afi
][safi
] = 1;
2262 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2264 if (!active
&& peer_active(peer
)) {
2265 bgp_timer_set(peer
);
2267 if (peer_established(peer
)) {
2268 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2269 peer
->afc_adv
[afi
][safi
] = 1;
2270 bgp_capability_send(peer
, afi
, safi
,
2272 CAPABILITY_ACTION_SET
);
2273 if (peer
->afc_recv
[afi
][safi
]) {
2274 peer
->afc_nego
[afi
][safi
] = 1;
2275 bgp_announce_route(peer
, afi
, safi
,
2279 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2280 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2281 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2284 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2285 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2286 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2287 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2290 * If we are turning on a AFI/SAFI locally and we've
2291 * started bringing a peer up, we need to tell
2292 * the other peer to restart because we might loose
2293 * configuration here because when the doppelganger
2294 * gets to a established state due to how
2295 * we resolve we could just overwrite the afi/safi
2298 other
= peer
->doppelganger
;
2300 && (other
->status
== OpenSent
2301 || other
->status
== OpenConfirm
)) {
2302 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2303 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2304 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2311 /* Activate the peer or peer group for specified AFI and SAFI. */
2312 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2315 struct peer_group
*group
;
2316 struct listnode
*node
, *nnode
;
2317 struct peer
*tmp_peer
;
2320 /* Nothing to do if we've already activated this peer */
2321 if (peer
->afc
[afi
][safi
])
2326 /* This is a peer-group so activate all of the members of the
2327 * peer-group as well */
2328 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2330 /* Do not activate a peer for both SAFI_UNICAST and
2331 * SAFI_LABELED_UNICAST */
2332 if ((safi
== SAFI_UNICAST
2333 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2334 || (safi
== SAFI_LABELED_UNICAST
2335 && peer
->afc
[afi
][SAFI_UNICAST
]))
2336 return BGP_ERR_PEER_SAFI_CONFLICT
;
2338 peer
->afc
[afi
][safi
] = 1;
2339 group
= peer
->group
;
2341 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2342 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2345 ret
|= peer_activate_af(peer
, afi
, safi
);
2348 /* If this is the first peer to be activated for this
2349 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2350 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2351 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2353 if (BGP_DEBUG(zebra
, ZEBRA
))
2355 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2357 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2358 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2361 if (safi
== SAFI_FLOWSPEC
) {
2362 /* connect to table manager */
2363 bgp_zebra_init_tm_connect(bgp
);
2368 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2371 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2372 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2373 __func__
, peer
->host
);
2377 /* Nothing to do if we've already deactivated this peer */
2378 if (!peer
->afc
[afi
][safi
])
2381 /* De-activate the address family configuration. */
2382 peer
->afc
[afi
][safi
] = 0;
2384 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2385 flog_err(EC_BGP_PEER_DELETE
,
2386 "couldn't delete af structure for peer %s(%s, %s)",
2387 peer
->host
, afi2str(afi
), safi2str(safi
));
2391 if (peer_established(peer
)) {
2392 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2393 peer
->afc_adv
[afi
][safi
] = 0;
2394 peer
->afc_nego
[afi
][safi
] = 0;
2396 if (peer_active_nego(peer
)) {
2397 bgp_capability_send(peer
, afi
, safi
,
2399 CAPABILITY_ACTION_UNSET
);
2400 bgp_clear_route(peer
, afi
, safi
);
2401 peer
->pcount
[afi
][safi
] = 0;
2403 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2404 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2405 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2408 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2409 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2410 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2417 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2420 struct peer_group
*group
;
2421 struct peer
*tmp_peer
;
2422 struct listnode
*node
, *nnode
;
2425 /* Nothing to do if we've already de-activated this peer */
2426 if (!peer
->afc
[afi
][safi
])
2429 /* This is a peer-group so de-activate all of the members of the
2430 * peer-group as well */
2431 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2432 peer
->afc
[afi
][safi
] = 0;
2433 group
= peer
->group
;
2435 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2436 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2439 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2444 /* If this is the last peer to be deactivated for this
2445 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2446 if (safi
== SAFI_LABELED_UNICAST
2447 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2448 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2450 if (BGP_DEBUG(zebra
, ZEBRA
))
2452 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2454 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2455 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2460 void peer_nsf_stop(struct peer
*peer
)
2465 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2466 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2468 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2469 peer
->nsf
[afi
][safi
] = 0;
2470 THREAD_OFF(peer
->t_llgr_stale
[afi
][safi
]);
2473 if (peer
->t_gr_restart
) {
2474 THREAD_OFF(peer
->t_gr_restart
);
2475 if (bgp_debug_neighbor_events(peer
))
2476 zlog_debug("%pBP graceful restart timer stopped", peer
);
2478 if (peer
->t_gr_stale
) {
2479 THREAD_OFF(peer
->t_gr_stale
);
2480 if (bgp_debug_neighbor_events(peer
))
2482 "%pBP graceful restart stalepath timer stopped",
2485 bgp_clear_route_all(peer
);
2488 /* Delete peer from confguration.
2490 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2491 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2493 * This function /should/ take care to be idempotent, to guard against
2494 * it being called multiple times through stray events that come in
2495 * that happen to result in this function being called again. That
2496 * said, getting here for a "Deleted" peer is a bug in the neighbour
2499 int peer_delete(struct peer
*peer
)
2505 struct bgp_filter
*filter
;
2506 struct listnode
*pn
;
2509 assert(peer
->status
!= Deleted
);
2512 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2514 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2516 bgp_keepalives_off(peer
);
2517 bgp_reads_off(peer
);
2518 bgp_writes_off(peer
);
2519 thread_cancel_event_ready(bm
->master
, peer
);
2520 FOREACH_AFI_SAFI (afi
, safi
)
2521 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
2522 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2523 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2524 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2526 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2527 peer_nsf_stop(peer
);
2529 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2531 /* Remove BFD settings. */
2532 if (peer
->bfd_config
)
2533 bgp_peer_remove_bfd_config(peer
);
2535 /* If this peer belongs to peer group, clear up the
2538 if (peer_dynamic_neighbor(peer
))
2539 peer_drop_dynamic_neighbor(peer
);
2541 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2543 peer
); /* group->peer list reference */
2544 list_delete_node(peer
->group
->peer
, pn
);
2549 /* Withdraw all information from routing table. We can not use
2550 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2551 * executed after peer structure is deleted.
2553 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2555 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2557 if (peer
->doppelganger
) {
2558 peer
->doppelganger
->doppelganger
= NULL
;
2559 peer
->doppelganger
= NULL
;
2562 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2563 bgp_fsm_change_status(peer
, Deleted
);
2565 /* Remove from NHT */
2566 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2567 bgp_unlink_nexthop_by_peer(peer
);
2569 /* Password configuration */
2570 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2571 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2572 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2573 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2574 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2575 bgp_md5_unset(peer
);
2578 bgp_timer_set(peer
); /* stops all timers for Deleted */
2580 /* Delete from all peer list. */
2581 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2582 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2584 * Removing from the list node first because
2585 * peer_unlock *can* call peer_delete( I know,
2586 * I know ). So let's remove it and in
2587 * the su recalculate function we'll ensure
2588 * it's in there or not.
2590 list_delete_node(bgp
->peer
, pn
);
2591 hash_release(bgp
->peerhash
, peer
);
2592 peer_unlock(peer
); /* bgp peer list reference */
2597 stream_fifo_free(peer
->ibuf
);
2602 stream_fifo_free(peer
->obuf
);
2606 if (peer
->ibuf_work
) {
2607 ringbuf_del(peer
->ibuf_work
);
2608 peer
->ibuf_work
= NULL
;
2611 if (peer
->obuf_work
) {
2612 stream_free(peer
->obuf_work
);
2613 peer
->obuf_work
= NULL
;
2616 if (peer
->scratch
) {
2617 stream_free(peer
->scratch
);
2618 peer
->scratch
= NULL
;
2621 /* Local and remote addresses. */
2622 if (peer
->su_local
) {
2623 sockunion_free(peer
->su_local
);
2624 peer
->su_local
= NULL
;
2627 if (peer
->su_remote
) {
2628 sockunion_free(peer
->su_remote
);
2629 peer
->su_remote
= NULL
;
2632 /* Free filter related memory. */
2633 FOREACH_AFI_SAFI (afi
, safi
) {
2634 filter
= &peer
->filter
[afi
][safi
];
2636 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2637 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2638 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2639 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2642 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2643 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2646 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2647 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2648 ecommunity_free(&peer
->soo
[afi
][safi
]);
2651 FOREACH_AFI_SAFI (afi
, safi
)
2652 peer_af_delete(peer
, afi
, safi
);
2654 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2655 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2657 peer_unlock(peer
); /* initial reference */
2662 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2664 return strcmp(g1
->name
, g2
->name
);
2667 /* Peer group cofiguration. */
2668 static struct peer_group
*peer_group_new(void)
2670 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2673 static void peer_group_free(struct peer_group
*group
)
2675 XFREE(MTYPE_PEER_GROUP
, group
);
2678 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2680 struct peer_group
*group
;
2681 struct listnode
*node
, *nnode
;
2683 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2684 if (strcmp(group
->name
, name
) == 0)
2690 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2692 struct peer_group
*group
;
2696 group
= peer_group_lookup(bgp
, name
);
2700 group
= peer_group_new();
2702 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2703 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2704 group
->peer
= list_new();
2705 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2706 group
->listen_range
[afi
] = list_new();
2707 group
->conf
= peer_new(bgp
);
2708 FOREACH_AFI_SAFI (afi
, safi
) {
2709 if (bgp
->default_af
[afi
][safi
])
2710 group
->conf
->afc
[afi
][safi
] = 1;
2712 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2713 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2714 group
->conf
->group
= group
;
2715 group
->conf
->as
= 0;
2716 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2717 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2718 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2719 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2720 listnode_add_sort(bgp
->group
, group
);
2725 static void peer_group2peer_config_copy(struct peer_group
*group
,
2730 bool config_node
= !!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2736 peer
->as
= conf
->as
;
2739 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2740 peer
->change_local_as
= conf
->change_local_as
;
2742 /* If peer-group has configured TTL then override it */
2743 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2744 peer
->ttl
= conf
->ttl
;
2747 peer
->gtsm_hops
= conf
->gtsm_hops
;
2749 /* peer flags apply */
2750 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2751 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2752 flags_tmp
&= ~peer
->flags_override
;
2754 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2755 SET_FLAG(peer
->flags
, flags_tmp
);
2756 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2759 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2761 /* peer timers apply */
2762 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2763 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2764 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2767 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2768 PEER_ATTR_INHERIT(peer
, group
, connect
);
2769 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2770 peer
->v_connect
= conf
->connect
;
2772 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2775 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2776 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2777 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2778 peer
->v_delayopen
= conf
->delayopen
;
2780 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2783 /* advertisement-interval apply */
2784 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2785 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2786 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2787 peer
->v_routeadv
= conf
->routeadv
;
2789 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2790 ? BGP_DEFAULT_IBGP_ROUTEADV
2791 : BGP_DEFAULT_EBGP_ROUTEADV
;
2794 /* capability extended-nexthop apply */
2795 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2796 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2797 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2799 /* password apply */
2800 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2801 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2802 MTYPE_PEER_PASSWORD
);
2804 if (!BGP_PEER_SU_UNSPEC(peer
))
2807 /* update-source apply */
2808 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2809 if (conf
->update_source
) {
2810 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2811 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2812 } else if (conf
->update_if
) {
2813 sockunion_free(peer
->update_source
);
2814 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2815 MTYPE_PEER_UPDATE_SOURCE
);
2820 PEER_ATTR_INHERIT(peer
, group
, local_role
);
2822 /* Update GR flags for the peer. */
2823 bgp_peer_gr_flags_update(peer
);
2825 /* Apply BFD settings from group to peer if it exists. */
2826 if (conf
->bfd_config
) {
2827 bgp_peer_configure_bfd(peer
, false);
2828 bgp_peer_config_apply(peer
, group
);
2832 /* Peer group's remote AS configuration. */
2833 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2834 int as_type
, const char *as_str
)
2836 struct peer_group
*group
;
2838 struct listnode
*node
, *nnode
;
2840 group
= peer_group_lookup(bgp
, group_name
);
2844 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2848 /* When we setup peer-group AS number all peer group member's AS
2849 number must be updated to same number. */
2850 peer_as_change(group
->conf
, *as
, as_type
, as_str
);
2852 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2853 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2854 || (peer
->as_type
!= as_type
))
2855 peer_as_change(peer
, *as
, as_type
, as_str
);
2861 void peer_notify_unconfig(struct peer
*peer
)
2863 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2864 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2865 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2868 static void peer_notify_shutdown(struct peer
*peer
)
2870 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
2871 if (bgp_debug_neighbor_events(peer
))
2873 "%pBP configured Graceful-Restart, skipping shutdown notification",
2878 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2879 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2880 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
2883 void peer_group_notify_unconfig(struct peer_group
*group
)
2885 struct peer
*peer
, *other
;
2886 struct listnode
*node
, *nnode
;
2888 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2889 other
= peer
->doppelganger
;
2890 if (other
&& other
->status
!= Deleted
) {
2891 other
->group
= NULL
;
2892 peer_notify_unconfig(other
);
2894 peer_notify_unconfig(peer
);
2898 int peer_group_delete(struct peer_group
*group
)
2902 struct prefix
*prefix
;
2904 struct listnode
*node
, *nnode
;
2909 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2910 other
= peer
->doppelganger
;
2912 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2913 bgp_zebra_terminate_radv(bgp
, peer
);
2916 if (other
&& other
->status
!= Deleted
) {
2917 other
->group
= NULL
;
2921 list_delete(&group
->peer
);
2923 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2924 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2926 prefix_free(&prefix
);
2928 list_delete(&group
->listen_range
[afi
]);
2931 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2934 if (group
->conf
->bfd_config
)
2935 bgp_peer_remove_bfd_config(group
->conf
);
2937 group
->conf
->group
= NULL
;
2938 peer_delete(group
->conf
);
2940 /* Delete from all peer_group list. */
2941 listnode_delete(bgp
->group
, group
);
2943 peer_group_free(group
);
2948 int peer_group_remote_as_delete(struct peer_group
*group
)
2950 struct peer
*peer
, *other
;
2951 struct listnode
*node
, *nnode
;
2953 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2954 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2957 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2958 other
= peer
->doppelganger
;
2960 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2961 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2965 if (other
&& other
->status
!= Deleted
) {
2966 other
->group
= NULL
;
2970 list_delete_all_node(group
->peer
);
2972 group
->conf
->as
= 0;
2973 group
->conf
->as_type
= AS_UNSPECIFIED
;
2978 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2980 struct prefix
*prefix
;
2981 struct listnode
*node
, *nnode
;
2984 afi
= family2afi(range
->family
);
2986 /* Group needs remote AS configured. */
2987 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2988 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2990 /* Ensure no duplicates. Currently we don't care about overlaps. */
2991 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2992 if (prefix_same(range
, prefix
))
2996 prefix
= prefix_new();
2997 prefix_copy(prefix
, range
);
2998 listnode_add(group
->listen_range
[afi
], prefix
);
3000 /* Update passwords for new ranges */
3001 if (group
->conf
->password
)
3002 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
3007 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
3009 struct prefix
*prefix
, prefix2
;
3010 struct listnode
*node
, *nnode
;
3014 afi
= family2afi(range
->family
);
3016 /* Identify the listen range. */
3017 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
3018 if (prefix_same(range
, prefix
))
3023 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
3025 /* Dispose off any dynamic neighbors that exist due to this listen range
3027 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
3028 if (!peer_dynamic_neighbor(peer
))
3031 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
3032 && prefix_match(prefix
, &prefix2
)) {
3033 if (bgp_debug_neighbor_events(peer
))
3035 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
3036 peer
->host
, group
->name
, prefix
);
3041 /* Get rid of the listen range */
3042 listnode_delete(group
->listen_range
[afi
], prefix
);
3044 /* Remove passwords for deleted ranges */
3045 if (group
->conf
->password
)
3046 bgp_md5_unset_prefix(group
->bgp
, prefix
);
3051 /* Bind specified peer to peer group. */
3052 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
3053 struct peer_group
*group
, as_t
*as
)
3055 int first_member
= 0;
3058 enum bgp_peer_sort ptype
, gtype
;
3060 /* Lookup the peer. */
3062 peer
= peer_lookup(bgp
, su
);
3064 /* The peer exist, bind it to the peer-group */
3066 /* When the peer already belongs to a peer-group, check the
3068 if (peer_group_active(peer
)) {
3070 /* The peer is already bound to the peer-group,
3073 if (strcmp(peer
->group
->name
, group
->name
) == 0)
3076 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
3079 /* The peer has not specified a remote-as, inherit it from the
3081 if (peer
->as_type
== AS_UNSPECIFIED
) {
3082 peer
->as_type
= group
->conf
->as_type
;
3083 peer
->as
= group
->conf
->as
;
3084 peer
->sort
= group
->conf
->sort
;
3087 ptype
= peer_sort(peer
);
3088 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
3089 gtype
= peer_sort(group
->conf
);
3090 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
3093 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
3096 if (gtype
== BGP_PEER_INTERNAL
)
3100 peer_group2peer_config_copy(group
, peer
);
3102 FOREACH_AFI_SAFI (afi
, safi
) {
3103 if (group
->conf
->afc
[afi
][safi
]) {
3104 peer
->afc
[afi
][safi
] = 1;
3106 if (peer_af_find(peer
, afi
, safi
)
3107 || peer_af_create(peer
, afi
, safi
)) {
3108 peer_group2peer_config_copy_af(
3109 group
, peer
, afi
, safi
);
3111 } else if (peer
->afc
[afi
][safi
])
3112 peer_deactivate(peer
, afi
, safi
);
3116 assert(group
&& peer
->group
== group
);
3118 listnode_delete(bgp
->peer
, peer
);
3120 peer
->group
= group
;
3121 listnode_add_sort(bgp
->peer
, peer
);
3123 peer
= peer_lock(peer
); /* group->peer list reference */
3124 listnode_add(group
->peer
, peer
);
3128 gtype
= peer_sort(group
->conf
);
3129 /* Advertisement-interval reset */
3130 if (!CHECK_FLAG(group
->conf
->flags
,
3131 PEER_FLAG_ROUTEADV
)) {
3132 group
->conf
->v_routeadv
=
3133 (gtype
== BGP_PEER_IBGP
)
3134 ? BGP_DEFAULT_IBGP_ROUTEADV
3135 : BGP_DEFAULT_EBGP_ROUTEADV
;
3138 /* ebgp-multihop reset */
3139 if (gtype
== BGP_PEER_IBGP
)
3140 group
->conf
->ttl
= MAXTTL
;
3143 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3145 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3146 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3147 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3148 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3150 bgp_session_reset(peer
);
3154 /* Create a new peer. */
3156 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3157 && (!group
->conf
->as
)) {
3158 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3161 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3162 group
->conf
->as_type
, group
, true, NULL
);
3164 peer
= peer_lock(peer
); /* group->peer list reference */
3165 listnode_add(group
->peer
, peer
);
3167 peer_group2peer_config_copy(group
, peer
);
3169 /* If the peer-group is active for this afi/safi then activate
3171 FOREACH_AFI_SAFI (afi
, safi
) {
3172 if (group
->conf
->afc
[afi
][safi
]) {
3173 peer
->afc
[afi
][safi
] = 1;
3175 if (!peer_af_find(peer
, afi
, safi
))
3176 peer_af_create(peer
, afi
, safi
);
3178 peer_group2peer_config_copy_af(group
, peer
, afi
,
3180 } else if (peer
->afc
[afi
][safi
])
3181 peer_deactivate(peer
, afi
, safi
);
3184 /* Set up peer's events and timers. */
3185 if (peer_active(peer
))
3186 bgp_timer_set(peer
);
3192 static void bgp_startup_timer_expire(struct thread
*thread
)
3196 bgp
= THREAD_ARG(thread
);
3197 bgp
->t_startup
= NULL
;
3201 * On shutdown we call the cleanup function which
3202 * does a free of the link list nodes, free up
3203 * the data we are pointing at too.
3205 static void bgp_vrf_string_name_delete(void *data
)
3209 XFREE(MTYPE_TMP
, vname
);
3212 /* BGP instance creation by `router bgp' commands. */
3213 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3214 enum bgp_instance_type inst_type
,
3215 const char *as_pretty
,
3216 enum asnotation_mode asnotation
)
3222 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3225 bgp
->as_pretty
= XSTRDUP(MTYPE_BGP
, as_pretty
);
3227 bgp
->as_pretty
= XSTRDUP(MTYPE_BGP
, asn_asn2asplain(*as
));
3229 if (asnotation
!= ASNOTATION_UNDEFINED
) {
3230 bgp
->asnotation
= asnotation
;
3231 SET_FLAG(bgp
->config
, BGP_CONFIG_ASNOTATION
);
3233 asn_str2asn_notation(bgp
->as_pretty
, NULL
, &bgp
->asnotation
);
3235 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3236 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3237 zlog_debug("Creating Default VRF, AS %s",
3240 zlog_debug("Creating %s %s, AS %s",
3241 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3244 name
, bgp
->as_pretty
);
3247 /* Default the EVPN VRF to the default one */
3248 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3255 bgp
->allow_martian
= false;
3256 bgp_process_queue_init(bgp
);
3257 bgp
->heuristic_coalesce
= true;
3258 bgp
->inst_type
= inst_type
;
3259 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3261 bgp
->peer_self
= peer_new(bgp
);
3262 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3263 bgp
->peer_self
->host
=
3264 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3265 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3266 if (cmd_hostname_get())
3267 bgp
->peer_self
->hostname
=
3268 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3270 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3271 if (cmd_domainname_get())
3272 bgp
->peer_self
->domainname
=
3273 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3274 bgp
->peer
= list_new();
3275 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3276 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3278 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3280 bgp
->group
= list_new();
3281 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3283 FOREACH_AFI_SAFI (afi
, safi
) {
3284 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3285 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3286 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3288 /* Enable maximum-paths */
3289 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3291 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3293 /* Initialize graceful restart info */
3294 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3295 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3296 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3297 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3298 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3301 bgp
->v_update_delay
= bm
->v_update_delay
;
3302 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3303 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3304 bgp
->default_subgroup_pkt_queue_max
=
3305 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3306 bgp_tcp_keepalive_unset(bgp
);
3307 bgp_timers_unset(bgp
);
3308 bgp
->default_min_holdtime
= 0;
3309 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3310 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3311 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3312 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3313 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3314 bgp
->dynamic_neighbors_count
= 0;
3315 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3316 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3317 bgp
->reject_as_sets
= false;
3318 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3319 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3320 bgp
->fast_convergence
= false;
3321 bgp
->llgr_stale_time
= BGP_DEFAULT_LLGR_STALE_TIME
;
3323 #ifdef ENABLE_BGP_VNC
3324 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3325 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3327 assert(bgp
->rfapi_cfg
);
3329 #endif /* ENABLE_BGP_VNC */
3331 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3332 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3333 bgp
->vpn_policy
[afi
].afi
= afi
;
3334 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3335 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3338 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3339 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3340 bgp_vrf_string_name_delete
;
3341 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3342 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3343 bgp_vrf_string_name_delete
;
3344 SET_FLAG(bgp
->af_flags
[afi
][SAFI_MPLS_VPN
],
3345 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL
);
3348 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3350 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3351 bgp
->restart_time
, &bgp
->t_startup
);
3353 /* printable name we can use in debug messages */
3354 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3355 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3365 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3367 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3368 snprintf(bgp
->name_pretty
, len
, "%s %s",
3369 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3375 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3376 memory_order_relaxed
);
3377 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3378 memory_order_relaxed
);
3379 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3380 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3384 update_bgp_group_init(bgp
);
3386 /* assign a unique rd id for auto derivation of vrf's RD */
3387 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3389 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3390 sizeof(struct bgp_evpn_info
));
3392 bgp_evpn_vrf_es_init(bgp
);
3396 /*initilize global GR FSM */
3397 bgp_global_gr_init(bgp
);
3399 memset(&bgp
->ebgprequirespolicywarning
, 0,
3400 sizeof(bgp
->ebgprequirespolicywarning
));
3405 /* Return the "default VRF" instance of BGP. */
3406 struct bgp
*bgp_get_default(void)
3409 struct listnode
*node
, *nnode
;
3411 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3412 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3417 /* Lookup BGP entry. */
3418 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3421 struct listnode
*node
, *nnode
;
3423 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3425 && ((bgp
->name
== NULL
&& name
== NULL
)
3426 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3431 /* Lookup BGP structure by view name. */
3432 struct bgp
*bgp_lookup_by_name(const char *name
)
3435 struct listnode
*node
, *nnode
;
3437 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3438 if ((bgp
->name
== NULL
&& name
== NULL
)
3439 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3444 /* Lookup BGP instance based on VRF id. */
3445 /* Note: Only to be used for incoming messages from Zebra. */
3446 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3450 /* Lookup VRF (in tree) and follow link. */
3451 vrf
= vrf_lookup_by_id(vrf_id
);
3454 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3457 /* Sets the BGP instance where EVPN is enabled */
3458 void bgp_set_evpn(struct bgp
*bgp
)
3460 if (bm
->bgp_evpn
== bgp
)
3463 /* First, release the reference count we hold on the instance */
3465 bgp_unlock(bm
->bgp_evpn
);
3469 /* Increase the reference count on this new VRF */
3471 bgp_lock(bm
->bgp_evpn
);
3474 /* Returns the BGP instance where EVPN is enabled, if any */
3475 struct bgp
*bgp_get_evpn(void)
3477 return bm
->bgp_evpn
;
3480 /* handle socket creation or deletion, if necessary
3481 * this is called for all new BGP instances
3483 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3486 struct listnode
*node
;
3489 /* Create BGP server socket, if listen mode not disabled */
3490 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3492 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3494 * suppress vrf socket
3497 bgp_close_vrf_socket(bgp
);
3501 return BGP_ERR_INVALID_VALUE
;
3503 * if vrf_id did not change
3505 if (vrf
->vrf_id
== old_vrf_id
)
3507 if (old_vrf_id
!= VRF_UNKNOWN
) {
3508 /* look for old socket. close it. */
3509 bgp_close_vrf_socket(bgp
);
3511 /* if backend is not yet identified ( VRF_UNKNOWN) then
3512 * creation will be done later
3514 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3516 if (list_isempty(bm
->addresses
)) {
3517 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3518 return BGP_ERR_INVALID_VALUE
;
3520 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3521 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3522 return BGP_ERR_INVALID_VALUE
;
3526 return bgp_check_main_socket(create
, bgp
);
3529 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3530 enum bgp_instance_type inst_type
)
3534 /* Multiple instance check. */
3536 bgp
= bgp_lookup_by_name(name
);
3538 bgp
= bgp_get_default();
3542 if (bgp
->as
!= *as
) {
3544 return BGP_ERR_AS_MISMATCH
;
3546 if (bgp
->inst_type
!= inst_type
)
3547 return BGP_ERR_INSTANCE_MISMATCH
;
3555 /* Called from VTY commands. */
3556 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3557 enum bgp_instance_type inst_type
, const char *as_pretty
,
3558 enum asnotation_mode asnotation
)
3561 struct vrf
*vrf
= NULL
;
3564 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3565 if (ret
|| *bgp_val
)
3568 bgp
= bgp_create(as
, name
, inst_type
, as_pretty
, asnotation
);
3571 * view instances will never work inside of a vrf
3572 * as such they must always be in the VRF_DEFAULT
3573 * Also we must set this to something useful because
3574 * of the vrf socket code needing an actual useful
3575 * default value to send to the underlying OS.
3577 * This code is currently ignoring vrf based
3578 * code using the -Z option( and that is probably
3579 * best addressed elsewhere in the code )
3581 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3582 bgp
->vrf_id
= VRF_DEFAULT
;
3584 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3585 bgp_address_init(bgp
);
3586 bgp_tip_hash_init(bgp
);
3590 bgp
->t_rmap_def_originate_eval
= NULL
;
3592 /* If Default instance or VRF, link to the VRF structure, if present. */
3593 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3594 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3595 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3597 bgp_vrf_link(bgp
, vrf
);
3599 /* BGP server socket already processed if BGP instance
3600 * already part of the list
3602 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3603 listnode_add(bm
->bgp
, bgp
);
3605 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3606 if (BGP_DEBUG(zebra
, ZEBRA
))
3607 zlog_debug("%s: Registering BGP instance %s to zebra",
3608 __func__
, bgp
->name_pretty
);
3609 bgp_zebra_instance_register(bgp
);
3615 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3616 vrf_id_t vrf_id
, bool set
)
3620 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3623 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3627 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3629 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3633 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3637 struct list
*red_list
;
3638 struct listnode
*node
;
3639 struct bgp_redist
*red
;
3641 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3642 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3644 red_list
= bgp
->redist
[afi
][i
];
3648 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3649 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3656 * Make BGP instance "up". Applies only to VRFs (non-default) and
3657 * implies the VRF has been learnt from Zebra.
3659 void bgp_instance_up(struct bgp
*bgp
)
3662 struct listnode
*node
, *next
;
3664 bgp_set_redist_vrf_bitmaps(bgp
, true);
3666 /* Register with zebra. */
3667 bgp_zebra_instance_register(bgp
);
3669 /* Kick off any peers that may have been configured. */
3670 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3671 if (!BGP_PEER_START_SUPPRESSED(peer
))
3672 BGP_EVENT_ADD(peer
, BGP_Start
);
3675 /* Process any networks that have been configured. */
3676 bgp_static_add(bgp
);
3680 * Make BGP instance "down". Applies only to VRFs (non-default) and
3681 * implies the VRF has been deleted by Zebra.
3683 void bgp_instance_down(struct bgp
*bgp
)
3686 struct listnode
*node
;
3687 struct listnode
*next
;
3690 if (bgp
->t_rmap_def_originate_eval
) {
3691 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3692 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3696 /* Bring down peers, so corresponding routes are purged. */
3697 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3698 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3699 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3700 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3702 bgp_session_reset(peer
);
3705 /* Purge network and redistributed routes. */
3706 bgp_purge_static_redist_routes(bgp
);
3708 /* Cleanup registered nexthops (flags) */
3709 bgp_cleanup_nexthops(bgp
);
3711 bgp_zebra_instance_deregister(bgp
);
3713 bgp_set_redist_vrf_bitmaps(bgp
, false);
3716 /* Delete BGP instance. */
3717 int bgp_delete(struct bgp
*bgp
)
3720 struct peer_group
*group
;
3721 struct listnode
*node
, *next
;
3726 struct graceful_restart_info
*gr_info
;
3730 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3732 /* make sure we withdraw any exported routes */
3733 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3735 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3738 bgp_vpn_leak_unimport(bgp
);
3740 hook_call(bgp_inst_delete
, bgp
);
3742 FOREACH_AFI_SAFI (afi
, safi
)
3743 THREAD_OFF(bgp
->t_revalidate
[afi
][safi
]);
3745 THREAD_OFF(bgp
->t_condition_check
);
3746 THREAD_OFF(bgp
->t_startup
);
3747 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3748 THREAD_OFF(bgp
->t_update_delay
);
3749 THREAD_OFF(bgp
->t_establish_wait
);
3751 /* Set flag indicating bgp instance delete in progress */
3752 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3754 /* Delete the graceful restart info */
3755 FOREACH_AFI_SAFI (afi
, safi
) {
3758 gr_info
= &bgp
->gr_info
[afi
][safi
];
3761 t
= gr_info
->t_select_deferral
;
3763 void *info
= THREAD_ARG(t
);
3765 XFREE(MTYPE_TMP
, info
);
3767 THREAD_OFF(gr_info
->t_select_deferral
);
3769 t
= gr_info
->t_route_select
;
3771 void *info
= THREAD_ARG(t
);
3773 XFREE(MTYPE_TMP
, info
);
3775 THREAD_OFF(gr_info
->t_route_select
);
3778 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3779 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3780 zlog_debug("Deleting Default VRF");
3782 zlog_debug("Deleting %s %s",
3783 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3789 /* unmap from RT list */
3790 bgp_evpn_vrf_delete(bgp
);
3792 /* unmap bgp vrf label */
3793 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3794 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3797 if (bgp
->t_rmap_def_originate_eval
) {
3798 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3799 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3803 /* Inform peers we're going down. */
3804 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3805 peer_notify_shutdown(peer
);
3807 /* Delete static routes (networks). */
3808 bgp_static_delete(bgp
);
3810 /* Unset redistribution. */
3811 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3812 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3813 if (i
!= ZEBRA_ROUTE_BGP
)
3814 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3816 /* Free peers and peer-groups. */
3817 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3818 peer_group_delete(group
);
3820 while (listcount(bgp
->peer
)) {
3821 peer
= listnode_head(bgp
->peer
);
3825 if (bgp
->peer_self
) {
3826 peer_delete(bgp
->peer_self
);
3827 bgp
->peer_self
= NULL
;
3830 update_bgp_group_free(bgp
);
3832 /* TODO - Other memory may need to be freed - e.g., NHT */
3834 #ifdef ENABLE_BGP_VNC
3837 bgp_cleanup_routes(bgp
);
3839 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3840 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3843 &bgp
->vpn_policy
[afi
]
3844 .import_redirect_rtlist
);
3845 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3848 /* Free any memory allocated to holding routemap references */
3849 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3850 for (enum vpn_policy_direction dir
= 0;
3851 dir
< BGP_VPN_POLICY_DIR_MAX
; ++dir
) {
3852 if (bgp
->vpn_policy
[afi
].rmap_name
[dir
])
3853 XFREE(MTYPE_ROUTE_MAP_NAME
,
3854 bgp
->vpn_policy
[afi
].rmap_name
[dir
]);
3855 bgp
->vpn_policy
[afi
].rmap
[dir
] = NULL
;
3859 /* Deregister from Zebra, if needed */
3860 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3861 if (BGP_DEBUG(zebra
, ZEBRA
))
3863 "%s: deregistering this bgp %s instance from zebra",
3864 __func__
, bgp
->name
);
3865 bgp_zebra_instance_deregister(bgp
);
3868 /* Remove visibility via the master list - there may however still be
3869 * routes to be processed still referencing the struct bgp.
3871 listnode_delete(bm
->bgp
, bgp
);
3873 /* Free interfaces in this instance. */
3876 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3877 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3879 bgp_vrf_unlink(bgp
, vrf
);
3881 /* Update EVPN VRF pointer */
3882 if (bm
->bgp_evpn
== bgp
) {
3883 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3886 bgp_set_evpn(bgp_get_default());
3889 if (bgp
->process_queue
)
3890 work_queue_free_and_null(&bgp
->process_queue
);
3892 thread_master_free_unused(bm
->master
);
3893 bgp_unlock(bgp
); /* initial reference */
3898 void bgp_free(struct bgp
*bgp
)
3902 struct bgp_table
*table
;
3903 struct bgp_dest
*dest
;
3904 struct bgp_rmap
*rmap
;
3908 list_delete(&bgp
->group
);
3909 list_delete(&bgp
->peer
);
3911 if (bgp
->peerhash
) {
3912 hash_free(bgp
->peerhash
);
3913 bgp
->peerhash
= NULL
;
3916 FOREACH_AFI_SAFI (afi
, safi
) {
3917 /* Special handling for 2-level routing tables. */
3918 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3919 || safi
== SAFI_EVPN
) {
3920 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3921 dest
= bgp_route_next(dest
)) {
3922 table
= bgp_dest_get_bgp_table_info(dest
);
3923 bgp_table_finish(&table
);
3926 if (bgp
->route
[afi
][safi
])
3927 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3928 if (bgp
->aggregate
[afi
][safi
])
3929 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3930 if (bgp
->rib
[afi
][safi
])
3931 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3932 rmap
= &bgp
->table_map
[afi
][safi
];
3933 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3936 bgp_scan_finish(bgp
);
3937 bgp_address_destroy(bgp
);
3938 bgp_tip_hash_destroy(bgp
);
3940 /* release the auto RD id */
3941 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3943 bgp_evpn_cleanup(bgp
);
3944 bgp_pbr_cleanup(bgp
);
3945 bgp_srv6_cleanup(bgp
);
3946 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3948 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3949 enum vpn_policy_direction dir
;
3951 if (bgp
->vpn_policy
[afi
].import_vrf
)
3952 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3953 if (bgp
->vpn_policy
[afi
].export_vrf
)
3954 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3956 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3957 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3958 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3959 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3960 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3961 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3964 XFREE(MTYPE_BGP
, bgp
->as_pretty
);
3965 XFREE(MTYPE_BGP
, bgp
->name
);
3966 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3967 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3969 XFREE(MTYPE_BGP
, bgp
);
3972 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3975 struct listnode
*node
, *nnode
;
3981 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3982 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3983 && !CHECK_FLAG(peer
->sflags
,
3984 PEER_STATUS_ACCEPT_PEER
))
3986 } else if (bm
->bgp
!= NULL
) {
3987 struct listnode
*bgpnode
, *nbgpnode
;
3989 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3990 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3992 && !strcmp(peer
->conf_if
, conf_if
)
3993 && !CHECK_FLAG(peer
->sflags
,
3994 PEER_STATUS_ACCEPT_PEER
))
4000 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
4003 struct listnode
*node
, *nnode
;
4009 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
4010 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
4011 && !CHECK_FLAG(peer
->sflags
,
4012 PEER_STATUS_ACCEPT_PEER
))
4014 } else if (bm
->bgp
!= NULL
) {
4015 struct listnode
*bgpnode
, *nbgpnode
;
4017 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4018 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
4020 && !strcmp(peer
->hostname
, hostname
)
4021 && !CHECK_FLAG(peer
->sflags
,
4022 PEER_STATUS_ACCEPT_PEER
))
4028 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
4030 struct peer
*peer
= NULL
;
4031 struct peer tmp_peer
;
4033 memset(&tmp_peer
, 0, sizeof(struct peer
));
4036 * We do not want to find the doppelganger peer so search for the peer
4038 * the hash that has PEER_FLAG_CONFIG_NODE
4040 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
4045 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4046 } else if (bm
->bgp
!= NULL
) {
4047 struct listnode
*bgpnode
, *nbgpnode
;
4049 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
4050 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4059 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
4060 union sockunion
*su
,
4061 struct peer_group
*group
)
4067 /* Create peer first; we've already checked group config is valid. */
4068 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
4069 group
->conf
->as_type
, group
, true, NULL
);
4074 peer
= peer_lock(peer
);
4075 listnode_add(group
->peer
, peer
);
4077 peer_group2peer_config_copy(group
, peer
);
4080 * Bind peer for all AFs configured for the group. We don't call
4081 * peer_group_bind as that is sub-optimal and does some stuff we don't
4084 FOREACH_AFI_SAFI (afi
, safi
) {
4085 if (!group
->conf
->afc
[afi
][safi
])
4087 peer
->afc
[afi
][safi
] = 1;
4089 if (!peer_af_find(peer
, afi
, safi
))
4090 peer_af_create(peer
, afi
, safi
);
4092 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
4095 /* Mark as dynamic, but also as a "config node" for other things to
4097 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
4103 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
4104 struct prefix
*prefix
)
4106 struct listnode
*node
, *nnode
;
4107 struct prefix
*range
;
4110 afi
= family2afi(prefix
->family
);
4112 if (group
->listen_range
[afi
])
4113 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
4115 if (prefix_match(range
, prefix
))
4122 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
4123 struct prefix
**listen_range
)
4125 struct prefix
*range
= NULL
;
4126 struct peer_group
*group
= NULL
;
4127 struct listnode
*node
, *nnode
;
4129 *listen_range
= NULL
;
4131 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4132 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4135 } else if (bm
->bgp
!= NULL
) {
4136 struct listnode
*bgpnode
, *nbgpnode
;
4138 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4139 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4140 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4146 *listen_range
= range
;
4147 return (group
&& range
) ? group
: NULL
;
4150 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
4152 struct peer_group
*group
;
4155 struct prefix prefix
;
4156 struct prefix
*listen_range
;
4159 if (!sockunion2hostprefix(su
, &prefix
))
4162 /* See if incoming connection matches a configured listen range. */
4163 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
4174 if (bgp_debug_neighbor_events(NULL
))
4176 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4177 &prefix
, group
->name
, listen_range
);
4179 /* Are we within the listen limit? */
4180 dncount
= gbgp
->dynamic_neighbors_count
;
4182 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4183 if (bgp_debug_neighbor_events(NULL
))
4185 "Dynamic Neighbor %pFX rejected - at limit %d",
4186 &prefix
, gbgp
->dynamic_neighbors_limit
);
4190 /* Ensure group is not disabled. */
4191 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4192 if (bgp_debug_neighbor_events(NULL
))
4194 "Dynamic Neighbor %pFX rejected - group %s disabled",
4195 &prefix
, group
->name
);
4199 /* Check that at least one AF is activated for the group. */
4200 if (!peer_group_af_configured(group
)) {
4201 if (bgp_debug_neighbor_events(NULL
))
4203 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4204 &prefix
, group
->name
);
4208 /* Create dynamic peer and bind to associated group. */
4209 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4212 gbgp
->dynamic_neighbors_count
= ++dncount
;
4214 if (bgp_debug_neighbor_events(peer
))
4215 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4216 peer
->host
, group
->name
, dncount
);
4221 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4224 if (peer
->group
->bgp
) {
4225 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4227 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4229 if (bgp_debug_neighbor_events(peer
))
4230 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4231 peer
->group
->name
, dncount
);
4234 bool bgp_path_attribute_discard(struct peer
*peer
, char *buf
, size_t size
)
4241 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4242 if (peer
->discard_attrs
[i
])
4243 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4244 (strlen(buf
) > 0) ? " " : "", i
);
4247 if (strlen(buf
) > 0)
4253 /* If peer is configured at least one address family return 1. */
4254 bool peer_active(struct peer
*peer
)
4256 if (BGP_PEER_SU_UNSPEC(peer
))
4258 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4259 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4260 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4261 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4262 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4263 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4264 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4265 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4266 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4267 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4268 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4273 /* If peer is negotiated at least one address family return 1. */
4274 bool peer_active_nego(struct peer
*peer
)
4276 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4277 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4278 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4279 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4280 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4281 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4282 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4283 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4284 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4285 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4286 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4287 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4288 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4293 /* If peer received at least one address family MP, return true */
4294 bool peer_afc_received(struct peer
*peer
)
4299 FOREACH_AFI_SAFI (afi
, safi
)
4300 if (peer
->afc_recv
[afi
][safi
])
4306 /* If peer advertised at least one address family MP, return true */
4307 bool peer_afc_advertised(struct peer
*peer
)
4312 FOREACH_AFI_SAFI (afi
, safi
)
4313 if (peer
->afc_adv
[afi
][safi
])
4319 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4320 enum peer_change_type type
)
4322 struct peer_af
*paf
;
4324 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4327 if (!peer_established(peer
))
4330 if (type
== peer_change_reset
) {
4331 /* If we're resetting session, we've to delete both peer struct
4333 if ((peer
->doppelganger
)
4334 && (peer
->doppelganger
->status
!= Deleted
)
4335 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4336 PEER_FLAG_CONFIG_NODE
)))
4337 peer_delete(peer
->doppelganger
);
4339 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4340 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4341 } else if (type
== peer_change_reset_in
) {
4342 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4343 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4344 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4345 BGP_ROUTE_REFRESH_NORMAL
);
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
);
4356 } else if (type
== peer_change_reset_out
) {
4357 paf
= peer_af_find(peer
, afi
, safi
);
4358 if (paf
&& paf
->subgroup
)
4359 SET_FLAG(paf
->subgroup
->sflags
,
4360 SUBGRP_STATUS_FORCE_UPDATES
);
4362 update_group_adjust_peer(paf
);
4363 bgp_announce_route(peer
, afi
, safi
, false);
4367 struct peer_flag_action
{
4371 /* This flag can be set for peer-group member. */
4372 uint8_t not_for_member
;
4374 /* Action when the flag is changed. */
4375 enum peer_change_type type
;
4378 static const struct peer_flag_action peer_flag_action_list
[] = {
4379 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4380 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4381 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4382 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4383 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4384 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4385 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4386 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4387 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4388 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4389 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4390 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4391 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4392 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4393 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4394 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4395 {PEER_FLAG_LOCAL_AS
, 0, peer_change_reset
},
4396 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_reset
},
4397 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_reset
},
4398 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4399 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4400 {PEER_FLAG_EXTENDED_OPT_PARAMS
, 0, peer_change_reset
},
4401 {PEER_FLAG_ROLE_STRICT_MODE
, 0, peer_change_reset
},
4402 {PEER_FLAG_ROLE
, 0, peer_change_reset
},
4403 {PEER_FLAG_PORT
, 0, peer_change_reset
},
4404 {PEER_FLAG_AIGP
, 0, peer_change_none
},
4405 {PEER_FLAG_GRACEFUL_SHUTDOWN
, 0, peer_change_none
},
4408 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4409 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4410 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4411 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4412 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4413 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4414 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4415 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4416 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4417 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4418 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4419 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4420 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4421 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4422 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4423 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4424 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4425 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4426 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4427 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4428 {PEER_FLAG_MAX_PREFIX_OUT
, 0, peer_change_none
},
4429 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4430 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4431 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4432 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4433 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4434 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4435 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4436 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4437 {PEER_FLAG_SOO
, 0, peer_change_reset
},
4438 {PEER_FLAG_ACCEPT_OWN
, 0, peer_change_reset
},
4441 /* Proper action set. */
4442 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4443 int size
, struct peer_flag_action
*action
,
4450 const struct peer_flag_action
*match
= NULL
;
4452 /* Check peer's frag action. */
4453 for (i
= 0; i
< size
; i
++) {
4454 match
= &action_list
[i
];
4456 if (match
->flag
== 0)
4459 if (match
->flag
& flag
) {
4462 if (match
->type
== peer_change_reset_in
)
4464 if (match
->type
== peer_change_reset_out
)
4466 if (match
->type
== peer_change_reset
) {
4470 if (match
->not_for_member
)
4471 action
->not_for_member
= 1;
4475 /* Set peer clear type. */
4476 if (reset_in
&& reset_out
)
4477 action
->type
= peer_change_reset
;
4479 action
->type
= peer_change_reset_in
;
4481 action
->type
= peer_change_reset_out
;
4483 action
->type
= peer_change_none
;
4488 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4490 if (flag
== PEER_FLAG_SHUTDOWN
) {
4491 if (CHECK_FLAG(peer
->flags
, flag
)) {
4492 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4493 peer_nsf_stop(peer
);
4495 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4497 if (peer
->t_pmax_restart
) {
4498 THREAD_OFF(peer
->t_pmax_restart
);
4499 if (bgp_debug_neighbor_events(peer
))
4501 "%pBP Maximum-prefix restart timer canceled",
4505 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4506 char *msg
= peer
->tx_shutdown_message
;
4508 uint8_t msgbuf
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4510 if (!msg
&& peer_group_active(peer
))
4511 msg
= peer
->group
->conf
4512 ->tx_shutdown_message
;
4513 msglen
= msg
? strlen(msg
) : 0;
4514 if (msglen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4515 msglen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4519 memcpy(msgbuf
+ 1, msg
, msglen
);
4521 bgp_notify_send_with_data(
4522 peer
, BGP_NOTIFY_CEASE
,
4523 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4524 msgbuf
, msglen
+ 1);
4527 peer
, BGP_NOTIFY_CEASE
,
4528 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4530 bgp_session_reset(peer
);
4532 peer
->v_start
= BGP_INIT_START_TIMER
;
4533 BGP_EVENT_ADD(peer
, BGP_Stop
);
4535 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4536 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4537 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4538 else if (flag
== PEER_FLAG_PASSIVE
)
4539 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4540 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4541 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4543 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4544 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4546 bgp_session_reset(peer
);
4549 /* Enable global administrative shutdown of all peers of BGP instance */
4550 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4553 struct listnode
*node
;
4554 /* length(1) + message(N) */
4555 uint8_t data
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4557 /* do nothing if already shut down */
4558 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4561 /* informational log message */
4562 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4565 /* iterate through peers of BGP instance */
4566 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4567 /* continue, if peer is already in administrative shutdown. */
4568 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4571 /* send a RFC 4486 notification message if necessary */
4572 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4574 size_t datalen
= strlen(msg
);
4576 if (datalen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4577 datalen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4580 memcpy(data
+ 1, msg
, datalen
);
4582 bgp_notify_send_with_data(
4583 peer
, BGP_NOTIFY_CEASE
,
4584 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
, data
,
4588 peer
, BGP_NOTIFY_CEASE
,
4589 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4593 /* reset start timer to initial value */
4594 peer
->v_start
= BGP_INIT_START_TIMER
;
4596 /* trigger a RFC 4271 ManualStop event */
4597 BGP_EVENT_ADD(peer
, BGP_Stop
);
4600 /* set the BGP instances shutdown flag */
4601 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4604 /* Disable global administrative shutdown of all peers of BGP instance */
4605 void bgp_shutdown_disable(struct bgp
*bgp
)
4607 /* do nothing if not shut down. */
4608 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4611 /* informational log message */
4612 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4615 /* clear the BGP instances shutdown flag */
4616 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4619 /* Change specified peer flag. */
4620 static int peer_flag_modify(struct peer
*peer
, uint64_t flag
, int set
)
4624 bool invert
, member_invert
;
4625 struct peer
*member
;
4626 struct listnode
*node
, *nnode
;
4627 struct peer_flag_action action
;
4629 memset(&action
, 0, sizeof(struct peer_flag_action
));
4630 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4632 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4633 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4636 /* Abort if no flag action exists. */
4638 return BGP_ERR_INVALID_FLAG
;
4640 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4641 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4642 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4643 return BGP_ERR_PEER_FLAG_CONFLICT
;
4645 /* Handle flag updates where desired state matches current state. */
4646 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4647 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4648 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4652 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4653 COND_FLAG(peer
->flags_override
, flag
, invert
);
4658 /* Inherit from peer-group or set/unset flags accordingly. */
4659 if (peer_group_active(peer
) && set
== invert
)
4660 peer_flag_inherit(peer
, flag
);
4662 COND_FLAG(peer
->flags
, flag
, set
);
4664 /* Check if handling a regular peer. */
4665 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4666 /* Update flag override state accordingly. */
4667 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4670 * For the extended next-hop encoding flag we need to turn RAs
4671 * on if flag is being set, but only turn RAs off if the flag
4672 * is being unset on this peer and if this peer is a member of a
4673 * peer-group, the peer-group also doesn't have the flag set.
4675 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4677 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4678 } else if (peer_group_active(peer
)) {
4679 if (!CHECK_FLAG(peer
->group
->conf
->flags
,
4682 bgp_zebra_terminate_radv(peer
->bgp
,
4685 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4688 /* Execute flag action on peer. */
4689 if (action
.type
== peer_change_reset
)
4690 peer_flag_modify_action(peer
, flag
);
4692 /* Skip peer-group mechanics for regular peers. */
4697 * Update peer-group members, unless they are explicitly overriding
4698 * peer-group configuration.
4700 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4701 /* Skip peers with overridden configuration. */
4702 if (CHECK_FLAG(member
->flags_override
, flag
))
4705 /* Check if only member without group is inverted. */
4707 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4709 /* Skip peers with equivalent configuration. */
4710 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4713 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4716 /* Update flag on peer-group member. */
4717 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4719 if (flag
== PEER_FLAG_CAPABILITY_ENHE
&& !member
->conf_if
)
4720 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4721 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4723 /* Execute flag action on peer-group member. */
4724 if (action
.type
== peer_change_reset
)
4725 peer_flag_modify_action(member
, flag
);
4731 int peer_flag_set(struct peer
*peer
, uint64_t flag
)
4733 return peer_flag_modify(peer
, flag
, 1);
4736 int peer_flag_unset(struct peer
*peer
, uint64_t flag
)
4738 return peer_flag_modify(peer
, flag
, 0);
4741 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4742 uint64_t flag
, bool set
)
4746 bool invert
, member_invert
;
4747 struct peer
*member
;
4748 struct listnode
*node
, *nnode
;
4749 struct peer_flag_action action
;
4750 enum bgp_peer_sort ptype
;
4752 memset(&action
, 0, sizeof(struct peer_flag_action
));
4753 size
= sizeof(peer_af_flag_action_list
)
4754 / sizeof(struct peer_flag_action
);
4756 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4757 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4760 /* Abort if flag action exists. */
4762 return BGP_ERR_INVALID_FLAG
;
4764 ptype
= peer_sort(peer
);
4765 /* Special check for reflector client. */
4766 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4767 return BGP_ERR_NOT_INTERNAL_PEER
;
4769 /* Special check for remove-private-AS. */
4770 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4771 return BGP_ERR_REMOVE_PRIVATE_AS
;
4773 /* as-override is not allowed for IBGP peers */
4774 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4775 return BGP_ERR_AS_OVERRIDE
;
4777 /* Handle flag updates where desired state matches current state. */
4778 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4779 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4780 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4785 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4786 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4793 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4794 * if we are setting/unsetting flags which conflict with this flag
4795 * handle accordingly
4797 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4801 * if we are setting NEXTHOP_SELF, we need to unset the
4802 * NEXTHOP_UNCHANGED flag
4804 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4805 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4806 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4807 PEER_FLAG_NEXTHOP_UNCHANGED
);
4811 * if we are unsetting NEXTHOP_SELF, we need to set the
4812 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4814 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4815 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4816 SET_FLAG(peer
->af_flags
[afi
][safi
],
4817 PEER_FLAG_NEXTHOP_UNCHANGED
);
4822 * If the peer is a route server client let's not
4823 * muck with the nexthop on the way out the door
4825 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4827 SET_FLAG(peer
->af_flags
[afi
][safi
],
4828 PEER_FLAG_NEXTHOP_UNCHANGED
);
4830 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4831 PEER_FLAG_NEXTHOP_UNCHANGED
);
4834 /* Inherit from peer-group or set/unset flags accordingly. */
4835 if (peer_group_active(peer
) && set
== invert
)
4836 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4838 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4840 /* Execute action when peer is established. */
4841 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4842 && peer_established(peer
)) {
4843 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4844 bgp_clear_adj_in(peer
, afi
, safi
);
4846 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4847 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4848 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4849 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4850 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4851 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4852 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4853 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4855 peer_change_action(peer
, afi
, safi
, action
.type
);
4859 /* Check if handling a regular peer. */
4860 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4861 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4865 * Update peer-group members, unless they are explicitly
4866 * overriding peer-group configuration.
4868 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4870 /* Skip peers with overridden configuration. */
4871 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4875 /* Check if only member without group is inverted. */
4877 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4881 /* Skip peers with equivalent configuration. */
4882 if (set
!= member_invert
4883 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4886 if (set
== member_invert
4887 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4890 /* Update flag on peer-group member. */
4891 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4892 set
!= member_invert
);
4894 /* Execute flag action on peer-group member. */
4895 if (peer_established(member
)) {
4896 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4897 bgp_clear_adj_in(member
, afi
, safi
);
4899 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4900 member
->last_reset
=
4901 PEER_DOWN_RR_CLIENT_CHANGE
;
4903 == PEER_FLAG_RSERVER_CLIENT
)
4904 member
->last_reset
=
4905 PEER_DOWN_RS_CLIENT_CHANGE
;
4907 == PEER_FLAG_ORF_PREFIX_SM
)
4908 member
->last_reset
=
4909 PEER_DOWN_CAPABILITY_CHANGE
;
4911 == PEER_FLAG_ORF_PREFIX_RM
)
4912 member
->last_reset
=
4913 PEER_DOWN_CAPABILITY_CHANGE
;
4915 peer_change_action(member
, afi
, safi
,
4925 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4927 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4930 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4932 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4936 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4938 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4939 peer
->tx_shutdown_message
=
4940 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4943 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4945 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4949 /* EBGP multihop configuration. */
4950 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4952 struct peer_group
*group
;
4953 struct listnode
*node
, *nnode
;
4956 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4959 /* is there anything to do? */
4960 if (peer
->ttl
== ttl
)
4963 /* see comment in peer_ttl_security_hops_set() */
4964 if (ttl
!= MAXTTL
) {
4965 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4966 group
= peer
->group
;
4967 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4968 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4970 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4972 if (peer1
->sort
== BGP_PEER_IBGP
)
4975 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4976 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4979 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4980 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4986 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4987 if (peer
->sort
!= BGP_PEER_IBGP
) {
4988 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4989 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4990 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4992 bgp_session_reset(peer
);
4994 /* Reconfigure BFD peer with new TTL. */
4995 if (peer
->bfd_config
)
4996 bgp_peer_bfd_update_source(peer
);
4999 group
= peer
->group
;
5000 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5001 if (peer
->sort
== BGP_PEER_IBGP
)
5004 peer
->ttl
= group
->conf
->ttl
;
5006 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5007 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5008 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5010 bgp_session_reset(peer
);
5012 /* Reconfigure BFD peer with new TTL. */
5013 if (peer
->bfd_config
)
5014 bgp_peer_bfd_update_source(peer
);
5020 int peer_ebgp_multihop_unset(struct peer
*peer
)
5022 struct peer_group
*group
;
5023 struct listnode
*node
, *nnode
;
5026 if (peer
->sort
== BGP_PEER_IBGP
)
5029 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
5030 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
5032 if (peer_group_active(peer
))
5033 ttl
= peer
->group
->conf
->ttl
;
5035 ttl
= BGP_DEFAULT_TTL
;
5037 if (ttl
== peer
->ttl
)
5042 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5043 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5044 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5045 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5047 bgp_session_reset(peer
);
5049 /* Reconfigure BFD peer with new TTL. */
5050 if (peer
->bfd_config
)
5051 bgp_peer_bfd_update_source(peer
);
5053 group
= peer
->group
;
5054 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5055 if (peer
->sort
== BGP_PEER_IBGP
)
5058 peer
->ttl
= BGP_DEFAULT_TTL
;
5060 if (peer
->fd
>= 0) {
5061 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5063 peer
, BGP_NOTIFY_CEASE
,
5064 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5066 bgp_session_reset(peer
);
5069 /* Reconfigure BFD peer with new TTL. */
5070 if (peer
->bfd_config
)
5071 bgp_peer_bfd_update_source(peer
);
5077 /* Set Open Policy Role and check its correctness */
5078 int peer_role_set(struct peer
*peer
, uint8_t role
, bool strict_mode
)
5080 struct peer
*member
;
5081 struct listnode
*node
, *nnode
;
5083 peer_flag_set(peer
, PEER_FLAG_ROLE
);
5085 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5086 if (peer
->sort
!= BGP_PEER_EBGP
)
5087 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5089 if (peer
->local_role
== role
) {
5090 if (CHECK_FLAG(peer
->flags
,
5091 PEER_FLAG_ROLE_STRICT_MODE
) &&
5093 /* TODO: Is session restart needed if it was
5096 UNSET_FLAG(peer
->flags
,
5097 PEER_FLAG_ROLE_STRICT_MODE
);
5098 if (!CHECK_FLAG(peer
->flags
,
5099 PEER_FLAG_ROLE_STRICT_MODE
) &&
5101 SET_FLAG(peer
->flags
,
5102 PEER_FLAG_ROLE_STRICT_MODE
);
5103 /* Restart session to throw Role Mismatch
5106 if (peer
->remote_role
== ROLE_UNDEFINED
)
5107 bgp_session_reset(peer
);
5110 peer
->local_role
= role
;
5112 SET_FLAG(peer
->flags
,
5113 PEER_FLAG_ROLE_STRICT_MODE
);
5115 UNSET_FLAG(peer
->flags
,
5116 PEER_FLAG_ROLE_STRICT_MODE
);
5117 bgp_session_reset(peer
);
5123 peer
->local_role
= role
;
5124 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5125 if (member
->sort
!= BGP_PEER_EBGP
)
5126 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5128 if (member
->local_role
== role
) {
5129 if (CHECK_FLAG(member
->flags
,
5130 PEER_FLAG_ROLE_STRICT_MODE
) &&
5132 /* TODO: Is session restart needed if it was
5135 UNSET_FLAG(member
->flags
,
5136 PEER_FLAG_ROLE_STRICT_MODE
);
5137 if (!CHECK_FLAG(member
->flags
,
5138 PEER_FLAG_ROLE_STRICT_MODE
) &&
5140 SET_FLAG(peer
->flags
,
5141 PEER_FLAG_ROLE_STRICT_MODE
);
5142 SET_FLAG(member
->flags
,
5143 PEER_FLAG_ROLE_STRICT_MODE
);
5144 /* Restart session to throw Role Mismatch
5147 if (member
->remote_role
== ROLE_UNDEFINED
)
5148 bgp_session_reset(member
);
5151 member
->local_role
= role
;
5154 SET_FLAG(peer
->flags
,
5155 PEER_FLAG_ROLE_STRICT_MODE
);
5156 SET_FLAG(member
->flags
,
5157 PEER_FLAG_ROLE_STRICT_MODE
);
5159 UNSET_FLAG(member
->flags
,
5160 PEER_FLAG_ROLE_STRICT_MODE
);
5162 bgp_session_reset(member
);
5169 int peer_role_unset(struct peer
*peer
)
5171 struct peer
*member
;
5172 struct listnode
*node
, *nnode
;
5174 peer_flag_unset(peer
, PEER_FLAG_ROLE
);
5176 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5177 return peer_role_set(peer
, ROLE_UNDEFINED
, 0);
5179 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
))
5180 peer_role_set(member
, ROLE_UNDEFINED
, 0);
5185 /* Neighbor description. */
5186 void peer_description_set(struct peer
*peer
, const char *desc
)
5188 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5190 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
5193 void peer_description_unset(struct peer
*peer
)
5195 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5198 /* Neighbor update-source. */
5199 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
5201 struct peer
*member
;
5202 struct listnode
*node
, *nnode
;
5204 /* Set flag and configuration on peer. */
5205 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5206 if (peer
->update_if
) {
5207 if (strcmp(peer
->update_if
, ifname
) == 0)
5209 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5211 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5212 sockunion_free(peer
->update_source
);
5213 peer
->update_source
= NULL
;
5215 /* Check if handling a regular peer. */
5216 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5217 /* Send notification or reset peer depending on state. */
5218 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5219 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5220 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5221 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5223 bgp_session_reset(peer
);
5225 /* Apply new source configuration to BFD session. */
5226 if (peer
->bfd_config
)
5227 bgp_peer_bfd_update_source(peer
);
5229 /* Skip peer-group mechanics for regular peers. */
5234 * Set flag and configuration on all peer-group members, unless they are
5235 * explicitly overriding peer-group configuration.
5237 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5238 /* Skip peers with overridden configuration. */
5239 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5242 /* Skip peers with the same configuration. */
5243 if (member
->update_if
) {
5244 if (strcmp(member
->update_if
, ifname
) == 0)
5246 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5249 /* Set flag and configuration on peer-group member. */
5250 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5251 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5252 sockunion_free(member
->update_source
);
5253 member
->update_source
= NULL
;
5255 /* Send notification or reset peer depending on state. */
5256 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5257 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5258 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5259 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5261 bgp_session_reset(member
);
5263 /* Apply new source configuration to BFD session. */
5264 if (member
->bfd_config
)
5265 bgp_peer_bfd_update_source(member
);
5271 void peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
5273 struct peer
*member
;
5274 struct listnode
*node
, *nnode
;
5276 /* Set flag and configuration on peer. */
5277 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5278 if (peer
->update_source
) {
5279 if (sockunion_cmp(peer
->update_source
, su
) == 0)
5281 sockunion_free(peer
->update_source
);
5283 peer
->update_source
= sockunion_dup(su
);
5284 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5286 /* Check if handling a regular peer. */
5287 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5288 /* Send notification or reset peer depending on state. */
5289 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5290 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5291 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5292 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5294 bgp_session_reset(peer
);
5296 /* Apply new source configuration to BFD session. */
5297 if (peer
->bfd_config
)
5298 bgp_peer_bfd_update_source(peer
);
5300 /* Skip peer-group mechanics for regular peers. */
5305 * Set flag and configuration on all peer-group members, unless they are
5306 * explicitly overriding peer-group configuration.
5308 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5309 /* Skip peers with overridden configuration. */
5310 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5313 /* Skip peers with the same configuration. */
5314 if (member
->update_source
) {
5315 if (sockunion_cmp(member
->update_source
, su
) == 0)
5317 sockunion_free(member
->update_source
);
5320 /* Set flag and configuration on peer-group member. */
5321 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5322 member
->update_source
= sockunion_dup(su
);
5323 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5325 /* Send notification or reset peer depending on state. */
5326 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5327 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5328 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5329 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5331 bgp_session_reset(member
);
5333 /* Apply new source configuration to BFD session. */
5334 if (member
->bfd_config
)
5335 bgp_peer_bfd_update_source(member
);
5339 void peer_update_source_unset(struct peer
*peer
)
5341 struct peer
*member
;
5342 struct listnode
*node
, *nnode
;
5344 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
5347 /* Inherit configuration from peer-group if peer is member. */
5348 if (peer_group_active(peer
)) {
5349 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
5350 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
5351 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
5352 MTYPE_PEER_UPDATE_SOURCE
);
5354 /* Otherwise remove flag and configuration from peer. */
5355 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
5356 sockunion_free(peer
->update_source
);
5357 peer
->update_source
= NULL
;
5358 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5361 /* Check if handling a regular peer. */
5362 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5363 /* Send notification or reset peer depending on state. */
5364 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5365 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5366 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5367 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5369 bgp_session_reset(peer
);
5371 /* Apply new source configuration to BFD session. */
5372 if (peer
->bfd_config
)
5373 bgp_peer_bfd_update_source(peer
);
5375 /* Skip peer-group mechanics for regular peers. */
5380 * Set flag and configuration on all peer-group members, unless they are
5381 * explicitly overriding peer-group configuration.
5383 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5384 /* Skip peers with overridden configuration. */
5385 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5388 /* Skip peers with the same configuration. */
5389 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5390 && !member
->update_source
&& !member
->update_if
)
5393 /* Remove flag and configuration on peer-group member. */
5394 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5395 sockunion_free(member
->update_source
);
5396 member
->update_source
= NULL
;
5397 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5399 /* Send notification or reset peer depending on state. */
5400 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5401 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5402 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5403 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5405 bgp_session_reset(member
);
5407 /* Apply new source configuration to BFD session. */
5408 if (member
->bfd_config
)
5409 bgp_peer_bfd_update_source(member
);
5413 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5414 const char *rmap
, struct route_map
*route_map
)
5416 struct peer
*member
;
5417 struct listnode
*node
, *nnode
;
5418 struct update_subgroup
*subgrp
;
5420 /* Set flag and configuration on peer. */
5421 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5423 subgrp
= peer_subgroup(peer
, afi
, safi
);
5426 if (!peer
->default_rmap
[afi
][safi
].name
5427 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5428 if (peer
->default_rmap
[afi
][safi
].name
)
5429 XFREE(MTYPE_ROUTE_MAP_NAME
,
5430 peer
->default_rmap
[afi
][safi
].name
);
5433 * When there is a change in route-map policy,
5434 * this flow gets triggered. Since, the default
5435 * route is already originated, the flag is set.
5436 * The flag should be unset here,
5437 * to trigger the flow of sending update message.
5440 UNSET_FLAG(subgrp
->sflags
,
5441 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5443 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5444 peer
->default_rmap
[afi
][safi
].name
=
5445 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5446 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5447 route_map_counter_increment(route_map
);
5450 if (peer
->default_rmap
[afi
][safi
].name
)
5451 XFREE(MTYPE_ROUTE_MAP_NAME
,
5452 peer
->default_rmap
[afi
][safi
].name
);
5455 * This is triggered in case of route-map deletion.
5456 * The flag needs to be unset, to trigger the flow
5457 * of sending an update message.
5460 UNSET_FLAG(subgrp
->sflags
,
5461 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5463 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5464 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5465 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5468 /* Check if handling a regular peer. */
5469 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5470 /* Update peer route announcements. */
5471 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5472 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5473 bgp_default_originate(peer
, afi
, safi
, 0);
5474 bgp_announce_route(peer
, afi
, safi
, false);
5477 /* Skip peer-group mechanics for regular peers. */
5482 * Set flag and configuration on all peer-group members, unless they are
5483 * explicitly overriding peer-group configuration.
5485 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5486 /* Skip peers with overridden configuration. */
5487 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5488 PEER_FLAG_DEFAULT_ORIGINATE
))
5491 /* Set flag and configuration on peer-group member. */
5492 SET_FLAG(member
->af_flags
[afi
][safi
],
5493 PEER_FLAG_DEFAULT_ORIGINATE
);
5495 if (member
->default_rmap
[afi
][safi
].name
)
5496 XFREE(MTYPE_ROUTE_MAP_NAME
,
5497 member
->default_rmap
[afi
][safi
].name
);
5498 route_map_counter_decrement(
5499 member
->default_rmap
[afi
][safi
].map
);
5500 member
->default_rmap
[afi
][safi
].name
=
5501 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5502 member
->default_rmap
[afi
][safi
].map
= route_map
;
5503 route_map_counter_increment(route_map
);
5506 /* Update peer route announcements. */
5507 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5508 update_group_adjust_peer(
5509 peer_af_find(member
, afi
, safi
));
5510 bgp_default_originate(member
, afi
, safi
, 0);
5511 bgp_announce_route(member
, afi
, safi
, false);
5518 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5520 struct peer
*member
;
5521 struct listnode
*node
, *nnode
;
5523 /* Inherit configuration from peer-group if peer is member. */
5524 if (peer_group_active(peer
)) {
5525 peer_af_flag_inherit(peer
, afi
, safi
,
5526 PEER_FLAG_DEFAULT_ORIGINATE
);
5527 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5528 default_rmap
[afi
][safi
].name
,
5529 MTYPE_ROUTE_MAP_NAME
);
5530 PEER_ATTR_INHERIT(peer
, peer
->group
,
5531 default_rmap
[afi
][safi
].map
);
5533 /* Otherwise remove flag and configuration from peer. */
5534 peer_af_flag_unset(peer
, afi
, safi
,
5535 PEER_FLAG_DEFAULT_ORIGINATE
);
5536 if (peer
->default_rmap
[afi
][safi
].name
)
5537 XFREE(MTYPE_ROUTE_MAP_NAME
,
5538 peer
->default_rmap
[afi
][safi
].name
);
5539 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5540 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5541 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5544 /* Check if handling a regular peer. */
5545 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5546 /* Update peer route announcements. */
5547 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5548 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5549 bgp_default_originate(peer
, afi
, safi
, 1);
5550 bgp_announce_route(peer
, afi
, safi
, false);
5553 /* Skip peer-group mechanics for regular peers. */
5558 * Remove flag and configuration from all peer-group members, unless
5559 * they are explicitly overriding peer-group configuration.
5561 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5562 /* Skip peers with overridden configuration. */
5563 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5564 PEER_FLAG_DEFAULT_ORIGINATE
))
5567 /* Remove flag and configuration on peer-group member. */
5568 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5569 PEER_FLAG_DEFAULT_ORIGINATE
);
5570 if (member
->default_rmap
[afi
][safi
].name
)
5571 XFREE(MTYPE_ROUTE_MAP_NAME
,
5572 member
->default_rmap
[afi
][safi
].name
);
5573 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5574 member
->default_rmap
[afi
][safi
].name
= NULL
;
5575 member
->default_rmap
[afi
][safi
].map
= NULL
;
5577 /* Update peer route announcements. */
5578 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5579 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5580 bgp_default_originate(member
, afi
, safi
, 1);
5581 bgp_announce_route(member
, afi
, safi
, false);
5588 void peer_port_set(struct peer
*peer
, uint16_t port
)
5591 peer_flag_set(peer
, PEER_FLAG_PORT
);
5594 void peer_port_unset(struct peer
*peer
)
5596 peer
->port
= BGP_PORT_DEFAULT
;
5597 peer_flag_unset(peer
, PEER_FLAG_PORT
);
5600 /* Set the TCP-MSS value in the peer structure,
5601 * This gets applied only after connection reset
5602 * So this value will be used in bgp_connect.
5604 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5606 peer
->tcp_mss
= tcp_mss
;
5607 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5610 /* Reset the TCP-MSS value in the peer structure,
5611 * This gets applied only after connection reset
5612 * So this value will be used in bgp_connect.
5614 void peer_tcp_mss_unset(struct peer
*peer
)
5616 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5621 * Helper function that is called after the name of the policy
5622 * being used by a peer has changed (AF specific). Automatically
5623 * initiates inbound or outbound processing as needed.
5625 void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5629 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5630 if (peer_established(peer
))
5631 bgp_announce_route(peer
, afi
, safi
, false);
5633 if (!peer_established(peer
))
5636 if (bgp_soft_reconfig_in(peer
, afi
, safi
))
5639 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
) ||
5640 CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
)) {
5641 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5642 PEER_CAP_ORF_PREFIX_SM_ADV
) &&
5643 (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5644 PEER_CAP_ORF_PREFIX_RM_RCV
) ||
5645 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5646 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
)))
5647 peer_clear_soft(peer
, afi
, safi
,
5648 BGP_CLEAR_SOFT_IN_ORF_PREFIX
);
5650 bgp_route_refresh_send(
5651 peer
, afi
, safi
, 0, 0, 0,
5652 BGP_ROUTE_REFRESH_NORMAL
);
5658 /* neighbor weight. */
5659 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5661 struct peer
*member
;
5662 struct listnode
*node
, *nnode
;
5664 /* Set flag and configuration on peer. */
5665 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5666 if (peer
->weight
[afi
][safi
] != weight
) {
5667 peer
->weight
[afi
][safi
] = weight
;
5668 peer_on_policy_change(peer
, afi
, safi
, 0);
5671 /* Skip peer-group mechanics for regular peers. */
5672 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5676 * Set flag and configuration on all peer-group members, unless they are
5677 * explicitly overriding peer-group configuration.
5679 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5680 /* Skip peers with overridden configuration. */
5681 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5685 /* Set flag and configuration on peer-group member. */
5686 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5687 if (member
->weight
[afi
][safi
] != weight
) {
5688 member
->weight
[afi
][safi
] = weight
;
5689 peer_on_policy_change(member
, afi
, safi
, 0);
5696 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5698 struct peer
*member
;
5699 struct listnode
*node
, *nnode
;
5701 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5704 /* Inherit configuration from peer-group if peer is member. */
5705 if (peer_group_active(peer
)) {
5706 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5707 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5709 peer_on_policy_change(peer
, afi
, safi
, 0);
5713 /* Remove flag and configuration from peer. */
5714 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5715 peer
->weight
[afi
][safi
] = 0;
5716 peer_on_policy_change(peer
, afi
, safi
, 0);
5718 /* Skip peer-group mechanics for regular peers. */
5719 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5723 * Remove flag and configuration from all peer-group members, unless
5724 * they are explicitly overriding peer-group configuration.
5726 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5727 /* Skip peers with overridden configuration. */
5728 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5732 /* Skip peers where flag is already disabled. */
5733 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5736 /* Remove flag and configuration on peer-group member. */
5737 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5738 member
->weight
[afi
][safi
] = 0;
5739 peer_on_policy_change(member
, afi
, safi
, 0);
5745 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5747 struct peer
*member
;
5748 struct listnode
*node
, *nnode
;
5750 if (keepalive
> UINT16_MAX
)
5751 return BGP_ERR_INVALID_VALUE
;
5753 if (holdtime
> UINT16_MAX
)
5754 return BGP_ERR_INVALID_VALUE
;
5756 if (holdtime
< 3 && holdtime
!= 0)
5757 return BGP_ERR_INVALID_VALUE
;
5759 /* Set flag and configuration on peer. */
5760 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5761 peer
->holdtime
= holdtime
;
5762 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5764 /* Skip peer-group mechanics for regular peers. */
5765 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5769 * Set flag and configuration on all peer-group members, unless they are
5770 * explicitly overriding peer-group configuration.
5772 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5773 /* Skip peers with overridden configuration. */
5774 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5777 /* Set flag and configuration on peer-group member. */
5778 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5779 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5780 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5786 int peer_timers_unset(struct peer
*peer
)
5788 struct peer
*member
;
5789 struct listnode
*node
, *nnode
;
5791 /* Inherit configuration from peer-group if peer is member. */
5792 if (peer_group_active(peer
)) {
5793 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5794 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5795 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5797 /* Otherwise remove flag and configuration from peer. */
5798 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5800 peer
->keepalive
= 0;
5803 /* Skip peer-group mechanics for regular peers. */
5804 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5808 * Remove flag and configuration from all peer-group members, unless
5809 * they are explicitly overriding peer-group configuration.
5811 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5812 /* Skip peers with overridden configuration. */
5813 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5816 /* Remove flag and configuration on peer-group member. */
5817 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5818 member
->holdtime
= 0;
5819 member
->keepalive
= 0;
5825 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5827 struct peer
*member
;
5828 struct listnode
*node
, *nnode
;
5830 if (connect
> UINT16_MAX
)
5831 return BGP_ERR_INVALID_VALUE
;
5833 /* Set flag and configuration on peer. */
5834 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5835 peer
->connect
= connect
;
5836 peer
->v_connect
= connect
;
5838 /* Skip peer-group mechanics for regular peers. */
5839 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5840 if (!peer_established(peer
)) {
5841 if (peer_active(peer
))
5842 BGP_EVENT_ADD(peer
, BGP_Stop
);
5843 BGP_EVENT_ADD(peer
, BGP_Start
);
5848 * Set flag and configuration on all peer-group members, unless they are
5849 * explicitly overriding peer-group configuration.
5851 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5852 /* Skip peers with overridden configuration. */
5853 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5856 /* Set flag and configuration on peer-group member. */
5857 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5858 member
->connect
= connect
;
5859 member
->v_connect
= connect
;
5861 if (!peer_established(member
)) {
5862 if (peer_active(member
))
5863 BGP_EVENT_ADD(member
, BGP_Stop
);
5864 BGP_EVENT_ADD(member
, BGP_Start
);
5871 int peer_timers_connect_unset(struct peer
*peer
)
5873 struct peer
*member
;
5874 struct listnode
*node
, *nnode
;
5876 /* Inherit configuration from peer-group if peer is member. */
5877 if (peer_group_active(peer
)) {
5878 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5879 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5881 /* Otherwise remove flag and configuration from peer. */
5882 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5886 /* Set timer with fallback to default value. */
5888 peer
->v_connect
= peer
->connect
;
5890 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5892 /* Skip peer-group mechanics for regular peers. */
5893 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5894 if (!peer_established(peer
)) {
5895 if (peer_active(peer
))
5896 BGP_EVENT_ADD(peer
, BGP_Stop
);
5897 BGP_EVENT_ADD(peer
, BGP_Start
);
5902 * Remove flag and configuration from all peer-group members, unless
5903 * they are explicitly overriding peer-group configuration.
5905 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5906 /* Skip peers with overridden configuration. */
5907 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5910 /* Remove flag and configuration on peer-group member. */
5911 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5912 member
->connect
= 0;
5913 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5915 if (!peer_established(member
)) {
5916 if (peer_active(member
))
5917 BGP_EVENT_ADD(member
, BGP_Stop
);
5918 BGP_EVENT_ADD(member
, BGP_Start
);
5925 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5927 struct peer
*member
;
5928 struct listnode
*node
, *nnode
;
5931 return BGP_ERR_INVALID_VALUE
;
5933 /* Set flag and configuration on peer. */
5934 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5935 peer
->routeadv
= routeadv
;
5936 peer
->v_routeadv
= routeadv
;
5938 /* Check if handling a regular peer. */
5939 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5940 /* Update peer route announcements. */
5941 update_group_adjust_peer_afs(peer
);
5942 if (peer_established(peer
))
5943 bgp_announce_route_all(peer
);
5945 /* Skip peer-group mechanics for regular peers. */
5950 * Set flag and configuration on all peer-group members, unless they are
5951 * explicitly overriding peer-group configuration.
5953 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5954 /* Skip peers with overridden configuration. */
5955 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5958 /* Set flag and configuration on peer-group member. */
5959 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5960 member
->routeadv
= routeadv
;
5961 member
->v_routeadv
= routeadv
;
5963 /* Update peer route announcements. */
5964 update_group_adjust_peer_afs(member
);
5965 if (peer_established(member
))
5966 bgp_announce_route_all(member
);
5972 int peer_advertise_interval_unset(struct peer
*peer
)
5974 struct peer
*member
;
5975 struct listnode
*node
, *nnode
;
5977 /* Inherit configuration from peer-group if peer is member. */
5978 if (peer_group_active(peer
)) {
5979 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5980 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5982 /* Otherwise remove flag and configuration from peer. */
5983 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5987 /* Set timer with fallback to default value. */
5989 peer
->v_routeadv
= peer
->routeadv
;
5991 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5992 ? BGP_DEFAULT_IBGP_ROUTEADV
5993 : BGP_DEFAULT_EBGP_ROUTEADV
;
5995 /* Check if handling a regular peer. */
5996 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5997 /* Update peer route announcements. */
5998 update_group_adjust_peer_afs(peer
);
5999 if (peer_established(peer
))
6000 bgp_announce_route_all(peer
);
6002 /* Skip peer-group mechanics for regular peers. */
6007 * Remove flag and configuration from all peer-group members, unless
6008 * they are explicitly overriding peer-group configuration.
6010 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6011 /* Skip peers with overridden configuration. */
6012 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
6015 /* Remove flag and configuration on peer-group member. */
6016 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
6017 member
->routeadv
= 0;
6018 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
6019 ? BGP_DEFAULT_IBGP_ROUTEADV
6020 : BGP_DEFAULT_EBGP_ROUTEADV
;
6022 /* Update peer route announcements. */
6023 update_group_adjust_peer_afs(member
);
6024 if (peer_established(member
))
6025 bgp_announce_route_all(member
);
6031 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
6034 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
6036 struct peer
*member
;
6037 struct listnode
*node
;
6039 /* Set peers session attribute flag and timer interval. */
6040 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6041 peer
->delayopen
= delayopen
;
6042 peer
->v_delayopen
= delayopen
;
6044 /* Skip group mechanics for regular peers. */
6045 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6048 /* Set flag and configuration on all peer-group members, unless they are
6049 * explicitly overriding peer-group configuration.
6051 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6052 /* Skip peers with overridden configuration. */
6053 if (CHECK_FLAG(member
->flags_override
,
6054 PEER_FLAG_TIMER_DELAYOPEN
))
6057 /* Set session attribute flag and timer intervals on peer-group
6060 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6061 member
->delayopen
= delayopen
;
6062 member
->v_delayopen
= delayopen
;
6068 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
6069 * DelayOpenTimer interval to the default value.
6071 int peer_timers_delayopen_unset(struct peer
*peer
)
6073 struct peer
*member
;
6074 struct listnode
*node
;
6076 /* Inherit configuration from peer-group if peer is member. */
6077 if (peer_group_active(peer
)) {
6078 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6079 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
6081 /* Otherwise remove session attribute flag and set timer
6082 * interval to default value.
6084 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6085 peer
->delayopen
= peer
->bgp
->default_delayopen
;
6088 /* Set timer value to zero */
6089 peer
->v_delayopen
= 0;
6091 /* Skip peer-group mechanics for regular peers. */
6092 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6095 /* Remove flag and configuration from all peer-group members, unless
6096 * they are explicitly overriding peer-group configuration.
6098 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6099 /* Skip peers with overridden configuration. */
6100 if (CHECK_FLAG(member
->flags_override
,
6101 PEER_FLAG_TIMER_DELAYOPEN
))
6104 /* Remove session attribute flag, reset the timer interval to
6105 * the default value and set the timer value to zero.
6107 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6108 member
->delayopen
= peer
->bgp
->default_delayopen
;
6109 member
->v_delayopen
= 0;
6115 /* neighbor interface */
6116 void peer_interface_set(struct peer
*peer
, const char *str
)
6118 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6119 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
6122 void peer_interface_unset(struct peer
*peer
)
6124 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6128 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6129 int allow_num
, int origin
)
6131 struct peer
*member
;
6132 struct listnode
*node
, *nnode
;
6134 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
6135 return BGP_ERR_INVALID_VALUE
;
6137 /* Set flag and configuration on peer. */
6138 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6140 if (peer
->allowas_in
[afi
][safi
] != 0
6141 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6142 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6143 peer_af_flag_set(peer
, afi
, safi
,
6144 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6145 peer
->allowas_in
[afi
][safi
] = 0;
6146 peer_on_policy_change(peer
, afi
, safi
, 0);
6149 if (peer
->allowas_in
[afi
][safi
] != allow_num
6150 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6151 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6153 peer_af_flag_unset(peer
, afi
, safi
,
6154 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6155 peer
->allowas_in
[afi
][safi
] = allow_num
;
6156 peer_on_policy_change(peer
, afi
, safi
, 0);
6160 /* Skip peer-group mechanics for regular peers. */
6161 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6165 * Set flag and configuration on all peer-group members, unless
6166 * they are explicitly overriding peer-group configuration.
6168 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6169 /* Skip peers with overridden configuration. */
6170 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6171 PEER_FLAG_ALLOWAS_IN
))
6174 /* Set flag and configuration on peer-group member. */
6175 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6177 if (member
->allowas_in
[afi
][safi
] != 0
6178 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
6179 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6180 SET_FLAG(member
->af_flags
[afi
][safi
],
6181 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6182 member
->allowas_in
[afi
][safi
] = 0;
6183 peer_on_policy_change(peer
, afi
, safi
, 0);
6186 if (member
->allowas_in
[afi
][safi
] != allow_num
6187 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
6188 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6189 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6190 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6191 member
->allowas_in
[afi
][safi
] = allow_num
;
6192 peer_on_policy_change(peer
, afi
, safi
, 0);
6200 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6202 struct peer
*member
;
6203 struct listnode
*node
, *nnode
;
6205 /* Skip peer if flag is already disabled. */
6206 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
6209 /* Inherit configuration from peer-group if peer is member. */
6210 if (peer_group_active(peer
)) {
6211 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6212 peer_af_flag_inherit(peer
, afi
, safi
,
6213 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6214 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
6215 peer_on_policy_change(peer
, afi
, safi
, 0);
6220 /* Remove flag and configuration from peer. */
6221 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6222 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6223 peer
->allowas_in
[afi
][safi
] = 0;
6224 peer_on_policy_change(peer
, afi
, safi
, 0);
6226 /* Skip peer-group mechanics if handling a regular peer. */
6227 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6231 * Remove flags and configuration from all peer-group members, unless
6232 * they are explicitly overriding peer-group configuration.
6234 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6235 /* Skip peers with overridden configuration. */
6236 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6237 PEER_FLAG_ALLOWAS_IN
))
6240 /* Remove flags and configuration on peer-group member. */
6241 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6242 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6243 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6244 member
->allowas_in
[afi
][safi
] = 0;
6245 peer_on_policy_change(member
, afi
, safi
, 0);
6251 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
6252 bool replace_as
, const char *as_str
)
6254 bool old_no_prepend
, old_replace_as
;
6255 struct bgp
*bgp
= peer
->bgp
;
6256 struct peer
*member
;
6257 struct listnode
*node
, *nnode
;
6260 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
6262 /* Save previous flag states. */
6264 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6266 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6268 /* Set flag and configuration on peer. */
6269 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
6270 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
6271 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
6273 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
6274 && old_replace_as
== replace_as
)
6276 peer
->change_local_as
= as
;
6278 peer
->change_local_as_pretty
= XSTRDUP(MTYPE_BGP
, as_str
);
6280 (void)peer_sort(peer
);
6282 /* Check if handling a regular peer. */
6283 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6287 * Set flag and configuration on all peer-group members, unless they are
6288 * explicitly overriding peer-group configuration.
6290 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6291 /* Skip peers with overridden configuration. */
6292 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6295 /* Skip peers with the same configuration. */
6296 old_no_prepend
= CHECK_FLAG(member
->flags
,
6297 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6298 old_replace_as
= CHECK_FLAG(member
->flags
,
6299 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6300 if (member
->change_local_as
== as
6301 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
6302 && old_no_prepend
== no_prepend
6303 && old_replace_as
== replace_as
)
6306 /* Set flag and configuration on peer-group member. */
6307 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6308 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
6310 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
6312 member
->change_local_as
= as
;
6314 member
->change_local_as_pretty
=
6315 XSTRDUP(MTYPE_BGP
, as_str
);
6321 int peer_local_as_unset(struct peer
*peer
)
6323 struct peer
*member
;
6324 struct listnode
*node
, *nnode
;
6326 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
6329 /* Inherit configuration from peer-group if peer is member. */
6330 if (peer_group_active(peer
)) {
6331 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
6332 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6333 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6334 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
6336 /* Otherwise remove flag and configuration from peer. */
6337 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
6338 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6339 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6340 peer
->change_local_as
= 0;
6341 XFREE(MTYPE_BGP
, peer
->change_local_as_pretty
);
6344 /* Check if handling a regular peer. */
6345 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6346 /* Send notification or stop peer depending on state. */
6347 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6348 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6349 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6350 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6352 BGP_EVENT_ADD(peer
, BGP_Stop
);
6354 /* Skip peer-group mechanics for regular peers. */
6359 * Remove flag and configuration from all peer-group members, unless
6360 * they are explicitly overriding peer-group configuration.
6362 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6363 /* Skip peers with overridden configuration. */
6364 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6367 /* Remove flag and configuration on peer-group member. */
6368 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6369 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6370 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6371 member
->change_local_as
= 0;
6372 XFREE(MTYPE_BGP
, member
->change_local_as_pretty
);
6374 /* Send notification or stop peer depending on state. */
6375 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6376 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6377 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6378 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6380 bgp_session_reset(member
);
6386 /* Set password for authenticating with the peer. */
6387 int peer_password_set(struct peer
*peer
, const char *password
)
6389 struct peer
*member
;
6390 struct listnode
*node
, *nnode
;
6391 int len
= password
? strlen(password
) : 0;
6392 int ret
= BGP_SUCCESS
;
6394 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
6395 return BGP_ERR_INVALID_VALUE
;
6397 /* Set flag and configuration on peer. */
6398 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
6399 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
6401 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6402 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6404 /* Check if handling a regular peer. */
6405 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6406 /* Send notification or reset peer depending on state. */
6407 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6408 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6409 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6411 bgp_session_reset(peer
);
6414 * Attempt to install password on socket and skip peer-group
6417 if (BGP_PEER_SU_UNSPEC(peer
))
6419 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6420 : BGP_ERR_TCPSIG_FAILED
;
6424 * Set flag and configuration on all peer-group members, unless they are
6425 * explicitly overriding peer-group configuration.
6427 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6428 /* Skip peers with overridden configuration. */
6429 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6432 /* Skip peers with the same password. */
6433 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6436 /* Set flag and configuration on peer-group member. */
6437 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6438 if (member
->password
)
6439 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6440 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6442 /* Send notification or reset peer depending on state. */
6443 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6444 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6445 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6447 bgp_session_reset(member
);
6449 /* Attempt to install password on socket. */
6450 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6451 ret
= BGP_ERR_TCPSIG_FAILED
;
6454 /* Set flag and configuration on all peer-group listen ranges */
6455 struct listnode
*ln
;
6458 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6459 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6460 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6461 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6466 int peer_password_unset(struct peer
*peer
)
6468 struct peer
*member
;
6469 struct listnode
*node
, *nnode
;
6471 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6474 /* Inherit configuration from peer-group if peer is member. */
6475 if (peer_group_active(peer
)) {
6476 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6477 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6478 MTYPE_PEER_PASSWORD
);
6480 /* Otherwise remove flag and configuration from peer. */
6481 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6482 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6485 /* Check if handling a regular peer. */
6486 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6487 /* Send notification or reset peer depending on state. */
6488 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6489 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6490 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6492 bgp_session_reset(peer
);
6494 /* Attempt to uninstall password on socket. */
6495 if (!BGP_PEER_SU_UNSPEC(peer
))
6496 bgp_md5_unset(peer
);
6497 /* Skip peer-group mechanics for regular peers. */
6502 * Remove flag and configuration from all peer-group members, unless
6503 * they are explicitly overriding peer-group configuration.
6505 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6506 /* Skip peers with overridden configuration. */
6507 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6510 /* Remove flag and configuration on peer-group member. */
6511 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6512 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6514 /* Send notification or reset peer depending on state. */
6515 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6516 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6517 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6519 bgp_session_reset(member
);
6521 /* Attempt to uninstall password on socket. */
6522 if (!BGP_PEER_SU_UNSPEC(member
))
6523 bgp_md5_unset(member
);
6526 /* Set flag and configuration on all peer-group listen ranges */
6527 struct listnode
*ln
;
6530 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6531 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6532 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6533 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6539 /* Set distribute list to the peer. */
6540 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6543 struct peer
*member
;
6544 struct bgp_filter
*filter
;
6545 struct listnode
*node
, *nnode
;
6547 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6548 return BGP_ERR_INVALID_VALUE
;
6550 /* Set configuration on peer. */
6551 filter
= &peer
->filter
[afi
][safi
];
6552 if (filter
->plist
[direct
].name
)
6553 return BGP_ERR_PEER_FILTER_CONFLICT
;
6554 if (filter
->dlist
[direct
].name
)
6555 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6556 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6557 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6559 /* Check if handling a regular peer. */
6560 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6561 /* Set override-flag and process peer route updates. */
6562 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6563 PEER_FT_DISTRIBUTE_LIST
);
6564 peer_on_policy_change(peer
, afi
, safi
,
6565 (direct
== FILTER_OUT
) ? 1 : 0);
6567 /* Skip peer-group mechanics for regular peers. */
6572 * Set configuration on all peer-group members, un less they are
6573 * explicitly overriding peer-group configuration.
6575 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6576 /* Skip peers with overridden configuration. */
6577 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6578 PEER_FT_DISTRIBUTE_LIST
))
6581 /* Set configuration on peer-group member. */
6582 filter
= &member
->filter
[afi
][safi
];
6583 if (filter
->dlist
[direct
].name
)
6584 XFREE(MTYPE_BGP_FILTER_NAME
,
6585 filter
->dlist
[direct
].name
);
6586 filter
->dlist
[direct
].name
=
6587 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6588 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6590 /* Process peer route updates. */
6591 peer_on_policy_change(member
, afi
, safi
,
6592 (direct
== FILTER_OUT
) ? 1 : 0);
6598 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6600 struct peer
*member
;
6601 struct bgp_filter
*filter
;
6602 struct listnode
*node
, *nnode
;
6604 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6605 return BGP_ERR_INVALID_VALUE
;
6607 /* Unset override-flag unconditionally. */
6608 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6609 PEER_FT_DISTRIBUTE_LIST
);
6611 /* Inherit configuration from peer-group if peer is member. */
6612 if (peer_group_active(peer
)) {
6613 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6614 filter
[afi
][safi
].dlist
[direct
].name
,
6615 MTYPE_BGP_FILTER_NAME
);
6616 PEER_ATTR_INHERIT(peer
, peer
->group
,
6617 filter
[afi
][safi
].dlist
[direct
].alist
);
6619 /* Otherwise remove configuration from peer. */
6620 filter
= &peer
->filter
[afi
][safi
];
6621 if (filter
->dlist
[direct
].name
)
6622 XFREE(MTYPE_BGP_FILTER_NAME
,
6623 filter
->dlist
[direct
].name
);
6624 filter
->dlist
[direct
].name
= NULL
;
6625 filter
->dlist
[direct
].alist
= NULL
;
6628 /* Check if handling a regular peer. */
6629 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6630 /* Process peer route updates. */
6631 peer_on_policy_change(peer
, afi
, safi
,
6632 (direct
== FILTER_OUT
) ? 1 : 0);
6634 /* Skip peer-group mechanics for regular peers. */
6639 * Remove configuration on all peer-group members, unless they are
6640 * explicitly overriding peer-group configuration.
6642 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6643 /* Skip peers with overridden configuration. */
6644 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6645 PEER_FT_DISTRIBUTE_LIST
))
6648 /* Remove configuration on peer-group member. */
6649 filter
= &member
->filter
[afi
][safi
];
6650 if (filter
->dlist
[direct
].name
)
6651 XFREE(MTYPE_BGP_FILTER_NAME
,
6652 filter
->dlist
[direct
].name
);
6653 filter
->dlist
[direct
].name
= NULL
;
6654 filter
->dlist
[direct
].alist
= NULL
;
6656 /* Process peer route updates. */
6657 peer_on_policy_change(member
, afi
, safi
,
6658 (direct
== FILTER_OUT
) ? 1 : 0);
6664 /* Update distribute list. */
6665 static void peer_distribute_update(struct access_list
*access
)
6670 struct listnode
*mnode
, *mnnode
;
6671 struct listnode
*node
, *nnode
;
6674 struct peer_group
*group
;
6675 struct bgp_filter
*filter
;
6677 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6679 update_group_policy_update(bgp
,
6680 BGP_POLICY_DISTRIBUTE_LIST
,
6681 access
->name
, true, 0);
6682 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6683 FOREACH_AFI_SAFI (afi
, safi
) {
6684 filter
= &peer
->filter
[afi
][safi
];
6686 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6688 if (filter
->dlist
[direct
].name
)
6689 filter
->dlist
[direct
]
6690 .alist
= access_list_lookup(
6692 filter
->dlist
[direct
]
6695 filter
->dlist
[direct
].alist
=
6700 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6701 FOREACH_AFI_SAFI (afi
, safi
) {
6702 filter
= &group
->conf
->filter
[afi
][safi
];
6704 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6706 if (filter
->dlist
[direct
].name
)
6707 filter
->dlist
[direct
]
6708 .alist
= access_list_lookup(
6710 filter
->dlist
[direct
]
6713 filter
->dlist
[direct
].alist
=
6718 #ifdef ENABLE_BGP_VNC
6719 vnc_prefix_list_update(bgp
);
6724 /* Set prefix list to the peer. */
6725 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6728 struct peer
*member
;
6729 struct bgp_filter
*filter
;
6730 struct listnode
*node
, *nnode
;
6732 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6733 return BGP_ERR_INVALID_VALUE
;
6735 /* Set configuration on peer. */
6736 filter
= &peer
->filter
[afi
][safi
];
6737 if (filter
->dlist
[direct
].name
)
6738 return BGP_ERR_PEER_FILTER_CONFLICT
;
6739 if (filter
->plist
[direct
].name
)
6740 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6741 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6742 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6744 /* Check if handling a regular peer. */
6745 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6746 /* Set override-flag and process peer route updates. */
6747 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6748 PEER_FT_PREFIX_LIST
);
6749 peer_on_policy_change(peer
, afi
, safi
,
6750 (direct
== FILTER_OUT
) ? 1 : 0);
6752 /* Skip peer-group mechanics for regular peers. */
6757 * Set configuration on all peer-group members, unless they are
6758 * explicitly overriding peer-group configuration.
6760 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6761 /* Skip peers with overridden configuration. */
6762 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6763 PEER_FT_PREFIX_LIST
))
6766 /* Set configuration on peer-group member. */
6767 filter
= &member
->filter
[afi
][safi
];
6768 if (filter
->plist
[direct
].name
)
6769 XFREE(MTYPE_BGP_FILTER_NAME
,
6770 filter
->plist
[direct
].name
);
6771 filter
->plist
[direct
].name
=
6772 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6773 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6775 /* Process peer route updates. */
6776 peer_on_policy_change(member
, afi
, safi
,
6777 (direct
== FILTER_OUT
) ? 1 : 0);
6783 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6786 struct peer
*member
;
6787 struct bgp_filter
*filter
;
6788 struct listnode
*node
, *nnode
;
6790 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6791 return BGP_ERR_INVALID_VALUE
;
6793 /* Unset override-flag unconditionally. */
6794 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6795 PEER_FT_PREFIX_LIST
);
6797 /* Inherit configuration from peer-group if peer is member. */
6798 if (peer_group_active(peer
)) {
6799 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6800 filter
[afi
][safi
].plist
[direct
].name
,
6801 MTYPE_BGP_FILTER_NAME
);
6802 PEER_ATTR_INHERIT(peer
, peer
->group
,
6803 filter
[afi
][safi
].plist
[direct
].plist
);
6805 /* Otherwise remove configuration from peer. */
6806 filter
= &peer
->filter
[afi
][safi
];
6807 if (filter
->plist
[direct
].name
)
6808 XFREE(MTYPE_BGP_FILTER_NAME
,
6809 filter
->plist
[direct
].name
);
6810 filter
->plist
[direct
].name
= NULL
;
6811 filter
->plist
[direct
].plist
= NULL
;
6814 /* Check if handling a regular peer. */
6815 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6816 /* Process peer route updates. */
6817 peer_on_policy_change(peer
, afi
, safi
,
6818 (direct
== FILTER_OUT
) ? 1 : 0);
6820 /* Skip peer-group mechanics for regular peers. */
6825 * Remove configuration on all peer-group members, unless they are
6826 * explicitly overriding peer-group configuration.
6828 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6829 /* Skip peers with overridden configuration. */
6830 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6831 PEER_FT_PREFIX_LIST
))
6834 /* Remove configuration on peer-group member. */
6835 filter
= &member
->filter
[afi
][safi
];
6836 if (filter
->plist
[direct
].name
)
6837 XFREE(MTYPE_BGP_FILTER_NAME
,
6838 filter
->plist
[direct
].name
);
6839 filter
->plist
[direct
].name
= NULL
;
6840 filter
->plist
[direct
].plist
= NULL
;
6842 /* Process peer route updates. */
6843 peer_on_policy_change(member
, afi
, safi
,
6844 (direct
== FILTER_OUT
) ? 1 : 0);
6850 /* Update prefix-list list. */
6851 static void peer_prefix_list_update(struct prefix_list
*plist
)
6853 struct listnode
*mnode
, *mnnode
;
6854 struct listnode
*node
, *nnode
;
6857 struct peer_group
*group
;
6858 struct bgp_filter
*filter
;
6863 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6866 * Update the prefix-list on update groups.
6868 update_group_policy_update(
6869 bgp
, BGP_POLICY_PREFIX_LIST
,
6870 plist
? prefix_list_name(plist
) : NULL
, true, 0);
6872 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6873 FOREACH_AFI_SAFI (afi
, safi
) {
6874 filter
= &peer
->filter
[afi
][safi
];
6876 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6878 if (filter
->plist
[direct
].name
)
6879 filter
->plist
[direct
]
6880 .plist
= prefix_list_lookup(
6882 filter
->plist
[direct
]
6885 filter
->plist
[direct
].plist
=
6889 /* If we touch prefix-list, we need to process
6890 * new updates. This is important for ORF to
6891 * work correctly as well.
6893 if (peer
->afc_nego
[afi
][safi
])
6894 peer_on_policy_change(peer
, afi
, safi
,
6898 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6899 FOREACH_AFI_SAFI (afi
, safi
) {
6900 filter
= &group
->conf
->filter
[afi
][safi
];
6902 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6904 if (filter
->plist
[direct
].name
)
6905 filter
->plist
[direct
]
6906 .plist
= prefix_list_lookup(
6908 filter
->plist
[direct
]
6911 filter
->plist
[direct
].plist
=
6919 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6922 struct peer
*member
;
6923 struct bgp_filter
*filter
;
6924 struct listnode
*node
, *nnode
;
6926 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6927 return BGP_ERR_INVALID_VALUE
;
6929 /* Set configuration on peer. */
6930 filter
= &peer
->filter
[afi
][safi
];
6931 if (filter
->aslist
[direct
].name
)
6932 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6933 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6934 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6936 /* Check if handling a regular peer. */
6937 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6938 /* Set override-flag and process peer route updates. */
6939 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6940 PEER_FT_FILTER_LIST
);
6941 peer_on_policy_change(peer
, afi
, safi
,
6942 (direct
== FILTER_OUT
) ? 1 : 0);
6944 /* Skip peer-group mechanics for regular peers. */
6949 * Set configuration on all peer-group members, unless they are
6950 * explicitly overriding peer-group configuration.
6952 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6953 /* Skip peers with overridden configuration. */
6954 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6955 PEER_FT_FILTER_LIST
))
6958 /* Set configuration on peer-group member. */
6959 filter
= &member
->filter
[afi
][safi
];
6960 if (filter
->aslist
[direct
].name
)
6961 XFREE(MTYPE_BGP_FILTER_NAME
,
6962 filter
->aslist
[direct
].name
);
6963 filter
->aslist
[direct
].name
=
6964 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6965 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6967 /* Process peer route updates. */
6968 peer_on_policy_change(member
, afi
, safi
,
6969 (direct
== FILTER_OUT
) ? 1 : 0);
6975 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6977 struct peer
*member
;
6978 struct bgp_filter
*filter
;
6979 struct listnode
*node
, *nnode
;
6981 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6982 return BGP_ERR_INVALID_VALUE
;
6984 /* Unset override-flag unconditionally. */
6985 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6986 PEER_FT_FILTER_LIST
);
6988 /* Inherit configuration from peer-group if peer is member. */
6989 if (peer_group_active(peer
)) {
6990 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6991 filter
[afi
][safi
].aslist
[direct
].name
,
6992 MTYPE_BGP_FILTER_NAME
);
6993 PEER_ATTR_INHERIT(peer
, peer
->group
,
6994 filter
[afi
][safi
].aslist
[direct
].aslist
);
6996 /* Otherwise remove configuration from peer. */
6997 filter
= &peer
->filter
[afi
][safi
];
6998 if (filter
->aslist
[direct
].name
)
6999 XFREE(MTYPE_BGP_FILTER_NAME
,
7000 filter
->aslist
[direct
].name
);
7001 filter
->aslist
[direct
].name
= NULL
;
7002 filter
->aslist
[direct
].aslist
= NULL
;
7005 /* Check if handling a regular peer. */
7006 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7007 /* Process peer route updates. */
7008 peer_on_policy_change(peer
, afi
, safi
,
7009 (direct
== FILTER_OUT
) ? 1 : 0);
7011 /* Skip peer-group mechanics for regular peers. */
7016 * Remove configuration on all peer-group members, unless they are
7017 * explicitly overriding peer-group configuration.
7019 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7020 /* Skip peers with overridden configuration. */
7021 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7022 PEER_FT_FILTER_LIST
))
7025 /* Remove configuration on peer-group member. */
7026 filter
= &member
->filter
[afi
][safi
];
7027 if (filter
->aslist
[direct
].name
)
7028 XFREE(MTYPE_BGP_FILTER_NAME
,
7029 filter
->aslist
[direct
].name
);
7030 filter
->aslist
[direct
].name
= NULL
;
7031 filter
->aslist
[direct
].aslist
= NULL
;
7033 /* Process peer route updates. */
7034 peer_on_policy_change(member
, afi
, safi
,
7035 (direct
== FILTER_OUT
) ? 1 : 0);
7041 static void peer_aslist_update(const char *aslist_name
)
7046 struct listnode
*mnode
, *mnnode
;
7047 struct listnode
*node
, *nnode
;
7050 struct peer_group
*group
;
7051 struct bgp_filter
*filter
;
7053 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
7054 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
7055 aslist_name
, true, 0);
7057 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7058 FOREACH_AFI_SAFI (afi
, safi
) {
7059 filter
= &peer
->filter
[afi
][safi
];
7061 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7063 if (filter
->aslist
[direct
].name
)
7064 filter
->aslist
[direct
]
7065 .aslist
= as_list_lookup(
7066 filter
->aslist
[direct
]
7069 filter
->aslist
[direct
].aslist
=
7074 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
7075 FOREACH_AFI_SAFI (afi
, safi
) {
7076 filter
= &group
->conf
->filter
[afi
][safi
];
7078 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7080 if (filter
->aslist
[direct
].name
)
7081 filter
->aslist
[direct
]
7082 .aslist
= as_list_lookup(
7083 filter
->aslist
[direct
]
7086 filter
->aslist
[direct
].aslist
=
7094 static void peer_aslist_add(char *aslist_name
)
7096 peer_aslist_update(aslist_name
);
7097 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
7100 static void peer_aslist_del(const char *aslist_name
)
7102 peer_aslist_update(aslist_name
);
7103 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
7107 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
7108 const char *name
, struct route_map
*route_map
)
7110 struct peer
*member
;
7111 struct bgp_filter
*filter
;
7112 struct listnode
*node
, *nnode
;
7114 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7115 return BGP_ERR_INVALID_VALUE
;
7117 /* Set configuration on peer. */
7118 filter
= &peer
->filter
[afi
][safi
];
7119 if (filter
->map
[direct
].name
) {
7120 /* If the neighbor is configured with the same route-map
7121 * again then, ignore the duplicate configuration.
7123 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
7126 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7128 route_map_counter_decrement(filter
->map
[direct
].map
);
7129 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7130 filter
->map
[direct
].map
= route_map
;
7131 route_map_counter_increment(route_map
);
7133 /* Check if handling a regular peer. */
7134 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7135 /* Set override-flag and process peer route updates. */
7136 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
7138 peer_on_policy_change(peer
, afi
, safi
,
7139 (direct
== RMAP_OUT
) ? 1 : 0);
7141 /* Skip peer-group mechanics for regular peers. */
7146 * Set configuration on all peer-group members, unless they are
7147 * explicitly overriding peer-group configuration.
7149 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7150 /* Skip peers with overridden configuration. */
7151 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7155 /* Set configuration on peer-group member. */
7156 filter
= &member
->filter
[afi
][safi
];
7157 if (filter
->map
[direct
].name
)
7158 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7159 route_map_counter_decrement(filter
->map
[direct
].map
);
7160 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7161 filter
->map
[direct
].map
= route_map
;
7162 route_map_counter_increment(route_map
);
7164 /* Process peer route updates. */
7165 peer_on_policy_change(member
, afi
, safi
,
7166 (direct
== RMAP_OUT
) ? 1 : 0);
7171 /* Unset route-map from the peer. */
7172 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
7174 struct peer
*member
;
7175 struct bgp_filter
*filter
;
7176 struct listnode
*node
, *nnode
;
7178 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7179 return BGP_ERR_INVALID_VALUE
;
7181 /* Unset override-flag unconditionally. */
7182 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
7184 /* Inherit configuration from peer-group if peer is member. */
7185 if (peer_group_active(peer
)) {
7186 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7187 filter
[afi
][safi
].map
[direct
].name
,
7188 MTYPE_BGP_FILTER_NAME
);
7189 PEER_ATTR_INHERIT(peer
, peer
->group
,
7190 filter
[afi
][safi
].map
[direct
].map
);
7192 /* Otherwise remove configuration from peer. */
7193 filter
= &peer
->filter
[afi
][safi
];
7194 if (filter
->map
[direct
].name
)
7195 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7196 route_map_counter_decrement(filter
->map
[direct
].map
);
7197 filter
->map
[direct
].name
= NULL
;
7198 filter
->map
[direct
].map
= NULL
;
7201 /* Check if handling a regular peer. */
7202 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7203 /* Process peer route updates. */
7204 peer_on_policy_change(peer
, afi
, safi
,
7205 (direct
== RMAP_OUT
) ? 1 : 0);
7207 /* Skip peer-group mechanics for regular peers. */
7212 * Remove configuration on all peer-group members, unless they are
7213 * explicitly overriding peer-group configuration.
7215 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7216 /* Skip peers with overridden configuration. */
7217 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7221 /* Remove configuration on peer-group member. */
7222 filter
= &member
->filter
[afi
][safi
];
7223 if (filter
->map
[direct
].name
)
7224 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7225 route_map_counter_decrement(filter
->map
[direct
].map
);
7226 filter
->map
[direct
].name
= NULL
;
7227 filter
->map
[direct
].map
= NULL
;
7229 /* Process peer route updates. */
7230 peer_on_policy_change(member
, afi
, safi
,
7231 (direct
== RMAP_OUT
) ? 1 : 0);
7237 /* Set unsuppress-map to the peer. */
7238 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7239 const char *name
, struct route_map
*route_map
)
7241 struct peer
*member
;
7242 struct bgp_filter
*filter
;
7243 struct listnode
*node
, *nnode
;
7245 /* Set configuration on peer. */
7246 filter
= &peer
->filter
[afi
][safi
];
7247 if (filter
->usmap
.name
)
7248 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7249 route_map_counter_decrement(filter
->usmap
.map
);
7250 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7251 filter
->usmap
.map
= route_map
;
7252 route_map_counter_increment(route_map
);
7254 /* Check if handling a regular peer. */
7255 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7256 /* Set override-flag and process peer route updates. */
7257 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
7258 PEER_FT_UNSUPPRESS_MAP
);
7259 peer_on_policy_change(peer
, afi
, safi
, 1);
7261 /* Skip peer-group mechanics for regular peers. */
7266 * Set configuration on all peer-group members, unless they are
7267 * explicitly overriding peer-group configuration.
7269 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7270 /* Skip peers with overridden configuration. */
7271 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7272 PEER_FT_UNSUPPRESS_MAP
))
7275 /* Set configuration on peer-group member. */
7276 filter
= &member
->filter
[afi
][safi
];
7277 if (filter
->usmap
.name
)
7278 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7279 route_map_counter_decrement(filter
->usmap
.map
);
7280 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7281 filter
->usmap
.map
= route_map
;
7282 route_map_counter_increment(route_map
);
7284 /* Process peer route updates. */
7285 peer_on_policy_change(member
, afi
, safi
, 1);
7291 /* Unset route-map from the peer. */
7292 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7294 struct peer
*member
;
7295 struct bgp_filter
*filter
;
7296 struct listnode
*node
, *nnode
;
7298 /* Unset override-flag unconditionally. */
7299 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
7301 /* Inherit configuration from peer-group if peer is member. */
7302 if (peer_group_active(peer
)) {
7303 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7304 filter
[afi
][safi
].usmap
.name
,
7305 MTYPE_BGP_FILTER_NAME
);
7306 PEER_ATTR_INHERIT(peer
, peer
->group
,
7307 filter
[afi
][safi
].usmap
.map
);
7309 /* Otherwise remove configuration from peer. */
7310 filter
= &peer
->filter
[afi
][safi
];
7311 if (filter
->usmap
.name
)
7312 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7313 route_map_counter_decrement(filter
->usmap
.map
);
7314 filter
->usmap
.name
= NULL
;
7315 filter
->usmap
.map
= NULL
;
7318 /* Check if handling a regular peer. */
7319 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7320 /* Process peer route updates. */
7321 peer_on_policy_change(peer
, afi
, safi
, 1);
7323 /* Skip peer-group mechanics for regular peers. */
7328 * Remove configuration on all peer-group members, unless they are
7329 * explicitly overriding peer-group configuration.
7331 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7332 /* Skip peers with overridden configuration. */
7333 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7334 PEER_FT_UNSUPPRESS_MAP
))
7337 /* Remove configuration on peer-group member. */
7338 filter
= &member
->filter
[afi
][safi
];
7339 if (filter
->usmap
.name
)
7340 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7341 route_map_counter_decrement(filter
->usmap
.map
);
7342 filter
->usmap
.name
= NULL
;
7343 filter
->usmap
.map
= NULL
;
7345 /* Process peer route updates. */
7346 peer_on_policy_change(member
, afi
, safi
, 1);
7352 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7354 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7357 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7358 if (peer
->t_pmax_restart
) {
7359 THREAD_OFF(peer
->t_pmax_restart
);
7360 if (bgp_debug_neighbor_events(peer
))
7362 "%pBP Maximum-prefix restart timer cancelled",
7365 BGP_EVENT_ADD(peer
, BGP_Start
);
7369 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7370 uint32_t max
, uint8_t threshold
, int warning
,
7371 uint16_t restart
, bool force
)
7373 struct peer
*member
;
7374 struct listnode
*node
, *nnode
;
7376 /* Set flags and configuration on peer. */
7377 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7380 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7382 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7385 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7387 peer_af_flag_unset(peer
, afi
, safi
,
7388 PEER_FLAG_MAX_PREFIX_WARNING
);
7390 peer
->pmax
[afi
][safi
] = max
;
7391 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7392 peer
->pmax_restart
[afi
][safi
] = restart
;
7394 /* Check if handling a regular peer. */
7395 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7396 /* Re-check if peer violates maximum-prefix. */
7397 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7398 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7400 /* Skip peer-group mechanics for regular peers. */
7405 * Set flags and configuration on all peer-group members, unless they
7406 * are explicitly overriding peer-group configuration.
7408 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7409 /* Skip peers with overridden configuration. */
7410 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7411 PEER_FLAG_MAX_PREFIX
))
7414 /* Set flag and configuration on peer-group member. */
7415 member
->pmax
[afi
][safi
] = max
;
7416 member
->pmax_threshold
[afi
][safi
] = threshold
;
7417 member
->pmax_restart
[afi
][safi
] = restart
;
7420 SET_FLAG(member
->af_flags
[afi
][safi
],
7421 PEER_FLAG_MAX_PREFIX_FORCE
);
7423 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7424 PEER_FLAG_MAX_PREFIX_FORCE
);
7427 SET_FLAG(member
->af_flags
[afi
][safi
],
7428 PEER_FLAG_MAX_PREFIX_WARNING
);
7430 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7431 PEER_FLAG_MAX_PREFIX_WARNING
);
7433 /* Re-check if peer violates maximum-prefix. */
7434 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7435 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7441 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7443 /* Inherit configuration from peer-group if peer is member. */
7444 if (peer_group_active(peer
)) {
7445 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7446 peer_af_flag_inherit(peer
, afi
, safi
,
7447 PEER_FLAG_MAX_PREFIX_FORCE
);
7448 peer_af_flag_inherit(peer
, afi
, safi
,
7449 PEER_FLAG_MAX_PREFIX_WARNING
);
7450 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7451 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7452 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7457 /* Remove flags and configuration from peer. */
7458 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7459 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7460 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7461 peer
->pmax
[afi
][safi
] = 0;
7462 peer
->pmax_threshold
[afi
][safi
] = 0;
7463 peer
->pmax_restart
[afi
][safi
] = 0;
7466 * Remove flags and configuration from all peer-group members, unless
7467 * they are explicitly overriding peer-group configuration.
7469 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7470 struct peer
*member
;
7471 struct listnode
*node
;
7473 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7474 /* Skip peers with overridden configuration. */
7475 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7476 PEER_FLAG_MAX_PREFIX
))
7479 /* Remove flag and configuration on peer-group member.
7481 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7482 PEER_FLAG_MAX_PREFIX
);
7483 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7484 PEER_FLAG_MAX_PREFIX_FORCE
);
7485 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7486 PEER_FLAG_MAX_PREFIX_WARNING
);
7487 member
->pmax
[afi
][safi
] = 0;
7488 member
->pmax_threshold
[afi
][safi
] = 0;
7489 member
->pmax_restart
[afi
][safi
] = 0;
7491 peer_maximum_prefix_clear_overflow(member
);
7494 peer_maximum_prefix_clear_overflow(peer
);
7500 void peer_maximum_prefix_out_refresh_routes(struct peer
*peer
, afi_t afi
,
7503 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
7505 if (peer_established(peer
))
7506 bgp_announce_route(peer
, afi
, safi
, false);
7509 int peer_maximum_prefix_out_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7512 struct peer
*member
;
7513 struct listnode
*node
, *nnode
;
7515 /* Set flag on peer and peer-group member if any */
7516 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7517 /* Set configuration on peer. */
7518 peer
->pmax_out
[afi
][safi
] = max
;
7520 /* Check if handling a regular peer. */
7521 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7522 /* Skip peer-group mechanics for regular peers. */
7523 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7528 * Set flag and configuration on all peer-group members, unless they
7529 * are explicitly overriding peer-group configuration.
7531 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7532 /* Skip peers with overridden configuration. */
7533 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7534 PEER_FLAG_MAX_PREFIX_OUT
))
7537 /* Set configuration on peer-group member. */
7538 member
->pmax_out
[afi
][safi
] = max
;
7540 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7545 int peer_maximum_prefix_out_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7547 struct peer
*member
;
7548 struct listnode
*node
;
7549 /* Inherit configuration from peer-group if peer is member. */
7550 if (peer_group_active(peer
)) {
7551 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7552 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_out
[afi
][safi
]);
7554 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7558 /* Remove flag and configuration from peer. */
7559 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7560 peer
->pmax_out
[afi
][safi
] = 0;
7562 /* Check if handling a regular peer. */
7563 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7564 /* Skip peer-group mechanics for regular peers. */
7565 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7570 * Remove flag and configuration from all peer-group members, unless
7571 * they are explicitly overriding peer-group configuration.
7573 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7574 /* Skip peers with overridden configuration. */
7575 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7576 PEER_FLAG_MAX_PREFIX_OUT
))
7579 /* Remove flag and configuration on peer-group member.
7581 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7582 PEER_FLAG_MAX_PREFIX_OUT
);
7583 member
->pmax_out
[afi
][safi
] = 0;
7585 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7590 int is_ebgp_multihop_configured(struct peer
*peer
)
7592 struct peer_group
*group
;
7593 struct listnode
*node
, *nnode
;
7596 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7597 group
= peer
->group
;
7598 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7599 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7602 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7603 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7604 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7608 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7609 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7615 /* Set # of hops between us and BGP peer. */
7616 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7618 struct peer_group
*group
;
7620 struct listnode
*node
, *nnode
;
7623 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7626 /* We cannot configure ttl-security hops when ebgp-multihop is already
7627 set. For non peer-groups, the check is simple. For peer-groups,
7629 slightly messy, because we need to check both the peer-group
7631 and all peer-group members for any trace of ebgp-multihop
7633 before actually applying the ttl-security rules. Cisco really made a
7634 mess of this configuration parameter, and OpenBGPD got it right.
7637 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7638 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7639 if (is_ebgp_multihop_configured(peer
))
7640 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7642 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7643 peer
->gtsm_hops
= gtsm_hops
;
7645 /* Calling ebgp multihop also resets the session.
7646 * On restart, NHT will get setup correctly as will the
7647 * min & max ttls on the socket. The return value is
7650 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7655 group
= peer
->group
;
7656 group
->conf
->gtsm_hops
= gtsm_hops
;
7657 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7659 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7661 /* Calling ebgp multihop also resets the
7663 * On restart, NHT will get setup correctly as
7665 * min & max ttls on the socket. The return
7669 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7673 /* Post the first gtsm setup or if its ibgp, maxttl setting
7675 * necessary, just set the minttl.
7677 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7678 peer
->gtsm_hops
= gtsm_hops
;
7681 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7682 MAXTTL
+ 1 - gtsm_hops
);
7683 if ((peer
->status
< Established
) && peer
->doppelganger
7684 && (peer
->doppelganger
->fd
>= 0))
7685 sockopt_minttl(peer
->su
.sa
.sa_family
,
7686 peer
->doppelganger
->fd
,
7687 MAXTTL
+ 1 - gtsm_hops
);
7689 group
= peer
->group
;
7690 group
->conf
->gtsm_hops
= gtsm_hops
;
7691 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7693 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7695 /* Change setting of existing peer
7696 * established then change value (may break
7698 * not established yet (teardown session and
7700 * no session then do nothing (will get
7701 * handled by next connection)
7705 != BGP_GTSM_HOPS_DISABLED
)
7707 gpeer
->su
.sa
.sa_family
,
7709 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7710 if ((gpeer
->status
< Established
)
7711 && gpeer
->doppelganger
7712 && (gpeer
->doppelganger
->fd
>= 0))
7713 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7714 gpeer
->doppelganger
->fd
,
7715 MAXTTL
+ 1 - gtsm_hops
);
7723 int peer_ttl_security_hops_unset(struct peer
*peer
)
7725 struct peer_group
*group
;
7726 struct listnode
*node
, *nnode
;
7729 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7731 /* if a peer-group member, then reset to peer-group default rather than
7733 if (peer_group_active(peer
))
7734 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7736 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7738 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7739 /* Invoking ebgp_multihop_set will set the TTL back to the
7741 * value as well as restting the NHT and such. The session is
7744 if (peer
->sort
== BGP_PEER_EBGP
)
7745 ret
= peer_ebgp_multihop_unset(peer
);
7748 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7751 if ((peer
->status
< Established
) && peer
->doppelganger
7752 && (peer
->doppelganger
->fd
>= 0))
7753 sockopt_minttl(peer
->su
.sa
.sa_family
,
7754 peer
->doppelganger
->fd
, 0);
7757 group
= peer
->group
;
7758 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7759 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7760 if (peer
->sort
== BGP_PEER_EBGP
)
7761 ret
= peer_ebgp_multihop_unset(peer
);
7764 sockopt_minttl(peer
->su
.sa
.sa_family
,
7767 if ((peer
->status
< Established
)
7768 && peer
->doppelganger
7769 && (peer
->doppelganger
->fd
>= 0))
7770 sockopt_minttl(peer
->su
.sa
.sa_family
,
7771 peer
->doppelganger
->fd
,
7780 static void peer_reset_message_stats(struct peer
*peer
)
7783 atomic_store_explicit(&peer
->open_in
, 0, memory_order_relaxed
);
7784 atomic_store_explicit(&peer
->open_out
, 0, memory_order_relaxed
);
7785 atomic_store_explicit(&peer
->update_in
, 0,
7786 memory_order_relaxed
);
7787 atomic_store_explicit(&peer
->update_out
, 0,
7788 memory_order_relaxed
);
7789 atomic_store_explicit(&peer
->keepalive_in
, 0,
7790 memory_order_relaxed
);
7791 atomic_store_explicit(&peer
->keepalive_out
, 0,
7792 memory_order_relaxed
);
7793 atomic_store_explicit(&peer
->notify_in
, 0,
7794 memory_order_relaxed
);
7795 atomic_store_explicit(&peer
->notify_out
, 0,
7796 memory_order_relaxed
);
7797 atomic_store_explicit(&peer
->refresh_in
, 0,
7798 memory_order_relaxed
);
7799 atomic_store_explicit(&peer
->refresh_out
, 0,
7800 memory_order_relaxed
);
7801 atomic_store_explicit(&peer
->dynamic_cap_in
, 0,
7802 memory_order_relaxed
);
7803 atomic_store_explicit(&peer
->dynamic_cap_out
, 0,
7804 memory_order_relaxed
);
7809 * If peer clear is invoked in a loop for all peers on the BGP instance,
7810 * it may end up freeing the doppelganger, and if this was the next node
7811 * to the current node, we would end up accessing the freed next node.
7812 * Pass along additional parameter which can be updated if next node
7813 * is freed; only required when walking the peer list on BGP instance.
7815 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7817 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7818 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7819 if (peer_maximum_prefix_clear_overflow(peer
))
7822 peer
->v_start
= BGP_INIT_START_TIMER
;
7823 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7824 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7825 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7827 bgp_session_reset_safe(peer
, nnode
);
7832 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7833 enum bgp_clear_type stype
)
7835 struct peer_af
*paf
;
7837 if (!peer_established(peer
))
7840 if (!peer
->afc
[afi
][safi
])
7841 return BGP_ERR_AF_UNCONFIGURED
;
7843 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7845 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7846 /* Clear the "neighbor x.x.x.x default-originate" flag */
7847 paf
= peer_af_find(peer
, afi
, safi
);
7848 if (paf
&& paf
->subgroup
7849 && CHECK_FLAG(paf
->subgroup
->sflags
,
7850 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7851 UNSET_FLAG(paf
->subgroup
->sflags
,
7852 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7854 bgp_announce_route(peer
, afi
, safi
, false);
7857 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7858 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7859 PEER_CAP_ORF_PREFIX_SM_ADV
)
7860 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7861 PEER_CAP_ORF_PREFIX_RM_RCV
)
7862 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7863 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7864 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7865 uint8_t prefix_type
;
7867 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7868 PEER_CAP_ORF_PREFIX_RM_RCV
))
7869 prefix_type
= ORF_TYPE_PREFIX
;
7871 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7873 if (filter
->plist
[FILTER_IN
].plist
) {
7874 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7875 PEER_STATUS_ORF_PREFIX_SEND
))
7876 bgp_route_refresh_send(
7877 peer
, afi
, safi
, prefix_type
,
7879 BGP_ROUTE_REFRESH_NORMAL
);
7880 bgp_route_refresh_send(
7881 peer
, afi
, safi
, prefix_type
,
7882 REFRESH_IMMEDIATE
, 0,
7883 BGP_ROUTE_REFRESH_NORMAL
);
7885 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7886 PEER_STATUS_ORF_PREFIX_SEND
))
7887 bgp_route_refresh_send(
7888 peer
, afi
, safi
, prefix_type
,
7889 REFRESH_IMMEDIATE
, 1,
7890 BGP_ROUTE_REFRESH_NORMAL
);
7892 bgp_route_refresh_send(
7893 peer
, afi
, safi
, 0, 0, 0,
7894 BGP_ROUTE_REFRESH_NORMAL
);
7900 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7901 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7902 /* If neighbor has soft reconfiguration inbound flag.
7903 Use Adj-RIB-In database. */
7904 if (!bgp_soft_reconfig_in(peer
, afi
, safi
)) {
7905 /* If neighbor has route refresh capability, send route
7907 message to the peer. */
7908 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7909 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7910 bgp_route_refresh_send(
7911 peer
, afi
, safi
, 0, 0, 0,
7912 BGP_ROUTE_REFRESH_NORMAL
);
7914 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7918 if (stype
== BGP_CLEAR_MESSAGE_STATS
)
7919 peer_reset_message_stats(peer
);
7924 /* Display peer uptime.*/
7925 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7928 time_t uptime1
, epoch_tbuf
;
7931 /* If there is no connection has been done before print `never'. */
7934 json_object_string_add(json
, "peerUptime", "never");
7935 json_object_int_add(json
, "peerUptimeMsec", 0);
7937 snprintf(buf
, len
, "never");
7941 /* Get current time. */
7942 uptime1
= monotime(NULL
);
7944 gmtime_r(&uptime1
, &tm
);
7946 if (uptime1
< ONE_DAY_SECOND
)
7947 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7949 else if (uptime1
< ONE_WEEK_SECOND
)
7950 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7952 else if (uptime1
< ONE_YEAR_SECOND
)
7953 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7954 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7956 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7958 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7961 epoch_tbuf
= time(NULL
) - uptime1
;
7962 json_object_string_add(json
, "peerUptime", buf
);
7963 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7964 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7971 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7972 struct list
*addresses
)
7976 memset(&bgp_master
, 0, sizeof(bgp_master
));
7979 bm
->bgp
= list_new();
7980 bm
->listen_sockets
= list_new();
7981 bm
->port
= BGP_PORT_DEFAULT
;
7982 bm
->addresses
= addresses
;
7983 bm
->master
= master
;
7984 bm
->start_time
= monotime(NULL
);
7985 bm
->t_rmap_update
= NULL
;
7986 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7987 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7988 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7989 bm
->terminating
= false;
7990 bm
->socket_buffer
= buffer_size
;
7991 bm
->wait_for_fib
= false;
7992 bm
->tcp_dscp
= IPTOS_PREC_INTERNETCONTROL
;
7993 bm
->inq_limit
= BM_DEFAULT_Q_LIMIT
;
7994 bm
->outq_limit
= BM_DEFAULT_Q_LIMIT
;
7997 /* init the rd id space.
7998 assign 0th index in the bitfield,
7999 so that we start with id 1
8001 bf_init(bm
->rd_idspace
, UINT16_MAX
);
8002 bf_assign_zero_index(bm
->rd_idspace
);
8004 /* mpls label dynamic allocation pool */
8005 bgp_lp_init(bm
->master
, &bm
->labelpool
);
8009 QOBJ_REG(bm
, bgp_master
);
8013 * Free up connected routes and interfaces for a BGP instance. Invoked upon
8014 * instance delete (non-default only) or BGP exit.
8016 static void bgp_if_finish(struct bgp
*bgp
)
8019 struct interface
*ifp
;
8021 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
8023 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
8026 FOR_ALL_INTERFACES (vrf
, ifp
) {
8027 struct listnode
*c_node
, *c_nnode
;
8028 struct connected
*c
;
8030 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
8031 bgp_connected_delete(bgp
, c
);
8035 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
8037 struct vrf
*vrf
= NULL
;
8038 struct listnode
*next
;
8041 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
8042 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
8044 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8045 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
8048 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
8052 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
8054 struct listnode
*next
, *next2
;
8055 struct bgp
*bgp
, *bgp2
;
8056 char buf
[ASN_STRING_MAX_SIZE
];
8058 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8060 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
8061 if (bgp2
->as
== bgp
->as
)
8069 snprintf(buf
, sizeof(buf
), "%s", bgp
->as_pretty
);
8070 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
8074 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
8075 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
8076 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
8077 {.completions
= NULL
},
8080 struct frr_pthread
*bgp_pth_io
;
8081 struct frr_pthread
*bgp_pth_ka
;
8083 static void bgp_pthreads_init(void)
8085 assert(!bgp_pth_io
);
8086 assert(!bgp_pth_ka
);
8088 struct frr_pthread_attr io
= {
8089 .start
= frr_pthread_attr_default
.start
,
8090 .stop
= frr_pthread_attr_default
.stop
,
8092 struct frr_pthread_attr ka
= {
8093 .start
= bgp_keepalives_start
,
8094 .stop
= bgp_keepalives_stop
,
8096 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
8097 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
8100 void bgp_pthreads_run(void)
8102 frr_pthread_run(bgp_pth_io
, NULL
);
8103 frr_pthread_run(bgp_pth_ka
, NULL
);
8105 /* Wait until threads are ready. */
8106 frr_pthread_wait_running(bgp_pth_io
);
8107 frr_pthread_wait_running(bgp_pth_ka
);
8110 void bgp_pthreads_finish(void)
8112 frr_pthread_stop_all();
8115 static int peer_unshut_after_cfg(struct bgp
*bgp
)
8117 struct listnode
*node
;
8120 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
8121 if (!peer
->shut_during_cfg
)
8124 if (bgp_debug_neighbor_events(peer
))
8125 zlog_debug("%s: released from config-pending hold",
8128 peer
->shut_during_cfg
= false;
8129 if (peer_active(peer
) && peer
->status
!= Established
) {
8130 if (peer
->status
!= Idle
)
8131 BGP_EVENT_ADD(peer
, BGP_Stop
);
8132 BGP_EVENT_ADD(peer
, BGP_Start
);
8139 void bgp_init(unsigned short instance
)
8141 hook_register(bgp_config_end
, peer_unshut_after_cfg
);
8143 /* allocates some vital data structures used by peer commands in
8146 /* pre-init pthreads */
8147 bgp_pthreads_init();
8150 bgp_zebra_init(bm
->master
, instance
);
8152 #ifdef ENABLE_BGP_VNC
8153 vnc_zebra_init(bm
->master
);
8156 /* BGP VTY commands installation. */
8162 bgp_community_alias_init();
8165 bgp_route_map_init();
8166 bgp_scan_vty_init();
8168 #ifdef ENABLE_BGP_VNC
8171 bgp_ethernetvpn_init();
8172 bgp_flowspec_vty_init();
8174 /* Access list initialize. */
8176 access_list_add_hook(peer_distribute_update
);
8177 access_list_delete_hook(peer_distribute_update
);
8179 /* Filter list initialize. */
8181 as_list_add_hook(peer_aslist_add
);
8182 as_list_delete_hook(peer_aslist_del
);
8184 /* Prefix list initialize.*/
8186 prefix_list_add_hook(peer_prefix_list_update
);
8187 prefix_list_delete_hook(peer_prefix_list_update
);
8189 /* Community list initialize. */
8190 bgp_clist
= community_list_init();
8193 bgp_bfd_init(bm
->master
);
8197 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
8200 void bgp_terminate(void)
8204 struct listnode
*node
, *nnode
;
8205 struct listnode
*mnode
, *mnnode
;
8209 /* Close the listener sockets first as this prevents peers from
8211 * to reconnect on receiving the peer unconfig message. In the presence
8212 * of a large number of peers this will ensure that no peer is left with
8213 * a dangling connection
8217 /* reverse bgp_master_init */
8218 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
8219 bgp_close_vrf_socket(bgp
);
8220 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8221 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
8222 if (bgp_debug_neighbor_events(peer
))
8224 "%pBP configured Graceful-Restart, skipping unconfig notification",
8228 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
8229 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
8230 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
8234 if (bm
->listen_sockets
)
8235 list_delete(&bm
->listen_sockets
);
8237 THREAD_OFF(bm
->t_rmap_update
);
8242 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
8243 const char *ip_str
, bool use_json
)
8249 /* Get peer sockunion. */
8250 ret
= str2sockunion(ip_str
, &su
);
8252 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
8254 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
8258 json_object
*json_no
= NULL
;
8259 json_no
= json_object_new_object();
8260 json_object_string_add(
8262 "malformedAddressOrName",
8264 vty_json(vty
, json_no
);
8267 "%% Malformed address or name: %s\n",
8275 /* Peer structure lookup. */
8276 peer
= peer_lookup(bgp
, &su
);
8279 json_object
*json_no
= NULL
;
8280 json_no
= json_object_new_object();
8281 json_object_string_add(json_no
, "warning",
8282 "No such neighbor in this view/vrf");
8283 vty_json(vty
, json_no
);
8285 vty_out(vty
, "No such neighbor in this view/vrf\n");
8292 void bgp_gr_apply_running_config(void)
8294 struct peer
*peer
= NULL
;
8295 struct bgp
*bgp
= NULL
;
8296 struct listnode
*node
, *nnode
;
8297 bool gr_router_detected
= false;
8299 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
8300 zlog_debug("[BGP_GR] %s called !", __func__
);
8302 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
8303 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8304 bgp_peer_gr_flags_update(peer
);
8305 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
8306 gr_router_detected
= true;
8309 if (gr_router_detected
8310 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
8311 bgp_zebra_send_capabilities(bgp
, true);
8312 } else if (!gr_router_detected
8313 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
8314 bgp_zebra_send_capabilities(bgp
, false);
8317 gr_router_detected
= false;
8321 printfrr_ext_autoreg_p("BP", printfrr_bp
);
8322 static ssize_t
printfrr_bp(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
8325 const struct peer
*peer
= ptr
;
8328 return bputs(buf
, "(null)");
8330 return bprintfrr(buf
, "%s(%s)", peer
->host
,
8331 peer
->hostname
? peer
->hostname
: "Unknown");