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