blob: 770a7911960a8535140f61450b5092b6d9aea81e [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"
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
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. */
53int bgp_event (struct thread *);
54
55/* BGP thread functions. */
56static int bgp_start_timer (struct thread *);
57static int bgp_connect_timer (struct thread *);
58static int bgp_holdtime_timer (struct thread *);
59static int bgp_keepalive_timer (struct thread *);
60
61/* BGP FSM functions. */
62static int bgp_start (struct peer *);
63
64/* BGP start timer jitter. */
paul94f2b392005-06-28 12:44:16 +000065static int
paul718e3742002-12-13 20:15:29 +000066bgp_start_jitter (int time)
67{
68 return ((rand () % (time + 1)) - (time / 2));
69}
70
71/* Hook function called after bgp event is occered. And vty's
72 neighbor command invoke this function after making neighbor
73 structure. */
74void
75bgp_timer_set (struct peer *peer)
76{
77 int jitter = 0;
78
79 switch (peer->status)
80 {
81 case Idle:
82 /* First entry point of peer's finite state machine. In Idle
83 status start timer is on unless peer is shutdown or peer is
84 inactive. All other timer must be turned off */
85 if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN)
86 || CHECK_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW)
Paul Jakma64e580a2006-02-21 01:09:01 +000087 || CHECK_FLAG (peer->sflags, PEER_STATUS_CLEARING)
paul718e3742002-12-13 20:15:29 +000088 || ! peer_active (peer))
89 {
90 BGP_TIMER_OFF (peer->t_start);
91 }
92 else
93 {
94 jitter = bgp_start_jitter (peer->v_start);
95 BGP_TIMER_ON (peer->t_start, bgp_start_timer,
96 peer->v_start + jitter);
97 }
98 BGP_TIMER_OFF (peer->t_connect);
99 BGP_TIMER_OFF (peer->t_holdtime);
100 BGP_TIMER_OFF (peer->t_keepalive);
101 BGP_TIMER_OFF (peer->t_asorig);
102 BGP_TIMER_OFF (peer->t_routeadv);
103 break;
104
105 case Connect:
106 /* After start timer is expired, the peer moves to Connnect
107 status. Make sure start timer is off and connect timer is
108 on. */
109 BGP_TIMER_OFF (peer->t_start);
110 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
111 BGP_TIMER_OFF (peer->t_holdtime);
112 BGP_TIMER_OFF (peer->t_keepalive);
113 BGP_TIMER_OFF (peer->t_asorig);
114 BGP_TIMER_OFF (peer->t_routeadv);
115 break;
116
117 case Active:
118 /* Active is waiting connection from remote peer. And if
119 connect timer is expired, change status to Connect. */
120 BGP_TIMER_OFF (peer->t_start);
121 /* If peer is passive mode, do not set connect timer. */
hasso93406d82005-02-02 14:40:33 +0000122 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)
123 || CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
paul718e3742002-12-13 20:15:29 +0000124 {
125 BGP_TIMER_OFF (peer->t_connect);
126 }
127 else
128 {
129 BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
130 }
131 BGP_TIMER_OFF (peer->t_holdtime);
132 BGP_TIMER_OFF (peer->t_keepalive);
133 BGP_TIMER_OFF (peer->t_asorig);
134 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);
151 BGP_TIMER_OFF (peer->t_asorig);
152 BGP_TIMER_OFF (peer->t_routeadv);
153 break;
154
155 case OpenConfirm:
156 /* OpenConfirm status. */
157 BGP_TIMER_OFF (peer->t_start);
158 BGP_TIMER_OFF (peer->t_connect);
159
160 /* If the negotiated Hold Time value is zero, then the Hold Time
161 timer and KeepAlive timers are not started. */
162 if (peer->v_holdtime == 0)
163 {
164 BGP_TIMER_OFF (peer->t_holdtime);
165 BGP_TIMER_OFF (peer->t_keepalive);
166 }
167 else
168 {
169 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
170 peer->v_holdtime);
171 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
172 peer->v_keepalive);
173 }
174 BGP_TIMER_OFF (peer->t_asorig);
175 BGP_TIMER_OFF (peer->t_routeadv);
176 break;
177
178 case Established:
179 /* In Established status start and connect timer is turned
180 off. */
181 BGP_TIMER_OFF (peer->t_start);
182 BGP_TIMER_OFF (peer->t_connect);
183
184 /* Same as OpenConfirm, if holdtime is zero then both holdtime
185 and keepalive must be turned off. */
186 if (peer->v_holdtime == 0)
187 {
188 BGP_TIMER_OFF (peer->t_holdtime);
189 BGP_TIMER_OFF (peer->t_keepalive);
190 }
191 else
192 {
193 BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
194 peer->v_holdtime);
195 BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
196 peer->v_keepalive);
197 }
198 BGP_TIMER_OFF (peer->t_asorig);
199 break;
200 }
201}
202
203/* BGP start timer. This function set BGP_Start event to thread value
204 and process event. */
205static int
206bgp_start_timer (struct thread *thread)
207{
208 struct peer *peer;
209
210 peer = THREAD_ARG (thread);
211 peer->t_start = NULL;
212
213 if (BGP_DEBUG (fsm, FSM))
214 zlog (peer->log, LOG_DEBUG,
215 "%s [FSM] Timer (start timer expire).", peer->host);
216
217 THREAD_VAL (thread) = BGP_Start;
paul200df112005-06-01 11:17:05 +0000218 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000219
220 return 0;
221}
222
223/* BGP connect retry timer. */
224static int
225bgp_connect_timer (struct thread *thread)
226{
227 struct peer *peer;
228
229 peer = THREAD_ARG (thread);
230 peer->t_connect = NULL;
231
232 if (BGP_DEBUG (fsm, FSM))
233 zlog (peer->log, LOG_DEBUG, "%s [FSM] Timer (connect timer expire)",
234 peer->host);
235
236 THREAD_VAL (thread) = ConnectRetry_timer_expired;
paul200df112005-06-01 11:17:05 +0000237 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000238
239 return 0;
240}
241
242/* BGP holdtime timer. */
243static int
244bgp_holdtime_timer (struct thread *thread)
245{
246 struct peer *peer;
247
248 peer = THREAD_ARG (thread);
249 peer->t_holdtime = NULL;
250
251 if (BGP_DEBUG (fsm, FSM))
252 zlog (peer->log, LOG_DEBUG,
253 "%s [FSM] Timer (holdtime timer expire)",
254 peer->host);
255
256 THREAD_VAL (thread) = Hold_Timer_expired;
paul200df112005-06-01 11:17:05 +0000257 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000258
259 return 0;
260}
261
262/* BGP keepalive fire ! */
263static int
264bgp_keepalive_timer (struct thread *thread)
265{
266 struct peer *peer;
267
268 peer = THREAD_ARG (thread);
269 peer->t_keepalive = NULL;
270
271 if (BGP_DEBUG (fsm, FSM))
272 zlog (peer->log, LOG_DEBUG,
273 "%s [FSM] Timer (keepalive timer expire)",
274 peer->host);
275
276 THREAD_VAL (thread) = KeepAlive_timer_expired;
paul200df112005-06-01 11:17:05 +0000277 bgp_event (thread); /* bgp_event unlocks peer */
paul718e3742002-12-13 20:15:29 +0000278
279 return 0;
280}
281
paul94f2b392005-06-28 12:44:16 +0000282static int
paul718e3742002-12-13 20:15:29 +0000283bgp_routeadv_timer (struct thread *thread)
284{
285 struct peer *peer;
286
287 peer = THREAD_ARG (thread);
288 peer->t_routeadv = NULL;
289
290 if (BGP_DEBUG (fsm, FSM))
291 zlog (peer->log, LOG_DEBUG,
292 "%s [FSM] Timer (routeadv timer expire)",
293 peer->host);
294
295 peer->synctime = time (NULL);
296
pauleb821182004-05-01 08:44:08 +0000297 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000298
299 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer,
300 peer->v_routeadv);
301
302 return 0;
303}
304
305/* Reset bgp update timer */
306static void
307bgp_uptime_reset (struct peer *peer)
308{
309 peer->uptime = time (NULL);
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++)
352 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
353 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++)
385 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
386 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
399 /* Preserve old status and change into new status. */
400 peer->ostatus = peer->status;
401 peer->status = status;
402
403 if (BGP_DEBUG (normal, NORMAL))
404 zlog_debug ("%s went from %s to %s",
405 peer->host,
406 LOOKUP (bgp_status_msg, peer->ostatus),
407 LOOKUP (bgp_status_msg, peer->status));
408}
409
paul718e3742002-12-13 20:15:29 +0000410/* Administrative BGP peer stop event. */
411int
412bgp_stop (struct peer *peer)
413{
paul718e3742002-12-13 20:15:29 +0000414 afi_t afi;
415 safi_t safi;
paul200df112005-06-01 11:17:05 +0000416 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000417 char orf_name[BUFSIZ];
418
419 /* Increment Dropped count. */
420 if (peer->status == Established)
421 {
paul718e3742002-12-13 20:15:29 +0000422 peer->dropped++;
423 bgp_fsm_change_status (peer, Idle);
paul848973c2003-08-13 00:32:49 +0000424
425 /* bgp log-neighbor-changes of neighbor Down */
426 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
hassoe0701b72004-05-20 09:19:34 +0000427 zlog_info ("%%ADJCHANGE: neighbor %s Down %s", peer->host,
428 peer_down_str [(int) peer->last_reset]);
paul848973c2003-08-13 00:32:49 +0000429
hasso93406d82005-02-02 14:40:33 +0000430 /* graceful restart */
431 if (peer->t_gr_stale)
432 {
433 BGP_TIMER_OFF (peer->t_gr_stale);
434 if (BGP_DEBUG (events, EVENTS))
435 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
436 }
437 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT))
438 {
439 if (BGP_DEBUG (events, EVENTS))
440 {
441 zlog_debug ("%s graceful restart timer started for %d sec",
442 peer->host, peer->v_gr_restart);
443 zlog_debug ("%s graceful restart stalepath timer started for %d sec",
444 peer->host, peer->bgp->stalepath_time);
445 }
446 BGP_TIMER_ON (peer->t_gr_restart, bgp_graceful_restart_timer_expire,
447 peer->v_gr_restart);
448 BGP_TIMER_ON (peer->t_gr_stale, bgp_graceful_stale_timer_expire,
449 peer->bgp->stalepath_time);
450 }
451 else
452 {
453 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
454
455 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
456 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
457 peer->nsf[afi][safi] = 0;
458 }
459
paul848973c2003-08-13 00:32:49 +0000460 /* set last reset time */
461 peer->resettime = time (NULL);
paulc5317402004-05-03 13:25:06 +0000462 /* Reset uptime. */
463 bgp_uptime_reset (peer);
paul848973c2003-08-13 00:32:49 +0000464
paul718e3742002-12-13 20:15:29 +0000465#ifdef HAVE_SNMP
466 bgpTrapBackwardTransition (peer);
467#endif /* HAVE_SNMP */
paul718e3742002-12-13 20:15:29 +0000468
hasso538621f2004-05-21 09:31:30 +0000469 /* Reset uptime. */
470 bgp_uptime_reset (peer);
471
472 /* Need of clear of peer. */
473 bgp_clear_route_all (peer);
hassof4184462005-02-01 20:13:16 +0000474
475 /* Reset peer synctime */
476 peer->synctime = 0;
hasso538621f2004-05-21 09:31:30 +0000477 }
paul718e3742002-12-13 20:15:29 +0000478
479 /* Stop read and write threads when exists. */
480 BGP_READ_OFF (peer->t_read);
481 BGP_WRITE_OFF (peer->t_write);
482
483 /* Stop all timers. */
484 BGP_TIMER_OFF (peer->t_start);
485 BGP_TIMER_OFF (peer->t_connect);
486 BGP_TIMER_OFF (peer->t_holdtime);
487 BGP_TIMER_OFF (peer->t_keepalive);
488 BGP_TIMER_OFF (peer->t_asorig);
489 BGP_TIMER_OFF (peer->t_routeadv);
490
paul200df112005-06-01 11:17:05 +0000491 /* Delete all existing events of the peer,
492 and corresponding peer ref-count */
493 for (i = thread_cancel_event (master, peer); i > 0; i--)
494 peer_unlock (peer); /* thread event reference */
495
paul718e3742002-12-13 20:15:29 +0000496 /* Stream reset. */
497 peer->packet_size = 0;
498
499 /* Clear input and output buffer. */
500 if (peer->ibuf)
501 stream_reset (peer->ibuf);
502 if (peer->work)
503 stream_reset (peer->work);
paul200df112005-06-01 11:17:05 +0000504 if (peer->obuf)
505 stream_fifo_clean (peer->obuf);
paul718e3742002-12-13 20:15:29 +0000506
pauleb821182004-05-01 08:44:08 +0000507 /* Close of file descriptor. */
508 if (peer->fd >= 0)
509 {
510 close (peer->fd);
511 peer->fd = -1;
512 }
paul718e3742002-12-13 20:15:29 +0000513
paul718e3742002-12-13 20:15:29 +0000514 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
515 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
516 {
hasso538621f2004-05-21 09:31:30 +0000517 /* Reset all negotiated variables */
518 peer->afc_nego[afi][safi] = 0;
519 peer->afc_adv[afi][safi] = 0;
520 peer->afc_recv[afi][safi] = 0;
521
paul718e3742002-12-13 20:15:29 +0000522 /* peer address family capability flags*/
523 peer->af_cap[afi][safi] = 0;
hasso538621f2004-05-21 09:31:30 +0000524
paul718e3742002-12-13 20:15:29 +0000525 /* peer address family status flags*/
526 peer->af_sflags[afi][safi] = 0;
hasso538621f2004-05-21 09:31:30 +0000527
paul718e3742002-12-13 20:15:29 +0000528 /* Received ORF prefix-filter */
529 peer->orf_plist[afi][safi] = NULL;
hasso538621f2004-05-21 09:31:30 +0000530
paul718e3742002-12-13 20:15:29 +0000531 /* ORF received prefix-filter pnt */
532 sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi);
533 prefix_bgp_orf_remove_all (orf_name);
534 }
535
536 /* Reset keepalive and holdtime */
537 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
538 {
539 peer->v_keepalive = peer->keepalive;
540 peer->v_holdtime = peer->holdtime;
541 }
542 else
543 {
544 peer->v_keepalive = peer->bgp->default_keepalive;
545 peer->v_holdtime = peer->bgp->default_holdtime;
546 }
547
548 peer->update_time = 0;
549
550 /* Until we are sure that there is no problem about prefix count
551 this should be commented out.*/
552#if 0
553 /* Reset prefix count */
554 peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
555 peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
556 peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
557 peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
558 peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
559#endif /* 0 */
560
561 return 0;
562}
563
564/* BGP peer is stoped by the error. */
paul94f2b392005-06-28 12:44:16 +0000565static int
paul718e3742002-12-13 20:15:29 +0000566bgp_stop_with_error (struct peer *peer)
567{
568 /* Double start timer. */
569 peer->v_start *= 2;
570
571 /* Overflow check. */
572 if (peer->v_start >= (60 * 2))
573 peer->v_start = (60 * 2);
574
575 bgp_stop (peer);
576
577 return 0;
578}
579
580/* TCP connection open. Next we send open message to remote peer. And
581 add read thread for reading open message. */
paul94f2b392005-06-28 12:44:16 +0000582static int
paul718e3742002-12-13 20:15:29 +0000583bgp_connect_success (struct peer *peer)
584{
hassof4184462005-02-01 20:13:16 +0000585 char buf1[BUFSIZ];
586
pauleb821182004-05-01 08:44:08 +0000587 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000588 {
589 zlog_err ("bgp_connect_success peer's fd is negative value %d",
pauleb821182004-05-01 08:44:08 +0000590 peer->fd);
paul718e3742002-12-13 20:15:29 +0000591 return -1;
592 }
pauleb821182004-05-01 08:44:08 +0000593 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
paul718e3742002-12-13 20:15:29 +0000594
hassof4184462005-02-01 20:13:16 +0000595 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
596 bgp_getsockname (peer);
597
598 if (BGP_DEBUG (normal, NORMAL))
599 {
600 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
601 zlog_debug ("%s open active, local address %s", peer->host,
602 sockunion2str (peer->su_local, buf1, SU_ADDRSTRLEN));
603 else
604 zlog_debug ("%s passive open", peer->host);
605 }
paul718e3742002-12-13 20:15:29 +0000606
607 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
608 bgp_open_send (peer);
609
610 return 0;
611}
612
613/* TCP connect fail */
paul94f2b392005-06-28 12:44:16 +0000614static int
paul718e3742002-12-13 20:15:29 +0000615bgp_connect_fail (struct peer *peer)
616{
617 bgp_stop (peer);
618 return 0;
619}
620
621/* This function is the first starting point of all BGP connection. It
622 try to connect to remote peer with non-blocking IO. */
623int
624bgp_start (struct peer *peer)
625{
626 int status;
627
Paul Jakma33d5ab92006-07-02 11:01:50 +0000628 /* Scrub some information that might be left over from a previous,
629 * session
630 */
631 /* Connection information. */
632 if (peer->su_local)
633 {
634 sockunion_free (peer->su_local);
635 peer->su_local = NULL;
636 }
637
638 if (peer->su_remote)
639 {
640 sockunion_free (peer->su_remote);
641 peer->su_remote = NULL;
642 }
643
644 /* Clear remote router-id. */
645 peer->remote_id.s_addr = 0;
646
647 /* Clear peer capability flag. */
648 peer->cap = 0;
649
paul718e3742002-12-13 20:15:29 +0000650 /* If the peer is passive mode, force to move to Active mode. */
651 if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE))
652 {
653 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
654 return 0;
655 }
656
657 status = bgp_connect (peer);
658
659 switch (status)
660 {
661 case connect_error:
662 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000663 plog_debug (peer->log, "%s [FSM] Connect error", peer->host);
paul718e3742002-12-13 20:15:29 +0000664 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
665 break;
666 case connect_success:
667 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000668 plog_debug (peer->log, "%s [FSM] Connect immediately success",
pauleb821182004-05-01 08:44:08 +0000669 peer->host);
paul718e3742002-12-13 20:15:29 +0000670 BGP_EVENT_ADD (peer, TCP_connection_open);
671 break;
672 case connect_in_progress:
673 /* To check nonblocking connect, we wait until socket is
674 readable or writable. */
675 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +0000676 plog_debug (peer->log, "%s [FSM] Non blocking connect waiting result",
pauleb821182004-05-01 08:44:08 +0000677 peer->host);
678 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +0000679 {
680 zlog_err ("bgp_start peer's fd is negative value %d",
pauleb821182004-05-01 08:44:08 +0000681 peer->fd);
paul718e3742002-12-13 20:15:29 +0000682 return -1;
683 }
pauleb821182004-05-01 08:44:08 +0000684 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
685 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000686 break;
687 }
688 return 0;
689}
690
691/* Connect retry timer is expired when the peer status is Connect. */
paul94f2b392005-06-28 12:44:16 +0000692static int
paul718e3742002-12-13 20:15:29 +0000693bgp_reconnect (struct peer *peer)
694{
695 bgp_stop (peer);
696 bgp_start (peer);
697 return 0;
698}
699
paul94f2b392005-06-28 12:44:16 +0000700static int
paul718e3742002-12-13 20:15:29 +0000701bgp_fsm_open (struct peer *peer)
702{
703 /* Send keepalive and make keepalive timer */
704 bgp_keepalive_send (peer);
705
706 /* Reset holdtimer value. */
707 BGP_TIMER_OFF (peer->t_holdtime);
708
709 return 0;
710}
711
paul718e3742002-12-13 20:15:29 +0000712/* Keepalive send to peer. */
paul94f2b392005-06-28 12:44:16 +0000713static int
paul718e3742002-12-13 20:15:29 +0000714bgp_fsm_keepalive_expire (struct peer *peer)
715{
716 bgp_keepalive_send (peer);
717 return 0;
718}
719
720/* Hold timer expire. This is error of BGP connection. So cut the
721 peer and change to Idle status. */
paul94f2b392005-06-28 12:44:16 +0000722static int
paul718e3742002-12-13 20:15:29 +0000723bgp_fsm_holdtime_expire (struct peer *peer)
724{
725 if (BGP_DEBUG (fsm, FSM))
726 zlog (peer->log, LOG_DEBUG, "%s [FSM] Hold timer expire", peer->host);
727
728 /* Send notify to remote peer. */
729 bgp_notify_send (peer, BGP_NOTIFY_HOLD_ERR, 0);
730
731 /* Sweep if it is temporary peer. */
732 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
733 {
734 zlog_info ("%s [Event] Accepting BGP peer is deleted", peer->host);
735 peer_delete (peer);
736 return -1;
737 }
738
739 return 0;
740}
741
742/* Status goes to Established. Send keepalive packet then make first
743 update information. */
paul94f2b392005-06-28 12:44:16 +0000744static int
paul718e3742002-12-13 20:15:29 +0000745bgp_establish (struct peer *peer)
746{
747 struct bgp_notify *notify;
748 afi_t afi;
749 safi_t safi;
hasso93406d82005-02-02 14:40:33 +0000750 int nsf_af_count = 0;
paul718e3742002-12-13 20:15:29 +0000751
752 /* Reset capability open status flag. */
753 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
754 SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
755
756 /* Clear last notification data. */
757 notify = &peer->notify;
758 if (notify->data)
759 XFREE (MTYPE_TMP, notify->data);
760 memset (notify, 0, sizeof (struct bgp_notify));
761
762 /* Clear start timer value to default. */
763 peer->v_start = BGP_INIT_START_TIMER;
764
765 /* Increment established count. */
766 peer->established++;
767 bgp_fsm_change_status (peer, Established);
paul848973c2003-08-13 00:32:49 +0000768
769 /* bgp log-neighbor-changes of neighbor Up */
770 if (bgp_flag_check (peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES))
771 zlog_info ("%%ADJCHANGE: neighbor %s Up", peer->host);
772
hasso93406d82005-02-02 14:40:33 +0000773 /* graceful restart */
774 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
775 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
776 for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++)
777 {
778 if (peer->afc_nego[afi][safi]
779 && CHECK_FLAG (peer->cap, PEER_CAP_RESTART_ADV)
780 && CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV))
781 {
782 if (peer->nsf[afi][safi]
783 && ! CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV))
784 bgp_clear_stale_route (peer, afi, safi);
785
786 peer->nsf[afi][safi] = 1;
787 nsf_af_count++;
788 }
789 else
790 {
791 if (peer->nsf[afi][safi])
792 bgp_clear_stale_route (peer, afi, safi);
793 peer->nsf[afi][safi] = 0;
794 }
795 }
796
797 if (nsf_af_count)
798 SET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
799 else
800 {
801 UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE);
802 if (peer->t_gr_stale)
803 {
804 BGP_TIMER_OFF (peer->t_gr_stale);
805 if (BGP_DEBUG (events, EVENTS))
806 zlog_debug ("%s graceful restart stalepath timer stopped", peer->host);
807 }
808 }
809
810 if (peer->t_gr_restart)
811 {
812 BGP_TIMER_OFF (peer->t_gr_restart);
813 if (BGP_DEBUG (events, EVENTS))
814 zlog_debug ("%s graceful restart timer stopped", peer->host);
815 }
816
paul718e3742002-12-13 20:15:29 +0000817#ifdef HAVE_SNMP
818 bgpTrapEstablished (peer);
819#endif /* HAVE_SNMP */
820
821 /* Reset uptime, send keepalive, send current table. */
822 bgp_uptime_reset (peer);
823
824 /* Send route-refresh when ORF is enabled */
825 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
826 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
827 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV))
828 {
829 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV))
830 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX,
831 REFRESH_IMMEDIATE, 0);
832 else if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
833 bgp_route_refresh_send (peer, afi, safi, ORF_TYPE_PREFIX_OLD,
834 REFRESH_IMMEDIATE, 0);
835 }
836
837 if (peer->v_keepalive)
838 bgp_keepalive_send (peer);
839
840 /* First update is deferred until ORF or ROUTE-REFRESH is received */
841 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
842 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
843 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV))
844 if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
845 || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
846 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
847
848 bgp_announce_route_all (peer);
849
850 BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer, 1);
851
852 return 0;
853}
854
855/* Keepalive packet is received. */
paul94f2b392005-06-28 12:44:16 +0000856static int
paul718e3742002-12-13 20:15:29 +0000857bgp_fsm_keepalive (struct peer *peer)
858{
859 /* peer count update */
860 peer->keepalive_in++;
861
862 BGP_TIMER_OFF (peer->t_holdtime);
863 return 0;
864}
865
866/* Update packet is received. */
paul94f2b392005-06-28 12:44:16 +0000867static int
paul718e3742002-12-13 20:15:29 +0000868bgp_fsm_update (struct peer *peer)
869{
870 BGP_TIMER_OFF (peer->t_holdtime);
871 return 0;
872}
873
874/* This is empty event. */
paul94f2b392005-06-28 12:44:16 +0000875static int
paul718e3742002-12-13 20:15:29 +0000876bgp_ignore (struct peer *peer)
877{
878 if (BGP_DEBUG (fsm, FSM))
879 zlog (peer->log, LOG_DEBUG, "%s [FSM] bgp_ignore called", peer->host);
880 return 0;
881}
882
883/* Finite State Machine structure */
884struct {
885 int (*func) ();
886 int next_state;
887} FSM [BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] =
888{
889 {
890 /* Idle state: In Idle state, all events other than BGP_Start is
891 ignored. With BGP_Start event, finite state machine calls
892 bgp_start(). */
893 {bgp_start, Connect}, /* BGP_Start */
894 {bgp_stop, Idle}, /* BGP_Stop */
895 {bgp_stop, Idle}, /* TCP_connection_open */
896 {bgp_stop, Idle}, /* TCP_connection_closed */
897 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
898 {bgp_stop, Idle}, /* TCP_fatal_error */
899 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
900 {bgp_ignore, Idle}, /* Hold_Timer_expired */
901 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
902 {bgp_ignore, Idle}, /* Receive_OPEN_message */
903 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
904 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
905 {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
906 },
907 {
908 /* Connect */
909 {bgp_ignore, Connect}, /* BGP_Start */
910 {bgp_stop, Idle}, /* BGP_Stop */
911 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
912 {bgp_stop, Idle}, /* TCP_connection_closed */
913 {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
914 {bgp_connect_fail, Idle}, /* TCP_fatal_error */
915 {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
916 {bgp_ignore, Idle}, /* Hold_Timer_expired */
917 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
918 {bgp_ignore, Idle}, /* Receive_OPEN_message */
919 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
920 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
921 {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
922 },
923 {
924 /* Active, */
925 {bgp_ignore, Active}, /* BGP_Start */
926 {bgp_stop, Idle}, /* BGP_Stop */
927 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
928 {bgp_stop, Idle}, /* TCP_connection_closed */
929 {bgp_ignore, Active}, /* TCP_connection_open_failed */
930 {bgp_ignore, Idle}, /* TCP_fatal_error */
931 {bgp_start, Connect}, /* ConnectRetry_timer_expired */
932 {bgp_ignore, Idle}, /* Hold_Timer_expired */
933 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
934 {bgp_ignore, Idle}, /* Receive_OPEN_message */
935 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
936 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
937 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
938 },
939 {
940 /* OpenSent, */
941 {bgp_ignore, OpenSent}, /* BGP_Start */
942 {bgp_stop, Idle}, /* BGP_Stop */
943 {bgp_stop, Idle}, /* TCP_connection_open */
944 {bgp_stop, Active}, /* TCP_connection_closed */
945 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
946 {bgp_stop, Idle}, /* TCP_fatal_error */
947 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
948 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
949 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
950 {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
951 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
952 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
953 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
954 },
955 {
956 /* OpenConfirm, */
957 {bgp_ignore, OpenConfirm}, /* BGP_Start */
958 {bgp_stop, Idle}, /* BGP_Stop */
959 {bgp_stop, Idle}, /* TCP_connection_open */
960 {bgp_stop, Idle}, /* TCP_connection_closed */
961 {bgp_stop, Idle}, /* TCP_connection_open_failed */
962 {bgp_stop, Idle}, /* TCP_fatal_error */
963 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
964 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
965 {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
966 {bgp_ignore, Idle}, /* Receive_OPEN_message */
967 {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
968 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
969 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
970 },
971 {
972 /* Established, */
973 {bgp_ignore, Established}, /* BGP_Start */
974 {bgp_stop, Idle}, /* BGP_Stop */
975 {bgp_stop, Idle}, /* TCP_connection_open */
976 {bgp_stop, Idle}, /* TCP_connection_closed */
977 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
978 {bgp_stop, Idle}, /* TCP_fatal_error */
979 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
980 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
981 {bgp_fsm_keepalive_expire, Established}, /* KeepAlive_timer_expired */
982 {bgp_stop, Idle}, /* Receive_OPEN_message */
983 {bgp_fsm_keepalive, Established}, /* Receive_KEEPALIVE_message */
984 {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
985 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
986 },
987};
988
paulfd79ac92004-10-13 05:06:08 +0000989static const char *bgp_event_str[] =
paul718e3742002-12-13 20:15:29 +0000990{
991 NULL,
992 "BGP_Start",
993 "BGP_Stop",
994 "TCP_connection_open",
995 "TCP_connection_closed",
996 "TCP_connection_open_failed",
997 "TCP_fatal_error",
998 "ConnectRetry_timer_expired",
999 "Hold_Timer_expired",
1000 "KeepAlive_timer_expired",
1001 "Receive_OPEN_message",
1002 "Receive_KEEPALIVE_message",
1003 "Receive_UPDATE_message",
1004 "Receive_NOTIFICATION_message"
1005};
1006
1007/* Execute event process. */
1008int
1009bgp_event (struct thread *thread)
1010{
1011 int ret;
1012 int event;
1013 int next;
1014 struct peer *peer;
1015
1016 peer = THREAD_ARG (thread);
1017 event = THREAD_VAL (thread);
1018
1019 /* Logging this event. */
1020 next = FSM [peer->status -1][event - 1].next_state;
1021
1022 if (BGP_DEBUG (fsm, FSM))
ajs8c2e2002004-12-08 20:08:54 +00001023 plog_debug (peer->log, "%s [FSM] %s (%s->%s)", peer->host,
paul718e3742002-12-13 20:15:29 +00001024 bgp_event_str[event],
1025 LOOKUP (bgp_status_msg, peer->status),
1026 LOOKUP (bgp_status_msg, next));
paul718e3742002-12-13 20:15:29 +00001027
1028 /* Call function. */
1029 ret = (*(FSM [peer->status - 1][event - 1].func))(peer);
1030
1031 /* When function do not want proceed next job return -1. */
paul200df112005-06-01 11:17:05 +00001032 if (ret >= 0)
1033 {
1034 /* If status is changed. */
1035 if (next != peer->status)
1036 bgp_fsm_change_status (peer, next);
paul718e3742002-12-13 20:15:29 +00001037
paul200df112005-06-01 11:17:05 +00001038 /* Make sure timer is set. */
1039 bgp_timer_set (peer);
1040 }
1041
1042 peer_unlock (peer); /* bgp-event peer reference */
1043 return ret;
paul718e3742002-12-13 20:15:29 +00001044}