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 () */
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_table.h"
42 #include "bgpd/bgp_dump.h"
43 #include "bgpd/bgp_attr.h"
44 #include "bgpd/bgp_debug.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_route.h"
47 #include "bgpd/bgp_packet.h"
48 #include "bgpd/bgp_open.h"
49 #include "bgpd/bgp_aspath.h"
50 #include "bgpd/bgp_community.h"
51 #include "bgpd/bgp_ecommunity.h"
52 #include "bgpd/bgp_lcommunity.h"
53 #include "bgpd/bgp_network.h"
54 #include "bgpd/bgp_mplsvpn.h"
55 #include "bgpd/bgp_evpn.h"
56 #include "bgpd/bgp_advertise.h"
57 #include "bgpd/bgp_vty.h"
58 #include "bgpd/bgp_updgrp.h"
59 #include "bgpd/bgp_label.h"
60 #include "bgpd/bgp_io.h"
61 #include "bgpd/bgp_keepalives.h"
62 #include "bgpd/bgp_flowspec.h"
65 * Sets marker and type fields for a BGP message.
67 * @param s the stream containing the packet
68 * @param type the packet type
69 * @return the size of the stream
71 int bgp_packet_set_marker(struct stream
*s
, uint8_t type
)
76 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
79 /* Dummy total length. This field is should be filled in later on. */
82 /* BGP packet type. */
85 /* Return current stream size. */
86 return stream_get_endp(s
);
90 * Sets size field for a BGP message.
92 * Size field is set to the size of the stream passed.
94 * @param s the stream containing the packet
95 * @return the size of the stream
97 int bgp_packet_set_size(struct stream
*s
)
101 /* Preserve current pointer. */
102 cp
= stream_get_endp(s
);
103 stream_putw_at(s
, BGP_MARKER_SIZE
, cp
);
109 * Push a packet onto the beginning of the peer's output queue.
110 * This function acquires the peer's write mutex before proceeding.
112 static void bgp_packet_add(struct peer
*peer
, struct stream
*s
)
114 pthread_mutex_lock(&peer
->io_mtx
);
115 stream_fifo_push(peer
->obuf
, s
);
116 pthread_mutex_unlock(&peer
->io_mtx
);
119 static struct stream
*bgp_update_packet_eor(struct peer
*peer
, afi_t afi
,
124 iana_safi_t pkt_safi
;
126 if (DISABLE_BGP_ANNOUNCE
)
129 if (bgp_debug_neighbor_events(peer
))
130 zlog_debug("send End-of-RIB for %s to %s",
131 afi_safi_print(afi
, safi
), peer
->host
);
133 s
= stream_new(BGP_MAX_PACKET_SIZE
);
135 /* Make BGP update packet. */
136 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
138 /* Unfeasible Routes Length */
141 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
142 /* Total Path Attribute Length */
145 /* Convert AFI, SAFI to values for packet. */
146 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
148 /* Total Path Attribute Length */
150 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
151 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
153 stream_putw(s
, pkt_afi
);
154 stream_putc(s
, pkt_safi
);
157 bgp_packet_set_size(s
);
161 /* Called when there is a change in the EOR(implicit or explicit) status of a
162 * peer. Ends the update-delay if all expected peers are done with EORs. */
163 void bgp_check_update_delay(struct bgp
*bgp
)
165 struct listnode
*node
, *nnode
;
166 struct peer
*peer
= NULL
;
168 if (bgp_debug_neighbor_events(peer
))
169 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
170 bgp
->established
, bgp
->restarted_peers
,
171 bgp
->implicit_eors
, bgp
->explicit_eors
);
174 <= bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
) {
176 * This is an extra sanity check to make sure we wait for all
177 * the eligible configured peers. This check is performed if
178 * establish wait timer is on, or establish wait option is not
179 * given with the update-delay command
181 if (bgp
->t_establish_wait
182 || (bgp
->v_establish_wait
== bgp
->v_update_delay
))
183 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
184 if (CHECK_FLAG(peer
->flags
,
185 PEER_FLAG_CONFIG_NODE
)
186 && !CHECK_FLAG(peer
->flags
,
188 && !peer
->update_delay_over
) {
189 if (bgp_debug_neighbor_events(peer
))
191 " Peer %s pending, continuing read-only mode",
198 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
199 bgp
->restarted_peers
, bgp
->implicit_eors
,
201 bgp_update_delay_end(bgp
);
206 * Called if peer is known to have restarted. The restart-state bit in
207 * Graceful-Restart capability is used for that
209 void bgp_update_restarted_peers(struct peer
*peer
)
211 if (!bgp_update_delay_active(peer
->bgp
))
212 return; /* BGP update delay has ended */
213 if (peer
->update_delay_over
)
214 return; /* This peer has already been considered */
216 if (bgp_debug_neighbor_events(peer
))
217 zlog_debug("Peer %s: Checking restarted", peer
->host
);
219 if (peer
->status
== Established
) {
220 peer
->update_delay_over
= 1;
221 peer
->bgp
->restarted_peers
++;
222 bgp_check_update_delay(peer
->bgp
);
227 * Called as peer receives a keep-alive. Determines if this occurence can be
228 * taken as an implicit EOR for this peer.
229 * NOTE: The very first keep-alive after the Established state of a peer is
230 * considered implicit EOR for the update-delay purposes
232 void bgp_update_implicit_eors(struct peer
*peer
)
234 if (!bgp_update_delay_active(peer
->bgp
))
235 return; /* BGP update delay has ended */
236 if (peer
->update_delay_over
)
237 return; /* This peer has already been considered */
239 if (bgp_debug_neighbor_events(peer
))
240 zlog_debug("Peer %s: Checking implicit EORs", peer
->host
);
242 if (peer
->status
== Established
) {
243 peer
->update_delay_over
= 1;
244 peer
->bgp
->implicit_eors
++;
245 bgp_check_update_delay(peer
->bgp
);
250 * Should be called only when there is a change in the EOR_RECEIVED status
251 * for any afi/safi on a peer.
253 static void bgp_update_explicit_eors(struct peer
*peer
)
258 if (!bgp_update_delay_active(peer
->bgp
))
259 return; /* BGP update delay has ended */
260 if (peer
->update_delay_over
)
261 return; /* This peer has already been considered */
263 if (bgp_debug_neighbor_events(peer
))
264 zlog_debug("Peer %s: Checking explicit EORs", peer
->host
);
266 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
267 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
268 if (peer
->afc_nego
[afi
][safi
]
269 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
270 PEER_STATUS_EOR_RECEIVED
)) {
271 if (bgp_debug_neighbor_events(peer
))
273 " afi %d safi %d didnt receive EOR",
279 peer
->update_delay_over
= 1;
280 peer
->bgp
->explicit_eors
++;
281 bgp_check_update_delay(peer
->bgp
);
285 * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
287 * mp_withdraw, if set, is used to nullify attr structure on most of the
288 * calling safi function and for evpn, passed as parameter
290 int bgp_nlri_parse(struct peer
*peer
, struct attr
*attr
,
291 struct bgp_nlri
*packet
, int mp_withdraw
)
293 switch (packet
->safi
) {
296 return bgp_nlri_parse_ip(peer
, mp_withdraw
? NULL
: attr
,
298 case SAFI_LABELED_UNICAST
:
299 return bgp_nlri_parse_label(peer
, mp_withdraw
? NULL
: attr
,
302 return bgp_nlri_parse_vpn(peer
, mp_withdraw
? NULL
: attr
,
305 return bgp_nlri_parse_evpn(peer
, attr
, packet
, mp_withdraw
);
307 return bgp_nlri_parse_flowspec(peer
, attr
, packet
, mp_withdraw
);
313 * Checks a variety of conditions to determine whether the peer needs to be
314 * rescheduled for packet generation again, and does so if necessary.
316 * @param peer to check for rescheduling
318 static void bgp_write_proceed_actions(struct peer
*peer
)
323 struct bpacket
*next_pkt
;
324 struct update_subgroup
*subgrp
;
326 FOREACH_AFI_SAFI (afi
, safi
) {
327 paf
= peer_af_find(peer
, afi
, safi
);
330 subgrp
= paf
->subgroup
;
334 next_pkt
= paf
->next_pkt_to_send
;
335 if (next_pkt
&& next_pkt
->buffer
) {
336 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
337 bgp_generate_updgrp_packets
, 0);
341 /* No packets readily available for AFI/SAFI, are there
343 * that need to be generated? */
344 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
),
346 || subgroup_packets_to_build(subgrp
)) {
347 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
348 bgp_generate_updgrp_packets
, 0);
352 /* No packets to send, see if EOR is pending */
353 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
354 if (!subgrp
->t_coalesce
&& peer
->afc_nego
[afi
][safi
]
356 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
357 PEER_STATUS_EOR_SEND
)
358 && safi
!= SAFI_MPLS_VPN
) {
359 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
360 bgp_generate_updgrp_packets
, 0);
368 * Generate advertisement information (withdraws, updates, EOR) from each
369 * update group a peer belongs to, encode this information into packets, and
370 * enqueue the packets onto the peer's output buffer.
372 int bgp_generate_updgrp_packets(struct thread
*thread
)
374 struct peer
*peer
= THREAD_ARG(thread
);
378 struct bpacket
*next_pkt
;
380 uint32_t generated
= 0;
384 wpq
= atomic_load_explicit(&peer
->bgp
->wpkt_quanta
,
385 memory_order_relaxed
);
388 * The code beyond this part deals with update packets, proceed only
389 * if peer is Established and updates are not on hold (as part of
390 * update-delay post processing).
392 if (peer
->status
!= Established
)
395 if (peer
->bgp
->main_peers_update_hold
)
400 FOREACH_AFI_SAFI (afi
, safi
) {
401 paf
= peer_af_find(peer
, afi
, safi
);
402 if (!paf
|| !PAF_SUBGRP(paf
))
404 next_pkt
= paf
->next_pkt_to_send
;
407 * Try to generate a packet for the peer if we are at
408 * the end of the list. Always try to push out
411 if (!next_pkt
|| !next_pkt
->buffer
) {
412 next_pkt
= subgroup_withdraw_packet(
414 if (!next_pkt
|| !next_pkt
->buffer
)
415 subgroup_update_packet(PAF_SUBGRP(paf
));
416 next_pkt
= paf
->next_pkt_to_send
;
420 * If we still don't have a packet to send to the peer,
421 * then try to find out out if we have to send eor or
422 * if not, skip to the next AFI, SAFI. Don't send the
423 * EOR prematurely; if the subgroup's coalesce timer is
424 * running, the adjacency-out structure is not created
427 if (!next_pkt
|| !next_pkt
->buffer
) {
428 if (CHECK_FLAG(peer
->cap
,
429 PEER_CAP_RESTART_RCV
)) {
430 if (!(PAF_SUBGRP(paf
))->t_coalesce
431 && peer
->afc_nego
[afi
][safi
]
436 PEER_STATUS_EOR_SEND
)) {
437 SET_FLAG(peer
->af_sflags
[afi
]
439 PEER_STATUS_EOR_SEND
);
441 if ((s
= bgp_update_packet_eor(
444 bgp_packet_add(peer
, s
);
452 /* Found a packet template to send, overwrite
453 * packet with appropriate attributes from peer
454 * and advance peer */
455 s
= bpacket_reformat_for_peer(next_pkt
, paf
);
456 bgp_packet_add(peer
, s
);
457 bpacket_queue_advance_peer(paf
);
459 } while (s
&& (++generated
< wpq
));
464 bgp_write_proceed_actions(peer
);
470 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
472 void bgp_keepalive_send(struct peer
*peer
)
476 s
= stream_new(BGP_MAX_PACKET_SIZE
);
478 /* Make keepalive packet. */
479 bgp_packet_set_marker(s
, BGP_MSG_KEEPALIVE
);
481 /* Set packet size. */
482 (void)bgp_packet_set_size(s
);
484 /* Dump packet if debug option is set. */
485 /* bgp_packet_dump (s); */
487 if (bgp_debug_keepalive(peer
))
488 zlog_debug("%s sending KEEPALIVE", peer
->host
);
490 /* Add packet to the peer. */
491 bgp_packet_add(peer
, s
);
497 * Creates a BGP Open packet and appends it to the peer's output queue.
498 * Sets capabilities as necessary.
500 void bgp_open_send(struct peer
*peer
)
503 uint16_t send_holdtime
;
506 if (PEER_OR_GROUP_TIMER_SET(peer
))
507 send_holdtime
= peer
->holdtime
;
509 send_holdtime
= peer
->bgp
->default_holdtime
;
511 /* local-as Change */
512 if (peer
->change_local_as
)
513 local_as
= peer
->change_local_as
;
515 local_as
= peer
->local_as
;
517 s
= stream_new(BGP_MAX_PACKET_SIZE
);
519 /* Make open packet. */
520 bgp_packet_set_marker(s
, BGP_MSG_OPEN
);
522 /* Set open packet values. */
523 stream_putc(s
, BGP_VERSION_4
); /* BGP version */
524 stream_putw(s
, (local_as
<= BGP_AS_MAX
) ? (uint16_t)local_as
526 stream_putw(s
, send_holdtime
); /* Hold Time */
527 stream_put_in_addr(s
, &peer
->local_id
); /* BGP Identifier */
529 /* Set capability code. */
530 bgp_open_capability(s
, peer
);
532 /* Set BGP packet length. */
533 (void)bgp_packet_set_size(s
);
535 if (bgp_debug_neighbor_events(peer
))
537 "%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
538 peer
->host
, BGP_VERSION_4
, local_as
, send_holdtime
,
539 inet_ntoa(peer
->local_id
));
541 /* Dump packet if debug option is set. */
542 /* bgp_packet_dump (s); */
544 /* Add packet to the peer. */
545 bgp_packet_add(peer
, s
);
551 * Writes NOTIFICATION message directly to a peer socket without waiting for
554 * There must be exactly one stream on the peer->obuf FIFO, and the data within
555 * this stream must match the format of a BGP NOTIFICATION message.
556 * Transmission is best-effort.
558 * @requires peer->io_mtx
562 static int bgp_write_notify(struct peer
*peer
)
568 /* There should be at least one packet. */
569 s
= stream_fifo_pop(peer
->obuf
);
574 assert(stream_get_endp(s
) >= BGP_HEADER_SIZE
);
576 /* Stop collecting data within the socket */
577 sockopt_cork(peer
->fd
, 0);
580 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
581 * we only care about getting a clean shutdown at this point.
583 ret
= write(peer
->fd
, STREAM_DATA(s
), stream_get_endp(s
));
586 * only connection reset/close gets counted as TCP_fatal_error, failure
587 * to write the entire NOTIFY doesn't get different FSM treatment
591 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
595 /* Disable Nagle, make NOTIFY packet go out right away */
597 (void)setsockopt(peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
,
600 /* Retrieve BGP packet type. */
601 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
602 type
= stream_getc(s
);
604 assert(type
== BGP_MSG_NOTIFY
);
606 /* Type should be notify. */
607 atomic_fetch_add_explicit(&peer
->notify_out
, 1, memory_order_relaxed
);
610 /* Double start timer. */
613 /* Overflow check. */
614 if (peer
->v_start
>= (60 * 2))
615 peer
->v_start
= (60 * 2);
618 * Handle Graceful Restart case where the state changes to
619 * Connect instead of Idle
621 BGP_EVENT_ADD(peer
, BGP_Stop
);
629 * Creates a BGP Notify and appends it to the peer's output queue.
631 * This function attempts to write the packet from the thread it is called
632 * from, to ensure the packet gets out ASAP.
634 * This function may be called from multiple threads. Since the function
635 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
636 * call to prevent tampering from other threads.
638 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
639 * return, the peer structure *must* be reset; no assumptions about session
643 * @param code BGP error code
644 * @param sub_code BGP error subcode
645 * @param data Data portion
646 * @param datalen length of data portion
648 void bgp_notify_send_with_data(struct peer
*peer
, uint8_t code
,
649 uint8_t sub_code
, uint8_t *data
, size_t datalen
)
653 /* Lock I/O mutex to prevent other threads from pushing packets */
654 pthread_mutex_lock(&peer
->io_mtx
);
655 /* ============================================== */
657 /* Allocate new stream. */
658 s
= stream_new(BGP_MAX_PACKET_SIZE
);
660 /* Make notify packet. */
661 bgp_packet_set_marker(s
, BGP_MSG_NOTIFY
);
663 /* Set notify packet values. */
664 stream_putc(s
, code
); /* BGP notify code */
665 stream_putc(s
, sub_code
); /* BGP notify sub_code */
667 /* If notify data is present. */
669 stream_write(s
, data
, datalen
);
671 /* Set BGP packet length. */
672 bgp_packet_set_size(s
);
674 /* wipe output buffer */
675 stream_fifo_clean(peer
->obuf
);
678 * If possible, store last packet for debugging purposes. This check is
679 * in place because we are sometimes called with a doppelganger peer,
680 * who tends to have a plethora of fields nulled out.
682 if (peer
->curr
&& peer
->last_reset_cause_size
) {
683 size_t packetsize
= stream_get_endp(peer
->curr
);
684 assert(packetsize
<= peer
->last_reset_cause_size
);
685 memcpy(peer
->last_reset_cause
, peer
->curr
->data
, packetsize
);
686 peer
->last_reset_cause_size
= packetsize
;
691 struct bgp_notify bgp_notify
;
696 bgp_notify
.code
= code
;
697 bgp_notify
.subcode
= sub_code
;
698 bgp_notify
.data
= NULL
;
699 bgp_notify
.length
= datalen
;
700 bgp_notify
.raw_data
= data
;
702 peer
->notify
.code
= bgp_notify
.code
;
703 peer
->notify
.subcode
= bgp_notify
.subcode
;
705 if (bgp_notify
.length
&& data
) {
707 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
708 for (i
= 0; i
< bgp_notify
.length
; i
++)
710 sprintf(c
, " %02x", data
[i
]);
711 strcat(bgp_notify
.data
, c
);
714 sprintf(c
, "%02x", data
[i
]);
715 strcpy(bgp_notify
.data
, c
);
718 bgp_notify_print(peer
, &bgp_notify
, "sending");
720 if (bgp_notify
.data
) {
721 XFREE(MTYPE_TMP
, bgp_notify
.data
);
722 bgp_notify
.data
= NULL
;
723 bgp_notify
.length
= 0;
727 /* peer reset cause */
728 if (code
== BGP_NOTIFY_CEASE
) {
729 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
730 peer
->last_reset
= PEER_DOWN_USER_RESET
;
731 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
732 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
734 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
736 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
738 /* Add packet to peer's output queue */
739 stream_fifo_push(peer
->obuf
, s
);
741 bgp_write_notify(peer
);
743 /* ============================================== */
744 pthread_mutex_unlock(&peer
->io_mtx
);
748 * Creates a BGP Notify and appends it to the peer's output queue.
750 * This function attempts to write the packet from the thread it is called
751 * from, to ensure the packet gets out ASAP.
754 * @param code BGP error code
755 * @param sub_code BGP error subcode
757 void bgp_notify_send(struct peer
*peer
, uint8_t code
, uint8_t sub_code
)
759 bgp_notify_send_with_data(peer
, code
, sub_code
, NULL
, 0);
763 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
766 * @param afi Address Family Identifier
767 * @param safi Subsequent Address Family Identifier
768 * @param orf_type Outbound Route Filtering type
769 * @param when_to_refresh Whether to refresh immediately or defer
770 * @param remove Whether to remove ORF for specified AFI/SAFI
772 void bgp_route_refresh_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
773 uint8_t orf_type
, uint8_t when_to_refresh
,
777 struct bgp_filter
*filter
;
780 iana_safi_t pkt_safi
;
782 if (DISABLE_BGP_ANNOUNCE
)
785 filter
= &peer
->filter
[afi
][safi
];
787 /* Convert AFI, SAFI to values for packet. */
788 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
790 s
= stream_new(BGP_MAX_PACKET_SIZE
);
792 /* Make BGP update packet. */
793 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
794 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_NEW
);
796 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_OLD
);
798 /* Encode Route Refresh message. */
799 stream_putw(s
, pkt_afi
);
801 stream_putc(s
, pkt_safi
);
803 if (orf_type
== ORF_TYPE_PREFIX
|| orf_type
== ORF_TYPE_PREFIX_OLD
)
804 if (remove
|| filter
->plist
[FILTER_IN
].plist
) {
809 stream_putc(s
, when_to_refresh
);
810 stream_putc(s
, orf_type
);
811 orfp
= stream_get_endp(s
);
815 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
816 PEER_STATUS_ORF_PREFIX_SEND
);
817 stream_putc(s
, ORF_COMMON_PART_REMOVE_ALL
);
818 if (bgp_debug_neighbor_events(peer
))
820 "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
821 peer
->host
, orf_type
,
822 (when_to_refresh
== REFRESH_DEFER
827 SET_FLAG(peer
->af_sflags
[afi
][safi
],
828 PEER_STATUS_ORF_PREFIX_SEND
);
829 prefix_bgp_orf_entry(
830 s
, filter
->plist
[FILTER_IN
].plist
,
832 ORF_COMMON_PART_PERMIT
,
833 ORF_COMMON_PART_DENY
);
834 if (bgp_debug_neighbor_events(peer
))
836 "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
837 peer
->host
, orf_type
,
838 (when_to_refresh
== REFRESH_DEFER
844 /* Total ORF Entry Len. */
845 orf_len
= stream_get_endp(s
) - orfp
- 2;
846 stream_putw_at(s
, orfp
, orf_len
);
849 /* Set packet size. */
850 (void)bgp_packet_set_size(s
);
852 if (bgp_debug_neighbor_events(peer
)) {
854 zlog_debug("%s sending REFRESH_REQ for afi/safi: %d/%d",
855 peer
->host
, pkt_afi
, pkt_safi
);
858 /* Add packet to the peer. */
859 bgp_packet_add(peer
, s
);
865 * Create a BGP Capability packet and append it to the peer's output queue.
868 * @param afi Address Family Identifier
869 * @param safi Subsequent Address Family Identifier
870 * @param capability_code BGP Capability Code
871 * @param action Set or Remove capability
873 void bgp_capability_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
874 int capability_code
, int action
)
878 iana_safi_t pkt_safi
;
880 /* Convert AFI, SAFI to values for packet. */
881 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
883 s
= stream_new(BGP_MAX_PACKET_SIZE
);
885 /* Make BGP update packet. */
886 bgp_packet_set_marker(s
, BGP_MSG_CAPABILITY
);
888 /* Encode MP_EXT capability. */
889 if (capability_code
== CAPABILITY_CODE_MP
) {
890 stream_putc(s
, action
);
891 stream_putc(s
, CAPABILITY_CODE_MP
);
892 stream_putc(s
, CAPABILITY_CODE_MP_LEN
);
893 stream_putw(s
, pkt_afi
);
895 stream_putc(s
, pkt_safi
);
897 if (bgp_debug_neighbor_events(peer
))
899 "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
901 action
== CAPABILITY_ACTION_SET
? "Advertising"
906 /* Set packet size. */
907 (void)bgp_packet_set_size(s
);
909 /* Add packet to the peer. */
910 bgp_packet_add(peer
, s
);
915 /* RFC1771 6.8 Connection collision detection. */
916 static int bgp_collision_detect(struct peer
*new, struct in_addr remote_id
)
920 /* Upon receipt of an OPEN message, the local system must examine
921 all of its connections that are in the OpenConfirm state. A BGP
922 speaker may also examine connections in an OpenSent state if it
923 knows the BGP Identifier of the peer by means outside of the
924 protocol. If among these connections there is a connection to a
925 remote BGP speaker whose BGP Identifier equals the one in the
926 OPEN message, then the local system performs the following
927 collision resolution procedure: */
929 if ((peer
= new->doppelganger
) != NULL
) {
930 /* Do not accept the new connection in Established or Clearing
932 * Note that a peer GR is handled by closing the existing
934 * upon receipt of new one.
936 if (peer
->status
== Established
|| peer
->status
== Clearing
) {
937 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
938 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
940 } else if ((peer
->status
== OpenConfirm
)
941 || (peer
->status
== OpenSent
)) {
942 /* 1. The BGP Identifier of the local system is compared
944 the BGP Identifier of the remote system (as specified
946 the OPEN message). */
948 if (ntohl(peer
->local_id
.s_addr
)
949 < ntohl(remote_id
.s_addr
))
950 if (!CHECK_FLAG(peer
->sflags
,
951 PEER_STATUS_ACCEPT_PEER
)) {
952 /* 2. If the value of the local BGP
954 than the remote one, the local system
956 connection that already exists (the
958 already in the OpenConfirm state),
960 connection initiated by the remote
963 peer
, BGP_NOTIFY_CEASE
,
964 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
968 new, BGP_NOTIFY_CEASE
,
969 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
973 /* 3. Otherwise, the local system closes newly
975 BGP connection (the one associated with the
977 received OPEN message), and continues to use
979 existing one (the one that is already in the
980 OpenConfirm state). */
981 if (CHECK_FLAG(peer
->sflags
,
982 PEER_STATUS_ACCEPT_PEER
)) {
984 peer
, BGP_NOTIFY_CEASE
,
985 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
989 new, BGP_NOTIFY_CEASE
,
990 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
999 /* Packet processing routines ---------------------------------------------- */
1001 * This is a family of functions designed to be called from
1002 * bgp_process_packet(). These functions all share similar behavior and should
1003 * adhere to the following invariants and restrictions:
1007 * The return code of any one of those functions should be one of the FSM event
1008 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1009 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1010 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1011 * error and Receive_OPEN_message otherwise.
1013 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1018 * - May send NOTIFY messages
1019 * - May not modify peer->status
1020 * - May not call bgp_event_update()
1023 #define BGP_PACKET_NOOP 0
1026 * Process BGP OPEN message for peer.
1028 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1029 * and returns BGP_Stop.
1032 * @param size size of the packet
1033 * @return as in summary
1035 static int bgp_open_receive(struct peer
*peer
, bgp_size_t size
)
1041 uint16_t send_holdtime
;
1044 struct in_addr remote_id
;
1046 uint8_t notify_data_remote_as
[2];
1047 uint8_t notify_data_remote_as4
[4];
1048 uint8_t notify_data_remote_id
[4];
1049 uint16_t *holdtime_ptr
;
1051 /* Parse open packet. */
1052 version
= stream_getc(peer
->curr
);
1053 memcpy(notify_data_remote_as
, stream_pnt(peer
->curr
), 2);
1054 remote_as
= stream_getw(peer
->curr
);
1055 holdtime_ptr
= (uint16_t *)stream_pnt(peer
->curr
);
1056 holdtime
= stream_getw(peer
->curr
);
1057 memcpy(notify_data_remote_id
, stream_pnt(peer
->curr
), 4);
1058 remote_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1060 /* Receive OPEN message log */
1061 if (bgp_debug_neighbor_events(peer
))
1063 "%s rcv OPEN, version %d, remote-as (in open) %u,"
1064 " holdtime %d, id %s",
1065 peer
->host
, version
, remote_as
, holdtime
,
1066 inet_ntoa(remote_id
));
1068 /* BEGIN to read the capability here, but dont do it yet */
1070 optlen
= stream_getc(peer
->curr
);
1073 /* If not enough bytes, it is an error. */
1074 if (STREAM_READABLE(peer
->curr
) < optlen
) {
1075 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1076 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1080 /* We need the as4 capability value *right now* because
1081 * if it is there, we have not got the remote_as yet, and
1083 * that we do not know which peer is connecting to us now.
1085 as4
= peek_for_as4_capability(peer
, optlen
);
1086 memcpy(notify_data_remote_as4
, &as4
, 4);
1089 /* Just in case we have a silly peer who sends AS4 capability set to 0
1091 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
) {
1092 zlog_err("%s bad OPEN, got AS4 capability, but AS4 set to 0",
1094 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1095 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1096 notify_data_remote_as4
, 4);
1100 if (remote_as
== BGP_AS_TRANS
) {
1101 /* Take the AS4 from the capability. We must have received the
1102 * capability now! Otherwise we have a asn16 peer who uses
1103 * BGP_AS_TRANS, for some unknown reason.
1105 if (as4
== BGP_AS_TRANS
) {
1107 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1109 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1110 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1111 notify_data_remote_as4
, 4);
1115 if (!as4
&& BGP_DEBUG(as4
, AS4
))
1117 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
1118 " Odd, but proceeding.",
1120 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG(as4
, AS4
))
1122 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
1123 "in 2-bytes, very odd peer.",
1128 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1130 /* If we have got the capability, peer->as4cap must match
1132 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)
1133 && as4
!= remote_as
) {
1134 /* raise error, log this, close session */
1136 "%s bad OPEN, got AS4 capability, but remote_as %u"
1137 " mismatch with 16bit 'myasn' %u in open",
1138 peer
->host
, as4
, remote_as
);
1139 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1140 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1141 notify_data_remote_as4
, 4);
1146 /* remote router-id check. */
1147 if (remote_id
.s_addr
== 0 || IPV4_CLASS_DE(ntohl(remote_id
.s_addr
))
1148 || ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)) {
1149 if (bgp_debug_neighbor_events(peer
))
1150 zlog_debug("%s bad OPEN, wrong router identifier %s",
1151 peer
->host
, inet_ntoa(remote_id
));
1152 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1153 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1154 notify_data_remote_id
, 4);
1158 /* Set remote router-id */
1159 peer
->remote_id
= remote_id
;
1161 /* Peer BGP version check. */
1162 if (version
!= BGP_VERSION_4
) {
1163 uint16_t maxver
= htons(BGP_VERSION_4
);
1164 /* XXX this reply may not be correct if version < 4 XXX */
1165 if (bgp_debug_neighbor_events(peer
))
1167 "%s bad protocol version, remote requested %d, local request %d",
1168 peer
->host
, version
, BGP_VERSION_4
);
1169 /* Data must be in network byte order here */
1170 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1171 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1172 (uint8_t *)&maxver
, 2);
1176 /* Check neighbor as number. */
1177 if (peer
->as_type
== AS_UNSPECIFIED
) {
1178 if (bgp_debug_neighbor_events(peer
))
1180 "%s bad OPEN, remote AS is unspecified currently",
1182 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1183 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1184 notify_data_remote_as
, 2);
1186 } else if (peer
->as_type
== AS_INTERNAL
) {
1187 if (remote_as
!= peer
->bgp
->as
) {
1188 if (bgp_debug_neighbor_events(peer
))
1190 "%s bad OPEN, remote AS is %u, internal specified",
1191 peer
->host
, remote_as
);
1192 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1193 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1194 notify_data_remote_as
, 2);
1197 peer
->as
= peer
->local_as
;
1198 } else if (peer
->as_type
== AS_EXTERNAL
) {
1199 if (remote_as
== peer
->bgp
->as
) {
1200 if (bgp_debug_neighbor_events(peer
))
1202 "%s bad OPEN, remote AS is %u, external specified",
1203 peer
->host
, remote_as
);
1204 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1205 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1206 notify_data_remote_as
, 2);
1209 peer
->as
= remote_as
;
1210 } else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
)) {
1211 if (bgp_debug_neighbor_events(peer
))
1212 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1213 peer
->host
, remote_as
, peer
->as
);
1214 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1215 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1216 notify_data_remote_as
, 2);
1220 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1221 calculate the value of the Hold Timer by using the smaller of its
1222 configured Hold Time and the Hold Time received in the OPEN message.
1223 The Hold Time MUST be either zero or at least three seconds. An
1224 implementation may reject connections on the basis of the Hold Time.
1227 if (holdtime
< 3 && holdtime
!= 0) {
1228 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1229 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1230 (uint8_t *)holdtime_ptr
, 2);
1234 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1235 would be one third of the Hold Time interval. KEEPALIVE messages
1236 MUST NOT be sent more frequently than one per second. An
1237 implementation MAY adjust the rate at which it sends KEEPALIVE
1238 messages as a function of the Hold Time interval. */
1240 if (PEER_OR_GROUP_TIMER_SET(peer
))
1241 send_holdtime
= peer
->holdtime
;
1243 send_holdtime
= peer
->bgp
->default_holdtime
;
1245 if (holdtime
< send_holdtime
)
1246 peer
->v_holdtime
= holdtime
;
1248 peer
->v_holdtime
= send_holdtime
;
1250 if ((PEER_OR_GROUP_TIMER_SET(peer
))
1251 && (peer
->keepalive
< peer
->v_holdtime
/ 3))
1252 peer
->v_keepalive
= peer
->keepalive
;
1254 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1256 /* Open option part parse. */
1258 if ((ret
= bgp_open_option_parse(peer
, optlen
, &mp_capability
))
1262 if (bgp_debug_neighbor_events(peer
))
1263 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1268 * Assume that the peer supports the locally configured set of
1269 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1270 * capabilities, or if 'override-capability' is configured.
1273 || CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
1274 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] =
1275 peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1276 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] =
1277 peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1278 peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
] =
1279 peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
];
1280 peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
] =
1281 peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
];
1282 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] =
1283 peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1284 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] =
1285 peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1286 peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
] =
1287 peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
];
1288 peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
] =
1289 peer
->afc
[AFI_L2VPN
][SAFI_EVPN
];
1290 peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
] =
1291 peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
];
1294 /* When collision is detected and this peer is closed. Retrun
1296 ret
= bgp_collision_detect(peer
, remote_id
);
1301 if ((ret
= bgp_getsockname(peer
)) < 0) {
1302 zlog_err("%s: bgp_getsockname() failed for peer: %s",
1303 __FUNCTION__
, peer
->host
);
1307 /* Verify valid local address present based on negotiated
1308 * address-families. */
1309 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
1310 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
1311 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
1312 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
1313 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]) {
1314 if (!peer
->nexthop
.v4
.s_addr
) {
1315 #if defined(HAVE_CUMULUS)
1317 "%s: No local IPv4 addr resetting connection, fd %d",
1318 peer
->host
, peer
->fd
);
1319 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1320 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1325 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
1326 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
1327 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
1328 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
1329 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]) {
1330 if (IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_global
)) {
1331 #if defined(HAVE_CUMULUS)
1333 "%s: No local IPv6 addr resetting connection, fd %d",
1334 peer
->host
, peer
->fd
);
1335 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1336 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1341 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1343 return Receive_OPEN_message
;
1347 * Process BGP KEEPALIVE message for peer.
1350 * @param size size of the packet
1351 * @return as in summary
1353 static int bgp_keepalive_receive(struct peer
*peer
, bgp_size_t size
)
1355 if (bgp_debug_keepalive(peer
))
1356 zlog_debug("%s KEEPALIVE rcvd", peer
->host
);
1358 bgp_update_implicit_eors(peer
);
1360 return Receive_KEEPALIVE_message
;
1365 * Process BGP UPDATE message for peer.
1367 * Parses UPDATE and creates attribute object.
1370 * @param size size of the packet
1371 * @return as in summary
1373 static int bgp_update_receive(struct peer
*peer
, bgp_size_t size
)
1379 bgp_size_t attribute_len
;
1380 bgp_size_t update_len
;
1381 bgp_size_t withdraw_len
;
1390 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1392 /* Status must be Established. */
1393 if (peer
->status
!= Established
) {
1394 zlog_err("%s [FSM] Update packet received under status %s",
1396 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1397 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1401 /* Set initial values. */
1402 memset(&attr
, 0, sizeof(struct attr
));
1403 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
1404 attr
.label
= MPLS_INVALID_LABEL
;
1405 memset(&nlris
, 0, sizeof(nlris
));
1406 memset(peer
->rcvd_attr_str
, 0, BUFSIZ
);
1407 peer
->rcvd_attr_printed
= 0;
1410 end
= stream_pnt(s
) + size
;
1412 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1413 Length is too large (i.e., if Unfeasible Routes Length + Total
1414 Attribute Length + 23 exceeds the message Length), then the Error
1415 Subcode is set to Malformed Attribute List. */
1416 if (stream_pnt(s
) + 2 > end
) {
1418 "%s [Error] Update packet error"
1419 " (packet length is short for unfeasible length)",
1421 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1422 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1426 /* Unfeasible Route Length. */
1427 withdraw_len
= stream_getw(s
);
1429 /* Unfeasible Route Length check. */
1430 if (stream_pnt(s
) + withdraw_len
> end
) {
1432 "%s [Error] Update packet error"
1433 " (packet unfeasible length overflow %d)",
1434 peer
->host
, withdraw_len
);
1435 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1436 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1440 /* Unfeasible Route packet format check. */
1441 if (withdraw_len
> 0) {
1442 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1443 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1444 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt(s
);
1445 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1446 stream_forward_getp(s
, withdraw_len
);
1449 /* Attribute total length check. */
1450 if (stream_pnt(s
) + 2 > end
) {
1452 "%s [Error] Packet Error"
1453 " (update packet is short for attribute length)",
1455 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1456 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1460 /* Fetch attribute total length. */
1461 attribute_len
= stream_getw(s
);
1463 /* Attribute length check. */
1464 if (stream_pnt(s
) + attribute_len
> end
) {
1466 "%s [Error] Packet Error"
1467 " (update packet attribute length overflow %d)",
1468 peer
->host
, attribute_len
);
1469 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1470 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1474 /* Certain attribute parsing errors should not be considered bad enough
1475 * to reset the session for, most particularly any partial/optional
1476 * attributes that have 'tunneled' over speakers that don't understand
1477 * them. Instead we withdraw only the prefix concerned.
1479 * Complicates the flow a little though..
1481 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1482 /* This define morphs the update case into a withdraw when lower levels
1483 * have signalled an error condition where this is best.
1485 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1487 /* Parse attribute when it exists. */
1488 if (attribute_len
) {
1489 attr_parse_ret
= bgp_attr_parse(peer
, &attr
, attribute_len
,
1490 &nlris
[NLRI_MP_UPDATE
],
1491 &nlris
[NLRI_MP_WITHDRAW
]);
1492 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
) {
1493 bgp_attr_unintern_sub(&attr
);
1498 /* Logging the attribute. */
1499 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1500 || BGP_DEBUG(update
, UPDATE_IN
)
1501 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
1502 ret
= bgp_dump_attr(&attr
, peer
->rcvd_attr_str
, BUFSIZ
);
1504 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1506 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1509 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1)) {
1510 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
1511 peer
->rcvd_attr_str
);
1512 peer
->rcvd_attr_printed
= 1;
1516 /* Network Layer Reachability Information. */
1517 update_len
= end
- stream_pnt(s
);
1520 /* Set NLRI portion to structure. */
1521 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1522 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1523 nlris
[NLRI_UPDATE
].nlri
= stream_pnt(s
);
1524 nlris
[NLRI_UPDATE
].length
= update_len
;
1525 stream_forward_getp(s
, update_len
);
1528 if (BGP_DEBUG(update
, UPDATE_IN
))
1529 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1530 peer
->host
, withdraw_len
, attribute_len
, update_len
);
1532 /* Parse any given NLRIs */
1533 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++) {
1537 /* NLRI is processed iff the peer if configured for the specific
1539 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
]) {
1541 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1542 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1546 /* EoR handled later */
1547 if (nlris
[i
].length
== 0)
1552 case NLRI_MP_UPDATE
:
1553 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
1557 case NLRI_MP_WITHDRAW
:
1558 nlri_ret
= bgp_nlri_parse(peer
, &attr
, &nlris
[i
], 1);
1565 zlog_err("%s [Error] Error parsing NLRI", peer
->host
);
1566 if (peer
->status
== Established
)
1568 peer
, BGP_NOTIFY_UPDATE_ERR
,
1570 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1571 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1572 bgp_attr_unintern_sub(&attr
);
1579 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1580 * and MP EoR should have only an empty MP_UNREACH
1582 if ((!update_len
&& !withdraw_len
&& nlris
[NLRI_MP_UPDATE
].length
== 0)
1583 || (attr_parse_ret
== BGP_ATTR_PARSE_EOR
)) {
1587 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1589 * update and withdraw NLRI lengths are 0.
1591 if (!attribute_len
) {
1593 safi
= SAFI_UNICAST
;
1594 } else if (attr
.flag
& ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
)
1595 && nlris
[NLRI_MP_WITHDRAW
].length
== 0) {
1596 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1597 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1598 } else if (attr_parse_ret
== BGP_ATTR_PARSE_EOR
) {
1599 afi
= nlris
[NLRI_MP_UPDATE
].afi
;
1600 safi
= nlris
[NLRI_MP_UPDATE
].safi
;
1603 if (afi
&& peer
->afc
[afi
][safi
]) {
1604 /* End-of-RIB received */
1605 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1606 PEER_STATUS_EOR_RECEIVED
)) {
1607 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1608 PEER_STATUS_EOR_RECEIVED
);
1609 bgp_update_explicit_eors(peer
);
1612 /* NSF delete stale route */
1613 if (peer
->nsf
[afi
][safi
])
1614 bgp_clear_stale_route(peer
, afi
, safi
);
1616 if (bgp_debug_neighbor_events(peer
)) {
1617 zlog_debug("rcvd End-of-RIB for %s from %s",
1618 afi_safi_print(afi
, safi
),
1624 /* Everything is done. We unintern temporary structures which
1625 interned in bgp_attr_parse(). */
1626 bgp_attr_unintern_sub(&attr
);
1628 peer
->update_time
= bgp_clock();
1630 /* Rearm holdtime timer */
1631 BGP_TIMER_OFF(peer
->t_holdtime
);
1632 bgp_timer_set(peer
);
1634 return Receive_UPDATE_message
;
1638 * Process BGP NOTIFY message for peer.
1641 * @param size size of the packet
1642 * @return as in summary
1644 static int bgp_notify_receive(struct peer
*peer
, bgp_size_t size
)
1646 struct bgp_notify bgp_notify
;
1648 if (peer
->notify
.data
) {
1649 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1650 peer
->notify
.data
= NULL
;
1651 peer
->notify
.length
= 0;
1654 bgp_notify
.code
= stream_getc(peer
->curr
);
1655 bgp_notify
.subcode
= stream_getc(peer
->curr
);
1656 bgp_notify
.length
= size
- 2;
1657 bgp_notify
.data
= NULL
;
1659 /* Preserv notify code and sub code. */
1660 peer
->notify
.code
= bgp_notify
.code
;
1661 peer
->notify
.subcode
= bgp_notify
.subcode
;
1662 /* For further diagnostic record returned Data. */
1663 if (bgp_notify
.length
) {
1664 peer
->notify
.length
= size
- 2;
1665 peer
->notify
.data
= XMALLOC(MTYPE_TMP
, size
- 2);
1666 memcpy(peer
->notify
.data
, stream_pnt(peer
->curr
), size
- 2);
1675 if (bgp_notify
.length
) {
1677 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
1678 for (i
= 0; i
< bgp_notify
.length
; i
++)
1681 stream_getc(peer
->curr
));
1682 strcat(bgp_notify
.data
, c
);
1686 stream_getc(peer
->curr
));
1687 strcpy(bgp_notify
.data
, c
);
1689 bgp_notify
.raw_data
= (uint8_t *)peer
->notify
.data
;
1692 bgp_notify_print(peer
, &bgp_notify
, "received");
1693 if (bgp_notify
.data
) {
1694 XFREE(MTYPE_TMP
, bgp_notify
.data
);
1695 bgp_notify
.data
= NULL
;
1696 bgp_notify
.length
= 0;
1700 /* peer count update */
1701 atomic_fetch_add_explicit(&peer
->notify_in
, 1, memory_order_relaxed
);
1703 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1705 /* We have to check for Notify with Unsupported Optional Parameter.
1706 in that case we fallback to open without the capability option.
1707 But this done in bgp_stop. We just mark it here to avoid changing
1709 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
1710 && bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1711 UNSET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1713 return Receive_NOTIFICATION_message
;
1717 * Process BGP ROUTEREFRESH message for peer.
1720 * @param size size of the packet
1721 * @return as in summary
1723 static int bgp_route_refresh_receive(struct peer
*peer
, bgp_size_t size
)
1727 iana_safi_t pkt_safi
;
1730 struct peer_af
*paf
;
1731 struct update_group
*updgrp
;
1732 struct peer
*updgrp_peer
;
1734 /* If peer does not have the capability, send notification. */
1735 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_ADV
)) {
1736 zlog_err("%s [Error] BGP route refresh is not enabled",
1738 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
1739 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1743 /* Status must be Established. */
1744 if (peer
->status
!= Established
) {
1746 "%s [Error] Route refresh packet received under status %s",
1748 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1749 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1756 pkt_afi
= stream_getw(s
);
1757 (void)stream_getc(s
);
1758 pkt_safi
= stream_getc(s
);
1760 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1761 zlog_debug("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1762 peer
->host
, pkt_afi
, pkt_safi
);
1764 /* Convert AFI, SAFI to internal values and check. */
1765 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1767 "%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1768 peer
->host
, pkt_afi
, pkt_safi
);
1769 return BGP_PACKET_NOOP
;
1772 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) {
1774 uint8_t when_to_refresh
;
1778 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1780 zlog_info("%s ORF route refresh length error",
1782 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
1786 when_to_refresh
= stream_getc(s
);
1787 end
= stream_pnt(s
) + (size
- 5);
1789 while ((stream_pnt(s
) + 2) < end
) {
1790 orf_type
= stream_getc(s
);
1791 orf_len
= stream_getw(s
);
1793 /* orf_len in bounds? */
1794 if ((stream_pnt(s
) + orf_len
) > end
)
1795 break; /* XXX: Notify instead?? */
1796 if (orf_type
== ORF_TYPE_PREFIX
1797 || orf_type
== ORF_TYPE_PREFIX_OLD
) {
1798 uint8_t *p_pnt
= stream_pnt(s
);
1799 uint8_t *p_end
= stream_pnt(s
) + orf_len
;
1800 struct orf_prefix orfp
;
1805 int ret
= CMD_SUCCESS
;
1807 if (bgp_debug_neighbor_events(peer
)) {
1809 "%s rcvd Prefixlist ORF(%d) length %d",
1810 peer
->host
, orf_type
, orf_len
);
1813 /* we're going to read at least 1 byte of common
1815 * and 7 bytes of ORF Address-filter entry from
1821 /* ORF prefix-list name */
1822 sprintf(name
, "%s.%d.%d", peer
->host
, afi
,
1825 while (p_pnt
< p_end
) {
1826 /* If the ORF entry is malformed, want
1827 * to read as much of it
1828 * as possible without going beyond the
1829 * bounds of the entry,
1830 * to maximise debug information.
1834 sizeof(struct orf_prefix
));
1836 /* after ++: p_pnt <= p_end */
1838 & ORF_COMMON_PART_REMOVE_ALL
) {
1839 if (bgp_debug_neighbor_events(
1842 "%s rcvd Remove-All pfxlist ORF request",
1844 prefix_bgp_orf_remove_all(afi
,
1848 ok
= ((uint32_t)(p_end
- p_pnt
)
1849 >= sizeof(uint32_t));
1853 p_pnt
+= sizeof(uint32_t);
1854 orfp
.seq
= ntohl(seq
);
1858 if ((ok
= (p_pnt
< p_end
)))
1862 prefix_bgp_orf_set()
1864 if ((ok
= (p_pnt
< p_end
)))
1868 prefix_bgp_orf_set()
1870 if ((ok
= (p_pnt
< p_end
)))
1871 orfp
.p
.prefixlen
= *p_pnt
++;
1872 orfp
.p
.family
= afi2family(
1873 afi
); /* afi checked already */
1876 orfp
.p
.prefixlen
); /* 0 if not
1880 &orfp
.p
)) /* valid for
1884 psize
= prefix_blen(&orfp
.p
);
1887 > (p_end
- p_pnt
)) /* valid for
1891 psize
= p_end
- p_pnt
;
1895 memcpy(&orfp
.p
.u
.prefix
, p_pnt
,
1899 if (bgp_debug_neighbor_events(peer
)) {
1900 char buf
[INET6_BUFSIZ
];
1903 "%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
1905 (common
& ORF_COMMON_PART_REMOVE
1908 (common
& ORF_COMMON_PART_DENY
1919 ok
? "" : " MALFORMED");
1923 ret
= prefix_bgp_orf_set(
1925 (common
& ORF_COMMON_PART_DENY
1928 (common
& ORF_COMMON_PART_REMOVE
1932 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
)) {
1934 "%s Received misformatted prefixlist ORF."
1935 " Remove All pfxlist",
1937 prefix_bgp_orf_remove_all(afi
,
1943 peer
->orf_plist
[afi
][safi
] =
1944 prefix_bgp_orf_lookup(afi
, name
);
1946 stream_forward_getp(s
, orf_len
);
1948 if (bgp_debug_neighbor_events(peer
))
1949 zlog_debug("%s rcvd Refresh %s ORF request", peer
->host
,
1950 when_to_refresh
== REFRESH_DEFER
1953 if (when_to_refresh
== REFRESH_DEFER
)
1954 return BGP_PACKET_NOOP
;
1957 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1958 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1959 PEER_STATUS_ORF_WAIT_REFRESH
))
1960 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
1961 PEER_STATUS_ORF_WAIT_REFRESH
);
1963 paf
= peer_af_find(peer
, afi
, safi
);
1964 if (paf
&& paf
->subgroup
) {
1965 if (peer
->orf_plist
[afi
][safi
]) {
1966 updgrp
= PAF_UPDGRP(paf
);
1967 updgrp_peer
= UPDGRP_PEER(updgrp
);
1968 updgrp_peer
->orf_plist
[afi
][safi
] =
1969 peer
->orf_plist
[afi
][safi
];
1972 /* If the peer is configured for default-originate clear the
1973 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
1977 if (CHECK_FLAG(paf
->subgroup
->sflags
,
1978 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1979 UNSET_FLAG(paf
->subgroup
->sflags
,
1980 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1983 /* Perform route refreshment to the peer */
1984 bgp_announce_route(peer
, afi
, safi
);
1986 /* No FSM action necessary */
1987 return BGP_PACKET_NOOP
;
1991 * Parse BGP CAPABILITY message for peer.
1994 * @param size size of the packet
1995 * @return as in summary
1997 static int bgp_capability_msg_parse(struct peer
*peer
, uint8_t *pnt
,
2001 struct capability_mp_data mpc
;
2002 struct capability_header
*hdr
;
2006 iana_safi_t pkt_safi
;
2012 /* We need at least action, capability code and capability
2014 if (pnt
+ 3 > end
) {
2015 zlog_info("%s Capability length error", peer
->host
);
2016 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2020 hdr
= (struct capability_header
*)(pnt
+ 1);
2022 /* Action value check. */
2023 if (action
!= CAPABILITY_ACTION_SET
2024 && action
!= CAPABILITY_ACTION_UNSET
) {
2025 zlog_info("%s Capability Action Value error %d",
2026 peer
->host
, action
);
2027 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2031 if (bgp_debug_neighbor_events(peer
))
2033 "%s CAPABILITY has action: %d, code: %u, length %u",
2034 peer
->host
, action
, hdr
->code
, hdr
->length
);
2036 /* Capability length check. */
2037 if ((pnt
+ hdr
->length
+ 3) > end
) {
2038 zlog_info("%s Capability length error", peer
->host
);
2039 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2043 /* Fetch structure to the byte stream. */
2044 memcpy(&mpc
, pnt
+ 3, sizeof(struct capability_mp_data
));
2045 pnt
+= hdr
->length
+ 3;
2047 /* We know MP Capability Code. */
2048 if (hdr
->code
== CAPABILITY_CODE_MP
) {
2049 pkt_afi
= ntohs(mpc
.afi
);
2050 pkt_safi
= mpc
.safi
;
2052 /* Ignore capability when override-capability is set. */
2053 if (CHECK_FLAG(peer
->flags
,
2054 PEER_FLAG_OVERRIDE_CAPABILITY
))
2057 /* Convert AFI, SAFI to internal values. */
2058 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
,
2060 if (bgp_debug_neighbor_events(peer
))
2062 "%s Dynamic Capability MP_EXT afi/safi invalid "
2064 peer
->host
, pkt_afi
, pkt_safi
);
2068 /* Address family check. */
2069 if (bgp_debug_neighbor_events(peer
))
2071 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2073 action
== CAPABILITY_ACTION_SET
2078 if (action
== CAPABILITY_ACTION_SET
) {
2079 peer
->afc_recv
[afi
][safi
] = 1;
2080 if (peer
->afc
[afi
][safi
]) {
2081 peer
->afc_nego
[afi
][safi
] = 1;
2082 bgp_announce_route(peer
, afi
, safi
);
2085 peer
->afc_recv
[afi
][safi
] = 0;
2086 peer
->afc_nego
[afi
][safi
] = 0;
2088 if (peer_active_nego(peer
))
2089 bgp_clear_route(peer
, afi
, safi
);
2095 "%s unrecognized capability code: %d - ignored",
2096 peer
->host
, hdr
->code
);
2100 /* No FSM action necessary */
2101 return BGP_PACKET_NOOP
;
2105 * Parse BGP CAPABILITY message for peer.
2107 * Exported for unit testing.
2110 * @param size size of the packet
2111 * @return as in summary
2113 int bgp_capability_receive(struct peer
*peer
, bgp_size_t size
)
2117 /* Fetch pointer. */
2118 pnt
= stream_pnt(peer
->curr
);
2120 if (bgp_debug_neighbor_events(peer
))
2121 zlog_debug("%s rcv CAPABILITY", peer
->host
);
2123 /* If peer does not have the capability, send notification. */
2124 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_ADV
)) {
2125 zlog_err("%s [Error] BGP dynamic capability is not enabled",
2127 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2128 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2132 /* Status must be Established. */
2133 if (peer
->status
!= Established
) {
2135 "%s [Error] Dynamic capability packet received under status %s",
2137 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2138 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
2143 return bgp_capability_msg_parse(peer
, pnt
, size
);
2147 * Processes a peer's input buffer.
2149 * This function sidesteps the event loop and directly calls bgp_event_update()
2150 * after processing each BGP message. This is necessary to ensure proper
2151 * ordering of FSM events and unifies the behavior that was present previously,
2152 * whereby some of the packet handling functions would update the FSM and some
2153 * would not, making event flow difficult to understand. Please think twice
2154 * before hacking this.
2156 * Thread type: THREAD_EVENT
2160 int bgp_process_packet(struct thread
*thread
)
2162 /* Yes first of all get peer pointer. */
2163 struct peer
*peer
; // peer
2164 uint32_t rpkt_quanta_old
; // how many packets to read
2165 int fsm_update_result
; // return code of bgp_event_update()
2166 int mprc
; // message processing return code
2168 peer
= THREAD_ARG(thread
);
2169 rpkt_quanta_old
= atomic_load_explicit(&peer
->bgp
->rpkt_quanta
,
2170 memory_order_relaxed
);
2171 fsm_update_result
= 0;
2173 /* Guard against scheduled events that occur after peer deletion. */
2174 if (peer
->status
== Deleted
|| peer
->status
== Clearing
)
2177 unsigned int processed
= 0;
2179 while (processed
< rpkt_quanta_old
) {
2182 char notify_data_length
[2];
2184 pthread_mutex_lock(&peer
->io_mtx
);
2186 peer
->curr
= stream_fifo_pop(peer
->ibuf
);
2188 pthread_mutex_unlock(&peer
->io_mtx
);
2190 if (peer
->curr
== NULL
) // no packets to process, hmm...
2193 /* skip the marker and copy the packet length */
2194 stream_forward_getp(peer
->curr
, BGP_MARKER_SIZE
);
2195 memcpy(notify_data_length
, stream_pnt(peer
->curr
), 2);
2197 /* read in the packet length and type */
2198 size
= stream_getw(peer
->curr
);
2199 type
= stream_getc(peer
->curr
);
2201 /* BGP packet dump function. */
2202 bgp_dump_packet(peer
, type
, peer
->curr
);
2204 /* adjust size to exclude the marker + length + type */
2205 size
-= BGP_HEADER_SIZE
;
2207 /* Read rest of the packet and call each sort of packet routine
2211 atomic_fetch_add_explicit(&peer
->open_in
, 1,
2212 memory_order_relaxed
);
2213 mprc
= bgp_open_receive(peer
, size
);
2214 if (mprc
== BGP_Stop
)
2216 "%s: BGP OPEN receipt failed for peer: %s",
2217 __FUNCTION__
, peer
->host
);
2219 case BGP_MSG_UPDATE
:
2220 atomic_fetch_add_explicit(&peer
->update_in
, 1,
2221 memory_order_relaxed
);
2222 peer
->readtime
= monotime(NULL
);
2223 mprc
= bgp_update_receive(peer
, size
);
2224 if (mprc
== BGP_Stop
)
2226 "%s: BGP UPDATE receipt failed for peer: %s",
2227 __FUNCTION__
, peer
->host
);
2229 case BGP_MSG_NOTIFY
:
2230 atomic_fetch_add_explicit(&peer
->notify_in
, 1,
2231 memory_order_relaxed
);
2232 mprc
= bgp_notify_receive(peer
, size
);
2233 if (mprc
== BGP_Stop
)
2235 "%s: BGP NOTIFY receipt failed for peer: %s",
2236 __FUNCTION__
, peer
->host
);
2238 case BGP_MSG_KEEPALIVE
:
2239 peer
->readtime
= monotime(NULL
);
2240 atomic_fetch_add_explicit(&peer
->keepalive_in
, 1,
2241 memory_order_relaxed
);
2242 mprc
= bgp_keepalive_receive(peer
, size
);
2243 if (mprc
== BGP_Stop
)
2245 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2246 __FUNCTION__
, peer
->host
);
2248 case BGP_MSG_ROUTE_REFRESH_NEW
:
2249 case BGP_MSG_ROUTE_REFRESH_OLD
:
2250 atomic_fetch_add_explicit(&peer
->refresh_in
, 1,
2251 memory_order_relaxed
);
2252 mprc
= bgp_route_refresh_receive(peer
, size
);
2253 if (mprc
== BGP_Stop
)
2255 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2256 __FUNCTION__
, peer
->host
);
2258 case BGP_MSG_CAPABILITY
:
2259 atomic_fetch_add_explicit(&peer
->dynamic_cap_in
, 1,
2260 memory_order_relaxed
);
2261 mprc
= bgp_capability_receive(peer
, size
);
2262 if (mprc
== BGP_Stop
)
2264 "%s: BGP CAPABILITY receipt failed for peer: %s",
2265 __FUNCTION__
, peer
->host
);
2269 * The message type should have been sanitized before
2270 * we ever got here. Receipt of a message with an
2271 * invalid header at this point is indicative of a
2274 assert (!"Message of invalid type received during input processing");
2277 /* delete processed packet */
2278 stream_free(peer
->curr
);
2283 if (mprc
!= BGP_PACKET_NOOP
)
2284 fsm_update_result
= bgp_event_update(peer
, mprc
);
2289 * If peer was deleted, do not process any more packets. This
2290 * is usually due to executing BGP_Stop or a stub deletion.
2292 if (fsm_update_result
== FSM_PEER_TRANSFERRED
2293 || fsm_update_result
== FSM_PEER_STOPPED
)
2297 if (fsm_update_result
!= FSM_PEER_TRANSFERRED
2298 && fsm_update_result
!= FSM_PEER_STOPPED
) {
2299 pthread_mutex_lock(&peer
->io_mtx
);
2301 // more work to do, come back later
2302 if (peer
->ibuf
->count
> 0)
2303 thread_add_timer_msec(
2304 bm
->master
, bgp_process_packet
, peer
, 0,
2305 &peer
->t_process_packet
);
2307 pthread_mutex_unlock(&peer
->io_mtx
);