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