blob: 9ac333526e748c57af1846e610f9f3155f765384 [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);
104 BGP_TIMER_OFF (peer->t_asorig);
105 BGP_TIMER_OFF (peer->t_routeadv);
106 break;
107
108 case Connect:
109 /* After start timer is expired, the peer moves to Connnect
110 status. Make sure start timer is off and connect timer is
111 on. */
112 BGP_TIMER_OFF (peer->t_start);
113 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
114 BGP_TIMER_OFF (peer->t_holdtime);
115 BGP_TIMER_OFF (peer->t_keepalive);
116 BGP_TIMER_OFF (peer->t_asorig);
117 BGP_TIMER_OFF (peer->t_routeadv);
118 break;
119
120 case Active:
121 /* Active is waiting connection from remote peer. And if
122 connect timer is expired, change status to Connect. */
123 BGP_TIMER_OFF (peer->t_start);
124 /* If peer is passive mode, do not set connect timer. */
hasso93406d82005-02-02 14:40:33 +0000125 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)
126 || CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
paul718e3742002-12-13 20:15:29 +0000127 {
128 BGP_TIMER_OFF (peer->t_connect);
129 }
130 else
131 {
132 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
133 }
134 BGP_TIMER_OFF (peer->t_holdtime);
135 BGP_TIMER_OFF (peer->t_keepalive);
136 BGP_TIMER_OFF (peer->t_asorig);
137 BGP_TIMER_OFF (peer->t_routeadv);
138 break;
139
140 case OpenSent:
141 /* OpenSent status. */
142 BGP_TIMER_OFF (peer->t_start);
143 BGP_TIMER_OFF (peer->t_connect);
144 if (peer->v_holdtime != 0)
145 {
146 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
147 peer->v_holdtime);
148 }
149 else
150 {
151 BGP_TIMER_OFF (peer->t_holdtime);
152 }
153 BGP_TIMER_OFF (peer->t_keepalive);
154 BGP_TIMER_OFF (peer->t_asorig);
155 BGP_TIMER_OFF (peer->t_routeadv);
156 break;
157
158 case OpenConfirm:
159 /* OpenConfirm status. */
160 BGP_TIMER_OFF (peer->t_start);
161 BGP_TIMER_OFF (peer->t_connect);
162
163 /* If the negotiated Hold Time value is zero, then the Hold Time
164 timer and KeepAlive timers are not started. */
165 if (peer->v_holdtime == 0)
166 {
167 BGP_TIMER_OFF (peer->t_holdtime);
168 BGP_TIMER_OFF (peer->t_keepalive);
169 }
170 else
171 {
172 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
173 peer->v_holdtime);
174 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
175 peer->v_keepalive);
176 }
177 BGP_TIMER_OFF (peer->t_asorig);
178 BGP_TIMER_OFF (peer->t_routeadv);
179 break;
180
181 case Established:
182 /* In Established status start and connect timer is turned
183 off. */
184 BGP_TIMER_OFF (peer->t_start);
185 BGP_TIMER_OFF (peer->t_connect);
186
187 /* Same as OpenConfirm, if holdtime is zero then both holdtime
188 and keepalive must be turned off. */
189 if (peer->v_holdtime == 0)
190 {
191 BGP_TIMER_OFF (peer->t_holdtime);
192 BGP_TIMER_OFF (peer->t_keepalive);
193 }
194 else
195 {
196 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
197 peer->v_holdtime);
198 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
199 peer->v_keepalive);
200 }
201 BGP_TIMER_OFF (peer->t_asorig);
202 break;
Paul Jakmaca058a32006-09-14 02:58:49 +0000203 case Deleted:
204 BGP_TIMER_OFF (peer->t_gr_restart);
205 BGP_TIMER_OFF (peer->t_gr_stale);
206 BGP_TIMER_OFF (peer->t_pmax_restart);
207 case Clearing:
208 BGP_TIMER_OFF (peer->t_start);
209 BGP_TIMER_OFF (peer->t_connect);
210 BGP_TIMER_OFF (peer->t_holdtime);
211 BGP_TIMER_OFF (peer->t_keepalive);
212 BGP_TIMER_OFF (peer->t_asorig);
213 BGP_TIMER_OFF (peer->t_routeadv);
paul718e3742002-12-13 20:15:29 +0000214 }
215}
216
217/* BGP start timer. This function set BGP_Start event to thread value
218 and process event. */
219static int
220bgp_start_timer (struct thread *thread)
221{
222 struct peer *peer;
223
224 peer = THREAD_ARG (thread);
225 peer->t_start = NULL;
226
227 if (BGP_DEBUG (fsm, FSM))
228 zlog (peer->log, LOG_DEBUG,
229 "%s [FSM] Timer (start timer expire).", peer->host);
230
231 THREAD_VAL (thread) = BGP_Start;
paul200df112005-06-01 11:17:05 +0000232 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000233
234 return 0;
235}
236
237/* BGP connect retry timer. */
238static int
239bgp_connect_timer (struct thread *thread)
240{
241 struct peer *peer;
242
243 peer = THREAD_ARG (thread);
244 peer->t_connect = NULL;
245
246 if (BGP_DEBUG (fsm, FSM))
247 zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
248 peer->host);
249
250 THREAD_VAL (thread) = ConnectRetry_timer_expired;
paul200df112005-06-01 11:17:05 +0000251 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000252
253 return 0;
254}
255
256/* BGP holdtime timer. */
257static int
258bgp_holdtime_timer (struct thread *thread)
259{
260 struct peer *peer;
261
262 peer = THREAD_ARG (thread);
263 peer->t_holdtime = NULL;
264
265 if (BGP_DEBUG (fsm, FSM))
266 zlog (peer->log, LOG_DEBUG,
267 "%s [FSM] Timer (holdtime timer expire)",
268 peer->host);
269
270 THREAD_VAL (thread) = Hold_Timer_expired;
paul200df112005-06-01 11:17:05 +0000271 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000272
273 return 0;
274}
275
276/* BGP keepalive fire ! */
277static int
278bgp_keepalive_timer (struct thread *thread)
279{
280 struct peer *peer;
281
282 peer = THREAD_ARG (thread);
283 peer->t_keepalive = NULL;
284
285 if (BGP_DEBUG (fsm, FSM))
286 zlog (peer->log, LOG_DEBUG,
287 "%s [FSM] Timer (keepalive timer expire)",
288 peer->host);
289
290 THREAD_VAL (thread) = KeepAlive_timer_expired;
paul200df112005-06-01 11:17:05 +0000291 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000292
293 return 0;
294}
295
paul94f2b392005-06-28 12:44:16 +0000296static int
paul718e3742002-12-13 20:15:29 +0000297bgp_routeadv_timer (struct thread *thread)
298{
299 struct peer *peer;
300
301 peer = THREAD_ARG (thread);
302 peer->t_routeadv = NULL;
303
304 if (BGP_DEBUG (fsm, FSM))
305 zlog (peer->log, LOG_DEBUG,
306 "%s [FSM] Timer (routeadv timer expire)",
307 peer->host);
308
Stephen Hemminger65957882010-01-15 16:22:10 +0300309 peer->synctime = bgp_clock ();
paul718e3742002-12-13 20:15:29 +0000310
pauleb821182004-05-01 08:44:08 +0000311 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000312
313 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer,
314 peer->v_routeadv);
315
316 return 0;
317}
318
hassoe0701b72004-05-20 09:19:34 +0000319/* BGP Peer Down Cause */
paulfd79ac92004-10-13 05:06:08 +0000320const char *peer_down_str[] =
hassoe0701b72004-05-20 09:19:34 +0000321{
322 "",
323 "Router ID changed",
324 "Remote AS changed",
325 "Local AS change",
326 "Cluster ID changed",
327 "Confederation identifier changed",
328 "Confederation peer changed",
329 "RR client config change",
330 "RS client config change",
331 "Update source change",
332 "Address family activated",
333 "Admin. shutdown",
334 "User reset",
335 "BGP Notification received",
336 "BGP Notification send",
337 "Peer closed the session",
338 "Neighbor deleted",
339 "Peer-group add member",
340 "Peer-group delete member",
341 "Capability changed",
342 "Passive config change",
hasso93406d82005-02-02 14:40:33 +0000343 "Multihop config change",
344 "NSF peer closed the session"
hassoe0701b72004-05-20 09:19:34 +0000345};
346
paul94f2b392005-06-28 12:44:16 +0000347static int
hasso93406d82005-02-02 14:40:33 +0000348bgp_graceful_restart_timer_expire (struct thread *thread)
349{
350 struct peer *peer;
351 afi_t afi;
352 safi_t safi;
353
354 peer = THREAD_ARG (thread);
355 peer->t_gr_restart = NULL;
356
357 /* NSF delete stale route */
358 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
Denis Ovsienko0a281302011-07-17 19:33:21 +0400359 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
hasso93406d82005-02-02 14:40:33 +0000360 if (peer->nsf[afi][safi])
361 bgp_clear_stale_route (peer, afi, safi);
362
363 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
364 BGP_TIMER_OFF (peer->t_gr_stale);
365
366 if (BGP_DEBUG (events, EVENTS))
367 {
368 zlog_debug ("%s graceful restart timer expired", peer->host);
369 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
370 }
371
372 bgp_timer_set (peer);
373
374 return 0;
375}
376
paul94f2b392005-06-28 12:44:16 +0000377static int
hasso93406d82005-02-02 14:40:33 +0000378bgp_graceful_stale_timer_expire (struct thread *thread)
379{
380 struct peer *peer;
381 afi_t afi;
382 safi_t safi;
383
384 peer = THREAD_ARG (thread);
385 peer->t_gr_stale = NULL;
386
387 if (BGP_DEBUG (events, EVENTS))
388 zlog_debug ("%s graceful restart stalepath timer expired", peer->host);
389
390 /* NSF delete stale route */
391 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
Denis Ovsienko0a281302011-07-17 19:33:21 +0400392 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
hasso93406d82005-02-02 14:40:33 +0000393 if (peer->nsf[afi][safi])
394 bgp_clear_stale_route (peer, afi, safi);
395
396 return 0;
397}
398
paul200df112005-06-01 11:17:05 +0000399/* Called after event occured, this function change status and reset
400 read/write and timer thread. */
401void
402bgp_fsm_change_status (struct peer *peer, int status)
403{
404 bgp_dump_state (peer, peer->status, status);
405
Paul Jakmaf2c31ac2007-02-22 17:48:42 +0000406 /* Transition into Clearing or Deleted must /always/ clear all routes..
407 * (and must do so before actually changing into Deleted..
408 */
409 if (status >= Clearing)
Donald Sharp7ef42212015-03-30 06:32:52 -0700410 {
411 bgp_clear_route_all (peer);
412
413 /* If no route was queued for the clear-node processing, generate the
414 * completion event here. This is needed because if there are no routes
415 * to trigger the background clear-node thread, the event won't get
416 * generated and the peer would be stuck in Clearing. Note that this
417 * event is for the peer and helps the peer transition out of Clearing
418 * state; it should not be generated per (AFI,SAFI). The event is
419 * directly posted here without calling clear_node_complete() as we
420 * shouldn't do an extra unlock. This event will get processed after
421 * the state change that happens below, so peer will be in Clearing
422 * (or Deleted).
423 */
Paul Jakma782fb072015-09-15 16:17:22 +0100424 if (!work_queue_is_scheduled (peer->clear_node_queue))
Donald Sharp7ef42212015-03-30 06:32:52 -0700425 BGP_EVENT_ADD (peer, Clearing_Completed);
426 }
Paul Jakmaf2c31ac2007-02-22 17:48:42 +0000427
paul200df112005-06-01 11:17:05 +0000428 /* Preserve old status and change into new status. */
429 peer->ostatus = peer->status;
430 peer->status = status;
Paul Jakmaf2c31ac2007-02-22 17:48:42 +0000431
paul200df112005-06-01 11:17:05 +0000432 if (BGP_DEBUG (normal, NORMAL))
433 zlog_debug ("%s went from %s to %s",
434 peer->host,
435 LOOKUP (bgp_status_msg, peer->ostatus),
436 LOOKUP (bgp_status_msg, peer->status));
437}
438
Steve Hill3117b5c2009-07-28 17:50:00 +0100439/* Flush the event queue and ensure the peer is shut down */
Stephen Hemminger9e4ca892009-12-10 11:57:05 +0300440static int
Steve Hill3117b5c2009-07-28 17:50:00 +0100441bgp_clearing_completed (struct peer *peer)
442{
443 int rc = bgp_stop(peer);
444 BGP_EVENT_FLUSH (peer);
445
446 return rc;
447}
448
paul718e3742002-12-13 20:15:29 +0000449/* Administrative BGP peer stop event. */
Steve Hill3117b5c2009-07-28 17:50:00 +0100450/* May be called multiple times for the same peer */
paul718e3742002-12-13 20:15:29 +0000451int
452bgp_stop (struct peer *peer)
453{
paul718e3742002-12-13 20:15:29 +0000454 afi_t afi;
455 safi_t safi;
456 char orf_name[BUFSIZ];
457
Steve Hill3117b5c2009-07-28 17:50:00 +0100458 /* Can't do this in Clearing; events are used for state transitions */
459 if (peer->status != Clearing)
Paul Jakma2158ad22009-07-28 18:10:55 +0100460 {
461 /* Delete all existing events of the peer */
462 BGP_EVENT_FLUSH (peer);
463 }
Paul Jakmadcdf3992006-10-15 23:39:59 +0000464
paul718e3742002-12-13 20:15:29 +0000465 /* Increment Dropped count. */
466 if (peer->status == Established)
467 {
paul718e3742002-12-13 20:15:29 +0000468 peer->dropped++;
paul848973c2003-08-13 00:32:49 +0000469
470 /* bgp log-neighbor-changes of neighbor Down */
471 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
hassoe0701b72004-05-20 09:19:34 +0000472 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
473 peer_down_str [(int) peer->last_reset]);
paul848973c2003-08-13 00:32:49 +0000474
hasso93406d82005-02-02 14:40:33 +0000475 /* graceful restart */
476 if (peer->t_gr_stale)
477 {
478 BGP_TIMER_OFF (peer->t_gr_stale);
479 if (BGP_DEBUG (events, EVENTS))
480 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
481 }
482 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
483 {
484 if (BGP_DEBUG (events, EVENTS))
485 {
486 zlog_debug ("%s graceful restart timer started for %d sec",
487 peer->host, peer->v_gr_restart);
488 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
489 peer->host, peer->bgp->stalepath_time);
490 }
491 BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
492 peer->v_gr_restart);
493 BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
494 peer->bgp->stalepath_time);
495 }
496 else
497 {
498 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
499
500 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
Denis Ovsienko0a281302011-07-17 19:33:21 +0400501 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
hasso93406d82005-02-02 14:40:33 +0000502 peer->nsf[afi][safi] = 0;
503 }
504
paul848973c2003-08-13 00:32:49 +0000505 /* set last reset time */
Stephen Hemminger65957882010-01-15 16:22:10 +0300506 peer->resettime = peer->uptime = bgp_clock ();
paul848973c2003-08-13 00:32:49 +0000507
paul718e3742002-12-13 20:15:29 +0000508#ifdef HAVE_SNMP
509 bgpTrapBackwardTransition (peer);
510#endif /* HAVE_SNMP */
paul718e3742002-12-13 20:15:29 +0000511
hassof4184462005-02-01 20:13:16 +0000512 /* Reset peer synctime */
513 peer->synctime = 0;
hasso538621f2004-05-21 09:31:30 +0000514 }
paul718e3742002-12-13 20:15:29 +0000515
516 /* Stop read and write threads when exists. */
517 BGP_READ_OFF (peer->t_read);
518 BGP_WRITE_OFF (peer->t_write);
519
520 /* Stop all timers. */
521 BGP_TIMER_OFF (peer->t_start);
522 BGP_TIMER_OFF (peer->t_connect);
523 BGP_TIMER_OFF (peer->t_holdtime);
524 BGP_TIMER_OFF (peer->t_keepalive);
525 BGP_TIMER_OFF (peer->t_asorig);
526 BGP_TIMER_OFF (peer->t_routeadv);
527
paul718e3742002-12-13 20:15:29 +0000528 /* Stream reset. */
529 peer->packet_size = 0;
530
531 /* Clear input and output buffer. */
532 if (peer->ibuf)
533 stream_reset (peer->ibuf);
534 if (peer->work)
535 stream_reset (peer->work);
paul200df112005-06-01 11:17:05 +0000536 if (peer->obuf)
537 stream_fifo_clean (peer->obuf);
paul718e3742002-12-13 20:15:29 +0000538
pauleb821182004-05-01 08:44:08 +0000539 /* Close of file descriptor. */
540 if (peer->fd >= 0)
541 {
542 close (peer->fd);
543 peer->fd = -1;
544 }
paul718e3742002-12-13 20:15:29 +0000545
paul718e3742002-12-13 20:15:29 +0000546 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
547 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
548 {
hasso538621f2004-05-21 09:31:30 +0000549 /* Reset all negotiated variables */
550 peer->afc_nego[afi][safi] = 0;
551 peer->afc_adv[afi][safi] = 0;
552 peer->afc_recv[afi][safi] = 0;
553
paul718e3742002-12-13 20:15:29 +0000554 /* peer address family capability flags*/
555 peer->af_cap[afi][safi] = 0;
hasso538621f2004-05-21 09:31:30 +0000556
paul718e3742002-12-13 20:15:29 +0000557 /* peer address family status flags*/
558 peer->af_sflags[afi][safi] = 0;
hasso538621f2004-05-21 09:31:30 +0000559
paul718e3742002-12-13 20:15:29 +0000560 /* Received ORF prefix-filter */
561 peer->orf_plist[afi][safi] = NULL;
hasso538621f2004-05-21 09:31:30 +0000562
paul718e3742002-12-13 20:15:29 +0000563 /* ORF received prefix-filter pnt */
564 sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
David Lamparterc9c06d02015-04-13 10:21:35 +0200565 prefix_bgp_orf_remove_all (afi, orf_name);
paul718e3742002-12-13 20:15:29 +0000566 }
567
568 /* Reset keepalive and holdtime */
569 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
570 {
571 peer->v_keepalive = peer->keepalive;
572 peer->v_holdtime = peer->holdtime;
573 }
574 else
575 {
576 peer->v_keepalive = peer->bgp->default_keepalive;
577 peer->v_holdtime = peer->bgp->default_holdtime;
578 }
579
580 peer->update_time = 0;
581
582 /* Until we are sure that there is no problem about prefix count
583 this should be commented out.*/
584#if 0
585 /* Reset prefix count */
586 peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
587 peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
588 peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
589 peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
590 peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
591#endif /* 0 */
592
593 return 0;
594}
595
596/* BGP peer is stoped by the error. */
paul94f2b392005-06-28 12:44:16 +0000597static int
paul718e3742002-12-13 20:15:29 +0000598bgp_stop_with_error (struct peer *peer)
599{
600 /* Double start timer. */
601 peer->v_start *= 2;
602
603 /* Overflow check. */
604 if (peer->v_start >= (60 * 2))
605 peer->v_start = (60 * 2);
606
607 bgp_stop (peer);
608
609 return 0;
610}
611
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +0200612
613/* something went wrong, send notify and tear down */
614static int
615bgp_stop_with_notify (struct peer *peer, u_char code, u_char sub_code)
616{
617 /* Send notify to remote peer */
618 bgp_notify_send (peer, code, sub_code);
619
620 /* Sweep if it is temporary peer. */
621 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
622 {
623 zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
624 peer_delete (peer);
625 return -1;
626 }
627
628 /* Clear start timer value to default. */
629 peer->v_start = BGP_INIT_START_TIMER;
630
631 /* bgp_stop needs to be invoked while in Established state */
632 bgp_stop(peer);
633
634 return 0;
635}
636
637
paul718e3742002-12-13 20:15:29 +0000638/* TCP connection open. Next we send open message to remote peer. And
639 add read thread for reading open message. */
paul94f2b392005-06-28 12:44:16 +0000640static int
paul718e3742002-12-13 20:15:29 +0000641bgp_connect_success (struct peer *peer)
642{
pauleb821182004-05-01 08:44:08 +0000643 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000644 {
645 zlog_err ("bgp_connect_success peer's fd is negative value %d",
pauleb821182004-05-01 08:44:08 +0000646 peer->fd);
paul718e3742002-12-13 20:15:29 +0000647 return -1;
648 }
pauleb821182004-05-01 08:44:08 +0000649 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
paul718e3742002-12-13 20:15:29 +0000650
hassof4184462005-02-01 20:13:16 +0000651 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
652 bgp_getsockname (peer);
653
654 if (BGP_DEBUG (normal, NORMAL))
655 {
Jorge Boncompte [DTI2]682ca042012-04-10 16:57:27 +0200656 char buf1[SU_ADDRSTRLEN];
657
hassof4184462005-02-01 20:13:16 +0000658 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
659 zlog_debug ("%s open active, local address %s", peer->host,
660 sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
661 else
662 zlog_debug ("%s passive open", peer->host);
663 }
paul718e3742002-12-13 20:15:29 +0000664
Paul Jakma6556d8a2008-08-26 14:33:28 +0100665 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
paul718e3742002-12-13 20:15:29 +0000666 bgp_open_send (peer);
667
668 return 0;
669}
670
671/* TCP connect fail */
paul94f2b392005-06-28 12:44:16 +0000672static int
paul718e3742002-12-13 20:15:29 +0000673bgp_connect_fail (struct peer *peer)
674{
675 bgp_stop (peer);
676 return 0;
677}
678
679/* This function is the first starting point of all BGP connection. It
680 try to connect to remote peer with non-blocking IO. */
681int
682bgp_start (struct peer *peer)
683{
684 int status;
685
Paul Jakmaca058a32006-09-14 02:58:49 +0000686 if (BGP_PEER_START_SUPPRESSED (peer))
687 {
688 if (BGP_DEBUG (fsm, FSM))
689 plog_err (peer->log, "%s [FSM] Trying to start suppressed peer"
690 " - this is never supposed to happen!", peer->host);
691 return -1;
692 }
693
Paul Jakma33d5ab92006-07-02 11:01:50 +0000694 /* Scrub some information that might be left over from a previous,
695 * session
696 */
697 /* Connection information. */
698 if (peer->su_local)
699 {
700 sockunion_free (peer->su_local);
701 peer->su_local = NULL;
702 }
703
704 if (peer->su_remote)
705 {
706 sockunion_free (peer->su_remote);
707 peer->su_remote = NULL;
708 }
709
710 /* Clear remote router-id. */
711 peer->remote_id.s_addr = 0;
712
713 /* Clear peer capability flag. */
714 peer->cap = 0;
715
paul718e3742002-12-13 20:15:29 +0000716 /* If the peer is passive mode, force to move to Active mode. */
717 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
718 {
719 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
720 return 0;
721 }
722
723 status = bgp_connect (peer);
724
725 switch (status)
726 {
727 case connect_error:
728 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000729 plog_debug (peer->log, "%s [FSM] Connect error", peer->host);
paul718e3742002-12-13 20:15:29 +0000730 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
731 break;
732 case connect_success:
733 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000734 plog_debug (peer->log, "%s [FSM] Connect immediately success",
pauleb821182004-05-01 08:44:08 +0000735 peer->host);
paul718e3742002-12-13 20:15:29 +0000736 BGP_EVENT_ADD (peer, TCP_connection_open);
737 break;
738 case connect_in_progress:
739 /* To check nonblocking connect, we wait until socket is
740 readable or writable. */
741 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000742 plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result",
pauleb821182004-05-01 08:44:08 +0000743 peer->host);
744 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000745 {
746 zlog_err ("bgp_start peer's fd is negative value %d",
pauleb821182004-05-01 08:44:08 +0000747 peer->fd);
paul718e3742002-12-13 20:15:29 +0000748 return -1;
749 }
pauleb821182004-05-01 08:44:08 +0000750 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
751 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000752 break;
753 }
754 return 0;
755}
756
757/* Connect retry timer is expired when the peer status is Connect. */
paul94f2b392005-06-28 12:44:16 +0000758static int
paul718e3742002-12-13 20:15:29 +0000759bgp_reconnect (struct peer *peer)
760{
761 bgp_stop (peer);
762 bgp_start (peer);
763 return 0;
764}
765
paul94f2b392005-06-28 12:44:16 +0000766static int
paul718e3742002-12-13 20:15:29 +0000767bgp_fsm_open (struct peer *peer)
768{
769 /* Send keepalive and make keepalive timer */
770 bgp_keepalive_send (peer);
771
772 /* Reset holdtimer value. */
773 BGP_TIMER_OFF (peer->t_holdtime);
774
775 return 0;
776}
777
paul718e3742002-12-13 20:15:29 +0000778/* Keepalive send to peer. */
paul94f2b392005-06-28 12:44:16 +0000779static int
paul718e3742002-12-13 20:15:29 +0000780bgp_fsm_keepalive_expire (struct peer *peer)
781{
782 bgp_keepalive_send (peer);
783 return 0;
784}
785
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +0200786/* FSM error, unexpected event. This is error of BGP connection. So cut the
787 peer and change to Idle status. */
788static int
789bgp_fsm_event_error (struct peer *peer)
790{
791 plog_err (peer->log, "%s [FSM] unexpected packet received in state %s",
792 peer->host, LOOKUP (bgp_status_msg, peer->status));
793
794 return bgp_stop_with_notify (peer, BGP_NOTIFY_FSM_ERR, 0);
795}
796
paul718e3742002-12-13 20:15:29 +0000797/* Hold timer expire. This is error of BGP connection. So cut the
798 peer and change to Idle status. */
paul94f2b392005-06-28 12:44:16 +0000799static int
paul718e3742002-12-13 20:15:29 +0000800bgp_fsm_holdtime_expire (struct peer *peer)
801{
802 if (BGP_DEBUG (fsm, FSM))
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +0200803 plog_debug (peer->log, "%s [FSM] Hold timer expire", peer->host);
paul718e3742002-12-13 20:15:29 +0000804
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +0200805 return bgp_stop_with_notify (peer, BGP_NOTIFY_HOLD_ERR, 0);
paul718e3742002-12-13 20:15:29 +0000806}
807
808/* Status goes to Established. Send keepalive packet then make first
809 update information. */
paul94f2b392005-06-28 12:44:16 +0000810static int
paul718e3742002-12-13 20:15:29 +0000811bgp_establish (struct peer *peer)
812{
813 struct bgp_notify *notify;
814 afi_t afi;
815 safi_t safi;
hasso93406d82005-02-02 14:40:33 +0000816 int nsf_af_count = 0;
paul718e3742002-12-13 20:15:29 +0000817
818 /* Reset capability open status flag. */
819 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
820 SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
821
822 /* Clear last notification data. */
823 notify = &peer->notify;
824 if (notify->data)
825 XFREE (MTYPE_TMP, notify->data);
826 memset (notify, 0, sizeof (struct bgp_notify));
827
828 /* Clear start timer value to default. */
829 peer->v_start = BGP_INIT_START_TIMER;
830
831 /* Increment established count. */
832 peer->established++;
833 bgp_fsm_change_status (peer, Established);
paul848973c2003-08-13 00:32:49 +0000834
835 /* bgp log-neighbor-changes of neighbor Up */
836 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
837 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
838
hasso93406d82005-02-02 14:40:33 +0000839 /* graceful restart */
840 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
841 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
Denis Ovsienko0a281302011-07-17 19:33:21 +0400842 for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; safi++)
hasso93406d82005-02-02 14:40:33 +0000843 {
844 if (peer->afc_nego[afi][safi]
845 && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
846 && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
847 {
848 if (peer->nsf[afi][safi]
849 && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
850 bgp_clear_stale_route (peer, afi, safi);
851
852 peer->nsf[afi][safi] = 1;
853 nsf_af_count++;
854 }
855 else
856 {
857 if (peer->nsf[afi][safi])
858 bgp_clear_stale_route (peer, afi, safi);
859 peer->nsf[afi][safi] = 0;
860 }
861 }
862
863 if (nsf_af_count)
864 SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
865 else
866 {
867 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
868 if (peer->t_gr_stale)
869 {
870 BGP_TIMER_OFF (peer->t_gr_stale);
871 if (BGP_DEBUG (events, EVENTS))
872 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
873 }
874 }
875
876 if (peer->t_gr_restart)
877 {
878 BGP_TIMER_OFF (peer->t_gr_restart);
879 if (BGP_DEBUG (events, EVENTS))
880 zlog_debug ("%s graceful restart timer stopped", peer->host);
881 }
882
paul718e3742002-12-13 20:15:29 +0000883#ifdef HAVE_SNMP
884 bgpTrapEstablished (peer);
885#endif /* HAVE_SNMP */
886
887 /* Reset uptime, send keepalive, send current table. */
Stephen Hemminger65957882010-01-15 16:22:10 +0300888 peer->uptime = bgp_clock ();
paul718e3742002-12-13 20:15:29 +0000889
890 /* Send route-refresh when ORF is enabled */
891 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
892 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
893 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
894 {
895 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
896 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
897 REFRESH_IMMEDIATE, 0);
898 else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
899 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
900 REFRESH_IMMEDIATE, 0);
901 }
902
903 if (peer->v_keepalive)
904 bgp_keepalive_send (peer);
905
906 /* First update is deferred until ORF or ROUTE-REFRESH is received */
907 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
908 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
909 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
910 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
911 || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
912 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
913
914 bgp_announce_route_all (peer);
915
916 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1);
917
918 return 0;
919}
920
921/* Keepalive packet is received. */
paul94f2b392005-06-28 12:44:16 +0000922static int
paul718e3742002-12-13 20:15:29 +0000923bgp_fsm_keepalive (struct peer *peer)
924{
925 /* peer count update */
926 peer->keepalive_in++;
927
928 BGP_TIMER_OFF (peer->t_holdtime);
929 return 0;
930}
931
932/* Update packet is received. */
paul94f2b392005-06-28 12:44:16 +0000933static int
paul718e3742002-12-13 20:15:29 +0000934bgp_fsm_update (struct peer *peer)
935{
936 BGP_TIMER_OFF (peer->t_holdtime);
937 return 0;
938}
939
940/* This is empty event. */
paul94f2b392005-06-28 12:44:16 +0000941static int
paul718e3742002-12-13 20:15:29 +0000942bgp_ignore (struct peer *peer)
943{
944 if (BGP_DEBUG (fsm, FSM))
945 zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
946 return 0;
947}
David Lamparter6b0655a2014-06-04 06:53:35 +0200948
paul718e3742002-12-13 20:15:29 +0000949/* Finite State Machine structure */
Stephen Hemmingerfda1d3e2009-05-15 10:02:27 -0700950static const struct {
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800951 int (*func) (struct peer *);
paul718e3742002-12-13 20:15:29 +0000952 int next_state;
953} FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
954{
955 {
956 /* Idle state: In Idle state, all events other than BGP_Start is
957 ignored. With BGP_Start event, finite state machine calls
958 bgp_start(). */
959 {bgp_start, Connect}, /* BGP_Start */
960 {bgp_stop, Idle}, /* BGP_Stop */
961 {bgp_stop, Idle}, /* TCP_connection_open */
962 {bgp_stop, Idle}, /* TCP_connection_closed */
963 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
964 {bgp_stop, Idle}, /* TCP_fatal_error */
965 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
966 {bgp_ignore, Idle}, /* Hold_Timer_expired */
967 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
968 {bgp_ignore, Idle}, /* Receive_OPEN_message */
969 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
970 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
971 {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +0000972 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +0000973 },
974 {
975 /* Connect */
976 {bgp_ignore, Connect}, /* BGP_Start */
977 {bgp_stop, Idle}, /* BGP_Stop */
978 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
979 {bgp_stop, Idle}, /* TCP_connection_closed */
980 {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
981 {bgp_connect_fail, Idle}, /* TCP_fatal_error */
982 {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
983 {bgp_ignore, Idle}, /* Hold_Timer_expired */
984 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
985 {bgp_ignore, Idle}, /* Receive_OPEN_message */
986 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
987 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
988 {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +0000989 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +0000990 },
991 {
992 /* Active, */
993 {bgp_ignore, Active}, /* BGP_Start */
994 {bgp_stop, Idle}, /* BGP_Stop */
995 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
996 {bgp_stop, Idle}, /* TCP_connection_closed */
997 {bgp_ignore, Active}, /* TCP_connection_open_failed */
998 {bgp_ignore, Idle}, /* TCP_fatal_error */
999 {bgp_start, Connect}, /* ConnectRetry_timer_expired */
1000 {bgp_ignore, Idle}, /* Hold_Timer_expired */
1001 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1002 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1003 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
1004 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1005 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +00001006 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +00001007 },
1008 {
1009 /* OpenSent, */
1010 {bgp_ignore, OpenSent}, /* BGP_Start */
1011 {bgp_stop, Idle}, /* BGP_Stop */
Paul Jakma536792c2007-06-22 19:11:14 +00001012 {bgp_stop, Active}, /* TCP_connection_open */
paul718e3742002-12-13 20:15:29 +00001013 {bgp_stop, Active}, /* TCP_connection_closed */
Paul Jakma536792c2007-06-22 19:11:14 +00001014 {bgp_stop, Active}, /* TCP_connection_open_failed */
1015 {bgp_stop, Active}, /* TCP_fatal_error */
paul718e3742002-12-13 20:15:29 +00001016 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1017 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1018 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1019 {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
Leonid Rosenboim397b5bd2013-07-30 20:14:25 +02001020 {bgp_fsm_event_error, Idle}, /* Receive_KEEPALIVE_message */
1021 {bgp_fsm_event_error, Idle}, /* Receive_UPDATE_message */
paul718e3742002-12-13 20:15:29 +00001022 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +00001023 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +00001024 },
1025 {
1026 /* OpenConfirm, */
1027 {bgp_ignore, OpenConfirm}, /* BGP_Start */
1028 {bgp_stop, Idle}, /* BGP_Stop */
1029 {bgp_stop, Idle}, /* TCP_connection_open */
1030 {bgp_stop, Idle}, /* TCP_connection_closed */
1031 {bgp_stop, Idle}, /* TCP_connection_open_failed */
1032 {bgp_stop, Idle}, /* TCP_fatal_error */
1033 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1034 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1035 {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
1036 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1037 {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
1038 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1039 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
Paul Jakmaca058a32006-09-14 02:58:49 +00001040 {bgp_ignore, Idle}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +00001041 },
1042 {
1043 /* Established, */
Paul Jakmaca058a32006-09-14 02:58:49 +00001044 {bgp_ignore, Established}, /* BGP_Start */
1045 {bgp_stop, Clearing}, /* BGP_Stop */
1046 {bgp_stop, Clearing}, /* TCP_connection_open */
1047 {bgp_stop, Clearing}, /* TCP_connection_closed */
Steve Hill3117b5c2009-07-28 17:50:00 +01001048 {bgp_stop, Clearing}, /* TCP_connection_open_failed */
Paul Jakmaca058a32006-09-14 02:58:49 +00001049 {bgp_stop, Clearing}, /* TCP_fatal_error */
Steve Hill3117b5c2009-07-28 17:50:00 +01001050 {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
Paul Jakmaca058a32006-09-14 02:58:49 +00001051 {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */
paul718e3742002-12-13 20:15:29 +00001052 {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
Paul Jakmaca058a32006-09-14 02:58:49 +00001053 {bgp_stop, Clearing}, /* Receive_OPEN_message */
1054 {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
1055 {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
1056 {bgp_stop_with_error, Clearing}, /* Receive_NOTIFICATION_message */
1057 {bgp_ignore, Idle}, /* Clearing_Completed */
1058 },
1059 {
1060 /* Clearing, */
1061 {bgp_ignore, Clearing}, /* BGP_Start */
Steve Hill3117b5c2009-07-28 17:50:00 +01001062 {bgp_stop, Clearing}, /* BGP_Stop */
1063 {bgp_stop, Clearing}, /* TCP_connection_open */
1064 {bgp_stop, Clearing}, /* TCP_connection_closed */
1065 {bgp_stop, Clearing}, /* TCP_connection_open_failed */
1066 {bgp_stop, Clearing}, /* TCP_fatal_error */
1067 {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
1068 {bgp_stop, Clearing}, /* Hold_Timer_expired */
1069 {bgp_stop, Clearing}, /* KeepAlive_timer_expired */
1070 {bgp_stop, Clearing}, /* Receive_OPEN_message */
1071 {bgp_stop, Clearing}, /* Receive_KEEPALIVE_message */
1072 {bgp_stop, Clearing}, /* Receive_UPDATE_message */
1073 {bgp_stop, Clearing}, /* Receive_NOTIFICATION_message */
1074 {bgp_clearing_completed, Idle}, /* Clearing_Completed */
Paul Jakmaca058a32006-09-14 02:58:49 +00001075 },
1076 {
1077 /* Deleted, */
1078 {bgp_ignore, Deleted}, /* BGP_Start */
1079 {bgp_ignore, Deleted}, /* BGP_Stop */
1080 {bgp_ignore, Deleted}, /* TCP_connection_open */
1081 {bgp_ignore, Deleted}, /* TCP_connection_closed */
1082 {bgp_ignore, Deleted}, /* TCP_connection_open_failed */
1083 {bgp_ignore, Deleted}, /* TCP_fatal_error */
1084 {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */
1085 {bgp_ignore, Deleted}, /* Hold_Timer_expired */
1086 {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */
1087 {bgp_ignore, Deleted}, /* Receive_OPEN_message */
1088 {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */
1089 {bgp_ignore, Deleted}, /* Receive_UPDATE_message */
1090 {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */
1091 {bgp_ignore, Deleted}, /* Clearing_Completed */
paul718e3742002-12-13 20:15:29 +00001092 },
1093};
1094
paulfd79ac92004-10-13 05:06:08 +00001095static const char *bgp_event_str[] =
paul718e3742002-12-13 20:15:29 +00001096{
1097 NULL,
1098 "BGP_Start",
1099 "BGP_Stop",
1100 "TCP_connection_open",
1101 "TCP_connection_closed",
1102 "TCP_connection_open_failed",
1103 "TCP_fatal_error",
1104 "ConnectRetry_timer_expired",
1105 "Hold_Timer_expired",
1106 "KeepAlive_timer_expired",
1107 "Receive_OPEN_message",
1108 "Receive_KEEPALIVE_message",
1109 "Receive_UPDATE_message",
Paul Jakmaca058a32006-09-14 02:58:49 +00001110 "Receive_NOTIFICATION_message",
1111 "Clearing_Completed",
paul718e3742002-12-13 20:15:29 +00001112};
1113
1114/* Execute event process. */
1115int
1116bgp_event (struct thread *thread)
1117{
Paul Jakmaca058a32006-09-14 02:58:49 +00001118 int ret = 0;
paul718e3742002-12-13 20:15:29 +00001119 int event;
1120 int next;
1121 struct peer *peer;
1122
1123 peer = THREAD_ARG (thread);
1124 event = THREAD_VAL (thread);
1125
1126 /* Logging this event. */
1127 next = FSM [peer->status -1][event - 1].next_state;
1128
Paul Jakmaca058a32006-09-14 02:58:49 +00001129 if (BGP_DEBUG (fsm, FSM) && peer->status != next)
ajs8c2e2002004-12-08 20:08:54 +00001130 plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
paul718e3742002-12-13 20:15:29 +00001131 bgp_event_str[event],
1132 LOOKUP (bgp_status_msg, peer->status),
1133 LOOKUP (bgp_status_msg, next));
paul718e3742002-12-13 20:15:29 +00001134
1135 /* Call function. */
Paul Jakmaca058a32006-09-14 02:58:49 +00001136 if (FSM [peer->status -1][event - 1].func)
1137 ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
paul718e3742002-12-13 20:15:29 +00001138
1139 /* When function do not want proceed next job return -1. */
paul200df112005-06-01 11:17:05 +00001140 if (ret >= 0)
1141 {
1142 /* If status is changed. */
1143 if (next != peer->status)
Paul Jakmaf2c31ac2007-02-22 17:48:42 +00001144 bgp_fsm_change_status (peer, next);
Paul Jakma95fdcd82006-12-08 00:31:22 +00001145
paul200df112005-06-01 11:17:05 +00001146 /* Make sure timer is set. */
1147 bgp_timer_set (peer);
1148 }
1149
paul200df112005-06-01 11:17:05 +00001150 return ret;
paul718e3742002-12-13 20:15:29 +00001151}