]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_fsm.c
bgpd: Abstract bgp_static retrieving/setting from info pointer
[mirror_frr.git] / bgpd / bgp_fsm.c
CommitLineData
d62a17ae 1/* BGP-4 Finite State Machine
896014f4
DL
2 * From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
3 * Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
718e3744 21
22#include <zebra.h>
23
24#include "linklist.h"
25#include "prefix.h"
718e3744 26#include "sockunion.h"
27#include "thread.h"
28#include "log.h"
29#include "stream.h"
74ffbfe6 30#include "ringbuf.h"
718e3744 31#include "memory.h"
32#include "plist.h"
f188f2c4 33#include "workqueue.h"
3f9c7369 34#include "queue.h"
039f3a34 35#include "filter.h"
4dcadbef 36#include "command.h"
02705213 37#include "lib_errors.h"
718e3744 38
856ca177 39#include "lib/json.h"
718e3744 40#include "bgpd/bgpd.h"
41#include "bgpd/bgp_attr.h"
42#include "bgpd/bgp_debug.h"
14454c9f 43#include "bgpd/bgp_errors.h"
718e3744 44#include "bgpd/bgp_fsm.h"
45#include "bgpd/bgp_packet.h"
46#include "bgpd/bgp_network.h"
47#include "bgpd/bgp_route.h"
48#include "bgpd/bgp_dump.h"
49#include "bgpd/bgp_open.h"
f188f2c4 50#include "bgpd/bgp_advertise.h"
3f9c7369 51#include "bgpd/bgp_updgrp.h"
ffd0c037 52#include "bgpd/bgp_nht.h"
c43ed2e4 53#include "bgpd/bgp_bfd.h"
4a1ab8e4 54#include "bgpd/bgp_memory.h"
03014d48 55#include "bgpd/bgp_keepalives.h"
56257a44 56#include "bgpd/bgp_io.h"
c42eab4b 57#include "bgpd/bgp_zebra.h"
6b0655a2 58
d62a17ae 59DEFINE_HOOK(peer_backward_transition, (struct peer * peer), (peer))
60DEFINE_HOOK(peer_established, (struct peer * peer), (peer))
3012671f 61
6403814c
DS
62/* Definition of display strings corresponding to FSM events. This should be
63 * kept consistent with the events defined in bgpd.h
64 */
d62a17ae 65static const char *bgp_event_str[] = {
66 NULL,
67 "BGP_Start",
68 "BGP_Stop",
69 "TCP_connection_open",
70 "TCP_connection_closed",
71 "TCP_connection_open_failed",
72 "TCP_fatal_error",
73 "ConnectRetry_timer_expired",
74 "Hold_Timer_expired",
75 "KeepAlive_timer_expired",
76 "Receive_OPEN_message",
77 "Receive_KEEPALIVE_message",
78 "Receive_UPDATE_message",
79 "Receive_NOTIFICATION_message",
80 "Clearing_Completed",
6403814c
DS
81};
82
718e3744 83/* BGP FSM (finite state machine) has three types of functions. Type
84 one is thread functions. Type two is event functions. Type three
85 is FSM functions. Timer functions are set by bgp_timer_set
86 function. */
87
88/* BGP event function. */
d62a17ae 89int bgp_event(struct thread *);
718e3744 90
91/* BGP thread functions. */
d62a17ae 92static int bgp_start_timer(struct thread *);
93static int bgp_connect_timer(struct thread *);
94static int bgp_holdtime_timer(struct thread *);
718e3744 95
96/* BGP FSM functions. */
d62a17ae 97static int bgp_start(struct peer *);
718e3744 98
d62a17ae 99static void peer_xfer_stats(struct peer *peer_dst, struct peer *peer_src)
1ff9a340 100{
d62a17ae 101 /* Copy stats over. These are only the pre-established state stats */
102 peer_dst->open_in += peer_src->open_in;
103 peer_dst->open_out += peer_src->open_out;
104 peer_dst->keepalive_in += peer_src->keepalive_in;
105 peer_dst->keepalive_out += peer_src->keepalive_out;
106 peer_dst->notify_in += peer_src->notify_in;
107 peer_dst->notify_out += peer_src->notify_out;
108 peer_dst->dynamic_cap_in += peer_src->dynamic_cap_in;
109 peer_dst->dynamic_cap_out += peer_src->dynamic_cap_out;
1ff9a340
DS
110}
111
d62a17ae 112static struct peer *peer_xfer_conn(struct peer *from_peer)
1ff9a340 113{
d62a17ae 114 struct peer *peer;
115 afi_t afi;
116 safi_t safi;
117 int fd;
118 int status, pstatus;
119 unsigned char last_evt, last_maj_evt;
120
121 assert(from_peer != NULL);
122
123 peer = from_peer->doppelganger;
124
125 if (!peer || !CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
126 return from_peer;
127
128 if (bgp_debug_neighbor_events(peer))
129 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
130 from_peer->host, from_peer, from_peer->fd, peer,
131 peer->fd);
132
424ab01d
QY
133 bgp_writes_off(peer);
134 bgp_reads_off(peer);
135 bgp_writes_off(from_peer);
136 bgp_reads_off(from_peer);
d62a17ae 137
138 BGP_TIMER_OFF(peer->t_routeadv);
424ab01d 139 BGP_TIMER_OFF(peer->t_connect);
387f984e
QY
140 BGP_TIMER_OFF(peer->t_connect_check_r);
141 BGP_TIMER_OFF(peer->t_connect_check_w);
d62a17ae 142 BGP_TIMER_OFF(from_peer->t_routeadv);
424ab01d 143 BGP_TIMER_OFF(from_peer->t_connect);
387f984e
QY
144 BGP_TIMER_OFF(from_peer->t_connect_check_r);
145 BGP_TIMER_OFF(from_peer->t_connect_check_w);
7a86aa5a 146 BGP_TIMER_OFF(from_peer->t_process_packet);
d3ecc69e 147
becedef6
QY
148 /*
149 * At this point in time, it is possible that there are packets pending
150 * on various buffers. Those need to be transferred or dropped,
151 * otherwise we'll get spurious failures during session establishment.
152 */
424ab01d
QY
153 pthread_mutex_lock(&peer->io_mtx);
154 pthread_mutex_lock(&from_peer->io_mtx);
d3ecc69e 155 {
424ab01d
QY
156 fd = peer->fd;
157 peer->fd = from_peer->fd;
158 from_peer->fd = fd;
159
160 stream_fifo_clean(peer->ibuf);
d3ecc69e 161 stream_fifo_clean(peer->obuf);
d3ecc69e 162
becedef6
QY
163 /*
164 * this should never happen, since bgp_process_packet() is the
165 * only task that sets and unsets the current packet and it
166 * runs in our pthread.
167 */
424ab01d 168 if (peer->curr) {
af4c2728 169 flog_err(
e50f7cfd 170 EC_BGP_PKT_PROCESS,
424ab01d
QY
171 "[%s] Dropping pending packet on connection transfer:",
172 peer->host);
d7c0a89a
QY
173 uint16_t type = stream_getc_from(peer->curr,
174 BGP_MARKER_SIZE + 2);
424ab01d
QY
175 bgp_dump_packet(peer, type, peer->curr);
176 stream_free(peer->curr);
177 peer->curr = NULL;
178 }
179
180 // copy each packet from old peer's output queue to new peer
727c4f87
QY
181 while (from_peer->obuf->head)
182 stream_fifo_push(peer->obuf,
183 stream_fifo_pop(from_peer->obuf));
424ab01d
QY
184
185 // copy each packet from old peer's input queue to new peer
186 while (from_peer->ibuf->head)
187 stream_fifo_push(peer->ibuf,
188 stream_fifo_pop(from_peer->ibuf));
7db44ec8 189
74ffbfe6
QY
190 ringbuf_wipe(peer->ibuf_work);
191 ringbuf_copy(peer->ibuf_work, from_peer->ibuf_work,
192 ringbuf_remain(from_peer->ibuf_work));
d3ecc69e 193 }
424ab01d
QY
194 pthread_mutex_unlock(&from_peer->io_mtx);
195 pthread_mutex_unlock(&peer->io_mtx);
d62a17ae 196
197 peer->as = from_peer->as;
198 peer->v_holdtime = from_peer->v_holdtime;
199 peer->v_keepalive = from_peer->v_keepalive;
d62a17ae 200 peer->v_routeadv = from_peer->v_routeadv;
201 peer->v_gr_restart = from_peer->v_gr_restart;
202 peer->cap = from_peer->cap;
203 status = peer->status;
204 pstatus = peer->ostatus;
205 last_evt = peer->last_event;
206 last_maj_evt = peer->last_major_event;
207 peer->status = from_peer->status;
208 peer->ostatus = from_peer->ostatus;
209 peer->last_event = from_peer->last_event;
210 peer->last_major_event = from_peer->last_major_event;
211 from_peer->status = status;
212 from_peer->ostatus = pstatus;
213 from_peer->last_event = last_evt;
214 from_peer->last_major_event = last_maj_evt;
215 peer->remote_id = from_peer->remote_id;
216
217 if (from_peer->hostname != NULL) {
218 if (peer->hostname) {
219 XFREE(MTYPE_BGP_PEER_HOST, peer->hostname);
220 peer->hostname = NULL;
221 }
222
223 peer->hostname = from_peer->hostname;
224 from_peer->hostname = NULL;
225 }
226
227 if (from_peer->domainname != NULL) {
228 if (peer->domainname) {
229 XFREE(MTYPE_BGP_PEER_HOST, peer->domainname);
230 peer->domainname = NULL;
231 }
232
233 peer->domainname = from_peer->domainname;
234 from_peer->domainname = NULL;
235 }
236
05c7a1cc
QY
237 FOREACH_AFI_SAFI (afi, safi) {
238 peer->af_flags[afi][safi] = from_peer->af_flags[afi][safi];
239 peer->af_sflags[afi][safi] = from_peer->af_sflags[afi][safi];
240 peer->af_cap[afi][safi] = from_peer->af_cap[afi][safi];
241 peer->afc_nego[afi][safi] = from_peer->afc_nego[afi][safi];
242 peer->afc_adv[afi][safi] = from_peer->afc_adv[afi][safi];
243 peer->afc_recv[afi][safi] = from_peer->afc_recv[afi][safi];
244 peer->orf_plist[afi][safi] = from_peer->orf_plist[afi][safi];
245 }
d62a17ae 246
247 if (bgp_getsockname(peer) < 0) {
af4c2728 248 flog_err(
450971aa 249 EC_LIB_SOCKET,
d62a17ae 250 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
251 (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)
252 ? "accept"
253 : ""),
254 peer->host, peer->fd, from_peer->fd);
255 bgp_stop(peer);
256 bgp_stop(from_peer);
257 return NULL;
258 }
259 if (from_peer->status > Active) {
260 if (bgp_getsockname(from_peer) < 0) {
af4c2728 261 flog_err(
450971aa 262 EC_LIB_SOCKET,
d62a17ae 263 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
14454c9f 264
d62a17ae 265 (CHECK_FLAG(from_peer->sflags,
266 PEER_STATUS_ACCEPT_PEER)
267 ? "accept"
268 : ""),
269 from_peer->host, from_peer->fd, peer->fd);
270 bgp_stop(from_peer);
271 from_peer = NULL;
272 }
273 }
274
0112e9e0
QY
275
276 // Note: peer_xfer_stats() must be called with I/O turned OFF
277 if (from_peer)
278 peer_xfer_stats(peer, from_peer);
279
424ab01d
QY
280 bgp_reads_on(peer);
281 bgp_writes_on(peer);
7a86aa5a
QY
282 thread_add_timer_msec(bm->master, bgp_process_packet, peer, 0,
283 &peer->t_process_packet);
d62a17ae 284
d62a17ae 285 return (peer);
1ff9a340
DS
286}
287
718e3744 288/* Hook function called after bgp event is occered. And vty's
289 neighbor command invoke this function after making neighbor
290 structure. */
d62a17ae 291void bgp_timer_set(struct peer *peer)
718e3744 292{
d62a17ae 293 switch (peer->status) {
294 case Idle:
295 /* First entry point of peer's finite state machine. In Idle
296 status start timer is on unless peer is shutdown or peer is
297 inactive. All other timer must be turned off */
f62abc7d
PG
298 if (BGP_PEER_START_SUPPRESSED(peer) || !peer_active(peer)
299 || peer->bgp->vrf_id == VRF_UNKNOWN) {
d62a17ae 300 BGP_TIMER_OFF(peer->t_start);
301 } else {
302 BGP_TIMER_ON(peer->t_start, bgp_start_timer,
303 peer->v_start);
304 }
305 BGP_TIMER_OFF(peer->t_connect);
306 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 307 bgp_keepalives_off(peer);
d62a17ae 308 BGP_TIMER_OFF(peer->t_routeadv);
309 break;
310
311 case Connect:
312 /* After start timer is expired, the peer moves to Connect
313 status. Make sure start timer is off and connect timer is
314 on. */
315 BGP_TIMER_OFF(peer->t_start);
316 BGP_TIMER_ON(peer->t_connect, bgp_connect_timer,
317 peer->v_connect);
318 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 319 bgp_keepalives_off(peer);
d62a17ae 320 BGP_TIMER_OFF(peer->t_routeadv);
321 break;
322
323 case Active:
324 /* Active is waiting connection from remote peer. And if
325 connect timer is expired, change status to Connect. */
326 BGP_TIMER_OFF(peer->t_start);
327 /* If peer is passive mode, do not set connect timer. */
328 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE)
329 || CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
330 BGP_TIMER_OFF(peer->t_connect);
331 } else {
332 BGP_TIMER_ON(peer->t_connect, bgp_connect_timer,
333 peer->v_connect);
334 }
335 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 336 bgp_keepalives_off(peer);
d62a17ae 337 BGP_TIMER_OFF(peer->t_routeadv);
338 break;
339
340 case OpenSent:
341 /* OpenSent status. */
342 BGP_TIMER_OFF(peer->t_start);
343 BGP_TIMER_OFF(peer->t_connect);
344 if (peer->v_holdtime != 0) {
345 BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
346 peer->v_holdtime);
347 } else {
348 BGP_TIMER_OFF(peer->t_holdtime);
349 }
b72b6f4f 350 bgp_keepalives_off(peer);
d62a17ae 351 BGP_TIMER_OFF(peer->t_routeadv);
352 break;
353
354 case OpenConfirm:
355 /* OpenConfirm status. */
356 BGP_TIMER_OFF(peer->t_start);
357 BGP_TIMER_OFF(peer->t_connect);
358
359 /* If the negotiated Hold Time value is zero, then the Hold Time
360 timer and KeepAlive timers are not started. */
361 if (peer->v_holdtime == 0) {
362 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 363 bgp_keepalives_off(peer);
d62a17ae 364 } else {
365 BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
366 peer->v_holdtime);
b72b6f4f 367 bgp_keepalives_on(peer);
d62a17ae 368 }
369 BGP_TIMER_OFF(peer->t_routeadv);
370 break;
371
372 case Established:
373 /* In Established status start and connect timer is turned
374 off. */
375 BGP_TIMER_OFF(peer->t_start);
376 BGP_TIMER_OFF(peer->t_connect);
377
378 /* Same as OpenConfirm, if holdtime is zero then both holdtime
379 and keepalive must be turned off. */
380 if (peer->v_holdtime == 0) {
381 BGP_TIMER_OFF(peer->t_holdtime);
bea01226 382 bgp_keepalives_off(peer);
d62a17ae 383 } else {
384 BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
385 peer->v_holdtime);
bea01226 386 bgp_keepalives_on(peer);
d62a17ae 387 }
388 break;
389 case Deleted:
390 BGP_TIMER_OFF(peer->t_gr_restart);
391 BGP_TIMER_OFF(peer->t_gr_stale);
392 BGP_TIMER_OFF(peer->t_pmax_restart);
393 /* fallthru */
394 case Clearing:
395 BGP_TIMER_OFF(peer->t_start);
396 BGP_TIMER_OFF(peer->t_connect);
397 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 398 bgp_keepalives_off(peer);
d62a17ae 399 BGP_TIMER_OFF(peer->t_routeadv);
400 break;
718e3744 401 }
718e3744 402}
403
404/* BGP start timer. This function set BGP_Start event to thread value
405 and process event. */
d62a17ae 406static int bgp_start_timer(struct thread *thread)
718e3744 407{
d62a17ae 408 struct peer *peer;
718e3744 409
d62a17ae 410 peer = THREAD_ARG(thread);
411 peer->t_start = NULL;
718e3744 412
d62a17ae 413 if (bgp_debug_neighbor_events(peer))
414 zlog_debug("%s [FSM] Timer (start timer expire).", peer->host);
718e3744 415
d62a17ae 416 THREAD_VAL(thread) = BGP_Start;
417 bgp_event(thread); /* bgp_event unlocks peer */
718e3744 418
d62a17ae 419 return 0;
718e3744 420}
421
422/* BGP connect retry timer. */
d62a17ae 423static int bgp_connect_timer(struct thread *thread)
718e3744 424{
d62a17ae 425 struct peer *peer;
426 int ret;
427
428 peer = THREAD_ARG(thread);
424ab01d
QY
429
430 assert(!peer->t_write);
431 assert(!peer->t_read);
432
d62a17ae 433 peer->t_connect = NULL;
434
435 if (bgp_debug_neighbor_events(peer))
436 zlog_debug("%s [FSM] Timer (connect timer expire)", peer->host);
437
438 if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
439 bgp_stop(peer);
440 ret = -1;
441 } else {
442 THREAD_VAL(thread) = ConnectRetry_timer_expired;
443 bgp_event(thread); /* bgp_event unlocks peer */
444 ret = 0;
445 }
446
447 return ret;
718e3744 448}
449
450/* BGP holdtime timer. */
d62a17ae 451static int bgp_holdtime_timer(struct thread *thread)
718e3744 452{
d62a17ae 453 struct peer *peer;
718e3744 454
d62a17ae 455 peer = THREAD_ARG(thread);
456 peer->t_holdtime = NULL;
718e3744 457
d62a17ae 458 if (bgp_debug_neighbor_events(peer))
459 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
460 peer->host);
718e3744 461
d62a17ae 462 THREAD_VAL(thread) = Hold_Timer_expired;
463 bgp_event(thread); /* bgp_event unlocks peer */
718e3744 464
d62a17ae 465 return 0;
718e3744 466}
467
d62a17ae 468int bgp_routeadv_timer(struct thread *thread)
718e3744 469{
d62a17ae 470 struct peer *peer;
718e3744 471
d62a17ae 472 peer = THREAD_ARG(thread);
473 peer->t_routeadv = NULL;
718e3744 474
d62a17ae 475 if (bgp_debug_neighbor_events(peer))
476 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
477 peer->host);
718e3744 478
d62a17ae 479 peer->synctime = bgp_clock();
718e3744 480
a9794991 481 thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets, peer, 0,
424ab01d
QY
482 &peer->t_generate_updgrp_packets);
483
d62a17ae 484 /* MRAI timer will be started again when FIFO is built, no need to
485 * do it here.
486 */
487 return 0;
718e3744 488}
489
e0701b79 490/* BGP Peer Down Cause */
d62a17ae 491const char *peer_down_str[] = {"",
492 "Router ID changed",
493 "Remote AS changed",
494 "Local AS change",
495 "Cluster ID changed",
496 "Confederation identifier changed",
497 "Confederation peer changed",
498 "RR client config change",
499 "RS client config change",
500 "Update source change",
501 "Address family activated",
502 "Admin. shutdown",
503 "User reset",
504 "BGP Notification received",
505 "BGP Notification send",
506 "Peer closed the session",
507 "Neighbor deleted",
508 "Peer-group add member",
509 "Peer-group delete member",
510 "Capability changed",
511 "Passive config change",
512 "Multihop config change",
513 "NSF peer closed the session",
514 "Intf peering v6only config change",
515 "BFD down received",
516 "Interface down",
517 "Neighbor address lost"};
518
519static int bgp_graceful_restart_timer_expire(struct thread *thread)
e0701b79 520{
d62a17ae 521 struct peer *peer;
522 afi_t afi;
523 safi_t safi;
524
525 peer = THREAD_ARG(thread);
526 peer->t_gr_restart = NULL;
527
528 /* NSF delete stale route */
529 for (afi = AFI_IP; afi < AFI_MAX; afi++)
a08ca0a7 530 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++)
d62a17ae 531 if (peer->nsf[afi][safi])
532 bgp_clear_stale_route(peer, afi, safi);
533
534 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
535 BGP_TIMER_OFF(peer->t_gr_stale);
536
537 if (bgp_debug_neighbor_events(peer)) {
538 zlog_debug("%s graceful restart timer expired", peer->host);
539 zlog_debug("%s graceful restart stalepath timer stopped",
540 peer->host);
541 }
93406d87 542
d62a17ae 543 bgp_timer_set(peer);
93406d87 544
d62a17ae 545 return 0;
93406d87 546}
547
d62a17ae 548static int bgp_graceful_stale_timer_expire(struct thread *thread)
93406d87 549{
d62a17ae 550 struct peer *peer;
551 afi_t afi;
552 safi_t safi;
93406d87 553
d62a17ae 554 peer = THREAD_ARG(thread);
555 peer->t_gr_stale = NULL;
93406d87 556
d62a17ae 557 if (bgp_debug_neighbor_events(peer))
558 zlog_debug("%s graceful restart stalepath timer expired",
559 peer->host);
93406d87 560
d62a17ae 561 /* NSF delete stale route */
562 for (afi = AFI_IP; afi < AFI_MAX; afi++)
a08ca0a7 563 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++)
d62a17ae 564 if (peer->nsf[afi][safi])
565 bgp_clear_stale_route(peer, afi, safi);
93406d87 566
d62a17ae 567 return 0;
93406d87 568}
569
d62a17ae 570static int bgp_update_delay_applicable(struct bgp *bgp)
f188f2c4 571{
d62a17ae 572 /* update_delay_over flag should be reset (set to 0) for any new
573 applicability of the update-delay during BGP process lifetime.
574 And it should be set after an occurence of the update-delay is
575 over)*/
576 if (!bgp->update_delay_over)
577 return 1;
578
579 return 0;
f188f2c4
DS
580}
581
d62a17ae 582int bgp_update_delay_active(struct bgp *bgp)
f188f2c4 583{
d62a17ae 584 if (bgp->t_update_delay)
585 return 1;
f188f2c4 586
d62a17ae 587 return 0;
f188f2c4
DS
588}
589
d62a17ae 590int bgp_update_delay_configured(struct bgp *bgp)
f188f2c4 591{
d62a17ae 592 if (bgp->v_update_delay)
593 return 1;
f188f2c4 594
d62a17ae 595 return 0;
f188f2c4
DS
596}
597
598/* Do the post-processing needed when bgp comes out of the read-only mode
599 on ending the update delay. */
d62a17ae 600void bgp_update_delay_end(struct bgp *bgp)
f188f2c4 601{
d62a17ae 602 THREAD_TIMER_OFF(bgp->t_update_delay);
603 THREAD_TIMER_OFF(bgp->t_establish_wait);
604
605 /* Reset update-delay related state */
606 bgp->update_delay_over = 1;
607 bgp->established = 0;
608 bgp->restarted_peers = 0;
609 bgp->implicit_eors = 0;
610 bgp->explicit_eors = 0;
611
612 quagga_timestamp(3, bgp->update_delay_end_time,
613 sizeof(bgp->update_delay_end_time));
614
615 /*
616 * Add an end-of-initial-update marker to the main process queues so
617 * that
618 * the route advertisement timer for the peers can be started. Also set
619 * the zebra and peer update hold flags. These flags are used to achieve
620 * three stages in the update-delay post processing:
621 * 1. Finish best-path selection for all the prefixes held on the
622 * queues.
623 * (routes in BGP are updated, and peers sync queues are populated
624 * too)
625 * 2. As the eoiu mark is reached in the bgp process routine, ship all
626 * the
627 * routes to zebra. With that zebra should see updates from BGP
628 * close
629 * to each other.
630 * 3. Unblock the peer update writes. With that peer update packing
631 * with
632 * the prefixes should be at its maximum.
633 */
634 bgp_add_eoiu_mark(bgp);
635 bgp->main_zebra_update_hold = 1;
636 bgp->main_peers_update_hold = 1;
637
638 /* Resume the queue processing. This should trigger the event that would
639 take
640 care of processing any work that was queued during the read-only
641 mode. */
642 work_queue_unplug(bm->process_main_queue);
f188f2c4
DS
643}
644
cb1faec9
DS
645/**
646 * see bgp_fsm.h
647 */
d62a17ae 648void bgp_start_routeadv(struct bgp *bgp)
cb1faec9 649{
d62a17ae 650 struct listnode *node, *nnode;
651 struct peer *peer;
cb1faec9 652
d62a17ae 653 zlog_info("bgp_start_routeadv(), update hold status %d",
654 bgp->main_peers_update_hold);
4a16ae86 655
d62a17ae 656 if (bgp->main_peers_update_hold)
657 return;
4a16ae86 658
d62a17ae 659 quagga_timestamp(3, bgp->update_delay_peers_resume_time,
660 sizeof(bgp->update_delay_peers_resume_time));
4a16ae86 661
d62a17ae 662 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
663 if (peer->status != Established)
664 continue;
665 BGP_TIMER_OFF(peer->t_routeadv);
666 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
667 }
cb1faec9
DS
668}
669
670/**
671 * see bgp_fsm.h
672 */
d62a17ae 673void bgp_adjust_routeadv(struct peer *peer)
cb1faec9 674{
d62a17ae 675 time_t nowtime = bgp_clock();
676 double diff;
677 unsigned long remain;
678
679 /* Bypass checks for special case of MRAI being 0 */
680 if (peer->v_routeadv == 0) {
681 /* Stop existing timer, just in case it is running for a
682 * different
683 * duration and schedule write thread immediately.
684 */
685 if (peer->t_routeadv)
686 BGP_TIMER_OFF(peer->t_routeadv);
687
688 peer->synctime = bgp_clock();
a9794991 689 thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets,
424ab01d
QY
690 peer, 0,
691 &peer->t_generate_updgrp_packets);
d62a17ae 692 return;
693 }
694
695
696 /*
697 * CASE I:
698 * If the last update was written more than MRAI back, expire the timer
699 * instantly so that we can send the update out sooner.
700 *
701 * <------- MRAI --------->
702 * |-----------------|-----------------------|
703 * <------------- m ------------>
704 * ^ ^ ^
705 * | | |
706 * | | current time
707 * | timer start
708 * last write
709 *
710 * m > MRAI
711 */
712 diff = difftime(nowtime, peer->last_update);
713 if (diff > (double)peer->v_routeadv) {
714 BGP_TIMER_OFF(peer->t_routeadv);
715 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
716 return;
717 }
718
719 /*
720 * CASE II:
721 * - Find when to expire the MRAI timer.
722 * If MRAI timer is not active, assume we can start it now.
723 *
724 * <------- MRAI --------->
725 * |------------|-----------------------|
726 * <-------- m ----------><----- r ----->
727 * ^ ^ ^
728 * | | |
729 * | | current time
730 * | timer start
731 * last write
732 *
733 * (MRAI - m) < r
734 */
735 if (peer->t_routeadv)
736 remain = thread_timer_remain_second(peer->t_routeadv);
737 else
738 remain = peer->v_routeadv;
739 diff = peer->v_routeadv - diff;
740 if (diff <= (double)remain) {
741 BGP_TIMER_OFF(peer->t_routeadv);
742 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, diff);
743 }
cb1faec9
DS
744}
745
d62a17ae 746static int bgp_maxmed_onstartup_applicable(struct bgp *bgp)
abc920f8 747{
d62a17ae 748 if (!bgp->maxmed_onstartup_over)
749 return 1;
abc920f8 750
d62a17ae 751 return 0;
abc920f8
DS
752}
753
d62a17ae 754int bgp_maxmed_onstartup_configured(struct bgp *bgp)
abc920f8 755{
d62a17ae 756 if (bgp->v_maxmed_onstartup != BGP_MAXMED_ONSTARTUP_UNCONFIGURED)
757 return 1;
abc920f8 758
d62a17ae 759 return 0;
abc920f8
DS
760}
761
d62a17ae 762int bgp_maxmed_onstartup_active(struct bgp *bgp)
abc920f8 763{
d62a17ae 764 if (bgp->t_maxmed_onstartup)
765 return 1;
abc920f8 766
d62a17ae 767 return 0;
abc920f8
DS
768}
769
d62a17ae 770void bgp_maxmed_update(struct bgp *bgp)
abc920f8 771{
d7c0a89a
QY
772 uint8_t maxmed_active;
773 uint32_t maxmed_value;
d62a17ae 774
775 if (bgp->v_maxmed_admin) {
776 maxmed_active = 1;
777 maxmed_value = bgp->maxmed_admin_value;
778 } else if (bgp->t_maxmed_onstartup) {
779 maxmed_active = 1;
780 maxmed_value = bgp->maxmed_onstartup_value;
781 } else {
782 maxmed_active = 0;
783 maxmed_value = BGP_MAXMED_VALUE_DEFAULT;
784 }
785
786 if (bgp->maxmed_active != maxmed_active
787 || bgp->maxmed_value != maxmed_value) {
788 bgp->maxmed_active = maxmed_active;
789 bgp->maxmed_value = maxmed_value;
790
791 update_group_announce(bgp);
792 }
abc920f8
DS
793}
794
795/* The maxmed onstartup timer expiry callback. */
d62a17ae 796static int bgp_maxmed_onstartup_timer(struct thread *thread)
abc920f8 797{
d62a17ae 798 struct bgp *bgp;
abc920f8 799
d62a17ae 800 zlog_info("Max med on startup ended - timer expired.");
abc920f8 801
d62a17ae 802 bgp = THREAD_ARG(thread);
803 THREAD_TIMER_OFF(bgp->t_maxmed_onstartup);
804 bgp->maxmed_onstartup_over = 1;
abc920f8 805
d62a17ae 806 bgp_maxmed_update(bgp);
abc920f8 807
d62a17ae 808 return 0;
abc920f8
DS
809}
810
d62a17ae 811static void bgp_maxmed_onstartup_begin(struct bgp *bgp)
abc920f8 812{
d62a17ae 813 /* Applicable only once in the process lifetime on the startup */
814 if (bgp->maxmed_onstartup_over)
815 return;
abc920f8 816
d62a17ae 817 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
818 bgp->v_maxmed_onstartup);
abc920f8 819
d62a17ae 820 thread_add_timer(bm->master, bgp_maxmed_onstartup_timer, bgp,
821 bgp->v_maxmed_onstartup, &bgp->t_maxmed_onstartup);
abc920f8 822
d62a17ae 823 if (!bgp->v_maxmed_admin) {
824 bgp->maxmed_active = 1;
825 bgp->maxmed_value = bgp->maxmed_onstartup_value;
826 }
abc920f8 827
d62a17ae 828 /* Route announce to all peers should happen after this in
829 * bgp_establish() */
abc920f8
DS
830}
831
d62a17ae 832static void bgp_maxmed_onstartup_process_status_change(struct peer *peer)
abc920f8 833{
d62a17ae 834 if (peer->status == Established && !peer->bgp->established) {
835 bgp_maxmed_onstartup_begin(peer->bgp);
836 }
abc920f8
DS
837}
838
f188f2c4 839/* The update delay timer expiry callback. */
d62a17ae 840static int bgp_update_delay_timer(struct thread *thread)
f188f2c4 841{
d62a17ae 842 struct bgp *bgp;
f188f2c4 843
d62a17ae 844 zlog_info("Update delay ended - timer expired.");
f188f2c4 845
d62a17ae 846 bgp = THREAD_ARG(thread);
847 THREAD_TIMER_OFF(bgp->t_update_delay);
848 bgp_update_delay_end(bgp);
f188f2c4 849
d62a17ae 850 return 0;
f188f2c4
DS
851}
852
853/* The establish wait timer expiry callback. */
d62a17ae 854static int bgp_establish_wait_timer(struct thread *thread)
f188f2c4 855{
d62a17ae 856 struct bgp *bgp;
f188f2c4 857
d62a17ae 858 zlog_info("Establish wait - timer expired.");
f188f2c4 859
d62a17ae 860 bgp = THREAD_ARG(thread);
861 THREAD_TIMER_OFF(bgp->t_establish_wait);
862 bgp_check_update_delay(bgp);
f188f2c4 863
d62a17ae 864 return 0;
f188f2c4
DS
865}
866
867/* Steps to begin the update delay:
868 - initialize queues if needed
869 - stop the queue processing
870 - start the timer */
d62a17ae 871static void bgp_update_delay_begin(struct bgp *bgp)
f188f2c4 872{
d62a17ae 873 struct listnode *node, *nnode;
874 struct peer *peer;
f188f2c4 875
d62a17ae 876 /* Stop the processing of queued work. Enqueue shall continue */
877 work_queue_plug(bm->process_main_queue);
f188f2c4 878
d62a17ae 879 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
880 peer->update_delay_over = 0;
f188f2c4 881
d62a17ae 882 /* Start the update-delay timer */
883 thread_add_timer(bm->master, bgp_update_delay_timer, bgp,
884 bgp->v_update_delay, &bgp->t_update_delay);
f188f2c4 885
d62a17ae 886 if (bgp->v_establish_wait != bgp->v_update_delay)
887 thread_add_timer(bm->master, bgp_establish_wait_timer, bgp,
888 bgp->v_establish_wait, &bgp->t_establish_wait);
f188f2c4 889
d62a17ae 890 quagga_timestamp(3, bgp->update_delay_begin_time,
891 sizeof(bgp->update_delay_begin_time));
f188f2c4
DS
892}
893
d62a17ae 894static void bgp_update_delay_process_status_change(struct peer *peer)
f188f2c4 895{
d62a17ae 896 if (peer->status == Established) {
897 if (!peer->bgp->established++) {
898 bgp_update_delay_begin(peer->bgp);
899 zlog_info(
900 "Begin read-only mode - update-delay timer %d seconds",
901 peer->bgp->v_update_delay);
902 }
903 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_BIT_RCV))
904 bgp_update_restarted_peers(peer);
905 }
906 if (peer->ostatus == Established
907 && bgp_update_delay_active(peer->bgp)) {
908 /* Adjust the update-delay state to account for this flap.
909 NOTE: Intentionally skipping adjusting implicit_eors or
910 explicit_eors
911 counters. Extra sanity check in bgp_check_update_delay()
912 should
913 be enough to take care of any additive discrepancy in bgp eor
914 counters */
915 peer->bgp->established--;
916 peer->update_delay_over = 0;
917 }
f188f2c4
DS
918}
919
200df115 920/* Called after event occured, this function change status and reset
921 read/write and timer thread. */
d62a17ae 922void bgp_fsm_change_status(struct peer *peer, int status)
200df115 923{
1ff9a340 924
d62a17ae 925 bgp_dump_state(peer, peer->status, status);
926
927 /* Transition into Clearing or Deleted must /always/ clear all routes..
928 * (and must do so before actually changing into Deleted..
929 */
930 if (status >= Clearing) {
931 bgp_clear_route_all(peer);
932
933 /* If no route was queued for the clear-node processing,
934 * generate the
935 * completion event here. This is needed because if there are no
936 * routes
937 * to trigger the background clear-node thread, the event won't
938 * get
939 * generated and the peer would be stuck in Clearing. Note that
940 * this
941 * event is for the peer and helps the peer transition out of
942 * Clearing
943 * state; it should not be generated per (AFI,SAFI). The event
944 * is
945 * directly posted here without calling clear_node_complete() as
946 * we
947 * shouldn't do an extra unlock. This event will get processed
948 * after
949 * the state change that happens below, so peer will be in
950 * Clearing
951 * (or Deleted).
952 */
953 if (!work_queue_is_scheduled(peer->clear_node_queue))
954 BGP_EVENT_ADD(peer, Clearing_Completed);
955 }
956
957 /* Preserve old status and change into new status. */
958 peer->ostatus = peer->status;
959 peer->status = status;
960
961 /* Save event that caused status change. */
962 peer->last_major_event = peer->cur_event;
963
964 if (status == Established)
965 UNSET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
966
967 /* If max-med processing is applicable, do the necessary. */
968 if (status == Established) {
969 if (bgp_maxmed_onstartup_configured(peer->bgp)
970 && bgp_maxmed_onstartup_applicable(peer->bgp))
971 bgp_maxmed_onstartup_process_status_change(peer);
972 else
973 peer->bgp->maxmed_onstartup_over = 1;
974 }
975
976 /* If update-delay processing is applicable, do the necessary. */
977 if (bgp_update_delay_configured(peer->bgp)
978 && bgp_update_delay_applicable(peer->bgp))
979 bgp_update_delay_process_status_change(peer);
980
981 if (bgp_debug_neighbor_events(peer))
982 zlog_debug("%s went from %s to %s", peer->host,
983 lookup_msg(bgp_status_msg, peer->ostatus, NULL),
984 lookup_msg(bgp_status_msg, peer->status, NULL));
200df115 985}
986
3117b5c4 987/* Flush the event queue and ensure the peer is shut down */
d62a17ae 988static int bgp_clearing_completed(struct peer *peer)
3117b5c4 989{
d62a17ae 990 int rc = bgp_stop(peer);
1ff9a340 991
d62a17ae 992 if (rc >= 0)
993 BGP_EVENT_FLUSH(peer);
3117b5c4 994
d62a17ae 995 return rc;
3117b5c4
SH
996}
997
718e3744 998/* Administrative BGP peer stop event. */
3117b5c4 999/* May be called multiple times for the same peer */
d62a17ae 1000int bgp_stop(struct peer *peer)
718e3744 1001{
d62a17ae 1002 afi_t afi;
1003 safi_t safi;
1004 char orf_name[BUFSIZ];
1005 int ret = 0;
1006
1007 if (peer_dynamic_neighbor(peer)
1008 && !(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) {
1009 if (bgp_debug_neighbor_events(peer))
1010 zlog_debug("%s (dynamic neighbor) deleted", peer->host);
1011 peer_delete(peer);
1012 return -1;
c22767d8 1013 }
848973c7 1014
d62a17ae 1015 /* Can't do this in Clearing; events are used for state transitions */
1016 if (peer->status != Clearing) {
1017 /* Delete all existing events of the peer */
1018 BGP_EVENT_FLUSH(peer);
93406d87 1019 }
d62a17ae 1020
1021 /* Increment Dropped count. */
1022 if (peer->status == Established) {
1023 peer->dropped++;
1024
1025 /* bgp log-neighbor-changes of neighbor Down */
1026 if (bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) {
1027 struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
1028 zlog_info(
1029 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1030 peer->host,
1031 (peer->hostname) ? peer->hostname : "Unknown",
1032 vrf ? ((vrf->vrf_id != VRF_DEFAULT) ? vrf->name
1033 : "Default")
1034 : "",
1035 peer_down_str[(int)peer->last_reset]);
1036 }
1037
1038 /* graceful restart */
1039 if (peer->t_gr_stale) {
1040 BGP_TIMER_OFF(peer->t_gr_stale);
1041 if (bgp_debug_neighbor_events(peer))
1042 zlog_debug(
1043 "%s graceful restart stalepath timer stopped",
1044 peer->host);
1045 }
1046 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
1047 if (bgp_debug_neighbor_events(peer)) {
1048 zlog_debug(
1049 "%s graceful restart timer started for %d sec",
1050 peer->host, peer->v_gr_restart);
1051 zlog_debug(
1052 "%s graceful restart stalepath timer started for %d sec",
1053 peer->host, peer->bgp->stalepath_time);
1054 }
1055 BGP_TIMER_ON(peer->t_gr_restart,
1056 bgp_graceful_restart_timer_expire,
1057 peer->v_gr_restart);
1058 BGP_TIMER_ON(peer->t_gr_stale,
1059 bgp_graceful_stale_timer_expire,
1060 peer->bgp->stalepath_time);
1061 } else {
1062 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
1063
1064 for (afi = AFI_IP; afi < AFI_MAX; afi++)
996c9314
LB
1065 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN;
1066 safi++)
d62a17ae 1067 peer->nsf[afi][safi] = 0;
1068 }
1069
1070 /* set last reset time */
1071 peer->resettime = peer->uptime = bgp_clock();
1072
1073 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1074 zlog_debug("%s remove from all update group",
1075 peer->host);
1076 update_group_remove_peer_afs(peer);
1077
1078 hook_call(peer_backward_transition, peer);
1079
1080 /* Reset peer synctime */
1081 peer->synctime = 0;
1082
1083 bgp_bfd_deregister_peer(peer);
93406d87 1084 }
93406d87 1085
424ab01d 1086 /* stop keepalives */
b72b6f4f 1087 bgp_keepalives_off(peer);
424ab01d
QY
1088
1089 /* Stop read and write threads. */
1090 bgp_writes_off(peer);
1091 bgp_reads_off(peer);
1092
387f984e
QY
1093 THREAD_OFF(peer->t_connect_check_r);
1094 THREAD_OFF(peer->t_connect_check_w);
d62a17ae 1095
1096 /* Stop all timers. */
1097 BGP_TIMER_OFF(peer->t_start);
1098 BGP_TIMER_OFF(peer->t_connect);
1099 BGP_TIMER_OFF(peer->t_holdtime);
d62a17ae 1100 BGP_TIMER_OFF(peer->t_routeadv);
d62a17ae 1101
1102 /* Clear input and output buffer. */
424ab01d 1103 pthread_mutex_lock(&peer->io_mtx);
d3ecc69e 1104 {
424ab01d
QY
1105 if (peer->ibuf)
1106 stream_fifo_clean(peer->ibuf);
d3ecc69e
QY
1107 if (peer->obuf)
1108 stream_fifo_clean(peer->obuf);
424ab01d
QY
1109
1110 if (peer->ibuf_work)
74ffbfe6 1111 ringbuf_wipe(peer->ibuf_work);
424ab01d
QY
1112 if (peer->obuf_work)
1113 stream_reset(peer->obuf_work);
1114
1115 if (peer->curr) {
1116 stream_free(peer->curr);
1117 peer->curr = NULL;
1118 }
d3ecc69e 1119 }
424ab01d 1120 pthread_mutex_unlock(&peer->io_mtx);
d62a17ae 1121
1122 /* Close of file descriptor. */
1123 if (peer->fd >= 0) {
1124 close(peer->fd);
1125 peer->fd = -1;
93406d87 1126 }
1127
05c7a1cc
QY
1128 FOREACH_AFI_SAFI (afi, safi) {
1129 /* Reset all negotiated variables */
1130 peer->afc_nego[afi][safi] = 0;
1131 peer->afc_adv[afi][safi] = 0;
1132 peer->afc_recv[afi][safi] = 0;
1133
1134 /* peer address family capability flags*/
1135 peer->af_cap[afi][safi] = 0;
1136
1137 /* peer address family status flags*/
1138 peer->af_sflags[afi][safi] = 0;
1139
1140 /* Received ORF prefix-filter */
1141 peer->orf_plist[afi][safi] = NULL;
1142
1143 if ((peer->status == OpenConfirm)
1144 || (peer->status == Established)) {
1145 /* ORF received prefix-filter pnt */
1146 sprintf(orf_name, "%s.%d.%d", peer->host, afi, safi);
1147 prefix_bgp_orf_remove_all(afi, orf_name);
d62a17ae 1148 }
05c7a1cc 1149 }
d62a17ae 1150
1151 /* Reset keepalive and holdtime */
b90a8e13 1152 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) {
d62a17ae 1153 peer->v_keepalive = peer->keepalive;
1154 peer->v_holdtime = peer->holdtime;
1155 } else {
1156 peer->v_keepalive = peer->bgp->default_keepalive;
1157 peer->v_holdtime = peer->bgp->default_holdtime;
1ff9a340 1158 }
d62a17ae 1159
1160 peer->update_time = 0;
1161
1162/* Until we are sure that there is no problem about prefix count
1163 this should be commented out.*/
718e3744 1164#if 0
1165 /* Reset prefix count */
1166 peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
1167 peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
cd1964ff 1168 peer->pcount[AFI_IP][SAFI_LABELED_UNICAST] = 0;
718e3744 1169 peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
1170 peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
1171 peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
cd1964ff 1172 peer->pcount[AFI_IP6][SAFI_LABELED_UNICAST] = 0;
718e3744 1173#endif /* 0 */
1174
d62a17ae 1175 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)
1176 && !(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) {
1177 peer_delete(peer);
1178 ret = -1;
1179 } else {
1180 bgp_peer_conf_if_to_su_update(peer);
1181 }
1182
1183 return ret;
718e3744 1184}
1185
1186/* BGP peer is stoped by the error. */
d62a17ae 1187static int bgp_stop_with_error(struct peer *peer)
718e3744 1188{
d62a17ae 1189 /* Double start timer. */
1190 peer->v_start *= 2;
1191
1192 /* Overflow check. */
1193 if (peer->v_start >= (60 * 2))
1194 peer->v_start = (60 * 2);
1195
1196 if (peer_dynamic_neighbor(peer)) {
1197 if (bgp_debug_neighbor_events(peer))
1198 zlog_debug("%s (dynamic neighbor) deleted", peer->host);
1199 peer_delete(peer);
1200 return -1;
1201 }
1202
1203 return (bgp_stop(peer));
718e3744 1204}
1205
397b5bde
LR
1206
1207/* something went wrong, send notify and tear down */
d7c0a89a
QY
1208static int bgp_stop_with_notify(struct peer *peer, uint8_t code,
1209 uint8_t sub_code)
397b5bde 1210{
d62a17ae 1211 /* Send notify to remote peer */
1212 bgp_notify_send(peer, code, sub_code);
1213
1214 if (peer_dynamic_neighbor(peer)) {
1215 if (bgp_debug_neighbor_events(peer))
1216 zlog_debug("%s (dynamic neighbor) deleted", peer->host);
1217 peer_delete(peer);
1218 return -1;
1219 }
f14e6fdb 1220
d62a17ae 1221 /* Clear start timer value to default. */
1222 peer->v_start = BGP_INIT_START_TIMER;
397b5bde 1223
d62a17ae 1224 return (bgp_stop(peer));
397b5bde
LR
1225}
1226
07a16526
QY
1227/**
1228 * Determines whether a TCP session has successfully established for a peer and
1229 * events as appropriate.
1230 *
1231 * This function is called when setting up a new session. After connect() is
387f984e
QY
1232 * called on the peer's socket (in bgp_start()), the fd is passed to poll()
1233 * to wait for connection success or failure. When poll() returns, this
07a16526 1234 * function is called to evaluate the result.
387f984e
QY
1235 *
1236 * Due to differences in behavior of poll() on Linux and BSD - specifically,
1237 * the value of .revents in the case of a closed connection - this function is
1238 * scheduled both for a read and a write event. The write event is triggered
1239 * when the connection is established. A read event is triggered when the
1240 * connection is closed. Thus we need to cancel whichever one did not occur.
07a16526
QY
1241 */
1242static int bgp_connect_check(struct thread *thread)
1243{
1244 int status;
1245 socklen_t slen;
1246 int ret;
1247 struct peer *peer;
1248
1249 peer = THREAD_ARG(thread);
424ab01d
QY
1250 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
1251 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
1252 assert(!peer->t_read);
1253 assert(!peer->t_write);
07a16526 1254
387f984e
QY
1255 THREAD_OFF(peer->t_connect_check_r);
1256 THREAD_OFF(peer->t_connect_check_w);
dc1188bb 1257
07a16526
QY
1258 /* Check file descriptor. */
1259 slen = sizeof(status);
1260 ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *)&status,
1261 &slen);
1262
1263 /* If getsockopt is fail, this is fatal error. */
1264 if (ret < 0) {
54ff5e9b
DS
1265 zlog_info("can't get sockopt for nonblocking connect: %d(%s)",
1266 errno, safe_strerror(errno));
07a16526
QY
1267 BGP_EVENT_ADD(peer, TCP_fatal_error);
1268 return -1;
1269 }
1270
1271 /* When status is 0 then TCP connection is established. */
1272 if (status == 0) {
1273 BGP_EVENT_ADD(peer, TCP_connection_open);
1274 return 1;
1275 } else {
1276 if (bgp_debug_neighbor_events(peer))
54ff5e9b
DS
1277 zlog_debug("%s [Event] Connect failed %d(%s)",
1278 peer->host, status, safe_strerror(status));
07a16526
QY
1279 BGP_EVENT_ADD(peer, TCP_connection_open_failed);
1280 return 0;
1281 }
1282}
397b5bde 1283
718e3744 1284/* TCP connection open. Next we send open message to remote peer. And
1285 add read thread for reading open message. */
d62a17ae 1286static int bgp_connect_success(struct peer *peer)
718e3744 1287{
d62a17ae 1288 if (peer->fd < 0) {
e50f7cfd 1289 flog_err(EC_BGP_CONNECT,
1c50c1c0
QY
1290 "bgp_connect_success peer's fd is negative value %d",
1291 peer->fd);
d62a17ae 1292 bgp_stop(peer);
1293 return -1;
1294 }
1295
1296 if (bgp_getsockname(peer) < 0) {
450971aa 1297 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
1298 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1299 __FUNCTION__, peer->host, peer->fd);
d62a17ae 1300 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
1301 0); /* internal error */
424ab01d 1302 bgp_writes_on(peer);
d62a17ae 1303 return -1;
1304 }
1305
424ab01d 1306 bgp_reads_on(peer);
d62a17ae 1307
1308 if (bgp_debug_neighbor_events(peer)) {
1309 char buf1[SU_ADDRSTRLEN];
1310
1311 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER))
1312 zlog_debug("%s open active, local address %s",
1313 peer->host,
1314 sockunion2str(peer->su_local, buf1,
1315 SU_ADDRSTRLEN));
1316 else
1317 zlog_debug("%s passive open", peer->host);
1318 }
1319
1320 bgp_open_send(peer);
1321
1322 return 0;
718e3744 1323}
1324
1325/* TCP connect fail */
d62a17ae 1326static int bgp_connect_fail(struct peer *peer)
718e3744 1327{
d62a17ae 1328 if (peer_dynamic_neighbor(peer)) {
1329 if (bgp_debug_neighbor_events(peer))
1330 zlog_debug("%s (dynamic neighbor) deleted", peer->host);
1331 peer_delete(peer);
1332 return -1;
1333 }
1334
1335 return (bgp_stop(peer));
718e3744 1336}
1337
1338/* This function is the first starting point of all BGP connection. It
1339 try to connect to remote peer with non-blocking IO. */
d62a17ae 1340int bgp_start(struct peer *peer)
718e3744 1341{
d62a17ae 1342 int status;
1343 int connected = 0;
1344
1345 bgp_peer_conf_if_to_su_update(peer);
1346
1347 if (peer->su.sa.sa_family == AF_UNSPEC) {
1348 if (bgp_debug_neighbor_events(peer))
1349 zlog_debug(
1350 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1351 peer->host);
1352 return -1;
1353 }
1354
1355 if (BGP_PEER_START_SUPPRESSED(peer)) {
1356 if (bgp_debug_neighbor_events(peer))
e50f7cfd 1357 flog_err(EC_BGP_FSM,
1c50c1c0
QY
1358 "%s [FSM] Trying to start suppressed peer"
1359 " - this is never supposed to happen!",
1360 peer->host);
d62a17ae 1361 return -1;
1362 }
1363
1364 /* Scrub some information that might be left over from a previous,
1365 * session
1366 */
1367 /* Connection information. */
1368 if (peer->su_local) {
1369 sockunion_free(peer->su_local);
1370 peer->su_local = NULL;
1371 }
1372
1373 if (peer->su_remote) {
1374 sockunion_free(peer->su_remote);
1375 peer->su_remote = NULL;
1376 }
1377
1378 /* Clear remote router-id. */
1379 peer->remote_id.s_addr = 0;
1380
1381 /* Clear peer capability flag. */
1382 peer->cap = 0;
1383
1384 /* If the peer is passive mode, force to move to Active mode. */
1385 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE)) {
1386 BGP_EVENT_ADD(peer, TCP_connection_open_failed);
1387 return 0;
1388 }
1389
5410015a 1390 if (peer->bgp->vrf_id == VRF_UNKNOWN) {
61cf4b37 1391 if (bgp_debug_neighbor_events(peer))
af4c2728 1392 flog_err(
e50f7cfd 1393 EC_BGP_FSM,
996c9314
LB
1394 "%s [FSM] In a VRF that is not initialised yet",
1395 peer->host);
61cf4b37
PG
1396 return -1;
1397 }
1398
d62a17ae 1399 /* Register to be notified on peer up */
1400 if (peer->sort == BGP_PEER_EBGP && peer->ttl == 1
1401 && !CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1402 && !bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1403 connected = 1;
1404 else
1405 connected = 0;
1406
960035b2 1407 if (!bgp_find_or_add_nexthop(peer->bgp, peer->bgp,
d62a17ae 1408 family2afi(peer->su.sa.sa_family), NULL,
1409 peer, connected)) {
c42eab4b
DS
1410 if (bgp_zebra_num_connects()) {
1411 if (bgp_debug_neighbor_events(peer))
1412 zlog_debug("%s [FSM] Waiting for NHT",
1413 peer->host);
d62a17ae 1414
c42eab4b
DS
1415 BGP_EVENT_ADD(peer, TCP_connection_open_failed);
1416 return 0;
1417 }
718e3744 1418 }
d62a17ae 1419
424ab01d
QY
1420 assert(!peer->t_write);
1421 assert(!peer->t_read);
1422 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
1423 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
d62a17ae 1424 status = bgp_connect(peer);
1425
1426 switch (status) {
1427 case connect_error:
1428 if (bgp_debug_neighbor_events(peer))
1429 zlog_debug("%s [FSM] Connect error", peer->host);
1430 BGP_EVENT_ADD(peer, TCP_connection_open_failed);
1431 break;
1432 case connect_success:
1433 if (bgp_debug_neighbor_events(peer))
1434 zlog_debug(
1435 "%s [FSM] Connect immediately success, fd %d",
1436 peer->host, peer->fd);
1437 BGP_EVENT_ADD(peer, TCP_connection_open);
1438 break;
1439 case connect_in_progress:
1440 /* To check nonblocking connect, we wait until socket is
1441 readable or writable. */
1442 if (bgp_debug_neighbor_events(peer))
1443 zlog_debug(
1444 "%s [FSM] Non blocking connect waiting result, fd %d",
1445 peer->host, peer->fd);
1446 if (peer->fd < 0) {
e50f7cfd 1447 flog_err(EC_BGP_FSM,
1c50c1c0
QY
1448 "bgp_start peer's fd is negative value %d",
1449 peer->fd);
d62a17ae 1450 return -1;
1451 }
becedef6 1452 /*
387f984e
QY
1453 * - when the socket becomes ready, poll() will signify POLLOUT
1454 * - if it fails to connect, poll() will signify POLLHUP
1455 * - POLLHUP is handled as a 'read' event by thread.c
1456 *
1457 * therefore, we schedule both a read and a write event with
1458 * bgp_connect_check() as the handler for each and cancel the
1459 * unused event in that function.
becedef6 1460 */
424ab01d 1461 thread_add_read(bm->master, bgp_connect_check, peer, peer->fd,
387f984e
QY
1462 &peer->t_connect_check_r);
1463 thread_add_write(bm->master, bgp_connect_check, peer, peer->fd,
1464 &peer->t_connect_check_w);
d62a17ae 1465 break;
1466 }
1467 return 0;
718e3744 1468}
1469
1470/* Connect retry timer is expired when the peer status is Connect. */
d62a17ae 1471static int bgp_reconnect(struct peer *peer)
718e3744 1472{
d62a17ae 1473 if (bgp_stop(peer) < 0)
1474 return -1;
1ff9a340 1475
d62a17ae 1476 bgp_start(peer);
1477 return 0;
718e3744 1478}
1479
d62a17ae 1480static int bgp_fsm_open(struct peer *peer)
718e3744 1481{
d62a17ae 1482 /* Send keepalive and make keepalive timer */
1483 bgp_keepalive_send(peer);
718e3744 1484
d62a17ae 1485 /* Reset holdtimer value. */
1486 BGP_TIMER_OFF(peer->t_holdtime);
718e3744 1487
d62a17ae 1488 return 0;
718e3744 1489}
1490
397b5bde
LR
1491/* FSM error, unexpected event. This is error of BGP connection. So cut the
1492 peer and change to Idle status. */
d62a17ae 1493static int bgp_fsm_event_error(struct peer *peer)
397b5bde 1494{
1c50c1c0
QY
1495 flog_err(EC_BGP_FSM, "%s [FSM] unexpected packet received in state %s",
1496 peer->host, lookup_msg(bgp_status_msg, peer->status, NULL));
397b5bde 1497
d62a17ae 1498 return bgp_stop_with_notify(peer, BGP_NOTIFY_FSM_ERR, 0);
397b5bde
LR
1499}
1500
718e3744 1501/* Hold timer expire. This is error of BGP connection. So cut the
1502 peer and change to Idle status. */
d62a17ae 1503static int bgp_fsm_holdtime_expire(struct peer *peer)
718e3744 1504{
d62a17ae 1505 if (bgp_debug_neighbor_events(peer))
1506 zlog_debug("%s [FSM] Hold timer expire", peer->host);
718e3744 1507
d62a17ae 1508 return bgp_stop_with_notify(peer, BGP_NOTIFY_HOLD_ERR, 0);
718e3744 1509}
1510
727c4f87
QY
1511/**
1512 * Transition to Established state.
1513 *
1514 * Convert peer from stub to full fledged peer, set some timers, and generate
1515 * initial updates.
1516 */
d62a17ae 1517static int bgp_establish(struct peer *peer)
718e3744 1518{
d62a17ae 1519 afi_t afi;
1520 safi_t safi;
1521 int nsf_af_count = 0;
1522 int ret = 0;
1523 struct peer *other;
1524
1525 other = peer->doppelganger;
1526 peer = peer_xfer_conn(peer);
1527 if (!peer) {
e50f7cfd 1528 flog_err(EC_BGP_CONNECT, "%%Neighbor failed in xfer_conn");
d62a17ae 1529 return -1;
93406d87 1530 }
93406d87 1531
d62a17ae 1532 if (other == peer)
996c9314
LB
1533 ret = 1; /* bgp_establish specific code when xfer_conn
1534 happens. */
d62a17ae 1535
1536 /* Reset capability open status flag. */
1537 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
1538 SET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1539
1540 /* Clear start timer value to default. */
1541 peer->v_start = BGP_INIT_START_TIMER;
1542
1543 /* Increment established count. */
1544 peer->established++;
1545 bgp_fsm_change_status(peer, Established);
1546
1547 /* bgp log-neighbor-changes of neighbor Up */
1548 if (bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) {
1549 struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
1550 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1551 peer->host,
1552 (peer->hostname) ? peer->hostname : "Unknown",
1553 vrf ? ((vrf->vrf_id != VRF_DEFAULT) ? vrf->name
1554 : "Default")
1555 : "");
1556 }
1557 /* assign update-group/subgroup */
1558 update_group_adjust_peer_afs(peer);
1559
1560 /* graceful restart */
1561 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
1562 for (afi = AFI_IP; afi < AFI_MAX; afi++)
a08ca0a7 1563 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++) {
d62a17ae 1564 if (peer->afc_nego[afi][safi]
1565 && CHECK_FLAG(peer->cap, PEER_CAP_RESTART_ADV)
1566 && CHECK_FLAG(peer->af_cap[afi][safi],
1567 PEER_CAP_RESTART_AF_RCV)) {
1568 if (peer->nsf[afi][safi]
1569 && !CHECK_FLAG(
1570 peer->af_cap[afi][safi],
1571 PEER_CAP_RESTART_AF_PRESERVE_RCV))
1572 bgp_clear_stale_route(peer, afi, safi);
1573
1574 peer->nsf[afi][safi] = 1;
1575 nsf_af_count++;
1576 } else {
1577 if (peer->nsf[afi][safi])
1578 bgp_clear_stale_route(peer, afi, safi);
1579 peer->nsf[afi][safi] = 0;
1580 }
1581 }
1582
1583 if (nsf_af_count)
1584 SET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
1585 else {
1586 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
1587 if (peer->t_gr_stale) {
1588 BGP_TIMER_OFF(peer->t_gr_stale);
1589 if (bgp_debug_neighbor_events(peer))
1590 zlog_debug(
1591 "%s graceful restart stalepath timer stopped",
1592 peer->host);
1593 }
1594 }
93406d87 1595
d62a17ae 1596 if (peer->t_gr_restart) {
1597 BGP_TIMER_OFF(peer->t_gr_restart);
1598 if (bgp_debug_neighbor_events(peer))
1599 zlog_debug("%s graceful restart timer stopped",
1600 peer->host);
1601 }
718e3744 1602
d62a17ae 1603 hook_call(peer_established, peer);
1604
9eb217ff
QY
1605 /* Reset uptime, turn on keepalives, send current table. */
1606 if (!peer->v_holdtime)
1607 bgp_keepalives_on(peer);
1608
d62a17ae 1609 peer->uptime = bgp_clock();
1610
1611 /* Send route-refresh when ORF is enabled */
05c7a1cc
QY
1612 FOREACH_AFI_SAFI (afi, safi) {
1613 if (CHECK_FLAG(peer->af_cap[afi][safi],
1614 PEER_CAP_ORF_PREFIX_SM_ADV)) {
d62a17ae 1615 if (CHECK_FLAG(peer->af_cap[afi][safi],
05c7a1cc
QY
1616 PEER_CAP_ORF_PREFIX_RM_RCV))
1617 bgp_route_refresh_send(peer, afi, safi,
1618 ORF_TYPE_PREFIX,
1619 REFRESH_IMMEDIATE, 0);
1620 else if (CHECK_FLAG(peer->af_cap[afi][safi],
1621 PEER_CAP_ORF_PREFIX_RM_OLD_RCV))
1622 bgp_route_refresh_send(peer, afi, safi,
1623 ORF_TYPE_PREFIX_OLD,
1624 REFRESH_IMMEDIATE, 0);
1625 }
1626 }
d62a17ae 1627
1628 /* First update is deferred until ORF or ROUTE-REFRESH is received */
05c7a1cc
QY
1629 FOREACH_AFI_SAFI (afi, safi) {
1630 if (CHECK_FLAG(peer->af_cap[afi][safi],
1631 PEER_CAP_ORF_PREFIX_RM_ADV))
d62a17ae 1632 if (CHECK_FLAG(peer->af_cap[afi][safi],
05c7a1cc
QY
1633 PEER_CAP_ORF_PREFIX_SM_RCV)
1634 || CHECK_FLAG(peer->af_cap[afi][safi],
1635 PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
1636 SET_FLAG(peer->af_sflags[afi][safi],
1637 PEER_STATUS_ORF_WAIT_REFRESH);
1638 }
d62a17ae 1639
1640 bgp_announce_peer(peer);
1641
1642 /* Start the route advertisement timer to send updates to the peer - if
1643 * BGP
1644 * is not in read-only mode. If it is, the timer will be started at the
1645 * end
1646 * of read-only mode.
1647 */
1648 if (!bgp_update_delay_active(peer->bgp)) {
1649 BGP_TIMER_OFF(peer->t_routeadv);
1650 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
1651 }
718e3744 1652
d62a17ae 1653 if (peer->doppelganger && (peer->doppelganger->status != Deleted)) {
1654 if (bgp_debug_neighbor_events(peer))
1655 zlog_debug(
1656 "[Event] Deleting stub connection for peer %s",
1657 peer->host);
1658
1659 if (peer->doppelganger->status > Active)
1660 bgp_notify_send(peer->doppelganger, BGP_NOTIFY_CEASE,
1661 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1662 else
1663 peer_delete(peer->doppelganger);
718e3744 1664 }
1665
d62a17ae 1666 bgp_bfd_register_peer(peer);
1667 return ret;
718e3744 1668}
1669
1670/* Keepalive packet is received. */
d62a17ae 1671static int bgp_fsm_keepalive(struct peer *peer)
718e3744 1672{
d62a17ae 1673 BGP_TIMER_OFF(peer->t_holdtime);
1674 return 0;
718e3744 1675}
1676
1677/* Update packet is received. */
d62a17ae 1678static int bgp_fsm_update(struct peer *peer)
718e3744 1679{
d62a17ae 1680 BGP_TIMER_OFF(peer->t_holdtime);
1681 return 0;
718e3744 1682}
1683
1684/* This is empty event. */
d62a17ae 1685static int bgp_ignore(struct peer *peer)
718e3744 1686{
af4c2728 1687 flog_err(
e50f7cfd 1688 EC_BGP_FSM,
d62a17ae 1689 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1690 peer->host, bgp_event_str[peer->cur_event],
1691 lookup_msg(bgp_status_msg, peer->status, NULL),
1692 bgp_event_str[peer->last_event],
1693 bgp_event_str[peer->last_major_event], peer->fd);
1694 return 0;
718e3744 1695}
6b0655a2 1696
6403814c 1697/* This is to handle unexpected events.. */
d62a17ae 1698static int bgp_fsm_exeption(struct peer *peer)
6403814c 1699{
af4c2728 1700 flog_err(
e50f7cfd 1701 EC_BGP_FSM,
d62a17ae 1702 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1703 peer->host, bgp_event_str[peer->cur_event],
1704 lookup_msg(bgp_status_msg, peer->status, NULL),
1705 bgp_event_str[peer->last_event],
1706 bgp_event_str[peer->last_major_event], peer->fd);
1707 return (bgp_stop(peer));
6403814c
DS
1708}
1709
d62a17ae 1710void bgp_fsm_nht_update(struct peer *peer, int valid)
fc9a856f 1711{
d62a17ae 1712 if (!peer)
1713 return;
1714
1715 switch (peer->status) {
1716 case Idle:
1717 if (valid)
1718 BGP_EVENT_ADD(peer, BGP_Start);
1719 break;
1720 case Connect:
1721 if (!valid) {
1722 BGP_TIMER_OFF(peer->t_connect);
1723 BGP_EVENT_ADD(peer, TCP_fatal_error);
1724 }
1725 break;
1726 case Active:
1727 if (valid) {
1728 BGP_TIMER_OFF(peer->t_connect);
1729 BGP_EVENT_ADD(peer, ConnectRetry_timer_expired);
1730 }
1731 break;
1732 case OpenSent:
1733 case OpenConfirm:
1734 case Established:
1735 if (!valid && (peer->gtsm_hops == 1))
1736 BGP_EVENT_ADD(peer, TCP_fatal_error);
1737 case Clearing:
1738 case Deleted:
1739 default:
1740 break;
fc9a856f 1741 }
fc9a856f
DS
1742}
1743
1744
718e3744 1745/* Finite State Machine structure */
fda1d3e0 1746static const struct {
d62a17ae 1747 int (*func)(struct peer *);
1748 int next_state;
1749} FSM[BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] = {
1750 {
1751 /* Idle state: In Idle state, all events other than BGP_Start is
1752 ignored. With BGP_Start event, finite state machine calls
1753 bgp_start(). */
1754 {bgp_start, Connect}, /* BGP_Start */
1755 {bgp_stop, Idle}, /* BGP_Stop */
1756 {bgp_stop, Idle}, /* TCP_connection_open */
1757 {bgp_stop, Idle}, /* TCP_connection_closed */
1758 {bgp_ignore, Idle}, /* TCP_connection_open_failed */
1759 {bgp_stop, Idle}, /* TCP_fatal_error */
1760 {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */
1761 {bgp_ignore, Idle}, /* Hold_Timer_expired */
1762 {bgp_ignore, Idle}, /* KeepAlive_timer_expired */
1763 {bgp_ignore, Idle}, /* Receive_OPEN_message */
1764 {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */
1765 {bgp_ignore, Idle}, /* Receive_UPDATE_message */
1766 {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */
1767 {bgp_ignore, Idle}, /* Clearing_Completed */
1768 },
1769 {
1770 /* Connect */
1771 {bgp_ignore, Connect}, /* BGP_Start */
1772 {bgp_stop, Idle}, /* BGP_Stop */
1773 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
1774 {bgp_stop, Idle}, /* TCP_connection_closed */
1775 {bgp_connect_fail, Active}, /* TCP_connection_open_failed */
1776 {bgp_connect_fail, Idle}, /* TCP_fatal_error */
1777 {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */
1778 {bgp_fsm_exeption, Idle}, /* Hold_Timer_expired */
1779 {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */
1780 {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */
1781 {bgp_fsm_exeption, Idle}, /* Receive_KEEPALIVE_message */
1782 {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */
1783 {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */
1784 {bgp_fsm_exeption, Idle}, /* Clearing_Completed */
1785 },
1786 {
1787 /* Active, */
1788 {bgp_ignore, Active}, /* BGP_Start */
1789 {bgp_stop, Idle}, /* BGP_Stop */
1790 {bgp_connect_success, OpenSent}, /* TCP_connection_open */
1791 {bgp_stop, Idle}, /* TCP_connection_closed */
1792 {bgp_ignore, Active}, /* TCP_connection_open_failed */
1793 {bgp_fsm_exeption, Idle}, /* TCP_fatal_error */
1794 {bgp_start, Connect}, /* ConnectRetry_timer_expired */
1795 {bgp_fsm_exeption, Idle}, /* Hold_Timer_expired */
1796 {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */
1797 {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */
1798 {bgp_fsm_exeption, Idle}, /* Receive_KEEPALIVE_message */
1799 {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */
1800 {bgp_fsm_exeption, Idle}, /* Receive_NOTIFICATION_message */
1801 {bgp_fsm_exeption, Idle}, /* Clearing_Completed */
1802 },
1803 {
1804 /* OpenSent, */
1805 {bgp_ignore, OpenSent}, /* BGP_Start */
1806 {bgp_stop, Idle}, /* BGP_Stop */
1807 {bgp_stop, Active}, /* TCP_connection_open */
1808 {bgp_stop, Active}, /* TCP_connection_closed */
1809 {bgp_stop, Active}, /* TCP_connection_open_failed */
1810 {bgp_stop, Active}, /* TCP_fatal_error */
1811 {bgp_fsm_exeption, Idle}, /* ConnectRetry_timer_expired */
1812 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1813 {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */
1814 {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */
1815 {bgp_fsm_event_error, Idle}, /* Receive_KEEPALIVE_message */
1816 {bgp_fsm_event_error, Idle}, /* Receive_UPDATE_message */
1817 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1818 {bgp_fsm_exeption, Idle}, /* Clearing_Completed */
1819 },
1820 {
1821 /* OpenConfirm, */
1822 {bgp_ignore, OpenConfirm}, /* BGP_Start */
1823 {bgp_stop, Idle}, /* BGP_Stop */
1824 {bgp_stop, Idle}, /* TCP_connection_open */
1825 {bgp_stop, Idle}, /* TCP_connection_closed */
1826 {bgp_stop, Idle}, /* TCP_connection_open_failed */
1827 {bgp_stop, Idle}, /* TCP_fatal_error */
1828 {bgp_fsm_exeption, Idle}, /* ConnectRetry_timer_expired */
1829 {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */
1830 {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */
1831 {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */
1832 {bgp_establish, Established}, /* Receive_KEEPALIVE_message */
1833 {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */
1834 {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */
1835 {bgp_fsm_exeption, Idle}, /* Clearing_Completed */
1836 },
1837 {
1838 /* Established, */
1839 {bgp_ignore, Established}, /* BGP_Start */
1840 {bgp_stop, Clearing}, /* BGP_Stop */
1841 {bgp_stop, Clearing}, /* TCP_connection_open */
1842 {bgp_stop, Clearing}, /* TCP_connection_closed */
1843 {bgp_stop, Clearing}, /* TCP_connection_open_failed */
1844 {bgp_stop, Clearing}, /* TCP_fatal_error */
1845 {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
1846 {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */
03014d48
QY
1847 {bgp_ignore, Established}, /* KeepAlive_timer_expired */
1848 {bgp_stop, Clearing}, /* Receive_OPEN_message */
d62a17ae 1849 {bgp_fsm_keepalive,
1850 Established}, /* Receive_KEEPALIVE_message */
1851 {bgp_fsm_update, Established}, /* Receive_UPDATE_message */
1852 {bgp_stop_with_error,
1853 Clearing}, /* Receive_NOTIFICATION_message */
1854 {bgp_fsm_exeption, Idle}, /* Clearing_Completed */
1855 },
1856 {
1857 /* Clearing, */
1858 {bgp_ignore, Clearing}, /* BGP_Start */
1859 {bgp_stop, Clearing}, /* BGP_Stop */
1860 {bgp_stop, Clearing}, /* TCP_connection_open */
1861 {bgp_stop, Clearing}, /* TCP_connection_closed */
1862 {bgp_stop, Clearing}, /* TCP_connection_open_failed */
1863 {bgp_stop, Clearing}, /* TCP_fatal_error */
1864 {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */
1865 {bgp_stop, Clearing}, /* Hold_Timer_expired */
1866 {bgp_stop, Clearing}, /* KeepAlive_timer_expired */
1867 {bgp_stop, Clearing}, /* Receive_OPEN_message */
1868 {bgp_stop, Clearing}, /* Receive_KEEPALIVE_message */
1869 {bgp_stop, Clearing}, /* Receive_UPDATE_message */
1870 {bgp_stop, Clearing}, /* Receive_NOTIFICATION_message */
1871 {bgp_clearing_completed, Idle}, /* Clearing_Completed */
1872 },
1873 {
1874 /* Deleted, */
1875 {bgp_ignore, Deleted}, /* BGP_Start */
1876 {bgp_ignore, Deleted}, /* BGP_Stop */
1877 {bgp_ignore, Deleted}, /* TCP_connection_open */
1878 {bgp_ignore, Deleted}, /* TCP_connection_closed */
1879 {bgp_ignore, Deleted}, /* TCP_connection_open_failed */
1880 {bgp_ignore, Deleted}, /* TCP_fatal_error */
1881 {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */
1882 {bgp_ignore, Deleted}, /* Hold_Timer_expired */
1883 {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */
1884 {bgp_ignore, Deleted}, /* Receive_OPEN_message */
1885 {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */
1886 {bgp_ignore, Deleted}, /* Receive_UPDATE_message */
1887 {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */
1888 {bgp_ignore, Deleted}, /* Clearing_Completed */
1889 },
718e3744 1890};
1891
718e3744 1892/* Execute event process. */
d62a17ae 1893int bgp_event(struct thread *thread)
718e3744 1894{
d62a17ae 1895 int event;
1896 struct peer *peer;
1897 int ret;
718e3744 1898
d62a17ae 1899 peer = THREAD_ARG(thread);
1900 event = THREAD_VAL(thread);
718e3744 1901
d62a17ae 1902 ret = bgp_event_update(peer, event);
1ff9a340 1903
d62a17ae 1904 return (ret);
1ff9a340
DS
1905}
1906
d62a17ae 1907int bgp_event_update(struct peer *peer, int event)
1ff9a340 1908{
d62a17ae 1909 int next;
1910 int ret = 0;
1911 struct peer *other;
1912 int passive_conn = 0;
1913 int dyn_nbr;
1914
d8151687
QY
1915 /* default return code */
1916 ret = FSM_PEER_NOOP;
1917
d62a17ae 1918 other = peer->doppelganger;
1919 passive_conn =
1920 (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) ? 1 : 0;
1921 dyn_nbr = peer_dynamic_neighbor(peer);
1922
1923 /* Logging this event. */
1924 next = FSM[peer->status - 1][event - 1].next_state;
1925
1926 if (bgp_debug_neighbor_events(peer) && peer->status != next)
1927 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer->host,
1928 bgp_event_str[event],
1929 lookup_msg(bgp_status_msg, peer->status, NULL),
1930 lookup_msg(bgp_status_msg, next, NULL), peer->fd);
1931
1932 peer->last_event = peer->cur_event;
1933 peer->cur_event = event;
1934
1935 /* Call function. */
1936 if (FSM[peer->status - 1][event - 1].func)
1937 ret = (*(FSM[peer->status - 1][event - 1].func))(peer);
1938
d62a17ae 1939 if (ret >= 0) {
1940 if (ret == 1 && next == Established) {
1941 /* The case when doppelganger swap accurred in
1942 bgp_establish.
1943 Update the peer pointer accordingly */
d8151687 1944 ret = FSM_PEER_TRANSFERRED;
d62a17ae 1945 peer = other;
1946 }
1947
1948 /* If status is changed. */
d8151687 1949 if (next != peer->status) {
d62a17ae 1950 bgp_fsm_change_status(peer, next);
1951
becedef6
QY
1952 /*
1953 * If we're going to ESTABLISHED then we executed a
1954 * peer transfer. In this case we can either return
1955 * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED.
1956 * Opting for TRANSFERRED since transfer implies
1957 * session establishment.
1958 */
d8151687
QY
1959 if (ret != FSM_PEER_TRANSFERRED)
1960 ret = FSM_PEER_TRANSITIONED;
1961 }
1962
d62a17ae 1963 /* Make sure timer is set. */
1964 bgp_timer_set(peer);
1965
bea01226 1966 } else {
becedef6
QY
1967 /*
1968 * If we got a return value of -1, that means there was an
1969 * error, restart the FSM. Since bgp_stop() was called on the
1970 * peer. only a few fields are safe to access here. In any case
1971 * we need to indicate that the peer was stopped in the return
1972 * code.
1973 */
bea01226 1974 if (!dyn_nbr && !passive_conn && peer->bgp) {
af4c2728 1975 flog_err(
e50f7cfd 1976 EC_BGP_FSM,
bea01226
QY
1977 "%s [FSM] Failure handling event %s in state %s, "
1978 "prior events %s, %s, fd %d",
1979 peer->host, bgp_event_str[peer->cur_event],
1980 lookup_msg(bgp_status_msg, peer->status, NULL),
1981 bgp_event_str[peer->last_event],
1982 bgp_event_str[peer->last_major_event],
1983 peer->fd);
1984 bgp_stop(peer);
1985 bgp_fsm_change_status(peer, Idle);
1986 bgp_timer_set(peer);
1987 }
1988 ret = FSM_PEER_STOPPED;
d62a17ae 1989 }
bea01226 1990
d62a17ae 1991 return ret;
718e3744 1992}