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
)
654 /* Lock I/O mutex to prevent other threads from pushing packets */
655 pthread_mutex_lock(&peer
->io_mtx
);
656 /* ============================================== */
658 /* Allocate new stream. */
659 s
= stream_new(BGP_MAX_PACKET_SIZE
);
661 /* Make notify packet. */
662 bgp_packet_set_marker(s
, BGP_MSG_NOTIFY
);
664 /* Set notify packet values. */
665 stream_putc(s
, code
); /* BGP notify code */
666 stream_putc(s
, sub_code
); /* BGP notify sub_code */
668 /* If notify data is present. */
670 stream_write(s
, data
, datalen
);
672 /* Set BGP packet length. */
673 length
= bgp_packet_set_size(s
);
675 /* wipe output buffer */
676 stream_fifo_clean(peer
->obuf
);
679 * If possible, store last packet for debugging purposes. This check is
680 * in place because we are sometimes called with a doppelganger peer,
681 * who tends to have a plethora of fields nulled out.
683 if (peer
->curr
&& peer
->last_reset_cause_size
) {
684 size_t packetsize
= stream_get_endp(peer
->curr
);
685 assert(packetsize
<= peer
->last_reset_cause_size
);
686 memcpy(peer
->last_reset_cause
, peer
->curr
->data
, packetsize
);
687 peer
->last_reset_cause_size
= packetsize
;
692 struct bgp_notify bgp_notify
;
697 bgp_notify
.code
= code
;
698 bgp_notify
.subcode
= sub_code
;
699 bgp_notify
.data
= NULL
;
700 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
701 bgp_notify
.raw_data
= data
;
703 peer
->notify
.code
= bgp_notify
.code
;
704 peer
->notify
.subcode
= bgp_notify
.subcode
;
706 if (bgp_notify
.length
) {
708 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
709 for (i
= 0; i
< bgp_notify
.length
; i
++)
711 sprintf(c
, " %02x", data
[i
]);
712 strcat(bgp_notify
.data
, c
);
715 sprintf(c
, "%02x", data
[i
]);
716 strcpy(bgp_notify
.data
, c
);
719 bgp_notify_print(peer
, &bgp_notify
, "sending");
721 if (bgp_notify
.data
) {
722 XFREE(MTYPE_TMP
, bgp_notify
.data
);
723 bgp_notify
.data
= NULL
;
724 bgp_notify
.length
= 0;
728 /* peer reset cause */
729 if (code
== BGP_NOTIFY_CEASE
) {
730 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
731 peer
->last_reset
= PEER_DOWN_USER_RESET
;
732 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
733 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
735 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
737 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
739 /* Add packet to peer's output queue */
740 stream_fifo_push(peer
->obuf
, s
);
742 bgp_write_notify(peer
);
744 /* ============================================== */
745 pthread_mutex_unlock(&peer
->io_mtx
);
749 * Creates a BGP Notify and appends it to the peer's output queue.
751 * This function attempts to write the packet from the thread it is called
752 * from, to ensure the packet gets out ASAP.
755 * @param code BGP error code
756 * @param sub_code BGP error subcode
758 void bgp_notify_send(struct peer
*peer
, uint8_t code
, uint8_t sub_code
)
760 bgp_notify_send_with_data(peer
, code
, sub_code
, NULL
, 0);
764 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
767 * @param afi Address Family Identifier
768 * @param safi Subsequent Address Family Identifier
769 * @param orf_type Outbound Route Filtering type
770 * @param when_to_refresh Whether to refresh immediately or defer
771 * @param remove Whether to remove ORF for specified AFI/SAFI
773 void bgp_route_refresh_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
774 uint8_t orf_type
, uint8_t when_to_refresh
,
778 struct bgp_filter
*filter
;
781 iana_safi_t pkt_safi
;
783 if (DISABLE_BGP_ANNOUNCE
)
786 filter
= &peer
->filter
[afi
][safi
];
788 /* Convert AFI, SAFI to values for packet. */
789 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
791 s
= stream_new(BGP_MAX_PACKET_SIZE
);
793 /* Make BGP update packet. */
794 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
795 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_NEW
);
797 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_OLD
);
799 /* Encode Route Refresh message. */
800 stream_putw(s
, pkt_afi
);
802 stream_putc(s
, pkt_safi
);
804 if (orf_type
== ORF_TYPE_PREFIX
|| orf_type
== ORF_TYPE_PREFIX_OLD
)
805 if (remove
|| filter
->plist
[FILTER_IN
].plist
) {
810 stream_putc(s
, when_to_refresh
);
811 stream_putc(s
, orf_type
);
812 orfp
= stream_get_endp(s
);
816 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
817 PEER_STATUS_ORF_PREFIX_SEND
);
818 stream_putc(s
, ORF_COMMON_PART_REMOVE_ALL
);
819 if (bgp_debug_neighbor_events(peer
))
821 "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
822 peer
->host
, orf_type
,
823 (when_to_refresh
== REFRESH_DEFER
828 SET_FLAG(peer
->af_sflags
[afi
][safi
],
829 PEER_STATUS_ORF_PREFIX_SEND
);
830 prefix_bgp_orf_entry(
831 s
, filter
->plist
[FILTER_IN
].plist
,
833 ORF_COMMON_PART_PERMIT
,
834 ORF_COMMON_PART_DENY
);
835 if (bgp_debug_neighbor_events(peer
))
837 "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
838 peer
->host
, orf_type
,
839 (when_to_refresh
== REFRESH_DEFER
845 /* Total ORF Entry Len. */
846 orf_len
= stream_get_endp(s
) - orfp
- 2;
847 stream_putw_at(s
, orfp
, orf_len
);
850 /* Set packet size. */
851 (void)bgp_packet_set_size(s
);
853 if (bgp_debug_neighbor_events(peer
)) {
855 zlog_debug("%s sending REFRESH_REQ for afi/safi: %d/%d",
856 peer
->host
, pkt_afi
, pkt_safi
);
859 /* Add packet to the peer. */
860 bgp_packet_add(peer
, s
);
866 * Create a BGP Capability packet and append it to the peer's output queue.
869 * @param afi Address Family Identifier
870 * @param safi Subsequent Address Family Identifier
871 * @param capability_code BGP Capability Code
872 * @param action Set or Remove capability
874 void bgp_capability_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
875 int capability_code
, int action
)
879 iana_safi_t pkt_safi
;
881 /* Convert AFI, SAFI to values for packet. */
882 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
884 s
= stream_new(BGP_MAX_PACKET_SIZE
);
886 /* Make BGP update packet. */
887 bgp_packet_set_marker(s
, BGP_MSG_CAPABILITY
);
889 /* Encode MP_EXT capability. */
890 if (capability_code
== CAPABILITY_CODE_MP
) {
891 stream_putc(s
, action
);
892 stream_putc(s
, CAPABILITY_CODE_MP
);
893 stream_putc(s
, CAPABILITY_CODE_MP_LEN
);
894 stream_putw(s
, pkt_afi
);
896 stream_putc(s
, pkt_safi
);
898 if (bgp_debug_neighbor_events(peer
))
900 "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
902 action
== CAPABILITY_ACTION_SET
? "Advertising"
907 /* Set packet size. */
908 (void)bgp_packet_set_size(s
);
910 /* Add packet to the peer. */
911 bgp_packet_add(peer
, s
);
916 /* RFC1771 6.8 Connection collision detection. */
917 static int bgp_collision_detect(struct peer
*new, struct in_addr remote_id
)
921 /* Upon receipt of an OPEN message, the local system must examine
922 all of its connections that are in the OpenConfirm state. A BGP
923 speaker may also examine connections in an OpenSent state if it
924 knows the BGP Identifier of the peer by means outside of the
925 protocol. If among these connections there is a connection to a
926 remote BGP speaker whose BGP Identifier equals the one in the
927 OPEN message, then the local system performs the following
928 collision resolution procedure: */
930 if ((peer
= new->doppelganger
) != NULL
) {
931 /* Do not accept the new connection in Established or Clearing
933 * Note that a peer GR is handled by closing the existing
935 * upon receipt of new one.
937 if (peer
->status
== Established
|| peer
->status
== Clearing
) {
938 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
939 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
941 } else if ((peer
->status
== OpenConfirm
)
942 || (peer
->status
== OpenSent
)) {
943 /* 1. The BGP Identifier of the local system is compared
945 the BGP Identifier of the remote system (as specified
947 the OPEN message). */
949 if (ntohl(peer
->local_id
.s_addr
)
950 < ntohl(remote_id
.s_addr
))
951 if (!CHECK_FLAG(peer
->sflags
,
952 PEER_STATUS_ACCEPT_PEER
)) {
953 /* 2. If the value of the local BGP
955 than the remote one, the local system
957 connection that already exists (the
959 already in the OpenConfirm state),
961 connection initiated by the remote
964 peer
, BGP_NOTIFY_CEASE
,
965 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
969 new, BGP_NOTIFY_CEASE
,
970 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
974 /* 3. Otherwise, the local system closes newly
976 BGP connection (the one associated with the
978 received OPEN message), and continues to use
980 existing one (the one that is already in the
981 OpenConfirm state). */
982 if (CHECK_FLAG(peer
->sflags
,
983 PEER_STATUS_ACCEPT_PEER
)) {
985 peer
, BGP_NOTIFY_CEASE
,
986 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
990 new, BGP_NOTIFY_CEASE
,
991 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1000 /* Packet processing routines ---------------------------------------------- */
1002 * This is a family of functions designed to be called from
1003 * bgp_process_packet(). These functions all share similar behavior and should
1004 * adhere to the following invariants and restrictions:
1008 * The return code of any one of those functions should be one of the FSM event
1009 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1010 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1011 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1012 * error and Receive_OPEN_message otherwise.
1014 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1019 * - May send NOTIFY messages
1020 * - May not modify peer->status
1021 * - May not call bgp_event_update()
1024 #define BGP_PACKET_NOOP 0
1027 * Process BGP OPEN message for peer.
1029 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1030 * and returns BGP_Stop.
1033 * @param size size of the packet
1034 * @return as in summary
1036 static int bgp_open_receive(struct peer
*peer
, bgp_size_t size
)
1042 uint16_t send_holdtime
;
1045 struct in_addr remote_id
;
1047 uint8_t notify_data_remote_as
[2];
1048 uint8_t notify_data_remote_as4
[4];
1049 uint8_t notify_data_remote_id
[4];
1050 uint16_t *holdtime_ptr
;
1052 /* Parse open packet. */
1053 version
= stream_getc(peer
->curr
);
1054 memcpy(notify_data_remote_as
, stream_pnt(peer
->curr
), 2);
1055 remote_as
= stream_getw(peer
->curr
);
1056 holdtime_ptr
= (uint16_t *)stream_pnt(peer
->curr
);
1057 holdtime
= stream_getw(peer
->curr
);
1058 memcpy(notify_data_remote_id
, stream_pnt(peer
->curr
), 4);
1059 remote_id
.s_addr
= stream_get_ipv4(peer
->curr
);
1061 /* Receive OPEN message log */
1062 if (bgp_debug_neighbor_events(peer
))
1064 "%s rcv OPEN, version %d, remote-as (in open) %u,"
1065 " holdtime %d, id %s",
1066 peer
->host
, version
, remote_as
, holdtime
,
1067 inet_ntoa(remote_id
));
1069 /* BEGIN to read the capability here, but dont do it yet */
1071 optlen
= stream_getc(peer
->curr
);
1074 /* If not enough bytes, it is an error. */
1075 if (STREAM_READABLE(peer
->curr
) < optlen
) {
1076 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
1077 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
1081 /* We need the as4 capability value *right now* because
1082 * if it is there, we have not got the remote_as yet, and
1084 * that we do not know which peer is connecting to us now.
1086 as4
= peek_for_as4_capability(peer
, optlen
);
1087 memcpy(notify_data_remote_as4
, &as4
, 4);
1090 /* Just in case we have a silly peer who sends AS4 capability set to 0
1092 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
) {
1093 zlog_err("%s bad OPEN, got AS4 capability, but AS4 set to 0",
1095 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1096 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1097 notify_data_remote_as4
, 4);
1101 if (remote_as
== BGP_AS_TRANS
) {
1102 /* Take the AS4 from the capability. We must have received the
1103 * capability now! Otherwise we have a asn16 peer who uses
1104 * BGP_AS_TRANS, for some unknown reason.
1106 if (as4
== BGP_AS_TRANS
) {
1108 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1110 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1111 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1112 notify_data_remote_as4
, 4);
1116 if (!as4
&& BGP_DEBUG(as4
, AS4
))
1118 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
1119 " Odd, but proceeding.",
1121 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG(as4
, AS4
))
1123 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
1124 "in 2-bytes, very odd peer.",
1129 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1131 /* If we have got the capability, peer->as4cap must match
1133 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)
1134 && as4
!= remote_as
) {
1135 /* raise error, log this, close session */
1137 "%s bad OPEN, got AS4 capability, but remote_as %u"
1138 " mismatch with 16bit 'myasn' %u in open",
1139 peer
->host
, as4
, remote_as
);
1140 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1141 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1142 notify_data_remote_as4
, 4);
1147 /* remote router-id check. */
1148 if (remote_id
.s_addr
== 0 || IPV4_CLASS_DE(ntohl(remote_id
.s_addr
))
1149 || ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)) {
1150 if (bgp_debug_neighbor_events(peer
))
1151 zlog_debug("%s bad OPEN, wrong router identifier %s",
1152 peer
->host
, inet_ntoa(remote_id
));
1153 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1154 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1155 notify_data_remote_id
, 4);
1159 /* Set remote router-id */
1160 peer
->remote_id
= remote_id
;
1162 /* Peer BGP version check. */
1163 if (version
!= BGP_VERSION_4
) {
1164 uint16_t maxver
= htons(BGP_VERSION_4
);
1165 /* XXX this reply may not be correct if version < 4 XXX */
1166 if (bgp_debug_neighbor_events(peer
))
1168 "%s bad protocol version, remote requested %d, local request %d",
1169 peer
->host
, version
, BGP_VERSION_4
);
1170 /* Data must be in network byte order here */
1171 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1172 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1173 (uint8_t *)&maxver
, 2);
1177 /* Check neighbor as number. */
1178 if (peer
->as_type
== AS_UNSPECIFIED
) {
1179 if (bgp_debug_neighbor_events(peer
))
1181 "%s bad OPEN, remote AS is unspecified currently",
1183 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1184 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1185 notify_data_remote_as
, 2);
1187 } else if (peer
->as_type
== AS_INTERNAL
) {
1188 if (remote_as
!= peer
->bgp
->as
) {
1189 if (bgp_debug_neighbor_events(peer
))
1191 "%s bad OPEN, remote AS is %u, internal specified",
1192 peer
->host
, remote_as
);
1193 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1194 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1195 notify_data_remote_as
, 2);
1198 peer
->as
= peer
->local_as
;
1199 } else if (peer
->as_type
== AS_EXTERNAL
) {
1200 if (remote_as
== peer
->bgp
->as
) {
1201 if (bgp_debug_neighbor_events(peer
))
1203 "%s bad OPEN, remote AS is %u, external specified",
1204 peer
->host
, remote_as
);
1205 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1206 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1207 notify_data_remote_as
, 2);
1210 peer
->as
= remote_as
;
1211 } else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
)) {
1212 if (bgp_debug_neighbor_events(peer
))
1213 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1214 peer
->host
, remote_as
, peer
->as
);
1215 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1216 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1217 notify_data_remote_as
, 2);
1221 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1222 calculate the value of the Hold Timer by using the smaller of its
1223 configured Hold Time and the Hold Time received in the OPEN message.
1224 The Hold Time MUST be either zero or at least three seconds. An
1225 implementation may reject connections on the basis of the Hold Time.
1228 if (holdtime
< 3 && holdtime
!= 0) {
1229 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1230 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1231 (uint8_t *)holdtime_ptr
, 2);
1235 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1236 would be one third of the Hold Time interval. KEEPALIVE messages
1237 MUST NOT be sent more frequently than one per second. An
1238 implementation MAY adjust the rate at which it sends KEEPALIVE
1239 messages as a function of the Hold Time interval. */
1241 if (PEER_OR_GROUP_TIMER_SET(peer
))
1242 send_holdtime
= peer
->holdtime
;
1244 send_holdtime
= peer
->bgp
->default_holdtime
;
1246 if (holdtime
< send_holdtime
)
1247 peer
->v_holdtime
= holdtime
;
1249 peer
->v_holdtime
= send_holdtime
;
1251 if ((PEER_OR_GROUP_TIMER_SET(peer
))
1252 && (peer
->keepalive
< peer
->v_holdtime
/ 3))
1253 peer
->v_keepalive
= peer
->keepalive
;
1255 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1257 /* Open option part parse. */
1259 if ((ret
= bgp_open_option_parse(peer
, optlen
, &mp_capability
))
1263 if (bgp_debug_neighbor_events(peer
))
1264 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1269 * Assume that the peer supports the locally configured set of
1270 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1271 * capabilities, or if 'override-capability' is configured.
1274 || CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
1275 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] =
1276 peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1277 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] =
1278 peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1279 peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
] =
1280 peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
];
1281 peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
] =
1282 peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
];
1283 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] =
1284 peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1285 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] =
1286 peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1287 peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
] =
1288 peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
];
1289 peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
] =
1290 peer
->afc
[AFI_L2VPN
][SAFI_EVPN
];
1291 peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
] =
1292 peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
];
1295 /* When collision is detected and this peer is closed. Retrun
1297 ret
= bgp_collision_detect(peer
, remote_id
);
1302 if ((ret
= bgp_getsockname(peer
)) < 0) {
1303 zlog_err("%s: bgp_getsockname() failed for peer: %s",
1304 __FUNCTION__
, peer
->host
);
1308 /* Verify valid local address present based on negotiated
1309 * address-families. */
1310 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
1311 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
1312 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
1313 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
1314 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]) {
1315 if (!peer
->nexthop
.v4
.s_addr
) {
1316 #if defined(HAVE_CUMULUS)
1318 "%s: No local IPv4 addr resetting connection, fd %d",
1319 peer
->host
, peer
->fd
);
1320 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1321 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1326 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
1327 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
1328 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
1329 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
1330 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]) {
1331 if (IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_global
)) {
1332 #if defined(HAVE_CUMULUS)
1334 "%s: No local IPv6 addr resetting connection, fd %d",
1335 peer
->host
, peer
->fd
);
1336 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1337 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1342 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1344 return Receive_OPEN_message
;
1348 * Process BGP KEEPALIVE message for peer.
1351 * @param size size of the packet
1352 * @return as in summary
1354 static int bgp_keepalive_receive(struct peer
*peer
, bgp_size_t size
)
1356 if (bgp_debug_keepalive(peer
))
1357 zlog_debug("%s KEEPALIVE rcvd", peer
->host
);
1359 bgp_update_implicit_eors(peer
);
1361 return Receive_KEEPALIVE_message
;
1366 * Process BGP UPDATE message for peer.
1368 * Parses UPDATE and creates attribute object.
1371 * @param size size of the packet
1372 * @return as in summary
1374 static int bgp_update_receive(struct peer
*peer
, bgp_size_t size
)
1380 bgp_size_t attribute_len
;
1381 bgp_size_t update_len
;
1382 bgp_size_t withdraw_len
;
1391 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1393 /* Status must be Established. */
1394 if (peer
->status
!= Established
) {
1395 zlog_err("%s [FSM] Update packet received under status %s",
1397 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1398 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1402 /* Set initial values. */
1403 memset(&attr
, 0, sizeof(struct attr
));
1404 attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
1405 attr
.label
= MPLS_INVALID_LABEL
;
1406 memset(&nlris
, 0, sizeof(nlris
));
1407 memset(peer
->rcvd_attr_str
, 0, BUFSIZ
);
1408 peer
->rcvd_attr_printed
= 0;
1411 end
= stream_pnt(s
) + size
;
1413 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1414 Length is too large (i.e., if Unfeasible Routes Length + Total
1415 Attribute Length + 23 exceeds the message Length), then the Error
1416 Subcode is set to Malformed Attribute List. */
1417 if (stream_pnt(s
) + 2 > end
) {
1419 "%s [Error] Update packet error"
1420 " (packet length is short for unfeasible length)",
1422 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1423 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1427 /* Unfeasible Route Length. */
1428 withdraw_len
= stream_getw(s
);
1430 /* Unfeasible Route Length check. */
1431 if (stream_pnt(s
) + withdraw_len
> end
) {
1433 "%s [Error] Update packet error"
1434 " (packet unfeasible length overflow %d)",
1435 peer
->host
, withdraw_len
);
1436 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1437 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1441 /* Unfeasible Route packet format check. */
1442 if (withdraw_len
> 0) {
1443 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1444 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1445 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt(s
);
1446 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1447 stream_forward_getp(s
, withdraw_len
);
1450 /* Attribute total length check. */
1451 if (stream_pnt(s
) + 2 > end
) {
1453 "%s [Error] Packet Error"
1454 " (update packet is short for attribute length)",
1456 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1457 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1461 /* Fetch attribute total length. */
1462 attribute_len
= stream_getw(s
);
1464 /* Attribute length check. */
1465 if (stream_pnt(s
) + attribute_len
> end
) {
1467 "%s [Error] Packet Error"
1468 " (update packet attribute length overflow %d)",
1469 peer
->host
, attribute_len
);
1470 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1471 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1475 /* Certain attribute parsing errors should not be considered bad enough
1476 * to reset the session for, most particularly any partial/optional
1477 * attributes that have 'tunneled' over speakers that don't understand
1478 * them. Instead we withdraw only the prefix concerned.
1480 * Complicates the flow a little though..
1482 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1483 /* This define morphs the update case into a withdraw when lower levels
1484 * have signalled an error condition where this is best.
1486 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1488 /* Parse attribute when it exists. */
1489 if (attribute_len
) {
1490 attr_parse_ret
= bgp_attr_parse(peer
, &attr
, attribute_len
,
1491 &nlris
[NLRI_MP_UPDATE
],
1492 &nlris
[NLRI_MP_WITHDRAW
]);
1493 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
) {
1494 bgp_attr_unintern_sub(&attr
);
1499 /* Logging the attribute. */
1500 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1501 || BGP_DEBUG(update
, UPDATE_IN
)
1502 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
1503 ret
= bgp_dump_attr(&attr
, peer
->rcvd_attr_str
, BUFSIZ
);
1505 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1507 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1510 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1)) {
1511 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
1512 peer
->rcvd_attr_str
);
1513 peer
->rcvd_attr_printed
= 1;
1517 /* Network Layer Reachability Information. */
1518 update_len
= end
- stream_pnt(s
);
1521 /* Set NLRI portion to structure. */
1522 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1523 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1524 nlris
[NLRI_UPDATE
].nlri
= stream_pnt(s
);
1525 nlris
[NLRI_UPDATE
].length
= update_len
;
1526 stream_forward_getp(s
, update_len
);
1529 if (BGP_DEBUG(update
, UPDATE_IN
))
1530 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1531 peer
->host
, withdraw_len
, attribute_len
, update_len
);
1533 /* Parse any given NLRIs */
1534 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++) {
1538 /* NLRI is processed iff the peer if configured for the specific
1540 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
]) {
1542 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1543 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1547 /* EoR handled later */
1548 if (nlris
[i
].length
== 0)
1553 case NLRI_MP_UPDATE
:
1554 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
1558 case NLRI_MP_WITHDRAW
:
1559 nlri_ret
= bgp_nlri_parse(peer
, &attr
, &nlris
[i
], 1);
1566 zlog_err("%s [Error] Error parsing NLRI", peer
->host
);
1567 if (peer
->status
== Established
)
1569 peer
, BGP_NOTIFY_UPDATE_ERR
,
1571 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1572 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1573 bgp_attr_unintern_sub(&attr
);
1580 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1581 * and MP EoR should have only an empty MP_UNREACH
1583 if ((!update_len
&& !withdraw_len
&& nlris
[NLRI_MP_UPDATE
].length
== 0)
1584 || (attr_parse_ret
== BGP_ATTR_PARSE_EOR
)) {
1588 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1590 * update and withdraw NLRI lengths are 0.
1592 if (!attribute_len
) {
1594 safi
= SAFI_UNICAST
;
1595 } else if (attr
.flag
& ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
)
1596 && nlris
[NLRI_MP_WITHDRAW
].length
== 0) {
1597 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1598 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1599 } else if (attr_parse_ret
== BGP_ATTR_PARSE_EOR
) {
1600 afi
= nlris
[NLRI_MP_UPDATE
].afi
;
1601 safi
= nlris
[NLRI_MP_UPDATE
].safi
;
1604 if (afi
&& peer
->afc
[afi
][safi
]) {
1605 /* End-of-RIB received */
1606 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1607 PEER_STATUS_EOR_RECEIVED
)) {
1608 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1609 PEER_STATUS_EOR_RECEIVED
);
1610 bgp_update_explicit_eors(peer
);
1613 /* NSF delete stale route */
1614 if (peer
->nsf
[afi
][safi
])
1615 bgp_clear_stale_route(peer
, afi
, safi
);
1617 if (bgp_debug_neighbor_events(peer
)) {
1618 zlog_debug("rcvd End-of-RIB for %s from %s",
1619 afi_safi_print(afi
, safi
),
1625 /* Everything is done. We unintern temporary structures which
1626 interned in bgp_attr_parse(). */
1627 bgp_attr_unintern_sub(&attr
);
1629 peer
->update_time
= bgp_clock();
1631 /* Rearm holdtime timer */
1632 BGP_TIMER_OFF(peer
->t_holdtime
);
1633 bgp_timer_set(peer
);
1635 return Receive_UPDATE_message
;
1639 * Process BGP NOTIFY message for peer.
1642 * @param size size of the packet
1643 * @return as in summary
1645 static int bgp_notify_receive(struct peer
*peer
, bgp_size_t size
)
1647 struct bgp_notify bgp_notify
;
1649 if (peer
->notify
.data
) {
1650 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1651 peer
->notify
.data
= NULL
;
1652 peer
->notify
.length
= 0;
1655 bgp_notify
.code
= stream_getc(peer
->curr
);
1656 bgp_notify
.subcode
= stream_getc(peer
->curr
);
1657 bgp_notify
.length
= size
- 2;
1658 bgp_notify
.data
= NULL
;
1660 /* Preserv notify code and sub code. */
1661 peer
->notify
.code
= bgp_notify
.code
;
1662 peer
->notify
.subcode
= bgp_notify
.subcode
;
1663 /* For further diagnostic record returned Data. */
1664 if (bgp_notify
.length
) {
1665 peer
->notify
.length
= size
- 2;
1666 peer
->notify
.data
= XMALLOC(MTYPE_TMP
, size
- 2);
1667 memcpy(peer
->notify
.data
, stream_pnt(peer
->curr
), size
- 2);
1676 if (bgp_notify
.length
) {
1678 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
1679 for (i
= 0; i
< bgp_notify
.length
; i
++)
1682 stream_getc(peer
->curr
));
1683 strcat(bgp_notify
.data
, c
);
1687 stream_getc(peer
->curr
));
1688 strcpy(bgp_notify
.data
, c
);
1690 bgp_notify
.raw_data
= (uint8_t *)peer
->notify
.data
;
1693 bgp_notify_print(peer
, &bgp_notify
, "received");
1694 if (bgp_notify
.data
) {
1695 XFREE(MTYPE_TMP
, bgp_notify
.data
);
1696 bgp_notify
.data
= NULL
;
1697 bgp_notify
.length
= 0;
1701 /* peer count update */
1702 atomic_fetch_add_explicit(&peer
->notify_in
, 1, memory_order_relaxed
);
1704 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1706 /* We have to check for Notify with Unsupported Optional Parameter.
1707 in that case we fallback to open without the capability option.
1708 But this done in bgp_stop. We just mark it here to avoid changing
1710 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
1711 && bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1712 UNSET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1714 return Receive_NOTIFICATION_message
;
1718 * Process BGP ROUTEREFRESH message for peer.
1721 * @param size size of the packet
1722 * @return as in summary
1724 static int bgp_route_refresh_receive(struct peer
*peer
, bgp_size_t size
)
1728 iana_safi_t pkt_safi
;
1731 struct peer_af
*paf
;
1732 struct update_group
*updgrp
;
1733 struct peer
*updgrp_peer
;
1735 /* If peer does not have the capability, send notification. */
1736 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_ADV
)) {
1737 zlog_err("%s [Error] BGP route refresh is not enabled",
1739 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
1740 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1744 /* Status must be Established. */
1745 if (peer
->status
!= Established
) {
1747 "%s [Error] Route refresh packet received under status %s",
1749 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1750 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1757 pkt_afi
= stream_getw(s
);
1758 (void)stream_getc(s
);
1759 pkt_safi
= stream_getc(s
);
1761 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1762 zlog_debug("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1763 peer
->host
, pkt_afi
, pkt_safi
);
1765 /* Convert AFI, SAFI to internal values and check. */
1766 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1768 "%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1769 peer
->host
, pkt_afi
, pkt_safi
);
1770 return BGP_PACKET_NOOP
;
1773 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) {
1775 uint8_t when_to_refresh
;
1779 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1781 zlog_info("%s ORF route refresh length error",
1783 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
1787 when_to_refresh
= stream_getc(s
);
1788 end
= stream_pnt(s
) + (size
- 5);
1790 while ((stream_pnt(s
) + 2) < end
) {
1791 orf_type
= stream_getc(s
);
1792 orf_len
= stream_getw(s
);
1794 /* orf_len in bounds? */
1795 if ((stream_pnt(s
) + orf_len
) > end
)
1796 break; /* XXX: Notify instead?? */
1797 if (orf_type
== ORF_TYPE_PREFIX
1798 || orf_type
== ORF_TYPE_PREFIX_OLD
) {
1799 uint8_t *p_pnt
= stream_pnt(s
);
1800 uint8_t *p_end
= stream_pnt(s
) + orf_len
;
1801 struct orf_prefix orfp
;
1806 int ret
= CMD_SUCCESS
;
1808 if (bgp_debug_neighbor_events(peer
)) {
1810 "%s rcvd Prefixlist ORF(%d) length %d",
1811 peer
->host
, orf_type
, orf_len
);
1814 /* we're going to read at least 1 byte of common
1816 * and 7 bytes of ORF Address-filter entry from
1822 /* ORF prefix-list name */
1823 sprintf(name
, "%s.%d.%d", peer
->host
, afi
,
1826 while (p_pnt
< p_end
) {
1827 /* If the ORF entry is malformed, want
1828 * to read as much of it
1829 * as possible without going beyond the
1830 * bounds of the entry,
1831 * to maximise debug information.
1835 sizeof(struct orf_prefix
));
1837 /* after ++: p_pnt <= p_end */
1839 & ORF_COMMON_PART_REMOVE_ALL
) {
1840 if (bgp_debug_neighbor_events(
1843 "%s rcvd Remove-All pfxlist ORF request",
1845 prefix_bgp_orf_remove_all(afi
,
1849 ok
= ((uint32_t)(p_end
- p_pnt
)
1850 >= sizeof(uint32_t));
1854 p_pnt
+= sizeof(uint32_t);
1855 orfp
.seq
= ntohl(seq
);
1859 if ((ok
= (p_pnt
< p_end
)))
1863 prefix_bgp_orf_set()
1865 if ((ok
= (p_pnt
< p_end
)))
1869 prefix_bgp_orf_set()
1871 if ((ok
= (p_pnt
< p_end
)))
1872 orfp
.p
.prefixlen
= *p_pnt
++;
1873 orfp
.p
.family
= afi2family(
1874 afi
); /* afi checked already */
1877 orfp
.p
.prefixlen
); /* 0 if not
1881 &orfp
.p
)) /* valid for
1885 psize
= prefix_blen(&orfp
.p
);
1888 > (p_end
- p_pnt
)) /* valid for
1892 psize
= p_end
- p_pnt
;
1896 memcpy(&orfp
.p
.u
.prefix
, p_pnt
,
1900 if (bgp_debug_neighbor_events(peer
)) {
1901 char buf
[INET6_BUFSIZ
];
1904 "%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
1906 (common
& ORF_COMMON_PART_REMOVE
1909 (common
& ORF_COMMON_PART_DENY
1920 ok
? "" : " MALFORMED");
1924 ret
= prefix_bgp_orf_set(
1926 (common
& ORF_COMMON_PART_DENY
1929 (common
& ORF_COMMON_PART_REMOVE
1933 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
)) {
1935 "%s Received misformatted prefixlist ORF."
1936 " Remove All pfxlist",
1938 prefix_bgp_orf_remove_all(afi
,
1944 peer
->orf_plist
[afi
][safi
] =
1945 prefix_bgp_orf_lookup(afi
, name
);
1947 stream_forward_getp(s
, orf_len
);
1949 if (bgp_debug_neighbor_events(peer
))
1950 zlog_debug("%s rcvd Refresh %s ORF request", peer
->host
,
1951 when_to_refresh
== REFRESH_DEFER
1954 if (when_to_refresh
== REFRESH_DEFER
)
1955 return BGP_PACKET_NOOP
;
1958 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1959 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1960 PEER_STATUS_ORF_WAIT_REFRESH
))
1961 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
1962 PEER_STATUS_ORF_WAIT_REFRESH
);
1964 paf
= peer_af_find(peer
, afi
, safi
);
1965 if (paf
&& paf
->subgroup
) {
1966 if (peer
->orf_plist
[afi
][safi
]) {
1967 updgrp
= PAF_UPDGRP(paf
);
1968 updgrp_peer
= UPDGRP_PEER(updgrp
);
1969 updgrp_peer
->orf_plist
[afi
][safi
] =
1970 peer
->orf_plist
[afi
][safi
];
1973 /* If the peer is configured for default-originate clear the
1974 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
1978 if (CHECK_FLAG(paf
->subgroup
->sflags
,
1979 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1980 UNSET_FLAG(paf
->subgroup
->sflags
,
1981 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1984 /* Perform route refreshment to the peer */
1985 bgp_announce_route(peer
, afi
, safi
);
1987 /* No FSM action necessary */
1988 return BGP_PACKET_NOOP
;
1992 * Parse BGP CAPABILITY message for peer.
1995 * @param size size of the packet
1996 * @return as in summary
1998 static int bgp_capability_msg_parse(struct peer
*peer
, uint8_t *pnt
,
2002 struct capability_mp_data mpc
;
2003 struct capability_header
*hdr
;
2007 iana_safi_t pkt_safi
;
2013 /* We need at least action, capability code and capability
2015 if (pnt
+ 3 > end
) {
2016 zlog_info("%s Capability length error", peer
->host
);
2017 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2021 hdr
= (struct capability_header
*)(pnt
+ 1);
2023 /* Action value check. */
2024 if (action
!= CAPABILITY_ACTION_SET
2025 && action
!= CAPABILITY_ACTION_UNSET
) {
2026 zlog_info("%s Capability Action Value error %d",
2027 peer
->host
, action
);
2028 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2032 if (bgp_debug_neighbor_events(peer
))
2034 "%s CAPABILITY has action: %d, code: %u, length %u",
2035 peer
->host
, action
, hdr
->code
, hdr
->length
);
2037 /* Capability length check. */
2038 if ((pnt
+ hdr
->length
+ 3) > end
) {
2039 zlog_info("%s Capability length error", peer
->host
);
2040 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
2044 /* Fetch structure to the byte stream. */
2045 memcpy(&mpc
, pnt
+ 3, sizeof(struct capability_mp_data
));
2046 pnt
+= hdr
->length
+ 3;
2048 /* We know MP Capability Code. */
2049 if (hdr
->code
== CAPABILITY_CODE_MP
) {
2050 pkt_afi
= ntohs(mpc
.afi
);
2051 pkt_safi
= mpc
.safi
;
2053 /* Ignore capability when override-capability is set. */
2054 if (CHECK_FLAG(peer
->flags
,
2055 PEER_FLAG_OVERRIDE_CAPABILITY
))
2058 /* Convert AFI, SAFI to internal values. */
2059 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
,
2061 if (bgp_debug_neighbor_events(peer
))
2063 "%s Dynamic Capability MP_EXT afi/safi invalid "
2065 peer
->host
, pkt_afi
, pkt_safi
);
2069 /* Address family check. */
2070 if (bgp_debug_neighbor_events(peer
))
2072 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2074 action
== CAPABILITY_ACTION_SET
2079 if (action
== CAPABILITY_ACTION_SET
) {
2080 peer
->afc_recv
[afi
][safi
] = 1;
2081 if (peer
->afc
[afi
][safi
]) {
2082 peer
->afc_nego
[afi
][safi
] = 1;
2083 bgp_announce_route(peer
, afi
, safi
);
2086 peer
->afc_recv
[afi
][safi
] = 0;
2087 peer
->afc_nego
[afi
][safi
] = 0;
2089 if (peer_active_nego(peer
))
2090 bgp_clear_route(peer
, afi
, safi
);
2096 "%s unrecognized capability code: %d - ignored",
2097 peer
->host
, hdr
->code
);
2101 /* No FSM action necessary */
2102 return BGP_PACKET_NOOP
;
2106 * Parse BGP CAPABILITY message for peer.
2108 * Exported for unit testing.
2111 * @param size size of the packet
2112 * @return as in summary
2114 int bgp_capability_receive(struct peer
*peer
, bgp_size_t size
)
2118 /* Fetch pointer. */
2119 pnt
= stream_pnt(peer
->curr
);
2121 if (bgp_debug_neighbor_events(peer
))
2122 zlog_debug("%s rcv CAPABILITY", peer
->host
);
2124 /* If peer does not have the capability, send notification. */
2125 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_ADV
)) {
2126 zlog_err("%s [Error] BGP dynamic capability is not enabled",
2128 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2129 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2133 /* Status must be Established. */
2134 if (peer
->status
!= Established
) {
2136 "%s [Error] Dynamic capability packet received under status %s",
2138 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2139 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
2144 return bgp_capability_msg_parse(peer
, pnt
, size
);
2148 * Processes a peer's input buffer.
2150 * This function sidesteps the event loop and directly calls bgp_event_update()
2151 * after processing each BGP message. This is necessary to ensure proper
2152 * ordering of FSM events and unifies the behavior that was present previously,
2153 * whereby some of the packet handling functions would update the FSM and some
2154 * would not, making event flow difficult to understand. Please think twice
2155 * before hacking this.
2157 * Thread type: THREAD_EVENT
2161 int bgp_process_packet(struct thread
*thread
)
2163 /* Yes first of all get peer pointer. */
2164 struct peer
*peer
; // peer
2165 uint32_t rpkt_quanta_old
; // how many packets to read
2166 int fsm_update_result
; // return code of bgp_event_update()
2167 int mprc
; // message processing return code
2169 peer
= THREAD_ARG(thread
);
2170 rpkt_quanta_old
= atomic_load_explicit(&peer
->bgp
->rpkt_quanta
,
2171 memory_order_relaxed
);
2172 fsm_update_result
= 0;
2174 /* Guard against scheduled events that occur after peer deletion. */
2175 if (peer
->status
== Deleted
|| peer
->status
== Clearing
)
2178 unsigned int processed
= 0;
2180 while (processed
< rpkt_quanta_old
) {
2183 char notify_data_length
[2];
2185 pthread_mutex_lock(&peer
->io_mtx
);
2187 peer
->curr
= stream_fifo_pop(peer
->ibuf
);
2189 pthread_mutex_unlock(&peer
->io_mtx
);
2191 if (peer
->curr
== NULL
) // no packets to process, hmm...
2194 /* skip the marker and copy the packet length */
2195 stream_forward_getp(peer
->curr
, BGP_MARKER_SIZE
);
2196 memcpy(notify_data_length
, stream_pnt(peer
->curr
), 2);
2198 /* read in the packet length and type */
2199 size
= stream_getw(peer
->curr
);
2200 type
= stream_getc(peer
->curr
);
2202 /* BGP packet dump function. */
2203 bgp_dump_packet(peer
, type
, peer
->curr
);
2205 /* adjust size to exclude the marker + length + type */
2206 size
-= BGP_HEADER_SIZE
;
2208 /* Read rest of the packet and call each sort of packet routine
2212 atomic_fetch_add_explicit(&peer
->open_in
, 1,
2213 memory_order_relaxed
);
2214 mprc
= bgp_open_receive(peer
, size
);
2215 if (mprc
== BGP_Stop
)
2217 "%s: BGP OPEN receipt failed for peer: %s",
2218 __FUNCTION__
, peer
->host
);
2220 case BGP_MSG_UPDATE
:
2221 atomic_fetch_add_explicit(&peer
->update_in
, 1,
2222 memory_order_relaxed
);
2223 peer
->readtime
= monotime(NULL
);
2224 mprc
= bgp_update_receive(peer
, size
);
2225 if (mprc
== BGP_Stop
)
2227 "%s: BGP UPDATE receipt failed for peer: %s",
2228 __FUNCTION__
, peer
->host
);
2230 case BGP_MSG_NOTIFY
:
2231 atomic_fetch_add_explicit(&peer
->notify_in
, 1,
2232 memory_order_relaxed
);
2233 mprc
= bgp_notify_receive(peer
, size
);
2234 if (mprc
== BGP_Stop
)
2236 "%s: BGP NOTIFY receipt failed for peer: %s",
2237 __FUNCTION__
, peer
->host
);
2239 case BGP_MSG_KEEPALIVE
:
2240 peer
->readtime
= monotime(NULL
);
2241 atomic_fetch_add_explicit(&peer
->keepalive_in
, 1,
2242 memory_order_relaxed
);
2243 mprc
= bgp_keepalive_receive(peer
, size
);
2244 if (mprc
== BGP_Stop
)
2246 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2247 __FUNCTION__
, peer
->host
);
2249 case BGP_MSG_ROUTE_REFRESH_NEW
:
2250 case BGP_MSG_ROUTE_REFRESH_OLD
:
2251 atomic_fetch_add_explicit(&peer
->refresh_in
, 1,
2252 memory_order_relaxed
);
2253 mprc
= bgp_route_refresh_receive(peer
, size
);
2254 if (mprc
== BGP_Stop
)
2256 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2257 __FUNCTION__
, peer
->host
);
2259 case BGP_MSG_CAPABILITY
:
2260 atomic_fetch_add_explicit(&peer
->dynamic_cap_in
, 1,
2261 memory_order_relaxed
);
2262 mprc
= bgp_capability_receive(peer
, size
);
2263 if (mprc
== BGP_Stop
)
2265 "%s: BGP CAPABILITY receipt failed for peer: %s",
2266 __FUNCTION__
, peer
->host
);
2270 * The message type should have been sanitized before
2271 * we ever got here. Receipt of a message with an
2272 * invalid header at this point is indicative of a
2275 assert (!"Message of invalid type received during input processing");
2278 /* delete processed packet */
2279 stream_free(peer
->curr
);
2284 if (mprc
!= BGP_PACKET_NOOP
)
2285 fsm_update_result
= bgp_event_update(peer
, mprc
);
2290 * If peer was deleted, do not process any more packets. This
2291 * is usually due to executing BGP_Stop or a stub deletion.
2293 if (fsm_update_result
== FSM_PEER_TRANSFERRED
2294 || fsm_update_result
== FSM_PEER_STOPPED
)
2298 if (fsm_update_result
!= FSM_PEER_TRANSFERRED
2299 && fsm_update_result
!= FSM_PEER_STOPPED
) {
2300 pthread_mutex_lock(&peer
->io_mtx
);
2302 // more work to do, come back later
2303 if (peer
->ibuf
->count
> 0)
2304 thread_add_timer_msec(
2305 bm
->master
, bgp_process_packet
, peer
, 0,
2306 &peer
->t_process_packet
);
2308 pthread_mutex_unlock(&peer
->io_mtx
);