1 /* BGP-4 Finite State Machine
2 * From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
3 * Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
26 #include "sockunion.h"
33 #include "workqueue.h"
37 #include "lib_errors.h"
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_attr.h"
42 #include "bgpd/bgp_debug.h"
43 #include "bgpd/bgp_errors.h"
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"
50 #include "bgpd/bgp_advertise.h"
51 #include "bgpd/bgp_updgrp.h"
52 #include "bgpd/bgp_nht.h"
53 #include "bgpd/bgp_bfd.h"
54 #include "bgpd/bgp_memory.h"
55 #include "bgpd/bgp_keepalives.h"
56 #include "bgpd/bgp_io.h"
57 #include "bgpd/bgp_zebra.h"
59 DEFINE_HOOK(peer_backward_transition
, (struct peer
* peer
), (peer
))
60 DEFINE_HOOK(peer_established
, (struct peer
* peer
), (peer
))
62 /* Definition of display strings corresponding to FSM events. This should be
63 * kept consistent with the events defined in bgpd.h
65 static const char *bgp_event_str
[] = {
69 "TCP_connection_open",
70 "TCP_connection_closed",
71 "TCP_connection_open_failed",
73 "ConnectRetry_timer_expired",
75 "KeepAlive_timer_expired",
76 "Receive_OPEN_message",
77 "Receive_KEEPALIVE_message",
78 "Receive_UPDATE_message",
79 "Receive_NOTIFICATION_message",
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
88 /* BGP event function. */
89 int bgp_event(struct thread
*);
91 /* BGP thread functions. */
92 static int bgp_start_timer(struct thread
*);
93 static int bgp_connect_timer(struct thread
*);
94 static int bgp_holdtime_timer(struct thread
*);
96 /* BGP FSM functions. */
97 static int bgp_start(struct peer
*);
99 /* Register peer with NHT */
100 static int bgp_peer_reg_with_nht(struct peer
*peer
)
104 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
105 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DISABLE_CONNECTED_CHECK
)
106 && !bgp_flag_check(peer
->bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
109 return bgp_find_or_add_nexthop(
110 peer
->bgp
, peer
->bgp
, family2afi(peer
->su
.sa
.sa_family
),
111 NULL
, peer
, connected
);
114 static void peer_xfer_stats(struct peer
*peer_dst
, struct peer
*peer_src
)
116 /* Copy stats over. These are only the pre-established state stats */
117 peer_dst
->open_in
+= peer_src
->open_in
;
118 peer_dst
->open_out
+= peer_src
->open_out
;
119 peer_dst
->keepalive_in
+= peer_src
->keepalive_in
;
120 peer_dst
->keepalive_out
+= peer_src
->keepalive_out
;
121 peer_dst
->notify_in
+= peer_src
->notify_in
;
122 peer_dst
->notify_out
+= peer_src
->notify_out
;
123 peer_dst
->dynamic_cap_in
+= peer_src
->dynamic_cap_in
;
124 peer_dst
->dynamic_cap_out
+= peer_src
->dynamic_cap_out
;
127 static struct peer
*peer_xfer_conn(struct peer
*from_peer
)
134 unsigned char last_evt
, last_maj_evt
;
136 assert(from_peer
!= NULL
);
138 peer
= from_peer
->doppelganger
;
140 if (!peer
|| !CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
144 * Let's check that we are not going to loose known configuration
145 * state based upon doppelganger rules.
147 FOREACH_AFI_SAFI (afi
, safi
) {
148 if (from_peer
->afc
[afi
][safi
] != peer
->afc
[afi
][safi
]) {
150 EC_BGP_DOPPELGANGER_CONFIG
,
151 "from_peer->afc[%d][%d] is not the same as what we are overwriting",
157 if (bgp_debug_neighbor_events(peer
))
158 zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
159 from_peer
->host
, from_peer
, from_peer
->fd
, peer
,
162 bgp_writes_off(peer
);
164 bgp_writes_off(from_peer
);
165 bgp_reads_off(from_peer
);
167 BGP_TIMER_OFF(peer
->t_routeadv
);
168 BGP_TIMER_OFF(peer
->t_connect
);
169 BGP_TIMER_OFF(peer
->t_connect_check_r
);
170 BGP_TIMER_OFF(peer
->t_connect_check_w
);
171 BGP_TIMER_OFF(from_peer
->t_routeadv
);
172 BGP_TIMER_OFF(from_peer
->t_connect
);
173 BGP_TIMER_OFF(from_peer
->t_connect_check_r
);
174 BGP_TIMER_OFF(from_peer
->t_connect_check_w
);
175 BGP_TIMER_OFF(from_peer
->t_process_packet
);
178 * At this point in time, it is possible that there are packets pending
179 * on various buffers. Those need to be transferred or dropped,
180 * otherwise we'll get spurious failures during session establishment.
182 pthread_mutex_lock(&peer
->io_mtx
);
183 pthread_mutex_lock(&from_peer
->io_mtx
);
186 peer
->fd
= from_peer
->fd
;
189 stream_fifo_clean(peer
->ibuf
);
190 stream_fifo_clean(peer
->obuf
);
193 * this should never happen, since bgp_process_packet() is the
194 * only task that sets and unsets the current packet and it
195 * runs in our pthread.
200 "[%s] Dropping pending packet on connection transfer:",
202 /* there used to be a bgp_packet_dump call here, but
203 * that's extremely confusing since there's no way to
204 * identify the packet in MRT dumps or BMP as dropped
205 * due to connection transfer.
207 stream_free(peer
->curr
);
211 // copy each packet from old peer's output queue to new peer
212 while (from_peer
->obuf
->head
)
213 stream_fifo_push(peer
->obuf
,
214 stream_fifo_pop(from_peer
->obuf
));
216 // copy each packet from old peer's input queue to new peer
217 while (from_peer
->ibuf
->head
)
218 stream_fifo_push(peer
->ibuf
,
219 stream_fifo_pop(from_peer
->ibuf
));
221 ringbuf_wipe(peer
->ibuf_work
);
222 ringbuf_copy(peer
->ibuf_work
, from_peer
->ibuf_work
,
223 ringbuf_remain(from_peer
->ibuf_work
));
225 pthread_mutex_unlock(&from_peer
->io_mtx
);
226 pthread_mutex_unlock(&peer
->io_mtx
);
228 peer
->as
= from_peer
->as
;
229 peer
->v_holdtime
= from_peer
->v_holdtime
;
230 peer
->v_keepalive
= from_peer
->v_keepalive
;
231 peer
->v_routeadv
= from_peer
->v_routeadv
;
232 peer
->v_gr_restart
= from_peer
->v_gr_restart
;
233 peer
->cap
= from_peer
->cap
;
234 status
= peer
->status
;
235 pstatus
= peer
->ostatus
;
236 last_evt
= peer
->last_event
;
237 last_maj_evt
= peer
->last_major_event
;
238 peer
->status
= from_peer
->status
;
239 peer
->ostatus
= from_peer
->ostatus
;
240 peer
->last_event
= from_peer
->last_event
;
241 peer
->last_major_event
= from_peer
->last_major_event
;
242 from_peer
->status
= status
;
243 from_peer
->ostatus
= pstatus
;
244 from_peer
->last_event
= last_evt
;
245 from_peer
->last_major_event
= last_maj_evt
;
246 peer
->remote_id
= from_peer
->remote_id
;
248 if (from_peer
->hostname
!= NULL
) {
249 if (peer
->hostname
) {
250 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
251 peer
->hostname
= NULL
;
254 peer
->hostname
= from_peer
->hostname
;
255 from_peer
->hostname
= NULL
;
258 if (from_peer
->domainname
!= NULL
) {
259 if (peer
->domainname
) {
260 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
261 peer
->domainname
= NULL
;
264 peer
->domainname
= from_peer
->domainname
;
265 from_peer
->domainname
= NULL
;
268 FOREACH_AFI_SAFI (afi
, safi
) {
269 peer
->af_flags
[afi
][safi
] = from_peer
->af_flags
[afi
][safi
];
270 peer
->af_sflags
[afi
][safi
] = from_peer
->af_sflags
[afi
][safi
];
271 peer
->af_cap
[afi
][safi
] = from_peer
->af_cap
[afi
][safi
];
272 peer
->afc_nego
[afi
][safi
] = from_peer
->afc_nego
[afi
][safi
];
273 peer
->afc_adv
[afi
][safi
] = from_peer
->afc_adv
[afi
][safi
];
274 peer
->afc_recv
[afi
][safi
] = from_peer
->afc_recv
[afi
][safi
];
275 peer
->orf_plist
[afi
][safi
] = from_peer
->orf_plist
[afi
][safi
];
278 if (bgp_getsockname(peer
) < 0) {
281 "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
282 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)
285 peer
->host
, peer
->fd
, from_peer
->fd
);
290 if (from_peer
->status
> Active
) {
291 if (bgp_getsockname(from_peer
) < 0) {
294 "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
296 (CHECK_FLAG(from_peer
->sflags
,
297 PEER_STATUS_ACCEPT_PEER
)
300 from_peer
->host
, from_peer
->fd
, peer
->fd
);
307 // Note: peer_xfer_stats() must be called with I/O turned OFF
309 peer_xfer_stats(peer
, from_peer
);
311 /* Register peer for NHT. This is to allow RAs to be enabled when
312 * needed, even on a passive connection.
314 bgp_peer_reg_with_nht(peer
);
318 thread_add_timer_msec(bm
->master
, bgp_process_packet
, peer
, 0,
319 &peer
->t_process_packet
);
324 /* Hook function called after bgp event is occered. And vty's
325 neighbor command invoke this function after making neighbor
327 void bgp_timer_set(struct peer
*peer
)
329 switch (peer
->status
) {
331 /* First entry point of peer's finite state machine. In Idle
332 status start timer is on unless peer is shutdown or peer is
333 inactive. All other timer must be turned off */
334 if (BGP_PEER_START_SUPPRESSED(peer
) || !peer_active(peer
)
335 || (peer
->bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
&&
336 peer
->bgp
->vrf_id
== VRF_UNKNOWN
)) {
337 BGP_TIMER_OFF(peer
->t_start
);
339 BGP_TIMER_ON(peer
->t_start
, bgp_start_timer
,
342 BGP_TIMER_OFF(peer
->t_connect
);
343 BGP_TIMER_OFF(peer
->t_holdtime
);
344 bgp_keepalives_off(peer
);
345 BGP_TIMER_OFF(peer
->t_routeadv
);
349 /* After start timer is expired, the peer moves to Connect
350 status. Make sure start timer is off and connect timer is
352 BGP_TIMER_OFF(peer
->t_start
);
353 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
355 BGP_TIMER_OFF(peer
->t_holdtime
);
356 bgp_keepalives_off(peer
);
357 BGP_TIMER_OFF(peer
->t_routeadv
);
361 /* Active is waiting connection from remote peer. And if
362 connect timer is expired, change status to Connect. */
363 BGP_TIMER_OFF(peer
->t_start
);
364 /* If peer is passive mode, do not set connect timer. */
365 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)
366 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
367 BGP_TIMER_OFF(peer
->t_connect
);
369 BGP_TIMER_ON(peer
->t_connect
, bgp_connect_timer
,
372 BGP_TIMER_OFF(peer
->t_holdtime
);
373 bgp_keepalives_off(peer
);
374 BGP_TIMER_OFF(peer
->t_routeadv
);
378 /* OpenSent status. */
379 BGP_TIMER_OFF(peer
->t_start
);
380 BGP_TIMER_OFF(peer
->t_connect
);
381 if (peer
->v_holdtime
!= 0) {
382 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
385 BGP_TIMER_OFF(peer
->t_holdtime
);
387 bgp_keepalives_off(peer
);
388 BGP_TIMER_OFF(peer
->t_routeadv
);
392 /* OpenConfirm status. */
393 BGP_TIMER_OFF(peer
->t_start
);
394 BGP_TIMER_OFF(peer
->t_connect
);
396 /* If the negotiated Hold Time value is zero, then the Hold Time
397 timer and KeepAlive timers are not started. */
398 if (peer
->v_holdtime
== 0) {
399 BGP_TIMER_OFF(peer
->t_holdtime
);
400 bgp_keepalives_off(peer
);
402 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
404 bgp_keepalives_on(peer
);
406 BGP_TIMER_OFF(peer
->t_routeadv
);
410 /* In Established status start and connect timer is turned
412 BGP_TIMER_OFF(peer
->t_start
);
413 BGP_TIMER_OFF(peer
->t_connect
);
415 /* Same as OpenConfirm, if holdtime is zero then both holdtime
416 and keepalive must be turned off. */
417 if (peer
->v_holdtime
== 0) {
418 BGP_TIMER_OFF(peer
->t_holdtime
);
419 bgp_keepalives_off(peer
);
421 BGP_TIMER_ON(peer
->t_holdtime
, bgp_holdtime_timer
,
423 bgp_keepalives_on(peer
);
427 BGP_TIMER_OFF(peer
->t_gr_restart
);
428 BGP_TIMER_OFF(peer
->t_gr_stale
);
429 BGP_TIMER_OFF(peer
->t_pmax_restart
);
432 BGP_TIMER_OFF(peer
->t_start
);
433 BGP_TIMER_OFF(peer
->t_connect
);
434 BGP_TIMER_OFF(peer
->t_holdtime
);
435 bgp_keepalives_off(peer
);
436 BGP_TIMER_OFF(peer
->t_routeadv
);
441 /* BGP start timer. This function set BGP_Start event to thread value
442 and process event. */
443 static int bgp_start_timer(struct thread
*thread
)
447 peer
= THREAD_ARG(thread
);
448 peer
->t_start
= NULL
;
450 if (bgp_debug_neighbor_events(peer
))
451 zlog_debug("%s [FSM] Timer (start timer expire).", peer
->host
);
453 THREAD_VAL(thread
) = BGP_Start
;
454 bgp_event(thread
); /* bgp_event unlocks peer */
459 /* BGP connect retry timer. */
460 static int bgp_connect_timer(struct thread
*thread
)
465 peer
= THREAD_ARG(thread
);
467 assert(!peer
->t_write
);
468 assert(!peer
->t_read
);
470 peer
->t_connect
= NULL
;
472 if (bgp_debug_neighbor_events(peer
))
473 zlog_debug("%s [FSM] Timer (connect timer expire)", peer
->host
);
475 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
479 THREAD_VAL(thread
) = ConnectRetry_timer_expired
;
480 bgp_event(thread
); /* bgp_event unlocks peer */
487 /* BGP holdtime timer. */
488 static int bgp_holdtime_timer(struct thread
*thread
)
492 peer
= THREAD_ARG(thread
);
493 peer
->t_holdtime
= NULL
;
495 if (bgp_debug_neighbor_events(peer
))
496 zlog_debug("%s [FSM] Timer (holdtime timer expire)",
499 THREAD_VAL(thread
) = Hold_Timer_expired
;
500 bgp_event(thread
); /* bgp_event unlocks peer */
505 int bgp_routeadv_timer(struct thread
*thread
)
509 peer
= THREAD_ARG(thread
);
510 peer
->t_routeadv
= NULL
;
512 if (bgp_debug_neighbor_events(peer
))
513 zlog_debug("%s [FSM] Timer (routeadv timer expire)",
516 peer
->synctime
= bgp_clock();
518 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
, peer
, 0,
519 &peer
->t_generate_updgrp_packets
);
521 /* MRAI timer will be started again when FIFO is built, no need to
527 /* BGP Peer Down Cause */
528 const char *peer_down_str
[] = {"",
532 "Cluster ID changed",
533 "Confederation identifier changed",
534 "Confederation peer changed",
535 "RR client config change",
536 "RS client config change",
537 "Update source change",
538 "Address family activated",
541 "BGP Notification received",
542 "BGP Notification send",
543 "Peer closed the session",
545 "Peer-group add member",
546 "Peer-group delete member",
547 "Capability changed",
548 "Passive config change",
549 "Multihop config change",
550 "NSF peer closed the session",
551 "Intf peering v6only config change",
554 "Neighbor address lost"};
556 static int bgp_graceful_restart_timer_expire(struct thread
*thread
)
562 peer
= THREAD_ARG(thread
);
563 peer
->t_gr_restart
= NULL
;
565 /* NSF delete stale route */
566 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
567 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
568 if (peer
->nsf
[afi
][safi
])
569 bgp_clear_stale_route(peer
, afi
, safi
);
571 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
572 BGP_TIMER_OFF(peer
->t_gr_stale
);
574 if (bgp_debug_neighbor_events(peer
)) {
575 zlog_debug("%s graceful restart timer expired", peer
->host
);
576 zlog_debug("%s graceful restart stalepath timer stopped",
585 static int bgp_graceful_stale_timer_expire(struct thread
*thread
)
591 peer
= THREAD_ARG(thread
);
592 peer
->t_gr_stale
= NULL
;
594 if (bgp_debug_neighbor_events(peer
))
595 zlog_debug("%s graceful restart stalepath timer expired",
598 /* NSF delete stale route */
599 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
600 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
601 if (peer
->nsf
[afi
][safi
])
602 bgp_clear_stale_route(peer
, afi
, safi
);
607 static int bgp_update_delay_applicable(struct bgp
*bgp
)
609 /* update_delay_over flag should be reset (set to 0) for any new
610 applicability of the update-delay during BGP process lifetime.
611 And it should be set after an occurence of the update-delay is
613 if (!bgp
->update_delay_over
)
619 int bgp_update_delay_active(struct bgp
*bgp
)
621 if (bgp
->t_update_delay
)
627 int bgp_update_delay_configured(struct bgp
*bgp
)
629 if (bgp
->v_update_delay
)
635 /* Do the post-processing needed when bgp comes out of the read-only mode
636 on ending the update delay. */
637 void bgp_update_delay_end(struct bgp
*bgp
)
639 THREAD_TIMER_OFF(bgp
->t_update_delay
);
640 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
642 /* Reset update-delay related state */
643 bgp
->update_delay_over
= 1;
644 bgp
->established
= 0;
645 bgp
->restarted_peers
= 0;
646 bgp
->implicit_eors
= 0;
647 bgp
->explicit_eors
= 0;
649 quagga_timestamp(3, bgp
->update_delay_end_time
,
650 sizeof(bgp
->update_delay_end_time
));
653 * Add an end-of-initial-update marker to the main process queues so
655 * the route advertisement timer for the peers can be started. Also set
656 * the zebra and peer update hold flags. These flags are used to achieve
657 * three stages in the update-delay post processing:
658 * 1. Finish best-path selection for all the prefixes held on the
660 * (routes in BGP are updated, and peers sync queues are populated
662 * 2. As the eoiu mark is reached in the bgp process routine, ship all
664 * routes to zebra. With that zebra should see updates from BGP
667 * 3. Unblock the peer update writes. With that peer update packing
669 * the prefixes should be at its maximum.
671 bgp_add_eoiu_mark(bgp
);
672 bgp
->main_zebra_update_hold
= 1;
673 bgp
->main_peers_update_hold
= 1;
675 /* Resume the queue processing. This should trigger the event that would
677 care of processing any work that was queued during the read-only
679 work_queue_unplug(bm
->process_main_queue
);
685 void bgp_start_routeadv(struct bgp
*bgp
)
687 struct listnode
*node
, *nnode
;
690 zlog_info("bgp_start_routeadv(), update hold status %d",
691 bgp
->main_peers_update_hold
);
693 if (bgp
->main_peers_update_hold
)
696 quagga_timestamp(3, bgp
->update_delay_peers_resume_time
,
697 sizeof(bgp
->update_delay_peers_resume_time
));
699 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
700 if (peer
->status
!= Established
)
702 BGP_TIMER_OFF(peer
->t_routeadv
);
703 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
710 void bgp_adjust_routeadv(struct peer
*peer
)
712 time_t nowtime
= bgp_clock();
714 unsigned long remain
;
716 /* Bypass checks for special case of MRAI being 0 */
717 if (peer
->v_routeadv
== 0) {
718 /* Stop existing timer, just in case it is running for a
720 * duration and schedule write thread immediately.
722 if (peer
->t_routeadv
)
723 BGP_TIMER_OFF(peer
->t_routeadv
);
725 peer
->synctime
= bgp_clock();
726 thread_add_timer_msec(bm
->master
, bgp_generate_updgrp_packets
,
728 &peer
->t_generate_updgrp_packets
);
735 * If the last update was written more than MRAI back, expire the timer
736 * instantly so that we can send the update out sooner.
738 * <------- MRAI --------->
739 * |-----------------|-----------------------|
740 * <------------- m ------------>
749 diff
= difftime(nowtime
, peer
->last_update
);
750 if (diff
> (double)peer
->v_routeadv
) {
751 BGP_TIMER_OFF(peer
->t_routeadv
);
752 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
758 * - Find when to expire the MRAI timer.
759 * If MRAI timer is not active, assume we can start it now.
761 * <------- MRAI --------->
762 * |------------|-----------------------|
763 * <-------- m ----------><----- r ----->
772 if (peer
->t_routeadv
)
773 remain
= thread_timer_remain_second(peer
->t_routeadv
);
775 remain
= peer
->v_routeadv
;
776 diff
= peer
->v_routeadv
- diff
;
777 if (diff
<= (double)remain
) {
778 BGP_TIMER_OFF(peer
->t_routeadv
);
779 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, diff
);
783 static int bgp_maxmed_onstartup_applicable(struct bgp
*bgp
)
785 if (!bgp
->maxmed_onstartup_over
)
791 int bgp_maxmed_onstartup_configured(struct bgp
*bgp
)
793 if (bgp
->v_maxmed_onstartup
!= BGP_MAXMED_ONSTARTUP_UNCONFIGURED
)
799 int bgp_maxmed_onstartup_active(struct bgp
*bgp
)
801 if (bgp
->t_maxmed_onstartup
)
807 void bgp_maxmed_update(struct bgp
*bgp
)
809 uint8_t maxmed_active
;
810 uint32_t maxmed_value
;
812 if (bgp
->v_maxmed_admin
) {
814 maxmed_value
= bgp
->maxmed_admin_value
;
815 } else if (bgp
->t_maxmed_onstartup
) {
817 maxmed_value
= bgp
->maxmed_onstartup_value
;
820 maxmed_value
= BGP_MAXMED_VALUE_DEFAULT
;
823 if (bgp
->maxmed_active
!= maxmed_active
824 || bgp
->maxmed_value
!= maxmed_value
) {
825 bgp
->maxmed_active
= maxmed_active
;
826 bgp
->maxmed_value
= maxmed_value
;
828 update_group_announce(bgp
);
832 /* The maxmed onstartup timer expiry callback. */
833 static int bgp_maxmed_onstartup_timer(struct thread
*thread
)
837 zlog_info("Max med on startup ended - timer expired.");
839 bgp
= THREAD_ARG(thread
);
840 THREAD_TIMER_OFF(bgp
->t_maxmed_onstartup
);
841 bgp
->maxmed_onstartup_over
= 1;
843 bgp_maxmed_update(bgp
);
848 static void bgp_maxmed_onstartup_begin(struct bgp
*bgp
)
850 /* Applicable only once in the process lifetime on the startup */
851 if (bgp
->maxmed_onstartup_over
)
854 zlog_info("Begin maxmed onstartup mode - timer %d seconds",
855 bgp
->v_maxmed_onstartup
);
857 thread_add_timer(bm
->master
, bgp_maxmed_onstartup_timer
, bgp
,
858 bgp
->v_maxmed_onstartup
, &bgp
->t_maxmed_onstartup
);
860 if (!bgp
->v_maxmed_admin
) {
861 bgp
->maxmed_active
= 1;
862 bgp
->maxmed_value
= bgp
->maxmed_onstartup_value
;
865 /* Route announce to all peers should happen after this in
869 static void bgp_maxmed_onstartup_process_status_change(struct peer
*peer
)
871 if (peer
->status
== Established
&& !peer
->bgp
->established
) {
872 bgp_maxmed_onstartup_begin(peer
->bgp
);
876 /* The update delay timer expiry callback. */
877 static int bgp_update_delay_timer(struct thread
*thread
)
881 zlog_info("Update delay ended - timer expired.");
883 bgp
= THREAD_ARG(thread
);
884 THREAD_TIMER_OFF(bgp
->t_update_delay
);
885 bgp_update_delay_end(bgp
);
890 /* The establish wait timer expiry callback. */
891 static int bgp_establish_wait_timer(struct thread
*thread
)
895 zlog_info("Establish wait - timer expired.");
897 bgp
= THREAD_ARG(thread
);
898 THREAD_TIMER_OFF(bgp
->t_establish_wait
);
899 bgp_check_update_delay(bgp
);
904 /* Steps to begin the update delay:
905 - initialize queues if needed
906 - stop the queue processing
908 static void bgp_update_delay_begin(struct bgp
*bgp
)
910 struct listnode
*node
, *nnode
;
913 /* Stop the processing of queued work. Enqueue shall continue */
914 work_queue_plug(bm
->process_main_queue
);
916 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
917 peer
->update_delay_over
= 0;
919 /* Start the update-delay timer */
920 thread_add_timer(bm
->master
, bgp_update_delay_timer
, bgp
,
921 bgp
->v_update_delay
, &bgp
->t_update_delay
);
923 if (bgp
->v_establish_wait
!= bgp
->v_update_delay
)
924 thread_add_timer(bm
->master
, bgp_establish_wait_timer
, bgp
,
925 bgp
->v_establish_wait
, &bgp
->t_establish_wait
);
927 quagga_timestamp(3, bgp
->update_delay_begin_time
,
928 sizeof(bgp
->update_delay_begin_time
));
931 static void bgp_update_delay_process_status_change(struct peer
*peer
)
933 if (peer
->status
== Established
) {
934 if (!peer
->bgp
->established
++) {
935 bgp_update_delay_begin(peer
->bgp
);
937 "Begin read-only mode - update-delay timer %d seconds",
938 peer
->bgp
->v_update_delay
);
940 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_BIT_RCV
))
941 bgp_update_restarted_peers(peer
);
943 if (peer
->ostatus
== Established
944 && bgp_update_delay_active(peer
->bgp
)) {
945 /* Adjust the update-delay state to account for this flap.
946 NOTE: Intentionally skipping adjusting implicit_eors or
948 counters. Extra sanity check in bgp_check_update_delay()
950 be enough to take care of any additive discrepancy in bgp eor
952 peer
->bgp
->established
--;
953 peer
->update_delay_over
= 0;
957 /* Called after event occurred, this function change status and reset
958 read/write and timer thread. */
959 void bgp_fsm_change_status(struct peer
*peer
, int status
)
964 bgp_dump_state(peer
, peer
->status
, status
);
967 peer_count
= bgp
->established_peers
;
969 if (status
== Established
)
970 bgp
->established_peers
++;
971 else if ((peer
->status
== Established
) && (status
!= Established
))
972 bgp
->established_peers
--;
974 if (bgp_debug_neighbor_events(peer
)) {
975 struct vrf
*vrf
= vrf_lookup_by_id(bgp
->vrf_id
);
977 zlog_debug("%s : vrf %s(%u), Status: %s established_peers %u", __func__
,
978 vrf
? vrf
->name
: "Unknown", bgp
->vrf_id
,
979 lookup_msg(bgp_status_msg
, status
, NULL
),
980 bgp
->established_peers
);
983 /* Set to router ID to the value provided by RIB if there are no peers
984 * in the established state and peer count did not change
986 if ((peer_count
!= bgp
->established_peers
) &&
987 (bgp
->established_peers
== 0))
988 bgp_router_id_zebra_bump(bgp
->vrf_id
, NULL
);
990 /* Transition into Clearing or Deleted must /always/ clear all routes..
991 * (and must do so before actually changing into Deleted..
993 if (status
>= Clearing
) {
994 bgp_clear_route_all(peer
);
996 /* If no route was queued for the clear-node processing,
998 * completion event here. This is needed because if there are no
1000 * to trigger the background clear-node thread, the event won't
1002 * generated and the peer would be stuck in Clearing. Note that
1004 * event is for the peer and helps the peer transition out of
1006 * state; it should not be generated per (AFI,SAFI). The event
1008 * directly posted here without calling clear_node_complete() as
1010 * shouldn't do an extra unlock. This event will get processed
1012 * the state change that happens below, so peer will be in
1016 if (!work_queue_is_scheduled(peer
->clear_node_queue
))
1017 BGP_EVENT_ADD(peer
, Clearing_Completed
);
1020 /* Preserve old status and change into new status. */
1021 peer
->ostatus
= peer
->status
;
1022 peer
->status
= status
;
1024 /* Save event that caused status change. */
1025 peer
->last_major_event
= peer
->cur_event
;
1027 if (status
== Established
)
1028 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
1030 /* If max-med processing is applicable, do the necessary. */
1031 if (status
== Established
) {
1032 if (bgp_maxmed_onstartup_configured(peer
->bgp
)
1033 && bgp_maxmed_onstartup_applicable(peer
->bgp
))
1034 bgp_maxmed_onstartup_process_status_change(peer
);
1036 peer
->bgp
->maxmed_onstartup_over
= 1;
1039 /* If update-delay processing is applicable, do the necessary. */
1040 if (bgp_update_delay_configured(peer
->bgp
)
1041 && bgp_update_delay_applicable(peer
->bgp
))
1042 bgp_update_delay_process_status_change(peer
);
1044 if (bgp_debug_neighbor_events(peer
))
1045 zlog_debug("%s went from %s to %s", peer
->host
,
1046 lookup_msg(bgp_status_msg
, peer
->ostatus
, NULL
),
1047 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1050 /* Flush the event queue and ensure the peer is shut down */
1051 static int bgp_clearing_completed(struct peer
*peer
)
1053 int rc
= bgp_stop(peer
);
1056 BGP_EVENT_FLUSH(peer
);
1061 /* Administrative BGP peer stop event. */
1062 /* May be called multiple times for the same peer */
1063 int bgp_stop(struct peer
*peer
)
1067 char orf_name
[BUFSIZ
];
1070 if (peer_dynamic_neighbor(peer
)
1071 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1072 if (bgp_debug_neighbor_events(peer
))
1073 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1078 /* Can't do this in Clearing; events are used for state transitions */
1079 if (peer
->status
!= Clearing
) {
1080 /* Delete all existing events of the peer */
1081 BGP_EVENT_FLUSH(peer
);
1084 /* Increment Dropped count. */
1085 if (peer
->status
== Established
) {
1088 /* bgp log-neighbor-changes of neighbor Down */
1089 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1090 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1092 "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s",
1094 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1095 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
)
1099 peer_down_str
[(int)peer
->last_reset
]);
1102 /* graceful restart */
1103 if (peer
->t_gr_stale
) {
1104 BGP_TIMER_OFF(peer
->t_gr_stale
);
1105 if (bgp_debug_neighbor_events(peer
))
1107 "%s graceful restart stalepath timer stopped",
1110 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
1111 if (bgp_debug_neighbor_events(peer
)) {
1113 "%s graceful restart timer started for %d sec",
1114 peer
->host
, peer
->v_gr_restart
);
1116 "%s graceful restart stalepath timer started for %d sec",
1117 peer
->host
, peer
->bgp
->stalepath_time
);
1119 BGP_TIMER_ON(peer
->t_gr_restart
,
1120 bgp_graceful_restart_timer_expire
,
1121 peer
->v_gr_restart
);
1122 BGP_TIMER_ON(peer
->t_gr_stale
,
1123 bgp_graceful_stale_timer_expire
,
1124 peer
->bgp
->stalepath_time
);
1126 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1128 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1129 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
;
1131 peer
->nsf
[afi
][safi
] = 0;
1134 /* set last reset time */
1135 peer
->resettime
= peer
->uptime
= bgp_clock();
1137 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1138 zlog_debug("%s remove from all update group",
1140 update_group_remove_peer_afs(peer
);
1142 hook_call(peer_backward_transition
, peer
);
1144 /* Reset peer synctime */
1148 /* stop keepalives */
1149 bgp_keepalives_off(peer
);
1151 /* Stop read and write threads. */
1152 bgp_writes_off(peer
);
1153 bgp_reads_off(peer
);
1155 THREAD_OFF(peer
->t_connect_check_r
);
1156 THREAD_OFF(peer
->t_connect_check_w
);
1158 /* Stop all timers. */
1159 BGP_TIMER_OFF(peer
->t_start
);
1160 BGP_TIMER_OFF(peer
->t_connect
);
1161 BGP_TIMER_OFF(peer
->t_holdtime
);
1162 BGP_TIMER_OFF(peer
->t_routeadv
);
1164 /* Clear input and output buffer. */
1165 pthread_mutex_lock(&peer
->io_mtx
);
1168 stream_fifo_clean(peer
->ibuf
);
1170 stream_fifo_clean(peer
->obuf
);
1172 if (peer
->ibuf_work
)
1173 ringbuf_wipe(peer
->ibuf_work
);
1174 if (peer
->obuf_work
)
1175 stream_reset(peer
->obuf_work
);
1178 stream_free(peer
->curr
);
1182 pthread_mutex_unlock(&peer
->io_mtx
);
1184 /* Close of file descriptor. */
1185 if (peer
->fd
>= 0) {
1190 FOREACH_AFI_SAFI (afi
, safi
) {
1191 /* Reset all negotiated variables */
1192 peer
->afc_nego
[afi
][safi
] = 0;
1193 peer
->afc_adv
[afi
][safi
] = 0;
1194 peer
->afc_recv
[afi
][safi
] = 0;
1196 /* peer address family capability flags*/
1197 peer
->af_cap
[afi
][safi
] = 0;
1199 /* peer address family status flags*/
1200 peer
->af_sflags
[afi
][safi
] = 0;
1202 /* Received ORF prefix-filter */
1203 peer
->orf_plist
[afi
][safi
] = NULL
;
1205 if ((peer
->status
== OpenConfirm
)
1206 || (peer
->status
== Established
)) {
1207 /* ORF received prefix-filter pnt */
1208 sprintf(orf_name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1209 prefix_bgp_orf_remove_all(afi
, orf_name
);
1213 /* Reset keepalive and holdtime */
1214 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1215 peer
->v_keepalive
= peer
->keepalive
;
1216 peer
->v_holdtime
= peer
->holdtime
;
1218 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1219 peer
->v_holdtime
= peer
->bgp
->default_holdtime
;
1222 peer
->update_time
= 0;
1224 /* Until we are sure that there is no problem about prefix count
1225 this should be commented out.*/
1227 /* Reset prefix count */
1228 peer
->pcount
[AFI_IP
][SAFI_UNICAST
] = 0;
1229 peer
->pcount
[AFI_IP
][SAFI_MULTICAST
] = 0;
1230 peer
->pcount
[AFI_IP
][SAFI_LABELED_UNICAST
] = 0;
1231 peer
->pcount
[AFI_IP
][SAFI_MPLS_VPN
] = 0;
1232 peer
->pcount
[AFI_IP6
][SAFI_UNICAST
] = 0;
1233 peer
->pcount
[AFI_IP6
][SAFI_MULTICAST
] = 0;
1234 peer
->pcount
[AFI_IP6
][SAFI_LABELED_UNICAST
] = 0;
1237 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1238 && !(CHECK_FLAG(peer
->flags
, PEER_FLAG_DELETE
))) {
1242 bgp_peer_conf_if_to_su_update(peer
);
1248 /* BGP peer is stoped by the error. */
1249 static int bgp_stop_with_error(struct peer
*peer
)
1251 /* Double start timer. */
1254 /* Overflow check. */
1255 if (peer
->v_start
>= (60 * 2))
1256 peer
->v_start
= (60 * 2);
1258 if (peer_dynamic_neighbor(peer
)) {
1259 if (bgp_debug_neighbor_events(peer
))
1260 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1265 return (bgp_stop(peer
));
1269 /* something went wrong, send notify and tear down */
1270 static int bgp_stop_with_notify(struct peer
*peer
, uint8_t code
,
1273 /* Send notify to remote peer */
1274 bgp_notify_send(peer
, code
, sub_code
);
1276 if (peer_dynamic_neighbor(peer
)) {
1277 if (bgp_debug_neighbor_events(peer
))
1278 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1283 /* Clear start timer value to default. */
1284 peer
->v_start
= BGP_INIT_START_TIMER
;
1286 return (bgp_stop(peer
));
1290 * Determines whether a TCP session has successfully established for a peer and
1291 * events as appropriate.
1293 * This function is called when setting up a new session. After connect() is
1294 * called on the peer's socket (in bgp_start()), the fd is passed to poll()
1295 * to wait for connection success or failure. When poll() returns, this
1296 * function is called to evaluate the result.
1298 * Due to differences in behavior of poll() on Linux and BSD - specifically,
1299 * the value of .revents in the case of a closed connection - this function is
1300 * scheduled both for a read and a write event. The write event is triggered
1301 * when the connection is established. A read event is triggered when the
1302 * connection is closed. Thus we need to cancel whichever one did not occur.
1304 static int bgp_connect_check(struct thread
*thread
)
1311 peer
= THREAD_ARG(thread
);
1312 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1313 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1314 assert(!peer
->t_read
);
1315 assert(!peer
->t_write
);
1317 THREAD_OFF(peer
->t_connect_check_r
);
1318 THREAD_OFF(peer
->t_connect_check_w
);
1320 /* Check file descriptor. */
1321 slen
= sizeof(status
);
1322 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
1325 /* If getsockopt is fail, this is fatal error. */
1327 zlog_info("can't get sockopt for nonblocking connect: %d(%s)",
1328 errno
, safe_strerror(errno
));
1329 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1333 /* When status is 0 then TCP connection is established. */
1335 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1338 if (bgp_debug_neighbor_events(peer
))
1339 zlog_debug("%s [Event] Connect failed %d(%s)",
1340 peer
->host
, status
, safe_strerror(status
));
1341 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1346 /* TCP connection open. Next we send open message to remote peer. And
1347 add read thread for reading open message. */
1348 static int bgp_connect_success(struct peer
*peer
)
1351 flog_err(EC_BGP_CONNECT
,
1352 "bgp_connect_success peer's fd is negative value %d",
1358 if (bgp_getsockname(peer
) < 0) {
1359 flog_err_sys(EC_LIB_SOCKET
,
1360 "%s: bgp_getsockname(): failed for peer %s, fd %d",
1361 __FUNCTION__
, peer
->host
, peer
->fd
);
1362 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1363 0); /* internal error */
1364 bgp_writes_on(peer
);
1370 if (bgp_debug_neighbor_events(peer
)) {
1371 char buf1
[SU_ADDRSTRLEN
];
1373 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1374 zlog_debug("%s open active, local address %s",
1376 sockunion2str(peer
->su_local
, buf1
,
1379 zlog_debug("%s passive open", peer
->host
);
1382 bgp_open_send(peer
);
1387 /* TCP connect fail */
1388 static int bgp_connect_fail(struct peer
*peer
)
1390 if (peer_dynamic_neighbor(peer
)) {
1391 if (bgp_debug_neighbor_events(peer
))
1392 zlog_debug("%s (dynamic neighbor) deleted", peer
->host
);
1397 return (bgp_stop(peer
));
1400 /* This function is the first starting point of all BGP connection. It
1401 try to connect to remote peer with non-blocking IO. */
1402 int bgp_start(struct peer
*peer
)
1406 bgp_peer_conf_if_to_su_update(peer
);
1408 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
) {
1409 if (bgp_debug_neighbor_events(peer
))
1411 "%s [FSM] Unable to get neighbor's IP address, waiting...",
1416 if (BGP_PEER_START_SUPPRESSED(peer
)) {
1417 if (bgp_debug_neighbor_events(peer
))
1418 flog_err(EC_BGP_FSM
,
1419 "%s [FSM] Trying to start suppressed peer"
1420 " - this is never supposed to happen!",
1425 /* Scrub some information that might be left over from a previous,
1428 /* Connection information. */
1429 if (peer
->su_local
) {
1430 sockunion_free(peer
->su_local
);
1431 peer
->su_local
= NULL
;
1434 if (peer
->su_remote
) {
1435 sockunion_free(peer
->su_remote
);
1436 peer
->su_remote
= NULL
;
1439 /* Clear remote router-id. */
1440 peer
->remote_id
.s_addr
= 0;
1442 /* Clear peer capability flag. */
1445 /* If the peer is passive mode, force to move to Active mode. */
1446 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSIVE
)) {
1447 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1451 if (peer
->bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
&&
1452 peer
->bgp
->vrf_id
== VRF_UNKNOWN
) {
1453 if (bgp_debug_neighbor_events(peer
))
1456 "%s [FSM] In a VRF that is not initialised yet",
1461 /* Register peer for NHT. If next hop is already resolved, proceed
1462 * with connection setup, else wait.
1464 if (!bgp_peer_reg_with_nht(peer
)) {
1465 if (bgp_zebra_num_connects()) {
1466 if (bgp_debug_neighbor_events(peer
))
1467 zlog_debug("%s [FSM] Waiting for NHT",
1470 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1475 assert(!peer
->t_write
);
1476 assert(!peer
->t_read
);
1477 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
1478 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
1479 status
= bgp_connect(peer
);
1483 if (bgp_debug_neighbor_events(peer
))
1484 zlog_debug("%s [FSM] Connect error", peer
->host
);
1485 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
1487 case connect_success
:
1488 if (bgp_debug_neighbor_events(peer
))
1490 "%s [FSM] Connect immediately success, fd %d",
1491 peer
->host
, peer
->fd
);
1492 BGP_EVENT_ADD(peer
, TCP_connection_open
);
1494 case connect_in_progress
:
1495 /* To check nonblocking connect, we wait until socket is
1496 readable or writable. */
1497 if (bgp_debug_neighbor_events(peer
))
1499 "%s [FSM] Non blocking connect waiting result, fd %d",
1500 peer
->host
, peer
->fd
);
1502 flog_err(EC_BGP_FSM
,
1503 "bgp_start peer's fd is negative value %d",
1508 * - when the socket becomes ready, poll() will signify POLLOUT
1509 * - if it fails to connect, poll() will signify POLLHUP
1510 * - POLLHUP is handled as a 'read' event by thread.c
1512 * therefore, we schedule both a read and a write event with
1513 * bgp_connect_check() as the handler for each and cancel the
1514 * unused event in that function.
1516 thread_add_read(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1517 &peer
->t_connect_check_r
);
1518 thread_add_write(bm
->master
, bgp_connect_check
, peer
, peer
->fd
,
1519 &peer
->t_connect_check_w
);
1525 /* Connect retry timer is expired when the peer status is Connect. */
1526 static int bgp_reconnect(struct peer
*peer
)
1528 if (bgp_stop(peer
) < 0)
1535 static int bgp_fsm_open(struct peer
*peer
)
1537 /* Send keepalive and make keepalive timer */
1538 bgp_keepalive_send(peer
);
1540 /* Reset holdtimer value. */
1541 BGP_TIMER_OFF(peer
->t_holdtime
);
1546 /* FSM error, unexpected event. This is error of BGP connection. So cut the
1547 peer and change to Idle status. */
1548 static int bgp_fsm_event_error(struct peer
*peer
)
1550 flog_err(EC_BGP_FSM
, "%s [FSM] unexpected packet received in state %s",
1551 peer
->host
, lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1553 return bgp_stop_with_notify(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1556 /* Hold timer expire. This is error of BGP connection. So cut the
1557 peer and change to Idle status. */
1558 static int bgp_fsm_holdtime_expire(struct peer
*peer
)
1560 if (bgp_debug_neighbor_events(peer
))
1561 zlog_debug("%s [FSM] Hold timer expire", peer
->host
);
1563 return bgp_stop_with_notify(peer
, BGP_NOTIFY_HOLD_ERR
, 0);
1567 * Transition to Established state.
1569 * Convert peer from stub to full fledged peer, set some timers, and generate
1572 static int bgp_establish(struct peer
*peer
)
1576 int nsf_af_count
= 0;
1580 other
= peer
->doppelganger
;
1581 peer
= peer_xfer_conn(peer
);
1583 flog_err(EC_BGP_CONNECT
, "%%Neighbor failed in xfer_conn");
1588 ret
= 1; /* bgp_establish specific code when xfer_conn
1591 /* Reset capability open status flag. */
1592 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
))
1593 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1595 /* Clear start timer value to default. */
1596 peer
->v_start
= BGP_INIT_START_TIMER
;
1598 /* Increment established count. */
1599 peer
->established
++;
1600 bgp_fsm_change_status(peer
, Established
);
1602 /* bgp log-neighbor-changes of neighbor Up */
1603 if (bgp_flag_check(peer
->bgp
, BGP_FLAG_LOG_NEIGHBOR_CHANGES
)) {
1604 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1605 zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up",
1607 (peer
->hostname
) ? peer
->hostname
: "Unknown",
1608 vrf
? ((vrf
->vrf_id
!= VRF_DEFAULT
)
1613 /* assign update-group/subgroup */
1614 update_group_adjust_peer_afs(peer
);
1616 /* graceful restart */
1617 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
1618 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1619 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++) {
1620 if (peer
->afc_nego
[afi
][safi
]
1621 && CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_ADV
)
1622 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1623 PEER_CAP_RESTART_AF_RCV
)) {
1624 if (peer
->nsf
[afi
][safi
]
1626 peer
->af_cap
[afi
][safi
],
1627 PEER_CAP_RESTART_AF_PRESERVE_RCV
))
1628 bgp_clear_stale_route(peer
, afi
, safi
);
1630 peer
->nsf
[afi
][safi
] = 1;
1633 if (peer
->nsf
[afi
][safi
])
1634 bgp_clear_stale_route(peer
, afi
, safi
);
1635 peer
->nsf
[afi
][safi
] = 0;
1640 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1642 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
1643 if (peer
->t_gr_stale
) {
1644 BGP_TIMER_OFF(peer
->t_gr_stale
);
1645 if (bgp_debug_neighbor_events(peer
))
1647 "%s graceful restart stalepath timer stopped",
1652 if (peer
->t_gr_restart
) {
1653 BGP_TIMER_OFF(peer
->t_gr_restart
);
1654 if (bgp_debug_neighbor_events(peer
))
1655 zlog_debug("%s graceful restart timer stopped",
1659 hook_call(peer_established
, peer
);
1661 /* Reset uptime, turn on keepalives, send current table. */
1662 if (!peer
->v_holdtime
)
1663 bgp_keepalives_on(peer
);
1665 peer
->uptime
= bgp_clock();
1667 /* Send route-refresh when ORF is enabled */
1668 FOREACH_AFI_SAFI (afi
, safi
) {
1669 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1670 PEER_CAP_ORF_PREFIX_SM_ADV
)) {
1671 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1672 PEER_CAP_ORF_PREFIX_RM_RCV
))
1673 bgp_route_refresh_send(peer
, afi
, safi
,
1675 REFRESH_IMMEDIATE
, 0);
1676 else if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1677 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))
1678 bgp_route_refresh_send(peer
, afi
, safi
,
1679 ORF_TYPE_PREFIX_OLD
,
1680 REFRESH_IMMEDIATE
, 0);
1684 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1685 FOREACH_AFI_SAFI (afi
, safi
) {
1686 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1687 PEER_CAP_ORF_PREFIX_RM_ADV
))
1688 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1689 PEER_CAP_ORF_PREFIX_SM_RCV
)
1690 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1691 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
1692 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1693 PEER_STATUS_ORF_WAIT_REFRESH
);
1696 bgp_announce_peer(peer
);
1698 /* Start the route advertisement timer to send updates to the peer - if
1700 * is not in read-only mode. If it is, the timer will be started at the
1702 * of read-only mode.
1704 if (!bgp_update_delay_active(peer
->bgp
)) {
1705 BGP_TIMER_OFF(peer
->t_routeadv
);
1706 BGP_TIMER_ON(peer
->t_routeadv
, bgp_routeadv_timer
, 0);
1709 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)) {
1710 if (bgp_debug_neighbor_events(peer
))
1712 "[Event] Deleting stub connection for peer %s",
1715 if (peer
->doppelganger
->status
> Active
)
1716 bgp_notify_send(peer
->doppelganger
, BGP_NOTIFY_CEASE
,
1717 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1719 peer_delete(peer
->doppelganger
);
1723 * If we are replacing the old peer for a doppelganger
1724 * then switch it around in the bgp->peerhash
1725 * the doppelgangers su and this peer's su are the same
1726 * so the hash_release is the same for either.
1728 hash_release(peer
->bgp
->peerhash
, peer
);
1729 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
1731 bgp_bfd_register_peer(peer
);
1735 /* Keepalive packet is received. */
1736 static int bgp_fsm_keepalive(struct peer
*peer
)
1738 BGP_TIMER_OFF(peer
->t_holdtime
);
1742 /* Update packet is received. */
1743 static int bgp_fsm_update(struct peer
*peer
)
1745 BGP_TIMER_OFF(peer
->t_holdtime
);
1749 /* This is empty event. */
1750 static int bgp_ignore(struct peer
*peer
)
1754 "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d",
1755 peer
->host
, bgp_event_str
[peer
->cur_event
],
1756 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1757 bgp_event_str
[peer
->last_event
],
1758 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1762 /* This is to handle unexpected events.. */
1763 static int bgp_fsm_exeption(struct peer
*peer
)
1767 "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d",
1768 peer
->host
, bgp_event_str
[peer
->cur_event
],
1769 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1770 bgp_event_str
[peer
->last_event
],
1771 bgp_event_str
[peer
->last_major_event
], peer
->fd
);
1772 return (bgp_stop(peer
));
1775 void bgp_fsm_event_update(struct peer
*peer
, int valid
)
1780 switch (peer
->status
) {
1783 BGP_EVENT_ADD(peer
, BGP_Start
);
1787 BGP_TIMER_OFF(peer
->t_connect
);
1788 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1793 BGP_TIMER_OFF(peer
->t_connect
);
1794 BGP_EVENT_ADD(peer
, ConnectRetry_timer_expired
);
1800 if (!valid
&& (peer
->gtsm_hops
== 1))
1801 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
1809 /* Finite State Machine structure */
1810 static const struct {
1811 int (*func
)(struct peer
*);
1813 } FSM
[BGP_STATUS_MAX
- 1][BGP_EVENTS_MAX
- 1] = {
1815 /* Idle state: In Idle state, all events other than BGP_Start is
1816 ignored. With BGP_Start event, finite state machine calls
1818 {bgp_start
, Connect
}, /* BGP_Start */
1819 {bgp_stop
, Idle
}, /* BGP_Stop */
1820 {bgp_stop
, Idle
}, /* TCP_connection_open */
1821 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1822 {bgp_ignore
, Idle
}, /* TCP_connection_open_failed */
1823 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1824 {bgp_ignore
, Idle
}, /* ConnectRetry_timer_expired */
1825 {bgp_ignore
, Idle
}, /* Hold_Timer_expired */
1826 {bgp_ignore
, Idle
}, /* KeepAlive_timer_expired */
1827 {bgp_ignore
, Idle
}, /* Receive_OPEN_message */
1828 {bgp_ignore
, Idle
}, /* Receive_KEEPALIVE_message */
1829 {bgp_ignore
, Idle
}, /* Receive_UPDATE_message */
1830 {bgp_ignore
, Idle
}, /* Receive_NOTIFICATION_message */
1831 {bgp_ignore
, Idle
}, /* Clearing_Completed */
1835 {bgp_ignore
, Connect
}, /* BGP_Start */
1836 {bgp_stop
, Idle
}, /* BGP_Stop */
1837 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1838 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1839 {bgp_connect_fail
, Active
}, /* TCP_connection_open_failed */
1840 {bgp_connect_fail
, Idle
}, /* TCP_fatal_error */
1841 {bgp_reconnect
, Connect
}, /* ConnectRetry_timer_expired */
1842 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1843 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1844 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1845 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1846 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1847 {bgp_stop
, Idle
}, /* Receive_NOTIFICATION_message */
1848 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1852 {bgp_ignore
, Active
}, /* BGP_Start */
1853 {bgp_stop
, Idle
}, /* BGP_Stop */
1854 {bgp_connect_success
, OpenSent
}, /* TCP_connection_open */
1855 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1856 {bgp_ignore
, Active
}, /* TCP_connection_open_failed */
1857 {bgp_fsm_exeption
, Idle
}, /* TCP_fatal_error */
1858 {bgp_start
, Connect
}, /* ConnectRetry_timer_expired */
1859 {bgp_fsm_exeption
, Idle
}, /* Hold_Timer_expired */
1860 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1861 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1862 {bgp_fsm_exeption
, Idle
}, /* Receive_KEEPALIVE_message */
1863 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1864 {bgp_fsm_exeption
, Idle
}, /* Receive_NOTIFICATION_message */
1865 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1869 {bgp_ignore
, OpenSent
}, /* BGP_Start */
1870 {bgp_stop
, Idle
}, /* BGP_Stop */
1871 {bgp_stop
, Active
}, /* TCP_connection_open */
1872 {bgp_stop
, Active
}, /* TCP_connection_closed */
1873 {bgp_stop
, Active
}, /* TCP_connection_open_failed */
1874 {bgp_stop
, Active
}, /* TCP_fatal_error */
1875 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1876 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1877 {bgp_fsm_exeption
, Idle
}, /* KeepAlive_timer_expired */
1878 {bgp_fsm_open
, OpenConfirm
}, /* Receive_OPEN_message */
1879 {bgp_fsm_event_error
, Idle
}, /* Receive_KEEPALIVE_message */
1880 {bgp_fsm_event_error
, Idle
}, /* Receive_UPDATE_message */
1881 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1882 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1886 {bgp_ignore
, OpenConfirm
}, /* BGP_Start */
1887 {bgp_stop
, Idle
}, /* BGP_Stop */
1888 {bgp_stop
, Idle
}, /* TCP_connection_open */
1889 {bgp_stop
, Idle
}, /* TCP_connection_closed */
1890 {bgp_stop
, Idle
}, /* TCP_connection_open_failed */
1891 {bgp_stop
, Idle
}, /* TCP_fatal_error */
1892 {bgp_fsm_exeption
, Idle
}, /* ConnectRetry_timer_expired */
1893 {bgp_fsm_holdtime_expire
, Idle
}, /* Hold_Timer_expired */
1894 {bgp_ignore
, OpenConfirm
}, /* KeepAlive_timer_expired */
1895 {bgp_fsm_exeption
, Idle
}, /* Receive_OPEN_message */
1896 {bgp_establish
, Established
}, /* Receive_KEEPALIVE_message */
1897 {bgp_fsm_exeption
, Idle
}, /* Receive_UPDATE_message */
1898 {bgp_stop_with_error
, Idle
}, /* Receive_NOTIFICATION_message */
1899 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1903 {bgp_ignore
, Established
}, /* 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_fsm_holdtime_expire
, Clearing
}, /* Hold_Timer_expired */
1911 {bgp_ignore
, Established
}, /* KeepAlive_timer_expired */
1912 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1914 Established
}, /* Receive_KEEPALIVE_message */
1915 {bgp_fsm_update
, Established
}, /* Receive_UPDATE_message */
1916 {bgp_stop_with_error
,
1917 Clearing
}, /* Receive_NOTIFICATION_message */
1918 {bgp_fsm_exeption
, Idle
}, /* Clearing_Completed */
1922 {bgp_ignore
, Clearing
}, /* BGP_Start */
1923 {bgp_stop
, Clearing
}, /* BGP_Stop */
1924 {bgp_stop
, Clearing
}, /* TCP_connection_open */
1925 {bgp_stop
, Clearing
}, /* TCP_connection_closed */
1926 {bgp_stop
, Clearing
}, /* TCP_connection_open_failed */
1927 {bgp_stop
, Clearing
}, /* TCP_fatal_error */
1928 {bgp_stop
, Clearing
}, /* ConnectRetry_timer_expired */
1929 {bgp_stop
, Clearing
}, /* Hold_Timer_expired */
1930 {bgp_stop
, Clearing
}, /* KeepAlive_timer_expired */
1931 {bgp_stop
, Clearing
}, /* Receive_OPEN_message */
1932 {bgp_stop
, Clearing
}, /* Receive_KEEPALIVE_message */
1933 {bgp_stop
, Clearing
}, /* Receive_UPDATE_message */
1934 {bgp_stop
, Clearing
}, /* Receive_NOTIFICATION_message */
1935 {bgp_clearing_completed
, Idle
}, /* Clearing_Completed */
1939 {bgp_ignore
, Deleted
}, /* BGP_Start */
1940 {bgp_ignore
, Deleted
}, /* BGP_Stop */
1941 {bgp_ignore
, Deleted
}, /* TCP_connection_open */
1942 {bgp_ignore
, Deleted
}, /* TCP_connection_closed */
1943 {bgp_ignore
, Deleted
}, /* TCP_connection_open_failed */
1944 {bgp_ignore
, Deleted
}, /* TCP_fatal_error */
1945 {bgp_ignore
, Deleted
}, /* ConnectRetry_timer_expired */
1946 {bgp_ignore
, Deleted
}, /* Hold_Timer_expired */
1947 {bgp_ignore
, Deleted
}, /* KeepAlive_timer_expired */
1948 {bgp_ignore
, Deleted
}, /* Receive_OPEN_message */
1949 {bgp_ignore
, Deleted
}, /* Receive_KEEPALIVE_message */
1950 {bgp_ignore
, Deleted
}, /* Receive_UPDATE_message */
1951 {bgp_ignore
, Deleted
}, /* Receive_NOTIFICATION_message */
1952 {bgp_ignore
, Deleted
}, /* Clearing_Completed */
1956 /* Execute event process. */
1957 int bgp_event(struct thread
*thread
)
1963 peer
= THREAD_ARG(thread
);
1964 event
= THREAD_VAL(thread
);
1966 ret
= bgp_event_update(peer
, event
);
1971 int bgp_event_update(struct peer
*peer
, int event
)
1976 int passive_conn
= 0;
1979 /* default return code */
1980 ret
= FSM_PEER_NOOP
;
1982 other
= peer
->doppelganger
;
1984 (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) ? 1 : 0;
1985 dyn_nbr
= peer_dynamic_neighbor(peer
);
1987 /* Logging this event. */
1988 next
= FSM
[peer
->status
- 1][event
- 1].next_state
;
1990 if (bgp_debug_neighbor_events(peer
) && peer
->status
!= next
)
1991 zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer
->host
,
1992 bgp_event_str
[event
],
1993 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
1994 lookup_msg(bgp_status_msg
, next
, NULL
), peer
->fd
);
1996 peer
->last_event
= peer
->cur_event
;
1997 peer
->cur_event
= event
;
1999 /* Call function. */
2000 if (FSM
[peer
->status
- 1][event
- 1].func
)
2001 ret
= (*(FSM
[peer
->status
- 1][event
- 1].func
))(peer
);
2004 if (ret
== 1 && next
== Established
) {
2005 /* The case when doppelganger swap accurred in
2007 Update the peer pointer accordingly */
2008 ret
= FSM_PEER_TRANSFERRED
;
2012 /* If status is changed. */
2013 if (next
!= peer
->status
) {
2014 bgp_fsm_change_status(peer
, next
);
2017 * If we're going to ESTABLISHED then we executed a
2018 * peer transfer. In this case we can either return
2019 * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED.
2020 * Opting for TRANSFERRED since transfer implies
2021 * session establishment.
2023 if (ret
!= FSM_PEER_TRANSFERRED
)
2024 ret
= FSM_PEER_TRANSITIONED
;
2027 /* Make sure timer is set. */
2028 bgp_timer_set(peer
);
2032 * If we got a return value of -1, that means there was an
2033 * error, restart the FSM. Since bgp_stop() was called on the
2034 * peer. only a few fields are safe to access here. In any case
2035 * we need to indicate that the peer was stopped in the return
2038 if (!dyn_nbr
&& !passive_conn
&& peer
->bgp
) {
2041 "%s [FSM] Failure handling event %s in state %s, "
2042 "prior events %s, %s, fd %d",
2043 peer
->host
, bgp_event_str
[peer
->cur_event
],
2044 lookup_msg(bgp_status_msg
, peer
->status
, NULL
),
2045 bgp_event_str
[peer
->last_event
],
2046 bgp_event_str
[peer
->last_major_event
],
2049 bgp_fsm_change_status(peer
, Idle
);
2050 bgp_timer_set(peer
);
2052 ret
= FSM_PEER_STOPPED
;