usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / zebra / bgpd / bgp_fsm.c
blob83da82a26beb905df2a9928d3d327ca015eaa88b
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
10 later version.
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
20 02111-1307, USA. */
22 #include <zebra.h>
24 #include "linklist.h"
25 #include "prefix.h"
26 #include "vty.h"
27 #include "sockunion.h"
28 #include "thread.h"
29 #include "log.h"
30 #include "stream.h"
31 #include "memory.h"
32 #include "plist.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"
43 #ifdef HAVE_SNMP
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
50 function. */
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 active delay jitter. */
65 int
66 bgp_active_delay_jitter (int time)
68 return ((rand () % (time + 1)) - (time / 2));
71 /* Hook function called after bgp event is occered. And vty's
72 neighbor command invoke this function after making neighbor
73 structure. */
74 void
75 bgp_timer_set (struct peer *peer)
77 afi_t afi;
78 safi_t safi;
79 int active_delay = 0;
81 switch (peer->status)
83 case Idle:
84 /* First entry point of peer's finite state machine. In Idle
85 status start timer is on unless peer is shutdown or peer is
86 inactive. All other timer must be turned off */
87 if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN)
88 || CHECK_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW)
89 || ! peer_active (peer))
91 BGP_TIMER_OFF (peer->t_start);
93 else
95 if (CHECK_FLAG (peer->sflags, PEER_STATUS_CREATE_INIT))
97 BGP_TIMER_ON (peer->t_start, bgp_start_timer, BGP_PEER_FIRST_CREATE_TIMER);
99 else
101 BGP_TIMER_ON (peer->t_start, bgp_start_timer, peer->v_start);
104 BGP_TIMER_OFF (peer->t_connect);
105 BGP_TIMER_OFF (peer->t_holdtime);
106 BGP_TIMER_OFF (peer->t_keepalive);
107 BGP_TIMER_OFF (peer->t_asorig);
108 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
109 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
110 BGP_TIMER_OFF (peer->t_routeadv[afi][safi]);
111 break;
113 case Connect:
114 /* After start timer is expired, the peer moves to Connnect
115 status. Make sure start timer is off and connect timer is
116 on. */
117 BGP_TIMER_OFF (peer->t_start);
118 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
119 BGP_TIMER_OFF (peer->t_holdtime);
120 BGP_TIMER_OFF (peer->t_keepalive);
121 BGP_TIMER_OFF (peer->t_asorig);
122 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
123 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
124 BGP_TIMER_OFF (peer->t_routeadv[afi][safi]);
125 break;
127 case Active:
128 /* Active is waiting connection from remote peer. And if
129 connect timer is expired, change status to Connect. */
130 BGP_TIMER_OFF (peer->t_start);
131 /* If peer is passive mode, do not set connect timer. */
132 if (CHECK_FLAG (peer->flags, PEER_FLAG_CONNECT_MODE_PASSIVE))
134 if (BGP_DEBUG (normal, NORMAL))
135 zlog_info ("%s active open failed - TCP session must be opened passively", peer->host);
136 BGP_TIMER_OFF (peer->t_connect);
138 else
140 if (peer->ostatus == Idle
141 && ! CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
143 active_delay = peer->v_active_delay;
144 active_delay += bgp_active_delay_jitter (BGP_ACTIVE_DELAY_TIMER);
146 if (BGP_DEBUG (normal, NORMAL))
147 zlog_info ("%s open active, delay %d sec", peer->host, active_delay);
148 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, active_delay);
150 else
152 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer,
153 peer->v_connect);
156 BGP_TIMER_OFF (peer->t_holdtime);
157 BGP_TIMER_OFF (peer->t_keepalive);
158 BGP_TIMER_OFF (peer->t_asorig);
159 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
160 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
161 BGP_TIMER_OFF (peer->t_routeadv[afi][safi]);
162 break;
164 case OpenSent:
165 /* OpenSent status. */
166 BGP_TIMER_OFF (peer->t_start);
167 BGP_TIMER_OFF (peer->t_connect);
168 if (peer->v_holdtime != 0)
170 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
171 peer->v_holdtime);
173 else
175 BGP_TIMER_OFF (peer->t_holdtime);
177 BGP_TIMER_OFF (peer->t_keepalive);
178 BGP_TIMER_OFF (peer->t_asorig);
179 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
180 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
181 BGP_TIMER_OFF (peer->t_routeadv[afi][safi]);
182 break;
184 case OpenConfirm:
185 /* OpenConfirm status. */
186 BGP_TIMER_OFF (peer->t_start);
187 BGP_TIMER_OFF (peer->t_connect);
189 /* If the negotiated Hold Time value is zero, then the Hold Time
190 timer and KeepAlive timers are not started. */
191 if (peer->v_holdtime == 0)
193 BGP_TIMER_OFF (peer->t_holdtime);
194 BGP_TIMER_OFF (peer->t_keepalive);
196 else
198 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
199 peer->v_holdtime);
200 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
201 peer->v_keepalive);
203 BGP_TIMER_OFF (peer->t_asorig);
204 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
205 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
206 BGP_TIMER_OFF (peer->t_routeadv[afi][safi]);
207 break;
209 case Established:
210 /* In Established status start and connect timer is turned
211 off. */
212 BGP_TIMER_OFF (peer->t_start);
213 BGP_TIMER_OFF (peer->t_connect);
215 /* Same as OpenConfirm, if holdtime is zero then both holdtime
216 and keepalive must be turned off. */
217 if (peer->v_holdtime == 0)
219 BGP_TIMER_OFF (peer->t_holdtime);
220 BGP_TIMER_OFF (peer->t_keepalive);
222 else
224 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
225 peer->v_holdtime);
226 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
227 peer->v_keepalive);
229 BGP_TIMER_OFF (peer->t_asorig);
230 break;
234 /* BGP start timer. This function set BGP_Start event to thread value
235 and process event. */
236 static int
237 bgp_start_timer (struct thread *thread)
239 struct peer *peer;
241 peer = THREAD_ARG (thread);
242 peer->t_start = NULL;
244 UNSET_FLAG (peer->sflags, PEER_STATUS_CREATE_INIT);
246 if (BGP_DEBUG (fsm, FSM))
247 zlog (peer->log, LOG_DEBUG,
248 "%s [FSM] Timer (start timer expire).", peer->host);
250 THREAD_VAL (thread) = BGP_Start;
251 bgp_event (thread);
253 return 0;
256 /* BGP connect retry timer. */
257 static int
258 bgp_connect_timer (struct thread *thread)
260 struct peer *peer;
262 peer = THREAD_ARG (thread);
263 peer->t_connect = NULL;
265 if (BGP_DEBUG (fsm, FSM))
266 zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
267 peer->host);
269 THREAD_VAL (thread) = ConnectRetry_timer_expired;
270 bgp_event (thread);
272 return 0;
275 /* BGP holdtime timer. */
276 static int
277 bgp_holdtime_timer (struct thread *thread)
279 struct peer *peer;
281 peer = THREAD_ARG (thread);
282 peer->t_holdtime = NULL;
284 if (BGP_DEBUG (fsm, FSM))
285 zlog (peer->log, LOG_DEBUG,
286 "%s [FSM] Timer (holdtime timer expire)",
287 peer->host);
289 THREAD_VAL (thread) = Hold_Timer_expired;
290 bgp_event (thread);
292 return 0;
295 /* BGP keepalive fire ! */
296 static int
297 bgp_keepalive_timer (struct thread *thread)
299 struct peer *peer;
301 peer = THREAD_ARG (thread);
302 peer->t_keepalive = NULL;
304 if (BGP_DEBUG (fsm, FSM))
305 zlog (peer->log, LOG_DEBUG,
306 "%s [FSM] Timer (keepalive timer expire)",
307 peer->host);
309 THREAD_VAL (thread) = KeepAlive_timer_expired;
310 bgp_event (thread);
312 return 0;
316 bgp_routeadv_timer_ipv4_unicast (struct thread *thread)
318 struct peer *peer;
320 peer = THREAD_ARG (thread);
321 peer->t_routeadv[AFI_IP][SAFI_UNICAST] = NULL;
323 if (BGP_DEBUG (events, EVENTS))
324 zlog_info ("%s routeadv timer expired for IPv4 Unicast", peer->host);
326 peer->synctime[AFI_IP][SAFI_UNICAST] = time (NULL);
328 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
330 BGP_TIMER_ON (peer->t_routeadv[AFI_IP][SAFI_UNICAST], bgp_routeadv_timer_ipv4_unicast,
331 peer->v_routeadv);
333 return 0;
337 bgp_routeadv_timer_ipv4_multicast (struct thread *thread)
339 struct peer *peer;
341 peer = THREAD_ARG (thread);
342 peer->t_routeadv[AFI_IP][SAFI_MULTICAST] = NULL;
344 if (BGP_DEBUG (events, EVENTS))
345 zlog_info ("%s routeadv timer expired for IPv4 Multicast", peer->host);
347 peer->synctime[AFI_IP][SAFI_MULTICAST] = time (NULL);
349 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
351 BGP_TIMER_ON (peer->t_routeadv[AFI_IP][SAFI_MULTICAST], bgp_routeadv_timer_ipv4_multicast,
352 peer->v_routeadv);
354 return 0;
358 bgp_routeadv_timer_ipv6_unicast (struct thread *thread)
360 struct peer *peer;
362 peer = THREAD_ARG (thread);
363 peer->t_routeadv[AFI_IP6][SAFI_UNICAST] = NULL;
365 if (BGP_DEBUG (events, EVENTS))
366 zlog_info ("%s routeadv timer expired for IPv6 Unicast", peer->host);
368 peer->synctime[AFI_IP6][SAFI_UNICAST] = time (NULL);
370 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
372 BGP_TIMER_ON (peer->t_routeadv[AFI_IP6][SAFI_UNICAST], bgp_routeadv_timer_ipv6_unicast,
373 peer->v_routeadv);
375 return 0;
379 bgp_routeadv_timer_vpnv4_unicast (struct thread *thread)
381 struct peer *peer;
383 peer = THREAD_ARG (thread);
384 peer->t_routeadv[AFI_IP][SAFI_MPLS_VPN] = NULL;
386 if (BGP_DEBUG (events, EVENTS))
387 zlog_info ("%s routeadv timer expired for VPNv4 unicast", peer->host);
389 peer->synctime[AFI_IP][SAFI_MPLS_VPN] = time (NULL);
391 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
393 BGP_TIMER_ON (peer->t_routeadv[AFI_IP][SAFI_MPLS_VPN], bgp_routeadv_timer_vpnv4_unicast,
394 peer->v_routeadv);
396 return 0;
399 void
400 bgp_routeadv_timer (struct peer *peer, afi_t afi, safi_t safi)
402 if (afi == AFI_IP && safi == SAFI_UNICAST)
403 BGP_TIMER_ON (peer->t_routeadv[afi][safi], bgp_routeadv_timer_ipv4_unicast, 1);
404 else if (afi == AFI_IP && safi == SAFI_MULTICAST)
405 BGP_TIMER_ON (peer->t_routeadv[afi][safi], bgp_routeadv_timer_ipv4_multicast, 1);
406 else if (afi == AFI_IP6 && safi == SAFI_UNICAST)
407 BGP_TIMER_ON (peer->t_routeadv[afi][safi], bgp_routeadv_timer_ipv6_unicast, 1);
408 else if (afi == AFI_IP && safi == SAFI_MPLS_VPN)
409 BGP_TIMER_ON (peer->t_routeadv[afi][safi], bgp_routeadv_timer_vpnv4_unicast, 1);
412 /* Reset bgp update timer */
413 static void
414 bgp_uptime_reset (struct peer *peer)
416 peer->uptime = time (NULL);
419 /* BGP Peer Down Cause */
420 char *peer_down_str[] =
423 "Router ID changed",
424 "Remote AS changed",
425 "Local AS change",
426 "Cluster ID changed",
427 "Confederation identifier changed",
428 "Confederation peer changed",
429 "RR client config change",
430 "RS client config change",
431 "Update source change",
432 "Address family activated",
433 "Admin. shutdown",
434 "User reset",
435 "BGP Notification received",
436 "BGP Notification send",
437 "Peer closed the session",
438 "Neighbor deleted",
439 "Peer-group add member",
440 "Peer-group delete member",
441 "Capability changed",
442 "Multihop config change",
443 "Password change",
444 "NSF peer closed the session"
448 bgp_graceful_restart_timer_expire (struct thread *thread)
450 struct peer *peer;
451 afi_t afi;
452 safi_t safi;
454 peer = THREAD_ARG (thread);
455 peer->t_gr_restart = NULL;
457 /* NSF delete stale route */
458 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
459 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
460 if (peer->nsf[afi][safi])
461 bgp_clear_stale_route (peer, afi, safi);
463 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
464 BGP_TIMER_OFF (peer->t_gr_stale);
466 if (BGP_DEBUG (events, EVENTS))
468 zlog_info ("%s graceful restart timer expired", peer->host);
469 zlog_info ("%s graceful restart stalepath timer stopped", peer->host);
472 return 0;
476 bgp_graceful_stale_timer_expire (struct thread *thread)
478 struct peer *peer;
479 afi_t afi;
480 safi_t safi;
482 peer = THREAD_ARG (thread);
483 peer->t_gr_stale = NULL;
485 if (BGP_DEBUG (events, EVENTS))
486 zlog_info ("%s graceful restart stalepath timer expired", peer->host);
488 /* NSF delete stale route */
489 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
490 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
491 if (peer->nsf[afi][safi])
492 bgp_clear_stale_route (peer, afi, safi);
494 return 0;
497 /* Administrative BGP peer stop event. */
499 bgp_stop (struct peer *peer)
501 afi_t afi;
502 safi_t safi;
503 char orf_name[BUFSIZ];
505 if (CHECK_FLAG (peer->sflags, PEER_STATUS_CREATE_INIT))
506 return 0;
508 /* Increment Dropped count. */
509 if (peer->status == Established)
511 bgp_fsm_change_status (peer, Idle);
512 peer->dropped++;
514 /* bgp log-neighbor-changes of neighbor Down */
515 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
516 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
517 peer_down_str [(int) peer->last_reset]);
519 /* graceful restart */
520 if (peer->t_gr_stale)
522 BGP_TIMER_OFF (peer->t_gr_stale);
523 if (BGP_DEBUG (events, EVENTS))
524 zlog_info ("%s graceful restart stalepath timer stopped", peer->host);
526 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
528 if (BGP_DEBUG (events, EVENTS))
530 zlog_info ("%s graceful restart timer started for %d sec",
531 peer->host, peer->v_gr_restart);
532 zlog_info ("%s graceful restart stalepath timer started for %d sec",
533 peer->host, peer->bgp->stalepath_time);
535 BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
536 peer->v_gr_restart);
537 BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
538 peer->bgp->stalepath_time);
540 else
542 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
544 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
545 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
546 peer->nsf[afi][safi] = 0;
549 /* set last reset time */
550 peer->resettime = time (NULL);
552 #ifdef HAVE_SNMP
553 bgpTrapBackwardTransition (peer);
554 #endif /* HAVE_SNMP */
556 /* Reset uptime. */
557 bgp_uptime_reset (peer);
559 /* Need of clear of peer. */
560 bgp_clear_route_all (peer);
562 /* Reset peer synctime */
563 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
564 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
565 peer->synctime[afi][safi] = 0;
568 /* Stop read and write threads when exists. */
569 BGP_READ_OFF (peer->t_read);
570 BGP_WRITE_OFF (peer->t_write);
572 /* Stop all timers. */
573 BGP_TIMER_OFF (peer->t_start);
574 BGP_TIMER_OFF (peer->t_connect);
575 BGP_TIMER_OFF (peer->t_holdtime);
576 BGP_TIMER_OFF (peer->t_keepalive);
577 BGP_TIMER_OFF (peer->t_asorig);
578 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
579 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
580 BGP_TIMER_OFF (peer->t_routeadv[afi][safi]);
582 /* Delete all existing events of the peer. */
583 BGP_EVENT_DELETE (peer);
585 /* Stream reset. */
586 peer->packet_size = 0;
588 /* Clear input and output buffer. */
589 if (peer->ibuf)
590 stream_reset (peer->ibuf);
591 if (peer->work)
592 stream_reset (peer->work);
593 stream_fifo_clean (peer->obuf);
595 /* Close of file descriptor. */
596 if (peer->fd >= 0)
598 close (peer->fd);
599 peer->fd = -1;
602 /* Connection information. */
603 if (peer->su_local)
605 XFREE (MTYPE_SOCKUNION, peer->su_local);
606 peer->su_local = NULL;
609 if (peer->su_remote)
611 XFREE (MTYPE_SOCKUNION, peer->su_remote);
612 peer->su_remote = NULL;
615 /* Clear remote router-id. */
616 peer->remote_id.s_addr = 0;
618 /* Clear peer capability flag. */
619 peer->cap = 0;
621 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
622 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
624 /* Reset all negotiated variables */
625 peer->afc_nego[afi][safi] = 0;
626 peer->afc_adv[afi][safi] = 0;
627 peer->afc_recv[afi][safi] = 0;
629 /* peer address family capability flags*/
630 peer->af_cap[afi][safi] = 0;
632 /* peer address family status flags*/
633 peer->af_sflags[afi][safi] = 0;
635 /* Received ORF prefix-filter */
636 peer->orf_plist[afi][safi] = NULL;
638 /* ORF received prefix-filter pnt */
639 sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
640 prefix_bgp_orf_remove_all (orf_name);
643 /* Reset keepalive and holdtime */
644 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
646 peer->v_keepalive = peer->keepalive;
647 peer->v_holdtime = peer->holdtime;
649 else
651 peer->v_keepalive = peer->bgp->default_keepalive;
652 peer->v_holdtime = peer->bgp->default_holdtime;
655 peer->update_time = 0;
657 /* Until we are sure that there is no problem about prefix count
658 this should be commented out.*/
659 #if 0
660 /* Reset prefix count */
661 peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
662 peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
663 peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
664 peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
665 peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
666 #endif /* 0 */
668 return 0;
671 /* BGP peer is stoped by the error. */
673 bgp_stop_with_error (struct peer *peer)
675 /* Double start timer. */
676 peer->v_active_delay *= 2;
678 /* Overflow check. */
679 if (peer->v_active_delay > BGP_DEFAULT_CONNECT_RETRY)
680 peer->v_active_delay = BGP_DEFAULT_CONNECT_RETRY;
682 bgp_stop (peer);
684 return 0;
687 /* TCP connection open. Next we send open message to remote peer. And
688 add read thread for reading open message. */
690 bgp_connect_success (struct peer *peer)
692 char buf1[BUFSIZ];
694 if (peer->fd < 0)
696 zlog_err ("bgp_connect_success peer's fd is negative value %d",
697 peer->fd);
698 return -1;
700 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
702 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
703 bgp_getsockname (peer);
705 if (BGP_DEBUG (normal, NORMAL))
707 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
708 zlog_info ("%s open active, local address %s", peer->host,
709 sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
710 else
711 zlog_info ("%s passive open", peer->host);
714 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
715 bgp_open_send (peer);
717 return 0;
720 /* TCP connect fail */
722 bgp_connect_fail (struct peer *peer)
724 bgp_stop (peer);
725 return 0;
728 /* This function is the first starting point of all BGP connection. It
729 try to connect to remote peer with non-blocking IO. */
731 bgp_start (struct peer *peer)
733 int status;
735 status = bgp_connect (peer);
737 switch (status)
739 case connect_error:
740 if (BGP_DEBUG (fsm, FSM))
741 plog_info (peer->log, "%s [FSM] Connect error", peer->host);
742 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
743 break;
744 case connect_success:
745 if (BGP_DEBUG (fsm, FSM))
746 plog_info (peer->log, "%s [FSM] Connect immediately success",
747 peer->host);
748 BGP_EVENT_ADD (peer, TCP_connection_open);
749 break;
750 case connect_in_progress:
751 /* To check nonblocking connect, we wait until socket is
752 readable or writable. */
753 if (BGP_DEBUG (fsm, FSM))
754 plog_info (peer->log, "%s [FSM] Non blocking connect waiting result",
755 peer->host);
756 if (peer->fd < 0)
758 zlog_err ("bgp_start peer's fd is negative value %d",
759 peer->fd);
760 return -1;
762 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
763 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
764 break;
766 return 0;
769 /* Connect retry timer is expired when the peer status is Connect. */
771 bgp_reconnect (struct peer *peer)
773 bgp_stop (peer);
774 bgp_start (peer);
775 return 0;
779 bgp_fsm_open (struct peer *peer)
781 /* Send keepalive and make keepalive timer */
782 bgp_keepalive_send (peer);
784 /* Reset holdtimer value. */
785 BGP_TIMER_OFF (peer->t_holdtime);
787 return 0;
790 /* Called after event occured, this function change status and reset
791 read/write and timer thread. */
792 void
793 bgp_fsm_change_status (struct peer *peer, int status)
795 bgp_dump_state (peer, peer->status, status);
797 /* Preserve old status and change into new status. */
798 peer->ostatus = peer->status;
799 peer->status = status;
801 if (BGP_DEBUG (normal, NORMAL))
802 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
803 zlog_info ("%s went from %s to %s", peer->host,
804 LOOKUP (bgp_status_msg, peer->ostatus),
805 LOOKUP (bgp_status_msg, peer->status));
808 /* Keepalive send to peer. */
810 bgp_fsm_keepalive_expire (struct peer *peer)
812 bgp_keepalive_send (peer);
813 return 0;
816 /* Hold timer expire. This is error of BGP connection. So cut the
817 peer and change to Idle status. */
819 bgp_fsm_holdtime_expire (struct peer *peer)
821 if (BGP_DEBUG (fsm, FSM))
822 zlog (peer->log, LOG_DEBUG, "%s [FSM] Hold timer expire", peer->host);
824 /* Send notify to remote peer. */
825 bgp_notify_send (peer, BGP_NOTIFY_HOLD_ERR, 0);
827 /* Sweep if it is temporary peer. */
828 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
830 zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
831 peer_delete (peer);
832 return -1;
835 return 0;
838 /* Status goes to Established. Send keepalive packet then make first
839 update information. */
841 bgp_establish (struct peer *peer)
843 struct bgp_notify *notify;
844 afi_t afi;
845 safi_t safi;
846 int nsf_af_count = 0;
848 /* Reset capability open status flag. */
849 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
850 SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
852 /* Clear last notification data. */
853 notify = &peer->notify;
854 if (notify->data)
855 XFREE (MTYPE_TMP, notify->data);
856 memset (notify, 0, sizeof (struct bgp_notify));
858 /* Clear active delay timer value to default. */
859 peer->v_active_delay = BGP_ACTIVE_DELAY_TIMER;
861 /* Increment established count. */
862 peer->established++;
863 bgp_fsm_change_status (peer, Established);
865 /* bgp log-neighbor-changes of neighbor Up */
866 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
867 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
869 /* graceful restart */
870 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
871 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
872 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
874 if (peer->afc_nego[afi][safi]
875 && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
876 && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
878 if (peer->nsf[afi][safi]
879 && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
880 bgp_clear_stale_route (peer, afi, safi);
882 peer->nsf[afi][safi] = 1;
883 nsf_af_count++;
885 else
887 if (peer->nsf[afi][safi])
888 bgp_clear_stale_route (peer, afi, safi);
889 peer->nsf[afi][safi] = 0;
893 if (nsf_af_count)
894 SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
895 else
897 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
898 if (peer->t_gr_stale)
900 BGP_TIMER_OFF (peer->t_gr_stale);
901 if (BGP_DEBUG (events, EVENTS))
902 zlog_info ("%s graceful restart stalepath timer stopped", peer->host);
906 if (peer->t_gr_restart)
908 BGP_TIMER_OFF (peer->t_gr_restart);
909 if (BGP_DEBUG (events, EVENTS))
910 zlog_info ("%s graceful restart timer stopped", peer->host);
913 #ifdef HAVE_SNMP
914 bgpTrapEstablished (peer);
915 #endif /* HAVE_SNMP */
917 /* Reset uptime, send keepalive, send current table. */
918 bgp_uptime_reset (peer);
920 /* Send route-refresh when ORF is enabled */
921 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
922 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
923 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
925 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
926 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
927 REFRESH_IMMEDIATE, 0);
928 else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
929 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
930 REFRESH_IMMEDIATE, 0);
933 if (peer->v_keepalive)
934 bgp_keepalive_send (peer);
936 /* First update is deferred until ORF or ROUTE-REFRESH is received */
937 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
938 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
939 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
940 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
941 || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
942 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
944 bgp_announce_route_all (peer);
945 return 0;
948 /* Keepalive packet is received. */
950 bgp_fsm_keepalive (struct peer *peer)
952 /* peer count update */
953 peer->keepalive_in++;
955 BGP_TIMER_OFF (peer->t_holdtime);
956 return 0;
959 /* Update packet is received. */
961 bgp_fsm_update (struct peer *peer)
963 BGP_TIMER_OFF (peer->t_holdtime);
964 return 0;
967 /* This is empty event. */
969 bgp_ignore (struct peer *peer)
971 if (BGP_DEBUG (fsm, FSM))
972 zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
973 return 0;
976 /* Finite State Machine structure */
977 struct {
978 int (*func) ();
979 int next_state;
980 } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
983 /* Idle state: In Idle state, all events other than BGP_Start is
984 ignored. With BGP_Start event, finite state machine calls
985 bgp_start(). */
986 {bgp_ignore, Active}, /* BGP_Start */
987 {bgp_stop, Idle}, /* BGP_Stop */
988 {bgp_stop, Idle}, /* TCP_connection_open */
989 {bgp_stop, Idle}, /* TCP_connection_closed */
990 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
991 {bgp_stop, Idle}, /* TCP_fatal_error */
992 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
993 {bgp_ignore, Idle}, /* Hold_Timer_expired */
994 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
995 {bgp_ignore, Idle}, /* Receive_OPEN_message */
996 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
997 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
998 {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
1001 /* Connect */
1002 {bgp_ignore, Connect}, /* BGP_Start */
1003 {bgp_stop, Idle}, /* BGP_Stop */
1004 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
1005 {bgp_stop, Idle}, /* TCP_connection_closed */
1006 {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
1007 {bgp_connect_fail, Idle}, /* TCP_fatal_error */
1008 {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
1009 {bgp_ignore, Idle}, /* Hold_Timer_expired */
1010 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1011 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1012 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
1013 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1014 {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
1017 /* Active, */
1018 {bgp_ignore, Active}, /* BGP_Start */
1019 {bgp_stop, Idle}, /* BGP_Stop */
1020 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
1021 {bgp_stop, Idle}, /* TCP_connection_closed */
1022 {bgp_ignore, Active}, /* TCP_connection_open_failed */
1023 {bgp_ignore, Idle}, /* TCP_fatal_error */
1024 {bgp_start, Connect}, /* ConnectRetry_timer_expired */
1025 {bgp_ignore, Idle}, /* Hold_Timer_expired */
1026 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1027 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1028 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
1029 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1030 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1033 /* OpenSent, */
1034 {bgp_ignore, OpenSent}, /* BGP_Start */
1035 {bgp_stop, Idle}, /* BGP_Stop */
1036 {bgp_stop, Idle}, /* TCP_connection_open */
1037 {bgp_stop, Active}, /* TCP_connection_closed */
1038 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
1039 {bgp_stop, Idle}, /* TCP_fatal_error */
1040 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1041 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1042 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1043 {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
1044 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
1045 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1046 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1049 /* OpenConfirm, */
1050 {bgp_ignore, OpenConfirm}, /* BGP_Start */
1051 {bgp_stop, Idle}, /* BGP_Stop */
1052 {bgp_stop, Idle}, /* TCP_connection_open */
1053 {bgp_stop, Idle}, /* TCP_connection_closed */
1054 {bgp_stop, Idle}, /* TCP_connection_open_failed */
1055 {bgp_stop, Idle}, /* TCP_fatal_error */
1056 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1057 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1058 {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
1059 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1060 {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
1061 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1062 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1065 /* Established, */
1066 {bgp_ignore, Established}, /* BGP_Start */
1067 {bgp_stop, Idle}, /* BGP_Stop */
1068 {bgp_stop, Idle}, /* TCP_connection_open */
1069 {bgp_stop, Idle}, /* TCP_connection_closed */
1070 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
1071 {bgp_stop, Idle}, /* TCP_fatal_error */
1072 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1073 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1074 {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
1075 {bgp_stop, Idle}, /* Receive_OPEN_message */
1076 {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
1077 {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
1078 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1082 static char *bgp_event_str[] =
1084 NULL,
1085 "BGP_Start",
1086 "BGP_Stop",
1087 "TCP_connection_open",
1088 "TCP_connection_closed",
1089 "TCP_connection_open_failed",
1090 "TCP_fatal_error",
1091 "ConnectRetry_timer_expired",
1092 "Hold_Timer_expired",
1093 "KeepAlive_timer_expired",
1094 "Receive_OPEN_message",
1095 "Receive_KEEPALIVE_message",
1096 "Receive_UPDATE_message",
1097 "Receive_NOTIFICATION_message"
1100 /* Execute event process. */
1102 bgp_event (struct thread *thread)
1104 int ret;
1105 int event;
1106 int next;
1107 struct peer *peer;
1109 peer = THREAD_ARG (thread);
1110 event = THREAD_VAL (thread);
1112 /* Logging this event. */
1113 next = FSM [peer->status -1][event - 1].next_state;
1115 if (BGP_DEBUG (fsm, FSM))
1116 plog_info (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
1117 bgp_event_str[event],
1118 LOOKUP (bgp_status_msg, peer->status),
1119 LOOKUP (bgp_status_msg, next));
1121 /* Call function. */
1122 ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
1124 /* When function do not want proceed next job return -1. */
1125 if (ret < 0)
1126 return ret;
1128 /* If status is changed. */
1129 if (next != peer->status)
1130 bgp_fsm_change_status (peer, next);
1132 /* Make sure timer is set. */
1133 bgp_timer_set (peer);
1135 return 0;