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