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
109 * @return the size of the stream
111 int bgp_packet_set_size(struct stream
*s
)
115 /* Preserve current pointer. */
116 cp
= stream_get_endp(s
);
117 stream_putw_at(s
, BGP_MARKER_SIZE
, cp
);
123 * Push a packet onto the beginning of the peer's output queue.
124 * This function acquires the peer's write mutex before proceeding.
126 static void bgp_packet_add(struct peer
*peer
, struct stream
*s
)
128 frr_with_mutex(&peer
->io_mtx
) {
129 stream_fifo_push(peer
->obuf
, s
);
133 static struct stream
*bgp_update_packet_eor(struct peer
*peer
, afi_t afi
,
138 iana_safi_t pkt_safi
;
140 if (DISABLE_BGP_ANNOUNCE
)
143 if (bgp_debug_neighbor_events(peer
))
144 zlog_debug("send End-of-RIB for %s to %s",
145 get_afi_safi_str(afi
, safi
, false), peer
->host
);
147 s
= stream_new(peer
->max_packet_size
);
149 /* Make BGP update packet. */
150 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
152 /* Unfeasible Routes Length */
155 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
156 /* Total Path Attribute Length */
159 /* Convert AFI, SAFI to values for packet. */
160 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
162 /* Total Path Attribute Length */
164 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
165 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
167 stream_putw(s
, pkt_afi
);
168 stream_putc(s
, pkt_safi
);
171 bgp_packet_set_size(s
);
175 /* Called when there is a change in the EOR(implicit or explicit) status of a
176 * peer. Ends the update-delay if all expected peers are done with EORs. */
177 void bgp_check_update_delay(struct bgp
*bgp
)
179 struct listnode
*node
, *nnode
;
180 struct peer
*peer
= NULL
;
182 if (bgp_debug_neighbor_events(peer
))
183 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
184 bgp
->established
, bgp
->restarted_peers
,
185 bgp
->implicit_eors
, bgp
->explicit_eors
);
188 <= bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
) {
190 * This is an extra sanity check to make sure we wait for all
191 * the eligible configured peers. This check is performed if
192 * establish wait timer is on, or establish wait option is not
193 * given with the update-delay command
195 if (bgp
->t_establish_wait
196 || (bgp
->v_establish_wait
== bgp
->v_update_delay
))
197 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
198 if (CHECK_FLAG(peer
->flags
,
199 PEER_FLAG_CONFIG_NODE
)
200 && !CHECK_FLAG(peer
->flags
,
202 && !CHECK_FLAG(peer
->bgp
->flags
,
204 && !peer
->update_delay_over
) {
205 if (bgp_debug_neighbor_events(peer
))
207 " Peer %s pending, continuing read-only mode",
214 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
215 bgp
->restarted_peers
, bgp
->implicit_eors
,
217 bgp_update_delay_end(bgp
);
222 * Called if peer is known to have restarted. The restart-state bit in
223 * Graceful-Restart capability is used for that
225 void bgp_update_restarted_peers(struct peer
*peer
)
227 if (!bgp_update_delay_active(peer
->bgp
))
228 return; /* BGP update delay has ended */
229 if (peer
->update_delay_over
)
230 return; /* This peer has already been considered */
232 if (bgp_debug_neighbor_events(peer
))
233 zlog_debug("Peer %s: Checking restarted", peer
->host
);
235 if (peer_established(peer
)) {
236 peer
->update_delay_over
= 1;
237 peer
->bgp
->restarted_peers
++;
238 bgp_check_update_delay(peer
->bgp
);
243 * Called as peer receives a keep-alive. Determines if this occurence can be
244 * taken as an implicit EOR for this peer.
245 * NOTE: The very first keep-alive after the Established state of a peer is
246 * considered implicit EOR for the update-delay purposes
248 void bgp_update_implicit_eors(struct peer
*peer
)
250 if (!bgp_update_delay_active(peer
->bgp
))
251 return; /* BGP update delay has ended */
252 if (peer
->update_delay_over
)
253 return; /* This peer has already been considered */
255 if (bgp_debug_neighbor_events(peer
))
256 zlog_debug("Peer %s: Checking implicit EORs", peer
->host
);
258 if (peer_established(peer
)) {
259 peer
->update_delay_over
= 1;
260 peer
->bgp
->implicit_eors
++;
261 bgp_check_update_delay(peer
->bgp
);
266 * Should be called only when there is a change in the EOR_RECEIVED status
267 * for any afi/safi on a peer.
269 static void bgp_update_explicit_eors(struct peer
*peer
)
274 if (!bgp_update_delay_active(peer
->bgp
))
275 return; /* BGP update delay has ended */
276 if (peer
->update_delay_over
)
277 return; /* This peer has already been considered */
279 if (bgp_debug_neighbor_events(peer
))
280 zlog_debug("Peer %s: Checking explicit EORs", peer
->host
);
282 FOREACH_AFI_SAFI (afi
, safi
) {
283 if (peer
->afc_nego
[afi
][safi
]
284 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
285 PEER_STATUS_EOR_RECEIVED
)) {
286 if (bgp_debug_neighbor_events(peer
))
288 " afi %d safi %d didn't receive EOR",
294 peer
->update_delay_over
= 1;
295 peer
->bgp
->explicit_eors
++;
296 bgp_check_update_delay(peer
->bgp
);
300 * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
302 * mp_withdraw, if set, is used to nullify attr structure on most of the
303 * calling safi function and for evpn, passed as parameter
305 int bgp_nlri_parse(struct peer
*peer
, struct attr
*attr
,
306 struct bgp_nlri
*packet
, int mp_withdraw
)
308 switch (packet
->safi
) {
311 return bgp_nlri_parse_ip(peer
, mp_withdraw
? NULL
: attr
,
313 case SAFI_LABELED_UNICAST
:
314 return bgp_nlri_parse_label(peer
, mp_withdraw
? NULL
: attr
,
317 return bgp_nlri_parse_vpn(peer
, mp_withdraw
? NULL
: attr
,
320 return bgp_nlri_parse_evpn(peer
, attr
, packet
, mp_withdraw
);
322 return bgp_nlri_parse_flowspec(peer
, attr
, packet
, mp_withdraw
);
324 return BGP_NLRI_PARSE_ERROR
;
328 * Checks a variety of conditions to determine whether the peer needs to be
329 * rescheduled for packet generation again, and does so if necessary.
331 * @param peer to check for rescheduling
333 static void bgp_write_proceed_actions(struct peer
*peer
)
338 struct bpacket
*next_pkt
;
339 struct update_subgroup
*subgrp
;
341 FOREACH_AFI_SAFI (afi
, safi
) {
342 paf
= peer_af_find(peer
, afi
, safi
);
345 subgrp
= paf
->subgroup
;
349 next_pkt
= paf
->next_pkt_to_send
;
350 if (next_pkt
&& next_pkt
->buffer
) {
351 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
352 bgp_generate_updgrp_packets
, 0);
356 /* No packets readily available for AFI/SAFI, are there
358 * that need to be generated? */
359 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
),
361 || subgroup_packets_to_build(subgrp
)) {
362 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
363 bgp_generate_updgrp_packets
, 0);
367 /* No packets to send, see if EOR is pending */
368 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
369 if (!subgrp
->t_coalesce
&& peer
->afc_nego
[afi
][safi
]
371 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
372 PEER_STATUS_EOR_SEND
)
373 && safi
!= SAFI_MPLS_VPN
) {
374 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
375 bgp_generate_updgrp_packets
, 0);
383 * Generate advertisement information (withdraws, updates, EOR) from each
384 * update group a peer belongs to, encode this information into packets, and
385 * enqueue the packets onto the peer's output buffer.
387 int bgp_generate_updgrp_packets(struct thread
*thread
)
389 struct peer
*peer
= THREAD_ARG(thread
);
393 struct bpacket
*next_pkt
;
395 uint32_t generated
= 0;
399 wpq
= atomic_load_explicit(&peer
->bgp
->wpkt_quanta
,
400 memory_order_relaxed
);
403 * The code beyond this part deals with update packets, proceed only
404 * if peer is Established and updates are not on hold (as part of
405 * update-delay processing).
407 if (!peer_established(peer
))
410 if ((peer
->bgp
->main_peers_update_hold
)
411 || bgp_update_delay_active(peer
->bgp
))
414 if (peer
->t_routeadv
)
419 FOREACH_AFI_SAFI (afi
, safi
) {
420 paf
= peer_af_find(peer
, afi
, safi
);
421 if (!paf
|| !PAF_SUBGRP(paf
))
423 next_pkt
= paf
->next_pkt_to_send
;
426 * Try to generate a packet for the peer if we are at
427 * the end of the list. Always try to push out
430 if (!next_pkt
|| !next_pkt
->buffer
) {
431 next_pkt
= subgroup_withdraw_packet(
433 if (!next_pkt
|| !next_pkt
->buffer
)
434 subgroup_update_packet(PAF_SUBGRP(paf
));
435 next_pkt
= paf
->next_pkt_to_send
;
439 * If we still don't have a packet to send to the peer,
440 * then try to find out out if we have to send eor or
441 * if not, skip to the next AFI, SAFI. Don't send the
442 * EOR prematurely; if the subgroup's coalesce timer is
443 * running, the adjacency-out structure is not created
446 if (!next_pkt
|| !next_pkt
->buffer
) {
447 if (!paf
->t_announce_route
) {
448 /* Make sure we supress BGP UPDATES
449 * for normal processing later again.
451 UNSET_FLAG(paf
->subgroup
->sflags
,
452 SUBGRP_STATUS_FORCE_UPDATES
);
454 /* If route-refresh BoRR message was
455 * already sent and we are done with
456 * re-announcing tables for a decent
457 * afi/safi, we ready to send
461 peer
->af_sflags
[afi
][safi
],
462 PEER_STATUS_BORR_SEND
)) {
463 bgp_route_refresh_send(
464 peer
, afi
, safi
, 0, 0,
466 BGP_ROUTE_REFRESH_EORR
);
468 SET_FLAG(peer
->af_sflags
[afi
]
470 PEER_STATUS_EORR_SEND
);
474 PEER_STATUS_BORR_SEND
);
476 if (bgp_debug_neighbor_events(
479 "%s sending route-refresh (EoRR) for %s/%s",
486 if (CHECK_FLAG(peer
->cap
,
487 PEER_CAP_RESTART_RCV
)) {
488 if (!(PAF_SUBGRP(paf
))->t_coalesce
489 && peer
->afc_nego
[afi
][safi
]
492 peer
->af_sflags
[afi
][safi
],
493 PEER_STATUS_EOR_SEND
)) {
494 /* If EOR is disabled,
495 * the message is not sent
497 if (BGP_SEND_EOR(peer
->bgp
, afi
,
503 PEER_STATUS_EOR_SEND
);
521 /* Update packet send time */
522 peer
->pkt_stime
[afi
][safi
] = monotime(NULL
);
524 /* Found a packet template to send, overwrite
525 * packet with appropriate attributes from peer
526 * and advance peer */
527 s
= bpacket_reformat_for_peer(next_pkt
, paf
);
528 bgp_packet_add(peer
, s
);
529 bpacket_queue_advance_peer(paf
);
531 } while (s
&& (++generated
< wpq
));
536 bgp_write_proceed_actions(peer
);
542 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
544 void bgp_keepalive_send(struct peer
*peer
)
548 s
= stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
);
550 /* Make keepalive packet. */
551 bgp_packet_set_marker(s
, BGP_MSG_KEEPALIVE
);
553 /* Set packet size. */
554 (void)bgp_packet_set_size(s
);
556 /* Dump packet if debug option is set. */
557 /* bgp_packet_dump (s); */
559 if (bgp_debug_keepalive(peer
))
560 zlog_debug("%s sending KEEPALIVE", peer
->host
);
562 /* Add packet to the peer. */
563 bgp_packet_add(peer
, s
);
569 * Creates a BGP Open packet and appends it to the peer's output queue.
570 * Sets capabilities as necessary.
572 void bgp_open_send(struct peer
*peer
)
575 uint16_t send_holdtime
;
578 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
579 send_holdtime
= peer
->holdtime
;
581 send_holdtime
= peer
->bgp
->default_holdtime
;
583 /* local-as Change */
584 if (peer
->change_local_as
)
585 local_as
= peer
->change_local_as
;
587 local_as
= peer
->local_as
;
589 s
= stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
);
591 /* Make open packet. */
592 bgp_packet_set_marker(s
, BGP_MSG_OPEN
);
594 /* Set open packet values. */
595 stream_putc(s
, BGP_VERSION_4
); /* BGP version */
596 stream_putw(s
, (local_as
<= BGP_AS_MAX
) ? (uint16_t)local_as
598 stream_putw(s
, send_holdtime
); /* Hold Time */
599 stream_put_in_addr(s
, &peer
->local_id
); /* BGP Identifier */
601 /* Set capability code. */
602 bgp_open_capability(s
, peer
);
604 /* Set BGP packet length. */
605 (void)bgp_packet_set_size(s
);
607 if (bgp_debug_neighbor_events(peer
))
609 "%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4",
610 peer
->host
, BGP_VERSION_4
, local_as
, send_holdtime
,
613 /* Dump packet if debug option is set. */
614 /* bgp_packet_dump (s); */
615 hook_call(bgp_packet_send
, peer
, BGP_MSG_OPEN
, stream_get_endp(s
), s
);
617 /* Add packet to the peer. */
618 bgp_packet_add(peer
, s
);
624 * Writes NOTIFICATION message directly to a peer socket without waiting for
627 * There must be exactly one stream on the peer->obuf FIFO, and the data within
628 * this stream must match the format of a BGP NOTIFICATION message.
629 * Transmission is best-effort.
631 * @requires peer->io_mtx
635 static void bgp_write_notify(struct peer
*peer
)
641 /* There should be at least one packet. */
642 s
= stream_fifo_pop(peer
->obuf
);
647 assert(stream_get_endp(s
) >= BGP_HEADER_SIZE
);
649 /* Stop collecting data within the socket */
650 sockopt_cork(peer
->fd
, 0);
653 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
654 * we only care about getting a clean shutdown at this point.
656 ret
= write(peer
->fd
, STREAM_DATA(s
), stream_get_endp(s
));
659 * only connection reset/close gets counted as TCP_fatal_error, failure
660 * to write the entire NOTIFY doesn't get different FSM treatment
664 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
668 /* Disable Nagle, make NOTIFY packet go out right away */
670 (void)setsockopt(peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
,
673 /* Retrieve BGP packet type. */
674 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
675 type
= stream_getc(s
);
677 assert(type
== BGP_MSG_NOTIFY
);
679 /* Type should be notify. */
680 atomic_fetch_add_explicit(&peer
->notify_out
, 1, memory_order_relaxed
);
683 /* Double start timer. */
686 /* Overflow check. */
687 if (peer
->v_start
>= (60 * 2))
688 peer
->v_start
= (60 * 2);
691 * Handle Graceful Restart case where the state changes to
692 * Connect instead of Idle
694 BGP_EVENT_ADD(peer
, BGP_Stop
);
700 * Creates a BGP Notify and appends it to the peer's output queue.
702 * This function attempts to write the packet from the thread it is called
703 * from, to ensure the packet gets out ASAP.
705 * This function may be called from multiple threads. Since the function
706 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
707 * call to prevent tampering from other threads.
709 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
710 * return, the peer structure *must* be reset; no assumptions about session
714 * @param code BGP error code
715 * @param sub_code BGP error subcode
716 * @param data Data portion
717 * @param datalen length of data portion
719 void bgp_notify_send_with_data(struct peer
*peer
, uint8_t code
,
720 uint8_t sub_code
, uint8_t *data
, size_t datalen
)
724 /* Lock I/O mutex to prevent other threads from pushing packets */
725 frr_mutex_lock_autounlock(&peer
->io_mtx
);
726 /* ============================================== */
728 /* Allocate new stream. */
729 s
= stream_new(peer
->max_packet_size
);
731 /* Make notify packet. */
732 bgp_packet_set_marker(s
, BGP_MSG_NOTIFY
);
734 /* Set notify packet values. */
735 stream_putc(s
, code
); /* BGP notify code */
736 stream_putc(s
, sub_code
); /* BGP notify sub_code */
738 /* If notify data is present. */
740 stream_write(s
, data
, datalen
);
742 /* Set BGP packet length. */
743 bgp_packet_set_size(s
);
745 /* wipe output buffer */
746 stream_fifo_clean(peer
->obuf
);
749 * If possible, store last packet for debugging purposes. This check is
750 * in place because we are sometimes called with a doppelganger peer,
751 * who tends to have a plethora of fields nulled out.
754 size_t packetsize
= stream_get_endp(peer
->curr
);
755 assert(packetsize
<= peer
->max_packet_size
);
756 memcpy(peer
->last_reset_cause
, peer
->curr
->data
, packetsize
);
757 peer
->last_reset_cause_size
= packetsize
;
762 struct bgp_notify bgp_notify
;
767 bgp_notify
.code
= code
;
768 bgp_notify
.subcode
= sub_code
;
769 bgp_notify
.data
= NULL
;
770 bgp_notify
.length
= datalen
;
771 bgp_notify
.raw_data
= data
;
773 peer
->notify
.code
= bgp_notify
.code
;
774 peer
->notify
.subcode
= bgp_notify
.subcode
;
776 if (bgp_notify
.length
&& data
) {
778 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
779 for (i
= 0; i
< bgp_notify
.length
; i
++)
781 snprintf(c
, sizeof(c
), " %02x",
784 strlcat(bgp_notify
.data
, c
,
789 snprintf(c
, sizeof(c
), "%02x", data
[i
]);
791 strlcpy(bgp_notify
.data
, c
,
795 bgp_notify_print(peer
, &bgp_notify
, "sending");
797 if (bgp_notify
.data
) {
798 XFREE(MTYPE_TMP
, bgp_notify
.data
);
799 bgp_notify
.length
= 0;
803 /* peer reset cause */
804 if (code
== BGP_NOTIFY_CEASE
) {
805 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
806 peer
->last_reset
= PEER_DOWN_USER_RESET
;
807 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
808 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
810 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
812 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
814 /* Add packet to peer's output queue */
815 stream_fifo_push(peer
->obuf
, s
);
817 bgp_peer_gr_flags_update(peer
);
818 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
821 bgp_write_notify(peer
);
825 * Creates a BGP Notify and appends it to the peer's output queue.
827 * This function attempts to write the packet from the thread it is called
828 * from, to ensure the packet gets out ASAP.
831 * @param code BGP error code
832 * @param sub_code BGP error subcode
834 void bgp_notify_send(struct peer
*peer
, uint8_t code
, uint8_t sub_code
)
836 bgp_notify_send_with_data(peer
, code
, sub_code
, NULL
, 0);
840 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
843 * @param afi Address Family Identifier
844 * @param safi Subsequent Address Family Identifier
845 * @param orf_type Outbound Route Filtering type
846 * @param when_to_refresh Whether to refresh immediately or defer
847 * @param remove Whether to remove ORF for specified AFI/SAFI
849 void bgp_route_refresh_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
850 uint8_t orf_type
, uint8_t when_to_refresh
,
851 int remove
, uint8_t subtype
)
854 struct bgp_filter
*filter
;
857 iana_safi_t pkt_safi
;
859 if (DISABLE_BGP_ANNOUNCE
)
862 filter
= &peer
->filter
[afi
][safi
];
864 /* Convert AFI, SAFI to values for packet. */
865 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
867 s
= stream_new(peer
->max_packet_size
);
869 /* Make BGP update packet. */
870 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
871 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_NEW
);
873 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_OLD
);
875 /* Encode Route Refresh message. */
876 stream_putw(s
, pkt_afi
);
878 stream_putc(s
, subtype
);
881 stream_putc(s
, pkt_safi
);
883 if (orf_type
== ORF_TYPE_PREFIX
|| orf_type
== ORF_TYPE_PREFIX_OLD
)
884 if (remove
|| filter
->plist
[FILTER_IN
].plist
) {
889 stream_putc(s
, when_to_refresh
);
890 stream_putc(s
, orf_type
);
891 orfp
= stream_get_endp(s
);
895 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
896 PEER_STATUS_ORF_PREFIX_SEND
);
897 stream_putc(s
, ORF_COMMON_PART_REMOVE_ALL
);
898 if (bgp_debug_neighbor_events(peer
))
900 "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s",
901 peer
->host
, orf_type
,
902 (when_to_refresh
== REFRESH_DEFER
905 iana_afi2str(pkt_afi
),
906 iana_safi2str(pkt_safi
));
908 SET_FLAG(peer
->af_sflags
[afi
][safi
],
909 PEER_STATUS_ORF_PREFIX_SEND
);
910 prefix_bgp_orf_entry(
911 s
, filter
->plist
[FILTER_IN
].plist
,
913 ORF_COMMON_PART_PERMIT
,
914 ORF_COMMON_PART_DENY
);
915 if (bgp_debug_neighbor_events(peer
))
917 "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s",
918 peer
->host
, orf_type
,
919 (when_to_refresh
== REFRESH_DEFER
922 iana_afi2str(pkt_afi
),
923 iana_safi2str(pkt_safi
));
926 /* Total ORF Entry Len. */
927 orf_len
= stream_get_endp(s
) - orfp
- 2;
928 stream_putw_at(s
, orfp
, orf_len
);
931 /* Set packet size. */
932 (void)bgp_packet_set_size(s
);
934 if (bgp_debug_neighbor_events(peer
)) {
936 zlog_debug("%s sending REFRESH_REQ for afi/safi: %s/%s",
937 peer
->host
, iana_afi2str(pkt_afi
),
938 iana_safi2str(pkt_safi
));
941 /* Add packet to the peer. */
942 bgp_packet_add(peer
, s
);
948 * Create a BGP Capability packet and append it to the peer's output queue.
951 * @param afi Address Family Identifier
952 * @param safi Subsequent Address Family Identifier
953 * @param capability_code BGP Capability Code
954 * @param action Set or Remove capability
956 void bgp_capability_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
957 int capability_code
, int action
)
961 iana_safi_t pkt_safi
;
963 /* Convert AFI, SAFI to values for packet. */
964 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
966 s
= stream_new(peer
->max_packet_size
);
968 /* Make BGP update packet. */
969 bgp_packet_set_marker(s
, BGP_MSG_CAPABILITY
);
971 /* Encode MP_EXT capability. */
972 if (capability_code
== CAPABILITY_CODE_MP
) {
973 stream_putc(s
, action
);
974 stream_putc(s
, CAPABILITY_CODE_MP
);
975 stream_putc(s
, CAPABILITY_CODE_MP_LEN
);
976 stream_putw(s
, pkt_afi
);
978 stream_putc(s
, pkt_safi
);
980 if (bgp_debug_neighbor_events(peer
))
982 "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
984 action
== CAPABILITY_ACTION_SET
? "Advertising"
986 iana_afi2str(pkt_afi
), iana_safi2str(pkt_safi
));
989 /* Set packet size. */
990 (void)bgp_packet_set_size(s
);
992 /* Add packet to the peer. */
993 bgp_packet_add(peer
, s
);
998 /* RFC1771 6.8 Connection collision detection. */
999 static int bgp_collision_detect(struct peer
*new, struct in_addr remote_id
)
1004 * Upon receipt of an OPEN message, the local system must examine
1005 * all of its connections that are in the OpenConfirm state. A BGP
1006 * speaker may also examine connections in an OpenSent state if it
1007 * knows the BGP Identifier of the peer by means outside of the
1008 * protocol. If among these connections there is a connection to a
1009 * remote BGP speaker whose BGP Identifier equals the one in the
1010 * OPEN message, then the local system performs the following
1011 * collision resolution procedure:
1013 peer
= new->doppelganger
;
1018 * Do not accept the new connection in Established or Clearing
1019 * states. Note that a peer GR is handled by closing the existing
1020 * connection upon receipt of new one.
1022 if (peer_established(peer
) || peer
->status
== Clearing
) {
1023 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1024 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1028 if ((peer
->status
!= OpenConfirm
) && (peer
->status
!= OpenSent
))
1032 * 1. The BGP Identifier of the local system is
1033 * compared to the BGP Identifier of the remote
1034 * system (as specified in the OPEN message).
1036 * If the BGP Identifiers of the peers
1037 * involved in the connection collision
1038 * are identical, then the connection
1039 * initiated by the BGP speaker with the
1040 * larger AS number is preserved.
1042 if (ntohl(peer
->local_id
.s_addr
) < ntohl(remote_id
.s_addr
)
1043 || (ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)
1044 && peer
->local_as
< peer
->as
))
1045 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
1047 * 2. If the value of the local BGP
1048 * Identifier is less than the remote one,
1049 * the local system closes BGP connection
1050 * that already exists (the one that is
1051 * already in the OpenConfirm state),
1052 * and accepts BGP connection initiated by
1053 * the remote system.
1055 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1056 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1059 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1060 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1064 if (ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)
1065 && peer
->local_as
== peer
->as
)
1066 flog_err(EC_BGP_ROUTER_ID_SAME
,
1067 "Peer's router-id %pI4 is the same as ours",
1071 * 3. Otherwise, the local system closes newly
1072 * created BGP connection (the one associated with the
1073 * newly received OPEN message), and continues to use
1074 * the existing one (the one that is already in the
1075 * OpenConfirm state).
1077 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
1078 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1079 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1082 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1083 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1089 /* Packet processing routines ---------------------------------------------- */
1091 * This is a family of functions designed to be called from
1092 * bgp_process_packet(). These functions all share similar behavior and should
1093 * adhere to the following invariants and restrictions:
1097 * The return code of any one of those functions should be one of the FSM event
1098 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1099 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1100 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1101 * error and Receive_OPEN_message otherwise.
1103 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1108 * - May send NOTIFY messages
1109 * - May not modify peer->status
1110 * - May not call bgp_event_update()
1113 #define BGP_PACKET_NOOP 0
1116 * Process BGP OPEN message for peer.
1118 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1119 * and returns BGP_Stop.
1122 * @param size size of the packet
1123 * @return as in summary
1125 static int bgp_open_receive(struct peer
*peer
, bgp_size_t size
)
1131 uint16_t send_holdtime
;
1133 as_t as4
= 0, as4_be
;
1134 struct in_addr remote_id
;
1136 uint8_t notify_data_remote_as
[2];
1137 uint8_t notify_data_remote_as4
[4];
1138 uint8_t notify_data_remote_id
[4];
1139 uint16_t *holdtime_ptr
;
1141 /* Parse open packet. */
1142 version
= stream_getc(peer
->curr
);
1143 memcpy(notify_data_remote_as
, stream_pnt(peer
->curr
), 2);
1144 remote_as
= stream_getw(peer
->curr
);
1145 holdtime_ptr
= (uint16_t *)stream_pnt(peer
->curr
);
1146 holdtime
= stream_getw(peer
->curr
);
1147 memcpy(notify_data_remote_id
, stream_pnt(peer
->curr
), 4);
1148 remote_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1150 /* Receive OPEN message log */
1151 if (bgp_debug_neighbor_events(peer
))
1153 "%s rcv OPEN, version %d, remote-as (in open) %u, holdtime %d, id %pI4",
1154 peer
->host
, version
, remote_as
, holdtime
, &remote_id
);
1156 /* BEGIN to read the capability here, but dont do it yet */
1158 optlen
= stream_getc(peer
->curr
);
1161 /* If not enough bytes, it is an error. */
1162 if (STREAM_READABLE(peer
->curr
) < optlen
) {
1163 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1164 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1168 /* We need the as4 capability value *right now* because
1169 * if it is there, we have not got the remote_as yet, and
1171 * that we do not know which peer is connecting to us now.
1173 as4
= peek_for_as4_capability(peer
, optlen
);
1176 as4_be
= htonl(as4
);
1177 memcpy(notify_data_remote_as4
, &as4_be
, 4);
1179 /* Just in case we have a silly peer who sends AS4 capability set to 0
1181 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
) {
1182 flog_err(EC_BGP_PKT_OPEN
,
1183 "%s bad OPEN, got AS4 capability, but AS4 set to 0",
1185 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1186 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1187 notify_data_remote_as4
, 4);
1191 /* Codification of AS 0 Processing */
1192 if (remote_as
== BGP_AS_ZERO
) {
1193 flog_err(EC_BGP_PKT_OPEN
, "%s bad OPEN, got AS set to 0",
1195 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1196 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1200 if (remote_as
== BGP_AS_TRANS
) {
1201 /* Take the AS4 from the capability. We must have received the
1202 * capability now! Otherwise we have a asn16 peer who uses
1203 * BGP_AS_TRANS, for some unknown reason.
1205 if (as4
== BGP_AS_TRANS
) {
1208 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1210 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1211 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1212 notify_data_remote_as4
, 4);
1216 if (!as4
&& BGP_DEBUG(as4
, AS4
))
1218 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.",
1220 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG(as4
, AS4
))
1222 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.",
1227 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1229 /* If we have got the capability, peer->as4cap must match
1231 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)
1232 && as4
!= remote_as
) {
1233 /* raise error, log this, close session */
1236 "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
1237 peer
->host
, as4
, remote_as
);
1238 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1239 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1240 notify_data_remote_as4
, 4);
1246 * If the BGP Identifier field of the OPEN message
1247 * is zero, or if it is the same as the BGP Identifier
1248 * of the local BGP speaker and the message is from an
1249 * internal peer, then the Error Subcode is set to
1250 * "Bad BGP Identifier".
1252 if (remote_id
.s_addr
== INADDR_ANY
1253 || (peer
->sort
== BGP_PEER_IBGP
1254 && ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
))) {
1255 if (bgp_debug_neighbor_events(peer
))
1256 zlog_debug("%s bad OPEN, wrong router identifier %pI4",
1257 peer
->host
, &remote_id
);
1258 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1259 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1260 notify_data_remote_id
, 4);
1264 /* Peer BGP version check. */
1265 if (version
!= BGP_VERSION_4
) {
1266 uint16_t maxver
= htons(BGP_VERSION_4
);
1267 /* XXX this reply may not be correct if version < 4 XXX */
1268 if (bgp_debug_neighbor_events(peer
))
1270 "%s bad protocol version, remote requested %d, local request %d",
1271 peer
->host
, version
, BGP_VERSION_4
);
1272 /* Data must be in network byte order here */
1273 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1274 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1275 (uint8_t *)&maxver
, 2);
1279 /* Check neighbor as number. */
1280 if (peer
->as_type
== AS_UNSPECIFIED
) {
1281 if (bgp_debug_neighbor_events(peer
))
1283 "%s bad OPEN, remote AS is unspecified currently",
1285 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1286 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1287 notify_data_remote_as
, 2);
1289 } else if (peer
->as_type
== AS_INTERNAL
) {
1290 if (remote_as
!= peer
->bgp
->as
) {
1291 if (bgp_debug_neighbor_events(peer
))
1293 "%s bad OPEN, remote AS is %u, internal specified",
1294 peer
->host
, remote_as
);
1295 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1296 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1297 notify_data_remote_as
, 2);
1300 peer
->as
= peer
->local_as
;
1301 } else if (peer
->as_type
== AS_EXTERNAL
) {
1302 if (remote_as
== peer
->bgp
->as
) {
1303 if (bgp_debug_neighbor_events(peer
))
1305 "%s bad OPEN, remote AS is %u, external specified",
1306 peer
->host
, remote_as
);
1307 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1308 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1309 notify_data_remote_as
, 2);
1312 peer
->as
= remote_as
;
1313 } else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
)) {
1314 if (bgp_debug_neighbor_events(peer
))
1315 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1316 peer
->host
, remote_as
, peer
->as
);
1317 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1318 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1319 notify_data_remote_as
, 2);
1324 * When collision is detected and this peer is closed.
1325 * Return immediately.
1327 ret
= bgp_collision_detect(peer
, remote_id
);
1332 if (bgp_getsockname(peer
) < 0) {
1333 flog_err_sys(EC_LIB_SOCKET
,
1334 "%s: bgp_getsockname() failed for peer: %s",
1335 __func__
, peer
->host
);
1339 /* Set remote router-id */
1340 peer
->remote_id
= remote_id
;
1342 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1343 calculate the value of the Hold Timer by using the smaller of its
1344 configured Hold Time and the Hold Time received in the OPEN message.
1345 The Hold Time MUST be either zero or at least three seconds. An
1346 implementation may reject connections on the basis of the Hold Time.
1349 if (holdtime
< 3 && holdtime
!= 0) {
1350 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1351 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1352 (uint8_t *)holdtime_ptr
, 2);
1356 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1357 would be one third of the Hold Time interval. KEEPALIVE messages
1358 MUST NOT be sent more frequently than one per second. An
1359 implementation MAY adjust the rate at which it sends KEEPALIVE
1360 messages as a function of the Hold Time interval. */
1362 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
1363 send_holdtime
= peer
->holdtime
;
1365 send_holdtime
= peer
->bgp
->default_holdtime
;
1367 if (holdtime
< send_holdtime
)
1368 peer
->v_holdtime
= holdtime
;
1370 peer
->v_holdtime
= send_holdtime
;
1372 /* Set effective keepalive to 1/3 the effective holdtime.
1373 * Use configured keeplive when < effective keepalive.
1375 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1376 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1377 if (peer
->keepalive
&& peer
->keepalive
< peer
->v_keepalive
)
1378 peer
->v_keepalive
= peer
->keepalive
;
1380 if (peer
->bgp
->default_keepalive
1381 && peer
->bgp
->default_keepalive
< peer
->v_keepalive
)
1382 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1385 /* Open option part parse. */
1387 if (bgp_open_option_parse(peer
, optlen
, &mp_capability
) < 0)
1390 if (bgp_debug_neighbor_events(peer
))
1391 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1396 * Assume that the peer supports the locally configured set of
1397 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1398 * capabilities, or if 'override-capability' is configured.
1401 || CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
1402 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] =
1403 peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1404 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] =
1405 peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1406 peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
] =
1407 peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
];
1408 peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
] =
1409 peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
];
1410 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] =
1411 peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1412 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] =
1413 peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1414 peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
] =
1415 peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
];
1416 peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
] =
1417 peer
->afc
[AFI_L2VPN
][SAFI_EVPN
];
1418 peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
] =
1419 peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
];
1422 /* Verify valid local address present based on negotiated
1423 * address-families. */
1424 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
1425 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
1426 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
1427 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
1428 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]) {
1429 if (peer
->nexthop
.v4
.s_addr
== INADDR_ANY
) {
1430 #if defined(HAVE_CUMULUS)
1431 zlog_warn("%s: No local IPv4 addr, BGP routing may not work",
1436 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
1437 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
1438 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
1439 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
1440 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]) {
1441 if (IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_global
)) {
1442 #if defined(HAVE_CUMULUS)
1443 zlog_warn("%s: No local IPv6 address, BGP routing may not work",
1448 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1450 return Receive_OPEN_message
;
1454 * Process BGP KEEPALIVE message for peer.
1457 * @param size size of the packet
1458 * @return as in summary
1460 static int bgp_keepalive_receive(struct peer
*peer
, bgp_size_t size
)
1462 if (bgp_debug_keepalive(peer
))
1463 zlog_debug("%s KEEPALIVE rcvd", peer
->host
);
1465 bgp_update_implicit_eors(peer
);
1467 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1469 /* If the peer's RTT is higher than expected, shutdown
1470 * the peer automatically.
1472 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_RTT_SHUTDOWN
)
1473 && peer
->rtt
> peer
->rtt_expected
) {
1475 peer
->rtt_keepalive_rcv
++;
1477 if (peer
->rtt_keepalive_rcv
> peer
->rtt_keepalive_conf
) {
1479 "%s shutdown due to high round-trip-time (%dms > %dms)",
1480 peer
->host
, peer
->rtt
, peer
->rtt_expected
);
1481 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1484 if (peer
->rtt_keepalive_rcv
)
1485 peer
->rtt_keepalive_rcv
--;
1488 return Receive_KEEPALIVE_message
;
1491 static int bgp_refresh_stalepath_timer_expire(struct thread
*thread
)
1493 struct peer_af
*paf
;
1495 paf
= THREAD_ARG(thread
);
1497 afi_t afi
= paf
->afi
;
1498 safi_t safi
= paf
->safi
;
1499 struct peer
*peer
= paf
->peer
;
1501 peer
->t_refresh_stalepath
= NULL
;
1503 if (peer
->nsf
[afi
][safi
])
1504 bgp_clear_stale_route(peer
, afi
, safi
);
1506 if (bgp_debug_neighbor_events(peer
))
1507 zlog_debug("%s: route-refresh (BoRR) timer for %s/%s expired",
1508 peer
->host
, afi2str(afi
), safi2str(safi
));
1510 bgp_timer_set(peer
);
1516 * Process BGP UPDATE message for peer.
1518 * Parses UPDATE and creates attribute object.
1521 * @param size size of the packet
1522 * @return as in summary
1524 static int bgp_update_receive(struct peer
*peer
, bgp_size_t size
)
1530 bgp_size_t attribute_len
;
1531 bgp_size_t update_len
;
1532 bgp_size_t withdraw_len
;
1533 bool restart
= false;
1542 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1544 /* Status must be Established. */
1545 if (!peer_established(peer
)) {
1546 flog_err(EC_BGP_INVALID_STATUS
,
1547 "%s [FSM] Update packet received under status %s",
1549 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1550 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1551 bgp_fsm_error_subcode(peer
->status
));
1555 /* Set initial values. */
1556 memset(&attr
, 0, sizeof(struct attr
));
1557 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
1558 attr
.label
= MPLS_INVALID_LABEL
;
1559 memset(&nlris
, 0, sizeof(nlris
));
1560 memset(peer
->rcvd_attr_str
, 0, BUFSIZ
);
1561 peer
->rcvd_attr_printed
= 0;
1564 end
= stream_pnt(s
) + size
;
1566 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1567 Length is too large (i.e., if Unfeasible Routes Length + Total
1568 Attribute Length + 23 exceeds the message Length), then the Error
1569 Subcode is set to Malformed Attribute List. */
1570 if (stream_pnt(s
) + 2 > end
) {
1571 flog_err(EC_BGP_UPDATE_RCV
,
1572 "%s [Error] Update packet error (packet length is short for unfeasible length)",
1574 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1575 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1579 /* Unfeasible Route Length. */
1580 withdraw_len
= stream_getw(s
);
1582 /* Unfeasible Route Length check. */
1583 if (stream_pnt(s
) + withdraw_len
> end
) {
1584 flog_err(EC_BGP_UPDATE_RCV
,
1585 "%s [Error] Update packet error (packet unfeasible length overflow %d)",
1586 peer
->host
, withdraw_len
);
1587 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1588 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1592 /* Unfeasible Route packet format check. */
1593 if (withdraw_len
> 0) {
1594 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1595 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1596 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt(s
);
1597 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1598 stream_forward_getp(s
, withdraw_len
);
1601 /* Attribute total length check. */
1602 if (stream_pnt(s
) + 2 > end
) {
1604 EC_BGP_UPDATE_PACKET_SHORT
,
1605 "%s [Error] Packet Error (update packet is short for attribute length)",
1607 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1608 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1612 /* Fetch attribute total length. */
1613 attribute_len
= stream_getw(s
);
1615 /* Attribute length check. */
1616 if (stream_pnt(s
) + attribute_len
> end
) {
1618 EC_BGP_UPDATE_PACKET_LONG
,
1619 "%s [Error] Packet Error (update packet attribute length overflow %d)",
1620 peer
->host
, attribute_len
);
1621 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1622 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1626 /* Certain attribute parsing errors should not be considered bad enough
1627 * to reset the session for, most particularly any partial/optional
1628 * attributes that have 'tunneled' over speakers that don't understand
1629 * them. Instead we withdraw only the prefix concerned.
1631 * Complicates the flow a little though..
1633 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1634 /* This define morphs the update case into a withdraw when lower levels
1635 * have signalled an error condition where this is best.
1637 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1639 /* Parse attribute when it exists. */
1640 if (attribute_len
) {
1641 attr_parse_ret
= bgp_attr_parse(peer
, &attr
, attribute_len
,
1642 &nlris
[NLRI_MP_UPDATE
],
1643 &nlris
[NLRI_MP_WITHDRAW
]);
1644 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
) {
1645 bgp_attr_unintern_sub(&attr
);
1650 /* Logging the attribute. */
1651 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1652 || BGP_DEBUG(update
, UPDATE_IN
)
1653 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
1654 ret
= bgp_dump_attr(&attr
, peer
->rcvd_attr_str
,
1655 sizeof(peer
->rcvd_attr_str
));
1657 peer
->stat_upd_7606
++;
1659 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1662 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1665 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1)) {
1666 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
1667 peer
->rcvd_attr_str
);
1668 peer
->rcvd_attr_printed
= 1;
1672 /* Network Layer Reachability Information. */
1673 update_len
= end
- stream_pnt(s
);
1676 /* Set NLRI portion to structure. */
1677 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1678 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1679 nlris
[NLRI_UPDATE
].nlri
= stream_pnt(s
);
1680 nlris
[NLRI_UPDATE
].length
= update_len
;
1681 stream_forward_getp(s
, update_len
);
1683 if (CHECK_FLAG(attr
.flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
))) {
1685 * We skipped nexthop attribute validation earlier so
1686 * validate the nexthop now.
1688 if (bgp_attr_nexthop_valid(peer
, &attr
) < 0) {
1689 bgp_attr_unintern_sub(&attr
);
1695 if (BGP_DEBUG(update
, UPDATE_IN
))
1696 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1697 peer
->host
, withdraw_len
, attribute_len
, update_len
);
1699 /* Parse any given NLRIs */
1700 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++) {
1704 /* NLRI is processed iff the peer if configured for the specific
1706 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
]) {
1708 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1709 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1713 /* EoR handled later */
1714 if (nlris
[i
].length
== 0)
1719 case NLRI_MP_UPDATE
:
1720 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
1724 case NLRI_MP_WITHDRAW
:
1725 nlri_ret
= bgp_nlri_parse(peer
, &attr
, &nlris
[i
], 1);
1728 nlri_ret
= BGP_NLRI_PARSE_ERROR
;
1731 if (nlri_ret
< BGP_NLRI_PARSE_OK
1732 && nlri_ret
!= BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW
) {
1733 flog_err(EC_BGP_UPDATE_RCV
,
1734 "%s [Error] Error parsing NLRI", peer
->host
);
1735 if (peer_established(peer
))
1737 peer
, BGP_NOTIFY_UPDATE_ERR
,
1739 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1740 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1741 bgp_attr_unintern_sub(&attr
);
1748 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1749 * and MP EoR should have only an empty MP_UNREACH
1751 if ((!update_len
&& !withdraw_len
&& nlris
[NLRI_MP_UPDATE
].length
== 0)
1752 || (attr_parse_ret
== BGP_ATTR_PARSE_EOR
)) {
1755 struct graceful_restart_info
*gr_info
;
1757 /* Restarting router */
1758 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)
1759 && BGP_PEER_RESTARTING_MODE(peer
))
1762 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1764 * update and withdraw NLRI lengths are 0.
1766 if (!attribute_len
) {
1768 safi
= SAFI_UNICAST
;
1769 } else if (attr
.flag
& ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
)
1770 && nlris
[NLRI_MP_WITHDRAW
].length
== 0) {
1771 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1772 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1773 } else if (attr_parse_ret
== BGP_ATTR_PARSE_EOR
) {
1774 afi
= nlris
[NLRI_MP_UPDATE
].afi
;
1775 safi
= nlris
[NLRI_MP_UPDATE
].safi
;
1778 if (afi
&& peer
->afc
[afi
][safi
]) {
1779 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1781 /* End-of-RIB received */
1782 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1783 PEER_STATUS_EOR_RECEIVED
)) {
1784 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1785 PEER_STATUS_EOR_RECEIVED
);
1786 bgp_update_explicit_eors(peer
);
1787 /* Update graceful restart information */
1788 gr_info
= &(peer
->bgp
->gr_info
[afi
][safi
]);
1790 gr_info
->eor_received
++;
1791 /* If EOR received from all peers and selection
1792 * deferral timer is running, cancel the timer
1793 * and invoke the best path calculation
1795 if (gr_info
->eor_required
1796 == gr_info
->eor_received
) {
1797 if (bgp_debug_neighbor_events(peer
))
1801 gr_info
->eor_required
,
1803 gr_info
->eor_received
);
1805 gr_info
->t_select_deferral
);
1806 gr_info
->eor_required
= 0;
1807 gr_info
->eor_received
= 0;
1808 /* Best path selection */
1809 if (bgp_best_path_select_defer(
1810 peer
->bgp
, afi
, safi
)
1816 /* NSF delete stale route */
1817 if (peer
->nsf
[afi
][safi
])
1818 bgp_clear_stale_route(peer
, afi
, safi
);
1821 "%s: rcvd End-of-RIB for %s from %s in vrf %s",
1822 __func__
, get_afi_safi_str(afi
, safi
, false),
1823 peer
->host
, vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
1827 /* Everything is done. We unintern temporary structures which
1828 interned in bgp_attr_parse(). */
1829 bgp_attr_unintern_sub(&attr
);
1831 peer
->update_time
= bgp_clock();
1833 /* Notify BGP Conditional advertisement scanner process */
1834 peer
->advmap_table_change
= true;
1836 return Receive_UPDATE_message
;
1840 * Process BGP NOTIFY message for peer.
1843 * @param size size of the packet
1844 * @return as in summary
1846 static int bgp_notify_receive(struct peer
*peer
, bgp_size_t size
)
1848 struct bgp_notify bgp_notify
;
1850 if (peer
->notify
.data
) {
1851 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1852 peer
->notify
.length
= 0;
1855 bgp_notify
.code
= stream_getc(peer
->curr
);
1856 bgp_notify
.subcode
= stream_getc(peer
->curr
);
1857 bgp_notify
.length
= size
- 2;
1858 bgp_notify
.data
= NULL
;
1859 bgp_notify
.raw_data
= NULL
;
1861 /* Preserv notify code and sub code. */
1862 peer
->notify
.code
= bgp_notify
.code
;
1863 peer
->notify
.subcode
= bgp_notify
.subcode
;
1864 /* For further diagnostic record returned Data. */
1865 if (bgp_notify
.length
) {
1866 peer
->notify
.length
= size
- 2;
1867 peer
->notify
.data
= XMALLOC(MTYPE_TMP
, size
- 2);
1868 memcpy(peer
->notify
.data
, stream_pnt(peer
->curr
), size
- 2);
1877 if (bgp_notify
.length
) {
1879 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
1880 for (i
= 0; i
< bgp_notify
.length
; i
++)
1882 snprintf(c
, sizeof(c
), " %02x",
1883 stream_getc(peer
->curr
));
1885 strlcat(bgp_notify
.data
, c
,
1886 bgp_notify
.length
* 3);
1890 snprintf(c
, sizeof(c
), "%02x",
1891 stream_getc(peer
->curr
));
1893 strlcpy(bgp_notify
.data
, c
,
1894 bgp_notify
.length
* 3);
1896 bgp_notify
.raw_data
= (uint8_t *)peer
->notify
.data
;
1899 bgp_notify_print(peer
, &bgp_notify
, "received");
1900 if (bgp_notify
.data
) {
1901 XFREE(MTYPE_TMP
, bgp_notify
.data
);
1902 bgp_notify
.length
= 0;
1906 /* peer count update */
1907 atomic_fetch_add_explicit(&peer
->notify_in
, 1, memory_order_relaxed
);
1909 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1911 /* We have to check for Notify with Unsupported Optional Parameter.
1912 in that case we fallback to open without the capability option.
1913 But this done in bgp_stop. We just mark it here to avoid changing
1915 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
1916 && bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1917 UNSET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1919 bgp_peer_gr_flags_update(peer
);
1920 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
1923 return Receive_NOTIFICATION_message
;
1927 * Process BGP ROUTEREFRESH message for peer.
1930 * @param size size of the packet
1931 * @return as in summary
1933 static int bgp_route_refresh_receive(struct peer
*peer
, bgp_size_t size
)
1937 iana_safi_t pkt_safi
;
1940 struct peer_af
*paf
;
1941 struct update_group
*updgrp
;
1942 struct peer
*updgrp_peer
;
1944 bgp_size_t msg_length
=
1945 size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
);
1947 /* If peer does not have the capability, send notification. */
1948 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_ADV
)) {
1949 flog_err(EC_BGP_NO_CAP
,
1950 "%s [Error] BGP route refresh is not enabled",
1952 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
1953 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1957 /* Status must be Established. */
1958 if (!peer_established(peer
)) {
1960 EC_BGP_INVALID_STATUS
,
1961 "%s [Error] Route refresh packet received under status %s",
1963 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1964 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1965 bgp_fsm_error_subcode(peer
->status
));
1972 pkt_afi
= stream_getw(s
);
1973 subtype
= stream_getc(s
);
1974 pkt_safi
= stream_getc(s
);
1976 /* Convert AFI, SAFI to internal values and check. */
1977 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1979 "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
1980 peer
->host
, iana_afi2str(pkt_afi
),
1981 iana_safi2str(pkt_safi
));
1982 return BGP_PACKET_NOOP
;
1985 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) {
1987 uint8_t when_to_refresh
;
1992 /* If the length, excluding the fixed-size message
1993 * header, of the received ROUTE-REFRESH message with
1994 * Message Subtype 1 and 2 is not 4, then the BGP
1995 * speaker MUST send a NOTIFICATION message with the
1996 * Error Code of "ROUTE-REFRESH Message Error" and the
1997 * subcode of "Invalid Message Length".
1999 if (msg_length
!= 4) {
2001 "%s Enhanced Route Refresh message length error",
2004 peer
, BGP_NOTIFY_ROUTE_REFRESH_ERR
,
2005 BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN
);
2008 /* When the BGP speaker receives a ROUTE-REFRESH message
2009 * with a "Message Subtype" field other than 0, 1, or 2,
2010 * it MUST ignore the received ROUTE-REFRESH message.
2014 "%s Enhanced Route Refresh invalid subtype",
2018 if (msg_length
< 5) {
2019 zlog_info("%s ORF route refresh length error",
2021 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2022 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2026 when_to_refresh
= stream_getc(s
);
2027 end
= stream_pnt(s
) + (size
- 5);
2029 while ((stream_pnt(s
) + 2) < end
) {
2030 orf_type
= stream_getc(s
);
2031 orf_len
= stream_getw(s
);
2033 /* orf_len in bounds? */
2034 if ((stream_pnt(s
) + orf_len
) > end
)
2035 break; /* XXX: Notify instead?? */
2036 if (orf_type
== ORF_TYPE_PREFIX
2037 || orf_type
== ORF_TYPE_PREFIX_OLD
) {
2038 uint8_t *p_pnt
= stream_pnt(s
);
2039 uint8_t *p_end
= stream_pnt(s
) + orf_len
;
2040 struct orf_prefix orfp
;
2045 int ret
= CMD_SUCCESS
;
2047 if (bgp_debug_neighbor_events(peer
)) {
2049 "%s rcvd Prefixlist ORF(%d) length %d",
2050 peer
->host
, orf_type
, orf_len
);
2053 /* we're going to read at least 1 byte of common
2055 * and 7 bytes of ORF Address-filter entry from
2061 /* ORF prefix-list name */
2062 snprintf(name
, sizeof(name
), "%s.%d.%d",
2063 peer
->host
, afi
, safi
);
2065 while (p_pnt
< p_end
) {
2066 /* If the ORF entry is malformed, want
2067 * to read as much of it
2068 * as possible without going beyond the
2069 * bounds of the entry,
2070 * to maximise debug information.
2074 sizeof(struct orf_prefix
));
2076 /* after ++: p_pnt <= p_end */
2078 & ORF_COMMON_PART_REMOVE_ALL
) {
2079 if (bgp_debug_neighbor_events(
2082 "%s rcvd Remove-All pfxlist ORF request",
2084 prefix_bgp_orf_remove_all(afi
,
2088 ok
= ((uint32_t)(p_end
- p_pnt
)
2089 >= sizeof(uint32_t));
2093 p_pnt
+= sizeof(uint32_t);
2094 orfp
.seq
= ntohl(seq
);
2098 /* val checked in prefix_bgp_orf_set */
2102 /* val checked in prefix_bgp_orf_set */
2106 if ((ok
= (p_pnt
< p_end
)))
2107 orfp
.p
.prefixlen
= *p_pnt
++;
2109 /* afi checked already */
2110 orfp
.p
.family
= afi2family(afi
);
2113 psize
= PSIZE(orfp
.p
.prefixlen
);
2114 /* valid for family ? */
2115 if (psize
> prefix_blen(&orfp
.p
)) {
2117 psize
= prefix_blen(&orfp
.p
);
2119 /* valid for packet ? */
2120 if (psize
> (p_end
- p_pnt
)) {
2122 psize
= p_end
- p_pnt
;
2126 memcpy(&orfp
.p
.u
.prefix
, p_pnt
,
2130 if (bgp_debug_neighbor_events(peer
)) {
2131 char buf
[INET6_BUFSIZ
];
2134 "%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
2136 (common
& ORF_COMMON_PART_REMOVE
2139 (common
& ORF_COMMON_PART_DENY
2150 ok
? "" : " MALFORMED");
2154 ret
= prefix_bgp_orf_set(
2156 (common
& ORF_COMMON_PART_DENY
2159 (common
& ORF_COMMON_PART_REMOVE
2163 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
)) {
2165 "%s Received misformatted prefixlist ORF. Remove All pfxlist",
2167 prefix_bgp_orf_remove_all(afi
,
2173 peer
->orf_plist
[afi
][safi
] =
2174 prefix_bgp_orf_lookup(afi
, name
);
2176 stream_forward_getp(s
, orf_len
);
2178 if (bgp_debug_neighbor_events(peer
))
2179 zlog_debug("%s rcvd Refresh %s ORF request", peer
->host
,
2180 when_to_refresh
== REFRESH_DEFER
2183 if (when_to_refresh
== REFRESH_DEFER
)
2184 return BGP_PACKET_NOOP
;
2187 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2188 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2189 PEER_STATUS_ORF_WAIT_REFRESH
))
2190 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2191 PEER_STATUS_ORF_WAIT_REFRESH
);
2193 paf
= peer_af_find(peer
, afi
, safi
);
2194 if (paf
&& paf
->subgroup
) {
2195 if (peer
->orf_plist
[afi
][safi
]) {
2196 updgrp
= PAF_UPDGRP(paf
);
2197 updgrp_peer
= UPDGRP_PEER(updgrp
);
2198 updgrp_peer
->orf_plist
[afi
][safi
] =
2199 peer
->orf_plist
[afi
][safi
];
2202 /* Avoid supressing duplicate routes later
2203 * when processing in subgroup_announce_table().
2205 SET_FLAG(paf
->subgroup
->sflags
, SUBGRP_STATUS_FORCE_UPDATES
);
2207 /* If the peer is configured for default-originate clear the
2208 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
2212 if (CHECK_FLAG(paf
->subgroup
->sflags
,
2213 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
2214 UNSET_FLAG(paf
->subgroup
->sflags
,
2215 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
2218 if (subtype
== BGP_ROUTE_REFRESH_BORR
) {
2219 /* A BGP speaker that has received the Graceful Restart
2220 * Capability from its neighbor MUST ignore any BoRRs for
2221 * an <AFI, SAFI> from the neighbor before the speaker
2222 * receives the EoR for the given <AFI, SAFI> from the
2225 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)
2226 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2227 PEER_STATUS_EOR_RECEIVED
)) {
2228 if (bgp_debug_neighbor_events(peer
))
2230 "%s rcvd route-refresh (BoRR) for %s/%s before EoR",
2231 peer
->host
, afi2str(afi
),
2233 return BGP_PACKET_NOOP
;
2236 if (peer
->t_refresh_stalepath
) {
2237 if (bgp_debug_neighbor_events(peer
))
2239 "%s rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
2240 peer
->host
, afi2str(afi
),
2242 return BGP_PACKET_NOOP
;
2245 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_BORR_RECEIVED
);
2246 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2247 PEER_STATUS_EORR_RECEIVED
);
2249 /* When a BGP speaker receives a BoRR message from
2250 * a peer, it MUST mark all the routes with the given
2251 * Address Family Identifier and Subsequent Address
2252 * Family Identifier, <AFI, SAFI> [RFC2918], from
2253 * that peer as stale.
2255 if (peer_active_nego(peer
)) {
2256 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2257 PEER_STATUS_ENHANCED_REFRESH
);
2258 bgp_set_stale_route(peer
, afi
, safi
);
2261 if (peer_established(peer
))
2262 thread_add_timer(bm
->master
,
2263 bgp_refresh_stalepath_timer_expire
,
2264 paf
, peer
->bgp
->stalepath_time
,
2265 &peer
->t_refresh_stalepath
);
2267 if (bgp_debug_neighbor_events(peer
))
2269 "%s rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
2270 peer
->host
, afi2str(afi
), safi2str(safi
),
2271 peer
->bgp
->stalepath_time
);
2272 } else if (subtype
== BGP_ROUTE_REFRESH_EORR
) {
2273 if (!peer
->t_refresh_stalepath
) {
2275 "%s rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
2276 peer
->host
, afi2str(afi
), safi2str(safi
));
2277 return BGP_PACKET_NOOP
;
2280 BGP_TIMER_OFF(peer
->t_refresh_stalepath
);
2282 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_EORR_RECEIVED
);
2283 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2284 PEER_STATUS_BORR_RECEIVED
);
2286 if (bgp_debug_neighbor_events(peer
))
2288 "%s rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
2289 peer
->host
, afi2str(afi
), safi2str(safi
));
2291 if (peer
->nsf
[afi
][safi
])
2292 bgp_clear_stale_route(peer
, afi
, safi
);
2294 if (bgp_debug_neighbor_events(peer
))
2295 zlog_debug("%s rcvd route-refresh (REQUEST) for %s/%s",
2296 peer
->host
, afi2str(afi
), safi2str(safi
));
2298 /* In response to a "normal route refresh request" from the
2299 * peer, the speaker MUST send a BoRR message.
2301 if (CHECK_FLAG(peer
->cap
, PEER_CAP_ENHANCED_RR_RCV
)) {
2302 /* For a BGP speaker that supports the BGP Graceful
2303 * Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
2304 * to a neighbor before it sends the EoR for the
2305 * <AFI, SAFI> to the neighbor.
2307 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2308 PEER_STATUS_EOR_SEND
)) {
2309 if (bgp_debug_neighbor_events(peer
))
2311 "%s rcvd route-refresh (REQUEST) for %s/%s before EoR",
2312 peer
->host
, afi2str(afi
),
2314 return BGP_PACKET_NOOP
;
2317 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
2318 BGP_ROUTE_REFRESH_BORR
);
2320 if (bgp_debug_neighbor_events(peer
))
2322 "%s sending route-refresh (BoRR) for %s/%s",
2323 peer
->host
, afi2str(afi
),
2326 /* Set flag Ready-To-Send to know when we can send EoRR
2329 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2330 PEER_STATUS_BORR_SEND
);
2331 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2332 PEER_STATUS_EORR_SEND
);
2336 /* Perform route refreshment to the peer */
2337 bgp_announce_route(peer
, afi
, safi
);
2339 /* No FSM action necessary */
2340 return BGP_PACKET_NOOP
;
2344 * Parse BGP CAPABILITY message for peer.
2347 * @param size size of the packet
2348 * @return as in summary
2350 static int bgp_capability_msg_parse(struct peer
*peer
, uint8_t *pnt
,
2354 struct capability_mp_data mpc
;
2355 struct capability_header
*hdr
;
2359 iana_safi_t pkt_safi
;
2365 /* We need at least action, capability code and capability
2367 if (pnt
+ 3 > end
) {
2368 zlog_info("%s Capability length error", peer
->host
);
2369 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2370 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2374 hdr
= (struct capability_header
*)(pnt
+ 1);
2376 /* Action value check. */
2377 if (action
!= CAPABILITY_ACTION_SET
2378 && action
!= CAPABILITY_ACTION_UNSET
) {
2379 zlog_info("%s Capability Action Value error %d",
2380 peer
->host
, action
);
2381 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2382 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2386 if (bgp_debug_neighbor_events(peer
))
2388 "%s CAPABILITY has action: %d, code: %u, length %u",
2389 peer
->host
, action
, hdr
->code
, hdr
->length
);
2391 /* Capability length check. */
2392 if ((pnt
+ hdr
->length
+ 3) > end
) {
2393 zlog_info("%s Capability length error", peer
->host
);
2394 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2395 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2399 /* Fetch structure to the byte stream. */
2400 memcpy(&mpc
, pnt
+ 3, sizeof(struct capability_mp_data
));
2401 pnt
+= hdr
->length
+ 3;
2403 /* We know MP Capability Code. */
2404 if (hdr
->code
== CAPABILITY_CODE_MP
) {
2405 pkt_afi
= ntohs(mpc
.afi
);
2406 pkt_safi
= mpc
.safi
;
2408 /* Ignore capability when override-capability is set. */
2409 if (CHECK_FLAG(peer
->flags
,
2410 PEER_FLAG_OVERRIDE_CAPABILITY
))
2413 /* Convert AFI, SAFI to internal values. */
2414 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
,
2416 if (bgp_debug_neighbor_events(peer
))
2418 "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)",
2420 iana_afi2str(pkt_afi
),
2421 iana_safi2str(pkt_safi
));
2425 /* Address family check. */
2426 if (bgp_debug_neighbor_events(peer
))
2428 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
2430 action
== CAPABILITY_ACTION_SET
2433 iana_afi2str(pkt_afi
),
2434 iana_safi2str(pkt_safi
));
2436 if (action
== CAPABILITY_ACTION_SET
) {
2437 peer
->afc_recv
[afi
][safi
] = 1;
2438 if (peer
->afc
[afi
][safi
]) {
2439 peer
->afc_nego
[afi
][safi
] = 1;
2440 bgp_announce_route(peer
, afi
, safi
);
2443 peer
->afc_recv
[afi
][safi
] = 0;
2444 peer
->afc_nego
[afi
][safi
] = 0;
2446 if (peer_active_nego(peer
))
2447 bgp_clear_route(peer
, afi
, safi
);
2453 EC_BGP_UNRECOGNIZED_CAPABILITY
,
2454 "%s unrecognized capability code: %d - ignored",
2455 peer
->host
, hdr
->code
);
2459 /* No FSM action necessary */
2460 return BGP_PACKET_NOOP
;
2464 * Parse BGP CAPABILITY message for peer.
2466 * Exported for unit testing.
2469 * @param size size of the packet
2470 * @return as in summary
2472 int bgp_capability_receive(struct peer
*peer
, bgp_size_t size
)
2476 /* Fetch pointer. */
2477 pnt
= stream_pnt(peer
->curr
);
2479 if (bgp_debug_neighbor_events(peer
))
2480 zlog_debug("%s rcv CAPABILITY", peer
->host
);
2482 /* If peer does not have the capability, send notification. */
2483 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_ADV
)) {
2484 flog_err(EC_BGP_NO_CAP
,
2485 "%s [Error] BGP dynamic capability is not enabled",
2487 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2488 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2492 /* Status must be Established. */
2493 if (!peer_established(peer
)) {
2496 "%s [Error] Dynamic capability packet received under status %s",
2498 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2499 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
2500 bgp_fsm_error_subcode(peer
->status
));
2505 return bgp_capability_msg_parse(peer
, pnt
, size
);
2509 * Processes a peer's input buffer.
2511 * This function sidesteps the event loop and directly calls bgp_event_update()
2512 * after processing each BGP message. This is necessary to ensure proper
2513 * ordering of FSM events and unifies the behavior that was present previously,
2514 * whereby some of the packet handling functions would update the FSM and some
2515 * would not, making event flow difficult to understand. Please think twice
2516 * before hacking this.
2518 * Thread type: THREAD_EVENT
2522 int bgp_process_packet(struct thread
*thread
)
2524 /* Yes first of all get peer pointer. */
2525 struct peer
*peer
; // peer
2526 uint32_t rpkt_quanta_old
; // how many packets to read
2527 int fsm_update_result
; // return code of bgp_event_update()
2528 int mprc
; // message processing return code
2530 peer
= THREAD_ARG(thread
);
2531 rpkt_quanta_old
= atomic_load_explicit(&peer
->bgp
->rpkt_quanta
,
2532 memory_order_relaxed
);
2533 fsm_update_result
= 0;
2535 /* Guard against scheduled events that occur after peer deletion. */
2536 if (peer
->status
== Deleted
|| peer
->status
== Clearing
)
2539 unsigned int processed
= 0;
2541 while (processed
< rpkt_quanta_old
) {
2544 char notify_data_length
[2];
2546 frr_with_mutex(&peer
->io_mtx
) {
2547 peer
->curr
= stream_fifo_pop(peer
->ibuf
);
2550 if (peer
->curr
== NULL
) // no packets to process, hmm...
2553 /* skip the marker and copy the packet length */
2554 stream_forward_getp(peer
->curr
, BGP_MARKER_SIZE
);
2555 memcpy(notify_data_length
, stream_pnt(peer
->curr
), 2);
2557 /* read in the packet length and type */
2558 size
= stream_getw(peer
->curr
);
2559 type
= stream_getc(peer
->curr
);
2561 hook_call(bgp_packet_dump
, peer
, type
, size
, peer
->curr
);
2563 /* adjust size to exclude the marker + length + type */
2564 size
-= BGP_HEADER_SIZE
;
2566 /* Read rest of the packet and call each sort of packet routine
2570 frrtrace(2, frr_bgp
, open_process
, peer
, size
);
2571 atomic_fetch_add_explicit(&peer
->open_in
, 1,
2572 memory_order_relaxed
);
2573 mprc
= bgp_open_receive(peer
, size
);
2574 if (mprc
== BGP_Stop
)
2577 "%s: BGP OPEN receipt failed for peer: %s",
2578 __func__
, peer
->host
);
2580 case BGP_MSG_UPDATE
:
2581 frrtrace(2, frr_bgp
, update_process
, peer
, size
);
2582 atomic_fetch_add_explicit(&peer
->update_in
, 1,
2583 memory_order_relaxed
);
2584 peer
->readtime
= monotime(NULL
);
2585 mprc
= bgp_update_receive(peer
, size
);
2586 if (mprc
== BGP_Stop
)
2589 "%s: BGP UPDATE receipt failed for peer: %s",
2590 __func__
, peer
->host
);
2592 case BGP_MSG_NOTIFY
:
2593 frrtrace(2, frr_bgp
, notification_process
, peer
, size
);
2594 atomic_fetch_add_explicit(&peer
->notify_in
, 1,
2595 memory_order_relaxed
);
2596 mprc
= bgp_notify_receive(peer
, size
);
2597 if (mprc
== BGP_Stop
)
2600 "%s: BGP NOTIFY receipt failed for peer: %s",
2601 __func__
, peer
->host
);
2603 case BGP_MSG_KEEPALIVE
:
2604 frrtrace(2, frr_bgp
, keepalive_process
, peer
, size
);
2605 peer
->readtime
= monotime(NULL
);
2606 atomic_fetch_add_explicit(&peer
->keepalive_in
, 1,
2607 memory_order_relaxed
);
2608 mprc
= bgp_keepalive_receive(peer
, size
);
2609 if (mprc
== BGP_Stop
)
2612 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2613 __func__
, peer
->host
);
2615 case BGP_MSG_ROUTE_REFRESH_NEW
:
2616 case BGP_MSG_ROUTE_REFRESH_OLD
:
2617 frrtrace(2, frr_bgp
, refresh_process
, peer
, size
);
2618 atomic_fetch_add_explicit(&peer
->refresh_in
, 1,
2619 memory_order_relaxed
);
2620 mprc
= bgp_route_refresh_receive(peer
, size
);
2621 if (mprc
== BGP_Stop
)
2624 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2625 __func__
, peer
->host
);
2627 case BGP_MSG_CAPABILITY
:
2628 frrtrace(2, frr_bgp
, capability_process
, peer
, size
);
2629 atomic_fetch_add_explicit(&peer
->dynamic_cap_in
, 1,
2630 memory_order_relaxed
);
2631 mprc
= bgp_capability_receive(peer
, size
);
2632 if (mprc
== BGP_Stop
)
2635 "%s: BGP CAPABILITY receipt failed for peer: %s",
2636 __func__
, peer
->host
);
2639 /* Suppress uninitialized variable warning */
2643 * The message type should have been sanitized before
2644 * we ever got here. Receipt of a message with an
2645 * invalid header at this point is indicative of a
2648 assert (!"Message of invalid type received during input processing");
2651 /* delete processed packet */
2652 stream_free(peer
->curr
);
2657 if (mprc
!= BGP_PACKET_NOOP
)
2658 fsm_update_result
= bgp_event_update(peer
, mprc
);
2663 * If peer was deleted, do not process any more packets. This
2664 * is usually due to executing BGP_Stop or a stub deletion.
2666 if (fsm_update_result
== FSM_PEER_TRANSFERRED
2667 || fsm_update_result
== FSM_PEER_STOPPED
)
2671 if (fsm_update_result
!= FSM_PEER_TRANSFERRED
2672 && fsm_update_result
!= FSM_PEER_STOPPED
) {
2673 frr_with_mutex(&peer
->io_mtx
) {
2674 // more work to do, come back later
2675 if (peer
->ibuf
->count
> 0)
2677 bm
->master
, bgp_process_packet
, peer
, 0,
2678 &peer
->t_process_packet
);
2685 /* Send EOR when routes are processed by selection deferral timer */
2686 void bgp_send_delayed_eor(struct bgp
*bgp
)
2689 struct listnode
*node
, *nnode
;
2691 /* EOR message sent in bgp_write_proceed_actions */
2692 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
2693 bgp_write_proceed_actions(peer
);
2697 * Task callback to handle socket error encountered in the io pthread. We avoid
2698 * having the io pthread try to enqueue fsm events or mess with the peer
2701 int bgp_packet_process_error(struct thread
*thread
)
2706 peer
= THREAD_ARG(thread
);
2707 code
= THREAD_VAL(thread
);
2709 if (bgp_debug_neighbor_events(peer
))
2710 zlog_debug("%s [Event] BGP error %d on fd %d",
2711 peer
->host
, peer
->fd
, code
);
2713 /* Closed connection or error on the socket */
2714 if (peer_established(peer
)) {
2715 if ((CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
2716 || CHECK_FLAG(peer
->flags
,
2717 PEER_FLAG_GRACEFUL_RESTART_HELPER
))
2718 && CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)) {
2719 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2720 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2722 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2725 bgp_event_update(peer
, code
);