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