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_network.h"
50 #include "bgpd/bgp_mplsvpn.h"
51 #include "bgpd/bgp_encap.h"
52 #include "bgpd/bgp_advertise.h"
53 #include "bgpd/bgp_vty.h"
54 #include "bgpd/bgp_updgrp.h"
56 /* Set up BGP packet marker and packet type. */
58 bgp_packet_set_marker (struct stream
*s
, u_char type
)
63 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
64 stream_putc (s
, 0xff);
66 /* Dummy total length. This field is should be filled in later on. */
69 /* BGP packet type. */
70 stream_putc (s
, 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
79 bgp_packet_set_size (struct stream
*s
)
83 /* Preserve current pointer. */
84 cp
= stream_get_endp (s
);
85 stream_putw_at (s
, BGP_MARKER_SIZE
, cp
);
90 /* Add new packet to the peer. */
92 bgp_packet_add (struct peer
*peer
, struct stream
*s
)
94 /* Add packet to the end of list. */
95 stream_fifo_push (peer
->obuf
, s
);
98 /* Free first packet. */
100 bgp_packet_delete (struct peer
*peer
)
102 stream_free (stream_fifo_pop (peer
->obuf
));
105 /* Check file descriptor whether connect is established. */
107 bgp_connect_check (struct peer
*peer
, int change_state
)
113 /* Anyway I have to reset read and write thread. */
114 BGP_READ_OFF (peer
->t_read
);
115 BGP_WRITE_OFF (peer
->t_write
);
117 /* Check file descriptor. */
118 slen
= sizeof (status
);
119 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &status
, &slen
);
121 /* If getsockopt is fail, this is fatal error. */
124 zlog_info ("can't get sockopt for nonblocking connect");
125 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
129 /* When status is 0 then TCP connection is established. */
132 BGP_EVENT_ADD (peer
, TCP_connection_open
);
137 if (bgp_debug_neighbor_events(peer
))
138 zlog_debug ("%s [Event] Connect failed (%s)",
139 peer
->host
, safe_strerror (errno
));
141 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
146 static struct stream
*
147 bgp_update_packet_eor (struct peer
*peer
, afi_t afi
, safi_t safi
)
151 if (DISABLE_BGP_ANNOUNCE
)
154 if (bgp_debug_neighbor_events(peer
))
155 zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi
, safi
), peer
->host
);
157 s
= stream_new (BGP_MAX_PACKET_SIZE
);
159 /* Make BGP update packet. */
160 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
162 /* Unfeasible Routes Length */
165 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
167 /* Total Path Attribute Length */
172 /* Total Path Attribute Length */
174 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
175 stream_putc (s
, BGP_ATTR_MP_UNREACH_NLRI
);
177 stream_putw (s
, afi
);
178 stream_putc (s
, safi
);
181 bgp_packet_set_size (s
);
182 bgp_packet_add (peer
, s
);
186 /* Get next packet to be written. */
187 static struct stream
*
188 bgp_write_packet (struct peer
*peer
)
190 struct stream
*s
= NULL
;
192 struct bpacket
*next_pkt
;
196 s
= stream_fifo_head (peer
->obuf
);
201 * The code beyond this part deals with update packets, proceed only
202 * if peer is Established and updates are not on hold (as part of
203 * update-delay post processing).
205 if (peer
->status
!= Established
)
208 if (peer
->bgp
&& peer
->bgp
->main_peers_update_hold
)
211 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
212 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
214 paf
= peer_af_find (peer
, afi
, safi
);
215 if (!paf
|| !PAF_SUBGRP(paf
))
217 next_pkt
= paf
->next_pkt_to_send
;
219 /* Try to generate a packet for the peer if we are at the end of
220 * the list. Always try to push out WITHDRAWs first. */
221 if (!next_pkt
|| !next_pkt
->buffer
)
223 next_pkt
= subgroup_withdraw_packet(PAF_SUBGRP(paf
));
224 if (!next_pkt
|| !next_pkt
->buffer
)
225 subgroup_update_packet (PAF_SUBGRP(paf
));
226 next_pkt
= paf
->next_pkt_to_send
;
229 /* If we still don't have a packet to send to the peer, then
230 * try to find out out if we have to send eor or if not, skip to
231 * the next AFI, SAFI.
232 * Don't send the EOR prematurely... if the subgroup's coalesce
233 * timer is running, the adjacency-out structure is not created
236 if (!next_pkt
|| !next_pkt
->buffer
)
238 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
240 if (!(PAF_SUBGRP(paf
))->t_coalesce
&&
241 peer
->afc_nego
[afi
][safi
] && peer
->synctime
242 && ! CHECK_FLAG (peer
->af_sflags
[afi
][safi
],
243 PEER_STATUS_EOR_SEND
)
244 && safi
!= SAFI_MPLS_VPN
)
246 SET_FLAG (peer
->af_sflags
[afi
][safi
],
247 PEER_STATUS_EOR_SEND
);
248 return bgp_update_packet_eor (peer
, afi
, safi
);
257 * Found a packet template to send, overwrite packet with appropriate
258 * attributes from peer and advance peer
260 s
= bpacket_reformat_for_peer (next_pkt
, paf
);
261 bpacket_queue_advance_peer (paf
);
268 /* The next action for the peer from a write perspective */
270 bgp_write_proceed_actions (struct peer
*peer
)
275 struct bpacket
*next_pkt
;
277 struct update_subgroup
*subgrp
;
279 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
++)
288 paf
= peer_af_find (peer
, afi
, safi
);
291 subgrp
= paf
->subgroup
;
295 next_pkt
= paf
->next_pkt_to_send
;
296 if (next_pkt
&& next_pkt
->buffer
)
298 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
302 /* No packets readily available for AFI/SAFI, are there subgroup packets
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
))
309 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
313 /* No packets to send, see if EOR is pending */
314 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
316 if (!subgrp
->t_coalesce
&&
317 peer
->afc_nego
[afi
][safi
] &&
319 !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
320 PEER_STATUS_EOR_SEND
) &&
321 safi
!= SAFI_MPLS_VPN
)
323 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
331 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
336 /* Write packet to the peer. */
338 bgp_write (struct thread
*thread
)
344 int update_last_write
= 0;
345 unsigned int count
= 0;
348 /* Yes first of all get peer pointer. */
349 peer
= THREAD_ARG (thread
);
350 peer
->t_write
= NULL
;
352 /* For non-blocking IO check. */
353 if (peer
->status
== Connect
)
355 bgp_connect_check (peer
, 1);
359 s
= bgp_write_packet (peer
);
362 bgp_write_proceed_actions (peer
);
366 sockopt_cork (peer
->fd
, 1);
368 oc
= peer
->update_out
;
370 /* Nonblocking write until TCP output buffer is full. */
375 /* Number of bytes to be sent. */
376 writenum
= stream_get_endp (s
) - stream_get_getp (s
);
378 /* Call write() system call. */
379 num
= write (peer
->fd
, STREAM_PNT (s
), writenum
);
382 /* write failed either retry needed or error */
383 if (ERRNO_IO_RETRY(errno
))
386 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
393 stream_forward_getp (s
, num
);
397 /* Retrieve BGP packet type. */
398 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
399 type
= stream_getc (s
);
411 /* Double start timer. */
414 /* Overflow check. */
415 if (peer
->v_start
>= (60 * 2))
416 peer
->v_start
= (60 * 2);
418 /* Flush any existing events */
419 BGP_EVENT_ADD (peer
, BGP_Stop
);
422 case BGP_MSG_KEEPALIVE
:
423 peer
->keepalive_out
++;
425 case BGP_MSG_ROUTE_REFRESH_NEW
:
426 case BGP_MSG_ROUTE_REFRESH_OLD
:
429 case BGP_MSG_CAPABILITY
:
430 peer
->dynamic_cap_out
++;
434 /* OK we send packet so delete it. */
435 bgp_packet_delete (peer
);
436 update_last_write
= 1;
438 while (++count
< peer
->bgp
->wpkt_quanta
&&
439 (s
= bgp_write_packet (peer
)) != NULL
);
441 bgp_write_proceed_actions (peer
);
444 /* Update last_update if UPDATEs were written. */
445 if (peer
->update_out
> oc
)
446 peer
->last_update
= bgp_clock ();
448 /* If we TXed any flavor of packet update last_write */
449 if (update_last_write
)
450 peer
->last_write
= bgp_clock ();
452 sockopt_cork (peer
->fd
, 0);
456 /* This is only for sending NOTIFICATION message to neighbor. */
458 bgp_write_notify (struct peer
*peer
)
464 /* There should be at least one packet. */
465 s
= stream_fifo_head (peer
->obuf
);
468 assert (stream_get_endp (s
) >= BGP_HEADER_SIZE
);
470 /* Stop collecting data within the socket */
471 sockopt_cork (peer
->fd
, 0);
473 /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
474 * we only care about getting a clean shutdown at this point. */
475 ret
= write (peer
->fd
, STREAM_DATA (s
), stream_get_endp (s
));
477 /* only connection reset/close gets counted as TCP_fatal_error, failure
478 * to write the entire NOTIFY doesn't get different FSM treatment */
481 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
485 /* Disable Nagle, make NOTIFY packet go out right away */
487 (void) setsockopt (peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
488 (char *) &val
, sizeof (val
));
490 /* Retrieve BGP packet type. */
491 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
492 type
= stream_getc (s
);
494 assert (type
== BGP_MSG_NOTIFY
);
496 /* Type should be notify. */
499 /* Double start timer. */
502 /* Overflow check. */
503 if (peer
->v_start
>= (60 * 2))
504 peer
->v_start
= (60 * 2);
506 /* Handle Graceful Restart case where the state changes to
507 Connect instead of Idle */
508 BGP_EVENT_ADD (peer
, BGP_Stop
);
513 /* Make keepalive packet and send it to the peer. */
515 bgp_keepalive_send (struct peer
*peer
)
519 s
= stream_new (BGP_MAX_PACKET_SIZE
);
521 /* Make keepalive packet. */
522 bgp_packet_set_marker (s
, BGP_MSG_KEEPALIVE
);
524 /* Set packet size. */
525 (void)bgp_packet_set_size (s
);
527 /* Dump packet if debug option is set. */
528 /* bgp_packet_dump (s); */
530 if (bgp_debug_keepalive(peer
))
531 zlog_debug ("%s sending KEEPALIVE", peer
->host
);
533 /* Add packet to the peer. */
534 bgp_packet_add (peer
, s
);
536 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
539 /* Make open packet and send it to the peer. */
541 bgp_open_send (struct peer
*peer
)
544 u_int16_t send_holdtime
;
547 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
548 send_holdtime
= peer
->holdtime
;
550 send_holdtime
= peer
->bgp
->default_holdtime
;
552 /* local-as Change */
553 if (peer
->change_local_as
)
554 local_as
= peer
->change_local_as
;
556 local_as
= peer
->local_as
;
558 s
= stream_new (BGP_MAX_PACKET_SIZE
);
560 /* Make open packet. */
561 bgp_packet_set_marker (s
, BGP_MSG_OPEN
);
563 /* Set open packet values. */
564 stream_putc (s
, BGP_VERSION_4
); /* BGP version */
565 stream_putw (s
, (local_as
<= BGP_AS_MAX
) ? (u_int16_t
) local_as
567 stream_putw (s
, send_holdtime
); /* Hold Time */
568 stream_put_in_addr (s
, &peer
->local_id
); /* BGP Identifier */
570 /* Set capability code. */
571 bgp_open_capability (s
, peer
);
573 /* Set BGP packet length. */
574 (void)bgp_packet_set_size (s
);
576 if (bgp_debug_neighbor_events(peer
))
577 zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
578 peer
->host
, BGP_VERSION_4
, local_as
,
579 send_holdtime
, inet_ntoa (peer
->local_id
));
581 /* Dump packet if debug option is set. */
582 /* bgp_packet_dump (s); */
584 /* Add packet to the peer. */
585 bgp_packet_add (peer
, s
);
587 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
590 /* Send BGP notify packet with data potion. */
592 bgp_notify_send_with_data (struct peer
*peer
, u_char code
, u_char sub_code
,
593 u_char
*data
, size_t datalen
)
598 /* Allocate new stream. */
599 s
= stream_new (BGP_MAX_PACKET_SIZE
);
601 /* Make nitify packet. */
602 bgp_packet_set_marker (s
, BGP_MSG_NOTIFY
);
604 /* Set notify packet values. */
605 stream_putc (s
, code
); /* BGP notify code */
606 stream_putc (s
, sub_code
); /* BGP notify sub_code */
608 /* If notify data is present. */
610 stream_write (s
, data
, datalen
);
612 /* Set BGP packet length. */
613 length
= bgp_packet_set_size (s
);
615 /* Add packet to the peer. */
616 stream_fifo_clean (peer
->obuf
);
617 bgp_packet_add (peer
, s
);
621 struct bgp_notify bgp_notify
;
626 bgp_notify
.code
= code
;
627 bgp_notify
.subcode
= sub_code
;
628 bgp_notify
.data
= NULL
;
629 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
631 peer
->notify
.code
= bgp_notify
.code
;
632 peer
->notify
.subcode
= bgp_notify
.subcode
;
634 if (bgp_notify
.length
)
636 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
637 for (i
= 0; i
< bgp_notify
.length
; i
++)
640 sprintf (c
, " %02x", data
[i
]);
641 strcat (bgp_notify
.data
, c
);
646 sprintf (c
, "%02x", data
[i
]);
647 strcpy (bgp_notify
.data
, c
);
650 bgp_notify_print (peer
, &bgp_notify
, "sending");
654 XFREE (MTYPE_TMP
, bgp_notify
.data
);
655 bgp_notify
.data
= NULL
;
656 bgp_notify
.length
= 0;
660 /* peer reset cause */
661 if (code
== BGP_NOTIFY_CEASE
)
663 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
664 peer
->last_reset
= PEER_DOWN_USER_RESET
;
665 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
666 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
668 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
671 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
673 /* Call immediately. */
674 BGP_WRITE_OFF (peer
->t_write
);
676 bgp_write_notify (peer
);
679 /* Send BGP notify packet. */
681 bgp_notify_send (struct peer
*peer
, u_char code
, u_char sub_code
)
683 bgp_notify_send_with_data (peer
, code
, sub_code
, NULL
, 0);
686 /* Send route refresh message to the peer. */
688 bgp_route_refresh_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
689 u_char orf_type
, u_char when_to_refresh
, int remove
)
692 struct bgp_filter
*filter
;
695 if (DISABLE_BGP_ANNOUNCE
)
698 filter
= &peer
->filter
[afi
][safi
];
700 /* Adjust safi code. */
701 if (safi
== SAFI_MPLS_VPN
)
702 safi
= SAFI_MPLS_LABELED_VPN
;
704 s
= stream_new (BGP_MAX_PACKET_SIZE
);
706 /* Make BGP update packet. */
707 if (CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
708 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_NEW
);
710 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_OLD
);
712 /* Encode Route Refresh message. */
713 stream_putw (s
, afi
);
715 stream_putc (s
, safi
);
717 if (orf_type
== ORF_TYPE_PREFIX
718 || orf_type
== ORF_TYPE_PREFIX_OLD
)
719 if (remove
|| filter
->plist
[FILTER_IN
].plist
)
725 stream_putc (s
, when_to_refresh
);
726 stream_putc (s
, orf_type
);
727 orfp
= stream_get_endp (s
);
732 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
733 stream_putc (s
, ORF_COMMON_PART_REMOVE_ALL
);
734 if (bgp_debug_neighbor_events(peer
))
735 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
736 peer
->host
, orf_type
,
737 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
742 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
743 prefix_bgp_orf_entry (s
, filter
->plist
[FILTER_IN
].plist
,
744 ORF_COMMON_PART_ADD
, ORF_COMMON_PART_PERMIT
,
745 ORF_COMMON_PART_DENY
);
746 if (bgp_debug_neighbor_events(peer
))
747 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
748 peer
->host
, orf_type
,
749 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
753 /* Total ORF Entry Len. */
754 orf_len
= stream_get_endp (s
) - orfp
- 2;
755 stream_putw_at (s
, orfp
, orf_len
);
758 /* Set packet size. */
759 (void)bgp_packet_set_size (s
);
761 if (bgp_debug_neighbor_events(peer
))
764 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
765 peer
->host
, afi
, safi
);
768 /* Add packet to the peer. */
769 bgp_packet_add (peer
, s
);
771 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
774 /* Send capability message to the peer. */
776 bgp_capability_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
777 int capability_code
, int action
)
781 /* Adjust safi code. */
782 if (safi
== SAFI_MPLS_VPN
)
783 safi
= SAFI_MPLS_LABELED_VPN
;
785 s
= stream_new (BGP_MAX_PACKET_SIZE
);
787 /* Make BGP update packet. */
788 bgp_packet_set_marker (s
, BGP_MSG_CAPABILITY
);
790 /* Encode MP_EXT capability. */
791 if (capability_code
== CAPABILITY_CODE_MP
)
793 stream_putc (s
, action
);
794 stream_putc (s
, CAPABILITY_CODE_MP
);
795 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
796 stream_putw (s
, afi
);
798 stream_putc (s
, safi
);
800 if (bgp_debug_neighbor_events(peer
))
801 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
802 peer
->host
, action
== CAPABILITY_ACTION_SET
?
803 "Advertising" : "Removing", afi
, safi
);
806 /* Set packet size. */
807 (void)bgp_packet_set_size (s
);
809 /* Add packet to the peer. */
810 bgp_packet_add (peer
, s
);
812 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
815 /* RFC1771 6.8 Connection collision detection. */
817 bgp_collision_detect (struct peer
*new, struct in_addr remote_id
)
821 /* Upon receipt of an OPEN message, the local system must examine
822 all of its connections that are in the OpenConfirm state. A BGP
823 speaker may also examine connections in an OpenSent state if it
824 knows the BGP Identifier of the peer by means outside of the
825 protocol. If among these connections there is a connection to a
826 remote BGP speaker whose BGP Identifier equals the one in the
827 OPEN message, then the local system performs the following
828 collision resolution procedure: */
830 if ((peer
= new->doppelganger
) != NULL
)
832 /* Do not accept the new connection in Established or Clearing states.
833 * Note that a peer GR is handled by closing the existing connection
834 * upon receipt of new one.
836 if (peer
->status
== Established
|| peer
->status
== Clearing
)
838 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
839 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
842 else if ((peer
->status
== OpenConfirm
) || (peer
->status
== OpenSent
))
844 /* 1. The BGP Identifier of the local system is compared to
845 the BGP Identifier of the remote system (as specified in
846 the OPEN message). */
848 if (ntohl (peer
->local_id
.s_addr
) < ntohl (remote_id
.s_addr
))
849 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
851 /* 2. If the value of the local BGP Identifier is less
852 than the remote one, the local system closes BGP
853 connection that already exists (the one that is
854 already in the OpenConfirm state), and accepts BGP
855 connection initiated by the remote system. */
856 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
857 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
862 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
863 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
868 /* 3. Otherwise, the local system closes newly created
869 BGP connection (the one associated with the newly
870 received OPEN message), and continues to use the
871 existing one (the one that is already in the
872 OpenConfirm state). */
873 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
875 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
876 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
881 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
882 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
892 bgp_open_receive (struct peer
*peer
, bgp_size_t size
)
898 u_int16_t send_holdtime
;
901 struct in_addr remote_id
;
903 u_int8_t notify_data_remote_as
[2];
904 u_int8_t notify_data_remote_as4
[4];
905 u_int8_t notify_data_remote_id
[4];
906 u_int16_t
*holdtime_ptr
;
908 /* Parse open packet. */
909 version
= stream_getc (peer
->ibuf
);
910 memcpy (notify_data_remote_as
, stream_pnt (peer
->ibuf
), 2);
911 remote_as
= stream_getw (peer
->ibuf
);
912 holdtime_ptr
= (u_int16_t
*)stream_pnt (peer
->ibuf
);
913 holdtime
= stream_getw (peer
->ibuf
);
914 memcpy (notify_data_remote_id
, stream_pnt (peer
->ibuf
), 4);
915 remote_id
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
917 /* Receive OPEN message log */
918 if (bgp_debug_neighbor_events(peer
))
919 zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
920 " holdtime %d, id %s",
921 peer
->host
, version
, remote_as
, holdtime
,
922 inet_ntoa (remote_id
));
924 /* BEGIN to read the capability here, but dont do it yet */
926 optlen
= stream_getc (peer
->ibuf
);
930 /* If not enough bytes, it is an error. */
931 if (STREAM_READABLE(peer
->ibuf
) < optlen
)
933 bgp_notify_send (peer
, BGP_NOTIFY_OPEN_ERR
,
934 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
938 /* We need the as4 capability value *right now* because
939 * if it is there, we have not got the remote_as yet, and without
940 * that we do not know which peer is connecting to us now.
942 as4
= peek_for_as4_capability (peer
, optlen
);
943 memcpy (notify_data_remote_as4
, &as4
, 4);
946 /* Just in case we have a silly peer who sends AS4 capability set to 0 */
947 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
)
949 zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
951 bgp_notify_send_with_data (peer
,
953 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
954 notify_data_remote_as4
, 4);
958 if (remote_as
== BGP_AS_TRANS
)
960 /* Take the AS4 from the capability. We must have received the
961 * capability now! Otherwise we have a asn16 peer who uses
962 * BGP_AS_TRANS, for some unknown reason.
964 if (as4
== BGP_AS_TRANS
)
966 zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
968 bgp_notify_send_with_data (peer
,
970 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
971 notify_data_remote_as4
, 4);
975 if (!as4
&& BGP_DEBUG (as4
, AS4
))
976 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
977 " Odd, but proceeding.", peer
->host
);
978 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG (as4
, AS4
))
979 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
980 "in 2-bytes, very odd peer.", peer
->host
, as4
);
986 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
987 /* If we have got the capability, peer->as4cap must match remote_as */
988 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
)
991 /* raise error, log this, close session */
992 zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
993 " mismatch with 16bit 'myasn' %u in open",
994 peer
->host
, as4
, remote_as
);
995 bgp_notify_send_with_data (peer
,
997 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
998 notify_data_remote_as4
, 4);
1003 /* remote router-id check. */
1004 if (remote_id
.s_addr
== 0
1005 || IPV4_CLASS_DE (ntohl (remote_id
.s_addr
))
1006 || ntohl (peer
->local_id
.s_addr
) == ntohl (remote_id
.s_addr
))
1008 if (bgp_debug_neighbor_events(peer
))
1009 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1010 peer
->host
, inet_ntoa (remote_id
));
1011 bgp_notify_send_with_data (peer
,
1012 BGP_NOTIFY_OPEN_ERR
,
1013 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1014 notify_data_remote_id
, 4);
1018 /* Set remote router-id */
1019 peer
->remote_id
= remote_id
;
1021 /* Peer BGP version check. */
1022 if (version
!= BGP_VERSION_4
)
1024 u_int16_t maxver
= htons(BGP_VERSION_4
);
1025 /* XXX this reply may not be correct if version < 4 XXX */
1026 if (bgp_debug_neighbor_events(peer
))
1027 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
1028 peer
->host
, version
, BGP_VERSION_4
);
1029 /* Data must be in network byte order here */
1030 bgp_notify_send_with_data (peer
,
1031 BGP_NOTIFY_OPEN_ERR
,
1032 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1033 (u_int8_t
*) &maxver
, 2);
1037 /* Check neighbor as number. */
1038 if (peer
->as_type
== AS_UNSPECIFIED
)
1040 if (bgp_debug_neighbor_events(peer
))
1041 zlog_debug("%s bad OPEN, remote AS is unspecified currently", peer
->host
);
1042 bgp_notify_send_with_data(peer
,
1043 BGP_NOTIFY_OPEN_ERR
,
1044 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1045 notify_data_remote_as
, 2);
1048 else if (peer
->as_type
== AS_INTERNAL
)
1050 if (remote_as
!= peer
->bgp
->as
)
1052 if (bgp_debug_neighbor_events(peer
))
1053 zlog_debug ("%s bad OPEN, remote AS is %u, internal specified",
1054 peer
->host
, remote_as
);
1055 bgp_notify_send_with_data (peer
,
1056 BGP_NOTIFY_OPEN_ERR
,
1057 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1058 notify_data_remote_as
, 2);
1061 peer
->as
= peer
->local_as
;
1063 else if (peer
->as_type
== AS_EXTERNAL
)
1065 if (remote_as
== peer
->bgp
->as
)
1067 if (bgp_debug_neighbor_events(peer
))
1068 zlog_debug ("%s bad OPEN, remote AS is %u, external specified",
1069 peer
->host
, remote_as
);
1070 bgp_notify_send_with_data (peer
,
1071 BGP_NOTIFY_OPEN_ERR
,
1072 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1073 notify_data_remote_as
, 2);
1076 peer
->as
= remote_as
;
1078 else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
))
1080 if (bgp_debug_neighbor_events(peer
))
1081 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1082 peer
->host
, remote_as
, peer
->as
);
1083 bgp_notify_send_with_data (peer
,
1084 BGP_NOTIFY_OPEN_ERR
,
1085 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1086 notify_data_remote_as
, 2);
1090 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1091 calculate the value of the Hold Timer by using the smaller of its
1092 configured Hold Time and the Hold Time received in the OPEN message.
1093 The Hold Time MUST be either zero or at least three seconds. An
1094 implementation may reject connections on the basis of the Hold Time. */
1096 if (holdtime
< 3 && holdtime
!= 0)
1098 bgp_notify_send_with_data (peer
,
1099 BGP_NOTIFY_OPEN_ERR
,
1100 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1101 (u_char
*)holdtime_ptr
, 2);
1105 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1106 would be one third of the Hold Time interval. KEEPALIVE messages
1107 MUST NOT be sent more frequently than one per second. An
1108 implementation MAY adjust the rate at which it sends KEEPALIVE
1109 messages as a function of the Hold Time interval. */
1111 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1112 send_holdtime
= peer
->holdtime
;
1114 send_holdtime
= peer
->bgp
->default_holdtime
;
1116 if (holdtime
< send_holdtime
)
1117 peer
->v_holdtime
= holdtime
;
1119 peer
->v_holdtime
= send_holdtime
;
1121 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1122 peer
->v_keepalive
= peer
->keepalive
;
1124 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1126 /* Open option part parse. */
1129 if ((ret
= bgp_open_option_parse (peer
, optlen
, &mp_capability
)) < 0)
1134 if (bgp_debug_neighbor_events(peer
))
1135 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
1140 * Assume that the peer supports the locally configured set of
1141 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1142 * capabilities, or if 'override-capability' is configured.
1144 if (! mp_capability
||
1145 CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
1147 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] = peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1148 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1149 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] = peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1150 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1153 /* When collision is detected and this peer is closed. Retrun
1155 ret
= bgp_collision_detect (peer
, remote_id
);
1160 if ((ret
= bgp_getsockname (peer
)) < 0)
1162 zlog_err("%s: bgp_getsockname() failed for peer: %s", __FUNCTION__
,
1167 /* Verify valid local address present based on negotiated address-families. */
1168 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] ||
1169 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] ||
1170 peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
] ||
1171 peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
])
1173 if (!peer
->nexthop
.v4
.s_addr
)
1175 #if defined (HAVE_CUMULUS)
1176 zlog_err ("%s: No local IPv4 addr resetting connection, fd %d",
1177 peer
->host
, peer
->fd
);
1178 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1183 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] ||
1184 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] ||
1185 peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
] ||
1186 peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
])
1188 if (IN6_IS_ADDR_UNSPECIFIED (&peer
->nexthop
.v6_global
))
1190 #if defined (HAVE_CUMULUS)
1191 zlog_err ("%s: No local IPv6 addr resetting connection, fd %d",
1192 peer
->host
, peer
->fd
);
1193 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1198 peer
->rtt
= sockopt_tcp_rtt (peer
->fd
);
1200 if ((ret
= bgp_event_update(peer
, Receive_OPEN_message
)) < 0)
1202 zlog_err("%s: BGP event update failed for peer: %s", __FUNCTION__
,
1204 /* DD: bgp send notify and reset state */
1208 peer
->packet_size
= 0;
1210 stream_reset (peer
->ibuf
);
1215 /* Called when there is a change in the EOR(implicit or explicit) status of a peer.
1216 Ends the update-delay if all expected peers are done with EORs. */
1218 bgp_check_update_delay(struct bgp
*bgp
)
1220 struct listnode
*node
, *nnode
;
1221 struct peer
*peer
= NULL
;
1223 if (bgp_debug_neighbor_events(peer
))
1224 zlog_debug ("Checking update delay, T: %d R: %d I:%d E: %d", bgp
->established
,
1225 bgp
->restarted_peers
, bgp
->implicit_eors
, bgp
->explicit_eors
);
1227 if (bgp
->established
<=
1228 bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
)
1230 /* This is an extra sanity check to make sure we wait for all the
1231 eligible configured peers. This check is performed if establish wait
1232 timer is on, or establish wait option is not given with the
1233 update-delay command */
1234 if (bgp
->t_establish_wait
||
1235 (bgp
->v_establish_wait
== bgp
->v_update_delay
))
1236 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
1238 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1239 && !CHECK_FLAG (peer
->flags
, PEER_FLAG_SHUTDOWN
)
1240 && !peer
->update_delay_over
)
1242 if (bgp_debug_neighbor_events(peer
))
1243 zlog_debug (" Peer %s pending, continuing read-only mode",
1249 zlog_info ("Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
1250 bgp
->restarted_peers
, bgp
->implicit_eors
, bgp
->explicit_eors
);
1251 bgp_update_delay_end(bgp
);
1255 /* Called if peer is known to have restarted. The restart-state bit in
1256 Graceful-Restart capability is used for that */
1258 bgp_update_restarted_peers (struct peer
*peer
)
1260 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1261 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1263 if (bgp_debug_neighbor_events(peer
))
1264 zlog_debug ("Peer %s: Checking restarted", peer
->host
);
1266 if (peer
->status
== Established
)
1268 peer
->update_delay_over
= 1;
1269 peer
->bgp
->restarted_peers
++;
1270 bgp_check_update_delay(peer
->bgp
);
1274 /* Called as peer receives a keep-alive. Determines if this occurence can be
1275 taken as an implicit EOR for this peer.
1276 NOTE: The very first keep-alive after the Established state of a peer is
1277 considered implicit EOR for the update-delay purposes */
1279 bgp_update_implicit_eors (struct peer
*peer
)
1281 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1282 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1284 if (bgp_debug_neighbor_events(peer
))
1285 zlog_debug ("Peer %s: Checking implicit EORs", peer
->host
);
1287 if (peer
->status
== Established
)
1289 peer
->update_delay_over
= 1;
1290 peer
->bgp
->implicit_eors
++;
1291 bgp_check_update_delay(peer
->bgp
);
1295 /* Should be called only when there is a change in the EOR_RECEIVED status
1296 for any afi/safi on a peer */
1298 bgp_update_explicit_eors (struct peer
*peer
)
1303 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1304 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1306 if (bgp_debug_neighbor_events(peer
))
1307 zlog_debug ("Peer %s: Checking explicit EORs", peer
->host
);
1309 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1310 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1312 if (peer
->afc_nego
[afi
][safi
] &&
1313 !CHECK_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_RECEIVED
))
1315 if (bgp_debug_neighbor_events(peer
))
1316 zlog_debug (" afi %d safi %d didnt receive EOR", afi
, safi
);
1321 peer
->update_delay_over
= 1;
1322 peer
->bgp
->explicit_eors
++;
1323 bgp_check_update_delay(peer
->bgp
);
1326 /* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers */
1328 bgp_nlri_parse (struct peer
*peer
, struct attr
*attr
, struct bgp_nlri
*packet
)
1330 switch (packet
->safi
)
1333 case SAFI_MULTICAST
:
1334 return bgp_nlri_parse_ip (peer
, attr
, packet
);
1336 case SAFI_MPLS_LABELED_VPN
:
1337 return bgp_nlri_parse_vpn (peer
, attr
, packet
);
1339 return bgp_nlri_parse_encap (peer
, attr
, packet
);
1344 /* Parse BGP Update packet and make attribute object. */
1346 bgp_update_receive (struct peer
*peer
, bgp_size_t size
)
1352 struct attr_extra extra
;
1353 bgp_size_t attribute_len
;
1354 bgp_size_t update_len
;
1355 bgp_size_t withdraw_len
;
1364 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1366 /* Status must be Established. */
1367 if (peer
->status
!= Established
)
1369 zlog_err ("%s [FSM] Update packet received under status %s",
1370 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1371 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1375 /* Set initial values. */
1376 memset (&attr
, 0, sizeof (struct attr
));
1377 memset (&extra
, 0, sizeof (struct attr_extra
));
1378 memset (&nlris
, 0, sizeof (nlris
));
1379 attr
.extra
= &extra
;
1380 memset (peer
->rcvd_attr_str
, 0, BUFSIZ
);
1381 peer
->rcvd_attr_printed
= 0;
1384 end
= stream_pnt (s
) + size
;
1386 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1387 Length is too large (i.e., if Unfeasible Routes Length + Total
1388 Attribute Length + 23 exceeds the message Length), then the Error
1389 Subcode is set to Malformed Attribute List. */
1390 if (stream_pnt (s
) + 2 > end
)
1392 zlog_err ("%s [Error] Update packet error"
1393 " (packet length is short for unfeasible length)",
1395 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1396 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1400 /* Unfeasible Route Length. */
1401 withdraw_len
= stream_getw (s
);
1403 /* Unfeasible Route Length check. */
1404 if (stream_pnt (s
) + withdraw_len
> end
)
1406 zlog_err ("%s [Error] Update packet error"
1407 " (packet unfeasible length overflow %d)",
1408 peer
->host
, withdraw_len
);
1409 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1410 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1414 /* Unfeasible Route packet format check. */
1415 if (withdraw_len
> 0)
1417 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1418 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1419 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt (s
);
1420 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1421 stream_forward_getp (s
, withdraw_len
);
1424 /* Attribute total length check. */
1425 if (stream_pnt (s
) + 2 > end
)
1427 zlog_warn ("%s [Error] Packet Error"
1428 " (update packet is short for attribute length)",
1430 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1431 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1435 /* Fetch attribute total length. */
1436 attribute_len
= stream_getw (s
);
1438 /* Attribute length check. */
1439 if (stream_pnt (s
) + attribute_len
> end
)
1441 zlog_warn ("%s [Error] Packet Error"
1442 " (update packet attribute length overflow %d)",
1443 peer
->host
, attribute_len
);
1444 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1445 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1449 /* Certain attribute parsing errors should not be considered bad enough
1450 * to reset the session for, most particularly any partial/optional
1451 * attributes that have 'tunneled' over speakers that don't understand
1452 * them. Instead we withdraw only the prefix concerned.
1454 * Complicates the flow a little though..
1456 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1457 /* This define morphs the update case into a withdraw when lower levels
1458 * have signalled an error condition where this is best.
1460 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1462 /* Parse attribute when it exists. */
1465 attr_parse_ret
= bgp_attr_parse (peer
, &attr
, attribute_len
,
1466 &nlris
[NLRI_MP_UPDATE
], &nlris
[NLRI_MP_WITHDRAW
]);
1467 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
)
1469 bgp_attr_unintern_sub (&attr
);
1474 /* Logging the attribute. */
1475 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
||
1476 BGP_DEBUG (update
, UPDATE_IN
) ||
1477 BGP_DEBUG (update
, UPDATE_PREFIX
))
1479 ret
= bgp_dump_attr (peer
, &attr
, peer
->rcvd_attr_str
, BUFSIZ
);
1481 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1482 zlog_err ("%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1485 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1))
1487 zlog_debug ("%s rcvd UPDATE w/ attr: %s", peer
->host
, peer
->rcvd_attr_str
);
1488 peer
->rcvd_attr_printed
= 1;
1492 /* Network Layer Reachability Information. */
1493 update_len
= end
- stream_pnt (s
);
1497 /* Set NLRI portion to structure. */
1498 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1499 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1500 nlris
[NLRI_UPDATE
].nlri
= stream_pnt (s
);
1501 nlris
[NLRI_UPDATE
].length
= update_len
;
1502 stream_forward_getp (s
, update_len
);
1505 if (BGP_DEBUG (update
, UPDATE_IN
))
1506 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1507 peer
->host
, withdraw_len
, attribute_len
, update_len
);
1509 /* Parse any given NLRIs */
1510 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++)
1515 /* We use afi and safi as indices into tables and what not. It would
1516 * be impossible, at this time, to support unknown afi/safis. And
1517 * anyway, the peer needs to be configured to enable the afi/safi
1518 * explicitly which requires UI support.
1520 * Ignore unknown afi/safi NLRIs.
1522 * Note: This means nlri[x].afi/safi still can not be trusted for
1523 * indexing later in this function!
1525 * Note2: This will also remap the wire code-point for VPN safi to the
1526 * internal safi_t point, as needs be.
1528 if(!bgp_afi_safi_valid_indices (nlris
[i
].afi
, &nlris
[i
].safi
))
1530 zlog_info ("%s [Info] UPDATE with unsupported AFI/SAFI %u/%u",
1531 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1535 /* NLRI is processed iff the peer if configured for the specific afi/safi */
1536 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
])
1538 zlog_info ("%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1539 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1543 /* EoR handled later */
1544 if (nlris
[i
].length
== 0)
1550 case NLRI_MP_UPDATE
:
1551 nlri_ret
= bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &nlris
[i
]);
1554 case NLRI_MP_WITHDRAW
:
1555 nlri_ret
= bgp_nlri_parse (peer
, NULL
, &nlris
[i
]);
1563 zlog_err("%s [Error] Error parsing NLRI", peer
->host
);
1564 if (peer
->status
== Established
)
1565 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1566 i
<= NLRI_WITHDRAW
? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1567 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1568 bgp_attr_unintern_sub (&attr
);
1575 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1576 * and MP EoR should have only an empty MP_UNREACH
1578 if (!update_len
&& !withdraw_len
1579 && nlris
[NLRI_MP_UPDATE
].length
== 0)
1584 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already checked
1585 * update and withdraw NLRI lengths are 0.
1590 safi
= SAFI_UNICAST
;
1592 else if (attr
.flag
& ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI
)
1593 && nlris
[NLRI_MP_WITHDRAW
].length
== 0
1594 && bgp_afi_safi_valid_indices (nlris
[NLRI_MP_WITHDRAW
].afi
,
1595 &nlris
[NLRI_MP_WITHDRAW
].safi
))
1597 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1598 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1601 if (afi
&& peer
->afc
[afi
][safi
])
1603 /* End-of-RIB received */
1604 if (!CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_RECEIVED
))
1606 SET_FLAG (peer
->af_sflags
[afi
][safi
],
1607 PEER_STATUS_EOR_RECEIVED
);
1608 bgp_update_explicit_eors(peer
);
1611 /* NSF delete stale route */
1612 if (peer
->nsf
[afi
][safi
])
1613 bgp_clear_stale_route (peer
, afi
, safi
);
1615 if (bgp_debug_neighbor_events(peer
))
1617 zlog_debug ("rcvd End-of-RIB for %s from %s",
1618 afi_safi_print (afi
, safi
), peer
->host
);
1623 /* Everything is done. We unintern temporary structures which
1624 interned in bgp_attr_parse(). */
1625 bgp_attr_unintern_sub (&attr
);
1627 /* If peering is stopped due to some reason, do not generate BGP
1629 if (peer
->status
!= Established
)
1632 /* Increment packet counter. */
1634 peer
->update_time
= bgp_clock ();
1636 /* Rearm holdtime timer */
1637 BGP_TIMER_OFF (peer
->t_holdtime
);
1638 bgp_timer_set (peer
);
1643 /* Notify message treatment function. */
1645 bgp_notify_receive (struct peer
*peer
, bgp_size_t size
)
1647 struct bgp_notify bgp_notify
;
1649 if (peer
->notify
.data
)
1651 XFREE (MTYPE_TMP
, peer
->notify
.data
);
1652 peer
->notify
.data
= NULL
;
1653 peer
->notify
.length
= 0;
1656 bgp_notify
.code
= stream_getc (peer
->ibuf
);
1657 bgp_notify
.subcode
= stream_getc (peer
->ibuf
);
1658 bgp_notify
.length
= size
- 2;
1659 bgp_notify
.data
= NULL
;
1661 /* Preserv notify code and sub code. */
1662 peer
->notify
.code
= bgp_notify
.code
;
1663 peer
->notify
.subcode
= bgp_notify
.subcode
;
1664 /* For further diagnostic record returned Data. */
1665 if (bgp_notify
.length
)
1667 peer
->notify
.length
= size
- 2;
1668 peer
->notify
.data
= XMALLOC (MTYPE_TMP
, size
- 2);
1669 memcpy (peer
->notify
.data
, stream_pnt (peer
->ibuf
), size
- 2);
1678 if (bgp_notify
.length
)
1680 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
1681 for (i
= 0; i
< bgp_notify
.length
; i
++)
1684 sprintf (c
, " %02x", stream_getc (peer
->ibuf
));
1685 strcat (bgp_notify
.data
, c
);
1690 sprintf (c
, "%02x", stream_getc (peer
->ibuf
));
1691 strcpy (bgp_notify
.data
, c
);
1695 bgp_notify_print(peer
, &bgp_notify
, "received");
1696 if (bgp_notify
.data
)
1698 XFREE (MTYPE_TMP
, bgp_notify
.data
);
1699 bgp_notify
.data
= NULL
;
1700 bgp_notify
.length
= 0;
1704 /* peer count update */
1707 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1709 /* We have to check for Notify with Unsupported Optional Parameter.
1710 in that case we fallback to open without the capability option.
1711 But this done in bgp_stop. We just mark it here to avoid changing
1713 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1714 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1715 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1717 BGP_EVENT_ADD (peer
, Receive_NOTIFICATION_message
);
1720 /* Keepalive treatment function -- get keepalive send keepalive */
1722 bgp_keepalive_receive (struct peer
*peer
, bgp_size_t size
)
1724 if (bgp_debug_keepalive(peer
))
1725 zlog_debug ("%s KEEPALIVE rcvd", peer
->host
);
1727 BGP_EVENT_ADD (peer
, Receive_KEEPALIVE_message
);
1730 /* Route refresh message is received. */
1732 bgp_route_refresh_receive (struct peer
*peer
, bgp_size_t size
)
1737 struct peer_af
*paf
;
1738 struct update_group
*updgrp
;
1739 struct peer
*updgrp_peer
;
1741 /* If peer does not have the capability, send notification. */
1742 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_ADV
))
1744 zlog_err ("%s [Error] BGP route refresh is not enabled",
1746 bgp_notify_send (peer
,
1747 BGP_NOTIFY_HEADER_ERR
,
1748 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1752 /* Status must be Established. */
1753 if (peer
->status
!= Established
)
1755 zlog_err ("%s [Error] Route refresh packet received under status %s",
1756 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1757 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1764 afi
= stream_getw (s
);
1765 (void)stream_getc (s
);
1766 safi
= stream_getc (s
);
1768 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1769 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1770 peer
->host
, afi
, safi
);
1772 /* Check AFI and SAFI. */
1773 if ((afi
!= AFI_IP
&& afi
!= AFI_IP6
)
1774 || (safi
!= SAFI_UNICAST
&& safi
!= SAFI_MULTICAST
1775 && safi
!= SAFI_MPLS_LABELED_VPN
))
1777 zlog_info ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1778 peer
->host
, afi
, safi
);
1782 /* Adjust safi code. */
1783 if (safi
== SAFI_MPLS_LABELED_VPN
)
1784 safi
= SAFI_MPLS_VPN
;
1786 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1789 u_char when_to_refresh
;
1793 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) < 5)
1795 zlog_info ("%s ORF route refresh length error", peer
->host
);
1796 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1800 when_to_refresh
= stream_getc (s
);
1801 end
= stream_pnt (s
) + (size
- 5);
1803 while ((stream_pnt (s
) + 2) < end
)
1805 orf_type
= stream_getc (s
);
1806 orf_len
= stream_getw (s
);
1808 /* orf_len in bounds? */
1809 if ((stream_pnt (s
) + orf_len
) > end
)
1810 break; /* XXX: Notify instead?? */
1811 if (orf_type
== ORF_TYPE_PREFIX
1812 || orf_type
== ORF_TYPE_PREFIX_OLD
)
1814 uint8_t *p_pnt
= stream_pnt (s
);
1815 uint8_t *p_end
= stream_pnt (s
) + orf_len
;
1816 struct orf_prefix orfp
;
1821 int ret
= CMD_SUCCESS
;
1823 if (bgp_debug_neighbor_events(peer
))
1825 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
1826 peer
->host
, orf_type
, orf_len
);
1829 /* we're going to read at least 1 byte of common ORF header,
1830 * and 7 bytes of ORF Address-filter entry from the stream
1835 /* ORF prefix-list name */
1836 sprintf (name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1838 while (p_pnt
< p_end
)
1840 /* If the ORF entry is malformed, want to read as much of it
1841 * as possible without going beyond the bounds of the entry,
1842 * to maximise debug information.
1845 memset (&orfp
, 0, sizeof (struct orf_prefix
));
1847 /* after ++: p_pnt <= p_end */
1848 if (common
& ORF_COMMON_PART_REMOVE_ALL
)
1850 if (bgp_debug_neighbor_events(peer
))
1851 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer
->host
);
1852 prefix_bgp_orf_remove_all (afi
, name
);
1855 ok
= ((u_int32_t
)(p_end
- p_pnt
) >= sizeof(u_int32_t
)) ;
1858 memcpy (&seq
, p_pnt
, sizeof (u_int32_t
));
1859 p_pnt
+= sizeof (u_int32_t
);
1860 orfp
.seq
= ntohl (seq
);
1865 if ((ok
= (p_pnt
< p_end
)))
1866 orfp
.ge
= *p_pnt
++ ; /* value checked in prefix_bgp_orf_set() */
1867 if ((ok
= (p_pnt
< p_end
)))
1868 orfp
.le
= *p_pnt
++ ; /* value checked in prefix_bgp_orf_set() */
1869 if ((ok
= (p_pnt
< p_end
)))
1870 orfp
.p
.prefixlen
= *p_pnt
++ ;
1871 orfp
.p
.family
= afi2family (afi
); /* afi checked already */
1873 psize
= PSIZE (orfp
.p
.prefixlen
); /* 0 if not ok */
1874 if (psize
> prefix_blen(&orfp
.p
)) /* valid for family ? */
1877 psize
= prefix_blen(&orfp
.p
) ;
1879 if (psize
> (p_end
- p_pnt
)) /* valid for packet ? */
1882 psize
= p_end
- p_pnt
;
1886 memcpy (&orfp
.p
.u
.prefix
, p_pnt
, psize
);
1889 if (bgp_debug_neighbor_events(peer
))
1891 char buf
[INET6_BUFSIZ
];
1893 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
1895 (common
& ORF_COMMON_PART_REMOVE
? "Remove" : "Add"),
1896 (common
& ORF_COMMON_PART_DENY
? "deny" : "permit"),
1898 inet_ntop (orfp
.p
.family
, &orfp
.p
.u
.prefix
, buf
, INET6_BUFSIZ
),
1899 orfp
.p
.prefixlen
, orfp
.ge
, orfp
.le
,
1900 ok
? "" : " MALFORMED");
1904 ret
= prefix_bgp_orf_set (name
, afi
, &orfp
,
1905 (common
& ORF_COMMON_PART_DENY
? 0 : 1 ),
1906 (common
& ORF_COMMON_PART_REMOVE
? 0 : 1));
1908 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
))
1910 zlog_info ("%s Received misformatted prefixlist ORF."
1911 " Remove All pfxlist", peer
->host
);
1912 prefix_bgp_orf_remove_all (afi
, name
);
1917 peer
->orf_plist
[afi
][safi
] = prefix_bgp_orf_lookup (afi
, name
);
1919 stream_forward_getp (s
, orf_len
);
1921 if (bgp_debug_neighbor_events(peer
))
1922 zlog_debug ("%s rcvd Refresh %s ORF request", peer
->host
,
1923 when_to_refresh
== REFRESH_DEFER
? "Defer" : "Immediate");
1924 if (when_to_refresh
== REFRESH_DEFER
)
1928 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1929 if (CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
))
1930 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
1932 paf
= peer_af_find (peer
, afi
, safi
);
1933 if (paf
&& paf
->subgroup
)
1935 if (peer
->orf_plist
[afi
][safi
])
1937 updgrp
= PAF_UPDGRP(paf
);
1938 updgrp_peer
= UPDGRP_PEER(updgrp
);
1939 updgrp_peer
->orf_plist
[afi
][safi
] = peer
->orf_plist
[afi
][safi
];
1942 /* If the peer is configured for default-originate clear the
1943 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will re-advertise the
1946 if (CHECK_FLAG (paf
->subgroup
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1947 UNSET_FLAG (paf
->subgroup
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1950 /* Perform route refreshment to the peer */
1951 bgp_announce_route (peer
, afi
, safi
);
1955 bgp_capability_msg_parse (struct peer
*peer
, u_char
*pnt
, bgp_size_t length
)
1958 struct capability_mp_data mpc
;
1959 struct capability_header
*hdr
;
1968 /* We need at least action, capability code and capability length. */
1971 zlog_info ("%s Capability length error", peer
->host
);
1972 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1976 hdr
= (struct capability_header
*)(pnt
+ 1);
1978 /* Action value check. */
1979 if (action
!= CAPABILITY_ACTION_SET
1980 && action
!= CAPABILITY_ACTION_UNSET
)
1982 zlog_info ("%s Capability Action Value error %d",
1983 peer
->host
, action
);
1984 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1988 if (bgp_debug_neighbor_events(peer
))
1989 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
1990 peer
->host
, action
, hdr
->code
, hdr
->length
);
1992 /* Capability length check. */
1993 if ((pnt
+ hdr
->length
+ 3) > end
)
1995 zlog_info ("%s Capability length error", peer
->host
);
1996 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2000 /* Fetch structure to the byte stream. */
2001 memcpy (&mpc
, pnt
+ 3, sizeof (struct capability_mp_data
));
2003 /* We know MP Capability Code. */
2004 if (hdr
->code
== CAPABILITY_CODE_MP
)
2006 afi
= ntohs (mpc
.afi
);
2009 /* Ignore capability when override-capability is set. */
2010 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
2013 if (!bgp_afi_safi_valid_indices (afi
, &safi
))
2015 if (bgp_debug_neighbor_events(peer
))
2016 zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
2017 "(%u/%u)", peer
->host
, afi
, safi
);
2021 /* Address family check. */
2022 if (bgp_debug_neighbor_events(peer
))
2023 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2025 action
== CAPABILITY_ACTION_SET
2026 ? "Advertising" : "Removing",
2027 ntohs(mpc
.afi
) , mpc
.safi
);
2029 if (action
== CAPABILITY_ACTION_SET
)
2031 peer
->afc_recv
[afi
][safi
] = 1;
2032 if (peer
->afc
[afi
][safi
])
2034 peer
->afc_nego
[afi
][safi
] = 1;
2035 bgp_announce_route (peer
, afi
, safi
);
2040 peer
->afc_recv
[afi
][safi
] = 0;
2041 peer
->afc_nego
[afi
][safi
] = 0;
2043 if (peer_active_nego (peer
))
2044 bgp_clear_route (peer
, afi
, safi
);
2046 BGP_EVENT_ADD (peer
, BGP_Stop
);
2051 zlog_warn ("%s unrecognized capability code: %d - ignored",
2052 peer
->host
, hdr
->code
);
2054 pnt
+= hdr
->length
+ 3;
2059 /* Dynamic Capability is received.
2061 * This is exported for unit-test purposes
2064 bgp_capability_receive (struct peer
*peer
, bgp_size_t size
)
2068 /* Fetch pointer. */
2069 pnt
= stream_pnt (peer
->ibuf
);
2071 if (bgp_debug_neighbor_events(peer
))
2072 zlog_debug ("%s rcv CAPABILITY", peer
->host
);
2074 /* If peer does not have the capability, send notification. */
2075 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_ADV
))
2077 zlog_err ("%s [Error] BGP dynamic capability is not enabled",
2079 bgp_notify_send (peer
,
2080 BGP_NOTIFY_HEADER_ERR
,
2081 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2085 /* Status must be Established. */
2086 if (peer
->status
!= Established
)
2088 zlog_err ("%s [Error] Dynamic capability packet received under status %s",
2089 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
2090 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
2095 return bgp_capability_msg_parse (peer
, pnt
, size
);
2098 /* BGP read utility function. */
2100 bgp_read_packet (struct peer
*peer
)
2105 readsize
= peer
->packet_size
- stream_get_endp (peer
->ibuf
);
2107 /* If size is zero then return. */
2111 /* Read packet from fd. */
2112 nbytes
= stream_read_try (peer
->ibuf
, peer
->fd
, readsize
);
2114 /* If read byte is smaller than zero then error occured. */
2117 /* Transient error should retry */
2121 zlog_err ("%s [Error] bgp_read_packet error: %s",
2122 peer
->host
, safe_strerror (errno
));
2124 if (peer
->status
== Established
)
2126 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2128 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2129 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2132 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2135 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
2139 /* When read byte is zero : clear bgp peer and return */
2142 if (bgp_debug_neighbor_events(peer
))
2143 zlog_debug ("%s [Event] BGP connection closed fd %d",
2144 peer
->host
, peer
->fd
);
2146 if (peer
->status
== Established
)
2148 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2150 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2151 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2154 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2157 BGP_EVENT_ADD (peer
, TCP_connection_closed
);
2161 /* We read partial packet. */
2162 if (stream_get_endp (peer
->ibuf
) != peer
->packet_size
)
2170 bgp_marker_all_one (struct stream
*s
, int length
)
2174 for (i
= 0; i
< length
; i
++)
2175 if (s
->data
[i
] != 0xff)
2181 /* Recent thread time.
2182 On same clock base as bgp_clock (MONOTONIC)
2183 but can be time of last context switch to bgp_read thread. */
2185 bgp_recent_clock (void)
2187 return recent_relative_time().tv_sec
;
2190 /* Starting point of packet process function. */
2192 bgp_read (struct thread
*thread
)
2198 char notify_data_length
[2];
2199 u_int32_t notify_out
;
2201 /* Yes first of all get peer pointer. */
2202 peer
= THREAD_ARG (thread
);
2203 peer
->t_read
= NULL
;
2205 /* Note notify_out so we can check later to see if we sent another one */
2206 notify_out
= peer
->notify_out
;
2208 /* For non-blocking IO check. */
2209 if (peer
->status
== Connect
)
2211 bgp_connect_check (peer
, 1);
2218 zlog_err ("bgp_read peer's fd is negative value %d", peer
->fd
);
2221 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
2224 /* Read packet header to determine type of the packet */
2225 if (peer
->packet_size
== 0)
2226 peer
->packet_size
= BGP_HEADER_SIZE
;
2228 if (stream_get_endp (peer
->ibuf
) < BGP_HEADER_SIZE
)
2230 ret
= bgp_read_packet (peer
);
2232 /* Header read error or partial read packet. */
2236 /* Get size and type. */
2237 stream_forward_getp (peer
->ibuf
, BGP_MARKER_SIZE
);
2238 memcpy (notify_data_length
, stream_pnt (peer
->ibuf
), 2);
2239 size
= stream_getw (peer
->ibuf
);
2240 type
= stream_getc (peer
->ibuf
);
2243 if (((type
== BGP_MSG_OPEN
) || (type
== BGP_MSG_KEEPALIVE
))
2244 && ! bgp_marker_all_one (peer
->ibuf
, BGP_MARKER_SIZE
))
2246 bgp_notify_send (peer
,
2247 BGP_NOTIFY_HEADER_ERR
,
2248 BGP_NOTIFY_HEADER_NOT_SYNC
);
2252 /* BGP type check. */
2253 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
2254 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
2255 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
2256 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
2257 && type
!= BGP_MSG_CAPABILITY
)
2259 if (bgp_debug_neighbor_events(peer
))
2260 zlog_debug ("%s unknown message type 0x%02x",
2262 bgp_notify_send_with_data (peer
,
2263 BGP_NOTIFY_HEADER_ERR
,
2264 BGP_NOTIFY_HEADER_BAD_MESTYPE
,
2268 /* Mimimum packet length check. */
2269 if ((size
< BGP_HEADER_SIZE
)
2270 || (size
> BGP_MAX_PACKET_SIZE
)
2271 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
2272 || (type
== BGP_MSG_UPDATE
&& size
< BGP_MSG_UPDATE_MIN_SIZE
)
2273 || (type
== BGP_MSG_NOTIFY
&& size
< BGP_MSG_NOTIFY_MIN_SIZE
)
2274 || (type
== BGP_MSG_KEEPALIVE
&& size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
2275 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2276 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2277 || (type
== BGP_MSG_CAPABILITY
&& size
< BGP_MSG_CAPABILITY_MIN_SIZE
))
2279 if (bgp_debug_neighbor_events(peer
))
2280 zlog_debug ("%s bad message length - %d for %s",
2282 type
== 128 ? "ROUTE-REFRESH" :
2283 bgp_type_str
[(int) type
]);
2284 bgp_notify_send_with_data (peer
,
2285 BGP_NOTIFY_HEADER_ERR
,
2286 BGP_NOTIFY_HEADER_BAD_MESLEN
,
2287 (u_char
*) notify_data_length
, 2);
2291 /* Adjust size to message length. */
2292 peer
->packet_size
= size
;
2295 ret
= bgp_read_packet (peer
);
2299 /* Get size and type again. */
2300 (void)stream_getw_from (peer
->ibuf
, BGP_MARKER_SIZE
);
2301 type
= stream_getc_from (peer
->ibuf
, BGP_MARKER_SIZE
+ 2);
2303 /* BGP packet dump function. */
2304 bgp_dump_packet (peer
, type
, peer
->ibuf
);
2306 size
= (peer
->packet_size
- BGP_HEADER_SIZE
);
2308 /* Read rest of the packet and call each sort of packet routine */
2313 bgp_open_receive (peer
, size
); /* XXX return value ignored! */
2315 case BGP_MSG_UPDATE
:
2316 peer
->readtime
= bgp_recent_clock ();
2317 bgp_update_receive (peer
, size
);
2319 case BGP_MSG_NOTIFY
:
2320 bgp_notify_receive (peer
, size
);
2322 case BGP_MSG_KEEPALIVE
:
2323 peer
->readtime
= bgp_recent_clock ();
2324 bgp_keepalive_receive (peer
, size
);
2326 case BGP_MSG_ROUTE_REFRESH_NEW
:
2327 case BGP_MSG_ROUTE_REFRESH_OLD
:
2329 bgp_route_refresh_receive (peer
, size
);
2331 case BGP_MSG_CAPABILITY
:
2332 peer
->dynamic_cap_in
++;
2333 bgp_capability_receive (peer
, size
);
2337 /* If reading this packet caused us to send a NOTIFICATION then store a copy
2338 * of the packet for troubleshooting purposes
2340 if (notify_out
< peer
->notify_out
)
2342 memcpy(peer
->last_reset_cause
, peer
->ibuf
->data
, peer
->packet_size
);
2343 peer
->last_reset_cause_size
= peer
->packet_size
;
2344 notify_out
= peer
->notify_out
;
2347 /* Clear input buffer. */
2348 peer
->packet_size
= 0;
2350 stream_reset (peer
->ibuf
);
2353 /* If reading this packet caused us to send a NOTIFICATION then store a copy
2354 * of the packet for troubleshooting purposes
2356 if (notify_out
< peer
->notify_out
)
2358 memcpy(peer
->last_reset_cause
, peer
->ibuf
->data
, peer
->packet_size
);
2359 peer
->last_reset_cause_size
= peer
->packet_size
;