blob: 1823336735f0a9d9b329cc2f54ff70f624e29a1f [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP-4 Finite State Machine
2 From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
3 Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
4
5This file is part of GNU Zebra.
6
7GNU Zebra is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 2, or (at your option) any
10later version.
11
12GNU Zebra is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Zebra; see the file COPYING. If not, write to the Free
19Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
2002111-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"
Paul Jakma782fb072015-09-15 16:17:22 +010033#include "workqueue.h"
paul718e3742002-12-13 20:15:29 +000034
35#include "bgpd/bgpd.h"
36#include "bgpd/bgp_attr.h"
37#include "bgpd/bgp_debug.h"
38#include "bgpd/bgp_fsm.h"
39#include "bgpd/bgp_packet.h"
40#include "bgpd/bgp_network.h"
41#include "bgpd/bgp_route.h"
42#include "bgpd/bgp_dump.h"
43#include "bgpd/bgp_open.h"
44#ifdef HAVE_SNMP
45#include "bgpd/bgp_snmp.h"
46#endif /* HAVE_SNMP */
David Lamparter6b0655a2014-06-04 06:53:35 +020047
paul718e3742002-12-13 20:15:29 +000048/* BGP FSM (finite state machine) has three types of functions. Type
49 one is thread functions. Type two is event functions. Type three
50 is FSM functions. Timer functions are set by bgp_timer_set
51 function. */
52
53/* BGP event function. */
54int bgp_event (struct thread *);
55
56/* BGP thread functions. */
57static int bgp_start_timer (struct thread *);
58static int bgp_connect_timer (struct thread *);
59static int bgp_holdtime_timer (struct thread *);
60static int bgp_keepalive_timer (struct thread *);
61
62/* BGP FSM functions. */
63static int bgp_start (struct peer *);
64
65/* BGP start timer jitter. */
paul94f2b392005-06-28 12:44:16 +000066static int
paul718e3742002-12-13 20:15:29 +000067bgp_start_jitter (int time)
68{
Donald Sharpf31bab42015-06-19 19:26:19 -040069 return ((random () % (time + 1)) - (time / 2));
paul718e3742002-12-13 20:15:29 +000070}
71
Paul Jakmaca058a32006-09-14 02:58:49 +000072/* Check if suppress start/restart of sessions to peer. */
73#define BGP_PEER_START_SUPPRESSED(P) \
74 (CHECK_FLAG ((P)->flags, PEER_FLAG_SHUTDOWN) \
75 || CHECK_FLAG ((P)->sflags, PEER_STATUS_PREFIX_OVERFLOW))
76
paul718e3742002-12-13 20:15:29 +000077/* Hook function called after bgp event is occered. And vty's
78 neighbor command invoke this function after making neighbor
79 structure. */
80void
81bgp_timer_set (struct peer *peer)
82{
83 int jitter = 0;
84
85 switch (peer->status)
86 {
87 case Idle:
88 /* First entry point of peer's finite state machine. In Idle
89 status start timer is on unless peer is shutdown or peer is
90 inactive. All other timer must be turned off */
Paul Jakmaca058a32006-09-14 02:58:49 +000091 if (BGP_PEER_START_SUPPRESSED (peer) || ! peer_active (peer))
paul718e3742002-12-13 20:15:29 +000092 {
93 BGP_TIMER_OFF (peer->t_start);
94 }
95 else
96 {
97 jitter = bgp_start_jitter (peer->v_start);
98 BGP_TIMER_ON (peer->t_start, bgp_start_timer,
99 peer->v_start + jitter);
100 }
101 BGP_TIMER_OFF (peer->t_connect);
102 BGP_TIMER_OFF (peer->t_holdtime);
103 BGP_TIMER_OFF (peer->t_keepalive);
paul718e3742002-12-13 20:15:29 +0000104 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);
paul718e3742002-12-13 20:15:29 +0000115 BGP_TIMER_OFF (peer->t_routeadv);
116 break;
117
118 case Active:
119 /* Active is waiting connection from remote peer. And if
120 connect timer is expired, change status to Connect. */
121 BGP_TIMER_OFF (peer->t_start);
122 /* If peer is passive mode, do not set connect timer. */
hasso93406d82005-02-02 14:40:33 +0000123 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)
124 || CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
paul718e3742002-12-13 20:15:29 +0000125 {
126 BGP_TIMER_OFF (peer->t_connect);
127 }
128 else
129 {
130 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
131 }
132 BGP_TIMER_OFF (peer->t_holdtime);
133 BGP_TIMER_OFF (peer->t_keepalive);
paul718e3742002-12-13 20:15:29 +0000134 BGP_TIMER_OFF (peer->t_routeadv);
135 break;
136
137 case OpenSent:
138 /* OpenSent status. */
139 BGP_TIMER_OFF (peer->t_start);
140 BGP_TIMER_OFF (peer->t_connect);
141 if (peer->v_holdtime != 0)
142 {
143 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
144 peer->v_holdtime);
145 }
146 else
147 {
148 BGP_TIMER_OFF (peer->t_holdtime);
149 }
150 BGP_TIMER_OFF (peer->t_keepalive);
paul718e3742002-12-13 20:15:29 +0000151 BGP_TIMER_OFF (peer->t_routeadv);
152 break;
153
154 case OpenConfirm:
155 /* OpenConfirm status. */
156 BGP_TIMER_OFF (peer->t_start);
157 BGP_TIMER_OFF (peer->t_connect);
158
159 /* If the negotiated Hold Time value is zero, then the Hold Time
160 timer and KeepAlive timers are not started. */
161 if (peer->v_holdtime == 0)
162 {
163 BGP_TIMER_OFF (peer->t_holdtime);
164 BGP_TIMER_OFF (peer->t_keepalive);
165 }
166 else
167 {
168 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
169 peer->v_holdtime);
170 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
171 peer->v_keepalive);
172 }
paul718e3742002-12-13 20:15:29 +0000173 BGP_TIMER_OFF (peer->t_routeadv);
174 break;
175
176 case Established:
177 /* In Established status start and connect timer is turned
178 off. */
179 BGP_TIMER_OFF (peer->t_start);
180 BGP_TIMER_OFF (peer->t_connect);
181
182 /* Same as OpenConfirm, if holdtime is zero then both holdtime
183 and keepalive must be turned off. */
184 if (peer->v_holdtime == 0)
185 {
186 BGP_TIMER_OFF (peer->t_holdtime);
187 BGP_TIMER_OFF (peer->t_keepalive);
188 }
189 else
190 {
191 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
192 peer->v_holdtime);
193 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
194 peer->v_keepalive);
195 }
paul718e3742002-12-13 20:15:29 +0000196 break;
Paul Jakmaca058a32006-09-14 02:58:49 +0000197 case Deleted:
198 BGP_TIMER_OFF (peer->t_gr_restart);
199 BGP_TIMER_OFF (peer->t_gr_stale);
200 BGP_TIMER_OFF (peer->t_pmax_restart);
201 case Clearing:
202 BGP_TIMER_OFF (peer->t_start);
203 BGP_TIMER_OFF (peer->t_connect);
204 BGP_TIMER_OFF (peer->t_holdtime);
205 BGP_TIMER_OFF (peer->t_keepalive);
Paul Jakmaca058a32006-09-14 02:58:49 +0000206 BGP_TIMER_OFF (peer->t_routeadv);
paul718e3742002-12-13 20:15:29 +0000207 }
208}
209
210/* BGP start timer. This function set BGP_Start event to thread value
211 and process event. */
212static int
213bgp_start_timer (struct thread *thread)
214{
215 struct peer *peer;
216
217 peer = THREAD_ARG (thread);
218 peer->t_start = NULL;
219
220 if (BGP_DEBUG (fsm, FSM))
221 zlog (peer->log, LOG_DEBUG,
222 "%s [FSM] Timer (start timer expire).", peer->host);
223
224 THREAD_VAL (thread) = BGP_Start;
paul200df112005-06-01 11:17:05 +0000225 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000226
227 return 0;
228}
229
230/* BGP connect retry timer. */
231static int
232bgp_connect_timer (struct thread *thread)
233{
234 struct peer *peer;
235
236 peer = THREAD_ARG (thread);
237 peer->t_connect = NULL;
238
239 if (BGP_DEBUG (fsm, FSM))
240 zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
241 peer->host);
242
243 THREAD_VAL (thread) = ConnectRetry_timer_expired;
paul200df112005-06-01 11:17:05 +0000244 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000245
246 return 0;
247}
248
249/* BGP holdtime timer. */
250static int
251bgp_holdtime_timer (struct thread *thread)
252{
253 struct peer *peer;
254
255 peer = THREAD_ARG (thread);
256 peer->t_holdtime = NULL;
257
258 if (BGP_DEBUG (fsm, FSM))
259 zlog (peer->log, LOG_DEBUG,
260 "%s [FSM] Timer (holdtime timer expire)",
261 peer->host);
262
263 THREAD_VAL (thread) = Hold_Timer_expired;
paul200df112005-06-01 11:17:05 +0000264 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000265
266 return 0;
267}
268
269/* BGP keepalive fire ! */
270static int
271bgp_keepalive_timer (struct thread *thread)
272{
273 struct peer *peer;
274
275 peer = THREAD_ARG (thread);
276 peer->t_keepalive = NULL;
277
278 if (BGP_DEBUG (fsm, FSM))
279 zlog (peer->log, LOG_DEBUG,
280 "%s [FSM] Timer (keepalive timer expire)",
281 peer->host);
282
283 THREAD_VAL (thread) = KeepAlive_timer_expired;
paul200df112005-06-01 11:17:05 +0000284 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000285
286 return 0;
287}
288
paul94f2b392005-06-28 12:44:16 +0000289static int
paul718e3742002-12-13 20:15:29 +0000290bgp_routeadv_timer (struct thread *thread)
291{
292 struct peer *peer;
293
294 peer = THREAD_ARG (thread);
295 peer->t_routeadv = NULL;
296
297 if (BGP_DEBUG (fsm, FSM))
298 zlog (peer->log, LOG_DEBUG,
299 "%s [FSM] Timer (routeadv timer expire)",
300 peer->host);
301
Stephen Hemminger65957882010-01-15 16:22:10 +0300302 peer->synctime = bgp_clock ();
paul718e3742002-12-13 20:15:29 +0000303
pauleb821182004-05-01 08:44:08 +0000304 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000305
306 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer,
307 peer->v_routeadv);
308
309 return 0;
310}
311
hassoe0701b72004-05-20 09:19:34 +0000312/* BGP Peer Down Cause */
paulfd79ac92004-10-13 05:06:08 +0000313const char *peer_down_str[] =
hassoe0701b72004-05-20 09:19:34 +0000314{
315 "",
316 "Router ID changed",
317 "Remote AS changed",
318 "Local AS change",
319 "Cluster ID changed",
320 "Confederation identifier changed",
321 "Confederation peer changed",
322 "RR client config change",
323 "RS client config change",
324 "Update source change",
325 "Address family activated",
326 "Admin. shutdown",
327 "User reset",
328 "BGP Notification received",
329 "BGP Notification send",
330 "Peer closed the session",
331 "Neighbor deleted",
332 "Peer-group add member",
333 "Peer-group delete member",
334 "Capability changed",
335 "Passive config change",
hasso93406d82005-02-02 14:40:33 +0000336 "Multihop config change",
337 "NSF peer closed the session"
hassoe0701b72004-05-20 09:19:34 +0000338};
339
paul94f2b392005-06-28 12:44:16 +0000340static int
hasso93406d82005-02-02 14:40:33 +0000341bgp_graceful_restart_timer_expire (struct thread *thread)
342{
343 struct peer *peer;
344 afi_t afi;
345 safi_t safi;
346
347 peer = THREAD_ARG (thread);
348 peer->t_gr_restart = NULL;
349
350 /* NSF delete stale route */
351 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
Denis Ovsienko0a281302011-07-17 19:33:21 +0400352 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
hasso93406d82005-02-02 14:40:33 +0000353 if (peer->nsf[afi][safi])
354 bgp_clear_stale_route (peer, afi, safi);
355
356 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
357 BGP_TIMER_OFF (peer->t_gr_stale);
358
359 if (BGP_DEBUG (events, EVENTS))
360 {
361 zlog_debug ("%s graceful restart timer expired", peer->host);
362 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
363 }
364
365 bgp_timer_set (peer);
366
367 return 0;
368}
369
paul94f2b392005-06-28 12:44:16 +0000370static int
hasso93406d82005-02-02 14:40:33 +0000371bgp_graceful_stale_timer_expire (struct thread *thread)
372{
373 struct peer *peer;
374 afi_t afi;
375 safi_t safi;
376
377 peer = THREAD_ARG (thread);
378 peer->t_gr_stale = NULL;
379
380 if (BGP_DEBUG (events, EVENTS))
381 zlog_debug ("%s graceful restart stalepath timer expired", peer->host);
382
383 /* NSF delete stale route */
384 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
Denis Ovsienko0a281302011-07-17 19:33:21 +0400385 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
hasso93406d82005-02-02 14:40:33 +0000386 if (peer->nsf[afi][safi])
387 bgp_clear_stale_route (peer, afi, safi);
388
389 return 0;
390}
391
paul200df112005-06-01 11:17:05 +0000392/* Called after event occured, this function change status and reset
393 read/write and timer thread. */
394void
395bgp_fsm_change_status (struct peer *peer, int status)
396{
397 bgp_dump_state (peer, peer->status, status);
398
Paul Jakmaf2c31ac2007-02-22 17:48:42 +0000399 /* Transition into Clearing or Deleted must /always/ clear all routes..
400 * (and must do so before actually changing into Deleted..
401 */
402 if (status >= Clearing)
Donald Sharp7ef42212015-03-30 06:32:52 -0700403 {
404 bgp_clear_route_all (peer);
405
406 /* If no route was queued for the clear-node processing, generate the
407 * completion event here. This is needed because if there are no routes
408 * to trigger the background clear-node thread, the event won't get
409 * generated and the peer would be stuck in Clearing. Note that this
410 * event is for the peer and helps the peer transition out of Clearing
411 * state; it should not be generated per (AFI,SAFI). The event is
412 * directly posted here without calling clear_node_complete() as we
413 * shouldn't do an extra unlock. This event will get processed after
414 * the state change that happens below, so peer will be in Clearing
415 * (or Deleted).
416 */
Paul Jakma782fb072015-09-15 16:17:22 +0100417 if (!work_queue_is_scheduled (peer->clear_node_queue))
Donald Sharp7ef42212015-03-30 06:32:52 -0700418 BGP_EVENT_ADD (peer, Clearing_Completed);
419 }
Paul Jakmaf2c31ac2007-02-22 17:48:42 +0000420
paul200df112005-06-01 11:17:05 +0000421 /* Preserve old status and change into new status. */
422 peer->ostatus = peer->status;
423 peer->status = status;
Paul Jakmaf2c31ac2007-02-22 17:48:42 +0000424
paul200df112005-06-01 11:17:05 +0000425 if (BGP_DEBUG (normal, NORMAL))
426 zlog_debug ("%s went from %s to %s",
427 peer->host,
428 LOOKUP (bgp_status_msg, peer->ostatus),
429 LOOKUP (bgp_status_msg, peer->status));
430}
431
Steve Hill3117b5c2009-07-28 17:50:00 +0100432/* Flush the event queue and ensure the peer is shut down */
Stephen Hemminger9e4ca892009-12-10 11:57:05 +0300433static int
Steve Hill3117b5c2009-07-28 17:50:00 +0100434bgp_clearing_completed (struct peer *peer)
435{
436 int rc = bgp_stop(peer);
437 BGP_EVENT_FLUSH (peer);
438
439 return rc;
440}
441
paul718e3742002-12-13 20:15:29 +0000442/* Administrative BGP peer stop event. */
Steve Hill3117b5c2009-07-28 17:50:00 +0100443/* May be called multiple times for the same peer */
paul718e3742002-12-13 20:15:29 +0000444int
445bgp_stop (struct peer *peer)
446{
paul718e3742002-12-13 20:15:29 +0000447 afi_t afi;
448 safi_t safi;
449 char orf_name[BUFSIZ];
450
Steve Hill3117b5c2009-07-28 17:50:00 +0100451 /* Can't do this in Clearing; events are used for state transitions */
452 if (peer->status != Clearing)
Paul Jakma2158ad22009-07-28 18:10:55 +0100453 {
454 /* Delete all existing events of the peer */
455 BGP_EVENT_FLUSH (peer);
456 }
Paul Jakmadcdf3992006-10-15 23:39:59 +0000457
paul718e3742002-12-13 20:15:29 +0000458 /* Increment Dropped count. */
459 if (peer->status == Established)
460 {
paul718e3742002-12-13 20:15:29 +0000461 peer->dropped++;
paul848973c2003-08-13 00:32:49 +0000462
463 /* bgp log-neighbor-changes of neighbor Down */
464 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
hassoe0701b72004-05-20 09:19:34 +0000465 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
466 peer_down_str [(int) peer->last_reset]);
paul848973c2003-08-13 00:32:49 +0000467
hasso93406d82005-02-02 14:40:33 +0000468 /* graceful restart */
469 if (peer->t_gr_stale)
470 {
471 BGP_TIMER_OFF (peer->t_gr_stale);
472 if (BGP_DEBUG (events, EVENTS))
473 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
474 }
475 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
476 {
477 if (BGP_DEBUG (events, EVENTS))
478 {
479 zlog_debug ("%s graceful restart timer started for %d sec",
480 peer->host, peer->v_gr_restart);
481 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
482 peer->host, peer->bgp->stalepath_time);
483 }
484 BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
485 peer->v_gr_restart);
486 BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
487 peer->bgp->stalepath_time);
488 }
489 else
490 {
491 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
492
493 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
Denis Ovsienko0a281302011-07-17 19:33:21 +0400494 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
hasso93406d82005-02-02 14:40:33 +0000495 peer->nsf[afi][safi] = 0;
496 }
497
paul848973c2003-08-13 00:32:49 +0000498 /* set last reset time */
Stephen Hemminger65957882010-01-15 16:22:10 +0300499 peer->resettime = peer->uptime = bgp_clock ();
paul848973c2003-08-13 00:32:49 +0000500
paul718e3742002-12-13 20:15:29 +0000501#ifdef HAVE_SNMP
502 bgpTrapBackwardTransition (peer);
503#endif /* HAVE_SNMP */
paul718e3742002-12-13 20:15:29 +0000504
hassof4184462005-02-01 20:13:16 +0000505 /* Reset peer synctime */
506 peer->synctime = 0;
hasso538621f2004-05-21 09:31:30 +0000507 }
paul718e3742002-12-13 20:15:29 +0000508
509 /* Stop read and write threads when exists. */
510 BGP_READ_OFF (peer->t_read);
511 BGP_WRITE_OFF (peer->t_write);
512
513 /* Stop all timers. */
514 BGP_TIMER_OFF (peer->t_start);
515 BGP_TIMER_OFF (peer->t_connect);
516 BGP_TIMER_OFF (peer->t_holdtime);
517 BGP_TIMER_OFF (peer->t_keepalive);
paul718e3742002-12-13 20:15:29 +0000518 BGP_TIMER_OFF (peer->t_routeadv);
519
paul718e3742002-12-13 20:15:29 +0000520 /* Stream reset. */
521 peer->packet_size = 0;
522
523 /* Clear input and output buffer. */
524 if (peer->ibuf)
525 stream_reset (peer->ibuf);
526 if (peer->work)
527 stream_reset (peer->work);
paul200df112005-06-01 11:17:05 +0000528 if (peer->obuf)
529 stream_fifo_clean (peer->obuf);
paul718e3742002-12-13 20:15:29 +0000530
pauleb821182004-05-01 08:44:08 +0000531 /* Close of file descriptor. */
532 if (peer->fd >= 0)
533 {
534 close (peer->fd);
535 peer->fd = -1;
536 }
paul718e3742002-12-13 20:15:29 +0000537
paul718e3742002-12-13 20:15:29 +0000538 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
539 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
540 {
hasso538621f2004-05-21 09:31:30 +0000541 /* Reset all negotiated variables */
542 peer->afc_nego[afi][safi] = 0;
543 peer->afc_adv[afi][safi] = 0;
544 peer->afc_recv[afi][safi] = 0;
545
paul718e3742002-12-13 20:15:29 +0000546 /* peer address family capability flags*/
547 peer->af_cap[afi][safi] = 0;
hasso538621f2004-05-21 09:31:30 +0000548
paul718e3742002-12-13 20:15:29 +0000549 /* peer address family status flags*/
550 peer->af_sflags[afi][safi] = 0;
hasso538621f2004-05-21 09:31:30 +0000551
paul718e3742002-12-13 20:15:29 +0000552 /* Received ORF prefix-filter */
553 peer->orf_plist[afi][safi] = NULL;
hasso538621f2004-05-21 09:31:30 +0000554
paul718e3742002-12-13 20:15:29 +0000555 /* ORF received prefix-filter pnt */
556 sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
David Lamparterc9c06d02015-04-13 10:21:35 +0200557 prefix_bgp_orf_remove_all (afi, orf_name);
paul718e3742002-12-13 20:15:29 +0000558 }
559
560 /* Reset keepalive and holdtime */
561 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
562 {
563 peer->v_keepalive = peer->keepalive;
564 peer->v_holdtime = peer->holdtime;
565 }
566 else
567 {
568 peer->v_keepalive = peer->bgp->default_keepalive;
569 peer->v_holdtime = peer->bgp->default_holdtime;
570 }
571
572 peer->update_time = 0;
573
574 /* Until we are sure that there is no problem about prefix count
575 this should be commented out.*/
576#if 0
577 /* Reset prefix count */
578 peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
579 peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
580 peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
581 peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
582 peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
583#endif /* 0 */
584
585 return 0;
586}
587
588/* BGP peer is stoped by the error. */
paul94f2b392005-06-28 12:44:16 +0000589static int
paul718e3742002-12-13 20:15:29 +0000590bgp_stop_with_error (struct peer *peer)
591{
592 /* Double start timer. */
593 peer->v_start *= 2;
594
595 /* Overflow check. */
596 if (peer->v_start >= (60 * 2))
597 peer->v_start = (60 * 2);
598
599 bgp_stop (peer);
600
601 return 0;
602}
603
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +0200604
605/* something went wrong, send notify and tear down */
606static int
607bgp_stop_with_notify (struct peer *peer, u_char code, u_char sub_code)
608{
609 /* Send notify to remote peer */
610 bgp_notify_send (peer, code, sub_code);
611
612 /* Sweep if it is temporary peer. */
613 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
614 {
615 zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
616 peer_delete (peer);
617 return -1;
618 }
619
620 /* Clear start timer value to default. */
621 peer->v_start = BGP_INIT_START_TIMER;
622
623 /* bgp_stop needs to be invoked while in Established state */
624 bgp_stop(peer);
625
626 return 0;
627}
628
629
paul718e3742002-12-13 20:15:29 +0000630/* TCP connection open. Next we send open message to remote peer. And
631 add read thread for reading open message. */
paul94f2b392005-06-28 12:44:16 +0000632static int
paul718e3742002-12-13 20:15:29 +0000633bgp_connect_success (struct peer *peer)
634{
pauleb821182004-05-01 08:44:08 +0000635 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000636 {
637 zlog_err ("bgp_connect_success peer's fd is negative value %d",
pauleb821182004-05-01 08:44:08 +0000638 peer->fd);
paul718e3742002-12-13 20:15:29 +0000639 return -1;
640 }
pauleb821182004-05-01 08:44:08 +0000641 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
paul718e3742002-12-13 20:15:29 +0000642
hassof4184462005-02-01 20:13:16 +0000643 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
644 bgp_getsockname (peer);
645
646 if (BGP_DEBUG (normal, NORMAL))
647 {
Jorge Boncompte [DTI2]682ca042012-04-10 16:57:27 +0200648 char buf1[SU_ADDRSTRLEN];
649
hassof4184462005-02-01 20:13:16 +0000650 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
651 zlog_debug ("%s open active, local address %s", peer->host,
652 sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
653 else
654 zlog_debug ("%s passive open", peer->host);
655 }
paul718e3742002-12-13 20:15:29 +0000656
Paul Jakma6556d8a2008-08-26 14:33:28 +0100657 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
paul718e3742002-12-13 20:15:29 +0000658 bgp_open_send (peer);
659
660 return 0;
661}
662
663/* TCP connect fail */
paul94f2b392005-06-28 12:44:16 +0000664static int
paul718e3742002-12-13 20:15:29 +0000665bgp_connect_fail (struct peer *peer)
666{
667 bgp_stop (peer);
668 return 0;
669}
670
671/* This function is the first starting point of all BGP connection. It
672 try to connect to remote peer with non-blocking IO. */
673int
674bgp_start (struct peer *peer)
675{
676 int status;
677
Paul Jakmaca058a32006-09-14 02:58:49 +0000678 if (BGP_PEER_START_SUPPRESSED (peer))
679 {
680 if (BGP_DEBUG (fsm, FSM))
681 plog_err (peer->log, "%s [FSM] Trying to start suppressed peer"
682 " - this is never supposed to happen!", peer->host);
683 return -1;
684 }
685
Paul Jakma33d5ab92006-07-02 11:01:50 +0000686 /* Scrub some information that might be left over from a previous,
687 * session
688 */
689 /* Connection information. */
690 if (peer->su_local)
691 {
692 sockunion_free (peer->su_local);
693 peer->su_local = NULL;
694 }
695
696 if (peer->su_remote)
697 {
698 sockunion_free (peer->su_remote);
699 peer->su_remote = NULL;
700 }
701
702 /* Clear remote router-id. */
703 peer->remote_id.s_addr = 0;
704
705 /* Clear peer capability flag. */
706 peer->cap = 0;
707
paul718e3742002-12-13 20:15:29 +0000708 /* If the peer is passive mode, force to move to Active mode. */
709 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
710 {
711 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
712 return 0;
713 }
714
715 status = bgp_connect (peer);
716
717 switch (status)
718 {
719 case connect_error:
720 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000721 plog_debug (peer->log, "%s [FSM] Connect error", peer->host);
paul718e3742002-12-13 20:15:29 +0000722 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
723 break;
724 case connect_success:
725 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000726 plog_debug (peer->log, "%s [FSM] Connect immediately success",
pauleb821182004-05-01 08:44:08 +0000727 peer->host);
paul718e3742002-12-13 20:15:29 +0000728 BGP_EVENT_ADD (peer, TCP_connection_open);
729 break;
730 case connect_in_progress:
731 /* To check nonblocking connect, we wait until socket is
732 readable or writable. */
733 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000734 plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result",
pauleb821182004-05-01 08:44:08 +0000735 peer->host);
736 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000737 {
738 zlog_err ("bgp_start peer's fd is negative value %d",
pauleb821182004-05-01 08:44:08 +0000739 peer->fd);
paul718e3742002-12-13 20:15:29 +0000740 return -1;
741 }
pauleb821182004-05-01 08:44:08 +0000742 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
743 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000744 break;
745 }
746 return 0;
747}
748
749/* Connect retry timer is expired when the peer status is Connect. */
paul94f2b392005-06-28 12:44:16 +0000750static int
paul718e3742002-12-13 20:15:29 +0000751bgp_reconnect (struct peer *peer)
752{
753 bgp_stop (peer);
754 bgp_start (peer);
755 return 0;
756}
757
paul94f2b392005-06-28 12:44:16 +0000758static int
paul718e3742002-12-13 20:15:29 +0000759bgp_fsm_open (struct peer *peer)
760{
761 /* Send keepalive and make keepalive timer */
762 bgp_keepalive_send (peer);
763
764 /* Reset holdtimer value. */
765 BGP_TIMER_OFF (peer->t_holdtime);
766
767 return 0;
768}
769
paul718e3742002-12-13 20:15:29 +0000770/* Keepalive send to peer. */
paul94f2b392005-06-28 12:44:16 +0000771static int
paul718e3742002-12-13 20:15:29 +0000772bgp_fsm_keepalive_expire (struct peer *peer)
773{
774 bgp_keepalive_send (peer);
775 return 0;
776}
777
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +0200778/* FSM error, unexpected event. This is error of BGP connection. So cut the
779 peer and change to Idle status. */
780static int
781bgp_fsm_event_error (struct peer *peer)
782{
783 plog_err (peer->log, "%s [FSM] unexpected packet received in state %s",
784 peer->host, LOOKUP (bgp_status_msg, peer->status));
785
786 return bgp_stop_with_notify (peer, BGP_NOTIFY_FSM_ERR, 0);
787}
788
paul718e3742002-12-13 20:15:29 +0000789/* Hold timer expire. This is error of BGP connection. So cut the
790 peer and change to Idle status. */
paul94f2b392005-06-28 12:44:16 +0000791static int
paul718e3742002-12-13 20:15:29 +0000792bgp_fsm_holdtime_expire (struct peer *peer)
793{
794 if (BGP_DEBUG (fsm, FSM))
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +0200795 plog_debug (peer->log, "%s [FSM] Hold timer expire", peer->host);
paul718e3742002-12-13 20:15:29 +0000796
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +0200797 return bgp_stop_with_notify (peer, BGP_NOTIFY_HOLD_ERR, 0);
paul718e3742002-12-13 20:15:29 +0000798}
799
800/* Status goes to Established. Send keepalive packet then make first
801 update information. */
paul94f2b392005-06-28 12:44:16 +0000802static int
paul718e3742002-12-13 20:15:29 +0000803bgp_establish (struct peer *peer)
804{
805 struct bgp_notify *notify;
806 afi_t afi;
807 safi_t safi;
hasso93406d82005-02-02 14:40:33 +0000808 int nsf_af_count = 0;
paul718e3742002-12-13 20:15:29 +0000809
810 /* Reset capability open status flag. */
811 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
812 SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
813
814 /* Clear last notification data. */
815 notify = &peer->notify;
816 if (notify->data)
817 XFREE (MTYPE_TMP, notify->data);
818 memset (notify, 0, sizeof (struct bgp_notify));
819
820 /* Clear start timer value to default. */
821 peer->v_start = BGP_INIT_START_TIMER;
822
823 /* Increment established count. */
824 peer->established++;
825 bgp_fsm_change_status (peer, Established);
paul848973c2003-08-13 00:32:49 +0000826
827 /* bgp log-neighbor-changes of neighbor Up */
828 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
829 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
830
hasso93406d82005-02-02 14:40:33 +0000831 /* graceful restart */
832 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
833 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
Denis Ovsienko0a281302011-07-17 19:33:21 +0400834 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
hasso93406d82005-02-02 14:40:33 +0000835 {
836 if (peer->afc_nego[afi][safi]
837 && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
838 && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
839 {
840 if (peer->nsf[afi][safi]
841 && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
842 bgp_clear_stale_route (peer, afi, safi);
843
844 peer->nsf[afi][safi] = 1;
845 nsf_af_count++;
846 }
847 else
848 {
849 if (peer->nsf[afi][safi])
850 bgp_clear_stale_route (peer, afi, safi);
851 peer->nsf[afi][safi] = 0;
852 }
853 }
854
855 if (nsf_af_count)
856 SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
857 else
858 {
859 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
860 if (peer->t_gr_stale)
861 {
862 BGP_TIMER_OFF (peer->t_gr_stale);
863 if (BGP_DEBUG (events, EVENTS))
864 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
865 }
866 }
867
868 if (peer->t_gr_restart)
869 {
870 BGP_TIMER_OFF (peer->t_gr_restart);
871 if (BGP_DEBUG (events, EVENTS))
872 zlog_debug ("%s graceful restart timer stopped", peer->host);
873 }
874
paul718e3742002-12-13 20:15:29 +0000875#ifdef HAVE_SNMP
876 bgpTrapEstablished (peer);
877#endif /* HAVE_SNMP */
878
879 /* Reset uptime, send keepalive, send current table. */
Stephen Hemminger65957882010-01-15 16:22:10 +0300880 peer->uptime = bgp_clock ();
paul718e3742002-12-13 20:15:29 +0000881
882 /* Send route-refresh when ORF is enabled */
883 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
884 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
885 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
886 {
887 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
888 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
889 REFRESH_IMMEDIATE, 0);
890 else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
891 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
892 REFRESH_IMMEDIATE, 0);
893 }
894
895 if (peer->v_keepalive)
896 bgp_keepalive_send (peer);
897
898 /* First update is deferred until ORF or ROUTE-REFRESH is received */
899 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
900 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
901 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
902 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
903 || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
904 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
905
906 bgp_announce_route_all (peer);
907
908 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1);
909
910 return 0;
911}
912
913/* Keepalive packet is received. */
paul94f2b392005-06-28 12:44:16 +0000914static int
paul718e3742002-12-13 20:15:29 +0000915bgp_fsm_keepalive (struct peer *peer)
916{
917 /* peer count update */
918 peer->keepalive_in++;
919
920 BGP_TIMER_OFF (peer->t_holdtime);
921 return 0;
922}
923
924/* Update packet is received. */
paul94f2b392005-06-28 12:44:16 +0000925static int
paul718e3742002-12-13 20:15:29 +0000926bgp_fsm_update (struct peer *peer)
927{
928 BGP_TIMER_OFF (peer->t_holdtime);
929 return 0;
930}
931
932/* This is empty event. */
paul94f2b392005-06-28 12:44:16 +0000933static int
paul718e3742002-12-13 20:15:29 +0000934bgp_ignore (struct peer *peer)
935{
936 if (BGP_DEBUG (fsm, FSM))
937 zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
938 return 0;
939}
David Lamparter6b0655a2014-06-04 06:53:35 +0200940
paul718e3742002-12-13 20:15:29 +0000941/* Finite State Machine structure */
Stephen Hemmingerfda1d3e2009-05-15 10:02:27 -0700942static const struct {
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800943 int (*func) (struct peer *);
paul718e3742002-12-13 20:15:29 +0000944 int next_state;
945} FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
946{
947 {
948 /* Idle state: In Idle state, all events other than BGP_Start is
949 ignored. With BGP_Start event, finite state machine calls
950 bgp_start(). */
951 {bgp_start, Connect}, /* BGP_Start */
952 {bgp_stop, Idle}, /* BGP_Stop */
953 {bgp_stop, Idle}, /* TCP_connection_open */
954 {bgp_stop, Idle}, /* TCP_connection_closed */
955 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
956 {bgp_stop, Idle}, /* TCP_fatal_error */
957 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
958 {bgp_ignore, Idle}, /* Hold_Timer_expired */
959 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
960 {bgp_ignore, Idle}, /* Receive_OPEN_message */
961 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
962 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
963 {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +0000964 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +0000965 },
966 {
967 /* Connect */
968 {bgp_ignore, Connect}, /* BGP_Start */
969 {bgp_stop, Idle}, /* BGP_Stop */
970 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
971 {bgp_stop, Idle}, /* TCP_connection_closed */
972 {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
973 {bgp_connect_fail, Idle}, /* TCP_fatal_error */
974 {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
975 {bgp_ignore, Idle}, /* Hold_Timer_expired */
976 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
977 {bgp_ignore, Idle}, /* Receive_OPEN_message */
978 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
979 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
980 {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +0000981 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +0000982 },
983 {
984 /* Active, */
985 {bgp_ignore, Active}, /* BGP_Start */
986 {bgp_stop, Idle}, /* BGP_Stop */
987 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
988 {bgp_stop, Idle}, /* TCP_connection_closed */
989 {bgp_ignore, Active}, /* TCP_connection_open_failed */
990 {bgp_ignore, Idle}, /* TCP_fatal_error */
991 {bgp_start, Connect}, /* ConnectRetry_timer_expired */
992 {bgp_ignore, Idle}, /* Hold_Timer_expired */
993 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
994 {bgp_ignore, Idle}, /* Receive_OPEN_message */
995 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
996 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
997 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +0000998 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +0000999 },
1000 {
1001 /* OpenSent, */
1002 {bgp_ignore, OpenSent}, /* BGP_Start */
1003 {bgp_stop, Idle}, /* BGP_Stop */
Paul Jakma536792c2007-06-22 19:11:14 +00001004 {bgp_stop, Active}, /* TCP_connection_open */
paul718e3742002-12-13 20:15:29 +00001005 {bgp_stop, Active}, /* TCP_connection_closed */
Paul Jakma536792c2007-06-22 19:11:14 +00001006 {bgp_stop, Active}, /* TCP_connection_open_failed */
1007 {bgp_stop, Active}, /* TCP_fatal_error */
paul718e3742002-12-13 20:15:29 +00001008 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1009 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1010 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1011 {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +02001012 {bgp_fsm_event_error, Idle}, /* Receive_KEEPALIVE_message */
1013 {bgp_fsm_event_error, Idle}, /* Receive_UPDATE_message */
paul718e3742002-12-13 20:15:29 +00001014 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +00001015 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +00001016 },
1017 {
1018 /* OpenConfirm, */
1019 {bgp_ignore, OpenConfirm}, /* BGP_Start */
1020 {bgp_stop, Idle}, /* BGP_Stop */
1021 {bgp_stop, Idle}, /* TCP_connection_open */
1022 {bgp_stop, Idle}, /* TCP_connection_closed */
1023 {bgp_stop, Idle}, /* TCP_connection_open_failed */
1024 {bgp_stop, Idle}, /* TCP_fatal_error */
1025 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1026 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1027 {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
1028 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1029 {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
1030 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1031 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +00001032 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +00001033 },
1034 {
1035 /* Established, */
Paul Jakmaca058a32006-09-14 02:58:49 +00001036 {bgp_ignore, Established}, /* BGP_Start */
1037 {bgp_stop, Clearing}, /* BGP_Stop */
1038 {bgp_stop, Clearing}, /* TCP_connection_open */
1039 {bgp_stop, Clearing}, /* TCP_connection_closed */
Steve Hill3117b5c2009-07-28 17:50:00 +01001040 {bgp_stop, Clearing}, /* TCP_connection_open_failed */
Paul Jakmaca058a32006-09-14 02:58:49 +00001041 {bgp_stop, Clearing}, /* TCP_fatal_error */
Steve Hill3117b5c2009-07-28 17:50:00 +01001042 {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
Paul Jakmaca058a32006-09-14 02:58:49 +00001043 {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */
paul718e3742002-12-13 20:15:29 +00001044 {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
Paul Jakmaca058a32006-09-14 02:58:49 +00001045 {bgp_stop, Clearing}, /* Receive_OPEN_message */
1046 {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
1047 {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
1048 {bgp_stop_with_error, Clearing}, /* Receive_NOTIFICATION_message */
1049 {bgp_ignore, Idle}, /* Clearing_Completed */
1050 },
1051 {
1052 /* Clearing, */
1053 {bgp_ignore, Clearing}, /* BGP_Start */
Steve Hill3117b5c2009-07-28 17:50:00 +01001054 {bgp_stop, Clearing}, /* BGP_Stop */
1055 {bgp_stop, Clearing}, /* TCP_connection_open */
1056 {bgp_stop, Clearing}, /* TCP_connection_closed */
1057 {bgp_stop, Clearing}, /* TCP_connection_open_failed */
1058 {bgp_stop, Clearing}, /* TCP_fatal_error */
1059 {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
1060 {bgp_stop, Clearing}, /* Hold_Timer_expired */
1061 {bgp_stop, Clearing}, /* KeepAlive_timer_expired */
1062 {bgp_stop, Clearing}, /* Receive_OPEN_message */
1063 {bgp_stop, Clearing}, /* Receive_KEEPALIVE_message */
1064 {bgp_stop, Clearing}, /* Receive_UPDATE_message */
1065 {bgp_stop, Clearing}, /* Receive_NOTIFICATION_message */
1066 {bgp_clearing_completed, Idle}, /* Clearing_Completed */
Paul Jakmaca058a32006-09-14 02:58:49 +00001067 },
1068 {
1069 /* Deleted, */
1070 {bgp_ignore, Deleted}, /* BGP_Start */
1071 {bgp_ignore, Deleted}, /* BGP_Stop */
1072 {bgp_ignore, Deleted}, /* TCP_connection_open */
1073 {bgp_ignore, Deleted}, /* TCP_connection_closed */
1074 {bgp_ignore, Deleted}, /* TCP_connection_open_failed */
1075 {bgp_ignore, Deleted}, /* TCP_fatal_error */
1076 {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */
1077 {bgp_ignore, Deleted}, /* Hold_Timer_expired */
1078 {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */
1079 {bgp_ignore, Deleted}, /* Receive_OPEN_message */
1080 {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */
1081 {bgp_ignore, Deleted}, /* Receive_UPDATE_message */
1082 {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */
1083 {bgp_ignore, Deleted}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +00001084 },
1085};
1086
paulfd79ac92004-10-13 05:06:08 +00001087static const char *bgp_event_str[] =
paul718e3742002-12-13 20:15:29 +00001088{
1089 NULL,
1090 "BGP_Start",
1091 "BGP_Stop",
1092 "TCP_connection_open",
1093 "TCP_connection_closed",
1094 "TCP_connection_open_failed",
1095 "TCP_fatal_error",
1096 "ConnectRetry_timer_expired",
1097 "Hold_Timer_expired",
1098 "KeepAlive_timer_expired",
1099 "Receive_OPEN_message",
1100 "Receive_KEEPALIVE_message",
1101 "Receive_UPDATE_message",
Paul Jakmaca058a32006-09-14 02:58:49 +00001102 "Receive_NOTIFICATION_message",
1103 "Clearing_Completed",
paul718e3742002-12-13 20:15:29 +00001104};
1105
1106/* Execute event process. */
1107int
1108bgp_event (struct thread *thread)
1109{
Paul Jakmaca058a32006-09-14 02:58:49 +00001110 int ret = 0;
paul718e3742002-12-13 20:15:29 +00001111 int event;
1112 int next;
1113 struct peer *peer;
1114
1115 peer = THREAD_ARG (thread);
1116 event = THREAD_VAL (thread);
1117
1118 /* Logging this event. */
1119 next = FSM [peer->status -1][event - 1].next_state;
1120
Paul Jakmaca058a32006-09-14 02:58:49 +00001121 if (BGP_DEBUG (fsm, FSM) && peer->status != next)
ajs8c2e2002004-12-08 20:08:54 +00001122 plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
paul718e3742002-12-13 20:15:29 +00001123 bgp_event_str[event],
1124 LOOKUP (bgp_status_msg, peer->status),
1125 LOOKUP (bgp_status_msg, next));
paul718e3742002-12-13 20:15:29 +00001126
1127 /* Call function. */
Paul Jakmaca058a32006-09-14 02:58:49 +00001128 if (FSM [peer->status -1][event - 1].func)
1129 ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
paul718e3742002-12-13 20:15:29 +00001130
1131 /* When function do not want proceed next job return -1. */
paul200df112005-06-01 11:17:05 +00001132 if (ret >= 0)
1133 {
1134 /* If status is changed. */
1135 if (next != peer->status)
Paul Jakmaf2c31ac2007-02-22 17:48:42 +00001136 bgp_fsm_change_status (peer, next);
Paul Jakma95fdcd82006-12-08 00:31:22 +00001137
paul200df112005-06-01 11:17:05 +00001138 /* Make sure timer is set. */
1139 bgp_timer_set (peer);
1140 }
1141
paul200df112005-06-01 11:17:05 +00001142 return ret;
paul718e3742002-12-13 20:15:29 +00001143}