]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_fsm.c
[bgpd] simplify peer refcounts, squash slow peer leak
[mirror_frr.git] / bgpd / bgp_fsm.c
1 /* BGP-4 Finite State Machine
2 From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
3 Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
4
5 This file is part of GNU Zebra.
6
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.
11
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.
16
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. */
21
22 #include <zebra.h>
23
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"
33
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 */
46 \f
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. */
51
52 /* BGP event function. */
53 int bgp_event (struct thread *);
54
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 *);
60
61 /* BGP FSM functions. */
62 static int bgp_start (struct peer *);
63
64 /* BGP start timer jitter. */
65 static int
66 bgp_start_jitter (int time)
67 {
68 return ((rand () % (time + 1)) - (time / 2));
69 }
70
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))
75
76 /* Hook function called after bgp event is occered. And vty's
77 neighbor command invoke this function after making neighbor
78 structure. */
79 void
80 bgp_timer_set (struct peer *peer)
81 {
82 int jitter = 0;
83
84 switch (peer->status)
85 {
86 case Idle:
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))
91 {
92 BGP_TIMER_OFF (peer->t_start);
93 }
94 else
95 {
96 jitter = bgp_start_jitter (peer->v_start);
97 BGP_TIMER_ON (peer->t_start, bgp_start_timer,
98 peer->v_start + jitter);
99 }
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);
105 break;
106
107 case Connect:
108 /* After start timer is expired, the peer moves to Connnect
109 status. Make sure start timer is off and connect timer is
110 on. */
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);
117 break;
118
119 case Active:
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))
126 {
127 BGP_TIMER_OFF (peer->t_connect);
128 }
129 else
130 {
131 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
132 }
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);
137 break;
138
139 case OpenSent:
140 /* OpenSent status. */
141 BGP_TIMER_OFF (peer->t_start);
142 BGP_TIMER_OFF (peer->t_connect);
143 if (peer->v_holdtime != 0)
144 {
145 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
146 peer->v_holdtime);
147 }
148 else
149 {
150 BGP_TIMER_OFF (peer->t_holdtime);
151 }
152 BGP_TIMER_OFF (peer->t_keepalive);
153 BGP_TIMER_OFF (peer->t_asorig);
154 BGP_TIMER_OFF (peer->t_routeadv);
155 break;
156
157 case OpenConfirm:
158 /* OpenConfirm status. */
159 BGP_TIMER_OFF (peer->t_start);
160 BGP_TIMER_OFF (peer->t_connect);
161
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)
165 {
166 BGP_TIMER_OFF (peer->t_holdtime);
167 BGP_TIMER_OFF (peer->t_keepalive);
168 }
169 else
170 {
171 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
172 peer->v_holdtime);
173 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
174 peer->v_keepalive);
175 }
176 BGP_TIMER_OFF (peer->t_asorig);
177 BGP_TIMER_OFF (peer->t_routeadv);
178 break;
179
180 case Established:
181 /* In Established status start and connect timer is turned
182 off. */
183 BGP_TIMER_OFF (peer->t_start);
184 BGP_TIMER_OFF (peer->t_connect);
185
186 /* Same as OpenConfirm, if holdtime is zero then both holdtime
187 and keepalive must be turned off. */
188 if (peer->v_holdtime == 0)
189 {
190 BGP_TIMER_OFF (peer->t_holdtime);
191 BGP_TIMER_OFF (peer->t_keepalive);
192 }
193 else
194 {
195 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
196 peer->v_holdtime);
197 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
198 peer->v_keepalive);
199 }
200 BGP_TIMER_OFF (peer->t_asorig);
201 break;
202 case Deleted:
203 BGP_TIMER_OFF (peer->t_gr_restart);
204 BGP_TIMER_OFF (peer->t_gr_stale);
205 BGP_TIMER_OFF (peer->t_pmax_restart);
206 case Clearing:
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);
213 }
214 }
215
216 /* BGP start timer. This function set BGP_Start event to thread value
217 and process event. */
218 static int
219 bgp_start_timer (struct thread *thread)
220 {
221 struct peer *peer;
222
223 peer = THREAD_ARG (thread);
224 peer->t_start = NULL;
225
226 if (BGP_DEBUG (fsm, FSM))
227 zlog (peer->log, LOG_DEBUG,
228 "%s [FSM] Timer (start timer expire).", peer->host);
229
230 THREAD_VAL (thread) = BGP_Start;
231 bgp_event (thread); /* bgp_event unlocks peer */
232
233 return 0;
234 }
235
236 /* BGP connect retry timer. */
237 static int
238 bgp_connect_timer (struct thread *thread)
239 {
240 struct peer *peer;
241
242 peer = THREAD_ARG (thread);
243 peer->t_connect = NULL;
244
245 if (BGP_DEBUG (fsm, FSM))
246 zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
247 peer->host);
248
249 THREAD_VAL (thread) = ConnectRetry_timer_expired;
250 bgp_event (thread); /* bgp_event unlocks peer */
251
252 return 0;
253 }
254
255 /* BGP holdtime timer. */
256 static int
257 bgp_holdtime_timer (struct thread *thread)
258 {
259 struct peer *peer;
260
261 peer = THREAD_ARG (thread);
262 peer->t_holdtime = NULL;
263
264 if (BGP_DEBUG (fsm, FSM))
265 zlog (peer->log, LOG_DEBUG,
266 "%s [FSM] Timer (holdtime timer expire)",
267 peer->host);
268
269 THREAD_VAL (thread) = Hold_Timer_expired;
270 bgp_event (thread); /* bgp_event unlocks peer */
271
272 return 0;
273 }
274
275 /* BGP keepalive fire ! */
276 static int
277 bgp_keepalive_timer (struct thread *thread)
278 {
279 struct peer *peer;
280
281 peer = THREAD_ARG (thread);
282 peer->t_keepalive = NULL;
283
284 if (BGP_DEBUG (fsm, FSM))
285 zlog (peer->log, LOG_DEBUG,
286 "%s [FSM] Timer (keepalive timer expire)",
287 peer->host);
288
289 THREAD_VAL (thread) = KeepAlive_timer_expired;
290 bgp_event (thread); /* bgp_event unlocks peer */
291
292 return 0;
293 }
294
295 static int
296 bgp_routeadv_timer (struct thread *thread)
297 {
298 struct peer *peer;
299
300 peer = THREAD_ARG (thread);
301 peer->t_routeadv = NULL;
302
303 if (BGP_DEBUG (fsm, FSM))
304 zlog (peer->log, LOG_DEBUG,
305 "%s [FSM] Timer (routeadv timer expire)",
306 peer->host);
307
308 peer->synctime = time (NULL);
309
310 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
311
312 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer,
313 peer->v_routeadv);
314
315 return 0;
316 }
317
318 /* Reset bgp update timer */
319 static void
320 bgp_uptime_reset (struct peer *peer)
321 {
322 peer->uptime = time (NULL);
323 }
324
325 /* BGP Peer Down Cause */
326 const char *peer_down_str[] =
327 {
328 "",
329 "Router ID changed",
330 "Remote AS changed",
331 "Local AS change",
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",
339 "Admin. shutdown",
340 "User reset",
341 "BGP Notification received",
342 "BGP Notification send",
343 "Peer closed the session",
344 "Neighbor deleted",
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"
351 };
352
353 static int
354 bgp_graceful_restart_timer_expire (struct thread *thread)
355 {
356 struct peer *peer;
357 afi_t afi;
358 safi_t safi;
359
360 peer = THREAD_ARG (thread);
361 peer->t_gr_restart = NULL;
362
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);
368
369 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
370 BGP_TIMER_OFF (peer->t_gr_stale);
371
372 if (BGP_DEBUG (events, EVENTS))
373 {
374 zlog_debug ("%s graceful restart timer expired", peer->host);
375 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
376 }
377
378 bgp_timer_set (peer);
379
380 return 0;
381 }
382
383 static int
384 bgp_graceful_stale_timer_expire (struct thread *thread)
385 {
386 struct peer *peer;
387 afi_t afi;
388 safi_t safi;
389
390 peer = THREAD_ARG (thread);
391 peer->t_gr_stale = NULL;
392
393 if (BGP_DEBUG (events, EVENTS))
394 zlog_debug ("%s graceful restart stalepath timer expired", peer->host);
395
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);
401
402 return 0;
403 }
404
405 /* Called after event occured, this function change status and reset
406 read/write and timer thread. */
407 void
408 bgp_fsm_change_status (struct peer *peer, int status)
409 {
410 bgp_dump_state (peer, peer->status, status);
411
412 /* Preserve old status and change into new status. */
413 peer->ostatus = peer->status;
414 peer->status = status;
415
416 if (BGP_DEBUG (normal, NORMAL))
417 zlog_debug ("%s went from %s to %s",
418 peer->host,
419 LOOKUP (bgp_status_msg, peer->ostatus),
420 LOOKUP (bgp_status_msg, peer->status));
421 }
422
423 /* Administrative BGP peer stop event. */
424 int
425 bgp_stop (struct peer *peer)
426 {
427 afi_t afi;
428 safi_t safi;
429 char orf_name[BUFSIZ];
430
431 /* Increment Dropped count. */
432 if (peer->status == Established)
433 {
434 peer->dropped++;
435
436 /* bgp log-neighbor-changes of neighbor Down */
437 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
438 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
439 peer_down_str [(int) peer->last_reset]);
440
441 /* graceful restart */
442 if (peer->t_gr_stale)
443 {
444 BGP_TIMER_OFF (peer->t_gr_stale);
445 if (BGP_DEBUG (events, EVENTS))
446 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
447 }
448 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
449 {
450 if (BGP_DEBUG (events, EVENTS))
451 {
452 zlog_debug ("%s graceful restart timer started for %d sec",
453 peer->host, peer->v_gr_restart);
454 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
455 peer->host, peer->bgp->stalepath_time);
456 }
457 BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
458 peer->v_gr_restart);
459 BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
460 peer->bgp->stalepath_time);
461 }
462 else
463 {
464 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
465
466 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
467 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
468 peer->nsf[afi][safi] = 0;
469 }
470
471 /* set last reset time */
472 peer->resettime = time (NULL);
473 /* Reset uptime. */
474 bgp_uptime_reset (peer);
475
476 #ifdef HAVE_SNMP
477 bgpTrapBackwardTransition (peer);
478 #endif /* HAVE_SNMP */
479
480 /* Reset uptime. */
481 bgp_uptime_reset (peer);
482
483 /* Need of clear of peer. */
484 bgp_clear_route_all (peer);
485
486 /* Reset peer synctime */
487 peer->synctime = 0;
488 }
489
490 /* Stop read and write threads when exists. */
491 BGP_READ_OFF (peer->t_read);
492 BGP_WRITE_OFF (peer->t_write);
493
494 /* Stop all timers. */
495 BGP_TIMER_OFF (peer->t_start);
496 BGP_TIMER_OFF (peer->t_connect);
497 BGP_TIMER_OFF (peer->t_holdtime);
498 BGP_TIMER_OFF (peer->t_keepalive);
499 BGP_TIMER_OFF (peer->t_asorig);
500 BGP_TIMER_OFF (peer->t_routeadv);
501
502 /* Delete all existing events of the peer */
503 BGP_EVENT_FLUSH (peer);
504
505 /* Stream reset. */
506 peer->packet_size = 0;
507
508 /* Clear input and output buffer. */
509 if (peer->ibuf)
510 stream_reset (peer->ibuf);
511 if (peer->work)
512 stream_reset (peer->work);
513 if (peer->obuf)
514 stream_fifo_clean (peer->obuf);
515
516 /* Close of file descriptor. */
517 if (peer->fd >= 0)
518 {
519 close (peer->fd);
520 peer->fd = -1;
521 }
522
523 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
524 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
525 {
526 /* Reset all negotiated variables */
527 peer->afc_nego[afi][safi] = 0;
528 peer->afc_adv[afi][safi] = 0;
529 peer->afc_recv[afi][safi] = 0;
530
531 /* peer address family capability flags*/
532 peer->af_cap[afi][safi] = 0;
533
534 /* peer address family status flags*/
535 peer->af_sflags[afi][safi] = 0;
536
537 /* Received ORF prefix-filter */
538 peer->orf_plist[afi][safi] = NULL;
539
540 /* ORF received prefix-filter pnt */
541 sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
542 prefix_bgp_orf_remove_all (orf_name);
543 }
544
545 /* Reset keepalive and holdtime */
546 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
547 {
548 peer->v_keepalive = peer->keepalive;
549 peer->v_holdtime = peer->holdtime;
550 }
551 else
552 {
553 peer->v_keepalive = peer->bgp->default_keepalive;
554 peer->v_holdtime = peer->bgp->default_holdtime;
555 }
556
557 peer->update_time = 0;
558
559 /* Until we are sure that there is no problem about prefix count
560 this should be commented out.*/
561 #if 0
562 /* Reset prefix count */
563 peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
564 peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
565 peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
566 peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
567 peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
568 #endif /* 0 */
569
570 return 0;
571 }
572
573 /* BGP peer is stoped by the error. */
574 static int
575 bgp_stop_with_error (struct peer *peer)
576 {
577 /* Double start timer. */
578 peer->v_start *= 2;
579
580 /* Overflow check. */
581 if (peer->v_start >= (60 * 2))
582 peer->v_start = (60 * 2);
583
584 bgp_stop (peer);
585
586 return 0;
587 }
588
589 /* TCP connection open. Next we send open message to remote peer. And
590 add read thread for reading open message. */
591 static int
592 bgp_connect_success (struct peer *peer)
593 {
594 char buf1[BUFSIZ];
595
596 if (peer->fd < 0)
597 {
598 zlog_err ("bgp_connect_success peer's fd is negative value %d",
599 peer->fd);
600 return -1;
601 }
602 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
603
604 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
605 bgp_getsockname (peer);
606
607 if (BGP_DEBUG (normal, NORMAL))
608 {
609 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
610 zlog_debug ("%s open active, local address %s", peer->host,
611 sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
612 else
613 zlog_debug ("%s passive open", peer->host);
614 }
615
616 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
617 bgp_open_send (peer);
618
619 return 0;
620 }
621
622 /* TCP connect fail */
623 static int
624 bgp_connect_fail (struct peer *peer)
625 {
626 bgp_stop (peer);
627 return 0;
628 }
629
630 /* This function is the first starting point of all BGP connection. It
631 try to connect to remote peer with non-blocking IO. */
632 int
633 bgp_start (struct peer *peer)
634 {
635 int status;
636
637 if (BGP_PEER_START_SUPPRESSED (peer))
638 {
639 if (BGP_DEBUG (fsm, FSM))
640 plog_err (peer->log, "%s [FSM] Trying to start suppressed peer"
641 " - this is never supposed to happen!", peer->host);
642 return -1;
643 }
644
645 /* Scrub some information that might be left over from a previous,
646 * session
647 */
648 /* Connection information. */
649 if (peer->su_local)
650 {
651 sockunion_free (peer->su_local);
652 peer->su_local = NULL;
653 }
654
655 if (peer->su_remote)
656 {
657 sockunion_free (peer->su_remote);
658 peer->su_remote = NULL;
659 }
660
661 /* Clear remote router-id. */
662 peer->remote_id.s_addr = 0;
663
664 /* Clear peer capability flag. */
665 peer->cap = 0;
666
667 /* If the peer is passive mode, force to move to Active mode. */
668 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
669 {
670 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
671 return 0;
672 }
673
674 status = bgp_connect (peer);
675
676 switch (status)
677 {
678 case connect_error:
679 if (BGP_DEBUG (fsm, FSM))
680 plog_debug (peer->log, "%s [FSM] Connect error", peer->host);
681 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
682 break;
683 case connect_success:
684 if (BGP_DEBUG (fsm, FSM))
685 plog_debug (peer->log, "%s [FSM] Connect immediately success",
686 peer->host);
687 BGP_EVENT_ADD (peer, TCP_connection_open);
688 break;
689 case connect_in_progress:
690 /* To check nonblocking connect, we wait until socket is
691 readable or writable. */
692 if (BGP_DEBUG (fsm, FSM))
693 plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result",
694 peer->host);
695 if (peer->fd < 0)
696 {
697 zlog_err ("bgp_start peer's fd is negative value %d",
698 peer->fd);
699 return -1;
700 }
701 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
702 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
703 break;
704 }
705 return 0;
706 }
707
708 /* Connect retry timer is expired when the peer status is Connect. */
709 static int
710 bgp_reconnect (struct peer *peer)
711 {
712 bgp_stop (peer);
713 bgp_start (peer);
714 return 0;
715 }
716
717 static int
718 bgp_fsm_open (struct peer *peer)
719 {
720 /* Send keepalive and make keepalive timer */
721 bgp_keepalive_send (peer);
722
723 /* Reset holdtimer value. */
724 BGP_TIMER_OFF (peer->t_holdtime);
725
726 return 0;
727 }
728
729 /* Keepalive send to peer. */
730 static int
731 bgp_fsm_keepalive_expire (struct peer *peer)
732 {
733 bgp_keepalive_send (peer);
734 return 0;
735 }
736
737 /* Hold timer expire. This is error of BGP connection. So cut the
738 peer and change to Idle status. */
739 static int
740 bgp_fsm_holdtime_expire (struct peer *peer)
741 {
742 if (BGP_DEBUG (fsm, FSM))
743 zlog (peer->log, LOG_DEBUG, "%s [FSM] Hold timer expire", peer->host);
744
745 /* Send notify to remote peer. */
746 bgp_notify_send (peer, BGP_NOTIFY_HOLD_ERR, 0);
747
748 /* Sweep if it is temporary peer. */
749 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
750 {
751 zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
752 peer_delete (peer);
753 return -1;
754 }
755
756 return 0;
757 }
758
759 /* Status goes to Established. Send keepalive packet then make first
760 update information. */
761 static int
762 bgp_establish (struct peer *peer)
763 {
764 struct bgp_notify *notify;
765 afi_t afi;
766 safi_t safi;
767 int nsf_af_count = 0;
768
769 /* Reset capability open status flag. */
770 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
771 SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
772
773 /* Clear last notification data. */
774 notify = &peer->notify;
775 if (notify->data)
776 XFREE (MTYPE_TMP, notify->data);
777 memset (notify, 0, sizeof (struct bgp_notify));
778
779 /* Clear start timer value to default. */
780 peer->v_start = BGP_INIT_START_TIMER;
781
782 /* Increment established count. */
783 peer->established++;
784 bgp_fsm_change_status (peer, Established);
785
786 /* bgp log-neighbor-changes of neighbor Up */
787 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
788 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
789
790 /* graceful restart */
791 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
792 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
793 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
794 {
795 if (peer->afc_nego[afi][safi]
796 && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
797 && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
798 {
799 if (peer->nsf[afi][safi]
800 && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
801 bgp_clear_stale_route (peer, afi, safi);
802
803 peer->nsf[afi][safi] = 1;
804 nsf_af_count++;
805 }
806 else
807 {
808 if (peer->nsf[afi][safi])
809 bgp_clear_stale_route (peer, afi, safi);
810 peer->nsf[afi][safi] = 0;
811 }
812 }
813
814 if (nsf_af_count)
815 SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
816 else
817 {
818 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
819 if (peer->t_gr_stale)
820 {
821 BGP_TIMER_OFF (peer->t_gr_stale);
822 if (BGP_DEBUG (events, EVENTS))
823 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
824 }
825 }
826
827 if (peer->t_gr_restart)
828 {
829 BGP_TIMER_OFF (peer->t_gr_restart);
830 if (BGP_DEBUG (events, EVENTS))
831 zlog_debug ("%s graceful restart timer stopped", peer->host);
832 }
833
834 #ifdef HAVE_SNMP
835 bgpTrapEstablished (peer);
836 #endif /* HAVE_SNMP */
837
838 /* Reset uptime, send keepalive, send current table. */
839 bgp_uptime_reset (peer);
840
841 /* Send route-refresh when ORF is enabled */
842 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
843 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
844 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
845 {
846 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
847 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
848 REFRESH_IMMEDIATE, 0);
849 else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
850 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
851 REFRESH_IMMEDIATE, 0);
852 }
853
854 if (peer->v_keepalive)
855 bgp_keepalive_send (peer);
856
857 /* First update is deferred until ORF or ROUTE-REFRESH is received */
858 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
859 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
860 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
861 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
862 || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
863 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
864
865 bgp_announce_route_all (peer);
866
867 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1);
868
869 return 0;
870 }
871
872 /* Keepalive packet is received. */
873 static int
874 bgp_fsm_keepalive (struct peer *peer)
875 {
876 /* peer count update */
877 peer->keepalive_in++;
878
879 BGP_TIMER_OFF (peer->t_holdtime);
880 return 0;
881 }
882
883 /* Update packet is received. */
884 static int
885 bgp_fsm_update (struct peer *peer)
886 {
887 BGP_TIMER_OFF (peer->t_holdtime);
888 return 0;
889 }
890
891 /* This is empty event. */
892 static int
893 bgp_ignore (struct peer *peer)
894 {
895 if (BGP_DEBUG (fsm, FSM))
896 zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
897 return 0;
898 }
899 \f
900 /* Finite State Machine structure */
901 struct {
902 int (*func) ();
903 int next_state;
904 } FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
905 {
906 {
907 /* Idle state: In Idle state, all events other than BGP_Start is
908 ignored. With BGP_Start event, finite state machine calls
909 bgp_start(). */
910 {bgp_start, Connect}, /* BGP_Start */
911 {bgp_stop, Idle}, /* BGP_Stop */
912 {bgp_stop, Idle}, /* TCP_connection_open */
913 {bgp_stop, Idle}, /* TCP_connection_closed */
914 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
915 {bgp_stop, Idle}, /* TCP_fatal_error */
916 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
917 {bgp_ignore, Idle}, /* Hold_Timer_expired */
918 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
919 {bgp_ignore, Idle}, /* Receive_OPEN_message */
920 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
921 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
922 {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
923 {bgp_ignore, Idle}, /* Clearing_Completed */
924 },
925 {
926 /* Connect */
927 {bgp_ignore, Connect}, /* BGP_Start */
928 {bgp_stop, Idle}, /* BGP_Stop */
929 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
930 {bgp_stop, Idle}, /* TCP_connection_closed */
931 {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
932 {bgp_connect_fail, Idle}, /* TCP_fatal_error */
933 {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
934 {bgp_ignore, Idle}, /* Hold_Timer_expired */
935 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
936 {bgp_ignore, Idle}, /* Receive_OPEN_message */
937 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
938 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
939 {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
940 {bgp_ignore, Idle}, /* Clearing_Completed */
941 },
942 {
943 /* Active, */
944 {bgp_ignore, Active}, /* BGP_Start */
945 {bgp_stop, Idle}, /* BGP_Stop */
946 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
947 {bgp_stop, Idle}, /* TCP_connection_closed */
948 {bgp_ignore, Active}, /* TCP_connection_open_failed */
949 {bgp_ignore, Idle}, /* TCP_fatal_error */
950 {bgp_start, Connect}, /* ConnectRetry_timer_expired */
951 {bgp_ignore, Idle}, /* Hold_Timer_expired */
952 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
953 {bgp_ignore, Idle}, /* Receive_OPEN_message */
954 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
955 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
956 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
957 {bgp_ignore, Idle}, /* Clearing_Completed */
958 },
959 {
960 /* OpenSent, */
961 {bgp_ignore, OpenSent}, /* BGP_Start */
962 {bgp_stop, Idle}, /* BGP_Stop */
963 {bgp_stop, Idle}, /* TCP_connection_open */
964 {bgp_stop, Active}, /* TCP_connection_closed */
965 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
966 {bgp_stop, Idle}, /* TCP_fatal_error */
967 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
968 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
969 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
970 {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
971 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
972 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
973 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
974 {bgp_ignore, Idle}, /* Clearing_Completed */
975 },
976 {
977 /* OpenConfirm, */
978 {bgp_ignore, OpenConfirm}, /* BGP_Start */
979 {bgp_stop, Idle}, /* BGP_Stop */
980 {bgp_stop, Idle}, /* TCP_connection_open */
981 {bgp_stop, Idle}, /* TCP_connection_closed */
982 {bgp_stop, Idle}, /* TCP_connection_open_failed */
983 {bgp_stop, Idle}, /* TCP_fatal_error */
984 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
985 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
986 {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
987 {bgp_ignore, Idle}, /* Receive_OPEN_message */
988 {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
989 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
990 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
991 {bgp_ignore, Idle}, /* Clearing_Completed */
992 },
993 {
994 /* Established, */
995 {bgp_ignore, Established}, /* BGP_Start */
996 {bgp_stop, Clearing}, /* BGP_Stop */
997 {bgp_stop, Clearing}, /* TCP_connection_open */
998 {bgp_stop, Clearing}, /* TCP_connection_closed */
999 {bgp_ignore, Clearing}, /* TCP_connection_open_failed */
1000 {bgp_stop, Clearing}, /* TCP_fatal_error */
1001 {bgp_ignore, Clearing}, /* ConnectRetry_timer_expired */
1002 {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */
1003 {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
1004 {bgp_stop, Clearing}, /* Receive_OPEN_message */
1005 {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
1006 {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
1007 {bgp_stop_with_error, Clearing}, /* Receive_NOTIFICATION_message */
1008 {bgp_ignore, Idle}, /* Clearing_Completed */
1009 },
1010 {
1011 /* Clearing, */
1012 {bgp_ignore, Clearing}, /* BGP_Start */
1013 {bgp_ignore, Clearing}, /* BGP_Stop */
1014 {bgp_ignore, Clearing}, /* TCP_connection_open */
1015 {bgp_ignore, Clearing}, /* TCP_connection_closed */
1016 {bgp_ignore, Clearing}, /* TCP_connection_open_failed */
1017 {bgp_ignore, Clearing}, /* TCP_fatal_error */
1018 {bgp_ignore, Clearing}, /* ConnectRetry_timer_expired */
1019 {bgp_ignore, Clearing}, /* Hold_Timer_expired */
1020 {bgp_ignore, Clearing}, /* KeepAlive_timer_expired */
1021 {bgp_ignore, Clearing}, /* Receive_OPEN_message */
1022 {bgp_ignore, Clearing}, /* Receive_KEEPALIVE_message */
1023 {bgp_ignore, Clearing}, /* Receive_UPDATE_message */
1024 {bgp_ignore, Clearing}, /* Receive_NOTIFICATION_message */
1025 {bgp_ignore, Idle }, /* Clearing_Completed */
1026 },
1027 {
1028 /* Deleted, */
1029 {bgp_ignore, Deleted}, /* BGP_Start */
1030 {bgp_ignore, Deleted}, /* BGP_Stop */
1031 {bgp_ignore, Deleted}, /* TCP_connection_open */
1032 {bgp_ignore, Deleted}, /* TCP_connection_closed */
1033 {bgp_ignore, Deleted}, /* TCP_connection_open_failed */
1034 {bgp_ignore, Deleted}, /* TCP_fatal_error */
1035 {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */
1036 {bgp_ignore, Deleted}, /* Hold_Timer_expired */
1037 {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */
1038 {bgp_ignore, Deleted}, /* Receive_OPEN_message */
1039 {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */
1040 {bgp_ignore, Deleted}, /* Receive_UPDATE_message */
1041 {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */
1042 {bgp_ignore, Deleted}, /* Clearing_Completed */
1043 },
1044 };
1045
1046 static const char *bgp_event_str[] =
1047 {
1048 NULL,
1049 "BGP_Start",
1050 "BGP_Stop",
1051 "TCP_connection_open",
1052 "TCP_connection_closed",
1053 "TCP_connection_open_failed",
1054 "TCP_fatal_error",
1055 "ConnectRetry_timer_expired",
1056 "Hold_Timer_expired",
1057 "KeepAlive_timer_expired",
1058 "Receive_OPEN_message",
1059 "Receive_KEEPALIVE_message",
1060 "Receive_UPDATE_message",
1061 "Receive_NOTIFICATION_message",
1062 "Clearing_Completed",
1063 };
1064
1065 /* Execute event process. */
1066 int
1067 bgp_event (struct thread *thread)
1068 {
1069 int ret = 0;
1070 int event;
1071 int next;
1072 struct peer *peer;
1073
1074 peer = THREAD_ARG (thread);
1075 event = THREAD_VAL (thread);
1076
1077 /* Logging this event. */
1078 next = FSM [peer->status -1][event - 1].next_state;
1079
1080 if (BGP_DEBUG (fsm, FSM) && peer->status != next)
1081 plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
1082 bgp_event_str[event],
1083 LOOKUP (bgp_status_msg, peer->status),
1084 LOOKUP (bgp_status_msg, next));
1085
1086 /* Call function. */
1087 if (FSM [peer->status -1][event - 1].func)
1088 ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
1089
1090 /* When function do not want proceed next job return -1. */
1091 if (ret >= 0)
1092 {
1093 /* If status is changed. */
1094 if (next != peer->status)
1095 bgp_fsm_change_status (peer, next);
1096
1097 /* Make sure timer is set. */
1098 bgp_timer_set (peer);
1099 }
1100
1101 return ret;
1102 }