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