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