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"
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_attr.h"
36 #include "bgpd/bgp_debug.h"
37 #include "bgpd/bgp_fsm.h"
38 #include "bgpd/bgp_packet.h"
39 #include "bgpd/bgp_network.h"
40 #include "bgpd/bgp_route.h"
41 #include "bgpd/bgp_dump.h"
42 #include "bgpd/bgp_open.h"
44 #include "bgpd/bgp_snmp.h"
45 #endif /* HAVE_SNMP */
47 /* BGP FSM (finite state machine) has three types of functions. Type
48 one is thread functions. Type two is event functions. Type three
49 is FSM functions. Timer functions are set by bgp_timer_set
52 /* BGP event function. */
53 int bgp_event (struct thread
*);
55 /* BGP thread functions. */
56 static int bgp_start_timer (struct thread
*);
57 static int bgp_connect_timer (struct thread
*);
58 static int bgp_holdtime_timer (struct thread
*);
59 static int bgp_keepalive_timer (struct thread
*);
61 /* BGP FSM functions. */
62 static int bgp_start (struct peer
*);
64 /* BGP start timer jitter. */
66 bgp_start_jitter (int time
)
68 return ((rand () % (time
+ 1)) - (time
/ 2));
71 /* Check if suppress start/restart of sessions to peer. */
72 #define BGP_PEER_START_SUPPRESSED(P) \
73 (CHECK_FLAG ((P)->flags, PEER_FLAG_SHUTDOWN) \
74 || CHECK_FLAG ((P)->sflags, PEER_STATUS_PREFIX_OVERFLOW))
76 /* Hook function called after bgp event is occered. And vty's
77 neighbor command invoke this function after making neighbor
80 bgp_timer_set (struct peer
*peer
)
87 /* First entry point of peer's finite state machine. In Idle
88 status start timer is on unless peer is shutdown or peer is
89 inactive. All other timer must be turned off */
90 if (BGP_PEER_START_SUPPRESSED (peer
) || ! peer_active (peer
))
92 BGP_TIMER_OFF (peer
->t_start
);
96 jitter
= bgp_start_jitter (peer
->v_start
);
97 BGP_TIMER_ON (peer
->t_start
, bgp_start_timer
,
98 peer
->v_start
+ jitter
);
100 BGP_TIMER_OFF (peer
->t_connect
);
101 BGP_TIMER_OFF (peer
->t_holdtime
);
102 BGP_TIMER_OFF (peer
->t_keepalive
);
103 BGP_TIMER_OFF (peer
->t_asorig
);
104 BGP_TIMER_OFF (peer
->t_routeadv
);
108 /* After start timer is expired, the peer moves to Connnect
109 status. Make sure start timer is off and connect timer is
111 BGP_TIMER_OFF (peer
->t_start
);
112 BGP_TIMER_ON (peer
->t_connect
, bgp_connect_timer
, peer
->v_connect
);
113 BGP_TIMER_OFF (peer
->t_holdtime
);
114 BGP_TIMER_OFF (peer
->t_keepalive
);
115 BGP_TIMER_OFF (peer
->t_asorig
);
116 BGP_TIMER_OFF (peer
->t_routeadv
);
120 /* Active is waiting connection from remote peer. And if
121 connect timer is expired, change status to Connect. */
122 BGP_TIMER_OFF (peer
->t_start
);
123 /* If peer is passive mode, do not set connect timer. */
124 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_PASSIVE
)
125 || CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
))
127 BGP_TIMER_OFF (peer
->t_connect
);
131 BGP_TIMER_ON (peer
->t_connect
, bgp_connect_timer
, peer
->v_connect
);
133 BGP_TIMER_OFF (peer
->t_holdtime
);
134 BGP_TIMER_OFF (peer
->t_keepalive
);
135 BGP_TIMER_OFF (peer
->t_asorig
);
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_asorig
);
154 BGP_TIMER_OFF (peer
->t_routeadv
);
158 /* OpenConfirm status. */
159 BGP_TIMER_OFF (peer
->t_start
);
160 BGP_TIMER_OFF (peer
->t_connect
);
162 /* If the negotiated Hold Time value is zero, then the Hold Time
163 timer and KeepAlive timers are not started. */
164 if (peer
->v_holdtime
== 0)
166 BGP_TIMER_OFF (peer
->t_holdtime
);
167 BGP_TIMER_OFF (peer
->t_keepalive
);
171 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
173 BGP_TIMER_ON (peer
->t_keepalive
, bgp_keepalive_timer
,
176 BGP_TIMER_OFF (peer
->t_asorig
);
177 BGP_TIMER_OFF (peer
->t_routeadv
);
181 /* In Established status start and connect timer is turned
183 BGP_TIMER_OFF (peer
->t_start
);
184 BGP_TIMER_OFF (peer
->t_connect
);
186 /* Same as OpenConfirm, if holdtime is zero then both holdtime
187 and keepalive must be turned off. */
188 if (peer
->v_holdtime
== 0)
190 BGP_TIMER_OFF (peer
->t_holdtime
);
191 BGP_TIMER_OFF (peer
->t_keepalive
);
195 BGP_TIMER_ON (peer
->t_holdtime
, bgp_holdtime_timer
,
197 BGP_TIMER_ON (peer
->t_keepalive
, bgp_keepalive_timer
,
200 BGP_TIMER_OFF (peer
->t_asorig
);
203 BGP_TIMER_OFF (peer
->t_gr_restart
);
204 BGP_TIMER_OFF (peer
->t_gr_stale
);
205 BGP_TIMER_OFF (peer
->t_pmax_restart
);
207 BGP_TIMER_OFF (peer
->t_start
);
208 BGP_TIMER_OFF (peer
->t_connect
);
209 BGP_TIMER_OFF (peer
->t_holdtime
);
210 BGP_TIMER_OFF (peer
->t_keepalive
);
211 BGP_TIMER_OFF (peer
->t_asorig
);
212 BGP_TIMER_OFF (peer
->t_routeadv
);
216 /* BGP start timer. This function set BGP_Start event to thread value
217 and process event. */
219 bgp_start_timer (struct thread
*thread
)
223 peer
= THREAD_ARG (thread
);
224 peer
->t_start
= NULL
;
226 if (BGP_DEBUG (fsm
, FSM
))
227 zlog (peer
->log
, LOG_DEBUG
,
228 "%s [FSM] Timer (start timer expire).", peer
->host
);
230 THREAD_VAL (thread
) = BGP_Start
;
231 bgp_event (thread
); /* bgp_event unlocks peer */
236 /* BGP connect retry timer. */
238 bgp_connect_timer (struct thread
*thread
)
242 peer
= THREAD_ARG (thread
);
243 peer
->t_connect
= NULL
;
245 if (BGP_DEBUG (fsm
, FSM
))
246 zlog (peer
->log
, LOG_DEBUG
, "%s [FSM] Timer (connect timer expire)",
249 THREAD_VAL (thread
) = ConnectRetry_timer_expired
;
250 bgp_event (thread
); /* bgp_event unlocks peer */
255 /* BGP holdtime timer. */
257 bgp_holdtime_timer (struct thread
*thread
)
261 peer
= THREAD_ARG (thread
);
262 peer
->t_holdtime
= NULL
;
264 if (BGP_DEBUG (fsm
, FSM
))
265 zlog (peer
->log
, LOG_DEBUG
,
266 "%s [FSM] Timer (holdtime timer expire)",
269 THREAD_VAL (thread
) = Hold_Timer_expired
;
270 bgp_event (thread
); /* bgp_event unlocks peer */
275 /* BGP keepalive fire ! */
277 bgp_keepalive_timer (struct thread
*thread
)
281 peer
= THREAD_ARG (thread
);
282 peer
->t_keepalive
= NULL
;
284 if (BGP_DEBUG (fsm
, FSM
))
285 zlog (peer
->log
, LOG_DEBUG
,
286 "%s [FSM] Timer (keepalive timer expire)",
289 THREAD_VAL (thread
) = KeepAlive_timer_expired
;
290 bgp_event (thread
); /* bgp_event unlocks peer */
296 bgp_routeadv_timer (struct thread
*thread
)
300 peer
= THREAD_ARG (thread
);
301 peer
->t_routeadv
= NULL
;
303 if (BGP_DEBUG (fsm
, FSM
))
304 zlog (peer
->log
, LOG_DEBUG
,
305 "%s [FSM] Timer (routeadv timer expire)",
308 peer
->synctime
= time (NULL
);
310 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
312 BGP_TIMER_ON (peer
->t_routeadv
, bgp_routeadv_timer
,
318 /* Reset bgp update timer */
320 bgp_uptime_reset (struct peer
*peer
)
322 peer
->uptime
= time (NULL
);
325 /* BGP Peer Down Cause */
326 const char *peer_down_str
[] =
332 "Cluster ID changed",
333 "Confederation identifier changed",
334 "Confederation peer changed",
335 "RR client config change",
336 "RS client config change",
337 "Update source change",
338 "Address family activated",
341 "BGP Notification received",
342 "BGP Notification send",
343 "Peer closed the session",
345 "Peer-group add member",
346 "Peer-group delete member",
347 "Capability changed",
348 "Passive config change",
349 "Multihop config change",
350 "NSF peer closed the session"
354 bgp_graceful_restart_timer_expire (struct thread
*thread
)
360 peer
= THREAD_ARG (thread
);
361 peer
->t_gr_restart
= NULL
;
363 /* NSF delete stale route */
364 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
365 for (safi
= SAFI_UNICAST
; safi
< SAFI_UNICAST_MULTICAST
; safi
++)
366 if (peer
->nsf
[afi
][safi
])
367 bgp_clear_stale_route (peer
, afi
, safi
);
369 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
370 BGP_TIMER_OFF (peer
->t_gr_stale
);
372 if (BGP_DEBUG (events
, EVENTS
))
374 zlog_debug ("%s graceful restart timer expired", peer
->host
);
375 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
378 bgp_timer_set (peer
);
384 bgp_graceful_stale_timer_expire (struct thread
*thread
)
390 peer
= THREAD_ARG (thread
);
391 peer
->t_gr_stale
= NULL
;
393 if (BGP_DEBUG (events
, EVENTS
))
394 zlog_debug ("%s graceful restart stalepath timer expired", peer
->host
);
396 /* NSF delete stale route */
397 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
398 for (safi
= SAFI_UNICAST
; safi
< SAFI_UNICAST_MULTICAST
; safi
++)
399 if (peer
->nsf
[afi
][safi
])
400 bgp_clear_stale_route (peer
, afi
, safi
);
405 /* Called after event occured, this function change status and reset
406 read/write and timer thread. */
408 bgp_fsm_change_status (struct peer
*peer
, int status
)
410 bgp_dump_state (peer
, peer
->status
, status
);
412 /* Transition into Clearing or Deleted must /always/ clear all routes..
413 * (and must do so before actually changing into Deleted..
415 if (status
>= Clearing
)
416 bgp_clear_route_all (peer
);
418 /* Preserve old status and change into new status. */
419 peer
->ostatus
= peer
->status
;
420 peer
->status
= status
;
422 if (BGP_DEBUG (normal
, NORMAL
))
423 zlog_debug ("%s went from %s to %s",
425 LOOKUP (bgp_status_msg
, peer
->ostatus
),
426 LOOKUP (bgp_status_msg
, peer
->status
));
429 /* Flush the event queue and ensure the peer is shut down */
431 bgp_clearing_completed (struct peer
*peer
)
433 int rc
= bgp_stop(peer
);
434 BGP_EVENT_FLUSH (peer
);
439 /* Administrative BGP peer stop event. */
440 /* May be called multiple times for the same peer */
442 bgp_stop (struct peer
*peer
)
446 char orf_name
[BUFSIZ
];
448 /* Can't do this in Clearing; events are used for state transitions */
449 if (peer
->status
!= Clearing
)
451 /* Delete all existing events of the peer */
452 BGP_EVENT_FLUSH (peer
);
455 /* Increment Dropped count. */
456 if (peer
->status
== Established
)
460 /* bgp log-neighbor-changes of neighbor Down */
461 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
))
462 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer
->host
,
463 peer_down_str
[(int) peer
->last_reset
]);
465 /* graceful restart */
466 if (peer
->t_gr_stale
)
468 BGP_TIMER_OFF (peer
->t_gr_stale
);
469 if (BGP_DEBUG (events
, EVENTS
))
470 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
472 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
))
474 if (BGP_DEBUG (events
, EVENTS
))
476 zlog_debug ("%s graceful restart timer started for %d sec",
477 peer
->host
, peer
->v_gr_restart
);
478 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
479 peer
->host
, peer
->bgp
->stalepath_time
);
481 BGP_TIMER_ON (peer
->t_gr_restart
, bgp_graceful_restart_timer_expire
,
483 BGP_TIMER_ON (peer
->t_gr_stale
, bgp_graceful_stale_timer_expire
,
484 peer
->bgp
->stalepath_time
);
488 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
490 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
491 for (safi
= SAFI_UNICAST
; safi
< SAFI_UNICAST_MULTICAST
; safi
++)
492 peer
->nsf
[afi
][safi
] = 0;
495 /* set last reset time */
496 peer
->resettime
= time (NULL
);
498 bgp_uptime_reset (peer
);
501 bgpTrapBackwardTransition (peer
);
502 #endif /* HAVE_SNMP */
505 bgp_uptime_reset (peer
);
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_asorig
);
521 BGP_TIMER_OFF (peer
->t_routeadv
);
524 peer
->packet_size
= 0;
526 /* Clear input and output buffer. */
528 stream_reset (peer
->ibuf
);
530 stream_reset (peer
->work
);
532 stream_fifo_clean (peer
->obuf
);
534 /* Close of file descriptor. */
541 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
542 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
544 /* Reset all negotiated variables */
545 peer
->afc_nego
[afi
][safi
] = 0;
546 peer
->afc_adv
[afi
][safi
] = 0;
547 peer
->afc_recv
[afi
][safi
] = 0;
549 /* peer address family capability flags*/
550 peer
->af_cap
[afi
][safi
] = 0;
552 /* peer address family status flags*/
553 peer
->af_sflags
[afi
][safi
] = 0;
555 /* Received ORF prefix-filter */
556 peer
->orf_plist
[afi
][safi
] = NULL
;
558 /* ORF received prefix-filter pnt */
559 sprintf (orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
560 prefix_bgp_orf_remove_all (orf_name
);
563 /* Reset keepalive and holdtime */
564 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
566 peer
->v_keepalive
= peer
->keepalive
;
567 peer
->v_holdtime
= peer
->holdtime
;
571 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
572 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
575 peer
->update_time
= 0;
577 /* Until we are sure that there is no problem about prefix count
578 this should be commented out.*/
580 /* Reset prefix count */
581 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
582 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
583 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
584 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
585 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
591 /* BGP peer is stoped by the error. */
593 bgp_stop_with_error (struct peer
*peer
)
595 /* Double start timer. */
598 /* Overflow check. */
599 if (peer
->v_start
>= (60 * 2))
600 peer
->v_start
= (60 * 2);
607 /* TCP connection open. Next we send open message to remote peer. And
608 add read thread for reading open message. */
610 bgp_connect_success (struct peer
*peer
)
616 zlog_err ("bgp_connect_success peer's fd is negative value %d",
620 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
622 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
623 bgp_getsockname (peer
);
625 if (BGP_DEBUG (normal
, NORMAL
))
627 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
628 zlog_debug ("%s open active, local address %s", peer
->host
,
629 sockunion2str (peer
->su_local
, buf1
, SU_ADDRSTRLEN
));
631 zlog_debug ("%s passive open", peer
->host
);
634 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
635 bgp_open_send (peer
);
640 /* TCP connect fail */
642 bgp_connect_fail (struct peer
*peer
)
648 /* This function is the first starting point of all BGP connection. It
649 try to connect to remote peer with non-blocking IO. */
651 bgp_start (struct peer
*peer
)
655 if (BGP_PEER_START_SUPPRESSED (peer
))
657 if (BGP_DEBUG (fsm
, FSM
))
658 plog_err (peer
->log
, "%s [FSM] Trying to start suppressed peer"
659 " - this is never supposed to happen!", peer
->host
);
663 /* Scrub some information that might be left over from a previous,
666 /* Connection information. */
669 sockunion_free (peer
->su_local
);
670 peer
->su_local
= NULL
;
675 sockunion_free (peer
->su_remote
);
676 peer
->su_remote
= NULL
;
679 /* Clear remote router-id. */
680 peer
->remote_id
.s_addr
= 0;
682 /* Clear peer capability flag. */
685 /* If the peer is passive mode, force to move to Active mode. */
686 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_PASSIVE
))
688 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
692 status
= bgp_connect (peer
);
697 if (BGP_DEBUG (fsm
, FSM
))
698 plog_debug (peer
->log
, "%s [FSM] Connect error", peer
->host
);
699 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
701 case connect_success
:
702 if (BGP_DEBUG (fsm
, FSM
))
703 plog_debug (peer
->log
, "%s [FSM] Connect immediately success",
705 BGP_EVENT_ADD (peer
, TCP_connection_open
);
707 case connect_in_progress
:
708 /* To check nonblocking connect, we wait until socket is
709 readable or writable. */
710 if (BGP_DEBUG (fsm
, FSM
))
711 plog_debug (peer
->log
, "%s [FSM] Non blocking connect waiting result",
715 zlog_err ("bgp_start peer's fd is negative value %d",
719 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
720 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
726 /* Connect retry timer is expired when the peer status is Connect. */
728 bgp_reconnect (struct peer
*peer
)
736 bgp_fsm_open (struct peer
*peer
)
738 /* Send keepalive and make keepalive timer */
739 bgp_keepalive_send (peer
);
741 /* Reset holdtimer value. */
742 BGP_TIMER_OFF (peer
->t_holdtime
);
747 /* Keepalive send to peer. */
749 bgp_fsm_keepalive_expire (struct peer
*peer
)
751 bgp_keepalive_send (peer
);
755 /* Hold timer expire. This is error of BGP connection. So cut the
756 peer and change to Idle status. */
758 bgp_fsm_holdtime_expire (struct peer
*peer
)
760 if (BGP_DEBUG (fsm
, FSM
))
761 zlog (peer
->log
, LOG_DEBUG
, "%s [FSM] Hold timer expire", peer
->host
);
763 /* Send notify to remote peer. */
764 bgp_notify_send (peer
, BGP_NOTIFY_HOLD_ERR
, 0);
766 /* Sweep if it is temporary peer. */
767 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
769 zlog_info ("%s [Event] Accepting BGP peer is deleted", peer
->host
);
774 /* bgp_stop needs to be invoked while in Established state */
780 /* Status goes to Established. Send keepalive packet then make first
781 update information. */
783 bgp_establish (struct peer
*peer
)
785 struct bgp_notify
*notify
;
788 int nsf_af_count
= 0;
790 /* Reset capability open status flag. */
791 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
792 SET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
794 /* Clear last notification data. */
795 notify
= &peer
->notify
;
797 XFREE (MTYPE_TMP
, notify
->data
);
798 memset (notify
, 0, sizeof (struct bgp_notify
));
800 /* Clear start timer value to default. */
801 peer
->v_start
= BGP_INIT_START_TIMER
;
803 /* Increment established count. */
805 bgp_fsm_change_status (peer
, Established
);
807 /* bgp log-neighbor-changes of neighbor Up */
808 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
))
809 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer
->host
);
811 /* graceful restart */
812 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
813 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
814 for (safi
= SAFI_UNICAST
; safi
< SAFI_UNICAST_MULTICAST
; safi
++)
816 if (peer
->afc_nego
[afi
][safi
]
817 && CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_ADV
)
818 && CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_RCV
))
820 if (peer
->nsf
[afi
][safi
]
821 && ! CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_PRESERVE_RCV
))
822 bgp_clear_stale_route (peer
, afi
, safi
);
824 peer
->nsf
[afi
][safi
] = 1;
829 if (peer
->nsf
[afi
][safi
])
830 bgp_clear_stale_route (peer
, afi
, safi
);
831 peer
->nsf
[afi
][safi
] = 0;
836 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
839 UNSET_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
);
840 if (peer
->t_gr_stale
)
842 BGP_TIMER_OFF (peer
->t_gr_stale
);
843 if (BGP_DEBUG (events
, EVENTS
))
844 zlog_debug ("%s graceful restart stalepath timer stopped", peer
->host
);
848 if (peer
->t_gr_restart
)
850 BGP_TIMER_OFF (peer
->t_gr_restart
);
851 if (BGP_DEBUG (events
, EVENTS
))
852 zlog_debug ("%s graceful restart timer stopped", peer
->host
);
856 bgpTrapEstablished (peer
);
857 #endif /* HAVE_SNMP */
859 /* Reset uptime, send keepalive, send current table. */
860 bgp_uptime_reset (peer
);
862 /* Send route-refresh when ORF is enabled */
863 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
864 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
865 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_ADV
))
867 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_RCV
))
868 bgp_route_refresh_send (peer
, afi
, safi
, ORF_TYPE_PREFIX
,
869 REFRESH_IMMEDIATE
, 0);
870 else if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
871 bgp_route_refresh_send (peer
, afi
, safi
, ORF_TYPE_PREFIX_OLD
,
872 REFRESH_IMMEDIATE
, 0);
875 if (peer
->v_keepalive
)
876 bgp_keepalive_send (peer
);
878 /* First update is deferred until ORF or ROUTE-REFRESH is received */
879 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
880 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
881 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_ADV
))
882 if (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
883 || CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
884 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
886 bgp_announce_route_all (peer
);
888 BGP_TIMER_ON (peer
->t_routeadv
, bgp_routeadv_timer
, 1);
893 /* Keepalive packet is received. */
895 bgp_fsm_keepalive (struct peer
*peer
)
897 /* peer count update */
898 peer
->keepalive_in
++;
900 BGP_TIMER_OFF (peer
->t_holdtime
);
904 /* Update packet is received. */
906 bgp_fsm_update (struct peer
*peer
)
908 BGP_TIMER_OFF (peer
->t_holdtime
);
912 /* This is empty event. */
914 bgp_ignore (struct peer
*peer
)
916 if (BGP_DEBUG (fsm
, FSM
))
917 zlog (peer
->log
, LOG_DEBUG
, "%s [FSM] bgp_ignore called", peer
->host
);
921 /* Finite State Machine structure */
922 static const struct {
923 int (*func
) (struct peer
*);
925 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] =
928 /* Idle state: In Idle state, all events other than BGP_Start is
929 ignored. With BGP_Start event, finite state machine calls
931 {bgp_start
, Connect
}, /* BGP_Start */
932 {bgp_stop
, Idle
}, /* BGP_Stop */
933 {bgp_stop
, Idle
}, /* TCP_connection_open */
934 {bgp_stop
, Idle
}, /* TCP_connection_closed */
935 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
936 {bgp_stop
, Idle
}, /* TCP_fatal_error */
937 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
938 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
939 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
940 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
941 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
942 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
943 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
944 {bgp_ignore
, Idle
}, /* Clearing_Completed */
948 {bgp_ignore
, Connect
}, /* BGP_Start */
949 {bgp_stop
, Idle
}, /* BGP_Stop */
950 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
951 {bgp_stop
, Idle
}, /* TCP_connection_closed */
952 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
953 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
954 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
955 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
956 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
957 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
958 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
959 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
960 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
961 {bgp_ignore
, Idle
}, /* Clearing_Completed */
965 {bgp_ignore
, Active
}, /* BGP_Start */
966 {bgp_stop
, Idle
}, /* BGP_Stop */
967 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
968 {bgp_stop
, Idle
}, /* TCP_connection_closed */
969 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
970 {bgp_ignore
, Idle
}, /* TCP_fatal_error */
971 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
972 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
973 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
974 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
975 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
976 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
977 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
978 {bgp_ignore
, Idle
}, /* Clearing_Completed */
982 {bgp_ignore
, OpenSent
}, /* BGP_Start */
983 {bgp_stop
, Idle
}, /* BGP_Stop */
984 {bgp_stop
, Active
}, /* TCP_connection_open */
985 {bgp_stop
, Active
}, /* TCP_connection_closed */
986 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
987 {bgp_stop
, Active
}, /* TCP_fatal_error */
988 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
989 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
990 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
991 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
992 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
993 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
994 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
995 {bgp_ignore
, Idle
}, /* Clearing_Completed */
999 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1000 {bgp_stop
, Idle
}, /* BGP_Stop */
1001 {bgp_stop
, Idle
}, /* TCP_connection_open */
1002 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1003 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1004 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1005 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1006 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1007 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1008 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1009 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1010 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1011 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1012 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1016 {bgp_ignore
, Established
}, /* BGP_Start */
1017 {bgp_stop
, Clearing
}, /* BGP_Stop */
1018 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1019 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1020 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1021 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1022 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1023 {bgp_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1024 {bgp_fsm_keepalive_expire
, Established
}, /* KeepAlive_timer_expired */
1025 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1026 {bgp_fsm_keepalive
, Established
}, /* Receive_KEEPALIVE_message */
1027 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1028 {bgp_stop_with_error
, Clearing
}, /* Receive_NOTIFICATION_message */
1029 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1033 {bgp_ignore
, Clearing
}, /* BGP_Start */
1034 {bgp_stop
, Clearing
}, /* BGP_Stop */
1035 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1036 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1037 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1038 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1039 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1040 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1041 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1042 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1043 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1044 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1045 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1046 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1050 {bgp_ignore
, Deleted
}, /* BGP_Start */
1051 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1052 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1053 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1054 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1055 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1056 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1057 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1058 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1059 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1060 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1061 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1062 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1063 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1067 static const char *bgp_event_str
[] =
1072 "TCP_connection_open",
1073 "TCP_connection_closed",
1074 "TCP_connection_open_failed",
1076 "ConnectRetry_timer_expired",
1077 "Hold_Timer_expired",
1078 "KeepAlive_timer_expired",
1079 "Receive_OPEN_message",
1080 "Receive_KEEPALIVE_message",
1081 "Receive_UPDATE_message",
1082 "Receive_NOTIFICATION_message",
1083 "Clearing_Completed",
1086 /* Execute event process. */
1088 bgp_event (struct thread
*thread
)
1095 peer
= THREAD_ARG (thread
);
1096 event
= THREAD_VAL (thread
);
1098 /* Logging this event. */
1099 next
= FSM
[peer
->status
-1][event
- 1].next_state
;
1101 if (BGP_DEBUG (fsm
, FSM
) && peer
->status
!= next
)
1102 plog_debug (peer
->log
, "%s [FSM] %s (%s->%s)", peer
->host
,
1103 bgp_event_str
[event
],
1104 LOOKUP (bgp_status_msg
, peer
->status
),
1105 LOOKUP (bgp_status_msg
, next
));
1107 /* Call function. */
1108 if (FSM
[peer
->status
-1][event
- 1].func
)
1109 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
1111 /* When function do not want proceed next job return -1. */
1114 /* If status is changed. */
1115 if (next
!= peer
->status
)
1116 bgp_fsm_change_status (peer
, next
);
1118 /* Make sure timer is set. */
1119 bgp_timer_set (peer
);