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