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"
32 #include "workqueue.h"
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_fsm.h"
42 #include "bgpd/bgp_packet.h"
43 #include "bgpd/bgp_network.h"
44 #include "bgpd/bgp_route.h"
45 #include "bgpd/bgp_dump.h"
46 #include "bgpd/bgp_open.h"
47 #include "bgpd/bgp_advertise.h"
48 #include "bgpd/bgp_updgrp.h"
49 #include "bgpd/bgp_nht.h"
50 #include "bgpd/bgp_bfd.h"
51 #include "bgpd/bgp_memory.h"
53 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
))
54 DEFINE_HOOK(peer_established
, (struct peer
* peer
), (peer
))
56 /* Definition of display strings corresponding to FSM events. This should be
57 * kept consistent with the events defined in bgpd.h
59 static const char *bgp_event_str
[] = {
63 "TCP_connection_open",
64 "TCP_connection_closed",
65 "TCP_connection_open_failed",
67 "ConnectRetry_timer_expired",
69 "KeepAlive_timer_expired",
70 "Receive_OPEN_message",
71 "Receive_KEEPALIVE_message",
72 "Receive_UPDATE_message",
73 "Receive_NOTIFICATION_message",
77 /* BGP FSM (finite state machine) has three types of functions. Type
78 one is thread functions. Type two is event functions. Type three
79 is FSM functions. Timer functions are set by bgp_timer_set
82 /* BGP event function. */
83 int bgp_event(struct thread
*);
85 /* BGP thread functions. */
86 static int bgp_start_timer(struct thread
*);
87 static int bgp_connect_timer(struct thread
*);
88 static int bgp_holdtime_timer(struct thread
*);
89 static int bgp_keepalive_timer(struct thread
*);
91 /* BGP FSM functions. */
92 static int bgp_start(struct peer
*);
94 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
96 /* Copy stats over. These are only the pre-established state stats */
97 peer_dst
->open_in
+= peer_src
->open_in
;
98 peer_dst
->open_out
+= peer_src
->open_out
;
99 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
100 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
101 peer_dst
->notify_in
+= peer_src
->notify_in
;
102 peer_dst
->notify_out
+= peer_src
->notify_out
;
103 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
104 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
107 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
114 unsigned char last_evt
, last_maj_evt
;
116 assert(from_peer
!= NULL
);
118 peer
= from_peer
->doppelganger
;
120 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
123 if (bgp_debug_neighbor_events(peer
))
124 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
125 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
128 BGP_WRITE_OFF(peer
->t_write
);
129 BGP_READ_OFF(peer
->t_read
);
130 BGP_WRITE_OFF(from_peer
->t_write
);
131 BGP_READ_OFF(from_peer
->t_read
);
133 BGP_TIMER_OFF(peer
->t_routeadv
);
134 BGP_TIMER_OFF(from_peer
->t_routeadv
);
137 peer
->fd
= from_peer
->fd
;
139 stream_reset(peer
->ibuf
);
140 stream_fifo_clean(peer
->obuf
);
141 stream_fifo_clean(from_peer
->obuf
);
143 peer
->as
= from_peer
->as
;
144 peer
->v_holdtime
= from_peer
->v_holdtime
;
145 peer
->v_keepalive
= from_peer
->v_keepalive
;
146 peer
->routeadv
= from_peer
->routeadv
;
147 peer
->v_routeadv
= from_peer
->v_routeadv
;
148 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
149 peer
->cap
= from_peer
->cap
;
150 status
= peer
->status
;
151 pstatus
= peer
->ostatus
;
152 last_evt
= peer
->last_event
;
153 last_maj_evt
= peer
->last_major_event
;
154 peer
->status
= from_peer
->status
;
155 peer
->ostatus
= from_peer
->ostatus
;
156 peer
->last_event
= from_peer
->last_event
;
157 peer
->last_major_event
= from_peer
->last_major_event
;
158 from_peer
->status
= status
;
159 from_peer
->ostatus
= pstatus
;
160 from_peer
->last_event
= last_evt
;
161 from_peer
->last_major_event
= last_maj_evt
;
162 peer
->remote_id
= from_peer
->remote_id
;
164 if (from_peer
->hostname
!= NULL
) {
165 if (peer
->hostname
) {
166 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
167 peer
->hostname
= NULL
;
170 peer
->hostname
= from_peer
->hostname
;
171 from_peer
->hostname
= NULL
;
174 if (from_peer
->domainname
!= NULL
) {
175 if (peer
->domainname
) {
176 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
177 peer
->domainname
= NULL
;
180 peer
->domainname
= from_peer
->domainname
;
181 from_peer
->domainname
= NULL
;
184 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
185 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
186 peer
->af_flags
[afi
][safi
] =
187 from_peer
->af_flags
[afi
][safi
];
188 peer
->af_sflags
[afi
][safi
] =
189 from_peer
->af_sflags
[afi
][safi
];
190 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
191 peer
->afc_nego
[afi
][safi
] =
192 from_peer
->afc_nego
[afi
][safi
];
193 peer
->afc_adv
[afi
][safi
] =
194 from_peer
->afc_adv
[afi
][safi
];
195 peer
->afc_recv
[afi
][safi
] =
196 from_peer
->afc_recv
[afi
][safi
];
197 peer
->orf_plist
[afi
][safi
] =
198 from_peer
->orf_plist
[afi
][safi
];
201 if (bgp_getsockname(peer
) < 0) {
203 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
204 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
207 peer
->host
, peer
->fd
, from_peer
->fd
);
212 if (from_peer
->status
> Active
) {
213 if (bgp_getsockname(from_peer
) < 0) {
215 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
216 (CHECK_FLAG(from_peer
->sflags
,
217 PEER_STATUS_ACCEPT_PEER
)
220 from_peer
->host
, from_peer
->fd
, peer
->fd
);
226 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
227 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
230 peer_xfer_stats(peer
, from_peer
);
235 /* Hook function called after bgp event is occered. And vty's
236 neighbor command invoke this function after making neighbor
238 void bgp_timer_set(struct peer
*peer
)
240 switch (peer
->status
) {
242 /* First entry point of peer's finite state machine. In Idle
243 status start timer is on unless peer is shutdown or peer is
244 inactive. All other timer must be turned off */
245 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)) {
246 BGP_TIMER_OFF(peer
->t_start
);
248 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
251 BGP_TIMER_OFF(peer
->t_connect
);
252 BGP_TIMER_OFF(peer
->t_holdtime
);
253 BGP_TIMER_OFF(peer
->t_keepalive
);
254 BGP_TIMER_OFF(peer
->t_routeadv
);
258 /* After start timer is expired, the peer moves to Connect
259 status. Make sure start timer is off and connect timer is
261 BGP_TIMER_OFF(peer
->t_start
);
262 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
264 BGP_TIMER_OFF(peer
->t_holdtime
);
265 BGP_TIMER_OFF(peer
->t_keepalive
);
266 BGP_TIMER_OFF(peer
->t_routeadv
);
270 /* Active is waiting connection from remote peer. And if
271 connect timer is expired, change status to Connect. */
272 BGP_TIMER_OFF(peer
->t_start
);
273 /* If peer is passive mode, do not set connect timer. */
274 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
275 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
276 BGP_TIMER_OFF(peer
->t_connect
);
278 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
281 BGP_TIMER_OFF(peer
->t_holdtime
);
282 BGP_TIMER_OFF(peer
->t_keepalive
);
283 BGP_TIMER_OFF(peer
->t_routeadv
);
287 /* OpenSent status. */
288 BGP_TIMER_OFF(peer
->t_start
);
289 BGP_TIMER_OFF(peer
->t_connect
);
290 if (peer
->v_holdtime
!= 0) {
291 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
294 BGP_TIMER_OFF(peer
->t_holdtime
);
296 BGP_TIMER_OFF(peer
->t_keepalive
);
297 BGP_TIMER_OFF(peer
->t_routeadv
);
301 /* OpenConfirm status. */
302 BGP_TIMER_OFF(peer
->t_start
);
303 BGP_TIMER_OFF(peer
->t_connect
);
305 /* If the negotiated Hold Time value is zero, then the Hold Time
306 timer and KeepAlive timers are not started. */
307 if (peer
->v_holdtime
== 0) {
308 BGP_TIMER_OFF(peer
->t_holdtime
);
309 BGP_TIMER_OFF(peer
->t_keepalive
);
311 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
313 BGP_TIMER_ON(peer
->t_keepalive
, bgp_keepalive_timer
,
316 BGP_TIMER_OFF(peer
->t_routeadv
);
320 /* In Established status start and connect timer is turned
322 BGP_TIMER_OFF(peer
->t_start
);
323 BGP_TIMER_OFF(peer
->t_connect
);
325 /* Same as OpenConfirm, if holdtime is zero then both holdtime
326 and keepalive must be turned off. */
327 if (peer
->v_holdtime
== 0) {
328 BGP_TIMER_OFF(peer
->t_holdtime
);
329 BGP_TIMER_OFF(peer
->t_keepalive
);
331 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
333 BGP_TIMER_ON(peer
->t_keepalive
, bgp_keepalive_timer
,
338 BGP_TIMER_OFF(peer
->t_gr_restart
);
339 BGP_TIMER_OFF(peer
->t_gr_stale
);
340 BGP_TIMER_OFF(peer
->t_pmax_restart
);
343 BGP_TIMER_OFF(peer
->t_start
);
344 BGP_TIMER_OFF(peer
->t_connect
);
345 BGP_TIMER_OFF(peer
->t_holdtime
);
346 BGP_TIMER_OFF(peer
->t_keepalive
);
347 BGP_TIMER_OFF(peer
->t_routeadv
);
352 /* BGP start timer. This function set BGP_Start event to thread value
353 and process event. */
354 static int bgp_start_timer(struct thread
*thread
)
358 peer
= THREAD_ARG(thread
);
359 peer
->t_start
= NULL
;
361 if (bgp_debug_neighbor_events(peer
))
362 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
364 THREAD_VAL(thread
) = BGP_Start
;
365 bgp_event(thread
); /* bgp_event unlocks peer */
370 /* BGP connect retry timer. */
371 static int bgp_connect_timer(struct thread
*thread
)
376 peer
= THREAD_ARG(thread
);
377 peer
->t_connect
= NULL
;
379 if (bgp_debug_neighbor_events(peer
))
380 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
382 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
386 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
387 bgp_event(thread
); /* bgp_event unlocks peer */
394 /* BGP holdtime timer. */
395 static int bgp_holdtime_timer(struct thread
*thread
)
399 peer
= THREAD_ARG(thread
);
400 peer
->t_holdtime
= NULL
;
402 if (bgp_debug_neighbor_events(peer
))
403 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
406 THREAD_VAL(thread
) = Hold_Timer_expired
;
407 bgp_event(thread
); /* bgp_event unlocks peer */
412 /* BGP keepalive fire ! */
413 static int bgp_keepalive_timer(struct thread
*thread
)
417 peer
= THREAD_ARG(thread
);
418 peer
->t_keepalive
= NULL
;
420 if (bgp_debug_neighbor_events(peer
))
421 zlog_debug("%s [FSM] Timer (keepalive timer expire)",
424 THREAD_VAL(thread
) = KeepAlive_timer_expired
;
425 bgp_event(thread
); /* bgp_event unlocks peer */
430 int bgp_routeadv_timer(struct thread
*thread
)
434 peer
= THREAD_ARG(thread
);
435 peer
->t_routeadv
= NULL
;
437 if (bgp_debug_neighbor_events(peer
))
438 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
441 peer
->synctime
= bgp_clock();
443 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
445 /* MRAI timer will be started again when FIFO is built, no need to
451 /* BGP Peer Down Cause */
452 const char *peer_down_str
[] = {"",
456 "Cluster ID changed",
457 "Confederation identifier changed",
458 "Confederation peer changed",
459 "RR client config change",
460 "RS client config change",
461 "Update source change",
462 "Address family activated",
465 "BGP Notification received",
466 "BGP Notification send",
467 "Peer closed the session",
469 "Peer-group add member",
470 "Peer-group delete member",
471 "Capability changed",
472 "Passive config change",
473 "Multihop config change",
474 "NSF peer closed the session",
475 "Intf peering v6only config change",
478 "Neighbor address lost"};
480 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
486 peer
= THREAD_ARG(thread
);
487 peer
->t_gr_restart
= NULL
;
489 /* NSF delete stale route */
490 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
491 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_4
; safi
++)
492 if (peer
->nsf
[afi
][safi
])
493 bgp_clear_stale_route(peer
, afi
, safi
);
495 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
496 BGP_TIMER_OFF(peer
->t_gr_stale
);
498 if (bgp_debug_neighbor_events(peer
)) {
499 zlog_debug("%s graceful restart timer expired", peer
->host
);
500 zlog_debug("%s graceful restart stalepath timer stopped",
509 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
515 peer
= THREAD_ARG(thread
);
516 peer
->t_gr_stale
= NULL
;
518 if (bgp_debug_neighbor_events(peer
))
519 zlog_debug("%s graceful restart stalepath timer expired",
522 /* NSF delete stale route */
523 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
524 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_4
; safi
++)
525 if (peer
->nsf
[afi
][safi
])
526 bgp_clear_stale_route(peer
, afi
, safi
);
531 static int bgp_update_delay_applicable(struct bgp
*bgp
)
533 /* update_delay_over flag should be reset (set to 0) for any new
534 applicability of the update-delay during BGP process lifetime.
535 And it should be set after an occurence of the update-delay is
537 if (!bgp
->update_delay_over
)
543 int bgp_update_delay_active(struct bgp
*bgp
)
545 if (bgp
->t_update_delay
)
551 int bgp_update_delay_configured(struct bgp
*bgp
)
553 if (bgp
->v_update_delay
)
559 /* Do the post-processing needed when bgp comes out of the read-only mode
560 on ending the update delay. */
561 void bgp_update_delay_end(struct bgp
*bgp
)
563 THREAD_TIMER_OFF(bgp
->t_update_delay
);
564 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
566 /* Reset update-delay related state */
567 bgp
->update_delay_over
= 1;
568 bgp
->established
= 0;
569 bgp
->restarted_peers
= 0;
570 bgp
->implicit_eors
= 0;
571 bgp
->explicit_eors
= 0;
573 quagga_timestamp(3, bgp
->update_delay_end_time
,
574 sizeof(bgp
->update_delay_end_time
));
577 * Add an end-of-initial-update marker to the main process queues so
579 * the route advertisement timer for the peers can be started. Also set
580 * the zebra and peer update hold flags. These flags are used to achieve
581 * three stages in the update-delay post processing:
582 * 1. Finish best-path selection for all the prefixes held on the
584 * (routes in BGP are updated, and peers sync queues are populated
586 * 2. As the eoiu mark is reached in the bgp process routine, ship all
588 * routes to zebra. With that zebra should see updates from BGP
591 * 3. Unblock the peer update writes. With that peer update packing
593 * the prefixes should be at its maximum.
595 bgp_add_eoiu_mark(bgp
);
596 bgp
->main_zebra_update_hold
= 1;
597 bgp
->main_peers_update_hold
= 1;
599 /* Resume the queue processing. This should trigger the event that would
601 care of processing any work that was queued during the read-only
603 work_queue_unplug(bm
->process_main_queue
);
609 void bgp_start_routeadv(struct bgp
*bgp
)
611 struct listnode
*node
, *nnode
;
614 zlog_info("bgp_start_routeadv(), update hold status %d",
615 bgp
->main_peers_update_hold
);
617 if (bgp
->main_peers_update_hold
)
620 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
621 sizeof(bgp
->update_delay_peers_resume_time
));
623 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
624 if (peer
->status
!= Established
)
626 BGP_TIMER_OFF(peer
->t_routeadv
);
627 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
634 void bgp_adjust_routeadv(struct peer
*peer
)
636 time_t nowtime
= bgp_clock();
638 unsigned long remain
;
640 /* Bypass checks for special case of MRAI being 0 */
641 if (peer
->v_routeadv
== 0) {
642 /* Stop existing timer, just in case it is running for a
644 * duration and schedule write thread immediately.
646 if (peer
->t_routeadv
)
647 BGP_TIMER_OFF(peer
->t_routeadv
);
649 peer
->synctime
= bgp_clock();
650 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
657 * If the last update was written more than MRAI back, expire the timer
658 * instantly so that we can send the update out sooner.
660 * <------- MRAI --------->
661 * |-----------------|-----------------------|
662 * <------------- m ------------>
671 diff
= difftime(nowtime
, peer
->last_update
);
672 if (diff
> (double)peer
->v_routeadv
) {
673 BGP_TIMER_OFF(peer
->t_routeadv
);
674 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
680 * - Find when to expire the MRAI timer.
681 * If MRAI timer is not active, assume we can start it now.
683 * <------- MRAI --------->
684 * |------------|-----------------------|
685 * <-------- m ----------><----- r ----->
694 if (peer
->t_routeadv
)
695 remain
= thread_timer_remain_second(peer
->t_routeadv
);
697 remain
= peer
->v_routeadv
;
698 diff
= peer
->v_routeadv
- diff
;
699 if (diff
<= (double)remain
) {
700 BGP_TIMER_OFF(peer
->t_routeadv
);
701 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
705 static int bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
707 if (!bgp
->maxmed_onstartup_over
)
713 int bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
715 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
721 int bgp_maxmed_onstartup_active(struct bgp
*bgp
)
723 if (bgp
->t_maxmed_onstartup
)
729 void bgp_maxmed_update(struct bgp
*bgp
)
731 u_char maxmed_active
;
732 u_int32_t maxmed_value
;
734 if (bgp
->v_maxmed_admin
) {
736 maxmed_value
= bgp
->maxmed_admin_value
;
737 } else if (bgp
->t_maxmed_onstartup
) {
739 maxmed_value
= bgp
->maxmed_onstartup_value
;
742 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
745 if (bgp
->maxmed_active
!= maxmed_active
746 || bgp
->maxmed_value
!= maxmed_value
) {
747 bgp
->maxmed_active
= maxmed_active
;
748 bgp
->maxmed_value
= maxmed_value
;
750 update_group_announce(bgp
);
754 /* The maxmed onstartup timer expiry callback. */
755 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
759 zlog_info("Max med on startup ended - timer expired.");
761 bgp
= THREAD_ARG(thread
);
762 THREAD_TIMER_OFF(bgp
->t_maxmed_onstartup
);
763 bgp
->maxmed_onstartup_over
= 1;
765 bgp_maxmed_update(bgp
);
770 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
772 /* Applicable only once in the process lifetime on the startup */
773 if (bgp
->maxmed_onstartup_over
)
776 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
777 bgp
->v_maxmed_onstartup
);
779 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
780 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
782 if (!bgp
->v_maxmed_admin
) {
783 bgp
->maxmed_active
= 1;
784 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
787 /* Route announce to all peers should happen after this in
791 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
793 if (peer
->status
== Established
&& !peer
->bgp
->established
) {
794 bgp_maxmed_onstartup_begin(peer
->bgp
);
798 /* The update delay timer expiry callback. */
799 static int bgp_update_delay_timer(struct thread
*thread
)
803 zlog_info("Update delay ended - timer expired.");
805 bgp
= THREAD_ARG(thread
);
806 THREAD_TIMER_OFF(bgp
->t_update_delay
);
807 bgp_update_delay_end(bgp
);
812 /* The establish wait timer expiry callback. */
813 static int bgp_establish_wait_timer(struct thread
*thread
)
817 zlog_info("Establish wait - timer expired.");
819 bgp
= THREAD_ARG(thread
);
820 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
821 bgp_check_update_delay(bgp
);
826 /* Steps to begin the update delay:
827 - initialize queues if needed
828 - stop the queue processing
830 static void bgp_update_delay_begin(struct bgp
*bgp
)
832 struct listnode
*node
, *nnode
;
835 /* Stop the processing of queued work. Enqueue shall continue */
836 work_queue_plug(bm
->process_main_queue
);
838 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
839 peer
->update_delay_over
= 0;
841 /* Start the update-delay timer */
842 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
843 bgp
->v_update_delay
, &bgp
->t_update_delay
);
845 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
846 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
847 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
849 quagga_timestamp(3, bgp
->update_delay_begin_time
,
850 sizeof(bgp
->update_delay_begin_time
));
853 static void bgp_update_delay_process_status_change(struct peer
*peer
)
855 if (peer
->status
== Established
) {
856 if (!peer
->bgp
->established
++) {
857 bgp_update_delay_begin(peer
->bgp
);
859 "Begin read-only mode - update-delay timer %d seconds",
860 peer
->bgp
->v_update_delay
);
862 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
863 bgp_update_restarted_peers(peer
);
865 if (peer
->ostatus
== Established
866 && bgp_update_delay_active(peer
->bgp
)) {
867 /* Adjust the update-delay state to account for this flap.
868 NOTE: Intentionally skipping adjusting implicit_eors or
870 counters. Extra sanity check in bgp_check_update_delay()
872 be enough to take care of any additive discrepancy in bgp eor
874 peer
->bgp
->established
--;
875 peer
->update_delay_over
= 0;
879 /* Called after event occured, this function change status and reset
880 read/write and timer thread. */
881 void bgp_fsm_change_status(struct peer
*peer
, int status
)
884 bgp_dump_state(peer
, peer
->status
, status
);
886 /* Transition into Clearing or Deleted must /always/ clear all routes..
887 * (and must do so before actually changing into Deleted..
889 if (status
>= Clearing
) {
890 bgp_clear_route_all(peer
);
892 /* If no route was queued for the clear-node processing,
894 * completion event here. This is needed because if there are no
896 * to trigger the background clear-node thread, the event won't
898 * generated and the peer would be stuck in Clearing. Note that
900 * event is for the peer and helps the peer transition out of
902 * state; it should not be generated per (AFI,SAFI). The event
904 * directly posted here without calling clear_node_complete() as
906 * shouldn't do an extra unlock. This event will get processed
908 * the state change that happens below, so peer will be in
912 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
913 BGP_EVENT_ADD(peer
, Clearing_Completed
);
916 /* Preserve old status and change into new status. */
917 peer
->ostatus
= peer
->status
;
918 peer
->status
= status
;
920 /* Save event that caused status change. */
921 peer
->last_major_event
= peer
->cur_event
;
923 if (status
== Established
)
924 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
926 /* If max-med processing is applicable, do the necessary. */
927 if (status
== Established
) {
928 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
929 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
930 bgp_maxmed_onstartup_process_status_change(peer
);
932 peer
->bgp
->maxmed_onstartup_over
= 1;
935 /* If update-delay processing is applicable, do the necessary. */
936 if (bgp_update_delay_configured(peer
->bgp
)
937 && bgp_update_delay_applicable(peer
->bgp
))
938 bgp_update_delay_process_status_change(peer
);
940 if (bgp_debug_neighbor_events(peer
))
941 zlog_debug("%s went from %s to %s", peer
->host
,
942 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
943 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
946 /* Flush the event queue and ensure the peer is shut down */
947 static int bgp_clearing_completed(struct peer
*peer
)
949 int rc
= bgp_stop(peer
);
952 BGP_EVENT_FLUSH(peer
);
957 /* Administrative BGP peer stop event. */
958 /* May be called multiple times for the same peer */
959 int bgp_stop(struct peer
*peer
)
963 char orf_name
[BUFSIZ
];
966 if (peer_dynamic_neighbor(peer
)
967 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
968 if (bgp_debug_neighbor_events(peer
))
969 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
974 /* Can't do this in Clearing; events are used for state transitions */
975 if (peer
->status
!= Clearing
) {
976 /* Delete all existing events of the peer */
977 BGP_EVENT_FLUSH(peer
);
980 /* Increment Dropped count. */
981 if (peer
->status
== Established
) {
984 /* bgp log-neighbor-changes of neighbor Down */
985 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
986 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
988 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
990 (peer
->hostname
) ? peer
->hostname
: "Unknown",
991 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
994 peer_down_str
[(int)peer
->last_reset
]);
997 /* graceful restart */
998 if (peer
->t_gr_stale
) {
999 BGP_TIMER_OFF(peer
->t_gr_stale
);
1000 if (bgp_debug_neighbor_events(peer
))
1002 "%s graceful restart stalepath timer stopped",
1005 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
1006 if (bgp_debug_neighbor_events(peer
)) {
1008 "%s graceful restart timer started for %d sec",
1009 peer
->host
, peer
->v_gr_restart
);
1011 "%s graceful restart stalepath timer started for %d sec",
1012 peer
->host
, peer
->bgp
->stalepath_time
);
1014 BGP_TIMER_ON(peer
->t_gr_restart
,
1015 bgp_graceful_restart_timer_expire
,
1016 peer
->v_gr_restart
);
1017 BGP_TIMER_ON(peer
->t_gr_stale
,
1018 bgp_graceful_stale_timer_expire
,
1019 peer
->bgp
->stalepath_time
);
1021 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1023 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1024 for (safi
= SAFI_UNICAST
;
1025 safi
< SAFI_RESERVED_4
; safi
++)
1026 peer
->nsf
[afi
][safi
] = 0;
1029 /* set last reset time */
1030 peer
->resettime
= peer
->uptime
= bgp_clock();
1032 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1033 zlog_debug("%s remove from all update group",
1035 update_group_remove_peer_afs(peer
);
1037 hook_call(peer_backward_transition
, peer
);
1039 /* Reset peer synctime */
1042 bgp_bfd_deregister_peer(peer
);
1045 /* Stop read and write threads when exists. */
1046 BGP_READ_OFF(peer
->t_read
);
1047 BGP_WRITE_OFF(peer
->t_write
);
1049 /* Stop all timers. */
1050 BGP_TIMER_OFF(peer
->t_start
);
1051 BGP_TIMER_OFF(peer
->t_connect
);
1052 BGP_TIMER_OFF(peer
->t_holdtime
);
1053 BGP_TIMER_OFF(peer
->t_keepalive
);
1054 BGP_TIMER_OFF(peer
->t_routeadv
);
1057 peer
->packet_size
= 0;
1059 /* Clear input and output buffer. */
1061 stream_reset(peer
->ibuf
);
1063 stream_reset(peer
->work
);
1065 stream_fifo_clean(peer
->obuf
);
1067 /* Close of file descriptor. */
1068 if (peer
->fd
>= 0) {
1073 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1074 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
1075 /* Reset all negotiated variables */
1076 peer
->afc_nego
[afi
][safi
] = 0;
1077 peer
->afc_adv
[afi
][safi
] = 0;
1078 peer
->afc_recv
[afi
][safi
] = 0;
1080 /* peer address family capability flags*/
1081 peer
->af_cap
[afi
][safi
] = 0;
1083 /* peer address family status flags*/
1084 peer
->af_sflags
[afi
][safi
] = 0;
1086 /* Received ORF prefix-filter */
1087 peer
->orf_plist
[afi
][safi
] = NULL
;
1089 if ((peer
->status
== OpenConfirm
)
1090 || (peer
->status
== Established
)) {
1091 /* ORF received prefix-filter pnt */
1092 sprintf(orf_name
, "%s.%d.%d", peer
->host
, afi
,
1094 prefix_bgp_orf_remove_all(afi
, orf_name
);
1098 /* Reset keepalive and holdtime */
1099 if (CHECK_FLAG(peer
->config
, PEER_CONFIG_TIMER
)) {
1100 peer
->v_keepalive
= peer
->keepalive
;
1101 peer
->v_holdtime
= peer
->holdtime
;
1103 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1104 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1107 peer
->update_time
= 0;
1109 /* Until we are sure that there is no problem about prefix count
1110 this should be commented out.*/
1112 /* Reset prefix count */
1113 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1114 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1115 peer
->pcount
[AFI_IP
][SAFI_LABELED_UNICAST
] = 0;
1116 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1117 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1118 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1119 peer
->pcount
[AFI_IP6
][SAFI_LABELED_UNICAST
] = 0;
1122 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1123 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1127 bgp_peer_conf_if_to_su_update(peer
);
1133 /* BGP peer is stoped by the error. */
1134 static int bgp_stop_with_error(struct peer
*peer
)
1136 /* Double start timer. */
1139 /* Overflow check. */
1140 if (peer
->v_start
>= (60 * 2))
1141 peer
->v_start
= (60 * 2);
1143 if (peer_dynamic_neighbor(peer
)) {
1144 if (bgp_debug_neighbor_events(peer
))
1145 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1150 return (bgp_stop(peer
));
1154 /* something went wrong, send notify and tear down */
1155 static int bgp_stop_with_notify(struct peer
*peer
, u_char code
, u_char sub_code
)
1157 /* Send notify to remote peer */
1158 bgp_notify_send(peer
, code
, sub_code
);
1160 if (peer_dynamic_neighbor(peer
)) {
1161 if (bgp_debug_neighbor_events(peer
))
1162 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1167 /* Clear start timer value to default. */
1168 peer
->v_start
= BGP_INIT_START_TIMER
;
1170 return (bgp_stop(peer
));
1174 /* TCP connection open. Next we send open message to remote peer. And
1175 add read thread for reading open message. */
1176 static int bgp_connect_success(struct peer
*peer
)
1179 zlog_err("bgp_connect_success peer's fd is negative value %d",
1185 if (bgp_getsockname(peer
) < 0) {
1186 zlog_err("%s: bgp_getsockname(): failed for peer %s, fd %d",
1187 __FUNCTION__
, peer
->host
, peer
->fd
);
1188 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1189 0); /* internal error */
1193 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
1195 if (bgp_debug_neighbor_events(peer
)) {
1196 char buf1
[SU_ADDRSTRLEN
];
1198 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1199 zlog_debug("%s open active, local address %s",
1201 sockunion2str(peer
->su_local
, buf1
,
1204 zlog_debug("%s passive open", peer
->host
);
1207 bgp_open_send(peer
);
1212 /* TCP connect fail */
1213 static int bgp_connect_fail(struct peer
*peer
)
1215 if (peer_dynamic_neighbor(peer
)) {
1216 if (bgp_debug_neighbor_events(peer
))
1217 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1222 return (bgp_stop(peer
));
1225 /* This function is the first starting point of all BGP connection. It
1226 try to connect to remote peer with non-blocking IO. */
1227 int bgp_start(struct peer
*peer
)
1232 bgp_peer_conf_if_to_su_update(peer
);
1234 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1235 if (bgp_debug_neighbor_events(peer
))
1237 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1242 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1243 if (bgp_debug_neighbor_events(peer
))
1245 "%s [FSM] Trying to start suppressed peer"
1246 " - this is never supposed to happen!",
1251 /* Scrub some information that might be left over from a previous,
1254 /* Connection information. */
1255 if (peer
->su_local
) {
1256 sockunion_free(peer
->su_local
);
1257 peer
->su_local
= NULL
;
1260 if (peer
->su_remote
) {
1261 sockunion_free(peer
->su_remote
);
1262 peer
->su_remote
= NULL
;
1265 /* Clear remote router-id. */
1266 peer
->remote_id
.s_addr
= 0;
1268 /* Clear peer capability flag. */
1271 /* If the peer is passive mode, force to move to Active mode. */
1272 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1273 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1277 /* Register to be notified on peer up */
1278 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
1279 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
1280 && !bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
1285 if (!bgp_find_or_add_nexthop(peer
->bgp
,
1286 family2afi(peer
->su
.sa
.sa_family
), NULL
,
1288 #if defined(HAVE_CUMULUS)
1289 if (bgp_debug_neighbor_events(peer
))
1290 zlog_debug("%s [FSM] Waiting for NHT", peer
->host
);
1292 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1297 status
= bgp_connect(peer
);
1301 if (bgp_debug_neighbor_events(peer
))
1302 zlog_debug("%s [FSM] Connect error", peer
->host
);
1303 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1305 case connect_success
:
1306 if (bgp_debug_neighbor_events(peer
))
1308 "%s [FSM] Connect immediately success, fd %d",
1309 peer
->host
, peer
->fd
);
1310 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1312 case connect_in_progress
:
1313 /* To check nonblocking connect, we wait until socket is
1314 readable or writable. */
1315 if (bgp_debug_neighbor_events(peer
))
1317 "%s [FSM] Non blocking connect waiting result, fd %d",
1318 peer
->host
, peer
->fd
);
1320 zlog_err("bgp_start peer's fd is negative value %d",
1324 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
1325 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
1331 /* Connect retry timer is expired when the peer status is Connect. */
1332 static int bgp_reconnect(struct peer
*peer
)
1334 if (bgp_stop(peer
) < 0)
1341 static int bgp_fsm_open(struct peer
*peer
)
1343 /* Send keepalive and make keepalive timer */
1344 bgp_keepalive_send(peer
);
1346 /* Reset holdtimer value. */
1347 BGP_TIMER_OFF(peer
->t_holdtime
);
1352 /* Keepalive send to peer. */
1353 static int bgp_fsm_keepalive_expire(struct peer
*peer
)
1355 bgp_keepalive_send(peer
);
1359 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1360 peer and change to Idle status. */
1361 static int bgp_fsm_event_error(struct peer
*peer
)
1363 zlog_err("%s [FSM] unexpected packet received in state %s", peer
->host
,
1364 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1366 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1369 /* Hold timer expire. This is error of BGP connection. So cut the
1370 peer and change to Idle status. */
1371 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1373 if (bgp_debug_neighbor_events(peer
))
1374 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1376 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1379 /* Status goes to Established. Send keepalive packet then make first
1380 update information. */
1381 static int bgp_establish(struct peer
*peer
)
1385 int nsf_af_count
= 0;
1389 other
= peer
->doppelganger
;
1390 peer
= peer_xfer_conn(peer
);
1392 zlog_err("%%Neighbor failed in xfer_conn");
1398 1; /* bgp_establish specific code when xfer_conn
1401 /* Reset capability open status flag. */
1402 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1403 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1405 /* Clear start timer value to default. */
1406 peer
->v_start
= BGP_INIT_START_TIMER
;
1408 /* Increment established count. */
1409 peer
->established
++;
1410 bgp_fsm_change_status(peer
, Established
);
1412 /* bgp log-neighbor-changes of neighbor Up */
1413 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1414 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1415 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1417 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1418 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
) ? vrf
->name
1422 /* assign update-group/subgroup */
1423 update_group_adjust_peer_afs(peer
);
1425 /* graceful restart */
1426 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1427 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1428 for (safi
= SAFI_UNICAST
; safi
< SAFI_RESERVED_4
; safi
++) {
1429 if (peer
->afc_nego
[afi
][safi
]
1430 && CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
)
1431 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1432 PEER_CAP_RESTART_AF_RCV
)) {
1433 if (peer
->nsf
[afi
][safi
]
1435 peer
->af_cap
[afi
][safi
],
1436 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1437 bgp_clear_stale_route(peer
, afi
, safi
);
1439 peer
->nsf
[afi
][safi
] = 1;
1442 if (peer
->nsf
[afi
][safi
])
1443 bgp_clear_stale_route(peer
, afi
, safi
);
1444 peer
->nsf
[afi
][safi
] = 0;
1449 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1451 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1452 if (peer
->t_gr_stale
) {
1453 BGP_TIMER_OFF(peer
->t_gr_stale
);
1454 if (bgp_debug_neighbor_events(peer
))
1456 "%s graceful restart stalepath timer stopped",
1461 if (peer
->t_gr_restart
) {
1462 BGP_TIMER_OFF(peer
->t_gr_restart
);
1463 if (bgp_debug_neighbor_events(peer
))
1464 zlog_debug("%s graceful restart timer stopped",
1468 hook_call(peer_established
, peer
);
1470 /* Reset uptime, send keepalive, send current table. */
1471 peer
->uptime
= bgp_clock();
1473 /* Send route-refresh when ORF is enabled */
1474 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1475 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1476 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1477 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
1478 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1479 PEER_CAP_ORF_PREFIX_RM_RCV
))
1480 bgp_route_refresh_send(
1483 REFRESH_IMMEDIATE
, 0);
1486 peer
->af_cap
[afi
][safi
],
1487 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1488 bgp_route_refresh_send(
1490 ORF_TYPE_PREFIX_OLD
,
1491 REFRESH_IMMEDIATE
, 0);
1494 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1495 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1496 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1497 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1498 PEER_CAP_ORF_PREFIX_RM_ADV
))
1499 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1500 PEER_CAP_ORF_PREFIX_SM_RCV
)
1502 peer
->af_cap
[afi
][safi
],
1503 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1504 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1505 PEER_STATUS_ORF_WAIT_REFRESH
);
1507 bgp_announce_peer(peer
);
1509 /* Start the route advertisement timer to send updates to the peer - if
1511 * is not in read-only mode. If it is, the timer will be started at the
1513 * of read-only mode.
1515 if (!bgp_update_delay_active(peer
->bgp
)) {
1516 BGP_TIMER_OFF(peer
->t_routeadv
);
1517 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1520 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
1521 if (bgp_debug_neighbor_events(peer
))
1523 "[Event] Deleting stub connection for peer %s",
1526 if (peer
->doppelganger
->status
> Active
)
1527 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1528 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1530 peer_delete(peer
->doppelganger
);
1533 bgp_bfd_register_peer(peer
);
1537 /* Keepalive packet is received. */
1538 static int bgp_fsm_keepalive(struct peer
*peer
)
1540 bgp_update_implicit_eors(peer
);
1542 /* peer count update */
1543 peer
->keepalive_in
++;
1545 BGP_TIMER_OFF(peer
->t_holdtime
);
1549 /* Update packet is received. */
1550 static int bgp_fsm_update(struct peer
*peer
)
1552 BGP_TIMER_OFF(peer
->t_holdtime
);
1556 /* This is empty event. */
1557 static int bgp_ignore(struct peer
*peer
)
1560 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1561 peer
->host
, bgp_event_str
[peer
->cur_event
],
1562 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1563 bgp_event_str
[peer
->last_event
],
1564 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1568 /* This is to handle unexpected events.. */
1569 static int bgp_fsm_exeption(struct peer
*peer
)
1572 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1573 peer
->host
, bgp_event_str
[peer
->cur_event
],
1574 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1575 bgp_event_str
[peer
->last_event
],
1576 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1577 return (bgp_stop(peer
));
1580 void bgp_fsm_nht_update(struct peer
*peer
, int valid
)
1585 switch (peer
->status
) {
1588 BGP_EVENT_ADD(peer
, BGP_Start
);
1592 BGP_TIMER_OFF(peer
->t_connect
);
1593 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1598 BGP_TIMER_OFF(peer
->t_connect
);
1599 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1605 if (!valid
&& (peer
->gtsm_hops
== 1))
1606 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1615 /* Finite State Machine structure */
1616 static const struct {
1617 int (*func
)(struct peer
*);
1619 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
1621 /* Idle state: In Idle state, all events other than BGP_Start is
1622 ignored. With BGP_Start event, finite state machine calls
1624 {bgp_start
, Connect
}, /* BGP_Start */
1625 {bgp_stop
, Idle
}, /* BGP_Stop */
1626 {bgp_stop
, Idle
}, /* TCP_connection_open */
1627 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1628 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1629 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1630 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1631 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1632 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1633 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1634 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1635 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1636 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1637 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1641 {bgp_ignore
, Connect
}, /* BGP_Start */
1642 {bgp_stop
, Idle
}, /* BGP_Stop */
1643 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1644 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1645 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1646 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1647 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1648 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1649 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1650 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1651 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1652 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1653 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1654 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1658 {bgp_ignore
, Active
}, /* BGP_Start */
1659 {bgp_stop
, Idle
}, /* BGP_Stop */
1660 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1661 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1662 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1663 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1664 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1665 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1666 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1667 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1668 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1669 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1670 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1671 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1675 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1676 {bgp_stop
, Idle
}, /* BGP_Stop */
1677 {bgp_stop
, Active
}, /* TCP_connection_open */
1678 {bgp_stop
, Active
}, /* TCP_connection_closed */
1679 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1680 {bgp_stop
, Active
}, /* TCP_fatal_error */
1681 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1682 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1683 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1684 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1685 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1686 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1687 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1688 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1692 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1693 {bgp_stop
, Idle
}, /* BGP_Stop */
1694 {bgp_stop
, Idle
}, /* TCP_connection_open */
1695 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1696 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1697 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1698 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1699 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1700 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1701 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1702 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1703 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1704 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1705 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1709 {bgp_ignore
, Established
}, /* BGP_Start */
1710 {bgp_stop
, Clearing
}, /* BGP_Stop */
1711 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1712 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1713 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1714 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1715 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1716 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1717 {bgp_fsm_keepalive_expire
,
1718 Established
}, /* KeepAlive_timer_expired */
1719 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1721 Established
}, /* Receive_KEEPALIVE_message */
1722 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1723 {bgp_stop_with_error
,
1724 Clearing
}, /* Receive_NOTIFICATION_message */
1725 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1729 {bgp_ignore
, Clearing
}, /* BGP_Start */
1730 {bgp_stop
, Clearing
}, /* BGP_Stop */
1731 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1732 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1733 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1734 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1735 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1736 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1737 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1738 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1739 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1740 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1741 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1742 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1746 {bgp_ignore
, Deleted
}, /* BGP_Start */
1747 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1748 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1749 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1750 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1751 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1752 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1753 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1754 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1755 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1756 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1757 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1758 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1759 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1763 /* Execute event process. */
1764 int bgp_event(struct thread
*thread
)
1770 peer
= THREAD_ARG(thread
);
1771 event
= THREAD_VAL(thread
);
1773 ret
= bgp_event_update(peer
, event
);
1778 int bgp_event_update(struct peer
*peer
, int event
)
1783 int passive_conn
= 0;
1786 other
= peer
->doppelganger
;
1788 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1789 dyn_nbr
= peer_dynamic_neighbor(peer
);
1791 /* Logging this event. */
1792 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
1794 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1795 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1796 bgp_event_str
[event
],
1797 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1798 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
1800 peer
->last_event
= peer
->cur_event
;
1801 peer
->cur_event
= event
;
1803 /* Call function. */
1804 if (FSM
[peer
->status
- 1][event
- 1].func
)
1805 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1807 /* When function do not want proceed next job return -1. */
1809 if (ret
== 1 && next
== Established
) {
1810 /* The case when doppelganger swap accurred in
1812 Update the peer pointer accordingly */
1816 /* If status is changed. */
1817 if (next
!= peer
->status
)
1818 bgp_fsm_change_status(peer
, next
);
1820 /* Make sure timer is set. */
1821 bgp_timer_set(peer
);
1823 } else if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
1824 /* If we got a return value of -1, that means there was an
1826 * the FSM. If the peer structure was deleted
1829 "%s [FSM] Failure handling event %s in state %s, "
1830 "prior events %s, %s, fd %d",
1831 peer
->host
, bgp_event_str
[peer
->cur_event
],
1832 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1833 bgp_event_str
[peer
->last_event
],
1834 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1836 bgp_fsm_change_status(peer
, Idle
);
1837 bgp_timer_set(peer
);