1 /* BGP-4, BGP-4+ daemon program
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "sockunion.h"
38 #include "workqueue.h"
46 #include "lib/sockopt.h"
47 #include "frr_pthread.h"
50 #include "bgpd/bgpd.h"
51 #include "bgpd/bgp_table.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_route.h"
54 #include "bgpd/bgp_dump.h"
55 #include "bgpd/bgp_debug.h"
56 #include "bgpd/bgp_errors.h"
57 #include "bgpd/bgp_community.h"
58 #include "bgpd/bgp_community_alias.h"
59 #include "bgpd/bgp_conditional_adv.h"
60 #include "bgpd/bgp_attr.h"
61 #include "bgpd/bgp_regex.h"
62 #include "bgpd/bgp_clist.h"
63 #include "bgpd/bgp_fsm.h"
64 #include "bgpd/bgp_packet.h"
65 #include "bgpd/bgp_zebra.h"
66 #include "bgpd/bgp_open.h"
67 #include "bgpd/bgp_filter.h"
68 #include "bgpd/bgp_nexthop.h"
69 #include "bgpd/bgp_damp.h"
70 #include "bgpd/bgp_mplsvpn.h"
72 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
73 #include "bgpd/rfapi/rfapi_backend.h"
75 #include "bgpd/bgp_evpn.h"
76 #include "bgpd/bgp_advertise.h"
77 #include "bgpd/bgp_network.h"
78 #include "bgpd/bgp_vty.h"
79 #include "bgpd/bgp_mpath.h"
80 #include "bgpd/bgp_nht.h"
81 #include "bgpd/bgp_updgrp.h"
82 #include "bgpd/bgp_bfd.h"
83 #include "bgpd/bgp_memory.h"
84 #include "bgpd/bgp_evpn_vty.h"
85 #include "bgpd/bgp_keepalives.h"
86 #include "bgpd/bgp_io.h"
87 #include "bgpd/bgp_ecommunity.h"
88 #include "bgpd/bgp_flowspec.h"
89 #include "bgpd/bgp_labelpool.h"
90 #include "bgpd/bgp_pbr.h"
91 #include "bgpd/bgp_addpath.h"
92 #include "bgpd/bgp_evpn_private.h"
93 #include "bgpd/bgp_evpn_mh.h"
94 #include "bgpd/bgp_mac.h"
95 #include "bgpd/bgp_orr.h"
96 #include "bgp_trace.h"
98 DEFINE_MTYPE_STATIC(BGPD
, PEER_TX_SHUTDOWN_MSG
, "Peer shutdown message (TX)");
99 DEFINE_MTYPE_STATIC(BGPD
, BGP_EVPN_INFO
, "BGP EVPN instance information");
100 DEFINE_QOBJ_TYPE(bgp_master
);
101 DEFINE_QOBJ_TYPE(bgp
);
102 DEFINE_QOBJ_TYPE(peer
);
103 DEFINE_HOOK(bgp_inst_delete
, (struct bgp
*bgp
), (bgp
));
105 /* BGP process wide configuration. */
106 static struct bgp_master bgp_master
;
108 /* BGP process wide configuration pointer to export. */
109 struct bgp_master
*bm
;
111 /* BGP community-list. */
112 struct community_list_handler
*bgp_clist
;
114 unsigned int multipath_num
= MULTIPATH_NUM
;
116 /* Number of bgp instances configured for suppress fib config */
117 unsigned int bgp_suppress_fib_count
;
119 static void bgp_if_finish(struct bgp
*bgp
);
120 static void peer_drop_dynamic_neighbor(struct peer
*peer
);
122 extern struct zclient
*zclient
;
124 /* handle main socket creation or deletion */
125 static int bgp_check_main_socket(bool create
, struct bgp
*bgp
)
127 static int bgp_server_main_created
;
128 struct listnode
*node
;
132 if (bgp_server_main_created
)
134 if (list_isempty(bm
->addresses
)) {
135 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
136 return BGP_ERR_INVALID_VALUE
;
138 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
139 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
140 return BGP_ERR_INVALID_VALUE
;
142 bgp_server_main_created
= 1;
145 if (!bgp_server_main_created
)
148 bgp_server_main_created
= 0;
152 void bgp_session_reset(struct peer
*peer
)
154 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
155 && !(CHECK_FLAG(peer
->doppelganger
->flags
, PEER_FLAG_CONFIG_NODE
)))
156 peer_delete(peer
->doppelganger
);
158 BGP_EVENT_ADD(peer
, BGP_Stop
);
162 * During session reset, we may delete the doppelganger peer, which would
163 * be the next node to the current node. If the session reset was invoked
164 * during walk of peer list, we would end up accessing the freed next
165 * node. This function moves the next node along.
167 static void bgp_session_reset_safe(struct peer
*peer
, struct listnode
**nnode
)
172 n
= (nnode
) ? *nnode
: NULL
;
173 npeer
= (n
) ? listgetdata(n
) : NULL
;
175 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
176 && !(CHECK_FLAG(peer
->doppelganger
->flags
,
177 PEER_FLAG_CONFIG_NODE
))) {
178 if (peer
->doppelganger
== npeer
)
179 /* nnode and *nnode are confirmed to be non-NULL here */
180 *nnode
= (*nnode
)->next
;
181 peer_delete(peer
->doppelganger
);
184 BGP_EVENT_ADD(peer
, BGP_Stop
);
187 /* BGP global flag manipulation. */
188 int bgp_option_set(int flag
)
192 case BGP_OPT_NO_LISTEN
:
193 case BGP_OPT_NO_ZEBRA
:
194 SET_FLAG(bm
->options
, flag
);
197 return BGP_ERR_INVALID_FLAG
;
202 int bgp_option_unset(int flag
)
206 case BGP_OPT_NO_ZEBRA
:
208 UNSET_FLAG(bm
->options
, flag
);
211 return BGP_ERR_INVALID_FLAG
;
216 int bgp_option_check(int flag
)
218 return CHECK_FLAG(bm
->options
, flag
);
221 /* set the bgp no-rib option during runtime and remove installed routes */
222 void bgp_option_norib_set_runtime(void)
225 struct listnode
*node
;
229 if (bgp_option_check(BGP_OPT_NO_FIB
))
232 bgp_option_set(BGP_OPT_NO_FIB
);
234 zlog_info("Disabled BGP route installation to RIB (Zebra)");
236 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
237 FOREACH_AFI_SAFI (afi
, safi
) {
239 * Stop a crash, more work is needed
240 * here to properly add/remove these types of
243 if (!bgp_fibupd_safi(safi
))
246 bgp_zebra_withdraw_table_all_subtypes(bgp
, afi
, safi
);
250 zlog_info("All routes have been withdrawn from RIB (Zebra)");
253 /* unset the bgp no-rib option during runtime and announce routes to Zebra */
254 void bgp_option_norib_unset_runtime(void)
257 struct listnode
*node
;
261 if (!bgp_option_check(BGP_OPT_NO_FIB
))
264 bgp_option_unset(BGP_OPT_NO_FIB
);
266 zlog_info("Enabled BGP route installation to RIB (Zebra)");
268 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
269 FOREACH_AFI_SAFI (afi
, safi
) {
271 * Stop a crash, more work is needed
272 * here to properly add/remove these types
273 * of routes from zebra
275 if (!bgp_fibupd_safi(safi
))
278 bgp_zebra_announce_table_all_subtypes(bgp
, afi
, safi
);
282 zlog_info("All routes have been installed in RIB (Zebra)");
285 /* Internal function to set BGP structure configureation flag. */
286 static void bgp_config_set(struct bgp
*bgp
, int config
)
288 SET_FLAG(bgp
->config
, config
);
291 static void bgp_config_unset(struct bgp
*bgp
, int config
)
293 UNSET_FLAG(bgp
->config
, config
);
296 static int bgp_config_check(struct bgp
*bgp
, int config
)
298 return CHECK_FLAG(bgp
->config
, config
);
301 /* Set BGP router identifier; distinguish between explicit config and other
304 static int bgp_router_id_set(struct bgp
*bgp
, const struct in_addr
*id
,
308 struct listnode
*node
, *nnode
;
310 if (IPV4_ADDR_SAME(&bgp
->router_id
, id
))
313 /* EVPN uses router id in RD, withdraw them */
314 if (is_evpn_enabled())
315 bgp_evpn_handle_router_id_update(bgp
, true);
317 vpn_handle_router_id_update(bgp
, true, is_config
);
319 IPV4_ADDR_COPY(&bgp
->router_id
, id
);
321 /* Set all peer's local identifier with this value. */
322 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
323 IPV4_ADDR_COPY(&peer
->local_id
, id
);
325 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
326 peer
->last_reset
= PEER_DOWN_RID_CHANGE
;
327 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
328 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
332 /* EVPN uses router id in RD, update them */
333 if (is_evpn_enabled())
334 bgp_evpn_handle_router_id_update(bgp
, false);
336 vpn_handle_router_id_update(bgp
, false, is_config
);
341 void bgp_router_id_zebra_bump(vrf_id_t vrf_id
, const struct prefix
*router_id
)
343 struct listnode
*node
, *nnode
;
345 struct in_addr
*addr
= NULL
;
347 if (router_id
!= NULL
)
348 addr
= (struct in_addr
*)&(router_id
->u
.prefix4
);
350 if (vrf_id
== VRF_DEFAULT
) {
351 /* Router-id change for default VRF has to also update all
353 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
354 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
358 bgp
->router_id_zebra
= *addr
;
360 addr
= &bgp
->router_id_zebra
;
362 if (!bgp
->router_id_static
.s_addr
) {
363 /* Router ID is updated if there are no active
366 if (bgp
->established_peers
== 0) {
367 if (BGP_DEBUG(zebra
, ZEBRA
))
369 "RID change : vrf %s(%u), RTR ID %pI4",
373 * if old router-id was 0x0, set flag
374 * to use this new value
376 bgp_router_id_set(bgp
, addr
,
377 (bgp
->router_id
.s_addr
385 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
388 bgp
->router_id_zebra
= *addr
;
390 addr
= &bgp
->router_id_zebra
;
392 if (!bgp
->router_id_static
.s_addr
) {
393 /* Router ID is updated if there are no active
396 if (bgp
->established_peers
== 0) {
397 if (BGP_DEBUG(zebra
, ZEBRA
))
399 "RID change : vrf %s(%u), RTR ID %pI4",
403 * if old router-id was 0x0, set flag
404 * to use this new value
406 bgp_router_id_set(bgp
, addr
,
407 (bgp
->router_id
.s_addr
418 void bgp_router_id_static_set(struct bgp
*bgp
, struct in_addr id
)
420 bgp
->router_id_static
= id
;
421 bgp_router_id_set(bgp
,
422 id
.s_addr
!= INADDR_ANY
? &id
: &bgp
->router_id_zebra
,
423 true /* is config */);
426 void bm_wait_for_fib_set(bool set
)
428 bool send_msg
= false;
430 if (bm
->wait_for_fib
== set
)
433 bm
->wait_for_fib
= set
;
435 if (bgp_suppress_fib_count
== 0)
437 bgp_suppress_fib_count
++;
439 bgp_suppress_fib_count
--;
440 if (bgp_suppress_fib_count
== 0)
444 if (send_msg
&& zclient
)
445 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
449 /* Set the suppress fib pending for the bgp configuration */
450 void bgp_suppress_fib_pending_set(struct bgp
*bgp
, bool set
)
452 bool send_msg
= false;
454 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
458 SET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
459 /* Send msg to zebra for the first instance of bgp enabled
462 if (bgp_suppress_fib_count
== 0)
464 bgp_suppress_fib_count
++;
466 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
467 bgp_suppress_fib_count
--;
469 /* Send msg to zebra if there are no instances enabled
472 if (bgp_suppress_fib_count
== 0)
475 /* Send route notify request to RIB */
477 if (BGP_DEBUG(zebra
, ZEBRA
))
478 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
481 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
486 /* BGP's cluster-id control. */
487 void bgp_cluster_id_set(struct bgp
*bgp
, struct in_addr
*cluster_id
)
490 struct listnode
*node
, *nnode
;
492 if (bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
)
493 && IPV4_ADDR_SAME(&bgp
->cluster_id
, cluster_id
))
496 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
497 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
499 /* Clear all IBGP peer. */
500 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
501 if (peer
->sort
!= BGP_PEER_IBGP
)
504 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
505 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
506 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
507 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
512 void bgp_cluster_id_unset(struct bgp
*bgp
)
515 struct listnode
*node
, *nnode
;
517 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
520 bgp
->cluster_id
.s_addr
= 0;
521 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
523 /* Clear all IBGP peer. */
524 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
525 if (peer
->sort
!= BGP_PEER_IBGP
)
528 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
529 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
530 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
531 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
536 /* BGP timer configuration. */
537 void bgp_timers_set(struct bgp
*bgp
, uint32_t keepalive
, uint32_t holdtime
,
538 uint32_t connect_retry
, uint32_t delayopen
)
540 bgp
->default_keepalive
=
541 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
542 bgp
->default_holdtime
= holdtime
;
543 bgp
->default_connect_retry
= connect_retry
;
544 bgp
->default_delayopen
= delayopen
;
547 /* mostly for completeness - CLI uses its own defaults */
548 void bgp_timers_unset(struct bgp
*bgp
)
550 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
551 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
552 bgp
->default_connect_retry
= BGP_DEFAULT_CONNECT_RETRY
;
553 bgp
->default_delayopen
= BGP_DEFAULT_DELAYOPEN
;
556 void bgp_tcp_keepalive_set(struct bgp
*bgp
, uint16_t keepalive_idle
,
557 uint16_t keepalive_intvl
, uint16_t keepalive_probes
)
559 bgp
->tcp_keepalive_idle
= keepalive_idle
;
560 bgp
->tcp_keepalive_intvl
= keepalive_intvl
;
561 bgp
->tcp_keepalive_probes
= keepalive_probes
;
564 void bgp_tcp_keepalive_unset(struct bgp
*bgp
)
566 bgp
->tcp_keepalive_idle
= 0;
567 bgp
->tcp_keepalive_intvl
= 0;
568 bgp
->tcp_keepalive_probes
= 0;
571 /* BGP confederation configuration. */
572 void bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
575 struct listnode
*node
, *nnode
;
581 /* Remember - were we doing confederation before? */
582 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
584 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
586 /* If we were doing confederation already, this is just an external
587 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
588 were not doing confederation before, reset all EBGP sessions. */
589 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
590 enum bgp_peer_sort ptype
= peer_sort(peer
);
592 /* We're looking for peers who's AS is not local or part of our
594 if (already_confed
) {
595 if (ptype
== BGP_PEER_EBGP
) {
597 if (BGP_IS_VALID_STATE_FOR_NOTIF(
600 PEER_DOWN_CONFED_ID_CHANGE
;
602 peer
, BGP_NOTIFY_CEASE
,
603 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
605 bgp_session_reset_safe(peer
, &nnode
);
608 /* Not doign confederation before, so reset every
611 if (ptype
!= BGP_PEER_IBGP
) {
612 /* Reset the local_as to be our EBGP one */
613 if (ptype
== BGP_PEER_EBGP
)
615 if (BGP_IS_VALID_STATE_FOR_NOTIF(
618 PEER_DOWN_CONFED_ID_CHANGE
;
620 peer
, BGP_NOTIFY_CEASE
,
621 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
623 bgp_session_reset_safe(peer
, &nnode
);
630 void bgp_confederation_id_unset(struct bgp
*bgp
)
633 struct listnode
*node
, *nnode
;
636 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
638 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
639 /* We're looking for peers who's AS is not local */
640 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
641 peer
->local_as
= bgp
->as
;
642 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
643 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
644 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
645 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
649 bgp_session_reset_safe(peer
, &nnode
);
654 /* Is an AS part of the confed or not? */
655 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
662 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
663 if (bgp
->confed_peers
[i
] == as
)
669 /* Add an AS to the confederation set. */
670 void bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
673 struct listnode
*node
, *nnode
;
678 if (bgp_confederation_peers_check(bgp
, as
))
682 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
683 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
685 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
686 bgp
->confed_peers_cnt
++;
688 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
689 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
690 if (peer
->as
== as
) {
691 peer
->local_as
= bgp
->as
;
692 (void)peer_sort(peer
);
693 if (BGP_IS_VALID_STATE_FOR_NOTIF(
696 PEER_DOWN_CONFED_PEER_CHANGE
;
698 peer
, BGP_NOTIFY_CEASE
,
699 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
701 bgp_session_reset_safe(peer
, &nnode
);
707 /* Delete an AS from the confederation set. */
708 void bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
713 struct listnode
*node
, *nnode
;
718 if (!bgp_confederation_peers_check(bgp
, as
))
721 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
722 if (bgp
->confed_peers
[i
] == as
)
723 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
724 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
726 bgp
->confed_peers_cnt
--;
728 if (bgp
->confed_peers_cnt
== 0) {
729 if (bgp
->confed_peers
)
730 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
731 bgp
->confed_peers
= NULL
;
734 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
735 bgp
->confed_peers_cnt
* sizeof(as_t
));
737 /* Now reset any peer who's remote AS has just been removed from the
739 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
740 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
741 if (peer
->as
== as
) {
742 peer
->local_as
= bgp
->confed_id
;
743 (void)peer_sort(peer
);
744 if (BGP_IS_VALID_STATE_FOR_NOTIF(
747 PEER_DOWN_CONFED_PEER_CHANGE
;
749 peer
, BGP_NOTIFY_CEASE
,
750 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
752 bgp_session_reset_safe(peer
, &nnode
);
758 /* Local preference configuration. */
759 void bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
764 bgp
->default_local_pref
= local_pref
;
767 void bgp_default_local_preference_unset(struct bgp
*bgp
)
772 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
775 /* Local preference configuration. */
776 void bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
,
782 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
785 void bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
789 bgp
->default_subgroup_pkt_queue_max
=
790 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
793 /* Listen limit configuration. */
794 void bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
799 bgp
->dynamic_neighbors_limit
= listen_limit
;
802 void bgp_listen_limit_unset(struct bgp
*bgp
)
807 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
810 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
811 afi_t
*afi
, safi_t
*safi
)
813 /* Map from IANA values to internal values, return error if
814 * values are unrecognized.
816 *afi
= afi_iana2int(pkt_afi
);
817 *safi
= safi_iana2int(pkt_safi
);
818 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
824 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
825 iana_safi_t
*pkt_safi
)
827 /* Map from internal values to IANA values, return error if
828 * internal values are bad (unexpected).
830 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
832 *pkt_afi
= afi_int2iana(afi
);
833 *pkt_safi
= safi_int2iana(safi
);
837 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
846 afid
= afindex(afi
, safi
);
847 if (afid
>= BGP_AF_MAX
)
851 assert(peer
->peer_af_array
[afid
] == NULL
);
853 /* Allocate new peer af */
854 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
856 peer
->peer_af_array
[afid
] = af
;
861 bgp
->af_peer_count
[afi
][safi
]++;
866 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
873 afid
= afindex(afi
, safi
);
874 if (afid
>= BGP_AF_MAX
)
877 return peer
->peer_af_array
[afid
];
880 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
889 afid
= afindex(afi
, safi
);
890 if (afid
>= BGP_AF_MAX
)
893 af
= peer
->peer_af_array
[afid
];
898 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
900 bgp_stop_announce_route_timer(af
);
902 if (PAF_SUBGRP(af
)) {
903 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
904 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
905 af
->subgroup
->update_group
->id
,
906 af
->subgroup
->id
, peer
->host
);
910 update_subgroup_remove_peer(af
->subgroup
, af
);
912 if (bgp
->af_peer_count
[afi
][safi
])
913 bgp
->af_peer_count
[afi
][safi
]--;
915 peer
->peer_af_array
[afid
] = NULL
;
916 XFREE(MTYPE_BGP_PEER_AF
, af
);
920 /* Peer comparison function for sorting. */
921 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
923 if (p1
->group
&& !p2
->group
)
926 if (!p1
->group
&& p2
->group
)
929 if (p1
->group
== p2
->group
) {
930 if (p1
->conf_if
&& !p2
->conf_if
)
933 if (!p1
->conf_if
&& p2
->conf_if
)
936 if (p1
->conf_if
&& p2
->conf_if
)
937 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
939 return strcmp(p1
->group
->name
, p2
->group
->name
);
941 return sockunion_cmp(&p1
->su
, &p2
->su
);
944 static unsigned int peer_hash_key_make(const void *p
)
946 const struct peer
*peer
= p
;
947 return sockunion_hash(&peer
->su
);
950 static bool peer_hash_same(const void *p1
, const void *p2
)
952 const struct peer
*peer1
= p1
;
953 const struct peer
*peer2
= p2
;
955 return (sockunion_same(&peer1
->su
, &peer2
->su
)
956 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
957 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
960 void peer_flag_inherit(struct peer
*peer
, uint64_t flag
)
964 /* Skip if peer is not a peer-group member. */
965 if (!peer_group_active(peer
))
968 /* Unset override flag to signal inheritance from peer-group. */
969 UNSET_FLAG(peer
->flags_override
, flag
);
972 * Inherit flag state from peer-group. If the flag of the peer-group is
973 * not being inverted, the peer must inherit the inverse of the current
974 * peer-group flag state.
976 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
977 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
978 && CHECK_FLAG(peer
->flags_invert
, flag
))
979 COND_FLAG(peer
->flags
, flag
, !group_val
);
981 COND_FLAG(peer
->flags
, flag
, group_val
);
984 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
986 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
989 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
994 /* Skip if peer is not a peer-group member. */
995 if (!peer_group_active(peer
))
998 /* Unset override flag to signal inheritance from peer-group. */
999 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
1002 * Inherit flag state from peer-group. If the flag of the peer-group is
1003 * not being inverted, the peer must inherit the inverse of the current
1004 * peer-group flag state.
1006 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
1007 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
1008 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
1009 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
1011 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
1014 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
1015 static inline enum bgp_peer_sort
peer_calc_sort(struct peer
*peer
)
1022 if (peer
->change_local_as
)
1023 local_as
= peer
->change_local_as
;
1025 local_as
= peer
->local_as
;
1028 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1029 if (peer
->as_type
== AS_INTERNAL
)
1030 return BGP_PEER_IBGP
;
1032 else if (peer
->as_type
== AS_EXTERNAL
)
1033 return BGP_PEER_EBGP
;
1035 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1037 return (local_as
== peer
->as
? BGP_PEER_IBGP
1044 assert(peer
->group
);
1045 peer1
= listnode_head(peer
->group
->peer
);
1050 return BGP_PEER_INTERNAL
;
1054 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1056 return BGP_PEER_INTERNAL
;
1058 if (local_as
== peer
->as
) {
1059 if (bgp
->as
== bgp
->confed_id
) {
1060 if (local_as
== bgp
->as
)
1061 return BGP_PEER_IBGP
;
1063 return BGP_PEER_EBGP
;
1065 if (local_as
== bgp
->confed_id
)
1066 return BGP_PEER_EBGP
;
1068 return BGP_PEER_IBGP
;
1072 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1073 return BGP_PEER_CONFED
;
1075 return BGP_PEER_EBGP
;
1077 if (peer
->as_type
== AS_UNSPECIFIED
) {
1078 /* check if in peer-group with AS information */
1080 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1081 if (peer
->group
->conf
->as_type
1083 if (local_as
== peer
->group
->conf
->as
)
1084 return BGP_PEER_IBGP
;
1086 return BGP_PEER_EBGP
;
1087 } else if (peer
->group
->conf
->as_type
1089 return BGP_PEER_IBGP
;
1091 return BGP_PEER_EBGP
;
1093 /* no AS information anywhere, let caller know */
1094 return BGP_PEER_UNSPECIFIED
;
1095 } else if (peer
->as_type
!= AS_SPECIFIED
)
1096 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1099 return (local_as
== 0 ? BGP_PEER_INTERNAL
1100 : local_as
== peer
->as
? BGP_PEER_IBGP
1105 /* Calculate and cache the peer "sort" */
1106 enum bgp_peer_sort
peer_sort(struct peer
*peer
)
1108 peer
->sort
= peer_calc_sort(peer
);
1112 enum bgp_peer_sort
peer_sort_lookup(struct peer
*peer
)
1117 static void peer_free(struct peer
*peer
)
1122 assert(peer
->status
== Deleted
);
1126 /* this /ought/ to have been done already through bgp_stop earlier,
1127 * but just to be sure..
1129 bgp_timer_set(peer
);
1130 bgp_reads_off(peer
);
1131 bgp_writes_off(peer
);
1132 thread_cancel_event_ready(bm
->master
, peer
);
1133 FOREACH_AFI_SAFI (afi
, safi
)
1134 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
1135 assert(!peer
->t_write
);
1136 assert(!peer
->t_read
);
1137 BGP_EVENT_FLUSH(peer
);
1139 pthread_mutex_destroy(&peer
->io_mtx
);
1141 /* Free connected nexthop, if present */
1142 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1143 && !peer_dynamic_neighbor(peer
))
1144 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1147 FOREACH_AFI_SAFI (afi
, safi
) {
1148 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1149 XFREE(MTYPE_BGP_FILTER_NAME
,
1150 peer
->filter
[afi
][safi
].advmap
.aname
);
1151 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1152 XFREE(MTYPE_BGP_FILTER_NAME
,
1153 peer
->filter
[afi
][safi
].advmap
.cname
);
1156 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1158 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1159 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1160 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1161 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1163 /* Update source configuration. */
1164 if (peer
->update_source
) {
1165 sockunion_free(peer
->update_source
);
1166 peer
->update_source
= NULL
;
1169 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1171 XFREE(MTYPE_BGP_NOTIFICATION
, peer
->notify
.data
);
1172 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1174 if (peer
->clear_node_queue
)
1175 work_queue_free_and_null(&peer
->clear_node_queue
);
1177 bgp_sync_delete(peer
);
1179 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1181 /* Remove BFD configuration. */
1182 if (peer
->bfd_config
)
1183 bgp_peer_remove_bfd_config(peer
);
1185 FOREACH_AFI_SAFI (afi
, safi
)
1186 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1188 bgp_unlock(peer
->bgp
);
1190 memset(peer
, 0, sizeof(struct peer
));
1192 XFREE(MTYPE_BGP_PEER
, peer
);
1195 /* increase reference count on a struct peer */
1196 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1198 frrtrace(2, frr_bgp
, bgp_peer_lock
, peer
, name
);
1199 assert(peer
&& (peer
->lock
>= 0));
1206 /* decrease reference count on a struct peer
1207 * struct peer is freed and NULL returned if last reference
1209 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1211 frrtrace(2, frr_bgp
, bgp_peer_unlock
, peer
, name
);
1212 assert(peer
&& (peer
->lock
> 0));
1216 if (peer
->lock
== 0) {
1223 /* BGP GR changes */
1225 int bgp_global_gr_init(struct bgp
*bgp
)
1227 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1228 zlog_debug("%s called ..", __func__
);
1230 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1231 /* GLOBAL_HELPER Mode */
1234 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1235 GLOBAL_GR
, GLOBAL_INVALID
,
1236 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1237 GLOBAL_DISABLE
, GLOBAL_INVALID
1239 /* GLOBAL_GR Mode */
1242 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1243 GLOBAL_GR
, GLOBAL_HELPER
,
1244 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1245 GLOBAL_DISABLE
, GLOBAL_INVALID
1247 /* GLOBAL_DISABLE Mode */
1250 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1251 GLOBAL_GR
, GLOBAL_INVALID
,
1252 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1253 GLOBAL_INVALID
, GLOBAL_HELPER
1255 /* GLOBAL_INVALID Mode */
1258 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1259 GLOBAL_INVALID
, GLOBAL_INVALID
,
1260 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1261 GLOBAL_INVALID
, GLOBAL_INVALID
1264 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1265 sizeof(local_GLOBAL_GR_FSM
));
1267 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1268 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1270 return BGP_GR_SUCCESS
;
1273 int bgp_peer_gr_init(struct peer
*peer
)
1275 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1276 zlog_debug("%s called ..", __func__
);
1278 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1279 [BGP_PEER_GR_EVENT_CMD
] = {
1281 /* PEER_HELPER Mode */
1282 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1283 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1284 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1285 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1286 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1287 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1288 bgp_peer_gr_action
}
1292 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1293 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1294 bgp_peer_gr_action
},
1295 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1296 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1297 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1298 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1301 /* PEER_DISABLE Mode */
1302 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1303 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1304 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1305 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1306 bgp_peer_gr_action
},
1307 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1308 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1311 /* PEER_INVALID Mode */
1312 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1313 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1314 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1315 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1316 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1317 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1320 /* PEER_GLOBAL_INHERIT Mode */
1321 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1322 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1323 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1324 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1325 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1326 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1329 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1330 sizeof(local_Peer_GR_FSM
));
1331 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1332 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1334 return BGP_GR_SUCCESS
;
1337 static void bgp_srv6_init(struct bgp
*bgp
)
1339 bgp
->srv6_enabled
= false;
1340 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1341 bgp
->srv6_locator_chunks
= list_new();
1342 bgp
->srv6_functions
= list_new();
1345 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1347 if (bgp
->srv6_locator_chunks
)
1348 list_delete(&bgp
->srv6_locator_chunks
);
1349 if (bgp
->srv6_functions
)
1350 list_delete(&bgp
->srv6_functions
);
1353 /* Allocate new peer object, implicitely locked. */
1354 struct peer
*peer_new(struct bgp
*bgp
)
1361 /* bgp argument is absolutely required */
1364 /* Allocate new peer. */
1365 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1367 /* Set default value. */
1369 peer
->v_start
= BGP_INIT_START_TIMER
;
1370 peer
->v_connect
= bgp
->default_connect_retry
;
1371 peer
->status
= Idle
;
1372 peer
->ostatus
= Idle
;
1373 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1374 peer
->bgp
= bgp_lock(bgp
);
1375 peer
= peer_lock(peer
); /* initial reference */
1376 peer
->local_role
= ROLE_UNDEFINED
;
1377 peer
->remote_role
= ROLE_UNDEFINED
;
1378 peer
->password
= NULL
;
1379 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1381 /* Set default flags. */
1382 FOREACH_AFI_SAFI (afi
, safi
) {
1383 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1384 SET_FLAG(peer
->af_flags
[afi
][safi
],
1385 PEER_FLAG_SEND_EXT_COMMUNITY
);
1386 SET_FLAG(peer
->af_flags
[afi
][safi
],
1387 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1389 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1390 PEER_FLAG_SEND_COMMUNITY
);
1391 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1392 PEER_FLAG_SEND_EXT_COMMUNITY
);
1393 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1394 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1395 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1396 peer
->soo
[afi
][safi
] = NULL
;
1399 /* set nexthop-unchanged for l2vpn evpn by default */
1400 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1401 PEER_FLAG_NEXTHOP_UNCHANGED
);
1403 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1405 /* Initialize per peer bgp GR FSM */
1406 bgp_peer_gr_init(peer
);
1408 /* Create buffers. */
1409 peer
->ibuf
= stream_fifo_new();
1410 peer
->obuf
= stream_fifo_new();
1411 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1413 /* We use a larger buffer for peer->obuf_work in the event that:
1414 * - We RX a BGP_UPDATE where the attributes alone are just
1415 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1416 * - The user configures an outbound route-map that does many as-path
1417 * prepends or adds many communities. At most they can have
1418 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1419 * large they can make the attributes.
1421 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1422 * bounds checking for every single attribute as we construct an
1426 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1428 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1430 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1432 bgp_sync_init(peer
);
1434 /* Get service port number. */
1435 sp
= getservbyname("bgp", "tcp");
1436 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1438 QOBJ_REG(peer
, peer
);
1443 * This function is invoked when a duplicate peer structure associated with
1444 * a neighbor is being deleted. If this about-to-be-deleted structure is
1445 * the one with all the config, then we have to copy over the info.
1447 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1449 struct peer_af
*paf
;
1457 /* The following function is used by both peer group config copy to
1458 * individual peer and when we transfer config
1460 if (peer_src
->change_local_as
)
1461 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1463 /* peer flags apply */
1464 peer_dst
->flags
= peer_src
->flags
;
1466 * The doppelganger *must* not have a config node stored
1468 UNSET_FLAG(peer_dst
->flags
, PEER_FLAG_CONFIG_NODE
);
1469 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1470 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1472 peer_dst
->local_as
= peer_src
->local_as
;
1473 peer_dst
->port
= peer_src
->port
;
1474 /* copy tcp_mss value */
1475 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1476 (void)peer_sort(peer_dst
);
1477 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1478 peer_dst
->local_role
= peer_src
->local_role
;
1480 peer_dst
->max_packet_size
= peer_src
->max_packet_size
;
1483 peer_dst
->holdtime
= peer_src
->holdtime
;
1484 peer_dst
->keepalive
= peer_src
->keepalive
;
1485 peer_dst
->connect
= peer_src
->connect
;
1486 peer_dst
->delayopen
= peer_src
->delayopen
;
1487 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1488 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1489 peer_dst
->routeadv
= peer_src
->routeadv
;
1490 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1491 peer_dst
->v_delayopen
= peer_src
->v_delayopen
;
1493 /* password apply */
1494 if (peer_src
->password
&& !peer_dst
->password
)
1495 peer_dst
->password
=
1496 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1498 FOREACH_AFI_SAFI (afi
, safi
) {
1499 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1500 peer_dst
->af_flags
[afi
][safi
] = peer_src
->af_flags
[afi
][safi
];
1501 peer_dst
->allowas_in
[afi
][safi
] =
1502 peer_src
->allowas_in
[afi
][safi
];
1503 peer_dst
->weight
[afi
][safi
] = peer_src
->weight
[afi
][safi
];
1504 peer_dst
->addpath_type
[afi
][safi
] =
1505 peer_src
->addpath_type
[afi
][safi
];
1508 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1509 paf
= peer_src
->peer_af_array
[afidx
];
1511 if (!peer_af_find(peer_dst
, paf
->afi
, paf
->safi
))
1512 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1516 /* update-source apply */
1517 if (peer_src
->update_source
) {
1518 if (peer_dst
->update_source
)
1519 sockunion_free(peer_dst
->update_source
);
1520 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1521 peer_dst
->update_source
=
1522 sockunion_dup(peer_src
->update_source
);
1523 } else if (peer_src
->update_if
) {
1524 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1525 if (peer_dst
->update_source
) {
1526 sockunion_free(peer_dst
->update_source
);
1527 peer_dst
->update_source
= NULL
;
1529 peer_dst
->update_if
=
1530 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1533 if (peer_src
->ifname
) {
1534 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1537 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1541 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1542 struct interface
*ifp
)
1544 struct connected
*ifc
;
1547 struct listnode
*node
;
1549 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1550 * IPv4 address of the other end.
1552 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1553 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1554 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
1555 if (p
.prefixlen
== 30) {
1556 peer
->su
.sa
.sa_family
= AF_INET
;
1557 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1559 peer
->su
.sin
.sin_addr
.s_addr
=
1561 else if (addr
% 4 == 2)
1562 peer
->su
.sin
.sin_addr
.s_addr
=
1564 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1565 peer
->su
.sin
.sin_len
=
1566 sizeof(struct sockaddr_in
);
1567 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1569 } else if (p
.prefixlen
== 31) {
1570 peer
->su
.sa
.sa_family
= AF_INET
;
1571 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1573 peer
->su
.sin
.sin_addr
.s_addr
=
1576 peer
->su
.sin
.sin_addr
.s_addr
=
1578 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1579 peer
->su
.sin
.sin_len
=
1580 sizeof(struct sockaddr_in
);
1581 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1583 } else if (bgp_debug_neighbor_events(peer
))
1585 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1593 static bool bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1594 struct interface
*ifp
)
1596 struct nbr_connected
*ifc_nbr
;
1598 /* Have we learnt the peer's IPv6 link-local address? */
1599 if (ifp
->nbr_connected
1600 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1601 peer
->su
.sa
.sa_family
= AF_INET6
;
1602 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1603 sizeof(struct in6_addr
));
1605 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1607 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1615 * Set or reset the peer address socketunion structure based on the
1616 * learnt/derived peer address. If the address has changed, update the
1617 * password on the listen socket, if needed.
1619 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1621 struct interface
*ifp
;
1623 int peer_addr_updated
= 0;
1624 struct listnode
*node
;
1625 union sockunion old_su
;
1628 * This function is only ever needed when FRR an interface
1629 * based peering, so this simple test will tell us if
1630 * we are in an interface based configuration or not
1637 prev_family
= peer
->su
.sa
.sa_family
;
1638 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1640 /* If BGP unnumbered is not "v6only", we first see if we can
1642 * peer's IPv4 address.
1644 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1646 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1648 /* If "v6only" or we can't derive peer's IPv4 address, see if
1650 * learnt the peer's IPv6 link-local address. This is from the
1652 * IPv6 address in router advertisement.
1654 if (!peer_addr_updated
)
1656 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1658 /* If we could derive the peer address, we may need to install the
1660 * configured for the peer, if any, on the listen socket. Otherwise,
1662 * that peer's address is not available and uninstall the password, if
1665 if (peer_addr_updated
) {
1666 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1667 && prev_family
== AF_UNSPEC
)
1670 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1671 && prev_family
!= AF_UNSPEC
)
1672 bgp_md5_unset(peer
);
1673 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1674 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1678 * If they are the same, nothing to do here, move along
1680 if (!sockunion_same(&old_su
, &peer
->su
)) {
1681 union sockunion new_su
= peer
->su
;
1682 struct bgp
*bgp
= peer
->bgp
;
1685 * Our peer structure is stored in the bgp->peerhash
1686 * release it before we modify anything in both the
1687 * hash and the list. But *only* if the peer
1688 * is in the bgp->peerhash as that on deletion
1689 * we call bgp_stop which calls this function :(
1690 * so on deletion let's remove from the list first
1691 * and then do the deletion preventing this from
1692 * being added back on the list below when we
1693 * fail to remove it up here.
1697 * listnode_lookup just scans the list
1698 * for the peer structure so it's safe
1699 * to use without modifying the su
1701 node
= listnode_lookup(bgp
->peer
, peer
);
1704 * Let's reset the peer->su release and
1705 * reset it and put it back. We have to
1706 * do this because hash_release will
1707 * scan through looking for a matching
1711 hash_release(peer
->bgp
->peerhash
, peer
);
1712 listnode_delete(peer
->bgp
->peer
, peer
);
1715 (void)hash_get(peer
->bgp
->peerhash
, peer
,
1717 listnode_add_sort(peer
->bgp
->peer
, peer
);
1722 void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1724 struct bgp_dest
*dest
, *ndest
;
1725 struct bgp_table
*table
;
1727 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1728 dest
= bgp_route_next(dest
)) {
1729 table
= bgp_dest_get_bgp_table_info(dest
);
1730 if (table
!= NULL
) {
1731 /* Special handling for 2-level routing
1733 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1734 || safi
== SAFI_EVPN
) {
1735 for (ndest
= bgp_table_top(table
); ndest
;
1736 ndest
= bgp_route_next(ndest
))
1737 bgp_process(bgp
, ndest
, afi
, safi
);
1739 bgp_process(bgp
, dest
, afi
, safi
);
1744 /* Force a bestpath recalculation for all prefixes. This is used
1745 * when 'bgp bestpath' commands are entered.
1747 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1752 FOREACH_AFI_SAFI (afi
, safi
) {
1753 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1758 * Create new BGP peer.
1760 * conf_if and su are mutually exclusive if configuring from the cli.
1761 * If we are handing a doppelganger, then we *must* pass in both
1762 * the original peer's su and conf_if, so that we can appropriately
1763 * track the bgp->peerhash( ie we don't want to remove the current
1764 * one from the config ).
1766 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1767 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1768 int as_type
, struct peer_group
*group
,
1773 char buf
[SU_ADDRSTRLEN
];
1777 peer
= peer_new(bgp
);
1779 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1783 bgp_peer_conf_if_to_su_update(peer
);
1784 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1785 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1788 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1789 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1790 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1792 peer
->local_as
= local_as
;
1793 peer
->as
= remote_as
;
1794 peer
->as_type
= as_type
;
1795 peer
->local_id
= bgp
->router_id
;
1796 peer
->v_holdtime
= bgp
->default_holdtime
;
1797 peer
->v_keepalive
= bgp
->default_keepalive
;
1798 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1799 ? BGP_DEFAULT_IBGP_ROUTEADV
1800 : BGP_DEFAULT_EBGP_ROUTEADV
;
1801 if (bgp_config_inprocess())
1802 peer
->shut_during_cfg
= true;
1804 peer
= peer_lock(peer
); /* bgp peer list reference */
1805 peer
->group
= group
;
1806 listnode_add_sort(bgp
->peer
, peer
);
1809 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1811 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1813 /* Adjust update-group coalesce timer heuristics for # peers. */
1814 if (bgp
->heuristic_coalesce
) {
1815 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1817 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1818 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1821 active
= peer_active(peer
);
1823 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1824 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1826 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1829 /* Last read and reset time set */
1830 peer
->readtime
= peer
->resettime
= monotime(NULL
);
1832 /* Default TTL set. */
1833 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1835 /* Default configured keepalives count for shutdown rtt command */
1836 peer
->rtt_keepalive_conf
= 1;
1838 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1839 * neighbor for the corresponding address family. IPv4 Unicast is
1840 * the only address family enabled by default without expliict
1843 FOREACH_AFI_SAFI (afi
, safi
) {
1844 if (bgp
->default_af
[afi
][safi
]) {
1845 peer
->afc
[afi
][safi
] = 1;
1846 peer_af_create(peer
, afi
, safi
);
1850 /* auto shutdown if configured */
1851 if (bgp
->autoshutdown
)
1852 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1853 /* Set up peer's events and timers. */
1854 else if (!active
&& peer_active(peer
))
1855 bgp_timer_set(peer
);
1857 bgp_peer_gr_flags_update(peer
);
1858 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1863 /* Make accept BGP peer. This function is only called from the test code */
1864 struct peer
*peer_create_accept(struct bgp
*bgp
)
1868 peer
= peer_new(bgp
);
1870 peer
= peer_lock(peer
); /* bgp peer list reference */
1871 listnode_add_sort(bgp
->peer
, peer
);
1872 (void)hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1878 * Return true if we have a peer configured to use this afi/safi
1880 bool bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1882 struct listnode
*node
;
1885 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1886 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1889 if (peer
->afc
[afi
][safi
])
1896 /* Change peer's AS number. */
1897 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
)
1901 enum bgp_peer_sort origtype
, newtype
;
1904 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1905 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1906 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1907 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1908 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1910 bgp_session_reset(peer
);
1912 origtype
= peer_sort_lookup(peer
);
1914 peer
->as_type
= as_specified
;
1916 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1917 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1918 && peer
->bgp
->as
!= as
)
1919 peer
->local_as
= peer
->bgp
->confed_id
;
1921 peer
->local_as
= peer
->bgp
->as
;
1923 newtype
= peer_sort(peer
);
1924 /* Advertisement-interval reset */
1925 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1926 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1927 ? BGP_DEFAULT_IBGP_ROUTEADV
1928 : BGP_DEFAULT_EBGP_ROUTEADV
;
1932 if (newtype
== BGP_PEER_IBGP
)
1934 else if (origtype
== BGP_PEER_IBGP
)
1935 peer
->ttl
= BGP_DEFAULT_TTL
;
1937 /* reflector-client reset */
1938 if (newtype
!= BGP_PEER_IBGP
) {
1940 FOREACH_AFI_SAFI (afi
, safi
)
1941 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
1942 PEER_FLAG_ORR_GROUP
);
1944 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1945 PEER_FLAG_REFLECTOR_CLIENT
);
1946 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1947 PEER_FLAG_REFLECTOR_CLIENT
);
1948 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1949 PEER_FLAG_REFLECTOR_CLIENT
);
1950 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1951 PEER_FLAG_REFLECTOR_CLIENT
);
1952 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1953 PEER_FLAG_REFLECTOR_CLIENT
);
1954 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1955 PEER_FLAG_REFLECTOR_CLIENT
);
1956 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1957 PEER_FLAG_REFLECTOR_CLIENT
);
1958 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1959 PEER_FLAG_REFLECTOR_CLIENT
);
1960 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1961 PEER_FLAG_REFLECTOR_CLIENT
);
1962 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1963 PEER_FLAG_REFLECTOR_CLIENT
);
1964 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1965 PEER_FLAG_REFLECTOR_CLIENT
);
1966 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1967 PEER_FLAG_REFLECTOR_CLIENT
);
1968 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1969 PEER_FLAG_REFLECTOR_CLIENT
);
1973 /* If peer does not exist, create new one. If peer already exists,
1974 set AS number to the peer. */
1975 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1976 as_t
*as
, int as_type
)
1982 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1984 peer
= peer_lookup(bgp
, su
);
1987 /* Not allowed for a dynamic peer. */
1988 if (peer_dynamic_neighbor(peer
)) {
1990 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1993 /* When this peer is a member of peer-group. */
1995 /* peer-group already has AS number/internal/external */
1996 if (peer
->group
->conf
->as
1997 || peer
->group
->conf
->as_type
) {
1998 /* Return peer group's AS number. */
1999 *as
= peer
->group
->conf
->as
;
2000 return BGP_ERR_PEER_GROUP_MEMBER
;
2003 enum bgp_peer_sort peer_sort_type
=
2004 peer_sort(peer
->group
->conf
);
2006 /* Explicit AS numbers used, compare AS numbers */
2007 if (as_type
== AS_SPECIFIED
) {
2008 if (((peer_sort_type
== BGP_PEER_IBGP
)
2009 && (bgp
->as
!= *as
))
2010 || ((peer_sort_type
== BGP_PEER_EBGP
)
2011 && (bgp
->as
== *as
))) {
2013 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2016 /* internal/external used, compare as-types */
2017 if (((peer_sort_type
== BGP_PEER_IBGP
)
2018 && (as_type
!= AS_INTERNAL
))
2019 || ((peer_sort_type
== BGP_PEER_EBGP
)
2020 && (as_type
!= AS_EXTERNAL
))) {
2022 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2027 /* Existing peer's AS number change. */
2028 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2029 || (peer
->as_type
!= as_type
))
2030 peer_as_change(peer
, *as
, as_type
);
2033 return BGP_ERR_NO_INTERFACE_CONFIG
;
2035 /* If the peer is not part of our confederation, and its not an
2036 iBGP peer then spoof the source AS */
2037 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
2038 && !bgp_confederation_peers_check(bgp
, *as
)
2040 local_as
= bgp
->confed_id
;
2044 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
,
2051 const char *bgp_get_name_by_role(uint8_t role
)
2056 case ROLE_RS_SERVER
:
2058 case ROLE_RS_CLIENT
:
2064 case ROLE_UNDEFINED
:
2070 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
2071 struct peer
*peer
, afi_t afi
,
2075 int out
= FILTER_OUT
;
2077 uint64_t pflags_ovrd
;
2078 uint8_t *pfilter_ovrd
;
2082 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2083 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2085 /* peer af_flags apply */
2086 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2087 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2088 ^ peer
->af_flags_invert
[afi
][safi
];
2089 flags_tmp
&= ~pflags_ovrd
;
2091 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2092 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2093 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2094 conf
->af_flags_invert
[afi
][safi
]);
2096 /* maximum-prefix */
2097 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2098 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2099 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2100 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2103 /* maximum-prefix-out */
2104 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX_OUT
))
2105 PEER_ATTR_INHERIT(peer
, group
, pmax_out
[afi
][safi
]);
2108 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2109 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2112 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_SOO
))
2113 PEER_ATTR_INHERIT(peer
, group
, soo
[afi
][safi
]);
2116 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2117 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2119 /* default-originate route-map */
2120 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2121 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2122 MTYPE_ROUTE_MAP_NAME
);
2123 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2126 /* inbound filter apply */
2127 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2128 PEER_STR_ATTR_INHERIT(peer
, group
,
2129 filter
[afi
][safi
].dlist
[in
].name
,
2130 MTYPE_BGP_FILTER_NAME
);
2131 PEER_ATTR_INHERIT(peer
, group
,
2132 filter
[afi
][safi
].dlist
[in
].alist
);
2135 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2136 PEER_STR_ATTR_INHERIT(peer
, group
,
2137 filter
[afi
][safi
].plist
[in
].name
,
2138 MTYPE_BGP_FILTER_NAME
);
2139 PEER_ATTR_INHERIT(peer
, group
,
2140 filter
[afi
][safi
].plist
[in
].plist
);
2143 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2144 PEER_STR_ATTR_INHERIT(peer
, group
,
2145 filter
[afi
][safi
].aslist
[in
].name
,
2146 MTYPE_BGP_FILTER_NAME
);
2147 PEER_ATTR_INHERIT(peer
, group
,
2148 filter
[afi
][safi
].aslist
[in
].aslist
);
2151 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2152 PEER_STR_ATTR_INHERIT(peer
, group
,
2153 filter
[afi
][safi
].map
[in
].name
,
2154 MTYPE_BGP_FILTER_NAME
);
2155 PEER_ATTR_INHERIT(peer
, group
,
2156 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2159 /* outbound filter apply */
2160 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2161 PEER_STR_ATTR_INHERIT(peer
, group
,
2162 filter
[afi
][safi
].dlist
[out
].name
,
2163 MTYPE_BGP_FILTER_NAME
);
2164 PEER_ATTR_INHERIT(peer
, group
,
2165 filter
[afi
][safi
].dlist
[out
].alist
);
2168 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2169 PEER_STR_ATTR_INHERIT(peer
, group
,
2170 filter
[afi
][safi
].plist
[out
].name
,
2171 MTYPE_BGP_FILTER_NAME
);
2172 PEER_ATTR_INHERIT(peer
, group
,
2173 filter
[afi
][safi
].plist
[out
].plist
);
2176 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2177 PEER_STR_ATTR_INHERIT(peer
, group
,
2178 filter
[afi
][safi
].aslist
[out
].name
,
2179 MTYPE_BGP_FILTER_NAME
);
2180 PEER_ATTR_INHERIT(peer
, group
,
2181 filter
[afi
][safi
].aslist
[out
].aslist
);
2184 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2185 PEER_STR_ATTR_INHERIT(peer
, group
,
2186 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2187 MTYPE_BGP_FILTER_NAME
);
2188 PEER_ATTR_INHERIT(peer
, group
,
2189 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2192 /* nondirectional filter apply */
2193 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2194 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2195 MTYPE_BGP_FILTER_NAME
);
2196 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2199 /* Conditional Advertisements */
2200 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ADVERTISE_MAP
)) {
2201 PEER_STR_ATTR_INHERIT(peer
, group
,
2202 filter
[afi
][safi
].advmap
.aname
,
2203 MTYPE_BGP_FILTER_NAME
);
2204 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.amap
);
2205 PEER_STR_ATTR_INHERIT(peer
, group
,
2206 filter
[afi
][safi
].advmap
.cname
,
2207 MTYPE_BGP_FILTER_NAME
);
2208 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].advmap
.cmap
);
2209 PEER_ATTR_INHERIT(peer
, group
,
2210 filter
[afi
][safi
].advmap
.condition
);
2213 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2214 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2215 bgp_addpath_type_changed(conf
->bgp
);
2219 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2224 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2225 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2226 __func__
, peer
->host
);
2230 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2232 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2233 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2234 return BGP_ERR_PEER_SAFI_CONFLICT
;
2236 /* Nothing to do if we've already activated this peer */
2237 if (peer
->afc
[afi
][safi
])
2240 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2243 active
= peer_active(peer
);
2244 peer
->afc
[afi
][safi
] = 1;
2247 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2249 if (!active
&& peer_active(peer
)) {
2250 bgp_timer_set(peer
);
2252 if (peer_established(peer
)) {
2253 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2254 peer
->afc_adv
[afi
][safi
] = 1;
2255 bgp_capability_send(peer
, afi
, safi
,
2257 CAPABILITY_ACTION_SET
);
2258 if (peer
->afc_recv
[afi
][safi
]) {
2259 peer
->afc_nego
[afi
][safi
] = 1;
2260 bgp_announce_route(peer
, afi
, safi
,
2264 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2265 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2266 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2269 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2270 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2271 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2272 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2275 * If we are turning on a AFI/SAFI locally and we've
2276 * started bringing a peer up, we need to tell
2277 * the other peer to restart because we might loose
2278 * configuration here because when the doppelganger
2279 * gets to a established state due to how
2280 * we resolve we could just overwrite the afi/safi
2283 other
= peer
->doppelganger
;
2285 && (other
->status
== OpenSent
2286 || other
->status
== OpenConfirm
)) {
2287 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2288 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2289 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2296 /* Activate the peer or peer group for specified AFI and SAFI. */
2297 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2300 struct peer_group
*group
;
2301 struct listnode
*node
, *nnode
;
2302 struct peer
*tmp_peer
;
2305 /* Nothing to do if we've already activated this peer */
2306 if (peer
->afc
[afi
][safi
])
2311 /* This is a peer-group so activate all of the members of the
2312 * peer-group as well */
2313 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2315 /* Do not activate a peer for both SAFI_UNICAST and
2316 * SAFI_LABELED_UNICAST */
2317 if ((safi
== SAFI_UNICAST
2318 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2319 || (safi
== SAFI_LABELED_UNICAST
2320 && peer
->afc
[afi
][SAFI_UNICAST
]))
2321 return BGP_ERR_PEER_SAFI_CONFLICT
;
2323 peer
->afc
[afi
][safi
] = 1;
2324 group
= peer
->group
;
2326 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2327 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2330 ret
|= peer_activate_af(peer
, afi
, safi
);
2333 /* If this is the first peer to be activated for this
2334 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2335 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2336 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2338 if (BGP_DEBUG(zebra
, ZEBRA
))
2340 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2342 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2343 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2346 if (safi
== SAFI_FLOWSPEC
) {
2347 /* connect to table manager */
2348 bgp_zebra_init_tm_connect(bgp
);
2353 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2356 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2357 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2358 __func__
, peer
->host
);
2362 /* Nothing to do if we've already deactivated this peer */
2363 if (!peer
->afc
[afi
][safi
])
2366 /* De-activate the address family configuration. */
2367 peer
->afc
[afi
][safi
] = 0;
2369 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2370 flog_err(EC_BGP_PEER_DELETE
,
2371 "couldn't delete af structure for peer %s(%s, %s)",
2372 peer
->host
, afi2str(afi
), safi2str(safi
));
2376 if (peer_established(peer
)) {
2377 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2378 peer
->afc_adv
[afi
][safi
] = 0;
2379 peer
->afc_nego
[afi
][safi
] = 0;
2381 if (peer_active_nego(peer
)) {
2382 bgp_capability_send(peer
, afi
, safi
,
2384 CAPABILITY_ACTION_UNSET
);
2385 bgp_clear_route(peer
, afi
, safi
);
2386 peer
->pcount
[afi
][safi
] = 0;
2388 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2389 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2390 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2393 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2394 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2395 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2402 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2405 struct peer_group
*group
;
2406 struct peer
*tmp_peer
;
2407 struct listnode
*node
, *nnode
;
2410 /* Nothing to do if we've already de-activated this peer */
2411 if (!peer
->afc
[afi
][safi
])
2414 /* This is a peer-group so de-activate all of the members of the
2415 * peer-group as well */
2416 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2417 peer
->afc
[afi
][safi
] = 0;
2418 group
= peer
->group
;
2420 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2421 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2424 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2429 /* If this is the last peer to be deactivated for this
2430 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2431 if (safi
== SAFI_LABELED_UNICAST
2432 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2433 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2435 if (BGP_DEBUG(zebra
, ZEBRA
))
2437 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2439 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2440 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2445 void peer_nsf_stop(struct peer
*peer
)
2450 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2451 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2453 FOREACH_AFI_SAFI_NSF (afi
, safi
) {
2454 peer
->nsf
[afi
][safi
] = 0;
2455 THREAD_OFF(peer
->t_llgr_stale
[afi
][safi
]);
2458 if (peer
->t_gr_restart
) {
2459 THREAD_OFF(peer
->t_gr_restart
);
2460 if (bgp_debug_neighbor_events(peer
))
2461 zlog_debug("%pBP graceful restart timer stopped", peer
);
2463 if (peer
->t_gr_stale
) {
2464 THREAD_OFF(peer
->t_gr_stale
);
2465 if (bgp_debug_neighbor_events(peer
))
2467 "%pBP graceful restart stalepath timer stopped",
2470 bgp_clear_route_all(peer
);
2473 /* Delete peer from confguration.
2475 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2476 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2478 * This function /should/ take care to be idempotent, to guard against
2479 * it being called multiple times through stray events that come in
2480 * that happen to result in this function being called again. That
2481 * said, getting here for a "Deleted" peer is a bug in the neighbour
2484 int peer_delete(struct peer
*peer
)
2490 struct bgp_filter
*filter
;
2491 struct listnode
*pn
;
2494 assert(peer
->status
!= Deleted
);
2497 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2499 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2501 bgp_keepalives_off(peer
);
2502 bgp_reads_off(peer
);
2503 bgp_writes_off(peer
);
2504 thread_cancel_event_ready(bm
->master
, peer
);
2505 FOREACH_AFI_SAFI (afi
, safi
)
2506 THREAD_OFF(peer
->t_revalidate_all
[afi
][safi
]);
2507 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2508 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2509 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2511 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2512 peer_nsf_stop(peer
);
2514 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2516 /* Remove BFD settings. */
2517 if (peer
->bfd_config
)
2518 bgp_peer_remove_bfd_config(peer
);
2520 /* If this peer belongs to peer group, clear up the
2523 if (peer_dynamic_neighbor(peer
))
2524 peer_drop_dynamic_neighbor(peer
);
2526 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2528 peer
); /* group->peer list reference */
2529 list_delete_node(peer
->group
->peer
, pn
);
2534 /* Withdraw all information from routing table. We can not use
2535 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2536 * executed after peer structure is deleted.
2538 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2540 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2542 if (peer
->doppelganger
) {
2543 peer
->doppelganger
->doppelganger
= NULL
;
2544 peer
->doppelganger
= NULL
;
2547 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2548 bgp_fsm_change_status(peer
, Deleted
);
2550 /* Remove from NHT */
2551 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2552 bgp_unlink_nexthop_by_peer(peer
);
2554 /* Password configuration */
2555 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2556 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2557 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2558 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2559 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2560 bgp_md5_unset(peer
);
2563 bgp_timer_set(peer
); /* stops all timers for Deleted */
2565 /* Delete from all peer list. */
2566 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2567 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2569 * Removing from the list node first because
2570 * peer_unlock *can* call peer_delete( I know,
2571 * I know ). So let's remove it and in
2572 * the su recalculate function we'll ensure
2573 * it's in there or not.
2575 list_delete_node(bgp
->peer
, pn
);
2576 hash_release(bgp
->peerhash
, peer
);
2577 peer_unlock(peer
); /* bgp peer list reference */
2582 stream_fifo_free(peer
->ibuf
);
2587 stream_fifo_free(peer
->obuf
);
2591 if (peer
->ibuf_work
) {
2592 ringbuf_del(peer
->ibuf_work
);
2593 peer
->ibuf_work
= NULL
;
2596 if (peer
->obuf_work
) {
2597 stream_free(peer
->obuf_work
);
2598 peer
->obuf_work
= NULL
;
2601 if (peer
->scratch
) {
2602 stream_free(peer
->scratch
);
2603 peer
->scratch
= NULL
;
2606 /* Local and remote addresses. */
2607 if (peer
->su_local
) {
2608 sockunion_free(peer
->su_local
);
2609 peer
->su_local
= NULL
;
2612 if (peer
->su_remote
) {
2613 sockunion_free(peer
->su_remote
);
2614 peer
->su_remote
= NULL
;
2617 /* Free filter related memory. */
2618 FOREACH_AFI_SAFI (afi
, safi
) {
2619 filter
= &peer
->filter
[afi
][safi
];
2621 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2622 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2623 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2624 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2627 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2628 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2631 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2632 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2633 ecommunity_free(&peer
->soo
[afi
][safi
]);
2636 FOREACH_AFI_SAFI (afi
, safi
)
2637 peer_af_delete(peer
, afi
, safi
);
2639 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2640 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2642 peer_unlock(peer
); /* initial reference */
2647 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2649 return strcmp(g1
->name
, g2
->name
);
2652 /* Peer group cofiguration. */
2653 static struct peer_group
*peer_group_new(void)
2655 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2658 static void peer_group_free(struct peer_group
*group
)
2660 XFREE(MTYPE_PEER_GROUP
, group
);
2663 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2665 struct peer_group
*group
;
2666 struct listnode
*node
, *nnode
;
2668 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2669 if (strcmp(group
->name
, name
) == 0)
2675 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2677 struct peer_group
*group
;
2681 group
= peer_group_lookup(bgp
, name
);
2685 group
= peer_group_new();
2687 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2688 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2689 group
->peer
= list_new();
2690 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2691 group
->listen_range
[afi
] = list_new();
2692 group
->conf
= peer_new(bgp
);
2693 FOREACH_AFI_SAFI (afi
, safi
) {
2694 if (bgp
->default_af
[afi
][safi
])
2695 group
->conf
->afc
[afi
][safi
] = 1;
2697 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2698 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2699 group
->conf
->group
= group
;
2700 group
->conf
->as
= 0;
2701 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2702 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2703 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2704 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2705 listnode_add_sort(bgp
->group
, group
);
2710 static void peer_group2peer_config_copy(struct peer_group
*group
,
2715 bool config_node
= !!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2721 peer
->as
= conf
->as
;
2724 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2725 peer
->change_local_as
= conf
->change_local_as
;
2727 /* If peer-group has configured TTL then override it */
2728 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2729 peer
->ttl
= conf
->ttl
;
2732 peer
->gtsm_hops
= conf
->gtsm_hops
;
2734 /* peer flags apply */
2735 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2736 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2737 flags_tmp
&= ~peer
->flags_override
;
2739 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2740 SET_FLAG(peer
->flags
, flags_tmp
);
2741 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2744 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2746 /* peer timers apply */
2747 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2748 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2749 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2752 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2753 PEER_ATTR_INHERIT(peer
, group
, connect
);
2754 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2755 peer
->v_connect
= conf
->connect
;
2757 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2760 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2761 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2762 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2763 peer
->v_delayopen
= conf
->delayopen
;
2765 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2768 /* advertisement-interval apply */
2769 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2770 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2771 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2772 peer
->v_routeadv
= conf
->routeadv
;
2774 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2775 ? BGP_DEFAULT_IBGP_ROUTEADV
2776 : BGP_DEFAULT_EBGP_ROUTEADV
;
2779 /* capability extended-nexthop apply */
2780 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2781 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2782 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2784 /* password apply */
2785 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2786 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2787 MTYPE_PEER_PASSWORD
);
2789 if (!BGP_PEER_SU_UNSPEC(peer
))
2792 /* update-source apply */
2793 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2794 if (conf
->update_source
) {
2795 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2796 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2797 } else if (conf
->update_if
) {
2798 sockunion_free(peer
->update_source
);
2799 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2800 MTYPE_PEER_UPDATE_SOURCE
);
2805 PEER_ATTR_INHERIT(peer
, group
, local_role
);
2807 /* Update GR flags for the peer. */
2808 bgp_peer_gr_flags_update(peer
);
2810 /* Apply BFD settings from group to peer if it exists. */
2811 if (conf
->bfd_config
) {
2812 bgp_peer_configure_bfd(peer
, false);
2813 bgp_peer_config_apply(peer
, group
);
2817 /* Peer group's remote AS configuration. */
2818 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2821 struct peer_group
*group
;
2823 struct listnode
*node
, *nnode
;
2825 group
= peer_group_lookup(bgp
, group_name
);
2829 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2833 /* When we setup peer-group AS number all peer group member's AS
2834 number must be updated to same number. */
2835 peer_as_change(group
->conf
, *as
, as_type
);
2837 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2838 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2839 || (peer
->as_type
!= as_type
))
2840 peer_as_change(peer
, *as
, as_type
);
2846 void peer_notify_unconfig(struct peer
*peer
)
2848 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2849 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2850 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2853 static void peer_notify_shutdown(struct peer
*peer
)
2855 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
2856 if (bgp_debug_neighbor_events(peer
))
2858 "%pBP configured Graceful-Restart, skipping shutdown notification",
2863 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2864 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2865 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
2868 void peer_group_notify_unconfig(struct peer_group
*group
)
2870 struct peer
*peer
, *other
;
2871 struct listnode
*node
, *nnode
;
2873 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2874 other
= peer
->doppelganger
;
2875 if (other
&& other
->status
!= Deleted
) {
2876 other
->group
= NULL
;
2877 peer_notify_unconfig(other
);
2879 peer_notify_unconfig(peer
);
2883 int peer_group_delete(struct peer_group
*group
)
2887 struct prefix
*prefix
;
2889 struct listnode
*node
, *nnode
;
2894 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2895 other
= peer
->doppelganger
;
2897 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2898 bgp_zebra_terminate_radv(bgp
, peer
);
2901 if (other
&& other
->status
!= Deleted
) {
2902 other
->group
= NULL
;
2906 list_delete(&group
->peer
);
2908 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2909 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2911 prefix_free(&prefix
);
2913 list_delete(&group
->listen_range
[afi
]);
2916 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2919 if (group
->conf
->bfd_config
)
2920 bgp_peer_remove_bfd_config(group
->conf
);
2922 group
->conf
->group
= NULL
;
2923 peer_delete(group
->conf
);
2925 /* Delete from all peer_group list. */
2926 listnode_delete(bgp
->group
, group
);
2928 peer_group_free(group
);
2933 int peer_group_remote_as_delete(struct peer_group
*group
)
2935 struct peer
*peer
, *other
;
2936 struct listnode
*node
, *nnode
;
2938 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2939 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2942 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2943 other
= peer
->doppelganger
;
2945 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2946 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2950 if (other
&& other
->status
!= Deleted
) {
2951 other
->group
= NULL
;
2955 list_delete_all_node(group
->peer
);
2957 group
->conf
->as
= 0;
2958 group
->conf
->as_type
= AS_UNSPECIFIED
;
2963 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2965 struct prefix
*prefix
;
2966 struct listnode
*node
, *nnode
;
2969 afi
= family2afi(range
->family
);
2971 /* Group needs remote AS configured. */
2972 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2973 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2975 /* Ensure no duplicates. Currently we don't care about overlaps. */
2976 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2977 if (prefix_same(range
, prefix
))
2981 prefix
= prefix_new();
2982 prefix_copy(prefix
, range
);
2983 listnode_add(group
->listen_range
[afi
], prefix
);
2985 /* Update passwords for new ranges */
2986 if (group
->conf
->password
)
2987 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
2992 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2994 struct prefix
*prefix
, prefix2
;
2995 struct listnode
*node
, *nnode
;
2999 afi
= family2afi(range
->family
);
3001 /* Identify the listen range. */
3002 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
3003 if (prefix_same(range
, prefix
))
3008 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
3010 /* Dispose off any dynamic neighbors that exist due to this listen range
3012 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
3013 if (!peer_dynamic_neighbor(peer
))
3016 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
3017 && prefix_match(prefix
, &prefix2
)) {
3018 if (bgp_debug_neighbor_events(peer
))
3020 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
3021 peer
->host
, group
->name
, prefix
);
3026 /* Get rid of the listen range */
3027 listnode_delete(group
->listen_range
[afi
], prefix
);
3029 /* Remove passwords for deleted ranges */
3030 if (group
->conf
->password
)
3031 bgp_md5_unset_prefix(group
->bgp
, prefix
);
3036 /* Bind specified peer to peer group. */
3037 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
3038 struct peer_group
*group
, as_t
*as
)
3040 int first_member
= 0;
3043 enum bgp_peer_sort ptype
, gtype
;
3045 /* Lookup the peer. */
3047 peer
= peer_lookup(bgp
, su
);
3049 /* The peer exist, bind it to the peer-group */
3051 /* When the peer already belongs to a peer-group, check the
3053 if (peer_group_active(peer
)) {
3055 /* The peer is already bound to the peer-group,
3058 if (strcmp(peer
->group
->name
, group
->name
) == 0)
3061 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
3064 /* The peer has not specified a remote-as, inherit it from the
3066 if (peer
->as_type
== AS_UNSPECIFIED
) {
3067 peer
->as_type
= group
->conf
->as_type
;
3068 peer
->as
= group
->conf
->as
;
3069 peer
->sort
= group
->conf
->sort
;
3072 ptype
= peer_sort(peer
);
3073 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
3074 gtype
= peer_sort(group
->conf
);
3075 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
3078 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
3081 if (gtype
== BGP_PEER_INTERNAL
)
3085 peer_group2peer_config_copy(group
, peer
);
3087 FOREACH_AFI_SAFI (afi
, safi
) {
3088 if (group
->conf
->afc
[afi
][safi
]) {
3089 peer
->afc
[afi
][safi
] = 1;
3091 if (peer_af_find(peer
, afi
, safi
)
3092 || peer_af_create(peer
, afi
, safi
)) {
3093 peer_group2peer_config_copy_af(
3094 group
, peer
, afi
, safi
);
3096 } else if (peer
->afc
[afi
][safi
])
3097 peer_deactivate(peer
, afi
, safi
);
3101 assert(group
&& peer
->group
== group
);
3103 listnode_delete(bgp
->peer
, peer
);
3105 peer
->group
= group
;
3106 listnode_add_sort(bgp
->peer
, peer
);
3108 peer
= peer_lock(peer
); /* group->peer list reference */
3109 listnode_add(group
->peer
, peer
);
3113 gtype
= peer_sort(group
->conf
);
3114 /* Advertisement-interval reset */
3115 if (!CHECK_FLAG(group
->conf
->flags
,
3116 PEER_FLAG_ROUTEADV
)) {
3117 group
->conf
->v_routeadv
=
3118 (gtype
== BGP_PEER_IBGP
)
3119 ? BGP_DEFAULT_IBGP_ROUTEADV
3120 : BGP_DEFAULT_EBGP_ROUTEADV
;
3123 /* ebgp-multihop reset */
3124 if (gtype
== BGP_PEER_IBGP
)
3125 group
->conf
->ttl
= MAXTTL
;
3128 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3130 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3131 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3132 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3133 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3135 bgp_session_reset(peer
);
3139 /* Create a new peer. */
3141 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3142 && (!group
->conf
->as
)) {
3143 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3146 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3147 group
->conf
->as_type
, group
, true);
3149 peer
= peer_lock(peer
); /* group->peer list reference */
3150 listnode_add(group
->peer
, peer
);
3152 peer_group2peer_config_copy(group
, peer
);
3154 /* If the peer-group is active for this afi/safi then activate
3156 FOREACH_AFI_SAFI (afi
, safi
) {
3157 if (group
->conf
->afc
[afi
][safi
]) {
3158 peer
->afc
[afi
][safi
] = 1;
3160 if (!peer_af_find(peer
, afi
, safi
))
3161 peer_af_create(peer
, afi
, safi
);
3163 peer_group2peer_config_copy_af(group
, peer
, afi
,
3165 } else if (peer
->afc
[afi
][safi
])
3166 peer_deactivate(peer
, afi
, safi
);
3169 /* Set up peer's events and timers. */
3170 if (peer_active(peer
))
3171 bgp_timer_set(peer
);
3177 static void bgp_startup_timer_expire(struct thread
*thread
)
3181 bgp
= THREAD_ARG(thread
);
3182 bgp
->t_startup
= NULL
;
3186 * On shutdown we call the cleanup function which
3187 * does a free of the link list nodes, free up
3188 * the data we are pointing at too.
3190 static void bgp_vrf_string_name_delete(void *data
)
3194 XFREE(MTYPE_TMP
, vname
);
3197 /* BGP instance creation by `router bgp' commands. */
3198 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3199 enum bgp_instance_type inst_type
)
3205 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3207 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3208 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3209 zlog_debug("Creating Default VRF, AS %u", *as
);
3211 zlog_debug("Creating %s %s, AS %u",
3212 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3218 /* Default the EVPN VRF to the default one */
3219 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3226 bgp
->allow_martian
= false;
3227 bgp_process_queue_init(bgp
);
3228 bgp
->heuristic_coalesce
= true;
3229 bgp
->inst_type
= inst_type
;
3230 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3232 bgp
->peer_self
= peer_new(bgp
);
3233 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3234 bgp
->peer_self
->host
=
3235 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3236 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3237 if (cmd_hostname_get())
3238 bgp
->peer_self
->hostname
=
3239 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3241 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3242 if (cmd_domainname_get())
3243 bgp
->peer_self
->domainname
=
3244 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3245 bgp
->peer
= list_new();
3246 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3247 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3249 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3251 bgp
->group
= list_new();
3252 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3254 FOREACH_AFI_SAFI (afi
, safi
) {
3255 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3256 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3257 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3259 /* Enable maximum-paths */
3260 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3262 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3264 /* Initialize graceful restart info */
3265 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3266 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3267 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3268 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3269 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3272 bgp
->v_update_delay
= bm
->v_update_delay
;
3273 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3274 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3275 bgp
->default_subgroup_pkt_queue_max
=
3276 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3277 bgp_tcp_keepalive_unset(bgp
);
3278 bgp_timers_unset(bgp
);
3279 bgp
->default_min_holdtime
= 0;
3280 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3281 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3282 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3283 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3284 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3285 bgp
->dynamic_neighbors_count
= 0;
3286 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3287 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3288 bgp
->reject_as_sets
= false;
3289 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3290 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3291 bgp
->fast_convergence
= false;
3293 bgp
->llgr_stale_time
= BGP_DEFAULT_LLGR_STALE_TIME
;
3295 #ifdef ENABLE_BGP_VNC
3296 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3297 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3299 assert(bgp
->rfapi_cfg
);
3301 #endif /* ENABLE_BGP_VNC */
3303 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3304 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3305 bgp
->vpn_policy
[afi
].afi
= afi
;
3306 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3307 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3310 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3311 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3312 bgp_vrf_string_name_delete
;
3313 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3314 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3315 bgp_vrf_string_name_delete
;
3316 SET_FLAG(bgp
->af_flags
[afi
][SAFI_MPLS_VPN
],
3317 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL
);
3320 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3322 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3323 bgp
->restart_time
, &bgp
->t_startup
);
3325 /* printable name we can use in debug messages */
3326 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3327 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3337 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3339 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3340 snprintf(bgp
->name_pretty
, len
, "%s %s",
3341 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3347 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3348 memory_order_relaxed
);
3349 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3350 memory_order_relaxed
);
3351 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3352 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3356 update_bgp_group_init(bgp
);
3358 /* assign a unique rd id for auto derivation of vrf's RD */
3359 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3361 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3362 sizeof(struct bgp_evpn_info
));
3364 bgp_evpn_vrf_es_init(bgp
);
3368 /*initilize global GR FSM */
3369 bgp_global_gr_init(bgp
);
3371 memset(&bgp
->ebgprequirespolicywarning
, 0,
3372 sizeof(bgp
->ebgprequirespolicywarning
));
3377 /* Return the "default VRF" instance of BGP. */
3378 struct bgp
*bgp_get_default(void)
3381 struct listnode
*node
, *nnode
;
3383 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3384 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3389 /* Lookup BGP entry. */
3390 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3393 struct listnode
*node
, *nnode
;
3395 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3397 && ((bgp
->name
== NULL
&& name
== NULL
)
3398 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3403 /* Lookup BGP structure by view name. */
3404 struct bgp
*bgp_lookup_by_name(const char *name
)
3407 struct listnode
*node
, *nnode
;
3409 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3410 if ((bgp
->name
== NULL
&& name
== NULL
)
3411 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3416 /* Lookup BGP instance based on VRF id. */
3417 /* Note: Only to be used for incoming messages from Zebra. */
3418 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3422 /* Lookup VRF (in tree) and follow link. */
3423 vrf
= vrf_lookup_by_id(vrf_id
);
3426 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3429 /* Sets the BGP instance where EVPN is enabled */
3430 void bgp_set_evpn(struct bgp
*bgp
)
3432 if (bm
->bgp_evpn
== bgp
)
3435 /* First, release the reference count we hold on the instance */
3437 bgp_unlock(bm
->bgp_evpn
);
3441 /* Increase the reference count on this new VRF */
3443 bgp_lock(bm
->bgp_evpn
);
3446 /* Returns the BGP instance where EVPN is enabled, if any */
3447 struct bgp
*bgp_get_evpn(void)
3449 return bm
->bgp_evpn
;
3452 /* handle socket creation or deletion, if necessary
3453 * this is called for all new BGP instances
3455 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3458 struct listnode
*node
;
3461 /* Create BGP server socket, if listen mode not disabled */
3462 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3464 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3466 * suppress vrf socket
3469 bgp_close_vrf_socket(bgp
);
3473 return BGP_ERR_INVALID_VALUE
;
3475 * if vrf_id did not change
3477 if (vrf
->vrf_id
== old_vrf_id
)
3479 if (old_vrf_id
!= VRF_UNKNOWN
) {
3480 /* look for old socket. close it. */
3481 bgp_close_vrf_socket(bgp
);
3483 /* if backend is not yet identified ( VRF_UNKNOWN) then
3484 * creation will be done later
3486 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3488 if (list_isempty(bm
->addresses
)) {
3489 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3490 return BGP_ERR_INVALID_VALUE
;
3492 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3493 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3494 return BGP_ERR_INVALID_VALUE
;
3498 return bgp_check_main_socket(create
, bgp
);
3501 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3502 enum bgp_instance_type inst_type
)
3506 /* Multiple instance check. */
3508 bgp
= bgp_lookup_by_name(name
);
3510 bgp
= bgp_get_default();
3514 if (bgp
->as
!= *as
) {
3516 return BGP_ERR_AS_MISMATCH
;
3518 if (bgp
->inst_type
!= inst_type
)
3519 return BGP_ERR_INSTANCE_MISMATCH
;
3527 /* Called from VTY commands. */
3528 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3529 enum bgp_instance_type inst_type
)
3532 struct vrf
*vrf
= NULL
;
3535 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3536 if (ret
|| *bgp_val
)
3539 bgp
= bgp_create(as
, name
, inst_type
);
3542 * view instances will never work inside of a vrf
3543 * as such they must always be in the VRF_DEFAULT
3544 * Also we must set this to something useful because
3545 * of the vrf socket code needing an actual useful
3546 * default value to send to the underlying OS.
3548 * This code is currently ignoring vrf based
3549 * code using the -Z option( and that is probably
3550 * best addressed elsewhere in the code )
3552 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3553 bgp
->vrf_id
= VRF_DEFAULT
;
3555 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3556 bgp_address_init(bgp
);
3557 bgp_tip_hash_init(bgp
);
3561 bgp
->t_rmap_def_originate_eval
= NULL
;
3563 /* If Default instance or VRF, link to the VRF structure, if present. */
3564 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3565 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3566 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3568 bgp_vrf_link(bgp
, vrf
);
3570 /* BGP server socket already processed if BGP instance
3571 * already part of the list
3573 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3574 listnode_add(bm
->bgp
, bgp
);
3576 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3577 if (BGP_DEBUG(zebra
, ZEBRA
))
3578 zlog_debug("%s: Registering BGP instance %s to zebra",
3579 __func__
, bgp
->name_pretty
);
3580 bgp_zebra_instance_register(bgp
);
3586 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3587 vrf_id_t vrf_id
, bool set
)
3591 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3594 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3598 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3600 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3604 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3608 struct list
*red_list
;
3609 struct listnode
*node
;
3610 struct bgp_redist
*red
;
3612 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3613 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3615 red_list
= bgp
->redist
[afi
][i
];
3619 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3620 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3627 * Make BGP instance "up". Applies only to VRFs (non-default) and
3628 * implies the VRF has been learnt from Zebra.
3630 void bgp_instance_up(struct bgp
*bgp
)
3633 struct listnode
*node
, *next
;
3635 bgp_set_redist_vrf_bitmaps(bgp
, true);
3637 /* Register with zebra. */
3638 bgp_zebra_instance_register(bgp
);
3640 /* Kick off any peers that may have been configured. */
3641 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3642 if (!BGP_PEER_START_SUPPRESSED(peer
))
3643 BGP_EVENT_ADD(peer
, BGP_Start
);
3646 /* Process any networks that have been configured. */
3647 bgp_static_add(bgp
);
3651 * Make BGP instance "down". Applies only to VRFs (non-default) and
3652 * implies the VRF has been deleted by Zebra.
3654 void bgp_instance_down(struct bgp
*bgp
)
3657 struct listnode
*node
;
3658 struct listnode
*next
;
3661 if (bgp
->t_rmap_def_originate_eval
) {
3662 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3663 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3667 /* Bring down peers, so corresponding routes are purged. */
3668 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3669 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3670 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3671 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3673 bgp_session_reset(peer
);
3676 /* Purge network and redistributed routes. */
3677 bgp_purge_static_redist_routes(bgp
);
3679 /* Cleanup registered nexthops (flags) */
3680 bgp_cleanup_nexthops(bgp
);
3682 bgp_zebra_instance_deregister(bgp
);
3684 bgp_set_redist_vrf_bitmaps(bgp
, false);
3687 /* Delete BGP instance. */
3688 int bgp_delete(struct bgp
*bgp
)
3691 struct peer_group
*group
;
3692 struct listnode
*node
, *next
;
3697 struct graceful_restart_info
*gr_info
;
3701 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3703 /* make sure we withdraw any exported routes */
3704 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3706 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3709 bgp_vpn_leak_unimport(bgp
);
3711 hook_call(bgp_inst_delete
, bgp
);
3713 FOREACH_AFI_SAFI (afi
, safi
)
3714 THREAD_OFF(bgp
->t_revalidate
[afi
][safi
]);
3716 THREAD_OFF(bgp
->t_condition_check
);
3717 THREAD_OFF(bgp
->t_startup
);
3718 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3719 THREAD_OFF(bgp
->t_update_delay
);
3720 THREAD_OFF(bgp
->t_establish_wait
);
3722 /* Set flag indicating bgp instance delete in progress */
3723 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3725 /* Delete the graceful restart info */
3726 FOREACH_AFI_SAFI (afi
, safi
) {
3729 gr_info
= &bgp
->gr_info
[afi
][safi
];
3732 t
= gr_info
->t_select_deferral
;
3734 void *info
= THREAD_ARG(t
);
3736 XFREE(MTYPE_TMP
, info
);
3738 THREAD_OFF(gr_info
->t_select_deferral
);
3740 t
= gr_info
->t_route_select
;
3742 void *info
= THREAD_ARG(t
);
3744 XFREE(MTYPE_TMP
, info
);
3746 THREAD_OFF(gr_info
->t_route_select
);
3749 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3750 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3751 zlog_debug("Deleting Default VRF");
3753 zlog_debug("Deleting %s %s",
3754 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3760 /* unmap from RT list */
3761 bgp_evpn_vrf_delete(bgp
);
3763 /* unmap bgp vrf label */
3764 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3765 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3768 if (bgp
->t_rmap_def_originate_eval
) {
3769 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
3770 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3774 /* Inform peers we're going down. */
3775 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3776 peer_notify_shutdown(peer
);
3778 /* Delete static routes (networks). */
3779 bgp_static_delete(bgp
);
3781 /* Unset redistribution. */
3782 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3783 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3784 if (i
!= ZEBRA_ROUTE_BGP
)
3785 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3787 /* Free peers and peer-groups. */
3788 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3789 peer_group_delete(group
);
3791 while (listcount(bgp
->peer
)) {
3792 peer
= listnode_head(bgp
->peer
);
3796 if (bgp
->peer_self
) {
3797 peer_delete(bgp
->peer_self
);
3798 bgp
->peer_self
= NULL
;
3801 update_bgp_group_free(bgp
);
3803 /* TODO - Other memory may need to be freed - e.g., NHT */
3805 #ifdef ENABLE_BGP_VNC
3808 bgp_cleanup_routes(bgp
);
3810 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3811 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3814 &bgp
->vpn_policy
[afi
]
3815 .import_redirect_rtlist
);
3816 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3819 /* Free any memory allocated to holding routemap references */
3820 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3821 for (enum vpn_policy_direction dir
= 0;
3822 dir
< BGP_VPN_POLICY_DIR_MAX
; ++dir
) {
3823 if (bgp
->vpn_policy
[afi
].rmap_name
[dir
])
3824 XFREE(MTYPE_ROUTE_MAP_NAME
,
3825 bgp
->vpn_policy
[afi
].rmap_name
[dir
]);
3826 bgp
->vpn_policy
[afi
].rmap
[dir
] = NULL
;
3830 /* Deregister from Zebra, if needed */
3831 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3832 if (BGP_DEBUG(zebra
, ZEBRA
))
3834 "%s: deregistering this bgp %s instance from zebra",
3835 __func__
, bgp
->name
);
3836 bgp_zebra_instance_deregister(bgp
);
3839 /* Remove visibility via the master list - there may however still be
3840 * routes to be processed still referencing the struct bgp.
3842 listnode_delete(bm
->bgp
, bgp
);
3844 /* Free interfaces in this instance. */
3847 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3848 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3850 bgp_vrf_unlink(bgp
, vrf
);
3852 /* Update EVPN VRF pointer */
3853 if (bm
->bgp_evpn
== bgp
) {
3854 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3857 bgp_set_evpn(bgp_get_default());
3860 if (bgp
->process_queue
)
3861 work_queue_free_and_null(&bgp
->process_queue
);
3863 thread_master_free_unused(bm
->master
);
3864 bgp_unlock(bgp
); /* initial reference */
3869 void bgp_free(struct bgp
*bgp
)
3873 struct bgp_table
*table
;
3874 struct bgp_dest
*dest
;
3875 struct bgp_rmap
*rmap
;
3879 list_delete(&bgp
->group
);
3880 list_delete(&bgp
->peer
);
3882 if (bgp
->peerhash
) {
3883 hash_free(bgp
->peerhash
);
3884 bgp
->peerhash
= NULL
;
3887 FOREACH_AFI_SAFI (afi
, safi
) {
3888 /* Special handling for 2-level routing tables. */
3889 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3890 || safi
== SAFI_EVPN
) {
3891 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3892 dest
= bgp_route_next(dest
)) {
3893 table
= bgp_dest_get_bgp_table_info(dest
);
3894 bgp_table_finish(&table
);
3897 if (bgp
->route
[afi
][safi
])
3898 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3899 if (bgp
->aggregate
[afi
][safi
])
3900 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3901 if (bgp
->rib
[afi
][safi
])
3902 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3903 rmap
= &bgp
->table_map
[afi
][safi
];
3904 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3907 bgp_scan_finish(bgp
);
3908 bgp_address_destroy(bgp
);
3909 bgp_tip_hash_destroy(bgp
);
3911 /* release the auto RD id */
3912 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3914 bgp_evpn_cleanup(bgp
);
3915 bgp_pbr_cleanup(bgp
);
3916 bgp_srv6_cleanup(bgp
);
3917 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3919 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3920 enum vpn_policy_direction dir
;
3922 if (bgp
->vpn_policy
[afi
].import_vrf
)
3923 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3924 if (bgp
->vpn_policy
[afi
].export_vrf
)
3925 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3927 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3928 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3929 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3930 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3931 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3932 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3935 bgp_orr_cleanup(bgp
);
3937 XFREE(MTYPE_BGP
, bgp
->name
);
3938 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3939 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3941 XFREE(MTYPE_BGP
, bgp
);
3944 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3947 struct listnode
*node
, *nnode
;
3953 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3954 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3955 && !CHECK_FLAG(peer
->sflags
,
3956 PEER_STATUS_ACCEPT_PEER
))
3958 } else if (bm
->bgp
!= NULL
) {
3959 struct listnode
*bgpnode
, *nbgpnode
;
3961 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3962 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3964 && !strcmp(peer
->conf_if
, conf_if
)
3965 && !CHECK_FLAG(peer
->sflags
,
3966 PEER_STATUS_ACCEPT_PEER
))
3972 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3975 struct listnode
*node
, *nnode
;
3981 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3982 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3983 && !CHECK_FLAG(peer
->sflags
,
3984 PEER_STATUS_ACCEPT_PEER
))
3986 } else if (bm
->bgp
!= NULL
) {
3987 struct listnode
*bgpnode
, *nbgpnode
;
3989 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3990 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3992 && !strcmp(peer
->hostname
, hostname
)
3993 && !CHECK_FLAG(peer
->sflags
,
3994 PEER_STATUS_ACCEPT_PEER
))
4000 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
4002 struct peer
*peer
= NULL
;
4003 struct peer tmp_peer
;
4005 memset(&tmp_peer
, 0, sizeof(struct peer
));
4008 * We do not want to find the doppelganger peer so search for the peer
4010 * the hash that has PEER_FLAG_CONFIG_NODE
4012 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
4017 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4018 } else if (bm
->bgp
!= NULL
) {
4019 struct listnode
*bgpnode
, *nbgpnode
;
4021 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
4022 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
4031 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
4032 union sockunion
*su
,
4033 struct peer_group
*group
)
4039 /* Create peer first; we've already checked group config is valid. */
4040 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
4041 group
->conf
->as_type
, group
, true);
4046 peer
= peer_lock(peer
);
4047 listnode_add(group
->peer
, peer
);
4049 peer_group2peer_config_copy(group
, peer
);
4052 * Bind peer for all AFs configured for the group. We don't call
4053 * peer_group_bind as that is sub-optimal and does some stuff we don't
4056 FOREACH_AFI_SAFI (afi
, safi
) {
4057 if (!group
->conf
->afc
[afi
][safi
])
4059 peer
->afc
[afi
][safi
] = 1;
4061 if (!peer_af_find(peer
, afi
, safi
))
4062 peer_af_create(peer
, afi
, safi
);
4064 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
4067 /* Mark as dynamic, but also as a "config node" for other things to
4069 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
4075 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
4076 struct prefix
*prefix
)
4078 struct listnode
*node
, *nnode
;
4079 struct prefix
*range
;
4082 afi
= family2afi(prefix
->family
);
4084 if (group
->listen_range
[afi
])
4085 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
4087 if (prefix_match(range
, prefix
))
4094 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
4095 struct prefix
**listen_range
)
4097 struct prefix
*range
= NULL
;
4098 struct peer_group
*group
= NULL
;
4099 struct listnode
*node
, *nnode
;
4101 *listen_range
= NULL
;
4103 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4104 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4107 } else if (bm
->bgp
!= NULL
) {
4108 struct listnode
*bgpnode
, *nbgpnode
;
4110 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
4111 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
4112 if ((range
= peer_group_lookup_dynamic_neighbor_range(
4118 *listen_range
= range
;
4119 return (group
&& range
) ? group
: NULL
;
4122 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
4124 struct peer_group
*group
;
4127 struct prefix prefix
;
4128 struct prefix
*listen_range
;
4131 if (!sockunion2hostprefix(su
, &prefix
))
4134 /* See if incoming connection matches a configured listen range. */
4135 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
4146 if (bgp_debug_neighbor_events(NULL
))
4148 "Dynamic Neighbor %pFX matches group %s listen range %pFX",
4149 &prefix
, group
->name
, listen_range
);
4151 /* Are we within the listen limit? */
4152 dncount
= gbgp
->dynamic_neighbors_count
;
4154 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4155 if (bgp_debug_neighbor_events(NULL
))
4157 "Dynamic Neighbor %pFX rejected - at limit %d",
4158 &prefix
, gbgp
->dynamic_neighbors_limit
);
4162 /* Ensure group is not disabled. */
4163 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4164 if (bgp_debug_neighbor_events(NULL
))
4166 "Dynamic Neighbor %pFX rejected - group %s disabled",
4167 &prefix
, group
->name
);
4171 /* Check that at least one AF is activated for the group. */
4172 if (!peer_group_af_configured(group
)) {
4173 if (bgp_debug_neighbor_events(NULL
))
4175 "Dynamic Neighbor %pFX rejected - no AF activated for group %s",
4176 &prefix
, group
->name
);
4180 /* Create dynamic peer and bind to associated group. */
4181 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4184 gbgp
->dynamic_neighbors_count
= ++dncount
;
4186 if (bgp_debug_neighbor_events(peer
))
4187 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4188 peer
->host
, group
->name
, dncount
);
4193 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4196 if (peer
->group
->bgp
) {
4197 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4199 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4201 if (bgp_debug_neighbor_events(peer
))
4202 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4203 peer
->group
->name
, dncount
);
4206 bool bgp_path_attribute_discard(struct peer
*peer
, char *buf
, size_t size
)
4213 for (unsigned int i
= 0; i
< BGP_ATTR_MAX
; i
++) {
4214 if (peer
->discard_attrs
[i
])
4215 snprintf(buf
+ strlen(buf
), size
- strlen(buf
), "%s%d",
4216 (strlen(buf
) > 0) ? " " : "", i
);
4219 if (strlen(buf
) > 0)
4225 /* If peer is configured at least one address family return 1. */
4226 bool peer_active(struct peer
*peer
)
4228 if (BGP_PEER_SU_UNSPEC(peer
))
4230 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4231 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4232 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4233 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4234 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4235 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4236 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4237 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4238 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4239 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4240 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4245 /* If peer is negotiated at least one address family return 1. */
4246 bool peer_active_nego(struct peer
*peer
)
4248 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4249 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4250 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4251 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4252 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4253 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4254 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4255 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4256 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4257 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4258 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4259 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4260 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4265 /* If peer received at least one address family MP, return true */
4266 bool peer_afc_received(struct peer
*peer
)
4271 FOREACH_AFI_SAFI (afi
, safi
)
4272 if (peer
->afc_recv
[afi
][safi
])
4278 /* If peer advertised at least one address family MP, return true */
4279 bool peer_afc_advertised(struct peer
*peer
)
4284 FOREACH_AFI_SAFI (afi
, safi
)
4285 if (peer
->afc_adv
[afi
][safi
])
4291 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4292 enum peer_change_type type
)
4294 struct peer_af
*paf
;
4296 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4299 if (!peer_established(peer
))
4302 if (type
== peer_change_reset
) {
4303 /* If we're resetting session, we've to delete both peer struct
4305 if ((peer
->doppelganger
)
4306 && (peer
->doppelganger
->status
!= Deleted
)
4307 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4308 PEER_FLAG_CONFIG_NODE
)))
4309 peer_delete(peer
->doppelganger
);
4311 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4312 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4313 } else if (type
== peer_change_reset_in
) {
4314 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4315 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4316 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4317 BGP_ROUTE_REFRESH_NORMAL
);
4319 if ((peer
->doppelganger
)
4320 && (peer
->doppelganger
->status
!= Deleted
)
4321 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4322 PEER_FLAG_CONFIG_NODE
)))
4323 peer_delete(peer
->doppelganger
);
4325 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4326 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4328 } else if (type
== peer_change_reset_out
) {
4329 paf
= peer_af_find(peer
, afi
, safi
);
4330 if (paf
&& paf
->subgroup
)
4331 SET_FLAG(paf
->subgroup
->sflags
,
4332 SUBGRP_STATUS_FORCE_UPDATES
);
4334 update_group_adjust_peer(paf
);
4335 bgp_announce_route(peer
, afi
, safi
, false);
4339 struct peer_flag_action
{
4343 /* This flag can be set for peer-group member. */
4344 uint8_t not_for_member
;
4346 /* Action when the flag is changed. */
4347 enum peer_change_type type
;
4350 static const struct peer_flag_action peer_flag_action_list
[] = {
4351 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4352 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4353 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4354 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4355 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4356 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4357 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4358 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4359 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4360 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4361 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4362 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4363 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4364 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4365 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4366 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4367 {PEER_FLAG_LOCAL_AS
, 0, peer_change_reset
},
4368 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_reset
},
4369 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_reset
},
4370 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4371 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4372 {PEER_FLAG_EXTENDED_OPT_PARAMS
, 0, peer_change_reset
},
4373 {PEER_FLAG_ROLE_STRICT_MODE
, 0, peer_change_reset
},
4374 {PEER_FLAG_ROLE
, 0, peer_change_reset
},
4375 {PEER_FLAG_PORT
, 0, peer_change_reset
},
4376 {PEER_FLAG_AIGP
, 0, peer_change_none
},
4377 {PEER_FLAG_GRACEFUL_SHUTDOWN
, 0, peer_change_none
},
4380 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4381 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4382 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4383 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4384 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4385 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4386 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4387 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4388 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4389 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4390 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4391 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4392 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4393 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4394 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4395 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4396 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4397 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4398 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4399 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4400 {PEER_FLAG_MAX_PREFIX_OUT
, 0, peer_change_none
},
4401 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4402 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4403 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4404 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4405 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4406 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4407 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4408 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4409 {PEER_FLAG_SOO
, 0, peer_change_reset
},
4410 {PEER_FLAG_ACCEPT_OWN
, 0, peer_change_reset
},
4413 /* Proper action set. */
4414 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4415 int size
, struct peer_flag_action
*action
,
4422 const struct peer_flag_action
*match
= NULL
;
4424 /* Check peer's frag action. */
4425 for (i
= 0; i
< size
; i
++) {
4426 match
= &action_list
[i
];
4428 if (match
->flag
== 0)
4431 if (match
->flag
& flag
) {
4434 if (match
->type
== peer_change_reset_in
)
4436 if (match
->type
== peer_change_reset_out
)
4438 if (match
->type
== peer_change_reset
) {
4442 if (match
->not_for_member
)
4443 action
->not_for_member
= 1;
4447 /* Set peer clear type. */
4448 if (reset_in
&& reset_out
)
4449 action
->type
= peer_change_reset
;
4451 action
->type
= peer_change_reset_in
;
4453 action
->type
= peer_change_reset_out
;
4455 action
->type
= peer_change_none
;
4460 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4462 if (flag
== PEER_FLAG_SHUTDOWN
) {
4463 if (CHECK_FLAG(peer
->flags
, flag
)) {
4464 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4465 peer_nsf_stop(peer
);
4467 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4469 if (peer
->t_pmax_restart
) {
4470 THREAD_OFF(peer
->t_pmax_restart
);
4471 if (bgp_debug_neighbor_events(peer
))
4473 "%pBP Maximum-prefix restart timer canceled",
4477 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4478 char *msg
= peer
->tx_shutdown_message
;
4480 uint8_t msgbuf
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4482 if (!msg
&& peer_group_active(peer
))
4483 msg
= peer
->group
->conf
4484 ->tx_shutdown_message
;
4485 msglen
= msg
? strlen(msg
) : 0;
4486 if (msglen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4487 msglen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4491 memcpy(msgbuf
+ 1, msg
, msglen
);
4493 bgp_notify_send_with_data(
4494 peer
, BGP_NOTIFY_CEASE
,
4495 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4496 msgbuf
, msglen
+ 1);
4499 peer
, BGP_NOTIFY_CEASE
,
4500 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4502 bgp_session_reset(peer
);
4504 peer
->v_start
= BGP_INIT_START_TIMER
;
4505 BGP_EVENT_ADD(peer
, BGP_Stop
);
4507 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4508 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4509 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4510 else if (flag
== PEER_FLAG_PASSIVE
)
4511 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4512 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4513 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4515 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4516 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4518 bgp_session_reset(peer
);
4521 /* Enable global administrative shutdown of all peers of BGP instance */
4522 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4525 struct listnode
*node
;
4526 /* length(1) + message(N) */
4527 uint8_t data
[BGP_ADMIN_SHUTDOWN_MSG_LEN
+ 1];
4529 /* do nothing if already shut down */
4530 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4533 /* informational log message */
4534 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4537 /* iterate through peers of BGP instance */
4538 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4539 /* continue, if peer is already in administrative shutdown. */
4540 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4543 /* send a RFC 4486 notification message if necessary */
4544 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4546 size_t datalen
= strlen(msg
);
4548 if (datalen
> BGP_ADMIN_SHUTDOWN_MSG_LEN
)
4549 datalen
= BGP_ADMIN_SHUTDOWN_MSG_LEN
;
4552 memcpy(data
+ 1, msg
, datalen
);
4554 bgp_notify_send_with_data(
4555 peer
, BGP_NOTIFY_CEASE
,
4556 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
, data
,
4560 peer
, BGP_NOTIFY_CEASE
,
4561 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4565 /* reset start timer to initial value */
4566 peer
->v_start
= BGP_INIT_START_TIMER
;
4568 /* trigger a RFC 4271 ManualStop event */
4569 BGP_EVENT_ADD(peer
, BGP_Stop
);
4572 /* set the BGP instances shutdown flag */
4573 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4576 /* Disable global administrative shutdown of all peers of BGP instance */
4577 void bgp_shutdown_disable(struct bgp
*bgp
)
4579 /* do nothing if not shut down. */
4580 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4583 /* informational log message */
4584 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4587 /* clear the BGP instances shutdown flag */
4588 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4591 /* Change specified peer flag. */
4592 static int peer_flag_modify(struct peer
*peer
, uint64_t flag
, int set
)
4596 bool invert
, member_invert
;
4597 struct peer
*member
;
4598 struct listnode
*node
, *nnode
;
4599 struct peer_flag_action action
;
4601 memset(&action
, 0, sizeof(struct peer_flag_action
));
4602 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4604 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4605 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4608 /* Abort if no flag action exists. */
4610 return BGP_ERR_INVALID_FLAG
;
4612 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4613 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4614 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4615 return BGP_ERR_PEER_FLAG_CONFLICT
;
4617 /* Handle flag updates where desired state matches current state. */
4618 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4619 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4620 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4624 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4625 COND_FLAG(peer
->flags_override
, flag
, invert
);
4630 /* Inherit from peer-group or set/unset flags accordingly. */
4631 if (peer_group_active(peer
) && set
== invert
)
4632 peer_flag_inherit(peer
, flag
);
4634 COND_FLAG(peer
->flags
, flag
, set
);
4636 /* Check if handling a regular peer. */
4637 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4638 /* Update flag override state accordingly. */
4639 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4642 * For the extended next-hop encoding flag we need to turn RAs
4643 * on if flag is being set, but only turn RAs off if the flag
4644 * is being unset on this peer and if this peer is a member of a
4645 * peer-group, the peer-group also doesn't have the flag set.
4647 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4649 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4650 } else if (peer_group_active(peer
)) {
4651 if (!CHECK_FLAG(peer
->group
->conf
->flags
,
4654 bgp_zebra_terminate_radv(peer
->bgp
,
4657 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4660 /* Execute flag action on peer. */
4661 if (action
.type
== peer_change_reset
)
4662 peer_flag_modify_action(peer
, flag
);
4664 /* Skip peer-group mechanics for regular peers. */
4669 * Update peer-group members, unless they are explicitly overriding
4670 * peer-group configuration.
4672 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4673 /* Skip peers with overridden configuration. */
4674 if (CHECK_FLAG(member
->flags_override
, flag
))
4677 /* Check if only member without group is inverted. */
4679 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4681 /* Skip peers with equivalent configuration. */
4682 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4685 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4688 /* Update flag on peer-group member. */
4689 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4691 if (flag
== PEER_FLAG_CAPABILITY_ENHE
&& !member
->conf_if
)
4692 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4693 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4695 /* Execute flag action on peer-group member. */
4696 if (action
.type
== peer_change_reset
)
4697 peer_flag_modify_action(member
, flag
);
4703 int peer_flag_set(struct peer
*peer
, uint64_t flag
)
4705 return peer_flag_modify(peer
, flag
, 1);
4708 int peer_flag_unset(struct peer
*peer
, uint64_t flag
)
4710 return peer_flag_modify(peer
, flag
, 0);
4713 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4714 uint64_t flag
, bool set
)
4718 bool invert
, member_invert
;
4719 struct peer
*member
;
4720 struct listnode
*node
, *nnode
;
4721 struct peer_flag_action action
;
4722 enum bgp_peer_sort ptype
;
4724 memset(&action
, 0, sizeof(struct peer_flag_action
));
4725 size
= sizeof(peer_af_flag_action_list
)
4726 / sizeof(struct peer_flag_action
);
4728 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4729 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4732 /* Abort if flag action exists. */
4734 return BGP_ERR_INVALID_FLAG
;
4736 ptype
= peer_sort(peer
);
4737 /* Special check for reflector client. */
4738 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4739 return BGP_ERR_NOT_INTERNAL_PEER
;
4741 /* Do not remove reflector client when ORR is configured on this peer */
4742 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& !set
&&
4743 peer_orr_rrclient_check(peer
, afi
, safi
))
4744 return BGP_ERR_PEER_ORR_CONFIGURED
;
4746 /* Special check for remove-private-AS. */
4747 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4748 return BGP_ERR_REMOVE_PRIVATE_AS
;
4750 /* as-override is not allowed for IBGP peers */
4751 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4752 return BGP_ERR_AS_OVERRIDE
;
4754 /* Handle flag updates where desired state matches current state. */
4755 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4756 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4757 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4762 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4763 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4770 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4771 * if we are setting/unsetting flags which conflict with this flag
4772 * handle accordingly
4774 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4778 * if we are setting NEXTHOP_SELF, we need to unset the
4779 * NEXTHOP_UNCHANGED flag
4781 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4782 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4783 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4784 PEER_FLAG_NEXTHOP_UNCHANGED
);
4788 * if we are unsetting NEXTHOP_SELF, we need to set the
4789 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4791 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4792 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4793 SET_FLAG(peer
->af_flags
[afi
][safi
],
4794 PEER_FLAG_NEXTHOP_UNCHANGED
);
4799 * If the peer is a route server client let's not
4800 * muck with the nexthop on the way out the door
4802 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4804 SET_FLAG(peer
->af_flags
[afi
][safi
],
4805 PEER_FLAG_NEXTHOP_UNCHANGED
);
4807 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4808 PEER_FLAG_NEXTHOP_UNCHANGED
);
4811 /* Inherit from peer-group or set/unset flags accordingly. */
4812 if (peer_group_active(peer
) && set
== invert
)
4813 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4815 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4817 /* Execute action when peer is established. */
4818 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4819 && peer_established(peer
)) {
4820 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4821 bgp_clear_adj_in(peer
, afi
, safi
);
4823 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4824 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4825 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4826 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4827 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4828 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4829 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4830 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4832 peer_change_action(peer
, afi
, safi
, action
.type
);
4836 /* Check if handling a regular peer. */
4837 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4838 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4842 * Update peer-group members, unless they are explicitly
4843 * overriding peer-group configuration.
4845 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4847 /* Skip peers with overridden configuration. */
4848 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4852 /* Check if only member without group is inverted. */
4854 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4858 /* Skip peers with equivalent configuration. */
4859 if (set
!= member_invert
4860 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4863 if (set
== member_invert
4864 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4867 /* Update flag on peer-group member. */
4868 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4869 set
!= member_invert
);
4871 /* Execute flag action on peer-group member. */
4872 if (peer_established(member
)) {
4873 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4874 bgp_clear_adj_in(member
, afi
, safi
);
4876 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4877 member
->last_reset
=
4878 PEER_DOWN_RR_CLIENT_CHANGE
;
4880 == PEER_FLAG_RSERVER_CLIENT
)
4881 member
->last_reset
=
4882 PEER_DOWN_RS_CLIENT_CHANGE
;
4884 == PEER_FLAG_ORF_PREFIX_SM
)
4885 member
->last_reset
=
4886 PEER_DOWN_CAPABILITY_CHANGE
;
4888 == PEER_FLAG_ORF_PREFIX_RM
)
4889 member
->last_reset
=
4890 PEER_DOWN_CAPABILITY_CHANGE
;
4892 peer_change_action(member
, afi
, safi
,
4902 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4904 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4907 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint64_t flag
)
4909 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4913 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4915 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4916 peer
->tx_shutdown_message
=
4917 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4920 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4922 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4926 /* EBGP multihop configuration. */
4927 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4929 struct peer_group
*group
;
4930 struct listnode
*node
, *nnode
;
4933 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4936 /* is there anything to do? */
4937 if (peer
->ttl
== ttl
)
4940 /* see comment in peer_ttl_security_hops_set() */
4941 if (ttl
!= MAXTTL
) {
4942 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4943 group
= peer
->group
;
4944 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4945 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4947 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4949 if (peer1
->sort
== BGP_PEER_IBGP
)
4952 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4953 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4956 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4957 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4963 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4964 if (peer
->sort
!= BGP_PEER_IBGP
) {
4965 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4966 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4967 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4969 bgp_session_reset(peer
);
4971 /* Reconfigure BFD peer with new TTL. */
4972 if (peer
->bfd_config
)
4973 bgp_peer_bfd_update_source(peer
);
4976 group
= peer
->group
;
4977 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4978 if (peer
->sort
== BGP_PEER_IBGP
)
4981 peer
->ttl
= group
->conf
->ttl
;
4983 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4984 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4985 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4987 bgp_session_reset(peer
);
4989 /* Reconfigure BFD peer with new TTL. */
4990 if (peer
->bfd_config
)
4991 bgp_peer_bfd_update_source(peer
);
4997 int peer_ebgp_multihop_unset(struct peer
*peer
)
4999 struct peer_group
*group
;
5000 struct listnode
*node
, *nnode
;
5003 if (peer
->sort
== BGP_PEER_IBGP
)
5006 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
5007 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
5009 if (peer_group_active(peer
))
5010 ttl
= peer
->group
->conf
->ttl
;
5012 ttl
= BGP_DEFAULT_TTL
;
5014 if (ttl
== peer
->ttl
)
5019 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5020 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5021 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5022 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5024 bgp_session_reset(peer
);
5026 /* Reconfigure BFD peer with new TTL. */
5027 if (peer
->bfd_config
)
5028 bgp_peer_bfd_update_source(peer
);
5030 group
= peer
->group
;
5031 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5032 if (peer
->sort
== BGP_PEER_IBGP
)
5035 peer
->ttl
= BGP_DEFAULT_TTL
;
5037 if (peer
->fd
>= 0) {
5038 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5040 peer
, BGP_NOTIFY_CEASE
,
5041 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5043 bgp_session_reset(peer
);
5046 /* Reconfigure BFD peer with new TTL. */
5047 if (peer
->bfd_config
)
5048 bgp_peer_bfd_update_source(peer
);
5054 /* Set Open Policy Role and check its correctness */
5055 int peer_role_set(struct peer
*peer
, uint8_t role
, bool strict_mode
)
5057 struct peer
*member
;
5058 struct listnode
*node
, *nnode
;
5060 peer_flag_set(peer
, PEER_FLAG_ROLE
);
5062 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5063 if (peer
->sort
!= BGP_PEER_EBGP
)
5064 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5066 if (peer
->local_role
== role
) {
5067 if (CHECK_FLAG(peer
->flags
,
5068 PEER_FLAG_ROLE_STRICT_MODE
) &&
5070 /* TODO: Is session restart needed if it was
5073 UNSET_FLAG(peer
->flags
,
5074 PEER_FLAG_ROLE_STRICT_MODE
);
5075 if (!CHECK_FLAG(peer
->flags
,
5076 PEER_FLAG_ROLE_STRICT_MODE
) &&
5078 SET_FLAG(peer
->flags
,
5079 PEER_FLAG_ROLE_STRICT_MODE
);
5080 /* Restart session to throw Role Mismatch
5083 if (peer
->remote_role
== ROLE_UNDEFINED
)
5084 bgp_session_reset(peer
);
5087 peer
->local_role
= role
;
5089 SET_FLAG(peer
->flags
,
5090 PEER_FLAG_ROLE_STRICT_MODE
);
5092 UNSET_FLAG(peer
->flags
,
5093 PEER_FLAG_ROLE_STRICT_MODE
);
5094 bgp_session_reset(peer
);
5100 peer
->local_role
= role
;
5101 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5102 if (member
->sort
!= BGP_PEER_EBGP
)
5103 return BGP_ERR_INVALID_INTERNAL_ROLE
;
5105 if (member
->local_role
== role
) {
5106 if (CHECK_FLAG(member
->flags
,
5107 PEER_FLAG_ROLE_STRICT_MODE
) &&
5109 /* TODO: Is session restart needed if it was
5112 UNSET_FLAG(member
->flags
,
5113 PEER_FLAG_ROLE_STRICT_MODE
);
5114 if (!CHECK_FLAG(member
->flags
,
5115 PEER_FLAG_ROLE_STRICT_MODE
) &&
5117 SET_FLAG(peer
->flags
,
5118 PEER_FLAG_ROLE_STRICT_MODE
);
5119 SET_FLAG(member
->flags
,
5120 PEER_FLAG_ROLE_STRICT_MODE
);
5121 /* Restart session to throw Role Mismatch
5124 if (member
->remote_role
== ROLE_UNDEFINED
)
5125 bgp_session_reset(member
);
5128 member
->local_role
= role
;
5131 SET_FLAG(peer
->flags
,
5132 PEER_FLAG_ROLE_STRICT_MODE
);
5133 SET_FLAG(member
->flags
,
5134 PEER_FLAG_ROLE_STRICT_MODE
);
5136 UNSET_FLAG(member
->flags
,
5137 PEER_FLAG_ROLE_STRICT_MODE
);
5139 bgp_session_reset(member
);
5146 int peer_role_unset(struct peer
*peer
)
5148 struct peer
*member
;
5149 struct listnode
*node
, *nnode
;
5151 peer_flag_unset(peer
, PEER_FLAG_ROLE
);
5153 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5154 return peer_role_set(peer
, ROLE_UNDEFINED
, 0);
5156 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
))
5157 peer_role_set(member
, ROLE_UNDEFINED
, 0);
5162 /* Neighbor description. */
5163 void peer_description_set(struct peer
*peer
, const char *desc
)
5165 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5167 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
5170 void peer_description_unset(struct peer
*peer
)
5172 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
5175 /* Neighbor update-source. */
5176 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
5178 struct peer
*member
;
5179 struct listnode
*node
, *nnode
;
5181 /* Set flag and configuration on peer. */
5182 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5183 if (peer
->update_if
) {
5184 if (strcmp(peer
->update_if
, ifname
) == 0)
5186 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5188 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5189 sockunion_free(peer
->update_source
);
5190 peer
->update_source
= NULL
;
5192 /* Check if handling a regular peer. */
5193 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5194 /* Send notification or reset peer depending on state. */
5195 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5196 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5197 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5198 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5200 bgp_session_reset(peer
);
5202 /* Apply new source configuration to BFD session. */
5203 if (peer
->bfd_config
)
5204 bgp_peer_bfd_update_source(peer
);
5206 /* Skip peer-group mechanics for regular peers. */
5211 * Set flag and configuration on all peer-group members, unless they are
5212 * explicitly overriding peer-group configuration.
5214 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5215 /* Skip peers with overridden configuration. */
5216 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5219 /* Skip peers with the same configuration. */
5220 if (member
->update_if
) {
5221 if (strcmp(member
->update_if
, ifname
) == 0)
5223 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5226 /* Set flag and configuration on peer-group member. */
5227 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5228 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
5229 sockunion_free(member
->update_source
);
5230 member
->update_source
= NULL
;
5232 /* Send notification or reset peer depending on state. */
5233 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5234 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5235 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5236 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5238 bgp_session_reset(member
);
5240 /* Apply new source configuration to BFD session. */
5241 if (member
->bfd_config
)
5242 bgp_peer_bfd_update_source(member
);
5248 void peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
5250 struct peer
*member
;
5251 struct listnode
*node
, *nnode
;
5253 /* Set flag and configuration on peer. */
5254 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
5255 if (peer
->update_source
) {
5256 if (sockunion_cmp(peer
->update_source
, su
) == 0)
5258 sockunion_free(peer
->update_source
);
5260 peer
->update_source
= sockunion_dup(su
);
5261 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5263 /* Check if handling a regular peer. */
5264 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5265 /* Send notification or reset peer depending on state. */
5266 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5267 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5268 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5269 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5271 bgp_session_reset(peer
);
5273 /* Apply new source configuration to BFD session. */
5274 if (peer
->bfd_config
)
5275 bgp_peer_bfd_update_source(peer
);
5277 /* Skip peer-group mechanics for regular peers. */
5282 * Set flag and configuration on all peer-group members, unless they are
5283 * explicitly overriding peer-group configuration.
5285 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5286 /* Skip peers with overridden configuration. */
5287 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5290 /* Skip peers with the same configuration. */
5291 if (member
->update_source
) {
5292 if (sockunion_cmp(member
->update_source
, su
) == 0)
5294 sockunion_free(member
->update_source
);
5297 /* Set flag and configuration on peer-group member. */
5298 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5299 member
->update_source
= sockunion_dup(su
);
5300 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5302 /* Send notification or reset peer depending on state. */
5303 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5304 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5305 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5306 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5308 bgp_session_reset(member
);
5310 /* Apply new source configuration to BFD session. */
5311 if (member
->bfd_config
)
5312 bgp_peer_bfd_update_source(member
);
5316 void peer_update_source_unset(struct peer
*peer
)
5318 struct peer
*member
;
5319 struct listnode
*node
, *nnode
;
5321 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
5324 /* Inherit configuration from peer-group if peer is member. */
5325 if (peer_group_active(peer
)) {
5326 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
5327 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
5328 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
5329 MTYPE_PEER_UPDATE_SOURCE
);
5331 /* Otherwise remove flag and configuration from peer. */
5332 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
5333 sockunion_free(peer
->update_source
);
5334 peer
->update_source
= NULL
;
5335 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
5338 /* Check if handling a regular peer. */
5339 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5340 /* Send notification or reset peer depending on state. */
5341 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5342 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5343 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5344 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5346 bgp_session_reset(peer
);
5348 /* Apply new source configuration to BFD session. */
5349 if (peer
->bfd_config
)
5350 bgp_peer_bfd_update_source(peer
);
5352 /* Skip peer-group mechanics for regular peers. */
5357 * Set flag and configuration on all peer-group members, unless they are
5358 * explicitly overriding peer-group configuration.
5360 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5361 /* Skip peers with overridden configuration. */
5362 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5365 /* Skip peers with the same configuration. */
5366 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5367 && !member
->update_source
&& !member
->update_if
)
5370 /* Remove flag and configuration on peer-group member. */
5371 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5372 sockunion_free(member
->update_source
);
5373 member
->update_source
= NULL
;
5374 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5376 /* Send notification or reset peer depending on state. */
5377 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5378 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5379 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5380 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5382 bgp_session_reset(member
);
5384 /* Apply new source configuration to BFD session. */
5385 if (member
->bfd_config
)
5386 bgp_peer_bfd_update_source(member
);
5390 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5391 const char *rmap
, struct route_map
*route_map
)
5393 struct peer
*member
;
5394 struct listnode
*node
, *nnode
;
5395 struct update_subgroup
*subgrp
;
5397 /* Set flag and configuration on peer. */
5398 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5400 subgrp
= peer_subgroup(peer
, afi
, safi
);
5403 if (!peer
->default_rmap
[afi
][safi
].name
5404 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5405 if (peer
->default_rmap
[afi
][safi
].name
)
5406 XFREE(MTYPE_ROUTE_MAP_NAME
,
5407 peer
->default_rmap
[afi
][safi
].name
);
5410 * When there is a change in route-map policy,
5411 * this flow gets triggered. Since, the default
5412 * route is already originated, the flag is set.
5413 * The flag should be unset here,
5414 * to trigger the flow of sending update message.
5417 UNSET_FLAG(subgrp
->sflags
,
5418 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5420 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5421 peer
->default_rmap
[afi
][safi
].name
=
5422 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5423 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5424 route_map_counter_increment(route_map
);
5427 if (peer
->default_rmap
[afi
][safi
].name
)
5428 XFREE(MTYPE_ROUTE_MAP_NAME
,
5429 peer
->default_rmap
[afi
][safi
].name
);
5432 * This is triggered in case of route-map deletion.
5433 * The flag needs to be unset, to trigger the flow
5434 * of sending an update message.
5437 UNSET_FLAG(subgrp
->sflags
,
5438 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
5440 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5441 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5442 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5445 /* Check if handling a regular peer. */
5446 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5447 /* Update peer route announcements. */
5448 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5449 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5450 bgp_default_originate(peer
, afi
, safi
, 0);
5451 bgp_announce_route(peer
, afi
, safi
, false);
5454 /* Skip peer-group mechanics for regular peers. */
5459 * Set flag and configuration on all peer-group members, unless they are
5460 * explicitly overriding peer-group configuration.
5462 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5463 /* Skip peers with overridden configuration. */
5464 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5465 PEER_FLAG_DEFAULT_ORIGINATE
))
5468 /* Set flag and configuration on peer-group member. */
5469 SET_FLAG(member
->af_flags
[afi
][safi
],
5470 PEER_FLAG_DEFAULT_ORIGINATE
);
5472 if (member
->default_rmap
[afi
][safi
].name
)
5473 XFREE(MTYPE_ROUTE_MAP_NAME
,
5474 member
->default_rmap
[afi
][safi
].name
);
5475 route_map_counter_decrement(
5476 member
->default_rmap
[afi
][safi
].map
);
5477 member
->default_rmap
[afi
][safi
].name
=
5478 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5479 member
->default_rmap
[afi
][safi
].map
= route_map
;
5480 route_map_counter_increment(route_map
);
5483 /* Update peer route announcements. */
5484 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5485 update_group_adjust_peer(
5486 peer_af_find(member
, afi
, safi
));
5487 bgp_default_originate(member
, afi
, safi
, 0);
5488 bgp_announce_route(member
, afi
, safi
, false);
5495 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5497 struct peer
*member
;
5498 struct listnode
*node
, *nnode
;
5500 /* Inherit configuration from peer-group if peer is member. */
5501 if (peer_group_active(peer
)) {
5502 peer_af_flag_inherit(peer
, afi
, safi
,
5503 PEER_FLAG_DEFAULT_ORIGINATE
);
5504 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5505 default_rmap
[afi
][safi
].name
,
5506 MTYPE_ROUTE_MAP_NAME
);
5507 PEER_ATTR_INHERIT(peer
, peer
->group
,
5508 default_rmap
[afi
][safi
].map
);
5510 /* Otherwise remove flag and configuration from peer. */
5511 peer_af_flag_unset(peer
, afi
, safi
,
5512 PEER_FLAG_DEFAULT_ORIGINATE
);
5513 if (peer
->default_rmap
[afi
][safi
].name
)
5514 XFREE(MTYPE_ROUTE_MAP_NAME
,
5515 peer
->default_rmap
[afi
][safi
].name
);
5516 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5517 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5518 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5521 /* Check if handling a regular peer. */
5522 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5523 /* Update peer route announcements. */
5524 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5525 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5526 bgp_default_originate(peer
, afi
, safi
, 1);
5527 bgp_announce_route(peer
, afi
, safi
, false);
5530 /* Skip peer-group mechanics for regular peers. */
5535 * Remove flag and configuration from all peer-group members, unless
5536 * they are explicitly overriding peer-group configuration.
5538 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5539 /* Skip peers with overridden configuration. */
5540 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5541 PEER_FLAG_DEFAULT_ORIGINATE
))
5544 /* Remove flag and configuration on peer-group member. */
5545 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5546 PEER_FLAG_DEFAULT_ORIGINATE
);
5547 if (member
->default_rmap
[afi
][safi
].name
)
5548 XFREE(MTYPE_ROUTE_MAP_NAME
,
5549 member
->default_rmap
[afi
][safi
].name
);
5550 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5551 member
->default_rmap
[afi
][safi
].name
= NULL
;
5552 member
->default_rmap
[afi
][safi
].map
= NULL
;
5554 /* Update peer route announcements. */
5555 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5556 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5557 bgp_default_originate(member
, afi
, safi
, 1);
5558 bgp_announce_route(member
, afi
, safi
, false);
5565 void peer_port_set(struct peer
*peer
, uint16_t port
)
5568 peer_flag_set(peer
, PEER_FLAG_PORT
);
5571 void peer_port_unset(struct peer
*peer
)
5573 peer
->port
= BGP_PORT_DEFAULT
;
5574 peer_flag_unset(peer
, PEER_FLAG_PORT
);
5577 /* Set the TCP-MSS value in the peer structure,
5578 * This gets applied only after connection reset
5579 * So this value will be used in bgp_connect.
5581 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5583 peer
->tcp_mss
= tcp_mss
;
5584 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5587 /* Reset the TCP-MSS value in the peer structure,
5588 * This gets applied only after connection reset
5589 * So this value will be used in bgp_connect.
5591 void peer_tcp_mss_unset(struct peer
*peer
)
5593 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5598 * Helper function that is called after the name of the policy
5599 * being used by a peer has changed (AF specific). Automatically
5600 * initiates inbound or outbound processing as needed.
5602 void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5606 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5607 if (peer_established(peer
))
5608 bgp_announce_route(peer
, afi
, safi
, false);
5610 if (!peer_established(peer
))
5613 if (bgp_soft_reconfig_in(peer
, afi
, safi
))
5616 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
) ||
5617 CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
)) {
5618 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5619 PEER_CAP_ORF_PREFIX_SM_ADV
) &&
5620 (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5621 PEER_CAP_ORF_PREFIX_RM_RCV
) ||
5622 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
5623 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
)))
5624 peer_clear_soft(peer
, afi
, safi
,
5625 BGP_CLEAR_SOFT_IN_ORF_PREFIX
);
5627 bgp_route_refresh_send(
5628 peer
, afi
, safi
, 0, 0, 0,
5629 BGP_ROUTE_REFRESH_NORMAL
);
5635 /* neighbor weight. */
5636 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5638 struct peer
*member
;
5639 struct listnode
*node
, *nnode
;
5641 /* Set flag and configuration on peer. */
5642 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5643 if (peer
->weight
[afi
][safi
] != weight
) {
5644 peer
->weight
[afi
][safi
] = weight
;
5645 peer_on_policy_change(peer
, afi
, safi
, 0);
5648 /* Skip peer-group mechanics for regular peers. */
5649 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5653 * Set flag and configuration on all peer-group members, unless they are
5654 * explicitly overriding peer-group configuration.
5656 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5657 /* Skip peers with overridden configuration. */
5658 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5662 /* Set flag and configuration on peer-group member. */
5663 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5664 if (member
->weight
[afi
][safi
] != weight
) {
5665 member
->weight
[afi
][safi
] = weight
;
5666 peer_on_policy_change(member
, afi
, safi
, 0);
5673 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5675 struct peer
*member
;
5676 struct listnode
*node
, *nnode
;
5678 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5681 /* Inherit configuration from peer-group if peer is member. */
5682 if (peer_group_active(peer
)) {
5683 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5684 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5686 peer_on_policy_change(peer
, afi
, safi
, 0);
5690 /* Remove flag and configuration from peer. */
5691 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5692 peer
->weight
[afi
][safi
] = 0;
5693 peer_on_policy_change(peer
, afi
, safi
, 0);
5695 /* Skip peer-group mechanics for regular peers. */
5696 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5700 * Remove flag and configuration from all peer-group members, unless
5701 * they are explicitly overriding peer-group configuration.
5703 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5704 /* Skip peers with overridden configuration. */
5705 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5709 /* Skip peers where flag is already disabled. */
5710 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5713 /* Remove flag and configuration on peer-group member. */
5714 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5715 member
->weight
[afi
][safi
] = 0;
5716 peer_on_policy_change(member
, afi
, safi
, 0);
5722 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5724 struct peer
*member
;
5725 struct listnode
*node
, *nnode
;
5727 if (keepalive
> UINT16_MAX
)
5728 return BGP_ERR_INVALID_VALUE
;
5730 if (holdtime
> UINT16_MAX
)
5731 return BGP_ERR_INVALID_VALUE
;
5733 if (holdtime
< 3 && holdtime
!= 0)
5734 return BGP_ERR_INVALID_VALUE
;
5736 /* Set flag and configuration on peer. */
5737 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5738 peer
->holdtime
= holdtime
;
5739 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5741 /* Skip peer-group mechanics for regular peers. */
5742 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5746 * Set flag and configuration on all peer-group members, unless they are
5747 * explicitly overriding peer-group configuration.
5749 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5750 /* Skip peers with overridden configuration. */
5751 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5754 /* Set flag and configuration on peer-group member. */
5755 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5756 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5757 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5763 int peer_timers_unset(struct peer
*peer
)
5765 struct peer
*member
;
5766 struct listnode
*node
, *nnode
;
5768 /* Inherit configuration from peer-group if peer is member. */
5769 if (peer_group_active(peer
)) {
5770 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5771 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5772 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5774 /* Otherwise remove flag and configuration from peer. */
5775 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5777 peer
->keepalive
= 0;
5780 /* Skip peer-group mechanics for regular peers. */
5781 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5785 * Remove flag and configuration from all peer-group members, unless
5786 * they are explicitly overriding peer-group configuration.
5788 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5789 /* Skip peers with overridden configuration. */
5790 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5793 /* Remove flag and configuration on peer-group member. */
5794 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5795 member
->holdtime
= 0;
5796 member
->keepalive
= 0;
5802 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5804 struct peer
*member
;
5805 struct listnode
*node
, *nnode
;
5807 if (connect
> UINT16_MAX
)
5808 return BGP_ERR_INVALID_VALUE
;
5810 /* Set flag and configuration on peer. */
5811 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5812 peer
->connect
= connect
;
5813 peer
->v_connect
= connect
;
5815 /* Skip peer-group mechanics for regular peers. */
5816 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5817 if (!peer_established(peer
)) {
5818 if (peer_active(peer
))
5819 BGP_EVENT_ADD(peer
, BGP_Stop
);
5820 BGP_EVENT_ADD(peer
, BGP_Start
);
5825 * Set flag and configuration on all peer-group members, unless they are
5826 * explicitly overriding peer-group configuration.
5828 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5829 /* Skip peers with overridden configuration. */
5830 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5833 /* Set flag and configuration on peer-group member. */
5834 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5835 member
->connect
= connect
;
5836 member
->v_connect
= connect
;
5838 if (!peer_established(member
)) {
5839 if (peer_active(member
))
5840 BGP_EVENT_ADD(member
, BGP_Stop
);
5841 BGP_EVENT_ADD(member
, BGP_Start
);
5848 int peer_timers_connect_unset(struct peer
*peer
)
5850 struct peer
*member
;
5851 struct listnode
*node
, *nnode
;
5853 /* Inherit configuration from peer-group if peer is member. */
5854 if (peer_group_active(peer
)) {
5855 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5856 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5858 /* Otherwise remove flag and configuration from peer. */
5859 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5863 /* Set timer with fallback to default value. */
5865 peer
->v_connect
= peer
->connect
;
5867 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5869 /* Skip peer-group mechanics for regular peers. */
5870 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5871 if (!peer_established(peer
)) {
5872 if (peer_active(peer
))
5873 BGP_EVENT_ADD(peer
, BGP_Stop
);
5874 BGP_EVENT_ADD(peer
, BGP_Start
);
5879 * Remove flag and configuration from all peer-group members, unless
5880 * they are explicitly overriding peer-group configuration.
5882 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5883 /* Skip peers with overridden configuration. */
5884 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5887 /* Remove flag and configuration on peer-group member. */
5888 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5889 member
->connect
= 0;
5890 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5892 if (!peer_established(member
)) {
5893 if (peer_active(member
))
5894 BGP_EVENT_ADD(member
, BGP_Stop
);
5895 BGP_EVENT_ADD(member
, BGP_Start
);
5902 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5904 struct peer
*member
;
5905 struct listnode
*node
, *nnode
;
5908 return BGP_ERR_INVALID_VALUE
;
5910 /* Set flag and configuration on peer. */
5911 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5912 peer
->routeadv
= routeadv
;
5913 peer
->v_routeadv
= routeadv
;
5915 /* Check if handling a regular peer. */
5916 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5917 /* Update peer route announcements. */
5918 update_group_adjust_peer_afs(peer
);
5919 if (peer_established(peer
))
5920 bgp_announce_route_all(peer
);
5922 /* Skip peer-group mechanics for regular peers. */
5927 * Set flag and configuration on all peer-group members, unless they are
5928 * explicitly overriding peer-group configuration.
5930 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5931 /* Skip peers with overridden configuration. */
5932 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5935 /* Set flag and configuration on peer-group member. */
5936 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5937 member
->routeadv
= routeadv
;
5938 member
->v_routeadv
= routeadv
;
5940 /* Update peer route announcements. */
5941 update_group_adjust_peer_afs(member
);
5942 if (peer_established(member
))
5943 bgp_announce_route_all(member
);
5949 int peer_advertise_interval_unset(struct peer
*peer
)
5951 struct peer
*member
;
5952 struct listnode
*node
, *nnode
;
5954 /* Inherit configuration from peer-group if peer is member. */
5955 if (peer_group_active(peer
)) {
5956 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5957 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5959 /* Otherwise remove flag and configuration from peer. */
5960 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5964 /* Set timer with fallback to default value. */
5966 peer
->v_routeadv
= peer
->routeadv
;
5968 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5969 ? BGP_DEFAULT_IBGP_ROUTEADV
5970 : BGP_DEFAULT_EBGP_ROUTEADV
;
5972 /* Check if handling a regular peer. */
5973 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5974 /* Update peer route announcements. */
5975 update_group_adjust_peer_afs(peer
);
5976 if (peer_established(peer
))
5977 bgp_announce_route_all(peer
);
5979 /* Skip peer-group mechanics for regular peers. */
5984 * Remove flag and configuration from all peer-group members, unless
5985 * they are explicitly overriding peer-group configuration.
5987 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5988 /* Skip peers with overridden configuration. */
5989 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5992 /* Remove flag and configuration on peer-group member. */
5993 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5994 member
->routeadv
= 0;
5995 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
5996 ? BGP_DEFAULT_IBGP_ROUTEADV
5997 : BGP_DEFAULT_EBGP_ROUTEADV
;
5999 /* Update peer route announcements. */
6000 update_group_adjust_peer_afs(member
);
6001 if (peer_established(member
))
6002 bgp_announce_route_all(member
);
6008 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
6011 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
6013 struct peer
*member
;
6014 struct listnode
*node
;
6016 /* Set peers session attribute flag and timer interval. */
6017 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6018 peer
->delayopen
= delayopen
;
6019 peer
->v_delayopen
= delayopen
;
6021 /* Skip group mechanics for regular peers. */
6022 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6025 /* Set flag and configuration on all peer-group members, unless they are
6026 * explicitly overriding peer-group configuration.
6028 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6029 /* Skip peers with overridden configuration. */
6030 if (CHECK_FLAG(member
->flags_override
,
6031 PEER_FLAG_TIMER_DELAYOPEN
))
6034 /* Set session attribute flag and timer intervals on peer-group
6037 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6038 member
->delayopen
= delayopen
;
6039 member
->v_delayopen
= delayopen
;
6045 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
6046 * DelayOpenTimer interval to the default value.
6048 int peer_timers_delayopen_unset(struct peer
*peer
)
6050 struct peer
*member
;
6051 struct listnode
*node
;
6053 /* Inherit configuration from peer-group if peer is member. */
6054 if (peer_group_active(peer
)) {
6055 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6056 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
6058 /* Otherwise remove session attribute flag and set timer
6059 * interval to default value.
6061 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
6062 peer
->delayopen
= peer
->bgp
->default_delayopen
;
6065 /* Set timer value to zero */
6066 peer
->v_delayopen
= 0;
6068 /* Skip peer-group mechanics for regular peers. */
6069 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6072 /* Remove flag and configuration from all peer-group members, unless
6073 * they are explicitly overriding peer-group configuration.
6075 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
6076 /* Skip peers with overridden configuration. */
6077 if (CHECK_FLAG(member
->flags_override
,
6078 PEER_FLAG_TIMER_DELAYOPEN
))
6081 /* Remove session attribute flag, reset the timer interval to
6082 * the default value and set the timer value to zero.
6084 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
6085 member
->delayopen
= peer
->bgp
->default_delayopen
;
6086 member
->v_delayopen
= 0;
6092 /* neighbor interface */
6093 void peer_interface_set(struct peer
*peer
, const char *str
)
6095 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6096 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
6099 void peer_interface_unset(struct peer
*peer
)
6101 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
6105 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6106 int allow_num
, int origin
)
6108 struct peer
*member
;
6109 struct listnode
*node
, *nnode
;
6111 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
6112 return BGP_ERR_INVALID_VALUE
;
6114 /* Set flag and configuration on peer. */
6115 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6117 if (peer
->allowas_in
[afi
][safi
] != 0
6118 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6119 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6120 peer_af_flag_set(peer
, afi
, safi
,
6121 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6122 peer
->allowas_in
[afi
][safi
] = 0;
6123 peer_on_policy_change(peer
, afi
, safi
, 0);
6126 if (peer
->allowas_in
[afi
][safi
] != allow_num
6127 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6128 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6130 peer_af_flag_unset(peer
, afi
, safi
,
6131 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6132 peer
->allowas_in
[afi
][safi
] = allow_num
;
6133 peer_on_policy_change(peer
, afi
, safi
, 0);
6137 /* Skip peer-group mechanics for regular peers. */
6138 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6142 * Set flag and configuration on all peer-group members, unless
6143 * they are explicitly overriding peer-group configuration.
6145 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6146 /* Skip peers with overridden configuration. */
6147 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6148 PEER_FLAG_ALLOWAS_IN
))
6151 /* Set flag and configuration on peer-group member. */
6152 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6154 if (member
->allowas_in
[afi
][safi
] != 0
6155 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
6156 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6157 SET_FLAG(member
->af_flags
[afi
][safi
],
6158 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6159 member
->allowas_in
[afi
][safi
] = 0;
6160 peer_on_policy_change(peer
, afi
, safi
, 0);
6163 if (member
->allowas_in
[afi
][safi
] != allow_num
6164 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
6165 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6166 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6167 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6168 member
->allowas_in
[afi
][safi
] = allow_num
;
6169 peer_on_policy_change(peer
, afi
, safi
, 0);
6177 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6179 struct peer
*member
;
6180 struct listnode
*node
, *nnode
;
6182 /* Skip peer if flag is already disabled. */
6183 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
6186 /* Inherit configuration from peer-group if peer is member. */
6187 if (peer_group_active(peer
)) {
6188 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6189 peer_af_flag_inherit(peer
, afi
, safi
,
6190 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6191 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
6192 peer_on_policy_change(peer
, afi
, safi
, 0);
6197 /* Remove flag and configuration from peer. */
6198 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
6199 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6200 peer
->allowas_in
[afi
][safi
] = 0;
6201 peer_on_policy_change(peer
, afi
, safi
, 0);
6203 /* Skip peer-group mechanics if handling a regular peer. */
6204 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6208 * Remove flags and configuration from all peer-group members, unless
6209 * they are explicitly overriding peer-group configuration.
6211 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6212 /* Skip peers with overridden configuration. */
6213 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
6214 PEER_FLAG_ALLOWAS_IN
))
6217 /* Remove flags and configuration on peer-group member. */
6218 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
6219 UNSET_FLAG(member
->af_flags
[afi
][safi
],
6220 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
6221 member
->allowas_in
[afi
][safi
] = 0;
6222 peer_on_policy_change(member
, afi
, safi
, 0);
6228 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
6231 bool old_no_prepend
, old_replace_as
;
6232 struct bgp
*bgp
= peer
->bgp
;
6233 struct peer
*member
;
6234 struct listnode
*node
, *nnode
;
6237 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
6239 /* Save previous flag states. */
6241 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6243 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6245 /* Set flag and configuration on peer. */
6246 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
6247 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
6248 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
6250 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
6251 && old_replace_as
== replace_as
)
6253 peer
->change_local_as
= as
;
6254 (void)peer_sort(peer
);
6256 /* Check if handling a regular peer. */
6257 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
6261 * Set flag and configuration on all peer-group members, unless they are
6262 * explicitly overriding peer-group configuration.
6264 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6265 /* Skip peers with overridden configuration. */
6266 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6269 /* Skip peers with the same configuration. */
6270 old_no_prepend
= CHECK_FLAG(member
->flags
,
6271 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6272 old_replace_as
= CHECK_FLAG(member
->flags
,
6273 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6274 if (member
->change_local_as
== as
6275 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
6276 && old_no_prepend
== no_prepend
6277 && old_replace_as
== replace_as
)
6280 /* Set flag and configuration on peer-group member. */
6281 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6282 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
6284 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
6286 member
->change_local_as
= as
;
6292 int peer_local_as_unset(struct peer
*peer
)
6294 struct peer
*member
;
6295 struct listnode
*node
, *nnode
;
6297 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
6300 /* Inherit configuration from peer-group if peer is member. */
6301 if (peer_group_active(peer
)) {
6302 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
6303 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6304 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6305 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
6307 /* Otherwise remove flag and configuration from peer. */
6308 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
6309 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6310 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6311 peer
->change_local_as
= 0;
6314 /* Check if handling a regular peer. */
6315 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6316 /* Send notification or stop peer depending on state. */
6317 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
6318 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6319 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6320 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6322 BGP_EVENT_ADD(peer
, BGP_Stop
);
6324 /* Skip peer-group mechanics for regular peers. */
6329 * Remove flag and configuration from all peer-group members, unless
6330 * they are explicitly overriding peer-group configuration.
6332 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6333 /* Skip peers with overridden configuration. */
6334 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
6337 /* Remove flag and configuration on peer-group member. */
6338 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
6339 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
6340 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
6341 member
->change_local_as
= 0;
6343 /* Send notification or stop peer depending on state. */
6344 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
6345 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
6346 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6347 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6349 bgp_session_reset(member
);
6355 /* Set password for authenticating with the peer. */
6356 int peer_password_set(struct peer
*peer
, const char *password
)
6358 struct peer
*member
;
6359 struct listnode
*node
, *nnode
;
6360 int len
= password
? strlen(password
) : 0;
6361 int ret
= BGP_SUCCESS
;
6363 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
6364 return BGP_ERR_INVALID_VALUE
;
6366 /* Set flag and configuration on peer. */
6367 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
6368 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
6370 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6371 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6373 /* Check if handling a regular peer. */
6374 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6375 /* Send notification or reset peer depending on state. */
6376 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6377 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6378 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6380 bgp_session_reset(peer
);
6383 * Attempt to install password on socket and skip peer-group
6386 if (BGP_PEER_SU_UNSPEC(peer
))
6388 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6389 : BGP_ERR_TCPSIG_FAILED
;
6393 * Set flag and configuration on all peer-group members, unless they are
6394 * explicitly overriding peer-group configuration.
6396 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6397 /* Skip peers with overridden configuration. */
6398 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6401 /* Skip peers with the same password. */
6402 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6405 /* Set flag and configuration on peer-group member. */
6406 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6407 if (member
->password
)
6408 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6409 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6411 /* Send notification or reset peer depending on state. */
6412 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6413 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6414 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6416 bgp_session_reset(member
);
6418 /* Attempt to install password on socket. */
6419 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6420 ret
= BGP_ERR_TCPSIG_FAILED
;
6423 /* Set flag and configuration on all peer-group listen ranges */
6424 struct listnode
*ln
;
6427 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6428 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6429 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6430 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6435 int peer_password_unset(struct peer
*peer
)
6437 struct peer
*member
;
6438 struct listnode
*node
, *nnode
;
6440 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6443 /* Inherit configuration from peer-group if peer is member. */
6444 if (peer_group_active(peer
)) {
6445 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6446 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6447 MTYPE_PEER_PASSWORD
);
6449 /* Otherwise remove flag and configuration from peer. */
6450 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6451 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6454 /* Check if handling a regular peer. */
6455 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6456 /* Send notification or reset peer depending on state. */
6457 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6458 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6459 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6461 bgp_session_reset(peer
);
6463 /* Attempt to uninstall password on socket. */
6464 if (!BGP_PEER_SU_UNSPEC(peer
))
6465 bgp_md5_unset(peer
);
6466 /* Skip peer-group mechanics for regular peers. */
6471 * Remove flag and configuration from all peer-group members, unless
6472 * they are explicitly overriding peer-group configuration.
6474 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6475 /* Skip peers with overridden configuration. */
6476 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6479 /* Remove flag and configuration on peer-group member. */
6480 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6481 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6483 /* Send notification or reset peer depending on state. */
6484 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6485 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6486 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6488 bgp_session_reset(member
);
6490 /* Attempt to uninstall password on socket. */
6491 if (!BGP_PEER_SU_UNSPEC(member
))
6492 bgp_md5_unset(member
);
6495 /* Set flag and configuration on all peer-group listen ranges */
6496 struct listnode
*ln
;
6499 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6500 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6501 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6502 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6508 /* Set distribute list to the peer. */
6509 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6512 struct peer
*member
;
6513 struct bgp_filter
*filter
;
6514 struct listnode
*node
, *nnode
;
6516 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6517 return BGP_ERR_INVALID_VALUE
;
6519 /* Set configuration on peer. */
6520 filter
= &peer
->filter
[afi
][safi
];
6521 if (filter
->plist
[direct
].name
)
6522 return BGP_ERR_PEER_FILTER_CONFLICT
;
6523 if (filter
->dlist
[direct
].name
)
6524 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6525 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6526 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6528 /* Check if handling a regular peer. */
6529 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6530 /* Set override-flag and process peer route updates. */
6531 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6532 PEER_FT_DISTRIBUTE_LIST
);
6533 peer_on_policy_change(peer
, afi
, safi
,
6534 (direct
== FILTER_OUT
) ? 1 : 0);
6536 /* Skip peer-group mechanics for regular peers. */
6541 * Set configuration on all peer-group members, un less they are
6542 * explicitly overriding peer-group configuration.
6544 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6545 /* Skip peers with overridden configuration. */
6546 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6547 PEER_FT_DISTRIBUTE_LIST
))
6550 /* Set configuration on peer-group member. */
6551 filter
= &member
->filter
[afi
][safi
];
6552 if (filter
->dlist
[direct
].name
)
6553 XFREE(MTYPE_BGP_FILTER_NAME
,
6554 filter
->dlist
[direct
].name
);
6555 filter
->dlist
[direct
].name
=
6556 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6557 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6559 /* Process peer route updates. */
6560 peer_on_policy_change(member
, afi
, safi
,
6561 (direct
== FILTER_OUT
) ? 1 : 0);
6567 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6569 struct peer
*member
;
6570 struct bgp_filter
*filter
;
6571 struct listnode
*node
, *nnode
;
6573 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6574 return BGP_ERR_INVALID_VALUE
;
6576 /* Unset override-flag unconditionally. */
6577 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6578 PEER_FT_DISTRIBUTE_LIST
);
6580 /* Inherit configuration from peer-group if peer is member. */
6581 if (peer_group_active(peer
)) {
6582 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6583 filter
[afi
][safi
].dlist
[direct
].name
,
6584 MTYPE_BGP_FILTER_NAME
);
6585 PEER_ATTR_INHERIT(peer
, peer
->group
,
6586 filter
[afi
][safi
].dlist
[direct
].alist
);
6588 /* Otherwise remove configuration from peer. */
6589 filter
= &peer
->filter
[afi
][safi
];
6590 if (filter
->dlist
[direct
].name
)
6591 XFREE(MTYPE_BGP_FILTER_NAME
,
6592 filter
->dlist
[direct
].name
);
6593 filter
->dlist
[direct
].name
= NULL
;
6594 filter
->dlist
[direct
].alist
= NULL
;
6597 /* Check if handling a regular peer. */
6598 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6599 /* Process peer route updates. */
6600 peer_on_policy_change(peer
, afi
, safi
,
6601 (direct
== FILTER_OUT
) ? 1 : 0);
6603 /* Skip peer-group mechanics for regular peers. */
6608 * Remove configuration on all peer-group members, unless they are
6609 * explicitly overriding peer-group configuration.
6611 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6612 /* Skip peers with overridden configuration. */
6613 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6614 PEER_FT_DISTRIBUTE_LIST
))
6617 /* Remove configuration on peer-group member. */
6618 filter
= &member
->filter
[afi
][safi
];
6619 if (filter
->dlist
[direct
].name
)
6620 XFREE(MTYPE_BGP_FILTER_NAME
,
6621 filter
->dlist
[direct
].name
);
6622 filter
->dlist
[direct
].name
= NULL
;
6623 filter
->dlist
[direct
].alist
= NULL
;
6625 /* Process peer route updates. */
6626 peer_on_policy_change(member
, afi
, safi
,
6627 (direct
== FILTER_OUT
) ? 1 : 0);
6633 /* Update distribute list. */
6634 static void peer_distribute_update(struct access_list
*access
)
6639 struct listnode
*mnode
, *mnnode
;
6640 struct listnode
*node
, *nnode
;
6643 struct peer_group
*group
;
6644 struct bgp_filter
*filter
;
6646 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6648 update_group_policy_update(bgp
,
6649 BGP_POLICY_DISTRIBUTE_LIST
,
6650 access
->name
, true, 0);
6651 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6652 FOREACH_AFI_SAFI (afi
, safi
) {
6653 filter
= &peer
->filter
[afi
][safi
];
6655 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6657 if (filter
->dlist
[direct
].name
)
6658 filter
->dlist
[direct
]
6659 .alist
= access_list_lookup(
6661 filter
->dlist
[direct
]
6664 filter
->dlist
[direct
].alist
=
6669 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6670 FOREACH_AFI_SAFI (afi
, safi
) {
6671 filter
= &group
->conf
->filter
[afi
][safi
];
6673 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6675 if (filter
->dlist
[direct
].name
)
6676 filter
->dlist
[direct
]
6677 .alist
= access_list_lookup(
6679 filter
->dlist
[direct
]
6682 filter
->dlist
[direct
].alist
=
6687 #ifdef ENABLE_BGP_VNC
6688 vnc_prefix_list_update(bgp
);
6693 /* Set prefix list to the peer. */
6694 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6697 struct peer
*member
;
6698 struct bgp_filter
*filter
;
6699 struct listnode
*node
, *nnode
;
6701 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6702 return BGP_ERR_INVALID_VALUE
;
6704 /* Set configuration on peer. */
6705 filter
= &peer
->filter
[afi
][safi
];
6706 if (filter
->dlist
[direct
].name
)
6707 return BGP_ERR_PEER_FILTER_CONFLICT
;
6708 if (filter
->plist
[direct
].name
)
6709 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6710 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6711 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6713 /* Check if handling a regular peer. */
6714 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6715 /* Set override-flag and process peer route updates. */
6716 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6717 PEER_FT_PREFIX_LIST
);
6718 peer_on_policy_change(peer
, afi
, safi
,
6719 (direct
== FILTER_OUT
) ? 1 : 0);
6721 /* Skip peer-group mechanics for regular peers. */
6726 * Set configuration on all peer-group members, unless they are
6727 * explicitly overriding peer-group configuration.
6729 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6730 /* Skip peers with overridden configuration. */
6731 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6732 PEER_FT_PREFIX_LIST
))
6735 /* Set configuration on peer-group member. */
6736 filter
= &member
->filter
[afi
][safi
];
6737 if (filter
->plist
[direct
].name
)
6738 XFREE(MTYPE_BGP_FILTER_NAME
,
6739 filter
->plist
[direct
].name
);
6740 filter
->plist
[direct
].name
=
6741 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6742 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6744 /* Process peer route updates. */
6745 peer_on_policy_change(member
, afi
, safi
,
6746 (direct
== FILTER_OUT
) ? 1 : 0);
6752 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6755 struct peer
*member
;
6756 struct bgp_filter
*filter
;
6757 struct listnode
*node
, *nnode
;
6759 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6760 return BGP_ERR_INVALID_VALUE
;
6762 /* Unset override-flag unconditionally. */
6763 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6764 PEER_FT_PREFIX_LIST
);
6766 /* Inherit configuration from peer-group if peer is member. */
6767 if (peer_group_active(peer
)) {
6768 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6769 filter
[afi
][safi
].plist
[direct
].name
,
6770 MTYPE_BGP_FILTER_NAME
);
6771 PEER_ATTR_INHERIT(peer
, peer
->group
,
6772 filter
[afi
][safi
].plist
[direct
].plist
);
6774 /* Otherwise remove configuration from peer. */
6775 filter
= &peer
->filter
[afi
][safi
];
6776 if (filter
->plist
[direct
].name
)
6777 XFREE(MTYPE_BGP_FILTER_NAME
,
6778 filter
->plist
[direct
].name
);
6779 filter
->plist
[direct
].name
= NULL
;
6780 filter
->plist
[direct
].plist
= NULL
;
6783 /* Check if handling a regular peer. */
6784 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6785 /* Process peer route updates. */
6786 peer_on_policy_change(peer
, afi
, safi
,
6787 (direct
== FILTER_OUT
) ? 1 : 0);
6789 /* Skip peer-group mechanics for regular peers. */
6794 * Remove configuration on all peer-group members, unless they are
6795 * explicitly overriding peer-group configuration.
6797 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6798 /* Skip peers with overridden configuration. */
6799 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6800 PEER_FT_PREFIX_LIST
))
6803 /* Remove configuration on peer-group member. */
6804 filter
= &member
->filter
[afi
][safi
];
6805 if (filter
->plist
[direct
].name
)
6806 XFREE(MTYPE_BGP_FILTER_NAME
,
6807 filter
->plist
[direct
].name
);
6808 filter
->plist
[direct
].name
= NULL
;
6809 filter
->plist
[direct
].plist
= NULL
;
6811 /* Process peer route updates. */
6812 peer_on_policy_change(member
, afi
, safi
,
6813 (direct
== FILTER_OUT
) ? 1 : 0);
6819 /* Update prefix-list list. */
6820 static void peer_prefix_list_update(struct prefix_list
*plist
)
6822 struct listnode
*mnode
, *mnnode
;
6823 struct listnode
*node
, *nnode
;
6826 struct peer_group
*group
;
6827 struct bgp_filter
*filter
;
6832 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6835 * Update the prefix-list on update groups.
6837 update_group_policy_update(
6838 bgp
, BGP_POLICY_PREFIX_LIST
,
6839 plist
? prefix_list_name(plist
) : NULL
, true, 0);
6841 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6842 FOREACH_AFI_SAFI (afi
, safi
) {
6843 filter
= &peer
->filter
[afi
][safi
];
6845 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6847 if (filter
->plist
[direct
].name
)
6848 filter
->plist
[direct
]
6849 .plist
= prefix_list_lookup(
6851 filter
->plist
[direct
]
6854 filter
->plist
[direct
].plist
=
6858 /* If we touch prefix-list, we need to process
6859 * new updates. This is important for ORF to
6860 * work correctly as well.
6862 if (peer
->afc_nego
[afi
][safi
])
6863 peer_on_policy_change(peer
, afi
, safi
,
6867 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6868 FOREACH_AFI_SAFI (afi
, safi
) {
6869 filter
= &group
->conf
->filter
[afi
][safi
];
6871 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6873 if (filter
->plist
[direct
].name
)
6874 filter
->plist
[direct
]
6875 .plist
= prefix_list_lookup(
6877 filter
->plist
[direct
]
6880 filter
->plist
[direct
].plist
=
6888 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6891 struct peer
*member
;
6892 struct bgp_filter
*filter
;
6893 struct listnode
*node
, *nnode
;
6895 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6896 return BGP_ERR_INVALID_VALUE
;
6898 /* Set configuration on peer. */
6899 filter
= &peer
->filter
[afi
][safi
];
6900 if (filter
->aslist
[direct
].name
)
6901 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6902 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6903 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6905 /* Check if handling a regular peer. */
6906 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6907 /* Set override-flag and process peer route updates. */
6908 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6909 PEER_FT_FILTER_LIST
);
6910 peer_on_policy_change(peer
, afi
, safi
,
6911 (direct
== FILTER_OUT
) ? 1 : 0);
6913 /* Skip peer-group mechanics for regular peers. */
6918 * Set configuration on all peer-group members, unless they are
6919 * explicitly overriding peer-group configuration.
6921 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6922 /* Skip peers with overridden configuration. */
6923 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6924 PEER_FT_FILTER_LIST
))
6927 /* Set configuration on peer-group member. */
6928 filter
= &member
->filter
[afi
][safi
];
6929 if (filter
->aslist
[direct
].name
)
6930 XFREE(MTYPE_BGP_FILTER_NAME
,
6931 filter
->aslist
[direct
].name
);
6932 filter
->aslist
[direct
].name
=
6933 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6934 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6936 /* Process peer route updates. */
6937 peer_on_policy_change(member
, afi
, safi
,
6938 (direct
== FILTER_OUT
) ? 1 : 0);
6944 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6946 struct peer
*member
;
6947 struct bgp_filter
*filter
;
6948 struct listnode
*node
, *nnode
;
6950 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6951 return BGP_ERR_INVALID_VALUE
;
6953 /* Unset override-flag unconditionally. */
6954 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6955 PEER_FT_FILTER_LIST
);
6957 /* Inherit configuration from peer-group if peer is member. */
6958 if (peer_group_active(peer
)) {
6959 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6960 filter
[afi
][safi
].aslist
[direct
].name
,
6961 MTYPE_BGP_FILTER_NAME
);
6962 PEER_ATTR_INHERIT(peer
, peer
->group
,
6963 filter
[afi
][safi
].aslist
[direct
].aslist
);
6965 /* Otherwise remove configuration from peer. */
6966 filter
= &peer
->filter
[afi
][safi
];
6967 if (filter
->aslist
[direct
].name
)
6968 XFREE(MTYPE_BGP_FILTER_NAME
,
6969 filter
->aslist
[direct
].name
);
6970 filter
->aslist
[direct
].name
= NULL
;
6971 filter
->aslist
[direct
].aslist
= NULL
;
6974 /* Check if handling a regular peer. */
6975 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6976 /* Process peer route updates. */
6977 peer_on_policy_change(peer
, afi
, safi
,
6978 (direct
== FILTER_OUT
) ? 1 : 0);
6980 /* Skip peer-group mechanics for regular peers. */
6985 * Remove configuration on all peer-group members, unless they are
6986 * explicitly overriding peer-group configuration.
6988 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6989 /* Skip peers with overridden configuration. */
6990 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6991 PEER_FT_FILTER_LIST
))
6994 /* Remove configuration on peer-group member. */
6995 filter
= &member
->filter
[afi
][safi
];
6996 if (filter
->aslist
[direct
].name
)
6997 XFREE(MTYPE_BGP_FILTER_NAME
,
6998 filter
->aslist
[direct
].name
);
6999 filter
->aslist
[direct
].name
= NULL
;
7000 filter
->aslist
[direct
].aslist
= NULL
;
7002 /* Process peer route updates. */
7003 peer_on_policy_change(member
, afi
, safi
,
7004 (direct
== FILTER_OUT
) ? 1 : 0);
7010 static void peer_aslist_update(const char *aslist_name
)
7015 struct listnode
*mnode
, *mnnode
;
7016 struct listnode
*node
, *nnode
;
7019 struct peer_group
*group
;
7020 struct bgp_filter
*filter
;
7022 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
7023 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
7024 aslist_name
, true, 0);
7026 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7027 FOREACH_AFI_SAFI (afi
, safi
) {
7028 filter
= &peer
->filter
[afi
][safi
];
7030 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
7032 if (filter
->aslist
[direct
].name
)
7033 filter
->aslist
[direct
]
7034 .aslist
= as_list_lookup(
7035 filter
->aslist
[direct
]
7038 filter
->aslist
[direct
].aslist
=
7043 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
7044 FOREACH_AFI_SAFI (afi
, safi
) {
7045 filter
= &group
->conf
->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
=
7063 static void peer_aslist_add(char *aslist_name
)
7065 peer_aslist_update(aslist_name
);
7066 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
7069 static void peer_aslist_del(const char *aslist_name
)
7071 peer_aslist_update(aslist_name
);
7072 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
7076 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
7077 const char *name
, struct route_map
*route_map
)
7079 struct peer
*member
;
7080 struct bgp_filter
*filter
;
7081 struct listnode
*node
, *nnode
;
7083 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7084 return BGP_ERR_INVALID_VALUE
;
7086 /* Set configuration on peer. */
7087 filter
= &peer
->filter
[afi
][safi
];
7088 if (filter
->map
[direct
].name
) {
7089 /* If the neighbor is configured with the same route-map
7090 * again then, ignore the duplicate configuration.
7092 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
7095 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7097 route_map_counter_decrement(filter
->map
[direct
].map
);
7098 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7099 filter
->map
[direct
].map
= route_map
;
7100 route_map_counter_increment(route_map
);
7102 /* Check if handling a regular peer. */
7103 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7104 /* Set override-flag and process peer route updates. */
7105 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
7107 peer_on_policy_change(peer
, afi
, safi
,
7108 (direct
== RMAP_OUT
) ? 1 : 0);
7110 /* Skip peer-group mechanics for regular peers. */
7115 * Set configuration on all peer-group members, unless they are
7116 * explicitly overriding peer-group configuration.
7118 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7119 /* Skip peers with overridden configuration. */
7120 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7124 /* Set configuration on peer-group member. */
7125 filter
= &member
->filter
[afi
][safi
];
7126 if (filter
->map
[direct
].name
)
7127 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7128 route_map_counter_decrement(filter
->map
[direct
].map
);
7129 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7130 filter
->map
[direct
].map
= route_map
;
7131 route_map_counter_increment(route_map
);
7133 /* Process peer route updates. */
7134 peer_on_policy_change(member
, afi
, safi
,
7135 (direct
== RMAP_OUT
) ? 1 : 0);
7140 /* Unset route-map from the peer. */
7141 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
7143 struct peer
*member
;
7144 struct bgp_filter
*filter
;
7145 struct listnode
*node
, *nnode
;
7147 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
7148 return BGP_ERR_INVALID_VALUE
;
7150 /* Unset override-flag unconditionally. */
7151 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
7153 /* Inherit configuration from peer-group if peer is member. */
7154 if (peer_group_active(peer
)) {
7155 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7156 filter
[afi
][safi
].map
[direct
].name
,
7157 MTYPE_BGP_FILTER_NAME
);
7158 PEER_ATTR_INHERIT(peer
, peer
->group
,
7159 filter
[afi
][safi
].map
[direct
].map
);
7161 /* Otherwise remove configuration from peer. */
7162 filter
= &peer
->filter
[afi
][safi
];
7163 if (filter
->map
[direct
].name
)
7164 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7165 route_map_counter_decrement(filter
->map
[direct
].map
);
7166 filter
->map
[direct
].name
= NULL
;
7167 filter
->map
[direct
].map
= NULL
;
7170 /* Check if handling a regular peer. */
7171 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7172 /* Process peer route updates. */
7173 peer_on_policy_change(peer
, afi
, safi
,
7174 (direct
== RMAP_OUT
) ? 1 : 0);
7176 /* Skip peer-group mechanics for regular peers. */
7181 * Remove configuration on all peer-group members, unless they are
7182 * explicitly overriding peer-group configuration.
7184 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7185 /* Skip peers with overridden configuration. */
7186 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
7190 /* Remove configuration on peer-group member. */
7191 filter
= &member
->filter
[afi
][safi
];
7192 if (filter
->map
[direct
].name
)
7193 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
7194 route_map_counter_decrement(filter
->map
[direct
].map
);
7195 filter
->map
[direct
].name
= NULL
;
7196 filter
->map
[direct
].map
= NULL
;
7198 /* Process peer route updates. */
7199 peer_on_policy_change(member
, afi
, safi
,
7200 (direct
== RMAP_OUT
) ? 1 : 0);
7206 /* Set unsuppress-map to the peer. */
7207 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7208 const char *name
, struct route_map
*route_map
)
7210 struct peer
*member
;
7211 struct bgp_filter
*filter
;
7212 struct listnode
*node
, *nnode
;
7214 /* Set configuration on peer. */
7215 filter
= &peer
->filter
[afi
][safi
];
7216 if (filter
->usmap
.name
)
7217 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7218 route_map_counter_decrement(filter
->usmap
.map
);
7219 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7220 filter
->usmap
.map
= route_map
;
7221 route_map_counter_increment(route_map
);
7223 /* Check if handling a regular peer. */
7224 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7225 /* Set override-flag and process peer route updates. */
7226 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
7227 PEER_FT_UNSUPPRESS_MAP
);
7228 peer_on_policy_change(peer
, afi
, safi
, 1);
7230 /* Skip peer-group mechanics for regular peers. */
7235 * Set configuration on all peer-group members, unless they are
7236 * explicitly overriding peer-group configuration.
7238 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7239 /* Skip peers with overridden configuration. */
7240 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7241 PEER_FT_UNSUPPRESS_MAP
))
7244 /* Set configuration on peer-group member. */
7245 filter
= &member
->filter
[afi
][safi
];
7246 if (filter
->usmap
.name
)
7247 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7248 route_map_counter_decrement(filter
->usmap
.map
);
7249 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
7250 filter
->usmap
.map
= route_map
;
7251 route_map_counter_increment(route_map
);
7253 /* Process peer route updates. */
7254 peer_on_policy_change(member
, afi
, safi
, 1);
7260 /* Unset route-map from the peer. */
7261 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7263 struct peer
*member
;
7264 struct bgp_filter
*filter
;
7265 struct listnode
*node
, *nnode
;
7267 /* Unset override-flag unconditionally. */
7268 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
7270 /* Inherit configuration from peer-group if peer is member. */
7271 if (peer_group_active(peer
)) {
7272 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7273 filter
[afi
][safi
].usmap
.name
,
7274 MTYPE_BGP_FILTER_NAME
);
7275 PEER_ATTR_INHERIT(peer
, peer
->group
,
7276 filter
[afi
][safi
].usmap
.map
);
7278 /* Otherwise remove configuration from peer. */
7279 filter
= &peer
->filter
[afi
][safi
];
7280 if (filter
->usmap
.name
)
7281 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7282 route_map_counter_decrement(filter
->usmap
.map
);
7283 filter
->usmap
.name
= NULL
;
7284 filter
->usmap
.map
= NULL
;
7287 /* Check if handling a regular peer. */
7288 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7289 /* Process peer route updates. */
7290 peer_on_policy_change(peer
, afi
, safi
, 1);
7292 /* Skip peer-group mechanics for regular peers. */
7297 * Remove configuration on all peer-group members, unless they are
7298 * explicitly overriding peer-group configuration.
7300 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7301 /* Skip peers with overridden configuration. */
7302 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
7303 PEER_FT_UNSUPPRESS_MAP
))
7306 /* Remove configuration on peer-group member. */
7307 filter
= &member
->filter
[afi
][safi
];
7308 if (filter
->usmap
.name
)
7309 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
7310 route_map_counter_decrement(filter
->usmap
.map
);
7311 filter
->usmap
.name
= NULL
;
7312 filter
->usmap
.map
= NULL
;
7314 /* Process peer route updates. */
7315 peer_on_policy_change(member
, afi
, safi
, 1);
7321 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7323 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7326 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7327 if (peer
->t_pmax_restart
) {
7328 THREAD_OFF(peer
->t_pmax_restart
);
7329 if (bgp_debug_neighbor_events(peer
))
7331 "%pBP Maximum-prefix restart timer cancelled",
7334 BGP_EVENT_ADD(peer
, BGP_Start
);
7338 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7339 uint32_t max
, uint8_t threshold
, int warning
,
7340 uint16_t restart
, bool force
)
7342 struct peer
*member
;
7343 struct listnode
*node
, *nnode
;
7345 /* Set flags and configuration on peer. */
7346 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7349 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7351 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7354 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7356 peer_af_flag_unset(peer
, afi
, safi
,
7357 PEER_FLAG_MAX_PREFIX_WARNING
);
7359 peer
->pmax
[afi
][safi
] = max
;
7360 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7361 peer
->pmax_restart
[afi
][safi
] = restart
;
7363 /* Check if handling a regular peer. */
7364 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7365 /* Re-check if peer violates maximum-prefix. */
7366 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7367 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7369 /* Skip peer-group mechanics for regular peers. */
7374 * Set flags and configuration on all peer-group members, unless they
7375 * are explicitly overriding peer-group configuration.
7377 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7378 /* Skip peers with overridden configuration. */
7379 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7380 PEER_FLAG_MAX_PREFIX
))
7383 /* Set flag and configuration on peer-group member. */
7384 member
->pmax
[afi
][safi
] = max
;
7385 member
->pmax_threshold
[afi
][safi
] = threshold
;
7386 member
->pmax_restart
[afi
][safi
] = restart
;
7389 SET_FLAG(member
->af_flags
[afi
][safi
],
7390 PEER_FLAG_MAX_PREFIX_FORCE
);
7392 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7393 PEER_FLAG_MAX_PREFIX_FORCE
);
7396 SET_FLAG(member
->af_flags
[afi
][safi
],
7397 PEER_FLAG_MAX_PREFIX_WARNING
);
7399 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7400 PEER_FLAG_MAX_PREFIX_WARNING
);
7402 /* Re-check if peer violates maximum-prefix. */
7403 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7404 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7410 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7412 /* Inherit configuration from peer-group if peer is member. */
7413 if (peer_group_active(peer
)) {
7414 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7415 peer_af_flag_inherit(peer
, afi
, safi
,
7416 PEER_FLAG_MAX_PREFIX_FORCE
);
7417 peer_af_flag_inherit(peer
, afi
, safi
,
7418 PEER_FLAG_MAX_PREFIX_WARNING
);
7419 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7420 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7421 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7426 /* Remove flags and configuration from peer. */
7427 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7428 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7429 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7430 peer
->pmax
[afi
][safi
] = 0;
7431 peer
->pmax_threshold
[afi
][safi
] = 0;
7432 peer
->pmax_restart
[afi
][safi
] = 0;
7435 * Remove flags and configuration from all peer-group members, unless
7436 * they are explicitly overriding peer-group configuration.
7438 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7439 struct peer
*member
;
7440 struct listnode
*node
;
7442 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7443 /* Skip peers with overridden configuration. */
7444 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7445 PEER_FLAG_MAX_PREFIX
))
7448 /* Remove flag and configuration on peer-group member.
7450 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7451 PEER_FLAG_MAX_PREFIX
);
7452 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7453 PEER_FLAG_MAX_PREFIX_FORCE
);
7454 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7455 PEER_FLAG_MAX_PREFIX_WARNING
);
7456 member
->pmax
[afi
][safi
] = 0;
7457 member
->pmax_threshold
[afi
][safi
] = 0;
7458 member
->pmax_restart
[afi
][safi
] = 0;
7460 peer_maximum_prefix_clear_overflow(member
);
7463 peer_maximum_prefix_clear_overflow(peer
);
7469 void peer_maximum_prefix_out_refresh_routes(struct peer
*peer
, afi_t afi
,
7472 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
7474 if (peer_established(peer
))
7475 bgp_announce_route(peer
, afi
, safi
, false);
7478 int peer_maximum_prefix_out_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7481 struct peer
*member
;
7482 struct listnode
*node
, *nnode
;
7484 /* Set flag on peer and peer-group member if any */
7485 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7486 /* Set configuration on peer. */
7487 peer
->pmax_out
[afi
][safi
] = max
;
7489 /* Check if handling a regular peer. */
7490 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7491 /* Skip peer-group mechanics for regular peers. */
7492 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7497 * Set flag and configuration on all peer-group members, unless they
7498 * are explicitly overriding peer-group configuration.
7500 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7501 /* Skip peers with overridden configuration. */
7502 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7503 PEER_FLAG_MAX_PREFIX_OUT
))
7506 /* Set configuration on peer-group member. */
7507 member
->pmax_out
[afi
][safi
] = max
;
7509 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7514 int peer_maximum_prefix_out_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7516 struct peer
*member
;
7517 struct listnode
*node
;
7518 /* Inherit configuration from peer-group if peer is member. */
7519 if (peer_group_active(peer
)) {
7520 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7521 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_out
[afi
][safi
]);
7523 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7527 /* Remove flag and configuration from peer. */
7528 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_OUT
);
7529 peer
->pmax_out
[afi
][safi
] = 0;
7531 /* Check if handling a regular peer. */
7532 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7533 /* Skip peer-group mechanics for regular peers. */
7534 peer_maximum_prefix_out_refresh_routes(peer
, afi
, safi
);
7539 * Remove flag and configuration from all peer-group members, unless
7540 * they are explicitly overriding peer-group configuration.
7542 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7543 /* Skip peers with overridden configuration. */
7544 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7545 PEER_FLAG_MAX_PREFIX_OUT
))
7548 /* Remove flag and configuration on peer-group member.
7550 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7551 PEER_FLAG_MAX_PREFIX_OUT
);
7552 member
->pmax_out
[afi
][safi
] = 0;
7554 peer_maximum_prefix_out_refresh_routes(member
, afi
, safi
);
7559 int is_ebgp_multihop_configured(struct peer
*peer
)
7561 struct peer_group
*group
;
7562 struct listnode
*node
, *nnode
;
7565 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7566 group
= peer
->group
;
7567 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7568 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7571 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7572 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7573 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7577 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7578 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7584 /* Set # of hops between us and BGP peer. */
7585 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7587 struct peer_group
*group
;
7589 struct listnode
*node
, *nnode
;
7592 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7595 /* We cannot configure ttl-security hops when ebgp-multihop is already
7596 set. For non peer-groups, the check is simple. For peer-groups,
7598 slightly messy, because we need to check both the peer-group
7600 and all peer-group members for any trace of ebgp-multihop
7602 before actually applying the ttl-security rules. Cisco really made a
7603 mess of this configuration parameter, and OpenBGPD got it right.
7606 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7607 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7608 if (is_ebgp_multihop_configured(peer
))
7609 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7611 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7612 peer
->gtsm_hops
= gtsm_hops
;
7614 /* Calling ebgp multihop also resets the session.
7615 * On restart, NHT will get setup correctly as will the
7616 * min & max ttls on the socket. The return value is
7619 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7624 group
= peer
->group
;
7625 group
->conf
->gtsm_hops
= gtsm_hops
;
7626 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7628 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7630 /* Calling ebgp multihop also resets the
7632 * On restart, NHT will get setup correctly as
7634 * min & max ttls on the socket. The return
7638 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7642 /* Post the first gtsm setup or if its ibgp, maxttl setting
7644 * necessary, just set the minttl.
7646 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7647 peer
->gtsm_hops
= gtsm_hops
;
7650 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7651 MAXTTL
+ 1 - gtsm_hops
);
7652 if ((peer
->status
< Established
) && peer
->doppelganger
7653 && (peer
->doppelganger
->fd
>= 0))
7654 sockopt_minttl(peer
->su
.sa
.sa_family
,
7655 peer
->doppelganger
->fd
,
7656 MAXTTL
+ 1 - gtsm_hops
);
7658 group
= peer
->group
;
7659 group
->conf
->gtsm_hops
= gtsm_hops
;
7660 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7662 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7664 /* Change setting of existing peer
7665 * established then change value (may break
7667 * not established yet (teardown session and
7669 * no session then do nothing (will get
7670 * handled by next connection)
7674 != BGP_GTSM_HOPS_DISABLED
)
7676 gpeer
->su
.sa
.sa_family
,
7678 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7679 if ((gpeer
->status
< Established
)
7680 && gpeer
->doppelganger
7681 && (gpeer
->doppelganger
->fd
>= 0))
7682 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7683 gpeer
->doppelganger
->fd
,
7684 MAXTTL
+ 1 - gtsm_hops
);
7692 int peer_ttl_security_hops_unset(struct peer
*peer
)
7694 struct peer_group
*group
;
7695 struct listnode
*node
, *nnode
;
7698 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7700 /* if a peer-group member, then reset to peer-group default rather than
7702 if (peer_group_active(peer
))
7703 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7705 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7707 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7708 /* Invoking ebgp_multihop_set will set the TTL back to the
7710 * value as well as restting the NHT and such. The session is
7713 if (peer
->sort
== BGP_PEER_EBGP
)
7714 ret
= peer_ebgp_multihop_unset(peer
);
7717 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7720 if ((peer
->status
< Established
) && peer
->doppelganger
7721 && (peer
->doppelganger
->fd
>= 0))
7722 sockopt_minttl(peer
->su
.sa
.sa_family
,
7723 peer
->doppelganger
->fd
, 0);
7726 group
= peer
->group
;
7727 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7728 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7729 if (peer
->sort
== BGP_PEER_EBGP
)
7730 ret
= peer_ebgp_multihop_unset(peer
);
7733 sockopt_minttl(peer
->su
.sa
.sa_family
,
7736 if ((peer
->status
< Established
)
7737 && peer
->doppelganger
7738 && (peer
->doppelganger
->fd
>= 0))
7739 sockopt_minttl(peer
->su
.sa
.sa_family
,
7740 peer
->doppelganger
->fd
,
7749 static void peer_reset_message_stats(struct peer
*peer
)
7752 atomic_store_explicit(&peer
->open_in
, 0, memory_order_relaxed
);
7753 atomic_store_explicit(&peer
->open_out
, 0, memory_order_relaxed
);
7754 atomic_store_explicit(&peer
->update_in
, 0,
7755 memory_order_relaxed
);
7756 atomic_store_explicit(&peer
->update_out
, 0,
7757 memory_order_relaxed
);
7758 atomic_store_explicit(&peer
->keepalive_in
, 0,
7759 memory_order_relaxed
);
7760 atomic_store_explicit(&peer
->keepalive_out
, 0,
7761 memory_order_relaxed
);
7762 atomic_store_explicit(&peer
->notify_in
, 0,
7763 memory_order_relaxed
);
7764 atomic_store_explicit(&peer
->notify_out
, 0,
7765 memory_order_relaxed
);
7766 atomic_store_explicit(&peer
->refresh_in
, 0,
7767 memory_order_relaxed
);
7768 atomic_store_explicit(&peer
->refresh_out
, 0,
7769 memory_order_relaxed
);
7770 atomic_store_explicit(&peer
->dynamic_cap_in
, 0,
7771 memory_order_relaxed
);
7772 atomic_store_explicit(&peer
->dynamic_cap_out
, 0,
7773 memory_order_relaxed
);
7778 * If peer clear is invoked in a loop for all peers on the BGP instance,
7779 * it may end up freeing the doppelganger, and if this was the next node
7780 * to the current node, we would end up accessing the freed next node.
7781 * Pass along additional parameter which can be updated if next node
7782 * is freed; only required when walking the peer list on BGP instance.
7784 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7786 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7787 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7788 if (peer_maximum_prefix_clear_overflow(peer
))
7791 peer
->v_start
= BGP_INIT_START_TIMER
;
7792 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7793 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7794 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7796 bgp_session_reset_safe(peer
, nnode
);
7801 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7802 enum bgp_clear_type stype
)
7804 struct peer_af
*paf
;
7806 if (!peer_established(peer
))
7809 if (!peer
->afc
[afi
][safi
])
7810 return BGP_ERR_AF_UNCONFIGURED
;
7812 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7814 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7815 /* Clear the "neighbor x.x.x.x default-originate" flag */
7816 paf
= peer_af_find(peer
, afi
, safi
);
7817 if (paf
&& paf
->subgroup
7818 && CHECK_FLAG(paf
->subgroup
->sflags
,
7819 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7820 UNSET_FLAG(paf
->subgroup
->sflags
,
7821 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7823 bgp_announce_route(peer
, afi
, safi
, false);
7826 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7827 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7828 PEER_CAP_ORF_PREFIX_SM_ADV
)
7829 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7830 PEER_CAP_ORF_PREFIX_RM_RCV
)
7831 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7832 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7833 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7834 uint8_t prefix_type
;
7836 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7837 PEER_CAP_ORF_PREFIX_RM_RCV
))
7838 prefix_type
= ORF_TYPE_PREFIX
;
7840 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7842 if (filter
->plist
[FILTER_IN
].plist
) {
7843 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7844 PEER_STATUS_ORF_PREFIX_SEND
))
7845 bgp_route_refresh_send(
7846 peer
, afi
, safi
, prefix_type
,
7848 BGP_ROUTE_REFRESH_NORMAL
);
7849 bgp_route_refresh_send(
7850 peer
, afi
, safi
, prefix_type
,
7851 REFRESH_IMMEDIATE
, 0,
7852 BGP_ROUTE_REFRESH_NORMAL
);
7854 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7855 PEER_STATUS_ORF_PREFIX_SEND
))
7856 bgp_route_refresh_send(
7857 peer
, afi
, safi
, prefix_type
,
7858 REFRESH_IMMEDIATE
, 1,
7859 BGP_ROUTE_REFRESH_NORMAL
);
7861 bgp_route_refresh_send(
7862 peer
, afi
, safi
, 0, 0, 0,
7863 BGP_ROUTE_REFRESH_NORMAL
);
7869 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7870 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7871 /* If neighbor has soft reconfiguration inbound flag.
7872 Use Adj-RIB-In database. */
7873 if (!bgp_soft_reconfig_in(peer
, afi
, safi
)) {
7874 /* If neighbor has route refresh capability, send route
7876 message to the peer. */
7877 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7878 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7879 bgp_route_refresh_send(
7880 peer
, afi
, safi
, 0, 0, 0,
7881 BGP_ROUTE_REFRESH_NORMAL
);
7883 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7887 if (stype
== BGP_CLEAR_MESSAGE_STATS
)
7888 peer_reset_message_stats(peer
);
7893 /* Display peer uptime.*/
7894 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7897 time_t uptime1
, epoch_tbuf
;
7900 /* If there is no connection has been done before print `never'. */
7903 json_object_string_add(json
, "peerUptime", "never");
7904 json_object_int_add(json
, "peerUptimeMsec", 0);
7906 snprintf(buf
, len
, "never");
7910 /* Get current time. */
7911 uptime1
= monotime(NULL
);
7913 gmtime_r(&uptime1
, &tm
);
7915 if (uptime1
< ONE_DAY_SECOND
)
7916 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7918 else if (uptime1
< ONE_WEEK_SECOND
)
7919 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7921 else if (uptime1
< ONE_YEAR_SECOND
)
7922 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7923 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7925 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7927 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7930 epoch_tbuf
= time(NULL
) - uptime1
;
7931 json_object_string_add(json
, "peerUptime", buf
);
7932 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7933 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7940 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7941 struct list
*addresses
)
7945 memset(&bgp_master
, 0, sizeof(bgp_master
));
7948 bm
->bgp
= list_new();
7949 bm
->listen_sockets
= list_new();
7950 bm
->port
= BGP_PORT_DEFAULT
;
7951 bm
->addresses
= addresses
;
7952 bm
->master
= master
;
7953 bm
->start_time
= monotime(NULL
);
7954 bm
->t_rmap_update
= NULL
;
7955 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7956 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7957 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7958 bm
->terminating
= false;
7959 bm
->socket_buffer
= buffer_size
;
7960 bm
->wait_for_fib
= false;
7961 bm
->tcp_dscp
= IPTOS_PREC_INTERNETCONTROL
;
7962 bm
->inq_limit
= BM_DEFAULT_INQ_LIMIT
;
7965 /* init the rd id space.
7966 assign 0th index in the bitfield,
7967 so that we start with id 1
7969 bf_init(bm
->rd_idspace
, UINT16_MAX
);
7970 bf_assign_zero_index(bm
->rd_idspace
);
7972 /* mpls label dynamic allocation pool */
7973 bgp_lp_init(bm
->master
, &bm
->labelpool
);
7977 QOBJ_REG(bm
, bgp_master
);
7981 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7982 * instance delete (non-default only) or BGP exit.
7984 static void bgp_if_finish(struct bgp
*bgp
)
7987 struct interface
*ifp
;
7989 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
7991 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
7994 FOR_ALL_INTERFACES (vrf
, ifp
) {
7995 struct listnode
*c_node
, *c_nnode
;
7996 struct connected
*c
;
7998 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
7999 bgp_connected_delete(bgp
, c
);
8003 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
8005 struct vrf
*vrf
= NULL
;
8006 struct listnode
*next
;
8009 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
8010 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
8012 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8013 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
8016 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
8020 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
8022 struct listnode
*next
, *next2
;
8023 struct bgp
*bgp
, *bgp2
;
8026 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
8028 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
8029 if (bgp2
->as
== bgp
->as
)
8037 snprintf(buf
, sizeof(buf
), "%u", bgp
->as
);
8038 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
8042 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
8043 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
8044 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
8045 {.completions
= NULL
},
8048 struct frr_pthread
*bgp_pth_io
;
8049 struct frr_pthread
*bgp_pth_ka
;
8051 static void bgp_pthreads_init(void)
8053 assert(!bgp_pth_io
);
8054 assert(!bgp_pth_ka
);
8056 struct frr_pthread_attr io
= {
8057 .start
= frr_pthread_attr_default
.start
,
8058 .stop
= frr_pthread_attr_default
.stop
,
8060 struct frr_pthread_attr ka
= {
8061 .start
= bgp_keepalives_start
,
8062 .stop
= bgp_keepalives_stop
,
8064 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
8065 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
8068 void bgp_pthreads_run(void)
8070 frr_pthread_run(bgp_pth_io
, NULL
);
8071 frr_pthread_run(bgp_pth_ka
, NULL
);
8073 /* Wait until threads are ready. */
8074 frr_pthread_wait_running(bgp_pth_io
);
8075 frr_pthread_wait_running(bgp_pth_ka
);
8078 void bgp_pthreads_finish(void)
8080 frr_pthread_stop_all();
8083 static int peer_unshut_after_cfg(struct bgp
*bgp
)
8085 struct listnode
*node
;
8088 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
8089 if (!peer
->shut_during_cfg
)
8092 if (bgp_debug_neighbor_events(peer
))
8093 zlog_debug("%s: released from config-pending hold",
8096 peer
->shut_during_cfg
= false;
8097 if (peer_active(peer
) && peer
->status
!= Established
) {
8098 if (peer
->status
!= Idle
)
8099 BGP_EVENT_ADD(peer
, BGP_Stop
);
8100 BGP_EVENT_ADD(peer
, BGP_Start
);
8107 void bgp_init(unsigned short instance
)
8109 hook_register(bgp_config_end
, peer_unshut_after_cfg
);
8111 /* allocates some vital data structures used by peer commands in
8114 /* pre-init pthreads */
8115 bgp_pthreads_init();
8118 bgp_zebra_init(bm
->master
, instance
);
8120 #ifdef ENABLE_BGP_VNC
8121 vnc_zebra_init(bm
->master
);
8124 /* BGP VTY commands installation. */
8130 bgp_community_alias_init();
8133 bgp_route_map_init();
8134 bgp_scan_vty_init();
8136 #ifdef ENABLE_BGP_VNC
8139 bgp_ethernetvpn_init();
8140 bgp_flowspec_vty_init();
8142 /* Access list initialize. */
8144 access_list_add_hook(peer_distribute_update
);
8145 access_list_delete_hook(peer_distribute_update
);
8147 /* Filter list initialize. */
8149 as_list_add_hook(peer_aslist_add
);
8150 as_list_delete_hook(peer_aslist_del
);
8152 /* Prefix list initialize.*/
8154 prefix_list_add_hook(peer_prefix_list_update
);
8155 prefix_list_delete_hook(peer_prefix_list_update
);
8157 /* Community list initialize. */
8158 bgp_clist
= community_list_init();
8161 bgp_bfd_init(bm
->master
);
8165 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
8168 void bgp_terminate(void)
8172 struct listnode
*node
, *nnode
;
8173 struct listnode
*mnode
, *mnnode
;
8177 /* Close the listener sockets first as this prevents peers from
8179 * to reconnect on receiving the peer unconfig message. In the presence
8180 * of a large number of peers this will ensure that no peer is left with
8181 * a dangling connection
8185 /* reverse bgp_master_init */
8186 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
8187 bgp_close_vrf_socket(bgp
);
8188 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8189 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)) {
8190 if (bgp_debug_neighbor_events(peer
))
8192 "%pBP configured Graceful-Restart, skipping unconfig notification",
8196 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
8197 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
8198 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
8202 if (bm
->listen_sockets
)
8203 list_delete(&bm
->listen_sockets
);
8205 THREAD_OFF(bm
->t_rmap_update
);
8210 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
8211 const char *ip_str
, bool use_json
)
8217 /* Get peer sockunion. */
8218 ret
= str2sockunion(ip_str
, &su
);
8220 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
8222 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
8226 json_object
*json_no
= NULL
;
8227 json_no
= json_object_new_object();
8228 json_object_string_add(
8230 "malformedAddressOrName",
8232 vty_json(vty
, json_no
);
8235 "%% Malformed address or name: %s\n",
8243 /* Peer structure lookup. */
8244 peer
= peer_lookup(bgp
, &su
);
8247 json_object
*json_no
= NULL
;
8248 json_no
= json_object_new_object();
8249 json_object_string_add(json_no
, "warning",
8250 "No such neighbor in this view/vrf");
8251 vty_json(vty
, json_no
);
8253 vty_out(vty
, "No such neighbor in this view/vrf\n");
8260 void bgp_gr_apply_running_config(void)
8262 struct peer
*peer
= NULL
;
8263 struct bgp
*bgp
= NULL
;
8264 struct listnode
*node
, *nnode
;
8265 bool gr_router_detected
= false;
8267 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
8268 zlog_debug("[BGP_GR] %s called !", __func__
);
8270 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
8271 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8272 bgp_peer_gr_flags_update(peer
);
8273 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
8274 gr_router_detected
= true;
8277 if (gr_router_detected
8278 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
8279 bgp_zebra_send_capabilities(bgp
, true);
8280 } else if (!gr_router_detected
8281 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
8282 bgp_zebra_send_capabilities(bgp
, false);
8285 gr_router_detected
= false;
8289 printfrr_ext_autoreg_p("BP", printfrr_bp
);
8290 static ssize_t
printfrr_bp(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
8293 const struct peer
*peer
= ptr
;
8296 return bputs(buf
, "(null)");
8298 return bprintfrr(buf
, "%s(%s)", peer
->host
,
8299 peer
->hostname
? peer
->hostname
: "Unknown");