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