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