1 /* BGP-4 Finite State Machine
2 * From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
3 * Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "sockunion.h"
33 #include "workqueue.h"
37 #include "lib_errors.h"
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_attr.h"
42 #include "bgpd/bgp_debug.h"
43 #include "bgpd/bgp_errors.h"
44 #include "bgpd/bgp_fsm.h"
45 #include "bgpd/bgp_packet.h"
46 #include "bgpd/bgp_network.h"
47 #include "bgpd/bgp_route.h"
48 #include "bgpd/bgp_dump.h"
49 #include "bgpd/bgp_open.h"
50 #include "bgpd/bgp_advertise.h"
51 #include "bgpd/bgp_updgrp.h"
52 #include "bgpd/bgp_nht.h"
53 #include "bgpd/bgp_bfd.h"
54 #include "bgpd/bgp_memory.h"
55 #include "bgpd/bgp_keepalives.h"
56 #include "bgpd/bgp_io.h"
57 #include "bgpd/bgp_zebra.h"
59 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
))
60 DEFINE_HOOK(peer_established
, (struct peer
* peer
), (peer
))
62 /* Definition of display strings corresponding to FSM events. This should be
63 * kept consistent with the events defined in bgpd.h
65 static const char *bgp_event_str
[] = {
69 "TCP_connection_open",
70 "TCP_connection_closed",
71 "TCP_connection_open_failed",
73 "ConnectRetry_timer_expired",
75 "KeepAlive_timer_expired",
76 "Receive_OPEN_message",
77 "Receive_KEEPALIVE_message",
78 "Receive_UPDATE_message",
79 "Receive_NOTIFICATION_message",
83 /* BGP FSM (finite state machine) has three types of functions. Type
84 one is thread functions. Type two is event functions. Type three
85 is FSM functions. Timer functions are set by bgp_timer_set
88 /* BGP event function. */
89 int bgp_event(struct thread
*);
91 /* BGP thread functions. */
92 static int bgp_start_timer(struct thread
*);
93 static int bgp_connect_timer(struct thread
*);
94 static int bgp_holdtime_timer(struct thread
*);
96 /* BGP FSM functions. */
97 static int bgp_start(struct peer
*);
99 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
101 /* Copy stats over. These are only the pre-established state stats */
102 peer_dst
->open_in
+= peer_src
->open_in
;
103 peer_dst
->open_out
+= peer_src
->open_out
;
104 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
105 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
106 peer_dst
->notify_in
+= peer_src
->notify_in
;
107 peer_dst
->notify_out
+= peer_src
->notify_out
;
108 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
109 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
112 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
119 unsigned char last_evt
, last_maj_evt
;
121 assert(from_peer
!= NULL
);
123 peer
= from_peer
->doppelganger
;
125 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
129 * Let's check that we are not going to loose known configuration
130 * state based upon doppelganger rules.
132 FOREACH_AFI_SAFI (afi
, safi
) {
133 if (from_peer
->afc
[afi
][safi
] != peer
->afc
[afi
][safi
]) {
135 EC_BGP_DOPPELGANGER_CONFIG
,
136 "from_peer->afc[%d][%d] is not the same as what we are overwriting",
142 if (bgp_debug_neighbor_events(peer
))
143 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
144 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
147 bgp_writes_off(peer
);
149 bgp_writes_off(from_peer
);
150 bgp_reads_off(from_peer
);
152 BGP_TIMER_OFF(peer
->t_routeadv
);
153 BGP_TIMER_OFF(peer
->t_connect
);
154 BGP_TIMER_OFF(peer
->t_connect_check_r
);
155 BGP_TIMER_OFF(peer
->t_connect_check_w
);
156 BGP_TIMER_OFF(from_peer
->t_routeadv
);
157 BGP_TIMER_OFF(from_peer
->t_connect
);
158 BGP_TIMER_OFF(from_peer
->t_connect_check_r
);
159 BGP_TIMER_OFF(from_peer
->t_connect_check_w
);
160 BGP_TIMER_OFF(from_peer
->t_process_packet
);
163 * At this point in time, it is possible that there are packets pending
164 * on various buffers. Those need to be transferred or dropped,
165 * otherwise we'll get spurious failures during session establishment.
167 pthread_mutex_lock(&peer
->io_mtx
);
168 pthread_mutex_lock(&from_peer
->io_mtx
);
171 peer
->fd
= from_peer
->fd
;
174 stream_fifo_clean(peer
->ibuf
);
175 stream_fifo_clean(peer
->obuf
);
178 * this should never happen, since bgp_process_packet() is the
179 * only task that sets and unsets the current packet and it
180 * runs in our pthread.
185 "[%s] Dropping pending packet on connection transfer:",
187 uint16_t type
= stream_getc_from(peer
->curr
,
188 BGP_MARKER_SIZE
+ 2);
189 bgp_dump_packet(peer
, type
, peer
->curr
);
190 stream_free(peer
->curr
);
194 // copy each packet from old peer's output queue to new peer
195 while (from_peer
->obuf
->head
)
196 stream_fifo_push(peer
->obuf
,
197 stream_fifo_pop(from_peer
->obuf
));
199 // copy each packet from old peer's input queue to new peer
200 while (from_peer
->ibuf
->head
)
201 stream_fifo_push(peer
->ibuf
,
202 stream_fifo_pop(from_peer
->ibuf
));
204 ringbuf_wipe(peer
->ibuf_work
);
205 ringbuf_copy(peer
->ibuf_work
, from_peer
->ibuf_work
,
206 ringbuf_remain(from_peer
->ibuf_work
));
208 pthread_mutex_unlock(&from_peer
->io_mtx
);
209 pthread_mutex_unlock(&peer
->io_mtx
);
211 peer
->as
= from_peer
->as
;
212 peer
->v_holdtime
= from_peer
->v_holdtime
;
213 peer
->v_keepalive
= from_peer
->v_keepalive
;
214 peer
->v_routeadv
= from_peer
->v_routeadv
;
215 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
216 peer
->cap
= from_peer
->cap
;
217 status
= peer
->status
;
218 pstatus
= peer
->ostatus
;
219 last_evt
= peer
->last_event
;
220 last_maj_evt
= peer
->last_major_event
;
221 peer
->status
= from_peer
->status
;
222 peer
->ostatus
= from_peer
->ostatus
;
223 peer
->last_event
= from_peer
->last_event
;
224 peer
->last_major_event
= from_peer
->last_major_event
;
225 from_peer
->status
= status
;
226 from_peer
->ostatus
= pstatus
;
227 from_peer
->last_event
= last_evt
;
228 from_peer
->last_major_event
= last_maj_evt
;
229 peer
->remote_id
= from_peer
->remote_id
;
231 if (from_peer
->hostname
!= NULL
) {
232 if (peer
->hostname
) {
233 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
234 peer
->hostname
= NULL
;
237 peer
->hostname
= from_peer
->hostname
;
238 from_peer
->hostname
= NULL
;
241 if (from_peer
->domainname
!= NULL
) {
242 if (peer
->domainname
) {
243 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
244 peer
->domainname
= NULL
;
247 peer
->domainname
= from_peer
->domainname
;
248 from_peer
->domainname
= NULL
;
251 FOREACH_AFI_SAFI (afi
, safi
) {
252 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
253 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
254 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
255 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
256 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
257 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
258 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
261 if (bgp_getsockname(peer
) < 0) {
264 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
265 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
268 peer
->host
, peer
->fd
, from_peer
->fd
);
273 if (from_peer
->status
> Active
) {
274 if (bgp_getsockname(from_peer
) < 0) {
277 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
279 (CHECK_FLAG(from_peer
->sflags
,
280 PEER_STATUS_ACCEPT_PEER
)
283 from_peer
->host
, from_peer
->fd
, peer
->fd
);
290 // Note: peer_xfer_stats() must be called with I/O turned OFF
292 peer_xfer_stats(peer
, from_peer
);
296 thread_add_timer_msec(bm
->master
, bgp_process_packet
, peer
, 0,
297 &peer
->t_process_packet
);
302 /* Hook function called after bgp event is occered. And vty's
303 neighbor command invoke this function after making neighbor
305 void bgp_timer_set(struct peer
*peer
)
307 switch (peer
->status
) {
309 /* First entry point of peer's finite state machine. In Idle
310 status start timer is on unless peer is shutdown or peer is
311 inactive. All other timer must be turned off */
312 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)
313 || peer
->bgp
->vrf_id
== VRF_UNKNOWN
) {
314 BGP_TIMER_OFF(peer
->t_start
);
316 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
319 BGP_TIMER_OFF(peer
->t_connect
);
320 BGP_TIMER_OFF(peer
->t_holdtime
);
321 bgp_keepalives_off(peer
);
322 BGP_TIMER_OFF(peer
->t_routeadv
);
326 /* After start timer is expired, the peer moves to Connect
327 status. Make sure start timer is off and connect timer is
329 BGP_TIMER_OFF(peer
->t_start
);
330 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
332 BGP_TIMER_OFF(peer
->t_holdtime
);
333 bgp_keepalives_off(peer
);
334 BGP_TIMER_OFF(peer
->t_routeadv
);
338 /* Active is waiting connection from remote peer. And if
339 connect timer is expired, change status to Connect. */
340 BGP_TIMER_OFF(peer
->t_start
);
341 /* If peer is passive mode, do not set connect timer. */
342 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
343 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
344 BGP_TIMER_OFF(peer
->t_connect
);
346 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
349 BGP_TIMER_OFF(peer
->t_holdtime
);
350 bgp_keepalives_off(peer
);
351 BGP_TIMER_OFF(peer
->t_routeadv
);
355 /* OpenSent status. */
356 BGP_TIMER_OFF(peer
->t_start
);
357 BGP_TIMER_OFF(peer
->t_connect
);
358 if (peer
->v_holdtime
!= 0) {
359 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
362 BGP_TIMER_OFF(peer
->t_holdtime
);
364 bgp_keepalives_off(peer
);
365 BGP_TIMER_OFF(peer
->t_routeadv
);
369 /* OpenConfirm status. */
370 BGP_TIMER_OFF(peer
->t_start
);
371 BGP_TIMER_OFF(peer
->t_connect
);
373 /* If the negotiated Hold Time value is zero, then the Hold Time
374 timer and KeepAlive timers are not started. */
375 if (peer
->v_holdtime
== 0) {
376 BGP_TIMER_OFF(peer
->t_holdtime
);
377 bgp_keepalives_off(peer
);
379 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
381 bgp_keepalives_on(peer
);
383 BGP_TIMER_OFF(peer
->t_routeadv
);
387 /* In Established status start and connect timer is turned
389 BGP_TIMER_OFF(peer
->t_start
);
390 BGP_TIMER_OFF(peer
->t_connect
);
392 /* Same as OpenConfirm, if holdtime is zero then both holdtime
393 and keepalive must be turned off. */
394 if (peer
->v_holdtime
== 0) {
395 BGP_TIMER_OFF(peer
->t_holdtime
);
396 bgp_keepalives_off(peer
);
398 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
400 bgp_keepalives_on(peer
);
404 BGP_TIMER_OFF(peer
->t_gr_restart
);
405 BGP_TIMER_OFF(peer
->t_gr_stale
);
406 BGP_TIMER_OFF(peer
->t_pmax_restart
);
409 BGP_TIMER_OFF(peer
->t_start
);
410 BGP_TIMER_OFF(peer
->t_connect
);
411 BGP_TIMER_OFF(peer
->t_holdtime
);
412 bgp_keepalives_off(peer
);
413 BGP_TIMER_OFF(peer
->t_routeadv
);
418 /* BGP start timer. This function set BGP_Start event to thread value
419 and process event. */
420 static int bgp_start_timer(struct thread
*thread
)
424 peer
= THREAD_ARG(thread
);
425 peer
->t_start
= NULL
;
427 if (bgp_debug_neighbor_events(peer
))
428 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
430 THREAD_VAL(thread
) = BGP_Start
;
431 bgp_event(thread
); /* bgp_event unlocks peer */
436 /* BGP connect retry timer. */
437 static int bgp_connect_timer(struct thread
*thread
)
442 peer
= THREAD_ARG(thread
);
444 assert(!peer
->t_write
);
445 assert(!peer
->t_read
);
447 peer
->t_connect
= NULL
;
449 if (bgp_debug_neighbor_events(peer
))
450 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
452 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
456 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
457 bgp_event(thread
); /* bgp_event unlocks peer */
464 /* BGP holdtime timer. */
465 static int bgp_holdtime_timer(struct thread
*thread
)
469 peer
= THREAD_ARG(thread
);
470 peer
->t_holdtime
= NULL
;
472 if (bgp_debug_neighbor_events(peer
))
473 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
476 THREAD_VAL(thread
) = Hold_Timer_expired
;
477 bgp_event(thread
); /* bgp_event unlocks peer */
482 int bgp_routeadv_timer(struct thread
*thread
)
486 peer
= THREAD_ARG(thread
);
487 peer
->t_routeadv
= NULL
;
489 if (bgp_debug_neighbor_events(peer
))
490 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
493 peer
->synctime
= bgp_clock();
495 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
, peer
, 0,
496 &peer
->t_generate_updgrp_packets
);
498 /* MRAI timer will be started again when FIFO is built, no need to
504 /* BGP Peer Down Cause */
505 const char *peer_down_str
[] = {"",
509 "Cluster ID changed",
510 "Confederation identifier changed",
511 "Confederation peer changed",
512 "RR client config change",
513 "RS client config change",
514 "Update source change",
515 "Address family activated",
518 "BGP Notification received",
519 "BGP Notification send",
520 "Peer closed the session",
522 "Peer-group add member",
523 "Peer-group delete member",
524 "Capability changed",
525 "Passive config change",
526 "Multihop config change",
527 "NSF peer closed the session",
528 "Intf peering v6only config change",
531 "Neighbor address lost"};
533 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
539 peer
= THREAD_ARG(thread
);
540 peer
->t_gr_restart
= NULL
;
542 /* NSF delete stale route */
543 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
544 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
545 if (peer
->nsf
[afi
][safi
])
546 bgp_clear_stale_route(peer
, afi
, safi
);
548 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
549 BGP_TIMER_OFF(peer
->t_gr_stale
);
551 if (bgp_debug_neighbor_events(peer
)) {
552 zlog_debug("%s graceful restart timer expired", peer
->host
);
553 zlog_debug("%s graceful restart stalepath timer stopped",
562 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
568 peer
= THREAD_ARG(thread
);
569 peer
->t_gr_stale
= NULL
;
571 if (bgp_debug_neighbor_events(peer
))
572 zlog_debug("%s graceful restart stalepath timer expired",
575 /* NSF delete stale route */
576 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
577 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
578 if (peer
->nsf
[afi
][safi
])
579 bgp_clear_stale_route(peer
, afi
, safi
);
584 static int bgp_update_delay_applicable(struct bgp
*bgp
)
586 /* update_delay_over flag should be reset (set to 0) for any new
587 applicability of the update-delay during BGP process lifetime.
588 And it should be set after an occurence of the update-delay is
590 if (!bgp
->update_delay_over
)
596 int bgp_update_delay_active(struct bgp
*bgp
)
598 if (bgp
->t_update_delay
)
604 int bgp_update_delay_configured(struct bgp
*bgp
)
606 if (bgp
->v_update_delay
)
612 /* Do the post-processing needed when bgp comes out of the read-only mode
613 on ending the update delay. */
614 void bgp_update_delay_end(struct bgp
*bgp
)
616 THREAD_TIMER_OFF(bgp
->t_update_delay
);
617 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
619 /* Reset update-delay related state */
620 bgp
->update_delay_over
= 1;
621 bgp
->established
= 0;
622 bgp
->restarted_peers
= 0;
623 bgp
->implicit_eors
= 0;
624 bgp
->explicit_eors
= 0;
626 quagga_timestamp(3, bgp
->update_delay_end_time
,
627 sizeof(bgp
->update_delay_end_time
));
630 * Add an end-of-initial-update marker to the main process queues so
632 * the route advertisement timer for the peers can be started. Also set
633 * the zebra and peer update hold flags. These flags are used to achieve
634 * three stages in the update-delay post processing:
635 * 1. Finish best-path selection for all the prefixes held on the
637 * (routes in BGP are updated, and peers sync queues are populated
639 * 2. As the eoiu mark is reached in the bgp process routine, ship all
641 * routes to zebra. With that zebra should see updates from BGP
644 * 3. Unblock the peer update writes. With that peer update packing
646 * the prefixes should be at its maximum.
648 bgp_add_eoiu_mark(bgp
);
649 bgp
->main_zebra_update_hold
= 1;
650 bgp
->main_peers_update_hold
= 1;
652 /* Resume the queue processing. This should trigger the event that would
654 care of processing any work that was queued during the read-only
656 work_queue_unplug(bm
->process_main_queue
);
662 void bgp_start_routeadv(struct bgp
*bgp
)
664 struct listnode
*node
, *nnode
;
667 zlog_info("bgp_start_routeadv(), update hold status %d",
668 bgp
->main_peers_update_hold
);
670 if (bgp
->main_peers_update_hold
)
673 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
674 sizeof(bgp
->update_delay_peers_resume_time
));
676 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
677 if (peer
->status
!= Established
)
679 BGP_TIMER_OFF(peer
->t_routeadv
);
680 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
687 void bgp_adjust_routeadv(struct peer
*peer
)
689 time_t nowtime
= bgp_clock();
691 unsigned long remain
;
693 /* Bypass checks for special case of MRAI being 0 */
694 if (peer
->v_routeadv
== 0) {
695 /* Stop existing timer, just in case it is running for a
697 * duration and schedule write thread immediately.
699 if (peer
->t_routeadv
)
700 BGP_TIMER_OFF(peer
->t_routeadv
);
702 peer
->synctime
= bgp_clock();
703 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
,
705 &peer
->t_generate_updgrp_packets
);
712 * If the last update was written more than MRAI back, expire the timer
713 * instantly so that we can send the update out sooner.
715 * <------- MRAI --------->
716 * |-----------------|-----------------------|
717 * <------------- m ------------>
726 diff
= difftime(nowtime
, peer
->last_update
);
727 if (diff
> (double)peer
->v_routeadv
) {
728 BGP_TIMER_OFF(peer
->t_routeadv
);
729 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
735 * - Find when to expire the MRAI timer.
736 * If MRAI timer is not active, assume we can start it now.
738 * <------- MRAI --------->
739 * |------------|-----------------------|
740 * <-------- m ----------><----- r ----->
749 if (peer
->t_routeadv
)
750 remain
= thread_timer_remain_second(peer
->t_routeadv
);
752 remain
= peer
->v_routeadv
;
753 diff
= peer
->v_routeadv
- diff
;
754 if (diff
<= (double)remain
) {
755 BGP_TIMER_OFF(peer
->t_routeadv
);
756 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
760 static int bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
762 if (!bgp
->maxmed_onstartup_over
)
768 int bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
770 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
776 int bgp_maxmed_onstartup_active(struct bgp
*bgp
)
778 if (bgp
->t_maxmed_onstartup
)
784 void bgp_maxmed_update(struct bgp
*bgp
)
786 uint8_t maxmed_active
;
787 uint32_t maxmed_value
;
789 if (bgp
->v_maxmed_admin
) {
791 maxmed_value
= bgp
->maxmed_admin_value
;
792 } else if (bgp
->t_maxmed_onstartup
) {
794 maxmed_value
= bgp
->maxmed_onstartup_value
;
797 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
800 if (bgp
->maxmed_active
!= maxmed_active
801 || bgp
->maxmed_value
!= maxmed_value
) {
802 bgp
->maxmed_active
= maxmed_active
;
803 bgp
->maxmed_value
= maxmed_value
;
805 update_group_announce(bgp
);
809 /* The maxmed onstartup timer expiry callback. */
810 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
814 zlog_info("Max med on startup ended - timer expired.");
816 bgp
= THREAD_ARG(thread
);
817 THREAD_TIMER_OFF(bgp
->t_maxmed_onstartup
);
818 bgp
->maxmed_onstartup_over
= 1;
820 bgp_maxmed_update(bgp
);
825 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
827 /* Applicable only once in the process lifetime on the startup */
828 if (bgp
->maxmed_onstartup_over
)
831 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
832 bgp
->v_maxmed_onstartup
);
834 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
835 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
837 if (!bgp
->v_maxmed_admin
) {
838 bgp
->maxmed_active
= 1;
839 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
842 /* Route announce to all peers should happen after this in
846 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
848 if (peer
->status
== Established
&& !peer
->bgp
->established
) {
849 bgp_maxmed_onstartup_begin(peer
->bgp
);
853 /* The update delay timer expiry callback. */
854 static int bgp_update_delay_timer(struct thread
*thread
)
858 zlog_info("Update delay ended - timer expired.");
860 bgp
= THREAD_ARG(thread
);
861 THREAD_TIMER_OFF(bgp
->t_update_delay
);
862 bgp_update_delay_end(bgp
);
867 /* The establish wait timer expiry callback. */
868 static int bgp_establish_wait_timer(struct thread
*thread
)
872 zlog_info("Establish wait - timer expired.");
874 bgp
= THREAD_ARG(thread
);
875 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
876 bgp_check_update_delay(bgp
);
881 /* Steps to begin the update delay:
882 - initialize queues if needed
883 - stop the queue processing
885 static void bgp_update_delay_begin(struct bgp
*bgp
)
887 struct listnode
*node
, *nnode
;
890 /* Stop the processing of queued work. Enqueue shall continue */
891 work_queue_plug(bm
->process_main_queue
);
893 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
894 peer
->update_delay_over
= 0;
896 /* Start the update-delay timer */
897 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
898 bgp
->v_update_delay
, &bgp
->t_update_delay
);
900 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
901 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
902 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
904 quagga_timestamp(3, bgp
->update_delay_begin_time
,
905 sizeof(bgp
->update_delay_begin_time
));
908 static void bgp_update_delay_process_status_change(struct peer
*peer
)
910 if (peer
->status
== Established
) {
911 if (!peer
->bgp
->established
++) {
912 bgp_update_delay_begin(peer
->bgp
);
914 "Begin read-only mode - update-delay timer %d seconds",
915 peer
->bgp
->v_update_delay
);
917 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
918 bgp_update_restarted_peers(peer
);
920 if (peer
->ostatus
== Established
921 && bgp_update_delay_active(peer
->bgp
)) {
922 /* Adjust the update-delay state to account for this flap.
923 NOTE: Intentionally skipping adjusting implicit_eors or
925 counters. Extra sanity check in bgp_check_update_delay()
927 be enough to take care of any additive discrepancy in bgp eor
929 peer
->bgp
->established
--;
930 peer
->update_delay_over
= 0;
934 /* Called after event occurred, this function change status and reset
935 read/write and timer thread. */
936 void bgp_fsm_change_status(struct peer
*peer
, int status
)
939 bgp_dump_state(peer
, peer
->status
, status
);
941 /* Transition into Clearing or Deleted must /always/ clear all routes..
942 * (and must do so before actually changing into Deleted..
944 if (status
>= Clearing
) {
945 bgp_clear_route_all(peer
);
947 /* If no route was queued for the clear-node processing,
949 * completion event here. This is needed because if there are no
951 * to trigger the background clear-node thread, the event won't
953 * generated and the peer would be stuck in Clearing. Note that
955 * event is for the peer and helps the peer transition out of
957 * state; it should not be generated per (AFI,SAFI). The event
959 * directly posted here without calling clear_node_complete() as
961 * shouldn't do an extra unlock. This event will get processed
963 * the state change that happens below, so peer will be in
967 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
968 BGP_EVENT_ADD(peer
, Clearing_Completed
);
971 /* Preserve old status and change into new status. */
972 peer
->ostatus
= peer
->status
;
973 peer
->status
= status
;
975 /* Save event that caused status change. */
976 peer
->last_major_event
= peer
->cur_event
;
978 if (status
== Established
)
979 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
981 /* If max-med processing is applicable, do the necessary. */
982 if (status
== Established
) {
983 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
984 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
985 bgp_maxmed_onstartup_process_status_change(peer
);
987 peer
->bgp
->maxmed_onstartup_over
= 1;
990 /* If update-delay processing is applicable, do the necessary. */
991 if (bgp_update_delay_configured(peer
->bgp
)
992 && bgp_update_delay_applicable(peer
->bgp
))
993 bgp_update_delay_process_status_change(peer
);
995 if (bgp_debug_neighbor_events(peer
))
996 zlog_debug("%s went from %s to %s", peer
->host
,
997 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
998 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1001 /* Flush the event queue and ensure the peer is shut down */
1002 static int bgp_clearing_completed(struct peer
*peer
)
1004 int rc
= bgp_stop(peer
);
1007 BGP_EVENT_FLUSH(peer
);
1012 /* Administrative BGP peer stop event. */
1013 /* May be called multiple times for the same peer */
1014 int bgp_stop(struct peer
*peer
)
1018 char orf_name
[BUFSIZ
];
1021 if (peer_dynamic_neighbor(peer
)
1022 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1023 if (bgp_debug_neighbor_events(peer
))
1024 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1029 /* Can't do this in Clearing; events are used for state transitions */
1030 if (peer
->status
!= Clearing
) {
1031 /* Delete all existing events of the peer */
1032 BGP_EVENT_FLUSH(peer
);
1035 /* Increment Dropped count. */
1036 if (peer
->status
== Established
) {
1039 /* bgp log-neighbor-changes of neighbor Down */
1040 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1041 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1043 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1045 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1046 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1049 peer_down_str
[(int)peer
->last_reset
]);
1052 /* graceful restart */
1053 if (peer
->t_gr_stale
) {
1054 BGP_TIMER_OFF(peer
->t_gr_stale
);
1055 if (bgp_debug_neighbor_events(peer
))
1057 "%s graceful restart stalepath timer stopped",
1060 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
1061 if (bgp_debug_neighbor_events(peer
)) {
1063 "%s graceful restart timer started for %d sec",
1064 peer
->host
, peer
->v_gr_restart
);
1066 "%s graceful restart stalepath timer started for %d sec",
1067 peer
->host
, peer
->bgp
->stalepath_time
);
1069 BGP_TIMER_ON(peer
->t_gr_restart
,
1070 bgp_graceful_restart_timer_expire
,
1071 peer
->v_gr_restart
);
1072 BGP_TIMER_ON(peer
->t_gr_stale
,
1073 bgp_graceful_stale_timer_expire
,
1074 peer
->bgp
->stalepath_time
);
1076 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1078 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1079 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
;
1081 peer
->nsf
[afi
][safi
] = 0;
1084 /* set last reset time */
1085 peer
->resettime
= peer
->uptime
= bgp_clock();
1087 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1088 zlog_debug("%s remove from all update group",
1090 update_group_remove_peer_afs(peer
);
1092 hook_call(peer_backward_transition
, peer
);
1094 /* Reset peer synctime */
1097 bgp_bfd_deregister_peer(peer
);
1100 /* stop keepalives */
1101 bgp_keepalives_off(peer
);
1103 /* Stop read and write threads. */
1104 bgp_writes_off(peer
);
1105 bgp_reads_off(peer
);
1107 THREAD_OFF(peer
->t_connect_check_r
);
1108 THREAD_OFF(peer
->t_connect_check_w
);
1110 /* Stop all timers. */
1111 BGP_TIMER_OFF(peer
->t_start
);
1112 BGP_TIMER_OFF(peer
->t_connect
);
1113 BGP_TIMER_OFF(peer
->t_holdtime
);
1114 BGP_TIMER_OFF(peer
->t_routeadv
);
1116 /* Clear input and output buffer. */
1117 pthread_mutex_lock(&peer
->io_mtx
);
1120 stream_fifo_clean(peer
->ibuf
);
1122 stream_fifo_clean(peer
->obuf
);
1124 if (peer
->ibuf_work
)
1125 ringbuf_wipe(peer
->ibuf_work
);
1126 if (peer
->obuf_work
)
1127 stream_reset(peer
->obuf_work
);
1130 stream_free(peer
->curr
);
1134 pthread_mutex_unlock(&peer
->io_mtx
);
1136 /* Close of file descriptor. */
1137 if (peer
->fd
>= 0) {
1142 FOREACH_AFI_SAFI (afi
, safi
) {
1143 /* Reset all negotiated variables */
1144 peer
->afc_nego
[afi
][safi
] = 0;
1145 peer
->afc_adv
[afi
][safi
] = 0;
1146 peer
->afc_recv
[afi
][safi
] = 0;
1148 /* peer address family capability flags*/
1149 peer
->af_cap
[afi
][safi
] = 0;
1151 /* peer address family status flags*/
1152 peer
->af_sflags
[afi
][safi
] = 0;
1154 /* Received ORF prefix-filter */
1155 peer
->orf_plist
[afi
][safi
] = NULL
;
1157 if ((peer
->status
== OpenConfirm
)
1158 || (peer
->status
== Established
)) {
1159 /* ORF received prefix-filter pnt */
1160 sprintf(orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1161 prefix_bgp_orf_remove_all(afi
, orf_name
);
1165 /* Reset keepalive and holdtime */
1166 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1167 peer
->v_keepalive
= peer
->keepalive
;
1168 peer
->v_holdtime
= peer
->holdtime
;
1170 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1171 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1174 peer
->update_time
= 0;
1176 /* Until we are sure that there is no problem about prefix count
1177 this should be commented out.*/
1179 /* Reset prefix count */
1180 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1181 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1182 peer
->pcount
[AFI_IP
][SAFI_LABELED_UNICAST
] = 0;
1183 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1184 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1185 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1186 peer
->pcount
[AFI_IP6
][SAFI_LABELED_UNICAST
] = 0;
1189 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1190 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1194 bgp_peer_conf_if_to_su_update(peer
);
1200 /* BGP peer is stoped by the error. */
1201 static int bgp_stop_with_error(struct peer
*peer
)
1203 /* Double start timer. */
1206 /* Overflow check. */
1207 if (peer
->v_start
>= (60 * 2))
1208 peer
->v_start
= (60 * 2);
1210 if (peer_dynamic_neighbor(peer
)) {
1211 if (bgp_debug_neighbor_events(peer
))
1212 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1217 return (bgp_stop(peer
));
1221 /* something went wrong, send notify and tear down */
1222 static int bgp_stop_with_notify(struct peer
*peer
, uint8_t code
,
1225 /* Send notify to remote peer */
1226 bgp_notify_send(peer
, code
, sub_code
);
1228 if (peer_dynamic_neighbor(peer
)) {
1229 if (bgp_debug_neighbor_events(peer
))
1230 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1235 /* Clear start timer value to default. */
1236 peer
->v_start
= BGP_INIT_START_TIMER
;
1238 return (bgp_stop(peer
));
1242 * Determines whether a TCP session has successfully established for a peer and
1243 * events as appropriate.
1245 * This function is called when setting up a new session. After connect() is
1246 * called on the peer's socket (in bgp_start()), the fd is passed to poll()
1247 * to wait for connection success or failure. When poll() returns, this
1248 * function is called to evaluate the result.
1250 * Due to differences in behavior of poll() on Linux and BSD - specifically,
1251 * the value of .revents in the case of a closed connection - this function is
1252 * scheduled both for a read and a write event. The write event is triggered
1253 * when the connection is established. A read event is triggered when the
1254 * connection is closed. Thus we need to cancel whichever one did not occur.
1256 static int bgp_connect_check(struct thread
*thread
)
1263 peer
= THREAD_ARG(thread
);
1264 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1265 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1266 assert(!peer
->t_read
);
1267 assert(!peer
->t_write
);
1269 THREAD_OFF(peer
->t_connect_check_r
);
1270 THREAD_OFF(peer
->t_connect_check_w
);
1272 /* Check file descriptor. */
1273 slen
= sizeof(status
);
1274 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
1277 /* If getsockopt is fail, this is fatal error. */
1279 zlog_info("can't get sockopt for nonblocking connect: %d(%s)",
1280 errno
, safe_strerror(errno
));
1281 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1285 /* When status is 0 then TCP connection is established. */
1287 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1290 if (bgp_debug_neighbor_events(peer
))
1291 zlog_debug("%s [Event] Connect failed %d(%s)",
1292 peer
->host
, status
, safe_strerror(status
));
1293 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1298 /* TCP connection open. Next we send open message to remote peer. And
1299 add read thread for reading open message. */
1300 static int bgp_connect_success(struct peer
*peer
)
1303 flog_err(EC_BGP_CONNECT
,
1304 "bgp_connect_success peer's fd is negative value %d",
1310 if (bgp_getsockname(peer
) < 0) {
1311 flog_err_sys(EC_LIB_SOCKET
,
1312 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1313 __FUNCTION__
, peer
->host
, peer
->fd
);
1314 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1315 0); /* internal error */
1316 bgp_writes_on(peer
);
1322 if (bgp_debug_neighbor_events(peer
)) {
1323 char buf1
[SU_ADDRSTRLEN
];
1325 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1326 zlog_debug("%s open active, local address %s",
1328 sockunion2str(peer
->su_local
, buf1
,
1331 zlog_debug("%s passive open", peer
->host
);
1334 bgp_open_send(peer
);
1339 /* TCP connect fail */
1340 static int bgp_connect_fail(struct peer
*peer
)
1342 if (peer_dynamic_neighbor(peer
)) {
1343 if (bgp_debug_neighbor_events(peer
))
1344 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1349 return (bgp_stop(peer
));
1352 /* This function is the first starting point of all BGP connection. It
1353 try to connect to remote peer with non-blocking IO. */
1354 int bgp_start(struct peer
*peer
)
1359 bgp_peer_conf_if_to_su_update(peer
);
1361 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1362 if (bgp_debug_neighbor_events(peer
))
1364 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1369 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1370 if (bgp_debug_neighbor_events(peer
))
1371 flog_err(EC_BGP_FSM
,
1372 "%s [FSM] Trying to start suppressed peer"
1373 " - this is never supposed to happen!",
1378 /* Scrub some information that might be left over from a previous,
1381 /* Connection information. */
1382 if (peer
->su_local
) {
1383 sockunion_free(peer
->su_local
);
1384 peer
->su_local
= NULL
;
1387 if (peer
->su_remote
) {
1388 sockunion_free(peer
->su_remote
);
1389 peer
->su_remote
= NULL
;
1392 /* Clear remote router-id. */
1393 peer
->remote_id
.s_addr
= 0;
1395 /* Clear peer capability flag. */
1398 /* If the peer is passive mode, force to move to Active mode. */
1399 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1400 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1404 if (peer
->bgp
->vrf_id
== VRF_UNKNOWN
) {
1405 if (bgp_debug_neighbor_events(peer
))
1408 "%s [FSM] In a VRF that is not initialised yet",
1413 /* Register to be notified on peer up */
1414 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
1415 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1416 && !bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1421 if (!bgp_find_or_add_nexthop(peer
->bgp
, peer
->bgp
,
1422 family2afi(peer
->su
.sa
.sa_family
), NULL
,
1424 if (bgp_zebra_num_connects()) {
1425 if (bgp_debug_neighbor_events(peer
))
1426 zlog_debug("%s [FSM] Waiting for NHT",
1429 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1434 assert(!peer
->t_write
);
1435 assert(!peer
->t_read
);
1436 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1437 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1438 status
= bgp_connect(peer
);
1442 if (bgp_debug_neighbor_events(peer
))
1443 zlog_debug("%s [FSM] Connect error", peer
->host
);
1444 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1446 case connect_success
:
1447 if (bgp_debug_neighbor_events(peer
))
1449 "%s [FSM] Connect immediately success, fd %d",
1450 peer
->host
, peer
->fd
);
1451 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1453 case connect_in_progress
:
1454 /* To check nonblocking connect, we wait until socket is
1455 readable or writable. */
1456 if (bgp_debug_neighbor_events(peer
))
1458 "%s [FSM] Non blocking connect waiting result, fd %d",
1459 peer
->host
, peer
->fd
);
1461 flog_err(EC_BGP_FSM
,
1462 "bgp_start peer's fd is negative value %d",
1467 * - when the socket becomes ready, poll() will signify POLLOUT
1468 * - if it fails to connect, poll() will signify POLLHUP
1469 * - POLLHUP is handled as a 'read' event by thread.c
1471 * therefore, we schedule both a read and a write event with
1472 * bgp_connect_check() as the handler for each and cancel the
1473 * unused event in that function.
1475 thread_add_read(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1476 &peer
->t_connect_check_r
);
1477 thread_add_write(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1478 &peer
->t_connect_check_w
);
1484 /* Connect retry timer is expired when the peer status is Connect. */
1485 static int bgp_reconnect(struct peer
*peer
)
1487 if (bgp_stop(peer
) < 0)
1494 static int bgp_fsm_open(struct peer
*peer
)
1496 /* Send keepalive and make keepalive timer */
1497 bgp_keepalive_send(peer
);
1499 /* Reset holdtimer value. */
1500 BGP_TIMER_OFF(peer
->t_holdtime
);
1505 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1506 peer and change to Idle status. */
1507 static int bgp_fsm_event_error(struct peer
*peer
)
1509 flog_err(EC_BGP_FSM
, "%s [FSM] unexpected packet received in state %s",
1510 peer
->host
, lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1512 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1515 /* Hold timer expire. This is error of BGP connection. So cut the
1516 peer and change to Idle status. */
1517 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1519 if (bgp_debug_neighbor_events(peer
))
1520 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1522 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1526 * Transition to Established state.
1528 * Convert peer from stub to full fledged peer, set some timers, and generate
1531 static int bgp_establish(struct peer
*peer
)
1535 int nsf_af_count
= 0;
1539 other
= peer
->doppelganger
;
1540 peer
= peer_xfer_conn(peer
);
1542 flog_err(EC_BGP_CONNECT
, "%%Neighbor failed in xfer_conn");
1547 ret
= 1; /* bgp_establish specific code when xfer_conn
1550 /* Reset capability open status flag. */
1551 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1552 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1554 /* Clear start timer value to default. */
1555 peer
->v_start
= BGP_INIT_START_TIMER
;
1557 /* Increment established count. */
1558 peer
->established
++;
1559 bgp_fsm_change_status(peer
, Established
);
1561 /* bgp log-neighbor-changes of neighbor Up */
1562 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1563 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1564 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1566 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1567 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1571 /* assign update-group/subgroup */
1572 update_group_adjust_peer_afs(peer
);
1574 /* graceful restart */
1575 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1576 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1577 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++) {
1578 if (peer
->afc_nego
[afi
][safi
]
1579 && CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
)
1580 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1581 PEER_CAP_RESTART_AF_RCV
)) {
1582 if (peer
->nsf
[afi
][safi
]
1584 peer
->af_cap
[afi
][safi
],
1585 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1586 bgp_clear_stale_route(peer
, afi
, safi
);
1588 peer
->nsf
[afi
][safi
] = 1;
1591 if (peer
->nsf
[afi
][safi
])
1592 bgp_clear_stale_route(peer
, afi
, safi
);
1593 peer
->nsf
[afi
][safi
] = 0;
1598 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1600 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1601 if (peer
->t_gr_stale
) {
1602 BGP_TIMER_OFF(peer
->t_gr_stale
);
1603 if (bgp_debug_neighbor_events(peer
))
1605 "%s graceful restart stalepath timer stopped",
1610 if (peer
->t_gr_restart
) {
1611 BGP_TIMER_OFF(peer
->t_gr_restart
);
1612 if (bgp_debug_neighbor_events(peer
))
1613 zlog_debug("%s graceful restart timer stopped",
1617 hook_call(peer_established
, peer
);
1619 /* Reset uptime, turn on keepalives, send current table. */
1620 if (!peer
->v_holdtime
)
1621 bgp_keepalives_on(peer
);
1623 peer
->uptime
= bgp_clock();
1625 /* Send route-refresh when ORF is enabled */
1626 FOREACH_AFI_SAFI (afi
, safi
) {
1627 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1628 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
1629 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1630 PEER_CAP_ORF_PREFIX_RM_RCV
))
1631 bgp_route_refresh_send(peer
, afi
, safi
,
1633 REFRESH_IMMEDIATE
, 0);
1634 else if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1635 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1636 bgp_route_refresh_send(peer
, afi
, safi
,
1637 ORF_TYPE_PREFIX_OLD
,
1638 REFRESH_IMMEDIATE
, 0);
1642 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1643 FOREACH_AFI_SAFI (afi
, safi
) {
1644 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1645 PEER_CAP_ORF_PREFIX_RM_ADV
))
1646 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1647 PEER_CAP_ORF_PREFIX_SM_RCV
)
1648 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1649 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1650 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1651 PEER_STATUS_ORF_WAIT_REFRESH
);
1654 bgp_announce_peer(peer
);
1656 /* Start the route advertisement timer to send updates to the peer - if
1658 * is not in read-only mode. If it is, the timer will be started at the
1660 * of read-only mode.
1662 if (!bgp_update_delay_active(peer
->bgp
)) {
1663 BGP_TIMER_OFF(peer
->t_routeadv
);
1664 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1667 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
1668 if (bgp_debug_neighbor_events(peer
))
1670 "[Event] Deleting stub connection for peer %s",
1673 if (peer
->doppelganger
->status
> Active
)
1674 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1675 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1677 peer_delete(peer
->doppelganger
);
1681 * If we are replacing the old peer for a doppelganger
1682 * then switch it around in the bgp->peerhash
1683 * the doppelgangers su and this peer's su are the same
1684 * so the hash_release is the same for either.
1686 hash_release(peer
->bgp
->peerhash
, peer
);
1687 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
1689 bgp_bfd_register_peer(peer
);
1693 /* Keepalive packet is received. */
1694 static int bgp_fsm_keepalive(struct peer
*peer
)
1696 BGP_TIMER_OFF(peer
->t_holdtime
);
1700 /* Update packet is received. */
1701 static int bgp_fsm_update(struct peer
*peer
)
1703 BGP_TIMER_OFF(peer
->t_holdtime
);
1707 /* This is empty event. */
1708 static int bgp_ignore(struct peer
*peer
)
1712 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1713 peer
->host
, bgp_event_str
[peer
->cur_event
],
1714 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1715 bgp_event_str
[peer
->last_event
],
1716 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1720 /* This is to handle unexpected events.. */
1721 static int bgp_fsm_exeption(struct peer
*peer
)
1725 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1726 peer
->host
, bgp_event_str
[peer
->cur_event
],
1727 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1728 bgp_event_str
[peer
->last_event
],
1729 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1730 return (bgp_stop(peer
));
1733 void bgp_fsm_nht_update(struct peer
*peer
, int valid
)
1738 switch (peer
->status
) {
1741 BGP_EVENT_ADD(peer
, BGP_Start
);
1745 BGP_TIMER_OFF(peer
->t_connect
);
1746 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1751 BGP_TIMER_OFF(peer
->t_connect
);
1752 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1758 if (!valid
&& (peer
->gtsm_hops
== 1))
1759 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1768 /* Finite State Machine structure */
1769 static const struct {
1770 int (*func
)(struct peer
*);
1772 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
1774 /* Idle state: In Idle state, all events other than BGP_Start is
1775 ignored. With BGP_Start event, finite state machine calls
1777 {bgp_start
, Connect
}, /* BGP_Start */
1778 {bgp_stop
, Idle
}, /* BGP_Stop */
1779 {bgp_stop
, Idle
}, /* TCP_connection_open */
1780 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1781 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1782 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1783 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1784 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1785 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1786 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1787 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1788 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1789 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1790 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1794 {bgp_ignore
, Connect
}, /* BGP_Start */
1795 {bgp_stop
, Idle
}, /* BGP_Stop */
1796 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1797 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1798 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1799 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1800 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1801 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1802 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1803 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1804 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1805 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1806 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1807 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1811 {bgp_ignore
, Active
}, /* BGP_Start */
1812 {bgp_stop
, Idle
}, /* BGP_Stop */
1813 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1814 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1815 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1816 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1817 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1818 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1819 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1820 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1821 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1822 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1823 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1824 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1828 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1829 {bgp_stop
, Idle
}, /* BGP_Stop */
1830 {bgp_stop
, Active
}, /* TCP_connection_open */
1831 {bgp_stop
, Active
}, /* TCP_connection_closed */
1832 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1833 {bgp_stop
, Active
}, /* TCP_fatal_error */
1834 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1835 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1836 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1837 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1838 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1839 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1840 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1841 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1845 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1846 {bgp_stop
, Idle
}, /* BGP_Stop */
1847 {bgp_stop
, Idle
}, /* TCP_connection_open */
1848 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1849 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1850 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1851 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1852 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1853 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1854 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1855 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1856 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1857 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1858 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1862 {bgp_ignore
, Established
}, /* BGP_Start */
1863 {bgp_stop
, Clearing
}, /* BGP_Stop */
1864 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1865 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1866 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1867 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1868 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1869 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1870 {bgp_ignore
, Established
}, /* KeepAlive_timer_expired */
1871 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1873 Established
}, /* Receive_KEEPALIVE_message */
1874 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1875 {bgp_stop_with_error
,
1876 Clearing
}, /* Receive_NOTIFICATION_message */
1877 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1881 {bgp_ignore
, Clearing
}, /* BGP_Start */
1882 {bgp_stop
, Clearing
}, /* BGP_Stop */
1883 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1884 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1885 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1886 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1887 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1888 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1889 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1890 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1891 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1892 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1893 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1894 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1898 {bgp_ignore
, Deleted
}, /* BGP_Start */
1899 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1900 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1901 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1902 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1903 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1904 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1905 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1906 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1907 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1908 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1909 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1910 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1911 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1915 /* Execute event process. */
1916 int bgp_event(struct thread
*thread
)
1922 peer
= THREAD_ARG(thread
);
1923 event
= THREAD_VAL(thread
);
1925 ret
= bgp_event_update(peer
, event
);
1930 int bgp_event_update(struct peer
*peer
, int event
)
1935 int passive_conn
= 0;
1938 /* default return code */
1939 ret
= FSM_PEER_NOOP
;
1941 other
= peer
->doppelganger
;
1943 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1944 dyn_nbr
= peer_dynamic_neighbor(peer
);
1946 /* Logging this event. */
1947 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
1949 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1950 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1951 bgp_event_str
[event
],
1952 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1953 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
1955 peer
->last_event
= peer
->cur_event
;
1956 peer
->cur_event
= event
;
1958 /* Call function. */
1959 if (FSM
[peer
->status
- 1][event
- 1].func
)
1960 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1963 if (ret
== 1 && next
== Established
) {
1964 /* The case when doppelganger swap accurred in
1966 Update the peer pointer accordingly */
1967 ret
= FSM_PEER_TRANSFERRED
;
1971 /* If status is changed. */
1972 if (next
!= peer
->status
) {
1973 bgp_fsm_change_status(peer
, next
);
1976 * If we're going to ESTABLISHED then we executed a
1977 * peer transfer. In this case we can either return
1978 * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED.
1979 * Opting for TRANSFERRED since transfer implies
1980 * session establishment.
1982 if (ret
!= FSM_PEER_TRANSFERRED
)
1983 ret
= FSM_PEER_TRANSITIONED
;
1986 /* Make sure timer is set. */
1987 bgp_timer_set(peer
);
1991 * If we got a return value of -1, that means there was an
1992 * error, restart the FSM. Since bgp_stop() was called on the
1993 * peer. only a few fields are safe to access here. In any case
1994 * we need to indicate that the peer was stopped in the return
1997 if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
2000 "%s [FSM] Failure handling event %s in state %s, "
2001 "prior events %s, %s, fd %d",
2002 peer
->host
, bgp_event_str
[peer
->cur_event
],
2003 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2004 bgp_event_str
[peer
->last_event
],
2005 bgp_event_str
[peer
->last_major_event
],
2008 bgp_fsm_change_status(peer
, Idle
);
2009 bgp_timer_set(peer
);
2011 ret
= FSM_PEER_STOPPED
;