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
18 along with GNU Zebra; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "sockunion.h"
33 #include "workqueue.h"
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_debug.h"
39 #include "bgpd/bgp_fsm.h"
40 #include "bgpd/bgp_packet.h"
41 #include "bgpd/bgp_network.h"
42 #include "bgpd/bgp_route.h"
43 #include "bgpd/bgp_dump.h"
44 #include "bgpd/bgp_open.h"
45 #include "bgpd/bgp_nht.h"
47 #include "bgpd/bgp_snmp.h"
48 #endif /* HAVE_SNMP */
50 /* BGP FSM (finite state machine) has three types of functions. Type
51 one is thread functions. Type two is event functions. Type three
52 is FSM functions. Timer functions are set by bgp_timer_set
55 /* BGP event function. */
56 int bgp_event (struct thread *);
58 /* BGP thread functions. */
59 static int bgp_start_timer (struct thread *);
60 static int bgp_connect_timer (struct thread *);
61 static int bgp_holdtime_timer (struct thread *);
62 static int bgp_keepalive_timer (struct thread *);
64 /* BGP FSM functions. */
65 static int bgp_start (struct peer *);
67 /* BGP start timer jitter. */
69 bgp_start_jitter (int time)
71 return ((random () % (time + 1)) - (time / 2));
74 /* Check if suppress start/restart of sessions to peer. */
75 #define BGP_PEER_START_SUPPRESSED(P) \
76 (CHECK_FLAG ((P)->flags, PEER_FLAG_SHUTDOWN) \
77 || CHECK_FLAG ((P)->sflags, PEER_STATUS_PREFIX_OVERFLOW))
79 /* Hook function called after bgp event is occered. And vty's
80 neighbor command invoke this function after making neighbor
83 bgp_timer_set (struct peer *peer)
90 /* First entry point of peer's finite state machine. In Idle
91 status start timer is on unless peer is shutdown or peer is
92 inactive. All other timer must be turned off */
93 if (BGP_PEER_START_SUPPRESSED (peer) || ! peer_active (peer))
95 BGP_TIMER_OFF (peer->t_start);
99 jitter = bgp_start_jitter (peer->v_start);
100 BGP_TIMER_ON (peer->t_start, bgp_start_timer,
101 peer->v_start + jitter);
103 BGP_TIMER_OFF (peer->t_connect);
104 BGP_TIMER_OFF (peer->t_holdtime);
105 BGP_TIMER_OFF (peer->t_keepalive);
106 BGP_TIMER_OFF (peer->t_routeadv);
110 /* After start timer is expired, the peer moves to Connect
111 status. Make sure start timer is off and connect timer is
113 BGP_TIMER_OFF (peer->t_start);
114 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
115 BGP_TIMER_OFF (peer->t_holdtime);
116 BGP_TIMER_OFF (peer->t_keepalive);
117 BGP_TIMER_OFF (peer->t_routeadv);
121 /* Active is waiting connection from remote peer. And if
122 connect timer is expired, change status to Connect. */
123 BGP_TIMER_OFF (peer->t_start);
124 /* If peer is passive mode, do not set connect timer. */
125 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)
126 || CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
128 BGP_TIMER_OFF (peer->t_connect);
132 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
134 BGP_TIMER_OFF (peer->t_holdtime);
135 BGP_TIMER_OFF (peer->t_keepalive);
136 BGP_TIMER_OFF (peer->t_routeadv);
140 /* OpenSent status. */
141 BGP_TIMER_OFF (peer->t_start);
142 BGP_TIMER_OFF (peer->t_connect);
143 if (peer->v_holdtime != 0)
145 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
150 BGP_TIMER_OFF (peer->t_holdtime);
152 BGP_TIMER_OFF (peer->t_keepalive);
153 BGP_TIMER_OFF (peer->t_routeadv);
157 /* OpenConfirm status. */
158 BGP_TIMER_OFF (peer->t_start);
159 BGP_TIMER_OFF (peer->t_connect);
161 /* If the negotiated Hold Time value is zero, then the Hold Time
162 timer and KeepAlive timers are not started. */
163 if (peer->v_holdtime == 0)
165 BGP_TIMER_OFF (peer->t_holdtime);
166 BGP_TIMER_OFF (peer->t_keepalive);
170 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
172 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
175 BGP_TIMER_OFF (peer->t_routeadv);
179 /* In Established status start and connect timer is turned
181 BGP_TIMER_OFF (peer->t_start);
182 BGP_TIMER_OFF (peer->t_connect);
184 /* Same as OpenConfirm, if holdtime is zero then both holdtime
185 and keepalive must be turned off. */
186 if (peer->v_holdtime == 0)
188 BGP_TIMER_OFF (peer->t_holdtime);
189 BGP_TIMER_OFF (peer->t_keepalive);
193 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
195 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
200 BGP_TIMER_OFF (peer->t_gr_restart);
201 BGP_TIMER_OFF (peer->t_gr_stale);
202 BGP_TIMER_OFF (peer->t_pmax_restart);
204 BGP_TIMER_OFF (peer->t_start);
205 BGP_TIMER_OFF (peer->t_connect);
206 BGP_TIMER_OFF (peer->t_holdtime);
207 BGP_TIMER_OFF (peer->t_keepalive);
208 BGP_TIMER_OFF (peer->t_routeadv);
212 /* BGP start timer. This function set BGP_Start event to thread value
213 and process event. */
215 bgp_start_timer (struct thread *thread)
219 peer = THREAD_ARG (thread);
220 peer->t_start = NULL;
222 if (BGP_DEBUG (fsm, FSM))
223 zlog (peer->log, LOG_DEBUG,
224 "%s [FSM] Timer (start timer expire).", peer->host);
226 THREAD_VAL (thread) = BGP_Start;
227 bgp_event (thread); /* bgp_event unlocks peer */
232 /* BGP connect retry timer. */
234 bgp_connect_timer (struct thread *thread)
238 peer = THREAD_ARG (thread);
239 peer->t_connect = NULL;
241 if (BGP_DEBUG (fsm, FSM))
242 zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
245 THREAD_VAL (thread) = ConnectRetry_timer_expired;
246 bgp_event (thread); /* bgp_event unlocks peer */
251 /* BGP holdtime timer. */
253 bgp_holdtime_timer (struct thread *thread)
257 peer = THREAD_ARG (thread);
258 peer->t_holdtime = NULL;
260 if (BGP_DEBUG (fsm, FSM))
261 zlog (peer->log, LOG_DEBUG,
262 "%s [FSM] Timer (holdtime timer expire)",
265 THREAD_VAL (thread) = Hold_Timer_expired;
266 bgp_event (thread); /* bgp_event unlocks peer */
271 /* BGP keepalive fire ! */
273 bgp_keepalive_timer (struct thread *thread)
277 peer = THREAD_ARG (thread);
278 peer->t_keepalive = NULL;
280 if (BGP_DEBUG (fsm, FSM))
281 zlog (peer->log, LOG_DEBUG,
282 "%s [FSM] Timer (keepalive timer expire)",
285 THREAD_VAL (thread) = KeepAlive_timer_expired;
286 bgp_event (thread); /* bgp_event unlocks peer */
292 bgp_routeadv_timer (struct thread *thread)
296 peer = THREAD_ARG (thread);
297 peer->t_routeadv = NULL;
299 if (BGP_DEBUG (fsm, FSM))
300 zlog (peer->log, LOG_DEBUG,
301 "%s [FSM] Timer (routeadv timer expire)",
304 peer->synctime = bgp_clock ();
306 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
308 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer,
314 /* BGP Peer Down Cause */
315 const char *peer_down_str[] =
321 "Cluster ID changed",
322 "Confederation identifier changed",
323 "Confederation peer changed",
324 "RR client config change",
325 "RS client config change",
326 "Update source change",
327 "Address family activated",
330 "BGP Notification received",
331 "BGP Notification send",
332 "Peer closed the session",
334 "Peer-group add member",
335 "Peer-group delete member",
336 "Capability changed",
337 "Passive config change",
338 "Multihop config change",
339 "NSF peer closed the session"
343 bgp_graceful_restart_timer_expire (struct thread *thread)
349 peer = THREAD_ARG (thread);
350 peer->t_gr_restart = NULL;
352 /* NSF delete stale route */
353 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
354 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
355 if (peer->nsf[afi][safi])
356 bgp_clear_stale_route (peer, afi, safi);
358 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
359 BGP_TIMER_OFF (peer->t_gr_stale);
361 if (BGP_DEBUG (events, EVENTS))
363 zlog_debug ("%s graceful restart timer expired", peer->host);
364 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
367 bgp_timer_set (peer);
373 bgp_graceful_stale_timer_expire (struct thread *thread)
379 peer = THREAD_ARG (thread);
380 peer->t_gr_stale = NULL;
382 if (BGP_DEBUG (events, EVENTS))
383 zlog_debug ("%s graceful restart stalepath timer expired", peer->host);
385 /* NSF delete stale route */
386 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
387 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
388 if (peer->nsf[afi][safi])
389 bgp_clear_stale_route (peer, afi, safi);
394 /* Called after event occurred, this function change status and reset
395 read/write and timer thread. */
397 bgp_fsm_change_status (struct peer *peer, int status)
399 bgp_dump_state (peer, peer->status, status);
401 /* Transition into Clearing or Deleted must /always/ clear all routes..
402 * (and must do so before actually changing into Deleted..
404 if (status >= Clearing)
406 bgp_clear_route_all (peer);
408 /* If no route was queued for the clear-node processing, generate the
409 * completion event here. This is needed because if there are no routes
410 * to trigger the background clear-node thread, the event won't get
411 * generated and the peer would be stuck in Clearing. Note that this
412 * event is for the peer and helps the peer transition out of Clearing
413 * state; it should not be generated per (AFI,SAFI). The event is
414 * directly posted here without calling clear_node_complete() as we
415 * shouldn't do an extra unlock. This event will get processed after
416 * the state change that happens below, so peer will be in Clearing
419 if (!work_queue_is_scheduled (peer->clear_node_queue))
420 BGP_EVENT_ADD (peer, Clearing_Completed);
423 /* Preserve old status and change into new status. */
424 peer->ostatus = peer->status;
425 peer->status = status;
427 if (BGP_DEBUG (normal, NORMAL))
428 zlog_debug ("%s went from %s to %s",
430 LOOKUP (bgp_status_msg, peer->ostatus),
431 LOOKUP (bgp_status_msg, peer->status));
434 /* Flush the event queue and ensure the peer is shut down */
436 bgp_clearing_completed (struct peer *peer)
438 int rc = bgp_stop(peer);
439 BGP_EVENT_FLUSH (peer);
444 /* Administrative BGP peer stop event. */
445 /* May be called multiple times for the same peer */
447 bgp_stop (struct peer *peer)
451 char orf_name[BUFSIZ];
453 /* Can't do this in Clearing; events are used for state transitions */
454 if (peer->status != Clearing)
456 /* Delete all existing events of the peer */
457 BGP_EVENT_FLUSH (peer);
460 /* Increment Dropped count. */
461 if (peer->status == Established)
465 /* bgp log-neighbor-changes of neighbor Down */
466 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
467 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
468 peer_down_str [(int) peer->last_reset]);
470 /* graceful restart */
471 if (peer->t_gr_stale)
473 BGP_TIMER_OFF (peer->t_gr_stale);
474 if (BGP_DEBUG (events, EVENTS))
475 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
477 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
479 if (BGP_DEBUG (events, EVENTS))
481 zlog_debug ("%s graceful restart timer started for %d sec",
482 peer->host, peer->v_gr_restart);
483 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
484 peer->host, peer->bgp->stalepath_time);
486 BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
488 BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
489 peer->bgp->stalepath_time);
493 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
495 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
496 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
497 peer->nsf[afi][safi] = 0;
500 /* set last reset time */
501 peer->resettime = peer->uptime = bgp_clock ();
504 bgpTrapBackwardTransition (peer);
505 #endif /* HAVE_SNMP */
507 /* Reset peer synctime */
511 /* Stop read and write threads when exists. */
512 BGP_READ_OFF (peer->t_read);
513 BGP_WRITE_OFF (peer->t_write);
515 /* Stop all timers. */
516 BGP_TIMER_OFF (peer->t_start);
517 BGP_TIMER_OFF (peer->t_connect);
518 BGP_TIMER_OFF (peer->t_holdtime);
519 BGP_TIMER_OFF (peer->t_keepalive);
520 BGP_TIMER_OFF (peer->t_routeadv);
523 peer->packet_size = 0;
525 /* Clear input and output buffer. */
527 stream_reset (peer->ibuf);
529 stream_reset (peer->work);
531 stream_fifo_clean (peer->obuf);
533 /* Close of file descriptor. */
540 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
541 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
543 /* Reset all negotiated variables */
544 peer->afc_nego[afi][safi] = 0;
545 peer->afc_adv[afi][safi] = 0;
546 peer->afc_recv[afi][safi] = 0;
548 /* peer address family capability flags*/
549 peer->af_cap[afi][safi] = 0;
551 /* peer address family status flags*/
552 peer->af_sflags[afi][safi] = 0;
554 /* Received ORF prefix-filter */
555 peer->orf_plist[afi][safi] = NULL;
557 /* ORF received prefix-filter pnt */
558 sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
559 prefix_bgp_orf_remove_all (afi, orf_name);
562 /* Reset keepalive and holdtime */
563 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
565 peer->v_keepalive = peer->keepalive;
566 peer->v_holdtime = peer->holdtime;
570 peer->v_keepalive = peer->bgp->default_keepalive;
571 peer->v_holdtime = peer->bgp->default_holdtime;
574 peer->update_time = 0;
576 /* Until we are sure that there is no problem about prefix count
577 this should be commented out.*/
579 /* Reset prefix count */
580 peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
581 peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
582 peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
583 peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
584 peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
590 /* first-val * 2**x back-off, where x is the number of sucessive calls
591 * originally used for peer v_start back-off
593 __attribute__((unused))
595 back_off_exp2 (const int first, int val, const int max)
598 return (val < max ? val : max);
601 /* exponential back off, but biased downward by the initial value.
602 * this bias is significant at lower values, and tends to
603 * insignificance fairly quickly, so it is equal to the previous at
604 * scale. Is below first-val * 1.7**x at x == 6, and below first-val
607 * I.e., this function is useful to get slower growth for the initial
610 __attribute__((unused))
612 back_off_exp2_bias (const int first, int val, const int max)
614 val = (val << 1) - (val > first ? first : 0);
615 return (val < max ? val : max);
618 /* BGP peer is stoped by the error. */
620 bgp_stop_with_error (struct peer *peer)
623 = back_off_exp2_bias (BGP_INIT_START_TIMER, peer->v_start, 60);
629 /* something went wrong, send notify and tear down */
631 bgp_stop_with_notify (struct peer *peer, u_char code, u_char sub_code)
633 /* Send notify to remote peer */
634 bgp_notify_send (peer, code, sub_code);
636 /* Sweep if it is temporary peer. */
637 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
639 zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
644 /* Clear start timer value to default. */
645 peer->v_start = BGP_INIT_START_TIMER;
647 /* bgp_stop needs to be invoked while in Established state */
654 /* TCP connection open. Next we send open message to remote peer. And
655 add read thread for reading open message. */
657 bgp_connect_success (struct peer *peer)
659 struct peer *realpeer;
663 zlog_err ("bgp_connect_success peer's fd is negative value %d",
667 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
669 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
670 bgp_getsockname (peer);
672 if (BGP_DEBUG (normal, NORMAL))
674 char buf1[SU_ADDRSTRLEN];
676 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
677 zlog_debug ("%s open active, local address %s", peer->host,
678 sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
680 zlog_debug ("%s passive open", peer->host);
683 /* Generally we want to send OPEN ASAP. Except, some partial BGP
684 * implementations out there (e.g., conformance test tools / BGP
685 * traffic generators) seem to be a bit funny about connection collisions,
686 * and OPENs before they have sent.
688 * As a hack, delay sending OPEN on an inbound accept-peer session
689 * _IF_ we locally have an outbound connection in progress, i.e.
690 * we're in middle of a connection collision. If we delay, we delay until
691 * an Open is received - as per old Quagga behaviour.
693 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
695 realpeer = peer_lookup (peer->bgp, &peer->su);
697 if (realpeer->status > Idle && realpeer->status <= Established)
699 SET_FLAG (peer->sflags, PEER_STATUS_OPEN_DEFERRED);
704 bgp_open_send (peer);
709 /* TCP connect fail */
711 bgp_connect_fail (struct peer *peer)
717 /* This function is the first starting point of all BGP connection. It
718 try to connect to remote peer with non-blocking IO. */
720 bgp_start (struct peer *peer)
725 if (BGP_PEER_START_SUPPRESSED (peer))
727 if (BGP_DEBUG (fsm, FSM))
728 plog_err (peer->log, "%s [FSM] Trying to start suppressed peer"
729 " - this is never supposed to happen!", peer->host);
733 /* Scrub some information that might be left over from a previous,
736 /* Connection information. */
739 sockunion_free (peer->su_local);
740 peer->su_local = NULL;
745 sockunion_free (peer->su_remote);
746 peer->su_remote = NULL;
749 /* Clear remote router-id. */
750 peer->remote_id.s_addr = 0;
752 /* Clear peer capability flag. */
755 /* If the peer is passive mode, force to move to Active mode. */
756 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
758 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
762 /* Register to be notified on peer up */
763 if ((peer_ttl(peer) == 1 || peer->gtsm_hops == 1) &&
764 ! CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
767 bgp_ensure_nexthop (NULL, peer, connected);
768 status = bgp_connect (peer);
773 if (BGP_DEBUG (fsm, FSM))
774 plog_debug (peer->log, "%s [FSM] Connect error", peer->host);
775 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
777 case connect_success:
778 if (BGP_DEBUG (fsm, FSM))
779 plog_debug (peer->log, "%s [FSM] Connect immediately success",
781 BGP_EVENT_ADD (peer, TCP_connection_open);
783 case connect_in_progress:
784 /* To check nonblocking connect, we wait until socket is
785 readable or writable. */
786 if (BGP_DEBUG (fsm, FSM))
787 plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result",
791 zlog_err ("bgp_start peer's fd is negative value %d",
795 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
796 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
802 /* Connect retry timer is expired when the peer status is Connect. */
804 bgp_reconnect (struct peer *peer)
812 bgp_fsm_open (struct peer *peer)
814 /* Send keepalive and make keepalive timer */
815 bgp_keepalive_send (peer);
817 /* Reset holdtimer value. */
818 BGP_TIMER_OFF (peer->t_holdtime);
823 /* Keepalive send to peer. */
825 bgp_fsm_keepalive_expire (struct peer *peer)
827 bgp_keepalive_send (peer);
831 /* FSM error, unexpected event. This is error of BGP connection. So cut the
832 peer and change to Idle status. */
834 bgp_fsm_event_error (struct peer *peer)
836 plog_err (peer->log, "%s [FSM] unexpected packet received in state %s",
837 peer->host, LOOKUP (bgp_status_msg, peer->status));
839 return bgp_stop_with_notify (peer, BGP_NOTIFY_FSM_ERR, 0);
842 /* Hold timer expire. This is error of BGP connection. So cut the
843 peer and change to Idle status. */
845 bgp_fsm_holdtime_expire (struct peer *peer)
847 if (BGP_DEBUG (fsm, FSM))
848 plog_debug (peer->log, "%s [FSM] Hold timer expire", peer->host);
850 return bgp_stop_with_notify (peer, BGP_NOTIFY_HOLD_ERR, 0);
853 /* Status goes to Established. Send keepalive packet then make first
854 update information. */
856 bgp_establish (struct peer *peer)
858 struct bgp_notify *notify;
861 int nsf_af_count = 0;
863 /* Reset capability open status flag. */
864 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
865 SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
867 /* Clear last notification data. */
868 notify = &peer->notify;
870 XFREE (MTYPE_TMP, notify->data);
871 memset (notify, 0, sizeof (struct bgp_notify));
873 /* Clear start timer value to default. */
874 peer->v_start = BGP_INIT_START_TIMER;
876 /* Increment established count. */
878 bgp_fsm_change_status (peer, Established);
880 /* bgp log-neighbor-changes of neighbor Up */
881 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
882 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
884 /* graceful restart */
885 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
886 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
887 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
889 if (peer->afc_nego[afi][safi]
890 && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
891 && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
893 if (peer->nsf[afi][safi]
894 && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
895 bgp_clear_stale_route (peer, afi, safi);
897 peer->nsf[afi][safi] = 1;
902 if (peer->nsf[afi][safi])
903 bgp_clear_stale_route (peer, afi, safi);
904 peer->nsf[afi][safi] = 0;
909 SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
912 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
913 if (peer->t_gr_stale)
915 BGP_TIMER_OFF (peer->t_gr_stale);
916 if (BGP_DEBUG (events, EVENTS))
917 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
921 if (peer->t_gr_restart)
923 BGP_TIMER_OFF (peer->t_gr_restart);
924 if (BGP_DEBUG (events, EVENTS))
925 zlog_debug ("%s graceful restart timer stopped", peer->host);
929 bgpTrapEstablished (peer);
930 #endif /* HAVE_SNMP */
932 /* Reset uptime, send keepalive, send current table. */
933 peer->uptime = bgp_clock ();
935 /* Send route-refresh when ORF is enabled */
936 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
937 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
938 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
940 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
941 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
942 REFRESH_IMMEDIATE, 0);
943 else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
944 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
945 REFRESH_IMMEDIATE, 0);
948 if (peer->v_keepalive)
949 bgp_keepalive_send (peer);
951 /* First update is deferred until ORF or ROUTE-REFRESH is received */
952 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
953 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
954 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
955 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
956 || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
957 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
959 bgp_announce_route_all (peer);
961 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1);
966 /* Keepalive packet is received. */
968 bgp_fsm_keepalive (struct peer *peer)
970 /* peer count update */
971 peer->keepalive_in++;
973 BGP_TIMER_OFF (peer->t_holdtime);
977 /* Update packet is received. */
979 bgp_fsm_update (struct peer *peer)
981 BGP_TIMER_OFF (peer->t_holdtime);
985 /* This is empty event. */
987 bgp_ignore (struct peer *peer)
989 if (BGP_DEBUG (fsm, FSM))
990 zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
994 /* Finite State Machine structure */
995 static const struct {
996 int (*func) (struct peer *);
998 } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
1001 /* Idle state: In Idle state, all events other than BGP_Start is
1002 ignored. With BGP_Start event, finite state machine calls
1004 {bgp_start, Connect}, /* BGP_Start */
1005 {bgp_stop, Idle}, /* BGP_Stop */
1006 {bgp_stop, Idle}, /* TCP_connection_open */
1007 {bgp_stop, Idle}, /* TCP_connection_closed */
1008 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
1009 {bgp_stop, Idle}, /* TCP_fatal_error */
1010 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1011 {bgp_ignore, Idle}, /* Hold_Timer_expired */
1012 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1013 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1014 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
1015 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1016 {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
1017 {bgp_ignore, Idle}, /* Clearing_Completed */
1018 {bgp_ignore, Idle}, /* BGP_Stop_with_error */
1022 {bgp_ignore, Connect}, /* BGP_Start */
1023 {bgp_stop, Idle}, /* BGP_Stop */
1024 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
1025 {bgp_stop, Idle}, /* TCP_connection_closed */
1026 {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
1027 {bgp_connect_fail, Idle}, /* TCP_fatal_error */
1028 {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
1029 {bgp_ignore, Idle}, /* Hold_Timer_expired */
1030 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1031 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1032 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
1033 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1034 {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
1035 {bgp_ignore, Idle}, /* Clearing_Completed */
1036 {bgp_stop_with_error, Idle},/* BGP_Stop_with_error */
1040 {bgp_ignore, Active}, /* BGP_Start */
1041 {bgp_stop, Idle}, /* BGP_Stop */
1042 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
1043 {bgp_stop, Idle}, /* TCP_connection_closed */
1044 {bgp_ignore, Active}, /* TCP_connection_open_failed */
1045 {bgp_ignore, Idle}, /* TCP_fatal_error */
1046 {bgp_start, Connect}, /* ConnectRetry_timer_expired */
1047 {bgp_ignore, Idle}, /* Hold_Timer_expired */
1048 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1049 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1050 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
1051 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1052 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1053 {bgp_ignore, Idle}, /* Clearing_Completed */
1054 {bgp_stop_with_error, Idle},/* BGP_Stop_with_error */
1058 {bgp_ignore, OpenSent}, /* BGP_Start */
1059 {bgp_stop, Idle}, /* BGP_Stop */
1060 {bgp_stop, Active}, /* TCP_connection_open */
1061 {bgp_stop, Active}, /* TCP_connection_closed */
1062 {bgp_stop, Active}, /* TCP_connection_open_failed */
1063 {bgp_stop, Active}, /* TCP_fatal_error */
1064 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1065 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1066 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1067 {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
1068 {bgp_fsm_event_error, Idle}, /* Receive_KEEPALIVE_message */
1069 {bgp_fsm_event_error, Idle}, /* Receive_UPDATE_message */
1070 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1071 {bgp_ignore, Idle}, /* Clearing_Completed */
1072 {bgp_stop_with_error, Idle},/* BGP_Stop_with_error */
1076 {bgp_ignore, OpenConfirm}, /* BGP_Start */
1077 {bgp_stop, Idle}, /* BGP_Stop */
1078 {bgp_stop, Idle}, /* TCP_connection_open */
1079 {bgp_stop, Idle}, /* TCP_connection_closed */
1080 {bgp_stop, Idle}, /* TCP_connection_open_failed */
1081 {bgp_stop, Idle}, /* TCP_fatal_error */
1082 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1083 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1084 {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
1085 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1086 {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
1087 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1088 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1089 {bgp_ignore, Idle}, /* Clearing_Completed */
1090 {bgp_stop_with_error, Idle},/* BGP_Stop_with_error */
1094 {bgp_ignore, Established}, /* BGP_Start */
1095 {bgp_stop, Clearing}, /* BGP_Stop */
1096 {bgp_stop, Clearing}, /* TCP_connection_open */
1097 {bgp_stop, Clearing}, /* TCP_connection_closed */
1098 {bgp_stop, Clearing}, /* TCP_connection_open_failed */
1099 {bgp_stop, Clearing}, /* TCP_fatal_error */
1100 {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
1101 {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */
1102 {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
1103 {bgp_stop, Clearing}, /* Receive_OPEN_message */
1104 {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
1105 {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
1106 {bgp_stop_with_error, Clearing}, /* Receive_NOTIFICATION_message */
1107 {bgp_ignore, Idle}, /* Clearing_Completed */
1108 {bgp_stop_with_error, Clearing}, /* BGP_Stop_with_error */
1112 {bgp_ignore, Clearing}, /* BGP_Start */
1113 {bgp_stop, Clearing}, /* BGP_Stop */
1114 {bgp_stop, Clearing}, /* TCP_connection_open */
1115 {bgp_stop, Clearing}, /* TCP_connection_closed */
1116 {bgp_stop, Clearing}, /* TCP_connection_open_failed */
1117 {bgp_stop, Clearing}, /* TCP_fatal_error */
1118 {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
1119 {bgp_stop, Clearing}, /* Hold_Timer_expired */
1120 {bgp_stop, Clearing}, /* KeepAlive_timer_expired */
1121 {bgp_stop, Clearing}, /* Receive_OPEN_message */
1122 {bgp_stop, Clearing}, /* Receive_KEEPALIVE_message */
1123 {bgp_stop, Clearing}, /* Receive_UPDATE_message */
1124 {bgp_stop, Clearing}, /* Receive_NOTIFICATION_message */
1125 {bgp_clearing_completed, Idle}, /* Clearing_Completed */
1126 {bgp_stop_with_error, Clearing}, /* BGP_Stop_with_error */
1130 {bgp_ignore, Deleted}, /* BGP_Start */
1131 {bgp_ignore, Deleted}, /* BGP_Stop */
1132 {bgp_ignore, Deleted}, /* TCP_connection_open */
1133 {bgp_ignore, Deleted}, /* TCP_connection_closed */
1134 {bgp_ignore, Deleted}, /* TCP_connection_open_failed */
1135 {bgp_ignore, Deleted}, /* TCP_fatal_error */
1136 {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */
1137 {bgp_ignore, Deleted}, /* Hold_Timer_expired */
1138 {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */
1139 {bgp_ignore, Deleted}, /* Receive_OPEN_message */
1140 {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */
1141 {bgp_ignore, Deleted}, /* Receive_UPDATE_message */
1142 {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */
1143 {bgp_ignore, Deleted}, /* Clearing_Completed */
1144 {bgp_ignore, Deleted}, /* BGP_Stop_with_error */
1148 static const char *bgp_event_str[] =
1153 "TCP_connection_open",
1154 "TCP_connection_closed",
1155 "TCP_connection_open_failed",
1157 "ConnectRetry_timer_expired",
1158 "Hold_Timer_expired",
1159 "KeepAlive_timer_expired",
1160 "Receive_OPEN_message",
1161 "Receive_KEEPALIVE_message",
1162 "Receive_UPDATE_message",
1163 "Receive_NOTIFICATION_message",
1164 "Clearing_Completed",
1165 "BGP_Stop_with_error",
1168 /* Execute event process. */
1170 bgp_event (struct thread *thread)
1177 peer = THREAD_ARG (thread);
1178 event = THREAD_VAL (thread);
1180 /* Logging this event. */
1181 next = FSM [peer->status -1][event - 1].next_state;
1183 if (BGP_DEBUG (fsm, FSM) && peer->status != next)
1184 plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
1185 bgp_event_str[event],
1186 LOOKUP (bgp_status_msg, peer->status),
1187 LOOKUP (bgp_status_msg, next));
1189 /* Call function. */
1190 if (FSM [peer->status -1][event - 1].func)
1191 ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
1193 /* When function do not want proceed next job return -1. */
1196 /* If status is changed. */
1197 if (next != peer->status)
1198 bgp_fsm_change_status (peer, next);
1200 /* Make sure timer is set. */
1201 bgp_timer_set (peer);