1 /* BGP packet management routine.
2 * Contains utility functions for constructing and consuming BGP messages.
3 * Copyright (C) 2017 Cumulus Networks
4 * Copyright (C) 1999 Kunihiro Ishiguro
6 * This file is part of GNU Zebra.
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h" /* for inet_ntop () */
39 #include "lib_errors.h"
41 #include "bgpd/bgpd.h"
42 #include "bgpd/bgp_table.h"
43 #include "bgpd/bgp_dump.h"
44 #include "bgpd/bgp_bmp.h"
45 #include "bgpd/bgp_attr.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_errors.h"
48 #include "bgpd/bgp_fsm.h"
49 #include "bgpd/bgp_route.h"
50 #include "bgpd/bgp_packet.h"
51 #include "bgpd/bgp_open.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_community.h"
54 #include "bgpd/bgp_ecommunity.h"
55 #include "bgpd/bgp_lcommunity.h"
56 #include "bgpd/bgp_network.h"
57 #include "bgpd/bgp_mplsvpn.h"
58 #include "bgpd/bgp_evpn.h"
59 #include "bgpd/bgp_advertise.h"
60 #include "bgpd/bgp_vty.h"
61 #include "bgpd/bgp_updgrp.h"
62 #include "bgpd/bgp_label.h"
63 #include "bgpd/bgp_io.h"
64 #include "bgpd/bgp_keepalives.h"
65 #include "bgpd/bgp_flowspec.h"
66 #include "bgpd/bgp_trace.h"
68 DEFINE_HOOK(bgp_packet_dump
,
69 (struct peer
*peer
, uint8_t type
, bgp_size_t size
,
71 (peer
, type
, size
, s
));
73 DEFINE_HOOK(bgp_packet_send
,
74 (struct peer
*peer
, uint8_t type
, bgp_size_t size
,
76 (peer
, type
, size
, s
));
79 * Sets marker and type fields for a BGP message.
81 * @param s the stream containing the packet
82 * @param type the packet type
83 * @return the size of the stream
85 int bgp_packet_set_marker(struct stream
*s
, uint8_t type
)
90 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
93 /* Dummy total length. This field is should be filled in later on. */
96 /* BGP packet type. */
99 /* Return current stream size. */
100 return stream_get_endp(s
);
104 * Sets size field for a BGP message.
106 * Size field is set to the size of the stream passed.
108 * @param s the stream containing the packet
110 void bgp_packet_set_size(struct stream
*s
)
114 /* Preserve current pointer. */
115 cp
= stream_get_endp(s
);
116 stream_putw_at(s
, BGP_MARKER_SIZE
, cp
);
120 * Push a packet onto the beginning of the peer's output queue.
121 * This function acquires the peer's write mutex before proceeding.
123 static void bgp_packet_add(struct peer
*peer
, struct stream
*s
)
125 frr_with_mutex(&peer
->io_mtx
) {
126 stream_fifo_push(peer
->obuf
, s
);
130 static struct stream
*bgp_update_packet_eor(struct peer
*peer
, afi_t afi
,
134 iana_afi_t pkt_afi
= IANA_AFI_IPV4
;
135 iana_safi_t pkt_safi
= IANA_SAFI_UNICAST
;
137 if (DISABLE_BGP_ANNOUNCE
)
140 if (bgp_debug_neighbor_events(peer
))
141 zlog_debug("send End-of-RIB for %s to %s",
142 get_afi_safi_str(afi
, safi
, false), peer
->host
);
144 s
= stream_new(peer
->max_packet_size
);
146 /* Make BGP update packet. */
147 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
149 /* Unfeasible Routes Length */
152 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
153 /* Total Path Attribute Length */
156 /* Convert AFI, SAFI to values for packet. */
157 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
159 /* Total Path Attribute Length */
161 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
162 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
164 stream_putw(s
, pkt_afi
);
165 stream_putc(s
, pkt_safi
);
168 bgp_packet_set_size(s
);
172 /* Called when there is a change in the EOR(implicit or explicit) status of a
173 * peer. Ends the update-delay if all expected peers are done with EORs. */
174 void bgp_check_update_delay(struct bgp
*bgp
)
176 struct listnode
*node
, *nnode
;
177 struct peer
*peer
= NULL
;
179 if (bgp_debug_neighbor_events(peer
))
180 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
181 bgp
->established
, bgp
->restarted_peers
,
182 bgp
->implicit_eors
, bgp
->explicit_eors
);
185 <= bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
) {
187 * This is an extra sanity check to make sure we wait for all
188 * the eligible configured peers. This check is performed if
189 * establish wait timer is on, or establish wait option is not
190 * given with the update-delay command
192 if (bgp
->t_establish_wait
193 || (bgp
->v_establish_wait
== bgp
->v_update_delay
))
194 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
195 if (CHECK_FLAG(peer
->flags
,
196 PEER_FLAG_CONFIG_NODE
)
197 && !CHECK_FLAG(peer
->flags
,
199 && !CHECK_FLAG(peer
->bgp
->flags
,
201 && !peer
->update_delay_over
) {
202 if (bgp_debug_neighbor_events(peer
))
204 " Peer %s pending, continuing read-only mode",
211 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
212 bgp
->restarted_peers
, bgp
->implicit_eors
,
214 bgp_update_delay_end(bgp
);
219 * Called if peer is known to have restarted. The restart-state bit in
220 * Graceful-Restart capability is used for that
222 void bgp_update_restarted_peers(struct peer
*peer
)
224 if (!bgp_update_delay_active(peer
->bgp
))
225 return; /* BGP update delay has ended */
226 if (peer
->update_delay_over
)
227 return; /* This peer has already been considered */
229 if (bgp_debug_neighbor_events(peer
))
230 zlog_debug("Peer %s: Checking restarted", peer
->host
);
232 if (peer_established(peer
)) {
233 peer
->update_delay_over
= 1;
234 peer
->bgp
->restarted_peers
++;
235 bgp_check_update_delay(peer
->bgp
);
240 * Called as peer receives a keep-alive. Determines if this occurence can be
241 * taken as an implicit EOR for this peer.
242 * NOTE: The very first keep-alive after the Established state of a peer is
243 * considered implicit EOR for the update-delay purposes
245 void bgp_update_implicit_eors(struct peer
*peer
)
247 if (!bgp_update_delay_active(peer
->bgp
))
248 return; /* BGP update delay has ended */
249 if (peer
->update_delay_over
)
250 return; /* This peer has already been considered */
252 if (bgp_debug_neighbor_events(peer
))
253 zlog_debug("Peer %s: Checking implicit EORs", peer
->host
);
255 if (peer_established(peer
)) {
256 peer
->update_delay_over
= 1;
257 peer
->bgp
->implicit_eors
++;
258 bgp_check_update_delay(peer
->bgp
);
263 * Should be called only when there is a change in the EOR_RECEIVED status
264 * for any afi/safi on a peer.
266 static void bgp_update_explicit_eors(struct peer
*peer
)
271 if (!bgp_update_delay_active(peer
->bgp
))
272 return; /* BGP update delay has ended */
273 if (peer
->update_delay_over
)
274 return; /* This peer has already been considered */
276 if (bgp_debug_neighbor_events(peer
))
277 zlog_debug("Peer %s: Checking explicit EORs", peer
->host
);
279 FOREACH_AFI_SAFI (afi
, safi
) {
280 if (peer
->afc_nego
[afi
][safi
]
281 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
282 PEER_STATUS_EOR_RECEIVED
)) {
283 if (bgp_debug_neighbor_events(peer
))
285 " afi %d safi %d didn't receive EOR",
291 peer
->update_delay_over
= 1;
292 peer
->bgp
->explicit_eors
++;
293 bgp_check_update_delay(peer
->bgp
);
297 * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
299 * mp_withdraw, if set, is used to nullify attr structure on most of the
300 * calling safi function and for evpn, passed as parameter
302 int bgp_nlri_parse(struct peer
*peer
, struct attr
*attr
,
303 struct bgp_nlri
*packet
, int mp_withdraw
)
305 switch (packet
->safi
) {
308 return bgp_nlri_parse_ip(peer
, mp_withdraw
? NULL
: attr
,
310 case SAFI_LABELED_UNICAST
:
311 return bgp_nlri_parse_label(peer
, mp_withdraw
? NULL
: attr
,
314 return bgp_nlri_parse_vpn(peer
, mp_withdraw
? NULL
: attr
,
317 return bgp_nlri_parse_evpn(peer
, attr
, packet
, mp_withdraw
);
319 return bgp_nlri_parse_flowspec(peer
, attr
, packet
, mp_withdraw
);
321 return BGP_NLRI_PARSE_ERROR
;
325 * Checks a variety of conditions to determine whether the peer needs to be
326 * rescheduled for packet generation again, and does so if necessary.
328 * @param peer to check for rescheduling
330 static void bgp_write_proceed_actions(struct peer
*peer
)
335 struct bpacket
*next_pkt
;
336 struct update_subgroup
*subgrp
;
337 enum bgp_af_index index
;
339 for (index
= BGP_AF_START
; index
< BGP_AF_MAX
; index
++) {
340 paf
= peer
->peer_af_array
[index
];
344 subgrp
= paf
->subgroup
;
348 next_pkt
= paf
->next_pkt_to_send
;
349 if (next_pkt
&& next_pkt
->buffer
) {
350 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
351 bgp_generate_updgrp_packets
, 0);
355 /* No packets readily available for AFI/SAFI, are there
357 * that need to be generated? */
358 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
),
360 || subgroup_packets_to_build(subgrp
)) {
361 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
362 bgp_generate_updgrp_packets
, 0);
369 /* No packets to send, see if EOR is pending */
370 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
371 if (!subgrp
->t_coalesce
&& peer
->afc_nego
[afi
][safi
]
373 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
374 PEER_STATUS_EOR_SEND
)
375 && safi
!= SAFI_MPLS_VPN
) {
376 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
377 bgp_generate_updgrp_packets
, 0);
385 * Generate advertisement information (withdraws, updates, EOR) from each
386 * update group a peer belongs to, encode this information into packets, and
387 * enqueue the packets onto the peer's output buffer.
389 void bgp_generate_updgrp_packets(struct thread
*thread
)
391 struct peer
*peer
= THREAD_ARG(thread
);
395 struct bpacket
*next_pkt
;
397 uint32_t generated
= 0;
401 wpq
= atomic_load_explicit(&peer
->bgp
->wpkt_quanta
,
402 memory_order_relaxed
);
405 * The code beyond this part deals with update packets, proceed only
406 * if peer is Established and updates are not on hold (as part of
407 * update-delay processing).
409 if (!peer_established(peer
))
412 if ((peer
->bgp
->main_peers_update_hold
)
413 || bgp_update_delay_active(peer
->bgp
))
416 if (peer
->t_routeadv
)
420 enum bgp_af_index index
;
423 for (index
= BGP_AF_START
; index
< BGP_AF_MAX
; index
++) {
424 paf
= peer
->peer_af_array
[index
];
425 if (!paf
|| !PAF_SUBGRP(paf
))
430 next_pkt
= paf
->next_pkt_to_send
;
433 * Try to generate a packet for the peer if we are at
434 * the end of the list. Always try to push out
437 if (!next_pkt
|| !next_pkt
->buffer
) {
438 next_pkt
= subgroup_withdraw_packet(
440 if (!next_pkt
|| !next_pkt
->buffer
)
441 subgroup_update_packet(PAF_SUBGRP(paf
));
442 next_pkt
= paf
->next_pkt_to_send
;
446 * If we still don't have a packet to send to the peer,
447 * then try to find out out if we have to send eor or
448 * if not, skip to the next AFI, SAFI. Don't send the
449 * EOR prematurely; if the subgroup's coalesce timer is
450 * running, the adjacency-out structure is not created
453 if (!next_pkt
|| !next_pkt
->buffer
) {
454 if (!paf
->t_announce_route
) {
455 /* Make sure we supress BGP UPDATES
456 * for normal processing later again.
458 UNSET_FLAG(paf
->subgroup
->sflags
,
459 SUBGRP_STATUS_FORCE_UPDATES
);
461 /* If route-refresh BoRR message was
462 * already sent and we are done with
463 * re-announcing tables for a decent
464 * afi/safi, we ready to send
468 peer
->af_sflags
[afi
][safi
],
469 PEER_STATUS_BORR_SEND
)) {
470 bgp_route_refresh_send(
471 peer
, afi
, safi
, 0, 0,
473 BGP_ROUTE_REFRESH_EORR
);
475 SET_FLAG(peer
->af_sflags
[afi
]
477 PEER_STATUS_EORR_SEND
);
481 PEER_STATUS_BORR_SEND
);
483 if (bgp_debug_neighbor_events(
486 "%pBP sending route-refresh (EoRR) for %s/%s",
493 if (CHECK_FLAG(peer
->cap
,
494 PEER_CAP_RESTART_RCV
)) {
495 if (!(PAF_SUBGRP(paf
))->t_coalesce
496 && peer
->afc_nego
[afi
][safi
]
499 peer
->af_sflags
[afi
][safi
],
500 PEER_STATUS_EOR_SEND
)) {
501 /* If EOR is disabled,
502 * the message is not sent
504 if (BGP_SEND_EOR(peer
->bgp
, afi
,
510 PEER_STATUS_EOR_SEND
);
528 /* Update packet send time */
529 peer
->pkt_stime
[afi
][safi
] = monotime(NULL
);
531 /* Found a packet template to send, overwrite
532 * packet with appropriate attributes from peer
533 * and advance peer */
534 s
= bpacket_reformat_for_peer(next_pkt
, paf
);
535 bgp_packet_add(peer
, s
);
536 bpacket_queue_advance_peer(paf
);
538 } while (s
&& (++generated
< wpq
));
543 bgp_write_proceed_actions(peer
);
547 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
549 void bgp_keepalive_send(struct peer
*peer
)
553 s
= stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
);
555 /* Make keepalive packet. */
556 bgp_packet_set_marker(s
, BGP_MSG_KEEPALIVE
);
558 /* Set packet size. */
559 bgp_packet_set_size(s
);
561 /* Dump packet if debug option is set. */
562 /* bgp_packet_dump (s); */
564 if (bgp_debug_keepalive(peer
))
565 zlog_debug("%s sending KEEPALIVE", peer
->host
);
567 /* Add packet to the peer. */
568 bgp_packet_add(peer
, s
);
574 * Creates a BGP Open packet and appends it to the peer's output queue.
575 * Sets capabilities as necessary.
577 void bgp_open_send(struct peer
*peer
)
580 uint16_t send_holdtime
;
583 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
584 send_holdtime
= peer
->holdtime
;
586 send_holdtime
= peer
->bgp
->default_holdtime
;
588 /* local-as Change */
589 if (peer
->change_local_as
)
590 local_as
= peer
->change_local_as
;
592 local_as
= peer
->local_as
;
594 s
= stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
);
596 /* Make open packet. */
597 bgp_packet_set_marker(s
, BGP_MSG_OPEN
);
599 /* Set open packet values. */
600 stream_putc(s
, BGP_VERSION_4
); /* BGP version */
601 stream_putw(s
, (local_as
<= BGP_AS_MAX
) ? (uint16_t)local_as
603 stream_putw(s
, send_holdtime
); /* Hold Time */
604 stream_put_in_addr(s
, &peer
->local_id
); /* BGP Identifier */
606 /* Set capabilities */
607 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_EXTENDED_OPT_PARAMS
)) {
608 (void)bgp_open_capability(s
, peer
, true);
610 struct stream
*tmp
= stream_new(STREAM_SIZE(s
));
613 if (bgp_open_capability(tmp
, peer
, false)
614 > BGP_OPEN_NON_EXT_OPT_LEN
) {
616 (void)bgp_open_capability(s
, peer
, true);
623 /* Set BGP packet length. */
624 bgp_packet_set_size(s
);
626 if (bgp_debug_neighbor_events(peer
))
628 "%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4",
629 peer
->host
, BGP_VERSION_4
, local_as
, send_holdtime
,
632 /* Dump packet if debug option is set. */
633 /* bgp_packet_dump (s); */
634 hook_call(bgp_packet_send
, peer
, BGP_MSG_OPEN
, stream_get_endp(s
), s
);
636 /* Add packet to the peer. */
637 bgp_packet_add(peer
, s
);
643 * Writes NOTIFICATION message directly to a peer socket without waiting for
646 * There must be exactly one stream on the peer->obuf FIFO, and the data within
647 * this stream must match the format of a BGP NOTIFICATION message.
648 * Transmission is best-effort.
650 * @requires peer->io_mtx
654 static void bgp_write_notify(struct peer
*peer
)
660 /* There should be at least one packet. */
661 s
= stream_fifo_pop(peer
->obuf
);
666 assert(stream_get_endp(s
) >= BGP_HEADER_SIZE
);
669 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
670 * we only care about getting a clean shutdown at this point.
672 ret
= write(peer
->fd
, STREAM_DATA(s
), stream_get_endp(s
));
675 * only connection reset/close gets counted as TCP_fatal_error, failure
676 * to write the entire NOTIFY doesn't get different FSM treatment
680 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
684 /* Disable Nagle, make NOTIFY packet go out right away */
686 (void)setsockopt(peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
,
689 /* Retrieve BGP packet type. */
690 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
691 type
= stream_getc(s
);
693 assert(type
== BGP_MSG_NOTIFY
);
695 /* Type should be notify. */
696 atomic_fetch_add_explicit(&peer
->notify_out
, 1, memory_order_relaxed
);
699 /* Double start timer. */
702 /* Overflow check. */
703 if (peer
->v_start
>= (60 * 2))
704 peer
->v_start
= (60 * 2);
707 * Handle Graceful Restart case where the state changes to
708 * Connect instead of Idle
710 BGP_EVENT_ADD(peer
, BGP_Stop
);
716 * Encapsulate an original BGP CEASE Notification into Hard Reset
718 static uint8_t *bgp_notify_encapsulate_hard_reset(uint8_t code
, uint8_t subcode
,
719 uint8_t *data
, size_t datalen
)
721 uint8_t *message
= XCALLOC(MTYPE_BGP_NOTIFICATION
, datalen
+ 2);
726 message
[1] = subcode
;
729 memcpy(message
+ 2, data
, datalen
);
735 * Decapsulate an original BGP CEASE Notification from Hard Reset
737 struct bgp_notify
bgp_notify_decapsulate_hard_reset(struct bgp_notify
*notify
)
739 struct bgp_notify bn
= {};
741 bn
.code
= notify
->raw_data
[0];
742 bn
.subcode
= notify
->raw_data
[1];
743 bn
.length
= notify
->length
- 2;
745 bn
.raw_data
= XCALLOC(MTYPE_BGP_NOTIFICATION
, bn
.length
);
746 memcpy(bn
.raw_data
, notify
->raw_data
+ 2, bn
.length
);
751 /* Check if Graceful-Restart N-bit is exchanged */
752 bool bgp_has_graceful_restart_notification(struct peer
*peer
)
754 return CHECK_FLAG(peer
->cap
, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV
) &&
755 CHECK_FLAG(peer
->cap
, PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV
);
759 * Check if to send BGP CEASE Notification/Hard Reset?
761 bool bgp_notify_send_hard_reset(struct peer
*peer
, uint8_t code
,
764 /* When the "N" bit has been exchanged, a Hard Reset message is used to
765 * indicate to the peer that the session is to be fully terminated.
767 if (!bgp_has_graceful_restart_notification(peer
))
771 * https://datatracker.ietf.org/doc/html/rfc8538#section-5.1
773 if (code
== BGP_NOTIFY_CEASE
) {
775 case BGP_NOTIFY_CEASE_MAX_PREFIX
:
776 case BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
:
777 case BGP_NOTIFY_CEASE_PEER_UNCONFIG
:
778 case BGP_NOTIFY_CEASE_HARD_RESET
:
780 case BGP_NOTIFY_CEASE_ADMIN_RESET
:
781 /* Provide user control:
782 * `bgp hard-adminstrative-reset`
784 if (CHECK_FLAG(peer
->bgp
->flags
,
785 BGP_FLAG_HARD_ADMIN_RESET
))
798 * Check if received BGP CEASE Notification/Hard Reset?
800 bool bgp_notify_received_hard_reset(struct peer
*peer
, uint8_t code
,
803 /* When the "N" bit has been exchanged, a Hard Reset message is used to
804 * indicate to the peer that the session is to be fully terminated.
806 if (!bgp_has_graceful_restart_notification(peer
))
809 if (code
== BGP_NOTIFY_CEASE
&& subcode
== BGP_NOTIFY_CEASE_HARD_RESET
)
816 * Creates a BGP Notify and appends it to the peer's output queue.
818 * This function attempts to write the packet from the thread it is called
819 * from, to ensure the packet gets out ASAP.
821 * This function may be called from multiple threads. Since the function
822 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
823 * call to prevent tampering from other threads.
825 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
826 * return, the peer structure *must* be reset; no assumptions about session
830 * @param code BGP error code
831 * @param sub_code BGP error subcode
832 * @param data Data portion
833 * @param datalen length of data portion
835 void bgp_notify_send_with_data(struct peer
*peer
, uint8_t code
,
836 uint8_t sub_code
, uint8_t *data
, size_t datalen
)
839 bool hard_reset
= bgp_notify_send_hard_reset(peer
, code
, sub_code
);
841 /* Lock I/O mutex to prevent other threads from pushing packets */
842 frr_mutex_lock_autounlock(&peer
->io_mtx
);
843 /* ============================================== */
845 /* Allocate new stream. */
846 s
= stream_new(peer
->max_packet_size
);
848 /* Make notify packet. */
849 bgp_packet_set_marker(s
, BGP_MSG_NOTIFY
);
851 /* Check if we should send Hard Reset Notification or not */
853 uint8_t *hard_reset_message
= bgp_notify_encapsulate_hard_reset(
854 code
, sub_code
, data
, datalen
);
856 /* Hard Reset encapsulates another NOTIFICATION message
857 * in its data portion.
859 stream_putc(s
, BGP_NOTIFY_CEASE
);
860 stream_putc(s
, BGP_NOTIFY_CEASE_HARD_RESET
);
861 stream_write(s
, hard_reset_message
, datalen
+ 2);
863 XFREE(MTYPE_BGP_NOTIFICATION
, hard_reset_message
);
865 stream_putc(s
, code
);
866 stream_putc(s
, sub_code
);
868 stream_write(s
, data
, datalen
);
871 /* Set BGP packet length. */
872 bgp_packet_set_size(s
);
874 /* wipe output buffer */
875 stream_fifo_clean(peer
->obuf
);
878 * If possible, store last packet for debugging purposes. This check is
879 * in place because we are sometimes called with a doppelganger peer,
880 * who tends to have a plethora of fields nulled out.
883 size_t packetsize
= stream_get_endp(peer
->curr
);
884 assert(packetsize
<= peer
->max_packet_size
);
885 memcpy(peer
->last_reset_cause
, peer
->curr
->data
, packetsize
);
886 peer
->last_reset_cause_size
= packetsize
;
891 struct bgp_notify bgp_notify
;
896 bgp_notify
.code
= code
;
897 bgp_notify
.subcode
= sub_code
;
898 bgp_notify
.data
= NULL
;
899 bgp_notify
.length
= datalen
;
900 bgp_notify
.raw_data
= data
;
902 peer
->notify
.code
= bgp_notify
.code
;
903 peer
->notify
.subcode
= bgp_notify
.subcode
;
905 if (bgp_notify
.length
&& data
) {
907 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
908 for (i
= 0; i
< bgp_notify
.length
; i
++)
910 snprintf(c
, sizeof(c
), " %02x",
913 strlcat(bgp_notify
.data
, c
,
918 snprintf(c
, sizeof(c
), "%02x", data
[i
]);
920 strlcpy(bgp_notify
.data
, c
,
924 bgp_notify_print(peer
, &bgp_notify
, "sending", hard_reset
);
926 if (bgp_notify
.data
) {
927 XFREE(MTYPE_TMP
, bgp_notify
.data
);
928 bgp_notify
.length
= 0;
932 /* peer reset cause */
933 if (code
== BGP_NOTIFY_CEASE
) {
934 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
935 peer
->last_reset
= PEER_DOWN_USER_RESET
;
936 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
937 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
939 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
941 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
943 /* Add packet to peer's output queue */
944 stream_fifo_push(peer
->obuf
, s
);
946 bgp_peer_gr_flags_update(peer
);
947 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
950 bgp_write_notify(peer
);
954 * Creates a BGP Notify and appends it to the peer's output queue.
956 * This function attempts to write the packet from the thread it is called
957 * from, to ensure the packet gets out ASAP.
960 * @param code BGP error code
961 * @param sub_code BGP error subcode
963 void bgp_notify_send(struct peer
*peer
, uint8_t code
, uint8_t sub_code
)
965 bgp_notify_send_with_data(peer
, code
, sub_code
, NULL
, 0);
969 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
972 * @param afi Address Family Identifier
973 * @param safi Subsequent Address Family Identifier
974 * @param orf_type Outbound Route Filtering type
975 * @param when_to_refresh Whether to refresh immediately or defer
976 * @param remove Whether to remove ORF for specified AFI/SAFI
978 void bgp_route_refresh_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
979 uint8_t orf_type
, uint8_t when_to_refresh
,
980 int remove
, uint8_t subtype
)
983 struct bgp_filter
*filter
;
985 iana_afi_t pkt_afi
= IANA_AFI_IPV4
;
986 iana_safi_t pkt_safi
= IANA_SAFI_UNICAST
;
988 if (DISABLE_BGP_ANNOUNCE
)
991 filter
= &peer
->filter
[afi
][safi
];
993 /* Convert AFI, SAFI to values for packet. */
994 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
996 s
= stream_new(peer
->max_packet_size
);
998 /* Make BGP update packet. */
999 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
1000 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_NEW
);
1002 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_OLD
);
1004 /* Encode Route Refresh message. */
1005 stream_putw(s
, pkt_afi
);
1007 stream_putc(s
, subtype
);
1010 stream_putc(s
, pkt_safi
);
1012 if (orf_type
== ORF_TYPE_PREFIX
|| orf_type
== ORF_TYPE_PREFIX_OLD
)
1013 if (remove
|| filter
->plist
[FILTER_IN
].plist
) {
1018 stream_putc(s
, when_to_refresh
);
1019 stream_putc(s
, orf_type
);
1020 orfp
= stream_get_endp(s
);
1024 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
1025 PEER_STATUS_ORF_PREFIX_SEND
);
1026 stream_putc(s
, ORF_COMMON_PART_REMOVE_ALL
);
1027 if (bgp_debug_neighbor_events(peer
))
1029 "%pBP sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s",
1035 iana_afi2str(pkt_afi
),
1036 iana_safi2str(pkt_safi
));
1038 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1039 PEER_STATUS_ORF_PREFIX_SEND
);
1040 prefix_bgp_orf_entry(
1041 s
, filter
->plist
[FILTER_IN
].plist
,
1042 ORF_COMMON_PART_ADD
,
1043 ORF_COMMON_PART_PERMIT
,
1044 ORF_COMMON_PART_DENY
);
1045 if (bgp_debug_neighbor_events(peer
))
1047 "%pBP sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s",
1053 iana_afi2str(pkt_afi
),
1054 iana_safi2str(pkt_safi
));
1057 /* Total ORF Entry Len. */
1058 orf_len
= stream_get_endp(s
) - orfp
- 2;
1059 stream_putw_at(s
, orfp
, orf_len
);
1062 /* Set packet size. */
1063 bgp_packet_set_size(s
);
1065 if (bgp_debug_neighbor_events(peer
)) {
1068 "%pBP sending REFRESH_REQ for afi/safi: %s/%s",
1069 peer
, iana_afi2str(pkt_afi
),
1070 iana_safi2str(pkt_safi
));
1073 /* Add packet to the peer. */
1074 bgp_packet_add(peer
, s
);
1076 bgp_writes_on(peer
);
1080 * Create a BGP Capability packet and append it to the peer's output queue.
1083 * @param afi Address Family Identifier
1084 * @param safi Subsequent Address Family Identifier
1085 * @param capability_code BGP Capability Code
1086 * @param action Set or Remove capability
1088 void bgp_capability_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
1089 int capability_code
, int action
)
1092 iana_afi_t pkt_afi
= IANA_AFI_IPV4
;
1093 iana_safi_t pkt_safi
= IANA_SAFI_UNICAST
;
1095 /* Convert AFI, SAFI to values for packet. */
1096 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
1098 s
= stream_new(peer
->max_packet_size
);
1100 /* Make BGP update packet. */
1101 bgp_packet_set_marker(s
, BGP_MSG_CAPABILITY
);
1103 /* Encode MP_EXT capability. */
1104 if (capability_code
== CAPABILITY_CODE_MP
) {
1105 stream_putc(s
, action
);
1106 stream_putc(s
, CAPABILITY_CODE_MP
);
1107 stream_putc(s
, CAPABILITY_CODE_MP_LEN
);
1108 stream_putw(s
, pkt_afi
);
1110 stream_putc(s
, pkt_safi
);
1112 if (bgp_debug_neighbor_events(peer
))
1114 "%pBP sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
1116 action
== CAPABILITY_ACTION_SET
? "Advertising"
1118 iana_afi2str(pkt_afi
), iana_safi2str(pkt_safi
));
1121 /* Set packet size. */
1122 bgp_packet_set_size(s
);
1124 /* Add packet to the peer. */
1125 bgp_packet_add(peer
, s
);
1127 bgp_writes_on(peer
);
1130 /* RFC1771 6.8 Connection collision detection. */
1131 static int bgp_collision_detect(struct peer
*new, struct in_addr remote_id
)
1136 * Upon receipt of an OPEN message, the local system must examine
1137 * all of its connections that are in the OpenConfirm state. A BGP
1138 * speaker may also examine connections in an OpenSent state if it
1139 * knows the BGP Identifier of the peer by means outside of the
1140 * protocol. If among these connections there is a connection to a
1141 * remote BGP speaker whose BGP Identifier equals the one in the
1142 * OPEN message, then the local system performs the following
1143 * collision resolution procedure:
1145 peer
= new->doppelganger
;
1150 * Do not accept the new connection in Established or Clearing
1151 * states. Note that a peer GR is handled by closing the existing
1152 * connection upon receipt of new one.
1154 if (peer_established(peer
) || peer
->status
== Clearing
) {
1155 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1156 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1160 if ((peer
->status
!= OpenConfirm
) && (peer
->status
!= OpenSent
))
1164 * 1. The BGP Identifier of the local system is
1165 * compared to the BGP Identifier of the remote
1166 * system (as specified in the OPEN message).
1168 * If the BGP Identifiers of the peers
1169 * involved in the connection collision
1170 * are identical, then the connection
1171 * initiated by the BGP speaker with the
1172 * larger AS number is preserved.
1174 if (ntohl(peer
->local_id
.s_addr
) < ntohl(remote_id
.s_addr
)
1175 || (ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)
1176 && peer
->local_as
< peer
->as
))
1177 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
1179 * 2. If the value of the local BGP
1180 * Identifier is less than the remote one,
1181 * the local system closes BGP connection
1182 * that already exists (the one that is
1183 * already in the OpenConfirm state),
1184 * and accepts BGP connection initiated by
1185 * the remote system.
1187 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1188 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1191 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1192 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1196 if (ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)
1197 && peer
->local_as
== peer
->as
)
1198 flog_err(EC_BGP_ROUTER_ID_SAME
,
1199 "Peer's router-id %pI4 is the same as ours",
1203 * 3. Otherwise, the local system closes newly
1204 * created BGP connection (the one associated with the
1205 * newly received OPEN message), and continues to use
1206 * the existing one (the one that is already in the
1207 * OpenConfirm state).
1209 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
1210 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1211 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1214 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1215 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1221 /* Packet processing routines ---------------------------------------------- */
1223 * This is a family of functions designed to be called from
1224 * bgp_process_packet(). These functions all share similar behavior and should
1225 * adhere to the following invariants and restrictions:
1229 * The return code of any one of those functions should be one of the FSM event
1230 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1231 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1232 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1233 * error and Receive_OPEN_message otherwise.
1235 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1240 * - May send NOTIFY messages
1241 * - May not modify peer->status
1242 * - May not call bgp_event_update()
1245 #define BGP_PACKET_NOOP 0
1248 * Process BGP OPEN message for peer.
1250 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1251 * and returns BGP_Stop.
1254 * @param size size of the packet
1255 * @return as in summary
1257 static int bgp_open_receive(struct peer
*peer
, bgp_size_t size
)
1263 uint16_t send_holdtime
;
1265 as_t as4
= 0, as4_be
;
1266 struct in_addr remote_id
;
1268 uint8_t notify_data_remote_as
[2];
1269 uint8_t notify_data_remote_as4
[4];
1270 uint8_t notify_data_remote_id
[4];
1271 uint16_t *holdtime_ptr
;
1273 /* Parse open packet. */
1274 version
= stream_getc(peer
->curr
);
1275 memcpy(notify_data_remote_as
, stream_pnt(peer
->curr
), 2);
1276 remote_as
= stream_getw(peer
->curr
);
1277 holdtime_ptr
= (uint16_t *)stream_pnt(peer
->curr
);
1278 holdtime
= stream_getw(peer
->curr
);
1279 memcpy(notify_data_remote_id
, stream_pnt(peer
->curr
), 4);
1280 remote_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1282 /* BEGIN to read the capability here, but dont do it yet */
1284 optlen
= stream_getc(peer
->curr
);
1286 /* Extended Optional Parameters Length for BGP OPEN Message */
1287 if (optlen
== BGP_OPEN_NON_EXT_OPT_LEN
1288 || CHECK_FLAG(peer
->flags
, PEER_FLAG_EXTENDED_OPT_PARAMS
)) {
1291 opttype
= stream_getc(peer
->curr
);
1292 if (opttype
== BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH
) {
1293 optlen
= stream_getw(peer
->curr
);
1294 SET_FLAG(peer
->sflags
,
1295 PEER_STATUS_EXT_OPT_PARAMS_LENGTH
);
1299 /* Receive OPEN message log */
1300 if (bgp_debug_neighbor_events(peer
))
1302 "%s rcv OPEN%s, version %d, remote-as (in open) %u, holdtime %d, id %pI4",
1304 CHECK_FLAG(peer
->sflags
,
1305 PEER_STATUS_EXT_OPT_PARAMS_LENGTH
)
1308 version
, remote_as
, holdtime
, &remote_id
);
1311 /* If not enough bytes, it is an error. */
1312 if (STREAM_READABLE(peer
->curr
) < optlen
) {
1313 flog_err(EC_BGP_PKT_OPEN
,
1314 "%s: stream has not enough bytes (%u)",
1315 peer
->host
, optlen
);
1316 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1317 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1321 /* We need the as4 capability value *right now* because
1322 * if it is there, we have not got the remote_as yet, and
1324 * that we do not know which peer is connecting to us now.
1326 as4
= peek_for_as4_capability(peer
, optlen
);
1329 as4_be
= htonl(as4
);
1330 memcpy(notify_data_remote_as4
, &as4_be
, 4);
1332 /* Just in case we have a silly peer who sends AS4 capability set to 0
1334 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
) {
1335 flog_err(EC_BGP_PKT_OPEN
,
1336 "%s bad OPEN, got AS4 capability, but AS4 set to 0",
1338 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1339 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1340 notify_data_remote_as4
, 4);
1344 /* Codification of AS 0 Processing */
1345 if (remote_as
== BGP_AS_ZERO
) {
1346 flog_err(EC_BGP_PKT_OPEN
, "%s bad OPEN, got AS set to 0",
1348 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1349 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1353 if (remote_as
== BGP_AS_TRANS
) {
1354 /* Take the AS4 from the capability. We must have received the
1355 * capability now! Otherwise we have a asn16 peer who uses
1356 * BGP_AS_TRANS, for some unknown reason.
1358 if (as4
== BGP_AS_TRANS
) {
1361 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1363 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1364 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1365 notify_data_remote_as4
, 4);
1369 if (!as4
&& BGP_DEBUG(as4
, AS4
))
1371 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.",
1373 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG(as4
, AS4
))
1375 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.",
1380 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1382 /* If we have got the capability, peer->as4cap must match
1384 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)
1385 && as4
!= remote_as
) {
1386 /* raise error, log this, close session */
1389 "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
1390 peer
->host
, as4
, remote_as
);
1391 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1392 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1393 notify_data_remote_as4
, 4);
1399 * If the BGP Identifier field of the OPEN message
1400 * is zero, or if it is the same as the BGP Identifier
1401 * of the local BGP speaker and the message is from an
1402 * internal peer, then the Error Subcode is set to
1403 * "Bad BGP Identifier".
1405 if (remote_id
.s_addr
== INADDR_ANY
1406 || (peer
->sort
== BGP_PEER_IBGP
1407 && ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
))) {
1408 if (bgp_debug_neighbor_events(peer
))
1409 zlog_debug("%s bad OPEN, wrong router identifier %pI4",
1410 peer
->host
, &remote_id
);
1411 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1412 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1413 notify_data_remote_id
, 4);
1417 /* Peer BGP version check. */
1418 if (version
!= BGP_VERSION_4
) {
1419 uint16_t maxver
= htons(BGP_VERSION_4
);
1420 /* XXX this reply may not be correct if version < 4 XXX */
1421 if (bgp_debug_neighbor_events(peer
))
1423 "%s bad protocol version, remote requested %d, local request %d",
1424 peer
->host
, version
, BGP_VERSION_4
);
1425 /* Data must be in network byte order here */
1426 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1427 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1428 (uint8_t *)&maxver
, 2);
1432 /* Check neighbor as number. */
1433 if (peer
->as_type
== AS_UNSPECIFIED
) {
1434 if (bgp_debug_neighbor_events(peer
))
1436 "%s bad OPEN, remote AS is unspecified currently",
1438 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1439 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1440 notify_data_remote_as
, 2);
1442 } else if (peer
->as_type
== AS_INTERNAL
) {
1443 if (remote_as
!= peer
->bgp
->as
) {
1444 if (bgp_debug_neighbor_events(peer
))
1446 "%s bad OPEN, remote AS is %u, internal specified",
1447 peer
->host
, remote_as
);
1448 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1449 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1450 notify_data_remote_as
, 2);
1453 peer
->as
= peer
->local_as
;
1454 } else if (peer
->as_type
== AS_EXTERNAL
) {
1455 if (remote_as
== peer
->bgp
->as
) {
1456 if (bgp_debug_neighbor_events(peer
))
1458 "%s bad OPEN, remote AS is %u, external specified",
1459 peer
->host
, remote_as
);
1460 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1461 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1462 notify_data_remote_as
, 2);
1465 peer
->as
= remote_as
;
1466 } else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
)) {
1467 if (bgp_debug_neighbor_events(peer
))
1468 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1469 peer
->host
, remote_as
, peer
->as
);
1470 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1471 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1472 notify_data_remote_as
, 2);
1477 * When collision is detected and this peer is closed.
1478 * Return immediately.
1480 ret
= bgp_collision_detect(peer
, remote_id
);
1485 if (bgp_getsockname(peer
) < 0) {
1486 flog_err_sys(EC_LIB_SOCKET
,
1487 "%s: bgp_getsockname() failed for peer: %s",
1488 __func__
, peer
->host
);
1492 /* Set remote router-id */
1493 peer
->remote_id
= remote_id
;
1495 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1496 calculate the value of the Hold Timer by using the smaller of its
1497 configured Hold Time and the Hold Time received in the OPEN message.
1498 The Hold Time MUST be either zero or at least three seconds. An
1499 implementation may reject connections on the basis of the Hold Time.
1502 if (holdtime
< 3 && holdtime
!= 0) {
1503 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1504 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1505 (uint8_t *)holdtime_ptr
, 2);
1509 /* Send notification message when Hold Time received in the OPEN message
1510 * is smaller than configured minimum Hold Time. */
1511 if (holdtime
< peer
->bgp
->default_min_holdtime
1512 && peer
->bgp
->default_min_holdtime
!= 0) {
1513 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1514 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1515 (uint8_t *)holdtime_ptr
, 2);
1519 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1520 would be one third of the Hold Time interval. KEEPALIVE messages
1521 MUST NOT be sent more frequently than one per second. An
1522 implementation MAY adjust the rate at which it sends KEEPALIVE
1523 messages as a function of the Hold Time interval. */
1525 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
1526 send_holdtime
= peer
->holdtime
;
1528 send_holdtime
= peer
->bgp
->default_holdtime
;
1530 if (holdtime
< send_holdtime
)
1531 peer
->v_holdtime
= holdtime
;
1533 peer
->v_holdtime
= send_holdtime
;
1535 /* Set effective keepalive to 1/3 the effective holdtime.
1536 * Use configured keeplive when < effective keepalive.
1538 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1539 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1540 if (peer
->keepalive
&& peer
->keepalive
< peer
->v_keepalive
)
1541 peer
->v_keepalive
= peer
->keepalive
;
1543 if (peer
->bgp
->default_keepalive
1544 && peer
->bgp
->default_keepalive
< peer
->v_keepalive
)
1545 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1548 /* Open option part parse. */
1550 if (bgp_open_option_parse(peer
, optlen
, &mp_capability
) < 0)
1553 if (bgp_debug_neighbor_events(peer
))
1554 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1559 * Assume that the peer supports the locally configured set of
1560 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1561 * capabilities, or if 'override-capability' is configured.
1564 || CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
1565 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] =
1566 peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1567 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] =
1568 peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1569 peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
] =
1570 peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
];
1571 peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
] =
1572 peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
];
1573 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] =
1574 peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1575 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] =
1576 peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1577 peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
] =
1578 peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
];
1579 peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
] =
1580 peer
->afc
[AFI_L2VPN
][SAFI_EVPN
];
1581 peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
] =
1582 peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
];
1585 /* Verify valid local address present based on negotiated
1586 * address-families. */
1587 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
1588 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
1589 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
1590 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
1591 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]) {
1592 if (peer
->nexthop
.v4
.s_addr
== INADDR_ANY
) {
1593 #if defined(HAVE_CUMULUS)
1594 zlog_warn("%s: No local IPv4 addr, BGP routing may not work",
1599 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
1600 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
1601 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
1602 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
1603 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]) {
1604 if (IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_global
)) {
1605 #if defined(HAVE_CUMULUS)
1606 zlog_warn("%s: No local IPv6 address, BGP routing may not work",
1611 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1613 return Receive_OPEN_message
;
1617 * Process BGP KEEPALIVE message for peer.
1620 * @param size size of the packet
1621 * @return as in summary
1623 static int bgp_keepalive_receive(struct peer
*peer
, bgp_size_t size
)
1625 if (bgp_debug_keepalive(peer
))
1626 zlog_debug("%s KEEPALIVE rcvd", peer
->host
);
1628 bgp_update_implicit_eors(peer
);
1630 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1632 /* If the peer's RTT is higher than expected, shutdown
1633 * the peer automatically.
1635 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_RTT_SHUTDOWN
)
1636 && peer
->rtt
> peer
->rtt_expected
) {
1638 peer
->rtt_keepalive_rcv
++;
1640 if (peer
->rtt_keepalive_rcv
> peer
->rtt_keepalive_conf
) {
1642 "%s shutdown due to high round-trip-time (%dms > %dms)",
1643 peer
->host
, peer
->rtt
, peer
->rtt_expected
);
1644 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1647 if (peer
->rtt_keepalive_rcv
)
1648 peer
->rtt_keepalive_rcv
--;
1651 return Receive_KEEPALIVE_message
;
1654 static void bgp_refresh_stalepath_timer_expire(struct thread
*thread
)
1656 struct peer_af
*paf
;
1658 paf
= THREAD_ARG(thread
);
1660 afi_t afi
= paf
->afi
;
1661 safi_t safi
= paf
->safi
;
1662 struct peer
*peer
= paf
->peer
;
1664 peer
->t_refresh_stalepath
= NULL
;
1666 if (peer
->nsf
[afi
][safi
])
1667 bgp_clear_stale_route(peer
, afi
, safi
);
1669 if (bgp_debug_neighbor_events(peer
))
1671 "%pBP route-refresh (BoRR) timer expired for afi/safi: %d/%d",
1674 bgp_timer_set(peer
);
1678 * Process BGP UPDATE message for peer.
1680 * Parses UPDATE and creates attribute object.
1683 * @param size size of the packet
1684 * @return as in summary
1686 static int bgp_update_receive(struct peer
*peer
, bgp_size_t size
)
1692 bgp_size_t attribute_len
;
1693 bgp_size_t update_len
;
1694 bgp_size_t withdraw_len
;
1695 bool restart
= false;
1704 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1706 /* Status must be Established. */
1707 if (!peer_established(peer
)) {
1708 flog_err(EC_BGP_INVALID_STATUS
,
1709 "%s [FSM] Update packet received under status %s",
1711 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1712 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1713 bgp_fsm_error_subcode(peer
->status
));
1717 /* Set initial values. */
1718 memset(&attr
, 0, sizeof(attr
));
1719 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
1720 attr
.label
= MPLS_INVALID_LABEL
;
1721 memset(&nlris
, 0, sizeof(nlris
));
1722 memset(peer
->rcvd_attr_str
, 0, BUFSIZ
);
1723 peer
->rcvd_attr_printed
= 0;
1726 end
= stream_pnt(s
) + size
;
1728 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1729 Length is too large (i.e., if Unfeasible Routes Length + Total
1730 Attribute Length + 23 exceeds the message Length), then the Error
1731 Subcode is set to Malformed Attribute List. */
1732 if (stream_pnt(s
) + 2 > end
) {
1733 flog_err(EC_BGP_UPDATE_RCV
,
1734 "%s [Error] Update packet error (packet length is short for unfeasible length)",
1736 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1737 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1741 /* Unfeasible Route Length. */
1742 withdraw_len
= stream_getw(s
);
1744 /* Unfeasible Route Length check. */
1745 if (stream_pnt(s
) + withdraw_len
> end
) {
1746 flog_err(EC_BGP_UPDATE_RCV
,
1747 "%s [Error] Update packet error (packet unfeasible length overflow %d)",
1748 peer
->host
, withdraw_len
);
1749 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1750 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1754 /* Unfeasible Route packet format check. */
1755 if (withdraw_len
> 0) {
1756 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1757 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1758 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt(s
);
1759 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1760 stream_forward_getp(s
, withdraw_len
);
1763 /* Attribute total length check. */
1764 if (stream_pnt(s
) + 2 > end
) {
1766 EC_BGP_UPDATE_PACKET_SHORT
,
1767 "%s [Error] Packet Error (update packet is short for attribute length)",
1769 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1770 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1774 /* Fetch attribute total length. */
1775 attribute_len
= stream_getw(s
);
1777 /* Attribute length check. */
1778 if (stream_pnt(s
) + attribute_len
> end
) {
1780 EC_BGP_UPDATE_PACKET_LONG
,
1781 "%s [Error] Packet Error (update packet attribute length overflow %d)",
1782 peer
->host
, attribute_len
);
1783 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1784 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1788 /* Certain attribute parsing errors should not be considered bad enough
1789 * to reset the session for, most particularly any partial/optional
1790 * attributes that have 'tunneled' over speakers that don't understand
1791 * them. Instead we withdraw only the prefix concerned.
1793 * Complicates the flow a little though..
1795 enum bgp_attr_parse_ret attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1796 /* This define morphs the update case into a withdraw when lower levels
1797 * have signalled an error condition where this is best.
1799 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1801 /* Parse attribute when it exists. */
1802 if (attribute_len
) {
1803 attr_parse_ret
= bgp_attr_parse(peer
, &attr
, attribute_len
,
1804 &nlris
[NLRI_MP_UPDATE
],
1805 &nlris
[NLRI_MP_WITHDRAW
]);
1806 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
) {
1807 bgp_attr_unintern_sub(&attr
);
1812 /* Logging the attribute. */
1813 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1814 || BGP_DEBUG(update
, UPDATE_IN
)
1815 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
1816 ret
= bgp_dump_attr(&attr
, peer
->rcvd_attr_str
,
1817 sizeof(peer
->rcvd_attr_str
));
1819 peer
->stat_upd_7606
++;
1821 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1824 "%pBP rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1827 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1)) {
1828 zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer
,
1829 peer
->rcvd_attr_str
);
1830 peer
->rcvd_attr_printed
= 1;
1834 /* Network Layer Reachability Information. */
1835 update_len
= end
- stream_pnt(s
);
1838 /* Set NLRI portion to structure. */
1839 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1840 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1841 nlris
[NLRI_UPDATE
].nlri
= stream_pnt(s
);
1842 nlris
[NLRI_UPDATE
].length
= update_len
;
1843 stream_forward_getp(s
, update_len
);
1845 if (CHECK_FLAG(attr
.flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
))) {
1847 * We skipped nexthop attribute validation earlier so
1848 * validate the nexthop now.
1850 if (bgp_attr_nexthop_valid(peer
, &attr
) < 0) {
1851 bgp_attr_unintern_sub(&attr
);
1857 if (BGP_DEBUG(update
, UPDATE_IN
))
1858 zlog_debug("%pBP rcvd UPDATE wlen %d attrlen %d alen %d", peer
,
1859 withdraw_len
, attribute_len
, update_len
);
1861 /* Parse any given NLRIs */
1862 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++) {
1866 /* NLRI is processed iff the peer if configured for the specific
1868 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
]) {
1870 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1871 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1875 /* EoR handled later */
1876 if (nlris
[i
].length
== 0)
1881 case NLRI_MP_UPDATE
:
1882 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
1886 case NLRI_MP_WITHDRAW
:
1887 nlri_ret
= bgp_nlri_parse(peer
, &attr
, &nlris
[i
], 1);
1890 nlri_ret
= BGP_NLRI_PARSE_ERROR
;
1893 if (nlri_ret
< BGP_NLRI_PARSE_OK
1894 && nlri_ret
!= BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW
) {
1895 flog_err(EC_BGP_UPDATE_RCV
,
1896 "%s [Error] Error parsing NLRI", peer
->host
);
1897 if (peer_established(peer
))
1899 peer
, BGP_NOTIFY_UPDATE_ERR
,
1901 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1902 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1903 bgp_attr_unintern_sub(&attr
);
1910 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1911 * and MP EoR should have only an empty MP_UNREACH
1913 if ((!update_len
&& !withdraw_len
&& nlris
[NLRI_MP_UPDATE
].length
== 0)
1914 || (attr_parse_ret
== BGP_ATTR_PARSE_EOR
)) {
1917 struct graceful_restart_info
*gr_info
;
1919 /* Restarting router */
1920 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)
1921 && BGP_PEER_RESTARTING_MODE(peer
))
1924 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1926 * update and withdraw NLRI lengths are 0.
1928 if (!attribute_len
) {
1930 safi
= SAFI_UNICAST
;
1931 } else if (attr
.flag
& ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
)
1932 && nlris
[NLRI_MP_WITHDRAW
].length
== 0) {
1933 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1934 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1935 } else if (attr_parse_ret
== BGP_ATTR_PARSE_EOR
) {
1936 afi
= nlris
[NLRI_MP_UPDATE
].afi
;
1937 safi
= nlris
[NLRI_MP_UPDATE
].safi
;
1940 if (afi
&& peer
->afc
[afi
][safi
]) {
1941 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1943 /* End-of-RIB received */
1944 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1945 PEER_STATUS_EOR_RECEIVED
)) {
1946 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1947 PEER_STATUS_EOR_RECEIVED
);
1948 bgp_update_explicit_eors(peer
);
1949 /* Update graceful restart information */
1950 gr_info
= &(peer
->bgp
->gr_info
[afi
][safi
]);
1952 gr_info
->eor_received
++;
1953 /* If EOR received from all peers and selection
1954 * deferral timer is running, cancel the timer
1955 * and invoke the best path calculation
1957 if (gr_info
->eor_required
1958 == gr_info
->eor_received
) {
1959 if (bgp_debug_neighbor_events(peer
))
1963 gr_info
->eor_required
,
1965 gr_info
->eor_received
);
1967 gr_info
->t_select_deferral
);
1968 gr_info
->eor_required
= 0;
1969 gr_info
->eor_received
= 0;
1970 /* Best path selection */
1971 if (bgp_best_path_select_defer(
1972 peer
->bgp
, afi
, safi
)
1978 /* NSF delete stale route */
1979 if (peer
->nsf
[afi
][safi
])
1980 bgp_clear_stale_route(peer
, afi
, safi
);
1983 "%s: rcvd End-of-RIB for %s from %s in vrf %s",
1984 __func__
, get_afi_safi_str(afi
, safi
, false),
1985 peer
->host
, vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
1989 /* Everything is done. We unintern temporary structures which
1990 interned in bgp_attr_parse(). */
1991 bgp_attr_unintern_sub(&attr
);
1993 peer
->update_time
= bgp_clock();
1995 /* Notify BGP Conditional advertisement scanner process */
1996 peer
->advmap_table_change
= true;
1998 return Receive_UPDATE_message
;
2002 * Process BGP NOTIFY message for peer.
2005 * @param size size of the packet
2006 * @return as in summary
2008 static int bgp_notify_receive(struct peer
*peer
, bgp_size_t size
)
2010 struct bgp_notify outer
= {};
2011 struct bgp_notify inner
= {};
2012 bool hard_reset
= false;
2014 if (peer
->notify
.data
) {
2015 XFREE(MTYPE_BGP_NOTIFICATION
, peer
->notify
.data
);
2016 peer
->notify
.length
= 0;
2017 peer
->notify
.hard_reset
= false;
2020 outer
.code
= stream_getc(peer
->curr
);
2021 outer
.subcode
= stream_getc(peer
->curr
);
2022 outer
.length
= size
- 2;
2024 outer
.raw_data
= NULL
;
2026 outer
.raw_data
= XMALLOC(MTYPE_BGP_NOTIFICATION
, outer
.length
);
2027 memcpy(outer
.raw_data
, stream_pnt(peer
->curr
), outer
.length
);
2031 bgp_notify_received_hard_reset(peer
, outer
.code
, outer
.subcode
);
2032 if (hard_reset
&& outer
.length
) {
2033 inner
= bgp_notify_decapsulate_hard_reset(&outer
);
2034 peer
->notify
.hard_reset
= true;
2039 /* Preserv notify code and sub code. */
2040 peer
->notify
.code
= inner
.code
;
2041 peer
->notify
.subcode
= inner
.subcode
;
2042 /* For further diagnostic record returned Data. */
2044 peer
->notify
.length
= inner
.length
;
2046 XMALLOC(MTYPE_BGP_NOTIFICATION
, inner
.length
);
2047 memcpy(peer
->notify
.data
, inner
.raw_data
, inner
.length
);
2057 inner
.data
= XMALLOC(MTYPE_BGP_NOTIFICATION
,
2059 for (i
= 0; i
< inner
.length
; i
++)
2061 snprintf(c
, sizeof(c
), " %02x",
2062 stream_getc(peer
->curr
));
2064 strlcat(inner
.data
, c
,
2069 snprintf(c
, sizeof(c
), "%02x",
2070 stream_getc(peer
->curr
));
2072 strlcpy(inner
.data
, c
,
2077 bgp_notify_print(peer
, &inner
, "received", hard_reset
);
2079 XFREE(MTYPE_BGP_NOTIFICATION
, inner
.data
);
2083 XFREE(MTYPE_BGP_NOTIFICATION
, outer
.data
);
2084 XFREE(MTYPE_BGP_NOTIFICATION
, outer
.raw_data
);
2089 /* peer count update */
2090 atomic_fetch_add_explicit(&peer
->notify_in
, 1, memory_order_relaxed
);
2092 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
2094 /* We have to check for Notify with Unsupported Optional Parameter.
2095 in that case we fallback to open without the capability option.
2096 But this done in bgp_stop. We just mark it here to avoid changing
2098 if (inner
.code
== BGP_NOTIFY_OPEN_ERR
&&
2099 inner
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
2100 UNSET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
2102 /* If Graceful-Restart N-bit (Notification) is exchanged,
2103 * and it's not a Hard Reset, let's retain the routes.
2105 if (bgp_has_graceful_restart_notification(peer
) && !hard_reset
&&
2106 CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
))
2107 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2109 bgp_peer_gr_flags_update(peer
);
2110 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
2113 return Receive_NOTIFICATION_message
;
2117 * Process BGP ROUTEREFRESH message for peer.
2120 * @param size size of the packet
2121 * @return as in summary
2123 static int bgp_route_refresh_receive(struct peer
*peer
, bgp_size_t size
)
2127 iana_safi_t pkt_safi
;
2130 struct peer_af
*paf
;
2131 struct update_group
*updgrp
;
2132 struct peer
*updgrp_peer
;
2134 bool force_update
= false;
2135 bgp_size_t msg_length
=
2136 size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
);
2138 /* If peer does not have the capability, send notification. */
2139 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_ADV
)) {
2140 flog_err(EC_BGP_NO_CAP
,
2141 "%s [Error] BGP route refresh is not enabled",
2143 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2144 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2148 /* Status must be Established. */
2149 if (!peer_established(peer
)) {
2151 EC_BGP_INVALID_STATUS
,
2152 "%s [Error] Route refresh packet received under status %s",
2154 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2155 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
2156 bgp_fsm_error_subcode(peer
->status
));
2163 pkt_afi
= stream_getw(s
);
2164 subtype
= stream_getc(s
);
2165 pkt_safi
= stream_getc(s
);
2167 /* Convert AFI, SAFI to internal values and check. */
2168 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
2170 "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
2171 peer
->host
, iana_afi2str(pkt_afi
),
2172 iana_safi2str(pkt_safi
));
2173 return BGP_PACKET_NOOP
;
2176 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) {
2178 uint8_t when_to_refresh
;
2183 /* If the length, excluding the fixed-size message
2184 * header, of the received ROUTE-REFRESH message with
2185 * Message Subtype 1 and 2 is not 4, then the BGP
2186 * speaker MUST send a NOTIFICATION message with the
2187 * Error Code of "ROUTE-REFRESH Message Error" and the
2188 * subcode of "Invalid Message Length".
2190 if (msg_length
!= 4) {
2192 "%s Enhanced Route Refresh message length error",
2195 peer
, BGP_NOTIFY_ROUTE_REFRESH_ERR
,
2196 BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN
);
2199 /* When the BGP speaker receives a ROUTE-REFRESH message
2200 * with a "Message Subtype" field other than 0, 1, or 2,
2201 * it MUST ignore the received ROUTE-REFRESH message.
2205 "%s Enhanced Route Refresh invalid subtype",
2209 if (msg_length
< 5) {
2210 zlog_info("%s ORF route refresh length error",
2212 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2213 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2217 when_to_refresh
= stream_getc(s
);
2218 end
= stream_pnt(s
) + (size
- 5);
2220 while ((stream_pnt(s
) + 2) < end
) {
2221 orf_type
= stream_getc(s
);
2222 orf_len
= stream_getw(s
);
2224 /* orf_len in bounds? */
2225 if ((stream_pnt(s
) + orf_len
) > end
)
2226 break; /* XXX: Notify instead?? */
2227 if (orf_type
== ORF_TYPE_PREFIX
2228 || orf_type
== ORF_TYPE_PREFIX_OLD
) {
2229 uint8_t *p_pnt
= stream_pnt(s
);
2230 uint8_t *p_end
= stream_pnt(s
) + orf_len
;
2231 struct orf_prefix orfp
;
2236 int ret
= CMD_SUCCESS
;
2238 if (bgp_debug_neighbor_events(peer
)) {
2240 "%pBP rcvd Prefixlist ORF(%d) length %d",
2241 peer
, orf_type
, orf_len
);
2244 /* we're going to read at least 1 byte of common
2246 * and 7 bytes of ORF Address-filter entry from
2252 /* ORF prefix-list name */
2253 snprintf(name
, sizeof(name
), "%s.%d.%d",
2254 peer
->host
, afi
, safi
);
2256 while (p_pnt
< p_end
) {
2257 /* If the ORF entry is malformed, want
2258 * to read as much of it
2259 * as possible without going beyond the
2260 * bounds of the entry,
2261 * to maximise debug information.
2264 memset(&orfp
, 0, sizeof(orfp
));
2266 /* after ++: p_pnt <= p_end */
2268 & ORF_COMMON_PART_REMOVE_ALL
) {
2269 if (bgp_debug_neighbor_events(
2272 "%pBP rcvd Remove-All pfxlist ORF request",
2274 prefix_bgp_orf_remove_all(afi
,
2278 ok
= ((uint32_t)(p_end
- p_pnt
)
2279 >= sizeof(uint32_t));
2283 p_pnt
+= sizeof(uint32_t);
2284 orfp
.seq
= ntohl(seq
);
2288 /* val checked in prefix_bgp_orf_set */
2292 /* val checked in prefix_bgp_orf_set */
2296 if ((ok
= (p_pnt
< p_end
)))
2297 orfp
.p
.prefixlen
= *p_pnt
++;
2299 /* afi checked already */
2300 orfp
.p
.family
= afi2family(afi
);
2303 psize
= PSIZE(orfp
.p
.prefixlen
);
2304 /* valid for family ? */
2305 if (psize
> prefix_blen(&orfp
.p
)) {
2307 psize
= prefix_blen(&orfp
.p
);
2309 /* valid for packet ? */
2310 if (psize
> (p_end
- p_pnt
)) {
2312 psize
= p_end
- p_pnt
;
2316 memcpy(&orfp
.p
.u
.prefix
, p_pnt
,
2320 if (bgp_debug_neighbor_events(peer
)) {
2321 char buf
[INET6_BUFSIZ
];
2324 "%pBP rcvd %s %s seq %u %s/%d ge %d le %d%s",
2326 (common
& ORF_COMMON_PART_REMOVE
2329 (common
& ORF_COMMON_PART_DENY
2340 ok
? "" : " MALFORMED");
2344 ret
= prefix_bgp_orf_set(
2346 (common
& ORF_COMMON_PART_DENY
2349 (common
& ORF_COMMON_PART_REMOVE
2353 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
)) {
2355 "%pBP Received misformatted prefixlist ORF. Remove All pfxlist",
2357 prefix_bgp_orf_remove_all(afi
,
2363 peer
->orf_plist
[afi
][safi
] =
2364 prefix_bgp_orf_lookup(afi
, name
);
2366 stream_forward_getp(s
, orf_len
);
2368 if (bgp_debug_neighbor_events(peer
))
2369 zlog_debug("%pBP rcvd Refresh %s ORF request", peer
,
2370 when_to_refresh
== REFRESH_DEFER
2373 if (when_to_refresh
== REFRESH_DEFER
)
2374 return BGP_PACKET_NOOP
;
2377 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2378 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2379 PEER_STATUS_ORF_WAIT_REFRESH
))
2380 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2381 PEER_STATUS_ORF_WAIT_REFRESH
);
2383 paf
= peer_af_find(peer
, afi
, safi
);
2384 if (paf
&& paf
->subgroup
) {
2385 if (peer
->orf_plist
[afi
][safi
]) {
2386 updgrp
= PAF_UPDGRP(paf
);
2387 updgrp_peer
= UPDGRP_PEER(updgrp
);
2388 updgrp_peer
->orf_plist
[afi
][safi
] =
2389 peer
->orf_plist
[afi
][safi
];
2392 /* Avoid supressing duplicate routes later
2393 * when processing in subgroup_announce_table().
2395 force_update
= true;
2397 /* If the peer is configured for default-originate clear the
2398 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
2402 if (CHECK_FLAG(paf
->subgroup
->sflags
,
2403 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
2404 UNSET_FLAG(paf
->subgroup
->sflags
,
2405 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
2408 if (subtype
== BGP_ROUTE_REFRESH_BORR
) {
2409 /* A BGP speaker that has received the Graceful Restart
2410 * Capability from its neighbor MUST ignore any BoRRs for
2411 * an <AFI, SAFI> from the neighbor before the speaker
2412 * receives the EoR for the given <AFI, SAFI> from the
2415 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)
2416 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2417 PEER_STATUS_EOR_RECEIVED
)) {
2418 if (bgp_debug_neighbor_events(peer
))
2420 "%pBP rcvd route-refresh (BoRR) for %s/%s before EoR",
2421 peer
, afi2str(afi
), safi2str(safi
));
2422 return BGP_PACKET_NOOP
;
2425 if (peer
->t_refresh_stalepath
) {
2426 if (bgp_debug_neighbor_events(peer
))
2428 "%pBP rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
2429 peer
, afi2str(afi
), safi2str(safi
));
2430 return BGP_PACKET_NOOP
;
2433 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_BORR_RECEIVED
);
2434 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2435 PEER_STATUS_EORR_RECEIVED
);
2437 /* When a BGP speaker receives a BoRR message from
2438 * a peer, it MUST mark all the routes with the given
2439 * Address Family Identifier and Subsequent Address
2440 * Family Identifier, <AFI, SAFI> [RFC2918], from
2441 * that peer as stale.
2443 if (peer_active_nego(peer
)) {
2444 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2445 PEER_STATUS_ENHANCED_REFRESH
);
2446 bgp_set_stale_route(peer
, afi
, safi
);
2449 if (peer_established(peer
))
2450 thread_add_timer(bm
->master
,
2451 bgp_refresh_stalepath_timer_expire
,
2452 paf
, peer
->bgp
->stalepath_time
,
2453 &peer
->t_refresh_stalepath
);
2455 if (bgp_debug_neighbor_events(peer
))
2457 "%pBP rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
2458 peer
, afi2str(afi
), safi2str(safi
),
2459 peer
->bgp
->stalepath_time
);
2460 } else if (subtype
== BGP_ROUTE_REFRESH_EORR
) {
2461 if (!peer
->t_refresh_stalepath
) {
2463 "%pBP rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
2464 peer
, afi2str(afi
), safi2str(safi
));
2465 return BGP_PACKET_NOOP
;
2468 BGP_TIMER_OFF(peer
->t_refresh_stalepath
);
2470 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_EORR_RECEIVED
);
2471 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2472 PEER_STATUS_BORR_RECEIVED
);
2474 if (bgp_debug_neighbor_events(peer
))
2476 "%pBP rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
2477 peer
, afi2str(afi
), safi2str(safi
));
2479 if (peer
->nsf
[afi
][safi
])
2480 bgp_clear_stale_route(peer
, afi
, safi
);
2482 if (bgp_debug_neighbor_events(peer
))
2484 "%pBP rcvd route-refresh (REQUEST) for %s/%s",
2485 peer
, afi2str(afi
), safi2str(safi
));
2487 /* In response to a "normal route refresh request" from the
2488 * peer, the speaker MUST send a BoRR message.
2490 if (CHECK_FLAG(peer
->cap
, PEER_CAP_ENHANCED_RR_RCV
)) {
2491 /* For a BGP speaker that supports the BGP Graceful
2492 * Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
2493 * to a neighbor before it sends the EoR for the
2494 * <AFI, SAFI> to the neighbor.
2496 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2497 PEER_STATUS_EOR_SEND
)) {
2498 if (bgp_debug_neighbor_events(peer
))
2500 "%pBP rcvd route-refresh (REQUEST) for %s/%s before EoR",
2503 return BGP_PACKET_NOOP
;
2506 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
2507 BGP_ROUTE_REFRESH_BORR
);
2509 if (bgp_debug_neighbor_events(peer
))
2511 "%pBP sending route-refresh (BoRR) for %s/%s",
2512 peer
, afi2str(afi
), safi2str(safi
));
2514 /* Set flag Ready-To-Send to know when we can send EoRR
2517 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2518 PEER_STATUS_BORR_SEND
);
2519 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2520 PEER_STATUS_EORR_SEND
);
2524 /* Perform route refreshment to the peer */
2525 bgp_announce_route(peer
, afi
, safi
, force_update
);
2527 /* No FSM action necessary */
2528 return BGP_PACKET_NOOP
;
2532 * Parse BGP CAPABILITY message for peer.
2535 * @param size size of the packet
2536 * @return as in summary
2538 static int bgp_capability_msg_parse(struct peer
*peer
, uint8_t *pnt
,
2542 struct capability_mp_data mpc
;
2543 struct capability_header
*hdr
;
2547 iana_safi_t pkt_safi
;
2553 /* We need at least action, capability code and capability
2555 if (pnt
+ 3 > end
) {
2556 zlog_info("%s Capability length error", peer
->host
);
2557 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2558 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2562 hdr
= (struct capability_header
*)(pnt
+ 1);
2564 /* Action value check. */
2565 if (action
!= CAPABILITY_ACTION_SET
2566 && action
!= CAPABILITY_ACTION_UNSET
) {
2567 zlog_info("%s Capability Action Value error %d",
2568 peer
->host
, action
);
2569 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2570 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2574 if (bgp_debug_neighbor_events(peer
))
2576 "%s CAPABILITY has action: %d, code: %u, length %u",
2577 peer
->host
, action
, hdr
->code
, hdr
->length
);
2579 /* Capability length check. */
2580 if ((pnt
+ hdr
->length
+ 3) > end
) {
2581 zlog_info("%s Capability length error", peer
->host
);
2582 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2583 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2587 /* Fetch structure to the byte stream. */
2588 memcpy(&mpc
, pnt
+ 3, sizeof(struct capability_mp_data
));
2589 pnt
+= hdr
->length
+ 3;
2591 /* We know MP Capability Code. */
2592 if (hdr
->code
== CAPABILITY_CODE_MP
) {
2593 pkt_afi
= ntohs(mpc
.afi
);
2594 pkt_safi
= mpc
.safi
;
2596 /* Ignore capability when override-capability is set. */
2597 if (CHECK_FLAG(peer
->flags
,
2598 PEER_FLAG_OVERRIDE_CAPABILITY
))
2601 /* Convert AFI, SAFI to internal values. */
2602 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
,
2604 if (bgp_debug_neighbor_events(peer
))
2606 "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)",
2608 iana_afi2str(pkt_afi
),
2609 iana_safi2str(pkt_safi
));
2613 /* Address family check. */
2614 if (bgp_debug_neighbor_events(peer
))
2616 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
2618 action
== CAPABILITY_ACTION_SET
2621 iana_afi2str(pkt_afi
),
2622 iana_safi2str(pkt_safi
));
2624 if (action
== CAPABILITY_ACTION_SET
) {
2625 peer
->afc_recv
[afi
][safi
] = 1;
2626 if (peer
->afc
[afi
][safi
]) {
2627 peer
->afc_nego
[afi
][safi
] = 1;
2628 bgp_announce_route(peer
, afi
, safi
,
2632 peer
->afc_recv
[afi
][safi
] = 0;
2633 peer
->afc_nego
[afi
][safi
] = 0;
2635 if (peer_active_nego(peer
))
2636 bgp_clear_route(peer
, afi
, safi
);
2642 EC_BGP_UNRECOGNIZED_CAPABILITY
,
2643 "%s unrecognized capability code: %d - ignored",
2644 peer
->host
, hdr
->code
);
2648 /* No FSM action necessary */
2649 return BGP_PACKET_NOOP
;
2653 * Parse BGP CAPABILITY message for peer.
2655 * Exported for unit testing.
2658 * @param size size of the packet
2659 * @return as in summary
2661 int bgp_capability_receive(struct peer
*peer
, bgp_size_t size
)
2665 /* Fetch pointer. */
2666 pnt
= stream_pnt(peer
->curr
);
2668 if (bgp_debug_neighbor_events(peer
))
2669 zlog_debug("%s rcv CAPABILITY", peer
->host
);
2671 /* If peer does not have the capability, send notification. */
2672 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_ADV
)) {
2673 flog_err(EC_BGP_NO_CAP
,
2674 "%s [Error] BGP dynamic capability is not enabled",
2676 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2677 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2681 /* Status must be Established. */
2682 if (!peer_established(peer
)) {
2685 "%s [Error] Dynamic capability packet received under status %s",
2687 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2688 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
2689 bgp_fsm_error_subcode(peer
->status
));
2694 return bgp_capability_msg_parse(peer
, pnt
, size
);
2698 * Processes a peer's input buffer.
2700 * This function sidesteps the event loop and directly calls bgp_event_update()
2701 * after processing each BGP message. This is necessary to ensure proper
2702 * ordering of FSM events and unifies the behavior that was present previously,
2703 * whereby some of the packet handling functions would update the FSM and some
2704 * would not, making event flow difficult to understand. Please think twice
2705 * before hacking this.
2707 * Thread type: THREAD_EVENT
2711 void bgp_process_packet(struct thread
*thread
)
2713 /* Yes first of all get peer pointer. */
2714 struct peer
*peer
; // peer
2715 uint32_t rpkt_quanta_old
; // how many packets to read
2716 int fsm_update_result
; // return code of bgp_event_update()
2717 int mprc
; // message processing return code
2719 peer
= THREAD_ARG(thread
);
2720 rpkt_quanta_old
= atomic_load_explicit(&peer
->bgp
->rpkt_quanta
,
2721 memory_order_relaxed
);
2722 fsm_update_result
= 0;
2724 /* Guard against scheduled events that occur after peer deletion. */
2725 if (peer
->status
== Deleted
|| peer
->status
== Clearing
)
2728 unsigned int processed
= 0;
2730 while (processed
< rpkt_quanta_old
) {
2733 char notify_data_length
[2];
2735 frr_with_mutex(&peer
->io_mtx
) {
2736 peer
->curr
= stream_fifo_pop(peer
->ibuf
);
2739 if (peer
->curr
== NULL
) // no packets to process, hmm...
2742 /* skip the marker and copy the packet length */
2743 stream_forward_getp(peer
->curr
, BGP_MARKER_SIZE
);
2744 memcpy(notify_data_length
, stream_pnt(peer
->curr
), 2);
2746 /* read in the packet length and type */
2747 size
= stream_getw(peer
->curr
);
2748 type
= stream_getc(peer
->curr
);
2750 hook_call(bgp_packet_dump
, peer
, type
, size
, peer
->curr
);
2752 /* adjust size to exclude the marker + length + type */
2753 size
-= BGP_HEADER_SIZE
;
2755 /* Read rest of the packet and call each sort of packet routine
2759 frrtrace(2, frr_bgp
, open_process
, peer
, size
);
2760 atomic_fetch_add_explicit(&peer
->open_in
, 1,
2761 memory_order_relaxed
);
2762 mprc
= bgp_open_receive(peer
, size
);
2763 if (mprc
== BGP_Stop
)
2766 "%s: BGP OPEN receipt failed for peer: %s",
2767 __func__
, peer
->host
);
2769 case BGP_MSG_UPDATE
:
2770 frrtrace(2, frr_bgp
, update_process
, peer
, size
);
2771 atomic_fetch_add_explicit(&peer
->update_in
, 1,
2772 memory_order_relaxed
);
2773 peer
->readtime
= monotime(NULL
);
2774 mprc
= bgp_update_receive(peer
, size
);
2775 if (mprc
== BGP_Stop
)
2778 "%s: BGP UPDATE receipt failed for peer: %s",
2779 __func__
, peer
->host
);
2781 case BGP_MSG_NOTIFY
:
2782 frrtrace(2, frr_bgp
, notification_process
, peer
, size
);
2783 atomic_fetch_add_explicit(&peer
->notify_in
, 1,
2784 memory_order_relaxed
);
2785 mprc
= bgp_notify_receive(peer
, size
);
2786 if (mprc
== BGP_Stop
)
2789 "%s: BGP NOTIFY receipt failed for peer: %s",
2790 __func__
, peer
->host
);
2792 case BGP_MSG_KEEPALIVE
:
2793 frrtrace(2, frr_bgp
, keepalive_process
, peer
, size
);
2794 peer
->readtime
= monotime(NULL
);
2795 atomic_fetch_add_explicit(&peer
->keepalive_in
, 1,
2796 memory_order_relaxed
);
2797 mprc
= bgp_keepalive_receive(peer
, size
);
2798 if (mprc
== BGP_Stop
)
2801 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2802 __func__
, peer
->host
);
2804 case BGP_MSG_ROUTE_REFRESH_NEW
:
2805 case BGP_MSG_ROUTE_REFRESH_OLD
:
2806 frrtrace(2, frr_bgp
, refresh_process
, peer
, size
);
2807 atomic_fetch_add_explicit(&peer
->refresh_in
, 1,
2808 memory_order_relaxed
);
2809 mprc
= bgp_route_refresh_receive(peer
, size
);
2810 if (mprc
== BGP_Stop
)
2813 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2814 __func__
, peer
->host
);
2816 case BGP_MSG_CAPABILITY
:
2817 frrtrace(2, frr_bgp
, capability_process
, peer
, size
);
2818 atomic_fetch_add_explicit(&peer
->dynamic_cap_in
, 1,
2819 memory_order_relaxed
);
2820 mprc
= bgp_capability_receive(peer
, size
);
2821 if (mprc
== BGP_Stop
)
2824 "%s: BGP CAPABILITY receipt failed for peer: %s",
2825 __func__
, peer
->host
);
2828 /* Suppress uninitialized variable warning */
2832 * The message type should have been sanitized before
2833 * we ever got here. Receipt of a message with an
2834 * invalid header at this point is indicative of a
2837 assert (!"Message of invalid type received during input processing");
2840 /* delete processed packet */
2841 stream_free(peer
->curr
);
2846 if (mprc
!= BGP_PACKET_NOOP
)
2847 fsm_update_result
= bgp_event_update(peer
, mprc
);
2852 * If peer was deleted, do not process any more packets. This
2853 * is usually due to executing BGP_Stop or a stub deletion.
2855 if (fsm_update_result
== FSM_PEER_TRANSFERRED
2856 || fsm_update_result
== FSM_PEER_STOPPED
)
2860 if (fsm_update_result
!= FSM_PEER_TRANSFERRED
2861 && fsm_update_result
!= FSM_PEER_STOPPED
) {
2862 frr_with_mutex(&peer
->io_mtx
) {
2863 // more work to do, come back later
2864 if (peer
->ibuf
->count
> 0)
2866 bm
->master
, bgp_process_packet
, peer
, 0,
2867 &peer
->t_process_packet
);
2872 /* Send EOR when routes are processed by selection deferral timer */
2873 void bgp_send_delayed_eor(struct bgp
*bgp
)
2876 struct listnode
*node
, *nnode
;
2878 /* EOR message sent in bgp_write_proceed_actions */
2879 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
2880 bgp_write_proceed_actions(peer
);
2884 * Task callback to handle socket error encountered in the io pthread. We avoid
2885 * having the io pthread try to enqueue fsm events or mess with the peer
2888 void bgp_packet_process_error(struct thread
*thread
)
2893 peer
= THREAD_ARG(thread
);
2894 code
= THREAD_VAL(thread
);
2896 if (bgp_debug_neighbor_events(peer
))
2897 zlog_debug("%s [Event] BGP error %d on fd %d",
2898 peer
->host
, code
, peer
->fd
);
2900 /* Closed connection or error on the socket */
2901 if (peer_established(peer
)) {
2902 if ((CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
2903 || CHECK_FLAG(peer
->flags
,
2904 PEER_FLAG_GRACEFUL_RESTART_HELPER
))
2905 && CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)) {
2906 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2907 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2909 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2912 bgp_event_update(peer
, code
);