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