]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_fsm.c
Merge pull request #5737 from mjstapp/zebra_disable_kern_nhs
[mirror_frr.git] / bgpd / bgp_fsm.c
CommitLineData
d62a17ae 1/* BGP-4 Finite State Machine
896014f4
DL
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 */
718e3744 21
22#include <zebra.h>
23
24#include "linklist.h"
25#include "prefix.h"
718e3744 26#include "sockunion.h"
27#include "thread.h"
28#include "log.h"
29#include "stream.h"
74ffbfe6 30#include "ringbuf.h"
718e3744 31#include "memory.h"
32#include "plist.h"
f188f2c4 33#include "workqueue.h"
3f9c7369 34#include "queue.h"
039f3a34 35#include "filter.h"
4dcadbef 36#include "command.h"
02705213 37#include "lib_errors.h"
8c48b3b6 38#include "zclient.h"
856ca177 39#include "lib/json.h"
718e3744 40#include "bgpd/bgpd.h"
41#include "bgpd/bgp_attr.h"
42#include "bgpd/bgp_debug.h"
14454c9f 43#include "bgpd/bgp_errors.h"
718e3744 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"
f188f2c4 50#include "bgpd/bgp_advertise.h"
3f9c7369 51#include "bgpd/bgp_updgrp.h"
ffd0c037 52#include "bgpd/bgp_nht.h"
c43ed2e4 53#include "bgpd/bgp_bfd.h"
4a1ab8e4 54#include "bgpd/bgp_memory.h"
03014d48 55#include "bgpd/bgp_keepalives.h"
56257a44 56#include "bgpd/bgp_io.h"
c42eab4b 57#include "bgpd/bgp_zebra.h"
6b0655a2 58
d62a17ae 59DEFINE_HOOK(peer_backward_transition, (struct peer * peer), (peer))
7d8d0eab 60DEFINE_HOOK(peer_status_changed, (struct peer * peer), (peer))
f009ff26 61extern const char *get_afi_safi_str(afi_t afi,
62 safi_t safi, bool for_json);
6403814c
DS
63/* Definition of display strings corresponding to FSM events. This should be
64 * kept consistent with the events defined in bgpd.h
65 */
2b64873d 66static const char *const bgp_event_str[] = {
d62a17ae 67 NULL,
68 "BGP_Start",
69 "BGP_Stop",
70 "TCP_connection_open",
71 "TCP_connection_closed",
72 "TCP_connection_open_failed",
73 "TCP_fatal_error",
74 "ConnectRetry_timer_expired",
75 "Hold_Timer_expired",
76 "KeepAlive_timer_expired",
77 "Receive_OPEN_message",
78 "Receive_KEEPALIVE_message",
79 "Receive_UPDATE_message",
80 "Receive_NOTIFICATION_message",
81 "Clearing_Completed",
6403814c
DS
82};
83
718e3744 84/* BGP FSM (finite state machine) has three types of functions. Type
85 one is thread functions. Type two is event functions. Type three
86 is FSM functions. Timer functions are set by bgp_timer_set
87 function. */
88
89/* BGP event function. */
d62a17ae 90int bgp_event(struct thread *);
718e3744 91
92/* BGP thread functions. */
d62a17ae 93static int bgp_start_timer(struct thread *);
94static int bgp_connect_timer(struct thread *);
95static int bgp_holdtime_timer(struct thread *);
718e3744 96
97/* BGP FSM functions. */
d62a17ae 98static int bgp_start(struct peer *);
718e3744 99
e2d3a909 100/* Register peer with NHT */
101static int bgp_peer_reg_with_nht(struct peer *peer)
102{
103 int connected = 0;
104
c8d6f0d6 105 if (peer->sort == BGP_PEER_EBGP && peer->ttl == BGP_DEFAULT_TTL
e2d3a909 106 && !CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
107 && !bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
108 connected = 1;
109
110 return bgp_find_or_add_nexthop(
111 peer->bgp, peer->bgp, family2afi(peer->su.sa.sa_family),
112 NULL, peer, connected);
113}
114
d62a17ae 115static void peer_xfer_stats(struct peer *peer_dst, struct peer *peer_src)
1ff9a340 116{
d62a17ae 117 /* Copy stats over. These are only the pre-established state stats */
118 peer_dst->open_in += peer_src->open_in;
119 peer_dst->open_out += peer_src->open_out;
120 peer_dst->keepalive_in += peer_src->keepalive_in;
121 peer_dst->keepalive_out += peer_src->keepalive_out;
122 peer_dst->notify_in += peer_src->notify_in;
123 peer_dst->notify_out += peer_src->notify_out;
124 peer_dst->dynamic_cap_in += peer_src->dynamic_cap_in;
125 peer_dst->dynamic_cap_out += peer_src->dynamic_cap_out;
1ff9a340
DS
126}
127
d62a17ae 128static struct peer *peer_xfer_conn(struct peer *from_peer)
1ff9a340 129{
d62a17ae 130 struct peer *peer;
131 afi_t afi;
132 safi_t safi;
133 int fd;
134 int status, pstatus;
135 unsigned char last_evt, last_maj_evt;
136
137 assert(from_peer != NULL);
138
139 peer = from_peer->doppelganger;
140
141 if (!peer || !CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
142 return from_peer;
143
9bf904cc
DS
144 /*
145 * Let's check that we are not going to loose known configuration
146 * state based upon doppelganger rules.
147 */
148 FOREACH_AFI_SAFI (afi, safi) {
149 if (from_peer->afc[afi][safi] != peer->afc[afi][safi]) {
150 flog_err(
151 EC_BGP_DOPPELGANGER_CONFIG,
152 "from_peer->afc[%d][%d] is not the same as what we are overwriting",
153 afi, safi);
154 return NULL;
155 }
156 }
157
d62a17ae 158 if (bgp_debug_neighbor_events(peer))
159 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
160 from_peer->host, from_peer, from_peer->fd, peer,
161 peer->fd);
162
424ab01d
QY
163 bgp_writes_off(peer);
164 bgp_reads_off(peer);
165 bgp_writes_off(from_peer);
166 bgp_reads_off(from_peer);
d62a17ae 167
74e00a55
S
168 /*
169 * Before exchanging FD remove doppelganger from
170 * keepalive peer hash. It could be possible conf peer
171 * fd is set to -1. If blocked on lock then keepalive
172 * thread can access peer pointer with fd -1.
173 */
174 bgp_keepalives_off(from_peer);
175
d62a17ae 176 BGP_TIMER_OFF(peer->t_routeadv);
424ab01d 177 BGP_TIMER_OFF(peer->t_connect);
387f984e
QY
178 BGP_TIMER_OFF(peer->t_connect_check_r);
179 BGP_TIMER_OFF(peer->t_connect_check_w);
d62a17ae 180 BGP_TIMER_OFF(from_peer->t_routeadv);
424ab01d 181 BGP_TIMER_OFF(from_peer->t_connect);
387f984e
QY
182 BGP_TIMER_OFF(from_peer->t_connect_check_r);
183 BGP_TIMER_OFF(from_peer->t_connect_check_w);
7a86aa5a 184 BGP_TIMER_OFF(from_peer->t_process_packet);
d3ecc69e 185
becedef6
QY
186 /*
187 * At this point in time, it is possible that there are packets pending
188 * on various buffers. Those need to be transferred or dropped,
189 * otherwise we'll get spurious failures during session establishment.
190 */
00dffa8c 191 frr_with_mutex(&peer->io_mtx, &from_peer->io_mtx) {
424ab01d
QY
192 fd = peer->fd;
193 peer->fd = from_peer->fd;
194 from_peer->fd = fd;
195
196 stream_fifo_clean(peer->ibuf);
d3ecc69e 197 stream_fifo_clean(peer->obuf);
d3ecc69e 198
becedef6
QY
199 /*
200 * this should never happen, since bgp_process_packet() is the
201 * only task that sets and unsets the current packet and it
202 * runs in our pthread.
203 */
424ab01d 204 if (peer->curr) {
af4c2728 205 flog_err(
e50f7cfd 206 EC_BGP_PKT_PROCESS,
424ab01d
QY
207 "[%s] Dropping pending packet on connection transfer:",
208 peer->host);
584470fb
DL
209 /* there used to be a bgp_packet_dump call here, but
210 * that's extremely confusing since there's no way to
211 * identify the packet in MRT dumps or BMP as dropped
212 * due to connection transfer.
213 */
424ab01d
QY
214 stream_free(peer->curr);
215 peer->curr = NULL;
216 }
217
218 // copy each packet from old peer's output queue to new peer
727c4f87
QY
219 while (from_peer->obuf->head)
220 stream_fifo_push(peer->obuf,
221 stream_fifo_pop(from_peer->obuf));
424ab01d
QY
222
223 // copy each packet from old peer's input queue to new peer
224 while (from_peer->ibuf->head)
225 stream_fifo_push(peer->ibuf,
226 stream_fifo_pop(from_peer->ibuf));
7db44ec8 227
74ffbfe6
QY
228 ringbuf_wipe(peer->ibuf_work);
229 ringbuf_copy(peer->ibuf_work, from_peer->ibuf_work,
230 ringbuf_remain(from_peer->ibuf_work));
d3ecc69e 231 }
d62a17ae 232
233 peer->as = from_peer->as;
234 peer->v_holdtime = from_peer->v_holdtime;
235 peer->v_keepalive = from_peer->v_keepalive;
d62a17ae 236 peer->v_routeadv = from_peer->v_routeadv;
237 peer->v_gr_restart = from_peer->v_gr_restart;
238 peer->cap = from_peer->cap;
239 status = peer->status;
240 pstatus = peer->ostatus;
241 last_evt = peer->last_event;
242 last_maj_evt = peer->last_major_event;
243 peer->status = from_peer->status;
244 peer->ostatus = from_peer->ostatus;
245 peer->last_event = from_peer->last_event;
246 peer->last_major_event = from_peer->last_major_event;
247 from_peer->status = status;
248 from_peer->ostatus = pstatus;
249 from_peer->last_event = last_evt;
250 from_peer->last_major_event = last_maj_evt;
251 peer->remote_id = from_peer->remote_id;
3577f1c5 252 peer->last_reset = from_peer->last_reset;
d62a17ae 253
d7b3cda6 254 peer->peer_gr_present_state = from_peer->peer_gr_present_state;
255 peer->peer_gr_new_status_flag = from_peer->peer_gr_new_status_flag;
256 bgp_peer_gr_flags_update(peer);
257
8c48b3b6 258 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(
259 peer->bgp,
260 peer->bgp->peer);
261
d7b3cda6 262 if (bgp_peer_gr_mode_get(peer) == PEER_DISABLE) {
263
264 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
265
266 if (CHECK_FLAG(peer->sflags,
267 PEER_STATUS_NSF_WAIT)) {
268 peer_nsf_stop(peer);
269 }
270 }
271
d62a17ae 272 if (from_peer->hostname != NULL) {
273 if (peer->hostname) {
274 XFREE(MTYPE_BGP_PEER_HOST, peer->hostname);
275 peer->hostname = NULL;
276 }
277
278 peer->hostname = from_peer->hostname;
279 from_peer->hostname = NULL;
280 }
281
282 if (from_peer->domainname != NULL) {
283 if (peer->domainname) {
284 XFREE(MTYPE_BGP_PEER_HOST, peer->domainname);
285 peer->domainname = NULL;
286 }
287
288 peer->domainname = from_peer->domainname;
289 from_peer->domainname = NULL;
290 }
291
05c7a1cc
QY
292 FOREACH_AFI_SAFI (afi, safi) {
293 peer->af_flags[afi][safi] = from_peer->af_flags[afi][safi];
294 peer->af_sflags[afi][safi] = from_peer->af_sflags[afi][safi];
295 peer->af_cap[afi][safi] = from_peer->af_cap[afi][safi];
296 peer->afc_nego[afi][safi] = from_peer->afc_nego[afi][safi];
297 peer->afc_adv[afi][safi] = from_peer->afc_adv[afi][safi];
298 peer->afc_recv[afi][safi] = from_peer->afc_recv[afi][safi];
299 peer->orf_plist[afi][safi] = from_peer->orf_plist[afi][safi];
300 }
d62a17ae 301
302 if (bgp_getsockname(peer) < 0) {
af4c2728 303 flog_err(
450971aa 304 EC_LIB_SOCKET,
d62a17ae 305 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
306 (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)
307 ? "accept"
308 : ""),
309 peer->host, peer->fd, from_peer->fd);
310 bgp_stop(peer);
311 bgp_stop(from_peer);
312 return NULL;
313 }
314 if (from_peer->status > Active) {
315 if (bgp_getsockname(from_peer) < 0) {
af4c2728 316 flog_err(
450971aa 317 EC_LIB_SOCKET,
d62a17ae 318 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
14454c9f 319
d62a17ae 320 (CHECK_FLAG(from_peer->sflags,
321 PEER_STATUS_ACCEPT_PEER)
322 ? "accept"
323 : ""),
324 from_peer->host, from_peer->fd, peer->fd);
325 bgp_stop(from_peer);
326 from_peer = NULL;
327 }
328 }
329
0112e9e0
QY
330
331 // Note: peer_xfer_stats() must be called with I/O turned OFF
332 if (from_peer)
333 peer_xfer_stats(peer, from_peer);
334
e2d3a909 335 /* Register peer for NHT. This is to allow RAs to be enabled when
336 * needed, even on a passive connection.
337 */
338 bgp_peer_reg_with_nht(peer);
339
424ab01d
QY
340 bgp_reads_on(peer);
341 bgp_writes_on(peer);
7a86aa5a
QY
342 thread_add_timer_msec(bm->master, bgp_process_packet, peer, 0,
343 &peer->t_process_packet);
d62a17ae 344
d62a17ae 345 return (peer);
1ff9a340
DS
346}
347
718e3744 348/* Hook function called after bgp event is occered. And vty's
349 neighbor command invoke this function after making neighbor
350 structure. */
d62a17ae 351void bgp_timer_set(struct peer *peer)
718e3744 352{
d62a17ae 353 switch (peer->status) {
354 case Idle:
355 /* First entry point of peer's finite state machine. In Idle
356 status start timer is on unless peer is shutdown or peer is
357 inactive. All other timer must be turned off */
f62abc7d 358 if (BGP_PEER_START_SUPPRESSED(peer) || !peer_active(peer)
dded74d5
DS
359 || (peer->bgp->inst_type != BGP_INSTANCE_TYPE_VIEW &&
360 peer->bgp->vrf_id == VRF_UNKNOWN)) {
d62a17ae 361 BGP_TIMER_OFF(peer->t_start);
362 } else {
363 BGP_TIMER_ON(peer->t_start, bgp_start_timer,
364 peer->v_start);
365 }
366 BGP_TIMER_OFF(peer->t_connect);
367 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 368 bgp_keepalives_off(peer);
d62a17ae 369 BGP_TIMER_OFF(peer->t_routeadv);
370 break;
371
372 case Connect:
373 /* After start timer is expired, the peer moves to Connect
374 status. Make sure start timer is off and connect timer is
375 on. */
376 BGP_TIMER_OFF(peer->t_start);
377 BGP_TIMER_ON(peer->t_connect, bgp_connect_timer,
378 peer->v_connect);
379 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 380 bgp_keepalives_off(peer);
d62a17ae 381 BGP_TIMER_OFF(peer->t_routeadv);
382 break;
383
384 case Active:
385 /* Active is waiting connection from remote peer. And if
386 connect timer is expired, change status to Connect. */
387 BGP_TIMER_OFF(peer->t_start);
388 /* If peer is passive mode, do not set connect timer. */
389 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE)
390 || CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
391 BGP_TIMER_OFF(peer->t_connect);
392 } else {
393 BGP_TIMER_ON(peer->t_connect, bgp_connect_timer,
394 peer->v_connect);
395 }
396 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 397 bgp_keepalives_off(peer);
d62a17ae 398 BGP_TIMER_OFF(peer->t_routeadv);
399 break;
400
401 case OpenSent:
402 /* OpenSent status. */
403 BGP_TIMER_OFF(peer->t_start);
404 BGP_TIMER_OFF(peer->t_connect);
405 if (peer->v_holdtime != 0) {
406 BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
407 peer->v_holdtime);
408 } else {
409 BGP_TIMER_OFF(peer->t_holdtime);
410 }
b72b6f4f 411 bgp_keepalives_off(peer);
d62a17ae 412 BGP_TIMER_OFF(peer->t_routeadv);
413 break;
414
415 case OpenConfirm:
416 /* OpenConfirm status. */
417 BGP_TIMER_OFF(peer->t_start);
418 BGP_TIMER_OFF(peer->t_connect);
419
420 /* If the negotiated Hold Time value is zero, then the Hold Time
421 timer and KeepAlive timers are not started. */
422 if (peer->v_holdtime == 0) {
423 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 424 bgp_keepalives_off(peer);
d62a17ae 425 } else {
426 BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
427 peer->v_holdtime);
b72b6f4f 428 bgp_keepalives_on(peer);
d62a17ae 429 }
430 BGP_TIMER_OFF(peer->t_routeadv);
431 break;
432
433 case Established:
434 /* In Established status start and connect timer is turned
435 off. */
436 BGP_TIMER_OFF(peer->t_start);
437 BGP_TIMER_OFF(peer->t_connect);
438
439 /* Same as OpenConfirm, if holdtime is zero then both holdtime
440 and keepalive must be turned off. */
441 if (peer->v_holdtime == 0) {
442 BGP_TIMER_OFF(peer->t_holdtime);
bea01226 443 bgp_keepalives_off(peer);
d62a17ae 444 } else {
445 BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
446 peer->v_holdtime);
bea01226 447 bgp_keepalives_on(peer);
d62a17ae 448 }
449 break;
450 case Deleted:
451 BGP_TIMER_OFF(peer->t_gr_restart);
452 BGP_TIMER_OFF(peer->t_gr_stale);
453 BGP_TIMER_OFF(peer->t_pmax_restart);
454 /* fallthru */
455 case Clearing:
456 BGP_TIMER_OFF(peer->t_start);
457 BGP_TIMER_OFF(peer->t_connect);
458 BGP_TIMER_OFF(peer->t_holdtime);
b72b6f4f 459 bgp_keepalives_off(peer);
d62a17ae 460 BGP_TIMER_OFF(peer->t_routeadv);
461 break;
718e3744 462 }
718e3744 463}
464
465/* BGP start timer. This function set BGP_Start event to thread value
466 and process event. */
d62a17ae 467static int bgp_start_timer(struct thread *thread)
718e3744 468{
d62a17ae 469 struct peer *peer;
718e3744 470
d62a17ae 471 peer = THREAD_ARG(thread);
472 peer->t_start = NULL;
718e3744 473
d62a17ae 474 if (bgp_debug_neighbor_events(peer))
475 zlog_debug("%s [FSM] Timer (start timer expire).", peer->host);
718e3744 476
d62a17ae 477 THREAD_VAL(thread) = BGP_Start;
478 bgp_event(thread); /* bgp_event unlocks peer */
718e3744 479
d62a17ae 480 return 0;
718e3744 481}
482
483/* BGP connect retry timer. */
d62a17ae 484static int bgp_connect_timer(struct thread *thread)
718e3744 485{
d62a17ae 486 struct peer *peer;
487 int ret;
488
489 peer = THREAD_ARG(thread);
424ab01d
QY
490
491 assert(!peer->t_write);
492 assert(!peer->t_read);
493
d62a17ae 494 peer->t_connect = NULL;
495
496 if (bgp_debug_neighbor_events(peer))
497 zlog_debug("%s [FSM] Timer (connect timer expire)", peer->host);
498
499 if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
500 bgp_stop(peer);
501 ret = -1;
502 } else {
503 THREAD_VAL(thread) = ConnectRetry_timer_expired;
504 bgp_event(thread); /* bgp_event unlocks peer */
505 ret = 0;
506 }
507
508 return ret;
718e3744 509}
510
511/* BGP holdtime timer. */
d62a17ae 512static int bgp_holdtime_timer(struct thread *thread)
718e3744 513{
d62a17ae 514 struct peer *peer;
718e3744 515
d62a17ae 516 peer = THREAD_ARG(thread);
517 peer->t_holdtime = NULL;
718e3744 518
d62a17ae 519 if (bgp_debug_neighbor_events(peer))
520 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
521 peer->host);
718e3744 522
d62a17ae 523 THREAD_VAL(thread) = Hold_Timer_expired;
524 bgp_event(thread); /* bgp_event unlocks peer */
718e3744 525
d62a17ae 526 return 0;
718e3744 527}
528
d62a17ae 529int bgp_routeadv_timer(struct thread *thread)
718e3744 530{
d62a17ae 531 struct peer *peer;
718e3744 532
d62a17ae 533 peer = THREAD_ARG(thread);
534 peer->t_routeadv = NULL;
718e3744 535
d62a17ae 536 if (bgp_debug_neighbor_events(peer))
537 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
538 peer->host);
718e3744 539
d62a17ae 540 peer->synctime = bgp_clock();
718e3744 541
a9794991 542 thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets, peer, 0,
424ab01d
QY
543 &peer->t_generate_updgrp_packets);
544
d62a17ae 545 /* MRAI timer will be started again when FIFO is built, no need to
546 * do it here.
547 */
548 return 0;
718e3744 549}
550
e0701b79 551/* BGP Peer Down Cause */
2b64873d 552const char *const peer_down_str[] = {"",
d62a17ae 553 "Router ID changed",
554 "Remote AS changed",
555 "Local AS change",
556 "Cluster ID changed",
557 "Confederation identifier changed",
558 "Confederation peer changed",
559 "RR client config change",
560 "RS client config change",
561 "Update source change",
562 "Address family activated",
563 "Admin. shutdown",
564 "User reset",
565 "BGP Notification received",
566 "BGP Notification send",
567 "Peer closed the session",
568 "Neighbor deleted",
569 "Peer-group add member",
570 "Peer-group delete member",
571 "Capability changed",
572 "Passive config change",
573 "Multihop config change",
574 "NSF peer closed the session",
575 "Intf peering v6only config change",
576 "BFD down received",
577 "Interface down",
05912a17 578 "Neighbor address lost",
3577f1c5 579 "Waiting for NHT",
05912a17
DD
580 "Waiting for Peer IPv6 LLA",
581 "Waiting for VRF to be initialized",
582 "No AFI/SAFI activated for peer"};
d62a17ae 583
584static int bgp_graceful_restart_timer_expire(struct thread *thread)
e0701b79 585{
d62a17ae 586 struct peer *peer;
587 afi_t afi;
588 safi_t safi;
589
590 peer = THREAD_ARG(thread);
591 peer->t_gr_restart = NULL;
592
593 /* NSF delete stale route */
594 for (afi = AFI_IP; afi < AFI_MAX; afi++)
a08ca0a7 595 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++)
d62a17ae 596 if (peer->nsf[afi][safi])
597 bgp_clear_stale_route(peer, afi, safi);
598
599 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
600 BGP_TIMER_OFF(peer->t_gr_stale);
601
602 if (bgp_debug_neighbor_events(peer)) {
603 zlog_debug("%s graceful restart timer expired", peer->host);
604 zlog_debug("%s graceful restart stalepath timer stopped",
605 peer->host);
606 }
93406d87 607
d62a17ae 608 bgp_timer_set(peer);
93406d87 609
d62a17ae 610 return 0;
93406d87 611}
612
d62a17ae 613static int bgp_graceful_stale_timer_expire(struct thread *thread)
93406d87 614{
d62a17ae 615 struct peer *peer;
616 afi_t afi;
617 safi_t safi;
93406d87 618
d62a17ae 619 peer = THREAD_ARG(thread);
620 peer->t_gr_stale = NULL;
93406d87 621
d62a17ae 622 if (bgp_debug_neighbor_events(peer))
623 zlog_debug("%s graceful restart stalepath timer expired",
624 peer->host);
93406d87 625
d62a17ae 626 /* NSF delete stale route */
627 for (afi = AFI_IP; afi < AFI_MAX; afi++)
a08ca0a7 628 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++)
d62a17ae 629 if (peer->nsf[afi][safi])
630 bgp_clear_stale_route(peer, afi, safi);
93406d87 631
d62a17ae 632 return 0;
93406d87 633}
634
f009ff26 635/* Selection deferral timer processing function */
636static int bgp_graceful_deferral_timer_expire(struct thread *thread)
637{
638 struct afi_safi_info *info;
639 afi_t afi;
640 safi_t safi;
641 struct bgp *bgp;
642
643 info = THREAD_ARG(thread);
644 afi = info->afi;
645 safi = info->safi;
646 bgp = info->bgp;
647
648 if (BGP_DEBUG(update, UPDATE_OUT))
13909c4f
DS
649 zlog_debug(
650 "afi %d, safi %d : graceful restart deferral timer expired",
651 afi, safi);
f009ff26 652
653 bgp->gr_info[afi][safi].t_select_deferral = NULL;
654
655 bgp->gr_info[afi][safi].eor_required = 0;
656 bgp->gr_info[afi][safi].eor_received = 0;
657 XFREE(MTYPE_TMP, info);
658
659 /* Best path selection */
660 return bgp_best_path_select_defer(bgp, afi, safi);
661}
662
d62a17ae 663static int bgp_update_delay_applicable(struct bgp *bgp)
f188f2c4 664{
d62a17ae 665 /* update_delay_over flag should be reset (set to 0) for any new
666 applicability of the update-delay during BGP process lifetime.
667 And it should be set after an occurence of the update-delay is
668 over)*/
669 if (!bgp->update_delay_over)
670 return 1;
671
672 return 0;
f188f2c4
DS
673}
674
d62a17ae 675int bgp_update_delay_active(struct bgp *bgp)
f188f2c4 676{
d62a17ae 677 if (bgp->t_update_delay)
678 return 1;
f188f2c4 679
d62a17ae 680 return 0;
f188f2c4
DS
681}
682
d62a17ae 683int bgp_update_delay_configured(struct bgp *bgp)
f188f2c4 684{
d62a17ae 685 if (bgp->v_update_delay)
686 return 1;
f188f2c4 687
d62a17ae 688 return 0;
f188f2c4
DS
689}
690
691/* Do the post-processing needed when bgp comes out of the read-only mode
692 on ending the update delay. */
d62a17ae 693void bgp_update_delay_end(struct bgp *bgp)
f188f2c4 694{
d62a17ae 695 THREAD_TIMER_OFF(bgp->t_update_delay);
696 THREAD_TIMER_OFF(bgp->t_establish_wait);
697
698 /* Reset update-delay related state */
699 bgp->update_delay_over = 1;
700 bgp->established = 0;
701 bgp->restarted_peers = 0;
702 bgp->implicit_eors = 0;
703 bgp->explicit_eors = 0;
704
705 quagga_timestamp(3, bgp->update_delay_end_time,
706 sizeof(bgp->update_delay_end_time));
707
708 /*
709 * Add an end-of-initial-update marker to the main process queues so
710 * that
711 * the route advertisement timer for the peers can be started. Also set
712 * the zebra and peer update hold flags. These flags are used to achieve
713 * three stages in the update-delay post processing:
714 * 1. Finish best-path selection for all the prefixes held on the
715 * queues.
716 * (routes in BGP are updated, and peers sync queues are populated
717 * too)
718 * 2. As the eoiu mark is reached in the bgp process routine, ship all
719 * the
720 * routes to zebra. With that zebra should see updates from BGP
721 * close
722 * to each other.
723 * 3. Unblock the peer update writes. With that peer update packing
724 * with
725 * the prefixes should be at its maximum.
726 */
727 bgp_add_eoiu_mark(bgp);
728 bgp->main_zebra_update_hold = 1;
729 bgp->main_peers_update_hold = 1;
730
731 /* Resume the queue processing. This should trigger the event that would
732 take
733 care of processing any work that was queued during the read-only
734 mode. */
735 work_queue_unplug(bm->process_main_queue);
f188f2c4
DS
736}
737
cb1faec9
DS
738/**
739 * see bgp_fsm.h
740 */
d62a17ae 741void bgp_start_routeadv(struct bgp *bgp)
cb1faec9 742{
d62a17ae 743 struct listnode *node, *nnode;
744 struct peer *peer;
cb1faec9 745
d62a17ae 746 zlog_info("bgp_start_routeadv(), update hold status %d",
747 bgp->main_peers_update_hold);
4a16ae86 748
d62a17ae 749 if (bgp->main_peers_update_hold)
750 return;
4a16ae86 751
d62a17ae 752 quagga_timestamp(3, bgp->update_delay_peers_resume_time,
753 sizeof(bgp->update_delay_peers_resume_time));
4a16ae86 754
d62a17ae 755 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
756 if (peer->status != Established)
757 continue;
758 BGP_TIMER_OFF(peer->t_routeadv);
759 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
760 }
cb1faec9
DS
761}
762
763/**
764 * see bgp_fsm.h
765 */
d62a17ae 766void bgp_adjust_routeadv(struct peer *peer)
cb1faec9 767{
d62a17ae 768 time_t nowtime = bgp_clock();
769 double diff;
770 unsigned long remain;
771
772 /* Bypass checks for special case of MRAI being 0 */
773 if (peer->v_routeadv == 0) {
774 /* Stop existing timer, just in case it is running for a
775 * different
776 * duration and schedule write thread immediately.
777 */
778 if (peer->t_routeadv)
779 BGP_TIMER_OFF(peer->t_routeadv);
780
781 peer->synctime = bgp_clock();
a9794991 782 thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets,
424ab01d
QY
783 peer, 0,
784 &peer->t_generate_updgrp_packets);
d62a17ae 785 return;
786 }
787
788
789 /*
790 * CASE I:
791 * If the last update was written more than MRAI back, expire the timer
792 * instantly so that we can send the update out sooner.
793 *
794 * <------- MRAI --------->
795 * |-----------------|-----------------------|
796 * <------------- m ------------>
797 * ^ ^ ^
798 * | | |
799 * | | current time
800 * | timer start
801 * last write
802 *
803 * m > MRAI
804 */
805 diff = difftime(nowtime, peer->last_update);
806 if (diff > (double)peer->v_routeadv) {
807 BGP_TIMER_OFF(peer->t_routeadv);
808 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
809 return;
810 }
811
812 /*
813 * CASE II:
814 * - Find when to expire the MRAI timer.
815 * If MRAI timer is not active, assume we can start it now.
816 *
817 * <------- MRAI --------->
818 * |------------|-----------------------|
819 * <-------- m ----------><----- r ----->
820 * ^ ^ ^
821 * | | |
822 * | | current time
823 * | timer start
824 * last write
825 *
826 * (MRAI - m) < r
827 */
828 if (peer->t_routeadv)
829 remain = thread_timer_remain_second(peer->t_routeadv);
830 else
831 remain = peer->v_routeadv;
832 diff = peer->v_routeadv - diff;
833 if (diff <= (double)remain) {
834 BGP_TIMER_OFF(peer->t_routeadv);
835 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, diff);
836 }
cb1faec9
DS
837}
838
d62a17ae 839static int bgp_maxmed_onstartup_applicable(struct bgp *bgp)
abc920f8 840{
d62a17ae 841 if (!bgp->maxmed_onstartup_over)
842 return 1;
abc920f8 843
d62a17ae 844 return 0;
abc920f8
DS
845}
846
d62a17ae 847int bgp_maxmed_onstartup_configured(struct bgp *bgp)
abc920f8 848{
d62a17ae 849 if (bgp->v_maxmed_onstartup != BGP_MAXMED_ONSTARTUP_UNCONFIGURED)
850 return 1;
abc920f8 851
d62a17ae 852 return 0;
abc920f8
DS
853}
854
d62a17ae 855int bgp_maxmed_onstartup_active(struct bgp *bgp)
abc920f8 856{
d62a17ae 857 if (bgp->t_maxmed_onstartup)
858 return 1;
abc920f8 859
d62a17ae 860 return 0;
abc920f8
DS
861}
862
d62a17ae 863void bgp_maxmed_update(struct bgp *bgp)
abc920f8 864{
d7c0a89a
QY
865 uint8_t maxmed_active;
866 uint32_t maxmed_value;
d62a17ae 867
868 if (bgp->v_maxmed_admin) {
869 maxmed_active = 1;
870 maxmed_value = bgp->maxmed_admin_value;
871 } else if (bgp->t_maxmed_onstartup) {
872 maxmed_active = 1;
873 maxmed_value = bgp->maxmed_onstartup_value;
874 } else {
875 maxmed_active = 0;
876 maxmed_value = BGP_MAXMED_VALUE_DEFAULT;
877 }
878
879 if (bgp->maxmed_active != maxmed_active
880 || bgp->maxmed_value != maxmed_value) {
881 bgp->maxmed_active = maxmed_active;
882 bgp->maxmed_value = maxmed_value;
883
884 update_group_announce(bgp);
885 }
abc920f8
DS
886}
887
888/* The maxmed onstartup timer expiry callback. */
d62a17ae 889static int bgp_maxmed_onstartup_timer(struct thread *thread)
abc920f8 890{
d62a17ae 891 struct bgp *bgp;
abc920f8 892
d62a17ae 893 zlog_info("Max med on startup ended - timer expired.");
abc920f8 894
d62a17ae 895 bgp = THREAD_ARG(thread);
896 THREAD_TIMER_OFF(bgp->t_maxmed_onstartup);
897 bgp->maxmed_onstartup_over = 1;
abc920f8 898
d62a17ae 899 bgp_maxmed_update(bgp);
abc920f8 900
d62a17ae 901 return 0;
abc920f8
DS
902}
903
d62a17ae 904static void bgp_maxmed_onstartup_begin(struct bgp *bgp)
abc920f8 905{
d62a17ae 906 /* Applicable only once in the process lifetime on the startup */
907 if (bgp->maxmed_onstartup_over)
908 return;
abc920f8 909
d62a17ae 910 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
911 bgp->v_maxmed_onstartup);
abc920f8 912
d62a17ae 913 thread_add_timer(bm->master, bgp_maxmed_onstartup_timer, bgp,
914 bgp->v_maxmed_onstartup, &bgp->t_maxmed_onstartup);
abc920f8 915
d62a17ae 916 if (!bgp->v_maxmed_admin) {
917 bgp->maxmed_active = 1;
918 bgp->maxmed_value = bgp->maxmed_onstartup_value;
919 }
abc920f8 920
d62a17ae 921 /* Route announce to all peers should happen after this in
922 * bgp_establish() */
abc920f8
DS
923}
924
d62a17ae 925static void bgp_maxmed_onstartup_process_status_change(struct peer *peer)
abc920f8 926{
d62a17ae 927 if (peer->status == Established && !peer->bgp->established) {
928 bgp_maxmed_onstartup_begin(peer->bgp);
929 }
abc920f8
DS
930}
931
f188f2c4 932/* The update delay timer expiry callback. */
d62a17ae 933static int bgp_update_delay_timer(struct thread *thread)
f188f2c4 934{
d62a17ae 935 struct bgp *bgp;
f188f2c4 936
d62a17ae 937 zlog_info("Update delay ended - timer expired.");
f188f2c4 938
d62a17ae 939 bgp = THREAD_ARG(thread);
940 THREAD_TIMER_OFF(bgp->t_update_delay);
941 bgp_update_delay_end(bgp);
f188f2c4 942
d62a17ae 943 return 0;
f188f2c4
DS
944}
945
946/* The establish wait timer expiry callback. */
d62a17ae 947static int bgp_establish_wait_timer(struct thread *thread)
f188f2c4 948{
d62a17ae 949 struct bgp *bgp;
f188f2c4 950
d62a17ae 951 zlog_info("Establish wait - timer expired.");
f188f2c4 952
d62a17ae 953 bgp = THREAD_ARG(thread);
954 THREAD_TIMER_OFF(bgp->t_establish_wait);
955 bgp_check_update_delay(bgp);
f188f2c4 956
d62a17ae 957 return 0;
f188f2c4
DS
958}
959
960/* Steps to begin the update delay:
961 - initialize queues if needed
962 - stop the queue processing
963 - start the timer */
d62a17ae 964static void bgp_update_delay_begin(struct bgp *bgp)
f188f2c4 965{
d62a17ae 966 struct listnode *node, *nnode;
967 struct peer *peer;
f188f2c4 968
d62a17ae 969 /* Stop the processing of queued work. Enqueue shall continue */
970 work_queue_plug(bm->process_main_queue);
f188f2c4 971
d62a17ae 972 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
973 peer->update_delay_over = 0;
f188f2c4 974
d62a17ae 975 /* Start the update-delay timer */
976 thread_add_timer(bm->master, bgp_update_delay_timer, bgp,
977 bgp->v_update_delay, &bgp->t_update_delay);
f188f2c4 978
d62a17ae 979 if (bgp->v_establish_wait != bgp->v_update_delay)
980 thread_add_timer(bm->master, bgp_establish_wait_timer, bgp,
981 bgp->v_establish_wait, &bgp->t_establish_wait);
f188f2c4 982
d62a17ae 983 quagga_timestamp(3, bgp->update_delay_begin_time,
984 sizeof(bgp->update_delay_begin_time));
f188f2c4
DS
985}
986
d62a17ae 987static void bgp_update_delay_process_status_change(struct peer *peer)
f188f2c4 988{
d62a17ae 989 if (peer->status == Established) {
990 if (!peer->bgp->established++) {
991 bgp_update_delay_begin(peer->bgp);
992 zlog_info(
993 "Begin read-only mode - update-delay timer %d seconds",
994 peer->bgp->v_update_delay);
995 }
996 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_BIT_RCV))
997 bgp_update_restarted_peers(peer);
998 }
999 if (peer->ostatus == Established
1000 && bgp_update_delay_active(peer->bgp)) {
1001 /* Adjust the update-delay state to account for this flap.
1002 NOTE: Intentionally skipping adjusting implicit_eors or
1003 explicit_eors
1004 counters. Extra sanity check in bgp_check_update_delay()
1005 should
1006 be enough to take care of any additive discrepancy in bgp eor
1007 counters */
1008 peer->bgp->established--;
1009 peer->update_delay_over = 0;
1010 }
f188f2c4
DS
1011}
1012
0437e105 1013/* Called after event occurred, this function change status and reset
200df115 1014 read/write and timer thread. */
d62a17ae 1015void bgp_fsm_change_status(struct peer *peer, int status)
200df115 1016{
36dc7588 1017 struct bgp *bgp;
1018 uint32_t peer_count;
1ff9a340 1019
36dc7588 1020 bgp = peer->bgp;
1021 peer_count = bgp->established_peers;
1022
1023 if (status == Established)
1024 bgp->established_peers++;
1025 else if ((peer->status == Established) && (status != Established))
1026 bgp->established_peers--;
1027
1cfe005d
DS
1028 if (bgp_debug_neighbor_events(peer)) {
1029 struct vrf *vrf = vrf_lookup_by_id(bgp->vrf_id);
1030
1031 zlog_debug("%s : vrf %s(%u), Status: %s established_peers %u", __func__,
1032 vrf ? vrf->name : "Unknown", bgp->vrf_id,
1033 lookup_msg(bgp_status_msg, status, NULL),
1034 bgp->established_peers);
1035 }
1036
36dc7588 1037 /* Set to router ID to the value provided by RIB if there are no peers
1038 * in the established state and peer count did not change
1039 */
1040 if ((peer_count != bgp->established_peers) &&
1041 (bgp->established_peers == 0))
1042 bgp_router_id_zebra_bump(bgp->vrf_id, NULL);
1043
d62a17ae 1044 /* Transition into Clearing or Deleted must /always/ clear all routes..
1045 * (and must do so before actually changing into Deleted..
1046 */
1047 if (status >= Clearing) {
1048 bgp_clear_route_all(peer);
1049
1050 /* If no route was queued for the clear-node processing,
1051 * generate the
1052 * completion event here. This is needed because if there are no
1053 * routes
1054 * to trigger the background clear-node thread, the event won't
1055 * get
1056 * generated and the peer would be stuck in Clearing. Note that
1057 * this
1058 * event is for the peer and helps the peer transition out of
1059 * Clearing
1060 * state; it should not be generated per (AFI,SAFI). The event
1061 * is
1062 * directly posted here without calling clear_node_complete() as
1063 * we
1064 * shouldn't do an extra unlock. This event will get processed
1065 * after
1066 * the state change that happens below, so peer will be in
1067 * Clearing
1068 * (or Deleted).
1069 */
1070 if (!work_queue_is_scheduled(peer->clear_node_queue))
1071 BGP_EVENT_ADD(peer, Clearing_Completed);
1072 }
1073
1074 /* Preserve old status and change into new status. */
1075 peer->ostatus = peer->status;
1076 peer->status = status;
1077
1078 /* Save event that caused status change. */
1079 peer->last_major_event = peer->cur_event;
1080
7d8d0eab
MKS
1081 /* Operations after status change */
1082 hook_call(peer_status_changed, peer);
1083
d62a17ae 1084 if (status == Established)
1085 UNSET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
1086
1087 /* If max-med processing is applicable, do the necessary. */
1088 if (status == Established) {
1089 if (bgp_maxmed_onstartup_configured(peer->bgp)
1090 && bgp_maxmed_onstartup_applicable(peer->bgp))
1091 bgp_maxmed_onstartup_process_status_change(peer);
1092 else
1093 peer->bgp->maxmed_onstartup_over = 1;
1094 }
1095
1096 /* If update-delay processing is applicable, do the necessary. */
1097 if (bgp_update_delay_configured(peer->bgp)
1098 && bgp_update_delay_applicable(peer->bgp))
1099 bgp_update_delay_process_status_change(peer);
1100
1101 if (bgp_debug_neighbor_events(peer))
1102 zlog_debug("%s went from %s to %s", peer->host,
1103 lookup_msg(bgp_status_msg, peer->ostatus, NULL),
1104 lookup_msg(bgp_status_msg, peer->status, NULL));
200df115 1105}
1106
3117b5c4 1107/* Flush the event queue and ensure the peer is shut down */
d62a17ae 1108static int bgp_clearing_completed(struct peer *peer)
3117b5c4 1109{
d62a17ae 1110 int rc = bgp_stop(peer);
1ff9a340 1111
d62a17ae 1112 if (rc >= 0)
1113 BGP_EVENT_FLUSH(peer);
3117b5c4 1114
d62a17ae 1115 return rc;
3117b5c4
SH
1116}
1117
718e3744 1118/* Administrative BGP peer stop event. */
3117b5c4 1119/* May be called multiple times for the same peer */
d62a17ae 1120int bgp_stop(struct peer *peer)
718e3744 1121{
d62a17ae 1122 afi_t afi;
1123 safi_t safi;
1124 char orf_name[BUFSIZ];
1125 int ret = 0;
f009ff26 1126 struct bgp *bgp = peer->bgp;
1127 struct graceful_restart_info *gr_info = NULL;
d62a17ae 1128
2ba1fe69 1129 peer->nsf_af_count = 0;
1130
d62a17ae 1131 if (peer_dynamic_neighbor(peer)
1132 && !(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) {
1133 if (bgp_debug_neighbor_events(peer))
1134 zlog_debug("%s (dynamic neighbor) deleted", peer->host);
1135 peer_delete(peer);
1136 return -1;
c22767d8 1137 }
848973c7 1138
d62a17ae 1139 /* Can't do this in Clearing; events are used for state transitions */
1140 if (peer->status != Clearing) {
1141 /* Delete all existing events of the peer */
1142 BGP_EVENT_FLUSH(peer);
93406d87 1143 }
d62a17ae 1144
1145 /* Increment Dropped count. */
1146 if (peer->status == Established) {
1147 peer->dropped++;
1148
1149 /* bgp log-neighbor-changes of neighbor Down */
1150 if (bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) {
1151 struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
2986cac2 1152
d62a17ae 1153 zlog_info(
1154 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1155 peer->host,
1156 (peer->hostname) ? peer->hostname : "Unknown",
5742e42b
DS
1157 vrf ? ((vrf->vrf_id != VRF_DEFAULT)
1158 ? vrf->name
1159 : VRF_DEFAULT_NAME)
d62a17ae 1160 : "",
1161 peer_down_str[(int)peer->last_reset]);
1162 }
1163
1164 /* graceful restart */
1165 if (peer->t_gr_stale) {
1166 BGP_TIMER_OFF(peer->t_gr_stale);
1167 if (bgp_debug_neighbor_events(peer))
1168 zlog_debug(
1169 "%s graceful restart stalepath timer stopped",
1170 peer->host);
1171 }
1172 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
1173 if (bgp_debug_neighbor_events(peer)) {
1174 zlog_debug(
1175 "%s graceful restart timer started for %d sec",
1176 peer->host, peer->v_gr_restart);
1177 zlog_debug(
1178 "%s graceful restart stalepath timer started for %d sec",
1179 peer->host, peer->bgp->stalepath_time);
1180 }
1181 BGP_TIMER_ON(peer->t_gr_restart,
1182 bgp_graceful_restart_timer_expire,
1183 peer->v_gr_restart);
1184 BGP_TIMER_ON(peer->t_gr_stale,
1185 bgp_graceful_stale_timer_expire,
1186 peer->bgp->stalepath_time);
1187 } else {
1188 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
1189
1190 for (afi = AFI_IP; afi < AFI_MAX; afi++)
996c9314
LB
1191 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN;
1192 safi++)
d62a17ae 1193 peer->nsf[afi][safi] = 0;
1194 }
1195
f009ff26 1196 /* If peer reset before receiving EOR, decrement EOR count and
1197 * cancel the selection deferral timer if there are no
1198 * pending EOR messages to be received
1199 */
1200 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)) {
1201 FOREACH_AFI_SAFI (afi, safi) {
13909c4f
DS
1202 if (!peer->afc_nego[afi][safi]
1203 || CHECK_FLAG(peer->af_sflags[afi][safi],
1204 PEER_STATUS_EOR_RECEIVED))
1205 continue;
1206
1207 gr_info = &bgp->gr_info[afi][safi];
1208 if (!gr_info)
1209 continue;
1210
1211 if (gr_info->eor_required)
1212 gr_info->eor_required--;
1213
1214 if (BGP_DEBUG(update, UPDATE_OUT))
1215 zlog_debug("peer %s, EOR_required %d",
1216 peer->host,
1217 gr_info->eor_required);
1218
1219 /* There is no pending EOR message */
1220 if (gr_info->eor_required == 0) {
1221 BGP_TIMER_OFF(
f009ff26 1222 gr_info->t_select_deferral);
13909c4f 1223 gr_info->eor_received = 0;
f009ff26 1224 }
1225 }
1226 }
1227
d62a17ae 1228 /* set last reset time */
1229 peer->resettime = peer->uptime = bgp_clock();
1230
1231 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1232 zlog_debug("%s remove from all update group",
1233 peer->host);
1234 update_group_remove_peer_afs(peer);
1235
1236 hook_call(peer_backward_transition, peer);
1237
1238 /* Reset peer synctime */
1239 peer->synctime = 0;
93406d87 1240 }
93406d87 1241
424ab01d 1242 /* stop keepalives */
b72b6f4f 1243 bgp_keepalives_off(peer);
424ab01d
QY
1244
1245 /* Stop read and write threads. */
1246 bgp_writes_off(peer);
1247 bgp_reads_off(peer);
1248
387f984e
QY
1249 THREAD_OFF(peer->t_connect_check_r);
1250 THREAD_OFF(peer->t_connect_check_w);
d62a17ae 1251
1252 /* Stop all timers. */
1253 BGP_TIMER_OFF(peer->t_start);
1254 BGP_TIMER_OFF(peer->t_connect);
1255 BGP_TIMER_OFF(peer->t_holdtime);
d62a17ae 1256 BGP_TIMER_OFF(peer->t_routeadv);
d62a17ae 1257
1258 /* Clear input and output buffer. */
00dffa8c 1259 frr_with_mutex(&peer->io_mtx) {
424ab01d
QY
1260 if (peer->ibuf)
1261 stream_fifo_clean(peer->ibuf);
d3ecc69e
QY
1262 if (peer->obuf)
1263 stream_fifo_clean(peer->obuf);
424ab01d
QY
1264
1265 if (peer->ibuf_work)
74ffbfe6 1266 ringbuf_wipe(peer->ibuf_work);
424ab01d
QY
1267 if (peer->obuf_work)
1268 stream_reset(peer->obuf_work);
1269
1270 if (peer->curr) {
1271 stream_free(peer->curr);
1272 peer->curr = NULL;
1273 }
d3ecc69e 1274 }
d62a17ae 1275
1276 /* Close of file descriptor. */
1277 if (peer->fd >= 0) {
1278 close(peer->fd);
1279 peer->fd = -1;
93406d87 1280 }
1281
05c7a1cc
QY
1282 FOREACH_AFI_SAFI (afi, safi) {
1283 /* Reset all negotiated variables */
1284 peer->afc_nego[afi][safi] = 0;
1285 peer->afc_adv[afi][safi] = 0;
1286 peer->afc_recv[afi][safi] = 0;
1287
1288 /* peer address family capability flags*/
1289 peer->af_cap[afi][safi] = 0;
1290
1291 /* peer address family status flags*/
1292 peer->af_sflags[afi][safi] = 0;
1293
1294 /* Received ORF prefix-filter */
1295 peer->orf_plist[afi][safi] = NULL;
1296
1297 if ((peer->status == OpenConfirm)
1298 || (peer->status == Established)) {
1299 /* ORF received prefix-filter pnt */
1300 sprintf(orf_name, "%s.%d.%d", peer->host, afi, safi);
1301 prefix_bgp_orf_remove_all(afi, orf_name);
d62a17ae 1302 }
05c7a1cc 1303 }
d62a17ae 1304
1305 /* Reset keepalive and holdtime */
b90a8e13 1306 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) {
d62a17ae 1307 peer->v_keepalive = peer->keepalive;
1308 peer->v_holdtime = peer->holdtime;
1309 } else {
1310 peer->v_keepalive = peer->bgp->default_keepalive;
1311 peer->v_holdtime = peer->bgp->default_holdtime;
1ff9a340 1312 }
d62a17ae 1313
1314 peer->update_time = 0;
1315
1316/* Until we are sure that there is no problem about prefix count
1317 this should be commented out.*/
718e3744 1318#if 0
1319 /* Reset prefix count */
1320 peer->pcount[AFI_IP][SAFI_UNICAST] = 0;
1321 peer->pcount[AFI_IP][SAFI_MULTICAST] = 0;
cd1964ff 1322 peer->pcount[AFI_IP][SAFI_LABELED_UNICAST] = 0;
718e3744 1323 peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0;
1324 peer->pcount[AFI_IP6][SAFI_UNICAST] = 0;
1325 peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0;
cd1964ff 1326 peer->pcount[AFI_IP6][SAFI_LABELED_UNICAST] = 0;
718e3744 1327#endif /* 0 */
1328
d62a17ae 1329 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)
1330 && !(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) {
1331 peer_delete(peer);
1332 ret = -1;
1333 } else {
1334 bgp_peer_conf_if_to_su_update(peer);
1335 }
d62a17ae 1336 return ret;
718e3744 1337}
1338
1339/* BGP peer is stoped by the error. */
d62a17ae 1340static int bgp_stop_with_error(struct peer *peer)
718e3744 1341{
d62a17ae 1342 /* Double start timer. */
1343 peer->v_start *= 2;
1344
1345 /* Overflow check. */
1346 if (peer->v_start >= (60 * 2))
1347 peer->v_start = (60 * 2);
1348
1349 if (peer_dynamic_neighbor(peer)) {
1350 if (bgp_debug_neighbor_events(peer))
1351 zlog_debug("%s (dynamic neighbor) deleted", peer->host);
1352 peer_delete(peer);
1353 return -1;
1354 }
1355
1356 return (bgp_stop(peer));
718e3744 1357}
1358
397b5bde
LR
1359
1360/* something went wrong, send notify and tear down */
d7c0a89a
QY
1361static int bgp_stop_with_notify(struct peer *peer, uint8_t code,
1362 uint8_t sub_code)
397b5bde 1363{
d62a17ae 1364 /* Send notify to remote peer */
1365 bgp_notify_send(peer, code, sub_code);
1366
1367 if (peer_dynamic_neighbor(peer)) {
1368 if (bgp_debug_neighbor_events(peer))
1369 zlog_debug("%s (dynamic neighbor) deleted", peer->host);
1370 peer_delete(peer);
1371 return -1;
1372 }
f14e6fdb 1373
d62a17ae 1374 /* Clear start timer value to default. */
1375 peer->v_start = BGP_INIT_START_TIMER;
397b5bde 1376
d62a17ae 1377 return (bgp_stop(peer));
397b5bde
LR
1378}
1379
07a16526
QY
1380/**
1381 * Determines whether a TCP session has successfully established for a peer and
1382 * events as appropriate.
1383 *
1384 * This function is called when setting up a new session. After connect() is
387f984e
QY
1385 * called on the peer's socket (in bgp_start()), the fd is passed to poll()
1386 * to wait for connection success or failure. When poll() returns, this
07a16526 1387 * function is called to evaluate the result.
387f984e
QY
1388 *
1389 * Due to differences in behavior of poll() on Linux and BSD - specifically,
1390 * the value of .revents in the case of a closed connection - this function is
1391 * scheduled both for a read and a write event. The write event is triggered
1392 * when the connection is established. A read event is triggered when the
1393 * connection is closed. Thus we need to cancel whichever one did not occur.
07a16526
QY
1394 */
1395static int bgp_connect_check(struct thread *thread)
1396{
1397 int status;
1398 socklen_t slen;
1399 int ret;
1400 struct peer *peer;
1401
1402 peer = THREAD_ARG(thread);
424ab01d
QY
1403 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
1404 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
1405 assert(!peer->t_read);
1406 assert(!peer->t_write);
07a16526 1407
387f984e
QY
1408 THREAD_OFF(peer->t_connect_check_r);
1409 THREAD_OFF(peer->t_connect_check_w);
dc1188bb 1410
07a16526
QY
1411 /* Check file descriptor. */
1412 slen = sizeof(status);
1413 ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *)&status,
1414 &slen);
1415
1416 /* If getsockopt is fail, this is fatal error. */
1417 if (ret < 0) {
4cb5e18b 1418 zlog_err("can't get sockopt for nonblocking connect: %d(%s)",
54ff5e9b 1419 errno, safe_strerror(errno));
07a16526
QY
1420 BGP_EVENT_ADD(peer, TCP_fatal_error);
1421 return -1;
1422 }
1423
1424 /* When status is 0 then TCP connection is established. */
1425 if (status == 0) {
1426 BGP_EVENT_ADD(peer, TCP_connection_open);
1427 return 1;
1428 } else {
1429 if (bgp_debug_neighbor_events(peer))
54ff5e9b
DS
1430 zlog_debug("%s [Event] Connect failed %d(%s)",
1431 peer->host, status, safe_strerror(status));
07a16526
QY
1432 BGP_EVENT_ADD(peer, TCP_connection_open_failed);
1433 return 0;
1434 }
1435}
397b5bde 1436
718e3744 1437/* TCP connection open. Next we send open message to remote peer. And
1438 add read thread for reading open message. */
d62a17ae 1439static int bgp_connect_success(struct peer *peer)
718e3744 1440{
d62a17ae 1441 if (peer->fd < 0) {
e50f7cfd 1442 flog_err(EC_BGP_CONNECT,
1c50c1c0
QY
1443 "bgp_connect_success peer's fd is negative value %d",
1444 peer->fd);
d62a17ae 1445 bgp_stop(peer);
1446 return -1;
1447 }
1448
1449 if (bgp_getsockname(peer) < 0) {
450971aa 1450 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
1451 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1452 __FUNCTION__, peer->host, peer->fd);
0e35025e
DA
1453 bgp_notify_send(
1454 peer, BGP_NOTIFY_FSM_ERR,
1455 BGP_NOTIFY_SUBCODE_UNSPECIFIC); /* internal error */
424ab01d 1456 bgp_writes_on(peer);
d62a17ae 1457 return -1;
1458 }
1459
424ab01d 1460 bgp_reads_on(peer);
d62a17ae 1461
1462 if (bgp_debug_neighbor_events(peer)) {
1463 char buf1[SU_ADDRSTRLEN];
1464
1465 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER))
1466 zlog_debug("%s open active, local address %s",
1467 peer->host,
1468 sockunion2str(peer->su_local, buf1,
1469 SU_ADDRSTRLEN));
1470 else
1471 zlog_debug("%s passive open", peer->host);
1472 }
1473
1474 bgp_open_send(peer);
1475
1476 return 0;
718e3744 1477}
1478
1479/* TCP connect fail */
d62a17ae 1480static int bgp_connect_fail(struct peer *peer)
718e3744 1481{
d62a17ae 1482 if (peer_dynamic_neighbor(peer)) {
1483 if (bgp_debug_neighbor_events(peer))
1484 zlog_debug("%s (dynamic neighbor) deleted", peer->host);
1485 peer_delete(peer);
1486 return -1;
1487 }
1488
1489 return (bgp_stop(peer));
718e3744 1490}
1491
1492/* This function is the first starting point of all BGP connection. It
1493 try to connect to remote peer with non-blocking IO. */
d62a17ae 1494int bgp_start(struct peer *peer)
718e3744 1495{
d62a17ae 1496 int status;
d62a17ae 1497
1498 bgp_peer_conf_if_to_su_update(peer);
1499
1500 if (peer->su.sa.sa_family == AF_UNSPEC) {
1501 if (bgp_debug_neighbor_events(peer))
1502 zlog_debug(
1503 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1504 peer->host);
3577f1c5 1505 peer->last_reset = PEER_DOWN_NBR_ADDR;
d62a17ae 1506 return -1;
1507 }
1508
1509 if (BGP_PEER_START_SUPPRESSED(peer)) {
1510 if (bgp_debug_neighbor_events(peer))
e50f7cfd 1511 flog_err(EC_BGP_FSM,
1c50c1c0
QY
1512 "%s [FSM] Trying to start suppressed peer"
1513 " - this is never supposed to happen!",
1514 peer->host);
d62a17ae 1515 return -1;
1516 }
1517
1518 /* Scrub some information that might be left over from a previous,
1519 * session
1520 */
1521 /* Connection information. */
1522 if (peer->su_local) {
1523 sockunion_free(peer->su_local);
1524 peer->su_local = NULL;
1525 }
1526
1527 if (peer->su_remote) {
1528 sockunion_free(peer->su_remote);
1529 peer->su_remote = NULL;
1530 }
1531
1532 /* Clear remote router-id. */
1533 peer->remote_id.s_addr = 0;
1534
1535 /* Clear peer capability flag. */
1536 peer->cap = 0;
1537
1538 /* If the peer is passive mode, force to move to Active mode. */
1539 if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE)) {
1540 BGP_EVENT_ADD(peer, TCP_connection_open_failed);
1541 return 0;
1542 }
1543
dded74d5
DS
1544 if (peer->bgp->inst_type != BGP_INSTANCE_TYPE_VIEW &&
1545 peer->bgp->vrf_id == VRF_UNKNOWN) {
61cf4b37 1546 if (bgp_debug_neighbor_events(peer))
af4c2728 1547 flog_err(
e50f7cfd 1548 EC_BGP_FSM,
996c9314
LB
1549 "%s [FSM] In a VRF that is not initialised yet",
1550 peer->host);
3577f1c5 1551 peer->last_reset = PEER_DOWN_VRF_UNINIT;
61cf4b37
PG
1552 return -1;
1553 }
1554
e2d3a909 1555 /* Register peer for NHT. If next hop is already resolved, proceed
1556 * with connection setup, else wait.
1557 */
1558 if (!bgp_peer_reg_with_nht(peer)) {
c42eab4b
DS
1559 if (bgp_zebra_num_connects()) {
1560 if (bgp_debug_neighbor_events(peer))
1561 zlog_debug("%s [FSM] Waiting for NHT",
1562 peer->host);
3577f1c5 1563 peer->last_reset = PEER_DOWN_WAITING_NHT;
c42eab4b
DS
1564 BGP_EVENT_ADD(peer, TCP_connection_open_failed);
1565 return 0;
1566 }
718e3744 1567 }
d62a17ae 1568
424ab01d
QY
1569 assert(!peer->t_write);
1570 assert(!peer->t_read);
1571 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON));
1572 assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON));
d62a17ae 1573 status = bgp_connect(peer);
1574
1575 switch (status) {
1576 case connect_error:
1577 if (bgp_debug_neighbor_events(peer))
1578 zlog_debug("%s [FSM] Connect error", peer->host);
1579 BGP_EVENT_ADD(peer, TCP_connection_open_failed);
1580 break;
1581 case connect_success:
1582 if (bgp_debug_neighbor_events(peer))
1583 zlog_debug(
1584 "%s [FSM] Connect immediately success, fd %d",
1585 peer->host, peer->fd);
1586 BGP_EVENT_ADD(peer, TCP_connection_open);
1587 break;
1588 case connect_in_progress:
1589 /* To check nonblocking connect, we wait until socket is
1590 readable or writable. */
1591 if (bgp_debug_neighbor_events(peer))
1592 zlog_debug(
1593 "%s [FSM] Non blocking connect waiting result, fd %d",
1594 peer->host, peer->fd);
1595 if (peer->fd < 0) {
e50f7cfd 1596 flog_err(EC_BGP_FSM,
1c50c1c0
QY
1597 "bgp_start peer's fd is negative value %d",
1598 peer->fd);
d62a17ae 1599 return -1;
1600 }
becedef6 1601 /*
387f984e
QY
1602 * - when the socket becomes ready, poll() will signify POLLOUT
1603 * - if it fails to connect, poll() will signify POLLHUP
1604 * - POLLHUP is handled as a 'read' event by thread.c
1605 *
1606 * therefore, we schedule both a read and a write event with
1607 * bgp_connect_check() as the handler for each and cancel the
1608 * unused event in that function.
becedef6 1609 */
424ab01d 1610 thread_add_read(bm->master, bgp_connect_check, peer, peer->fd,
387f984e
QY
1611 &peer->t_connect_check_r);
1612 thread_add_write(bm->master, bgp_connect_check, peer, peer->fd,
1613 &peer->t_connect_check_w);
d62a17ae 1614 break;
1615 }
1616 return 0;
718e3744 1617}
1618
1619/* Connect retry timer is expired when the peer status is Connect. */
d62a17ae 1620static int bgp_reconnect(struct peer *peer)
718e3744 1621{
d62a17ae 1622 if (bgp_stop(peer) < 0)
1623 return -1;
1ff9a340 1624
8c48b3b6 1625 /* Send graceful restart capabilty */
1626 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(
1627 peer->bgp, peer->bgp->peer);
1628
d62a17ae 1629 bgp_start(peer);
1630 return 0;
718e3744 1631}
1632
d62a17ae 1633static int bgp_fsm_open(struct peer *peer)
718e3744 1634{
d62a17ae 1635 /* Send keepalive and make keepalive timer */
1636 bgp_keepalive_send(peer);
718e3744 1637
d62a17ae 1638 /* Reset holdtimer value. */
1639 BGP_TIMER_OFF(peer->t_holdtime);
718e3744 1640
d62a17ae 1641 return 0;
718e3744 1642}
1643
397b5bde
LR
1644/* FSM error, unexpected event. This is error of BGP connection. So cut the
1645 peer and change to Idle status. */
d62a17ae 1646static int bgp_fsm_event_error(struct peer *peer)
397b5bde 1647{
1c50c1c0
QY
1648 flog_err(EC_BGP_FSM, "%s [FSM] unexpected packet received in state %s",
1649 peer->host, lookup_msg(bgp_status_msg, peer->status, NULL));
397b5bde 1650
d62a17ae 1651 return bgp_stop_with_notify(peer, BGP_NOTIFY_FSM_ERR, 0);
397b5bde
LR
1652}
1653
718e3744 1654/* Hold timer expire. This is error of BGP connection. So cut the
1655 peer and change to Idle status. */
d62a17ae 1656static int bgp_fsm_holdtime_expire(struct peer *peer)
718e3744 1657{
d62a17ae 1658 if (bgp_debug_neighbor_events(peer))
1659 zlog_debug("%s [FSM] Hold timer expire", peer->host);
718e3744 1660
d62a17ae 1661 return bgp_stop_with_notify(peer, BGP_NOTIFY_HOLD_ERR, 0);
718e3744 1662}
1663
f009ff26 1664/* Start the selection deferral timer thread for the specified AFI, SAFI */
1665static int bgp_start_deferral_timer(struct bgp *bgp, afi_t afi, safi_t safi,
1666 struct graceful_restart_info *gr_info)
1667{
1668 struct afi_safi_info *thread_info;
1669
1670 /* If the deferral timer is active, then increment eor count */
1671 if (gr_info->t_select_deferral) {
1672 gr_info->eor_required++;
1673 return 0;
1674 }
1675
1676 /* Start the deferral timer when the first peer enabled for the graceful
1677 * restart is established
1678 */
1679 if (gr_info->eor_required == 0) {
1680 thread_info = XMALLOC(MTYPE_TMP, sizeof(struct afi_safi_info));
f009ff26 1681
1682 thread_info->afi = afi;
1683 thread_info->safi = safi;
1684 thread_info->bgp = bgp;
1685
13909c4f
DS
1686 thread_add_timer(bm->master, bgp_graceful_deferral_timer_expire,
1687 thread_info, bgp->select_defer_time,
1688 &gr_info->t_select_deferral);
f009ff26 1689 }
f009ff26 1690 gr_info->eor_required++;
8c48b3b6 1691 /* Send message to RIB indicating route update pending */
1692 if (gr_info->af_enabled[afi][safi] == false) {
1693 gr_info->af_enabled[afi][safi] = true;
1694 /* Send message to RIB */
1695 bgp_zebra_update(afi, safi, bgp->vrf_id,
1696 ZEBRA_CLIENT_ROUTE_UPDATE_PENDING);
1697 }
f009ff26 1698 if (BGP_DEBUG(update, UPDATE_OUT))
1699 zlog_debug("Started the deferral timer for %s eor_required %d",
1700 get_afi_safi_str(afi, safi, false),
1701 gr_info->eor_required);
1702 return 0;
1703}
1704
1705/* Update the graceful restart information for the specified AFI, SAFI */
1706static int bgp_update_gr_info(struct peer *peer, afi_t afi, safi_t safi)
1707{
1708 struct graceful_restart_info *gr_info;
1709 struct bgp *bgp = peer->bgp;
1710 int ret = 0;
1711
1712 if ((afi < AFI_IP) || (afi >= AFI_MAX)) {
1713 if (BGP_DEBUG(update, UPDATE_OUT))
1714 zlog_debug("%s : invalid afi %d", __func__, afi);
1715 return -1;
1716 }
1717
1718 if ((safi < SAFI_UNICAST) || (safi > SAFI_MPLS_VPN)) {
1719 if (BGP_DEBUG(update, UPDATE_OUT))
1720 zlog_debug("%s : invalid safi %d", __func__, safi);
1721 return -1;
1722 }
1723
1724 /* Restarting router */
1725 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer) &&
1726 BGP_PEER_RESTARTING_MODE(peer)) {
1727 /* Check if the forwarding state is preserved */
1728 if (bgp_flag_check(bgp, BGP_FLAG_GR_PRESERVE_FWD)) {
1729 gr_info = &(bgp->gr_info[afi][safi]);
1730 ret = bgp_start_deferral_timer(bgp, afi, safi, gr_info);
1731 }
1732 }
2ba1fe69 1733 return ret;
f009ff26 1734}
1735
727c4f87
QY
1736/**
1737 * Transition to Established state.
1738 *
1739 * Convert peer from stub to full fledged peer, set some timers, and generate
1740 * initial updates.
1741 */
d62a17ae 1742static int bgp_establish(struct peer *peer)
718e3744 1743{
d62a17ae 1744 afi_t afi;
1745 safi_t safi;
1746 int nsf_af_count = 0;
1747 int ret = 0;
1748 struct peer *other;
f009ff26 1749 int status;
d62a17ae 1750
1751 other = peer->doppelganger;
1752 peer = peer_xfer_conn(peer);
1753 if (!peer) {
e50f7cfd 1754 flog_err(EC_BGP_CONNECT, "%%Neighbor failed in xfer_conn");
d62a17ae 1755 return -1;
93406d87 1756 }
93406d87 1757
d62a17ae 1758 if (other == peer)
996c9314
LB
1759 ret = 1; /* bgp_establish specific code when xfer_conn
1760 happens. */
d62a17ae 1761
1762 /* Reset capability open status flag. */
1763 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN))
1764 SET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1765
1766 /* Clear start timer value to default. */
1767 peer->v_start = BGP_INIT_START_TIMER;
1768
1769 /* Increment established count. */
1770 peer->established++;
1771 bgp_fsm_change_status(peer, Established);
1772
1773 /* bgp log-neighbor-changes of neighbor Up */
1774 if (bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) {
1775 struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
1776 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1777 peer->host,
1778 (peer->hostname) ? peer->hostname : "Unknown",
5742e42b
DS
1779 vrf ? ((vrf->vrf_id != VRF_DEFAULT)
1780 ? vrf->name
1781 : VRF_DEFAULT_NAME)
d62a17ae 1782 : "");
1783 }
1784 /* assign update-group/subgroup */
1785 update_group_adjust_peer_afs(peer);
1786
1787 /* graceful restart */
1788 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
f009ff26 1789 if (bgp_debug_neighbor_events(peer)) {
1790 if (BGP_PEER_RESTARTING_MODE(peer))
1791 zlog_debug("peer %s BGP_RESTARTING_MODE",
1792 peer->host);
1793 else if (BGP_PEER_HELPER_MODE(peer))
1794 zlog_debug("peer %s BGP_HELPER_MODE",
1795 peer->host);
1796 }
d62a17ae 1797 for (afi = AFI_IP; afi < AFI_MAX; afi++)
a08ca0a7 1798 for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++) {
d62a17ae 1799 if (peer->afc_nego[afi][safi]
1800 && CHECK_FLAG(peer->cap, PEER_CAP_RESTART_ADV)
1801 && CHECK_FLAG(peer->af_cap[afi][safi],
1802 PEER_CAP_RESTART_AF_RCV)) {
1803 if (peer->nsf[afi][safi]
1804 && !CHECK_FLAG(
1805 peer->af_cap[afi][safi],
1806 PEER_CAP_RESTART_AF_PRESERVE_RCV))
1807 bgp_clear_stale_route(peer, afi, safi);
1808
1809 peer->nsf[afi][safi] = 1;
1810 nsf_af_count++;
1811 } else {
1812 if (peer->nsf[afi][safi])
1813 bgp_clear_stale_route(peer, afi, safi);
1814 peer->nsf[afi][safi] = 0;
1815 }
f009ff26 1816 /* Update the graceful restart information */
1817 if (peer->afc_nego[afi][safi]) {
1818 if (!BGP_SELECT_DEFER_DISABLE(peer->bgp)) {
1819 status = bgp_update_gr_info(peer, afi,
1820 safi);
1821 if (status < 0)
2ba1fe69 1822 zlog_err("Error in updating graceful restart for %s",
1823 get_afi_safi_str(afi,
9e3b51a7 1824 safi, false));
1825 } else {
1826 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(
1827 peer) &&
1828 BGP_PEER_RESTARTING_MODE(peer)
1829 && bgp_flag_check(peer->bgp,
1830 BGP_FLAG_GR_PRESERVE_FWD))
1831 peer->bgp->gr_info[afi][safi]
1832 .eor_required++;
f009ff26 1833 }
1834 }
d62a17ae 1835 }
1836
794b37d5 1837 peer->nsf_af_count = nsf_af_count;
1838
d62a17ae 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 }
93406d87 1851
d62a17ae 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 }
718e3744 1858
9eb217ff
QY
1859 /* Reset uptime, turn on keepalives, send current table. */
1860 if (!peer->v_holdtime)
1861 bgp_keepalives_on(peer);
1862
d62a17ae 1863 peer->uptime = bgp_clock();
1864
1865 /* Send route-refresh when ORF is enabled */
05c7a1cc
QY
1866 FOREACH_AFI_SAFI (afi, safi) {
1867 if (CHECK_FLAG(peer->af_cap[afi][safi],
1868 PEER_CAP_ORF_PREFIX_SM_ADV)) {
d62a17ae 1869 if (CHECK_FLAG(peer->af_cap[afi][safi],
05c7a1cc
QY
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 }
d62a17ae 1881
1882 /* First update is deferred until ORF or ROUTE-REFRESH is received */
05c7a1cc
QY
1883 FOREACH_AFI_SAFI (afi, safi) {
1884 if (CHECK_FLAG(peer->af_cap[afi][safi],
1885 PEER_CAP_ORF_PREFIX_RM_ADV))
d62a17ae 1886 if (CHECK_FLAG(peer->af_cap[afi][safi],
05c7a1cc
QY
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 }
d62a17ae 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 }
718e3744 1906
d62a17ae 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);
718e3744 1918 }
1919
19bd3dff
DS
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
d62a17ae 1929 bgp_bfd_register_peer(peer);
1930 return ret;
718e3744 1931}
1932
1933/* Keepalive packet is received. */
d62a17ae 1934static int bgp_fsm_keepalive(struct peer *peer)
718e3744 1935{
d62a17ae 1936 BGP_TIMER_OFF(peer->t_holdtime);
1937 return 0;
718e3744 1938}
1939
1940/* Update packet is received. */
d62a17ae 1941static int bgp_fsm_update(struct peer *peer)
718e3744 1942{
d62a17ae 1943 BGP_TIMER_OFF(peer->t_holdtime);
1944 return 0;
718e3744 1945}
1946
1947/* This is empty event. */
d62a17ae 1948static int bgp_ignore(struct peer *peer)
718e3744 1949{
af4c2728 1950 flog_err(
e50f7cfd 1951 EC_BGP_FSM,
d62a17ae 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;
718e3744 1958}
6b0655a2 1959
6403814c 1960/* This is to handle unexpected events.. */
d62a17ae 1961static int bgp_fsm_exeption(struct peer *peer)
6403814c 1962{
af4c2728 1963 flog_err(
e50f7cfd 1964 EC_BGP_FSM,
d62a17ae 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));
6403814c
DS
1971}
1972
fc04a677 1973void bgp_fsm_event_update(struct peer *peer, int valid)
fc9a856f 1974{
d62a17ae 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;
fc9a856f 2004 }
fc9a856f
DS
2005}
2006
718e3744 2007/* Finite State Machine structure */
fda1d3e0 2008static const struct {
d62a17ae 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 */
53b4aaec 2079 {bgp_fsm_event_error, Idle}, /* Receive_NOTIFICATION_message */
d62a17ae 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 */
03014d48
QY
2109 {bgp_ignore, Established}, /* KeepAlive_timer_expired */
2110 {bgp_stop, Clearing}, /* Receive_OPEN_message */
d62a17ae 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 },
718e3744 2152};
2153
718e3744 2154/* Execute event process. */
d62a17ae 2155int bgp_event(struct thread *thread)
718e3744 2156{
d62a17ae 2157 int event;
2158 struct peer *peer;
2159 int ret;
718e3744 2160
d62a17ae 2161 peer = THREAD_ARG(thread);
2162 event = THREAD_VAL(thread);
718e3744 2163
d62a17ae 2164 ret = bgp_event_update(peer, event);
1ff9a340 2165
d62a17ae 2166 return (ret);
1ff9a340
DS
2167}
2168
d62a17ae 2169int bgp_event_update(struct peer *peer, int event)
1ff9a340 2170{
d62a17ae 2171 int next;
2172 int ret = 0;
2173 struct peer *other;
2174 int passive_conn = 0;
2175 int dyn_nbr;
2176
d8151687
QY
2177 /* default return code */
2178 ret = FSM_PEER_NOOP;
2179
d62a17ae 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
d62a17ae 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 */
d8151687 2206 ret = FSM_PEER_TRANSFERRED;
d62a17ae 2207 peer = other;
2208 }
2209
2210 /* If status is changed. */
d8151687 2211 if (next != peer->status) {
d62a17ae 2212 bgp_fsm_change_status(peer, next);
2213
becedef6
QY
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 */
d8151687
QY
2221 if (ret != FSM_PEER_TRANSFERRED)
2222 ret = FSM_PEER_TRANSITIONED;
2223 }
2224
d62a17ae 2225 /* Make sure timer is set. */
2226 bgp_timer_set(peer);
2227
bea01226 2228 } else {
becedef6
QY
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 */
bea01226 2236 if (!dyn_nbr && !passive_conn && peer->bgp) {
af4c2728 2237 flog_err(
e50f7cfd 2238 EC_BGP_FSM,
bea01226
QY
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;
d62a17ae 2251 }
bea01226 2252
d62a17ae 2253 return ret;
718e3744 2254}
794b37d5 2255/* BGP GR Code */
2256
2257int 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(
2ba1fe69 2270 "%s [BGP_GR] Peer: (%s) :",
794b37d5 2271 __func__, peer->host);
2272
2273 peer_old_state = bgp_peer_gr_mode_get(peer);
2274
2275 if (peer_old_state == PEER_GLOBAL_INHERIT) {
2276
2277 /*
2278 *Reset only these peers and send a
2279 *new open message with the change capabilities.
2280 *Considering the mode to be "global_new_state" and
2281 *do all operation accordingly
2282 */
2283
2284 switch (global_new_state) {
794b37d5 2285 case GLOBAL_HELPER:
794b37d5 2286 BGP_PEER_GR_HELPER_ENABLE(peer);
2287 break;
2288 case GLOBAL_GR:
794b37d5 2289 BGP_PEER_GR_ENABLE(peer);
2290 break;
2291 case GLOBAL_DISABLE:
794b37d5 2292 BGP_PEER_GR_DISABLE(peer);
2293 break;
2294 case GLOBAL_INVALID:
794b37d5 2295 zlog_debug(
4a6e80fb 2296 "%s [BGP_GR] GLOBAL_INVALID",
794b37d5 2297 __func__);
2298 return BGP_ERR_GR_OPERATION_FAILED;
794b37d5 2299 }
2300 }
2301 }
2302
2303 bgp->global_gr_present_state = global_new_state;
2304
794b37d5 2305 return BGP_GR_SUCCESS;
2306}
2307
2ba1fe69 2308int bgp_gr_update_all(struct bgp *bgp, int global_gr_cmd)
794b37d5 2309{
2310 enum global_mode global_new_state = GLOBAL_INVALID;
2311 enum global_mode global_old_state = GLOBAL_INVALID;
2312
2313 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2314 zlog_debug(
2ba1fe69 2315 "%s [BGP_GR]START: global_gr_cmd :%s:",
2316 __func__, print_global_gr_cmd(global_gr_cmd));
794b37d5 2317
2318 global_old_state = bgp_global_gr_mode_get(bgp);
2319
2ba1fe69 2320 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2321 zlog_debug(
2322 "[BGP_GR] global_old_gr_state :%s:",
2323 print_global_gr_mode(global_old_state));
794b37d5 2324
2ba1fe69 2325 if (global_old_state != GLOBAL_INVALID) {
794b37d5 2326 global_new_state =
2ba1fe69 2327 bgp->GLOBAL_GR_FSM[global_old_state][global_gr_cmd];
2328
2329 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2330 zlog_debug(
2331 "[BGP_GR] global_new_gr_state :%s:",
2332 print_global_gr_mode(global_new_state));
794b37d5 2333 } else {
2ba1fe69 2334 zlog_err(
2335 "%s [BGP_GR] global_old_state == GLOBAL_INVALID",
794b37d5 2336 __func__);
2337 return BGP_ERR_GR_OPERATION_FAILED;
2338 }
2339
2340 if (global_new_state == GLOBAL_INVALID) {
2ba1fe69 2341 zlog_err(
2342 "%s [BGP_GR] global_new_state == GLOBAL_INVALID",
794b37d5 2343 __func__);
2344 return BGP_ERR_GR_INVALID_CMD;
2345 }
2346 if (global_new_state == global_old_state) {
2347 /* Trace msg */
2ba1fe69 2348 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2349 zlog_debug(
2350 "%s [BGP_GR] global_new_state == global_old_state :%s",
2351 __func__, print_global_gr_mode(global_new_state));
794b37d5 2352 return BGP_GR_NO_OPERATION;
2353 }
2354
2355 return bgp_gr_lookup_n_update_all_peer(bgp,
2356 global_new_state,
2357 global_old_state);
2358}
2359
2ba1fe69 2360const char *print_peer_gr_mode(enum peer_mode pr_mode)
2361{
2362 const char *peer_gr_mode = NULL;
2363
2364 switch (pr_mode) {
2365 case PEER_HELPER:
2366 peer_gr_mode = "PEER_HELPER";
2367 break;
2368 case PEER_GR:
2369 peer_gr_mode = "PEER_GR";
2370 break;
2371 case PEER_DISABLE:
2372 peer_gr_mode = "PEER_DISABLE";
2373 break;
2374 case PEER_INVALID:
2375 peer_gr_mode = "PEER_INVALID";
2376 break;
2377 case PEER_GLOBAL_INHERIT:
2378 peer_gr_mode = "PEER_GLOBAL_INHERIT";
2379 break;
2380 }
2381
2382 return peer_gr_mode;
2383}
2384
2385const char *print_peer_gr_cmd(enum peer_gr_command pr_gr_cmd)
2386{
2387 const char *peer_gr_cmd = NULL;
2388
2389 switch (pr_gr_cmd) {
2390 case PEER_GR_CMD:
2391 peer_gr_cmd = "PEER_GR_CMD";
2392 break;
2393 case NO_PEER_GR_CMD:
2394 peer_gr_cmd = "NO_PEER_GR_CMD";
2395 break;
2396 case PEER_DISABLE_CMD:
2397 peer_gr_cmd = "PEER_GR_CMD";
2398 break;
2399 case NO_PEER_DISABLE_CMD:
2400 peer_gr_cmd = "NO_PEER_GR_CMD";
2401 break;
2402 case PEER_HELPER_CMD:
2403 peer_gr_cmd = "PEER_HELPER_CMD";
2404 break;
2405 case NO_PEER_HELPER_CMD:
2406 peer_gr_cmd = "NO_PEER_HELPER_CMD";
2407 break;
2408 }
2409
2410 return peer_gr_cmd;
2411}
2412
2413const char *print_global_gr_mode(enum global_mode gl_mode)
2414{
2415 const char *global_gr_mode = NULL;
2416
2417 switch (gl_mode) {
2418 case GLOBAL_HELPER:
2419 global_gr_mode = "GLOBAL_HELPER";
2420 break;
2421 case GLOBAL_GR:
2422 global_gr_mode = "GLOBAL_GR";
2423 break;
2424 case GLOBAL_DISABLE:
2425 global_gr_mode = "GLOBAL_DISABLE";
2426 break;
2427 case GLOBAL_INVALID:
2428 global_gr_mode = "GLOBAL_INVALID";
2429 break;
2430 }
2431
2432 return global_gr_mode;
2433}
2434
2435const char *print_global_gr_cmd(enum global_gr_command gl_gr_cmd)
2436{
2437 const char *global_gr_cmd = NULL;
2438
2439 switch (gl_gr_cmd) {
2440 case GLOBAL_GR_CMD:
2441 global_gr_cmd = "GLOBAL_GR_CMD";
2442 break;
2443 case NO_GLOBAL_GR_CMD:
2444 global_gr_cmd = "NO_GLOBAL_GR_CMD";
2445 break;
2446 case GLOBAL_DISABLE_CMD:
2447 global_gr_cmd = "GLOBAL_DISABLE_CMD";
2448 break;
2449 case NO_GLOBAL_DISABLE_CMD:
2450 global_gr_cmd = "NO_GLOBAL_DISABLE_CMD";
2451 break;
2452 }
2453
2454 return global_gr_cmd;
2455}
2456
794b37d5 2457enum global_mode bgp_global_gr_mode_get(struct bgp *bgp)
2458{
2459 return bgp->global_gr_present_state;
2460}
2461
2462enum peer_mode bgp_peer_gr_mode_get(struct peer *peer)
2463{
2464 return peer->peer_gr_present_state;
2465}
2466
2467int bgp_neighbor_graceful_restart(struct peer *peer,
2ba1fe69 2468 int peer_gr_cmd)
794b37d5 2469{
2470 enum peer_mode peer_new_state = PEER_INVALID;
2471 enum peer_mode peer_old_state = PEER_INVALID;
2472 struct bgp_peer_gr peer_state;
2473 int result = BGP_GR_FAILURE;
2474
2475 /*
2476 * fetch peer_old_state from peer structure also
2477 * fetch global_old_state from bgp structure,
2478 * peer had a back pointer to bgpo struct ;
2479 */
2480
2481 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2482 zlog_debug(
2ba1fe69 2483 "%s [BGP_GR] START:Peer: (%s) : peer_gr_cmd :%s:",
2484 __func__, peer->host, print_peer_gr_cmd(peer_gr_cmd));
794b37d5 2485
2486 peer_old_state = bgp_peer_gr_mode_get(peer);
2487
2488 if (peer_old_state == PEER_INVALID) {
794b37d5 2489 zlog_debug(
2ba1fe69 2490 "[BGP_GR] peer_old_state == Invalid state !");
794b37d5 2491 return BGP_ERR_GR_OPERATION_FAILED;
2492 }
2493
2ba1fe69 2494 peer_state = peer->PEER_GR_FSM[peer_old_state][peer_gr_cmd];
794b37d5 2495 peer_new_state = peer_state.next_state;
2496
2497 if (peer_new_state == PEER_INVALID) {
794b37d5 2498 zlog_debug(
2ba1fe69 2499 "[BGP_GR] Invalid bgp graceful restart command used !");
794b37d5 2500 return BGP_ERR_GR_INVALID_CMD;
2501 }
2502
2503 if (peer_new_state != peer_old_state) {
2504 result = peer_state.action_fun(peer,
2505 peer_old_state,
2506 peer_new_state);
2507 } else {
2ba1fe69 2508 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2509 zlog_debug(
2510 "[BGP_GR] peer_old_state == peer_new_state !");
794b37d5 2511 return BGP_GR_NO_OPERATION;
2512 }
2513
2514 if (result == BGP_GR_SUCCESS) {
2515
2516 /* Update the mode i.e peer_new_state into the peer structure */
2517 peer->peer_gr_present_state = peer_new_state;
794b37d5 2518 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2ba1fe69 2519 zlog_debug("[BGP_GR] Succesfully change the state of the peer to : %s : !",
2520 print_peer_gr_mode(peer_new_state));
794b37d5 2521
2522 return BGP_GR_SUCCESS;
2523 }
2524
2525 return result;
2526}
2527
2528unsigned int bgp_peer_gr_action(struct peer *peer,
2529 int old_peer_state, int new_peer_state)
2530{
2531 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2532 zlog_debug(
2ba1fe69 2533 "%s [BGP_GR] Move peer from old_peer_state :%s: to new_peer_state :%s: !!!!",
2534 __func__, print_peer_gr_mode(old_peer_state),
2535 print_peer_gr_mode(new_peer_state));
794b37d5 2536
2537 int bgp_gr_global_mode = GLOBAL_INVALID;
2538 unsigned int ret = BGP_GR_FAILURE;
2539
2540 if (old_peer_state == new_peer_state) {
2541 /* Nothing to do over here as the present and old state is the same */
794b37d5 2542 return BGP_GR_NO_OPERATION;
2543 }
2544 if ((old_peer_state == PEER_INVALID) ||
2545 (new_peer_state == PEER_INVALID)) {
2546 /* something bad happend , print error message */
2547 return BGP_ERR_GR_INVALID_CMD;
2548 }
2549
2550 bgp_gr_global_mode = bgp_global_gr_mode_get(peer->bgp);
2551
2552 if ((old_peer_state == PEER_GLOBAL_INHERIT) &&
2553 (new_peer_state != PEER_GLOBAL_INHERIT)) {
2554
2555 /* fetch the Mode running in the Global state machine
2556 *from the bgp structure into a variable called
2557 *bgp_gr_global_mode
2558 */
2559
2560 /* Here we are checking if the
2561 *1. peer_new_state == global_mode == helper_mode
2562 *2. peer_new_state == global_mode == GR_mode
2563 *3. peer_new_state == global_mode == disabled_mode
2564 */
2565
2566 BGP_PEER_GR_GLOBAL_INHERIT_UNSET(peer);
2567
2568 if (new_peer_state == bgp_gr_global_mode) {
2569 /*This is incremental updates i.e no tear down
2570 *of the existing session
2571 *as the peer is already working in the same mode.
2572 */
794b37d5 2573 ret = BGP_GR_SUCCESS;
2574 } else {
2575 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2576 zlog_debug(
2ba1fe69 2577 "[BGP_GR] Peer state changed from :%s ",
2578 print_peer_gr_mode(old_peer_state));
794b37d5 2579
2580 bgp_peer_move_to_gr_mode(peer, new_peer_state);
2581
2582 ret = BGP_GR_SUCCESS;
2583 }
2584 }
2585 /* In the case below peer is going into Global inherit mode i.e.
2586 * the peer would work as the mode configured at the global level
2587 */
2588 else if ((new_peer_state == PEER_GLOBAL_INHERIT) &&
2589 (old_peer_state != PEER_GLOBAL_INHERIT)) {
2590 /* Here in this case it would be destructive
2591 * in all the cases except one case when,
2592 * Global GR is configured Disabled
2593 * and present_peer_state is not disable
2594 */
2595
2596 BGP_PEER_GR_GLOBAL_INHERIT_SET(peer);
2597
2598 if (old_peer_state == bgp_gr_global_mode) {
2599
2600 /* This is incremental updates
2601 *i.e no tear down of the existing session
2602 *as the peer is already working in the same mode.
2603 */
2604 ret = BGP_GR_SUCCESS;
2605 } else {
2606 /* Destructive always */
2607 /* Tear down the old session
2608 * and send the new capability
2609 * as per the bgp_gr_global_mode
2610 */
2611
2612 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2ba1fe69 2613 zlog_debug("[BGP_GR] Peer state changed from :%s",
2614 print_peer_gr_mode(old_peer_state));
794b37d5 2615
2616 bgp_peer_move_to_gr_mode(peer, bgp_gr_global_mode);
2617
2618 ret = BGP_GR_SUCCESS;
2619 }
2620 } else {
2621 /*
2622 *This else case, it include all the cases except -->
2623 *(new_peer_state != Peer_Global) &&
2624 *( old_peer_state != Peer_Global )
2625 */
2626 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2ba1fe69 2627 zlog_debug("[BGP_GR] Peer state changed from :%s",
2628 print_peer_gr_mode(old_peer_state));
794b37d5 2629
2630 bgp_peer_move_to_gr_mode(peer, new_peer_state);
2631
2632 ret = BGP_GR_SUCCESS;
2633 }
2634
2635 return ret;
2636}
2637
2638inline void bgp_peer_move_to_gr_mode(struct peer *peer, int new_state)
2639
2640{
2641 int bgp_global_gr_mode = bgp_global_gr_mode_get(peer->bgp);
2642
2643 switch (new_state) {
794b37d5 2644 case PEER_HELPER:
2645 BGP_PEER_GR_HELPER_ENABLE(peer);
2646 break;
794b37d5 2647 case PEER_GR:
2648 BGP_PEER_GR_ENABLE(peer);
2649 break;
794b37d5 2650 case PEER_DISABLE:
2651 BGP_PEER_GR_DISABLE(peer);
2652 break;
794b37d5 2653 case PEER_GLOBAL_INHERIT:
2654 BGP_PEER_GR_GLOBAL_INHERIT_SET(peer);
2655
2656 if (bgp_global_gr_mode == GLOBAL_HELPER) {
2657 BGP_PEER_GR_HELPER_ENABLE(peer);
2658 } else if (bgp_global_gr_mode == GLOBAL_GR) {
2659 BGP_PEER_GR_ENABLE(peer);
2660 } else if (bgp_global_gr_mode == GLOBAL_DISABLE) {
2661 BGP_PEER_GR_DISABLE(peer);
2662 } else {
2ba1fe69 2663 zlog_err(
2664 "[BGP_GR] Default switch inherit mode ::: SOMETHING IS WRONG !!!");
794b37d5 2665 }
2666 break;
2667 default:
2ba1fe69 2668 zlog_err("[BGP_GR] Default switch mode ::: SOMETHING IS WRONG !!!");
794b37d5 2669 break;
2670 }
2671 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2ba1fe69 2672 zlog_debug("[BGP_GR] Peer state changed --to--> : %d : !",
794b37d5 2673 new_state);
2674}
2675
2676void bgp_peer_gr_flags_update(struct peer *peer)
2677{
2678 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2679 zlog_debug(
2ba1fe69 2680 "%s [BGP_GR] called !",
794b37d5 2681 __func__);
2682 if (CHECK_FLAG(peer->peer_gr_new_status_flag,
2683 PEER_GRACEFUL_RESTART_NEW_STATE_HELPER))
2986cac2 2684 SET_FLAG(peer->flags,
794b37d5 2685 PEER_FLAG_GRACEFUL_RESTART_HELPER);
2686 else
2986cac2 2687 UNSET_FLAG(peer->flags,
794b37d5 2688 PEER_FLAG_GRACEFUL_RESTART_HELPER);
2689 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2690 zlog_debug(
2ba1fe69 2691 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART_HELPER : %s : !",
794b37d5 2692 peer->host,
2986cac2 2693 (CHECK_FLAG(peer->flags,
794b37d5 2694 PEER_FLAG_GRACEFUL_RESTART_HELPER) ?
2695 "Set" : "UnSet"));
2696 if (CHECK_FLAG(peer->peer_gr_new_status_flag,
2697 PEER_GRACEFUL_RESTART_NEW_STATE_RESTART))
2986cac2 2698 SET_FLAG(peer->flags,
794b37d5 2699 PEER_FLAG_GRACEFUL_RESTART);
2700 else
2986cac2 2701 UNSET_FLAG(peer->flags,
794b37d5 2702 PEER_FLAG_GRACEFUL_RESTART);
2703 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2704 zlog_debug(
2ba1fe69 2705 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART : %s : !",
794b37d5 2706 peer->host,
2986cac2 2707 (CHECK_FLAG(peer->flags,
794b37d5 2708 PEER_FLAG_GRACEFUL_RESTART) ?
2709 "Set" : "UnSet"));
2710 if (CHECK_FLAG(peer->peer_gr_new_status_flag,
2711 PEER_GRACEFUL_RESTART_NEW_STATE_INHERIT))
2986cac2 2712 SET_FLAG(peer->flags,
794b37d5 2713 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT);
2714 else
2986cac2 2715 UNSET_FLAG(peer->flags,
794b37d5 2716 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT);
2717 if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART))
2718 zlog_debug(
2ba1fe69 2719 "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT : %s : !",
794b37d5 2720 peer->host,
2986cac2 2721 (CHECK_FLAG(peer->flags,
794b37d5 2722 PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT) ?
d7b3cda6 2723 "Set" : "UnSet"));
2724
2725 if (!CHECK_FLAG(peer->flags,
2726 PEER_FLAG_GRACEFUL_RESTART) &&
2727 !CHECK_FLAG(peer->flags,
2728 PEER_FLAG_GRACEFUL_RESTART_HELPER)){
2729 zlog_debug(
2ba1fe69 2730 "[BGP_GR] Peer %s UNSET PEER_STATUS_NSF_MODE!",
d7b3cda6 2731 peer->host);
2732
2733 UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
2734
2735 if (CHECK_FLAG(peer->sflags,
2736 PEER_STATUS_NSF_WAIT)) {
2737
2738 peer_nsf_stop(peer);
2739 zlog_debug(
2ba1fe69 2740 "[BGP_GR] Peer %s UNSET PEER_STATUS_NSF_WAIT!",
d7b3cda6 2741 peer->host);
2742 }
2743 }
794b37d5 2744}