1 /* BGP packet management routine.
2 Copyright (C) 1999 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GNU Zebra is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include "sockunion.h" /* for inet_ntop () */
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_table.h"
39 #include "bgpd/bgp_dump.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_debug.h"
42 #include "bgpd/bgp_fsm.h"
43 #include "bgpd/bgp_route.h"
44 #include "bgpd/bgp_packet.h"
45 #include "bgpd/bgp_open.h"
46 #include "bgpd/bgp_aspath.h"
47 #include "bgpd/bgp_community.h"
48 #include "bgpd/bgp_ecommunity.h"
49 #include "bgpd/bgp_lcommunity.h"
50 #include "bgpd/bgp_network.h"
51 #include "bgpd/bgp_mplsvpn.h"
52 #include "bgpd/bgp_evpn.h"
53 #include "bgpd/bgp_advertise.h"
54 #include "bgpd/bgp_vty.h"
55 #include "bgpd/bgp_updgrp.h"
57 /* Set up BGP packet marker and packet type. */
58 int bgp_packet_set_marker(struct stream
*s
, u_char type
)
63 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
66 /* Dummy total length. This field is should be filled in later on. */
69 /* BGP packet type. */
72 /* Return current stream size. */
73 return stream_get_endp(s
);
76 /* Set BGP packet header size entry. If size is zero then use current
78 int bgp_packet_set_size(struct stream
*s
)
82 /* Preserve current pointer. */
83 cp
= stream_get_endp(s
);
84 stream_putw_at(s
, BGP_MARKER_SIZE
, cp
);
89 /* Add new packet to the peer. */
90 void bgp_packet_add(struct peer
*peer
, struct stream
*s
)
92 /* Add packet to the end of list. */
93 stream_fifo_push(peer
->obuf
, s
);
96 /* Free first packet. */
97 static void bgp_packet_delete(struct peer
*peer
)
99 stream_free(stream_fifo_pop(peer
->obuf
));
102 /* Check file descriptor whether connect is established. */
103 int bgp_connect_check(struct peer
*peer
, int change_state
)
109 /* Anyway I have to reset read and write thread. */
110 BGP_READ_OFF(peer
->t_read
);
111 BGP_WRITE_OFF(peer
->t_write
);
113 /* Check file descriptor. */
114 slen
= sizeof(status
);
115 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
118 /* If getsockopt is fail, this is fatal error. */
120 zlog_info("can't get sockopt for nonblocking connect");
121 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
125 /* When status is 0 then TCP connection is established. */
127 BGP_EVENT_ADD(peer
, TCP_connection_open
);
130 if (bgp_debug_neighbor_events(peer
))
131 zlog_debug("%s [Event] Connect failed (%s)", peer
->host
,
132 safe_strerror(errno
));
134 BGP_EVENT_ADD(peer
, TCP_connection_open_failed
);
139 static struct stream
*bgp_update_packet_eor(struct peer
*peer
, afi_t afi
,
146 if (DISABLE_BGP_ANNOUNCE
)
149 if (bgp_debug_neighbor_events(peer
))
150 zlog_debug("send End-of-RIB for %s to %s",
151 afi_safi_print(afi
, safi
), peer
->host
);
153 s
= stream_new(BGP_MAX_PACKET_SIZE
);
155 /* Make BGP update packet. */
156 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
158 /* Unfeasible Routes Length */
161 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
162 /* Total Path Attribute Length */
165 /* Convert AFI, SAFI to values for packet. */
166 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
168 /* Total Path Attribute Length */
170 stream_putc(s
, BGP_ATTR_FLAG_OPTIONAL
);
171 stream_putc(s
, BGP_ATTR_MP_UNREACH_NLRI
);
173 stream_putw(s
, pkt_afi
);
174 stream_putc(s
, pkt_safi
);
177 bgp_packet_set_size(s
);
178 bgp_packet_add(peer
, s
);
182 /* Get next packet to be written. */
183 static struct stream
*bgp_write_packet(struct peer
*peer
)
185 struct stream
*s
= NULL
;
187 struct bpacket
*next_pkt
;
191 s
= stream_fifo_head(peer
->obuf
);
196 * The code beyond this part deals with update packets, proceed only
197 * if peer is Established and updates are not on hold (as part of
198 * update-delay post processing).
200 if (peer
->status
!= Established
)
203 if (peer
->bgp
&& peer
->bgp
->main_peers_update_hold
)
206 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
207 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
208 paf
= peer_af_find(peer
, afi
, safi
);
209 if (!paf
|| !PAF_SUBGRP(paf
))
211 next_pkt
= paf
->next_pkt_to_send
;
213 /* Try to generate a packet for the peer if we are at
215 * the list. Always try to push out WITHDRAWs first. */
216 if (!next_pkt
|| !next_pkt
->buffer
) {
217 next_pkt
= subgroup_withdraw_packet(
219 if (!next_pkt
|| !next_pkt
->buffer
)
220 subgroup_update_packet(PAF_SUBGRP(paf
));
221 next_pkt
= paf
->next_pkt_to_send
;
224 /* If we still don't have a packet to send to the peer,
226 * try to find out out if we have to send eor or if not,
228 * the next AFI, SAFI.
229 * Don't send the EOR prematurely... if the subgroup's
231 * timer is running, the adjacency-out structure is not
235 if (!next_pkt
|| !next_pkt
->buffer
) {
236 if (CHECK_FLAG(peer
->cap
,
237 PEER_CAP_RESTART_RCV
)) {
238 if (!(PAF_SUBGRP(paf
))->t_coalesce
239 && peer
->afc_nego
[afi
][safi
]
244 PEER_STATUS_EOR_SEND
)
245 && safi
!= SAFI_EVPN
) {
246 SET_FLAG(peer
->af_sflags
[afi
]
248 PEER_STATUS_EOR_SEND
);
249 return bgp_update_packet_eor(
258 * Found a packet template to send, overwrite packet
260 * attributes from peer and advance peer
262 s
= bpacket_reformat_for_peer(next_pkt
, paf
);
263 bpacket_queue_advance_peer(paf
);
270 /* The next action for the peer from a write perspective */
271 static void bgp_write_proceed_actions(struct peer
*peer
)
276 struct bpacket
*next_pkt
;
278 struct update_subgroup
*subgrp
;
280 if (stream_fifo_head(peer
->obuf
)) {
281 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
285 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
286 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
287 paf
= peer_af_find(peer
, afi
, safi
);
290 subgrp
= paf
->subgroup
;
294 next_pkt
= paf
->next_pkt_to_send
;
295 if (next_pkt
&& next_pkt
->buffer
) {
296 BGP_WRITE_ON(peer
->t_write
, bgp_write
,
301 /* No packets readily available for AFI/SAFI, are there
303 * that need to be generated? */
304 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
),
305 SUBGRP_PKTQ(subgrp
)))
307 else if (subgroup_packets_to_build(subgrp
)) {
308 BGP_WRITE_ON(peer
->t_write
, bgp_write
,
313 /* No packets to send, see if EOR is pending */
314 if (CHECK_FLAG(peer
->cap
, PEER_CAP_RESTART_RCV
)) {
315 if (!subgrp
->t_coalesce
316 && peer
->afc_nego
[afi
][safi
]
318 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
319 PEER_STATUS_EOR_SEND
)
320 && safi
!= SAFI_MPLS_VPN
) {
321 BGP_WRITE_ON(peer
->t_write
, bgp_write
,
328 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
333 /* Write packet to the peer. */
334 int bgp_write(struct thread
*thread
)
340 int update_last_write
= 0;
341 unsigned int count
= 0;
344 /* Yes first of all get peer pointer. */
345 peer
= THREAD_ARG(thread
);
346 peer
->t_write
= NULL
;
348 /* For non-blocking IO check. */
349 if (peer
->status
== Connect
) {
350 bgp_connect_check(peer
, 1);
354 s
= bgp_write_packet(peer
);
356 bgp_write_proceed_actions(peer
);
360 sockopt_cork(peer
->fd
, 1);
362 oc
= peer
->update_out
;
364 /* Nonblocking write until TCP output buffer is full. */
368 /* Number of bytes to be sent. */
369 writenum
= stream_get_endp(s
) - stream_get_getp(s
);
371 /* Call write() system call. */
372 num
= write(peer
->fd
, STREAM_PNT(s
), writenum
);
374 /* write failed either retry needed or error */
375 if (ERRNO_IO_RETRY(errno
))
378 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
382 if (num
!= writenum
) {
384 stream_forward_getp(s
, num
);
388 /* Retrieve BGP packet type. */
389 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
390 type
= stream_getc(s
);
401 /* Double start timer. */
404 /* Overflow check. */
405 if (peer
->v_start
>= (60 * 2))
406 peer
->v_start
= (60 * 2);
408 /* Flush any existing events */
409 BGP_EVENT_ADD(peer
, BGP_Stop
);
412 case BGP_MSG_KEEPALIVE
:
413 peer
->keepalive_out
++;
415 case BGP_MSG_ROUTE_REFRESH_NEW
:
416 case BGP_MSG_ROUTE_REFRESH_OLD
:
419 case BGP_MSG_CAPABILITY
:
420 peer
->dynamic_cap_out
++;
424 /* OK we send packet so delete it. */
425 bgp_packet_delete(peer
);
426 update_last_write
= 1;
427 } while (++count
< peer
->bgp
->wpkt_quanta
428 && (s
= bgp_write_packet(peer
)) != NULL
);
430 bgp_write_proceed_actions(peer
);
433 /* Update last_update if UPDATEs were written. */
434 if (peer
->update_out
> oc
)
435 peer
->last_update
= bgp_clock();
437 /* If we TXed any flavor of packet update last_write */
438 if (update_last_write
)
439 peer
->last_write
= bgp_clock();
441 sockopt_cork(peer
->fd
, 0);
445 /* This is only for sending NOTIFICATION message to neighbor. */
446 static int bgp_write_notify(struct peer
*peer
)
452 /* There should be at least one packet. */
453 s
= stream_fifo_head(peer
->obuf
);
456 assert(stream_get_endp(s
) >= BGP_HEADER_SIZE
);
458 /* Stop collecting data within the socket */
459 sockopt_cork(peer
->fd
, 0);
461 /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
462 * we only care about getting a clean shutdown at this point. */
463 ret
= write(peer
->fd
, STREAM_DATA(s
), stream_get_endp(s
));
465 /* only connection reset/close gets counted as TCP_fatal_error, failure
466 * to write the entire NOTIFY doesn't get different FSM treatment */
468 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
472 /* Disable Nagle, make NOTIFY packet go out right away */
474 (void)setsockopt(peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
,
477 /* Retrieve BGP packet type. */
478 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
479 type
= stream_getc(s
);
481 assert(type
== BGP_MSG_NOTIFY
);
483 /* Type should be notify. */
486 /* Double start timer. */
489 /* Overflow check. */
490 if (peer
->v_start
>= (60 * 2))
491 peer
->v_start
= (60 * 2);
493 /* Handle Graceful Restart case where the state changes to
494 Connect instead of Idle */
495 BGP_EVENT_ADD(peer
, BGP_Stop
);
500 /* Make keepalive packet and send it to the peer. */
501 void bgp_keepalive_send(struct peer
*peer
)
505 s
= stream_new(BGP_MAX_PACKET_SIZE
);
507 /* Make keepalive packet. */
508 bgp_packet_set_marker(s
, BGP_MSG_KEEPALIVE
);
510 /* Set packet size. */
511 (void)bgp_packet_set_size(s
);
513 /* Dump packet if debug option is set. */
514 /* bgp_packet_dump (s); */
516 if (bgp_debug_keepalive(peer
))
517 zlog_debug("%s sending KEEPALIVE", peer
->host
);
519 /* Add packet to the peer. */
520 bgp_packet_add(peer
, s
);
522 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
525 /* Make open packet and send it to the peer. */
526 void bgp_open_send(struct peer
*peer
)
529 u_int16_t send_holdtime
;
532 if (CHECK_FLAG(peer
->config
, PEER_CONFIG_TIMER
))
533 send_holdtime
= peer
->holdtime
;
535 send_holdtime
= peer
->bgp
->default_holdtime
;
537 /* local-as Change */
538 if (peer
->change_local_as
)
539 local_as
= peer
->change_local_as
;
541 local_as
= peer
->local_as
;
543 s
= stream_new(BGP_MAX_PACKET_SIZE
);
545 /* Make open packet. */
546 bgp_packet_set_marker(s
, BGP_MSG_OPEN
);
548 /* Set open packet values. */
549 stream_putc(s
, BGP_VERSION_4
); /* BGP version */
551 (local_as
<= BGP_AS_MAX
) ? (u_int16_t
)local_as
553 stream_putw(s
, send_holdtime
); /* Hold Time */
554 stream_put_in_addr(s
, &peer
->local_id
); /* BGP Identifier */
556 /* Set capability code. */
557 bgp_open_capability(s
, peer
);
559 /* Set BGP packet length. */
560 (void)bgp_packet_set_size(s
);
562 if (bgp_debug_neighbor_events(peer
))
564 "%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
565 peer
->host
, BGP_VERSION_4
, local_as
, send_holdtime
,
566 inet_ntoa(peer
->local_id
));
568 /* Dump packet if debug option is set. */
569 /* bgp_packet_dump (s); */
571 /* Add packet to the peer. */
572 bgp_packet_add(peer
, s
);
574 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
577 /* Send BGP notify packet with data potion. */
578 void bgp_notify_send_with_data(struct peer
*peer
, u_char code
, u_char sub_code
,
579 u_char
*data
, size_t datalen
)
584 /* Allocate new stream. */
585 s
= stream_new(BGP_MAX_PACKET_SIZE
);
587 /* Make nitify packet. */
588 bgp_packet_set_marker(s
, BGP_MSG_NOTIFY
);
590 /* Set notify packet values. */
591 stream_putc(s
, code
); /* BGP notify code */
592 stream_putc(s
, sub_code
); /* BGP notify sub_code */
594 /* If notify data is present. */
596 stream_write(s
, data
, datalen
);
598 /* Set BGP packet length. */
599 length
= bgp_packet_set_size(s
);
601 /* Add packet to the peer. */
602 stream_fifo_clean(peer
->obuf
);
603 bgp_packet_add(peer
, s
);
607 struct bgp_notify bgp_notify
;
612 bgp_notify
.code
= code
;
613 bgp_notify
.subcode
= sub_code
;
614 bgp_notify
.data
= NULL
;
615 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
616 bgp_notify
.raw_data
= data
;
618 peer
->notify
.code
= bgp_notify
.code
;
619 peer
->notify
.subcode
= bgp_notify
.subcode
;
621 if (bgp_notify
.length
) {
623 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
624 for (i
= 0; i
< bgp_notify
.length
; i
++)
626 sprintf(c
, " %02x", data
[i
]);
627 strcat(bgp_notify
.data
, c
);
630 sprintf(c
, "%02x", data
[i
]);
631 strcpy(bgp_notify
.data
, c
);
634 bgp_notify_print(peer
, &bgp_notify
, "sending");
636 if (bgp_notify
.data
) {
637 XFREE(MTYPE_TMP
, bgp_notify
.data
);
638 bgp_notify
.data
= NULL
;
639 bgp_notify
.length
= 0;
643 /* peer reset cause */
644 if (code
== BGP_NOTIFY_CEASE
) {
645 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
646 peer
->last_reset
= PEER_DOWN_USER_RESET
;
647 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
648 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
650 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
652 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
654 /* Call immediately. */
655 BGP_WRITE_OFF(peer
->t_write
);
657 bgp_write_notify(peer
);
660 /* Send BGP notify packet. */
661 void bgp_notify_send(struct peer
*peer
, u_char code
, u_char sub_code
)
663 bgp_notify_send_with_data(peer
, code
, sub_code
, NULL
, 0);
666 /* Send route refresh message to the peer. */
667 void bgp_route_refresh_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
668 u_char orf_type
, u_char when_to_refresh
, int remove
)
671 struct bgp_filter
*filter
;
676 if (DISABLE_BGP_ANNOUNCE
)
679 filter
= &peer
->filter
[afi
][safi
];
681 /* Convert AFI, SAFI to values for packet. */
682 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
684 s
= stream_new(BGP_MAX_PACKET_SIZE
);
686 /* Make BGP update packet. */
687 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
688 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_NEW
);
690 bgp_packet_set_marker(s
, BGP_MSG_ROUTE_REFRESH_OLD
);
692 /* Encode Route Refresh message. */
693 stream_putw(s
, pkt_afi
);
695 stream_putc(s
, pkt_safi
);
697 if (orf_type
== ORF_TYPE_PREFIX
|| orf_type
== ORF_TYPE_PREFIX_OLD
)
698 if (remove
|| filter
->plist
[FILTER_IN
].plist
) {
703 stream_putc(s
, when_to_refresh
);
704 stream_putc(s
, orf_type
);
705 orfp
= stream_get_endp(s
);
709 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
710 PEER_STATUS_ORF_PREFIX_SEND
);
711 stream_putc(s
, ORF_COMMON_PART_REMOVE_ALL
);
712 if (bgp_debug_neighbor_events(peer
))
714 "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
715 peer
->host
, orf_type
,
716 (when_to_refresh
== REFRESH_DEFER
721 SET_FLAG(peer
->af_sflags
[afi
][safi
],
722 PEER_STATUS_ORF_PREFIX_SEND
);
723 prefix_bgp_orf_entry(
724 s
, filter
->plist
[FILTER_IN
].plist
,
726 ORF_COMMON_PART_PERMIT
,
727 ORF_COMMON_PART_DENY
);
728 if (bgp_debug_neighbor_events(peer
))
730 "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
731 peer
->host
, orf_type
,
732 (when_to_refresh
== REFRESH_DEFER
738 /* Total ORF Entry Len. */
739 orf_len
= stream_get_endp(s
) - orfp
- 2;
740 stream_putw_at(s
, orfp
, orf_len
);
743 /* Set packet size. */
744 (void)bgp_packet_set_size(s
);
746 if (bgp_debug_neighbor_events(peer
)) {
748 zlog_debug("%s sending REFRESH_REQ for afi/safi: %d/%d",
749 peer
->host
, pkt_afi
, pkt_safi
);
752 /* Add packet to the peer. */
753 bgp_packet_add(peer
, s
);
755 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
758 /* Send capability message to the peer. */
759 void bgp_capability_send(struct peer
*peer
, afi_t afi
, safi_t safi
,
760 int capability_code
, int action
)
766 /* Convert AFI, SAFI to values for packet. */
767 bgp_map_afi_safi_int2iana(afi
, safi
, &pkt_afi
, &pkt_safi
);
769 s
= stream_new(BGP_MAX_PACKET_SIZE
);
771 /* Make BGP update packet. */
772 bgp_packet_set_marker(s
, BGP_MSG_CAPABILITY
);
774 /* Encode MP_EXT capability. */
775 if (capability_code
== CAPABILITY_CODE_MP
) {
776 stream_putc(s
, action
);
777 stream_putc(s
, CAPABILITY_CODE_MP
);
778 stream_putc(s
, CAPABILITY_CODE_MP_LEN
);
779 stream_putw(s
, pkt_afi
);
781 stream_putc(s
, pkt_safi
);
783 if (bgp_debug_neighbor_events(peer
))
785 "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
787 action
== CAPABILITY_ACTION_SET
? "Advertising"
792 /* Set packet size. */
793 (void)bgp_packet_set_size(s
);
795 /* Add packet to the peer. */
796 bgp_packet_add(peer
, s
);
798 BGP_WRITE_ON(peer
->t_write
, bgp_write
, peer
->fd
);
801 /* RFC1771 6.8 Connection collision detection. */
802 static int bgp_collision_detect(struct peer
*new, struct in_addr remote_id
)
806 /* Upon receipt of an OPEN message, the local system must examine
807 all of its connections that are in the OpenConfirm state. A BGP
808 speaker may also examine connections in an OpenSent state if it
809 knows the BGP Identifier of the peer by means outside of the
810 protocol. If among these connections there is a connection to a
811 remote BGP speaker whose BGP Identifier equals the one in the
812 OPEN message, then the local system performs the following
813 collision resolution procedure: */
815 if ((peer
= new->doppelganger
) != NULL
) {
816 /* Do not accept the new connection in Established or Clearing
818 * Note that a peer GR is handled by closing the existing
820 * upon receipt of new one.
822 if (peer
->status
== Established
|| peer
->status
== Clearing
) {
823 bgp_notify_send(new, BGP_NOTIFY_CEASE
,
824 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
826 } else if ((peer
->status
== OpenConfirm
)
827 || (peer
->status
== OpenSent
)) {
828 /* 1. The BGP Identifier of the local system is compared
830 the BGP Identifier of the remote system (as specified
832 the OPEN message). */
834 if (ntohl(peer
->local_id
.s_addr
)
835 < ntohl(remote_id
.s_addr
))
836 if (!CHECK_FLAG(peer
->sflags
,
837 PEER_STATUS_ACCEPT_PEER
)) {
838 /* 2. If the value of the local BGP
840 than the remote one, the local system
842 connection that already exists (the
844 already in the OpenConfirm state),
846 connection initiated by the remote
849 peer
, BGP_NOTIFY_CEASE
,
850 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
854 new, BGP_NOTIFY_CEASE
,
855 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
859 /* 3. Otherwise, the local system closes newly
861 BGP connection (the one associated with the
863 received OPEN message), and continues to use
865 existing one (the one that is already in the
866 OpenConfirm state). */
867 if (CHECK_FLAG(peer
->sflags
,
868 PEER_STATUS_ACCEPT_PEER
)) {
870 peer
, BGP_NOTIFY_CEASE
,
871 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
875 new, BGP_NOTIFY_CEASE
,
876 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
885 static int bgp_open_receive(struct peer
*peer
, bgp_size_t size
)
891 u_int16_t send_holdtime
;
894 struct in_addr remote_id
;
896 u_int8_t notify_data_remote_as
[2];
897 u_int8_t notify_data_remote_as4
[4];
898 u_int8_t notify_data_remote_id
[4];
899 u_int16_t
*holdtime_ptr
;
901 /* Parse open packet. */
902 version
= stream_getc(peer
->ibuf
);
903 memcpy(notify_data_remote_as
, stream_pnt(peer
->ibuf
), 2);
904 remote_as
= stream_getw(peer
->ibuf
);
905 holdtime_ptr
= (u_int16_t
*)stream_pnt(peer
->ibuf
);
906 holdtime
= stream_getw(peer
->ibuf
);
907 memcpy(notify_data_remote_id
, stream_pnt(peer
->ibuf
), 4);
908 remote_id
.s_addr
= stream_get_ipv4(peer
->ibuf
);
910 /* Receive OPEN message log */
911 if (bgp_debug_neighbor_events(peer
))
913 "%s rcv OPEN, version %d, remote-as (in open) %u,"
914 " holdtime %d, id %s",
915 peer
->host
, version
, remote_as
, holdtime
,
916 inet_ntoa(remote_id
));
918 /* BEGIN to read the capability here, but dont do it yet */
920 optlen
= stream_getc(peer
->ibuf
);
923 /* If not enough bytes, it is an error. */
924 if (STREAM_READABLE(peer
->ibuf
) < optlen
) {
925 bgp_notify_send(peer
, BGP_NOTIFY_OPEN_ERR
,
926 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
930 /* We need the as4 capability value *right now* because
931 * if it is there, we have not got the remote_as yet, and
933 * that we do not know which peer is connecting to us now.
935 as4
= peek_for_as4_capability(peer
, optlen
);
936 memcpy(notify_data_remote_as4
, &as4
, 4);
939 /* Just in case we have a silly peer who sends AS4 capability set to 0
941 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
) {
942 zlog_err("%s bad OPEN, got AS4 capability, but AS4 set to 0",
944 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
945 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
946 notify_data_remote_as4
, 4);
950 if (remote_as
== BGP_AS_TRANS
) {
951 /* Take the AS4 from the capability. We must have received the
952 * capability now! Otherwise we have a asn16 peer who uses
953 * BGP_AS_TRANS, for some unknown reason.
955 if (as4
== BGP_AS_TRANS
) {
957 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
959 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
960 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
961 notify_data_remote_as4
, 4);
965 if (!as4
&& BGP_DEBUG(as4
, AS4
))
967 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
968 " Odd, but proceeding.",
970 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG(as4
, AS4
))
972 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
973 "in 2-bytes, very odd peer.",
978 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
980 /* If we have got the capability, peer->as4cap must match
982 if (CHECK_FLAG(peer
->cap
, PEER_CAP_AS4_RCV
)
983 && as4
!= remote_as
) {
984 /* raise error, log this, close session */
986 "%s bad OPEN, got AS4 capability, but remote_as %u"
987 " mismatch with 16bit 'myasn' %u in open",
988 peer
->host
, as4
, remote_as
);
989 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
990 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
991 notify_data_remote_as4
, 4);
996 /* remote router-id check. */
997 if (remote_id
.s_addr
== 0 || IPV4_CLASS_DE(ntohl(remote_id
.s_addr
))
998 || ntohl(peer
->local_id
.s_addr
) == ntohl(remote_id
.s_addr
)) {
999 if (bgp_debug_neighbor_events(peer
))
1000 zlog_debug("%s bad OPEN, wrong router identifier %s",
1001 peer
->host
, inet_ntoa(remote_id
));
1002 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1003 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1004 notify_data_remote_id
, 4);
1008 /* Set remote router-id */
1009 peer
->remote_id
= remote_id
;
1011 /* Peer BGP version check. */
1012 if (version
!= BGP_VERSION_4
) {
1013 u_int16_t maxver
= htons(BGP_VERSION_4
);
1014 /* XXX this reply may not be correct if version < 4 XXX */
1015 if (bgp_debug_neighbor_events(peer
))
1017 "%s bad protocol version, remote requested %d, local request %d",
1018 peer
->host
, version
, BGP_VERSION_4
);
1019 /* Data must be in network byte order here */
1020 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1021 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1022 (u_int8_t
*)&maxver
, 2);
1026 /* Check neighbor as number. */
1027 if (peer
->as_type
== AS_UNSPECIFIED
) {
1028 if (bgp_debug_neighbor_events(peer
))
1030 "%s bad OPEN, remote AS is unspecified currently",
1032 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1033 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1034 notify_data_remote_as
, 2);
1036 } else if (peer
->as_type
== AS_INTERNAL
) {
1037 if (remote_as
!= peer
->bgp
->as
) {
1038 if (bgp_debug_neighbor_events(peer
))
1040 "%s bad OPEN, remote AS is %u, internal specified",
1041 peer
->host
, remote_as
);
1042 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1043 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1044 notify_data_remote_as
, 2);
1047 peer
->as
= peer
->local_as
;
1048 } else if (peer
->as_type
== AS_EXTERNAL
) {
1049 if (remote_as
== peer
->bgp
->as
) {
1050 if (bgp_debug_neighbor_events(peer
))
1052 "%s bad OPEN, remote AS is %u, external specified",
1053 peer
->host
, remote_as
);
1054 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1055 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1056 notify_data_remote_as
, 2);
1059 peer
->as
= remote_as
;
1060 } else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
)) {
1061 if (bgp_debug_neighbor_events(peer
))
1062 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1063 peer
->host
, remote_as
, peer
->as
);
1064 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1065 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1066 notify_data_remote_as
, 2);
1070 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1071 calculate the value of the Hold Timer by using the smaller of its
1072 configured Hold Time and the Hold Time received in the OPEN message.
1073 The Hold Time MUST be either zero or at least three seconds. An
1074 implementation may reject connections on the basis of the Hold Time.
1077 if (holdtime
< 3 && holdtime
!= 0) {
1078 bgp_notify_send_with_data(peer
, BGP_NOTIFY_OPEN_ERR
,
1079 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1080 (u_char
*)holdtime_ptr
, 2);
1084 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1085 would be one third of the Hold Time interval. KEEPALIVE messages
1086 MUST NOT be sent more frequently than one per second. An
1087 implementation MAY adjust the rate at which it sends KEEPALIVE
1088 messages as a function of the Hold Time interval. */
1090 if (CHECK_FLAG(peer
->config
, PEER_CONFIG_TIMER
))
1091 send_holdtime
= peer
->holdtime
;
1093 send_holdtime
= peer
->bgp
->default_holdtime
;
1095 if (holdtime
< send_holdtime
)
1096 peer
->v_holdtime
= holdtime
;
1098 peer
->v_holdtime
= send_holdtime
;
1100 if (CHECK_FLAG(peer
->config
, PEER_CONFIG_TIMER
))
1101 peer
->v_keepalive
= peer
->keepalive
;
1103 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1105 /* Open option part parse. */
1107 if ((ret
= bgp_open_option_parse(peer
, optlen
, &mp_capability
))
1111 if (bgp_debug_neighbor_events(peer
))
1112 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1117 * Assume that the peer supports the locally configured set of
1118 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1119 * capabilities, or if 'override-capability' is configured.
1122 || CHECK_FLAG(peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
)) {
1123 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] =
1124 peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1125 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] =
1126 peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1127 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] =
1128 peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1129 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] =
1130 peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1133 /* When collision is detected and this peer is closed. Retrun
1135 ret
= bgp_collision_detect(peer
, remote_id
);
1140 if ((ret
= bgp_getsockname(peer
)) < 0) {
1141 zlog_err("%s: bgp_getsockname() failed for peer: %s",
1142 __FUNCTION__
, peer
->host
);
1146 /* Verify valid local address present based on negotiated
1147 * address-families. */
1148 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
1149 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
1150 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
1151 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]) {
1152 if (!peer
->nexthop
.v4
.s_addr
) {
1153 #if defined(HAVE_CUMULUS)
1155 "%s: No local IPv4 addr resetting connection, fd %d",
1156 peer
->host
, peer
->fd
);
1157 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1158 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1163 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
1164 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
1165 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
1166 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]) {
1167 if (IN6_IS_ADDR_UNSPECIFIED(&peer
->nexthop
.v6_global
)) {
1168 #if defined(HAVE_CUMULUS)
1170 "%s: No local IPv6 addr resetting connection, fd %d",
1171 peer
->host
, peer
->fd
);
1172 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1173 BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1178 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
1180 if ((ret
= bgp_event_update(peer
, Receive_OPEN_message
)) < 0) {
1181 zlog_err("%s: BGP event update failed for peer: %s",
1182 __FUNCTION__
, peer
->host
);
1183 /* DD: bgp send notify and reset state */
1187 peer
->packet_size
= 0;
1189 stream_reset(peer
->ibuf
);
1194 /* Called when there is a change in the EOR(implicit or explicit) status of a
1196 Ends the update-delay if all expected peers are done with EORs. */
1197 void bgp_check_update_delay(struct bgp
*bgp
)
1199 struct listnode
*node
, *nnode
;
1200 struct peer
*peer
= NULL
;
1202 if (bgp_debug_neighbor_events(peer
))
1203 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
1204 bgp
->established
, bgp
->restarted_peers
,
1205 bgp
->implicit_eors
, bgp
->explicit_eors
);
1207 if (bgp
->established
1208 <= bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
) {
1209 /* This is an extra sanity check to make sure we wait for all
1211 eligible configured peers. This check is performed if
1213 timer is on, or establish wait option is not given with the
1214 update-delay command */
1215 if (bgp
->t_establish_wait
1216 || (bgp
->v_establish_wait
== bgp
->v_update_delay
))
1217 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
1218 if (CHECK_FLAG(peer
->flags
,
1219 PEER_FLAG_CONFIG_NODE
)
1220 && !CHECK_FLAG(peer
->flags
,
1222 && !peer
->update_delay_over
) {
1223 if (bgp_debug_neighbor_events(peer
))
1225 " Peer %s pending, continuing read-only mode",
1232 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
1233 bgp
->restarted_peers
, bgp
->implicit_eors
,
1234 bgp
->explicit_eors
);
1235 bgp_update_delay_end(bgp
);
1239 /* Called if peer is known to have restarted. The restart-state bit in
1240 Graceful-Restart capability is used for that */
1241 void bgp_update_restarted_peers(struct peer
*peer
)
1243 if (!bgp_update_delay_active(peer
->bgp
))
1244 return; /* BGP update delay has ended */
1245 if (peer
->update_delay_over
)
1246 return; /* This peer has already been considered */
1248 if (bgp_debug_neighbor_events(peer
))
1249 zlog_debug("Peer %s: Checking restarted", peer
->host
);
1251 if (peer
->status
== Established
) {
1252 peer
->update_delay_over
= 1;
1253 peer
->bgp
->restarted_peers
++;
1254 bgp_check_update_delay(peer
->bgp
);
1258 /* Called as peer receives a keep-alive. Determines if this occurence can be
1259 taken as an implicit EOR for this peer.
1260 NOTE: The very first keep-alive after the Established state of a peer is
1261 considered implicit EOR for the update-delay purposes */
1262 void bgp_update_implicit_eors(struct peer
*peer
)
1264 if (!bgp_update_delay_active(peer
->bgp
))
1265 return; /* BGP update delay has ended */
1266 if (peer
->update_delay_over
)
1267 return; /* This peer has already been considered */
1269 if (bgp_debug_neighbor_events(peer
))
1270 zlog_debug("Peer %s: Checking implicit EORs", peer
->host
);
1272 if (peer
->status
== Established
) {
1273 peer
->update_delay_over
= 1;
1274 peer
->bgp
->implicit_eors
++;
1275 bgp_check_update_delay(peer
->bgp
);
1279 /* Should be called only when there is a change in the EOR_RECEIVED status
1280 for any afi/safi on a peer */
1281 static void bgp_update_explicit_eors(struct peer
*peer
)
1286 if (!bgp_update_delay_active(peer
->bgp
))
1287 return; /* BGP update delay has ended */
1288 if (peer
->update_delay_over
)
1289 return; /* This peer has already been considered */
1291 if (bgp_debug_neighbor_events(peer
))
1292 zlog_debug("Peer %s: Checking explicit EORs", peer
->host
);
1294 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1295 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
1296 if (peer
->afc_nego
[afi
][safi
]
1297 && !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1298 PEER_STATUS_EOR_RECEIVED
)) {
1299 if (bgp_debug_neighbor_events(peer
))
1301 " afi %d safi %d didnt receive EOR",
1307 peer
->update_delay_over
= 1;
1308 peer
->bgp
->explicit_eors
++;
1309 bgp_check_update_delay(peer
->bgp
);
1312 /* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers
1313 * mp_withdraw, if set, is used to nullify attr structure on most of the calling
1315 * and for evpn, passed as parameter
1317 int bgp_nlri_parse(struct peer
*peer
, struct attr
*attr
,
1318 struct bgp_nlri
*packet
, int mp_withdraw
)
1320 switch (packet
->safi
) {
1322 case SAFI_MULTICAST
:
1323 return bgp_nlri_parse_ip(peer
, mp_withdraw
? NULL
: attr
,
1326 return bgp_nlri_parse_vpn(peer
, mp_withdraw
? NULL
: attr
,
1329 return bgp_nlri_parse_evpn(peer
, attr
, packet
, mp_withdraw
);
1334 /* Parse BGP Update packet and make attribute object. */
1335 static int bgp_update_receive(struct peer
*peer
, bgp_size_t size
)
1341 struct attr_extra extra
;
1342 bgp_size_t attribute_len
;
1343 bgp_size_t update_len
;
1344 bgp_size_t withdraw_len
;
1353 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1355 /* Status must be Established. */
1356 if (peer
->status
!= Established
) {
1357 zlog_err("%s [FSM] Update packet received under status %s",
1359 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1360 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1364 /* Set initial values. */
1365 memset(&attr
, 0, sizeof(struct attr
));
1366 memset(&extra
, 0, sizeof(struct attr_extra
));
1367 memset(&nlris
, 0, sizeof(nlris
));
1368 attr
.extra
= &extra
;
1369 memset(peer
->rcvd_attr_str
, 0, BUFSIZ
);
1370 peer
->rcvd_attr_printed
= 0;
1373 end
= stream_pnt(s
) + size
;
1375 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1376 Length is too large (i.e., if Unfeasible Routes Length + Total
1377 Attribute Length + 23 exceeds the message Length), then the Error
1378 Subcode is set to Malformed Attribute List. */
1379 if (stream_pnt(s
) + 2 > end
) {
1381 "%s [Error] Update packet error"
1382 " (packet length is short for unfeasible length)",
1384 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1385 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1389 /* Unfeasible Route Length. */
1390 withdraw_len
= stream_getw(s
);
1392 /* Unfeasible Route Length check. */
1393 if (stream_pnt(s
) + withdraw_len
> end
) {
1395 "%s [Error] Update packet error"
1396 " (packet unfeasible length overflow %d)",
1397 peer
->host
, withdraw_len
);
1398 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1399 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1403 /* Unfeasible Route packet format check. */
1404 if (withdraw_len
> 0) {
1405 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1406 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1407 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt(s
);
1408 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1409 stream_forward_getp(s
, withdraw_len
);
1412 /* Attribute total length check. */
1413 if (stream_pnt(s
) + 2 > end
) {
1415 "%s [Error] Packet Error"
1416 " (update packet is short for attribute length)",
1418 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1419 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1423 /* Fetch attribute total length. */
1424 attribute_len
= stream_getw(s
);
1426 /* Attribute length check. */
1427 if (stream_pnt(s
) + attribute_len
> end
) {
1429 "%s [Error] Packet Error"
1430 " (update packet attribute length overflow %d)",
1431 peer
->host
, attribute_len
);
1432 bgp_notify_send(peer
, BGP_NOTIFY_UPDATE_ERR
,
1433 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1437 /* Certain attribute parsing errors should not be considered bad enough
1438 * to reset the session for, most particularly any partial/optional
1439 * attributes that have 'tunneled' over speakers that don't understand
1440 * them. Instead we withdraw only the prefix concerned.
1442 * Complicates the flow a little though..
1444 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1445 /* This define morphs the update case into a withdraw when lower levels
1446 * have signalled an error condition where this is best.
1448 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1450 /* Parse attribute when it exists. */
1451 if (attribute_len
) {
1452 attr_parse_ret
= bgp_attr_parse(peer
, &attr
, attribute_len
,
1453 &nlris
[NLRI_MP_UPDATE
],
1454 &nlris
[NLRI_MP_WITHDRAW
]);
1455 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
) {
1456 bgp_attr_unintern_sub(&attr
);
1461 /* Logging the attribute. */
1462 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1463 || BGP_DEBUG(update
, UPDATE_IN
)
1464 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
1465 ret
= bgp_dump_attr(peer
, &attr
, peer
->rcvd_attr_str
, BUFSIZ
);
1467 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1469 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1472 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1)) {
1473 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
1474 peer
->rcvd_attr_str
);
1475 peer
->rcvd_attr_printed
= 1;
1479 /* Network Layer Reachability Information. */
1480 update_len
= end
- stream_pnt(s
);
1483 /* Set NLRI portion to structure. */
1484 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1485 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1486 nlris
[NLRI_UPDATE
].nlri
= stream_pnt(s
);
1487 nlris
[NLRI_UPDATE
].length
= update_len
;
1488 stream_forward_getp(s
, update_len
);
1491 if (BGP_DEBUG(update
, UPDATE_IN
))
1492 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1493 peer
->host
, withdraw_len
, attribute_len
, update_len
);
1495 /* Parse any given NLRIs */
1496 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++) {
1500 /* NLRI is processed iff the peer if configured for the specific
1502 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
]) {
1504 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1505 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1509 /* EoR handled later */
1510 if (nlris
[i
].length
== 0)
1515 case NLRI_MP_UPDATE
:
1516 nlri_ret
= bgp_nlri_parse(peer
, NLRI_ATTR_ARG
,
1520 case NLRI_MP_WITHDRAW
:
1521 nlri_ret
= bgp_nlri_parse(peer
, &attr
, &nlris
[i
], 1);
1528 zlog_err("%s [Error] Error parsing NLRI", peer
->host
);
1529 if (peer
->status
== Established
)
1531 peer
, BGP_NOTIFY_UPDATE_ERR
,
1533 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1534 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1535 bgp_attr_unintern_sub(&attr
);
1542 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1543 * and MP EoR should have only an empty MP_UNREACH
1545 if (!update_len
&& !withdraw_len
&& nlris
[NLRI_MP_UPDATE
].length
== 0) {
1549 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1551 * update and withdraw NLRI lengths are 0.
1553 if (!attribute_len
) {
1555 safi
= SAFI_UNICAST
;
1556 } else if (attr
.flag
& ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI
)
1557 && nlris
[NLRI_MP_WITHDRAW
].length
== 0) {
1558 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1559 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1562 if (afi
&& peer
->afc
[afi
][safi
]) {
1563 /* End-of-RIB received */
1564 if (!CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1565 PEER_STATUS_EOR_RECEIVED
)) {
1566 SET_FLAG(peer
->af_sflags
[afi
][safi
],
1567 PEER_STATUS_EOR_RECEIVED
);
1568 bgp_update_explicit_eors(peer
);
1571 /* NSF delete stale route */
1572 if (peer
->nsf
[afi
][safi
])
1573 bgp_clear_stale_route(peer
, afi
, safi
);
1575 if (bgp_debug_neighbor_events(peer
)) {
1576 zlog_debug("rcvd End-of-RIB for %s from %s",
1577 afi_safi_print(afi
, safi
),
1583 /* Everything is done. We unintern temporary structures which
1584 interned in bgp_attr_parse(). */
1585 bgp_attr_unintern_sub(&attr
);
1587 /* If peering is stopped due to some reason, do not generate BGP
1589 if (peer
->status
!= Established
)
1592 /* Increment packet counter. */
1594 peer
->update_time
= bgp_clock();
1596 /* Rearm holdtime timer */
1597 BGP_TIMER_OFF(peer
->t_holdtime
);
1598 bgp_timer_set(peer
);
1603 /* Notify message treatment function. */
1604 static void bgp_notify_receive(struct peer
*peer
, bgp_size_t size
)
1606 struct bgp_notify bgp_notify
;
1608 if (peer
->notify
.data
) {
1609 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1610 peer
->notify
.data
= NULL
;
1611 peer
->notify
.length
= 0;
1614 bgp_notify
.code
= stream_getc(peer
->ibuf
);
1615 bgp_notify
.subcode
= stream_getc(peer
->ibuf
);
1616 bgp_notify
.length
= size
- 2;
1617 bgp_notify
.data
= NULL
;
1619 /* Preserv notify code and sub code. */
1620 peer
->notify
.code
= bgp_notify
.code
;
1621 peer
->notify
.subcode
= bgp_notify
.subcode
;
1622 /* For further diagnostic record returned Data. */
1623 if (bgp_notify
.length
) {
1624 peer
->notify
.length
= size
- 2;
1625 peer
->notify
.data
= XMALLOC(MTYPE_TMP
, size
- 2);
1626 memcpy(peer
->notify
.data
, stream_pnt(peer
->ibuf
), size
- 2);
1635 if (bgp_notify
.length
) {
1637 XMALLOC(MTYPE_TMP
, bgp_notify
.length
* 3);
1638 for (i
= 0; i
< bgp_notify
.length
; i
++)
1641 stream_getc(peer
->ibuf
));
1642 strcat(bgp_notify
.data
, c
);
1646 stream_getc(peer
->ibuf
));
1647 strcpy(bgp_notify
.data
, c
);
1649 bgp_notify
.raw_data
= (u_char
*)peer
->notify
.data
;
1652 bgp_notify_print(peer
, &bgp_notify
, "received");
1653 if (bgp_notify
.data
) {
1654 XFREE(MTYPE_TMP
, bgp_notify
.data
);
1655 bgp_notify
.data
= NULL
;
1656 bgp_notify
.length
= 0;
1660 /* peer count update */
1663 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1665 /* We have to check for Notify with Unsupported Optional Parameter.
1666 in that case we fallback to open without the capability option.
1667 But this done in bgp_stop. We just mark it here to avoid changing
1669 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
1670 && bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1671 UNSET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1673 BGP_EVENT_ADD(peer
, Receive_NOTIFICATION_message
);
1676 /* Keepalive treatment function -- get keepalive send keepalive */
1677 static void bgp_keepalive_receive(struct peer
*peer
, bgp_size_t size
)
1679 if (bgp_debug_keepalive(peer
))
1680 zlog_debug("%s KEEPALIVE rcvd", peer
->host
);
1682 BGP_EVENT_ADD(peer
, Receive_KEEPALIVE_message
);
1685 /* Route refresh message is received. */
1686 static void bgp_route_refresh_receive(struct peer
*peer
, bgp_size_t size
)
1690 safi_t pkt_safi
, safi
;
1692 struct peer_af
*paf
;
1693 struct update_group
*updgrp
;
1694 struct peer
*updgrp_peer
;
1696 /* If peer does not have the capability, send notification. */
1697 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_ADV
)) {
1698 zlog_err("%s [Error] BGP route refresh is not enabled",
1700 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
1701 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1705 /* Status must be Established. */
1706 if (peer
->status
!= Established
) {
1708 "%s [Error] Route refresh packet received under status %s",
1710 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
1711 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
1718 pkt_afi
= stream_getw(s
);
1719 (void)stream_getc(s
);
1720 pkt_safi
= stream_getc(s
);
1722 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1723 zlog_debug("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1724 peer
->host
, pkt_afi
, pkt_safi
);
1726 /* Convert AFI, SAFI to internal values and check. */
1727 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
, &safi
)) {
1729 "%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1730 peer
->host
, pkt_afi
, pkt_safi
);
1734 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) {
1736 u_char when_to_refresh
;
1740 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1742 zlog_info("%s ORF route refresh length error",
1744 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
1748 when_to_refresh
= stream_getc(s
);
1749 end
= stream_pnt(s
) + (size
- 5);
1751 while ((stream_pnt(s
) + 2) < end
) {
1752 orf_type
= stream_getc(s
);
1753 orf_len
= stream_getw(s
);
1755 /* orf_len in bounds? */
1756 if ((stream_pnt(s
) + orf_len
) > end
)
1757 break; /* XXX: Notify instead?? */
1758 if (orf_type
== ORF_TYPE_PREFIX
1759 || orf_type
== ORF_TYPE_PREFIX_OLD
) {
1760 uint8_t *p_pnt
= stream_pnt(s
);
1761 uint8_t *p_end
= stream_pnt(s
) + orf_len
;
1762 struct orf_prefix orfp
;
1767 int ret
= CMD_SUCCESS
;
1769 if (bgp_debug_neighbor_events(peer
)) {
1771 "%s rcvd Prefixlist ORF(%d) length %d",
1772 peer
->host
, orf_type
, orf_len
);
1775 /* we're going to read at least 1 byte of common
1777 * and 7 bytes of ORF Address-filter entry from
1783 /* ORF prefix-list name */
1784 sprintf(name
, "%s.%d.%d", peer
->host
, afi
,
1787 while (p_pnt
< p_end
) {
1788 /* If the ORF entry is malformed, want
1789 * to read as much of it
1790 * as possible without going beyond the
1791 * bounds of the entry,
1792 * to maximise debug information.
1796 sizeof(struct orf_prefix
));
1798 /* after ++: p_pnt <= p_end */
1800 & ORF_COMMON_PART_REMOVE_ALL
) {
1801 if (bgp_debug_neighbor_events(
1804 "%s rcvd Remove-All pfxlist ORF request",
1806 prefix_bgp_orf_remove_all(afi
,
1810 ok
= ((u_int32_t
)(p_end
- p_pnt
)
1811 >= sizeof(u_int32_t
));
1815 p_pnt
+= sizeof(u_int32_t
);
1816 orfp
.seq
= ntohl(seq
);
1820 if ((ok
= (p_pnt
< p_end
)))
1824 prefix_bgp_orf_set()
1826 if ((ok
= (p_pnt
< p_end
)))
1830 prefix_bgp_orf_set()
1832 if ((ok
= (p_pnt
< p_end
)))
1833 orfp
.p
.prefixlen
= *p_pnt
++;
1834 orfp
.p
.family
= afi2family(
1835 afi
); /* afi checked already */
1838 orfp
.p
.prefixlen
); /* 0 if not
1842 &orfp
.p
)) /* valid for
1846 psize
= prefix_blen(&orfp
.p
);
1849 > (p_end
- p_pnt
)) /* valid for
1853 psize
= p_end
- p_pnt
;
1857 memcpy(&orfp
.p
.u
.prefix
, p_pnt
,
1861 if (bgp_debug_neighbor_events(peer
)) {
1862 char buf
[INET6_BUFSIZ
];
1865 "%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
1867 (common
& ORF_COMMON_PART_REMOVE
1870 (common
& ORF_COMMON_PART_DENY
1881 ok
? "" : " MALFORMED");
1885 ret
= prefix_bgp_orf_set(
1887 (common
& ORF_COMMON_PART_DENY
1890 (common
& ORF_COMMON_PART_REMOVE
1894 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
)) {
1896 "%s Received misformatted prefixlist ORF."
1897 " Remove All pfxlist",
1899 prefix_bgp_orf_remove_all(afi
,
1905 peer
->orf_plist
[afi
][safi
] =
1906 prefix_bgp_orf_lookup(afi
, name
);
1908 stream_forward_getp(s
, orf_len
);
1910 if (bgp_debug_neighbor_events(peer
))
1911 zlog_debug("%s rcvd Refresh %s ORF request", peer
->host
,
1912 when_to_refresh
== REFRESH_DEFER
1915 if (when_to_refresh
== REFRESH_DEFER
)
1919 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1920 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
1921 PEER_STATUS_ORF_WAIT_REFRESH
))
1922 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
1923 PEER_STATUS_ORF_WAIT_REFRESH
);
1925 paf
= peer_af_find(peer
, afi
, safi
);
1926 if (paf
&& paf
->subgroup
) {
1927 if (peer
->orf_plist
[afi
][safi
]) {
1928 updgrp
= PAF_UPDGRP(paf
);
1929 updgrp_peer
= UPDGRP_PEER(updgrp
);
1930 updgrp_peer
->orf_plist
[afi
][safi
] =
1931 peer
->orf_plist
[afi
][safi
];
1934 /* If the peer is configured for default-originate clear the
1935 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
1939 if (CHECK_FLAG(paf
->subgroup
->sflags
,
1940 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1941 UNSET_FLAG(paf
->subgroup
->sflags
,
1942 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1945 /* Perform route refreshment to the peer */
1946 bgp_announce_route(peer
, afi
, safi
);
1949 static int bgp_capability_msg_parse(struct peer
*peer
, u_char
*pnt
,
1953 struct capability_mp_data mpc
;
1954 struct capability_header
*hdr
;
1958 safi_t pkt_safi
, safi
;
1963 /* We need at least action, capability code and capability
1965 if (pnt
+ 3 > end
) {
1966 zlog_info("%s Capability length error", peer
->host
);
1967 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
1971 hdr
= (struct capability_header
*)(pnt
+ 1);
1973 /* Action value check. */
1974 if (action
!= CAPABILITY_ACTION_SET
1975 && action
!= CAPABILITY_ACTION_UNSET
) {
1976 zlog_info("%s Capability Action Value error %d",
1977 peer
->host
, action
);
1978 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
1982 if (bgp_debug_neighbor_events(peer
))
1984 "%s CAPABILITY has action: %d, code: %u, length %u",
1985 peer
->host
, action
, hdr
->code
, hdr
->length
);
1987 /* Capability length check. */
1988 if ((pnt
+ hdr
->length
+ 3) > end
) {
1989 zlog_info("%s Capability length error", peer
->host
);
1990 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
, 0);
1994 /* Fetch structure to the byte stream. */
1995 memcpy(&mpc
, pnt
+ 3, sizeof(struct capability_mp_data
));
1997 /* We know MP Capability Code. */
1998 if (hdr
->code
== CAPABILITY_CODE_MP
) {
1999 pkt_afi
= ntohs(mpc
.afi
);
2000 pkt_safi
= mpc
.safi
;
2002 /* Ignore capability when override-capability is set. */
2003 if (CHECK_FLAG(peer
->flags
,
2004 PEER_FLAG_OVERRIDE_CAPABILITY
))
2007 /* Convert AFI, SAFI to internal values. */
2008 if (bgp_map_afi_safi_iana2int(pkt_afi
, pkt_safi
, &afi
,
2010 if (bgp_debug_neighbor_events(peer
))
2012 "%s Dynamic Capability MP_EXT afi/safi invalid "
2014 peer
->host
, pkt_afi
, pkt_safi
);
2018 /* Address family check. */
2019 if (bgp_debug_neighbor_events(peer
))
2021 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2023 action
== CAPABILITY_ACTION_SET
2028 if (action
== CAPABILITY_ACTION_SET
) {
2029 peer
->afc_recv
[afi
][safi
] = 1;
2030 if (peer
->afc
[afi
][safi
]) {
2031 peer
->afc_nego
[afi
][safi
] = 1;
2032 bgp_announce_route(peer
, afi
, safi
);
2035 peer
->afc_recv
[afi
][safi
] = 0;
2036 peer
->afc_nego
[afi
][safi
] = 0;
2038 if (peer_active_nego(peer
))
2039 bgp_clear_route(peer
, afi
, safi
);
2041 BGP_EVENT_ADD(peer
, BGP_Stop
);
2045 "%s unrecognized capability code: %d - ignored",
2046 peer
->host
, hdr
->code
);
2048 pnt
+= hdr
->length
+ 3;
2053 /* Dynamic Capability is received.
2055 * This is exported for unit-test purposes
2057 int bgp_capability_receive(struct peer
*peer
, bgp_size_t size
)
2061 /* Fetch pointer. */
2062 pnt
= stream_pnt(peer
->ibuf
);
2064 if (bgp_debug_neighbor_events(peer
))
2065 zlog_debug("%s rcv CAPABILITY", peer
->host
);
2067 /* If peer does not have the capability, send notification. */
2068 if (!CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_ADV
)) {
2069 zlog_err("%s [Error] BGP dynamic capability is not enabled",
2071 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2072 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2076 /* Status must be Established. */
2077 if (peer
->status
!= Established
) {
2079 "%s [Error] Dynamic capability packet received under status %s",
2081 lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
2082 bgp_notify_send(peer
, BGP_NOTIFY_FSM_ERR
, 0);
2087 return bgp_capability_msg_parse(peer
, pnt
, size
);
2090 /* BGP read utility function. */
2091 static int bgp_read_packet(struct peer
*peer
)
2096 readsize
= peer
->packet_size
- stream_get_endp(peer
->ibuf
);
2098 /* If size is zero then return. */
2102 /* Read packet from fd. */
2103 nbytes
= stream_read_try(peer
->ibuf
, peer
->fd
, readsize
);
2105 /* If read byte is smaller than zero then error occured. */
2107 /* Transient error should retry */
2111 zlog_err("%s [Error] bgp_read_packet error: %s", peer
->host
,
2112 safe_strerror(errno
));
2114 if (peer
->status
== Established
) {
2115 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)) {
2116 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2117 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2119 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2122 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
2126 /* When read byte is zero : clear bgp peer and return */
2128 if (bgp_debug_neighbor_events(peer
))
2129 zlog_debug("%s [Event] BGP connection closed fd %d",
2130 peer
->host
, peer
->fd
);
2132 if (peer
->status
== Established
) {
2133 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)) {
2134 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2135 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2137 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2140 BGP_EVENT_ADD(peer
, TCP_connection_closed
);
2144 /* We read partial packet. */
2145 if (stream_get_endp(peer
->ibuf
) != peer
->packet_size
)
2152 static int bgp_marker_all_one(struct stream
*s
, int length
)
2156 for (i
= 0; i
< length
; i
++)
2157 if (s
->data
[i
] != 0xff)
2163 /* Starting point of packet process function. */
2164 int bgp_read(struct thread
*thread
)
2170 char notify_data_length
[2];
2171 u_int32_t notify_out
;
2173 /* Yes first of all get peer pointer. */
2174 peer
= THREAD_ARG(thread
);
2175 peer
->t_read
= NULL
;
2177 /* Note notify_out so we can check later to see if we sent another one
2179 notify_out
= peer
->notify_out
;
2181 /* For non-blocking IO check. */
2182 if (peer
->status
== Connect
) {
2183 bgp_connect_check(peer
, 1);
2187 zlog_err("bgp_read peer's fd is negative value %d",
2191 BGP_READ_ON(peer
->t_read
, bgp_read
, peer
->fd
);
2194 /* Read packet header to determine type of the packet */
2195 if (peer
->packet_size
== 0)
2196 peer
->packet_size
= BGP_HEADER_SIZE
;
2198 if (stream_get_endp(peer
->ibuf
) < BGP_HEADER_SIZE
) {
2199 ret
= bgp_read_packet(peer
);
2201 /* Header read error or partial read packet. */
2205 /* Get size and type. */
2206 stream_forward_getp(peer
->ibuf
, BGP_MARKER_SIZE
);
2207 memcpy(notify_data_length
, stream_pnt(peer
->ibuf
), 2);
2208 size
= stream_getw(peer
->ibuf
);
2209 type
= stream_getc(peer
->ibuf
);
2212 if (((type
== BGP_MSG_OPEN
) || (type
== BGP_MSG_KEEPALIVE
))
2213 && !bgp_marker_all_one(peer
->ibuf
, BGP_MARKER_SIZE
)) {
2214 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
2215 BGP_NOTIFY_HEADER_NOT_SYNC
);
2219 /* BGP type check. */
2220 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
2221 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
2222 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
2223 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
2224 && type
!= BGP_MSG_CAPABILITY
) {
2225 if (bgp_debug_neighbor_events(peer
))
2226 zlog_debug("%s unknown message type 0x%02x",
2228 bgp_notify_send_with_data(peer
, BGP_NOTIFY_HEADER_ERR
,
2229 BGP_NOTIFY_HEADER_BAD_MESTYPE
,
2233 /* Mimimum packet length check. */
2234 if ((size
< BGP_HEADER_SIZE
) || (size
> BGP_MAX_PACKET_SIZE
)
2235 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
2236 || (type
== BGP_MSG_UPDATE
2237 && size
< BGP_MSG_UPDATE_MIN_SIZE
)
2238 || (type
== BGP_MSG_NOTIFY
2239 && size
< BGP_MSG_NOTIFY_MIN_SIZE
)
2240 || (type
== BGP_MSG_KEEPALIVE
2241 && size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
2242 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
2243 && size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2244 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
2245 && size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2246 || (type
== BGP_MSG_CAPABILITY
2247 && size
< BGP_MSG_CAPABILITY_MIN_SIZE
)) {
2248 if (bgp_debug_neighbor_events(peer
))
2249 zlog_debug("%s bad message length - %d for %s",
2253 : bgp_type_str
[(int)type
]);
2254 bgp_notify_send_with_data(peer
, BGP_NOTIFY_HEADER_ERR
,
2255 BGP_NOTIFY_HEADER_BAD_MESLEN
,
2256 (u_char
*)notify_data_length
,
2261 /* Adjust size to message length. */
2262 peer
->packet_size
= size
;
2265 ret
= bgp_read_packet(peer
);
2269 /* Get size and type again. */
2270 (void)stream_getw_from(peer
->ibuf
, BGP_MARKER_SIZE
);
2271 type
= stream_getc_from(peer
->ibuf
, BGP_MARKER_SIZE
+ 2);
2273 /* BGP packet dump function. */
2274 bgp_dump_packet(peer
, type
, peer
->ibuf
);
2276 size
= (peer
->packet_size
- BGP_HEADER_SIZE
);
2278 /* Read rest of the packet and call each sort of packet routine */
2282 bgp_open_receive(peer
, size
); /* XXX return value ignored! */
2284 case BGP_MSG_UPDATE
:
2285 peer
->readtime
= monotime(NULL
);
2286 bgp_update_receive(peer
, size
);
2288 case BGP_MSG_NOTIFY
:
2289 bgp_notify_receive(peer
, size
);
2291 case BGP_MSG_KEEPALIVE
:
2292 peer
->readtime
= monotime(NULL
);
2293 bgp_keepalive_receive(peer
, size
);
2295 case BGP_MSG_ROUTE_REFRESH_NEW
:
2296 case BGP_MSG_ROUTE_REFRESH_OLD
:
2298 bgp_route_refresh_receive(peer
, size
);
2300 case BGP_MSG_CAPABILITY
:
2301 peer
->dynamic_cap_in
++;
2302 bgp_capability_receive(peer
, size
);
2306 /* If reading this packet caused us to send a NOTIFICATION then store a
2308 * of the packet for troubleshooting purposes
2310 if (notify_out
< peer
->notify_out
) {
2311 memcpy(peer
->last_reset_cause
, peer
->ibuf
->data
,
2313 peer
->last_reset_cause_size
= peer
->packet_size
;
2314 notify_out
= peer
->notify_out
;
2317 /* Clear input buffer. */
2318 peer
->packet_size
= 0;
2320 stream_reset(peer
->ibuf
);
2323 /* If reading this packet caused us to send a NOTIFICATION then store a
2325 * of the packet for troubleshooting purposes
2327 if (notify_out
< peer
->notify_out
) {
2328 memcpy(peer
->last_reset_cause
, peer
->ibuf
->data
,
2330 peer
->last_reset_cause_size
= peer
->packet_size
;