1 /* BGP packet management routine.
2 * Contains utility functions for constructing and consuming BGP messages.
3 * Copyright (C) 2017 Cumulus Networks
4 * Copyright (C) 1999 Kunihiro Ishiguro
6 * This file is part of GNU Zebra.
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h" /* for inet_ntop () */
39 #include "lib_errors.h"
41 #include "bgpd/bgpd.h"
42 #include "bgpd/bgp_table.h"
43 #include "bgpd/bgp_dump.h"
44 #include "bgpd/bgp_bmp.h"
45 #include "bgpd/bgp_attr.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_errors.h"
48 #include "bgpd/bgp_fsm.h"
49 #include "bgpd/bgp_route.h"
50 #include "bgpd/bgp_packet.h"
51 #include "bgpd/bgp_open.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_community.h"
54 #include "bgpd/bgp_ecommunity.h"
55 #include "bgpd/bgp_lcommunity.h"
56 #include "bgpd/bgp_network.h"
57 #include "bgpd/bgp_mplsvpn.h"
58 #include "bgpd/bgp_evpn.h"
59 #include "bgpd/bgp_advertise.h"
60 #include "bgpd/bgp_vty.h"
61 #include "bgpd/bgp_updgrp.h"
62 #include "bgpd/bgp_label.h"
63 #include "bgpd/bgp_io.h"
64 #include "bgpd/bgp_keepalives.h"
65 #include "bgpd/bgp_flowspec.h"
66 #include "bgpd/bgp_trace.h"
68 DEFINE_HOOK(bgp_packet_dump
,
69 (struct peer
*peer
, uint8_t type
, bgp_size_t size
,
71 (peer
, type
, size
, s
));
73 DEFINE_HOOK(bgp_packet_send
,
74 (struct peer
*peer
, uint8_t type
, bgp_size_t size
,
76 (peer
, type
, size
, s
));
79 * Sets marker and type fields for a BGP message.
81 * @param s the stream containing the packet
82 * @param type the packet type
83 * @return the size of the stream
85 int bgp_packet_set_marker(struct stream
*s
, uint8_t type
)
90 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
93 /* Dummy total length. This field is should be filled in later on. */
96 /* BGP packet type. */
99 /* Return current stream size. */
100 return stream_get_endp(s
);
104 * Sets size field for a BGP message.
106 * Size field is set to the size of the stream passed.
108 * @param s the stream containing the packet
110 void bgp_packet_set_size(struct stream
*s
)
114 /* Preserve current pointer. */
115 cp
= stream_get_endp(s
);
116 stream_putw_at(s
, BGP_MARKER_SIZE
, cp
);
120 * Push a packet onto the beginning of the peer's output queue.
121 * This function acquires the peer's write mutex before proceeding.
123 static void bgp_packet_add(struct peer
*peer
, struct stream
*s
)
125 frr_with_mutex(&peer
->io_mtx
) {
126 stream_fifo_push(peer
->obuf
, s
);
130 static struct stream
*bgp_update_packet_eor(struct peer
*peer
, afi_t afi
,
135 iana_safi_t pkt_safi
;
137 if (DISABLE_BGP_ANNOUNCE
)
140 if (bgp_debug_neighbor_events(peer
))
141 zlog_debug("send End-of-RIB for %s to %s",
142 get_afi_safi_str(afi
, safi
, false), peer
->host
);
144 s
= stream_new(peer
->max_packet_size
);
146 /* Make BGP update packet. */
147 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
149 /* Unfeasible Routes Length */
152 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
153 /* Total Path Attribute Length */
156 /* Convert AFI, SAFI to values for packet. */
157 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
159 /* Total Path Attribute Length */
161 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
162 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
164 stream_putw(s
, pkt_afi
);
165 stream_putc(s
, pkt_safi
);
168 bgp_packet_set_size(s
);
172 /* Called when there is a change in the EOR(implicit or explicit) status of a
173 * peer. Ends the update-delay if all expected peers are done with EORs. */
174 void bgp_check_update_delay(struct bgp
*bgp
)
176 struct listnode
*node
, *nnode
;
177 struct peer
*peer
= NULL
;
179 if (bgp_debug_neighbor_events(peer
))
180 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
181 bgp
->established
, bgp
->restarted_peers
,
182 bgp
->implicit_eors
, bgp
->explicit_eors
);
185 <= bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
) {
187 * This is an extra sanity check to make sure we wait for all
188 * the eligible configured peers. This check is performed if
189 * establish wait timer is on, or establish wait option is not
190 * given with the update-delay command
192 if (bgp
->t_establish_wait
193 || (bgp
->v_establish_wait
== bgp
->v_update_delay
))
194 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
195 if (CHECK_FLAG(peer
->flags
,
196 PEER_FLAG_CONFIG_NODE
)
197 && !CHECK_FLAG(peer
->flags
,
199 && !CHECK_FLAG(peer
->bgp
->flags
,
201 && !peer
->update_delay_over
) {
202 if (bgp_debug_neighbor_events(peer
))
204 " Peer %s pending, continuing read-only mode",
211 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
212 bgp
->restarted_peers
, bgp
->implicit_eors
,
214 bgp_update_delay_end(bgp
);
219 * Called if peer is known to have restarted. The restart-state bit in
220 * Graceful-Restart capability is used for that
222 void bgp_update_restarted_peers(struct peer
*peer
)
224 if (!bgp_update_delay_active(peer
->bgp
))
225 return; /* BGP update delay has ended */
226 if (peer
->update_delay_over
)
227 return; /* This peer has already been considered */
229 if (bgp_debug_neighbor_events(peer
))
230 zlog_debug("Peer %s: Checking restarted", peer
->host
);
232 if (peer_established(peer
)) {
233 peer
->update_delay_over
= 1;
234 peer
->bgp
->restarted_peers
++;
235 bgp_check_update_delay(peer
->bgp
);
240 * Called as peer receives a keep-alive. Determines if this occurence can be
241 * taken as an implicit EOR for this peer.
242 * NOTE: The very first keep-alive after the Established state of a peer is
243 * considered implicit EOR for the update-delay purposes
245 void bgp_update_implicit_eors(struct peer
*peer
)
247 if (!bgp_update_delay_active(peer
->bgp
))
248 return; /* BGP update delay has ended */
249 if (peer
->update_delay_over
)
250 return; /* This peer has already been considered */
252 if (bgp_debug_neighbor_events(peer
))
253 zlog_debug("Peer %s: Checking implicit EORs", peer
->host
);
255 if (peer_established(peer
)) {
256 peer
->update_delay_over
= 1;
257 peer
->bgp
->implicit_eors
++;
258 bgp_check_update_delay(peer
->bgp
);
263 * Should be called only when there is a change in the EOR_RECEIVED status
264 * for any afi/safi on a peer.
266 static void bgp_update_explicit_eors(struct peer
*peer
)
271 if (!bgp_update_delay_active(peer
->bgp
))
272 return; /* BGP update delay has ended */
273 if (peer
->update_delay_over
)
274 return; /* This peer has already been considered */
276 if (bgp_debug_neighbor_events(peer
))
277 zlog_debug("Peer %s: Checking explicit EORs", peer
->host
);
279 FOREACH_AFI_SAFI (afi
, safi
) {
280 if (peer
->afc_nego
[afi
][safi
]
281 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
282 PEER_STATUS_EOR_RECEIVED
)) {
283 if (bgp_debug_neighbor_events(peer
))
285 " afi %d safi %d didn't receive EOR",
291 peer
->update_delay_over
= 1;
292 peer
->bgp
->explicit_eors
++;
293 bgp_check_update_delay(peer
->bgp
);
297 * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
299 * mp_withdraw, if set, is used to nullify attr structure on most of the
300 * calling safi function and for evpn, passed as parameter
302 int bgp_nlri_parse(struct peer
*peer
, struct attr
*attr
,
303 struct bgp_nlri
*packet
, int mp_withdraw
)
305 switch (packet
->safi
) {
308 return bgp_nlri_parse_ip(peer
, mp_withdraw
? NULL
: attr
,
310 case SAFI_LABELED_UNICAST
:
311 return bgp_nlri_parse_label(peer
, mp_withdraw
? NULL
: attr
,
314 return bgp_nlri_parse_vpn(peer
, mp_withdraw
? NULL
: attr
,
317 return bgp_nlri_parse_evpn(peer
, attr
, packet
, mp_withdraw
);
319 return bgp_nlri_parse_flowspec(peer
, attr
, packet
, mp_withdraw
);
321 return BGP_NLRI_PARSE_ERROR
;
325 * Checks a variety of conditions to determine whether the peer needs to be
326 * rescheduled for packet generation again, and does so if necessary.
328 * @param peer to check for rescheduling
330 static void bgp_write_proceed_actions(struct peer
*peer
)
335 struct bpacket
*next_pkt
;
336 struct update_subgroup
*subgrp
;
337 enum bgp_af_index index
;
339 for (index
= BGP_AF_START
; index
< BGP_AF_MAX
; index
++) {
340 paf
= peer
->peer_af_array
[index
];
344 subgrp
= paf
->subgroup
;
348 next_pkt
= paf
->next_pkt_to_send
;
349 if (next_pkt
&& next_pkt
->buffer
) {
350 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
351 bgp_generate_updgrp_packets
, 0);
355 /* No packets readily available for AFI/SAFI, are there
357 * that need to be generated? */
358 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
),
360 || subgroup_packets_to_build(subgrp
)) {
361 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
362 bgp_generate_updgrp_packets
, 0);
369 /* No packets to send, see if EOR is pending */
370 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
371 if (!subgrp
->t_coalesce
&& peer
->afc_nego
[afi
][safi
]
373 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
374 PEER_STATUS_EOR_SEND
)
375 && safi
!= SAFI_MPLS_VPN
) {
376 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
377 bgp_generate_updgrp_packets
, 0);
385 * Generate advertisement information (withdraws, updates, EOR) from each
386 * update group a peer belongs to, encode this information into packets, and
387 * enqueue the packets onto the peer's output buffer.
389 int bgp_generate_updgrp_packets(struct thread
*thread
)
391 struct peer
*peer
= THREAD_ARG(thread
);
395 struct bpacket
*next_pkt
;
397 uint32_t generated
= 0;
401 wpq
= atomic_load_explicit(&peer
->bgp
->wpkt_quanta
,
402 memory_order_relaxed
);
405 * The code beyond this part deals with update packets, proceed only
406 * if peer is Established and updates are not on hold (as part of
407 * update-delay processing).
409 if (!peer_established(peer
))
412 if ((peer
->bgp
->main_peers_update_hold
)
413 || bgp_update_delay_active(peer
->bgp
))
416 if (peer
->t_routeadv
)
420 enum bgp_af_index index
;
423 for (index
= BGP_AF_START
; index
< BGP_AF_MAX
; index
++) {
424 paf
= peer
->peer_af_array
[index
];
425 if (!paf
|| !PAF_SUBGRP(paf
))
430 next_pkt
= paf
->next_pkt_to_send
;
433 * Try to generate a packet for the peer if we are at
434 * the end of the list. Always try to push out
437 if (!next_pkt
|| !next_pkt
->buffer
) {
438 next_pkt
= subgroup_withdraw_packet(
440 if (!next_pkt
|| !next_pkt
->buffer
)
441 subgroup_update_packet(PAF_SUBGRP(paf
));
442 next_pkt
= paf
->next_pkt_to_send
;
446 * If we still don't have a packet to send to the peer,
447 * then try to find out out if we have to send eor or
448 * if not, skip to the next AFI, SAFI. Don't send the
449 * EOR prematurely; if the subgroup's coalesce timer is
450 * running, the adjacency-out structure is not created
453 if (!next_pkt
|| !next_pkt
->buffer
) {
454 if (!paf
->t_announce_route
) {
455 /* Make sure we supress BGP UPDATES
456 * for normal processing later again.
458 UNSET_FLAG(paf
->subgroup
->sflags
,
459 SUBGRP_STATUS_FORCE_UPDATES
);
461 /* If route-refresh BoRR message was
462 * already sent and we are done with
463 * re-announcing tables for a decent
464 * afi/safi, we ready to send
468 peer
->af_sflags
[afi
][safi
],
469 PEER_STATUS_BORR_SEND
)) {
470 bgp_route_refresh_send(
471 peer
, afi
, safi
, 0, 0,
473 BGP_ROUTE_REFRESH_EORR
);
475 SET_FLAG(peer
->af_sflags
[afi
]
477 PEER_STATUS_EORR_SEND
);
481 PEER_STATUS_BORR_SEND
);
483 if (bgp_debug_neighbor_events(
486 "%s sending route-refresh (EoRR) for %s/%s",
493 if (CHECK_FLAG(peer
->cap
,
494 PEER_CAP_RESTART_RCV
)) {
495 if (!(PAF_SUBGRP(paf
))->t_coalesce
496 && peer
->afc_nego
[afi
][safi
]
499 peer
->af_sflags
[afi
][safi
],
500 PEER_STATUS_EOR_SEND
)) {
501 /* If EOR is disabled,
502 * the message is not sent
504 if (BGP_SEND_EOR(peer
->bgp
, afi
,
510 PEER_STATUS_EOR_SEND
);
528 /* Update packet send time */
529 peer
->pkt_stime
[afi
][safi
] = monotime(NULL
);
531 /* Found a packet template to send, overwrite
532 * packet with appropriate attributes from peer
533 * and advance peer */
534 s
= bpacket_reformat_for_peer(next_pkt
, paf
);
535 bgp_packet_add(peer
, s
);
536 bpacket_queue_advance_peer(paf
);
538 } while (s
&& (++generated
< wpq
));
543 bgp_write_proceed_actions(peer
);
549 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
551 void bgp_keepalive_send(struct peer
*peer
)
555 s
= stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
);
557 /* Make keepalive packet. */
558 bgp_packet_set_marker(s
, BGP_MSG_KEEPALIVE
);
560 /* Set packet size. */
561 bgp_packet_set_size(s
);
563 /* Dump packet if debug option is set. */
564 /* bgp_packet_dump (s); */
566 if (bgp_debug_keepalive(peer
))
567 zlog_debug("%s sending KEEPALIVE", peer
->host
);
569 /* Add packet to the peer. */
570 bgp_packet_add(peer
, s
);
576 * Creates a BGP Open packet and appends it to the peer's output queue.
577 * Sets capabilities as necessary.
579 void bgp_open_send(struct peer
*peer
)
582 uint16_t send_holdtime
;
585 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
586 send_holdtime
= peer
->holdtime
;
588 send_holdtime
= peer
->bgp
->default_holdtime
;
590 /* local-as Change */
591 if (peer
->change_local_as
)
592 local_as
= peer
->change_local_as
;
594 local_as
= peer
->local_as
;
596 s
= stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
);
598 /* Make open packet. */
599 bgp_packet_set_marker(s
, BGP_MSG_OPEN
);
601 /* Set open packet values. */
602 stream_putc(s
, BGP_VERSION_4
); /* BGP version */
603 stream_putw(s
, (local_as
<= BGP_AS_MAX
) ? (uint16_t)local_as
605 stream_putw(s
, send_holdtime
); /* Hold Time */
606 stream_put_in_addr(s
, &peer
->local_id
); /* BGP Identifier */
608 /* Set capabilities */
609 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_EXTENDED_OPT_PARAMS
)) {
610 (void)bgp_open_capability(s
, peer
, true);
612 struct stream
*tmp
= stream_new(STREAM_SIZE(s
));
615 if (bgp_open_capability(tmp
, peer
, false)
616 > BGP_OPEN_NON_EXT_OPT_LEN
) {
618 (void)bgp_open_capability(s
, peer
, true);
625 /* Set BGP packet length. */
626 bgp_packet_set_size(s
);
628 if (bgp_debug_neighbor_events(peer
))
630 "%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4",
631 peer
->host
, BGP_VERSION_4
, local_as
, send_holdtime
,
634 /* Dump packet if debug option is set. */
635 /* bgp_packet_dump (s); */
636 hook_call(bgp_packet_send
, peer
, BGP_MSG_OPEN
, stream_get_endp(s
), s
);
638 /* Add packet to the peer. */
639 bgp_packet_add(peer
, s
);
645 * Writes NOTIFICATION message directly to a peer socket without waiting for
648 * There must be exactly one stream on the peer->obuf FIFO, and the data within
649 * this stream must match the format of a BGP NOTIFICATION message.
650 * Transmission is best-effort.
652 * @requires peer->io_mtx
656 static void bgp_write_notify(struct peer
*peer
)
662 /* There should be at least one packet. */
663 s
= stream_fifo_pop(peer
->obuf
);
668 assert(stream_get_endp(s
) >= BGP_HEADER_SIZE
);
670 /* Stop collecting data within the socket */
671 sockopt_cork(peer
->fd
, 0);
674 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
675 * we only care about getting a clean shutdown at this point.
677 ret
= write(peer
->fd
, STREAM_DATA(s
), stream_get_endp(s
));
680 * only connection reset/close gets counted as TCP_fatal_error, failure
681 * to write the entire NOTIFY doesn't get different FSM treatment
685 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
689 /* Disable Nagle, make NOTIFY packet go out right away */
691 (void)setsockopt(peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
,
694 /* Retrieve BGP packet type. */
695 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
696 type
= stream_getc(s
);
698 assert(type
== BGP_MSG_NOTIFY
);
700 /* Type should be notify. */
701 atomic_fetch_add_explicit(&peer
->notify_out
, 1, memory_order_relaxed
);
704 /* Double start timer. */
707 /* Overflow check. */
708 if (peer
->v_start
>= (60 * 2))
709 peer
->v_start
= (60 * 2);
712 * Handle Graceful Restart case where the state changes to
713 * Connect instead of Idle
715 BGP_EVENT_ADD(peer
, BGP_Stop
);
721 * Creates a BGP Notify and appends it to the peer's output queue.
723 * This function attempts to write the packet from the thread it is called
724 * from, to ensure the packet gets out ASAP.
726 * This function may be called from multiple threads. Since the function
727 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
728 * call to prevent tampering from other threads.
730 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
731 * return, the peer structure *must* be reset; no assumptions about session
735 * @param code BGP error code
736 * @param sub_code BGP error subcode
737 * @param data Data portion
738 * @param datalen length of data portion
740 void bgp_notify_send_with_data(struct peer
*peer
, uint8_t code
,
741 uint8_t sub_code
, uint8_t *data
, size_t datalen
)
745 /* Lock I/O mutex to prevent other threads from pushing packets */
746 frr_mutex_lock_autounlock(&peer
->io_mtx
);
747 /* ============================================== */
749 /* Allocate new stream. */
750 s
= stream_new(peer
->max_packet_size
);
752 /* Make notify packet. */
753 bgp_packet_set_marker(s
, BGP_MSG_NOTIFY
);
755 /* Set notify packet values. */
756 stream_putc(s
, code
); /* BGP notify code */
757 stream_putc(s
, sub_code
); /* BGP notify sub_code */
759 /* If notify data is present. */
761 stream_write(s
, data
, datalen
);
763 /* Set BGP packet length. */
764 bgp_packet_set_size(s
);
766 /* wipe output buffer */
767 stream_fifo_clean(peer
->obuf
);
770 * If possible, store last packet for debugging purposes. This check is
771 * in place because we are sometimes called with a doppelganger peer,
772 * who tends to have a plethora of fields nulled out.
775 size_t packetsize
= stream_get_endp(peer
->curr
);
776 assert(packetsize
<= peer
->max_packet_size
);
777 memcpy(peer
->last_reset_cause
, peer
->curr
->data
, packetsize
);
778 peer
->last_reset_cause_size
= packetsize
;
783 struct bgp_notify bgp_notify
;
788 bgp_notify
.code
= code
;
789 bgp_notify
.subcode
= sub_code
;
790 bgp_notify
.data
= NULL
;
791 bgp_notify
.length
= datalen
;
792 bgp_notify
.raw_data
= data
;
794 peer
->notify
.code
= bgp_notify
.code
;
795 peer
->notify
.subcode
= bgp_notify
.subcode
;
797 if (bgp_notify
.length
&& data
) {
799 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
800 for (i
= 0; i
< bgp_notify
.length
; i
++)
802 snprintf(c
, sizeof(c
), " %02x",
805 strlcat(bgp_notify
.data
, c
,
810 snprintf(c
, sizeof(c
), "%02x", data
[i
]);
812 strlcpy(bgp_notify
.data
, c
,
816 bgp_notify_print(peer
, &bgp_notify
, "sending");
818 if (bgp_notify
.data
) {
819 XFREE(MTYPE_TMP
, bgp_notify
.data
);
820 bgp_notify
.length
= 0;
824 /* peer reset cause */
825 if (code
== BGP_NOTIFY_CEASE
) {
826 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
827 peer
->last_reset
= PEER_DOWN_USER_RESET
;
828 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
829 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
831 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
833 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
835 /* Add packet to peer's output queue */
836 stream_fifo_push(peer
->obuf
, s
);
838 bgp_peer_gr_flags_update(peer
);
839 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
842 bgp_write_notify(peer
);
846 * Creates a BGP Notify and appends it to the peer's output queue.
848 * This function attempts to write the packet from the thread it is called
849 * from, to ensure the packet gets out ASAP.
852 * @param code BGP error code
853 * @param sub_code BGP error subcode
855 void bgp_notify_send(struct peer
*peer
, uint8_t code
, uint8_t sub_code
)
857 bgp_notify_send_with_data(peer
, code
, sub_code
, NULL
, 0);
861 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
864 * @param afi Address Family Identifier
865 * @param safi Subsequent Address Family Identifier
866 * @param orf_type Outbound Route Filtering type
867 * @param when_to_refresh Whether to refresh immediately or defer
868 * @param remove Whether to remove ORF for specified AFI/SAFI
870 void bgp_route_refresh_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
871 uint8_t orf_type
, uint8_t when_to_refresh
,
872 int remove
, uint8_t subtype
)
875 struct bgp_filter
*filter
;
878 iana_safi_t pkt_safi
;
880 if (DISABLE_BGP_ANNOUNCE
)
883 filter
= &peer
->filter
[afi
][safi
];
885 /* Convert AFI, SAFI to values for packet. */
886 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
888 s
= stream_new(peer
->max_packet_size
);
890 /* Make BGP update packet. */
891 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
892 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_NEW
);
894 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_OLD
);
896 /* Encode Route Refresh message. */
897 stream_putw(s
, pkt_afi
);
899 stream_putc(s
, subtype
);
902 stream_putc(s
, pkt_safi
);
904 if (orf_type
== ORF_TYPE_PREFIX
|| orf_type
== ORF_TYPE_PREFIX_OLD
)
905 if (remove
|| filter
->plist
[FILTER_IN
].plist
) {
910 stream_putc(s
, when_to_refresh
);
911 stream_putc(s
, orf_type
);
912 orfp
= stream_get_endp(s
);
916 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
917 PEER_STATUS_ORF_PREFIX_SEND
);
918 stream_putc(s
, ORF_COMMON_PART_REMOVE_ALL
);
919 if (bgp_debug_neighbor_events(peer
))
921 "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s",
922 peer
->host
, orf_type
,
923 (when_to_refresh
== REFRESH_DEFER
926 iana_afi2str(pkt_afi
),
927 iana_safi2str(pkt_safi
));
929 SET_FLAG(peer
->af_sflags
[afi
][safi
],
930 PEER_STATUS_ORF_PREFIX_SEND
);
931 prefix_bgp_orf_entry(
932 s
, filter
->plist
[FILTER_IN
].plist
,
934 ORF_COMMON_PART_PERMIT
,
935 ORF_COMMON_PART_DENY
);
936 if (bgp_debug_neighbor_events(peer
))
938 "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s",
939 peer
->host
, orf_type
,
940 (when_to_refresh
== REFRESH_DEFER
943 iana_afi2str(pkt_afi
),
944 iana_safi2str(pkt_safi
));
947 /* Total ORF Entry Len. */
948 orf_len
= stream_get_endp(s
) - orfp
- 2;
949 stream_putw_at(s
, orfp
, orf_len
);
952 /* Set packet size. */
953 bgp_packet_set_size(s
);
955 if (bgp_debug_neighbor_events(peer
)) {
957 zlog_debug("%s sending REFRESH_REQ for afi/safi: %s/%s",
958 peer
->host
, iana_afi2str(pkt_afi
),
959 iana_safi2str(pkt_safi
));
962 /* Add packet to the peer. */
963 bgp_packet_add(peer
, s
);
969 * Create a BGP Capability packet and append it to the peer's output queue.
972 * @param afi Address Family Identifier
973 * @param safi Subsequent Address Family Identifier
974 * @param capability_code BGP Capability Code
975 * @param action Set or Remove capability
977 void bgp_capability_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
978 int capability_code
, int action
)
982 iana_safi_t pkt_safi
;
984 /* Convert AFI, SAFI to values for packet. */
985 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
987 s
= stream_new(peer
->max_packet_size
);
989 /* Make BGP update packet. */
990 bgp_packet_set_marker(s
, BGP_MSG_CAPABILITY
);
992 /* Encode MP_EXT capability. */
993 if (capability_code
== CAPABILITY_CODE_MP
) {
994 stream_putc(s
, action
);
995 stream_putc(s
, CAPABILITY_CODE_MP
);
996 stream_putc(s
, CAPABILITY_CODE_MP_LEN
);
997 stream_putw(s
, pkt_afi
);
999 stream_putc(s
, pkt_safi
);
1001 if (bgp_debug_neighbor_events(peer
))
1003 "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
1005 action
== CAPABILITY_ACTION_SET
? "Advertising"
1007 iana_afi2str(pkt_afi
), iana_safi2str(pkt_safi
));
1010 /* Set packet size. */
1011 bgp_packet_set_size(s
);
1013 /* Add packet to the peer. */
1014 bgp_packet_add(peer
, s
);
1016 bgp_writes_on(peer
);
1019 /* RFC1771 6.8 Connection collision detection. */
1020 static int bgp_collision_detect(struct peer
*new, struct in_addr remote_id
)
1025 * Upon receipt of an OPEN message, the local system must examine
1026 * all of its connections that are in the OpenConfirm state. A BGP
1027 * speaker may also examine connections in an OpenSent state if it
1028 * knows the BGP Identifier of the peer by means outside of the
1029 * protocol. If among these connections there is a connection to a
1030 * remote BGP speaker whose BGP Identifier equals the one in the
1031 * OPEN message, then the local system performs the following
1032 * collision resolution procedure:
1034 peer
= new->doppelganger
;
1039 * Do not accept the new connection in Established or Clearing
1040 * states. Note that a peer GR is handled by closing the existing
1041 * connection upon receipt of new one.
1043 if (peer_established(peer
) || peer
->status
== Clearing
) {
1044 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1045 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1049 if ((peer
->status
!= OpenConfirm
) && (peer
->status
!= OpenSent
))
1053 * 1. The BGP Identifier of the local system is
1054 * compared to the BGP Identifier of the remote
1055 * system (as specified in the OPEN message).
1057 * If the BGP Identifiers of the peers
1058 * involved in the connection collision
1059 * are identical, then the connection
1060 * initiated by the BGP speaker with the
1061 * larger AS number is preserved.
1063 if (ntohl(peer
->local_id
.s_addr
) < ntohl(remote_id
.s_addr
)
1064 || (ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)
1065 && peer
->local_as
< peer
->as
))
1066 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
1068 * 2. If the value of the local BGP
1069 * Identifier is less than the remote one,
1070 * the local system closes BGP connection
1071 * that already exists (the one that is
1072 * already in the OpenConfirm state),
1073 * and accepts BGP connection initiated by
1074 * the remote system.
1076 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1077 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1080 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1081 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1085 if (ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)
1086 && peer
->local_as
== peer
->as
)
1087 flog_err(EC_BGP_ROUTER_ID_SAME
,
1088 "Peer's router-id %pI4 is the same as ours",
1092 * 3. Otherwise, the local system closes newly
1093 * created BGP connection (the one associated with the
1094 * newly received OPEN message), and continues to use
1095 * the existing one (the one that is already in the
1096 * OpenConfirm state).
1098 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
)) {
1099 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1100 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1103 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
1104 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1110 /* Packet processing routines ---------------------------------------------- */
1112 * This is a family of functions designed to be called from
1113 * bgp_process_packet(). These functions all share similar behavior and should
1114 * adhere to the following invariants and restrictions:
1118 * The return code of any one of those functions should be one of the FSM event
1119 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1120 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1121 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1122 * error and Receive_OPEN_message otherwise.
1124 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1129 * - May send NOTIFY messages
1130 * - May not modify peer->status
1131 * - May not call bgp_event_update()
1134 #define BGP_PACKET_NOOP 0
1137 * Process BGP OPEN message for peer.
1139 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1140 * and returns BGP_Stop.
1143 * @param size size of the packet
1144 * @return as in summary
1146 static int bgp_open_receive(struct peer
*peer
, bgp_size_t size
)
1152 uint16_t send_holdtime
;
1154 as_t as4
= 0, as4_be
;
1155 struct in_addr remote_id
;
1157 uint8_t notify_data_remote_as
[2];
1158 uint8_t notify_data_remote_as4
[4];
1159 uint8_t notify_data_remote_id
[4];
1160 uint16_t *holdtime_ptr
;
1162 /* Parse open packet. */
1163 version
= stream_getc(peer
->curr
);
1164 memcpy(notify_data_remote_as
, stream_pnt(peer
->curr
), 2);
1165 remote_as
= stream_getw(peer
->curr
);
1166 holdtime_ptr
= (uint16_t *)stream_pnt(peer
->curr
);
1167 holdtime
= stream_getw(peer
->curr
);
1168 memcpy(notify_data_remote_id
, stream_pnt(peer
->curr
), 4);
1169 remote_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1171 /* BEGIN to read the capability here, but dont do it yet */
1173 optlen
= stream_getc(peer
->curr
);
1175 /* Extended Optional Parameters Length for BGP OPEN Message */
1176 if (optlen
== BGP_OPEN_NON_EXT_OPT_LEN
1177 || CHECK_FLAG(peer
->flags
, PEER_FLAG_EXTENDED_OPT_PARAMS
)) {
1180 opttype
= stream_getc(peer
->curr
);
1181 if (opttype
== BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH
) {
1182 optlen
= stream_getw(peer
->curr
);
1183 SET_FLAG(peer
->sflags
,
1184 PEER_STATUS_EXT_OPT_PARAMS_LENGTH
);
1188 /* Receive OPEN message log */
1189 if (bgp_debug_neighbor_events(peer
))
1191 "%s rcv OPEN%s, version %d, remote-as (in open) %u, holdtime %d, id %pI4",
1193 CHECK_FLAG(peer
->sflags
,
1194 PEER_STATUS_EXT_OPT_PARAMS_LENGTH
)
1197 version
, remote_as
, holdtime
, &remote_id
);
1200 /* If not enough bytes, it is an error. */
1201 if (STREAM_READABLE(peer
->curr
) < optlen
) {
1202 flog_err(EC_BGP_PKT_OPEN
,
1203 "%s: stream has not enough bytes (%u)",
1204 peer
->host
, optlen
);
1205 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1206 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1210 /* We need the as4 capability value *right now* because
1211 * if it is there, we have not got the remote_as yet, and
1213 * that we do not know which peer is connecting to us now.
1215 as4
= peek_for_as4_capability(peer
, optlen
);
1218 as4_be
= htonl(as4
);
1219 memcpy(notify_data_remote_as4
, &as4_be
, 4);
1221 /* Just in case we have a silly peer who sends AS4 capability set to 0
1223 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
) {
1224 flog_err(EC_BGP_PKT_OPEN
,
1225 "%s bad OPEN, got AS4 capability, but AS4 set to 0",
1227 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1228 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1229 notify_data_remote_as4
, 4);
1233 /* Codification of AS 0 Processing */
1234 if (remote_as
== BGP_AS_ZERO
) {
1235 flog_err(EC_BGP_PKT_OPEN
, "%s bad OPEN, got AS set to 0",
1237 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1238 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1242 if (remote_as
== BGP_AS_TRANS
) {
1243 /* Take the AS4 from the capability. We must have received the
1244 * capability now! Otherwise we have a asn16 peer who uses
1245 * BGP_AS_TRANS, for some unknown reason.
1247 if (as4
== BGP_AS_TRANS
) {
1250 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1252 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1253 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1254 notify_data_remote_as4
, 4);
1258 if (!as4
&& BGP_DEBUG(as4
, AS4
))
1260 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.",
1262 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG(as4
, AS4
))
1264 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.",
1269 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1271 /* If we have got the capability, peer->as4cap must match
1273 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)
1274 && as4
!= remote_as
) {
1275 /* raise error, log this, close session */
1278 "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
1279 peer
->host
, as4
, remote_as
);
1280 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1281 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1282 notify_data_remote_as4
, 4);
1288 * If the BGP Identifier field of the OPEN message
1289 * is zero, or if it is the same as the BGP Identifier
1290 * of the local BGP speaker and the message is from an
1291 * internal peer, then the Error Subcode is set to
1292 * "Bad BGP Identifier".
1294 if (remote_id
.s_addr
== INADDR_ANY
1295 || (peer
->sort
== BGP_PEER_IBGP
1296 && ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
))) {
1297 if (bgp_debug_neighbor_events(peer
))
1298 zlog_debug("%s bad OPEN, wrong router identifier %pI4",
1299 peer
->host
, &remote_id
);
1300 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1301 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1302 notify_data_remote_id
, 4);
1306 /* Peer BGP version check. */
1307 if (version
!= BGP_VERSION_4
) {
1308 uint16_t maxver
= htons(BGP_VERSION_4
);
1309 /* XXX this reply may not be correct if version < 4 XXX */
1310 if (bgp_debug_neighbor_events(peer
))
1312 "%s bad protocol version, remote requested %d, local request %d",
1313 peer
->host
, version
, BGP_VERSION_4
);
1314 /* Data must be in network byte order here */
1315 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1316 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1317 (uint8_t *)&maxver
, 2);
1321 /* Check neighbor as number. */
1322 if (peer
->as_type
== AS_UNSPECIFIED
) {
1323 if (bgp_debug_neighbor_events(peer
))
1325 "%s bad OPEN, remote AS is unspecified currently",
1327 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1328 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1329 notify_data_remote_as
, 2);
1331 } else if (peer
->as_type
== AS_INTERNAL
) {
1332 if (remote_as
!= peer
->bgp
->as
) {
1333 if (bgp_debug_neighbor_events(peer
))
1335 "%s bad OPEN, remote AS is %u, internal specified",
1336 peer
->host
, remote_as
);
1337 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1338 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1339 notify_data_remote_as
, 2);
1342 peer
->as
= peer
->local_as
;
1343 } else if (peer
->as_type
== AS_EXTERNAL
) {
1344 if (remote_as
== peer
->bgp
->as
) {
1345 if (bgp_debug_neighbor_events(peer
))
1347 "%s bad OPEN, remote AS is %u, external specified",
1348 peer
->host
, remote_as
);
1349 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1350 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1351 notify_data_remote_as
, 2);
1354 peer
->as
= remote_as
;
1355 } else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
)) {
1356 if (bgp_debug_neighbor_events(peer
))
1357 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1358 peer
->host
, remote_as
, peer
->as
);
1359 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1360 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1361 notify_data_remote_as
, 2);
1366 * When collision is detected and this peer is closed.
1367 * Return immediately.
1369 ret
= bgp_collision_detect(peer
, remote_id
);
1374 if (bgp_getsockname(peer
) < 0) {
1375 flog_err_sys(EC_LIB_SOCKET
,
1376 "%s: bgp_getsockname() failed for peer: %s",
1377 __func__
, peer
->host
);
1381 /* Set remote router-id */
1382 peer
->remote_id
= remote_id
;
1384 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1385 calculate the value of the Hold Timer by using the smaller of its
1386 configured Hold Time and the Hold Time received in the OPEN message.
1387 The Hold Time MUST be either zero or at least three seconds. An
1388 implementation may reject connections on the basis of the Hold Time.
1391 if (holdtime
< 3 && holdtime
!= 0) {
1392 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1393 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1394 (uint8_t *)holdtime_ptr
, 2);
1398 /* Send notification message when Hold Time received in the OPEN message
1399 * is smaller than configured minimum Hold Time. */
1400 if (holdtime
< peer
->bgp
->default_min_holdtime
1401 && peer
->bgp
->default_min_holdtime
!= 0) {
1402 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1403 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1404 (uint8_t *)holdtime_ptr
, 2);
1408 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1409 would be one third of the Hold Time interval. KEEPALIVE messages
1410 MUST NOT be sent more frequently than one per second. An
1411 implementation MAY adjust the rate at which it sends KEEPALIVE
1412 messages as a function of the Hold Time interval. */
1414 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
1415 send_holdtime
= peer
->holdtime
;
1417 send_holdtime
= peer
->bgp
->default_holdtime
;
1419 if (holdtime
< send_holdtime
)
1420 peer
->v_holdtime
= holdtime
;
1422 peer
->v_holdtime
= send_holdtime
;
1424 /* Set effective keepalive to 1/3 the effective holdtime.
1425 * Use configured keeplive when < effective keepalive.
1427 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1428 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
)) {
1429 if (peer
->keepalive
&& peer
->keepalive
< peer
->v_keepalive
)
1430 peer
->v_keepalive
= peer
->keepalive
;
1432 if (peer
->bgp
->default_keepalive
1433 && peer
->bgp
->default_keepalive
< peer
->v_keepalive
)
1434 peer
->v_keepalive
= peer
->bgp
->default_keepalive
;
1437 /* Open option part parse. */
1439 if (bgp_open_option_parse(peer
, optlen
, &mp_capability
) < 0)
1442 if (bgp_debug_neighbor_events(peer
))
1443 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1448 * Assume that the peer supports the locally configured set of
1449 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1450 * capabilities, or if 'override-capability' is configured.
1453 || CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
1454 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] =
1455 peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1456 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] =
1457 peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1458 peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
] =
1459 peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
];
1460 peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
] =
1461 peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
];
1462 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] =
1463 peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1464 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] =
1465 peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1466 peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
] =
1467 peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
];
1468 peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
] =
1469 peer
->afc
[AFI_L2VPN
][SAFI_EVPN
];
1470 peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
] =
1471 peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
];
1474 /* Verify valid local address present based on negotiated
1475 * address-families. */
1476 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
1477 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
1478 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
1479 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
1480 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]) {
1481 if (peer
->nexthop
.v4
.s_addr
== INADDR_ANY
) {
1482 #if defined(HAVE_CUMULUS)
1483 zlog_warn("%s: No local IPv4 addr, BGP routing may not work",
1488 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
1489 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
1490 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
1491 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
1492 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]) {
1493 if (IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_global
)) {
1494 #if defined(HAVE_CUMULUS)
1495 zlog_warn("%s: No local IPv6 address, BGP routing may not work",
1500 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1502 return Receive_OPEN_message
;
1506 * Process BGP KEEPALIVE message for peer.
1509 * @param size size of the packet
1510 * @return as in summary
1512 static int bgp_keepalive_receive(struct peer
*peer
, bgp_size_t size
)
1514 if (bgp_debug_keepalive(peer
))
1515 zlog_debug("%s KEEPALIVE rcvd", peer
->host
);
1517 bgp_update_implicit_eors(peer
);
1519 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1521 /* If the peer's RTT is higher than expected, shutdown
1522 * the peer automatically.
1524 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_RTT_SHUTDOWN
)
1525 && peer
->rtt
> peer
->rtt_expected
) {
1527 peer
->rtt_keepalive_rcv
++;
1529 if (peer
->rtt_keepalive_rcv
> peer
->rtt_keepalive_conf
) {
1531 "%s shutdown due to high round-trip-time (%dms > %dms)",
1532 peer
->host
, peer
->rtt
, peer
->rtt_expected
);
1533 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1536 if (peer
->rtt_keepalive_rcv
)
1537 peer
->rtt_keepalive_rcv
--;
1540 return Receive_KEEPALIVE_message
;
1543 static int bgp_refresh_stalepath_timer_expire(struct thread
*thread
)
1545 struct peer_af
*paf
;
1547 paf
= THREAD_ARG(thread
);
1549 afi_t afi
= paf
->afi
;
1550 safi_t safi
= paf
->safi
;
1551 struct peer
*peer
= paf
->peer
;
1553 peer
->t_refresh_stalepath
= NULL
;
1555 if (peer
->nsf
[afi
][safi
])
1556 bgp_clear_stale_route(peer
, afi
, safi
);
1558 if (bgp_debug_neighbor_events(peer
))
1559 zlog_debug("%s: route-refresh (BoRR) timer for %s/%s expired",
1560 peer
->host
, afi2str(afi
), safi2str(safi
));
1562 bgp_timer_set(peer
);
1568 * Process BGP UPDATE message for peer.
1570 * Parses UPDATE and creates attribute object.
1573 * @param size size of the packet
1574 * @return as in summary
1576 static int bgp_update_receive(struct peer
*peer
, bgp_size_t size
)
1582 bgp_size_t attribute_len
;
1583 bgp_size_t update_len
;
1584 bgp_size_t withdraw_len
;
1585 bool restart
= false;
1594 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1596 /* Status must be Established. */
1597 if (!peer_established(peer
)) {
1598 flog_err(EC_BGP_INVALID_STATUS
,
1599 "%s [FSM] Update packet received under status %s",
1601 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1602 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
1603 bgp_fsm_error_subcode(peer
->status
));
1607 /* Set initial values. */
1608 memset(&attr
, 0, sizeof(struct attr
));
1609 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
1610 attr
.label
= MPLS_INVALID_LABEL
;
1611 memset(&nlris
, 0, sizeof(nlris
));
1612 memset(peer
->rcvd_attr_str
, 0, BUFSIZ
);
1613 peer
->rcvd_attr_printed
= 0;
1616 end
= stream_pnt(s
) + size
;
1618 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1619 Length is too large (i.e., if Unfeasible Routes Length + Total
1620 Attribute Length + 23 exceeds the message Length), then the Error
1621 Subcode is set to Malformed Attribute List. */
1622 if (stream_pnt(s
) + 2 > end
) {
1623 flog_err(EC_BGP_UPDATE_RCV
,
1624 "%s [Error] Update packet error (packet length is short for unfeasible length)",
1626 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1627 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1631 /* Unfeasible Route Length. */
1632 withdraw_len
= stream_getw(s
);
1634 /* Unfeasible Route Length check. */
1635 if (stream_pnt(s
) + withdraw_len
> end
) {
1636 flog_err(EC_BGP_UPDATE_RCV
,
1637 "%s [Error] Update packet error (packet unfeasible length overflow %d)",
1638 peer
->host
, withdraw_len
);
1639 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1640 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1644 /* Unfeasible Route packet format check. */
1645 if (withdraw_len
> 0) {
1646 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1647 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1648 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt(s
);
1649 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1650 stream_forward_getp(s
, withdraw_len
);
1653 /* Attribute total length check. */
1654 if (stream_pnt(s
) + 2 > end
) {
1656 EC_BGP_UPDATE_PACKET_SHORT
,
1657 "%s [Error] Packet Error (update packet is short for attribute length)",
1659 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1660 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1664 /* Fetch attribute total length. */
1665 attribute_len
= stream_getw(s
);
1667 /* Attribute length check. */
1668 if (stream_pnt(s
) + attribute_len
> end
) {
1670 EC_BGP_UPDATE_PACKET_LONG
,
1671 "%s [Error] Packet Error (update packet attribute length overflow %d)",
1672 peer
->host
, attribute_len
);
1673 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1674 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1678 /* Certain attribute parsing errors should not be considered bad enough
1679 * to reset the session for, most particularly any partial/optional
1680 * attributes that have 'tunneled' over speakers that don't understand
1681 * them. Instead we withdraw only the prefix concerned.
1683 * Complicates the flow a little though..
1685 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1686 /* This define morphs the update case into a withdraw when lower levels
1687 * have signalled an error condition where this is best.
1689 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1691 /* Parse attribute when it exists. */
1692 if (attribute_len
) {
1693 attr_parse_ret
= bgp_attr_parse(peer
, &attr
, attribute_len
,
1694 &nlris
[NLRI_MP_UPDATE
],
1695 &nlris
[NLRI_MP_WITHDRAW
]);
1696 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
) {
1697 bgp_attr_unintern_sub(&attr
);
1702 /* Logging the attribute. */
1703 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1704 || BGP_DEBUG(update
, UPDATE_IN
)
1705 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
1706 ret
= bgp_dump_attr(&attr
, peer
->rcvd_attr_str
,
1707 sizeof(peer
->rcvd_attr_str
));
1709 peer
->stat_upd_7606
++;
1711 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1714 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1717 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1)) {
1718 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
1719 peer
->rcvd_attr_str
);
1720 peer
->rcvd_attr_printed
= 1;
1724 /* Network Layer Reachability Information. */
1725 update_len
= end
- stream_pnt(s
);
1728 /* Set NLRI portion to structure. */
1729 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1730 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1731 nlris
[NLRI_UPDATE
].nlri
= stream_pnt(s
);
1732 nlris
[NLRI_UPDATE
].length
= update_len
;
1733 stream_forward_getp(s
, update_len
);
1735 if (CHECK_FLAG(attr
.flag
, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI
))) {
1737 * We skipped nexthop attribute validation earlier so
1738 * validate the nexthop now.
1740 if (bgp_attr_nexthop_valid(peer
, &attr
) < 0) {
1741 bgp_attr_unintern_sub(&attr
);
1747 if (BGP_DEBUG(update
, UPDATE_IN
))
1748 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1749 peer
->host
, withdraw_len
, attribute_len
, update_len
);
1751 /* Parse any given NLRIs */
1752 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++) {
1756 /* NLRI is processed iff the peer if configured for the specific
1758 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
]) {
1760 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1761 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1765 /* EoR handled later */
1766 if (nlris
[i
].length
== 0)
1771 case NLRI_MP_UPDATE
:
1772 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
1776 case NLRI_MP_WITHDRAW
:
1777 nlri_ret
= bgp_nlri_parse(peer
, &attr
, &nlris
[i
], 1);
1780 nlri_ret
= BGP_NLRI_PARSE_ERROR
;
1783 if (nlri_ret
< BGP_NLRI_PARSE_OK
1784 && nlri_ret
!= BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW
) {
1785 flog_err(EC_BGP_UPDATE_RCV
,
1786 "%s [Error] Error parsing NLRI", peer
->host
);
1787 if (peer_established(peer
))
1789 peer
, BGP_NOTIFY_UPDATE_ERR
,
1791 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1792 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1793 bgp_attr_unintern_sub(&attr
);
1800 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1801 * and MP EoR should have only an empty MP_UNREACH
1803 if ((!update_len
&& !withdraw_len
&& nlris
[NLRI_MP_UPDATE
].length
== 0)
1804 || (attr_parse_ret
== BGP_ATTR_PARSE_EOR
)) {
1807 struct graceful_restart_info
*gr_info
;
1809 /* Restarting router */
1810 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer
)
1811 && BGP_PEER_RESTARTING_MODE(peer
))
1814 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1816 * update and withdraw NLRI lengths are 0.
1818 if (!attribute_len
) {
1820 safi
= SAFI_UNICAST
;
1821 } else if (attr
.flag
& ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
)
1822 && nlris
[NLRI_MP_WITHDRAW
].length
== 0) {
1823 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1824 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1825 } else if (attr_parse_ret
== BGP_ATTR_PARSE_EOR
) {
1826 afi
= nlris
[NLRI_MP_UPDATE
].afi
;
1827 safi
= nlris
[NLRI_MP_UPDATE
].safi
;
1830 if (afi
&& peer
->afc
[afi
][safi
]) {
1831 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1833 /* End-of-RIB received */
1834 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1835 PEER_STATUS_EOR_RECEIVED
)) {
1836 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1837 PEER_STATUS_EOR_RECEIVED
);
1838 bgp_update_explicit_eors(peer
);
1839 /* Update graceful restart information */
1840 gr_info
= &(peer
->bgp
->gr_info
[afi
][safi
]);
1842 gr_info
->eor_received
++;
1843 /* If EOR received from all peers and selection
1844 * deferral timer is running, cancel the timer
1845 * and invoke the best path calculation
1847 if (gr_info
->eor_required
1848 == gr_info
->eor_received
) {
1849 if (bgp_debug_neighbor_events(peer
))
1853 gr_info
->eor_required
,
1855 gr_info
->eor_received
);
1857 gr_info
->t_select_deferral
);
1858 gr_info
->eor_required
= 0;
1859 gr_info
->eor_received
= 0;
1860 /* Best path selection */
1861 if (bgp_best_path_select_defer(
1862 peer
->bgp
, afi
, safi
)
1868 /* NSF delete stale route */
1869 if (peer
->nsf
[afi
][safi
])
1870 bgp_clear_stale_route(peer
, afi
, safi
);
1873 "%s: rcvd End-of-RIB for %s from %s in vrf %s",
1874 __func__
, get_afi_safi_str(afi
, safi
, false),
1875 peer
->host
, vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
1879 /* Everything is done. We unintern temporary structures which
1880 interned in bgp_attr_parse(). */
1881 bgp_attr_unintern_sub(&attr
);
1883 peer
->update_time
= bgp_clock();
1885 /* Notify BGP Conditional advertisement scanner process */
1886 peer
->advmap_table_change
= true;
1888 return Receive_UPDATE_message
;
1892 * Process BGP NOTIFY message for peer.
1895 * @param size size of the packet
1896 * @return as in summary
1898 static int bgp_notify_receive(struct peer
*peer
, bgp_size_t size
)
1900 struct bgp_notify bgp_notify
;
1902 if (peer
->notify
.data
) {
1903 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1904 peer
->notify
.length
= 0;
1907 bgp_notify
.code
= stream_getc(peer
->curr
);
1908 bgp_notify
.subcode
= stream_getc(peer
->curr
);
1909 bgp_notify
.length
= size
- 2;
1910 bgp_notify
.data
= NULL
;
1911 bgp_notify
.raw_data
= NULL
;
1913 /* Preserv notify code and sub code. */
1914 peer
->notify
.code
= bgp_notify
.code
;
1915 peer
->notify
.subcode
= bgp_notify
.subcode
;
1916 /* For further diagnostic record returned Data. */
1917 if (bgp_notify
.length
) {
1918 peer
->notify
.length
= size
- 2;
1919 peer
->notify
.data
= XMALLOC(MTYPE_TMP
, size
- 2);
1920 memcpy(peer
->notify
.data
, stream_pnt(peer
->curr
), size
- 2);
1929 if (bgp_notify
.length
) {
1931 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
1932 for (i
= 0; i
< bgp_notify
.length
; i
++)
1934 snprintf(c
, sizeof(c
), " %02x",
1935 stream_getc(peer
->curr
));
1937 strlcat(bgp_notify
.data
, c
,
1938 bgp_notify
.length
* 3);
1942 snprintf(c
, sizeof(c
), "%02x",
1943 stream_getc(peer
->curr
));
1945 strlcpy(bgp_notify
.data
, c
,
1946 bgp_notify
.length
* 3);
1948 bgp_notify
.raw_data
= (uint8_t *)peer
->notify
.data
;
1951 bgp_notify_print(peer
, &bgp_notify
, "received");
1952 if (bgp_notify
.data
) {
1953 XFREE(MTYPE_TMP
, bgp_notify
.data
);
1954 bgp_notify
.length
= 0;
1958 /* peer count update */
1959 atomic_fetch_add_explicit(&peer
->notify_in
, 1, memory_order_relaxed
);
1961 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1963 /* We have to check for Notify with Unsupported Optional Parameter.
1964 in that case we fallback to open without the capability option.
1965 But this done in bgp_stop. We just mark it here to avoid changing
1967 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
1968 && bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1969 UNSET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1971 bgp_peer_gr_flags_update(peer
);
1972 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
1975 return Receive_NOTIFICATION_message
;
1979 * Process BGP ROUTEREFRESH message for peer.
1982 * @param size size of the packet
1983 * @return as in summary
1985 static int bgp_route_refresh_receive(struct peer
*peer
, bgp_size_t size
)
1989 iana_safi_t pkt_safi
;
1992 struct peer_af
*paf
;
1993 struct update_group
*updgrp
;
1994 struct peer
*updgrp_peer
;
1996 bool force_update
= false;
1997 bgp_size_t msg_length
=
1998 size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
);
2000 /* If peer does not have the capability, send notification. */
2001 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_ADV
)) {
2002 flog_err(EC_BGP_NO_CAP
,
2003 "%s [Error] BGP route refresh is not enabled",
2005 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2006 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2010 /* Status must be Established. */
2011 if (!peer_established(peer
)) {
2013 EC_BGP_INVALID_STATUS
,
2014 "%s [Error] Route refresh packet received under status %s",
2016 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2017 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
2018 bgp_fsm_error_subcode(peer
->status
));
2025 pkt_afi
= stream_getw(s
);
2026 subtype
= stream_getc(s
);
2027 pkt_safi
= stream_getc(s
);
2029 /* Convert AFI, SAFI to internal values and check. */
2030 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
2032 "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
2033 peer
->host
, iana_afi2str(pkt_afi
),
2034 iana_safi2str(pkt_safi
));
2035 return BGP_PACKET_NOOP
;
2038 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) {
2040 uint8_t when_to_refresh
;
2045 /* If the length, excluding the fixed-size message
2046 * header, of the received ROUTE-REFRESH message with
2047 * Message Subtype 1 and 2 is not 4, then the BGP
2048 * speaker MUST send a NOTIFICATION message with the
2049 * Error Code of "ROUTE-REFRESH Message Error" and the
2050 * subcode of "Invalid Message Length".
2052 if (msg_length
!= 4) {
2054 "%s Enhanced Route Refresh message length error",
2057 peer
, BGP_NOTIFY_ROUTE_REFRESH_ERR
,
2058 BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN
);
2061 /* When the BGP speaker receives a ROUTE-REFRESH message
2062 * with a "Message Subtype" field other than 0, 1, or 2,
2063 * it MUST ignore the received ROUTE-REFRESH message.
2067 "%s Enhanced Route Refresh invalid subtype",
2071 if (msg_length
< 5) {
2072 zlog_info("%s ORF route refresh length error",
2074 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2075 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2079 when_to_refresh
= stream_getc(s
);
2080 end
= stream_pnt(s
) + (size
- 5);
2082 while ((stream_pnt(s
) + 2) < end
) {
2083 orf_type
= stream_getc(s
);
2084 orf_len
= stream_getw(s
);
2086 /* orf_len in bounds? */
2087 if ((stream_pnt(s
) + orf_len
) > end
)
2088 break; /* XXX: Notify instead?? */
2089 if (orf_type
== ORF_TYPE_PREFIX
2090 || orf_type
== ORF_TYPE_PREFIX_OLD
) {
2091 uint8_t *p_pnt
= stream_pnt(s
);
2092 uint8_t *p_end
= stream_pnt(s
) + orf_len
;
2093 struct orf_prefix orfp
;
2098 int ret
= CMD_SUCCESS
;
2100 if (bgp_debug_neighbor_events(peer
)) {
2102 "%s rcvd Prefixlist ORF(%d) length %d",
2103 peer
->host
, orf_type
, orf_len
);
2106 /* we're going to read at least 1 byte of common
2108 * and 7 bytes of ORF Address-filter entry from
2114 /* ORF prefix-list name */
2115 snprintf(name
, sizeof(name
), "%s.%d.%d",
2116 peer
->host
, afi
, safi
);
2118 while (p_pnt
< p_end
) {
2119 /* If the ORF entry is malformed, want
2120 * to read as much of it
2121 * as possible without going beyond the
2122 * bounds of the entry,
2123 * to maximise debug information.
2127 sizeof(struct orf_prefix
));
2129 /* after ++: p_pnt <= p_end */
2131 & ORF_COMMON_PART_REMOVE_ALL
) {
2132 if (bgp_debug_neighbor_events(
2135 "%s rcvd Remove-All pfxlist ORF request",
2137 prefix_bgp_orf_remove_all(afi
,
2141 ok
= ((uint32_t)(p_end
- p_pnt
)
2142 >= sizeof(uint32_t));
2146 p_pnt
+= sizeof(uint32_t);
2147 orfp
.seq
= ntohl(seq
);
2151 /* val checked in prefix_bgp_orf_set */
2155 /* val checked in prefix_bgp_orf_set */
2159 if ((ok
= (p_pnt
< p_end
)))
2160 orfp
.p
.prefixlen
= *p_pnt
++;
2162 /* afi checked already */
2163 orfp
.p
.family
= afi2family(afi
);
2166 psize
= PSIZE(orfp
.p
.prefixlen
);
2167 /* valid for family ? */
2168 if (psize
> prefix_blen(&orfp
.p
)) {
2170 psize
= prefix_blen(&orfp
.p
);
2172 /* valid for packet ? */
2173 if (psize
> (p_end
- p_pnt
)) {
2175 psize
= p_end
- p_pnt
;
2179 memcpy(&orfp
.p
.u
.prefix
, p_pnt
,
2183 if (bgp_debug_neighbor_events(peer
)) {
2184 char buf
[INET6_BUFSIZ
];
2187 "%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
2189 (common
& ORF_COMMON_PART_REMOVE
2192 (common
& ORF_COMMON_PART_DENY
2203 ok
? "" : " MALFORMED");
2207 ret
= prefix_bgp_orf_set(
2209 (common
& ORF_COMMON_PART_DENY
2212 (common
& ORF_COMMON_PART_REMOVE
2216 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
)) {
2218 "%s Received misformatted prefixlist ORF. Remove All pfxlist",
2220 prefix_bgp_orf_remove_all(afi
,
2226 peer
->orf_plist
[afi
][safi
] =
2227 prefix_bgp_orf_lookup(afi
, name
);
2229 stream_forward_getp(s
, orf_len
);
2231 if (bgp_debug_neighbor_events(peer
))
2232 zlog_debug("%s rcvd Refresh %s ORF request", peer
->host
,
2233 when_to_refresh
== REFRESH_DEFER
2236 if (when_to_refresh
== REFRESH_DEFER
)
2237 return BGP_PACKET_NOOP
;
2240 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2241 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2242 PEER_STATUS_ORF_WAIT_REFRESH
))
2243 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2244 PEER_STATUS_ORF_WAIT_REFRESH
);
2246 paf
= peer_af_find(peer
, afi
, safi
);
2247 if (paf
&& paf
->subgroup
) {
2248 if (peer
->orf_plist
[afi
][safi
]) {
2249 updgrp
= PAF_UPDGRP(paf
);
2250 updgrp_peer
= UPDGRP_PEER(updgrp
);
2251 updgrp_peer
->orf_plist
[afi
][safi
] =
2252 peer
->orf_plist
[afi
][safi
];
2255 /* Avoid supressing duplicate routes later
2256 * when processing in subgroup_announce_table().
2258 force_update
= true;
2260 /* If the peer is configured for default-originate clear the
2261 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
2265 if (CHECK_FLAG(paf
->subgroup
->sflags
,
2266 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
2267 UNSET_FLAG(paf
->subgroup
->sflags
,
2268 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
2271 if (subtype
== BGP_ROUTE_REFRESH_BORR
) {
2272 /* A BGP speaker that has received the Graceful Restart
2273 * Capability from its neighbor MUST ignore any BoRRs for
2274 * an <AFI, SAFI> from the neighbor before the speaker
2275 * receives the EoR for the given <AFI, SAFI> from the
2278 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)
2279 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2280 PEER_STATUS_EOR_RECEIVED
)) {
2281 if (bgp_debug_neighbor_events(peer
))
2283 "%s rcvd route-refresh (BoRR) for %s/%s before EoR",
2284 peer
->host
, afi2str(afi
),
2286 return BGP_PACKET_NOOP
;
2289 if (peer
->t_refresh_stalepath
) {
2290 if (bgp_debug_neighbor_events(peer
))
2292 "%s rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
2293 peer
->host
, afi2str(afi
),
2295 return BGP_PACKET_NOOP
;
2298 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_BORR_RECEIVED
);
2299 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2300 PEER_STATUS_EORR_RECEIVED
);
2302 /* When a BGP speaker receives a BoRR message from
2303 * a peer, it MUST mark all the routes with the given
2304 * Address Family Identifier and Subsequent Address
2305 * Family Identifier, <AFI, SAFI> [RFC2918], from
2306 * that peer as stale.
2308 if (peer_active_nego(peer
)) {
2309 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2310 PEER_STATUS_ENHANCED_REFRESH
);
2311 bgp_set_stale_route(peer
, afi
, safi
);
2314 if (peer_established(peer
))
2315 thread_add_timer(bm
->master
,
2316 bgp_refresh_stalepath_timer_expire
,
2317 paf
, peer
->bgp
->stalepath_time
,
2318 &peer
->t_refresh_stalepath
);
2320 if (bgp_debug_neighbor_events(peer
))
2322 "%s rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
2323 peer
->host
, afi2str(afi
), safi2str(safi
),
2324 peer
->bgp
->stalepath_time
);
2325 } else if (subtype
== BGP_ROUTE_REFRESH_EORR
) {
2326 if (!peer
->t_refresh_stalepath
) {
2328 "%s rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
2329 peer
->host
, afi2str(afi
), safi2str(safi
));
2330 return BGP_PACKET_NOOP
;
2333 BGP_TIMER_OFF(peer
->t_refresh_stalepath
);
2335 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_EORR_RECEIVED
);
2336 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2337 PEER_STATUS_BORR_RECEIVED
);
2339 if (bgp_debug_neighbor_events(peer
))
2341 "%s rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
2342 peer
->host
, afi2str(afi
), safi2str(safi
));
2344 if (peer
->nsf
[afi
][safi
])
2345 bgp_clear_stale_route(peer
, afi
, safi
);
2347 if (bgp_debug_neighbor_events(peer
))
2348 zlog_debug("%s rcvd route-refresh (REQUEST) for %s/%s",
2349 peer
->host
, afi2str(afi
), safi2str(safi
));
2351 /* In response to a "normal route refresh request" from the
2352 * peer, the speaker MUST send a BoRR message.
2354 if (CHECK_FLAG(peer
->cap
, PEER_CAP_ENHANCED_RR_RCV
)) {
2355 /* For a BGP speaker that supports the BGP Graceful
2356 * Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
2357 * to a neighbor before it sends the EoR for the
2358 * <AFI, SAFI> to the neighbor.
2360 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2361 PEER_STATUS_EOR_SEND
)) {
2362 if (bgp_debug_neighbor_events(peer
))
2364 "%s rcvd route-refresh (REQUEST) for %s/%s before EoR",
2365 peer
->host
, afi2str(afi
),
2367 return BGP_PACKET_NOOP
;
2370 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
2371 BGP_ROUTE_REFRESH_BORR
);
2373 if (bgp_debug_neighbor_events(peer
))
2375 "%s sending route-refresh (BoRR) for %s/%s",
2376 peer
->host
, afi2str(afi
),
2379 /* Set flag Ready-To-Send to know when we can send EoRR
2382 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2383 PEER_STATUS_BORR_SEND
);
2384 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2385 PEER_STATUS_EORR_SEND
);
2389 /* Perform route refreshment to the peer */
2390 bgp_announce_route(peer
, afi
, safi
, force_update
);
2392 /* No FSM action necessary */
2393 return BGP_PACKET_NOOP
;
2397 * Parse BGP CAPABILITY message for peer.
2400 * @param size size of the packet
2401 * @return as in summary
2403 static int bgp_capability_msg_parse(struct peer
*peer
, uint8_t *pnt
,
2407 struct capability_mp_data mpc
;
2408 struct capability_header
*hdr
;
2412 iana_safi_t pkt_safi
;
2418 /* We need at least action, capability code and capability
2420 if (pnt
+ 3 > end
) {
2421 zlog_info("%s Capability length error", peer
->host
);
2422 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2423 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2427 hdr
= (struct capability_header
*)(pnt
+ 1);
2429 /* Action value check. */
2430 if (action
!= CAPABILITY_ACTION_SET
2431 && action
!= CAPABILITY_ACTION_UNSET
) {
2432 zlog_info("%s Capability Action Value error %d",
2433 peer
->host
, action
);
2434 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2435 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2439 if (bgp_debug_neighbor_events(peer
))
2441 "%s CAPABILITY has action: %d, code: %u, length %u",
2442 peer
->host
, action
, hdr
->code
, hdr
->length
);
2444 /* Capability length check. */
2445 if ((pnt
+ hdr
->length
+ 3) > end
) {
2446 zlog_info("%s Capability length error", peer
->host
);
2447 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2448 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
2452 /* Fetch structure to the byte stream. */
2453 memcpy(&mpc
, pnt
+ 3, sizeof(struct capability_mp_data
));
2454 pnt
+= hdr
->length
+ 3;
2456 /* We know MP Capability Code. */
2457 if (hdr
->code
== CAPABILITY_CODE_MP
) {
2458 pkt_afi
= ntohs(mpc
.afi
);
2459 pkt_safi
= mpc
.safi
;
2461 /* Ignore capability when override-capability is set. */
2462 if (CHECK_FLAG(peer
->flags
,
2463 PEER_FLAG_OVERRIDE_CAPABILITY
))
2466 /* Convert AFI, SAFI to internal values. */
2467 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
,
2469 if (bgp_debug_neighbor_events(peer
))
2471 "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)",
2473 iana_afi2str(pkt_afi
),
2474 iana_safi2str(pkt_safi
));
2478 /* Address family check. */
2479 if (bgp_debug_neighbor_events(peer
))
2481 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
2483 action
== CAPABILITY_ACTION_SET
2486 iana_afi2str(pkt_afi
),
2487 iana_safi2str(pkt_safi
));
2489 if (action
== CAPABILITY_ACTION_SET
) {
2490 peer
->afc_recv
[afi
][safi
] = 1;
2491 if (peer
->afc
[afi
][safi
]) {
2492 peer
->afc_nego
[afi
][safi
] = 1;
2493 bgp_announce_route(peer
, afi
, safi
,
2497 peer
->afc_recv
[afi
][safi
] = 0;
2498 peer
->afc_nego
[afi
][safi
] = 0;
2500 if (peer_active_nego(peer
))
2501 bgp_clear_route(peer
, afi
, safi
);
2507 EC_BGP_UNRECOGNIZED_CAPABILITY
,
2508 "%s unrecognized capability code: %d - ignored",
2509 peer
->host
, hdr
->code
);
2513 /* No FSM action necessary */
2514 return BGP_PACKET_NOOP
;
2518 * Parse BGP CAPABILITY message for peer.
2520 * Exported for unit testing.
2523 * @param size size of the packet
2524 * @return as in summary
2526 int bgp_capability_receive(struct peer
*peer
, bgp_size_t size
)
2530 /* Fetch pointer. */
2531 pnt
= stream_pnt(peer
->curr
);
2533 if (bgp_debug_neighbor_events(peer
))
2534 zlog_debug("%s rcv CAPABILITY", peer
->host
);
2536 /* If peer does not have the capability, send notification. */
2537 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_ADV
)) {
2538 flog_err(EC_BGP_NO_CAP
,
2539 "%s [Error] BGP dynamic capability is not enabled",
2541 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2542 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2546 /* Status must be Established. */
2547 if (!peer_established(peer
)) {
2550 "%s [Error] Dynamic capability packet received under status %s",
2552 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2553 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
,
2554 bgp_fsm_error_subcode(peer
->status
));
2559 return bgp_capability_msg_parse(peer
, pnt
, size
);
2563 * Processes a peer's input buffer.
2565 * This function sidesteps the event loop and directly calls bgp_event_update()
2566 * after processing each BGP message. This is necessary to ensure proper
2567 * ordering of FSM events and unifies the behavior that was present previously,
2568 * whereby some of the packet handling functions would update the FSM and some
2569 * would not, making event flow difficult to understand. Please think twice
2570 * before hacking this.
2572 * Thread type: THREAD_EVENT
2576 int bgp_process_packet(struct thread
*thread
)
2578 /* Yes first of all get peer pointer. */
2579 struct peer
*peer
; // peer
2580 uint32_t rpkt_quanta_old
; // how many packets to read
2581 int fsm_update_result
; // return code of bgp_event_update()
2582 int mprc
; // message processing return code
2584 peer
= THREAD_ARG(thread
);
2585 rpkt_quanta_old
= atomic_load_explicit(&peer
->bgp
->rpkt_quanta
,
2586 memory_order_relaxed
);
2587 fsm_update_result
= 0;
2589 /* Guard against scheduled events that occur after peer deletion. */
2590 if (peer
->status
== Deleted
|| peer
->status
== Clearing
)
2593 unsigned int processed
= 0;
2595 while (processed
< rpkt_quanta_old
) {
2598 char notify_data_length
[2];
2600 frr_with_mutex(&peer
->io_mtx
) {
2601 peer
->curr
= stream_fifo_pop(peer
->ibuf
);
2604 if (peer
->curr
== NULL
) // no packets to process, hmm...
2607 /* skip the marker and copy the packet length */
2608 stream_forward_getp(peer
->curr
, BGP_MARKER_SIZE
);
2609 memcpy(notify_data_length
, stream_pnt(peer
->curr
), 2);
2611 /* read in the packet length and type */
2612 size
= stream_getw(peer
->curr
);
2613 type
= stream_getc(peer
->curr
);
2615 hook_call(bgp_packet_dump
, peer
, type
, size
, peer
->curr
);
2617 /* adjust size to exclude the marker + length + type */
2618 size
-= BGP_HEADER_SIZE
;
2620 /* Read rest of the packet and call each sort of packet routine
2624 frrtrace(2, frr_bgp
, open_process
, peer
, size
);
2625 atomic_fetch_add_explicit(&peer
->open_in
, 1,
2626 memory_order_relaxed
);
2627 mprc
= bgp_open_receive(peer
, size
);
2628 if (mprc
== BGP_Stop
)
2631 "%s: BGP OPEN receipt failed for peer: %s",
2632 __func__
, peer
->host
);
2634 case BGP_MSG_UPDATE
:
2635 frrtrace(2, frr_bgp
, update_process
, peer
, size
);
2636 atomic_fetch_add_explicit(&peer
->update_in
, 1,
2637 memory_order_relaxed
);
2638 peer
->readtime
= monotime(NULL
);
2639 mprc
= bgp_update_receive(peer
, size
);
2640 if (mprc
== BGP_Stop
)
2643 "%s: BGP UPDATE receipt failed for peer: %s",
2644 __func__
, peer
->host
);
2646 case BGP_MSG_NOTIFY
:
2647 frrtrace(2, frr_bgp
, notification_process
, peer
, size
);
2648 atomic_fetch_add_explicit(&peer
->notify_in
, 1,
2649 memory_order_relaxed
);
2650 mprc
= bgp_notify_receive(peer
, size
);
2651 if (mprc
== BGP_Stop
)
2654 "%s: BGP NOTIFY receipt failed for peer: %s",
2655 __func__
, peer
->host
);
2657 case BGP_MSG_KEEPALIVE
:
2658 frrtrace(2, frr_bgp
, keepalive_process
, peer
, size
);
2659 peer
->readtime
= monotime(NULL
);
2660 atomic_fetch_add_explicit(&peer
->keepalive_in
, 1,
2661 memory_order_relaxed
);
2662 mprc
= bgp_keepalive_receive(peer
, size
);
2663 if (mprc
== BGP_Stop
)
2666 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2667 __func__
, peer
->host
);
2669 case BGP_MSG_ROUTE_REFRESH_NEW
:
2670 case BGP_MSG_ROUTE_REFRESH_OLD
:
2671 frrtrace(2, frr_bgp
, refresh_process
, peer
, size
);
2672 atomic_fetch_add_explicit(&peer
->refresh_in
, 1,
2673 memory_order_relaxed
);
2674 mprc
= bgp_route_refresh_receive(peer
, size
);
2675 if (mprc
== BGP_Stop
)
2678 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2679 __func__
, peer
->host
);
2681 case BGP_MSG_CAPABILITY
:
2682 frrtrace(2, frr_bgp
, capability_process
, peer
, size
);
2683 atomic_fetch_add_explicit(&peer
->dynamic_cap_in
, 1,
2684 memory_order_relaxed
);
2685 mprc
= bgp_capability_receive(peer
, size
);
2686 if (mprc
== BGP_Stop
)
2689 "%s: BGP CAPABILITY receipt failed for peer: %s",
2690 __func__
, peer
->host
);
2693 /* Suppress uninitialized variable warning */
2697 * The message type should have been sanitized before
2698 * we ever got here. Receipt of a message with an
2699 * invalid header at this point is indicative of a
2702 assert (!"Message of invalid type received during input processing");
2705 /* delete processed packet */
2706 stream_free(peer
->curr
);
2711 if (mprc
!= BGP_PACKET_NOOP
)
2712 fsm_update_result
= bgp_event_update(peer
, mprc
);
2717 * If peer was deleted, do not process any more packets. This
2718 * is usually due to executing BGP_Stop or a stub deletion.
2720 if (fsm_update_result
== FSM_PEER_TRANSFERRED
2721 || fsm_update_result
== FSM_PEER_STOPPED
)
2725 if (fsm_update_result
!= FSM_PEER_TRANSFERRED
2726 && fsm_update_result
!= FSM_PEER_STOPPED
) {
2727 frr_with_mutex(&peer
->io_mtx
) {
2728 // more work to do, come back later
2729 if (peer
->ibuf
->count
> 0)
2731 bm
->master
, bgp_process_packet
, peer
, 0,
2732 &peer
->t_process_packet
);
2739 /* Send EOR when routes are processed by selection deferral timer */
2740 void bgp_send_delayed_eor(struct bgp
*bgp
)
2743 struct listnode
*node
, *nnode
;
2745 /* EOR message sent in bgp_write_proceed_actions */
2746 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
2747 bgp_write_proceed_actions(peer
);
2751 * Task callback to handle socket error encountered in the io pthread. We avoid
2752 * having the io pthread try to enqueue fsm events or mess with the peer
2755 int bgp_packet_process_error(struct thread
*thread
)
2760 peer
= THREAD_ARG(thread
);
2761 code
= THREAD_VAL(thread
);
2763 if (bgp_debug_neighbor_events(peer
))
2764 zlog_debug("%s [Event] BGP error %d on fd %d",
2765 peer
->host
, code
, peer
->fd
);
2767 /* Closed connection or error on the socket */
2768 if (peer_established(peer
)) {
2769 if ((CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
2770 || CHECK_FLAG(peer
->flags
,
2771 PEER_FLAG_GRACEFUL_RESTART_HELPER
))
2772 && CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)) {
2773 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2774 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2776 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2779 bgp_event_update(peer
, code
);