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_attr.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_fsm.h"
48 #include "bgpd/bgp_route.h"
49 #include "bgpd/bgp_packet.h"
50 #include "bgpd/bgp_open.h"
51 #include "bgpd/bgp_aspath.h"
52 #include "bgpd/bgp_community.h"
53 #include "bgpd/bgp_ecommunity.h"
54 #include "bgpd/bgp_lcommunity.h"
55 #include "bgpd/bgp_network.h"
56 #include "bgpd/bgp_mplsvpn.h"
57 #include "bgpd/bgp_evpn.h"
58 #include "bgpd/bgp_advertise.h"
59 #include "bgpd/bgp_vty.h"
60 #include "bgpd/bgp_updgrp.h"
61 #include "bgpd/bgp_label.h"
62 #include "bgpd/bgp_io.h"
63 #include "bgpd/bgp_keepalives.h"
64 #include "bgpd/bgp_flowspec.h"
67 * Sets marker and type fields for a BGP message.
69 * @param s the stream containing the packet
70 * @param type the packet type
71 * @return the size of the stream
73 int bgp_packet_set_marker(struct stream
*s
, uint8_t type
)
78 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
81 /* Dummy total length. This field is should be filled in later on. */
84 /* BGP packet type. */
87 /* Return current stream size. */
88 return stream_get_endp(s
);
92 * Sets size field for a BGP message.
94 * Size field is set to the size of the stream passed.
96 * @param s the stream containing the packet
97 * @return the size of the stream
99 int bgp_packet_set_size(struct stream
*s
)
103 /* Preserve current pointer. */
104 cp
= stream_get_endp(s
);
105 stream_putw_at(s
, BGP_MARKER_SIZE
, cp
);
111 * Push a packet onto the beginning of the peer's output queue.
112 * This function acquires the peer's write mutex before proceeding.
114 static void bgp_packet_add(struct peer
*peer
, struct stream
*s
)
116 pthread_mutex_lock(&peer
->io_mtx
);
117 stream_fifo_push(peer
->obuf
, s
);
118 pthread_mutex_unlock(&peer
->io_mtx
);
121 static struct stream
*bgp_update_packet_eor(struct peer
*peer
, afi_t afi
,
126 iana_safi_t pkt_safi
;
128 if (DISABLE_BGP_ANNOUNCE
)
131 if (bgp_debug_neighbor_events(peer
))
132 zlog_debug("send End-of-RIB for %s to %s",
133 afi_safi_print(afi
, safi
), peer
->host
);
135 s
= stream_new(BGP_MAX_PACKET_SIZE
);
137 /* Make BGP update packet. */
138 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
140 /* Unfeasible Routes Length */
143 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
144 /* Total Path Attribute Length */
147 /* Convert AFI, SAFI to values for packet. */
148 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
150 /* Total Path Attribute Length */
152 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
153 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
155 stream_putw(s
, pkt_afi
);
156 stream_putc(s
, pkt_safi
);
159 bgp_packet_set_size(s
);
163 /* Called when there is a change in the EOR(implicit or explicit) status of a
164 * peer. Ends the update-delay if all expected peers are done with EORs. */
165 void bgp_check_update_delay(struct bgp
*bgp
)
167 struct listnode
*node
, *nnode
;
168 struct peer
*peer
= NULL
;
170 if (bgp_debug_neighbor_events(peer
))
171 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
172 bgp
->established
, bgp
->restarted_peers
,
173 bgp
->implicit_eors
, bgp
->explicit_eors
);
176 <= bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
) {
178 * This is an extra sanity check to make sure we wait for all
179 * the eligible configured peers. This check is performed if
180 * establish wait timer is on, or establish wait option is not
181 * given with the update-delay command
183 if (bgp
->t_establish_wait
184 || (bgp
->v_establish_wait
== bgp
->v_update_delay
))
185 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
186 if (CHECK_FLAG(peer
->flags
,
187 PEER_FLAG_CONFIG_NODE
)
188 && !CHECK_FLAG(peer
->flags
,
190 && !peer
->update_delay_over
) {
191 if (bgp_debug_neighbor_events(peer
))
193 " Peer %s pending, continuing read-only mode",
200 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
201 bgp
->restarted_peers
, bgp
->implicit_eors
,
203 bgp_update_delay_end(bgp
);
208 * Called if peer is known to have restarted. The restart-state bit in
209 * Graceful-Restart capability is used for that
211 void bgp_update_restarted_peers(struct peer
*peer
)
213 if (!bgp_update_delay_active(peer
->bgp
))
214 return; /* BGP update delay has ended */
215 if (peer
->update_delay_over
)
216 return; /* This peer has already been considered */
218 if (bgp_debug_neighbor_events(peer
))
219 zlog_debug("Peer %s: Checking restarted", peer
->host
);
221 if (peer
->status
== Established
) {
222 peer
->update_delay_over
= 1;
223 peer
->bgp
->restarted_peers
++;
224 bgp_check_update_delay(peer
->bgp
);
229 * Called as peer receives a keep-alive. Determines if this occurence can be
230 * taken as an implicit EOR for this peer.
231 * NOTE: The very first keep-alive after the Established state of a peer is
232 * considered implicit EOR for the update-delay purposes
234 void bgp_update_implicit_eors(struct peer
*peer
)
236 if (!bgp_update_delay_active(peer
->bgp
))
237 return; /* BGP update delay has ended */
238 if (peer
->update_delay_over
)
239 return; /* This peer has already been considered */
241 if (bgp_debug_neighbor_events(peer
))
242 zlog_debug("Peer %s: Checking implicit EORs", peer
->host
);
244 if (peer
->status
== Established
) {
245 peer
->update_delay_over
= 1;
246 peer
->bgp
->implicit_eors
++;
247 bgp_check_update_delay(peer
->bgp
);
252 * Should be called only when there is a change in the EOR_RECEIVED status
253 * for any afi/safi on a peer.
255 static void bgp_update_explicit_eors(struct peer
*peer
)
260 if (!bgp_update_delay_active(peer
->bgp
))
261 return; /* BGP update delay has ended */
262 if (peer
->update_delay_over
)
263 return; /* This peer has already been considered */
265 if (bgp_debug_neighbor_events(peer
))
266 zlog_debug("Peer %s: Checking explicit EORs", peer
->host
);
268 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
269 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
270 if (peer
->afc_nego
[afi
][safi
]
271 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
272 PEER_STATUS_EOR_RECEIVED
)) {
273 if (bgp_debug_neighbor_events(peer
))
275 " afi %d safi %d didn't receive EOR",
281 peer
->update_delay_over
= 1;
282 peer
->bgp
->explicit_eors
++;
283 bgp_check_update_delay(peer
->bgp
);
287 * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
289 * mp_withdraw, if set, is used to nullify attr structure on most of the
290 * calling safi function and for evpn, passed as parameter
292 int bgp_nlri_parse(struct peer
*peer
, struct attr
*attr
,
293 struct bgp_nlri
*packet
, int mp_withdraw
)
295 switch (packet
->safi
) {
298 return bgp_nlri_parse_ip(peer
, mp_withdraw
? NULL
: attr
,
300 case SAFI_LABELED_UNICAST
:
301 return bgp_nlri_parse_label(peer
, mp_withdraw
? NULL
: attr
,
304 return bgp_nlri_parse_vpn(peer
, mp_withdraw
? NULL
: attr
,
307 return bgp_nlri_parse_evpn(peer
, attr
, packet
, mp_withdraw
);
309 return bgp_nlri_parse_flowspec(peer
, attr
, packet
, mp_withdraw
);
311 return BGP_NLRI_PARSE_ERROR
;
315 * Checks a variety of conditions to determine whether the peer needs to be
316 * rescheduled for packet generation again, and does so if necessary.
318 * @param peer to check for rescheduling
320 static void bgp_write_proceed_actions(struct peer
*peer
)
325 struct bpacket
*next_pkt
;
326 struct update_subgroup
*subgrp
;
328 FOREACH_AFI_SAFI (afi
, safi
) {
329 paf
= peer_af_find(peer
, afi
, safi
);
332 subgrp
= paf
->subgroup
;
336 next_pkt
= paf
->next_pkt_to_send
;
337 if (next_pkt
&& next_pkt
->buffer
) {
338 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
339 bgp_generate_updgrp_packets
, 0);
343 /* No packets readily available for AFI/SAFI, are there
345 * that need to be generated? */
346 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
),
348 || subgroup_packets_to_build(subgrp
)) {
349 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
350 bgp_generate_updgrp_packets
, 0);
354 /* No packets to send, see if EOR is pending */
355 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
356 if (!subgrp
->t_coalesce
&& peer
->afc_nego
[afi
][safi
]
358 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
359 PEER_STATUS_EOR_SEND
)
360 && safi
!= SAFI_MPLS_VPN
) {
361 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
362 bgp_generate_updgrp_packets
, 0);
370 * Generate advertisement information (withdraws, updates, EOR) from each
371 * update group a peer belongs to, encode this information into packets, and
372 * enqueue the packets onto the peer's output buffer.
374 int bgp_generate_updgrp_packets(struct thread
*thread
)
376 struct peer
*peer
= THREAD_ARG(thread
);
380 struct bpacket
*next_pkt
;
382 uint32_t generated
= 0;
386 wpq
= atomic_load_explicit(&peer
->bgp
->wpkt_quanta
,
387 memory_order_relaxed
);
390 * The code beyond this part deals with update packets, proceed only
391 * if peer is Established and updates are not on hold (as part of
392 * update-delay post processing).
394 if (peer
->status
!= Established
)
397 if (peer
->bgp
->main_peers_update_hold
)
402 FOREACH_AFI_SAFI (afi
, safi
) {
403 paf
= peer_af_find(peer
, afi
, safi
);
404 if (!paf
|| !PAF_SUBGRP(paf
))
406 next_pkt
= paf
->next_pkt_to_send
;
409 * Try to generate a packet for the peer if we are at
410 * the end of the list. Always try to push out
413 if (!next_pkt
|| !next_pkt
->buffer
) {
414 next_pkt
= subgroup_withdraw_packet(
416 if (!next_pkt
|| !next_pkt
->buffer
)
417 subgroup_update_packet(PAF_SUBGRP(paf
));
418 next_pkt
= paf
->next_pkt_to_send
;
422 * If we still don't have a packet to send to the peer,
423 * then try to find out out if we have to send eor or
424 * if not, skip to the next AFI, SAFI. Don't send the
425 * EOR prematurely; if the subgroup's coalesce timer is
426 * running, the adjacency-out structure is not created
429 if (!next_pkt
|| !next_pkt
->buffer
) {
430 if (CHECK_FLAG(peer
->cap
,
431 PEER_CAP_RESTART_RCV
)) {
432 if (!(PAF_SUBGRP(paf
))->t_coalesce
433 && peer
->afc_nego
[afi
][safi
]
438 PEER_STATUS_EOR_SEND
)) {
439 SET_FLAG(peer
->af_sflags
[afi
]
441 PEER_STATUS_EOR_SEND
);
443 if ((s
= bgp_update_packet_eor(
446 bgp_packet_add(peer
, s
);
454 /* Found a packet template to send, overwrite
455 * packet with appropriate attributes from peer
456 * and advance peer */
457 s
= bpacket_reformat_for_peer(next_pkt
, paf
);
458 bgp_packet_add(peer
, s
);
459 bpacket_queue_advance_peer(paf
);
461 } while (s
&& (++generated
< wpq
));
466 bgp_write_proceed_actions(peer
);
472 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
474 void bgp_keepalive_send(struct peer
*peer
)
478 s
= stream_new(BGP_MAX_PACKET_SIZE
);
480 /* Make keepalive packet. */
481 bgp_packet_set_marker(s
, BGP_MSG_KEEPALIVE
);
483 /* Set packet size. */
484 (void)bgp_packet_set_size(s
);
486 /* Dump packet if debug option is set. */
487 /* bgp_packet_dump (s); */
489 if (bgp_debug_keepalive(peer
))
490 zlog_debug("%s sending KEEPALIVE", peer
->host
);
492 /* Add packet to the peer. */
493 bgp_packet_add(peer
, s
);
499 * Creates a BGP Open packet and appends it to the peer's output queue.
500 * Sets capabilities as necessary.
502 void bgp_open_send(struct peer
*peer
)
505 uint16_t send_holdtime
;
508 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
509 send_holdtime
= peer
->holdtime
;
511 send_holdtime
= peer
->bgp
->default_holdtime
;
513 /* local-as Change */
514 if (peer
->change_local_as
)
515 local_as
= peer
->change_local_as
;
517 local_as
= peer
->local_as
;
519 s
= stream_new(BGP_MAX_PACKET_SIZE
);
521 /* Make open packet. */
522 bgp_packet_set_marker(s
, BGP_MSG_OPEN
);
524 /* Set open packet values. */
525 stream_putc(s
, BGP_VERSION_4
); /* BGP version */
526 stream_putw(s
, (local_as
<= BGP_AS_MAX
) ? (uint16_t)local_as
528 stream_putw(s
, send_holdtime
); /* Hold Time */
529 stream_put_in_addr(s
, &peer
->local_id
); /* BGP Identifier */
531 /* Set capability code. */
532 bgp_open_capability(s
, peer
);
534 /* Set BGP packet length. */
535 (void)bgp_packet_set_size(s
);
537 if (bgp_debug_neighbor_events(peer
))
539 "%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
540 peer
->host
, BGP_VERSION_4
, local_as
, send_holdtime
,
541 inet_ntoa(peer
->local_id
));
543 /* Dump packet if debug option is set. */
544 /* bgp_packet_dump (s); */
546 /* Add packet to the peer. */
547 bgp_packet_add(peer
, s
);
553 * Writes NOTIFICATION message directly to a peer socket without waiting for
556 * There must be exactly one stream on the peer->obuf FIFO, and the data within
557 * this stream must match the format of a BGP NOTIFICATION message.
558 * Transmission is best-effort.
560 * @requires peer->io_mtx
564 static int bgp_write_notify(struct peer
*peer
)
570 /* There should be at least one packet. */
571 s
= stream_fifo_pop(peer
->obuf
);
576 assert(stream_get_endp(s
) >= BGP_HEADER_SIZE
);
578 /* Stop collecting data within the socket */
579 sockopt_cork(peer
->fd
, 0);
582 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
583 * we only care about getting a clean shutdown at this point.
585 ret
= write(peer
->fd
, STREAM_DATA(s
), stream_get_endp(s
));
588 * only connection reset/close gets counted as TCP_fatal_error, failure
589 * to write the entire NOTIFY doesn't get different FSM treatment
593 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
597 /* Disable Nagle, make NOTIFY packet go out right away */
599 (void)setsockopt(peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
,
602 /* Retrieve BGP packet type. */
603 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
604 type
= stream_getc(s
);
606 assert(type
== BGP_MSG_NOTIFY
);
608 /* Type should be notify. */
609 atomic_fetch_add_explicit(&peer
->notify_out
, 1, memory_order_relaxed
);
612 /* Double start timer. */
615 /* Overflow check. */
616 if (peer
->v_start
>= (60 * 2))
617 peer
->v_start
= (60 * 2);
620 * Handle Graceful Restart case where the state changes to
621 * Connect instead of Idle
623 BGP_EVENT_ADD(peer
, BGP_Stop
);
631 * Creates a BGP Notify and appends it to the peer's output queue.
633 * This function attempts to write the packet from the thread it is called
634 * from, to ensure the packet gets out ASAP.
636 * This function may be called from multiple threads. Since the function
637 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
638 * call to prevent tampering from other threads.
640 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
641 * return, the peer structure *must* be reset; no assumptions about session
645 * @param code BGP error code
646 * @param sub_code BGP error subcode
647 * @param data Data portion
648 * @param datalen length of data portion
650 void bgp_notify_send_with_data(struct peer
*peer
, uint8_t code
,
651 uint8_t sub_code
, uint8_t *data
, size_t datalen
)
655 /* Lock I/O mutex to prevent other threads from pushing packets */
656 pthread_mutex_lock(&peer
->io_mtx
);
657 /* ============================================== */
659 /* Allocate new stream. */
660 s
= stream_new(BGP_MAX_PACKET_SIZE
);
662 /* Make notify packet. */
663 bgp_packet_set_marker(s
, BGP_MSG_NOTIFY
);
665 /* Set notify packet values. */
666 stream_putc(s
, code
); /* BGP notify code */
667 stream_putc(s
, sub_code
); /* BGP notify sub_code */
669 /* If notify data is present. */
671 stream_write(s
, data
, datalen
);
673 /* Set BGP packet length. */
674 bgp_packet_set_size(s
);
676 /* wipe output buffer */
677 stream_fifo_clean(peer
->obuf
);
680 * If possible, store last packet for debugging purposes. This check is
681 * in place because we are sometimes called with a doppelganger peer,
682 * who tends to have a plethora of fields nulled out.
684 if (peer
->curr
&& peer
->last_reset_cause_size
) {
685 size_t packetsize
= stream_get_endp(peer
->curr
);
686 assert(packetsize
<= peer
->last_reset_cause_size
);
687 memcpy(peer
->last_reset_cause
, peer
->curr
->data
, packetsize
);
688 peer
->last_reset_cause_size
= packetsize
;
693 struct bgp_notify bgp_notify
;
698 bgp_notify
.code
= code
;
699 bgp_notify
.subcode
= sub_code
;
700 bgp_notify
.data
= NULL
;
701 bgp_notify
.length
= datalen
;
702 bgp_notify
.raw_data
= data
;
704 peer
->notify
.code
= bgp_notify
.code
;
705 peer
->notify
.subcode
= bgp_notify
.subcode
;
707 if (bgp_notify
.length
&& data
) {
709 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
710 for (i
= 0; i
< bgp_notify
.length
; i
++)
712 sprintf(c
, " %02x", data
[i
]);
713 strcat(bgp_notify
.data
, c
);
716 sprintf(c
, "%02x", data
[i
]);
717 strcpy(bgp_notify
.data
, c
);
720 bgp_notify_print(peer
, &bgp_notify
, "sending");
722 if (bgp_notify
.data
) {
723 XFREE(MTYPE_TMP
, bgp_notify
.data
);
724 bgp_notify
.data
= NULL
;
725 bgp_notify
.length
= 0;
729 /* peer reset cause */
730 if (code
== BGP_NOTIFY_CEASE
) {
731 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
732 peer
->last_reset
= PEER_DOWN_USER_RESET
;
733 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
734 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
736 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
738 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
740 /* Add packet to peer's output queue */
741 stream_fifo_push(peer
->obuf
, s
);
743 bgp_write_notify(peer
);
745 /* ============================================== */
746 pthread_mutex_unlock(&peer
->io_mtx
);
750 * Creates a BGP Notify and appends it to the peer's output queue.
752 * This function attempts to write the packet from the thread it is called
753 * from, to ensure the packet gets out ASAP.
756 * @param code BGP error code
757 * @param sub_code BGP error subcode
759 void bgp_notify_send(struct peer
*peer
, uint8_t code
, uint8_t sub_code
)
761 bgp_notify_send_with_data(peer
, code
, sub_code
, NULL
, 0);
765 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
768 * @param afi Address Family Identifier
769 * @param safi Subsequent Address Family Identifier
770 * @param orf_type Outbound Route Filtering type
771 * @param when_to_refresh Whether to refresh immediately or defer
772 * @param remove Whether to remove ORF for specified AFI/SAFI
774 void bgp_route_refresh_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
775 uint8_t orf_type
, uint8_t when_to_refresh
,
779 struct bgp_filter
*filter
;
782 iana_safi_t pkt_safi
;
784 if (DISABLE_BGP_ANNOUNCE
)
787 filter
= &peer
->filter
[afi
][safi
];
789 /* Convert AFI, SAFI to values for packet. */
790 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
792 s
= stream_new(BGP_MAX_PACKET_SIZE
);
794 /* Make BGP update packet. */
795 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
796 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_NEW
);
798 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_OLD
);
800 /* Encode Route Refresh message. */
801 stream_putw(s
, pkt_afi
);
803 stream_putc(s
, pkt_safi
);
805 if (orf_type
== ORF_TYPE_PREFIX
|| orf_type
== ORF_TYPE_PREFIX_OLD
)
806 if (remove
|| filter
->plist
[FILTER_IN
].plist
) {
811 stream_putc(s
, when_to_refresh
);
812 stream_putc(s
, orf_type
);
813 orfp
= stream_get_endp(s
);
817 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
818 PEER_STATUS_ORF_PREFIX_SEND
);
819 stream_putc(s
, ORF_COMMON_PART_REMOVE_ALL
);
820 if (bgp_debug_neighbor_events(peer
))
822 "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
823 peer
->host
, orf_type
,
824 (when_to_refresh
== REFRESH_DEFER
829 SET_FLAG(peer
->af_sflags
[afi
][safi
],
830 PEER_STATUS_ORF_PREFIX_SEND
);
831 prefix_bgp_orf_entry(
832 s
, filter
->plist
[FILTER_IN
].plist
,
834 ORF_COMMON_PART_PERMIT
,
835 ORF_COMMON_PART_DENY
);
836 if (bgp_debug_neighbor_events(peer
))
838 "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
839 peer
->host
, orf_type
,
840 (when_to_refresh
== REFRESH_DEFER
846 /* Total ORF Entry Len. */
847 orf_len
= stream_get_endp(s
) - orfp
- 2;
848 stream_putw_at(s
, orfp
, orf_len
);
851 /* Set packet size. */
852 (void)bgp_packet_set_size(s
);
854 if (bgp_debug_neighbor_events(peer
)) {
856 zlog_debug("%s sending REFRESH_REQ for afi/safi: %d/%d",
857 peer
->host
, pkt_afi
, pkt_safi
);
860 /* Add packet to the peer. */
861 bgp_packet_add(peer
, s
);
867 * Create a BGP Capability packet and append it to the peer's output queue.
870 * @param afi Address Family Identifier
871 * @param safi Subsequent Address Family Identifier
872 * @param capability_code BGP Capability Code
873 * @param action Set or Remove capability
875 void bgp_capability_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
876 int capability_code
, int action
)
880 iana_safi_t pkt_safi
;
882 /* Convert AFI, SAFI to values for packet. */
883 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
885 s
= stream_new(BGP_MAX_PACKET_SIZE
);
887 /* Make BGP update packet. */
888 bgp_packet_set_marker(s
, BGP_MSG_CAPABILITY
);
890 /* Encode MP_EXT capability. */
891 if (capability_code
== CAPABILITY_CODE_MP
) {
892 stream_putc(s
, action
);
893 stream_putc(s
, CAPABILITY_CODE_MP
);
894 stream_putc(s
, CAPABILITY_CODE_MP_LEN
);
895 stream_putw(s
, pkt_afi
);
897 stream_putc(s
, pkt_safi
);
899 if (bgp_debug_neighbor_events(peer
))
901 "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
903 action
== CAPABILITY_ACTION_SET
? "Advertising"
908 /* Set packet size. */
909 (void)bgp_packet_set_size(s
);
911 /* Add packet to the peer. */
912 bgp_packet_add(peer
, s
);
917 /* RFC1771 6.8 Connection collision detection. */
918 static int bgp_collision_detect(struct peer
*new, struct in_addr remote_id
)
922 /* Upon receipt of an OPEN message, the local system must examine
923 all of its connections that are in the OpenConfirm state. A BGP
924 speaker may also examine connections in an OpenSent state if it
925 knows the BGP Identifier of the peer by means outside of the
926 protocol. If among these connections there is a connection to a
927 remote BGP speaker whose BGP Identifier equals the one in the
928 OPEN message, then the local system performs the following
929 collision resolution procedure: */
931 if ((peer
= new->doppelganger
) != NULL
) {
932 /* Do not accept the new connection in Established or Clearing
934 * Note that a peer GR is handled by closing the existing
936 * upon receipt of new one.
938 if (peer
->status
== Established
|| peer
->status
== Clearing
) {
939 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
940 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
942 } else if ((peer
->status
== OpenConfirm
)
943 || (peer
->status
== OpenSent
)) {
944 /* 1. The BGP Identifier of the local system is compared
946 the BGP Identifier of the remote system (as specified
948 the OPEN message). */
950 if (ntohl(peer
->local_id
.s_addr
)
951 < ntohl(remote_id
.s_addr
))
952 if (!CHECK_FLAG(peer
->sflags
,
953 PEER_STATUS_ACCEPT_PEER
)) {
954 /* 2. If the value of the local BGP
956 than the remote one, the local system
958 connection that already exists (the
960 already in the OpenConfirm state),
962 connection initiated by the remote
965 peer
, BGP_NOTIFY_CEASE
,
966 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
970 new, BGP_NOTIFY_CEASE
,
971 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
975 /* 3. Otherwise, the local system closes newly
977 BGP connection (the one associated with the
979 received OPEN message), and continues to use
981 existing one (the one that is already in the
982 OpenConfirm state). */
983 if (CHECK_FLAG(peer
->sflags
,
984 PEER_STATUS_ACCEPT_PEER
)) {
986 peer
, BGP_NOTIFY_CEASE
,
987 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
991 new, BGP_NOTIFY_CEASE
,
992 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1001 /* Packet processing routines ---------------------------------------------- */
1003 * This is a family of functions designed to be called from
1004 * bgp_process_packet(). These functions all share similar behavior and should
1005 * adhere to the following invariants and restrictions:
1009 * The return code of any one of those functions should be one of the FSM event
1010 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1011 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1012 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1013 * error and Receive_OPEN_message otherwise.
1015 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1020 * - May send NOTIFY messages
1021 * - May not modify peer->status
1022 * - May not call bgp_event_update()
1025 #define BGP_PACKET_NOOP 0
1028 * Process BGP OPEN message for peer.
1030 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1031 * and returns BGP_Stop.
1034 * @param size size of the packet
1035 * @return as in summary
1037 static int bgp_open_receive(struct peer
*peer
, bgp_size_t size
)
1043 uint16_t send_holdtime
;
1046 struct in_addr remote_id
;
1048 uint8_t notify_data_remote_as
[2];
1049 uint8_t notify_data_remote_as4
[4];
1050 uint8_t notify_data_remote_id
[4];
1051 uint16_t *holdtime_ptr
;
1053 /* Parse open packet. */
1054 version
= stream_getc(peer
->curr
);
1055 memcpy(notify_data_remote_as
, stream_pnt(peer
->curr
), 2);
1056 remote_as
= stream_getw(peer
->curr
);
1057 holdtime_ptr
= (uint16_t *)stream_pnt(peer
->curr
);
1058 holdtime
= stream_getw(peer
->curr
);
1059 memcpy(notify_data_remote_id
, stream_pnt(peer
->curr
), 4);
1060 remote_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1062 /* Receive OPEN message log */
1063 if (bgp_debug_neighbor_events(peer
))
1065 "%s rcv OPEN, version %d, remote-as (in open) %u,"
1066 " holdtime %d, id %s",
1067 peer
->host
, version
, remote_as
, holdtime
,
1068 inet_ntoa(remote_id
));
1070 /* BEGIN to read the capability here, but dont do it yet */
1072 optlen
= stream_getc(peer
->curr
);
1075 /* If not enough bytes, it is an error. */
1076 if (STREAM_READABLE(peer
->curr
) < optlen
) {
1077 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1078 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1082 /* We need the as4 capability value *right now* because
1083 * if it is there, we have not got the remote_as yet, and
1085 * that we do not know which peer is connecting to us now.
1087 as4
= peek_for_as4_capability(peer
, optlen
);
1088 memcpy(notify_data_remote_as4
, &as4
, 4);
1091 /* Just in case we have a silly peer who sends AS4 capability set to 0
1093 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
) {
1094 flog_err(EC_BGP_PKT_OPEN
,
1095 "%s bad OPEN, got AS4 capability, but AS4 set to 0",
1097 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1098 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1099 notify_data_remote_as4
, 4);
1103 if (remote_as
== BGP_AS_TRANS
) {
1104 /* Take the AS4 from the capability. We must have received the
1105 * capability now! Otherwise we have a asn16 peer who uses
1106 * BGP_AS_TRANS, for some unknown reason.
1108 if (as4
== BGP_AS_TRANS
) {
1111 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1113 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1114 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1115 notify_data_remote_as4
, 4);
1119 if (!as4
&& BGP_DEBUG(as4
, AS4
))
1121 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
1122 " Odd, but proceeding.",
1124 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG(as4
, AS4
))
1126 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
1127 "in 2-bytes, very odd peer.",
1132 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1134 /* If we have got the capability, peer->as4cap must match
1136 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)
1137 && as4
!= remote_as
) {
1138 /* raise error, log this, close session */
1141 "%s bad OPEN, got AS4 capability, but remote_as %u"
1142 " mismatch with 16bit 'myasn' %u in open",
1143 peer
->host
, as4
, remote_as
);
1144 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1145 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1146 notify_data_remote_as4
, 4);
1151 /* remote router-id check. */
1152 if (remote_id
.s_addr
== 0 || IPV4_CLASS_DE(ntohl(remote_id
.s_addr
))
1153 || ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)) {
1154 if (bgp_debug_neighbor_events(peer
))
1155 zlog_debug("%s bad OPEN, wrong router identifier %s",
1156 peer
->host
, inet_ntoa(remote_id
));
1157 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1158 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1159 notify_data_remote_id
, 4);
1163 /* Set remote router-id */
1164 peer
->remote_id
= remote_id
;
1166 /* Peer BGP version check. */
1167 if (version
!= BGP_VERSION_4
) {
1168 uint16_t maxver
= htons(BGP_VERSION_4
);
1169 /* XXX this reply may not be correct if version < 4 XXX */
1170 if (bgp_debug_neighbor_events(peer
))
1172 "%s bad protocol version, remote requested %d, local request %d",
1173 peer
->host
, version
, BGP_VERSION_4
);
1174 /* Data must be in network byte order here */
1175 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1176 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1177 (uint8_t *)&maxver
, 2);
1181 /* Check neighbor as number. */
1182 if (peer
->as_type
== AS_UNSPECIFIED
) {
1183 if (bgp_debug_neighbor_events(peer
))
1185 "%s bad OPEN, remote AS is unspecified currently",
1187 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1188 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1189 notify_data_remote_as
, 2);
1191 } else if (peer
->as_type
== AS_INTERNAL
) {
1192 if (remote_as
!= peer
->bgp
->as
) {
1193 if (bgp_debug_neighbor_events(peer
))
1195 "%s bad OPEN, remote AS is %u, internal specified",
1196 peer
->host
, remote_as
);
1197 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1198 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1199 notify_data_remote_as
, 2);
1202 peer
->as
= peer
->local_as
;
1203 } else if (peer
->as_type
== AS_EXTERNAL
) {
1204 if (remote_as
== peer
->bgp
->as
) {
1205 if (bgp_debug_neighbor_events(peer
))
1207 "%s bad OPEN, remote AS is %u, external specified",
1208 peer
->host
, remote_as
);
1209 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1210 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1211 notify_data_remote_as
, 2);
1214 peer
->as
= remote_as
;
1215 } else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
)) {
1216 if (bgp_debug_neighbor_events(peer
))
1217 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1218 peer
->host
, remote_as
, peer
->as
);
1219 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1220 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1221 notify_data_remote_as
, 2);
1225 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1226 calculate the value of the Hold Timer by using the smaller of its
1227 configured Hold Time and the Hold Time received in the OPEN message.
1228 The Hold Time MUST be either zero or at least three seconds. An
1229 implementation may reject connections on the basis of the Hold Time.
1232 if (holdtime
< 3 && holdtime
!= 0) {
1233 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1234 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1235 (uint8_t *)holdtime_ptr
, 2);
1239 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1240 would be one third of the Hold Time interval. KEEPALIVE messages
1241 MUST NOT be sent more frequently than one per second. An
1242 implementation MAY adjust the rate at which it sends KEEPALIVE
1243 messages as a function of the Hold Time interval. */
1245 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
1246 send_holdtime
= peer
->holdtime
;
1248 send_holdtime
= peer
->bgp
->default_holdtime
;
1250 if (holdtime
< send_holdtime
)
1251 peer
->v_holdtime
= holdtime
;
1253 peer
->v_holdtime
= send_holdtime
;
1255 if ((CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER
))
1256 && (peer
->keepalive
< peer
->v_holdtime
/ 3))
1257 peer
->v_keepalive
= peer
->keepalive
;
1259 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1261 /* Open option part parse. */
1263 if ((ret
= bgp_open_option_parse(peer
, optlen
, &mp_capability
))
1267 if (bgp_debug_neighbor_events(peer
))
1268 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1273 * Assume that the peer supports the locally configured set of
1274 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1275 * capabilities, or if 'override-capability' is configured.
1278 || CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
1279 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] =
1280 peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1281 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] =
1282 peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1283 peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
] =
1284 peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
];
1285 peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
] =
1286 peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
];
1287 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] =
1288 peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1289 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] =
1290 peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1291 peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
] =
1292 peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
];
1293 peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
] =
1294 peer
->afc
[AFI_L2VPN
][SAFI_EVPN
];
1295 peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
] =
1296 peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
];
1299 /* When collision is detected and this peer is closed. Retrun
1301 ret
= bgp_collision_detect(peer
, remote_id
);
1306 if ((ret
= bgp_getsockname(peer
)) < 0) {
1307 flog_err_sys(EC_LIB_SOCKET
,
1308 "%s: bgp_getsockname() failed for peer: %s",
1309 __FUNCTION__
, peer
->host
);
1313 /* Verify valid local address present based on negotiated
1314 * address-families. */
1315 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
1316 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
1317 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
1318 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
1319 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]) {
1320 if (!peer
->nexthop
.v4
.s_addr
) {
1321 #if defined(HAVE_CUMULUS)
1324 "%s: No local IPv4 addr resetting connection, fd %d",
1325 peer
->host
, peer
->fd
);
1326 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1327 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1332 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
1333 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
1334 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
1335 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
1336 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]) {
1337 if (IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_global
)) {
1338 #if defined(HAVE_CUMULUS)
1341 "%s: No local IPv6 addr resetting connection, fd %d",
1342 peer
->host
, peer
->fd
);
1343 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1344 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1349 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1351 return Receive_OPEN_message
;
1355 * Process BGP KEEPALIVE message for peer.
1358 * @param size size of the packet
1359 * @return as in summary
1361 static int bgp_keepalive_receive(struct peer
*peer
, bgp_size_t size
)
1363 if (bgp_debug_keepalive(peer
))
1364 zlog_debug("%s KEEPALIVE rcvd", peer
->host
);
1366 bgp_update_implicit_eors(peer
);
1368 return Receive_KEEPALIVE_message
;
1373 * Process BGP UPDATE message for peer.
1375 * Parses UPDATE and creates attribute object.
1378 * @param size size of the packet
1379 * @return as in summary
1381 static int bgp_update_receive(struct peer
*peer
, bgp_size_t size
)
1387 bgp_size_t attribute_len
;
1388 bgp_size_t update_len
;
1389 bgp_size_t withdraw_len
;
1398 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1400 /* Status must be Established. */
1401 if (peer
->status
!= Established
) {
1402 flog_err(EC_BGP_INVALID_STATUS
,
1403 "%s [FSM] Update packet received under status %s",
1405 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1406 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1410 /* Set initial values. */
1411 memset(&attr
, 0, sizeof(struct attr
));
1412 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
1413 attr
.label
= MPLS_INVALID_LABEL
;
1414 memset(&nlris
, 0, sizeof(nlris
));
1415 memset(peer
->rcvd_attr_str
, 0, BUFSIZ
);
1416 peer
->rcvd_attr_printed
= 0;
1419 end
= stream_pnt(s
) + size
;
1421 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1422 Length is too large (i.e., if Unfeasible Routes Length + Total
1423 Attribute Length + 23 exceeds the message Length), then the Error
1424 Subcode is set to Malformed Attribute List. */
1425 if (stream_pnt(s
) + 2 > end
) {
1426 flog_err(EC_BGP_UPDATE_RCV
,
1427 "%s [Error] Update packet error"
1428 " (packet length is short for unfeasible length)",
1430 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1431 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1435 /* Unfeasible Route Length. */
1436 withdraw_len
= stream_getw(s
);
1438 /* Unfeasible Route Length check. */
1439 if (stream_pnt(s
) + withdraw_len
> end
) {
1440 flog_err(EC_BGP_UPDATE_RCV
,
1441 "%s [Error] Update packet error"
1442 " (packet unfeasible length overflow %d)",
1443 peer
->host
, withdraw_len
);
1444 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1445 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1449 /* Unfeasible Route packet format check. */
1450 if (withdraw_len
> 0) {
1451 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1452 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1453 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt(s
);
1454 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1455 stream_forward_getp(s
, withdraw_len
);
1458 /* Attribute total length check. */
1459 if (stream_pnt(s
) + 2 > end
) {
1461 EC_BGP_UPDATE_PACKET_SHORT
,
1462 "%s [Error] Packet Error (update packet is short for attribute length)",
1464 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1465 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1469 /* Fetch attribute total length. */
1470 attribute_len
= stream_getw(s
);
1472 /* Attribute length check. */
1473 if (stream_pnt(s
) + attribute_len
> end
) {
1475 EC_BGP_UPDATE_PACKET_LONG
,
1476 "%s [Error] Packet Error (update packet attribute length overflow %d)",
1477 peer
->host
, attribute_len
);
1478 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1479 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1483 /* Certain attribute parsing errors should not be considered bad enough
1484 * to reset the session for, most particularly any partial/optional
1485 * attributes that have 'tunneled' over speakers that don't understand
1486 * them. Instead we withdraw only the prefix concerned.
1488 * Complicates the flow a little though..
1490 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1491 /* This define morphs the update case into a withdraw when lower levels
1492 * have signalled an error condition where this is best.
1494 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1496 /* Parse attribute when it exists. */
1497 if (attribute_len
) {
1498 attr_parse_ret
= bgp_attr_parse(peer
, &attr
, attribute_len
,
1499 &nlris
[NLRI_MP_UPDATE
],
1500 &nlris
[NLRI_MP_WITHDRAW
]);
1501 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
) {
1502 bgp_attr_unintern_sub(&attr
);
1507 /* Logging the attribute. */
1508 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1509 || BGP_DEBUG(update
, UPDATE_IN
)
1510 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
1511 ret
= bgp_dump_attr(&attr
, peer
->rcvd_attr_str
, BUFSIZ
);
1513 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1516 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1519 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1)) {
1520 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
1521 peer
->rcvd_attr_str
);
1522 peer
->rcvd_attr_printed
= 1;
1526 /* Network Layer Reachability Information. */
1527 update_len
= end
- stream_pnt(s
);
1530 /* Set NLRI portion to structure. */
1531 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1532 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1533 nlris
[NLRI_UPDATE
].nlri
= stream_pnt(s
);
1534 nlris
[NLRI_UPDATE
].length
= update_len
;
1535 stream_forward_getp(s
, update_len
);
1538 if (BGP_DEBUG(update
, UPDATE_IN
))
1539 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1540 peer
->host
, withdraw_len
, attribute_len
, update_len
);
1542 /* Parse any given NLRIs */
1543 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++) {
1547 /* NLRI is processed iff the peer if configured for the specific
1549 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
]) {
1551 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1552 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1556 /* EoR handled later */
1557 if (nlris
[i
].length
== 0)
1562 case NLRI_MP_UPDATE
:
1563 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
1567 case NLRI_MP_WITHDRAW
:
1568 nlri_ret
= bgp_nlri_parse(peer
, &attr
, &nlris
[i
], 1);
1571 nlri_ret
= BGP_NLRI_PARSE_ERROR
;
1574 if (nlri_ret
< BGP_NLRI_PARSE_OK
1575 && nlri_ret
!= BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW
) {
1576 flog_err(EC_BGP_UPDATE_RCV
,
1577 "%s [Error] Error parsing NLRI", peer
->host
);
1578 if (peer
->status
== Established
)
1580 peer
, BGP_NOTIFY_UPDATE_ERR
,
1582 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1583 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1584 bgp_attr_unintern_sub(&attr
);
1591 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1592 * and MP EoR should have only an empty MP_UNREACH
1594 if ((!update_len
&& !withdraw_len
&& nlris
[NLRI_MP_UPDATE
].length
== 0)
1595 || (attr_parse_ret
== BGP_ATTR_PARSE_EOR
)) {
1599 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1601 * update and withdraw NLRI lengths are 0.
1603 if (!attribute_len
) {
1605 safi
= SAFI_UNICAST
;
1606 } else if (attr
.flag
& ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
)
1607 && nlris
[NLRI_MP_WITHDRAW
].length
== 0) {
1608 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1609 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1610 } else if (attr_parse_ret
== BGP_ATTR_PARSE_EOR
) {
1611 afi
= nlris
[NLRI_MP_UPDATE
].afi
;
1612 safi
= nlris
[NLRI_MP_UPDATE
].safi
;
1615 if (afi
&& peer
->afc
[afi
][safi
]) {
1616 struct vrf
*vrf
= vrf_lookup_by_id(peer
->bgp
->vrf_id
);
1618 /* End-of-RIB received */
1619 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1620 PEER_STATUS_EOR_RECEIVED
)) {
1621 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1622 PEER_STATUS_EOR_RECEIVED
);
1623 bgp_update_explicit_eors(peer
);
1626 /* NSF delete stale route */
1627 if (peer
->nsf
[afi
][safi
])
1628 bgp_clear_stale_route(peer
, afi
, safi
);
1630 zlog_info("%%NOTIFICATION: rcvd End-of-RIB for %s from %s in vrf %s",
1631 afi_safi_print(afi
, safi
), peer
->host
,
1632 vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
1636 /* Everything is done. We unintern temporary structures which
1637 interned in bgp_attr_parse(). */
1638 bgp_attr_unintern_sub(&attr
);
1640 peer
->update_time
= bgp_clock();
1642 /* Rearm holdtime timer */
1643 BGP_TIMER_OFF(peer
->t_holdtime
);
1644 bgp_timer_set(peer
);
1646 return Receive_UPDATE_message
;
1650 * Process BGP NOTIFY message for peer.
1653 * @param size size of the packet
1654 * @return as in summary
1656 static int bgp_notify_receive(struct peer
*peer
, bgp_size_t size
)
1658 struct bgp_notify bgp_notify
;
1660 if (peer
->notify
.data
) {
1661 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1662 peer
->notify
.data
= NULL
;
1663 peer
->notify
.length
= 0;
1666 bgp_notify
.code
= stream_getc(peer
->curr
);
1667 bgp_notify
.subcode
= stream_getc(peer
->curr
);
1668 bgp_notify
.length
= size
- 2;
1669 bgp_notify
.data
= NULL
;
1671 /* Preserv notify code and sub code. */
1672 peer
->notify
.code
= bgp_notify
.code
;
1673 peer
->notify
.subcode
= bgp_notify
.subcode
;
1674 /* For further diagnostic record returned Data. */
1675 if (bgp_notify
.length
) {
1676 peer
->notify
.length
= size
- 2;
1677 peer
->notify
.data
= XMALLOC(MTYPE_TMP
, size
- 2);
1678 memcpy(peer
->notify
.data
, stream_pnt(peer
->curr
), size
- 2);
1687 if (bgp_notify
.length
) {
1689 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
1690 for (i
= 0; i
< bgp_notify
.length
; i
++)
1693 stream_getc(peer
->curr
));
1694 strcat(bgp_notify
.data
, c
);
1698 stream_getc(peer
->curr
));
1699 strcpy(bgp_notify
.data
, c
);
1701 bgp_notify
.raw_data
= (uint8_t *)peer
->notify
.data
;
1704 bgp_notify_print(peer
, &bgp_notify
, "received");
1705 if (bgp_notify
.data
) {
1706 XFREE(MTYPE_TMP
, bgp_notify
.data
);
1707 bgp_notify
.data
= NULL
;
1708 bgp_notify
.length
= 0;
1712 /* peer count update */
1713 atomic_fetch_add_explicit(&peer
->notify_in
, 1, memory_order_relaxed
);
1715 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1717 /* We have to check for Notify with Unsupported Optional Parameter.
1718 in that case we fallback to open without the capability option.
1719 But this done in bgp_stop. We just mark it here to avoid changing
1721 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
1722 && bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1723 UNSET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1725 return Receive_NOTIFICATION_message
;
1729 * Process BGP ROUTEREFRESH message for peer.
1732 * @param size size of the packet
1733 * @return as in summary
1735 static int bgp_route_refresh_receive(struct peer
*peer
, bgp_size_t size
)
1739 iana_safi_t pkt_safi
;
1742 struct peer_af
*paf
;
1743 struct update_group
*updgrp
;
1744 struct peer
*updgrp_peer
;
1746 /* If peer does not have the capability, send notification. */
1747 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_ADV
)) {
1748 flog_err(EC_BGP_NO_CAP
,
1749 "%s [Error] BGP route refresh is not enabled",
1751 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
1752 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1756 /* Status must be Established. */
1757 if (peer
->status
!= Established
) {
1759 EC_BGP_INVALID_STATUS
,
1760 "%s [Error] Route refresh packet received under status %s",
1762 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1763 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1770 pkt_afi
= stream_getw(s
);
1771 (void)stream_getc(s
);
1772 pkt_safi
= stream_getc(s
);
1774 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1775 zlog_debug("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1776 peer
->host
, pkt_afi
, pkt_safi
);
1778 /* Convert AFI, SAFI to internal values and check. */
1779 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1781 "%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1782 peer
->host
, pkt_afi
, pkt_safi
);
1783 return BGP_PACKET_NOOP
;
1786 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) {
1788 uint8_t when_to_refresh
;
1792 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1794 zlog_info("%s ORF route refresh length error",
1796 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
1800 when_to_refresh
= stream_getc(s
);
1801 end
= stream_pnt(s
) + (size
- 5);
1803 while ((stream_pnt(s
) + 2) < end
) {
1804 orf_type
= stream_getc(s
);
1805 orf_len
= stream_getw(s
);
1807 /* orf_len in bounds? */
1808 if ((stream_pnt(s
) + orf_len
) > end
)
1809 break; /* XXX: Notify instead?? */
1810 if (orf_type
== ORF_TYPE_PREFIX
1811 || orf_type
== ORF_TYPE_PREFIX_OLD
) {
1812 uint8_t *p_pnt
= stream_pnt(s
);
1813 uint8_t *p_end
= stream_pnt(s
) + orf_len
;
1814 struct orf_prefix orfp
;
1819 int ret
= CMD_SUCCESS
;
1821 if (bgp_debug_neighbor_events(peer
)) {
1823 "%s rcvd Prefixlist ORF(%d) length %d",
1824 peer
->host
, orf_type
, orf_len
);
1827 /* we're going to read at least 1 byte of common
1829 * and 7 bytes of ORF Address-filter entry from
1835 /* ORF prefix-list name */
1836 sprintf(name
, "%s.%d.%d", peer
->host
, afi
,
1839 while (p_pnt
< p_end
) {
1840 /* If the ORF entry is malformed, want
1841 * to read as much of it
1842 * as possible without going beyond the
1843 * bounds of the entry,
1844 * to maximise debug information.
1848 sizeof(struct orf_prefix
));
1850 /* after ++: p_pnt <= p_end */
1852 & ORF_COMMON_PART_REMOVE_ALL
) {
1853 if (bgp_debug_neighbor_events(
1856 "%s rcvd Remove-All pfxlist ORF request",
1858 prefix_bgp_orf_remove_all(afi
,
1862 ok
= ((uint32_t)(p_end
- p_pnt
)
1863 >= sizeof(uint32_t));
1867 p_pnt
+= sizeof(uint32_t);
1868 orfp
.seq
= ntohl(seq
);
1872 if ((ok
= (p_pnt
< p_end
)))
1876 prefix_bgp_orf_set()
1878 if ((ok
= (p_pnt
< p_end
)))
1882 prefix_bgp_orf_set()
1884 if ((ok
= (p_pnt
< p_end
)))
1885 orfp
.p
.prefixlen
= *p_pnt
++;
1886 orfp
.p
.family
= afi2family(
1887 afi
); /* afi checked already */
1890 orfp
.p
.prefixlen
); /* 0 if not
1894 &orfp
.p
)) /* valid for
1898 psize
= prefix_blen(&orfp
.p
);
1901 > (p_end
- p_pnt
)) /* valid for
1905 psize
= p_end
- p_pnt
;
1909 memcpy(&orfp
.p
.u
.prefix
, p_pnt
,
1913 if (bgp_debug_neighbor_events(peer
)) {
1914 char buf
[INET6_BUFSIZ
];
1917 "%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
1919 (common
& ORF_COMMON_PART_REMOVE
1922 (common
& ORF_COMMON_PART_DENY
1933 ok
? "" : " MALFORMED");
1937 ret
= prefix_bgp_orf_set(
1939 (common
& ORF_COMMON_PART_DENY
1942 (common
& ORF_COMMON_PART_REMOVE
1946 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
)) {
1948 "%s Received misformatted prefixlist ORF."
1949 " Remove All pfxlist",
1951 prefix_bgp_orf_remove_all(afi
,
1957 peer
->orf_plist
[afi
][safi
] =
1958 prefix_bgp_orf_lookup(afi
, name
);
1960 stream_forward_getp(s
, orf_len
);
1962 if (bgp_debug_neighbor_events(peer
))
1963 zlog_debug("%s rcvd Refresh %s ORF request", peer
->host
,
1964 when_to_refresh
== REFRESH_DEFER
1967 if (when_to_refresh
== REFRESH_DEFER
)
1968 return BGP_PACKET_NOOP
;
1971 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1972 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1973 PEER_STATUS_ORF_WAIT_REFRESH
))
1974 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
1975 PEER_STATUS_ORF_WAIT_REFRESH
);
1977 paf
= peer_af_find(peer
, afi
, safi
);
1978 if (paf
&& paf
->subgroup
) {
1979 if (peer
->orf_plist
[afi
][safi
]) {
1980 updgrp
= PAF_UPDGRP(paf
);
1981 updgrp_peer
= UPDGRP_PEER(updgrp
);
1982 updgrp_peer
->orf_plist
[afi
][safi
] =
1983 peer
->orf_plist
[afi
][safi
];
1986 /* If the peer is configured for default-originate clear the
1987 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
1991 if (CHECK_FLAG(paf
->subgroup
->sflags
,
1992 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1993 UNSET_FLAG(paf
->subgroup
->sflags
,
1994 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1997 /* Perform route refreshment to the peer */
1998 bgp_announce_route(peer
, afi
, safi
);
2000 /* No FSM action necessary */
2001 return BGP_PACKET_NOOP
;
2005 * Parse BGP CAPABILITY message for peer.
2008 * @param size size of the packet
2009 * @return as in summary
2011 static int bgp_capability_msg_parse(struct peer
*peer
, uint8_t *pnt
,
2015 struct capability_mp_data mpc
;
2016 struct capability_header
*hdr
;
2020 iana_safi_t pkt_safi
;
2026 /* We need at least action, capability code and capability
2028 if (pnt
+ 3 > end
) {
2029 zlog_info("%s Capability length error", peer
->host
);
2030 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2034 hdr
= (struct capability_header
*)(pnt
+ 1);
2036 /* Action value check. */
2037 if (action
!= CAPABILITY_ACTION_SET
2038 && action
!= CAPABILITY_ACTION_UNSET
) {
2039 zlog_info("%s Capability Action Value error %d",
2040 peer
->host
, action
);
2041 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2045 if (bgp_debug_neighbor_events(peer
))
2047 "%s CAPABILITY has action: %d, code: %u, length %u",
2048 peer
->host
, action
, hdr
->code
, hdr
->length
);
2050 /* Capability length check. */
2051 if ((pnt
+ hdr
->length
+ 3) > end
) {
2052 zlog_info("%s Capability length error", peer
->host
);
2053 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2057 /* Fetch structure to the byte stream. */
2058 memcpy(&mpc
, pnt
+ 3, sizeof(struct capability_mp_data
));
2059 pnt
+= hdr
->length
+ 3;
2061 /* We know MP Capability Code. */
2062 if (hdr
->code
== CAPABILITY_CODE_MP
) {
2063 pkt_afi
= ntohs(mpc
.afi
);
2064 pkt_safi
= mpc
.safi
;
2066 /* Ignore capability when override-capability is set. */
2067 if (CHECK_FLAG(peer
->flags
,
2068 PEER_FLAG_OVERRIDE_CAPABILITY
))
2071 /* Convert AFI, SAFI to internal values. */
2072 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
,
2074 if (bgp_debug_neighbor_events(peer
))
2076 "%s Dynamic Capability MP_EXT afi/safi invalid "
2078 peer
->host
, pkt_afi
, pkt_safi
);
2082 /* Address family check. */
2083 if (bgp_debug_neighbor_events(peer
))
2085 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2087 action
== CAPABILITY_ACTION_SET
2092 if (action
== CAPABILITY_ACTION_SET
) {
2093 peer
->afc_recv
[afi
][safi
] = 1;
2094 if (peer
->afc
[afi
][safi
]) {
2095 peer
->afc_nego
[afi
][safi
] = 1;
2096 bgp_announce_route(peer
, afi
, safi
);
2099 peer
->afc_recv
[afi
][safi
] = 0;
2100 peer
->afc_nego
[afi
][safi
] = 0;
2102 if (peer_active_nego(peer
))
2103 bgp_clear_route(peer
, afi
, safi
);
2109 EC_BGP_UNRECOGNIZED_CAPABILITY
,
2110 "%s unrecognized capability code: %d - ignored",
2111 peer
->host
, hdr
->code
);
2115 /* No FSM action necessary */
2116 return BGP_PACKET_NOOP
;
2120 * Parse BGP CAPABILITY message for peer.
2122 * Exported for unit testing.
2125 * @param size size of the packet
2126 * @return as in summary
2128 int bgp_capability_receive(struct peer
*peer
, bgp_size_t size
)
2132 /* Fetch pointer. */
2133 pnt
= stream_pnt(peer
->curr
);
2135 if (bgp_debug_neighbor_events(peer
))
2136 zlog_debug("%s rcv CAPABILITY", peer
->host
);
2138 /* If peer does not have the capability, send notification. */
2139 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_ADV
)) {
2140 flog_err(EC_BGP_NO_CAP
,
2141 "%s [Error] BGP dynamic capability is not enabled",
2143 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2144 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2148 /* Status must be Established. */
2149 if (peer
->status
!= Established
) {
2152 "%s [Error] Dynamic capability packet received under status %s",
2154 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2155 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
2160 return bgp_capability_msg_parse(peer
, pnt
, size
);
2164 * Processes a peer's input buffer.
2166 * This function sidesteps the event loop and directly calls bgp_event_update()
2167 * after processing each BGP message. This is necessary to ensure proper
2168 * ordering of FSM events and unifies the behavior that was present previously,
2169 * whereby some of the packet handling functions would update the FSM and some
2170 * would not, making event flow difficult to understand. Please think twice
2171 * before hacking this.
2173 * Thread type: THREAD_EVENT
2177 int bgp_process_packet(struct thread
*thread
)
2179 /* Yes first of all get peer pointer. */
2180 struct peer
*peer
; // peer
2181 uint32_t rpkt_quanta_old
; // how many packets to read
2182 int fsm_update_result
; // return code of bgp_event_update()
2183 int mprc
; // message processing return code
2185 peer
= THREAD_ARG(thread
);
2186 rpkt_quanta_old
= atomic_load_explicit(&peer
->bgp
->rpkt_quanta
,
2187 memory_order_relaxed
);
2188 fsm_update_result
= 0;
2190 /* Guard against scheduled events that occur after peer deletion. */
2191 if (peer
->status
== Deleted
|| peer
->status
== Clearing
)
2194 unsigned int processed
= 0;
2196 while (processed
< rpkt_quanta_old
) {
2199 char notify_data_length
[2];
2201 pthread_mutex_lock(&peer
->io_mtx
);
2203 peer
->curr
= stream_fifo_pop(peer
->ibuf
);
2205 pthread_mutex_unlock(&peer
->io_mtx
);
2207 if (peer
->curr
== NULL
) // no packets to process, hmm...
2210 /* skip the marker and copy the packet length */
2211 stream_forward_getp(peer
->curr
, BGP_MARKER_SIZE
);
2212 memcpy(notify_data_length
, stream_pnt(peer
->curr
), 2);
2214 /* read in the packet length and type */
2215 size
= stream_getw(peer
->curr
);
2216 type
= stream_getc(peer
->curr
);
2218 /* BGP packet dump function. */
2219 bgp_dump_packet(peer
, type
, peer
->curr
);
2221 /* adjust size to exclude the marker + length + type */
2222 size
-= BGP_HEADER_SIZE
;
2224 /* Read rest of the packet and call each sort of packet routine
2228 atomic_fetch_add_explicit(&peer
->open_in
, 1,
2229 memory_order_relaxed
);
2230 mprc
= bgp_open_receive(peer
, size
);
2231 if (mprc
== BGP_Stop
)
2234 "%s: BGP OPEN receipt failed for peer: %s",
2235 __FUNCTION__
, peer
->host
);
2237 case BGP_MSG_UPDATE
:
2238 atomic_fetch_add_explicit(&peer
->update_in
, 1,
2239 memory_order_relaxed
);
2240 peer
->readtime
= monotime(NULL
);
2241 mprc
= bgp_update_receive(peer
, size
);
2242 if (mprc
== BGP_Stop
)
2245 "%s: BGP UPDATE receipt failed for peer: %s",
2246 __FUNCTION__
, peer
->host
);
2248 case BGP_MSG_NOTIFY
:
2249 atomic_fetch_add_explicit(&peer
->notify_in
, 1,
2250 memory_order_relaxed
);
2251 mprc
= bgp_notify_receive(peer
, size
);
2252 if (mprc
== BGP_Stop
)
2255 "%s: BGP NOTIFY receipt failed for peer: %s",
2256 __FUNCTION__
, peer
->host
);
2258 case BGP_MSG_KEEPALIVE
:
2259 peer
->readtime
= monotime(NULL
);
2260 atomic_fetch_add_explicit(&peer
->keepalive_in
, 1,
2261 memory_order_relaxed
);
2262 mprc
= bgp_keepalive_receive(peer
, size
);
2263 if (mprc
== BGP_Stop
)
2266 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2267 __FUNCTION__
, peer
->host
);
2269 case BGP_MSG_ROUTE_REFRESH_NEW
:
2270 case BGP_MSG_ROUTE_REFRESH_OLD
:
2271 atomic_fetch_add_explicit(&peer
->refresh_in
, 1,
2272 memory_order_relaxed
);
2273 mprc
= bgp_route_refresh_receive(peer
, size
);
2274 if (mprc
== BGP_Stop
)
2277 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2278 __FUNCTION__
, peer
->host
);
2280 case BGP_MSG_CAPABILITY
:
2281 atomic_fetch_add_explicit(&peer
->dynamic_cap_in
, 1,
2282 memory_order_relaxed
);
2283 mprc
= bgp_capability_receive(peer
, size
);
2284 if (mprc
== BGP_Stop
)
2287 "%s: BGP CAPABILITY receipt failed for peer: %s",
2288 __FUNCTION__
, peer
->host
);
2292 * The message type should have been sanitized before
2293 * we ever got here. Receipt of a message with an
2294 * invalid header at this point is indicative of a
2297 assert (!"Message of invalid type received during input processing");
2300 /* delete processed packet */
2301 stream_free(peer
->curr
);
2306 if (mprc
!= BGP_PACKET_NOOP
)
2307 fsm_update_result
= bgp_event_update(peer
, mprc
);
2312 * If peer was deleted, do not process any more packets. This
2313 * is usually due to executing BGP_Stop or a stub deletion.
2315 if (fsm_update_result
== FSM_PEER_TRANSFERRED
2316 || fsm_update_result
== FSM_PEER_STOPPED
)
2320 if (fsm_update_result
!= FSM_PEER_TRANSFERRED
2321 && fsm_update_result
!= FSM_PEER_STOPPED
) {
2322 pthread_mutex_lock(&peer
->io_mtx
);
2324 // more work to do, come back later
2325 if (peer
->ibuf
->count
> 0)
2326 thread_add_timer_msec(
2327 bm
->master
, bgp_process_packet
, peer
, 0,
2328 &peer
->t_process_packet
);
2330 pthread_mutex_unlock(&peer
->io_mtx
);