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 XFREE(MTYPE_BGP_SOFT_VERSION
, peer
->soft_version
);
1183 /* Remove BFD configuration. */
1184 if (peer
->bfd_config
)
1185 bgp_peer_remove_bfd_config(peer
);
1187 FOREACH_AFI_SAFI (afi
, safi
)
1188 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1190 bgp_unlock(peer
->bgp
);
1192 memset(peer
, 0, sizeof(struct peer
));
1194 XFREE(MTYPE_BGP_PEER
, peer
);
1197 /* increase reference count on a struct peer */
1198 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1200 frrtrace(2, frr_bgp
, bgp_peer_lock
, peer
, name
);
1201 assert(peer
&& (peer
->lock
>= 0));
1208 /* decrease reference count on a struct peer
1209 * struct peer is freed and NULL returned if last reference
1211 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1213 frrtrace(2, frr_bgp
, bgp_peer_unlock
, peer
, name
);
1214 assert(peer
&& (peer
->lock
> 0));
1218 if (peer
->lock
== 0) {
1225 /* BGP GR changes */
1227 int bgp_global_gr_init(struct bgp
*bgp
)
1229 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1230 zlog_debug("%s called ..", __func__
);
1232 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1233 /* GLOBAL_HELPER Mode */
1236 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1237 GLOBAL_GR
, GLOBAL_INVALID
,
1238 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1239 GLOBAL_DISABLE
, GLOBAL_INVALID
1241 /* GLOBAL_GR Mode */
1244 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1245 GLOBAL_GR
, GLOBAL_HELPER
,
1246 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1247 GLOBAL_DISABLE
, GLOBAL_INVALID
1249 /* GLOBAL_DISABLE Mode */
1252 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1253 GLOBAL_GR
, GLOBAL_INVALID
,
1254 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1255 GLOBAL_INVALID
, GLOBAL_HELPER
1257 /* GLOBAL_INVALID Mode */
1260 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1261 GLOBAL_INVALID
, GLOBAL_INVALID
,
1262 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1263 GLOBAL_INVALID
, GLOBAL_INVALID
1266 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1267 sizeof(local_GLOBAL_GR_FSM
));
1269 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1270 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1272 return BGP_GR_SUCCESS
;
1275 int bgp_peer_gr_init(struct peer
*peer
)
1277 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1278 zlog_debug("%s called ..", __func__
);
1280 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1281 [BGP_PEER_GR_EVENT_CMD
] = {
1283 /* PEER_HELPER Mode */
1284 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1285 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1286 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1287 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1288 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1289 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1290 bgp_peer_gr_action
}
1294 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1295 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1296 bgp_peer_gr_action
},
1297 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1298 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1299 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1300 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1303 /* PEER_DISABLE Mode */
1304 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1305 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1306 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1307 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1308 bgp_peer_gr_action
},
1309 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1310 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1313 /* PEER_INVALID Mode */
1314 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1315 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1316 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1317 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1318 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1319 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1322 /* PEER_GLOBAL_INHERIT Mode */
1323 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1324 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1325 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1326 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1327 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1328 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1331 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1332 sizeof(local_Peer_GR_FSM
));
1333 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1334 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1336 return BGP_GR_SUCCESS
;
1339 static void bgp_srv6_init(struct bgp
*bgp
)
1341 bgp
->srv6_enabled
= false;
1342 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1343 bgp
->srv6_locator_chunks
= list_new();
1344 bgp
->srv6_functions
= list_new();
1347 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1349 if (bgp
->srv6_locator_chunks
)
1350 list_delete(&bgp
->srv6_locator_chunks
);
1351 if (bgp
->srv6_functions
)
1352 list_delete(&bgp
->srv6_functions
);
1355 /* Allocate new peer object, implicitely locked. */
1356 struct peer
*peer_new(struct bgp
*bgp
)
1363 /* bgp argument is absolutely required */
1366 /* Allocate new peer. */
1367 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1369 /* Set default value. */
1371 peer
->v_start
= BGP_INIT_START_TIMER
;
1372 peer
->v_connect
= bgp
->default_connect_retry
;
1373 peer
->status
= Idle
;
1374 peer
->ostatus
= Idle
;
1375 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1376 peer
->bgp
= bgp_lock(bgp
);
1377 peer
= peer_lock(peer
); /* initial reference */
1378 peer
->local_role
= ROLE_UNDEFINED
;
1379 peer
->remote_role
= ROLE_UNDEFINED
;
1380 peer
->password
= NULL
;
1381 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1383 /* Set default flags. */
1384 FOREACH_AFI_SAFI (afi
, safi
) {
1385 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1386 SET_FLAG(peer
->af_flags
[afi
][safi
],
1387 PEER_FLAG_SEND_EXT_COMMUNITY
);
1388 SET_FLAG(peer
->af_flags
[afi
][safi
],
1389 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1391 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1392 PEER_FLAG_SEND_COMMUNITY
);
1393 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1394 PEER_FLAG_SEND_EXT_COMMUNITY
);
1395 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1396 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1397 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1398 peer
->soo
[afi
][safi
] = NULL
;
1401 /* set nexthop-unchanged for l2vpn evpn by default */
1402 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1403 PEER_FLAG_NEXTHOP_UNCHANGED
);
1405 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1407 /* Initialize per peer bgp GR FSM */
1408 bgp_peer_gr_init(peer
);
1410 /* Create buffers. */
1411 peer
->ibuf
= stream_fifo_new();
1412 peer
->obuf
= stream_fifo_new();
1413 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1415 /* We use a larger buffer for peer->obuf_work in the event that:
1416 * - We RX a BGP_UPDATE where the attributes alone are just
1417 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1418 * - The user configures an outbound route-map that does many as-path
1419 * prepends or adds many communities. At most they can have
1420 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1421 * large they can make the attributes.
1423 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1424 * bounds checking for every single attribute as we construct an
1428 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1430 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1432 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1434 bgp_sync_init(peer
);
1436 /* Get service port number. */
1437 sp
= getservbyname("bgp", "tcp");
1438 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1440 QOBJ_REG(peer
, peer
);
1445 * This function is invoked when a duplicate peer structure associated with
1446 * a neighbor is being deleted. If this about-to-be-deleted structure is
1447 * the one with all the config, then we have to copy over the info.
1449 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1451 struct peer_af
*paf
;
1459 /* The following function is used by both peer group config copy to
1460 * individual peer and when we transfer config
1462 if (peer_src
->change_local_as
)
1463 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1465 /* peer flags apply */
1466 peer_dst
->flags
= peer_src
->flags
;
1468 * The doppelganger *must* not have a config node stored
1470 UNSET_FLAG(peer_dst
->flags
, PEER_FLAG_CONFIG_NODE
);
1471 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1472 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1474 peer_dst
->local_as
= peer_src
->local_as
;
1475 peer_dst
->port
= peer_src
->port
;
1476 /* copy tcp_mss value */
1477 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1478 (void)peer_sort(peer_dst
);
1479 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1480 peer_dst
->local_role
= peer_src
->local_role
;
1482 peer_dst
->max_packet_size
= peer_src
->max_packet_size
;
1485 peer_dst
->holdtime
= peer_src
->holdtime
;
1486 peer_dst
->keepalive
= peer_src
->keepalive
;
1487 peer_dst
->connect
= peer_src
->connect
;
1488 peer_dst
->delayopen
= peer_src
->delayopen
;
1489 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1490 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1491 peer_dst
->routeadv
= peer_src
->routeadv
;
1492 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1493 peer_dst
->v_delayopen
= peer_src
->v_delayopen
;
1495 /* password apply */
1496 if (peer_src
->password
&& !peer_dst
->password
)
1497 peer_dst
->password
=
1498 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1500 FOREACH_AFI_SAFI (afi
, safi
) {
1501 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1502 peer_dst
->af_flags
[afi
][safi
] = peer_src
->af_flags
[afi
][safi
];
1503 peer_dst
->allowas_in
[afi
][safi
] =
1504 peer_src
->allowas_in
[afi
][safi
];
1505 peer_dst
->weight
[afi
][safi
] = peer_src
->weight
[afi
][safi
];
1506 peer_dst
->addpath_type
[afi
][safi
] =
1507 peer_src
->addpath_type
[afi
][safi
];
1510 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1511 paf
= peer_src
->peer_af_array
[afidx
];
1513 if (!peer_af_find(peer_dst
, paf
->afi
, paf
->safi
))
1514 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1518 /* update-source apply */
1519 if (peer_src
->update_source
) {
1520 if (peer_dst
->update_source
)
1521 sockunion_free(peer_dst
->update_source
);
1522 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1523 peer_dst
->update_source
=
1524 sockunion_dup(peer_src
->update_source
);
1525 } else if (peer_src
->update_if
) {
1526 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1527 if (peer_dst
->update_source
) {
1528 sockunion_free(peer_dst
->update_source
);
1529 peer_dst
->update_source
= NULL
;
1531 peer_dst
->update_if
=
1532 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1535 if (peer_src
->ifname
) {
1536 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1539 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1543 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1544 struct interface
*ifp
)
1546 struct connected
*ifc
;
1549 struct listnode
*node
;
1551 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1552 * IPv4 address of the other end.
1554 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1555 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1556 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
1557 if (p
.prefixlen
== 30) {
1558 peer
->su
.sa
.sa_family
= AF_INET
;
1559 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1561 peer
->su
.sin
.sin_addr
.s_addr
=
1563 else if (addr
% 4 == 2)
1564 peer
->su
.sin
.sin_addr
.s_addr
=
1566 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1567 peer
->su
.sin
.sin_len
=
1568 sizeof(struct sockaddr_in
);
1569 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1571 } else if (p
.prefixlen
== 31) {
1572 peer
->su
.sa
.sa_family
= AF_INET
;
1573 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1575 peer
->su
.sin
.sin_addr
.s_addr
=
1578 peer
->su
.sin
.sin_addr
.s_addr
=
1580 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1581 peer
->su
.sin
.sin_len
=
1582 sizeof(struct sockaddr_in
);
1583 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1585 } else if (bgp_debug_neighbor_events(peer
))
1587 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1595 static bool bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1596 struct interface
*ifp
)
1598 struct nbr_connected
*ifc_nbr
;
1600 /* Have we learnt the peer's IPv6 link-local address? */
1601 if (ifp
->nbr_connected
1602 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1603 peer
->su
.sa
.sa_family
= AF_INET6
;
1604 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1605 sizeof(struct in6_addr
));
1607 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1609 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1617 * Set or reset the peer address socketunion structure based on the
1618 * learnt/derived peer address. If the address has changed, update the
1619 * password on the listen socket, if needed.
1621 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1623 struct interface
*ifp
;
1625 int peer_addr_updated
= 0;
1626 struct listnode
*node
;
1627 union sockunion old_su
;
1630 * This function is only ever needed when FRR an interface
1631 * based peering, so this simple test will tell us if
1632 * we are in an interface based configuration or not
1639 prev_family
= peer
->su
.sa
.sa_family
;
1640 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1642 /* If BGP unnumbered is not "v6only", we first see if we can
1644 * peer's IPv4 address.
1646 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1648 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1650 /* If "v6only" or we can't derive peer's IPv4 address, see if
1652 * learnt the peer's IPv6 link-local address. This is from the
1654 * IPv6 address in router advertisement.
1656 if (!peer_addr_updated
)
1658 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1660 /* If we could derive the peer address, we may need to install the
1662 * configured for the peer, if any, on the listen socket. Otherwise,
1664 * that peer's address is not available and uninstall the password, if
1667 if (peer_addr_updated
) {
1668 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1669 && prev_family
== AF_UNSPEC
)
1672 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1673 && prev_family
!= AF_UNSPEC
)
1674 bgp_md5_unset(peer
);
1675 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1676 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1680 * If they are the same, nothing to do here, move along
1682 if (!sockunion_same(&old_su
, &peer
->su
)) {
1683 union sockunion new_su
= peer
->su
;
1684 struct bgp
*bgp
= peer
->bgp
;
1687 * Our peer structure is stored in the bgp->peerhash
1688 * release it before we modify anything in both the
1689 * hash and the list. But *only* if the peer
1690 * is in the bgp->peerhash as that on deletion
1691 * we call bgp_stop which calls this function :(
1692 * so on deletion let's remove from the list first
1693 * and then do the deletion preventing this from
1694 * being added back on the list below when we
1695 * fail to remove it up here.
1699 * listnode_lookup just scans the list
1700 * for the peer structure so it's safe
1701 * to use without modifying the su
1703 node
= listnode_lookup(bgp
->peer
, peer
);
1706 * Let's reset the peer->su release and
1707 * reset it and put it back. We have to
1708 * do this because hash_release will
1709 * scan through looking for a matching
1713 hash_release(peer
->bgp
->peerhash
, peer
);
1714 listnode_delete(peer
->bgp
->peer
, peer
);
1717 (void)hash_get(peer
->bgp
->peerhash
, peer
,
1719 listnode_add_sort(peer
->bgp
->peer
, peer
);
1724 void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1726 struct bgp_dest
*dest
, *ndest
;
1727 struct bgp_table
*table
;
1729 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1730 dest
= bgp_route_next(dest
)) {
1731 table
= bgp_dest_get_bgp_table_info(dest
);
1732 if (table
!= NULL
) {
1733 /* Special handling for 2-level routing
1735 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1736 || safi
== SAFI_EVPN
) {
1737 for (ndest
= bgp_table_top(table
); ndest
;
1738 ndest
= bgp_route_next(ndest
))
1739 bgp_process(bgp
, ndest
, afi
, safi
);
1741 bgp_process(bgp
, dest
, afi
, safi
);
1746 /* Force a bestpath recalculation for all prefixes. This is used
1747 * when 'bgp bestpath' commands are entered.
1749 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1754 FOREACH_AFI_SAFI (afi
, safi
) {
1755 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1760 * Create new BGP peer.
1762 * conf_if and su are mutually exclusive if configuring from the cli.
1763 * If we are handing a doppelganger, then we *must* pass in both
1764 * the original peer's su and conf_if, so that we can appropriately
1765 * track the bgp->peerhash( ie we don't want to remove the current
1766 * one from the config ).
1768 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1769 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1770 int as_type
, struct peer_group
*group
,
1775 char buf
[SU_ADDRSTRLEN
];
1779 peer
= peer_new(bgp
);
1781 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1785 bgp_peer_conf_if_to_su_update(peer
);
1786 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1787 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1790 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1791 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1792 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1794 peer
->local_as
= local_as
;
1795 peer
->as
= remote_as
;
1796 peer
->as_type
= as_type
;
1797 peer
->local_id
= bgp
->router_id
;
1798 peer
->v_holdtime
= bgp
->default_holdtime
;
1799 peer
->v_keepalive
= bgp
->default_keepalive
;
1800 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1801 ? BGP_DEFAULT_IBGP_ROUTEADV
1802 : BGP_DEFAULT_EBGP_ROUTEADV
;
1803 if (bgp_config_inprocess())
1804 peer
->shut_during_cfg
= true;
1806 peer
= peer_lock(peer
); /* bgp peer list reference */
1807 peer
->group
= group
;
1808 listnode_add_sort(bgp
->peer
, peer
);
1811 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1813 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1815 /* Adjust update-group coalesce timer heuristics for # peers. */
1816 if (bgp
->heuristic_coalesce
) {
1817 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1819 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1820 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1823 active
= peer_active(peer
);
1825 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1826 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1828 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1831 /* Last read and reset time set */
1832 peer
->readtime
= peer
->resettime
= monotime(NULL
);
1834 /* Default TTL set. */
1835 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1837 /* Default configured keepalives count for shutdown rtt command */
1838 peer
->rtt_keepalive_conf
= 1;
1840 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1841 * neighbor for the corresponding address family. IPv4 Unicast is
1842 * the only address family enabled by default without expliict
1845 FOREACH_AFI_SAFI (afi
, safi
) {
1846 if (bgp
->default_af
[afi
][safi
]) {
1847 peer
->afc
[afi
][safi
] = 1;
1848 peer_af_create(peer
, afi
, safi
);
1852 /* auto shutdown if configured */
1853 if (bgp
->autoshutdown
)
1854 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1855 /* Set up peer's events and timers. */
1856 else if (!active
&& peer_active(peer
))
1857 bgp_timer_set(peer
);
1859 bgp_peer_gr_flags_update(peer
);
1860 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1865 /* Make accept BGP peer. This function is only called from the test code */
1866 struct peer
*peer_create_accept(struct bgp
*bgp
)
1870 peer
= peer_new(bgp
);
1872 peer
= peer_lock(peer
); /* bgp peer list reference */
1873 listnode_add_sort(bgp
->peer
, peer
);
1874 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1880 * Return true if we have a peer configured to use this afi/safi
1882 bool bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1884 struct listnode
*node
;
1887 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1888 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1891 if (peer
->afc
[afi
][safi
])
1898 /* Change peer's AS number. */
1899 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
)
1901 enum bgp_peer_sort origtype
, newtype
;
1904 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1905 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1906 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1907 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1908 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1910 bgp_session_reset(peer
);
1912 origtype
= peer_sort_lookup(peer
);
1914 peer
->as_type
= as_specified
;
1916 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1917 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1918 && peer
->bgp
->as
!= as
)
1919 peer
->local_as
= peer
->bgp
->confed_id
;
1921 peer
->local_as
= peer
->bgp
->as
;
1923 newtype
= peer_sort(peer
);
1924 /* Advertisement-interval reset */
1925 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1926 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1927 ? BGP_DEFAULT_IBGP_ROUTEADV
1928 : BGP_DEFAULT_EBGP_ROUTEADV
;
1932 if (newtype
== BGP_PEER_IBGP
)
1934 else if (origtype
== BGP_PEER_IBGP
)
1935 peer
->ttl
= BGP_DEFAULT_TTL
;
1937 /* reflector-client reset */
1938 if (newtype
!= BGP_PEER_IBGP
) {
1939 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1940 PEER_FLAG_REFLECTOR_CLIENT
);
1941 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1942 PEER_FLAG_REFLECTOR_CLIENT
);
1943 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1944 PEER_FLAG_REFLECTOR_CLIENT
);
1945 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1946 PEER_FLAG_REFLECTOR_CLIENT
);
1947 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1948 PEER_FLAG_REFLECTOR_CLIENT
);
1949 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1950 PEER_FLAG_REFLECTOR_CLIENT
);
1951 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1952 PEER_FLAG_REFLECTOR_CLIENT
);
1953 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1954 PEER_FLAG_REFLECTOR_CLIENT
);
1955 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1956 PEER_FLAG_REFLECTOR_CLIENT
);
1957 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1958 PEER_FLAG_REFLECTOR_CLIENT
);
1959 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1960 PEER_FLAG_REFLECTOR_CLIENT
);
1961 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1962 PEER_FLAG_REFLECTOR_CLIENT
);
1963 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1964 PEER_FLAG_REFLECTOR_CLIENT
);
1968 /* If peer does not exist, create new one. If peer already exists,
1969 set AS number to the peer. */
1970 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1971 as_t
*as
, int as_type
)
1977 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1979 peer
= peer_lookup(bgp
, su
);
1982 /* Not allowed for a dynamic peer. */
1983 if (peer_dynamic_neighbor(peer
)) {
1985 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1988 /* When this peer is a member of peer-group. */
1990 /* peer-group already has AS number/internal/external */
1991 if (peer
->group
->conf
->as
1992 || peer
->group
->conf
->as_type
) {
1993 /* Return peer group's AS number. */
1994 *as
= peer
->group
->conf
->as
;
1995 return BGP_ERR_PEER_GROUP_MEMBER
;
1998 enum bgp_peer_sort peer_sort_type
=
1999 peer_sort(peer
->group
->conf
);
2001 /* Explicit AS numbers used, compare AS numbers */
2002 if (as_type
== AS_SPECIFIED
) {
2003 if (((peer_sort_type
== BGP_PEER_IBGP
)
2004 && (bgp
->as
!= *as
))
2005 || ((peer_sort_type
== BGP_PEER_EBGP
)
2006 && (bgp
->as
== *as
))) {
2008 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2011 /* internal/external used, compare as-types */
2012 if (((peer_sort_type
== BGP_PEER_IBGP
)
2013 && (as_type
!= AS_INTERNAL
))
2014 || ((peer_sort_type
== BGP_PEER_EBGP
)
2015 && (as_type
!= AS_EXTERNAL
))) {
2017 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2022 /* Existing peer's AS number change. */
2023 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2024 || (peer
->as_type
!= as_type
))
2025 peer_as_change(peer
, *as
, as_type
);
2028 return BGP_ERR_NO_INTERFACE_CONFIG
;
2030 /* If the peer is not part of our confederation, and its not an
2031 iBGP peer then spoof the source AS */
2032 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
2033 && !bgp_confederation_peers_check(bgp
, *as
)
2035 local_as
= bgp
->confed_id
;
2039 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
,
2046 const char *bgp_get_name_by_role(uint8_t role
)
2051 case ROLE_RS_SERVER
:
2053 case ROLE_RS_CLIENT
:
2059 case ROLE_UNDEFINED
:
2065 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
2066 struct peer
*peer
, afi_t afi
,
2070 int out
= FILTER_OUT
;
2072 uint64_t pflags_ovrd
;
2073 uint8_t *pfilter_ovrd
;
2077 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2078 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2080 /* peer af_flags apply */
2081 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2082 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2083 ^ peer
->af_flags_invert
[afi
][safi
];
2084 flags_tmp
&= ~pflags_ovrd
;
2086 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2087 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2088 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2089 conf
->af_flags_invert
[afi
][safi
]);
2091 /* maximum-prefix */
2092 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2093 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2094 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2095 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2098 /* maximum-prefix-out */
2099 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX_OUT
))
2100 PEER_ATTR_INHERIT(peer
, group
, pmax_out
[afi
][safi
]);
2103 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2104 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2107 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_SOO
))
2108 PEER_ATTR_INHERIT(peer
, group
, soo
[afi
][safi
]);
2111 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2112 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2114 /* default-originate route-map */
2115 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2116 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2117 MTYPE_ROUTE_MAP_NAME
);
2118 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2121 /* inbound filter apply */
2122 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2123 PEER_STR_ATTR_INHERIT(peer
, group
,
2124 filter
[afi
][safi
].dlist
[in
].name
,
2125 MTYPE_BGP_FILTER_NAME
);
2126 PEER_ATTR_INHERIT(peer
, group
,
2127 filter
[afi
][safi
].dlist
[in
].alist
);
2130 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2131 PEER_STR_ATTR_INHERIT(peer
, group
,
2132 filter
[afi
][safi
].plist
[in
].name
,
2133 MTYPE_BGP_FILTER_NAME
);
2134 PEER_ATTR_INHERIT(peer
, group
,
2135 filter
[afi
][safi
].plist
[in
].plist
);
2138 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2139 PEER_STR_ATTR_INHERIT(peer
, group
,
2140 filter
[afi
][safi
].aslist
[in
].name
,
2141 MTYPE_BGP_FILTER_NAME
);
2142 PEER_ATTR_INHERIT(peer
, group
,
2143 filter
[afi
][safi
].aslist
[in
].aslist
);
2146 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2147 PEER_STR_ATTR_INHERIT(peer
, group
,
2148 filter
[afi
][safi
].map
[in
].name
,
2149 MTYPE_BGP_FILTER_NAME
);
2150 PEER_ATTR_INHERIT(peer
, group
,
2151 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2154 /* outbound filter apply */
2155 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2156 PEER_STR_ATTR_INHERIT(peer
, group
,
2157 filter
[afi
][safi
].dlist
[out
].name
,
2158 MTYPE_BGP_FILTER_NAME
);
2159 PEER_ATTR_INHERIT(peer
, group
,
2160 filter
[afi
][safi
].dlist
[out
].alist
);
2163 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2164 PEER_STR_ATTR_INHERIT(peer
, group
,
2165 filter
[afi
][safi
].plist
[out
].name
,
2166 MTYPE_BGP_FILTER_NAME
);
2167 PEER_ATTR_INHERIT(peer
, group
,
2168 filter
[afi
][safi
].plist
[out
].plist
);
2171 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2172 PEER_STR_ATTR_INHERIT(peer
, group
,
2173 filter
[afi
][safi
].aslist
[out
].name
,
2174 MTYPE_BGP_FILTER_NAME
);
2175 PEER_ATTR_INHERIT(peer
, group
,
2176 filter
[afi
][safi
].aslist
[out
].aslist
);
2179 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2180 PEER_STR_ATTR_INHERIT(peer
, group
,
2181 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2182 MTYPE_BGP_FILTER_NAME
);
2183 PEER_ATTR_INHERIT(peer
, group
,
2184 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2187 /* nondirectional filter apply */
2188 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2189 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2190 MTYPE_BGP_FILTER_NAME
);
2191 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2194 /* Conditional Advertisements */
2195 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ADVERTISE_MAP
)) {
2196 PEER_STR_ATTR_INHERIT(peer
, group
,
2197 filter
[afi
][safi
].advmap
.aname
,
2198 MTYPE_BGP_FILTER_NAME
);
2199 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.amap
);
2200 PEER_STR_ATTR_INHERIT(peer
, group
,
2201 filter
[afi
][safi
].advmap
.cname
,
2202 MTYPE_BGP_FILTER_NAME
);
2203 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.cmap
);
2204 PEER_ATTR_INHERIT(peer
, group
,
2205 filter
[afi
][safi
].advmap
.condition
);
2208 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2209 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2210 bgp_addpath_type_changed(conf
->bgp
);
2214 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2219 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2220 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2221 __func__
, peer
->host
);
2225 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2227 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2228 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2229 return BGP_ERR_PEER_SAFI_CONFLICT
;
2231 /* Nothing to do if we've already activated this peer */
2232 if (peer
->afc
[afi
][safi
])
2235 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2238 active
= peer_active(peer
);
2239 peer
->afc
[afi
][safi
] = 1;
2242 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2244 if (!active
&& peer_active(peer
)) {
2245 bgp_timer_set(peer
);
2247 if (peer_established(peer
)) {
2248 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2249 peer
->afc_adv
[afi
][safi
] = 1;
2250 bgp_capability_send(peer
, afi
, safi
,
2252 CAPABILITY_ACTION_SET
);
2253 if (peer
->afc_recv
[afi
][safi
]) {
2254 peer
->afc_nego
[afi
][safi
] = 1;
2255 bgp_announce_route(peer
, afi
, safi
,
2259 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2260 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2261 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2264 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2265 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2266 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2267 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2270 * If we are turning on a AFI/SAFI locally and we've
2271 * started bringing a peer up, we need to tell
2272 * the other peer to restart because we might loose
2273 * configuration here because when the doppelganger
2274 * gets to a established state due to how
2275 * we resolve we could just overwrite the afi/safi
2278 other
= peer
->doppelganger
;
2280 && (other
->status
== OpenSent
2281 || other
->status
== OpenConfirm
)) {
2282 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2283 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2284 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2291 /* Activate the peer or peer group for specified AFI and SAFI. */
2292 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2295 struct peer_group
*group
;
2296 struct listnode
*node
, *nnode
;
2297 struct peer
*tmp_peer
;
2300 /* Nothing to do if we've already activated this peer */
2301 if (peer
->afc
[afi
][safi
])
2306 /* This is a peer-group so activate all of the members of the
2307 * peer-group as well */
2308 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2310 /* Do not activate a peer for both SAFI_UNICAST and
2311 * SAFI_LABELED_UNICAST */
2312 if ((safi
== SAFI_UNICAST
2313 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2314 || (safi
== SAFI_LABELED_UNICAST
2315 && peer
->afc
[afi
][SAFI_UNICAST
]))
2316 return BGP_ERR_PEER_SAFI_CONFLICT
;
2318 peer
->afc
[afi
][safi
] = 1;
2319 group
= peer
->group
;
2321 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2322 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2325 ret
|= peer_activate_af(peer
, afi
, safi
);
2328 /* If this is the first peer to be activated for this
2329 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2330 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2331 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2333 if (BGP_DEBUG(zebra
, ZEBRA
))
2335 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2337 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2338 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2341 if (safi
== SAFI_FLOWSPEC
) {
2342 /* connect to table manager */
2343 bgp_zebra_init_tm_connect(bgp
);
2348 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2351 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2352 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2353 __func__
, peer
->host
);
2357 /* Nothing to do if we've already deactivated this peer */
2358 if (!peer
->afc
[afi
][safi
])
2361 /* De-activate the address family configuration. */
2362 peer
->afc
[afi
][safi
] = 0;
2364 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2365 flog_err(EC_BGP_PEER_DELETE
,
2366 "couldn't delete af structure for peer %s(%s, %s)",
2367 peer
->host
, afi2str(afi
), safi2str(safi
));
2371 if (peer_established(peer
)) {
2372 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2373 peer
->afc_adv
[afi
][safi
] = 0;
2374 peer
->afc_nego
[afi
][safi
] = 0;
2376 if (peer_active_nego(peer
)) {
2377 bgp_capability_send(peer
, afi
, safi
,
2379 CAPABILITY_ACTION_UNSET
);
2380 bgp_clear_route(peer
, afi
, safi
);
2381 peer
->pcount
[afi
][safi
] = 0;
2383 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2384 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2385 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2388 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2389 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2390 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2397 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2400 struct peer_group
*group
;
2401 struct peer
*tmp_peer
;
2402 struct listnode
*node
, *nnode
;
2405 /* Nothing to do if we've already de-activated this peer */
2406 if (!peer
->afc
[afi
][safi
])
2409 /* This is a peer-group so de-activate all of the members of the
2410 * peer-group as well */
2411 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2412 peer
->afc
[afi
][safi
] = 0;
2413 group
= peer
->group
;
2415 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2416 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2419 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2424 /* If this is the last peer to be deactivated for this
2425 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2426 if (safi
== SAFI_LABELED_UNICAST
2427 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2428 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2430 if (BGP_DEBUG(zebra
, ZEBRA
))
2432 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2434 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2435 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2440 void peer_nsf_stop(struct peer
*peer
)
2445 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2446 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2448 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2449 peer
->nsf
[afi
][safi
] = 0;
2450 THREAD_OFF(peer
->t_llgr_stale
[afi
][safi
]);
2453 if (peer
->t_gr_restart
) {
2454 THREAD_OFF(peer
->t_gr_restart
);
2455 if (bgp_debug_neighbor_events(peer
))
2456 zlog_debug("%pBP graceful restart timer stopped", peer
);
2458 if (peer
->t_gr_stale
) {
2459 THREAD_OFF(peer
->t_gr_stale
);
2460 if (bgp_debug_neighbor_events(peer
))
2462 "%pBP graceful restart stalepath timer stopped",
2465 bgp_clear_route_all(peer
);
2468 /* Delete peer from confguration.
2470 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2471 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2473 * This function /should/ take care to be idempotent, to guard against
2474 * it being called multiple times through stray events that come in
2475 * that happen to result in this function being called again. That
2476 * said, getting here for a "Deleted" peer is a bug in the neighbour
2479 int peer_delete(struct peer
*peer
)
2485 struct bgp_filter
*filter
;
2486 struct listnode
*pn
;
2489 assert(peer
->status
!= Deleted
);
2492 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2494 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2496 bgp_keepalives_off(peer
);
2497 bgp_reads_off(peer
);
2498 bgp_writes_off(peer
);
2499 thread_cancel_event_ready(bm
->master
, peer
);
2500 FOREACH_AFI_SAFI (afi
, safi
)
2501 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
2502 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2503 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2504 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2506 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2507 peer_nsf_stop(peer
);
2509 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2511 /* Remove BFD settings. */
2512 if (peer
->bfd_config
)
2513 bgp_peer_remove_bfd_config(peer
);
2515 /* If this peer belongs to peer group, clear up the
2518 if (peer_dynamic_neighbor(peer
))
2519 peer_drop_dynamic_neighbor(peer
);
2521 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2523 peer
); /* group->peer list reference */
2524 list_delete_node(peer
->group
->peer
, pn
);
2529 /* Withdraw all information from routing table. We can not use
2530 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2531 * executed after peer structure is deleted.
2533 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2535 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2537 if (peer
->doppelganger
) {
2538 peer
->doppelganger
->doppelganger
= NULL
;
2539 peer
->doppelganger
= NULL
;
2542 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2543 bgp_fsm_change_status(peer
, Deleted
);
2545 /* Remove from NHT */
2546 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2547 bgp_unlink_nexthop_by_peer(peer
);
2549 /* Password configuration */
2550 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2551 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2552 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2553 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2554 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2555 bgp_md5_unset(peer
);
2558 bgp_timer_set(peer
); /* stops all timers for Deleted */
2560 /* Delete from all peer list. */
2561 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2562 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2564 * Removing from the list node first because
2565 * peer_unlock *can* call peer_delete( I know,
2566 * I know ). So let's remove it and in
2567 * the su recalculate function we'll ensure
2568 * it's in there or not.
2570 list_delete_node(bgp
->peer
, pn
);
2571 hash_release(bgp
->peerhash
, peer
);
2572 peer_unlock(peer
); /* bgp peer list reference */
2577 stream_fifo_free(peer
->ibuf
);
2582 stream_fifo_free(peer
->obuf
);
2586 if (peer
->ibuf_work
) {
2587 ringbuf_del(peer
->ibuf_work
);
2588 peer
->ibuf_work
= NULL
;
2591 if (peer
->obuf_work
) {
2592 stream_free(peer
->obuf_work
);
2593 peer
->obuf_work
= NULL
;
2596 if (peer
->scratch
) {
2597 stream_free(peer
->scratch
);
2598 peer
->scratch
= NULL
;
2601 /* Local and remote addresses. */
2602 if (peer
->su_local
) {
2603 sockunion_free(peer
->su_local
);
2604 peer
->su_local
= NULL
;
2607 if (peer
->su_remote
) {
2608 sockunion_free(peer
->su_remote
);
2609 peer
->su_remote
= NULL
;
2612 /* Free filter related memory. */
2613 FOREACH_AFI_SAFI (afi
, safi
) {
2614 filter
= &peer
->filter
[afi
][safi
];
2616 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2617 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2618 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2619 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2622 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2623 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2626 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2627 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2628 ecommunity_free(&peer
->soo
[afi
][safi
]);
2631 FOREACH_AFI_SAFI (afi
, safi
)
2632 peer_af_delete(peer
, afi
, safi
);
2634 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2635 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2636 XFREE(MTYPE_BGP_SOFT_VERSION
, peer
->soft_version
);
2638 peer_unlock(peer
); /* initial reference */
2643 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2645 return strcmp(g1
->name
, g2
->name
);
2648 /* Peer group cofiguration. */
2649 static struct peer_group
*peer_group_new(void)
2651 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2654 static void peer_group_free(struct peer_group
*group
)
2656 XFREE(MTYPE_PEER_GROUP
, group
);
2659 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2661 struct peer_group
*group
;
2662 struct listnode
*node
, *nnode
;
2664 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2665 if (strcmp(group
->name
, name
) == 0)
2671 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2673 struct peer_group
*group
;
2677 group
= peer_group_lookup(bgp
, name
);
2681 group
= peer_group_new();
2683 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2684 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2685 group
->peer
= list_new();
2686 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2687 group
->listen_range
[afi
] = list_new();
2688 group
->conf
= peer_new(bgp
);
2689 FOREACH_AFI_SAFI (afi
, safi
) {
2690 if (bgp
->default_af
[afi
][safi
])
2691 group
->conf
->afc
[afi
][safi
] = 1;
2693 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2694 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2695 group
->conf
->group
= group
;
2696 group
->conf
->as
= 0;
2697 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2698 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2699 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2700 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2701 listnode_add_sort(bgp
->group
, group
);
2706 static void peer_group2peer_config_copy(struct peer_group
*group
,
2711 bool config_node
= !!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2717 peer
->as
= conf
->as
;
2720 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2721 peer
->change_local_as
= conf
->change_local_as
;
2723 /* If peer-group has configured TTL then override it */
2724 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2725 peer
->ttl
= conf
->ttl
;
2728 peer
->gtsm_hops
= conf
->gtsm_hops
;
2730 /* peer flags apply */
2731 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2732 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2733 flags_tmp
&= ~peer
->flags_override
;
2735 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2736 SET_FLAG(peer
->flags
, flags_tmp
);
2737 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2740 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2742 /* peer timers apply */
2743 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2744 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2745 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2748 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2749 PEER_ATTR_INHERIT(peer
, group
, connect
);
2750 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2751 peer
->v_connect
= conf
->connect
;
2753 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2756 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2757 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2758 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2759 peer
->v_delayopen
= conf
->delayopen
;
2761 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2764 /* advertisement-interval apply */
2765 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2766 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2767 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2768 peer
->v_routeadv
= conf
->routeadv
;
2770 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2771 ? BGP_DEFAULT_IBGP_ROUTEADV
2772 : BGP_DEFAULT_EBGP_ROUTEADV
;
2775 /* capability extended-nexthop apply */
2776 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2777 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2778 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2780 /* capability software-version apply */
2781 if (!CHECK_FLAG(peer
->flags_override
,
2782 PEER_FLAG_CAPABILITY_SOFT_VERSION
))
2783 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_SOFT_VERSION
))
2784 SET_FLAG(peer
->flags
,
2785 PEER_FLAG_CAPABILITY_SOFT_VERSION
);
2787 /* password apply */
2788 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2789 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2790 MTYPE_PEER_PASSWORD
);
2792 if (!BGP_PEER_SU_UNSPEC(peer
))
2795 /* update-source apply */
2796 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2797 if (conf
->update_source
) {
2798 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2799 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2800 } else if (conf
->update_if
) {
2801 sockunion_free(peer
->update_source
);
2802 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2803 MTYPE_PEER_UPDATE_SOURCE
);
2808 PEER_ATTR_INHERIT(peer
, group
, local_role
);
2810 /* Update GR flags for the peer. */
2811 bgp_peer_gr_flags_update(peer
);
2813 /* Apply BFD settings from group to peer if it exists. */
2814 if (conf
->bfd_config
) {
2815 bgp_peer_configure_bfd(peer
, false);
2816 bgp_peer_config_apply(peer
, group
);
2820 /* Peer group's remote AS configuration. */
2821 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2824 struct peer_group
*group
;
2826 struct listnode
*node
, *nnode
;
2828 group
= peer_group_lookup(bgp
, group_name
);
2832 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2836 /* When we setup peer-group AS number all peer group member's AS
2837 number must be updated to same number. */
2838 peer_as_change(group
->conf
, *as
, as_type
);
2840 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2841 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2842 || (peer
->as_type
!= as_type
))
2843 peer_as_change(peer
, *as
, as_type
);
2849 void peer_notify_unconfig(struct peer
*peer
)
2851 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2852 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2853 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2856 static void peer_notify_shutdown(struct peer
*peer
)
2858 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
2859 if (bgp_debug_neighbor_events(peer
))
2861 "%pBP configured Graceful-Restart, skipping shutdown notification",
2866 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2867 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2868 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
2871 void peer_group_notify_unconfig(struct peer_group
*group
)
2873 struct peer
*peer
, *other
;
2874 struct listnode
*node
, *nnode
;
2876 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2877 other
= peer
->doppelganger
;
2878 if (other
&& other
->status
!= Deleted
) {
2879 other
->group
= NULL
;
2880 peer_notify_unconfig(other
);
2882 peer_notify_unconfig(peer
);
2886 int peer_group_delete(struct peer_group
*group
)
2890 struct prefix
*prefix
;
2892 struct listnode
*node
, *nnode
;
2897 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2898 other
= peer
->doppelganger
;
2900 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2901 bgp_zebra_terminate_radv(bgp
, peer
);
2904 if (other
&& other
->status
!= Deleted
) {
2905 other
->group
= NULL
;
2909 list_delete(&group
->peer
);
2911 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2912 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2914 prefix_free(&prefix
);
2916 list_delete(&group
->listen_range
[afi
]);
2919 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2922 if (group
->conf
->bfd_config
)
2923 bgp_peer_remove_bfd_config(group
->conf
);
2925 group
->conf
->group
= NULL
;
2926 peer_delete(group
->conf
);
2928 /* Delete from all peer_group list. */
2929 listnode_delete(bgp
->group
, group
);
2931 peer_group_free(group
);
2936 int peer_group_remote_as_delete(struct peer_group
*group
)
2938 struct peer
*peer
, *other
;
2939 struct listnode
*node
, *nnode
;
2941 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2942 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2945 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2946 other
= peer
->doppelganger
;
2948 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2949 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2953 if (other
&& other
->status
!= Deleted
) {
2954 other
->group
= NULL
;
2958 list_delete_all_node(group
->peer
);
2960 group
->conf
->as
= 0;
2961 group
->conf
->as_type
= AS_UNSPECIFIED
;
2966 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2968 struct prefix
*prefix
;
2969 struct listnode
*node
, *nnode
;
2972 afi
= family2afi(range
->family
);
2974 /* Group needs remote AS configured. */
2975 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2976 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2978 /* Ensure no duplicates. Currently we don't care about overlaps. */
2979 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2980 if (prefix_same(range
, prefix
))
2984 prefix
= prefix_new();
2985 prefix_copy(prefix
, range
);
2986 listnode_add(group
->listen_range
[afi
], prefix
);
2988 /* Update passwords for new ranges */
2989 if (group
->conf
->password
)
2990 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
2995 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2997 struct prefix
*prefix
, prefix2
;
2998 struct listnode
*node
, *nnode
;
3002 afi
= family2afi(range
->family
);
3004 /* Identify the listen range. */
3005 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
3006 if (prefix_same(range
, prefix
))
3011 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
3013 /* Dispose off any dynamic neighbors that exist due to this listen range
3015 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
3016 if (!peer_dynamic_neighbor(peer
))
3019 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
3020 && prefix_match(prefix
, &prefix2
)) {
3021 if (bgp_debug_neighbor_events(peer
))
3023 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
3024 peer
->host
, group
->name
, prefix
);
3029 /* Get rid of the listen range */
3030 listnode_delete(group
->listen_range
[afi
], prefix
);
3032 /* Remove passwords for deleted ranges */
3033 if (group
->conf
->password
)
3034 bgp_md5_unset_prefix(group
->bgp
, prefix
);
3039 /* Bind specified peer to peer group. */
3040 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
3041 struct peer_group
*group
, as_t
*as
)
3043 int first_member
= 0;
3046 enum bgp_peer_sort ptype
, gtype
;
3048 /* Lookup the peer. */
3050 peer
= peer_lookup(bgp
, su
);
3052 /* The peer exist, bind it to the peer-group */
3054 /* When the peer already belongs to a peer-group, check the
3056 if (peer_group_active(peer
)) {
3058 /* The peer is already bound to the peer-group,
3061 if (strcmp(peer
->group
->name
, group
->name
) == 0)
3064 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
3067 /* The peer has not specified a remote-as, inherit it from the
3069 if (peer
->as_type
== AS_UNSPECIFIED
) {
3070 peer
->as_type
= group
->conf
->as_type
;
3071 peer
->as
= group
->conf
->as
;
3072 peer
->sort
= group
->conf
->sort
;
3075 ptype
= peer_sort(peer
);
3076 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
3077 gtype
= peer_sort(group
->conf
);
3078 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
3081 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
3084 if (gtype
== BGP_PEER_INTERNAL
)
3088 peer_group2peer_config_copy(group
, peer
);
3090 FOREACH_AFI_SAFI (afi
, safi
) {
3091 if (group
->conf
->afc
[afi
][safi
]) {
3092 peer
->afc
[afi
][safi
] = 1;
3094 if (peer_af_find(peer
, afi
, safi
)
3095 || peer_af_create(peer
, afi
, safi
)) {
3096 peer_group2peer_config_copy_af(
3097 group
, peer
, afi
, safi
);
3099 } else if (peer
->afc
[afi
][safi
])
3100 peer_deactivate(peer
, afi
, safi
);
3104 assert(group
&& peer
->group
== group
);
3106 listnode_delete(bgp
->peer
, peer
);
3108 peer
->group
= group
;
3109 listnode_add_sort(bgp
->peer
, peer
);
3111 peer
= peer_lock(peer
); /* group->peer list reference */
3112 listnode_add(group
->peer
, peer
);
3116 gtype
= peer_sort(group
->conf
);
3117 /* Advertisement-interval reset */
3118 if (!CHECK_FLAG(group
->conf
->flags
,
3119 PEER_FLAG_ROUTEADV
)) {
3120 group
->conf
->v_routeadv
=
3121 (gtype
== BGP_PEER_IBGP
)
3122 ? BGP_DEFAULT_IBGP_ROUTEADV
3123 : BGP_DEFAULT_EBGP_ROUTEADV
;
3126 /* ebgp-multihop reset */
3127 if (gtype
== BGP_PEER_IBGP
)
3128 group
->conf
->ttl
= MAXTTL
;
3131 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3133 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3134 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3135 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3136 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3138 bgp_session_reset(peer
);
3142 /* Create a new peer. */
3144 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3145 && (!group
->conf
->as
)) {
3146 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3149 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3150 group
->conf
->as_type
, group
, true);
3152 peer
= peer_lock(peer
); /* group->peer list reference */
3153 listnode_add(group
->peer
, peer
);
3155 peer_group2peer_config_copy(group
, peer
);
3157 /* If the peer-group is active for this afi/safi then activate
3159 FOREACH_AFI_SAFI (afi
, safi
) {
3160 if (group
->conf
->afc
[afi
][safi
]) {
3161 peer
->afc
[afi
][safi
] = 1;
3163 if (!peer_af_find(peer
, afi
, safi
))
3164 peer_af_create(peer
, afi
, safi
);
3166 peer_group2peer_config_copy_af(group
, peer
, afi
,
3168 } else if (peer
->afc
[afi
][safi
])
3169 peer_deactivate(peer
, afi
, safi
);
3172 /* Set up peer's events and timers. */
3173 if (peer_active(peer
))
3174 bgp_timer_set(peer
);
3180 static void bgp_startup_timer_expire(struct thread
*thread
)
3184 bgp
= THREAD_ARG(thread
);
3185 bgp
->t_startup
= NULL
;
3189 * On shutdown we call the cleanup function which
3190 * does a free of the link list nodes, free up
3191 * the data we are pointing at too.
3193 static void bgp_vrf_string_name_delete(void *data
)
3197 XFREE(MTYPE_TMP
, vname
);
3200 /* BGP instance creation by `router bgp' commands. */
3201 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3202 enum bgp_instance_type inst_type
)
3208 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3210 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3211 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3212 zlog_debug("Creating Default VRF, AS %u", *as
);
3214 zlog_debug("Creating %s %s, AS %u",
3215 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3221 /* Default the EVPN VRF to the default one */
3222 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3229 bgp
->allow_martian
= false;
3230 bgp_process_queue_init(bgp
);
3231 bgp
->heuristic_coalesce
= true;
3232 bgp
->inst_type
= inst_type
;
3233 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3235 bgp
->peer_self
= peer_new(bgp
);
3236 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3237 bgp
->peer_self
->host
=
3238 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3239 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3240 if (cmd_hostname_get())
3241 bgp
->peer_self
->hostname
=
3242 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3244 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3245 if (cmd_domainname_get())
3246 bgp
->peer_self
->domainname
=
3247 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3248 bgp
->peer
= list_new();
3249 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3250 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3252 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3254 bgp
->group
= list_new();
3255 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3257 FOREACH_AFI_SAFI (afi
, safi
) {
3258 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3259 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3260 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3262 /* Enable maximum-paths */
3263 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3265 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3267 /* Initialize graceful restart info */
3268 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3269 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3270 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3271 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3272 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3275 bgp
->v_update_delay
= bm
->v_update_delay
;
3276 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3277 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3278 bgp
->default_subgroup_pkt_queue_max
=
3279 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3280 bgp_tcp_keepalive_unset(bgp
);
3281 bgp_timers_unset(bgp
);
3282 bgp
->default_min_holdtime
= 0;
3283 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3284 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3285 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3286 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3287 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3288 bgp
->dynamic_neighbors_count
= 0;
3289 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3290 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3291 bgp
->reject_as_sets
= false;
3292 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3293 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3294 bgp
->fast_convergence
= false;
3296 bgp
->llgr_stale_time
= BGP_DEFAULT_LLGR_STALE_TIME
;
3298 #ifdef ENABLE_BGP_VNC
3299 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3300 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3302 assert(bgp
->rfapi_cfg
);
3304 #endif /* ENABLE_BGP_VNC */
3306 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3307 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3308 bgp
->vpn_policy
[afi
].afi
= afi
;
3309 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3310 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3313 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3314 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3315 bgp_vrf_string_name_delete
;
3316 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3317 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3318 bgp_vrf_string_name_delete
;
3319 SET_FLAG(bgp
->af_flags
[afi
][SAFI_MPLS_VPN
],
3320 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL
);
3323 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3325 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3326 bgp
->restart_time
, &bgp
->t_startup
);
3328 /* printable name we can use in debug messages */
3329 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3330 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3340 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3342 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3343 snprintf(bgp
->name_pretty
, len
, "%s %s",
3344 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3350 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3351 memory_order_relaxed
);
3352 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3353 memory_order_relaxed
);
3354 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3355 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3359 update_bgp_group_init(bgp
);
3361 /* assign a unique rd id for auto derivation of vrf's RD */
3362 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3364 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3365 sizeof(struct bgp_evpn_info
));
3367 bgp_evpn_vrf_es_init(bgp
);
3371 /*initilize global GR FSM */
3372 bgp_global_gr_init(bgp
);
3374 memset(&bgp
->ebgprequirespolicywarning
, 0,
3375 sizeof(bgp
->ebgprequirespolicywarning
));
3380 /* Return the "default VRF" instance of BGP. */
3381 struct bgp
*bgp_get_default(void)
3384 struct listnode
*node
, *nnode
;
3386 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3387 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3392 /* Lookup BGP entry. */
3393 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3396 struct listnode
*node
, *nnode
;
3398 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3400 && ((bgp
->name
== NULL
&& name
== NULL
)
3401 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3406 /* Lookup BGP structure by view name. */
3407 struct bgp
*bgp_lookup_by_name(const char *name
)
3410 struct listnode
*node
, *nnode
;
3412 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3413 if ((bgp
->name
== NULL
&& name
== NULL
)
3414 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3419 /* Lookup BGP instance based on VRF id. */
3420 /* Note: Only to be used for incoming messages from Zebra. */
3421 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3425 /* Lookup VRF (in tree) and follow link. */
3426 vrf
= vrf_lookup_by_id(vrf_id
);
3429 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3432 /* Sets the BGP instance where EVPN is enabled */
3433 void bgp_set_evpn(struct bgp
*bgp
)
3435 if (bm
->bgp_evpn
== bgp
)
3438 /* First, release the reference count we hold on the instance */
3440 bgp_unlock(bm
->bgp_evpn
);
3444 /* Increase the reference count on this new VRF */
3446 bgp_lock(bm
->bgp_evpn
);
3449 /* Returns the BGP instance where EVPN is enabled, if any */
3450 struct bgp
*bgp_get_evpn(void)
3452 return bm
->bgp_evpn
;
3455 /* handle socket creation or deletion, if necessary
3456 * this is called for all new BGP instances
3458 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3461 struct listnode
*node
;
3464 /* Create BGP server socket, if listen mode not disabled */
3465 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3467 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3469 * suppress vrf socket
3472 bgp_close_vrf_socket(bgp
);
3476 return BGP_ERR_INVALID_VALUE
;
3478 * if vrf_id did not change
3480 if (vrf
->vrf_id
== old_vrf_id
)
3482 if (old_vrf_id
!= VRF_UNKNOWN
) {
3483 /* look for old socket. close it. */
3484 bgp_close_vrf_socket(bgp
);
3486 /* if backend is not yet identified ( VRF_UNKNOWN) then
3487 * creation will be done later
3489 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3491 if (list_isempty(bm
->addresses
)) {
3492 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3493 return BGP_ERR_INVALID_VALUE
;
3495 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3496 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3497 return BGP_ERR_INVALID_VALUE
;
3501 return bgp_check_main_socket(create
, bgp
);
3504 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3505 enum bgp_instance_type inst_type
)
3509 /* Multiple instance check. */
3511 bgp
= bgp_lookup_by_name(name
);
3513 bgp
= bgp_get_default();
3517 if (bgp
->as
!= *as
) {
3519 return BGP_ERR_AS_MISMATCH
;
3521 if (bgp
->inst_type
!= inst_type
)
3522 return BGP_ERR_INSTANCE_MISMATCH
;
3530 /* Called from VTY commands. */
3531 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3532 enum bgp_instance_type inst_type
)
3535 struct vrf
*vrf
= NULL
;
3538 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3539 if (ret
|| *bgp_val
)
3542 bgp
= bgp_create(as
, name
, inst_type
);
3545 * view instances will never work inside of a vrf
3546 * as such they must always be in the VRF_DEFAULT
3547 * Also we must set this to something useful because
3548 * of the vrf socket code needing an actual useful
3549 * default value to send to the underlying OS.
3551 * This code is currently ignoring vrf based
3552 * code using the -Z option( and that is probably
3553 * best addressed elsewhere in the code )
3555 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3556 bgp
->vrf_id
= VRF_DEFAULT
;
3558 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3559 bgp_address_init(bgp
);
3560 bgp_tip_hash_init(bgp
);
3564 bgp
->t_rmap_def_originate_eval
= NULL
;
3566 /* If Default instance or VRF, link to the VRF structure, if present. */
3567 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3568 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3569 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3571 bgp_vrf_link(bgp
, vrf
);
3573 /* BGP server socket already processed if BGP instance
3574 * already part of the list
3576 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3577 listnode_add(bm
->bgp
, bgp
);
3579 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3580 if (BGP_DEBUG(zebra
, ZEBRA
))
3581 zlog_debug("%s: Registering BGP instance %s to zebra",
3582 __func__
, bgp
->name_pretty
);
3583 bgp_zebra_instance_register(bgp
);
3589 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3590 vrf_id_t vrf_id
, bool set
)
3594 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3597 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3601 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3603 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3607 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3611 struct list
*red_list
;
3612 struct listnode
*node
;
3613 struct bgp_redist
*red
;
3615 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3616 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3618 red_list
= bgp
->redist
[afi
][i
];
3622 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3623 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3630 * Make BGP instance "up". Applies only to VRFs (non-default) and
3631 * implies the VRF has been learnt from Zebra.
3633 void bgp_instance_up(struct bgp
*bgp
)
3636 struct listnode
*node
, *next
;
3638 bgp_set_redist_vrf_bitmaps(bgp
, true);
3640 /* Register with zebra. */
3641 bgp_zebra_instance_register(bgp
);
3643 /* Kick off any peers that may have been configured. */
3644 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3645 if (!BGP_PEER_START_SUPPRESSED(peer
))
3646 BGP_EVENT_ADD(peer
, BGP_Start
);
3649 /* Process any networks that have been configured. */
3650 bgp_static_add(bgp
);
3654 * Make BGP instance "down". Applies only to VRFs (non-default) and
3655 * implies the VRF has been deleted by Zebra.
3657 void bgp_instance_down(struct bgp
*bgp
)
3660 struct listnode
*node
;
3661 struct listnode
*next
;
3664 if (bgp
->t_rmap_def_originate_eval
) {
3665 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3666 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3670 /* Bring down peers, so corresponding routes are purged. */
3671 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3672 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3673 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3674 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3676 bgp_session_reset(peer
);
3679 /* Purge network and redistributed routes. */
3680 bgp_purge_static_redist_routes(bgp
);
3682 /* Cleanup registered nexthops (flags) */
3683 bgp_cleanup_nexthops(bgp
);
3685 bgp_zebra_instance_deregister(bgp
);
3687 bgp_set_redist_vrf_bitmaps(bgp
, false);
3690 /* Delete BGP instance. */
3691 int bgp_delete(struct bgp
*bgp
)
3694 struct peer_group
*group
;
3695 struct listnode
*node
, *next
;
3700 struct graceful_restart_info
*gr_info
;
3704 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3706 /* make sure we withdraw any exported routes */
3707 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3709 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3712 bgp_vpn_leak_unimport(bgp
);
3714 hook_call(bgp_inst_delete
, bgp
);
3716 FOREACH_AFI_SAFI (afi
, safi
)
3717 THREAD_OFF(bgp
->t_revalidate
[afi
][safi
]);
3719 THREAD_OFF(bgp
->t_condition_check
);
3720 THREAD_OFF(bgp
->t_startup
);
3721 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3722 THREAD_OFF(bgp
->t_update_delay
);
3723 THREAD_OFF(bgp
->t_establish_wait
);
3725 /* Set flag indicating bgp instance delete in progress */
3726 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3728 /* Delete the graceful restart info */
3729 FOREACH_AFI_SAFI (afi
, safi
) {
3732 gr_info
= &bgp
->gr_info
[afi
][safi
];
3735 t
= gr_info
->t_select_deferral
;
3737 void *info
= THREAD_ARG(t
);
3739 XFREE(MTYPE_TMP
, info
);
3741 THREAD_OFF(gr_info
->t_select_deferral
);
3743 t
= gr_info
->t_route_select
;
3745 void *info
= THREAD_ARG(t
);
3747 XFREE(MTYPE_TMP
, info
);
3749 THREAD_OFF(gr_info
->t_route_select
);
3752 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3753 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3754 zlog_debug("Deleting Default VRF");
3756 zlog_debug("Deleting %s %s",
3757 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3763 /* unmap from RT list */
3764 bgp_evpn_vrf_delete(bgp
);
3766 /* unmap bgp vrf label */
3767 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3768 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3771 if (bgp
->t_rmap_def_originate_eval
) {
3772 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3773 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3777 /* Inform peers we're going down. */
3778 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3779 peer_notify_shutdown(peer
);
3781 /* Delete static routes (networks). */
3782 bgp_static_delete(bgp
);
3784 /* Unset redistribution. */
3785 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3786 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3787 if (i
!= ZEBRA_ROUTE_BGP
)
3788 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3790 /* Free peers and peer-groups. */
3791 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3792 peer_group_delete(group
);
3794 while (listcount(bgp
->peer
)) {
3795 peer
= listnode_head(bgp
->peer
);
3799 if (bgp
->peer_self
) {
3800 peer_delete(bgp
->peer_self
);
3801 bgp
->peer_self
= NULL
;
3804 update_bgp_group_free(bgp
);
3806 /* TODO - Other memory may need to be freed - e.g., NHT */
3808 #ifdef ENABLE_BGP_VNC
3811 bgp_cleanup_routes(bgp
);
3813 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3814 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3817 &bgp
->vpn_policy
[afi
]
3818 .import_redirect_rtlist
);
3819 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3822 /* Free any memory allocated to holding routemap references */
3823 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3824 for (enum vpn_policy_direction dir
= 0;
3825 dir
< BGP_VPN_POLICY_DIR_MAX
; ++dir
) {
3826 if (bgp
->vpn_policy
[afi
].rmap_name
[dir
])
3827 XFREE(MTYPE_ROUTE_MAP_NAME
,
3828 bgp
->vpn_policy
[afi
].rmap_name
[dir
]);
3829 bgp
->vpn_policy
[afi
].rmap
[dir
] = NULL
;
3833 /* Deregister from Zebra, if needed */
3834 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3835 if (BGP_DEBUG(zebra
, ZEBRA
))
3837 "%s: deregistering this bgp %s instance from zebra",
3838 __func__
, bgp
->name
);
3839 bgp_zebra_instance_deregister(bgp
);
3842 /* Remove visibility via the master list - there may however still be
3843 * routes to be processed still referencing the struct bgp.
3845 listnode_delete(bm
->bgp
, bgp
);
3847 /* Free interfaces in this instance. */
3850 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3851 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3853 bgp_vrf_unlink(bgp
, vrf
);
3855 /* Update EVPN VRF pointer */
3856 if (bm
->bgp_evpn
== bgp
) {
3857 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3860 bgp_set_evpn(bgp_get_default());
3863 if (bgp
->process_queue
)
3864 work_queue_free_and_null(&bgp
->process_queue
);
3866 thread_master_free_unused(bm
->master
);
3867 bgp_unlock(bgp
); /* initial reference */
3872 void bgp_free(struct bgp
*bgp
)
3876 struct bgp_table
*table
;
3877 struct bgp_dest
*dest
;
3878 struct bgp_rmap
*rmap
;
3882 list_delete(&bgp
->group
);
3883 list_delete(&bgp
->peer
);
3885 if (bgp
->peerhash
) {
3886 hash_free(bgp
->peerhash
);
3887 bgp
->peerhash
= NULL
;
3890 FOREACH_AFI_SAFI (afi
, safi
) {
3891 /* Special handling for 2-level routing tables. */
3892 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3893 || safi
== SAFI_EVPN
) {
3894 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3895 dest
= bgp_route_next(dest
)) {
3896 table
= bgp_dest_get_bgp_table_info(dest
);
3897 bgp_table_finish(&table
);
3900 if (bgp
->route
[afi
][safi
])
3901 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3902 if (bgp
->aggregate
[afi
][safi
])
3903 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3904 if (bgp
->rib
[afi
][safi
])
3905 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3906 rmap
= &bgp
->table_map
[afi
][safi
];
3907 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3910 bgp_scan_finish(bgp
);
3911 bgp_address_destroy(bgp
);
3912 bgp_tip_hash_destroy(bgp
);
3914 /* release the auto RD id */
3915 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3917 bgp_evpn_cleanup(bgp
);
3918 bgp_pbr_cleanup(bgp
);
3919 bgp_srv6_cleanup(bgp
);
3920 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3922 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3923 enum vpn_policy_direction dir
;
3925 if (bgp
->vpn_policy
[afi
].import_vrf
)
3926 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3927 if (bgp
->vpn_policy
[afi
].export_vrf
)
3928 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3930 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3931 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3932 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3933 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3934 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3935 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3938 XFREE(MTYPE_BGP
, bgp
->name
);
3939 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3940 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3942 XFREE(MTYPE_BGP
, bgp
);
3945 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3948 struct listnode
*node
, *nnode
;
3954 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3955 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3956 && !CHECK_FLAG(peer
->sflags
,
3957 PEER_STATUS_ACCEPT_PEER
))
3959 } else if (bm
->bgp
!= NULL
) {
3960 struct listnode
*bgpnode
, *nbgpnode
;
3962 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3963 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3965 && !strcmp(peer
->conf_if
, conf_if
)
3966 && !CHECK_FLAG(peer
->sflags
,
3967 PEER_STATUS_ACCEPT_PEER
))
3973 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3976 struct listnode
*node
, *nnode
;
3982 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3983 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3984 && !CHECK_FLAG(peer
->sflags
,
3985 PEER_STATUS_ACCEPT_PEER
))
3987 } else if (bm
->bgp
!= NULL
) {
3988 struct listnode
*bgpnode
, *nbgpnode
;
3990 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3991 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3993 && !strcmp(peer
->hostname
, hostname
)
3994 && !CHECK_FLAG(peer
->sflags
,
3995 PEER_STATUS_ACCEPT_PEER
))
4001 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
4003 struct peer
*peer
= NULL
;
4004 struct peer tmp_peer
;
4006 memset(&tmp_peer
, 0, sizeof(struct peer
));
4009 * We do not want to find the doppelganger peer so search for the peer
4011 * the hash that has PEER_FLAG_CONFIG_NODE
4013 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
4018 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4019 } else if (bm
->bgp
!= NULL
) {
4020 struct listnode
*bgpnode
, *nbgpnode
;
4022 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
4023 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4032 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
4033 union sockunion
*su
,
4034 struct peer_group
*group
)
4040 /* Create peer first; we've already checked group config is valid. */
4041 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
4042 group
->conf
->as_type
, group
, true);
4047 peer
= peer_lock(peer
);
4048 listnode_add(group
->peer
, peer
);
4050 peer_group2peer_config_copy(group
, peer
);
4053 * Bind peer for all AFs configured for the group. We don't call
4054 * peer_group_bind as that is sub-optimal and does some stuff we don't
4057 FOREACH_AFI_SAFI (afi
, safi
) {
4058 if (!group
->conf
->afc
[afi
][safi
])
4060 peer
->afc
[afi
][safi
] = 1;
4062 if (!peer_af_find(peer
, afi
, safi
))
4063 peer_af_create(peer
, afi
, safi
);
4065 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
4068 /* Mark as dynamic, but also as a "config node" for other things to
4070 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
4076 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
4077 struct prefix
*prefix
)
4079 struct listnode
*node
, *nnode
;
4080 struct prefix
*range
;
4083 afi
= family2afi(prefix
->family
);
4085 if (group
->listen_range
[afi
])
4086 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
4088 if (prefix_match(range
, prefix
))
4095 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
4096 struct prefix
**listen_range
)
4098 struct prefix
*range
= NULL
;
4099 struct peer_group
*group
= NULL
;
4100 struct listnode
*node
, *nnode
;
4102 *listen_range
= NULL
;
4104 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4105 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4108 } else if (bm
->bgp
!= NULL
) {
4109 struct listnode
*bgpnode
, *nbgpnode
;
4111 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4112 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4113 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4119 *listen_range
= range
;
4120 return (group
&& range
) ? group
: NULL
;
4123 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
4125 struct peer_group
*group
;
4128 struct prefix prefix
;
4129 struct prefix
*listen_range
;
4132 if (!sockunion2hostprefix(su
, &prefix
))
4135 /* See if incoming connection matches a configured listen range. */
4136 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
4147 if (bgp_debug_neighbor_events(NULL
))
4149 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4150 &prefix
, group
->name
, listen_range
);
4152 /* Are we within the listen limit? */
4153 dncount
= gbgp
->dynamic_neighbors_count
;
4155 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4156 if (bgp_debug_neighbor_events(NULL
))
4158 "Dynamic Neighbor %pFX rejected - at limit %d",
4159 &prefix
, gbgp
->dynamic_neighbors_limit
);
4163 /* Ensure group is not disabled. */
4164 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4165 if (bgp_debug_neighbor_events(NULL
))
4167 "Dynamic Neighbor %pFX rejected - group %s disabled",
4168 &prefix
, group
->name
);
4172 /* Check that at least one AF is activated for the group. */
4173 if (!peer_group_af_configured(group
)) {
4174 if (bgp_debug_neighbor_events(NULL
))
4176 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4177 &prefix
, group
->name
);
4181 /* Create dynamic peer and bind to associated group. */
4182 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4185 gbgp
->dynamic_neighbors_count
= ++dncount
;
4187 if (bgp_debug_neighbor_events(peer
))
4188 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4189 peer
->host
, group
->name
, dncount
);
4194 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4197 if (peer
->group
->bgp
) {
4198 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4200 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4202 if (bgp_debug_neighbor_events(peer
))
4203 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4204 peer
->group
->name
, dncount
);
4207 bool bgp_path_attribute_discard(struct peer
*peer
, char *buf
, size_t size
)
4214 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4215 if (peer
->discard_attrs
[i
])
4216 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4217 (strlen(buf
) > 0) ? " " : "", i
);
4220 if (strlen(buf
) > 0)
4226 bool bgp_path_attribute_treat_as_withdraw(struct peer
*peer
, char *buf
,
4234 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4235 if (peer
->withdraw_attrs
[i
])
4236 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4237 (strlen(buf
) > 0) ? " " : "", i
);
4240 if (strlen(buf
) > 0)
4246 /* If peer is configured at least one address family return 1. */
4247 bool peer_active(struct peer
*peer
)
4249 if (BGP_PEER_SU_UNSPEC(peer
))
4251 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4252 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4253 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4254 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4255 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4256 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4257 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4258 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4259 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4260 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4261 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4266 /* If peer is negotiated at least one address family return 1. */
4267 bool peer_active_nego(struct peer
*peer
)
4269 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4270 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4271 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4272 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4273 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4274 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4275 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4276 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4277 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4278 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4279 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4280 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4281 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4286 /* If peer received at least one address family MP, return true */
4287 bool peer_afc_received(struct peer
*peer
)
4292 FOREACH_AFI_SAFI (afi
, safi
)
4293 if (peer
->afc_recv
[afi
][safi
])
4299 /* If peer advertised at least one address family MP, return true */
4300 bool peer_afc_advertised(struct peer
*peer
)
4305 FOREACH_AFI_SAFI (afi
, safi
)
4306 if (peer
->afc_adv
[afi
][safi
])
4312 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4313 enum peer_change_type type
)
4315 struct peer_af
*paf
;
4317 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4320 if (!peer_established(peer
))
4323 if (type
== peer_change_reset
) {
4324 /* If we're resetting session, we've to delete both peer struct
4326 if ((peer
->doppelganger
)
4327 && (peer
->doppelganger
->status
!= Deleted
)
4328 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4329 PEER_FLAG_CONFIG_NODE
)))
4330 peer_delete(peer
->doppelganger
);
4332 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4333 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4334 } else if (type
== peer_change_reset_in
) {
4335 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4336 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4337 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4338 BGP_ROUTE_REFRESH_NORMAL
);
4340 if ((peer
->doppelganger
)
4341 && (peer
->doppelganger
->status
!= Deleted
)
4342 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4343 PEER_FLAG_CONFIG_NODE
)))
4344 peer_delete(peer
->doppelganger
);
4346 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4347 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4349 } else if (type
== peer_change_reset_out
) {
4350 paf
= peer_af_find(peer
, afi
, safi
);
4351 if (paf
&& paf
->subgroup
)
4352 SET_FLAG(paf
->subgroup
->sflags
,
4353 SUBGRP_STATUS_FORCE_UPDATES
);
4355 update_group_adjust_peer(paf
);
4356 bgp_announce_route(peer
, afi
, safi
, false);
4360 struct peer_flag_action
{
4364 /* This flag can be set for peer-group member. */
4365 uint8_t not_for_member
;
4367 /* Action when the flag is changed. */
4368 enum peer_change_type type
;
4371 static const struct peer_flag_action peer_flag_action_list
[] = {
4372 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4373 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4374 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4375 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4376 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4377 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4378 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4379 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4380 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4381 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4382 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4383 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4384 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4385 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4386 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4387 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4388 {PEER_FLAG_LOCAL_AS
, 0, peer_change_reset
},
4389 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_reset
},
4390 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_reset
},
4391 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4392 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4393 {PEER_FLAG_EXTENDED_OPT_PARAMS
, 0, peer_change_reset
},
4394 {PEER_FLAG_ROLE_STRICT_MODE
, 0, peer_change_reset
},
4395 {PEER_FLAG_ROLE
, 0, peer_change_reset
},
4396 {PEER_FLAG_PORT
, 0, peer_change_reset
},
4397 {PEER_FLAG_AIGP
, 0, peer_change_none
},
4398 {PEER_FLAG_GRACEFUL_SHUTDOWN
, 0, peer_change_none
},
4399 {PEER_FLAG_CAPABILITY_SOFT_VERSION
, 0, peer_change_reset
},
4402 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4403 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4404 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4405 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4406 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4407 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4408 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4409 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4410 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4411 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4412 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4413 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4414 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4415 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4416 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4417 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4418 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4419 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4420 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4421 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4422 {PEER_FLAG_MAX_PREFIX_OUT
, 0, peer_change_none
},
4423 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4424 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4425 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4426 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4427 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4428 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4429 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4430 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4431 {PEER_FLAG_SOO
, 0, peer_change_reset
},
4432 {PEER_FLAG_ACCEPT_OWN
, 0, peer_change_reset
},
4435 /* Proper action set. */
4436 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4437 int size
, struct peer_flag_action
*action
,
4444 const struct peer_flag_action
*match
= NULL
;
4446 /* Check peer's frag action. */
4447 for (i
= 0; i
< size
; i
++) {
4448 match
= &action_list
[i
];
4450 if (match
->flag
== 0)
4453 if (match
->flag
& flag
) {
4456 if (match
->type
== peer_change_reset_in
)
4458 if (match
->type
== peer_change_reset_out
)
4460 if (match
->type
== peer_change_reset
) {
4464 if (match
->not_for_member
)
4465 action
->not_for_member
= 1;
4469 /* Set peer clear type. */
4470 if (reset_in
&& reset_out
)
4471 action
->type
= peer_change_reset
;
4473 action
->type
= peer_change_reset_in
;
4475 action
->type
= peer_change_reset_out
;
4477 action
->type
= peer_change_none
;
4482 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4484 if (flag
== PEER_FLAG_SHUTDOWN
) {
4485 if (CHECK_FLAG(peer
->flags
, flag
)) {
4486 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4487 peer_nsf_stop(peer
);
4489 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4491 if (peer
->t_pmax_restart
) {
4492 THREAD_OFF(peer
->t_pmax_restart
);
4493 if (bgp_debug_neighbor_events(peer
))
4495 "%pBP Maximum-prefix restart timer canceled",
4499 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4500 char *msg
= peer
->tx_shutdown_message
;
4502 uint8_t msgbuf
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4504 if (!msg
&& peer_group_active(peer
))
4505 msg
= peer
->group
->conf
4506 ->tx_shutdown_message
;
4507 msglen
= msg
? strlen(msg
) : 0;
4508 if (msglen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4509 msglen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4513 memcpy(msgbuf
+ 1, msg
, msglen
);
4515 bgp_notify_send_with_data(
4516 peer
, BGP_NOTIFY_CEASE
,
4517 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4518 msgbuf
, msglen
+ 1);
4521 peer
, BGP_NOTIFY_CEASE
,
4522 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4524 bgp_session_reset(peer
);
4526 peer
->v_start
= BGP_INIT_START_TIMER
;
4527 BGP_EVENT_ADD(peer
, BGP_Stop
);
4529 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4530 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4531 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4532 else if (flag
== PEER_FLAG_PASSIVE
)
4533 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4534 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4535 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4537 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4538 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4540 bgp_session_reset(peer
);
4543 /* Enable global administrative shutdown of all peers of BGP instance */
4544 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4547 struct listnode
*node
;
4548 /* length(1) + message(N) */
4549 uint8_t data
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4551 /* do nothing if already shut down */
4552 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4555 /* informational log message */
4556 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4559 /* iterate through peers of BGP instance */
4560 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4561 /* continue, if peer is already in administrative shutdown. */
4562 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4565 /* send a RFC 4486 notification message if necessary */
4566 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4568 size_t datalen
= strlen(msg
);
4570 if (datalen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4571 datalen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4574 memcpy(data
+ 1, msg
, datalen
);
4576 bgp_notify_send_with_data(
4577 peer
, BGP_NOTIFY_CEASE
,
4578 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
, data
,
4582 peer
, BGP_NOTIFY_CEASE
,
4583 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4587 /* reset start timer to initial value */
4588 peer
->v_start
= BGP_INIT_START_TIMER
;
4590 /* trigger a RFC 4271 ManualStop event */
4591 BGP_EVENT_ADD(peer
, BGP_Stop
);
4594 /* set the BGP instances shutdown flag */
4595 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4598 /* Disable global administrative shutdown of all peers of BGP instance */
4599 void bgp_shutdown_disable(struct bgp
*bgp
)
4601 /* do nothing if not shut down. */
4602 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4605 /* informational log message */
4606 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4609 /* clear the BGP instances shutdown flag */
4610 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4613 /* Change specified peer flag. */
4614 static int peer_flag_modify(struct peer
*peer
, uint64_t flag
, int set
)
4618 bool invert
, member_invert
;
4619 struct peer
*member
;
4620 struct listnode
*node
, *nnode
;
4621 struct peer_flag_action action
;
4623 memset(&action
, 0, sizeof(struct peer_flag_action
));
4624 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4626 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4627 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4630 /* Abort if no flag action exists. */
4632 return BGP_ERR_INVALID_FLAG
;
4634 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4635 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4636 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4637 return BGP_ERR_PEER_FLAG_CONFLICT
;
4639 /* Handle flag updates where desired state matches current state. */
4640 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4641 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4642 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4646 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4647 COND_FLAG(peer
->flags_override
, flag
, invert
);
4652 /* Inherit from peer-group or set/unset flags accordingly. */
4653 if (peer_group_active(peer
) && set
== invert
)
4654 peer_flag_inherit(peer
, flag
);
4656 COND_FLAG(peer
->flags
, flag
, set
);
4658 /* Check if handling a regular peer. */
4659 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4660 /* Update flag override state accordingly. */
4661 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4664 * For the extended next-hop encoding flag we need to turn RAs
4665 * on if flag is being set, but only turn RAs off if the flag
4666 * is being unset on this peer and if this peer is a member of a
4667 * peer-group, the peer-group also doesn't have the flag set.
4669 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4671 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4672 } else if (peer_group_active(peer
)) {
4673 if (!CHECK_FLAG(peer
->group
->conf
->flags
,
4676 bgp_zebra_terminate_radv(peer
->bgp
,
4679 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4682 /* Execute flag action on peer. */
4683 if (action
.type
== peer_change_reset
)
4684 peer_flag_modify_action(peer
, flag
);
4686 /* Skip peer-group mechanics for regular peers. */
4691 * Update peer-group members, unless they are explicitly overriding
4692 * peer-group configuration.
4694 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4695 /* Skip peers with overridden configuration. */
4696 if (CHECK_FLAG(member
->flags_override
, flag
))
4699 /* Check if only member without group is inverted. */
4701 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4703 /* Skip peers with equivalent configuration. */
4704 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4707 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4710 /* Update flag on peer-group member. */
4711 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4713 if (flag
== PEER_FLAG_CAPABILITY_ENHE
&& !member
->conf_if
)
4714 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4715 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4717 /* Execute flag action on peer-group member. */
4718 if (action
.type
== peer_change_reset
)
4719 peer_flag_modify_action(member
, flag
);
4725 int peer_flag_set(struct peer
*peer
, uint64_t flag
)
4727 return peer_flag_modify(peer
, flag
, 1);
4730 int peer_flag_unset(struct peer
*peer
, uint64_t flag
)
4732 return peer_flag_modify(peer
, flag
, 0);
4735 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4736 uint64_t flag
, bool set
)
4740 bool invert
, member_invert
;
4741 struct peer
*member
;
4742 struct listnode
*node
, *nnode
;
4743 struct peer_flag_action action
;
4744 enum bgp_peer_sort ptype
;
4746 memset(&action
, 0, sizeof(struct peer_flag_action
));
4747 size
= sizeof(peer_af_flag_action_list
)
4748 / sizeof(struct peer_flag_action
);
4750 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4751 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4754 /* Abort if flag action exists. */
4756 return BGP_ERR_INVALID_FLAG
;
4758 ptype
= peer_sort(peer
);
4759 /* Special check for reflector client. */
4760 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4761 return BGP_ERR_NOT_INTERNAL_PEER
;
4763 /* Special check for remove-private-AS. */
4764 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4765 return BGP_ERR_REMOVE_PRIVATE_AS
;
4767 /* as-override is not allowed for IBGP peers */
4768 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4769 return BGP_ERR_AS_OVERRIDE
;
4771 /* Handle flag updates where desired state matches current state. */
4772 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4773 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4774 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4779 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4780 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4787 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4788 * if we are setting/unsetting flags which conflict with this flag
4789 * handle accordingly
4791 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4795 * if we are setting NEXTHOP_SELF, we need to unset the
4796 * NEXTHOP_UNCHANGED flag
4798 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4799 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4800 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4801 PEER_FLAG_NEXTHOP_UNCHANGED
);
4805 * if we are unsetting NEXTHOP_SELF, we need to set the
4806 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4808 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4809 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4810 SET_FLAG(peer
->af_flags
[afi
][safi
],
4811 PEER_FLAG_NEXTHOP_UNCHANGED
);
4816 * If the peer is a route server client let's not
4817 * muck with the nexthop on the way out the door
4819 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4821 SET_FLAG(peer
->af_flags
[afi
][safi
],
4822 PEER_FLAG_NEXTHOP_UNCHANGED
);
4824 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4825 PEER_FLAG_NEXTHOP_UNCHANGED
);
4828 /* Inherit from peer-group or set/unset flags accordingly. */
4829 if (peer_group_active(peer
) && set
== invert
)
4830 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4832 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4834 /* Execute action when peer is established. */
4835 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4836 && peer_established(peer
)) {
4837 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4838 bgp_clear_adj_in(peer
, afi
, safi
);
4840 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4841 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4842 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4843 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4844 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4845 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4846 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4847 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4849 peer_change_action(peer
, afi
, safi
, action
.type
);
4853 /* Check if handling a regular peer. */
4854 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4855 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4859 * Update peer-group members, unless they are explicitly
4860 * overriding peer-group configuration.
4862 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4864 /* Skip peers with overridden configuration. */
4865 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4869 /* Check if only member without group is inverted. */
4871 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4875 /* Skip peers with equivalent configuration. */
4876 if (set
!= member_invert
4877 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4880 if (set
== member_invert
4881 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4884 /* Update flag on peer-group member. */
4885 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4886 set
!= member_invert
);
4888 /* Execute flag action on peer-group member. */
4889 if (peer_established(member
)) {
4890 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4891 bgp_clear_adj_in(member
, afi
, safi
);
4893 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4894 member
->last_reset
=
4895 PEER_DOWN_RR_CLIENT_CHANGE
;
4897 == PEER_FLAG_RSERVER_CLIENT
)
4898 member
->last_reset
=
4899 PEER_DOWN_RS_CLIENT_CHANGE
;
4901 == PEER_FLAG_ORF_PREFIX_SM
)
4902 member
->last_reset
=
4903 PEER_DOWN_CAPABILITY_CHANGE
;
4905 == PEER_FLAG_ORF_PREFIX_RM
)
4906 member
->last_reset
=
4907 PEER_DOWN_CAPABILITY_CHANGE
;
4909 peer_change_action(member
, afi
, safi
,
4919 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4921 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4924 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4926 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4930 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4932 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4933 peer
->tx_shutdown_message
=
4934 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4937 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4939 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4943 /* EBGP multihop configuration. */
4944 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4946 struct peer_group
*group
;
4947 struct listnode
*node
, *nnode
;
4950 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4953 /* is there anything to do? */
4954 if (peer
->ttl
== ttl
)
4957 /* see comment in peer_ttl_security_hops_set() */
4958 if (ttl
!= MAXTTL
) {
4959 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4960 group
= peer
->group
;
4961 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4962 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4964 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4966 if (peer1
->sort
== BGP_PEER_IBGP
)
4969 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4970 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4973 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4974 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4980 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4981 if (peer
->sort
!= BGP_PEER_IBGP
) {
4982 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4983 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4984 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4986 bgp_session_reset(peer
);
4988 /* Reconfigure BFD peer with new TTL. */
4989 if (peer
->bfd_config
)
4990 bgp_peer_bfd_update_source(peer
);
4993 group
= peer
->group
;
4994 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4995 if (peer
->sort
== BGP_PEER_IBGP
)
4998 peer
->ttl
= group
->conf
->ttl
;
5000 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5001 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5002 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5004 bgp_session_reset(peer
);
5006 /* Reconfigure BFD peer with new TTL. */
5007 if (peer
->bfd_config
)
5008 bgp_peer_bfd_update_source(peer
);
5014 int peer_ebgp_multihop_unset(struct peer
*peer
)
5016 struct peer_group
*group
;
5017 struct listnode
*node
, *nnode
;
5020 if (peer
->sort
== BGP_PEER_IBGP
)
5023 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
5024 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
5026 if (peer_group_active(peer
))
5027 ttl
= peer
->group
->conf
->ttl
;
5029 ttl
= BGP_DEFAULT_TTL
;
5031 if (ttl
== peer
->ttl
)
5036 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5037 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5038 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5039 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5041 bgp_session_reset(peer
);
5043 /* Reconfigure BFD peer with new TTL. */
5044 if (peer
->bfd_config
)
5045 bgp_peer_bfd_update_source(peer
);
5047 group
= peer
->group
;
5048 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5049 if (peer
->sort
== BGP_PEER_IBGP
)
5052 peer
->ttl
= BGP_DEFAULT_TTL
;
5054 if (peer
->fd
>= 0) {
5055 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5057 peer
, BGP_NOTIFY_CEASE
,
5058 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5060 bgp_session_reset(peer
);
5063 /* Reconfigure BFD peer with new TTL. */
5064 if (peer
->bfd_config
)
5065 bgp_peer_bfd_update_source(peer
);
5071 /* Set Open Policy Role and check its correctness */
5072 int peer_role_set(struct peer
*peer
, uint8_t role
, bool strict_mode
)
5074 struct peer
*member
;
5075 struct listnode
*node
, *nnode
;
5077 peer_flag_set(peer
, PEER_FLAG_ROLE
);
5079 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5080 if (peer
->sort
!= BGP_PEER_EBGP
)
5081 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5083 if (peer
->local_role
== role
) {
5084 if (CHECK_FLAG(peer
->flags
,
5085 PEER_FLAG_ROLE_STRICT_MODE
) &&
5087 /* TODO: Is session restart needed if it was
5090 UNSET_FLAG(peer
->flags
,
5091 PEER_FLAG_ROLE_STRICT_MODE
);
5092 if (!CHECK_FLAG(peer
->flags
,
5093 PEER_FLAG_ROLE_STRICT_MODE
) &&
5095 SET_FLAG(peer
->flags
,
5096 PEER_FLAG_ROLE_STRICT_MODE
);
5097 /* Restart session to throw Role Mismatch
5100 if (peer
->remote_role
== ROLE_UNDEFINED
)
5101 bgp_session_reset(peer
);
5104 peer
->local_role
= role
;
5106 SET_FLAG(peer
->flags
,
5107 PEER_FLAG_ROLE_STRICT_MODE
);
5109 UNSET_FLAG(peer
->flags
,
5110 PEER_FLAG_ROLE_STRICT_MODE
);
5111 bgp_session_reset(peer
);
5117 peer
->local_role
= role
;
5118 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5119 if (member
->sort
!= BGP_PEER_EBGP
)
5120 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5122 if (member
->local_role
== role
) {
5123 if (CHECK_FLAG(member
->flags
,
5124 PEER_FLAG_ROLE_STRICT_MODE
) &&
5126 /* TODO: Is session restart needed if it was
5129 UNSET_FLAG(member
->flags
,
5130 PEER_FLAG_ROLE_STRICT_MODE
);
5131 if (!CHECK_FLAG(member
->flags
,
5132 PEER_FLAG_ROLE_STRICT_MODE
) &&
5134 SET_FLAG(peer
->flags
,
5135 PEER_FLAG_ROLE_STRICT_MODE
);
5136 SET_FLAG(member
->flags
,
5137 PEER_FLAG_ROLE_STRICT_MODE
);
5138 /* Restart session to throw Role Mismatch
5141 if (member
->remote_role
== ROLE_UNDEFINED
)
5142 bgp_session_reset(member
);
5145 member
->local_role
= role
;
5148 SET_FLAG(peer
->flags
,
5149 PEER_FLAG_ROLE_STRICT_MODE
);
5150 SET_FLAG(member
->flags
,
5151 PEER_FLAG_ROLE_STRICT_MODE
);
5153 UNSET_FLAG(member
->flags
,
5154 PEER_FLAG_ROLE_STRICT_MODE
);
5156 bgp_session_reset(member
);
5163 int peer_role_unset(struct peer
*peer
)
5165 struct peer
*member
;
5166 struct listnode
*node
, *nnode
;
5168 peer_flag_unset(peer
, PEER_FLAG_ROLE
);
5170 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5171 return peer_role_set(peer
, ROLE_UNDEFINED
, 0);
5173 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
))
5174 peer_role_set(member
, ROLE_UNDEFINED
, 0);
5179 /* Neighbor description. */
5180 void peer_description_set(struct peer
*peer
, const char *desc
)
5182 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5184 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
5187 void peer_description_unset(struct peer
*peer
)
5189 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5192 /* Neighbor update-source. */
5193 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
5195 struct peer
*member
;
5196 struct listnode
*node
, *nnode
;
5198 /* Set flag and configuration on peer. */
5199 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5200 if (peer
->update_if
) {
5201 if (strcmp(peer
->update_if
, ifname
) == 0)
5203 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5205 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5206 sockunion_free(peer
->update_source
);
5207 peer
->update_source
= NULL
;
5209 /* Check if handling a regular peer. */
5210 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5211 /* Send notification or reset peer depending on state. */
5212 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5213 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5214 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5215 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5217 bgp_session_reset(peer
);
5219 /* Apply new source configuration to BFD session. */
5220 if (peer
->bfd_config
)
5221 bgp_peer_bfd_update_source(peer
);
5223 /* Skip peer-group mechanics for regular peers. */
5228 * Set flag and configuration on all peer-group members, unless they are
5229 * explicitly overriding peer-group configuration.
5231 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5232 /* Skip peers with overridden configuration. */
5233 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5236 /* Skip peers with the same configuration. */
5237 if (member
->update_if
) {
5238 if (strcmp(member
->update_if
, ifname
) == 0)
5240 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5243 /* Set flag and configuration on peer-group member. */
5244 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5245 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5246 sockunion_free(member
->update_source
);
5247 member
->update_source
= NULL
;
5249 /* Send notification or reset peer depending on state. */
5250 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5251 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5252 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5253 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5255 bgp_session_reset(member
);
5257 /* Apply new source configuration to BFD session. */
5258 if (member
->bfd_config
)
5259 bgp_peer_bfd_update_source(member
);
5265 void peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
5267 struct peer
*member
;
5268 struct listnode
*node
, *nnode
;
5270 /* Set flag and configuration on peer. */
5271 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5272 if (peer
->update_source
) {
5273 if (sockunion_cmp(peer
->update_source
, su
) == 0)
5275 sockunion_free(peer
->update_source
);
5277 peer
->update_source
= sockunion_dup(su
);
5278 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5280 /* Check if handling a regular peer. */
5281 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5282 /* Send notification or reset peer depending on state. */
5283 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5284 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5285 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5286 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5288 bgp_session_reset(peer
);
5290 /* Apply new source configuration to BFD session. */
5291 if (peer
->bfd_config
)
5292 bgp_peer_bfd_update_source(peer
);
5294 /* Skip peer-group mechanics for regular peers. */
5299 * Set flag and configuration on all peer-group members, unless they are
5300 * explicitly overriding peer-group configuration.
5302 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5303 /* Skip peers with overridden configuration. */
5304 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5307 /* Skip peers with the same configuration. */
5308 if (member
->update_source
) {
5309 if (sockunion_cmp(member
->update_source
, su
) == 0)
5311 sockunion_free(member
->update_source
);
5314 /* Set flag and configuration on peer-group member. */
5315 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5316 member
->update_source
= sockunion_dup(su
);
5317 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5319 /* Send notification or reset peer depending on state. */
5320 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5321 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5322 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5323 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5325 bgp_session_reset(member
);
5327 /* Apply new source configuration to BFD session. */
5328 if (member
->bfd_config
)
5329 bgp_peer_bfd_update_source(member
);
5333 void peer_update_source_unset(struct peer
*peer
)
5335 struct peer
*member
;
5336 struct listnode
*node
, *nnode
;
5338 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
5341 /* Inherit configuration from peer-group if peer is member. */
5342 if (peer_group_active(peer
)) {
5343 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
5344 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
5345 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
5346 MTYPE_PEER_UPDATE_SOURCE
);
5348 /* Otherwise remove flag and configuration from peer. */
5349 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
5350 sockunion_free(peer
->update_source
);
5351 peer
->update_source
= NULL
;
5352 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5355 /* Check if handling a regular peer. */
5356 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5357 /* Send notification or reset peer depending on state. */
5358 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5359 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5360 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5361 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5363 bgp_session_reset(peer
);
5365 /* Apply new source configuration to BFD session. */
5366 if (peer
->bfd_config
)
5367 bgp_peer_bfd_update_source(peer
);
5369 /* Skip peer-group mechanics for regular peers. */
5374 * Set flag and configuration on all peer-group members, unless they are
5375 * explicitly overriding peer-group configuration.
5377 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5378 /* Skip peers with overridden configuration. */
5379 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5382 /* Skip peers with the same configuration. */
5383 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5384 && !member
->update_source
&& !member
->update_if
)
5387 /* Remove flag and configuration on peer-group member. */
5388 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5389 sockunion_free(member
->update_source
);
5390 member
->update_source
= NULL
;
5391 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5393 /* Send notification or reset peer depending on state. */
5394 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5395 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5396 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5397 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5399 bgp_session_reset(member
);
5401 /* Apply new source configuration to BFD session. */
5402 if (member
->bfd_config
)
5403 bgp_peer_bfd_update_source(member
);
5407 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5408 const char *rmap
, struct route_map
*route_map
)
5410 struct peer
*member
;
5411 struct listnode
*node
, *nnode
;
5412 struct update_subgroup
*subgrp
;
5414 /* Set flag and configuration on peer. */
5415 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5417 subgrp
= peer_subgroup(peer
, afi
, safi
);
5420 if (!peer
->default_rmap
[afi
][safi
].name
5421 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5422 if (peer
->default_rmap
[afi
][safi
].name
)
5423 XFREE(MTYPE_ROUTE_MAP_NAME
,
5424 peer
->default_rmap
[afi
][safi
].name
);
5427 * When there is a change in route-map policy,
5428 * this flow gets triggered. Since, the default
5429 * route is already originated, the flag is set.
5430 * The flag should be unset here,
5431 * to trigger the flow of sending update message.
5434 UNSET_FLAG(subgrp
->sflags
,
5435 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5437 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5438 peer
->default_rmap
[afi
][safi
].name
=
5439 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5440 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5441 route_map_counter_increment(route_map
);
5444 if (peer
->default_rmap
[afi
][safi
].name
)
5445 XFREE(MTYPE_ROUTE_MAP_NAME
,
5446 peer
->default_rmap
[afi
][safi
].name
);
5449 * This is triggered in case of route-map deletion.
5450 * The flag needs to be unset, to trigger the flow
5451 * of sending an update message.
5454 UNSET_FLAG(subgrp
->sflags
,
5455 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5457 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5458 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5459 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5462 /* Check if handling a regular peer. */
5463 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5464 /* Update peer route announcements. */
5465 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5466 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5467 bgp_default_originate(peer
, afi
, safi
, 0);
5468 bgp_announce_route(peer
, afi
, safi
, false);
5471 /* Skip peer-group mechanics for regular peers. */
5476 * Set flag and configuration on all peer-group members, unless they are
5477 * explicitly overriding peer-group configuration.
5479 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5480 /* Skip peers with overridden configuration. */
5481 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5482 PEER_FLAG_DEFAULT_ORIGINATE
))
5485 /* Set flag and configuration on peer-group member. */
5486 SET_FLAG(member
->af_flags
[afi
][safi
],
5487 PEER_FLAG_DEFAULT_ORIGINATE
);
5489 if (member
->default_rmap
[afi
][safi
].name
)
5490 XFREE(MTYPE_ROUTE_MAP_NAME
,
5491 member
->default_rmap
[afi
][safi
].name
);
5492 route_map_counter_decrement(
5493 member
->default_rmap
[afi
][safi
].map
);
5494 member
->default_rmap
[afi
][safi
].name
=
5495 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5496 member
->default_rmap
[afi
][safi
].map
= route_map
;
5497 route_map_counter_increment(route_map
);
5500 /* Update peer route announcements. */
5501 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5502 update_group_adjust_peer(
5503 peer_af_find(member
, afi
, safi
));
5504 bgp_default_originate(member
, afi
, safi
, 0);
5505 bgp_announce_route(member
, afi
, safi
, false);
5512 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5514 struct peer
*member
;
5515 struct listnode
*node
, *nnode
;
5517 /* Inherit configuration from peer-group if peer is member. */
5518 if (peer_group_active(peer
)) {
5519 peer_af_flag_inherit(peer
, afi
, safi
,
5520 PEER_FLAG_DEFAULT_ORIGINATE
);
5521 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5522 default_rmap
[afi
][safi
].name
,
5523 MTYPE_ROUTE_MAP_NAME
);
5524 PEER_ATTR_INHERIT(peer
, peer
->group
,
5525 default_rmap
[afi
][safi
].map
);
5527 /* Otherwise remove flag and configuration from peer. */
5528 peer_af_flag_unset(peer
, afi
, safi
,
5529 PEER_FLAG_DEFAULT_ORIGINATE
);
5530 if (peer
->default_rmap
[afi
][safi
].name
)
5531 XFREE(MTYPE_ROUTE_MAP_NAME
,
5532 peer
->default_rmap
[afi
][safi
].name
);
5533 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5534 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5535 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5538 /* Check if handling a regular peer. */
5539 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5540 /* Update peer route announcements. */
5541 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5542 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5543 bgp_default_originate(peer
, afi
, safi
, 1);
5544 bgp_announce_route(peer
, afi
, safi
, false);
5547 /* Skip peer-group mechanics for regular peers. */
5552 * Remove flag and configuration from all peer-group members, unless
5553 * they are explicitly overriding peer-group configuration.
5555 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5556 /* Skip peers with overridden configuration. */
5557 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5558 PEER_FLAG_DEFAULT_ORIGINATE
))
5561 /* Remove flag and configuration on peer-group member. */
5562 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5563 PEER_FLAG_DEFAULT_ORIGINATE
);
5564 if (member
->default_rmap
[afi
][safi
].name
)
5565 XFREE(MTYPE_ROUTE_MAP_NAME
,
5566 member
->default_rmap
[afi
][safi
].name
);
5567 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5568 member
->default_rmap
[afi
][safi
].name
= NULL
;
5569 member
->default_rmap
[afi
][safi
].map
= NULL
;
5571 /* Update peer route announcements. */
5572 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5573 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5574 bgp_default_originate(member
, afi
, safi
, 1);
5575 bgp_announce_route(member
, afi
, safi
, false);
5582 void peer_port_set(struct peer
*peer
, uint16_t port
)
5585 peer_flag_set(peer
, PEER_FLAG_PORT
);
5588 void peer_port_unset(struct peer
*peer
)
5590 peer
->port
= BGP_PORT_DEFAULT
;
5591 peer_flag_unset(peer
, PEER_FLAG_PORT
);
5594 /* Set the TCP-MSS value in the peer structure,
5595 * This gets applied only after connection reset
5596 * So this value will be used in bgp_connect.
5598 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5600 peer
->tcp_mss
= tcp_mss
;
5601 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5604 /* Reset the TCP-MSS value in the peer structure,
5605 * This gets applied only after connection reset
5606 * So this value will be used in bgp_connect.
5608 void peer_tcp_mss_unset(struct peer
*peer
)
5610 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5615 * Helper function that is called after the name of the policy
5616 * being used by a peer has changed (AF specific). Automatically
5617 * initiates inbound or outbound processing as needed.
5619 void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5623 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5624 if (peer_established(peer
))
5625 bgp_announce_route(peer
, afi
, safi
, false);
5627 if (!peer_established(peer
))
5630 if (bgp_soft_reconfig_in(peer
, afi
, safi
))
5633 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
) ||
5634 CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
)) {
5635 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5636 PEER_CAP_ORF_PREFIX_SM_ADV
) &&
5637 (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5638 PEER_CAP_ORF_PREFIX_RM_RCV
) ||
5639 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5640 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
)))
5641 peer_clear_soft(peer
, afi
, safi
,
5642 BGP_CLEAR_SOFT_IN_ORF_PREFIX
);
5644 bgp_route_refresh_send(
5645 peer
, afi
, safi
, 0, 0, 0,
5646 BGP_ROUTE_REFRESH_NORMAL
);
5652 /* neighbor weight. */
5653 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5655 struct peer
*member
;
5656 struct listnode
*node
, *nnode
;
5658 /* Set flag and configuration on peer. */
5659 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5660 if (peer
->weight
[afi
][safi
] != weight
) {
5661 peer
->weight
[afi
][safi
] = weight
;
5662 peer_on_policy_change(peer
, afi
, safi
, 0);
5665 /* Skip peer-group mechanics for regular peers. */
5666 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5670 * Set flag and configuration on all peer-group members, unless they are
5671 * explicitly overriding peer-group configuration.
5673 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5674 /* Skip peers with overridden configuration. */
5675 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5679 /* Set flag and configuration on peer-group member. */
5680 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5681 if (member
->weight
[afi
][safi
] != weight
) {
5682 member
->weight
[afi
][safi
] = weight
;
5683 peer_on_policy_change(member
, afi
, safi
, 0);
5690 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5692 struct peer
*member
;
5693 struct listnode
*node
, *nnode
;
5695 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5698 /* Inherit configuration from peer-group if peer is member. */
5699 if (peer_group_active(peer
)) {
5700 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5701 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5703 peer_on_policy_change(peer
, afi
, safi
, 0);
5707 /* Remove flag and configuration from peer. */
5708 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5709 peer
->weight
[afi
][safi
] = 0;
5710 peer_on_policy_change(peer
, afi
, safi
, 0);
5712 /* Skip peer-group mechanics for regular peers. */
5713 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5717 * Remove flag and configuration from all peer-group members, unless
5718 * they are explicitly overriding peer-group configuration.
5720 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5721 /* Skip peers with overridden configuration. */
5722 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5726 /* Skip peers where flag is already disabled. */
5727 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5730 /* Remove flag and configuration on peer-group member. */
5731 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5732 member
->weight
[afi
][safi
] = 0;
5733 peer_on_policy_change(member
, afi
, safi
, 0);
5739 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5741 struct peer
*member
;
5742 struct listnode
*node
, *nnode
;
5744 if (keepalive
> UINT16_MAX
)
5745 return BGP_ERR_INVALID_VALUE
;
5747 if (holdtime
> UINT16_MAX
)
5748 return BGP_ERR_INVALID_VALUE
;
5750 if (holdtime
< 3 && holdtime
!= 0)
5751 return BGP_ERR_INVALID_VALUE
;
5753 /* Set flag and configuration on peer. */
5754 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5755 peer
->holdtime
= holdtime
;
5756 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5758 /* Skip peer-group mechanics for regular peers. */
5759 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5763 * Set flag and configuration on all peer-group members, unless they are
5764 * explicitly overriding peer-group configuration.
5766 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5767 /* Skip peers with overridden configuration. */
5768 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5771 /* Set flag and configuration on peer-group member. */
5772 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5773 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5774 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5780 int peer_timers_unset(struct peer
*peer
)
5782 struct peer
*member
;
5783 struct listnode
*node
, *nnode
;
5785 /* Inherit configuration from peer-group if peer is member. */
5786 if (peer_group_active(peer
)) {
5787 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5788 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5789 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5791 /* Otherwise remove flag and configuration from peer. */
5792 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5794 peer
->keepalive
= 0;
5797 /* Skip peer-group mechanics for regular peers. */
5798 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5802 * Remove flag and configuration from all peer-group members, unless
5803 * they are explicitly overriding peer-group configuration.
5805 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5806 /* Skip peers with overridden configuration. */
5807 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5810 /* Remove flag and configuration on peer-group member. */
5811 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5812 member
->holdtime
= 0;
5813 member
->keepalive
= 0;
5819 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5821 struct peer
*member
;
5822 struct listnode
*node
, *nnode
;
5824 if (connect
> UINT16_MAX
)
5825 return BGP_ERR_INVALID_VALUE
;
5827 /* Set flag and configuration on peer. */
5828 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5829 peer
->connect
= connect
;
5830 peer
->v_connect
= connect
;
5832 /* Skip peer-group mechanics for regular peers. */
5833 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5834 if (!peer_established(peer
)) {
5835 if (peer_active(peer
))
5836 BGP_EVENT_ADD(peer
, BGP_Stop
);
5837 BGP_EVENT_ADD(peer
, BGP_Start
);
5842 * Set flag and configuration on all peer-group members, unless they are
5843 * explicitly overriding peer-group configuration.
5845 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5846 /* Skip peers with overridden configuration. */
5847 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5850 /* Set flag and configuration on peer-group member. */
5851 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5852 member
->connect
= connect
;
5853 member
->v_connect
= connect
;
5855 if (!peer_established(member
)) {
5856 if (peer_active(member
))
5857 BGP_EVENT_ADD(member
, BGP_Stop
);
5858 BGP_EVENT_ADD(member
, BGP_Start
);
5865 int peer_timers_connect_unset(struct peer
*peer
)
5867 struct peer
*member
;
5868 struct listnode
*node
, *nnode
;
5870 /* Inherit configuration from peer-group if peer is member. */
5871 if (peer_group_active(peer
)) {
5872 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5873 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5875 /* Otherwise remove flag and configuration from peer. */
5876 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5880 /* Set timer with fallback to default value. */
5882 peer
->v_connect
= peer
->connect
;
5884 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5886 /* Skip peer-group mechanics for regular peers. */
5887 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5888 if (!peer_established(peer
)) {
5889 if (peer_active(peer
))
5890 BGP_EVENT_ADD(peer
, BGP_Stop
);
5891 BGP_EVENT_ADD(peer
, BGP_Start
);
5896 * Remove flag and configuration from all peer-group members, unless
5897 * they are explicitly overriding peer-group configuration.
5899 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5900 /* Skip peers with overridden configuration. */
5901 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5904 /* Remove flag and configuration on peer-group member. */
5905 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5906 member
->connect
= 0;
5907 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5909 if (!peer_established(member
)) {
5910 if (peer_active(member
))
5911 BGP_EVENT_ADD(member
, BGP_Stop
);
5912 BGP_EVENT_ADD(member
, BGP_Start
);
5919 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5921 struct peer
*member
;
5922 struct listnode
*node
, *nnode
;
5925 return BGP_ERR_INVALID_VALUE
;
5927 /* Set flag and configuration on peer. */
5928 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5929 peer
->routeadv
= routeadv
;
5930 peer
->v_routeadv
= routeadv
;
5932 /* Check if handling a regular peer. */
5933 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5934 /* Update peer route announcements. */
5935 update_group_adjust_peer_afs(peer
);
5936 if (peer_established(peer
))
5937 bgp_announce_route_all(peer
);
5939 /* Skip peer-group mechanics for regular peers. */
5944 * Set flag and configuration on all peer-group members, unless they are
5945 * explicitly overriding peer-group configuration.
5947 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5948 /* Skip peers with overridden configuration. */
5949 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5952 /* Set flag and configuration on peer-group member. */
5953 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5954 member
->routeadv
= routeadv
;
5955 member
->v_routeadv
= routeadv
;
5957 /* Update peer route announcements. */
5958 update_group_adjust_peer_afs(member
);
5959 if (peer_established(member
))
5960 bgp_announce_route_all(member
);
5966 int peer_advertise_interval_unset(struct peer
*peer
)
5968 struct peer
*member
;
5969 struct listnode
*node
, *nnode
;
5971 /* Inherit configuration from peer-group if peer is member. */
5972 if (peer_group_active(peer
)) {
5973 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5974 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5976 /* Otherwise remove flag and configuration from peer. */
5977 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5981 /* Set timer with fallback to default value. */
5983 peer
->v_routeadv
= peer
->routeadv
;
5985 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5986 ? BGP_DEFAULT_IBGP_ROUTEADV
5987 : BGP_DEFAULT_EBGP_ROUTEADV
;
5989 /* Check if handling a regular peer. */
5990 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5991 /* Update peer route announcements. */
5992 update_group_adjust_peer_afs(peer
);
5993 if (peer_established(peer
))
5994 bgp_announce_route_all(peer
);
5996 /* Skip peer-group mechanics for regular peers. */
6001 * Remove flag and configuration from all peer-group members, unless
6002 * they are explicitly overriding peer-group configuration.
6004 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6005 /* Skip peers with overridden configuration. */
6006 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
6009 /* Remove flag and configuration on peer-group member. */
6010 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
6011 member
->routeadv
= 0;
6012 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
6013 ? BGP_DEFAULT_IBGP_ROUTEADV
6014 : BGP_DEFAULT_EBGP_ROUTEADV
;
6016 /* Update peer route announcements. */
6017 update_group_adjust_peer_afs(member
);
6018 if (peer_established(member
))
6019 bgp_announce_route_all(member
);
6025 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
6028 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
6030 struct peer
*member
;
6031 struct listnode
*node
;
6033 /* Set peers session attribute flag and timer interval. */
6034 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6035 peer
->delayopen
= delayopen
;
6036 peer
->v_delayopen
= delayopen
;
6038 /* Skip group mechanics for regular peers. */
6039 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6042 /* Set flag and configuration on all peer-group members, unless they are
6043 * explicitly overriding peer-group configuration.
6045 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6046 /* Skip peers with overridden configuration. */
6047 if (CHECK_FLAG(member
->flags_override
,
6048 PEER_FLAG_TIMER_DELAYOPEN
))
6051 /* Set session attribute flag and timer intervals on peer-group
6054 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6055 member
->delayopen
= delayopen
;
6056 member
->v_delayopen
= delayopen
;
6062 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
6063 * DelayOpenTimer interval to the default value.
6065 int peer_timers_delayopen_unset(struct peer
*peer
)
6067 struct peer
*member
;
6068 struct listnode
*node
;
6070 /* Inherit configuration from peer-group if peer is member. */
6071 if (peer_group_active(peer
)) {
6072 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6073 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
6075 /* Otherwise remove session attribute flag and set timer
6076 * interval to default value.
6078 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6079 peer
->delayopen
= peer
->bgp
->default_delayopen
;
6082 /* Set timer value to zero */
6083 peer
->v_delayopen
= 0;
6085 /* Skip peer-group mechanics for regular peers. */
6086 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6089 /* Remove flag and configuration from all peer-group members, unless
6090 * they are explicitly overriding peer-group configuration.
6092 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6093 /* Skip peers with overridden configuration. */
6094 if (CHECK_FLAG(member
->flags_override
,
6095 PEER_FLAG_TIMER_DELAYOPEN
))
6098 /* Remove session attribute flag, reset the timer interval to
6099 * the default value and set the timer value to zero.
6101 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6102 member
->delayopen
= peer
->bgp
->default_delayopen
;
6103 member
->v_delayopen
= 0;
6109 /* neighbor interface */
6110 void peer_interface_set(struct peer
*peer
, const char *str
)
6112 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6113 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
6116 void peer_interface_unset(struct peer
*peer
)
6118 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6122 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6123 int allow_num
, int origin
)
6125 struct peer
*member
;
6126 struct listnode
*node
, *nnode
;
6128 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
6129 return BGP_ERR_INVALID_VALUE
;
6131 /* Set flag and configuration on peer. */
6132 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6134 if (peer
->allowas_in
[afi
][safi
] != 0
6135 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6136 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6137 peer_af_flag_set(peer
, afi
, safi
,
6138 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6139 peer
->allowas_in
[afi
][safi
] = 0;
6140 peer_on_policy_change(peer
, afi
, safi
, 0);
6143 if (peer
->allowas_in
[afi
][safi
] != allow_num
6144 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6145 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6147 peer_af_flag_unset(peer
, afi
, safi
,
6148 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6149 peer
->allowas_in
[afi
][safi
] = allow_num
;
6150 peer_on_policy_change(peer
, afi
, safi
, 0);
6154 /* Skip peer-group mechanics for regular peers. */
6155 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6159 * Set flag and configuration on all peer-group members, unless
6160 * they are explicitly overriding peer-group configuration.
6162 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6163 /* Skip peers with overridden configuration. */
6164 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6165 PEER_FLAG_ALLOWAS_IN
))
6168 /* Set flag and configuration on peer-group member. */
6169 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6171 if (member
->allowas_in
[afi
][safi
] != 0
6172 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
6173 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6174 SET_FLAG(member
->af_flags
[afi
][safi
],
6175 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6176 member
->allowas_in
[afi
][safi
] = 0;
6177 peer_on_policy_change(peer
, afi
, safi
, 0);
6180 if (member
->allowas_in
[afi
][safi
] != allow_num
6181 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
6182 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6183 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6184 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6185 member
->allowas_in
[afi
][safi
] = allow_num
;
6186 peer_on_policy_change(peer
, afi
, safi
, 0);
6194 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6196 struct peer
*member
;
6197 struct listnode
*node
, *nnode
;
6199 /* Skip peer if flag is already disabled. */
6200 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
6203 /* Inherit configuration from peer-group if peer is member. */
6204 if (peer_group_active(peer
)) {
6205 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6206 peer_af_flag_inherit(peer
, afi
, safi
,
6207 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6208 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
6209 peer_on_policy_change(peer
, afi
, safi
, 0);
6214 /* Remove flag and configuration from peer. */
6215 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6216 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6217 peer
->allowas_in
[afi
][safi
] = 0;
6218 peer_on_policy_change(peer
, afi
, safi
, 0);
6220 /* Skip peer-group mechanics if handling a regular peer. */
6221 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6225 * Remove flags and configuration from all peer-group members, unless
6226 * they are explicitly overriding peer-group configuration.
6228 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6229 /* Skip peers with overridden configuration. */
6230 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6231 PEER_FLAG_ALLOWAS_IN
))
6234 /* Remove flags and configuration on peer-group member. */
6235 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6236 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6237 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6238 member
->allowas_in
[afi
][safi
] = 0;
6239 peer_on_policy_change(member
, afi
, safi
, 0);
6245 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
6248 bool old_no_prepend
, old_replace_as
;
6249 struct bgp
*bgp
= peer
->bgp
;
6250 struct peer
*member
;
6251 struct listnode
*node
, *nnode
;
6254 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
6256 /* Save previous flag states. */
6258 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6260 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6262 /* Set flag and configuration on peer. */
6263 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
6264 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
6265 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
6267 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
6268 && old_replace_as
== replace_as
)
6270 peer
->change_local_as
= as
;
6271 (void)peer_sort(peer
);
6273 /* Check if handling a regular peer. */
6274 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6278 * Set flag and configuration on all peer-group members, unless they are
6279 * explicitly overriding peer-group configuration.
6281 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6282 /* Skip peers with overridden configuration. */
6283 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6286 /* Skip peers with the same configuration. */
6287 old_no_prepend
= CHECK_FLAG(member
->flags
,
6288 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6289 old_replace_as
= CHECK_FLAG(member
->flags
,
6290 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6291 if (member
->change_local_as
== as
6292 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
6293 && old_no_prepend
== no_prepend
6294 && old_replace_as
== replace_as
)
6297 /* Set flag and configuration on peer-group member. */
6298 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6299 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
6301 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
6303 member
->change_local_as
= as
;
6309 int peer_local_as_unset(struct peer
*peer
)
6311 struct peer
*member
;
6312 struct listnode
*node
, *nnode
;
6314 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
6317 /* Inherit configuration from peer-group if peer is member. */
6318 if (peer_group_active(peer
)) {
6319 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
6320 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6321 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6322 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
6324 /* Otherwise remove flag and configuration from peer. */
6325 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
6326 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6327 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6328 peer
->change_local_as
= 0;
6331 /* Check if handling a regular peer. */
6332 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6333 /* Send notification or stop peer depending on state. */
6334 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6335 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6336 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6337 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6339 BGP_EVENT_ADD(peer
, BGP_Stop
);
6341 /* Skip peer-group mechanics for regular peers. */
6346 * Remove flag and configuration from all peer-group members, unless
6347 * they are explicitly overriding peer-group configuration.
6349 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6350 /* Skip peers with overridden configuration. */
6351 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6354 /* Remove flag and configuration on peer-group member. */
6355 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6356 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6357 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6358 member
->change_local_as
= 0;
6360 /* Send notification or stop peer depending on state. */
6361 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6362 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6363 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6364 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6366 bgp_session_reset(member
);
6372 /* Set password for authenticating with the peer. */
6373 int peer_password_set(struct peer
*peer
, const char *password
)
6375 struct peer
*member
;
6376 struct listnode
*node
, *nnode
;
6377 int len
= password
? strlen(password
) : 0;
6378 int ret
= BGP_SUCCESS
;
6380 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
6381 return BGP_ERR_INVALID_VALUE
;
6383 /* Set flag and configuration on peer. */
6384 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
6385 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
6387 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6388 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6390 /* Check if handling a regular peer. */
6391 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6392 /* Send notification or reset peer depending on state. */
6393 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6394 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6395 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6397 bgp_session_reset(peer
);
6400 * Attempt to install password on socket and skip peer-group
6403 if (BGP_PEER_SU_UNSPEC(peer
))
6405 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6406 : BGP_ERR_TCPSIG_FAILED
;
6410 * Set flag and configuration on all peer-group members, unless they are
6411 * explicitly overriding peer-group configuration.
6413 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6414 /* Skip peers with overridden configuration. */
6415 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6418 /* Skip peers with the same password. */
6419 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6422 /* Set flag and configuration on peer-group member. */
6423 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6424 if (member
->password
)
6425 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6426 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6428 /* Send notification or reset peer depending on state. */
6429 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6430 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6431 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6433 bgp_session_reset(member
);
6435 /* Attempt to install password on socket. */
6436 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6437 ret
= BGP_ERR_TCPSIG_FAILED
;
6440 /* Set flag and configuration on all peer-group listen ranges */
6441 struct listnode
*ln
;
6444 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6445 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6446 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6447 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6452 int peer_password_unset(struct peer
*peer
)
6454 struct peer
*member
;
6455 struct listnode
*node
, *nnode
;
6457 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6460 /* Inherit configuration from peer-group if peer is member. */
6461 if (peer_group_active(peer
)) {
6462 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6463 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6464 MTYPE_PEER_PASSWORD
);
6466 /* Otherwise remove flag and configuration from peer. */
6467 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6468 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6471 /* Check if handling a regular peer. */
6472 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6473 /* Send notification or reset peer depending on state. */
6474 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6475 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6476 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6478 bgp_session_reset(peer
);
6480 /* Attempt to uninstall password on socket. */
6481 if (!BGP_PEER_SU_UNSPEC(peer
))
6482 bgp_md5_unset(peer
);
6483 /* Skip peer-group mechanics for regular peers. */
6488 * Remove flag and configuration from all peer-group members, unless
6489 * they are explicitly overriding peer-group configuration.
6491 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6492 /* Skip peers with overridden configuration. */
6493 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6496 /* Remove flag and configuration on peer-group member. */
6497 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6498 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6500 /* Send notification or reset peer depending on state. */
6501 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6502 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6503 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6505 bgp_session_reset(member
);
6507 /* Attempt to uninstall password on socket. */
6508 if (!BGP_PEER_SU_UNSPEC(member
))
6509 bgp_md5_unset(member
);
6512 /* Set flag and configuration on all peer-group listen ranges */
6513 struct listnode
*ln
;
6516 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6517 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6518 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6519 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6525 /* Set distribute list to the peer. */
6526 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6529 struct peer
*member
;
6530 struct bgp_filter
*filter
;
6531 struct listnode
*node
, *nnode
;
6533 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6534 return BGP_ERR_INVALID_VALUE
;
6536 /* Set configuration on peer. */
6537 filter
= &peer
->filter
[afi
][safi
];
6538 if (filter
->plist
[direct
].name
)
6539 return BGP_ERR_PEER_FILTER_CONFLICT
;
6540 if (filter
->dlist
[direct
].name
)
6541 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6542 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6543 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6545 /* Check if handling a regular peer. */
6546 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6547 /* Set override-flag and process peer route updates. */
6548 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6549 PEER_FT_DISTRIBUTE_LIST
);
6550 peer_on_policy_change(peer
, afi
, safi
,
6551 (direct
== FILTER_OUT
) ? 1 : 0);
6553 /* Skip peer-group mechanics for regular peers. */
6558 * Set configuration on all peer-group members, un less they are
6559 * explicitly overriding peer-group configuration.
6561 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6562 /* Skip peers with overridden configuration. */
6563 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6564 PEER_FT_DISTRIBUTE_LIST
))
6567 /* Set configuration on peer-group member. */
6568 filter
= &member
->filter
[afi
][safi
];
6569 if (filter
->dlist
[direct
].name
)
6570 XFREE(MTYPE_BGP_FILTER_NAME
,
6571 filter
->dlist
[direct
].name
);
6572 filter
->dlist
[direct
].name
=
6573 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6574 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6576 /* Process peer route updates. */
6577 peer_on_policy_change(member
, afi
, safi
,
6578 (direct
== FILTER_OUT
) ? 1 : 0);
6584 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6586 struct peer
*member
;
6587 struct bgp_filter
*filter
;
6588 struct listnode
*node
, *nnode
;
6590 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6591 return BGP_ERR_INVALID_VALUE
;
6593 /* Unset override-flag unconditionally. */
6594 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6595 PEER_FT_DISTRIBUTE_LIST
);
6597 /* Inherit configuration from peer-group if peer is member. */
6598 if (peer_group_active(peer
)) {
6599 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6600 filter
[afi
][safi
].dlist
[direct
].name
,
6601 MTYPE_BGP_FILTER_NAME
);
6602 PEER_ATTR_INHERIT(peer
, peer
->group
,
6603 filter
[afi
][safi
].dlist
[direct
].alist
);
6605 /* Otherwise remove configuration from peer. */
6606 filter
= &peer
->filter
[afi
][safi
];
6607 if (filter
->dlist
[direct
].name
)
6608 XFREE(MTYPE_BGP_FILTER_NAME
,
6609 filter
->dlist
[direct
].name
);
6610 filter
->dlist
[direct
].name
= NULL
;
6611 filter
->dlist
[direct
].alist
= NULL
;
6614 /* Check if handling a regular peer. */
6615 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6616 /* Process peer route updates. */
6617 peer_on_policy_change(peer
, afi
, safi
,
6618 (direct
== FILTER_OUT
) ? 1 : 0);
6620 /* Skip peer-group mechanics for regular peers. */
6625 * Remove configuration on all peer-group members, unless they are
6626 * explicitly overriding peer-group configuration.
6628 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6629 /* Skip peers with overridden configuration. */
6630 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6631 PEER_FT_DISTRIBUTE_LIST
))
6634 /* Remove configuration on peer-group member. */
6635 filter
= &member
->filter
[afi
][safi
];
6636 if (filter
->dlist
[direct
].name
)
6637 XFREE(MTYPE_BGP_FILTER_NAME
,
6638 filter
->dlist
[direct
].name
);
6639 filter
->dlist
[direct
].name
= NULL
;
6640 filter
->dlist
[direct
].alist
= NULL
;
6642 /* Process peer route updates. */
6643 peer_on_policy_change(member
, afi
, safi
,
6644 (direct
== FILTER_OUT
) ? 1 : 0);
6650 /* Update distribute list. */
6651 static void peer_distribute_update(struct access_list
*access
)
6656 struct listnode
*mnode
, *mnnode
;
6657 struct listnode
*node
, *nnode
;
6660 struct peer_group
*group
;
6661 struct bgp_filter
*filter
;
6663 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6665 update_group_policy_update(bgp
,
6666 BGP_POLICY_DISTRIBUTE_LIST
,
6667 access
->name
, true, 0);
6668 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6669 FOREACH_AFI_SAFI (afi
, safi
) {
6670 filter
= &peer
->filter
[afi
][safi
];
6672 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6674 if (filter
->dlist
[direct
].name
)
6675 filter
->dlist
[direct
]
6676 .alist
= access_list_lookup(
6678 filter
->dlist
[direct
]
6681 filter
->dlist
[direct
].alist
=
6686 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6687 FOREACH_AFI_SAFI (afi
, safi
) {
6688 filter
= &group
->conf
->filter
[afi
][safi
];
6690 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6692 if (filter
->dlist
[direct
].name
)
6693 filter
->dlist
[direct
]
6694 .alist
= access_list_lookup(
6696 filter
->dlist
[direct
]
6699 filter
->dlist
[direct
].alist
=
6704 #ifdef ENABLE_BGP_VNC
6705 vnc_prefix_list_update(bgp
);
6710 /* Set prefix list to the peer. */
6711 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6714 struct peer
*member
;
6715 struct bgp_filter
*filter
;
6716 struct listnode
*node
, *nnode
;
6718 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6719 return BGP_ERR_INVALID_VALUE
;
6721 /* Set configuration on peer. */
6722 filter
= &peer
->filter
[afi
][safi
];
6723 if (filter
->dlist
[direct
].name
)
6724 return BGP_ERR_PEER_FILTER_CONFLICT
;
6725 if (filter
->plist
[direct
].name
)
6726 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6727 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6728 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6730 /* Check if handling a regular peer. */
6731 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6732 /* Set override-flag and process peer route updates. */
6733 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6734 PEER_FT_PREFIX_LIST
);
6735 peer_on_policy_change(peer
, afi
, safi
,
6736 (direct
== FILTER_OUT
) ? 1 : 0);
6738 /* Skip peer-group mechanics for regular peers. */
6743 * Set configuration on all peer-group members, unless they are
6744 * explicitly overriding peer-group configuration.
6746 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6747 /* Skip peers with overridden configuration. */
6748 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6749 PEER_FT_PREFIX_LIST
))
6752 /* Set configuration on peer-group member. */
6753 filter
= &member
->filter
[afi
][safi
];
6754 if (filter
->plist
[direct
].name
)
6755 XFREE(MTYPE_BGP_FILTER_NAME
,
6756 filter
->plist
[direct
].name
);
6757 filter
->plist
[direct
].name
=
6758 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6759 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6761 /* Process peer route updates. */
6762 peer_on_policy_change(member
, afi
, safi
,
6763 (direct
== FILTER_OUT
) ? 1 : 0);
6769 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6772 struct peer
*member
;
6773 struct bgp_filter
*filter
;
6774 struct listnode
*node
, *nnode
;
6776 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6777 return BGP_ERR_INVALID_VALUE
;
6779 /* Unset override-flag unconditionally. */
6780 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6781 PEER_FT_PREFIX_LIST
);
6783 /* Inherit configuration from peer-group if peer is member. */
6784 if (peer_group_active(peer
)) {
6785 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6786 filter
[afi
][safi
].plist
[direct
].name
,
6787 MTYPE_BGP_FILTER_NAME
);
6788 PEER_ATTR_INHERIT(peer
, peer
->group
,
6789 filter
[afi
][safi
].plist
[direct
].plist
);
6791 /* Otherwise remove configuration from peer. */
6792 filter
= &peer
->filter
[afi
][safi
];
6793 if (filter
->plist
[direct
].name
)
6794 XFREE(MTYPE_BGP_FILTER_NAME
,
6795 filter
->plist
[direct
].name
);
6796 filter
->plist
[direct
].name
= NULL
;
6797 filter
->plist
[direct
].plist
= NULL
;
6800 /* Check if handling a regular peer. */
6801 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6802 /* Process peer route updates. */
6803 peer_on_policy_change(peer
, afi
, safi
,
6804 (direct
== FILTER_OUT
) ? 1 : 0);
6806 /* Skip peer-group mechanics for regular peers. */
6811 * Remove configuration on all peer-group members, unless they are
6812 * explicitly overriding peer-group configuration.
6814 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6815 /* Skip peers with overridden configuration. */
6816 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6817 PEER_FT_PREFIX_LIST
))
6820 /* Remove configuration on peer-group member. */
6821 filter
= &member
->filter
[afi
][safi
];
6822 if (filter
->plist
[direct
].name
)
6823 XFREE(MTYPE_BGP_FILTER_NAME
,
6824 filter
->plist
[direct
].name
);
6825 filter
->plist
[direct
].name
= NULL
;
6826 filter
->plist
[direct
].plist
= NULL
;
6828 /* Process peer route updates. */
6829 peer_on_policy_change(member
, afi
, safi
,
6830 (direct
== FILTER_OUT
) ? 1 : 0);
6836 /* Update prefix-list list. */
6837 static void peer_prefix_list_update(struct prefix_list
*plist
)
6839 struct listnode
*mnode
, *mnnode
;
6840 struct listnode
*node
, *nnode
;
6843 struct peer_group
*group
;
6844 struct bgp_filter
*filter
;
6849 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6852 * Update the prefix-list on update groups.
6854 update_group_policy_update(
6855 bgp
, BGP_POLICY_PREFIX_LIST
,
6856 plist
? prefix_list_name(plist
) : NULL
, true, 0);
6858 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6859 FOREACH_AFI_SAFI (afi
, safi
) {
6860 filter
= &peer
->filter
[afi
][safi
];
6862 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6864 if (filter
->plist
[direct
].name
)
6865 filter
->plist
[direct
]
6866 .plist
= prefix_list_lookup(
6868 filter
->plist
[direct
]
6871 filter
->plist
[direct
].plist
=
6875 /* If we touch prefix-list, we need to process
6876 * new updates. This is important for ORF to
6877 * work correctly as well.
6879 if (peer
->afc_nego
[afi
][safi
])
6880 peer_on_policy_change(peer
, afi
, safi
,
6884 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6885 FOREACH_AFI_SAFI (afi
, safi
) {
6886 filter
= &group
->conf
->filter
[afi
][safi
];
6888 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6890 if (filter
->plist
[direct
].name
)
6891 filter
->plist
[direct
]
6892 .plist
= prefix_list_lookup(
6894 filter
->plist
[direct
]
6897 filter
->plist
[direct
].plist
=
6905 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6908 struct peer
*member
;
6909 struct bgp_filter
*filter
;
6910 struct listnode
*node
, *nnode
;
6912 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6913 return BGP_ERR_INVALID_VALUE
;
6915 /* Set configuration on peer. */
6916 filter
= &peer
->filter
[afi
][safi
];
6917 if (filter
->aslist
[direct
].name
)
6918 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6919 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6920 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6922 /* Check if handling a regular peer. */
6923 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6924 /* Set override-flag and process peer route updates. */
6925 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6926 PEER_FT_FILTER_LIST
);
6927 peer_on_policy_change(peer
, afi
, safi
,
6928 (direct
== FILTER_OUT
) ? 1 : 0);
6930 /* Skip peer-group mechanics for regular peers. */
6935 * Set configuration on all peer-group members, unless they are
6936 * explicitly overriding peer-group configuration.
6938 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6939 /* Skip peers with overridden configuration. */
6940 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6941 PEER_FT_FILTER_LIST
))
6944 /* Set configuration on peer-group member. */
6945 filter
= &member
->filter
[afi
][safi
];
6946 if (filter
->aslist
[direct
].name
)
6947 XFREE(MTYPE_BGP_FILTER_NAME
,
6948 filter
->aslist
[direct
].name
);
6949 filter
->aslist
[direct
].name
=
6950 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6951 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6953 /* Process peer route updates. */
6954 peer_on_policy_change(member
, afi
, safi
,
6955 (direct
== FILTER_OUT
) ? 1 : 0);
6961 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6963 struct peer
*member
;
6964 struct bgp_filter
*filter
;
6965 struct listnode
*node
, *nnode
;
6967 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6968 return BGP_ERR_INVALID_VALUE
;
6970 /* Unset override-flag unconditionally. */
6971 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6972 PEER_FT_FILTER_LIST
);
6974 /* Inherit configuration from peer-group if peer is member. */
6975 if (peer_group_active(peer
)) {
6976 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6977 filter
[afi
][safi
].aslist
[direct
].name
,
6978 MTYPE_BGP_FILTER_NAME
);
6979 PEER_ATTR_INHERIT(peer
, peer
->group
,
6980 filter
[afi
][safi
].aslist
[direct
].aslist
);
6982 /* Otherwise remove configuration from peer. */
6983 filter
= &peer
->filter
[afi
][safi
];
6984 if (filter
->aslist
[direct
].name
)
6985 XFREE(MTYPE_BGP_FILTER_NAME
,
6986 filter
->aslist
[direct
].name
);
6987 filter
->aslist
[direct
].name
= NULL
;
6988 filter
->aslist
[direct
].aslist
= NULL
;
6991 /* Check if handling a regular peer. */
6992 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6993 /* Process peer route updates. */
6994 peer_on_policy_change(peer
, afi
, safi
,
6995 (direct
== FILTER_OUT
) ? 1 : 0);
6997 /* Skip peer-group mechanics for regular peers. */
7002 * Remove configuration on all peer-group members, unless they are
7003 * explicitly overriding peer-group configuration.
7005 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7006 /* Skip peers with overridden configuration. */
7007 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7008 PEER_FT_FILTER_LIST
))
7011 /* Remove configuration on peer-group member. */
7012 filter
= &member
->filter
[afi
][safi
];
7013 if (filter
->aslist
[direct
].name
)
7014 XFREE(MTYPE_BGP_FILTER_NAME
,
7015 filter
->aslist
[direct
].name
);
7016 filter
->aslist
[direct
].name
= NULL
;
7017 filter
->aslist
[direct
].aslist
= NULL
;
7019 /* Process peer route updates. */
7020 peer_on_policy_change(member
, afi
, safi
,
7021 (direct
== FILTER_OUT
) ? 1 : 0);
7027 static void peer_aslist_update(const char *aslist_name
)
7032 struct listnode
*mnode
, *mnnode
;
7033 struct listnode
*node
, *nnode
;
7036 struct peer_group
*group
;
7037 struct bgp_filter
*filter
;
7039 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
7040 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
7041 aslist_name
, true, 0);
7043 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7044 FOREACH_AFI_SAFI (afi
, safi
) {
7045 filter
= &peer
->filter
[afi
][safi
];
7047 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7049 if (filter
->aslist
[direct
].name
)
7050 filter
->aslist
[direct
]
7051 .aslist
= as_list_lookup(
7052 filter
->aslist
[direct
]
7055 filter
->aslist
[direct
].aslist
=
7060 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
7061 FOREACH_AFI_SAFI (afi
, safi
) {
7062 filter
= &group
->conf
->filter
[afi
][safi
];
7064 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7066 if (filter
->aslist
[direct
].name
)
7067 filter
->aslist
[direct
]
7068 .aslist
= as_list_lookup(
7069 filter
->aslist
[direct
]
7072 filter
->aslist
[direct
].aslist
=
7080 static void peer_aslist_add(char *aslist_name
)
7082 peer_aslist_update(aslist_name
);
7083 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
7086 static void peer_aslist_del(const char *aslist_name
)
7088 peer_aslist_update(aslist_name
);
7089 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
7093 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
7094 const char *name
, struct route_map
*route_map
)
7096 struct peer
*member
;
7097 struct bgp_filter
*filter
;
7098 struct listnode
*node
, *nnode
;
7100 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7101 return BGP_ERR_INVALID_VALUE
;
7103 /* Set configuration on peer. */
7104 filter
= &peer
->filter
[afi
][safi
];
7105 if (filter
->map
[direct
].name
) {
7106 /* If the neighbor is configured with the same route-map
7107 * again then, ignore the duplicate configuration.
7109 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
7112 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7114 route_map_counter_decrement(filter
->map
[direct
].map
);
7115 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7116 filter
->map
[direct
].map
= route_map
;
7117 route_map_counter_increment(route_map
);
7119 /* Check if handling a regular peer. */
7120 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7121 /* Set override-flag and process peer route updates. */
7122 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
7124 peer_on_policy_change(peer
, afi
, safi
,
7125 (direct
== RMAP_OUT
) ? 1 : 0);
7127 /* Skip peer-group mechanics for regular peers. */
7132 * Set configuration on all peer-group members, unless they are
7133 * explicitly overriding peer-group configuration.
7135 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7136 /* Skip peers with overridden configuration. */
7137 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7141 /* Set configuration on peer-group member. */
7142 filter
= &member
->filter
[afi
][safi
];
7143 if (filter
->map
[direct
].name
)
7144 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7145 route_map_counter_decrement(filter
->map
[direct
].map
);
7146 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7147 filter
->map
[direct
].map
= route_map
;
7148 route_map_counter_increment(route_map
);
7150 /* Process peer route updates. */
7151 peer_on_policy_change(member
, afi
, safi
,
7152 (direct
== RMAP_OUT
) ? 1 : 0);
7157 /* Unset route-map from the peer. */
7158 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
7160 struct peer
*member
;
7161 struct bgp_filter
*filter
;
7162 struct listnode
*node
, *nnode
;
7164 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7165 return BGP_ERR_INVALID_VALUE
;
7167 /* Unset override-flag unconditionally. */
7168 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
7170 /* Inherit configuration from peer-group if peer is member. */
7171 if (peer_group_active(peer
)) {
7172 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7173 filter
[afi
][safi
].map
[direct
].name
,
7174 MTYPE_BGP_FILTER_NAME
);
7175 PEER_ATTR_INHERIT(peer
, peer
->group
,
7176 filter
[afi
][safi
].map
[direct
].map
);
7178 /* Otherwise remove configuration from peer. */
7179 filter
= &peer
->filter
[afi
][safi
];
7180 if (filter
->map
[direct
].name
)
7181 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7182 route_map_counter_decrement(filter
->map
[direct
].map
);
7183 filter
->map
[direct
].name
= NULL
;
7184 filter
->map
[direct
].map
= NULL
;
7187 /* Check if handling a regular peer. */
7188 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7189 /* Process peer route updates. */
7190 peer_on_policy_change(peer
, afi
, safi
,
7191 (direct
== RMAP_OUT
) ? 1 : 0);
7193 /* Skip peer-group mechanics for regular peers. */
7198 * Remove configuration on all peer-group members, unless they are
7199 * explicitly overriding peer-group configuration.
7201 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7202 /* Skip peers with overridden configuration. */
7203 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7207 /* Remove configuration on peer-group member. */
7208 filter
= &member
->filter
[afi
][safi
];
7209 if (filter
->map
[direct
].name
)
7210 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7211 route_map_counter_decrement(filter
->map
[direct
].map
);
7212 filter
->map
[direct
].name
= NULL
;
7213 filter
->map
[direct
].map
= NULL
;
7215 /* Process peer route updates. */
7216 peer_on_policy_change(member
, afi
, safi
,
7217 (direct
== RMAP_OUT
) ? 1 : 0);
7223 /* Set unsuppress-map to the peer. */
7224 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7225 const char *name
, struct route_map
*route_map
)
7227 struct peer
*member
;
7228 struct bgp_filter
*filter
;
7229 struct listnode
*node
, *nnode
;
7231 /* Set configuration on peer. */
7232 filter
= &peer
->filter
[afi
][safi
];
7233 if (filter
->usmap
.name
)
7234 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7235 route_map_counter_decrement(filter
->usmap
.map
);
7236 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7237 filter
->usmap
.map
= route_map
;
7238 route_map_counter_increment(route_map
);
7240 /* Check if handling a regular peer. */
7241 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7242 /* Set override-flag and process peer route updates. */
7243 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
7244 PEER_FT_UNSUPPRESS_MAP
);
7245 peer_on_policy_change(peer
, afi
, safi
, 1);
7247 /* Skip peer-group mechanics for regular peers. */
7252 * Set configuration on all peer-group members, unless they are
7253 * explicitly overriding peer-group configuration.
7255 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7256 /* Skip peers with overridden configuration. */
7257 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7258 PEER_FT_UNSUPPRESS_MAP
))
7261 /* Set configuration on peer-group member. */
7262 filter
= &member
->filter
[afi
][safi
];
7263 if (filter
->usmap
.name
)
7264 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7265 route_map_counter_decrement(filter
->usmap
.map
);
7266 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7267 filter
->usmap
.map
= route_map
;
7268 route_map_counter_increment(route_map
);
7270 /* Process peer route updates. */
7271 peer_on_policy_change(member
, afi
, safi
, 1);
7277 /* Unset route-map from the peer. */
7278 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7280 struct peer
*member
;
7281 struct bgp_filter
*filter
;
7282 struct listnode
*node
, *nnode
;
7284 /* Unset override-flag unconditionally. */
7285 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
7287 /* Inherit configuration from peer-group if peer is member. */
7288 if (peer_group_active(peer
)) {
7289 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7290 filter
[afi
][safi
].usmap
.name
,
7291 MTYPE_BGP_FILTER_NAME
);
7292 PEER_ATTR_INHERIT(peer
, peer
->group
,
7293 filter
[afi
][safi
].usmap
.map
);
7295 /* Otherwise remove configuration from peer. */
7296 filter
= &peer
->filter
[afi
][safi
];
7297 if (filter
->usmap
.name
)
7298 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7299 route_map_counter_decrement(filter
->usmap
.map
);
7300 filter
->usmap
.name
= NULL
;
7301 filter
->usmap
.map
= NULL
;
7304 /* Check if handling a regular peer. */
7305 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7306 /* Process peer route updates. */
7307 peer_on_policy_change(peer
, afi
, safi
, 1);
7309 /* Skip peer-group mechanics for regular peers. */
7314 * Remove configuration on all peer-group members, unless they are
7315 * explicitly overriding peer-group configuration.
7317 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7318 /* Skip peers with overridden configuration. */
7319 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7320 PEER_FT_UNSUPPRESS_MAP
))
7323 /* Remove configuration on peer-group member. */
7324 filter
= &member
->filter
[afi
][safi
];
7325 if (filter
->usmap
.name
)
7326 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7327 route_map_counter_decrement(filter
->usmap
.map
);
7328 filter
->usmap
.name
= NULL
;
7329 filter
->usmap
.map
= NULL
;
7331 /* Process peer route updates. */
7332 peer_on_policy_change(member
, afi
, safi
, 1);
7338 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7340 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7343 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7344 if (peer
->t_pmax_restart
) {
7345 THREAD_OFF(peer
->t_pmax_restart
);
7346 if (bgp_debug_neighbor_events(peer
))
7348 "%pBP Maximum-prefix restart timer cancelled",
7351 BGP_EVENT_ADD(peer
, BGP_Start
);
7355 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7356 uint32_t max
, uint8_t threshold
, int warning
,
7357 uint16_t restart
, bool force
)
7359 struct peer
*member
;
7360 struct listnode
*node
, *nnode
;
7362 /* Set flags and configuration on peer. */
7363 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7366 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7368 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7371 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7373 peer_af_flag_unset(peer
, afi
, safi
,
7374 PEER_FLAG_MAX_PREFIX_WARNING
);
7376 peer
->pmax
[afi
][safi
] = max
;
7377 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7378 peer
->pmax_restart
[afi
][safi
] = restart
;
7380 /* Check if handling a regular peer. */
7381 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7382 /* Re-check if peer violates maximum-prefix. */
7383 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7384 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7386 /* Skip peer-group mechanics for regular peers. */
7391 * Set flags and configuration on all peer-group members, unless they
7392 * are explicitly overriding peer-group configuration.
7394 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7395 /* Skip peers with overridden configuration. */
7396 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7397 PEER_FLAG_MAX_PREFIX
))
7400 /* Set flag and configuration on peer-group member. */
7401 member
->pmax
[afi
][safi
] = max
;
7402 member
->pmax_threshold
[afi
][safi
] = threshold
;
7403 member
->pmax_restart
[afi
][safi
] = restart
;
7406 SET_FLAG(member
->af_flags
[afi
][safi
],
7407 PEER_FLAG_MAX_PREFIX_FORCE
);
7409 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7410 PEER_FLAG_MAX_PREFIX_FORCE
);
7413 SET_FLAG(member
->af_flags
[afi
][safi
],
7414 PEER_FLAG_MAX_PREFIX_WARNING
);
7416 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7417 PEER_FLAG_MAX_PREFIX_WARNING
);
7419 /* Re-check if peer violates maximum-prefix. */
7420 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7421 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7427 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7429 /* Inherit configuration from peer-group if peer is member. */
7430 if (peer_group_active(peer
)) {
7431 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7432 peer_af_flag_inherit(peer
, afi
, safi
,
7433 PEER_FLAG_MAX_PREFIX_FORCE
);
7434 peer_af_flag_inherit(peer
, afi
, safi
,
7435 PEER_FLAG_MAX_PREFIX_WARNING
);
7436 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7437 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7438 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7443 /* Remove flags and configuration from peer. */
7444 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7445 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7446 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7447 peer
->pmax
[afi
][safi
] = 0;
7448 peer
->pmax_threshold
[afi
][safi
] = 0;
7449 peer
->pmax_restart
[afi
][safi
] = 0;
7452 * Remove flags and configuration from all peer-group members, unless
7453 * they are explicitly overriding peer-group configuration.
7455 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7456 struct peer
*member
;
7457 struct listnode
*node
;
7459 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7460 /* Skip peers with overridden configuration. */
7461 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7462 PEER_FLAG_MAX_PREFIX
))
7465 /* Remove flag and configuration on peer-group member.
7467 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7468 PEER_FLAG_MAX_PREFIX
);
7469 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7470 PEER_FLAG_MAX_PREFIX_FORCE
);
7471 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7472 PEER_FLAG_MAX_PREFIX_WARNING
);
7473 member
->pmax
[afi
][safi
] = 0;
7474 member
->pmax_threshold
[afi
][safi
] = 0;
7475 member
->pmax_restart
[afi
][safi
] = 0;
7477 peer_maximum_prefix_clear_overflow(member
);
7480 peer_maximum_prefix_clear_overflow(peer
);
7486 void peer_maximum_prefix_out_refresh_routes(struct peer
*peer
, afi_t afi
,
7489 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
7491 if (peer_established(peer
))
7492 bgp_announce_route(peer
, afi
, safi
, false);
7495 int peer_maximum_prefix_out_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7498 struct peer
*member
;
7499 struct listnode
*node
, *nnode
;
7501 /* Set flag on peer and peer-group member if any */
7502 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7503 /* Set configuration on peer. */
7504 peer
->pmax_out
[afi
][safi
] = max
;
7506 /* Check if handling a regular peer. */
7507 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7508 /* Skip peer-group mechanics for regular peers. */
7509 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7514 * Set flag and configuration on all peer-group members, unless they
7515 * are explicitly overriding peer-group configuration.
7517 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7518 /* Skip peers with overridden configuration. */
7519 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7520 PEER_FLAG_MAX_PREFIX_OUT
))
7523 /* Set configuration on peer-group member. */
7524 member
->pmax_out
[afi
][safi
] = max
;
7526 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7531 int peer_maximum_prefix_out_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7533 struct peer
*member
;
7534 struct listnode
*node
;
7535 /* Inherit configuration from peer-group if peer is member. */
7536 if (peer_group_active(peer
)) {
7537 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7538 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_out
[afi
][safi
]);
7540 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7544 /* Remove flag and configuration from peer. */
7545 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7546 peer
->pmax_out
[afi
][safi
] = 0;
7548 /* Check if handling a regular peer. */
7549 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7550 /* Skip peer-group mechanics for regular peers. */
7551 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7556 * Remove flag and configuration from all peer-group members, unless
7557 * they are explicitly overriding peer-group configuration.
7559 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7560 /* Skip peers with overridden configuration. */
7561 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7562 PEER_FLAG_MAX_PREFIX_OUT
))
7565 /* Remove flag and configuration on peer-group member.
7567 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7568 PEER_FLAG_MAX_PREFIX_OUT
);
7569 member
->pmax_out
[afi
][safi
] = 0;
7571 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7576 int is_ebgp_multihop_configured(struct peer
*peer
)
7578 struct peer_group
*group
;
7579 struct listnode
*node
, *nnode
;
7582 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7583 group
= peer
->group
;
7584 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7585 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7588 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7589 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7590 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7594 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7595 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7601 /* Set # of hops between us and BGP peer. */
7602 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7604 struct peer_group
*group
;
7606 struct listnode
*node
, *nnode
;
7609 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7612 /* We cannot configure ttl-security hops when ebgp-multihop is already
7613 set. For non peer-groups, the check is simple. For peer-groups,
7615 slightly messy, because we need to check both the peer-group
7617 and all peer-group members for any trace of ebgp-multihop
7619 before actually applying the ttl-security rules. Cisco really made a
7620 mess of this configuration parameter, and OpenBGPD got it right.
7623 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7624 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7625 if (is_ebgp_multihop_configured(peer
))
7626 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7628 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7629 peer
->gtsm_hops
= gtsm_hops
;
7631 /* Calling ebgp multihop also resets the session.
7632 * On restart, NHT will get setup correctly as will the
7633 * min & max ttls on the socket. The return value is
7636 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7641 group
= peer
->group
;
7642 group
->conf
->gtsm_hops
= gtsm_hops
;
7643 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7645 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7647 /* Calling ebgp multihop also resets the
7649 * On restart, NHT will get setup correctly as
7651 * min & max ttls on the socket. The return
7655 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7659 /* Post the first gtsm setup or if its ibgp, maxttl setting
7661 * necessary, just set the minttl.
7663 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7664 peer
->gtsm_hops
= gtsm_hops
;
7667 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7668 MAXTTL
+ 1 - gtsm_hops
);
7669 if ((peer
->status
< Established
) && peer
->doppelganger
7670 && (peer
->doppelganger
->fd
>= 0))
7671 sockopt_minttl(peer
->su
.sa
.sa_family
,
7672 peer
->doppelganger
->fd
,
7673 MAXTTL
+ 1 - gtsm_hops
);
7675 group
= peer
->group
;
7676 group
->conf
->gtsm_hops
= gtsm_hops
;
7677 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7679 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7681 /* Change setting of existing peer
7682 * established then change value (may break
7684 * not established yet (teardown session and
7686 * no session then do nothing (will get
7687 * handled by next connection)
7691 != BGP_GTSM_HOPS_DISABLED
)
7693 gpeer
->su
.sa
.sa_family
,
7695 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7696 if ((gpeer
->status
< Established
)
7697 && gpeer
->doppelganger
7698 && (gpeer
->doppelganger
->fd
>= 0))
7699 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7700 gpeer
->doppelganger
->fd
,
7701 MAXTTL
+ 1 - gtsm_hops
);
7709 int peer_ttl_security_hops_unset(struct peer
*peer
)
7711 struct peer_group
*group
;
7712 struct listnode
*node
, *nnode
;
7715 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7717 /* if a peer-group member, then reset to peer-group default rather than
7719 if (peer_group_active(peer
))
7720 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7722 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7724 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7725 /* Invoking ebgp_multihop_set will set the TTL back to the
7727 * value as well as restting the NHT and such. The session is
7730 if (peer
->sort
== BGP_PEER_EBGP
)
7731 ret
= peer_ebgp_multihop_unset(peer
);
7734 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7737 if ((peer
->status
< Established
) && peer
->doppelganger
7738 && (peer
->doppelganger
->fd
>= 0))
7739 sockopt_minttl(peer
->su
.sa
.sa_family
,
7740 peer
->doppelganger
->fd
, 0);
7743 group
= peer
->group
;
7744 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7745 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7746 if (peer
->sort
== BGP_PEER_EBGP
)
7747 ret
= peer_ebgp_multihop_unset(peer
);
7750 sockopt_minttl(peer
->su
.sa
.sa_family
,
7753 if ((peer
->status
< Established
)
7754 && peer
->doppelganger
7755 && (peer
->doppelganger
->fd
>= 0))
7756 sockopt_minttl(peer
->su
.sa
.sa_family
,
7757 peer
->doppelganger
->fd
,
7766 static void peer_reset_message_stats(struct peer
*peer
)
7769 atomic_store_explicit(&peer
->open_in
, 0, memory_order_relaxed
);
7770 atomic_store_explicit(&peer
->open_out
, 0, memory_order_relaxed
);
7771 atomic_store_explicit(&peer
->update_in
, 0,
7772 memory_order_relaxed
);
7773 atomic_store_explicit(&peer
->update_out
, 0,
7774 memory_order_relaxed
);
7775 atomic_store_explicit(&peer
->keepalive_in
, 0,
7776 memory_order_relaxed
);
7777 atomic_store_explicit(&peer
->keepalive_out
, 0,
7778 memory_order_relaxed
);
7779 atomic_store_explicit(&peer
->notify_in
, 0,
7780 memory_order_relaxed
);
7781 atomic_store_explicit(&peer
->notify_out
, 0,
7782 memory_order_relaxed
);
7783 atomic_store_explicit(&peer
->refresh_in
, 0,
7784 memory_order_relaxed
);
7785 atomic_store_explicit(&peer
->refresh_out
, 0,
7786 memory_order_relaxed
);
7787 atomic_store_explicit(&peer
->dynamic_cap_in
, 0,
7788 memory_order_relaxed
);
7789 atomic_store_explicit(&peer
->dynamic_cap_out
, 0,
7790 memory_order_relaxed
);
7795 * If peer clear is invoked in a loop for all peers on the BGP instance,
7796 * it may end up freeing the doppelganger, and if this was the next node
7797 * to the current node, we would end up accessing the freed next node.
7798 * Pass along additional parameter which can be updated if next node
7799 * is freed; only required when walking the peer list on BGP instance.
7801 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7803 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7804 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7805 if (peer_maximum_prefix_clear_overflow(peer
))
7808 peer
->v_start
= BGP_INIT_START_TIMER
;
7809 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7810 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7811 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7813 bgp_session_reset_safe(peer
, nnode
);
7818 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7819 enum bgp_clear_type stype
)
7821 struct peer_af
*paf
;
7823 if (!peer_established(peer
))
7826 if (!peer
->afc
[afi
][safi
])
7827 return BGP_ERR_AF_UNCONFIGURED
;
7829 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7831 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7832 /* Clear the "neighbor x.x.x.x default-originate" flag */
7833 paf
= peer_af_find(peer
, afi
, safi
);
7834 if (paf
&& paf
->subgroup
7835 && CHECK_FLAG(paf
->subgroup
->sflags
,
7836 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7837 UNSET_FLAG(paf
->subgroup
->sflags
,
7838 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7840 bgp_announce_route(peer
, afi
, safi
, false);
7843 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7844 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7845 PEER_CAP_ORF_PREFIX_SM_ADV
)
7846 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7847 PEER_CAP_ORF_PREFIX_RM_RCV
)
7848 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7849 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7850 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7851 uint8_t prefix_type
;
7853 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7854 PEER_CAP_ORF_PREFIX_RM_RCV
))
7855 prefix_type
= ORF_TYPE_PREFIX
;
7857 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7859 if (filter
->plist
[FILTER_IN
].plist
) {
7860 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7861 PEER_STATUS_ORF_PREFIX_SEND
))
7862 bgp_route_refresh_send(
7863 peer
, afi
, safi
, prefix_type
,
7865 BGP_ROUTE_REFRESH_NORMAL
);
7866 bgp_route_refresh_send(
7867 peer
, afi
, safi
, prefix_type
,
7868 REFRESH_IMMEDIATE
, 0,
7869 BGP_ROUTE_REFRESH_NORMAL
);
7871 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7872 PEER_STATUS_ORF_PREFIX_SEND
))
7873 bgp_route_refresh_send(
7874 peer
, afi
, safi
, prefix_type
,
7875 REFRESH_IMMEDIATE
, 1,
7876 BGP_ROUTE_REFRESH_NORMAL
);
7878 bgp_route_refresh_send(
7879 peer
, afi
, safi
, 0, 0, 0,
7880 BGP_ROUTE_REFRESH_NORMAL
);
7886 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7887 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7888 /* If neighbor has soft reconfiguration inbound flag.
7889 Use Adj-RIB-In database. */
7890 if (!bgp_soft_reconfig_in(peer
, afi
, safi
)) {
7891 /* If neighbor has route refresh capability, send route
7893 message to the peer. */
7894 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7895 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7896 bgp_route_refresh_send(
7897 peer
, afi
, safi
, 0, 0, 0,
7898 BGP_ROUTE_REFRESH_NORMAL
);
7900 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7904 if (stype
== BGP_CLEAR_MESSAGE_STATS
)
7905 peer_reset_message_stats(peer
);
7910 /* Display peer uptime.*/
7911 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7914 time_t uptime1
, epoch_tbuf
;
7917 /* If there is no connection has been done before print `never'. */
7920 json_object_string_add(json
, "peerUptime", "never");
7921 json_object_int_add(json
, "peerUptimeMsec", 0);
7923 snprintf(buf
, len
, "never");
7927 /* Get current time. */
7928 uptime1
= monotime(NULL
);
7930 gmtime_r(&uptime1
, &tm
);
7932 if (uptime1
< ONE_DAY_SECOND
)
7933 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7935 else if (uptime1
< ONE_WEEK_SECOND
)
7936 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7938 else if (uptime1
< ONE_YEAR_SECOND
)
7939 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7940 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7942 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7944 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7947 epoch_tbuf
= time(NULL
) - uptime1
;
7948 json_object_string_add(json
, "peerUptime", buf
);
7949 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7950 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7957 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7958 struct list
*addresses
)
7962 memset(&bgp_master
, 0, sizeof(bgp_master
));
7965 bm
->bgp
= list_new();
7966 bm
->listen_sockets
= list_new();
7967 bm
->port
= BGP_PORT_DEFAULT
;
7968 bm
->addresses
= addresses
;
7969 bm
->master
= master
;
7970 bm
->start_time
= monotime(NULL
);
7971 bm
->t_rmap_update
= NULL
;
7972 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7973 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7974 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7975 bm
->terminating
= false;
7976 bm
->socket_buffer
= buffer_size
;
7977 bm
->wait_for_fib
= false;
7978 bm
->tcp_dscp
= IPTOS_PREC_INTERNETCONTROL
;
7979 bm
->inq_limit
= BM_DEFAULT_Q_LIMIT
;
7980 bm
->outq_limit
= BM_DEFAULT_Q_LIMIT
;
7983 /* init the rd id space.
7984 assign 0th index in the bitfield,
7985 so that we start with id 1
7987 bf_init(bm
->rd_idspace
, UINT16_MAX
);
7988 bf_assign_zero_index(bm
->rd_idspace
);
7990 /* mpls label dynamic allocation pool */
7991 bgp_lp_init(bm
->master
, &bm
->labelpool
);
7995 QOBJ_REG(bm
, bgp_master
);
7999 * Free up connected routes and interfaces for a BGP instance. Invoked upon
8000 * instance delete (non-default only) or BGP exit.
8002 static void bgp_if_finish(struct bgp
*bgp
)
8005 struct interface
*ifp
;
8007 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
8009 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
8012 FOR_ALL_INTERFACES (vrf
, ifp
) {
8013 struct listnode
*c_node
, *c_nnode
;
8014 struct connected
*c
;
8016 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
8017 bgp_connected_delete(bgp
, c
);
8021 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
8023 struct vrf
*vrf
= NULL
;
8024 struct listnode
*next
;
8027 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
8028 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
8030 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8031 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
8034 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
8038 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
8040 struct listnode
*next
, *next2
;
8041 struct bgp
*bgp
, *bgp2
;
8044 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8046 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
8047 if (bgp2
->as
== bgp
->as
)
8055 snprintf(buf
, sizeof(buf
), "%u", bgp
->as
);
8056 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
8060 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
8061 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
8062 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
8063 {.completions
= NULL
},
8066 struct frr_pthread
*bgp_pth_io
;
8067 struct frr_pthread
*bgp_pth_ka
;
8069 static void bgp_pthreads_init(void)
8071 assert(!bgp_pth_io
);
8072 assert(!bgp_pth_ka
);
8074 struct frr_pthread_attr io
= {
8075 .start
= frr_pthread_attr_default
.start
,
8076 .stop
= frr_pthread_attr_default
.stop
,
8078 struct frr_pthread_attr ka
= {
8079 .start
= bgp_keepalives_start
,
8080 .stop
= bgp_keepalives_stop
,
8082 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
8083 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
8086 void bgp_pthreads_run(void)
8088 frr_pthread_run(bgp_pth_io
, NULL
);
8089 frr_pthread_run(bgp_pth_ka
, NULL
);
8091 /* Wait until threads are ready. */
8092 frr_pthread_wait_running(bgp_pth_io
);
8093 frr_pthread_wait_running(bgp_pth_ka
);
8096 void bgp_pthreads_finish(void)
8098 frr_pthread_stop_all();
8101 static int peer_unshut_after_cfg(struct bgp
*bgp
)
8103 struct listnode
*node
;
8106 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
8107 if (!peer
->shut_during_cfg
)
8110 if (bgp_debug_neighbor_events(peer
))
8111 zlog_debug("%s: released from config-pending hold",
8114 peer
->shut_during_cfg
= false;
8115 if (peer_active(peer
) && peer
->status
!= Established
) {
8116 if (peer
->status
!= Idle
)
8117 BGP_EVENT_ADD(peer
, BGP_Stop
);
8118 BGP_EVENT_ADD(peer
, BGP_Start
);
8125 void bgp_init(unsigned short instance
)
8127 hook_register(bgp_config_end
, peer_unshut_after_cfg
);
8129 /* allocates some vital data structures used by peer commands in
8132 /* pre-init pthreads */
8133 bgp_pthreads_init();
8136 bgp_zebra_init(bm
->master
, instance
);
8138 #ifdef ENABLE_BGP_VNC
8139 vnc_zebra_init(bm
->master
);
8142 /* BGP VTY commands installation. */
8148 bgp_community_alias_init();
8151 bgp_route_map_init();
8152 bgp_scan_vty_init();
8154 #ifdef ENABLE_BGP_VNC
8157 bgp_ethernetvpn_init();
8158 bgp_flowspec_vty_init();
8160 /* Access list initialize. */
8162 access_list_add_hook(peer_distribute_update
);
8163 access_list_delete_hook(peer_distribute_update
);
8165 /* Filter list initialize. */
8167 as_list_add_hook(peer_aslist_add
);
8168 as_list_delete_hook(peer_aslist_del
);
8170 /* Prefix list initialize.*/
8172 prefix_list_add_hook(peer_prefix_list_update
);
8173 prefix_list_delete_hook(peer_prefix_list_update
);
8175 /* Community list initialize. */
8176 bgp_clist
= community_list_init();
8179 bgp_bfd_init(bm
->master
);
8183 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
8186 void bgp_terminate(void)
8190 struct listnode
*node
, *nnode
;
8191 struct listnode
*mnode
, *mnnode
;
8195 /* Close the listener sockets first as this prevents peers from
8197 * to reconnect on receiving the peer unconfig message. In the presence
8198 * of a large number of peers this will ensure that no peer is left with
8199 * a dangling connection
8203 /* reverse bgp_master_init */
8204 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
8205 bgp_close_vrf_socket(bgp
);
8206 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8207 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
8208 if (bgp_debug_neighbor_events(peer
))
8210 "%pBP configured Graceful-Restart, skipping unconfig notification",
8214 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
8215 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
8216 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
8220 if (bm
->listen_sockets
)
8221 list_delete(&bm
->listen_sockets
);
8223 THREAD_OFF(bm
->t_rmap_update
);
8228 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
8229 const char *ip_str
, bool use_json
)
8235 /* Get peer sockunion. */
8236 ret
= str2sockunion(ip_str
, &su
);
8238 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
8240 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
8244 json_object
*json_no
= NULL
;
8245 json_no
= json_object_new_object();
8246 json_object_string_add(
8248 "malformedAddressOrName",
8250 vty_json(vty
, json_no
);
8253 "%% Malformed address or name: %s\n",
8261 /* Peer structure lookup. */
8262 peer
= peer_lookup(bgp
, &su
);
8265 json_object
*json_no
= NULL
;
8266 json_no
= json_object_new_object();
8267 json_object_string_add(json_no
, "warning",
8268 "No such neighbor in this view/vrf");
8269 vty_json(vty
, json_no
);
8271 vty_out(vty
, "No such neighbor in this view/vrf\n");
8278 void bgp_gr_apply_running_config(void)
8280 struct peer
*peer
= NULL
;
8281 struct bgp
*bgp
= NULL
;
8282 struct listnode
*node
, *nnode
;
8283 bool gr_router_detected
= false;
8285 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
8286 zlog_debug("[BGP_GR] %s called !", __func__
);
8288 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
8289 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8290 bgp_peer_gr_flags_update(peer
);
8291 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
8292 gr_router_detected
= true;
8295 if (gr_router_detected
8296 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
8297 bgp_zebra_send_capabilities(bgp
, true);
8298 } else if (!gr_router_detected
8299 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
8300 bgp_zebra_send_capabilities(bgp
, false);
8303 gr_router_detected
= false;
8307 printfrr_ext_autoreg_p("BP", printfrr_bp
);
8308 static ssize_t
printfrr_bp(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
8311 const struct peer
*peer
= ptr
;
8314 return bputs(buf
, "(null)");
8316 return bprintfrr(buf
, "%s(%s)", peer
->host
,
8317 peer
->hostname
? peer
->hostname
: "Unknown");