1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP packet management routine.
3 * Contains utility functions for constructing and consuming BGP messages.
4 * Copyright (C) 2017 Cumulus Networks
5 * Copyright (C) 1999 Kunihiro Ishiguro
18 #include "sockunion.h" /* for inet_ntop () */
24 #include "lib_errors.h"
26 #include "bgpd/bgpd.h"
27 #include "bgpd/bgp_table.h"
28 #include "bgpd/bgp_dump.h"
29 #include "bgpd/bgp_bmp.h"
30 #include "bgpd/bgp_attr.h"
31 #include "bgpd/bgp_debug.h"
32 #include "bgpd/bgp_errors.h"
33 #include "bgpd/bgp_fsm.h"
34 #include "bgpd/bgp_route.h"
35 #include "bgpd/bgp_packet.h"
36 #include "bgpd/bgp_open.h"
37 #include "bgpd/bgp_aspath.h"
38 #include "bgpd/bgp_community.h"
39 #include "bgpd/bgp_ecommunity.h"
40 #include "bgpd/bgp_lcommunity.h"
41 #include "bgpd/bgp_network.h"
42 #include "bgpd/bgp_mplsvpn.h"
43 #include "bgpd/bgp_evpn.h"
44 #include "bgpd/bgp_advertise.h"
45 #include "bgpd/bgp_vty.h"
46 #include "bgpd/bgp_updgrp.h"
47 #include "bgpd/bgp_label.h"
48 #include "bgpd/bgp_io.h"
49 #include "bgpd/bgp_keepalives.h"
50 #include "bgpd/bgp_flowspec.h"
51 #include "bgpd/bgp_trace.h"
53 DEFINE_HOOK(bgp_packet_dump
,
54 (struct peer
*peer
, uint8_t type
, bgp_size_t size
,
56 (peer
, type
, size
, s
));
58 DEFINE_HOOK(bgp_packet_send
,
59 (struct peer
*peer
, uint8_t type
, bgp_size_t size
,
61 (peer
, type
, size
, s
));
64 * Sets marker and type fields for a BGP message.
66 * @param s the stream containing the packet
67 * @param type the packet type
68 * @return the size of the stream
70 int bgp_packet_set_marker(struct stream
*s
, uint8_t type
)
75 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
78 /* Dummy total length. This field is should be filled in later on. */
81 /* BGP packet type. */
84 /* Return current stream size. */
85 return stream_get_endp(s
);
89 * Sets size field for a BGP message.
91 * Size field is set to the size of the stream passed.
93 * @param s the stream containing the packet
95 void bgp_packet_set_size(struct stream
*s
)
99 /* Preserve current pointer. */
100 cp
= stream_get_endp(s
);
101 stream_putw_at(s
, BGP_MARKER_SIZE
, cp
);
105 * Push a packet onto the beginning of the peer's output queue.
106 * This function acquires the peer's write mutex before proceeding.
108 static void bgp_packet_add(struct peer
*peer
, struct stream
*s
)
112 intmax_t sendholdtime
;
114 frr_with_mutex (&peer
->io_mtx
) {
115 /* if the queue is empty, reset the "last OK" timestamp to
116 * now, otherwise if we write another packet immediately
117 * after it'll get confused
119 if (!stream_fifo_count_safe(peer
->obuf
))
120 peer
->last_sendq_ok
= monotime(NULL
);
122 stream_fifo_push(peer
->obuf
, s
);
124 delta
= monotime(NULL
) - peer
->last_sendq_ok
;
126 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
127 holdtime
= atomic_load_explicit(&peer
->holdtime
,
128 memory_order_relaxed
);
130 holdtime
= peer
->bgp
->default_holdtime
;
132 sendholdtime
= holdtime
* 2;
134 /* Note that when we're here, we're adding some packet to the
135 * OutQ. That includes keepalives when there is nothing to
136 * do, so there's a guarantee we pass by here once in a while.
138 * That implies there is no need to go set up another separate
139 * timer that ticks down SendHoldTime, as we'll be here sooner
140 * or later anyway and will see the checks below failing.
143 /* no holdtime, do nothing. */
144 } else if (delta
> sendholdtime
) {
146 EC_BGP_SENDQ_STUCK_PROPER
,
147 "%pBP has not made any SendQ progress for 2 holdtimes (%jds), terminating session",
149 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
150 } else if (delta
> (intmax_t)holdtime
&&
151 monotime(NULL
) - peer
->last_sendq_warn
> 5) {
153 EC_BGP_SENDQ_STUCK_WARN
,
154 "%pBP has not made any SendQ progress for 1 holdtime (%us), peer overloaded?",
156 peer
->last_sendq_warn
= monotime(NULL
);
161 static struct stream
*bgp_update_packet_eor(struct peer
*peer
, afi_t afi
,
165 iana_afi_t pkt_afi
= IANA_AFI_IPV4
;
166 iana_safi_t pkt_safi
= IANA_SAFI_UNICAST
;
168 if (DISABLE_BGP_ANNOUNCE
)
171 if (bgp_debug_neighbor_events(peer
))
172 zlog_debug("send End-of-RIB for %s to %s",
173 get_afi_safi_str(afi
, safi
, false), peer
->host
);
175 s
= stream_new(peer
->max_packet_size
);
177 /* Make BGP update packet. */
178 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
180 /* Unfeasible Routes Length */
183 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
184 /* Total Path Attribute Length */
187 /* Convert AFI, SAFI to values for packet. */
188 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
190 /* Total Path Attribute Length */
192 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
193 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
195 stream_putw(s
, pkt_afi
);
196 stream_putc(s
, pkt_safi
);
199 bgp_packet_set_size(s
);
203 /* Called when there is a change in the EOR(implicit or explicit) status of a
204 * peer. Ends the update-delay if all expected peers are done with EORs. */
205 void bgp_check_update_delay(struct bgp
*bgp
)
207 struct listnode
*node
, *nnode
;
208 struct peer
*peer
= NULL
;
210 if (bgp_debug_neighbor_events(peer
))
211 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
212 bgp
->established
, bgp
->restarted_peers
,
213 bgp
->implicit_eors
, bgp
->explicit_eors
);
216 <= bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
) {
218 * This is an extra sanity check to make sure we wait for all
219 * the eligible configured peers. This check is performed if
220 * establish wait timer is on, or establish wait option is not
221 * given with the update-delay command
223 if (bgp
->t_establish_wait
224 || (bgp
->v_establish_wait
== bgp
->v_update_delay
))
225 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
226 if (CHECK_FLAG(peer
->flags
,
227 PEER_FLAG_CONFIG_NODE
)
228 && !CHECK_FLAG(peer
->flags
,
230 && !CHECK_FLAG(peer
->bgp
->flags
,
232 && !peer
->update_delay_over
) {
233 if (bgp_debug_neighbor_events(peer
))
235 " Peer %s pending, continuing read-only mode",
242 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
243 bgp
->restarted_peers
, bgp
->implicit_eors
,
245 bgp_update_delay_end(bgp
);
250 * Called if peer is known to have restarted. The restart-state bit in
251 * Graceful-Restart capability is used for that
253 void bgp_update_restarted_peers(struct peer
*peer
)
255 if (!bgp_update_delay_active(peer
->bgp
))
256 return; /* BGP update delay has ended */
257 if (peer
->update_delay_over
)
258 return; /* This peer has already been considered */
260 if (bgp_debug_neighbor_events(peer
))
261 zlog_debug("Peer %s: Checking restarted", peer
->host
);
263 if (peer_established(peer
)) {
264 peer
->update_delay_over
= 1;
265 peer
->bgp
->restarted_peers
++;
266 bgp_check_update_delay(peer
->bgp
);
271 * Called as peer receives a keep-alive. Determines if this occurence can be
272 * taken as an implicit EOR for this peer.
273 * NOTE: The very first keep-alive after the Established state of a peer is
274 * considered implicit EOR for the update-delay purposes
276 void bgp_update_implicit_eors(struct peer
*peer
)
278 if (!bgp_update_delay_active(peer
->bgp
))
279 return; /* BGP update delay has ended */
280 if (peer
->update_delay_over
)
281 return; /* This peer has already been considered */
283 if (bgp_debug_neighbor_events(peer
))
284 zlog_debug("Peer %s: Checking implicit EORs", peer
->host
);
286 if (peer_established(peer
)) {
287 peer
->update_delay_over
= 1;
288 peer
->bgp
->implicit_eors
++;
289 bgp_check_update_delay(peer
->bgp
);
294 * Should be called only when there is a change in the EOR_RECEIVED status
295 * for any afi/safi on a peer.
297 static void bgp_update_explicit_eors(struct peer
*peer
)
302 if (!bgp_update_delay_active(peer
->bgp
))
303 return; /* BGP update delay has ended */
304 if (peer
->update_delay_over
)
305 return; /* This peer has already been considered */
307 if (bgp_debug_neighbor_events(peer
))
308 zlog_debug("Peer %s: Checking explicit EORs", peer
->host
);
310 FOREACH_AFI_SAFI (afi
, safi
) {
311 if (peer
->afc_nego
[afi
][safi
]
312 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
313 PEER_STATUS_EOR_RECEIVED
)) {
314 if (bgp_debug_neighbor_events(peer
))
316 " afi %d safi %d didn't receive EOR",
322 peer
->update_delay_over
= 1;
323 peer
->bgp
->explicit_eors
++;
324 bgp_check_update_delay(peer
->bgp
);
328 * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
330 * mp_withdraw, if set, is used to nullify attr structure on most of the
331 * calling safi function and for evpn, passed as parameter
333 int bgp_nlri_parse(struct peer
*peer
, struct attr
*attr
,
334 struct bgp_nlri
*packet
, bool mp_withdraw
)
336 switch (packet
->safi
) {
339 return bgp_nlri_parse_ip(peer
, mp_withdraw
? NULL
: attr
,
341 case SAFI_LABELED_UNICAST
:
342 return bgp_nlri_parse_label(peer
, mp_withdraw
? NULL
: attr
,
345 return bgp_nlri_parse_vpn(peer
, mp_withdraw
? NULL
: attr
,
348 return bgp_nlri_parse_evpn(peer
, attr
, packet
, mp_withdraw
);
350 return bgp_nlri_parse_flowspec(peer
, attr
, packet
, mp_withdraw
);
352 return BGP_NLRI_PARSE_ERROR
;
357 * Check if route-refresh request from peer is pending (received before EoR),
358 * and process it now.
360 static void bgp_process_pending_refresh(struct peer
*peer
, afi_t afi
,
363 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
364 PEER_STATUS_REFRESH_PENDING
)) {
365 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
366 PEER_STATUS_REFRESH_PENDING
);
367 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
368 BGP_ROUTE_REFRESH_BORR
);
369 if (bgp_debug_neighbor_events(peer
))
371 "%pBP sending route-refresh (BoRR) for %s/%s (for pending REQUEST)",
372 peer
, afi2str(afi
), safi2str(safi
));
374 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_BORR_SEND
);
375 UNSET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_EORR_SEND
);
376 bgp_announce_route(peer
, afi
, safi
, true);
381 * Checks a variety of conditions to determine whether the peer needs to be
382 * rescheduled for packet generation again, and does so if necessary.
384 * @param peer to check for rescheduling
386 static void bgp_write_proceed_actions(struct peer
*peer
)
391 struct bpacket
*next_pkt
;
392 struct update_subgroup
*subgrp
;
393 enum bgp_af_index index
;
395 for (index
= BGP_AF_START
; index
< BGP_AF_MAX
; index
++) {
396 paf
= peer
->peer_af_array
[index
];
400 subgrp
= paf
->subgroup
;
404 next_pkt
= paf
->next_pkt_to_send
;
405 if (next_pkt
&& next_pkt
->buffer
) {
406 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
407 bgp_generate_updgrp_packets
, 0);
411 /* No packets readily available for AFI/SAFI, are there
413 * that need to be generated? */
414 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
),
416 || subgroup_packets_to_build(subgrp
)) {
417 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
418 bgp_generate_updgrp_packets
, 0);
425 /* No packets to send, see if EOR is pending */
426 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
427 if (!subgrp
->t_coalesce
&& peer
->afc_nego
[afi
][safi
]
429 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
430 PEER_STATUS_EOR_SEND
)
431 && safi
!= SAFI_MPLS_VPN
) {
432 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
433 bgp_generate_updgrp_packets
, 0);
441 * Generate advertisement information (withdraws, updates, EOR) from each
442 * update group a peer belongs to, encode this information into packets, and
443 * enqueue the packets onto the peer's output buffer.
445 void bgp_generate_updgrp_packets(struct event
*thread
)
447 struct peer
*peer
= EVENT_ARG(thread
);
451 struct bpacket
*next_pkt
;
453 uint32_t generated
= 0;
457 wpq
= atomic_load_explicit(&peer
->bgp
->wpkt_quanta
,
458 memory_order_relaxed
);
461 * The code beyond this part deals with update packets, proceed only
462 * if peer is Established and updates are not on hold (as part of
463 * update-delay processing).
465 if (!peer_established(peer
))
468 if ((peer
->bgp
->main_peers_update_hold
)
469 || bgp_update_delay_active(peer
->bgp
))
472 if (peer
->t_routeadv
)
476 * Since the following is a do while loop
477 * let's stop adding to the outq if we are
478 * already at the limit.
480 if (peer
->obuf
->count
>= bm
->outq_limit
) {
481 bgp_write_proceed_actions(peer
);
486 enum bgp_af_index index
;
489 for (index
= BGP_AF_START
; index
< BGP_AF_MAX
; index
++) {
490 paf
= peer
->peer_af_array
[index
];
491 if (!paf
|| !PAF_SUBGRP(paf
))
496 next_pkt
= paf
->next_pkt_to_send
;
499 * Try to generate a packet for the peer if we are at
500 * the end of the list. Always try to push out
503 if (!next_pkt
|| !next_pkt
->buffer
) {
504 next_pkt
= subgroup_withdraw_packet(
506 if (!next_pkt
|| !next_pkt
->buffer
)
507 subgroup_update_packet(PAF_SUBGRP(paf
));
508 next_pkt
= paf
->next_pkt_to_send
;
512 * If we still don't have a packet to send to the peer,
513 * then try to find out out if we have to send eor or
514 * if not, skip to the next AFI, SAFI. Don't send the
515 * EOR prematurely; if the subgroup's coalesce timer is
516 * running, the adjacency-out structure is not created
519 if (!next_pkt
|| !next_pkt
->buffer
) {
520 if (!paf
->t_announce_route
) {
521 /* Make sure we supress BGP UPDATES
522 * for normal processing later again.
524 UNSET_FLAG(paf
->subgroup
->sflags
,
525 SUBGRP_STATUS_FORCE_UPDATES
);
527 /* If route-refresh BoRR message was
528 * already sent and we are done with
529 * re-announcing tables for a decent
530 * afi/safi, we ready to send
534 peer
->af_sflags
[afi
][safi
],
535 PEER_STATUS_BORR_SEND
)) {
536 bgp_route_refresh_send(
537 peer
, afi
, safi
, 0, 0,
539 BGP_ROUTE_REFRESH_EORR
);
541 SET_FLAG(peer
->af_sflags
[afi
]
543 PEER_STATUS_EORR_SEND
);
547 PEER_STATUS_BORR_SEND
);
549 if (bgp_debug_neighbor_events(
552 "%pBP sending route-refresh (EoRR) for %s/%s",
559 if (CHECK_FLAG(peer
->cap
,
560 PEER_CAP_RESTART_RCV
)) {
561 if (!(PAF_SUBGRP(paf
))->t_coalesce
562 && peer
->afc_nego
[afi
][safi
]
565 peer
->af_sflags
[afi
][safi
],
566 PEER_STATUS_EOR_SEND
)) {
567 /* If EOR is disabled,
568 * the message is not sent
570 if (BGP_SEND_EOR(peer
->bgp
, afi
,
576 PEER_STATUS_EOR_SEND
);
588 bgp_process_pending_refresh(
597 /* Update packet send time */
598 peer
->pkt_stime
[afi
][safi
] = monotime(NULL
);
600 /* Found a packet template to send, overwrite
601 * packet with appropriate attributes from peer
602 * and advance peer */
603 s
= bpacket_reformat_for_peer(next_pkt
, paf
);
604 bgp_packet_add(peer
, s
);
605 bpacket_queue_advance_peer(paf
);
607 } while (s
&& (++generated
< wpq
) &&
608 (peer
->obuf
->count
<= bm
->outq_limit
));
613 bgp_write_proceed_actions(peer
);
617 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
619 void bgp_keepalive_send(struct peer
*peer
)
623 s
= stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
);
625 /* Make keepalive packet. */
626 bgp_packet_set_marker(s
, BGP_MSG_KEEPALIVE
);
628 /* Set packet size. */
629 bgp_packet_set_size(s
);
631 /* Dump packet if debug option is set. */
632 /* bgp_packet_dump (s); */
634 if (bgp_debug_keepalive(peer
))
635 zlog_debug("%s sending KEEPALIVE", peer
->host
);
637 /* Add packet to the peer. */
638 bgp_packet_add(peer
, s
);
644 * Creates a BGP Open packet and appends it to the peer's output queue.
645 * Sets capabilities as necessary.
647 void bgp_open_send(struct peer
*peer
)
650 uint16_t send_holdtime
;
653 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
654 send_holdtime
= peer
->holdtime
;
656 send_holdtime
= peer
->bgp
->default_holdtime
;
658 /* local-as Change */
659 if (peer
->change_local_as
)
660 local_as
= peer
->change_local_as
;
662 local_as
= peer
->local_as
;
664 s
= stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
);
666 /* Make open packet. */
667 bgp_packet_set_marker(s
, BGP_MSG_OPEN
);
669 /* Set open packet values. */
670 stream_putc(s
, BGP_VERSION_4
); /* BGP version */
671 stream_putw(s
, (local_as
<= BGP_AS_MAX
) ? (uint16_t)local_as
673 stream_putw(s
, send_holdtime
); /* Hold Time */
674 stream_put_in_addr(s
, &peer
->local_id
); /* BGP Identifier */
676 /* Set capabilities */
677 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_EXTENDED_OPT_PARAMS
)) {
678 (void)bgp_open_capability(s
, peer
, true);
680 struct stream
*tmp
= stream_new(STREAM_SIZE(s
));
683 if (bgp_open_capability(tmp
, peer
, false)
684 > BGP_OPEN_NON_EXT_OPT_LEN
) {
686 (void)bgp_open_capability(s
, peer
, true);
693 /* Set BGP packet length. */
694 bgp_packet_set_size(s
);
696 if (bgp_debug_neighbor_events(peer
))
698 "%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4",
699 peer
->host
, BGP_VERSION_4
, local_as
, send_holdtime
,
702 /* Dump packet if debug option is set. */
703 /* bgp_packet_dump (s); */
704 hook_call(bgp_packet_send
, peer
, BGP_MSG_OPEN
, stream_get_endp(s
), s
);
706 /* Add packet to the peer. */
707 bgp_packet_add(peer
, s
);
713 * Writes NOTIFICATION message directly to a peer socket without waiting for
716 * There must be exactly one stream on the peer->obuf FIFO, and the data within
717 * this stream must match the format of a BGP NOTIFICATION message.
718 * Transmission is best-effort.
720 * @requires peer->io_mtx
724 static void bgp_write_notify(struct peer
*peer
)
730 /* There should be at least one packet. */
731 s
= stream_fifo_pop(peer
->obuf
);
736 assert(stream_get_endp(s
) >= BGP_HEADER_SIZE
);
739 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
740 * we only care about getting a clean shutdown at this point.
742 ret
= write(peer
->fd
, STREAM_DATA(s
), stream_get_endp(s
));
745 * only connection reset/close gets counted as TCP_fatal_error, failure
746 * to write the entire NOTIFY doesn't get different FSM treatment
750 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
754 /* Disable Nagle, make NOTIFY packet go out right away */
756 (void)setsockopt(peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
,
759 /* Retrieve BGP packet type. */
760 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
761 type
= stream_getc(s
);
763 assert(type
== BGP_MSG_NOTIFY
);
765 /* Type should be notify. */
766 atomic_fetch_add_explicit(&peer
->notify_out
, 1, memory_order_relaxed
);
768 /* Double start timer. */
771 /* Overflow check. */
772 if (peer
->v_start
>= (60 * 2))
773 peer
->v_start
= (60 * 2);
776 * Handle Graceful Restart case where the state changes to
777 * Connect instead of Idle
779 BGP_EVENT_ADD(peer
, BGP_Stop
);
785 * Encapsulate an original BGP CEASE Notification into Hard Reset
787 static uint8_t *bgp_notify_encapsulate_hard_reset(uint8_t code
, uint8_t subcode
,
788 uint8_t *data
, size_t datalen
)
790 uint8_t *message
= XCALLOC(MTYPE_BGP_NOTIFICATION
, datalen
+ 2);
795 message
[1] = subcode
;
798 memcpy(message
+ 2, data
, datalen
);
804 * Decapsulate an original BGP CEASE Notification from Hard Reset
806 struct bgp_notify
bgp_notify_decapsulate_hard_reset(struct bgp_notify
*notify
)
808 struct bgp_notify bn
= {};
810 bn
.code
= notify
->raw_data
[0];
811 bn
.subcode
= notify
->raw_data
[1];
812 bn
.length
= notify
->length
- 2;
814 bn
.raw_data
= XMALLOC(MTYPE_BGP_NOTIFICATION
, bn
.length
);
815 memcpy(bn
.raw_data
, notify
->raw_data
+ 2, bn
.length
);
820 /* Check if Graceful-Restart N-bit is exchanged */
821 bool bgp_has_graceful_restart_notification(struct peer
*peer
)
823 return CHECK_FLAG(peer
->cap
, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV
) &&
824 CHECK_FLAG(peer
->cap
, PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV
);
828 * Check if to send BGP CEASE Notification/Hard Reset?
830 bool bgp_notify_send_hard_reset(struct peer
*peer
, uint8_t code
,
833 /* When the "N" bit has been exchanged, a Hard Reset message is used to
834 * indicate to the peer that the session is to be fully terminated.
836 if (!bgp_has_graceful_restart_notification(peer
))
840 * https://datatracker.ietf.org/doc/html/rfc8538#section-5.1
842 if (code
== BGP_NOTIFY_CEASE
) {
844 case BGP_NOTIFY_CEASE_MAX_PREFIX
:
845 case BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
:
846 case BGP_NOTIFY_CEASE_PEER_UNCONFIG
:
847 case BGP_NOTIFY_CEASE_HARD_RESET
:
848 case BGP_NOTIFY_CEASE_BFD_DOWN
:
850 case BGP_NOTIFY_CEASE_ADMIN_RESET
:
851 /* Provide user control:
852 * `bgp hard-adminstrative-reset`
854 if (CHECK_FLAG(peer
->bgp
->flags
,
855 BGP_FLAG_HARD_ADMIN_RESET
))
868 * Check if received BGP CEASE Notification/Hard Reset?
870 bool bgp_notify_received_hard_reset(struct peer
*peer
, uint8_t code
,
873 /* When the "N" bit has been exchanged, a Hard Reset message is used to
874 * indicate to the peer that the session is to be fully terminated.
876 if (!bgp_has_graceful_restart_notification(peer
))
879 if (code
== BGP_NOTIFY_CEASE
&& subcode
== BGP_NOTIFY_CEASE_HARD_RESET
)
886 * Creates a BGP Notify and appends it to the peer's output queue.
888 * This function attempts to write the packet from the thread it is called
889 * from, to ensure the packet gets out ASAP.
891 * This function may be called from multiple threads. Since the function
892 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
893 * call to prevent tampering from other threads.
895 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
896 * return, the peer structure *must* be reset; no assumptions about session
900 * @param code BGP error code
901 * @param sub_code BGP error subcode
902 * @param data Data portion
903 * @param datalen length of data portion
905 static void bgp_notify_send_internal(struct peer
*peer
, uint8_t code
,
906 uint8_t sub_code
, uint8_t *data
,
907 size_t datalen
, bool use_curr
)
910 bool hard_reset
= bgp_notify_send_hard_reset(peer
, code
, sub_code
);
912 /* Lock I/O mutex to prevent other threads from pushing packets */
913 frr_mutex_lock_autounlock(&peer
->io_mtx
);
914 /* ============================================== */
916 /* Allocate new stream. */
917 s
= stream_new(peer
->max_packet_size
);
919 /* Make notify packet. */
920 bgp_packet_set_marker(s
, BGP_MSG_NOTIFY
);
922 /* Check if we should send Hard Reset Notification or not */
924 uint8_t *hard_reset_message
= bgp_notify_encapsulate_hard_reset(
925 code
, sub_code
, data
, datalen
);
927 /* Hard Reset encapsulates another NOTIFICATION message
928 * in its data portion.
930 stream_putc(s
, BGP_NOTIFY_CEASE
);
931 stream_putc(s
, BGP_NOTIFY_CEASE_HARD_RESET
);
932 stream_write(s
, hard_reset_message
, datalen
+ 2);
934 XFREE(MTYPE_BGP_NOTIFICATION
, hard_reset_message
);
936 stream_putc(s
, code
);
937 stream_putc(s
, sub_code
);
939 stream_write(s
, data
, datalen
);
942 /* Set BGP packet length. */
943 bgp_packet_set_size(s
);
945 /* wipe output buffer */
946 stream_fifo_clean(peer
->obuf
);
949 * If possible, store last packet for debugging purposes. This check is
950 * in place because we are sometimes called with a doppelganger peer,
951 * who tends to have a plethora of fields nulled out.
953 * Some callers should not attempt this - the io pthread for example
954 * should not touch internals of the peer struct.
956 if (use_curr
&& peer
->curr
) {
957 size_t packetsize
= stream_get_endp(peer
->curr
);
958 assert(packetsize
<= peer
->max_packet_size
);
959 memcpy(peer
->last_reset_cause
, peer
->curr
->data
, packetsize
);
960 peer
->last_reset_cause_size
= packetsize
;
965 struct bgp_notify bgp_notify
;
970 bgp_notify
.code
= code
;
971 bgp_notify
.subcode
= sub_code
;
972 bgp_notify
.data
= NULL
;
973 bgp_notify
.length
= datalen
;
974 bgp_notify
.raw_data
= data
;
976 peer
->notify
.code
= bgp_notify
.code
;
977 peer
->notify
.subcode
= bgp_notify
.subcode
;
978 peer
->notify
.length
= bgp_notify
.length
;
980 if (bgp_notify
.length
&& data
) {
981 bgp_notify
.data
= XMALLOC(MTYPE_BGP_NOTIFICATION
,
982 bgp_notify
.length
* 3);
983 for (i
= 0; i
< bgp_notify
.length
; i
++)
985 snprintf(c
, sizeof(c
), " %02x",
988 strlcat(bgp_notify
.data
, c
,
993 snprintf(c
, sizeof(c
), "%02x", data
[i
]);
995 strlcpy(bgp_notify
.data
, c
,
999 bgp_notify_print(peer
, &bgp_notify
, "sending", hard_reset
);
1001 if (bgp_notify
.data
) {
1003 XFREE(MTYPE_BGP_NOTIFICATION
,
1005 peer
->notify
.data
= XCALLOC(
1006 MTYPE_BGP_NOTIFICATION
, datalen
);
1007 memcpy(peer
->notify
.data
, data
, datalen
);
1010 XFREE(MTYPE_BGP_NOTIFICATION
, bgp_notify
.data
);
1011 bgp_notify
.length
= 0;
1015 /* peer reset cause */
1016 if (code
== BGP_NOTIFY_CEASE
) {
1017 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
1018 peer
->last_reset
= PEER_DOWN_USER_RESET
;
1019 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
) {
1020 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_RTT_SHUTDOWN
))
1021 peer
->last_reset
= PEER_DOWN_RTT_SHUTDOWN
;
1023 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
1025 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
1027 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
1029 /* Add packet to peer's output queue */
1030 stream_fifo_push(peer
->obuf
, s
);
1032 bgp_peer_gr_flags_update(peer
);
1033 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
1036 bgp_write_notify(peer
);
1040 * Creates a BGP Notify and appends it to the peer's output queue.
1042 * This function attempts to write the packet from the thread it is called
1043 * from, to ensure the packet gets out ASAP.
1046 * @param code BGP error code
1047 * @param sub_code BGP error subcode
1049 void bgp_notify_send(struct peer
*peer
, uint8_t code
, uint8_t sub_code
)
1051 bgp_notify_send_internal(peer
, code
, sub_code
, NULL
, 0, true);
1055 * Enqueue notification; called from the main pthread, peer object access is ok.
1057 void bgp_notify_send_with_data(struct peer
*peer
, uint8_t code
,
1058 uint8_t sub_code
, uint8_t *data
, size_t datalen
)
1060 bgp_notify_send_internal(peer
, code
, sub_code
, data
, datalen
, true);
1064 * For use by the io pthread, queueing a notification but avoiding access to
1067 void bgp_notify_io_invalid(struct peer
*peer
, uint8_t code
, uint8_t sub_code
,
1068 uint8_t *data
, size_t datalen
)
1070 /* Avoid touching the peer object */
1071 bgp_notify_send_internal(peer
, code
, sub_code
, data
, datalen
, false);
1075 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
1078 * @param afi Address Family Identifier
1079 * @param safi Subsequent Address Family Identifier
1080 * @param orf_type Outbound Route Filtering type
1081 * @param when_to_refresh Whether to refresh immediately or defer
1082 * @param remove Whether to remove ORF for specified AFI/SAFI
1084 void bgp_route_refresh_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
1085 uint8_t orf_type
, uint8_t when_to_refresh
,
1086 int remove
, uint8_t subtype
)
1089 struct bgp_filter
*filter
;
1090 int orf_refresh
= 0;
1091 iana_afi_t pkt_afi
= IANA_AFI_IPV4
;
1092 iana_safi_t pkt_safi
= IANA_SAFI_UNICAST
;
1094 if (DISABLE_BGP_ANNOUNCE
)
1097 filter
= &peer
->filter
[afi
][safi
];
1099 /* Convert AFI, SAFI to values for packet. */
1100 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
1102 s
= stream_new(peer
->max_packet_size
);
1104 /* Make BGP update packet. */
1105 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
1106 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_NEW
);
1108 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_OLD
);
1110 /* Encode Route Refresh message. */
1111 stream_putw(s
, pkt_afi
);
1113 stream_putc(s
, subtype
);
1116 stream_putc(s
, pkt_safi
);
1118 if (orf_type
== ORF_TYPE_PREFIX
|| orf_type
== ORF_TYPE_PREFIX_OLD
)
1119 if (remove
|| filter
->plist
[FILTER_IN
].plist
) {
1124 stream_putc(s
, when_to_refresh
);
1125 stream_putc(s
, orf_type
);
1126 orfp
= stream_get_endp(s
);
1130 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
1131 PEER_STATUS_ORF_PREFIX_SEND
);
1132 stream_putc(s
, ORF_COMMON_PART_REMOVE_ALL
);
1133 if (bgp_debug_neighbor_events(peer
))
1135 "%pBP sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s",
1141 iana_afi2str(pkt_afi
),
1142 iana_safi2str(pkt_safi
));
1144 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1145 PEER_STATUS_ORF_PREFIX_SEND
);
1146 prefix_bgp_orf_entry(
1147 s
, filter
->plist
[FILTER_IN
].plist
,
1148 ORF_COMMON_PART_ADD
,
1149 ORF_COMMON_PART_PERMIT
,
1150 ORF_COMMON_PART_DENY
);
1151 if (bgp_debug_neighbor_events(peer
))
1153 "%pBP sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s",
1159 iana_afi2str(pkt_afi
),
1160 iana_safi2str(pkt_safi
));
1163 /* Total ORF Entry Len. */
1164 orf_len
= stream_get_endp(s
) - orfp
- 2;
1165 stream_putw_at(s
, orfp
, orf_len
);
1168 /* Set packet size. */
1169 bgp_packet_set_size(s
);
1171 if (bgp_debug_neighbor_events(peer
)) {
1174 "%pBP sending REFRESH_REQ for afi/safi: %s/%s",
1175 peer
, iana_afi2str(pkt_afi
),
1176 iana_safi2str(pkt_safi
));
1179 /* Add packet to the peer. */
1180 bgp_packet_add(peer
, s
);
1182 bgp_writes_on(peer
);
1186 * Create a BGP Capability packet and append it to the peer's output queue.
1189 * @param afi Address Family Identifier
1190 * @param safi Subsequent Address Family Identifier
1191 * @param capability_code BGP Capability Code
1192 * @param action Set or Remove capability
1194 void bgp_capability_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
1195 int capability_code
, int action
)
1198 iana_afi_t pkt_afi
= IANA_AFI_IPV4
;
1199 iana_safi_t pkt_safi
= IANA_SAFI_UNICAST
;
1201 /* Convert AFI, SAFI to values for packet. */
1202 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
1204 s
= stream_new(peer
->max_packet_size
);
1206 /* Make BGP update packet. */
1207 bgp_packet_set_marker(s
, BGP_MSG_CAPABILITY
);
1209 /* Encode MP_EXT capability. */
1210 if (capability_code
== CAPABILITY_CODE_MP
) {
1211 stream_putc(s
, action
);
1212 stream_putc(s
, CAPABILITY_CODE_MP
);
1213 stream_putc(s
, CAPABILITY_CODE_MP_LEN
);
1214 stream_putw(s
, pkt_afi
);
1216 stream_putc(s
, pkt_safi
);
1218 if (bgp_debug_neighbor_events(peer
))
1220 "%pBP sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
1222 action
== CAPABILITY_ACTION_SET
? "Advertising"
1224 iana_afi2str(pkt_afi
), iana_safi2str(pkt_safi
));
1227 /* Set packet size. */
1228 bgp_packet_set_size(s
);
1230 /* Add packet to the peer. */
1231 bgp_packet_add(peer
, s
);
1233 bgp_writes_on(peer
);
1236 /* RFC1771 6.8 Connection collision detection. */
1237 static int bgp_collision_detect(struct peer
*new, struct in_addr remote_id
)
1242 * Upon receipt of an OPEN message, the local system must examine
1243 * all of its connections that are in the OpenConfirm state. A BGP
1244 * speaker may also examine connections in an OpenSent state if it
1245 * knows the BGP Identifier of the peer by means outside of the
1246 * protocol. If among these connections there is a connection to a
1247 * remote BGP speaker whose BGP Identifier equals the one in the
1248 * OPEN message, then the local system performs the following
1249 * collision resolution procedure:
1251 peer
= new->doppelganger
;
1256 * Do not accept the new connection in Established or Clearing
1257 * states. Note that a peer GR is handled by closing the existing
1258 * connection upon receipt of new one.
1260 if (peer_established(peer
) || peer
->status
== Clearing
) {
1261 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1262 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1266 if ((peer
->status
!= OpenConfirm
) && (peer
->status
!= OpenSent
))
1270 * 1. The BGP Identifier of the local system is
1271 * compared to the BGP Identifier of the remote
1272 * system (as specified in the OPEN message).
1274 * If the BGP Identifiers of the peers
1275 * involved in the connection collision
1276 * are identical, then the connection
1277 * initiated by the BGP speaker with the
1278 * larger AS number is preserved.
1280 if (ntohl(peer
->local_id
.s_addr
) < ntohl(remote_id
.s_addr
)
1281 || (ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)
1282 && peer
->local_as
< peer
->as
))
1283 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
1285 * 2. If the value of the local BGP
1286 * Identifier is less than the remote one,
1287 * the local system closes BGP connection
1288 * that already exists (the one that is
1289 * already in the OpenConfirm state),
1290 * and accepts BGP connection initiated by
1291 * the remote system.
1293 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1294 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1297 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1298 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1302 if (ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)
1303 && peer
->local_as
== peer
->as
)
1304 flog_err(EC_BGP_ROUTER_ID_SAME
,
1305 "Peer's router-id %pI4 is the same as ours",
1309 * 3. Otherwise, the local system closes newly
1310 * created BGP connection (the one associated with the
1311 * newly received OPEN message), and continues to use
1312 * the existing one (the one that is already in the
1313 * OpenConfirm state).
1315 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
1316 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1317 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1320 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1321 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1327 /* Packet processing routines ---------------------------------------------- */
1329 * This is a family of functions designed to be called from
1330 * bgp_process_packet(). These functions all share similar behavior and should
1331 * adhere to the following invariants and restrictions:
1335 * The return code of any one of those functions should be one of the FSM event
1336 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1337 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1338 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1339 * error and Receive_OPEN_message otherwise.
1341 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1346 * - May send NOTIFY messages
1347 * - May not modify peer->status
1348 * - May not call bgp_event_update()
1351 #define BGP_PACKET_NOOP 0
1354 * Process BGP OPEN message for peer.
1356 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1357 * and returns BGP_Stop.
1360 * @param size size of the packet
1361 * @return as in summary
1363 static int bgp_open_receive(struct peer
*peer
, bgp_size_t size
)
1369 uint16_t send_holdtime
;
1371 as_t as4
= 0, as4_be
;
1372 struct in_addr remote_id
;
1374 uint8_t notify_data_remote_as
[2];
1375 uint8_t notify_data_remote_as4
[4];
1376 uint8_t notify_data_remote_id
[4];
1377 uint16_t *holdtime_ptr
;
1379 /* Parse open packet. */
1380 version
= stream_getc(peer
->curr
);
1381 memcpy(notify_data_remote_as
, stream_pnt(peer
->curr
), 2);
1382 remote_as
= stream_getw(peer
->curr
);
1383 holdtime_ptr
= (uint16_t *)stream_pnt(peer
->curr
);
1384 holdtime
= stream_getw(peer
->curr
);
1385 memcpy(notify_data_remote_id
, stream_pnt(peer
->curr
), 4);
1386 remote_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1388 /* BEGIN to read the capability here, but dont do it yet */
1390 optlen
= stream_getc(peer
->curr
);
1392 /* Extended Optional Parameters Length for BGP OPEN Message */
1393 if (optlen
== BGP_OPEN_NON_EXT_OPT_LEN
1394 || CHECK_FLAG(peer
->flags
, PEER_FLAG_EXTENDED_OPT_PARAMS
)) {
1397 if (STREAM_READABLE(peer
->curr
) < 1) {
1400 "%s: stream does not have enough bytes for extended optional parameters",
1402 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1403 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1407 opttype
= stream_getc(peer
->curr
);
1408 if (opttype
== BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH
) {
1409 if (STREAM_READABLE(peer
->curr
) < 2) {
1412 "%s: stream does not have enough bytes to read the extended optional parameters optlen",
1414 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1415 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1418 optlen
= stream_getw(peer
->curr
);
1419 SET_FLAG(peer
->sflags
,
1420 PEER_STATUS_EXT_OPT_PARAMS_LENGTH
);
1424 /* Receive OPEN message log */
1425 if (bgp_debug_neighbor_events(peer
))
1427 "%s rcv OPEN%s, version %d, remote-as (in open) %u, holdtime %d, id %pI4",
1429 CHECK_FLAG(peer
->sflags
,
1430 PEER_STATUS_EXT_OPT_PARAMS_LENGTH
)
1433 version
, remote_as
, holdtime
, &remote_id
);
1436 /* If not enough bytes, it is an error. */
1437 if (STREAM_READABLE(peer
->curr
) < optlen
) {
1438 flog_err(EC_BGP_PKT_OPEN
,
1439 "%s: stream has not enough bytes (%u)",
1440 peer
->host
, optlen
);
1441 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1442 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1446 /* We need the as4 capability value *right now* because
1447 * if it is there, we have not got the remote_as yet, and
1449 * that we do not know which peer is connecting to us now.
1451 as4
= peek_for_as4_capability(peer
, optlen
);
1454 as4_be
= htonl(as4
);
1455 memcpy(notify_data_remote_as4
, &as4_be
, 4);
1457 /* Just in case we have a silly peer who sends AS4 capability set to 0
1459 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
) {
1460 flog_err(EC_BGP_PKT_OPEN
,
1461 "%s bad OPEN, got AS4 capability, but AS4 set to 0",
1463 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1464 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1465 notify_data_remote_as4
, 4);
1469 /* Codification of AS 0 Processing */
1470 if (remote_as
== BGP_AS_ZERO
) {
1471 flog_err(EC_BGP_PKT_OPEN
, "%s bad OPEN, got AS set to 0",
1473 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1474 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1478 if (remote_as
== BGP_AS_TRANS
) {
1479 /* Take the AS4 from the capability. We must have received the
1480 * capability now! Otherwise we have a asn16 peer who uses
1481 * BGP_AS_TRANS, for some unknown reason.
1483 if (as4
== BGP_AS_TRANS
) {
1486 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1488 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1489 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1490 notify_data_remote_as4
, 4);
1494 if (!as4
&& BGP_DEBUG(as4
, AS4
))
1496 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.",
1498 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG(as4
, AS4
))
1500 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.",
1505 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1507 /* If we have got the capability, peer->as4cap must match
1509 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)
1510 && as4
!= remote_as
) {
1511 /* raise error, log this, close session */
1514 "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
1515 peer
->host
, as4
, remote_as
);
1516 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1517 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1518 notify_data_remote_as4
, 4);
1524 * If the BGP Identifier field of the OPEN message
1525 * is zero, or if it is the same as the BGP Identifier
1526 * of the local BGP speaker and the message is from an
1527 * internal peer, then the Error Subcode is set to
1528 * "Bad BGP Identifier".
1530 if (remote_id
.s_addr
== INADDR_ANY
1531 || (peer
->sort
== BGP_PEER_IBGP
1532 && ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
))) {
1533 if (bgp_debug_neighbor_events(peer
))
1534 zlog_debug("%s bad OPEN, wrong router identifier %pI4",
1535 peer
->host
, &remote_id
);
1536 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1537 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1538 notify_data_remote_id
, 4);
1542 /* Peer BGP version check. */
1543 if (version
!= BGP_VERSION_4
) {
1544 uint16_t maxver
= htons(BGP_VERSION_4
);
1545 /* XXX this reply may not be correct if version < 4 XXX */
1546 if (bgp_debug_neighbor_events(peer
))
1548 "%s bad protocol version, remote requested %d, local request %d",
1549 peer
->host
, version
, BGP_VERSION_4
);
1550 /* Data must be in network byte order here */
1551 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1552 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1553 (uint8_t *)&maxver
, 2);
1557 /* Check neighbor as number. */
1558 if (peer
->as_type
== AS_UNSPECIFIED
) {
1559 if (bgp_debug_neighbor_events(peer
))
1561 "%s bad OPEN, remote AS is unspecified currently",
1563 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1564 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1565 notify_data_remote_as
, 2);
1567 } else if (peer
->as_type
== AS_INTERNAL
) {
1568 if (remote_as
!= peer
->bgp
->as
) {
1569 if (bgp_debug_neighbor_events(peer
))
1571 "%s bad OPEN, remote AS is %u, internal specified",
1572 peer
->host
, remote_as
);
1573 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1574 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1575 notify_data_remote_as
, 2);
1578 peer
->as
= peer
->local_as
;
1579 } else if (peer
->as_type
== AS_EXTERNAL
) {
1580 if (remote_as
== peer
->bgp
->as
) {
1581 if (bgp_debug_neighbor_events(peer
))
1583 "%s bad OPEN, remote AS is %u, external specified",
1584 peer
->host
, remote_as
);
1585 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1586 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1587 notify_data_remote_as
, 2);
1590 peer
->as
= remote_as
;
1591 } else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
)) {
1592 if (bgp_debug_neighbor_events(peer
))
1593 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1594 peer
->host
, remote_as
, peer
->as
);
1595 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1596 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1597 notify_data_remote_as
, 2);
1602 * When collision is detected and this peer is closed.
1603 * Return immediately.
1605 ret
= bgp_collision_detect(peer
, remote_id
);
1610 if (bgp_getsockname(peer
) < 0) {
1611 flog_err_sys(EC_LIB_SOCKET
,
1612 "%s: bgp_getsockname() failed for peer: %s",
1613 __func__
, peer
->host
);
1617 /* Set remote router-id */
1618 peer
->remote_id
= remote_id
;
1620 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1621 calculate the value of the Hold Timer by using the smaller of its
1622 configured Hold Time and the Hold Time received in the OPEN message.
1623 The Hold Time MUST be either zero or at least three seconds. An
1624 implementation may reject connections on the basis of the Hold Time.
1627 if (holdtime
< 3 && holdtime
!= 0) {
1628 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1629 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1630 (uint8_t *)holdtime_ptr
, 2);
1634 /* Send notification message when Hold Time received in the OPEN message
1635 * is smaller than configured minimum Hold Time. */
1636 if (holdtime
< peer
->bgp
->default_min_holdtime
1637 && peer
->bgp
->default_min_holdtime
!= 0) {
1638 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1639 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1640 (uint8_t *)holdtime_ptr
, 2);
1644 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1645 would be one third of the Hold Time interval. KEEPALIVE messages
1646 MUST NOT be sent more frequently than one per second. An
1647 implementation MAY adjust the rate at which it sends KEEPALIVE
1648 messages as a function of the Hold Time interval. */
1650 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
1651 send_holdtime
= peer
->holdtime
;
1653 send_holdtime
= peer
->bgp
->default_holdtime
;
1655 if (holdtime
< send_holdtime
)
1656 peer
->v_holdtime
= holdtime
;
1658 peer
->v_holdtime
= send_holdtime
;
1660 /* Set effective keepalive to 1/3 the effective holdtime.
1661 * Use configured keeplive when < effective keepalive.
1663 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1664 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1665 if (peer
->keepalive
&& peer
->keepalive
< peer
->v_keepalive
)
1666 peer
->v_keepalive
= peer
->keepalive
;
1668 if (peer
->bgp
->default_keepalive
1669 && peer
->bgp
->default_keepalive
< peer
->v_keepalive
)
1670 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1673 /* If another side disabled sending Software Version capability,
1674 * we MUST drop the previous from showing in the outputs to avoid
1675 * stale information and due to security reasons.
1677 if (peer
->soft_version
)
1678 XFREE(MTYPE_BGP_SOFT_VERSION
, peer
->soft_version
);
1680 /* Open option part parse. */
1682 if (bgp_open_option_parse(peer
, optlen
, &mp_capability
) < 0)
1685 if (bgp_debug_neighbor_events(peer
))
1686 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1691 * Assume that the peer supports the locally configured set of
1692 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1693 * capabilities, or if 'override-capability' is configured.
1696 || CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
1697 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] =
1698 peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1699 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] =
1700 peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1701 peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
] =
1702 peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
];
1703 peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
] =
1704 peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
];
1705 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] =
1706 peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1707 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] =
1708 peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1709 peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
] =
1710 peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
];
1711 peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
] =
1712 peer
->afc
[AFI_L2VPN
][SAFI_EVPN
];
1713 peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
] =
1714 peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
];
1717 /* Verify valid local address present based on negotiated
1718 * address-families. */
1719 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
1720 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
1721 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
1722 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
1723 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]) {
1724 if (peer
->nexthop
.v4
.s_addr
== INADDR_ANY
) {
1725 #if defined(HAVE_CUMULUS)
1726 zlog_warn("%s: No local IPv4 addr, BGP routing may not work",
1731 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
1732 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
1733 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
1734 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
1735 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]) {
1736 if (IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_global
)) {
1737 #if defined(HAVE_CUMULUS)
1738 zlog_warn("%s: No local IPv6 address, BGP routing may not work",
1743 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1745 return Receive_OPEN_message
;
1749 * Process BGP KEEPALIVE message for peer.
1752 * @param size size of the packet
1753 * @return as in summary
1755 static int bgp_keepalive_receive(struct peer
*peer
, bgp_size_t size
)
1757 if (bgp_debug_keepalive(peer
))
1758 zlog_debug("%s KEEPALIVE rcvd", peer
->host
);
1760 bgp_update_implicit_eors(peer
);
1762 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1764 /* If the peer's RTT is higher than expected, shutdown
1765 * the peer automatically.
1767 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_RTT_SHUTDOWN
))
1768 return Receive_KEEPALIVE_message
;
1770 if (peer
->rtt
> peer
->rtt_expected
) {
1771 peer
->rtt_keepalive_rcv
++;
1773 if (peer
->rtt_keepalive_rcv
> peer
->rtt_keepalive_conf
) {
1774 char rtt_shutdown_reason
[BUFSIZ
] = {};
1777 rtt_shutdown_reason
,
1778 sizeof(rtt_shutdown_reason
),
1779 "shutdown due to high round-trip-time (%dms > %dms, hit %u times)",
1780 peer
->rtt
, peer
->rtt_expected
,
1781 peer
->rtt_keepalive_rcv
);
1782 zlog_warn("%s %s", peer
->host
, rtt_shutdown_reason
);
1783 SET_FLAG(peer
->sflags
, PEER_STATUS_RTT_SHUTDOWN
);
1784 peer_tx_shutdown_message_set(peer
, rtt_shutdown_reason
);
1785 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1788 if (peer
->rtt_keepalive_rcv
)
1789 peer
->rtt_keepalive_rcv
--;
1792 return Receive_KEEPALIVE_message
;
1795 static void bgp_refresh_stalepath_timer_expire(struct event
*thread
)
1797 struct peer_af
*paf
;
1799 paf
= EVENT_ARG(thread
);
1801 afi_t afi
= paf
->afi
;
1802 safi_t safi
= paf
->safi
;
1803 struct peer
*peer
= paf
->peer
;
1805 peer
->t_refresh_stalepath
= NULL
;
1807 if (peer
->nsf
[afi
][safi
])
1808 bgp_clear_stale_route(peer
, afi
, safi
);
1810 if (bgp_debug_neighbor_events(peer
))
1812 "%pBP route-refresh (BoRR) timer expired for afi/safi: %d/%d",
1815 bgp_timer_set(peer
);
1819 * Process BGP UPDATE message for peer.
1821 * Parses UPDATE and creates attribute object.
1824 * @param size size of the packet
1825 * @return as in summary
1827 static int bgp_update_receive(struct peer
*peer
, bgp_size_t size
)
1833 bgp_size_t attribute_len
;
1834 bgp_size_t update_len
;
1835 bgp_size_t withdraw_len
;
1836 bool restart
= false;
1845 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1847 /* Status must be Established. */
1848 if (!peer_established(peer
)) {
1849 flog_err(EC_BGP_INVALID_STATUS
,
1850 "%s [FSM] Update packet received under status %s",
1852 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1853 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1854 bgp_fsm_error_subcode(peer
->status
));
1858 /* Set initial values. */
1859 memset(&attr
, 0, sizeof(attr
));
1860 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
1861 attr
.label
= MPLS_INVALID_LABEL
;
1862 memset(&nlris
, 0, sizeof(nlris
));
1863 memset(peer
->rcvd_attr_str
, 0, BUFSIZ
);
1864 peer
->rcvd_attr_printed
= 0;
1867 end
= stream_pnt(s
) + size
;
1869 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1870 Length is too large (i.e., if Unfeasible Routes Length + Total
1871 Attribute Length + 23 exceeds the message Length), then the Error
1872 Subcode is set to Malformed Attribute List. */
1873 if (stream_pnt(s
) + 2 > end
) {
1874 flog_err(EC_BGP_UPDATE_RCV
,
1875 "%s [Error] Update packet error (packet length is short for unfeasible length)",
1877 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1878 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1882 /* Unfeasible Route Length. */
1883 withdraw_len
= stream_getw(s
);
1885 /* Unfeasible Route Length check. */
1886 if (stream_pnt(s
) + withdraw_len
> end
) {
1887 flog_err(EC_BGP_UPDATE_RCV
,
1888 "%s [Error] Update packet error (packet unfeasible length overflow %d)",
1889 peer
->host
, withdraw_len
);
1890 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1891 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1895 /* Unfeasible Route packet format check. */
1896 if (withdraw_len
> 0) {
1897 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1898 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1899 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt(s
);
1900 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1901 stream_forward_getp(s
, withdraw_len
);
1904 /* Attribute total length check. */
1905 if (stream_pnt(s
) + 2 > end
) {
1907 EC_BGP_UPDATE_PACKET_SHORT
,
1908 "%s [Error] Packet Error (update packet is short for attribute length)",
1910 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1911 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1915 /* Fetch attribute total length. */
1916 attribute_len
= stream_getw(s
);
1918 /* Attribute length check. */
1919 if (stream_pnt(s
) + attribute_len
> end
) {
1921 EC_BGP_UPDATE_PACKET_LONG
,
1922 "%s [Error] Packet Error (update packet attribute length overflow %d)",
1923 peer
->host
, attribute_len
);
1924 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1925 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1929 /* Certain attribute parsing errors should not be considered bad enough
1930 * to reset the session for, most particularly any partial/optional
1931 * attributes that have 'tunneled' over speakers that don't understand
1932 * them. Instead we withdraw only the prefix concerned.
1934 * Complicates the flow a little though..
1936 enum bgp_attr_parse_ret attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1937 /* This define morphs the update case into a withdraw when lower levels
1938 * have signalled an error condition where this is best.
1940 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1942 /* Parse attribute when it exists. */
1943 if (attribute_len
) {
1944 attr_parse_ret
= bgp_attr_parse(peer
, &attr
, attribute_len
,
1945 &nlris
[NLRI_MP_UPDATE
],
1946 &nlris
[NLRI_MP_WITHDRAW
]);
1947 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
) {
1948 bgp_attr_unintern_sub(&attr
);
1953 /* Logging the attribute. */
1954 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1955 || BGP_DEBUG(update
, UPDATE_IN
)
1956 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
1957 ret
= bgp_dump_attr(&attr
, peer
->rcvd_attr_str
,
1958 sizeof(peer
->rcvd_attr_str
));
1960 peer
->stat_upd_7606
++;
1962 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1965 "%pBP rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1968 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1)) {
1969 zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer
,
1970 peer
->rcvd_attr_str
);
1971 peer
->rcvd_attr_printed
= 1;
1975 /* Network Layer Reachability Information. */
1976 update_len
= end
- stream_pnt(s
);
1979 /* Set NLRI portion to structure. */
1980 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1981 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1982 nlris
[NLRI_UPDATE
].nlri
= stream_pnt(s
);
1983 nlris
[NLRI_UPDATE
].length
= update_len
;
1984 stream_forward_getp(s
, update_len
);
1986 if (CHECK_FLAG(attr
.flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
))) {
1988 * We skipped nexthop attribute validation earlier so
1989 * validate the nexthop now.
1991 if (bgp_attr_nexthop_valid(peer
, &attr
) < 0) {
1992 bgp_attr_unintern_sub(&attr
);
1998 if (BGP_DEBUG(update
, UPDATE_IN
))
1999 zlog_debug("%pBP rcvd UPDATE wlen %d attrlen %d alen %d", peer
,
2000 withdraw_len
, attribute_len
, update_len
);
2002 /* Parse any given NLRIs */
2003 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++) {
2007 /* NLRI is processed iff the peer if configured for the specific
2009 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
]) {
2011 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
2012 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
2016 /* EoR handled later */
2017 if (nlris
[i
].length
== 0)
2022 case NLRI_MP_UPDATE
:
2023 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
2027 case NLRI_MP_WITHDRAW
:
2028 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
2032 nlri_ret
= BGP_NLRI_PARSE_ERROR
;
2035 if (nlri_ret
< BGP_NLRI_PARSE_OK
2036 && nlri_ret
!= BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW
) {
2037 flog_err(EC_BGP_UPDATE_RCV
,
2038 "%s [Error] Error parsing NLRI", peer
->host
);
2039 if (peer_established(peer
))
2041 peer
, BGP_NOTIFY_UPDATE_ERR
,
2043 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
2044 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
2045 bgp_attr_unintern_sub(&attr
);
2052 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
2053 * and MP EoR should have only an empty MP_UNREACH
2055 if ((!update_len
&& !withdraw_len
&& nlris
[NLRI_MP_UPDATE
].length
== 0)
2056 || (attr_parse_ret
== BGP_ATTR_PARSE_EOR
)) {
2059 struct graceful_restart_info
*gr_info
;
2061 /* Restarting router */
2062 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)
2063 && BGP_PEER_RESTARTING_MODE(peer
))
2066 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
2068 * update and withdraw NLRI lengths are 0.
2070 if (!attribute_len
) {
2072 safi
= SAFI_UNICAST
;
2073 } else if (attr
.flag
& ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
)
2074 && nlris
[NLRI_MP_WITHDRAW
].length
== 0) {
2075 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
2076 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
2077 } else if (attr_parse_ret
== BGP_ATTR_PARSE_EOR
) {
2078 afi
= nlris
[NLRI_MP_UPDATE
].afi
;
2079 safi
= nlris
[NLRI_MP_UPDATE
].safi
;
2082 if (afi
&& peer
->afc
[afi
][safi
]) {
2083 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
2085 /* End-of-RIB received */
2086 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2087 PEER_STATUS_EOR_RECEIVED
)) {
2088 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2089 PEER_STATUS_EOR_RECEIVED
);
2090 bgp_update_explicit_eors(peer
);
2091 /* Update graceful restart information */
2092 gr_info
= &(peer
->bgp
->gr_info
[afi
][safi
]);
2094 gr_info
->eor_received
++;
2095 /* If EOR received from all peers and selection
2096 * deferral timer is running, cancel the timer
2097 * and invoke the best path calculation
2099 if (gr_info
->eor_required
2100 == gr_info
->eor_received
) {
2101 if (bgp_debug_neighbor_events(peer
))
2105 gr_info
->eor_required
,
2107 gr_info
->eor_received
);
2108 if (gr_info
->t_select_deferral
) {
2109 void *info
= EVENT_ARG(
2110 gr_info
->t_select_deferral
);
2111 XFREE(MTYPE_TMP
, info
);
2113 EVENT_OFF(gr_info
->t_select_deferral
);
2114 gr_info
->eor_required
= 0;
2115 gr_info
->eor_received
= 0;
2116 /* Best path selection */
2117 bgp_best_path_select_defer(peer
->bgp
,
2122 /* NSF delete stale route */
2123 if (peer
->nsf
[afi
][safi
])
2124 bgp_clear_stale_route(peer
, afi
, safi
);
2127 "%s: rcvd End-of-RIB for %s from %s in vrf %s",
2128 __func__
, get_afi_safi_str(afi
, safi
, false),
2129 peer
->host
, vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
2133 /* Everything is done. We unintern temporary structures which
2134 interned in bgp_attr_parse(). */
2135 bgp_attr_unintern_sub(&attr
);
2137 peer
->update_time
= monotime(NULL
);
2139 /* Notify BGP Conditional advertisement scanner process */
2140 peer
->advmap_table_change
= true;
2142 return Receive_UPDATE_message
;
2146 * Process BGP NOTIFY message for peer.
2149 * @param size size of the packet
2150 * @return as in summary
2152 static int bgp_notify_receive(struct peer
*peer
, bgp_size_t size
)
2154 struct bgp_notify outer
= {};
2155 struct bgp_notify inner
= {};
2156 bool hard_reset
= false;
2158 if (peer
->notify
.data
) {
2159 XFREE(MTYPE_BGP_NOTIFICATION
, peer
->notify
.data
);
2160 peer
->notify
.length
= 0;
2161 peer
->notify
.hard_reset
= false;
2164 outer
.code
= stream_getc(peer
->curr
);
2165 outer
.subcode
= stream_getc(peer
->curr
);
2166 outer
.length
= size
- 2;
2168 outer
.raw_data
= NULL
;
2170 outer
.raw_data
= XMALLOC(MTYPE_BGP_NOTIFICATION
, outer
.length
);
2171 memcpy(outer
.raw_data
, stream_pnt(peer
->curr
), outer
.length
);
2175 bgp_notify_received_hard_reset(peer
, outer
.code
, outer
.subcode
);
2176 if (hard_reset
&& outer
.length
) {
2177 inner
= bgp_notify_decapsulate_hard_reset(&outer
);
2178 peer
->notify
.hard_reset
= true;
2183 /* Preserv notify code and sub code. */
2184 peer
->notify
.code
= inner
.code
;
2185 peer
->notify
.subcode
= inner
.subcode
;
2186 /* For further diagnostic record returned Data. */
2188 peer
->notify
.length
= inner
.length
;
2190 XMALLOC(MTYPE_BGP_NOTIFICATION
, inner
.length
);
2191 memcpy(peer
->notify
.data
, inner
.raw_data
, inner
.length
);
2201 inner
.data
= XMALLOC(MTYPE_BGP_NOTIFICATION
,
2203 for (i
= 0; i
< inner
.length
; i
++)
2205 snprintf(c
, sizeof(c
), " %02x",
2206 stream_getc(peer
->curr
));
2208 strlcat(inner
.data
, c
,
2213 snprintf(c
, sizeof(c
), "%02x",
2214 stream_getc(peer
->curr
));
2216 strlcpy(inner
.data
, c
,
2221 bgp_notify_print(peer
, &inner
, "received", hard_reset
);
2223 XFREE(MTYPE_BGP_NOTIFICATION
, inner
.data
);
2227 XFREE(MTYPE_BGP_NOTIFICATION
, outer
.data
);
2228 XFREE(MTYPE_BGP_NOTIFICATION
, outer
.raw_data
);
2230 /* If this is a Hard Reset notification, we MUST free
2231 * the inner (encapsulated) notification too.
2234 XFREE(MTYPE_BGP_NOTIFICATION
, inner
.raw_data
);
2239 /* peer count update */
2240 atomic_fetch_add_explicit(&peer
->notify_in
, 1, memory_order_relaxed
);
2242 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
2244 /* We have to check for Notify with Unsupported Optional Parameter.
2245 in that case we fallback to open without the capability option.
2246 But this done in bgp_stop. We just mark it here to avoid changing
2248 if (inner
.code
== BGP_NOTIFY_OPEN_ERR
&&
2249 inner
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
2250 UNSET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
2252 /* If Graceful-Restart N-bit (Notification) is exchanged,
2253 * and it's not a Hard Reset, let's retain the routes.
2255 if (bgp_has_graceful_restart_notification(peer
) && !hard_reset
&&
2256 CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
))
2257 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2259 bgp_peer_gr_flags_update(peer
);
2260 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
2263 return Receive_NOTIFICATION_message
;
2267 * Process BGP ROUTEREFRESH message for peer.
2270 * @param size size of the packet
2271 * @return as in summary
2273 static int bgp_route_refresh_receive(struct peer
*peer
, bgp_size_t size
)
2277 iana_safi_t pkt_safi
;
2280 struct peer_af
*paf
;
2281 struct update_group
*updgrp
;
2282 struct peer
*updgrp_peer
;
2284 bool force_update
= false;
2285 bgp_size_t msg_length
=
2286 size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
);
2288 /* If peer does not have the capability, send notification. */
2289 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_ADV
)) {
2290 flog_err(EC_BGP_NO_CAP
,
2291 "%s [Error] BGP route refresh is not enabled",
2293 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2294 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2298 /* Status must be Established. */
2299 if (!peer_established(peer
)) {
2301 EC_BGP_INVALID_STATUS
,
2302 "%s [Error] Route refresh packet received under status %s",
2304 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2305 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
2306 bgp_fsm_error_subcode(peer
->status
));
2313 pkt_afi
= stream_getw(s
);
2314 subtype
= stream_getc(s
);
2315 pkt_safi
= stream_getc(s
);
2317 /* Convert AFI, SAFI to internal values and check. */
2318 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
2320 "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
2321 peer
->host
, iana_afi2str(pkt_afi
),
2322 iana_safi2str(pkt_safi
));
2323 return BGP_PACKET_NOOP
;
2326 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) {
2328 uint8_t when_to_refresh
;
2333 /* If the length, excluding the fixed-size message
2334 * header, of the received ROUTE-REFRESH message with
2335 * Message Subtype 1 and 2 is not 4, then the BGP
2336 * speaker MUST send a NOTIFICATION message with the
2337 * Error Code of "ROUTE-REFRESH Message Error" and the
2338 * subcode of "Invalid Message Length".
2340 if (msg_length
!= 4) {
2342 "%s Enhanced Route Refresh message length error",
2345 peer
, BGP_NOTIFY_ROUTE_REFRESH_ERR
,
2346 BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN
);
2349 /* When the BGP speaker receives a ROUTE-REFRESH message
2350 * with a "Message Subtype" field other than 0, 1, or 2,
2351 * it MUST ignore the received ROUTE-REFRESH message.
2355 "%s Enhanced Route Refresh invalid subtype",
2359 if (msg_length
< 5) {
2360 zlog_info("%s ORF route refresh length error",
2362 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2363 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2367 when_to_refresh
= stream_getc(s
);
2368 end
= stream_pnt(s
) + (size
- 5);
2370 while ((stream_pnt(s
) + 2) < end
) {
2371 orf_type
= stream_getc(s
);
2372 orf_len
= stream_getw(s
);
2374 /* orf_len in bounds? */
2375 if ((stream_pnt(s
) + orf_len
) > end
)
2376 break; /* XXX: Notify instead?? */
2377 if (orf_type
== ORF_TYPE_PREFIX
2378 || orf_type
== ORF_TYPE_PREFIX_OLD
) {
2379 uint8_t *p_pnt
= stream_pnt(s
);
2380 uint8_t *p_end
= stream_pnt(s
) + orf_len
;
2381 struct orf_prefix orfp
;
2386 int ret
= CMD_SUCCESS
;
2388 if (bgp_debug_neighbor_events(peer
)) {
2390 "%pBP rcvd Prefixlist ORF(%d) length %d",
2391 peer
, orf_type
, orf_len
);
2394 /* ORF prefix-list name */
2395 snprintf(name
, sizeof(name
), "%s.%d.%d",
2396 peer
->host
, afi
, safi
);
2398 /* we're going to read at least 1 byte of common
2400 * and 7 bytes of ORF Address-filter entry from
2403 if (*p_pnt
& ORF_COMMON_PART_REMOVE_ALL
) {
2404 if (bgp_debug_neighbor_events(peer
))
2406 "%pBP rcvd Remove-All pfxlist ORF request",
2408 prefix_bgp_orf_remove_all(afi
, name
);
2415 while (p_pnt
< p_end
) {
2416 /* If the ORF entry is malformed, want
2417 * to read as much of it
2418 * as possible without going beyond the
2419 * bounds of the entry,
2420 * to maximise debug information.
2423 memset(&orfp
, 0, sizeof(orfp
));
2425 /* after ++: p_pnt <= p_end */
2426 ok
= ((uint32_t)(p_end
- p_pnt
)
2427 >= sizeof(uint32_t));
2431 p_pnt
+= sizeof(uint32_t);
2432 orfp
.seq
= ntohl(seq
);
2436 /* val checked in prefix_bgp_orf_set */
2440 /* val checked in prefix_bgp_orf_set */
2444 if ((ok
= (p_pnt
< p_end
)))
2445 orfp
.p
.prefixlen
= *p_pnt
++;
2447 /* afi checked already */
2448 orfp
.p
.family
= afi2family(afi
);
2451 psize
= PSIZE(orfp
.p
.prefixlen
);
2452 /* valid for family ? */
2453 if (psize
> prefix_blen(&orfp
.p
)) {
2455 psize
= prefix_blen(&orfp
.p
);
2457 /* valid for packet ? */
2458 if (psize
> (p_end
- p_pnt
)) {
2460 psize
= p_end
- p_pnt
;
2464 memcpy(&orfp
.p
.u
.prefix
, p_pnt
,
2468 if (bgp_debug_neighbor_events(peer
)) {
2469 char buf
[INET6_BUFSIZ
];
2472 "%pBP rcvd %s %s seq %u %s/%d ge %d le %d%s",
2474 (common
& ORF_COMMON_PART_REMOVE
2477 (common
& ORF_COMMON_PART_DENY
2488 ok
? "" : " MALFORMED");
2492 ret
= prefix_bgp_orf_set(
2494 (common
& ORF_COMMON_PART_DENY
2497 (common
& ORF_COMMON_PART_REMOVE
2501 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
)) {
2503 "%pBP Received misformatted prefixlist ORF. Remove All pfxlist",
2505 prefix_bgp_orf_remove_all(afi
,
2511 peer
->orf_plist
[afi
][safi
] =
2512 prefix_bgp_orf_lookup(afi
, name
);
2514 stream_forward_getp(s
, orf_len
);
2516 if (bgp_debug_neighbor_events(peer
))
2517 zlog_debug("%pBP rcvd Refresh %s ORF request", peer
,
2518 when_to_refresh
== REFRESH_DEFER
2521 if (when_to_refresh
== REFRESH_DEFER
)
2522 return BGP_PACKET_NOOP
;
2525 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2526 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2527 PEER_STATUS_ORF_WAIT_REFRESH
))
2528 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2529 PEER_STATUS_ORF_WAIT_REFRESH
);
2531 paf
= peer_af_find(peer
, afi
, safi
);
2532 if (paf
&& paf
->subgroup
) {
2533 if (peer
->orf_plist
[afi
][safi
]) {
2534 updgrp
= PAF_UPDGRP(paf
);
2535 updgrp_peer
= UPDGRP_PEER(updgrp
);
2536 updgrp_peer
->orf_plist
[afi
][safi
] =
2537 peer
->orf_plist
[afi
][safi
];
2540 /* Avoid supressing duplicate routes later
2541 * when processing in subgroup_announce_table().
2543 force_update
= true;
2545 /* If the peer is configured for default-originate clear the
2546 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
2550 if (CHECK_FLAG(paf
->subgroup
->sflags
,
2551 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
2552 UNSET_FLAG(paf
->subgroup
->sflags
,
2553 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
2556 if (subtype
== BGP_ROUTE_REFRESH_BORR
) {
2557 /* A BGP speaker that has received the Graceful Restart
2558 * Capability from its neighbor MUST ignore any BoRRs for
2559 * an <AFI, SAFI> from the neighbor before the speaker
2560 * receives the EoR for the given <AFI, SAFI> from the
2563 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)
2564 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2565 PEER_STATUS_EOR_RECEIVED
)) {
2566 if (bgp_debug_neighbor_events(peer
))
2568 "%pBP rcvd route-refresh (BoRR) for %s/%s before EoR",
2569 peer
, afi2str(afi
), safi2str(safi
));
2570 return BGP_PACKET_NOOP
;
2573 if (peer
->t_refresh_stalepath
) {
2574 if (bgp_debug_neighbor_events(peer
))
2576 "%pBP rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
2577 peer
, afi2str(afi
), safi2str(safi
));
2578 return BGP_PACKET_NOOP
;
2581 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_BORR_RECEIVED
);
2582 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2583 PEER_STATUS_EORR_RECEIVED
);
2585 /* When a BGP speaker receives a BoRR message from
2586 * a peer, it MUST mark all the routes with the given
2587 * Address Family Identifier and Subsequent Address
2588 * Family Identifier, <AFI, SAFI> [RFC2918], from
2589 * that peer as stale.
2591 if (peer_active_nego(peer
)) {
2592 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2593 PEER_STATUS_ENHANCED_REFRESH
);
2594 bgp_set_stale_route(peer
, afi
, safi
);
2597 if (peer_established(peer
))
2598 event_add_timer(bm
->master
,
2599 bgp_refresh_stalepath_timer_expire
, paf
,
2600 peer
->bgp
->stalepath_time
,
2601 &peer
->t_refresh_stalepath
);
2603 if (bgp_debug_neighbor_events(peer
))
2605 "%pBP rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
2606 peer
, afi2str(afi
), safi2str(safi
),
2607 peer
->bgp
->stalepath_time
);
2608 } else if (subtype
== BGP_ROUTE_REFRESH_EORR
) {
2609 if (!peer
->t_refresh_stalepath
) {
2611 "%pBP rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
2612 peer
, afi2str(afi
), safi2str(safi
));
2613 return BGP_PACKET_NOOP
;
2616 EVENT_OFF(peer
->t_refresh_stalepath
);
2618 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_EORR_RECEIVED
);
2619 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2620 PEER_STATUS_BORR_RECEIVED
);
2622 if (bgp_debug_neighbor_events(peer
))
2624 "%pBP rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
2625 peer
, afi2str(afi
), safi2str(safi
));
2627 if (peer
->nsf
[afi
][safi
])
2628 bgp_clear_stale_route(peer
, afi
, safi
);
2630 if (bgp_debug_neighbor_events(peer
))
2632 "%pBP rcvd route-refresh (REQUEST) for %s/%s",
2633 peer
, afi2str(afi
), safi2str(safi
));
2635 /* In response to a "normal route refresh request" from the
2636 * peer, the speaker MUST send a BoRR message.
2638 if (CHECK_FLAG(peer
->cap
, PEER_CAP_ENHANCED_RR_RCV
)) {
2639 /* For a BGP speaker that supports the BGP Graceful
2640 * Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
2641 * to a neighbor before it sends the EoR for the
2642 * <AFI, SAFI> to the neighbor.
2644 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2645 PEER_STATUS_EOR_SEND
)) {
2646 if (bgp_debug_neighbor_events(peer
))
2648 "%pBP rcvd route-refresh (REQUEST) for %s/%s before EoR",
2651 /* Can't send BoRR now, postpone after EoR */
2652 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2653 PEER_STATUS_REFRESH_PENDING
);
2654 return BGP_PACKET_NOOP
;
2657 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
2658 BGP_ROUTE_REFRESH_BORR
);
2660 if (bgp_debug_neighbor_events(peer
))
2662 "%pBP sending route-refresh (BoRR) for %s/%s",
2663 peer
, afi2str(afi
), safi2str(safi
));
2665 /* Set flag Ready-To-Send to know when we can send EoRR
2668 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2669 PEER_STATUS_BORR_SEND
);
2670 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2671 PEER_STATUS_EORR_SEND
);
2675 /* Perform route refreshment to the peer */
2676 bgp_announce_route(peer
, afi
, safi
, force_update
);
2678 /* No FSM action necessary */
2679 return BGP_PACKET_NOOP
;
2683 * Parse BGP CAPABILITY message for peer.
2686 * @param size size of the packet
2687 * @return as in summary
2689 static int bgp_capability_msg_parse(struct peer
*peer
, uint8_t *pnt
,
2693 struct capability_mp_data mpc
;
2694 struct capability_header
*hdr
;
2698 iana_safi_t pkt_safi
;
2704 /* We need at least action, capability code and capability
2706 if (pnt
+ 3 > end
) {
2707 zlog_info("%s Capability length error", peer
->host
);
2708 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2709 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2713 hdr
= (struct capability_header
*)(pnt
+ 1);
2715 /* Action value check. */
2716 if (action
!= CAPABILITY_ACTION_SET
2717 && action
!= CAPABILITY_ACTION_UNSET
) {
2718 zlog_info("%s Capability Action Value error %d",
2719 peer
->host
, action
);
2720 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2721 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2725 if (bgp_debug_neighbor_events(peer
))
2727 "%s CAPABILITY has action: %d, code: %u, length %u",
2728 peer
->host
, action
, hdr
->code
, hdr
->length
);
2730 if (hdr
->length
< sizeof(struct capability_mp_data
)) {
2732 "%pBP Capability structure is not properly filled out, expected at least %zu bytes but header length specified is %d",
2733 peer
, sizeof(struct capability_mp_data
),
2738 /* Capability length check. */
2739 if ((pnt
+ hdr
->length
+ 3) > end
) {
2740 zlog_info("%s Capability length error", peer
->host
);
2741 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2742 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2746 /* Fetch structure to the byte stream. */
2747 memcpy(&mpc
, pnt
+ 3, sizeof(struct capability_mp_data
));
2748 pnt
+= hdr
->length
+ 3;
2750 /* We know MP Capability Code. */
2751 if (hdr
->code
== CAPABILITY_CODE_MP
) {
2752 pkt_afi
= ntohs(mpc
.afi
);
2753 pkt_safi
= mpc
.safi
;
2755 /* Ignore capability when override-capability is set. */
2756 if (CHECK_FLAG(peer
->flags
,
2757 PEER_FLAG_OVERRIDE_CAPABILITY
))
2760 /* Convert AFI, SAFI to internal values. */
2761 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
,
2763 if (bgp_debug_neighbor_events(peer
))
2765 "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)",
2767 iana_afi2str(pkt_afi
),
2768 iana_safi2str(pkt_safi
));
2772 /* Address family check. */
2773 if (bgp_debug_neighbor_events(peer
))
2775 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
2777 action
== CAPABILITY_ACTION_SET
2780 iana_afi2str(pkt_afi
),
2781 iana_safi2str(pkt_safi
));
2783 if (action
== CAPABILITY_ACTION_SET
) {
2784 peer
->afc_recv
[afi
][safi
] = 1;
2785 if (peer
->afc
[afi
][safi
]) {
2786 peer
->afc_nego
[afi
][safi
] = 1;
2787 bgp_announce_route(peer
, afi
, safi
,
2791 peer
->afc_recv
[afi
][safi
] = 0;
2792 peer
->afc_nego
[afi
][safi
] = 0;
2794 if (peer_active_nego(peer
))
2795 bgp_clear_route(peer
, afi
, safi
);
2801 EC_BGP_UNRECOGNIZED_CAPABILITY
,
2802 "%s unrecognized capability code: %d - ignored",
2803 peer
->host
, hdr
->code
);
2807 /* No FSM action necessary */
2808 return BGP_PACKET_NOOP
;
2812 * Parse BGP CAPABILITY message for peer.
2814 * Exported for unit testing.
2817 * @param size size of the packet
2818 * @return as in summary
2820 int bgp_capability_receive(struct peer
*peer
, bgp_size_t size
)
2824 /* Fetch pointer. */
2825 pnt
= stream_pnt(peer
->curr
);
2827 if (bgp_debug_neighbor_events(peer
))
2828 zlog_debug("%s rcv CAPABILITY", peer
->host
);
2830 /* If peer does not have the capability, send notification. */
2831 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_ADV
)) {
2832 flog_err(EC_BGP_NO_CAP
,
2833 "%s [Error] BGP dynamic capability is not enabled",
2835 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2836 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2840 /* Status must be Established. */
2841 if (!peer_established(peer
)) {
2844 "%s [Error] Dynamic capability packet received under status %s",
2846 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2847 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
2848 bgp_fsm_error_subcode(peer
->status
));
2853 return bgp_capability_msg_parse(peer
, pnt
, size
);
2857 * Processes a peer's input buffer.
2859 * This function sidesteps the event loop and directly calls bgp_event_update()
2860 * after processing each BGP message. This is necessary to ensure proper
2861 * ordering of FSM events and unifies the behavior that was present previously,
2862 * whereby some of the packet handling functions would update the FSM and some
2863 * would not, making event flow difficult to understand. Please think twice
2864 * before hacking this.
2866 * Thread type: EVENT_EVENT
2870 void bgp_process_packet(struct event
*thread
)
2872 /* Yes first of all get peer pointer. */
2873 struct peer
*peer
; // peer
2874 uint32_t rpkt_quanta_old
; // how many packets to read
2875 int fsm_update_result
; // return code of bgp_event_update()
2876 int mprc
; // message processing return code
2878 peer
= EVENT_ARG(thread
);
2879 rpkt_quanta_old
= atomic_load_explicit(&peer
->bgp
->rpkt_quanta
,
2880 memory_order_relaxed
);
2881 fsm_update_result
= 0;
2883 /* Guard against scheduled events that occur after peer deletion. */
2884 if (peer
->status
== Deleted
|| peer
->status
== Clearing
)
2887 unsigned int processed
= 0;
2889 while (processed
< rpkt_quanta_old
) {
2892 char notify_data_length
[2];
2894 frr_with_mutex (&peer
->io_mtx
) {
2895 peer
->curr
= stream_fifo_pop(peer
->ibuf
);
2898 if (peer
->curr
== NULL
) // no packets to process, hmm...
2901 /* skip the marker and copy the packet length */
2902 stream_forward_getp(peer
->curr
, BGP_MARKER_SIZE
);
2903 memcpy(notify_data_length
, stream_pnt(peer
->curr
), 2);
2905 /* read in the packet length and type */
2906 size
= stream_getw(peer
->curr
);
2907 type
= stream_getc(peer
->curr
);
2909 hook_call(bgp_packet_dump
, peer
, type
, size
, peer
->curr
);
2911 /* adjust size to exclude the marker + length + type */
2912 size
-= BGP_HEADER_SIZE
;
2914 /* Read rest of the packet and call each sort of packet routine
2918 frrtrace(2, frr_bgp
, open_process
, peer
, size
);
2919 atomic_fetch_add_explicit(&peer
->open_in
, 1,
2920 memory_order_relaxed
);
2921 mprc
= bgp_open_receive(peer
, size
);
2922 if (mprc
== BGP_Stop
)
2925 "%s: BGP OPEN receipt failed for peer: %s",
2926 __func__
, peer
->host
);
2928 case BGP_MSG_UPDATE
:
2929 frrtrace(2, frr_bgp
, update_process
, peer
, size
);
2930 atomic_fetch_add_explicit(&peer
->update_in
, 1,
2931 memory_order_relaxed
);
2932 peer
->readtime
= monotime(NULL
);
2933 mprc
= bgp_update_receive(peer
, size
);
2934 if (mprc
== BGP_Stop
)
2937 "%s: BGP UPDATE receipt failed for peer: %s",
2938 __func__
, peer
->host
);
2940 case BGP_MSG_NOTIFY
:
2941 frrtrace(2, frr_bgp
, notification_process
, peer
, size
);
2942 atomic_fetch_add_explicit(&peer
->notify_in
, 1,
2943 memory_order_relaxed
);
2944 mprc
= bgp_notify_receive(peer
, size
);
2945 if (mprc
== BGP_Stop
)
2948 "%s: BGP NOTIFY receipt failed for peer: %s",
2949 __func__
, peer
->host
);
2951 case BGP_MSG_KEEPALIVE
:
2952 frrtrace(2, frr_bgp
, keepalive_process
, peer
, size
);
2953 peer
->readtime
= monotime(NULL
);
2954 atomic_fetch_add_explicit(&peer
->keepalive_in
, 1,
2955 memory_order_relaxed
);
2956 mprc
= bgp_keepalive_receive(peer
, size
);
2957 if (mprc
== BGP_Stop
)
2960 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2961 __func__
, peer
->host
);
2963 case BGP_MSG_ROUTE_REFRESH_NEW
:
2964 case BGP_MSG_ROUTE_REFRESH_OLD
:
2965 frrtrace(2, frr_bgp
, refresh_process
, peer
, size
);
2966 atomic_fetch_add_explicit(&peer
->refresh_in
, 1,
2967 memory_order_relaxed
);
2968 mprc
= bgp_route_refresh_receive(peer
, size
);
2969 if (mprc
== BGP_Stop
)
2972 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2973 __func__
, peer
->host
);
2975 case BGP_MSG_CAPABILITY
:
2976 frrtrace(2, frr_bgp
, capability_process
, peer
, size
);
2977 atomic_fetch_add_explicit(&peer
->dynamic_cap_in
, 1,
2978 memory_order_relaxed
);
2979 mprc
= bgp_capability_receive(peer
, size
);
2980 if (mprc
== BGP_Stop
)
2983 "%s: BGP CAPABILITY receipt failed for peer: %s",
2984 __func__
, peer
->host
);
2987 /* Suppress uninitialized variable warning */
2991 * The message type should have been sanitized before
2992 * we ever got here. Receipt of a message with an
2993 * invalid header at this point is indicative of a
2996 assert (!"Message of invalid type received during input processing");
2999 /* delete processed packet */
3000 stream_free(peer
->curr
);
3005 if (mprc
!= BGP_PACKET_NOOP
)
3006 fsm_update_result
= bgp_event_update(peer
, mprc
);
3011 * If peer was deleted, do not process any more packets. This
3012 * is usually due to executing BGP_Stop or a stub deletion.
3014 if (fsm_update_result
== FSM_PEER_TRANSFERRED
3015 || fsm_update_result
== FSM_PEER_STOPPED
)
3019 if (fsm_update_result
!= FSM_PEER_TRANSFERRED
3020 && fsm_update_result
!= FSM_PEER_STOPPED
) {
3021 frr_with_mutex (&peer
->io_mtx
) {
3022 // more work to do, come back later
3023 if (peer
->ibuf
->count
> 0)
3024 event_add_event(bm
->master
, bgp_process_packet
,
3026 &peer
->t_process_packet
);
3031 /* Send EOR when routes are processed by selection deferral timer */
3032 void bgp_send_delayed_eor(struct bgp
*bgp
)
3035 struct listnode
*node
, *nnode
;
3037 /* EOR message sent in bgp_write_proceed_actions */
3038 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3039 bgp_write_proceed_actions(peer
);
3043 * Task callback to handle socket error encountered in the io pthread. We avoid
3044 * having the io pthread try to enqueue fsm events or mess with the peer
3047 void bgp_packet_process_error(struct event
*thread
)
3052 peer
= EVENT_ARG(thread
);
3053 code
= EVENT_VAL(thread
);
3055 if (bgp_debug_neighbor_events(peer
))
3056 zlog_debug("%s [Event] BGP error %d on fd %d",
3057 peer
->host
, code
, peer
->fd
);
3059 /* Closed connection or error on the socket */
3060 if (peer_established(peer
)) {
3061 if ((CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
3062 || CHECK_FLAG(peer
->flags
,
3063 PEER_FLAG_GRACEFUL_RESTART_HELPER
))
3064 && CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)) {
3065 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
3066 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
3068 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
3071 bgp_event_update(peer
, code
);