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