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
28 #include "sockunion.h"
37 #include "workqueue.h"
46 #include "bgpd/bgpd.h"
47 #include "bgpd/bgp_table.h"
48 #include "bgpd/bgp_aspath.h"
49 #include "bgpd/bgp_route.h"
50 #include "bgpd/bgp_dump.h"
51 #include "bgpd/bgp_debug.h"
52 #include "bgpd/bgp_community.h"
53 #include "bgpd/bgp_attr.h"
54 #include "bgpd/bgp_regex.h"
55 #include "bgpd/bgp_clist.h"
56 #include "bgpd/bgp_fsm.h"
57 #include "bgpd/bgp_packet.h"
58 #include "bgpd/bgp_zebra.h"
59 #include "bgpd/bgp_open.h"
60 #include "bgpd/bgp_filter.h"
61 #include "bgpd/bgp_nexthop.h"
62 #include "bgpd/bgp_damp.h"
63 #include "bgpd/bgp_mplsvpn.h"
65 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
66 #include "bgpd/rfapi/rfapi_backend.h"
68 #include "bgpd/bgp_evpn.h"
69 #include "bgpd/bgp_advertise.h"
70 #include "bgpd/bgp_network.h"
71 #include "bgpd/bgp_vty.h"
72 #include "bgpd/bgp_mpath.h"
73 #include "bgpd/bgp_nht.h"
74 #include "bgpd/bgp_updgrp.h"
75 #include "bgpd/bgp_bfd.h"
76 #include "bgpd/bgp_memory.h"
77 #include "bgpd/bgp_evpn_vty.h"
80 DEFINE_MTYPE_STATIC(BGPD
, PEER_TX_SHUTDOWN_MSG
, "Peer shutdown message (TX)");
81 DEFINE_QOBJ_TYPE(bgp_master
)
83 DEFINE_QOBJ_TYPE(peer
)
85 /* BGP process wide configuration. */
86 static struct bgp_master bgp_master
;
88 /* BGP process wide configuration pointer to export. */
89 struct bgp_master
*bm
;
91 /* BGP community-list. */
92 struct community_list_handler
*bgp_clist
;
94 unsigned int multipath_num
= MULTIPATH_NUM
;
96 static void bgp_if_finish(struct bgp
*bgp
);
98 extern struct zclient
*zclient
;
100 void bgp_session_reset(struct peer
*peer
)
102 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
103 && !(CHECK_FLAG(peer
->doppelganger
->flags
, PEER_FLAG_CONFIG_NODE
)))
104 peer_delete(peer
->doppelganger
);
106 BGP_EVENT_ADD(peer
, BGP_Stop
);
110 * During session reset, we may delete the doppelganger peer, which would
111 * be the next node to the current node. If the session reset was invoked
112 * during walk of peer list, we would end up accessing the freed next
113 * node. This function moves the next node along.
115 static void bgp_session_reset_safe(struct peer
*peer
, struct listnode
**nnode
)
120 n
= (nnode
) ? *nnode
: NULL
;
121 npeer
= (n
) ? listgetdata(n
) : NULL
;
123 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
124 && !(CHECK_FLAG(peer
->doppelganger
->flags
,
125 PEER_FLAG_CONFIG_NODE
))) {
126 if (peer
->doppelganger
== npeer
)
127 /* nnode and *nnode are confirmed to be non-NULL here */
128 *nnode
= (*nnode
)->next
;
129 peer_delete(peer
->doppelganger
);
132 BGP_EVENT_ADD(peer
, BGP_Stop
);
135 /* BGP global flag manipulation. */
136 int bgp_option_set(int flag
)
140 case BGP_OPT_MULTIPLE_INSTANCE
:
141 case BGP_OPT_CONFIG_CISCO
:
142 case BGP_OPT_NO_LISTEN
:
143 SET_FLAG(bm
->options
, flag
);
146 return BGP_ERR_INVALID_FLAG
;
151 int bgp_option_unset(int flag
)
154 case BGP_OPT_MULTIPLE_INSTANCE
:
155 if (listcount(bm
->bgp
) > 1)
156 return BGP_ERR_MULTIPLE_INSTANCE_USED
;
159 case BGP_OPT_CONFIG_CISCO
:
160 UNSET_FLAG(bm
->options
, flag
);
163 return BGP_ERR_INVALID_FLAG
;
168 int bgp_option_check(int flag
)
170 return CHECK_FLAG(bm
->options
, flag
);
173 /* BGP flag manipulation. */
174 int bgp_flag_set(struct bgp
*bgp
, int flag
)
176 SET_FLAG(bgp
->flags
, flag
);
180 int bgp_flag_unset(struct bgp
*bgp
, int flag
)
182 UNSET_FLAG(bgp
->flags
, flag
);
186 int bgp_flag_check(struct bgp
*bgp
, int flag
)
188 return CHECK_FLAG(bgp
->flags
, flag
);
191 /* Internal function to set BGP structure configureation flag. */
192 static void bgp_config_set(struct bgp
*bgp
, int config
)
194 SET_FLAG(bgp
->config
, config
);
197 static void bgp_config_unset(struct bgp
*bgp
, int config
)
199 UNSET_FLAG(bgp
->config
, config
);
202 static int bgp_config_check(struct bgp
*bgp
, int config
)
204 return CHECK_FLAG(bgp
->config
, config
);
207 /* Set BGP router identifier. */
208 static int bgp_router_id_set(struct bgp
*bgp
, const struct in_addr
*id
)
211 struct listnode
*node
, *nnode
;
213 if (IPV4_ADDR_SAME(&bgp
->router_id
, id
))
216 /* EVPN uses router id in RD, withdraw them */
217 if (bgp
->advertise_all_vni
)
218 bgp_evpn_handle_router_id_update(bgp
, TRUE
);
220 IPV4_ADDR_COPY(&bgp
->router_id
, id
);
222 /* Set all peer's local identifier with this value. */
223 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
224 IPV4_ADDR_COPY(&peer
->local_id
, id
);
226 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
227 peer
->last_reset
= PEER_DOWN_RID_CHANGE
;
228 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
229 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
233 /* EVPN uses router id in RD, update them */
234 if (bgp
->advertise_all_vni
)
235 bgp_evpn_handle_router_id_update(bgp
, FALSE
);
240 void bgp_router_id_zebra_bump(vrf_id_t vrf_id
, const struct prefix
*router_id
)
242 struct listnode
*node
, *nnode
;
245 if (vrf_id
== VRF_DEFAULT
) {
246 /* Router-id change for default VRF has to also update all
248 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
249 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
252 bgp
->router_id_zebra
= router_id
->u
.prefix4
;
253 if (!bgp
->router_id_static
.s_addr
)
254 bgp_router_id_set(bgp
, &router_id
->u
.prefix4
);
257 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
259 bgp
->router_id_zebra
= router_id
->u
.prefix4
;
261 if (!bgp
->router_id_static
.s_addr
)
262 bgp_router_id_set(bgp
, &router_id
->u
.prefix4
);
267 int bgp_router_id_static_set(struct bgp
*bgp
, struct in_addr id
)
269 bgp
->router_id_static
= id
;
270 bgp_router_id_set(bgp
, id
.s_addr
? &id
: &bgp
->router_id_zebra
);
274 /* BGP's cluster-id control. */
275 int bgp_cluster_id_set(struct bgp
*bgp
, struct in_addr
*cluster_id
)
278 struct listnode
*node
, *nnode
;
280 if (bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
)
281 && IPV4_ADDR_SAME(&bgp
->cluster_id
, cluster_id
))
284 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
285 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
287 /* Clear all IBGP peer. */
288 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
289 if (peer
->sort
!= BGP_PEER_IBGP
)
292 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
293 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
294 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
295 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
301 int bgp_cluster_id_unset(struct bgp
*bgp
)
304 struct listnode
*node
, *nnode
;
306 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
309 bgp
->cluster_id
.s_addr
= 0;
310 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
312 /* Clear all IBGP peer. */
313 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
314 if (peer
->sort
!= BGP_PEER_IBGP
)
317 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
318 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
319 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
320 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
326 /* time_t value that is monotonicly increasing
327 * and uneffected by adjustments to system clock
329 time_t bgp_clock(void)
337 /* BGP timer configuration. */
338 int bgp_timers_set(struct bgp
*bgp
, u_int32_t keepalive
, u_int32_t holdtime
)
340 bgp
->default_keepalive
=
341 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
342 bgp
->default_holdtime
= holdtime
;
347 int bgp_timers_unset(struct bgp
*bgp
)
349 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
350 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
355 /* BGP confederation configuration. */
356 int bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
359 struct listnode
*node
, *nnode
;
363 return BGP_ERR_INVALID_AS
;
365 /* Remember - were we doing confederation before? */
366 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
368 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
370 /* If we were doing confederation already, this is just an external
371 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
372 were not doing confederation before, reset all EBGP sessions. */
373 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
374 /* We're looking for peers who's AS is not local or part of our
376 if (already_confed
) {
377 if (peer_sort(peer
) == BGP_PEER_EBGP
) {
379 if (BGP_IS_VALID_STATE_FOR_NOTIF(
382 PEER_DOWN_CONFED_ID_CHANGE
;
384 peer
, BGP_NOTIFY_CEASE
,
385 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
387 bgp_session_reset_safe(peer
, &nnode
);
390 /* Not doign confederation before, so reset every
393 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
394 /* Reset the local_as to be our EBGP one */
395 if (peer_sort(peer
) == BGP_PEER_EBGP
)
397 if (BGP_IS_VALID_STATE_FOR_NOTIF(
400 PEER_DOWN_CONFED_ID_CHANGE
;
402 peer
, BGP_NOTIFY_CEASE
,
403 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
405 bgp_session_reset_safe(peer
, &nnode
);
412 int bgp_confederation_id_unset(struct bgp
*bgp
)
415 struct listnode
*node
, *nnode
;
418 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
420 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
421 /* We're looking for peers who's AS is not local */
422 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
423 peer
->local_as
= bgp
->as
;
424 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
425 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
426 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
427 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
431 bgp_session_reset_safe(peer
, &nnode
);
437 /* Is an AS part of the confed or not? */
438 int bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
445 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
446 if (bgp
->confed_peers
[i
] == as
)
452 /* Add an AS to the confederation set. */
453 int bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
456 struct listnode
*node
, *nnode
;
459 return BGP_ERR_INVALID_BGP
;
462 return BGP_ERR_INVALID_AS
;
464 if (bgp_confederation_peers_check(bgp
, as
))
467 if (bgp
->confed_peers
)
469 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
470 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
473 XMALLOC(MTYPE_BGP_CONFED_LIST
,
474 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
476 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
477 bgp
->confed_peers_cnt
++;
479 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
480 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
481 if (peer
->as
== as
) {
482 peer
->local_as
= bgp
->as
;
483 if (BGP_IS_VALID_STATE_FOR_NOTIF(
486 PEER_DOWN_CONFED_PEER_CHANGE
;
488 peer
, BGP_NOTIFY_CEASE
,
489 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
491 bgp_session_reset_safe(peer
, &nnode
);
498 /* Delete an AS from the confederation set. */
499 int bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
504 struct listnode
*node
, *nnode
;
509 if (!bgp_confederation_peers_check(bgp
, as
))
512 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
513 if (bgp
->confed_peers
[i
] == as
)
514 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
515 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
517 bgp
->confed_peers_cnt
--;
519 if (bgp
->confed_peers_cnt
== 0) {
520 if (bgp
->confed_peers
)
521 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
522 bgp
->confed_peers
= NULL
;
525 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
526 bgp
->confed_peers_cnt
* sizeof(as_t
));
528 /* Now reset any peer who's remote AS has just been removed from the
530 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
531 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
532 if (peer
->as
== as
) {
533 peer
->local_as
= bgp
->confed_id
;
534 if (BGP_IS_VALID_STATE_FOR_NOTIF(
537 PEER_DOWN_CONFED_PEER_CHANGE
;
539 peer
, BGP_NOTIFY_CEASE
,
540 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
542 bgp_session_reset_safe(peer
, &nnode
);
550 /* Local preference configuration. */
551 int bgp_default_local_preference_set(struct bgp
*bgp
, u_int32_t local_pref
)
556 bgp
->default_local_pref
= local_pref
;
561 int bgp_default_local_preference_unset(struct bgp
*bgp
)
566 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
571 /* Local preference configuration. */
572 int bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
,
573 u_int32_t queue_size
)
578 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
583 int bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
587 bgp
->default_subgroup_pkt_queue_max
=
588 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
593 /* Listen limit configuration. */
594 int bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
599 bgp
->dynamic_neighbors_limit
= listen_limit
;
604 int bgp_listen_limit_unset(struct bgp
*bgp
)
609 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
614 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
615 afi_t
*afi
, safi_t
*safi
)
617 /* Map from IANA values to internal values, return error if
618 * values are unrecognized.
620 *afi
= afi_iana2int(pkt_afi
);
621 *safi
= safi_iana2int(pkt_safi
);
622 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
628 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
629 iana_safi_t
*pkt_safi
)
631 /* Map from internal values to IANA values, return error if
632 * internal values are bad (unexpected).
634 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
636 *pkt_afi
= afi_int2iana(afi
);
637 *pkt_safi
= safi_int2iana(safi
);
641 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
649 afid
= afindex(afi
, safi
);
650 if (afid
>= BGP_AF_MAX
)
653 assert(peer
->peer_af_array
[afid
] == NULL
);
655 /* Allocate new peer af */
656 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
659 zlog_err("Could not create af structure for peer %s",
664 peer
->peer_af_array
[afid
] = af
;
673 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
680 afid
= afindex(afi
, safi
);
681 if (afid
>= BGP_AF_MAX
)
684 return peer
->peer_af_array
[afid
];
687 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
695 afid
= afindex(afi
, safi
);
696 if (afid
>= BGP_AF_MAX
)
699 af
= peer
->peer_af_array
[afid
];
703 bgp_stop_announce_route_timer(af
);
705 if (PAF_SUBGRP(af
)) {
706 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
707 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
708 af
->subgroup
->update_group
->id
,
709 af
->subgroup
->id
, peer
->host
);
712 update_subgroup_remove_peer(af
->subgroup
, af
);
714 peer
->peer_af_array
[afid
] = NULL
;
715 XFREE(MTYPE_BGP_PEER_AF
, af
);
719 /* Peer comparison function for sorting. */
720 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
722 if (p1
->group
&& !p2
->group
)
725 if (!p1
->group
&& p2
->group
)
728 if (p1
->group
== p2
->group
) {
729 if (p1
->conf_if
&& !p2
->conf_if
)
732 if (!p1
->conf_if
&& p2
->conf_if
)
735 if (p1
->conf_if
&& p2
->conf_if
)
736 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
738 return strcmp(p1
->group
->name
, p2
->group
->name
);
740 return sockunion_cmp(&p1
->su
, &p2
->su
);
743 static unsigned int peer_hash_key_make(void *p
)
745 struct peer
*peer
= p
;
746 return sockunion_hash(&peer
->su
);
749 static int peer_hash_same(const void *p1
, const void *p2
)
751 const struct peer
*peer1
= p1
;
752 const struct peer
*peer2
= p2
;
753 return (sockunion_same(&peer1
->su
, &peer2
->su
)
754 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
755 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
758 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
,
761 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
764 /* Return true if flag is set for the peer but not the peer-group */
765 static int peergroup_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
,
768 struct peer
*g_peer
= NULL
;
770 if (peer_af_flag_check(peer
, afi
, safi
, flag
)) {
771 if (peer_group_active(peer
)) {
772 g_peer
= peer
->group
->conf
;
774 /* If this flag is not set for the peer's peer-group
775 * then return true */
776 if (!peer_af_flag_check(g_peer
, afi
, safi
, flag
)) {
781 /* peer is not in a peer-group but the flag is set to return
791 /* Reset all address family specific configuration. */
792 static void peer_af_flag_reset(struct peer
*peer
, afi_t afi
, safi_t safi
)
795 struct bgp_filter
*filter
;
796 char orf_name
[BUFSIZ
];
798 filter
= &peer
->filter
[afi
][safi
];
800 /* Clear neighbor filter and route-map */
801 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
802 if (filter
->dlist
[i
].name
) {
803 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
804 filter
->dlist
[i
].name
= NULL
;
806 if (filter
->plist
[i
].name
) {
807 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
808 filter
->plist
[i
].name
= NULL
;
810 if (filter
->aslist
[i
].name
) {
811 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
812 filter
->aslist
[i
].name
= NULL
;
815 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
816 if (filter
->map
[i
].name
) {
817 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
818 filter
->map
[i
].name
= NULL
;
822 /* Clear unsuppress map. */
823 if (filter
->usmap
.name
)
824 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
825 filter
->usmap
.name
= NULL
;
826 filter
->usmap
.map
= NULL
;
828 /* Clear neighbor's all address family flags. */
829 peer
->af_flags
[afi
][safi
] = 0;
831 /* Clear neighbor's all address family sflags. */
832 peer
->af_sflags
[afi
][safi
] = 0;
834 /* Clear neighbor's all address family capabilities. */
835 peer
->af_cap
[afi
][safi
] = 0;
838 peer
->orf_plist
[afi
][safi
] = NULL
;
839 sprintf(orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
840 prefix_bgp_orf_remove_all(afi
, orf_name
);
842 /* Set default neighbor send-community. */
843 if (!bgp_option_check(BGP_OPT_CONFIG_CISCO
)) {
844 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
845 SET_FLAG(peer
->af_flags
[afi
][safi
],
846 PEER_FLAG_SEND_EXT_COMMUNITY
);
847 SET_FLAG(peer
->af_flags
[afi
][safi
],
848 PEER_FLAG_SEND_LARGE_COMMUNITY
);
851 /* Clear neighbor default_originate_rmap */
852 if (peer
->default_rmap
[afi
][safi
].name
)
853 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
854 peer
->default_rmap
[afi
][safi
].name
= NULL
;
855 peer
->default_rmap
[afi
][safi
].map
= NULL
;
857 /* Clear neighbor maximum-prefix */
858 peer
->pmax
[afi
][safi
] = 0;
859 peer
->pmax_threshold
[afi
][safi
] = MAXIMUM_PREFIX_THRESHOLD_DEFAULT
;
862 /* peer global config reset */
863 static void peer_global_config_reset(struct peer
*peer
)
868 peer
->change_local_as
= 0;
869 peer
->ttl
= (peer_sort(peer
) == BGP_PEER_IBGP
? MAXTTL
: 1);
870 if (peer
->update_source
) {
871 sockunion_free(peer
->update_source
);
872 peer
->update_source
= NULL
;
874 if (peer
->update_if
) {
875 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
876 peer
->update_if
= NULL
;
879 if (peer_sort(peer
) == BGP_PEER_IBGP
)
880 peer
->v_routeadv
= BGP_DEFAULT_IBGP_ROUTEADV
;
882 peer
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
884 /* This is a per-peer specific flag and so we must preserve it */
885 v6only
= CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
);
890 SET_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
);
896 peer
->v_connect
= BGP_DEFAULT_CONNECT_RETRY
;
898 /* Reset some other configs back to defaults. */
899 peer
->v_start
= BGP_INIT_START_TIMER
;
900 peer
->password
= NULL
;
901 peer
->local_id
= peer
->bgp
->router_id
;
902 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
903 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
905 bfd_info_free(&(peer
->bfd_info
));
907 /* Set back the CONFIG_NODE flag. */
908 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
911 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
912 static bgp_peer_sort_t
peer_calc_sort(struct peer
*peer
)
919 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
920 if (peer
->as_type
== AS_INTERNAL
)
921 return BGP_PEER_IBGP
;
923 else if (peer
->as_type
== AS_EXTERNAL
)
924 return BGP_PEER_EBGP
;
926 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
)
927 return (bgp
->as
== peer
->as
? BGP_PEER_IBGP
932 peer1
= listnode_head(peer
->group
->peer
);
937 return BGP_PEER_INTERNAL
;
941 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
942 if (peer
->local_as
== 0)
943 return BGP_PEER_INTERNAL
;
945 if (peer
->local_as
== peer
->as
) {
946 if (bgp
->as
== bgp
->confed_id
) {
947 if (peer
->local_as
== bgp
->as
)
948 return BGP_PEER_IBGP
;
950 return BGP_PEER_EBGP
;
952 if (peer
->local_as
== bgp
->confed_id
)
953 return BGP_PEER_EBGP
;
955 return BGP_PEER_IBGP
;
959 if (bgp_confederation_peers_check(bgp
, peer
->as
))
960 return BGP_PEER_CONFED
;
962 return BGP_PEER_EBGP
;
964 if (peer
->as_type
!= AS_SPECIFIED
)
965 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
968 return (peer
->local_as
== 0
970 : peer
->local_as
== peer
->as
? BGP_PEER_IBGP
975 /* Calculate and cache the peer "sort" */
976 bgp_peer_sort_t
peer_sort(struct peer
*peer
)
978 peer
->sort
= peer_calc_sort(peer
);
982 static void peer_free(struct peer
*peer
)
984 assert(peer
->status
== Deleted
);
988 /* this /ought/ to have been done already through bgp_stop earlier,
989 * but just to be sure..
992 BGP_READ_OFF(peer
->t_read
);
993 BGP_WRITE_OFF(peer
->t_write
);
994 BGP_EVENT_FLUSH(peer
);
996 /* Free connected nexthop, if present */
997 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
998 && !peer_dynamic_neighbor(peer
))
999 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1002 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1005 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1009 /* Free allocated host character. */
1011 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1015 if (peer
->domainname
) {
1016 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1017 peer
->domainname
= NULL
;
1021 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1022 peer
->ifname
= NULL
;
1025 /* Update source configuration. */
1026 if (peer
->update_source
) {
1027 sockunion_free(peer
->update_source
);
1028 peer
->update_source
= NULL
;
1031 if (peer
->update_if
) {
1032 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1033 peer
->update_if
= NULL
;
1036 if (peer
->notify
.data
)
1037 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1038 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1040 if (peer
->clear_node_queue
) {
1041 work_queue_free(peer
->clear_node_queue
);
1042 peer
->clear_node_queue
= NULL
;
1045 bgp_sync_delete(peer
);
1047 if (peer
->conf_if
) {
1048 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1049 peer
->conf_if
= NULL
;
1052 bfd_info_free(&(peer
->bfd_info
));
1054 bgp_unlock(peer
->bgp
);
1056 memset(peer
, 0, sizeof(struct peer
));
1058 XFREE(MTYPE_BGP_PEER
, peer
);
1061 /* increase reference count on a struct peer */
1062 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1064 assert(peer
&& (peer
->lock
>= 0));
1067 zlog_debug("%s peer_lock %p %d", name
, peer
, peer
->lock
);
1075 /* decrease reference count on a struct peer
1076 * struct peer is freed and NULL returned if last reference
1078 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1080 assert(peer
&& (peer
->lock
> 0));
1083 zlog_debug("%s peer_unlock %p %d", name
, peer
, peer
->lock
);
1088 if (peer
->lock
== 0) {
1096 /* Allocate new peer object, implicitely locked. */
1097 struct peer
*peer_new(struct bgp
*bgp
)
1104 /* bgp argument is absolutely required */
1109 /* Allocate new peer. */
1110 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1112 /* Set default value. */
1114 peer
->v_start
= BGP_INIT_START_TIMER
;
1115 peer
->v_connect
= BGP_DEFAULT_CONNECT_RETRY
;
1116 peer
->status
= Idle
;
1117 peer
->ostatus
= Idle
;
1118 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1119 peer
->bgp
= bgp_lock(bgp
);
1120 peer
= peer_lock(peer
); /* initial reference */
1121 peer
->password
= NULL
;
1123 /* Set default flags. */
1124 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1125 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
1126 if (!bgp_option_check(BGP_OPT_CONFIG_CISCO
)) {
1127 SET_FLAG(peer
->af_flags
[afi
][safi
],
1128 PEER_FLAG_SEND_COMMUNITY
);
1129 SET_FLAG(peer
->af_flags
[afi
][safi
],
1130 PEER_FLAG_SEND_EXT_COMMUNITY
);
1131 SET_FLAG(peer
->af_flags
[afi
][safi
],
1132 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1134 peer
->orf_plist
[afi
][safi
] = NULL
;
1136 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1138 /* Create buffers. */
1139 peer
->ibuf
= stream_new(BGP_MAX_PACKET_SIZE
);
1140 peer
->obuf
= stream_fifo_new();
1142 /* We use a larger buffer for peer->work in the event that:
1143 * - We RX a BGP_UPDATE where the attributes alone are just
1144 * under BGP_MAX_PACKET_SIZE
1145 * - The user configures an outbound route-map that does many as-path
1146 * prepends or adds many communities. At most they can have
1148 * args in a route-map so there is a finite limit on how large they
1150 * make the attributes.
1152 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1154 * checking for every single attribute as we construct an UPDATE.
1157 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1158 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1161 bgp_sync_init(peer
);
1163 /* Get service port number. */
1164 sp
= getservbyname("bgp", "tcp");
1165 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1167 QOBJ_REG(peer
, peer
);
1172 * This function is invoked when a duplicate peer structure associated with
1173 * a neighbor is being deleted. If this about-to-be-deleted structure is
1174 * the one with all the config, then we have to copy over the info.
1176 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1178 struct peer_af
*paf
;
1186 /* The following function is used by both peer group config copy to
1187 * individual peer and when we transfer config
1189 if (peer_src
->change_local_as
)
1190 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1192 /* peer flags apply */
1193 peer_dst
->flags
= peer_src
->flags
;
1194 peer_dst
->cap
= peer_src
->cap
;
1195 peer_dst
->config
= peer_src
->config
;
1197 peer_dst
->local_as
= peer_src
->local_as
;
1198 peer_dst
->ifindex
= peer_src
->ifindex
;
1199 peer_dst
->port
= peer_src
->port
;
1200 peer_sort(peer_dst
);
1201 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1204 peer_dst
->holdtime
= peer_src
->holdtime
;
1205 peer_dst
->keepalive
= peer_src
->keepalive
;
1206 peer_dst
->connect
= peer_src
->connect
;
1207 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1208 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1209 peer_dst
->routeadv
= peer_src
->routeadv
;
1210 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1212 /* password apply */
1213 if (peer_src
->password
&& !peer_dst
->password
)
1214 peer_dst
->password
=
1215 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1217 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1218 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
1219 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1220 peer_dst
->af_flags
[afi
][safi
] =
1221 peer_src
->af_flags
[afi
][safi
];
1222 peer_dst
->allowas_in
[afi
][safi
] =
1223 peer_src
->allowas_in
[afi
][safi
];
1224 peer_dst
->weight
[afi
][safi
] =
1225 peer_src
->weight
[afi
][safi
];
1228 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1229 paf
= peer_src
->peer_af_array
[afidx
];
1231 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1234 /* update-source apply */
1235 if (peer_src
->update_source
) {
1236 if (peer_dst
->update_source
)
1237 sockunion_free(peer_dst
->update_source
);
1238 if (peer_dst
->update_if
) {
1239 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1240 peer_dst
->update_if
= NULL
;
1242 peer_dst
->update_source
=
1243 sockunion_dup(peer_src
->update_source
);
1244 } else if (peer_src
->update_if
) {
1245 if (peer_dst
->update_if
)
1246 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1247 if (peer_dst
->update_source
) {
1248 sockunion_free(peer_dst
->update_source
);
1249 peer_dst
->update_source
= NULL
;
1251 peer_dst
->update_if
=
1252 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1255 if (peer_src
->ifname
) {
1256 if (peer_dst
->ifname
)
1257 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1260 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1264 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1265 struct interface
*ifp
)
1267 struct connected
*ifc
;
1270 struct listnode
*node
;
1272 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1273 * IPv4 address of the other end.
1275 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1276 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1277 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
1278 if (p
.prefixlen
== 30) {
1279 peer
->su
.sa
.sa_family
= AF_INET
;
1280 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1282 peer
->su
.sin
.sin_addr
.s_addr
=
1284 else if (addr
% 4 == 2)
1285 peer
->su
.sin
.sin_addr
.s_addr
=
1287 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1288 peer
->su
.sin
.sin_len
=
1289 sizeof(struct sockaddr_in
);
1290 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1292 } else if (p
.prefixlen
== 31) {
1293 peer
->su
.sa
.sa_family
= AF_INET
;
1294 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1296 peer
->su
.sin
.sin_addr
.s_addr
=
1299 peer
->su
.sin
.sin_addr
.s_addr
=
1301 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1302 peer
->su
.sin
.sin_len
=
1303 sizeof(struct sockaddr_in
);
1304 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1306 } else if (bgp_debug_neighbor_events(peer
))
1308 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1316 static int bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1317 struct interface
*ifp
)
1319 struct nbr_connected
*ifc_nbr
;
1321 /* Have we learnt the peer's IPv6 link-local address? */
1322 if (ifp
->nbr_connected
1323 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1324 peer
->su
.sa
.sa_family
= AF_INET6
;
1325 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1326 sizeof(struct in6_addr
));
1328 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1330 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1338 * Set or reset the peer address socketunion structure based on the
1339 * learnt/derived peer address. If the address has changed, update the
1340 * password on the listen socket, if needed.
1342 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1344 struct interface
*ifp
;
1346 int peer_addr_updated
= 0;
1351 prev_family
= peer
->su
.sa
.sa_family
;
1352 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1354 /* If BGP unnumbered is not "v6only", we first see if we can
1356 * peer's IPv4 address.
1358 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1360 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1362 /* If "v6only" or we can't derive peer's IPv4 address, see if
1364 * learnt the peer's IPv6 link-local address. This is from the
1366 * IPv6 address in router advertisement.
1368 if (!peer_addr_updated
)
1370 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1372 /* If we could derive the peer address, we may need to install the
1374 * configured for the peer, if any, on the listen socket. Otherwise,
1376 * that peer's address is not available and uninstall the password, if
1379 if (peer_addr_updated
) {
1380 if (peer
->password
&& prev_family
== AF_UNSPEC
)
1383 if (peer
->password
&& prev_family
!= AF_UNSPEC
)
1384 bgp_md5_unset(peer
);
1385 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1386 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1389 /* Since our su changed we need to del/add peer to the peerhash */
1390 hash_release(peer
->bgp
->peerhash
, peer
);
1391 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
1394 static void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
,
1397 struct bgp_node
*rn
, *nrn
;
1399 for (rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn
;
1400 rn
= bgp_route_next(rn
)) {
1401 if (rn
->info
!= NULL
) {
1402 /* Special handling for 2-level routing
1404 if (safi
== SAFI_MPLS_VPN
1405 || safi
== SAFI_ENCAP
1406 || safi
== SAFI_EVPN
) {
1407 for (nrn
= bgp_table_top((
1411 nrn
= bgp_route_next(nrn
))
1412 bgp_process(bgp
, nrn
,
1415 bgp_process(bgp
, rn
, afi
, safi
);
1420 /* Force a bestpath recalculation for all prefixes. This is used
1421 * when 'bgp bestpath' commands are entered.
1423 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1428 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1429 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
1430 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1435 /* Create new BGP peer. */
1436 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1437 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1438 int as_type
, afi_t afi
, safi_t safi
,
1439 struct peer_group
*group
)
1443 char buf
[SU_ADDRSTRLEN
];
1445 peer
= peer_new(bgp
);
1447 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1448 bgp_peer_conf_if_to_su_update(peer
);
1450 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1451 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1454 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1456 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1457 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1459 peer
->local_as
= local_as
;
1460 peer
->as
= remote_as
;
1461 peer
->as_type
= as_type
;
1462 peer
->local_id
= bgp
->router_id
;
1463 peer
->v_holdtime
= bgp
->default_holdtime
;
1464 peer
->v_keepalive
= bgp
->default_keepalive
;
1465 if (peer_sort(peer
) == BGP_PEER_IBGP
)
1466 peer
->v_routeadv
= BGP_DEFAULT_IBGP_ROUTEADV
;
1468 peer
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
1470 peer
= peer_lock(peer
); /* bgp peer list reference */
1471 peer
->group
= group
;
1472 listnode_add_sort(bgp
->peer
, peer
);
1473 hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1475 active
= peer_active(peer
);
1477 /* Last read and reset time set */
1478 peer
->readtime
= peer
->resettime
= bgp_clock();
1480 /* Default TTL set. */
1481 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: 1;
1483 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1486 peer
->afc
[afi
][safi
] = 1;
1487 peer_af_create(peer
, afi
, safi
);
1490 /* Set up peer's events and timers. */
1491 if (!active
&& peer_active(peer
))
1492 bgp_timer_set(peer
);
1497 /* Make accept BGP peer. This function is only called from the test code */
1498 struct peer
*peer_create_accept(struct bgp
*bgp
)
1502 peer
= peer_new(bgp
);
1504 peer
= peer_lock(peer
); /* bgp peer list reference */
1505 listnode_add_sort(bgp
->peer
, peer
);
1511 * Return true if we have a peer configured to use this afi/safi
1513 int bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1515 struct listnode
*node
;
1518 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1519 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1522 if (peer
->afc
[afi
][safi
])
1529 /* Change peer's AS number. */
1530 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
)
1532 bgp_peer_sort_t type
;
1536 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1537 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1538 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1539 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1540 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1542 bgp_session_reset(peer
);
1544 type
= peer_sort(peer
);
1546 peer
->as_type
= as_specified
;
1548 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1549 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1550 && peer
->bgp
->as
!= as
)
1551 peer
->local_as
= peer
->bgp
->confed_id
;
1553 peer
->local_as
= peer
->bgp
->as
;
1555 /* Advertisement-interval reset */
1558 conf
= peer
->group
->conf
;
1560 if (conf
&& CHECK_FLAG(conf
->config
, PEER_CONFIG_ROUTEADV
)) {
1561 peer
->v_routeadv
= conf
->routeadv
;
1563 /* Only go back to the default advertisement-interval if the user had
1565 * already configured it */
1566 else if (!CHECK_FLAG(peer
->config
, PEER_CONFIG_ROUTEADV
)) {
1567 if (peer_sort(peer
) == BGP_PEER_IBGP
)
1568 peer
->v_routeadv
= BGP_DEFAULT_IBGP_ROUTEADV
;
1570 peer
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
1573 if (peer_sort(peer
) == BGP_PEER_IBGP
)
1575 else if (type
== BGP_PEER_IBGP
)
1578 /* reflector-client reset */
1579 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
1580 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1581 PEER_FLAG_REFLECTOR_CLIENT
);
1582 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1583 PEER_FLAG_REFLECTOR_CLIENT
);
1584 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1585 PEER_FLAG_REFLECTOR_CLIENT
);
1586 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1587 PEER_FLAG_REFLECTOR_CLIENT
);
1588 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1589 PEER_FLAG_REFLECTOR_CLIENT
);
1590 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1591 PEER_FLAG_REFLECTOR_CLIENT
);
1592 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1593 PEER_FLAG_REFLECTOR_CLIENT
);
1594 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1595 PEER_FLAG_REFLECTOR_CLIENT
);
1596 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1597 PEER_FLAG_REFLECTOR_CLIENT
);
1598 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1599 PEER_FLAG_REFLECTOR_CLIENT
);
1600 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1601 PEER_FLAG_REFLECTOR_CLIENT
);
1604 /* local-as reset */
1605 if (peer_sort(peer
) != BGP_PEER_EBGP
) {
1606 peer
->change_local_as
= 0;
1607 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
1608 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
1612 /* If peer does not exist, create new one. If peer already exists,
1613 set AS number to the peer. */
1614 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1615 as_t
*as
, int as_type
, afi_t afi
, safi_t safi
)
1621 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1623 peer
= peer_lookup(bgp
, su
);
1626 /* Not allowed for a dynamic peer. */
1627 if (peer_dynamic_neighbor(peer
)) {
1629 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1632 /* When this peer is a member of peer-group. */
1634 if (peer
->group
->conf
->as
) {
1635 /* Return peer group's AS number. */
1636 *as
= peer
->group
->conf
->as
;
1637 return BGP_ERR_PEER_GROUP_MEMBER
;
1639 if (peer_sort(peer
->group
->conf
) == BGP_PEER_IBGP
) {
1640 if ((as_type
!= AS_INTERNAL
)
1641 && (bgp
->as
!= *as
)) {
1643 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1646 if ((as_type
!= AS_EXTERNAL
)
1647 && (bgp
->as
== *as
)) {
1649 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1654 /* Existing peer's AS number change. */
1655 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
1656 || (peer
->as_type
!= as_type
))
1657 peer_as_change(peer
, *as
, as_type
);
1660 return BGP_ERR_NO_INTERFACE_CONFIG
;
1662 /* If the peer is not part of our confederation, and its not an
1663 iBGP peer then spoof the source AS */
1664 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
1665 && !bgp_confederation_peers_check(bgp
, *as
)
1667 local_as
= bgp
->confed_id
;
1671 /* If this is IPv4 unicast configuration and "no bgp default
1672 ipv4-unicast" is specified. */
1674 if (bgp_flag_check(bgp
, BGP_FLAG_NO_DEFAULT_IPV4
)
1675 && afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
1676 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, 0,
1679 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
,
1686 static int non_peergroup_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
1690 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1691 zlog_err("%s was called for peer-group %s", __func__
,
1696 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
1698 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
1699 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
1700 return BGP_ERR_PEER_SAFI_CONFLICT
;
1702 /* Nothing to do if we've already activated this peer */
1703 if (peer
->afc
[afi
][safi
])
1706 if (peer_af_create(peer
, afi
, safi
) == NULL
)
1709 active
= peer_active(peer
);
1710 peer
->afc
[afi
][safi
] = 1;
1712 if (!active
&& peer_active(peer
)) {
1713 bgp_timer_set(peer
);
1715 if (peer
->status
== Established
) {
1716 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
1717 peer
->afc_adv
[afi
][safi
] = 1;
1718 bgp_capability_send(peer
, afi
, safi
,
1720 CAPABILITY_ACTION_SET
);
1721 if (peer
->afc_recv
[afi
][safi
]) {
1722 peer
->afc_nego
[afi
][safi
] = 1;
1723 bgp_announce_route(peer
, afi
, safi
);
1726 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
1727 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1728 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1736 /* Activate the peer or peer group for specified AFI and SAFI. */
1737 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
1740 struct peer_group
*group
;
1741 struct listnode
*node
, *nnode
;
1742 struct peer
*tmp_peer
;
1745 /* Nothing to do if we've already activated this peer */
1746 if (peer
->afc
[afi
][safi
])
1751 /* This is a peer-group so activate all of the members of the
1752 * peer-group as well */
1753 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1755 /* Do not activate a peer for both SAFI_UNICAST and
1756 * SAFI_LABELED_UNICAST */
1757 if ((safi
== SAFI_UNICAST
1758 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
1759 || (safi
== SAFI_LABELED_UNICAST
1760 && peer
->afc
[afi
][SAFI_UNICAST
]))
1761 return BGP_ERR_PEER_SAFI_CONFLICT
;
1763 peer
->afc
[afi
][safi
] = 1;
1764 group
= peer
->group
;
1766 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
1767 ret
|= non_peergroup_activate_af(tmp_peer
, afi
, safi
);
1770 ret
|= non_peergroup_activate_af(peer
, afi
, safi
);
1773 /* If this is the first peer to be activated for this afi/labeled-unicast
1774 * recalc bestpaths to trigger label allocation */
1775 if (safi
== SAFI_LABELED_UNICAST
&& !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
1777 if (BGP_DEBUG(zebra
, ZEBRA
))
1778 zlog_info("peer(s) are now active for labeled-unicast, allocate MPLS labels");
1780 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
1781 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
1787 static int non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
1790 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1791 zlog_err("%s was called for peer-group %s", __func__
,
1796 /* Nothing to do if we've already deactivated this peer */
1797 if (!peer
->afc
[afi
][safi
])
1800 /* De-activate the address family configuration. */
1801 peer
->afc
[afi
][safi
] = 0;
1803 if (peer_af_delete(peer
, afi
, safi
) != 0) {
1804 zlog_err("couldn't delete af structure for peer %s",
1809 if (peer
->status
== Established
) {
1810 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
1811 peer
->afc_adv
[afi
][safi
] = 0;
1812 peer
->afc_nego
[afi
][safi
] = 0;
1814 if (peer_active_nego(peer
)) {
1815 bgp_capability_send(peer
, afi
, safi
,
1817 CAPABILITY_ACTION_UNSET
);
1818 bgp_clear_route(peer
, afi
, safi
);
1819 peer
->pcount
[afi
][safi
] = 0;
1821 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
1822 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1823 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1826 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
1827 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1828 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1835 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
1838 struct peer_group
*group
;
1839 struct peer
*tmp_peer
;
1840 struct listnode
*node
, *nnode
;
1843 /* Nothing to do if we've already de-activated this peer */
1844 if (!peer
->afc
[afi
][safi
])
1847 /* This is a peer-group so de-activate all of the members of the
1848 * peer-group as well */
1849 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1850 peer
->afc
[afi
][safi
] = 0;
1851 group
= peer
->group
;
1853 if (peer_af_delete(peer
, afi
, safi
) != 0) {
1854 zlog_err("couldn't delete af structure for peer %s",
1858 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
1859 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
1862 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
1867 /* If this is the last peer to be deactivated for this afi/labeled-unicast
1868 * recalc bestpaths to trigger label deallocation */
1869 if (safi
== SAFI_LABELED_UNICAST
&&
1870 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] &&
1871 !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
1873 if (BGP_DEBUG(zebra
, ZEBRA
))
1874 zlog_info("peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
1876 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
1877 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
1882 int peer_afc_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int enable
)
1885 return peer_activate(peer
, afi
, safi
);
1887 return peer_deactivate(peer
, afi
, safi
);
1890 static void peer_nsf_stop(struct peer
*peer
)
1895 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1896 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1898 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1899 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
1900 peer
->nsf
[afi
][safi
] = 0;
1902 if (peer
->t_gr_restart
) {
1903 BGP_TIMER_OFF(peer
->t_gr_restart
);
1904 if (bgp_debug_neighbor_events(peer
))
1905 zlog_debug("%s graceful restart timer stopped",
1908 if (peer
->t_gr_stale
) {
1909 BGP_TIMER_OFF(peer
->t_gr_stale
);
1910 if (bgp_debug_neighbor_events(peer
))
1912 "%s graceful restart stalepath timer stopped",
1915 bgp_clear_route_all(peer
);
1918 /* Delete peer from confguration.
1920 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
1921 * it to "cool off" and refcounts to hit 0, at which state it is freed.
1923 * This function /should/ take care to be idempotent, to guard against
1924 * it being called multiple times through stray events that come in
1925 * that happen to result in this function being called again. That
1926 * said, getting here for a "Deleted" peer is a bug in the neighbour
1929 int peer_delete(struct peer
*peer
)
1935 struct bgp_filter
*filter
;
1936 struct listnode
*pn
;
1939 assert(peer
->status
!= Deleted
);
1942 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
1944 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
1945 peer_nsf_stop(peer
);
1947 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
1949 /* If this peer belongs to peer group, clear up the
1952 if (peer_dynamic_neighbor(peer
))
1953 peer_drop_dynamic_neighbor(peer
);
1955 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
1957 peer
); /* group->peer list reference */
1958 list_delete_node(peer
->group
->peer
, pn
);
1963 /* Withdraw all information from routing table. We can not use
1964 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
1965 * executed after peer structure is deleted.
1967 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
1969 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
1971 if (peer
->doppelganger
) {
1972 peer
->doppelganger
->doppelganger
= NULL
;
1973 peer
->doppelganger
= NULL
;
1976 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
1977 bgp_fsm_change_status(peer
, Deleted
);
1979 /* Remove from NHT */
1980 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1981 bgp_unlink_nexthop_by_peer(peer
);
1983 /* Password configuration */
1984 if (peer
->password
) {
1985 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
1986 peer
->password
= NULL
;
1988 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
1989 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
1990 bgp_md5_unset(peer
);
1993 bgp_timer_set(peer
); /* stops all timers for Deleted */
1995 /* Delete from all peer list. */
1996 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
1997 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
1998 peer_unlock(peer
); /* bgp peer list reference */
1999 list_delete_node(bgp
->peer
, pn
);
2000 hash_release(bgp
->peerhash
, peer
);
2005 stream_free(peer
->ibuf
);
2010 stream_fifo_free(peer
->obuf
);
2015 stream_free(peer
->work
);
2019 if (peer
->scratch
) {
2020 stream_free(peer
->scratch
);
2021 peer
->scratch
= NULL
;
2024 /* Local and remote addresses. */
2025 if (peer
->su_local
) {
2026 sockunion_free(peer
->su_local
);
2027 peer
->su_local
= NULL
;
2030 if (peer
->su_remote
) {
2031 sockunion_free(peer
->su_remote
);
2032 peer
->su_remote
= NULL
;
2035 /* Free filter related memory. */
2036 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2037 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
2038 filter
= &peer
->filter
[afi
][safi
];
2040 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2041 if (filter
->dlist
[i
].name
) {
2042 XFREE(MTYPE_BGP_FILTER_NAME
,
2043 filter
->dlist
[i
].name
);
2044 filter
->dlist
[i
].name
= NULL
;
2047 if (filter
->plist
[i
].name
) {
2048 XFREE(MTYPE_BGP_FILTER_NAME
,
2049 filter
->plist
[i
].name
);
2050 filter
->plist
[i
].name
= NULL
;
2053 if (filter
->aslist
[i
].name
) {
2054 XFREE(MTYPE_BGP_FILTER_NAME
,
2055 filter
->aslist
[i
].name
);
2056 filter
->aslist
[i
].name
= NULL
;
2060 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2061 if (filter
->map
[i
].name
) {
2062 XFREE(MTYPE_BGP_FILTER_NAME
,
2063 filter
->map
[i
].name
);
2064 filter
->map
[i
].name
= NULL
;
2068 if (filter
->usmap
.name
) {
2069 XFREE(MTYPE_BGP_FILTER_NAME
,
2070 filter
->usmap
.name
);
2071 filter
->usmap
.name
= NULL
;
2074 if (peer
->default_rmap
[afi
][safi
].name
) {
2075 XFREE(MTYPE_ROUTE_MAP_NAME
,
2076 peer
->default_rmap
[afi
][safi
].name
);
2077 peer
->default_rmap
[afi
][safi
].name
= NULL
;
2081 FOREACH_AFI_SAFI (afi
, safi
)
2082 peer_af_delete(peer
, afi
, safi
);
2084 if (peer
->hostname
) {
2085 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2086 peer
->hostname
= NULL
;
2089 if (peer
->domainname
) {
2090 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2091 peer
->domainname
= NULL
;
2094 peer_unlock(peer
); /* initial reference */
2099 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2101 return strcmp(g1
->name
, g2
->name
);
2104 /* Peer group cofiguration. */
2105 static struct peer_group
*peer_group_new(void)
2107 return (struct peer_group
*)XCALLOC(MTYPE_PEER_GROUP
,
2108 sizeof(struct peer_group
));
2111 static void peer_group_free(struct peer_group
*group
)
2113 XFREE(MTYPE_PEER_GROUP
, group
);
2116 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2118 struct peer_group
*group
;
2119 struct listnode
*node
, *nnode
;
2121 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2122 if (strcmp(group
->name
, name
) == 0)
2128 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2130 struct peer_group
*group
;
2133 group
= peer_group_lookup(bgp
, name
);
2137 group
= peer_group_new();
2140 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2141 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2142 group
->peer
= list_new();
2143 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2144 group
->listen_range
[afi
] = list_new();
2145 group
->conf
= peer_new(bgp
);
2146 if (!bgp_flag_check(bgp
, BGP_FLAG_NO_DEFAULT_IPV4
))
2147 group
->conf
->afc
[AFI_IP
][SAFI_UNICAST
] = 1;
2148 if (group
->conf
->host
)
2149 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2150 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2151 group
->conf
->group
= group
;
2152 group
->conf
->as
= 0;
2153 group
->conf
->ttl
= 1;
2154 group
->conf
->gtsm_hops
= 0;
2155 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2156 UNSET_FLAG(group
->conf
->config
, PEER_CONFIG_TIMER
);
2157 UNSET_FLAG(group
->conf
->config
, PEER_CONFIG_CONNECT
);
2158 group
->conf
->keepalive
= 0;
2159 group
->conf
->holdtime
= 0;
2160 group
->conf
->connect
= 0;
2161 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2162 listnode_add_sort(bgp
->group
, group
);
2167 static void peer_group2peer_config_copy(struct peer_group
*group
,
2177 peer
->as
= conf
->as
;
2180 if (conf
->change_local_as
)
2181 peer
->change_local_as
= conf
->change_local_as
;
2184 peer
->ttl
= conf
->ttl
;
2187 peer
->gtsm_hops
= conf
->gtsm_hops
;
2189 /* this flag is per-neighbor and so has to be preserved */
2190 v6only
= CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
);
2192 /* peer flags apply */
2193 peer
->flags
= conf
->flags
;
2196 SET_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
);
2198 /* peer config apply */
2199 peer
->config
= conf
->config
;
2201 /* peer timers apply */
2202 peer
->holdtime
= conf
->holdtime
;
2203 peer
->keepalive
= conf
->keepalive
;
2204 peer
->connect
= conf
->connect
;
2205 if (CHECK_FLAG(conf
->config
, PEER_CONFIG_CONNECT
))
2206 peer
->v_connect
= conf
->connect
;
2208 peer
->v_connect
= BGP_DEFAULT_CONNECT_RETRY
;
2210 /* advertisement-interval reset */
2211 if (CHECK_FLAG(conf
->config
, PEER_CONFIG_ROUTEADV
))
2212 peer
->v_routeadv
= conf
->routeadv
;
2213 else if (peer_sort(peer
) == BGP_PEER_IBGP
)
2214 peer
->v_routeadv
= BGP_DEFAULT_IBGP_ROUTEADV
;
2216 peer
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2218 /* password apply */
2219 if (conf
->password
&& !peer
->password
)
2220 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, conf
->password
);
2222 if (!BGP_PEER_SU_UNSPEC(peer
))
2225 /* update-source apply */
2226 if (conf
->update_source
) {
2227 if (peer
->update_source
)
2228 sockunion_free(peer
->update_source
);
2229 if (peer
->update_if
) {
2230 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2231 peer
->update_if
= NULL
;
2233 peer
->update_source
= sockunion_dup(conf
->update_source
);
2234 } else if (conf
->update_if
) {
2235 if (peer
->update_if
)
2236 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2237 if (peer
->update_source
) {
2238 sockunion_free(peer
->update_source
);
2239 peer
->update_source
= NULL
;
2242 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, conf
->update_if
);
2245 bgp_bfd_peer_group2peer_copy(conf
, peer
);
2248 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
2249 struct peer
*peer
, afi_t afi
,
2253 int out
= FILTER_OUT
;
2255 struct bgp_filter
*pfilter
;
2256 struct bgp_filter
*gfilter
;
2259 pfilter
= &peer
->filter
[afi
][safi
];
2260 gfilter
= &conf
->filter
[afi
][safi
];
2262 /* peer af_flags apply */
2263 peer
->af_flags
[afi
][safi
] = conf
->af_flags
[afi
][safi
];
2265 /* maximum-prefix */
2266 peer
->pmax
[afi
][safi
] = conf
->pmax
[afi
][safi
];
2267 peer
->pmax_threshold
[afi
][safi
] = conf
->pmax_threshold
[afi
][safi
];
2268 peer
->pmax_restart
[afi
][safi
] = conf
->pmax_restart
[afi
][safi
];
2271 peer
->allowas_in
[afi
][safi
] = conf
->allowas_in
[afi
][safi
];
2274 peer
->weight
[afi
][safi
] = conf
->weight
[afi
][safi
];
2276 /* default-originate route-map */
2277 if (conf
->default_rmap
[afi
][safi
].name
) {
2278 if (peer
->default_rmap
[afi
][safi
].name
)
2279 XFREE(MTYPE_BGP_FILTER_NAME
,
2280 peer
->default_rmap
[afi
][safi
].name
);
2281 peer
->default_rmap
[afi
][safi
].name
=
2282 XSTRDUP(MTYPE_BGP_FILTER_NAME
,
2283 conf
->default_rmap
[afi
][safi
].name
);
2284 peer
->default_rmap
[afi
][safi
].map
=
2285 conf
->default_rmap
[afi
][safi
].map
;
2288 /* inbound filter apply */
2289 if (gfilter
->dlist
[in
].name
&& !pfilter
->dlist
[in
].name
) {
2290 if (pfilter
->dlist
[in
].name
)
2291 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->dlist
[in
].name
);
2292 pfilter
->dlist
[in
].name
=
2293 XSTRDUP(MTYPE_BGP_FILTER_NAME
, gfilter
->dlist
[in
].name
);
2294 pfilter
->dlist
[in
].alist
= gfilter
->dlist
[in
].alist
;
2297 if (gfilter
->plist
[in
].name
&& !pfilter
->plist
[in
].name
) {
2298 if (pfilter
->plist
[in
].name
)
2299 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->plist
[in
].name
);
2300 pfilter
->plist
[in
].name
=
2301 XSTRDUP(MTYPE_BGP_FILTER_NAME
, gfilter
->plist
[in
].name
);
2302 pfilter
->plist
[in
].plist
= gfilter
->plist
[in
].plist
;
2305 if (gfilter
->aslist
[in
].name
&& !pfilter
->aslist
[in
].name
) {
2306 if (pfilter
->aslist
[in
].name
)
2307 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->aslist
[in
].name
);
2308 pfilter
->aslist
[in
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
,
2309 gfilter
->aslist
[in
].name
);
2310 pfilter
->aslist
[in
].aslist
= gfilter
->aslist
[in
].aslist
;
2313 if (gfilter
->map
[RMAP_IN
].name
&& !pfilter
->map
[RMAP_IN
].name
) {
2314 if (pfilter
->map
[RMAP_IN
].name
)
2315 XFREE(MTYPE_BGP_FILTER_NAME
,
2316 pfilter
->map
[RMAP_IN
].name
);
2317 pfilter
->map
[RMAP_IN
].name
= XSTRDUP(
2318 MTYPE_BGP_FILTER_NAME
, gfilter
->map
[RMAP_IN
].name
);
2319 pfilter
->map
[RMAP_IN
].map
= gfilter
->map
[RMAP_IN
].map
;
2322 /* outbound filter apply */
2323 if (gfilter
->dlist
[out
].name
) {
2324 if (pfilter
->dlist
[out
].name
)
2325 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->dlist
[out
].name
);
2326 pfilter
->dlist
[out
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
,
2327 gfilter
->dlist
[out
].name
);
2328 pfilter
->dlist
[out
].alist
= gfilter
->dlist
[out
].alist
;
2330 if (pfilter
->dlist
[out
].name
)
2331 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->dlist
[out
].name
);
2332 pfilter
->dlist
[out
].name
= NULL
;
2333 pfilter
->dlist
[out
].alist
= NULL
;
2336 if (gfilter
->plist
[out
].name
) {
2337 if (pfilter
->plist
[out
].name
)
2338 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->plist
[out
].name
);
2339 pfilter
->plist
[out
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
,
2340 gfilter
->plist
[out
].name
);
2341 pfilter
->plist
[out
].plist
= gfilter
->plist
[out
].plist
;
2343 if (pfilter
->plist
[out
].name
)
2344 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->plist
[out
].name
);
2345 pfilter
->plist
[out
].name
= NULL
;
2346 pfilter
->plist
[out
].plist
= NULL
;
2349 if (gfilter
->aslist
[out
].name
) {
2350 if (pfilter
->aslist
[out
].name
)
2351 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->aslist
[out
].name
);
2352 pfilter
->aslist
[out
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
,
2353 gfilter
->aslist
[out
].name
);
2354 pfilter
->aslist
[out
].aslist
= gfilter
->aslist
[out
].aslist
;
2356 if (pfilter
->aslist
[out
].name
)
2357 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->aslist
[out
].name
);
2358 pfilter
->aslist
[out
].name
= NULL
;
2359 pfilter
->aslist
[out
].aslist
= NULL
;
2362 if (gfilter
->map
[RMAP_OUT
].name
) {
2363 if (pfilter
->map
[RMAP_OUT
].name
)
2364 XFREE(MTYPE_BGP_FILTER_NAME
,
2365 pfilter
->map
[RMAP_OUT
].name
);
2366 pfilter
->map
[RMAP_OUT
].name
= XSTRDUP(
2367 MTYPE_BGP_FILTER_NAME
, gfilter
->map
[RMAP_OUT
].name
);
2368 pfilter
->map
[RMAP_OUT
].map
= gfilter
->map
[RMAP_OUT
].map
;
2370 if (pfilter
->map
[RMAP_OUT
].name
)
2371 XFREE(MTYPE_BGP_FILTER_NAME
,
2372 pfilter
->map
[RMAP_OUT
].name
);
2373 pfilter
->map
[RMAP_OUT
].name
= NULL
;
2374 pfilter
->map
[RMAP_OUT
].map
= NULL
;
2377 if (gfilter
->usmap
.name
) {
2378 if (pfilter
->usmap
.name
)
2379 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->usmap
.name
);
2380 pfilter
->usmap
.name
=
2381 XSTRDUP(MTYPE_BGP_FILTER_NAME
, gfilter
->usmap
.name
);
2382 pfilter
->usmap
.map
= gfilter
->usmap
.map
;
2384 if (pfilter
->usmap
.name
)
2385 XFREE(MTYPE_BGP_FILTER_NAME
, pfilter
->usmap
.name
);
2386 pfilter
->usmap
.name
= NULL
;
2387 pfilter
->usmap
.map
= NULL
;
2391 /* Peer group's remote AS configuration. */
2392 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2395 struct peer_group
*group
;
2397 struct listnode
*node
, *nnode
;
2399 group
= peer_group_lookup(bgp
, group_name
);
2403 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2407 /* When we setup peer-group AS number all peer group member's AS
2408 number must be updated to same number. */
2409 peer_as_change(group
->conf
, *as
, as_type
);
2411 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2412 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2413 || (peer
->as_type
!= as_type
))
2414 peer_as_change(peer
, *as
, as_type
);
2420 int peer_group_delete(struct peer_group
*group
)
2424 struct prefix
*prefix
;
2426 struct listnode
*node
, *nnode
;
2431 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2432 other
= peer
->doppelganger
;
2434 if (other
&& other
->status
!= Deleted
) {
2435 other
->group
= NULL
;
2439 list_delete_and_null(&group
->peer
);
2441 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2442 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2444 prefix_free(prefix
);
2446 list_delete_and_null(&group
->listen_range
[afi
]);
2449 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2452 bfd_info_free(&(group
->conf
->bfd_info
));
2454 group
->conf
->group
= NULL
;
2455 peer_delete(group
->conf
);
2457 /* Delete from all peer_group list. */
2458 listnode_delete(bgp
->group
, group
);
2460 peer_group_free(group
);
2465 int peer_group_remote_as_delete(struct peer_group
*group
)
2467 struct peer
*peer
, *other
;
2468 struct listnode
*node
, *nnode
;
2470 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2471 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2474 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2475 other
= peer
->doppelganger
;
2479 if (other
&& other
->status
!= Deleted
) {
2480 other
->group
= NULL
;
2484 list_delete_all_node(group
->peer
);
2486 group
->conf
->as
= 0;
2487 group
->conf
->as_type
= AS_UNSPECIFIED
;
2492 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2494 struct prefix
*prefix
;
2495 struct listnode
*node
, *nnode
;
2498 afi
= family2afi(range
->family
);
2500 /* Group needs remote AS configured. */
2501 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2502 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2504 /* Ensure no duplicates. Currently we don't care about overlaps. */
2505 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2506 if (prefix_same(range
, prefix
))
2510 prefix
= prefix_new();
2511 prefix_copy(prefix
, range
);
2512 listnode_add(group
->listen_range
[afi
], prefix
);
2516 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2518 struct prefix
*prefix
, prefix2
;
2519 struct listnode
*node
, *nnode
;
2522 char buf
[PREFIX2STR_BUFFER
];
2524 afi
= family2afi(range
->family
);
2526 /* Identify the listen range. */
2527 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2528 if (prefix_same(range
, prefix
))
2533 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
2535 prefix2str(prefix
, buf
, sizeof(buf
));
2537 /* Dispose off any dynamic neighbors that exist due to this listen range
2539 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2540 if (!peer_dynamic_neighbor(peer
))
2543 sockunion2hostprefix(&peer
->su
, &prefix2
);
2544 if (prefix_match(prefix
, &prefix2
)) {
2545 if (bgp_debug_neighbor_events(peer
))
2547 "Deleting dynamic neighbor %s group %s upon "
2548 "delete of listen range %s",
2549 peer
->host
, group
->name
, buf
);
2554 /* Get rid of the listen range */
2555 listnode_delete(group
->listen_range
[afi
], prefix
);
2560 /* Bind specified peer to peer group. */
2561 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
2562 struct peer_group
*group
, as_t
*as
)
2564 int first_member
= 0;
2567 int cap_enhe_preset
= 0;
2569 /* Lookup the peer. */
2571 peer
= peer_lookup(bgp
, su
);
2573 /* The peer exist, bind it to the peer-group */
2575 /* When the peer already belongs to a peer-group, check the
2577 if (peer_group_active(peer
)) {
2579 /* The peer is already bound to the peer-group,
2582 if (strcmp(peer
->group
->name
, group
->name
) == 0)
2585 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
2588 /* The peer has not specified a remote-as, inherit it from the
2590 if (peer
->as_type
== AS_UNSPECIFIED
) {
2591 peer
->as_type
= group
->conf
->as_type
;
2592 peer
->as
= group
->conf
->as
;
2595 if (!group
->conf
->as
) {
2596 if (peer_sort(group
->conf
) != BGP_PEER_INTERNAL
2597 && peer_sort(group
->conf
) != peer_sort(peer
)) {
2600 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2603 if (peer_sort(group
->conf
) == BGP_PEER_INTERNAL
)
2607 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2608 cap_enhe_preset
= 1;
2610 peer_group2peer_config_copy(group
, peer
);
2613 * Capability extended-nexthop is enabled for an interface
2615 * default. So, fix that up here.
2617 if (peer
->conf_if
&& cap_enhe_preset
)
2618 peer_flag_set(peer
, PEER_FLAG_CAPABILITY_ENHE
);
2620 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2621 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
2622 if (group
->conf
->afc
[afi
][safi
]) {
2623 peer
->afc
[afi
][safi
] = 1;
2625 if (peer_af_find(peer
, afi
, safi
)
2626 || peer_af_create(peer
, afi
,
2628 peer_group2peer_config_copy_af(
2629 group
, peer
, afi
, safi
);
2631 } else if (peer
->afc
[afi
][safi
])
2632 peer_deactivate(peer
, afi
, safi
);
2636 assert(group
&& peer
->group
== group
);
2638 struct listnode
*pn
;
2639 pn
= listnode_lookup(bgp
->peer
, peer
);
2640 list_delete_node(bgp
->peer
, pn
);
2641 peer
->group
= group
;
2642 listnode_add_sort(bgp
->peer
, peer
);
2644 peer
= peer_lock(peer
); /* group->peer list reference */
2645 listnode_add(group
->peer
, peer
);
2649 /* Advertisement-interval reset */
2650 if (!CHECK_FLAG(group
->conf
->config
,
2651 PEER_CONFIG_ROUTEADV
)) {
2652 if (peer_sort(group
->conf
) == BGP_PEER_IBGP
)
2653 group
->conf
->v_routeadv
=
2654 BGP_DEFAULT_IBGP_ROUTEADV
;
2656 group
->conf
->v_routeadv
=
2657 BGP_DEFAULT_EBGP_ROUTEADV
;
2660 /* ebgp-multihop reset */
2661 if (peer_sort(group
->conf
) == BGP_PEER_IBGP
)
2662 group
->conf
->ttl
= MAXTTL
;
2664 /* local-as reset */
2665 if (peer_sort(group
->conf
) != BGP_PEER_EBGP
) {
2666 group
->conf
->change_local_as
= 0;
2667 UNSET_FLAG(peer
->flags
,
2668 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
2669 UNSET_FLAG(peer
->flags
,
2670 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
2674 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2676 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
2677 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
2678 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2679 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2681 bgp_session_reset(peer
);
2685 /* Create a new peer. */
2687 if ((group
->conf
->as_type
== AS_SPECIFIED
)
2688 && (!group
->conf
->as
)) {
2689 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2692 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
2693 group
->conf
->as_type
, 0, 0, group
);
2695 peer
= peer_lock(peer
); /* group->peer list reference */
2696 listnode_add(group
->peer
, peer
);
2698 peer_group2peer_config_copy(group
, peer
);
2700 /* If the peer-group is active for this afi/safi then activate
2702 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2703 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
2704 if (group
->conf
->afc
[afi
][safi
]) {
2705 peer
->afc
[afi
][safi
] = 1;
2706 peer_af_create(peer
, afi
, safi
);
2707 peer_group2peer_config_copy_af(
2708 group
, peer
, afi
, safi
);
2709 } else if (peer
->afc
[afi
][safi
])
2710 peer_deactivate(peer
, afi
, safi
);
2712 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
2714 /* Set up peer's events and timers. */
2715 if (peer_active(peer
))
2716 bgp_timer_set(peer
);
2722 int peer_group_unbind(struct bgp
*bgp
, struct peer
*peer
,
2723 struct peer_group
*group
)
2729 if (group
!= peer
->group
)
2730 return BGP_ERR_PEER_GROUP_MISMATCH
;
2732 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2733 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
2734 if (peer
->afc
[afi
][safi
]) {
2735 peer
->afc
[afi
][safi
] = 0;
2736 peer_af_flag_reset(peer
, afi
, safi
);
2738 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2740 "couldn't delete af structure for peer %s",
2746 assert(listnode_lookup(group
->peer
, peer
));
2747 peer_unlock(peer
); /* peer group list reference */
2748 listnode_delete(group
->peer
, peer
);
2750 other
= peer
->doppelganger
;
2752 if (group
->conf
->as
) {
2754 if (other
&& other
->status
!= Deleted
) {
2757 listnode_delete(group
->peer
, other
);
2759 other
->group
= NULL
;
2765 bgp_bfd_deregister_peer(peer
);
2766 peer_global_config_reset(peer
);
2768 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
2769 peer
->last_reset
= PEER_DOWN_RMAP_UNBIND
;
2770 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2771 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2773 bgp_session_reset(peer
);
2778 static int bgp_startup_timer_expire(struct thread
*thread
)
2782 bgp
= THREAD_ARG(thread
);
2783 bgp
->t_startup
= NULL
;
2788 /* BGP instance creation by `router bgp' commands. */
2789 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
2790 enum bgp_instance_type inst_type
)
2796 if ((bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
))) == NULL
)
2799 if (BGP_DEBUG(zebra
, ZEBRA
)) {
2800 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
2801 zlog_debug("Creating Default VRF, AS %u", *as
);
2803 zlog_debug("Creating %s %s, AS %u",
2804 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
2811 bgp
->inst_type
= inst_type
;
2812 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
2814 bgp
->peer_self
= peer_new(bgp
);
2815 if (bgp
->peer_self
->host
)
2816 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
2817 bgp
->peer_self
->host
=
2818 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
2819 if (bgp
->peer_self
->hostname
!= NULL
) {
2820 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
2821 bgp
->peer_self
->hostname
= NULL
;
2823 if (cmd_hostname_get())
2824 bgp
->peer_self
->hostname
=
2825 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
2827 if (bgp
->peer_self
->domainname
!= NULL
) {
2828 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
2829 bgp
->peer_self
->domainname
= NULL
;
2831 if (cmd_domainname_get())
2832 bgp
->peer_self
->domainname
=
2833 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
2834 bgp
->peer
= list_new();
2835 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
2836 bgp
->peerhash
= hash_create(peer_hash_key_make
,
2839 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
2841 bgp
->group
= list_new();
2842 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
2844 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2845 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
2846 bgp
->route
[afi
][safi
] = bgp_table_init(afi
, safi
);
2847 bgp
->aggregate
[afi
][safi
] = bgp_table_init(afi
, safi
);
2848 bgp
->rib
[afi
][safi
] = bgp_table_init(afi
, safi
);
2850 /* Enable maximum-paths */
2851 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
2853 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
2857 bgp
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
2858 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
2859 bgp
->default_subgroup_pkt_queue_max
=
2860 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
2861 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
2862 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
2863 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
2864 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
2865 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
2866 bgp
->dynamic_neighbors_count
= 0;
2867 #if DFLT_BGP_IMPORT_CHECK
2868 bgp_flag_set(bgp
, BGP_FLAG_IMPORT_CHECK
);
2870 #if DFLT_BGP_SHOW_HOSTNAME
2871 bgp_flag_set(bgp
, BGP_FLAG_SHOW_HOSTNAME
);
2873 #if DFLT_BGP_LOG_NEIGHBOR_CHANGES
2874 bgp_flag_set(bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
);
2876 #if DFLT_BGP_DETERMINISTIC_MED
2877 bgp_flag_set(bgp
, BGP_FLAG_DETERMINISTIC_MED
);
2879 bgp
->addpath_tx_id
= BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
;
2884 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
2885 bgp
->rfapi
= bgp_rfapi_new(bgp
);
2887 assert(bgp
->rfapi_cfg
);
2889 #endif /* ENABLE_BGP_VNC */
2892 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
2894 /* TODO - The startup timer needs to be run for the whole of BGP
2896 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
2897 bgp
->restart_time
, &bgp
->t_startup
);
2900 bgp
->wpkt_quanta
= BGP_WRITE_PACKET_MAX
;
2901 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
2905 update_bgp_group_init(bgp
);
2910 /* Return the "default VRF" instance of BGP. */
2911 struct bgp
*bgp_get_default(void)
2914 struct listnode
*node
, *nnode
;
2916 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
2917 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
2922 /* Lookup BGP entry. */
2923 struct bgp
*bgp_lookup(as_t as
, const char *name
)
2926 struct listnode
*node
, *nnode
;
2928 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
2930 && ((bgp
->name
== NULL
&& name
== NULL
)
2931 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
2936 /* Lookup BGP structure by view name. */
2937 struct bgp
*bgp_lookup_by_name(const char *name
)
2940 struct listnode
*node
, *nnode
;
2942 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
2943 if ((bgp
->name
== NULL
&& name
== NULL
)
2944 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
2949 /* Lookup BGP instance based on VRF id. */
2950 /* Note: Only to be used for incoming messages from Zebra. */
2951 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
2955 /* Lookup VRF (in tree) and follow link. */
2956 vrf
= vrf_lookup_by_id(vrf_id
);
2959 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
2962 /* Called from VTY commands. */
2963 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
2964 enum bgp_instance_type inst_type
)
2968 /* Multiple instance check. */
2969 if (bgp_option_check(BGP_OPT_MULTIPLE_INSTANCE
)) {
2971 bgp
= bgp_lookup_by_name(name
);
2973 bgp
= bgp_get_default();
2975 /* Already exists. */
2977 if (bgp
->as
!= *as
) {
2979 return BGP_ERR_INSTANCE_MISMATCH
;
2981 if (bgp
->inst_type
!= inst_type
)
2982 return BGP_ERR_INSTANCE_MISMATCH
;
2987 /* BGP instance name can not be specified for single instance.
2990 return BGP_ERR_MULTIPLE_INSTANCE_NOT_SET
;
2992 /* Get default BGP structure if exists. */
2993 bgp
= bgp_get_default();
2996 if (bgp
->as
!= *as
) {
2998 return BGP_ERR_AS_MISMATCH
;
3005 bgp
= bgp_create(as
, name
, inst_type
);
3006 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
);
3007 bgp_address_init(bgp
);
3008 bgp_tip_hash_init(bgp
);
3012 bgp
->t_rmap_def_originate_eval
= NULL
;
3014 /* Create BGP server socket, if first instance. */
3015 if (list_isempty(bm
->bgp
) && !bgp_option_check(BGP_OPT_NO_LISTEN
)) {
3016 if (bgp_socket(bm
->port
, bm
->address
) < 0)
3017 return BGP_ERR_INVALID_VALUE
;
3020 listnode_add(bm
->bgp
, bgp
);
3022 /* If Default instance or VRF, link to the VRF structure, if present. */
3023 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3024 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3027 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3029 bgp_vrf_link(bgp
, vrf
);
3032 /* Register with Zebra, if needed */
3033 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
3034 bgp_zebra_instance_register(bgp
);
3041 * Make BGP instance "up". Applies only to VRFs (non-default) and
3042 * implies the VRF has been learnt from Zebra.
3044 void bgp_instance_up(struct bgp
*bgp
)
3047 struct listnode
*node
, *next
;
3049 /* Register with zebra. */
3050 bgp_zebra_instance_register(bgp
);
3052 /* Kick off any peers that may have been configured. */
3053 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3054 if (!BGP_PEER_START_SUPPRESSED(peer
))
3055 BGP_EVENT_ADD(peer
, BGP_Start
);
3058 /* Process any networks that have been configured. */
3059 bgp_static_add(bgp
);
3063 * Make BGP instance "down". Applies only to VRFs (non-default) and
3064 * implies the VRF has been deleted by Zebra.
3066 void bgp_instance_down(struct bgp
*bgp
)
3069 struct listnode
*node
;
3070 struct listnode
*next
;
3073 if (bgp
->t_rmap_def_originate_eval
) {
3074 BGP_TIMER_OFF(bgp
->t_rmap_def_originate_eval
);
3075 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3079 /* Bring down peers, so corresponding routes are purged. */
3080 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3081 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3082 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3083 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3085 bgp_session_reset(peer
);
3088 /* Purge network and redistributed routes. */
3089 bgp_purge_static_redist_routes(bgp
);
3091 /* Cleanup registered nexthops (flags) */
3092 bgp_cleanup_nexthops(bgp
);
3095 /* Delete BGP instance. */
3096 int bgp_delete(struct bgp
*bgp
)
3099 struct peer_group
*group
;
3100 struct listnode
*node
, *next
;
3105 THREAD_OFF(bgp
->t_startup
);
3107 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3108 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3109 zlog_debug("Deleting Default VRF");
3111 zlog_debug("Deleting %s %s",
3112 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3119 if (bgp
->t_rmap_def_originate_eval
) {
3120 BGP_TIMER_OFF(bgp
->t_rmap_def_originate_eval
);
3121 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3125 /* Inform peers we're going down. */
3126 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3127 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3128 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3129 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3132 /* Delete static routes (networks). */
3133 bgp_static_delete(bgp
);
3135 /* Unset redistribution. */
3136 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3137 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3138 if (i
!= ZEBRA_ROUTE_BGP
)
3139 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3141 /* Free peers and peer-groups. */
3142 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3143 peer_group_delete(group
);
3145 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3148 if (bgp
->peer_self
) {
3149 peer_delete(bgp
->peer_self
);
3150 bgp
->peer_self
= NULL
;
3153 update_bgp_group_free(bgp
);
3155 /* TODO - Other memory may need to be freed - e.g., NHT */
3160 bgp_cleanup_routes(bgp
);
3162 /* Remove visibility via the master list - there may however still be
3163 * routes to be processed still referencing the struct bgp.
3165 listnode_delete(bm
->bgp
, bgp
);
3166 if (list_isempty(bm
->bgp
))
3169 /* Deregister from Zebra, if needed */
3170 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
))
3171 bgp_zebra_instance_deregister(bgp
);
3173 /* Free interfaces in this instance. */
3176 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3178 bgp_vrf_unlink(bgp
, vrf
);
3180 thread_master_free_unused(bm
->master
);
3181 bgp_unlock(bgp
); /* initial reference */
3186 void bgp_free(struct bgp
*bgp
)
3190 struct bgp_table
*table
;
3191 struct bgp_node
*rn
;
3192 struct bgp_rmap
*rmap
;
3196 list_delete_and_null(&bgp
->group
);
3197 list_delete_and_null(&bgp
->peer
);
3199 if (bgp
->peerhash
) {
3200 hash_free(bgp
->peerhash
);
3201 bgp
->peerhash
= NULL
;
3204 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3205 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
3206 /* Special handling for 2-level routing tables. */
3207 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3208 || safi
== SAFI_EVPN
) {
3209 for (rn
= bgp_table_top(bgp
->rib
[afi
][safi
]);
3210 rn
; rn
= bgp_route_next(rn
)) {
3211 table
= (struct bgp_table
*)rn
->info
;
3212 bgp_table_finish(&table
);
3215 if (bgp
->route
[afi
][safi
])
3216 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3217 if (bgp
->aggregate
[afi
][safi
])
3218 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3219 if (bgp
->rib
[afi
][safi
])
3220 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3221 rmap
= &bgp
->table_map
[afi
][safi
];
3223 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3226 bgp_scan_finish(bgp
);
3227 bgp_address_destroy(bgp
);
3228 bgp_tip_hash_destroy(bgp
);
3230 bgp_evpn_cleanup(bgp
);
3233 XFREE(MTYPE_BGP
, bgp
->name
);
3235 XFREE(MTYPE_BGP
, bgp
);
3238 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3241 struct listnode
*node
, *nnode
;
3247 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3248 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3249 && !CHECK_FLAG(peer
->sflags
,
3250 PEER_STATUS_ACCEPT_PEER
))
3252 } else if (bm
->bgp
!= NULL
) {
3253 struct listnode
*bgpnode
, *nbgpnode
;
3255 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3256 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3258 && !strcmp(peer
->conf_if
, conf_if
)
3259 && !CHECK_FLAG(peer
->sflags
,
3260 PEER_STATUS_ACCEPT_PEER
))
3266 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3269 struct listnode
*node
, *nnode
;
3275 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3276 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3277 && !CHECK_FLAG(peer
->sflags
,
3278 PEER_STATUS_ACCEPT_PEER
))
3280 } else if (bm
->bgp
!= NULL
) {
3281 struct listnode
*bgpnode
, *nbgpnode
;
3283 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3284 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3286 && !strcmp(peer
->hostname
, hostname
)
3287 && !CHECK_FLAG(peer
->sflags
,
3288 PEER_STATUS_ACCEPT_PEER
))
3294 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
3296 struct peer
*peer
= NULL
;
3297 struct peer tmp_peer
;
3299 memset(&tmp_peer
, 0, sizeof(struct peer
));
3302 * We do not want to find the doppelganger peer so search for the peer
3304 * the hash that has PEER_FLAG_CONFIG_NODE
3306 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
3311 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
3312 } else if (bm
->bgp
!= NULL
) {
3313 struct listnode
*bgpnode
, *nbgpnode
;
3315 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
3316 /* Skip VRFs, this function will not be invoked without
3318 * when examining VRFs.
3320 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3323 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
3333 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
3334 union sockunion
*su
,
3335 struct peer_group
*group
)
3341 /* Create peer first; we've already checked group config is valid. */
3342 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3343 group
->conf
->as_type
, 0, 0, group
);
3348 peer
= peer_lock(peer
);
3349 listnode_add(group
->peer
, peer
);
3351 peer_group2peer_config_copy(group
, peer
);
3354 * Bind peer for all AFs configured for the group. We don't call
3355 * peer_group_bind as that is sub-optimal and does some stuff we don't
3358 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3359 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
3360 if (!group
->conf
->afc
[afi
][safi
])
3362 peer
->afc
[afi
][safi
] = 1;
3364 if (!peer_af_find(peer
, afi
, safi
))
3365 peer_af_create(peer
, afi
, safi
);
3367 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
3370 /* Mark as dynamic, but also as a "config node" for other things to
3372 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
3373 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3379 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
3380 struct prefix
*prefix
)
3382 struct listnode
*node
, *nnode
;
3383 struct prefix
*range
;
3386 afi
= family2afi(prefix
->family
);
3388 if (group
->listen_range
[afi
])
3389 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
3391 if (prefix_match(range
, prefix
))
3398 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
3399 struct prefix
**listen_range
)
3401 struct prefix
*range
= NULL
;
3402 struct peer_group
*group
= NULL
;
3403 struct listnode
*node
, *nnode
;
3405 *listen_range
= NULL
;
3407 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
3408 if ((range
= peer_group_lookup_dynamic_neighbor_range(
3411 } else if (bm
->bgp
!= NULL
) {
3412 struct listnode
*bgpnode
, *nbgpnode
;
3414 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3415 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
3416 if ((range
= peer_group_lookup_dynamic_neighbor_range(
3422 *listen_range
= range
;
3423 return (group
&& range
) ? group
: NULL
;
3426 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
3428 struct peer_group
*group
;
3431 struct prefix prefix
;
3432 struct prefix
*listen_range
;
3434 char buf
[PREFIX2STR_BUFFER
];
3435 char buf1
[PREFIX2STR_BUFFER
];
3437 sockunion2hostprefix(su
, &prefix
);
3439 /* See if incoming connection matches a configured listen range. */
3440 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
3451 prefix2str(&prefix
, buf
, sizeof(buf
));
3452 prefix2str(listen_range
, buf1
, sizeof(buf1
));
3454 if (bgp_debug_neighbor_events(NULL
))
3456 "Dynamic Neighbor %s matches group %s listen range %s",
3457 buf
, group
->name
, buf1
);
3459 /* Are we within the listen limit? */
3460 dncount
= gbgp
->dynamic_neighbors_count
;
3462 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
3463 if (bgp_debug_neighbor_events(NULL
))
3464 zlog_debug("Dynamic Neighbor %s rejected - at limit %d",
3465 inet_sutop(su
, buf
),
3466 gbgp
->dynamic_neighbors_limit
);
3470 /* Ensure group is not disabled. */
3471 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
3472 if (bgp_debug_neighbor_events(NULL
))
3474 "Dynamic Neighbor %s rejected - group %s disabled",
3479 /* Check that at least one AF is activated for the group. */
3480 if (!peer_group_af_configured(group
)) {
3481 if (bgp_debug_neighbor_events(NULL
))
3483 "Dynamic Neighbor %s rejected - no AF activated for group %s",
3488 /* Create dynamic peer and bind to associated group. */
3489 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
3492 gbgp
->dynamic_neighbors_count
= ++dncount
;
3494 if (bgp_debug_neighbor_events(peer
))
3495 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
3496 peer
->host
, group
->name
, dncount
);
3501 void peer_drop_dynamic_neighbor(struct peer
*peer
)
3504 if (peer
->group
&& peer
->group
->bgp
) {
3505 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
3507 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
3509 if (bgp_debug_neighbor_events(peer
))
3510 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
3511 peer
->group
->name
, dncount
);
3515 /* If peer is configured at least one address family return 1. */
3516 int peer_active(struct peer
*peer
)
3518 if (BGP_PEER_SU_UNSPEC(peer
))
3520 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
3521 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
3522 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
3523 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
3524 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
3525 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
3526 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
3527 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
3528 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
3533 /* If peer is negotiated at least one address family return 1. */
3534 int peer_active_nego(struct peer
*peer
)
3536 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
3537 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
3538 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
3539 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
3540 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
3541 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
3542 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
3543 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
3544 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
3545 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
3546 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
3551 /* peer_flag_change_type. */
3552 enum peer_change_type
{
3555 peer_change_reset_in
,
3556 peer_change_reset_out
,
3559 static void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
3560 enum peer_change_type type
)
3562 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
3565 if (peer
->status
!= Established
)
3568 if (type
== peer_change_reset
) {
3569 /* If we're resetting session, we've to delete both peer struct
3571 if ((peer
->doppelganger
)
3572 && (peer
->doppelganger
->status
!= Deleted
)
3573 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
3574 PEER_FLAG_CONFIG_NODE
)))
3575 peer_delete(peer
->doppelganger
);
3577 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3578 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3579 } else if (type
== peer_change_reset_in
) {
3580 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
3581 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
3582 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0);
3584 if ((peer
->doppelganger
)
3585 && (peer
->doppelganger
->status
!= Deleted
)
3586 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
3587 PEER_FLAG_CONFIG_NODE
)))
3588 peer_delete(peer
->doppelganger
);
3590 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3591 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3593 } else if (type
== peer_change_reset_out
) {
3594 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
3595 bgp_announce_route(peer
, afi
, safi
);
3599 struct peer_flag_action
{
3603 /* This flag can be set for peer-group member. */
3604 u_char not_for_member
;
3606 /* Action when the flag is changed. */
3607 enum peer_change_type type
;
3609 /* Peer down cause */
3613 static const struct peer_flag_action peer_flag_action_list
[] = {
3614 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
3615 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
3616 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
3617 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
3618 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
3619 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
3620 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
3621 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
3624 static const struct peer_flag_action peer_af_flag_action_list
[] = {
3625 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
3626 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
3627 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
3628 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
3629 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
3630 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
3631 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
3632 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
3633 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
3634 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
3635 // PEER_FLAG_DEFAULT_ORIGINATE
3636 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
3637 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
3638 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
3639 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
3640 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
3641 // PEER_FLAG_MAX_PREFIX
3642 // PEER_FLAG_MAX_PREFIX_WARNING
3643 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
3644 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
3645 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
3646 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
3647 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
3648 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
3649 {PEER_FLAG_ADDPATH_TX_ALL_PATHS
, 1, peer_change_reset
},
3650 {PEER_FLAG_ADDPATH_TX_BESTPATH_PER_AS
, 1, peer_change_reset
},
3651 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
3654 /* Proper action set. */
3655 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
3656 int size
, struct peer_flag_action
*action
,
3663 const struct peer_flag_action
*match
= NULL
;
3665 /* Check peer's frag action. */
3666 for (i
= 0; i
< size
; i
++) {
3667 match
= &action_list
[i
];
3669 if (match
->flag
== 0)
3672 if (match
->flag
& flag
) {
3675 if (match
->type
== peer_change_reset_in
)
3677 if (match
->type
== peer_change_reset_out
)
3679 if (match
->type
== peer_change_reset
) {
3683 if (match
->not_for_member
)
3684 action
->not_for_member
= 1;
3688 /* Set peer clear type. */
3689 if (reset_in
&& reset_out
)
3690 action
->type
= peer_change_reset
;
3692 action
->type
= peer_change_reset_in
;
3694 action
->type
= peer_change_reset_out
;
3696 action
->type
= peer_change_none
;
3701 static void peer_flag_modify_action(struct peer
*peer
, u_int32_t flag
)
3703 if (flag
== PEER_FLAG_SHUTDOWN
) {
3704 if (CHECK_FLAG(peer
->flags
, flag
)) {
3705 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
3706 peer_nsf_stop(peer
);
3708 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
3709 if (peer
->t_pmax_restart
) {
3710 BGP_TIMER_OFF(peer
->t_pmax_restart
);
3711 if (bgp_debug_neighbor_events(peer
))
3713 "%s Maximum-prefix restart timer canceled",
3717 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
3718 peer_nsf_stop(peer
);
3720 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3721 char *msg
= peer
->tx_shutdown_message
;
3724 if (!msg
&& peer_group_active(peer
))
3725 msg
= peer
->group
->conf
3726 ->tx_shutdown_message
;
3727 msglen
= msg
? strlen(msg
) : 0;
3735 memcpy(msgbuf
+ 1, msg
, msglen
);
3737 bgp_notify_send_with_data(
3738 peer
, BGP_NOTIFY_CEASE
,
3739 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
3740 msgbuf
, msglen
+ 1);
3743 peer
, BGP_NOTIFY_CEASE
,
3744 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3746 bgp_session_reset(peer
);
3748 peer
->v_start
= BGP_INIT_START_TIMER
;
3749 BGP_EVENT_ADD(peer
, BGP_Stop
);
3751 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3752 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
3753 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
3754 else if (flag
== PEER_FLAG_PASSIVE
)
3755 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
3756 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
3757 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
3759 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3760 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3762 bgp_session_reset(peer
);
3765 /* Change specified peer flag. */
3766 static int peer_flag_modify(struct peer
*peer
, u_int32_t flag
, int set
)
3770 struct peer_group
*group
;
3771 struct peer
*tmp_peer
;
3772 struct listnode
*node
, *nnode
;
3773 struct peer_flag_action action
;
3775 memset(&action
, 0, sizeof(struct peer_flag_action
));
3776 size
= sizeof peer_flag_action_list
/ sizeof(struct peer_flag_action
);
3778 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
3781 /* No flag action is found. */
3783 return BGP_ERR_INVALID_FLAG
;
3785 /* When unset the peer-group member's flag we have to check
3786 peer-group configuration. */
3787 if (!set
&& peer_group_active(peer
))
3788 if (CHECK_FLAG(peer
->group
->conf
->flags
, flag
)) {
3789 if (flag
== PEER_FLAG_SHUTDOWN
)
3790 return BGP_ERR_PEER_GROUP_SHUTDOWN
;
3793 /* Flag conflict check. */
3794 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
3795 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
3796 return BGP_ERR_PEER_FLAG_CONFLICT
;
3798 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
3799 if (set
&& CHECK_FLAG(peer
->flags
, flag
) == flag
)
3801 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
))
3806 SET_FLAG(peer
->flags
, flag
);
3808 UNSET_FLAG(peer
->flags
, flag
);
3810 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
3811 if (action
.type
== peer_change_reset
)
3812 peer_flag_modify_action(peer
, flag
);
3817 /* peer-group member updates. */
3818 group
= peer
->group
;
3820 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
3822 if (set
&& CHECK_FLAG(tmp_peer
->flags
, flag
) == flag
)
3825 if (!set
&& !CHECK_FLAG(tmp_peer
->flags
, flag
))
3829 SET_FLAG(tmp_peer
->flags
, flag
);
3831 UNSET_FLAG(tmp_peer
->flags
, flag
);
3833 if (action
.type
== peer_change_reset
)
3834 peer_flag_modify_action(tmp_peer
, flag
);
3839 int peer_flag_set(struct peer
*peer
, u_int32_t flag
)
3841 return peer_flag_modify(peer
, flag
, 1);
3844 int peer_flag_unset(struct peer
*peer
, u_int32_t flag
)
3846 return peer_flag_modify(peer
, flag
, 0);
3849 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
3850 u_int32_t flag
, int set
)
3854 struct listnode
*node
, *nnode
;
3855 struct peer_group
*group
;
3856 struct peer_flag_action action
;
3857 struct peer
*tmp_peer
;
3859 int addpath_tx_used
;
3861 memset(&action
, 0, sizeof(struct peer_flag_action
));
3862 size
= sizeof peer_af_flag_action_list
3863 / sizeof(struct peer_flag_action
);
3865 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
3868 /* No flag action is found. */
3870 return BGP_ERR_INVALID_FLAG
;
3872 /* Special check for reflector client. */
3873 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
3874 && peer_sort(peer
) != BGP_PEER_IBGP
)
3875 return BGP_ERR_NOT_INTERNAL_PEER
;
3877 /* Special check for remove-private-AS. */
3878 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
3879 && peer_sort(peer
) == BGP_PEER_IBGP
)
3880 return BGP_ERR_REMOVE_PRIVATE_AS
;
3882 /* as-override is not allowed for IBGP peers */
3883 if (flag
& PEER_FLAG_AS_OVERRIDE
&& peer_sort(peer
) == BGP_PEER_IBGP
)
3884 return BGP_ERR_AS_OVERRIDE
;
3886 /* When current flag configuration is same as requested one. */
3887 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
3888 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
) == flag
)
3890 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
))
3895 SET_FLAG(peer
->af_flags
[afi
][safi
], flag
);
3897 UNSET_FLAG(peer
->af_flags
[afi
][safi
], flag
);
3899 /* Execute action when peer is established. */
3900 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
3901 && peer
->status
== Established
) {
3902 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
3903 bgp_clear_adj_in(peer
, afi
, safi
);
3905 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
3906 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
3907 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
3908 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
3909 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
3910 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
3911 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
3912 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
3914 peer_change_action(peer
, afi
, safi
, action
.type
);
3918 /* Peer group member updates. */
3919 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
3920 group
= peer
->group
;
3922 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
3924 && CHECK_FLAG(tmp_peer
->af_flags
[afi
][safi
], flag
)
3929 && !CHECK_FLAG(tmp_peer
->af_flags
[afi
][safi
], flag
))
3933 SET_FLAG(tmp_peer
->af_flags
[afi
][safi
], flag
);
3935 UNSET_FLAG(tmp_peer
->af_flags
[afi
][safi
], flag
);
3937 if (tmp_peer
->status
== Established
) {
3938 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
3939 bgp_clear_adj_in(tmp_peer
, afi
, safi
);
3941 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
3942 tmp_peer
->last_reset
=
3943 PEER_DOWN_RR_CLIENT_CHANGE
;
3945 == PEER_FLAG_RSERVER_CLIENT
)
3946 tmp_peer
->last_reset
=
3947 PEER_DOWN_RS_CLIENT_CHANGE
;
3949 == PEER_FLAG_ORF_PREFIX_SM
)
3950 tmp_peer
->last_reset
=
3951 PEER_DOWN_CAPABILITY_CHANGE
;
3953 == PEER_FLAG_ORF_PREFIX_RM
)
3954 tmp_peer
->last_reset
=
3955 PEER_DOWN_CAPABILITY_CHANGE
;
3957 peer_change_action(tmp_peer
, afi
, safi
,
3964 /* Track if addpath TX is in use */
3965 if (flag
& (PEER_FLAG_ADDPATH_TX_ALL_PATHS
3966 | PEER_FLAG_ADDPATH_TX_BESTPATH_PER_AS
)) {
3968 addpath_tx_used
= 0;
3971 addpath_tx_used
= 1;
3973 if (flag
& PEER_FLAG_ADDPATH_TX_BESTPATH_PER_AS
) {
3974 if (!bgp_flag_check(
3975 bgp
, BGP_FLAG_DETERMINISTIC_MED
)) {
3977 "%s: enabling bgp deterministic-med, this is required"
3978 " for addpath-tx-bestpath-per-AS",
3982 BGP_FLAG_DETERMINISTIC_MED
);
3983 bgp_recalculate_all_bestpaths(bgp
);
3987 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
,
3989 if (CHECK_FLAG(tmp_peer
->af_flags
[afi
][safi
],
3990 PEER_FLAG_ADDPATH_TX_ALL_PATHS
)
3992 tmp_peer
->af_flags
[afi
][safi
],
3993 PEER_FLAG_ADDPATH_TX_BESTPATH_PER_AS
)) {
3994 addpath_tx_used
= 1;
4000 bgp
->addpath_tx_used
[afi
][safi
] = addpath_tx_used
;
4006 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, u_int32_t flag
)
4008 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4011 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
4014 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4018 int peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4020 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4021 peer
->tx_shutdown_message
=
4022 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4026 int peer_tx_shutdown_message_unset(struct peer
*peer
)
4028 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4033 /* EBGP multihop configuration. */
4034 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4036 struct peer_group
*group
;
4037 struct listnode
*node
, *nnode
;
4040 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4043 /* see comment in peer_ttl_security_hops_set() */
4044 if (ttl
!= MAXTTL
) {
4045 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4046 group
= peer
->group
;
4047 if (group
->conf
->gtsm_hops
!= 0)
4048 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4050 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4052 if (peer1
->sort
== BGP_PEER_IBGP
)
4055 if (peer1
->gtsm_hops
!= 0)
4056 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4059 if (peer
->gtsm_hops
!= 0)
4060 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4066 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4067 if (peer
->fd
>= 0 && peer
->sort
!= BGP_PEER_IBGP
) {
4068 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4069 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4070 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4072 bgp_session_reset(peer
);
4075 group
= peer
->group
;
4076 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4077 if (peer
->sort
== BGP_PEER_IBGP
)
4080 peer
->ttl
= group
->conf
->ttl
;
4082 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4083 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4084 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4086 bgp_session_reset(peer
);
4092 int peer_ebgp_multihop_unset(struct peer
*peer
)
4094 struct peer_group
*group
;
4095 struct listnode
*node
, *nnode
;
4097 if (peer
->sort
== BGP_PEER_IBGP
)
4100 if (peer
->gtsm_hops
!= 0 && peer
->ttl
!= MAXTTL
)
4101 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4103 if (peer_group_active(peer
))
4104 peer
->ttl
= peer
->group
->conf
->ttl
;
4108 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4109 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4110 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4111 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4113 bgp_session_reset(peer
);
4115 group
= peer
->group
;
4116 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4117 if (peer
->sort
== BGP_PEER_IBGP
)
4122 if (peer
->fd
>= 0) {
4123 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4125 peer
, BGP_NOTIFY_CEASE
,
4126 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4128 bgp_session_reset(peer
);
4135 /* Neighbor description. */
4136 int peer_description_set(struct peer
*peer
, const char *desc
)
4139 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
4141 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
4146 int peer_description_unset(struct peer
*peer
)
4149 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
4156 /* Neighbor update-source. */
4157 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
4159 struct peer_group
*group
;
4160 struct listnode
*node
, *nnode
;
4162 if (peer
->update_if
) {
4163 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4164 && strcmp(peer
->update_if
, ifname
) == 0)
4167 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4168 peer
->update_if
= NULL
;
4171 if (peer
->update_source
) {
4172 sockunion_free(peer
->update_source
);
4173 peer
->update_source
= NULL
;
4176 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
4178 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4179 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4180 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4181 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4182 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4184 bgp_session_reset(peer
);
4188 /* peer-group member updates. */
4189 group
= peer
->group
;
4190 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4191 if (peer
->update_if
) {
4192 if (strcmp(peer
->update_if
, ifname
) == 0)
4195 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4196 peer
->update_if
= NULL
;
4199 if (peer
->update_source
) {
4200 sockunion_free(peer
->update_source
);
4201 peer
->update_source
= NULL
;
4204 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
4206 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4207 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4208 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4209 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4211 bgp_session_reset(peer
);
4216 int peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
4218 struct peer_group
*group
;
4219 struct listnode
*node
, *nnode
;
4221 if (peer
->update_source
) {
4222 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4223 && sockunion_cmp(peer
->update_source
, su
) == 0)
4225 sockunion_free(peer
->update_source
);
4226 peer
->update_source
= NULL
;
4229 if (peer
->update_if
) {
4230 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4231 peer
->update_if
= NULL
;
4234 peer
->update_source
= sockunion_dup(su
);
4236 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4237 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4238 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4239 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4240 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4242 bgp_session_reset(peer
);
4246 /* peer-group member updates. */
4247 group
= peer
->group
;
4248 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4249 if (peer
->update_source
) {
4250 if (sockunion_cmp(peer
->update_source
, su
) == 0)
4252 sockunion_free(peer
->update_source
);
4253 peer
->update_source
= NULL
;
4256 if (peer
->update_if
) {
4257 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4258 peer
->update_if
= NULL
;
4261 peer
->update_source
= sockunion_dup(su
);
4263 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4264 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4265 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4266 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4268 bgp_session_reset(peer
);
4273 int peer_update_source_unset(struct peer
*peer
)
4275 union sockunion
*su
;
4276 struct peer_group
*group
;
4277 struct listnode
*node
, *nnode
;
4279 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
) && !peer
->update_source
4280 && !peer
->update_if
)
4283 if (peer
->update_source
) {
4284 sockunion_free(peer
->update_source
);
4285 peer
->update_source
= NULL
;
4287 if (peer
->update_if
) {
4288 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4289 peer
->update_if
= NULL
;
4292 if (peer_group_active(peer
)) {
4293 group
= peer
->group
;
4295 if (group
->conf
->update_source
) {
4296 su
= sockunion_dup(group
->conf
->update_source
);
4297 peer
->update_source
= su
;
4298 } else if (group
->conf
->update_if
)
4299 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
,
4300 group
->conf
->update_if
);
4303 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4304 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4305 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4306 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4307 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4309 bgp_session_reset(peer
);
4313 /* peer-group member updates. */
4314 group
= peer
->group
;
4315 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4316 if (!peer
->update_source
&& !peer
->update_if
)
4319 if (peer
->update_source
) {
4320 sockunion_free(peer
->update_source
);
4321 peer
->update_source
= NULL
;
4324 if (peer
->update_if
) {
4325 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4326 peer
->update_if
= NULL
;
4329 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4330 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4331 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4332 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4334 bgp_session_reset(peer
);
4339 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
4342 struct peer_group
*group
;
4343 struct listnode
*node
, *nnode
;
4345 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_DEFAULT_ORIGINATE
)
4346 || (rmap
&& !peer
->default_rmap
[afi
][safi
].name
)
4348 && strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0)) {
4349 SET_FLAG(peer
->af_flags
[afi
][safi
],
4350 PEER_FLAG_DEFAULT_ORIGINATE
);
4353 if (peer
->default_rmap
[afi
][safi
].name
)
4354 XFREE(MTYPE_ROUTE_MAP_NAME
,
4355 peer
->default_rmap
[afi
][safi
].name
);
4356 peer
->default_rmap
[afi
][safi
].name
=
4357 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
4358 peer
->default_rmap
[afi
][safi
].map
=
4359 route_map_lookup_by_name(rmap
);
4363 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4364 if (peer
->status
== Established
&& peer
->afc_nego
[afi
][safi
]) {
4365 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
4366 bgp_default_originate(peer
, afi
, safi
, 0);
4367 bgp_announce_route(peer
, afi
, safi
);
4372 /* peer-group member updates. */
4373 group
= peer
->group
;
4374 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4375 SET_FLAG(peer
->af_flags
[afi
][safi
],
4376 PEER_FLAG_DEFAULT_ORIGINATE
);
4379 if (peer
->default_rmap
[afi
][safi
].name
)
4380 XFREE(MTYPE_ROUTE_MAP_NAME
,
4381 peer
->default_rmap
[afi
][safi
].name
);
4382 peer
->default_rmap
[afi
][safi
].name
=
4383 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
4384 peer
->default_rmap
[afi
][safi
].map
=
4385 route_map_lookup_by_name(rmap
);
4388 if (peer
->status
== Established
&& peer
->afc_nego
[afi
][safi
]) {
4389 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
4390 bgp_default_originate(peer
, afi
, safi
, 0);
4391 bgp_announce_route(peer
, afi
, safi
);
4397 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
4399 struct peer_group
*group
;
4400 struct listnode
*node
, *nnode
;
4402 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4403 PEER_FLAG_DEFAULT_ORIGINATE
)) {
4404 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4405 PEER_FLAG_DEFAULT_ORIGINATE
);
4407 if (peer
->default_rmap
[afi
][safi
].name
)
4408 XFREE(MTYPE_ROUTE_MAP_NAME
,
4409 peer
->default_rmap
[afi
][safi
].name
);
4410 peer
->default_rmap
[afi
][safi
].name
= NULL
;
4411 peer
->default_rmap
[afi
][safi
].map
= NULL
;
4414 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4415 if (peer
->status
== Established
&& peer
->afc_nego
[afi
][safi
]) {
4416 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
4417 bgp_default_originate(peer
, afi
, safi
, 1);
4418 bgp_announce_route(peer
, afi
, safi
);
4423 /* peer-group member updates. */
4424 group
= peer
->group
;
4425 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4426 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4427 PEER_FLAG_DEFAULT_ORIGINATE
);
4429 if (peer
->default_rmap
[afi
][safi
].name
)
4430 XFREE(MTYPE_ROUTE_MAP_NAME
,
4431 peer
->default_rmap
[afi
][safi
].name
);
4432 peer
->default_rmap
[afi
][safi
].name
= NULL
;
4433 peer
->default_rmap
[afi
][safi
].map
= NULL
;
4435 if (peer
->status
== Established
&& peer
->afc_nego
[afi
][safi
]) {
4436 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
4437 bgp_default_originate(peer
, afi
, safi
, 1);
4438 bgp_announce_route(peer
, afi
, safi
);
4444 int peer_port_set(struct peer
*peer
, u_int16_t port
)
4450 int peer_port_unset(struct peer
*peer
)
4452 peer
->port
= BGP_PORT_DEFAULT
;
4457 * Helper function that is called after the name of the policy
4458 * being used by a peer has changed (AF specific). Automatically
4459 * initiates inbound or outbound processing as needed.
4461 static void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
4465 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
4466 if (peer
->status
== Established
)
4467 bgp_announce_route(peer
, afi
, safi
);
4469 if (peer
->status
!= Established
)
4472 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4473 PEER_FLAG_SOFT_RECONFIG
))
4474 bgp_soft_reconfig_in(peer
, afi
, safi
);
4475 else if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4476 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4477 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0);
4482 /* neighbor weight. */
4483 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, u_int16_t weight
)
4485 struct peer_group
*group
;
4486 struct listnode
*node
, *nnode
;
4488 if (peer
->weight
[afi
][safi
] != weight
) {
4489 peer
->weight
[afi
][safi
] = weight
;
4490 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
4491 peer_on_policy_change(peer
, afi
, safi
, 0);
4494 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4497 /* peer-group member updates. */
4498 group
= peer
->group
;
4499 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4500 if (peer
->weight
[afi
][safi
] != weight
) {
4501 peer
->weight
[afi
][safi
] = weight
;
4502 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
4503 peer_on_policy_change(peer
, afi
, safi
, 0);
4509 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
4511 struct peer_group
*group
;
4512 struct listnode
*node
, *nnode
;
4514 /* not the peer-group itself but a peer in a peer-group */
4515 if (peer_group_active(peer
)) {
4516 group
= peer
->group
;
4518 /* inherit weight from the peer-group */
4519 if (CHECK_FLAG(group
->conf
->af_flags
[afi
][safi
],
4520 PEER_FLAG_WEIGHT
)) {
4521 peer
->weight
[afi
][safi
] =
4522 group
->conf
->weight
[afi
][safi
];
4523 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
4524 peer_on_policy_change(peer
, afi
, safi
, 0);
4526 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4527 PEER_FLAG_WEIGHT
)) {
4528 peer
->weight
[afi
][safi
] = 0;
4529 peer_af_flag_unset(peer
, afi
, safi
,
4531 peer_on_policy_change(peer
, afi
, safi
, 0);
4537 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
)) {
4538 peer
->weight
[afi
][safi
] = 0;
4539 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
4540 peer_on_policy_change(peer
, afi
, safi
, 0);
4543 /* peer-group member updates. */
4544 group
= peer
->group
;
4547 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4549 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4550 PEER_FLAG_WEIGHT
)) {
4551 peer
->weight
[afi
][safi
] = 0;
4552 peer_af_flag_unset(peer
, afi
, safi
,
4554 peer_on_policy_change(peer
, afi
, safi
,
4563 int peer_timers_set(struct peer
*peer
, u_int32_t keepalive
, u_int32_t holdtime
)
4565 struct peer_group
*group
;
4566 struct listnode
*node
, *nnode
;
4568 /* keepalive value check. */
4569 if (keepalive
> 65535)
4570 return BGP_ERR_INVALID_VALUE
;
4572 /* Holdtime value check. */
4573 if (holdtime
> 65535)
4574 return BGP_ERR_INVALID_VALUE
;
4576 /* Holdtime value must be either 0 or greater than 3. */
4577 if (holdtime
< 3 && holdtime
!= 0)
4578 return BGP_ERR_INVALID_VALUE
;
4580 /* Set value to the configuration. */
4581 SET_FLAG(peer
->config
, PEER_CONFIG_TIMER
);
4582 peer
->holdtime
= holdtime
;
4583 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
4585 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4588 /* peer-group member updates. */
4589 group
= peer
->group
;
4590 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4591 SET_FLAG(peer
->config
, PEER_CONFIG_TIMER
);
4592 peer
->holdtime
= group
->conf
->holdtime
;
4593 peer
->keepalive
= group
->conf
->keepalive
;
4598 int peer_timers_unset(struct peer
*peer
)
4600 struct peer_group
*group
;
4601 struct listnode
*node
, *nnode
;
4603 /* Clear configuration. */
4604 UNSET_FLAG(peer
->config
, PEER_CONFIG_TIMER
);
4605 peer
->keepalive
= 0;
4608 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4611 /* peer-group member updates. */
4612 group
= peer
->group
;
4613 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4614 UNSET_FLAG(peer
->config
, PEER_CONFIG_TIMER
);
4616 peer
->keepalive
= 0;
4622 int peer_timers_connect_set(struct peer
*peer
, u_int32_t connect
)
4624 struct peer_group
*group
;
4625 struct listnode
*node
, *nnode
;
4627 if (connect
> 65535)
4628 return BGP_ERR_INVALID_VALUE
;
4630 /* Set value to the configuration. */
4631 SET_FLAG(peer
->config
, PEER_CONFIG_CONNECT
);
4632 peer
->connect
= connect
;
4634 /* Set value to timer setting. */
4635 peer
->v_connect
= connect
;
4637 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4640 /* peer-group member updates. */
4641 group
= peer
->group
;
4642 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4643 SET_FLAG(peer
->config
, PEER_CONFIG_CONNECT
);
4644 peer
->connect
= connect
;
4645 peer
->v_connect
= connect
;
4650 int peer_timers_connect_unset(struct peer
*peer
)
4652 struct peer_group
*group
;
4653 struct listnode
*node
, *nnode
;
4655 /* Clear configuration. */
4656 UNSET_FLAG(peer
->config
, PEER_CONFIG_CONNECT
);
4659 /* Set timer setting to default value. */
4660 peer
->v_connect
= BGP_DEFAULT_CONNECT_RETRY
;
4662 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4665 /* peer-group member updates. */
4666 group
= peer
->group
;
4667 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4668 UNSET_FLAG(peer
->config
, PEER_CONFIG_CONNECT
);
4670 peer
->v_connect
= BGP_DEFAULT_CONNECT_RETRY
;
4675 int peer_advertise_interval_set(struct peer
*peer
, u_int32_t routeadv
)
4677 struct peer_group
*group
;
4678 struct listnode
*node
, *nnode
;
4681 return BGP_ERR_INVALID_VALUE
;
4683 SET_FLAG(peer
->config
, PEER_CONFIG_ROUTEADV
);
4684 peer
->routeadv
= routeadv
;
4685 peer
->v_routeadv
= routeadv
;
4687 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4688 update_group_adjust_peer_afs(peer
);
4689 if (peer
->status
== Established
)
4690 bgp_announce_route_all(peer
);
4694 /* peer-group member updates. */
4695 group
= peer
->group
;
4696 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4697 SET_FLAG(peer
->config
, PEER_CONFIG_ROUTEADV
);
4698 peer
->routeadv
= routeadv
;
4699 peer
->v_routeadv
= routeadv
;
4700 update_group_adjust_peer_afs(peer
);
4701 if (peer
->status
== Established
)
4702 bgp_announce_route_all(peer
);
4708 int peer_advertise_interval_unset(struct peer
*peer
)
4710 struct peer_group
*group
;
4711 struct listnode
*node
, *nnode
;
4713 UNSET_FLAG(peer
->config
, PEER_CONFIG_ROUTEADV
);
4716 if (peer
->sort
== BGP_PEER_IBGP
)
4717 peer
->v_routeadv
= BGP_DEFAULT_IBGP_ROUTEADV
;
4719 peer
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
4721 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4722 update_group_adjust_peer_afs(peer
);
4723 if (peer
->status
== Established
)
4724 bgp_announce_route_all(peer
);
4728 /* peer-group member updates. */
4729 group
= peer
->group
;
4730 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4731 UNSET_FLAG(peer
->config
, PEER_CONFIG_ROUTEADV
);
4734 if (peer
->sort
== BGP_PEER_IBGP
)
4735 peer
->v_routeadv
= BGP_DEFAULT_IBGP_ROUTEADV
;
4737 peer
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
4739 update_group_adjust_peer_afs(peer
);
4740 if (peer
->status
== Established
)
4741 bgp_announce_route_all(peer
);
4747 /* neighbor interface */
4748 void peer_interface_set(struct peer
*peer
, const char *str
)
4751 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
4752 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
4755 void peer_interface_unset(struct peer
*peer
)
4758 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
4759 peer
->ifname
= NULL
;
4763 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
4764 int allow_num
, int origin
)
4766 struct peer_group
*group
;
4767 struct listnode
*node
, *nnode
;
4770 if (peer
->allowas_in
[afi
][safi
]
4771 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4772 PEER_FLAG_ALLOWAS_IN
)
4773 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4774 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
4775 peer
->allowas_in
[afi
][safi
] = 0;
4776 peer_af_flag_unset(peer
, afi
, safi
,
4777 PEER_FLAG_ALLOWAS_IN
);
4778 peer_af_flag_set(peer
, afi
, safi
,
4779 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
4780 peer_on_policy_change(peer
, afi
, safi
, 0);
4783 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4786 group
= peer
->group
;
4787 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4788 if (peer
->allowas_in
[afi
][safi
]
4789 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4790 PEER_FLAG_ALLOWAS_IN
)
4791 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4792 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
4793 peer
->allowas_in
[afi
][safi
] = 0;
4794 peer_af_flag_unset(peer
, afi
, safi
,
4795 PEER_FLAG_ALLOWAS_IN
);
4796 peer_af_flag_set(peer
, afi
, safi
,
4797 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
4798 peer_on_policy_change(peer
, afi
, safi
, 0);
4802 if (allow_num
< 1 || allow_num
> 10)
4803 return BGP_ERR_INVALID_VALUE
;
4805 if (peer
->allowas_in
[afi
][safi
] != allow_num
4806 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4807 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
4808 peer
->allowas_in
[afi
][safi
] = allow_num
;
4809 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
4810 peer_af_flag_unset(peer
, afi
, safi
,
4811 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
4812 peer_on_policy_change(peer
, afi
, safi
, 0);
4815 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4818 group
= peer
->group
;
4819 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4820 if (peer
->allowas_in
[afi
][safi
] != allow_num
4821 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4822 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
4823 peer
->allowas_in
[afi
][safi
] = allow_num
;
4824 peer_af_flag_set(peer
, afi
, safi
,
4825 PEER_FLAG_ALLOWAS_IN
);
4826 peer_af_flag_unset(peer
, afi
, safi
,
4827 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
4828 peer_on_policy_change(peer
, afi
, safi
, 0);
4836 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
4838 struct peer_group
*group
;
4839 struct peer
*tmp_peer
;
4840 struct listnode
*node
, *nnode
;
4842 /* If this is a peer-group we must first clear the flags for all of the
4843 * peer-group members
4845 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4846 group
= peer
->group
;
4847 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
4848 if (CHECK_FLAG(tmp_peer
->af_flags
[afi
][safi
],
4849 PEER_FLAG_ALLOWAS_IN
)
4850 || CHECK_FLAG(tmp_peer
->af_flags
[afi
][safi
],
4851 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
4852 tmp_peer
->allowas_in
[afi
][safi
] = 0;
4853 peer_af_flag_unset(tmp_peer
, afi
, safi
,
4854 PEER_FLAG_ALLOWAS_IN
);
4855 peer_af_flag_unset(tmp_peer
, afi
, safi
,
4856 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
4857 peer_on_policy_change(tmp_peer
, afi
, safi
, 0);
4862 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
)
4863 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
4864 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
4865 peer
->allowas_in
[afi
][safi
] = 0;
4866 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
4867 peer_af_flag_unset(peer
, afi
, safi
,
4868 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
4869 peer_on_policy_change(peer
, afi
, safi
, 0);
4875 int peer_local_as_set(struct peer
*peer
, as_t as
, int no_prepend
,
4878 struct bgp
*bgp
= peer
->bgp
;
4879 struct peer_group
*group
;
4880 struct listnode
*node
, *nnode
;
4882 if (peer_sort(peer
) != BGP_PEER_EBGP
4883 && peer_sort(peer
) != BGP_PEER_INTERNAL
)
4884 return BGP_ERR_LOCAL_AS_ALLOWED_ONLY_FOR_EBGP
;
4887 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
4890 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS_REMOTE_AS
;
4892 if (peer
->change_local_as
== as
4893 && ((CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
)
4895 || (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
)
4897 && ((CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
)
4899 || (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
)
4903 peer
->change_local_as
= as
;
4905 SET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
4907 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
4910 SET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
4912 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
4914 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4915 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4916 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
4917 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4918 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4920 bgp_session_reset(peer
);
4924 group
= peer
->group
;
4925 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4926 peer
->change_local_as
= as
;
4928 SET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
4930 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
4933 SET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
4935 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
4937 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4938 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
4939 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4940 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4942 BGP_EVENT_ADD(peer
, BGP_Stop
);
4948 int peer_local_as_unset(struct peer
*peer
)
4950 struct peer_group
*group
;
4951 struct listnode
*node
, *nnode
;
4953 if (!peer
->change_local_as
)
4956 peer
->change_local_as
= 0;
4957 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
4958 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
4960 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4961 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4962 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
4963 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4964 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4966 BGP_EVENT_ADD(peer
, BGP_Stop
);
4971 group
= peer
->group
;
4972 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4973 peer
->change_local_as
= 0;
4974 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
4975 UNSET_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
4977 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4978 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
4979 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4980 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4982 bgp_session_reset(peer
);
4987 /* Set password for authenticating with the peer. */
4988 int peer_password_set(struct peer
*peer
, const char *password
)
4990 struct listnode
*nn
, *nnode
;
4991 int len
= password
? strlen(password
) : 0;
4992 int ret
= BGP_SUCCESS
;
4994 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
4995 return BGP_ERR_INVALID_VALUE
;
4997 if (peer
->password
&& strcmp(peer
->password
, password
) == 0
4998 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5002 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
5004 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
5006 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5007 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5008 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5009 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5011 bgp_session_reset(peer
);
5013 if (BGP_PEER_SU_UNSPEC(peer
))
5016 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
5017 : BGP_ERR_TCPSIG_FAILED
;
5020 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, nn
, nnode
, peer
)) {
5021 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
5025 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
5027 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
5029 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5030 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5031 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5033 bgp_session_reset(peer
);
5035 if (!BGP_PEER_SU_UNSPEC(peer
)) {
5036 if (bgp_md5_set(peer
) < 0)
5037 ret
= BGP_ERR_TCPSIG_FAILED
;
5044 int peer_password_unset(struct peer
*peer
)
5046 struct listnode
*nn
, *nnode
;
5048 if (!peer
->password
&& !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5051 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5052 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5053 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5054 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5056 bgp_session_reset(peer
);
5059 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
5061 peer
->password
= NULL
;
5063 if (!BGP_PEER_SU_UNSPEC(peer
))
5064 bgp_md5_unset(peer
);
5069 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
5070 peer
->password
= NULL
;
5072 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, nn
, nnode
, peer
)) {
5073 if (!peer
->password
)
5076 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5077 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5078 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5080 bgp_session_reset(peer
);
5082 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
5083 peer
->password
= NULL
;
5085 if (!BGP_PEER_SU_UNSPEC(peer
))
5086 bgp_md5_unset(peer
);
5093 /* Set distribute list to the peer. */
5094 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
5097 struct bgp_filter
*filter
;
5098 struct peer_group
*group
;
5099 struct listnode
*node
, *nnode
;
5101 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
5102 return BGP_ERR_INVALID_VALUE
;
5104 filter
= &peer
->filter
[afi
][safi
];
5106 if (filter
->plist
[direct
].name
)
5107 return BGP_ERR_PEER_FILTER_CONFLICT
;
5109 if (filter
->dlist
[direct
].name
)
5110 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
5111 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5112 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
5114 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5115 peer_on_policy_change(peer
, afi
, safi
,
5116 (direct
== FILTER_OUT
) ? 1 : 0);
5120 group
= peer
->group
;
5121 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5122 filter
= &peer
->filter
[afi
][safi
];
5124 if (filter
->dlist
[direct
].name
)
5125 XFREE(MTYPE_BGP_FILTER_NAME
,
5126 filter
->dlist
[direct
].name
);
5127 filter
->dlist
[direct
].name
=
5128 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5129 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
5130 peer_on_policy_change(peer
, afi
, safi
,
5131 (direct
== FILTER_OUT
) ? 1 : 0);
5137 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
5139 struct bgp_filter
*filter
;
5140 struct bgp_filter
*gfilter
;
5141 struct peer_group
*group
;
5142 struct listnode
*node
, *nnode
;
5144 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
5145 return BGP_ERR_INVALID_VALUE
;
5147 filter
= &peer
->filter
[afi
][safi
];
5149 /* apply peer-group filter */
5150 if (peer_group_active(peer
)) {
5151 gfilter
= &peer
->group
->conf
->filter
[afi
][safi
];
5153 if (gfilter
->dlist
[direct
].name
) {
5154 if (filter
->dlist
[direct
].name
)
5155 XFREE(MTYPE_BGP_FILTER_NAME
,
5156 filter
->dlist
[direct
].name
);
5157 filter
->dlist
[direct
].name
=
5158 XSTRDUP(MTYPE_BGP_FILTER_NAME
,
5159 gfilter
->dlist
[direct
].name
);
5160 filter
->dlist
[direct
].alist
=
5161 gfilter
->dlist
[direct
].alist
;
5162 peer_on_policy_change(peer
, afi
, safi
,
5163 (direct
== FILTER_OUT
) ? 1 : 0);
5168 if (filter
->dlist
[direct
].name
)
5169 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
5170 filter
->dlist
[direct
].name
= NULL
;
5171 filter
->dlist
[direct
].alist
= NULL
;
5173 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5174 peer_on_policy_change(peer
, afi
, safi
,
5175 (direct
== FILTER_OUT
) ? 1 : 0);
5179 group
= peer
->group
;
5180 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5181 filter
= &peer
->filter
[afi
][safi
];
5183 if (filter
->dlist
[direct
].name
)
5184 XFREE(MTYPE_BGP_FILTER_NAME
,
5185 filter
->dlist
[direct
].name
);
5186 filter
->dlist
[direct
].name
= NULL
;
5187 filter
->dlist
[direct
].alist
= NULL
;
5188 peer_on_policy_change(peer
, afi
, safi
,
5189 (direct
== FILTER_OUT
) ? 1 : 0);
5195 /* Update distribute list. */
5196 static void peer_distribute_update(struct access_list
*access
)
5201 struct listnode
*mnode
, *mnnode
;
5202 struct listnode
*node
, *nnode
;
5205 struct peer_group
*group
;
5206 struct bgp_filter
*filter
;
5208 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
5210 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
5211 access
->name
, 0, 0);
5212 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
5213 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
5214 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
;
5216 filter
= &peer
->filter
[afi
][safi
];
5218 for (direct
= FILTER_IN
;
5219 direct
< FILTER_MAX
; direct
++) {
5220 if (filter
->dlist
[direct
].name
)
5221 filter
->dlist
[direct
]
5222 .alist
= access_list_lookup(
5224 filter
->dlist
[direct
]
5227 filter
->dlist
[direct
]
5232 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
5233 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
5234 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
;
5237 &group
->conf
->filter
[afi
][safi
];
5239 for (direct
= FILTER_IN
;
5240 direct
< FILTER_MAX
; direct
++) {
5241 if (filter
->dlist
[direct
].name
)
5242 filter
->dlist
[direct
]
5243 .alist
= access_list_lookup(
5245 filter
->dlist
[direct
]
5248 filter
->dlist
[direct
]
5254 vnc_prefix_list_update(bgp
);
5259 /* Set prefix list to the peer. */
5260 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
5263 struct bgp_filter
*filter
;
5264 struct peer_group
*group
;
5265 struct listnode
*node
, *nnode
;
5267 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
5268 return BGP_ERR_INVALID_VALUE
;
5270 filter
= &peer
->filter
[afi
][safi
];
5272 if (filter
->dlist
[direct
].name
)
5273 return BGP_ERR_PEER_FILTER_CONFLICT
;
5275 if (filter
->plist
[direct
].name
)
5276 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
5277 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5278 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
5280 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5281 peer_on_policy_change(peer
, afi
, safi
,
5282 (direct
== FILTER_OUT
) ? 1 : 0);
5286 group
= peer
->group
;
5287 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5288 filter
= &peer
->filter
[afi
][safi
];
5290 if (filter
->plist
[direct
].name
)
5291 XFREE(MTYPE_BGP_FILTER_NAME
,
5292 filter
->plist
[direct
].name
);
5293 filter
->plist
[direct
].name
=
5294 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5295 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
5296 peer_on_policy_change(peer
, afi
, safi
,
5297 (direct
== FILTER_OUT
) ? 1 : 0);
5302 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
5305 struct bgp_filter
*filter
;
5306 struct bgp_filter
*gfilter
;
5307 struct peer_group
*group
;
5308 struct listnode
*node
, *nnode
;
5310 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
5311 return BGP_ERR_INVALID_VALUE
;
5313 filter
= &peer
->filter
[afi
][safi
];
5315 /* apply peer-group filter */
5316 if (peer_group_active(peer
)) {
5317 gfilter
= &peer
->group
->conf
->filter
[afi
][safi
];
5319 if (gfilter
->plist
[direct
].name
) {
5320 if (filter
->plist
[direct
].name
)
5321 XFREE(MTYPE_BGP_FILTER_NAME
,
5322 filter
->plist
[direct
].name
);
5323 filter
->plist
[direct
].name
=
5324 XSTRDUP(MTYPE_BGP_FILTER_NAME
,
5325 gfilter
->plist
[direct
].name
);
5326 filter
->plist
[direct
].plist
=
5327 gfilter
->plist
[direct
].plist
;
5328 peer_on_policy_change(peer
, afi
, safi
,
5329 (direct
== FILTER_OUT
) ? 1 : 0);
5334 if (filter
->plist
[direct
].name
)
5335 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
5336 filter
->plist
[direct
].name
= NULL
;
5337 filter
->plist
[direct
].plist
= NULL
;
5339 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5340 peer_on_policy_change(peer
, afi
, safi
,
5341 (direct
== FILTER_OUT
) ? 1 : 0);
5345 group
= peer
->group
;
5346 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5347 filter
= &peer
->filter
[afi
][safi
];
5349 if (filter
->plist
[direct
].name
)
5350 XFREE(MTYPE_BGP_FILTER_NAME
,
5351 filter
->plist
[direct
].name
);
5352 filter
->plist
[direct
].name
= NULL
;
5353 filter
->plist
[direct
].plist
= NULL
;
5354 peer_on_policy_change(peer
, afi
, safi
,
5355 (direct
== FILTER_OUT
) ? 1 : 0);
5361 /* Update prefix-list list. */
5362 static void peer_prefix_list_update(struct prefix_list
*plist
)
5364 struct listnode
*mnode
, *mnnode
;
5365 struct listnode
*node
, *nnode
;
5368 struct peer_group
*group
;
5369 struct bgp_filter
*filter
;
5374 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
5377 * Update the prefix-list on update groups.
5379 update_group_policy_update(
5380 bgp
, BGP_POLICY_PREFIX_LIST
,
5381 plist
? prefix_list_name(plist
) : NULL
, 0, 0);
5383 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
5384 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
5385 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
;
5387 filter
= &peer
->filter
[afi
][safi
];
5389 for (direct
= FILTER_IN
;
5390 direct
< FILTER_MAX
; direct
++) {
5391 if (filter
->plist
[direct
].name
)
5392 filter
->plist
[direct
]
5393 .plist
= prefix_list_lookup(
5395 filter
->plist
[direct
]
5398 filter
->plist
[direct
]
5403 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
5404 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
5405 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
;
5408 &group
->conf
->filter
[afi
][safi
];
5410 for (direct
= FILTER_IN
;
5411 direct
< FILTER_MAX
; direct
++) {
5412 if (filter
->plist
[direct
].name
)
5413 filter
->plist
[direct
]
5414 .plist
= prefix_list_lookup(
5416 filter
->plist
[direct
]
5419 filter
->plist
[direct
]
5427 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
5430 struct bgp_filter
*filter
;
5431 struct peer_group
*group
;
5432 struct listnode
*node
, *nnode
;
5434 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
5435 return BGP_ERR_INVALID_VALUE
;
5437 filter
= &peer
->filter
[afi
][safi
];
5439 if (filter
->aslist
[direct
].name
)
5440 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
5441 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5442 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
5444 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5445 peer_on_policy_change(peer
, afi
, safi
,
5446 (direct
== FILTER_OUT
) ? 1 : 0);
5450 group
= peer
->group
;
5451 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5452 filter
= &peer
->filter
[afi
][safi
];
5454 if (filter
->aslist
[direct
].name
)
5455 XFREE(MTYPE_BGP_FILTER_NAME
,
5456 filter
->aslist
[direct
].name
);
5457 filter
->aslist
[direct
].name
=
5458 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5459 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
5460 peer_on_policy_change(peer
, afi
, safi
,
5461 (direct
== FILTER_OUT
) ? 1 : 0);
5466 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
5468 struct bgp_filter
*filter
;
5469 struct bgp_filter
*gfilter
;
5470 struct peer_group
*group
;
5471 struct listnode
*node
, *nnode
;
5473 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
5474 return BGP_ERR_INVALID_VALUE
;
5476 filter
= &peer
->filter
[afi
][safi
];
5478 /* apply peer-group filter */
5479 if (peer_group_active(peer
)) {
5480 gfilter
= &peer
->group
->conf
->filter
[afi
][safi
];
5482 if (gfilter
->aslist
[direct
].name
) {
5483 if (filter
->aslist
[direct
].name
)
5484 XFREE(MTYPE_BGP_FILTER_NAME
,
5485 filter
->aslist
[direct
].name
);
5486 filter
->aslist
[direct
].name
=
5487 XSTRDUP(MTYPE_BGP_FILTER_NAME
,
5488 gfilter
->aslist
[direct
].name
);
5489 filter
->aslist
[direct
].aslist
=
5490 gfilter
->aslist
[direct
].aslist
;
5491 peer_on_policy_change(peer
, afi
, safi
,
5492 (direct
== FILTER_OUT
) ? 1 : 0);
5497 if (filter
->aslist
[direct
].name
)
5498 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
5499 filter
->aslist
[direct
].name
= NULL
;
5500 filter
->aslist
[direct
].aslist
= NULL
;
5502 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5503 peer_on_policy_change(peer
, afi
, safi
,
5504 (direct
== FILTER_OUT
) ? 1 : 0);
5508 group
= peer
->group
;
5509 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5510 filter
= &peer
->filter
[afi
][safi
];
5512 if (filter
->aslist
[direct
].name
)
5513 XFREE(MTYPE_BGP_FILTER_NAME
,
5514 filter
->aslist
[direct
].name
);
5515 filter
->aslist
[direct
].name
= NULL
;
5516 filter
->aslist
[direct
].aslist
= NULL
;
5517 peer_on_policy_change(peer
, afi
, safi
,
5518 (direct
== FILTER_OUT
) ? 1 : 0);
5524 static void peer_aslist_update(const char *aslist_name
)
5529 struct listnode
*mnode
, *mnnode
;
5530 struct listnode
*node
, *nnode
;
5533 struct peer_group
*group
;
5534 struct bgp_filter
*filter
;
5536 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
5537 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
5540 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
5541 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
5542 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
;
5544 filter
= &peer
->filter
[afi
][safi
];
5546 for (direct
= FILTER_IN
;
5547 direct
< FILTER_MAX
; direct
++) {
5548 if (filter
->aslist
[direct
].name
)
5549 filter
->aslist
[direct
]
5550 .aslist
= as_list_lookup(
5551 filter
->aslist
[direct
]
5554 filter
->aslist
[direct
]
5559 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
5560 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
5561 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
;
5564 &group
->conf
->filter
[afi
][safi
];
5566 for (direct
= FILTER_IN
;
5567 direct
< FILTER_MAX
; direct
++) {
5568 if (filter
->aslist
[direct
].name
)
5569 filter
->aslist
[direct
]
5570 .aslist
= as_list_lookup(
5571 filter
->aslist
[direct
]
5574 filter
->aslist
[direct
]
5582 static void peer_aslist_add(char *aslist_name
)
5584 peer_aslist_update(aslist_name
);
5585 route_map_notify_dependencies((char *)aslist_name
,
5586 RMAP_EVENT_ASLIST_ADDED
);
5589 static void peer_aslist_del(const char *aslist_name
)
5591 peer_aslist_update(aslist_name
);
5592 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
5596 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
5599 struct bgp_filter
*filter
;
5600 struct peer_group
*group
;
5601 struct listnode
*node
, *nnode
;
5603 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
5604 return BGP_ERR_INVALID_VALUE
;
5606 filter
= &peer
->filter
[afi
][safi
];
5608 if (filter
->map
[direct
].name
)
5609 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
5611 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5612 filter
->map
[direct
].map
= route_map_lookup_by_name(name
);
5614 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5615 peer_on_policy_change(peer
, afi
, safi
,
5616 (direct
== RMAP_OUT
) ? 1 : 0);
5620 group
= peer
->group
;
5621 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5622 filter
= &peer
->filter
[afi
][safi
];
5624 if (filter
->map
[direct
].name
)
5625 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
5626 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5627 filter
->map
[direct
].map
= route_map_lookup_by_name(name
);
5628 peer_on_policy_change(peer
, afi
, safi
,
5629 (direct
== RMAP_OUT
) ? 1 : 0);
5634 /* Unset route-map from the peer. */
5635 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
5637 struct bgp_filter
*filter
;
5638 struct bgp_filter
*gfilter
;
5639 struct peer_group
*group
;
5640 struct listnode
*node
, *nnode
;
5642 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
5643 return BGP_ERR_INVALID_VALUE
;
5645 filter
= &peer
->filter
[afi
][safi
];
5647 /* apply peer-group filter */
5648 if (peer_group_active(peer
)) {
5649 gfilter
= &peer
->group
->conf
->filter
[afi
][safi
];
5651 if (gfilter
->map
[direct
].name
) {
5652 if (filter
->map
[direct
].name
)
5653 XFREE(MTYPE_BGP_FILTER_NAME
,
5654 filter
->map
[direct
].name
);
5655 filter
->map
[direct
].name
=
5656 XSTRDUP(MTYPE_BGP_FILTER_NAME
,
5657 gfilter
->map
[direct
].name
);
5658 filter
->map
[direct
].map
= gfilter
->map
[direct
].map
;
5659 peer_on_policy_change(peer
, afi
, safi
,
5660 (direct
== RMAP_OUT
) ? 1 : 0);
5665 if (filter
->map
[direct
].name
)
5666 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
5667 filter
->map
[direct
].name
= NULL
;
5668 filter
->map
[direct
].map
= NULL
;
5670 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5671 peer_on_policy_change(peer
, afi
, safi
,
5672 (direct
== RMAP_OUT
) ? 1 : 0);
5676 group
= peer
->group
;
5677 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5678 filter
= &peer
->filter
[afi
][safi
];
5680 if (filter
->map
[direct
].name
)
5681 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
5682 filter
->map
[direct
].name
= NULL
;
5683 filter
->map
[direct
].map
= NULL
;
5684 peer_on_policy_change(peer
, afi
, safi
,
5685 (direct
== RMAP_OUT
) ? 1 : 0);
5690 /* Set unsuppress-map to the peer. */
5691 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5694 struct bgp_filter
*filter
;
5695 struct peer_group
*group
;
5696 struct listnode
*node
, *nnode
;
5698 filter
= &peer
->filter
[afi
][safi
];
5700 if (filter
->usmap
.name
)
5701 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
5703 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5704 filter
->usmap
.map
= route_map_lookup_by_name(name
);
5706 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5707 peer_on_policy_change(peer
, afi
, safi
, 1);
5711 group
= peer
->group
;
5712 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5713 filter
= &peer
->filter
[afi
][safi
];
5715 if (filter
->usmap
.name
)
5716 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
5717 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
5718 filter
->usmap
.map
= route_map_lookup_by_name(name
);
5719 peer_on_policy_change(peer
, afi
, safi
, 1);
5724 /* Unset route-map from the peer. */
5725 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5727 struct bgp_filter
*filter
;
5728 struct peer_group
*group
;
5729 struct listnode
*node
, *nnode
;
5731 filter
= &peer
->filter
[afi
][safi
];
5733 if (filter
->usmap
.name
)
5734 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
5735 filter
->usmap
.name
= NULL
;
5736 filter
->usmap
.map
= NULL
;
5738 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5739 peer_on_policy_change(peer
, afi
, safi
, 1);
5743 group
= peer
->group
;
5744 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5745 filter
= &peer
->filter
[afi
][safi
];
5747 if (filter
->usmap
.name
)
5748 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
5749 filter
->usmap
.name
= NULL
;
5750 filter
->usmap
.map
= NULL
;
5751 peer_on_policy_change(peer
, afi
, safi
, 1);
5756 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5757 u_int32_t max
, u_char threshold
, int warning
,
5760 struct peer_group
*group
;
5761 struct listnode
*node
, *nnode
;
5763 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX
);
5764 peer
->pmax
[afi
][safi
] = max
;
5765 peer
->pmax_threshold
[afi
][safi
] = threshold
;
5766 peer
->pmax_restart
[afi
][safi
] = restart
;
5768 SET_FLAG(peer
->af_flags
[afi
][safi
],
5769 PEER_FLAG_MAX_PREFIX_WARNING
);
5771 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
5772 PEER_FLAG_MAX_PREFIX_WARNING
);
5774 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5775 group
= peer
->group
;
5776 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5777 SET_FLAG(peer
->af_flags
[afi
][safi
],
5778 PEER_FLAG_MAX_PREFIX
);
5779 peer
->pmax
[afi
][safi
] = max
;
5780 peer
->pmax_threshold
[afi
][safi
] = threshold
;
5781 peer
->pmax_restart
[afi
][safi
] = restart
;
5783 SET_FLAG(peer
->af_flags
[afi
][safi
],
5784 PEER_FLAG_MAX_PREFIX_WARNING
);
5786 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
5787 PEER_FLAG_MAX_PREFIX_WARNING
);
5789 if ((peer
->status
== Established
)
5790 && (peer
->afc
[afi
][safi
]))
5791 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
5794 if ((peer
->status
== Established
) && (peer
->afc
[afi
][safi
]))
5795 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
5801 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5803 struct peer_group
*group
;
5804 struct listnode
*node
, *nnode
;
5806 /* apply peer-group config */
5807 if (peer_group_active(peer
)) {
5808 if (CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
],
5809 PEER_FLAG_MAX_PREFIX
))
5810 SET_FLAG(peer
->af_flags
[afi
][safi
],
5811 PEER_FLAG_MAX_PREFIX
);
5813 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
5814 PEER_FLAG_MAX_PREFIX
);
5816 if (CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
],
5817 PEER_FLAG_MAX_PREFIX_WARNING
))
5818 SET_FLAG(peer
->af_flags
[afi
][safi
],
5819 PEER_FLAG_MAX_PREFIX_WARNING
);
5821 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
5822 PEER_FLAG_MAX_PREFIX_WARNING
);
5824 peer
->pmax
[afi
][safi
] = peer
->group
->conf
->pmax
[afi
][safi
];
5825 peer
->pmax_threshold
[afi
][safi
] =
5826 peer
->group
->conf
->pmax_threshold
[afi
][safi
];
5827 peer
->pmax_restart
[afi
][safi
] =
5828 peer
->group
->conf
->pmax_restart
[afi
][safi
];
5832 UNSET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX
);
5833 UNSET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX_WARNING
);
5834 peer
->pmax
[afi
][safi
] = 0;
5835 peer
->pmax_threshold
[afi
][safi
] = 0;
5836 peer
->pmax_restart
[afi
][safi
] = 0;
5838 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5841 group
= peer
->group
;
5842 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
5843 UNSET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX
);
5844 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
5845 PEER_FLAG_MAX_PREFIX_WARNING
);
5846 peer
->pmax
[afi
][safi
] = 0;
5847 peer
->pmax_threshold
[afi
][safi
] = 0;
5848 peer
->pmax_restart
[afi
][safi
] = 0;
5853 int is_ebgp_multihop_configured(struct peer
*peer
)
5855 struct peer_group
*group
;
5856 struct listnode
*node
, *nnode
;
5859 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5860 group
= peer
->group
;
5861 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
5862 && (group
->conf
->ttl
!= 1))
5865 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
5866 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
5867 && (peer1
->ttl
!= 1))
5871 if ((peer_sort(peer
) != BGP_PEER_IBGP
) && (peer
->ttl
!= 1))
5877 /* Set # of hops between us and BGP peer. */
5878 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
5880 struct peer_group
*group
;
5881 struct listnode
*node
, *nnode
;
5884 zlog_debug("peer_ttl_security_hops_set: set gtsm_hops to %d for %s",
5885 gtsm_hops
, peer
->host
);
5887 /* We cannot configure ttl-security hops when ebgp-multihop is already
5888 set. For non peer-groups, the check is simple. For peer-groups,
5890 slightly messy, because we need to check both the peer-group
5892 and all peer-group members for any trace of ebgp-multihop
5894 before actually applying the ttl-security rules. Cisco really made a
5895 mess of this configuration parameter, and OpenBGPD got it right.
5898 if ((peer
->gtsm_hops
== 0) && (peer
->sort
!= BGP_PEER_IBGP
)) {
5899 if (is_ebgp_multihop_configured(peer
))
5900 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
5902 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5903 peer
->gtsm_hops
= gtsm_hops
;
5905 /* Calling ebgp multihop also resets the session.
5906 * On restart, NHT will get setup correctly as will the
5907 * min & max ttls on the socket. The return value is
5910 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
5915 group
= peer
->group
;
5916 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
5918 peer
->gtsm_hops
= group
->conf
->gtsm_hops
;
5920 /* Calling ebgp multihop also resets the
5922 * On restart, NHT will get setup correctly as
5924 * min & max ttls on the socket. The return
5928 peer_ebgp_multihop_set(peer
, MAXTTL
);
5932 /* Post the first gtsm setup or if its ibgp, maxttl setting
5934 * necessary, just set the minttl.
5936 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5937 peer
->gtsm_hops
= gtsm_hops
;
5940 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
5941 MAXTTL
+ 1 - gtsm_hops
);
5942 if ((peer
->status
< Established
) && peer
->doppelganger
5943 && (peer
->doppelganger
->fd
>= 0))
5944 sockopt_minttl(peer
->su
.sa
.sa_family
,
5945 peer
->doppelganger
->fd
,
5946 MAXTTL
+ 1 - gtsm_hops
);
5948 group
= peer
->group
;
5949 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
5951 peer
->gtsm_hops
= group
->conf
->gtsm_hops
;
5953 /* Change setting of existing peer
5954 * established then change value (may break
5956 * not established yet (teardown session and
5958 * no session then do nothing (will get
5959 * handled by next connection)
5961 if (peer
->fd
>= 0 && peer
->gtsm_hops
!= 0)
5963 peer
->su
.sa
.sa_family
, peer
->fd
,
5964 MAXTTL
+ 1 - peer
->gtsm_hops
);
5965 if ((peer
->status
< Established
)
5966 && peer
->doppelganger
5967 && (peer
->doppelganger
->fd
>= 0))
5968 sockopt_minttl(peer
->su
.sa
.sa_family
,
5969 peer
->doppelganger
->fd
,
5970 MAXTTL
+ 1 - gtsm_hops
);
5978 int peer_ttl_security_hops_unset(struct peer
*peer
)
5980 struct peer_group
*group
;
5981 struct listnode
*node
, *nnode
;
5984 zlog_debug("peer_ttl_security_hops_unset: set gtsm_hops to zero for %s",
5987 /* if a peer-group member, then reset to peer-group default rather than
5989 if (peer_group_active(peer
))
5990 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
5992 peer
->gtsm_hops
= 0;
5994 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5995 /* Invoking ebgp_multihop_set will set the TTL back to the
5997 * value as well as restting the NHT and such. The session is
6000 if (peer
->sort
== BGP_PEER_EBGP
)
6001 ret
= peer_ebgp_multihop_unset(peer
);
6004 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
6007 if ((peer
->status
< Established
) && peer
->doppelganger
6008 && (peer
->doppelganger
->fd
>= 0))
6009 sockopt_minttl(peer
->su
.sa
.sa_family
,
6010 peer
->doppelganger
->fd
, 0);
6013 group
= peer
->group
;
6014 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
6015 peer
->gtsm_hops
= 0;
6016 if (peer
->sort
== BGP_PEER_EBGP
)
6017 ret
= peer_ebgp_multihop_unset(peer
);
6020 sockopt_minttl(peer
->su
.sa
.sa_family
,
6023 if ((peer
->status
< Established
)
6024 && peer
->doppelganger
6025 && (peer
->doppelganger
->fd
>= 0))
6026 sockopt_minttl(peer
->su
.sa
.sa_family
,
6027 peer
->doppelganger
->fd
,
6037 * If peer clear is invoked in a loop for all peers on the BGP instance,
6038 * it may end up freeing the doppelganger, and if this was the next node
6039 * to the current node, we would end up accessing the freed next node.
6040 * Pass along additional parameter which can be updated if next node
6041 * is freed; only required when walking the peer list on BGP instance.
6043 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
6045 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)) {
6046 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
)) {
6047 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
6048 if (peer
->t_pmax_restart
) {
6049 BGP_TIMER_OFF(peer
->t_pmax_restart
);
6050 if (bgp_debug_neighbor_events(peer
))
6052 "%s Maximum-prefix restart timer canceled",
6055 BGP_EVENT_ADD(peer
, BGP_Start
);
6059 peer
->v_start
= BGP_INIT_START_TIMER
;
6060 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6061 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6062 BGP_NOTIFY_CEASE_ADMIN_RESET
);
6064 bgp_session_reset_safe(peer
, nnode
);
6069 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
6070 enum bgp_clear_type stype
)
6072 struct peer_af
*paf
;
6074 if (peer
->status
!= Established
)
6077 if (!peer
->afc
[afi
][safi
])
6078 return BGP_ERR_AF_UNCONFIGURED
;
6080 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
6082 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
6083 /* Clear the "neighbor x.x.x.x default-originate" flag */
6084 paf
= peer_af_find(peer
, afi
, safi
);
6085 if (paf
&& paf
->subgroup
6086 && CHECK_FLAG(paf
->subgroup
->sflags
,
6087 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
6088 UNSET_FLAG(paf
->subgroup
->sflags
,
6089 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
6091 bgp_announce_route(peer
, afi
, safi
);
6094 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
6095 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
6096 PEER_CAP_ORF_PREFIX_SM_ADV
)
6097 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
6098 PEER_CAP_ORF_PREFIX_RM_RCV
)
6099 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
6100 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
6101 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
6104 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
6105 PEER_CAP_ORF_PREFIX_RM_RCV
))
6106 prefix_type
= ORF_TYPE_PREFIX
;
6108 prefix_type
= ORF_TYPE_PREFIX_OLD
;
6110 if (filter
->plist
[FILTER_IN
].plist
) {
6111 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
6112 PEER_STATUS_ORF_PREFIX_SEND
))
6113 bgp_route_refresh_send(
6114 peer
, afi
, safi
, prefix_type
,
6116 bgp_route_refresh_send(peer
, afi
, safi
,
6118 REFRESH_IMMEDIATE
, 0);
6120 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
6121 PEER_STATUS_ORF_PREFIX_SEND
))
6122 bgp_route_refresh_send(
6123 peer
, afi
, safi
, prefix_type
,
6124 REFRESH_IMMEDIATE
, 1);
6126 bgp_route_refresh_send(peer
, afi
, safi
,
6133 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
6134 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
6135 /* If neighbor has soft reconfiguration inbound flag.
6136 Use Adj-RIB-In database. */
6137 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6138 PEER_FLAG_SOFT_RECONFIG
))
6139 bgp_soft_reconfig_in(peer
, afi
, safi
);
6141 /* If neighbor has route refresh capability, send route
6143 message to the peer. */
6144 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
6145 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
6146 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0,
6149 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
6155 /* Display peer uptime.*/
6156 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, u_char use_json
,
6159 time_t uptime1
, epoch_tbuf
;
6162 /* Check buffer length. */
6163 if (len
< BGP_UPTIME_LEN
) {
6165 zlog_warn("peer_uptime (): buffer shortage %lu",
6167 /* XXX: should return status instead of buf... */
6168 snprintf(buf
, len
, "<error> ");
6173 /* If there is no connection has been done before print `never'. */
6176 json_object_string_add(json
, "peerUptime", "never");
6177 json_object_int_add(json
, "peerUptimeMsec", 0);
6179 snprintf(buf
, len
, "never");
6183 /* Get current time. */
6184 uptime1
= bgp_clock();
6186 tm
= gmtime(&uptime1
);
6188 if (uptime1
< ONE_DAY_SECOND
)
6189 snprintf(buf
, len
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
6191 else if (uptime1
< ONE_WEEK_SECOND
)
6192 snprintf(buf
, len
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
6194 else if (uptime1
< ONE_YEAR_SECOND
)
6195 snprintf(buf
, len
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
6196 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
6198 snprintf(buf
, len
, "%02dy%02dw%dd", tm
->tm_year
- 70,
6200 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7));
6203 epoch_tbuf
= time(NULL
) - uptime1
;
6204 json_object_string_add(json
, "peerUptime", buf
);
6205 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
6206 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
6213 static void bgp_config_write_filter(struct vty
*vty
, struct peer
*peer
,
6214 afi_t afi
, safi_t safi
)
6216 struct bgp_filter
*filter
;
6217 struct bgp_filter
*gfilter
= NULL
;
6220 int out
= FILTER_OUT
;
6223 filter
= &peer
->filter
[afi
][safi
];
6225 if (peer_group_active(peer
))
6226 gfilter
= &peer
->group
->conf
->filter
[afi
][safi
];
6228 /* distribute-list. */
6229 if (filter
->dlist
[in
].name
)
6230 if (!gfilter
|| !gfilter
->dlist
[in
].name
6231 || strcmp(filter
->dlist
[in
].name
, gfilter
->dlist
[in
].name
)
6233 vty_out(vty
, " neighbor %s distribute-list %s in\n",
6234 addr
, filter
->dlist
[in
].name
);
6237 if (filter
->dlist
[out
].name
&& !gfilter
) {
6238 vty_out(vty
, " neighbor %s distribute-list %s out\n", addr
,
6239 filter
->dlist
[out
].name
);
6243 if (filter
->plist
[in
].name
)
6244 if (!gfilter
|| !gfilter
->plist
[in
].name
6245 || strcmp(filter
->plist
[in
].name
, gfilter
->plist
[in
].name
)
6247 vty_out(vty
, " neighbor %s prefix-list %s in\n", addr
,
6248 filter
->plist
[in
].name
);
6251 if (filter
->plist
[out
].name
)
6252 if (!gfilter
|| !gfilter
->plist
[out
].name
6253 || strcmp(filter
->plist
[out
].name
, gfilter
->plist
[out
].name
)
6255 vty_out(vty
, " neighbor %s prefix-list %s out\n", addr
,
6256 filter
->plist
[out
].name
);
6260 if (filter
->map
[RMAP_IN
].name
)
6261 if (!gfilter
|| !gfilter
->map
[RMAP_IN
].name
6262 || strcmp(filter
->map
[RMAP_IN
].name
,
6263 gfilter
->map
[RMAP_IN
].name
)
6265 vty_out(vty
, " neighbor %s route-map %s in\n", addr
,
6266 filter
->map
[RMAP_IN
].name
);
6269 if (filter
->map
[RMAP_OUT
].name
)
6270 if (!gfilter
|| !gfilter
->map
[RMAP_OUT
].name
6271 || strcmp(filter
->map
[RMAP_OUT
].name
,
6272 gfilter
->map
[RMAP_OUT
].name
)
6274 vty_out(vty
, " neighbor %s route-map %s out\n", addr
,
6275 filter
->map
[RMAP_OUT
].name
);
6278 /* unsuppress-map */
6279 if (filter
->usmap
.name
&& !gfilter
) {
6280 vty_out(vty
, " neighbor %s unsuppress-map %s\n", addr
,
6281 filter
->usmap
.name
);
6285 if (filter
->aslist
[in
].name
)
6286 if (!gfilter
|| !gfilter
->aslist
[in
].name
6287 || strcmp(filter
->aslist
[in
].name
, gfilter
->aslist
[in
].name
)
6289 vty_out(vty
, " neighbor %s filter-list %s in\n", addr
,
6290 filter
->aslist
[in
].name
);
6293 if (filter
->aslist
[out
].name
&& !gfilter
) {
6294 vty_out(vty
, " neighbor %s filter-list %s out\n", addr
,
6295 filter
->aslist
[out
].name
);
6299 /* BGP peer configuration display function. */
6300 static void bgp_config_write_peer_global(struct vty
*vty
, struct bgp
*bgp
,
6303 struct peer
*g_peer
= NULL
;
6304 char buf
[SU_ADDRSTRLEN
];
6306 int if_pg_printed
= FALSE
;
6307 int if_ras_printed
= FALSE
;
6309 /* Skip dynamic neighbors. */
6310 if (peer_dynamic_neighbor(peer
))
6314 addr
= peer
->conf_if
;
6318 /************************************
6319 ****** Global to the neighbor ******
6320 ************************************/
6321 if (peer
->conf_if
) {
6322 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
6323 vty_out(vty
, " neighbor %s interface v6only", addr
);
6325 vty_out(vty
, " neighbor %s interface", addr
);
6327 if (peer_group_active(peer
)) {
6328 vty_out(vty
, " peer-group %s", peer
->group
->name
);
6329 if_pg_printed
= TRUE
;
6330 } else if (peer
->as_type
== AS_SPECIFIED
) {
6331 vty_out(vty
, " remote-as %u", peer
->as
);
6332 if_ras_printed
= TRUE
;
6333 } else if (peer
->as_type
== AS_INTERNAL
) {
6334 vty_out(vty
, " remote-as internal");
6335 if_ras_printed
= TRUE
;
6336 } else if (peer
->as_type
== AS_EXTERNAL
) {
6337 vty_out(vty
, " remote-as external");
6338 if_ras_printed
= TRUE
;
6344 /* remote-as and peer-group */
6345 /* peer is a member of a peer-group */
6346 if (peer_group_active(peer
)) {
6347 g_peer
= peer
->group
->conf
;
6349 if (g_peer
->as_type
== AS_UNSPECIFIED
&& !if_ras_printed
) {
6350 if (peer
->as_type
== AS_SPECIFIED
) {
6351 vty_out(vty
, " neighbor %s remote-as %u\n",
6353 } else if (peer
->as_type
== AS_INTERNAL
) {
6355 " neighbor %s remote-as internal\n",
6357 } else if (peer
->as_type
== AS_EXTERNAL
) {
6359 " neighbor %s remote-as external\n",
6364 /* For swpX peers we displayed the peer-group
6365 * via 'neighbor swpX interface peer-group WORD' */
6367 vty_out(vty
, " neighbor %s peer-group %s\n", addr
,
6371 /* peer is NOT a member of a peer-group */
6373 /* peer is a peer-group, declare the peer-group */
6374 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6375 vty_out(vty
, " neighbor %s peer-group\n", addr
);
6378 if (!if_ras_printed
) {
6379 if (peer
->as_type
== AS_SPECIFIED
) {
6380 vty_out(vty
, " neighbor %s remote-as %u\n",
6382 } else if (peer
->as_type
== AS_INTERNAL
) {
6384 " neighbor %s remote-as internal\n",
6386 } else if (peer
->as_type
== AS_EXTERNAL
) {
6388 " neighbor %s remote-as external\n",
6395 if (peer
->change_local_as
) {
6396 if (!peer_group_active(peer
)
6397 || peer
->change_local_as
!= g_peer
->change_local_as
6398 || (CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
)
6399 != CHECK_FLAG(g_peer
->flags
,
6400 PEER_FLAG_LOCAL_AS_NO_PREPEND
))
6401 || (CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
)
6402 != CHECK_FLAG(g_peer
->flags
,
6403 PEER_FLAG_LOCAL_AS_REPLACE_AS
))) {
6404 vty_out(vty
, " neighbor %s local-as %u%s%s\n", addr
,
6405 peer
->change_local_as
,
6406 CHECK_FLAG(peer
->flags
,
6407 PEER_FLAG_LOCAL_AS_NO_PREPEND
)
6410 CHECK_FLAG(peer
->flags
,
6411 PEER_FLAG_LOCAL_AS_REPLACE_AS
)
6419 vty_out(vty
, " neighbor %s description %s\n", addr
, peer
->desc
);
6423 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)) {
6424 if (!peer_group_active(peer
)
6425 || !CHECK_FLAG(g_peer
->flags
, PEER_FLAG_SHUTDOWN
)
6426 || peer
->tx_shutdown_message
) {
6427 if (peer
->tx_shutdown_message
)
6429 " neighbor %s shutdown message %s\n",
6430 addr
, peer
->tx_shutdown_message
);
6432 vty_out(vty
, " neighbor %s shutdown\n", addr
);
6437 if (peer
->bfd_info
) {
6438 if (!peer_group_active(peer
) || !g_peer
->bfd_info
) {
6439 bgp_bfd_peer_config_write(vty
, peer
, addr
);
6444 if (peer
->password
) {
6445 if (!peer_group_active(peer
) || !g_peer
->password
6446 || strcmp(peer
->password
, g_peer
->password
) != 0) {
6447 vty_out(vty
, " neighbor %s password %s\n", addr
,
6453 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
)) {
6454 if (!peer_group_active(peer
)) {
6455 vty_out(vty
, " neighbor %s solo\n", addr
);
6460 if (peer
->port
!= BGP_PORT_DEFAULT
) {
6461 vty_out(vty
, " neighbor %s port %d\n", addr
, peer
->port
);
6464 /* Local interface name */
6466 vty_out(vty
, " neighbor %s interface %s\n", addr
, peer
->ifname
);
6470 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
6471 if (!peer_group_active(peer
)
6472 || !CHECK_FLAG(g_peer
->flags
, PEER_FLAG_PASSIVE
)) {
6473 vty_out(vty
, " neighbor %s passive\n", addr
);
6478 if (peer
->sort
!= BGP_PEER_IBGP
&& peer
->ttl
!= 1
6479 && !(peer
->gtsm_hops
!= 0 && peer
->ttl
== MAXTTL
)) {
6480 if (!peer_group_active(peer
) || g_peer
->ttl
!= peer
->ttl
) {
6481 vty_out(vty
, " neighbor %s ebgp-multihop %d\n", addr
,
6486 /* ttl-security hops */
6487 if (peer
->gtsm_hops
!= 0) {
6488 if (!peer_group_active(peer
)
6489 || g_peer
->gtsm_hops
!= peer
->gtsm_hops
) {
6490 vty_out(vty
, " neighbor %s ttl-security hops %d\n",
6491 addr
, peer
->gtsm_hops
);
6495 /* disable-connected-check */
6496 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)) {
6497 if (!peer_group_active(peer
)
6498 || !CHECK_FLAG(g_peer
->flags
,
6499 PEER_FLAG_DISABLE_CONNECTED_CHECK
)) {
6500 vty_out(vty
, " neighbor %s disable-connected-check\n",
6506 if (peer
->update_if
) {
6507 if (!peer_group_active(peer
) || !g_peer
->update_if
6508 || strcmp(g_peer
->update_if
, peer
->update_if
) != 0) {
6509 vty_out(vty
, " neighbor %s update-source %s\n", addr
,
6513 if (peer
->update_source
) {
6514 if (!peer_group_active(peer
) || !g_peer
->update_source
6515 || sockunion_cmp(g_peer
->update_source
, peer
->update_source
)
6517 vty_out(vty
, " neighbor %s update-source %s\n", addr
,
6518 sockunion2str(peer
->update_source
, buf
,
6523 /* advertisement-interval */
6524 if (CHECK_FLAG(peer
->config
, PEER_CONFIG_ROUTEADV
)
6525 && ((!peer_group_active(peer
)
6526 && peer
->v_routeadv
!= BGP_DEFAULT_EBGP_ROUTEADV
)
6527 || (peer_group_active(peer
)
6528 && peer
->v_routeadv
!= g_peer
->v_routeadv
))) {
6529 vty_out(vty
, " neighbor %s advertisement-interval %u\n", addr
,
6534 if (CHECK_FLAG(peer
->config
, PEER_CONFIG_TIMER
)
6535 && ((!peer_group_active(peer
)
6536 && (peer
->keepalive
!= BGP_DEFAULT_KEEPALIVE
6537 || peer
->holdtime
!= BGP_DEFAULT_HOLDTIME
))
6538 || (peer_group_active(peer
)
6539 && (peer
->keepalive
!= g_peer
->keepalive
6540 || peer
->holdtime
!= g_peer
->holdtime
)))) {
6541 vty_out(vty
, " neighbor %s timers %u %u\n", addr
,
6542 peer
->keepalive
, peer
->holdtime
);
6545 if (CHECK_FLAG(peer
->config
, PEER_CONFIG_CONNECT
)
6546 && ((!peer_group_active(peer
)
6547 && peer
->connect
!= BGP_DEFAULT_CONNECT_RETRY
)
6548 || (peer_group_active(peer
)
6549 && peer
->connect
!= g_peer
->connect
)))
6552 vty_out(vty
, " neighbor %s timers connect %u\n", addr
,
6556 /* capability dynamic */
6557 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_CAPABILITY
)) {
6558 if (!peer_group_active(peer
)
6559 || !CHECK_FLAG(g_peer
->flags
,
6560 PEER_FLAG_DYNAMIC_CAPABILITY
)) {
6561 vty_out(vty
, " neighbor %s capability dynamic\n", addr
);
6565 /* capability extended-nexthop */
6566 if (peer
->ifp
&& !CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
)) {
6567 if (!peer_group_active(peer
)
6568 || !CHECK_FLAG(g_peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
)) {
6570 " no neighbor %s capability extended-nexthop\n",
6575 if (!peer
->ifp
&& CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
)) {
6576 if (!peer_group_active(peer
)
6577 || !CHECK_FLAG(g_peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
)) {
6579 " neighbor %s capability extended-nexthop\n",
6584 /* dont-capability-negotiation */
6585 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_DONT_CAPABILITY
)) {
6586 if (!peer_group_active(peer
)
6587 || !CHECK_FLAG(g_peer
->flags
, PEER_FLAG_DONT_CAPABILITY
)) {
6588 vty_out(vty
, " neighbor %s dont-capability-negotiate\n",
6593 /* override-capability */
6594 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
6595 if (!peer_group_active(peer
)
6596 || !CHECK_FLAG(g_peer
->flags
,
6597 PEER_FLAG_OVERRIDE_CAPABILITY
)) {
6598 vty_out(vty
, " neighbor %s override-capability\n",
6603 /* strict-capability-match */
6604 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_STRICT_CAP_MATCH
)) {
6605 if (!peer_group_active(peer
)
6606 || !CHECK_FLAG(g_peer
->flags
, PEER_FLAG_STRICT_CAP_MATCH
)) {
6607 vty_out(vty
, " neighbor %s strict-capability-match\n",
6613 /* BGP peer configuration display function. */
6614 static void bgp_config_write_peer_af(struct vty
*vty
, struct bgp
*bgp
,
6615 struct peer
*peer
, afi_t afi
, safi_t safi
)
6617 struct peer
*g_peer
= NULL
;
6620 /* Skip dynamic neighbors. */
6621 if (peer_dynamic_neighbor(peer
))
6625 addr
= peer
->conf_if
;
6629 /************************************
6630 ****** Per AF to the neighbor ******
6631 ************************************/
6632 if (peer_group_active(peer
)) {
6633 g_peer
= peer
->group
->conf
;
6635 /* If the peer-group is active but peer is not, print a 'no
6637 if (g_peer
->afc
[afi
][safi
] && !peer
->afc
[afi
][safi
]) {
6638 vty_out(vty
, " no neighbor %s activate\n", addr
);
6641 /* If the peer-group is not active but peer is, print an
6643 else if (!g_peer
->afc
[afi
][safi
] && peer
->afc
[afi
][safi
]) {
6644 vty_out(vty
, " neighbor %s activate\n", addr
);
6647 if (peer
->afc
[afi
][safi
]) {
6648 if ((afi
== AFI_IP
) && (safi
== SAFI_UNICAST
)) {
6649 if (bgp_flag_check(bgp
,
6650 BGP_FLAG_NO_DEFAULT_IPV4
)) {
6651 vty_out(vty
, " neighbor %s activate\n",
6655 vty_out(vty
, " neighbor %s activate\n", addr
);
6657 if ((afi
== AFI_IP
) && (safi
== SAFI_UNICAST
)) {
6658 if (!bgp_flag_check(bgp
,
6659 BGP_FLAG_NO_DEFAULT_IPV4
)) {
6661 " no neighbor %s activate\n",
6668 /* addpath TX knobs */
6669 if (peergroup_af_flag_check(peer
, afi
, safi
,
6670 PEER_FLAG_ADDPATH_TX_ALL_PATHS
)) {
6671 vty_out(vty
, " neighbor %s addpath-tx-all-paths\n", addr
);
6674 if (peergroup_af_flag_check(peer
, afi
, safi
,
6675 PEER_FLAG_ADDPATH_TX_BESTPATH_PER_AS
)) {
6676 vty_out(vty
, " neighbor %s addpath-tx-bestpath-per-AS\n",
6680 /* ORF capability. */
6681 if (peergroup_af_flag_check(peer
, afi
, safi
, PEER_FLAG_ORF_PREFIX_SM
)
6682 || peergroup_af_flag_check(peer
, afi
, safi
,
6683 PEER_FLAG_ORF_PREFIX_RM
)) {
6684 vty_out(vty
, " neighbor %s capability orf prefix-list", addr
);
6686 if (peergroup_af_flag_check(peer
, afi
, safi
,
6687 PEER_FLAG_ORF_PREFIX_SM
)
6688 && peergroup_af_flag_check(peer
, afi
, safi
,
6689 PEER_FLAG_ORF_PREFIX_RM
))
6690 vty_out(vty
, " both");
6691 else if (peergroup_af_flag_check(peer
, afi
, safi
,
6692 PEER_FLAG_ORF_PREFIX_SM
))
6693 vty_out(vty
, " send");
6695 vty_out(vty
, " receive");
6699 /* Route reflector client. */
6700 if (peergroup_af_flag_check(peer
, afi
, safi
,
6701 PEER_FLAG_REFLECTOR_CLIENT
)) {
6702 vty_out(vty
, " neighbor %s route-reflector-client\n", addr
);
6705 /* next-hop-self force */
6706 if (peergroup_af_flag_check(peer
, afi
, safi
,
6707 PEER_FLAG_FORCE_NEXTHOP_SELF
)) {
6708 vty_out(vty
, " neighbor %s next-hop-self force\n", addr
);
6712 if (peergroup_af_flag_check(peer
, afi
, safi
, PEER_FLAG_NEXTHOP_SELF
)) {
6713 vty_out(vty
, " neighbor %s next-hop-self\n", addr
);
6716 /* remove-private-AS */
6717 if (peergroup_af_flag_check(peer
, afi
, safi
,
6718 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
)) {
6719 vty_out(vty
, " neighbor %s remove-private-AS all replace-AS\n",
6723 else if (peergroup_af_flag_check(peer
, afi
, safi
,
6724 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
)) {
6725 vty_out(vty
, " neighbor %s remove-private-AS replace-AS\n",
6729 else if (peergroup_af_flag_check(peer
, afi
, safi
,
6730 PEER_FLAG_REMOVE_PRIVATE_AS_ALL
)) {
6731 vty_out(vty
, " neighbor %s remove-private-AS all\n", addr
);
6734 else if (peergroup_af_flag_check(peer
, afi
, safi
,
6735 PEER_FLAG_REMOVE_PRIVATE_AS
)) {
6736 vty_out(vty
, " neighbor %s remove-private-AS\n", addr
);
6740 if (peergroup_af_flag_check(peer
, afi
, safi
, PEER_FLAG_AS_OVERRIDE
)) {
6741 vty_out(vty
, " neighbor %s as-override\n", addr
);
6744 /* send-community print. */
6745 if (bgp_option_check(BGP_OPT_CONFIG_CISCO
)) {
6746 if (peergroup_af_flag_check(peer
, afi
, safi
,
6747 PEER_FLAG_SEND_COMMUNITY
)
6748 && peergroup_af_flag_check(peer
, afi
, safi
,
6749 PEER_FLAG_SEND_EXT_COMMUNITY
)
6750 && peergroup_af_flag_check(
6752 PEER_FLAG_SEND_LARGE_COMMUNITY
)) {
6753 vty_out(vty
, " neighbor %s send-community all\n",
6755 } else if (peergroup_af_flag_check(
6757 PEER_FLAG_SEND_LARGE_COMMUNITY
)) {
6758 vty_out(vty
, " neighbor %s send-community large\n",
6760 } else if (peergroup_af_flag_check(
6762 PEER_FLAG_SEND_EXT_COMMUNITY
)) {
6763 vty_out(vty
, " neighbor %s send-community extended\n",
6765 } else if (peergroup_af_flag_check(peer
, afi
, safi
,
6766 PEER_FLAG_SEND_COMMUNITY
)) {
6767 vty_out(vty
, " neighbor %s send-community\n", addr
);
6770 if (!peer_af_flag_check(peer
, afi
, safi
,
6771 PEER_FLAG_SEND_COMMUNITY
)
6772 && (!g_peer
|| peer_af_flag_check(g_peer
, afi
, safi
,
6773 PEER_FLAG_SEND_COMMUNITY
))
6774 && !peer_af_flag_check(peer
, afi
, safi
,
6775 PEER_FLAG_SEND_EXT_COMMUNITY
)
6777 || peer_af_flag_check(g_peer
, afi
, safi
,
6778 PEER_FLAG_SEND_EXT_COMMUNITY
))
6779 && !peer_af_flag_check(peer
, afi
, safi
,
6780 PEER_FLAG_SEND_LARGE_COMMUNITY
)
6781 && (!g_peer
|| peer_af_flag_check(
6783 PEER_FLAG_SEND_LARGE_COMMUNITY
))) {
6784 vty_out(vty
, " no neighbor %s send-community all\n",
6787 if (!peer_af_flag_check(peer
, afi
, safi
,
6788 PEER_FLAG_SEND_LARGE_COMMUNITY
)
6790 || peer_af_flag_check(
6792 PEER_FLAG_SEND_LARGE_COMMUNITY
))) {
6794 " no neighbor %s send-community large\n",
6798 if (!peer_af_flag_check(peer
, afi
, safi
,
6799 PEER_FLAG_SEND_EXT_COMMUNITY
)
6801 || peer_af_flag_check(
6803 PEER_FLAG_SEND_EXT_COMMUNITY
))) {
6805 " no neighbor %s send-community extended\n",
6809 if (!peer_af_flag_check(peer
, afi
, safi
,
6810 PEER_FLAG_SEND_COMMUNITY
)
6811 && (!g_peer
|| peer_af_flag_check(
6813 PEER_FLAG_SEND_COMMUNITY
))) {
6815 " no neighbor %s send-community\n",
6821 /* Default information */
6822 if (peergroup_af_flag_check(peer
, afi
, safi
,
6823 PEER_FLAG_DEFAULT_ORIGINATE
)
6825 && ((peer
->default_rmap
[afi
][safi
].name
6826 && !g_peer
->default_rmap
[afi
][safi
].name
)
6827 || (!peer
->default_rmap
[afi
][safi
].name
6828 && g_peer
->default_rmap
[afi
][safi
].name
)
6829 || (peer
->default_rmap
[afi
][safi
].name
6830 && strcmp(peer
->default_rmap
[afi
][safi
].name
,
6831 g_peer
->default_rmap
[afi
][safi
].name
))))) {
6832 vty_out(vty
, " neighbor %s default-originate", addr
);
6833 if (peer
->default_rmap
[afi
][safi
].name
)
6834 vty_out(vty
, " route-map %s",
6835 peer
->default_rmap
[afi
][safi
].name
);
6839 /* Soft reconfiguration inbound. */
6840 if (peergroup_af_flag_check(peer
, afi
, safi
, PEER_FLAG_SOFT_RECONFIG
)) {
6841 vty_out(vty
, " neighbor %s soft-reconfiguration inbound\n",
6845 /* maximum-prefix. */
6846 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX
))
6847 if (!peer_group_active(peer
)
6848 || g_peer
->pmax
[afi
][safi
] != peer
->pmax
[afi
][safi
]
6849 || g_peer
->pmax_threshold
[afi
][safi
]
6850 != peer
->pmax_threshold
[afi
][safi
]
6851 || CHECK_FLAG(g_peer
->af_flags
[afi
][safi
],
6852 PEER_FLAG_MAX_PREFIX_WARNING
)
6853 != CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6854 PEER_FLAG_MAX_PREFIX_WARNING
)) {
6855 vty_out(vty
, " neighbor %s maximum-prefix %lu", addr
,
6856 peer
->pmax
[afi
][safi
]);
6857 if (peer
->pmax_threshold
[afi
][safi
]
6858 != MAXIMUM_PREFIX_THRESHOLD_DEFAULT
)
6860 peer
->pmax_threshold
[afi
][safi
]);
6861 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
6862 PEER_FLAG_MAX_PREFIX_WARNING
))
6863 vty_out(vty
, " warning-only");
6864 if (peer
->pmax_restart
[afi
][safi
])
6865 vty_out(vty
, " restart %u",
6866 peer
->pmax_restart
[afi
][safi
]);
6870 /* Route server client. */
6871 if (peergroup_af_flag_check(peer
, afi
, safi
,
6872 PEER_FLAG_RSERVER_CLIENT
)) {
6873 vty_out(vty
, " neighbor %s route-server-client\n", addr
);
6876 /* Nexthop-local unchanged. */
6877 if (peergroup_af_flag_check(peer
, afi
, safi
,
6878 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
)) {
6879 vty_out(vty
, " neighbor %s nexthop-local unchanged\n", addr
);
6882 /* allowas-in <1-10> */
6883 if (peer_af_flag_check(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
)) {
6884 if (!peer_group_active(peer
)
6885 || !peer_af_flag_check(g_peer
, afi
, safi
,
6886 PEER_FLAG_ALLOWAS_IN
)
6887 || peer
->allowas_in
[afi
][safi
]
6888 != g_peer
->allowas_in
[afi
][safi
]) {
6889 if (peer
->allowas_in
[afi
][safi
] == 3) {
6890 vty_out(vty
, " neighbor %s allowas-in\n",
6893 vty_out(vty
, " neighbor %s allowas-in %d\n",
6894 addr
, peer
->allowas_in
[afi
][safi
]);
6899 /* allowas-in origin */
6900 else if (peer_af_flag_check(peer
, afi
, safi
,
6901 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6902 if (!peer_group_active(peer
)
6903 || !peer_af_flag_check(g_peer
, afi
, safi
,
6904 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
6905 vty_out(vty
, " neighbor %s allowas-in origin\n", addr
);
6910 if (peer_af_flag_check(peer
, afi
, safi
, PEER_FLAG_WEIGHT
))
6911 if (!peer_group_active(peer
)
6912 || !peer_af_flag_check(g_peer
, afi
, safi
, PEER_FLAG_WEIGHT
)
6913 || peer
->weight
[afi
][safi
] != g_peer
->weight
[afi
][safi
]) {
6914 if (peer
->weight
[afi
][safi
]) {
6915 vty_out(vty
, " neighbor %s weight %lu\n", addr
,
6916 peer
->weight
[afi
][safi
]);
6921 bgp_config_write_filter(vty
, peer
, afi
, safi
);
6923 /* atribute-unchanged. */
6924 if (peer_af_flag_check(peer
, afi
, safi
, PEER_FLAG_AS_PATH_UNCHANGED
) ||
6925 peer_af_flag_check(peer
, afi
, safi
, PEER_FLAG_NEXTHOP_UNCHANGED
) ||
6926 peer_af_flag_check(peer
, afi
, safi
, PEER_FLAG_MED_UNCHANGED
)) {
6928 if (!peer_group_active(peer
) ||
6929 peergroup_af_flag_check(peer
, afi
, safi
,
6930 PEER_FLAG_AS_PATH_UNCHANGED
) ||
6931 peergroup_af_flag_check(peer
, afi
, safi
,
6932 PEER_FLAG_NEXTHOP_UNCHANGED
) ||
6933 peergroup_af_flag_check(peer
, afi
, safi
,
6934 PEER_FLAG_MED_UNCHANGED
)) {
6937 " neighbor %s attribute-unchanged%s%s%s\n",
6939 peer_af_flag_check(peer
, afi
, safi
,
6940 PEER_FLAG_AS_PATH_UNCHANGED
)
6943 peer_af_flag_check(peer
, afi
, safi
,
6944 PEER_FLAG_NEXTHOP_UNCHANGED
)
6947 peer_af_flag_check(peer
, afi
, safi
,
6948 PEER_FLAG_MED_UNCHANGED
)
6955 /* Address family based peer configuration display. */
6956 static void bgp_config_write_family(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
6960 struct peer_group
*group
;
6961 struct listnode
*node
, *nnode
;
6964 vty_frame(vty
, " !\n address-family ");
6965 if (afi
== AFI_IP
) {
6966 if (safi
== SAFI_UNICAST
)
6967 vty_frame(vty
, "ipv4 unicast");
6968 else if (safi
== SAFI_LABELED_UNICAST
)
6969 vty_frame(vty
, "ipv4 labeled-unicast");
6970 else if (safi
== SAFI_MULTICAST
)
6971 vty_frame(vty
, "ipv4 multicast");
6972 else if (safi
== SAFI_MPLS_VPN
)
6973 vty_frame(vty
, "ipv4 vpn");
6974 else if (safi
== SAFI_ENCAP
)
6975 vty_frame(vty
, "ipv4 encap");
6976 } else if (afi
== AFI_IP6
) {
6977 if (safi
== SAFI_UNICAST
)
6978 vty_frame(vty
, "ipv6 unicast");
6979 else if (safi
== SAFI_LABELED_UNICAST
)
6980 vty_frame(vty
, "ipv6 labeled-unicast");
6981 else if (safi
== SAFI_MULTICAST
)
6982 vty_frame(vty
, "ipv6 multicast");
6983 else if (safi
== SAFI_MPLS_VPN
)
6984 vty_frame(vty
, "ipv6 vpn");
6985 else if (safi
== SAFI_ENCAP
)
6986 vty_frame(vty
, "ipv6 encap");
6987 } else if (afi
== AFI_L2VPN
) {
6988 if (safi
== SAFI_EVPN
)
6989 vty_frame(vty
, "l2vpn evpn");
6991 vty_frame(vty
, "\n");
6993 bgp_config_write_distance(vty
, bgp
, afi
, safi
);
6995 bgp_config_write_network(vty
, bgp
, afi
, safi
);
6997 bgp_config_write_redistribute(vty
, bgp
, afi
, safi
);
6999 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
7000 bgp_config_write_peer_af(vty
, bgp
, group
->conf
, afi
, safi
);
7002 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7003 /* Skip dynamic neighbors. */
7004 if (peer_dynamic_neighbor(peer
))
7007 /* Do not display doppelganger peers */
7008 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
7009 bgp_config_write_peer_af(vty
, bgp
, peer
, afi
, safi
);
7012 bgp_config_write_maxpaths(vty
, bgp
, afi
, safi
);
7013 bgp_config_write_table_map(vty
, bgp
, afi
, safi
);
7015 if (safi
== SAFI_EVPN
)
7016 bgp_config_write_evpn_info(vty
, bgp
, afi
, safi
);
7018 vty_endframe(vty
, " exit-address-family\n");
7021 int bgp_config_write(struct vty
*vty
)
7025 struct peer_group
*group
;
7027 struct listnode
*node
, *nnode
;
7028 struct listnode
*mnode
, *mnnode
;
7030 /* BGP Multiple instance. */
7031 if (!bgp_option_check(BGP_OPT_MULTIPLE_INSTANCE
)) {
7032 vty_out(vty
, "no bgp multiple-instance\n");
7036 /* BGP Config type. */
7037 if (bgp_option_check(BGP_OPT_CONFIG_CISCO
)) {
7038 vty_out(vty
, "bgp config-type cisco\n");
7042 if (bm
->rmap_update_timer
!= RMAP_DEFAULT_UPDATE_TIMER
)
7043 vty_out(vty
, "bgp route-map delay-timer %u\n",
7044 bm
->rmap_update_timer
);
7047 vty_out(vty
, "!\n");
7049 /* BGP configuration. */
7050 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
7051 /* Router bgp ASN */
7052 vty_out(vty
, "router bgp %u", bgp
->as
);
7054 if (bgp_option_check(BGP_OPT_MULTIPLE_INSTANCE
)) {
7056 vty_out(vty
, " %s %s",
7058 == BGP_INSTANCE_TYPE_VIEW
)
7065 /* No Synchronization */
7066 if (bgp_option_check(BGP_OPT_CONFIG_CISCO
))
7067 vty_out(vty
, " no synchronization\n");
7069 /* BGP fast-external-failover. */
7070 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_NO_FAST_EXT_FAILOVER
))
7071 vty_out(vty
, " no bgp fast-external-failover\n");
7073 /* BGP router ID. */
7074 if (bgp
->router_id_static
.s_addr
!= 0)
7075 vty_out(vty
, " bgp router-id %s\n",
7076 inet_ntoa(bgp
->router_id_static
));
7078 /* BGP log-neighbor-changes. */
7079 if (!!bgp_flag_check(bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)
7080 != DFLT_BGP_LOG_NEIGHBOR_CHANGES
)
7081 vty_out(vty
, " %sbgp log-neighbor-changes\n",
7083 BGP_FLAG_LOG_NEIGHBOR_CHANGES
)
7087 /* BGP configuration. */
7088 if (bgp_flag_check(bgp
, BGP_FLAG_ALWAYS_COMPARE_MED
))
7089 vty_out(vty
, " bgp always-compare-med\n");
7091 /* BGP default ipv4-unicast. */
7092 if (bgp_flag_check(bgp
, BGP_FLAG_NO_DEFAULT_IPV4
))
7093 vty_out(vty
, " no bgp default ipv4-unicast\n");
7095 /* BGP default local-preference. */
7096 if (bgp
->default_local_pref
!= BGP_DEFAULT_LOCAL_PREF
)
7097 vty_out(vty
, " bgp default local-preference %u\n",
7098 bgp
->default_local_pref
);
7100 /* BGP default show-hostname */
7101 if (!!bgp_flag_check(bgp
, BGP_FLAG_SHOW_HOSTNAME
)
7102 != DFLT_BGP_SHOW_HOSTNAME
)
7103 vty_out(vty
, " %sbgp default show-hostname\n",
7104 bgp_flag_check(bgp
, BGP_FLAG_SHOW_HOSTNAME
)
7108 /* BGP default subgroup-pkt-queue-max. */
7109 if (bgp
->default_subgroup_pkt_queue_max
7110 != BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
)
7111 vty_out(vty
, " bgp default subgroup-pkt-queue-max %u\n",
7112 bgp
->default_subgroup_pkt_queue_max
);
7114 /* BGP client-to-client reflection. */
7115 if (bgp_flag_check(bgp
, BGP_FLAG_NO_CLIENT_TO_CLIENT
))
7116 vty_out(vty
, " no bgp client-to-client reflection\n");
7118 /* BGP cluster ID. */
7119 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CLUSTER_ID
))
7120 vty_out(vty
, " bgp cluster-id %s\n",
7121 inet_ntoa(bgp
->cluster_id
));
7123 /* Disable ebgp connected nexthop check */
7124 if (bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
7126 " bgp disable-ebgp-connected-route-check\n");
7128 /* Confederation identifier*/
7129 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
))
7130 vty_out(vty
, " bgp confederation identifier %i\n",
7133 /* Confederation peer */
7134 if (bgp
->confed_peers_cnt
> 0) {
7137 vty_out(vty
, " bgp confederation peers");
7139 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
7140 vty_out(vty
, " %u", bgp
->confed_peers
[i
]);
7145 /* BGP enforce-first-as. */
7146 if (bgp_flag_check(bgp
, BGP_FLAG_ENFORCE_FIRST_AS
))
7147 vty_out(vty
, " bgp enforce-first-as\n");
7149 /* BGP deterministic-med. */
7150 if (!!bgp_flag_check(bgp
, BGP_FLAG_DETERMINISTIC_MED
)
7151 != DFLT_BGP_DETERMINISTIC_MED
)
7152 vty_out(vty
, " %sbgp deterministic-med\n",
7153 bgp_flag_check(bgp
, BGP_FLAG_DETERMINISTIC_MED
)
7157 /* BGP update-delay. */
7158 bgp_config_write_update_delay(vty
, bgp
);
7160 if (bgp
->v_maxmed_onstartup
7161 != BGP_MAXMED_ONSTARTUP_UNCONFIGURED
) {
7162 vty_out(vty
, " bgp max-med on-startup %u",
7163 bgp
->v_maxmed_onstartup
);
7164 if (bgp
->maxmed_onstartup_value
7165 != BGP_MAXMED_VALUE_DEFAULT
)
7167 bgp
->maxmed_onstartup_value
);
7170 if (bgp
->v_maxmed_admin
!= BGP_MAXMED_ADMIN_UNCONFIGURED
) {
7171 vty_out(vty
, " bgp max-med administrative");
7172 if (bgp
->maxmed_admin_value
!= BGP_MAXMED_VALUE_DEFAULT
)
7173 vty_out(vty
, " %u", bgp
->maxmed_admin_value
);
7178 bgp_config_write_wpkt_quanta(vty
, bgp
);
7181 bgp_config_write_coalesce_time(vty
, bgp
);
7183 /* BGP graceful-restart. */
7184 if (bgp
->stalepath_time
!= BGP_DEFAULT_STALEPATH_TIME
)
7186 " bgp graceful-restart stalepath-time %u\n",
7187 bgp
->stalepath_time
);
7188 if (bgp
->restart_time
!= BGP_DEFAULT_RESTART_TIME
)
7189 vty_out(vty
, " bgp graceful-restart restart-time %u\n",
7191 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_RESTART
))
7192 vty_out(vty
, " bgp graceful-restart\n");
7194 /* BGP graceful-shutdown */
7195 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
))
7196 vty_out(vty
, " bgp graceful-shutdown\n");
7198 /* BGP graceful-restart Preserve State F bit. */
7199 if (bgp_flag_check(bgp
, BGP_FLAG_GR_PRESERVE_FWD
))
7201 " bgp graceful-restart preserve-fw-state\n");
7203 /* BGP bestpath method. */
7204 if (bgp_flag_check(bgp
, BGP_FLAG_ASPATH_IGNORE
))
7205 vty_out(vty
, " bgp bestpath as-path ignore\n");
7206 if (bgp_flag_check(bgp
, BGP_FLAG_ASPATH_CONFED
))
7207 vty_out(vty
, " bgp bestpath as-path confed\n");
7209 if (bgp_flag_check(bgp
, BGP_FLAG_ASPATH_MULTIPATH_RELAX
)) {
7210 if (bgp_flag_check(bgp
,
7211 BGP_FLAG_MULTIPATH_RELAX_AS_SET
)) {
7213 " bgp bestpath as-path multipath-relax as-set\n");
7216 " bgp bestpath as-path multipath-relax\n");
7220 if (bgp_flag_check(bgp
, BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY
)) {
7222 " bgp route-reflector allow-outbound-policy\n");
7224 if (bgp_flag_check(bgp
, BGP_FLAG_COMPARE_ROUTER_ID
))
7225 vty_out(vty
, " bgp bestpath compare-routerid\n");
7226 if (bgp_flag_check(bgp
, BGP_FLAG_MED_CONFED
)
7227 || bgp_flag_check(bgp
, BGP_FLAG_MED_MISSING_AS_WORST
)) {
7228 vty_out(vty
, " bgp bestpath med");
7229 if (bgp_flag_check(bgp
, BGP_FLAG_MED_CONFED
))
7230 vty_out(vty
, " confed");
7231 if (bgp_flag_check(bgp
, BGP_FLAG_MED_MISSING_AS_WORST
))
7232 vty_out(vty
, " missing-as-worst");
7236 /* BGP network import check. */
7237 if (!!bgp_flag_check(bgp
, BGP_FLAG_IMPORT_CHECK
)
7238 != DFLT_BGP_IMPORT_CHECK
)
7239 vty_out(vty
, " %sbgp network import-check\n",
7240 bgp_flag_check(bgp
, BGP_FLAG_IMPORT_CHECK
)
7244 /* BGP flag dampening. */
7245 if (CHECK_FLAG(bgp
->af_flags
[AFI_IP
][SAFI_UNICAST
],
7246 BGP_CONFIG_DAMPENING
))
7247 bgp_config_write_damp(vty
);
7249 /* BGP timers configuration. */
7250 if (bgp
->default_keepalive
!= BGP_DEFAULT_KEEPALIVE
7251 && bgp
->default_holdtime
!= BGP_DEFAULT_HOLDTIME
)
7252 vty_out(vty
, " timers bgp %u %u\n",
7253 bgp
->default_keepalive
, bgp
->default_holdtime
);
7256 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
7257 bgp_config_write_peer_global(vty
, bgp
, group
->conf
);
7260 /* Normal neighbor configuration. */
7261 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7262 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
7263 bgp_config_write_peer_global(vty
, bgp
, peer
);
7266 /* listen range and limit for dynamic BGP neighbors */
7267 bgp_config_write_listen(vty
, bgp
);
7269 /* No auto-summary */
7270 if (bgp_option_check(BGP_OPT_CONFIG_CISCO
))
7271 vty_out(vty
, " no auto-summary\n");
7273 /* IPv4 unicast configuration. */
7274 bgp_config_write_family(vty
, bgp
, AFI_IP
, SAFI_UNICAST
);
7276 /* IPv4 multicast configuration. */
7277 bgp_config_write_family(vty
, bgp
, AFI_IP
, SAFI_MULTICAST
);
7279 /* IPv4 labeled-unicast configuration. */
7280 bgp_config_write_family(vty
, bgp
, AFI_IP
, SAFI_LABELED_UNICAST
);
7282 /* IPv4 VPN configuration. */
7283 bgp_config_write_family(vty
, bgp
, AFI_IP
, SAFI_MPLS_VPN
);
7285 /* ENCAPv4 configuration. */
7286 bgp_config_write_family(vty
, bgp
, AFI_IP
, SAFI_ENCAP
);
7288 /* IPv6 unicast configuration. */
7289 bgp_config_write_family(vty
, bgp
, AFI_IP6
, SAFI_UNICAST
);
7291 /* IPv6 multicast configuration. */
7292 bgp_config_write_family(vty
, bgp
, AFI_IP6
, SAFI_MULTICAST
);
7294 /* IPv6 labeled-unicast configuration. */
7295 bgp_config_write_family(vty
, bgp
, AFI_IP6
,
7296 SAFI_LABELED_UNICAST
);
7298 /* IPv6 VPN configuration. */
7299 bgp_config_write_family(vty
, bgp
, AFI_IP6
, SAFI_MPLS_VPN
);
7301 /* ENCAPv6 configuration. */
7302 bgp_config_write_family(vty
, bgp
, AFI_IP6
, SAFI_ENCAP
);
7304 /* EVPN configuration. */
7305 bgp_config_write_family(vty
, bgp
, AFI_L2VPN
, SAFI_EVPN
);
7308 bgp_rfapi_cfg_write(vty
, bgp
);
7311 vty_out(vty
, "!\n");
7316 void bgp_master_init(struct thread_master
*master
)
7320 memset(&bgp_master
, 0, sizeof(struct bgp_master
));
7323 bm
->bgp
= list_new();
7324 bm
->listen_sockets
= list_new();
7325 bm
->port
= BGP_PORT_DEFAULT
;
7326 bm
->master
= master
;
7327 bm
->start_time
= bgp_clock();
7328 bm
->t_rmap_update
= NULL
;
7329 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7331 bgp_process_queue_init();
7333 /* Enable multiple instances by default. */
7334 bgp_option_set(BGP_OPT_MULTIPLE_INSTANCE
);
7336 QOBJ_REG(bm
, bgp_master
);
7340 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7341 * instance delete (non-default only) or BGP exit.
7343 static void bgp_if_finish(struct bgp
*bgp
)
7345 struct listnode
*ifnode
, *ifnnode
;
7346 struct interface
*ifp
;
7348 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
7351 for (ALL_LIST_ELEMENTS(vrf_iflist(bgp
->vrf_id
), ifnode
, ifnnode
, ifp
)) {
7352 struct listnode
*c_node
, *c_nnode
;
7353 struct connected
*c
;
7355 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
7356 bgp_connected_delete(bgp
, c
);
7360 extern void bgp_snmp_init(void);
7362 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
7364 struct vrf
*vrf
= NULL
;
7365 struct listnode
*next
;
7368 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
7369 if (vrf
->vrf_id
!= VRF_DEFAULT
)
7370 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
7373 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
7374 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
7377 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
7381 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
7382 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
7383 {.completions
= NULL
},
7389 /* allocates some vital data structures used by peer commands in
7393 bgp_zebra_init(bm
->master
);
7396 vnc_zebra_init(bm
->master
);
7399 /* BGP VTY commands installation. */
7407 bgp_route_map_init();
7408 bgp_scan_vty_init();
7413 bgp_ethernetvpn_init();
7415 /* Access list initialize. */
7417 access_list_add_hook(peer_distribute_update
);
7418 access_list_delete_hook(peer_distribute_update
);
7420 /* Filter list initialize. */
7422 as_list_add_hook(peer_aslist_add
);
7423 as_list_delete_hook(peer_aslist_del
);
7425 /* Prefix list initialize.*/
7427 prefix_list_add_hook(peer_prefix_list_update
);
7428 prefix_list_delete_hook(peer_prefix_list_update
);
7430 /* Community list initialize. */
7431 bgp_clist
= community_list_init();
7436 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
7439 void bgp_terminate(void)
7443 struct listnode
*node
, *nnode
;
7444 struct listnode
*mnode
, *mnnode
;
7448 /* Close the listener sockets first as this prevents peers from
7450 * to reconnect on receiving the peer unconfig message. In the presence
7451 * of a large number of peers this will ensure that no peer is left with
7452 * a dangling connection
7454 /* reverse bgp_master_init */
7456 if (bm
->listen_sockets
)
7457 list_delete_and_null(&bm
->listen_sockets
);
7459 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
))
7460 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
7461 if (peer
->status
== Established
7462 || peer
->status
== OpenSent
7463 || peer
->status
== OpenConfirm
)
7464 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7465 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
7467 if (bm
->process_main_queue
) {
7468 work_queue_free(bm
->process_main_queue
);
7469 bm
->process_main_queue
= NULL
;
7472 if (bm
->t_rmap_update
)
7473 BGP_TIMER_OFF(bm
->t_rmap_update
);