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_encap.h"
54 #include "bgpd/bgp_evpn.h"
55 #include "bgpd/bgp_advertise.h"
56 #include "bgpd/bgp_vty.h"
57 #include "bgpd/bgp_updgrp.h"
59 /* Set up BGP packet marker and packet type. */
61 bgp_packet_set_marker (struct stream
*s
, u_char type
)
66 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
67 stream_putc (s
, 0xff);
69 /* Dummy total length. This field is should be filled in later on. */
72 /* BGP packet type. */
73 stream_putc (s
, type
);
75 /* Return current stream size. */
76 return stream_get_endp (s
);
79 /* Set BGP packet header size entry. If size is zero then use current
82 bgp_packet_set_size (struct stream
*s
)
86 /* Preserve current pointer. */
87 cp
= stream_get_endp (s
);
88 stream_putw_at (s
, BGP_MARKER_SIZE
, cp
);
93 /* Add new packet to the peer. */
95 bgp_packet_add (struct peer
*peer
, struct stream
*s
)
97 /* Add packet to the end of list. */
98 stream_fifo_push (peer
->obuf
, s
);
101 /* Free first packet. */
103 bgp_packet_delete (struct peer
*peer
)
105 stream_free (stream_fifo_pop (peer
->obuf
));
108 /* Check file descriptor whether connect is established. */
110 bgp_connect_check (struct peer
*peer
, int change_state
)
116 /* Anyway I have to reset read and write thread. */
117 BGP_READ_OFF (peer
->t_read
);
118 BGP_WRITE_OFF (peer
->t_write
);
120 /* Check file descriptor. */
121 slen
= sizeof (status
);
122 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &status
, &slen
);
124 /* If getsockopt is fail, this is fatal error. */
127 zlog_info ("can't get sockopt for nonblocking connect");
128 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
132 /* When status is 0 then TCP connection is established. */
135 BGP_EVENT_ADD (peer
, TCP_connection_open
);
140 if (bgp_debug_neighbor_events(peer
))
141 zlog_debug ("%s [Event] Connect failed (%s)",
142 peer
->host
, safe_strerror (errno
));
144 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
149 static struct stream
*
150 bgp_update_packet_eor (struct peer
*peer
, afi_t afi
, safi_t safi
)
156 if (DISABLE_BGP_ANNOUNCE
)
159 if (bgp_debug_neighbor_events(peer
))
160 zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi
, safi
), peer
->host
);
162 s
= stream_new (BGP_MAX_PACKET_SIZE
);
164 /* Make BGP update packet. */
165 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
167 /* Unfeasible Routes Length */
170 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
172 /* Total Path Attribute Length */
177 /* Convert AFI, SAFI to values for packet. */
178 bgp_map_afi_safi_int2iana (afi
, safi
, &pkt_afi
, &pkt_safi
);
180 /* Total Path Attribute Length */
182 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
183 stream_putc (s
, BGP_ATTR_MP_UNREACH_NLRI
);
185 stream_putw (s
, pkt_afi
);
186 stream_putc (s
, pkt_safi
);
189 bgp_packet_set_size (s
);
190 bgp_packet_add (peer
, s
);
194 /* Get next packet to be written. */
195 static struct stream
*
196 bgp_write_packet (struct peer
*peer
)
198 struct stream
*s
= NULL
;
200 struct bpacket
*next_pkt
;
204 s
= stream_fifo_head (peer
->obuf
);
209 * The code beyond this part deals with update packets, proceed only
210 * if peer is Established and updates are not on hold (as part of
211 * update-delay post processing).
213 if (peer
->status
!= Established
)
216 if (peer
->bgp
&& peer
->bgp
->main_peers_update_hold
)
219 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
220 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
222 paf
= peer_af_find (peer
, afi
, safi
);
223 if (!paf
|| !PAF_SUBGRP(paf
))
225 next_pkt
= paf
->next_pkt_to_send
;
227 /* Try to generate a packet for the peer if we are at the end of
228 * the list. Always try to push out WITHDRAWs first. */
229 if (!next_pkt
|| !next_pkt
->buffer
)
231 next_pkt
= subgroup_withdraw_packet(PAF_SUBGRP(paf
));
232 if (!next_pkt
|| !next_pkt
->buffer
)
233 subgroup_update_packet (PAF_SUBGRP(paf
));
234 next_pkt
= paf
->next_pkt_to_send
;
237 /* If we still don't have a packet to send to the peer, then
238 * try to find out out if we have to send eor or if not, skip to
239 * the next AFI, SAFI.
240 * Don't send the EOR prematurely... if the subgroup's coalesce
241 * timer is running, the adjacency-out structure is not created
244 if (!next_pkt
|| !next_pkt
->buffer
)
246 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
248 if (!(PAF_SUBGRP(paf
))->t_coalesce
&&
249 peer
->afc_nego
[afi
][safi
] && peer
->synctime
250 && ! CHECK_FLAG (peer
->af_sflags
[afi
][safi
],
251 PEER_STATUS_EOR_SEND
)
252 && safi
!= SAFI_EVPN
)
254 SET_FLAG (peer
->af_sflags
[afi
][safi
],
255 PEER_STATUS_EOR_SEND
);
256 return bgp_update_packet_eor (peer
, afi
, safi
);
264 * Found a packet template to send, overwrite packet with appropriate
265 * attributes from peer and advance peer
267 s
= bpacket_reformat_for_peer (next_pkt
, paf
);
268 bpacket_queue_advance_peer (paf
);
275 /* The next action for the peer from a write perspective */
277 bgp_write_proceed_actions (struct peer
*peer
)
282 struct bpacket
*next_pkt
;
284 struct update_subgroup
*subgrp
;
286 if (stream_fifo_head (peer
->obuf
))
288 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
292 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
293 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
295 paf
= peer_af_find (peer
, afi
, safi
);
298 subgrp
= paf
->subgroup
;
302 next_pkt
= paf
->next_pkt_to_send
;
303 if (next_pkt
&& next_pkt
->buffer
)
305 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
309 /* No packets readily available for AFI/SAFI, are there subgroup packets
310 * that need to be generated? */
311 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
),
312 SUBGRP_PKTQ(subgrp
)))
314 else if (subgroup_packets_to_build (subgrp
))
316 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
320 /* No packets to send, see if EOR is pending */
321 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
323 if (!subgrp
->t_coalesce
&&
324 peer
->afc_nego
[afi
][safi
] &&
326 !CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
327 PEER_STATUS_EOR_SEND
) &&
328 safi
!= SAFI_MPLS_VPN
)
330 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
338 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
343 /* Write packet to the peer. */
345 bgp_write (struct thread
*thread
)
351 int update_last_write
= 0;
352 unsigned int count
= 0;
355 /* Yes first of all get peer pointer. */
356 peer
= THREAD_ARG (thread
);
357 peer
->t_write
= NULL
;
359 /* For non-blocking IO check. */
360 if (peer
->status
== Connect
)
362 bgp_connect_check (peer
, 1);
366 s
= bgp_write_packet (peer
);
369 bgp_write_proceed_actions (peer
);
373 sockopt_cork (peer
->fd
, 1);
375 oc
= peer
->update_out
;
377 /* Nonblocking write until TCP output buffer is full. */
382 /* Number of bytes to be sent. */
383 writenum
= stream_get_endp (s
) - stream_get_getp (s
);
385 /* Call write() system call. */
386 num
= write (peer
->fd
, STREAM_PNT (s
), writenum
);
389 /* write failed either retry needed or error */
390 if (ERRNO_IO_RETRY(errno
))
393 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
400 stream_forward_getp (s
, num
);
404 /* Retrieve BGP packet type. */
405 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
406 type
= stream_getc (s
);
418 /* Double start timer. */
421 /* Overflow check. */
422 if (peer
->v_start
>= (60 * 2))
423 peer
->v_start
= (60 * 2);
425 /* Flush any existing events */
426 BGP_EVENT_ADD (peer
, BGP_Stop
);
429 case BGP_MSG_KEEPALIVE
:
430 peer
->keepalive_out
++;
432 case BGP_MSG_ROUTE_REFRESH_NEW
:
433 case BGP_MSG_ROUTE_REFRESH_OLD
:
436 case BGP_MSG_CAPABILITY
:
437 peer
->dynamic_cap_out
++;
441 /* OK we send packet so delete it. */
442 bgp_packet_delete (peer
);
443 update_last_write
= 1;
445 while (++count
< peer
->bgp
->wpkt_quanta
&&
446 (s
= bgp_write_packet (peer
)) != NULL
);
448 bgp_write_proceed_actions (peer
);
451 /* Update last_update if UPDATEs were written. */
452 if (peer
->update_out
> oc
)
453 peer
->last_update
= bgp_clock ();
455 /* If we TXed any flavor of packet update last_write */
456 if (update_last_write
)
457 peer
->last_write
= bgp_clock ();
459 sockopt_cork (peer
->fd
, 0);
463 /* This is only for sending NOTIFICATION message to neighbor. */
465 bgp_write_notify (struct peer
*peer
)
471 /* There should be at least one packet. */
472 s
= stream_fifo_head (peer
->obuf
);
475 assert (stream_get_endp (s
) >= BGP_HEADER_SIZE
);
477 /* Stop collecting data within the socket */
478 sockopt_cork (peer
->fd
, 0);
480 /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
481 * we only care about getting a clean shutdown at this point. */
482 ret
= write (peer
->fd
, STREAM_DATA (s
), stream_get_endp (s
));
484 /* only connection reset/close gets counted as TCP_fatal_error, failure
485 * to write the entire NOTIFY doesn't get different FSM treatment */
488 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
492 /* Disable Nagle, make NOTIFY packet go out right away */
494 (void) setsockopt (peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
495 (char *) &val
, sizeof (val
));
497 /* Retrieve BGP packet type. */
498 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
499 type
= stream_getc (s
);
501 assert (type
== BGP_MSG_NOTIFY
);
503 /* Type should be notify. */
506 /* Double start timer. */
509 /* Overflow check. */
510 if (peer
->v_start
>= (60 * 2))
511 peer
->v_start
= (60 * 2);
513 /* Handle Graceful Restart case where the state changes to
514 Connect instead of Idle */
515 BGP_EVENT_ADD (peer
, BGP_Stop
);
520 /* Make keepalive packet and send it to the peer. */
522 bgp_keepalive_send (struct peer
*peer
)
526 s
= stream_new (BGP_MAX_PACKET_SIZE
);
528 /* Make keepalive packet. */
529 bgp_packet_set_marker (s
, BGP_MSG_KEEPALIVE
);
531 /* Set packet size. */
532 (void)bgp_packet_set_size (s
);
534 /* Dump packet if debug option is set. */
535 /* bgp_packet_dump (s); */
537 if (bgp_debug_keepalive(peer
))
538 zlog_debug ("%s sending KEEPALIVE", peer
->host
);
540 /* Add packet to the peer. */
541 bgp_packet_add (peer
, s
);
543 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
546 /* Make open packet and send it to the peer. */
548 bgp_open_send (struct peer
*peer
)
551 u_int16_t send_holdtime
;
554 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
555 send_holdtime
= peer
->holdtime
;
557 send_holdtime
= peer
->bgp
->default_holdtime
;
559 /* local-as Change */
560 if (peer
->change_local_as
)
561 local_as
= peer
->change_local_as
;
563 local_as
= peer
->local_as
;
565 s
= stream_new (BGP_MAX_PACKET_SIZE
);
567 /* Make open packet. */
568 bgp_packet_set_marker (s
, BGP_MSG_OPEN
);
570 /* Set open packet values. */
571 stream_putc (s
, BGP_VERSION_4
); /* BGP version */
572 stream_putw (s
, (local_as
<= BGP_AS_MAX
) ? (u_int16_t
) local_as
574 stream_putw (s
, send_holdtime
); /* Hold Time */
575 stream_put_in_addr (s
, &peer
->local_id
); /* BGP Identifier */
577 /* Set capability code. */
578 bgp_open_capability (s
, peer
);
580 /* Set BGP packet length. */
581 (void)bgp_packet_set_size (s
);
583 if (bgp_debug_neighbor_events(peer
))
584 zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
585 peer
->host
, BGP_VERSION_4
, local_as
,
586 send_holdtime
, inet_ntoa (peer
->local_id
));
588 /* Dump packet if debug option is set. */
589 /* bgp_packet_dump (s); */
591 /* Add packet to the peer. */
592 bgp_packet_add (peer
, s
);
594 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
597 /* Send BGP notify packet with data potion. */
599 bgp_notify_send_with_data (struct peer
*peer
, u_char code
, u_char sub_code
,
600 u_char
*data
, size_t datalen
)
605 /* Allocate new stream. */
606 s
= stream_new (BGP_MAX_PACKET_SIZE
);
608 /* Make nitify packet. */
609 bgp_packet_set_marker (s
, BGP_MSG_NOTIFY
);
611 /* Set notify packet values. */
612 stream_putc (s
, code
); /* BGP notify code */
613 stream_putc (s
, sub_code
); /* BGP notify sub_code */
615 /* If notify data is present. */
617 stream_write (s
, data
, datalen
);
619 /* Set BGP packet length. */
620 length
= bgp_packet_set_size (s
);
622 /* Add packet to the peer. */
623 stream_fifo_clean (peer
->obuf
);
624 bgp_packet_add (peer
, s
);
628 struct bgp_notify bgp_notify
;
633 bgp_notify
.code
= code
;
634 bgp_notify
.subcode
= sub_code
;
635 bgp_notify
.data
= NULL
;
636 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
637 bgp_notify
.raw_data
= data
;
639 peer
->notify
.code
= bgp_notify
.code
;
640 peer
->notify
.subcode
= bgp_notify
.subcode
;
642 if (bgp_notify
.length
)
644 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
645 for (i
= 0; i
< bgp_notify
.length
; i
++)
648 sprintf (c
, " %02x", data
[i
]);
649 strcat (bgp_notify
.data
, c
);
654 sprintf (c
, "%02x", data
[i
]);
655 strcpy (bgp_notify
.data
, c
);
658 bgp_notify_print (peer
, &bgp_notify
, "sending");
662 XFREE (MTYPE_TMP
, bgp_notify
.data
);
663 bgp_notify
.data
= NULL
;
664 bgp_notify
.length
= 0;
668 /* peer reset cause */
669 if (code
== BGP_NOTIFY_CEASE
)
671 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
672 peer
->last_reset
= PEER_DOWN_USER_RESET
;
673 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
674 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
676 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
679 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
681 /* Call immediately. */
682 BGP_WRITE_OFF (peer
->t_write
);
684 bgp_write_notify (peer
);
687 /* Send BGP notify packet. */
689 bgp_notify_send (struct peer
*peer
, u_char code
, u_char sub_code
)
691 bgp_notify_send_with_data (peer
, code
, sub_code
, NULL
, 0);
694 /* Send route refresh message to the peer. */
696 bgp_route_refresh_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
697 u_char orf_type
, u_char when_to_refresh
, int remove
)
700 struct bgp_filter
*filter
;
705 if (DISABLE_BGP_ANNOUNCE
)
708 filter
= &peer
->filter
[afi
][safi
];
710 /* Convert AFI, SAFI to values for packet. */
711 bgp_map_afi_safi_int2iana (afi
, safi
, &pkt_afi
, &pkt_safi
);
713 s
= stream_new (BGP_MAX_PACKET_SIZE
);
715 /* Make BGP update packet. */
716 if (CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
717 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_NEW
);
719 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_OLD
);
721 /* Encode Route Refresh message. */
722 stream_putw (s
, pkt_afi
);
724 stream_putc (s
, pkt_safi
);
726 if (orf_type
== ORF_TYPE_PREFIX
727 || orf_type
== ORF_TYPE_PREFIX_OLD
)
728 if (remove
|| filter
->plist
[FILTER_IN
].plist
)
734 stream_putc (s
, when_to_refresh
);
735 stream_putc (s
, orf_type
);
736 orfp
= stream_get_endp (s
);
741 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
742 stream_putc (s
, ORF_COMMON_PART_REMOVE_ALL
);
743 if (bgp_debug_neighbor_events(peer
))
744 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
745 peer
->host
, orf_type
,
746 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
751 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
752 prefix_bgp_orf_entry (s
, filter
->plist
[FILTER_IN
].plist
,
753 ORF_COMMON_PART_ADD
, ORF_COMMON_PART_PERMIT
,
754 ORF_COMMON_PART_DENY
);
755 if (bgp_debug_neighbor_events(peer
))
756 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
757 peer
->host
, orf_type
,
758 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
762 /* Total ORF Entry Len. */
763 orf_len
= stream_get_endp (s
) - orfp
- 2;
764 stream_putw_at (s
, orfp
, orf_len
);
767 /* Set packet size. */
768 (void)bgp_packet_set_size (s
);
770 if (bgp_debug_neighbor_events(peer
))
773 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
774 peer
->host
, pkt_afi
, pkt_safi
);
777 /* Add packet to the peer. */
778 bgp_packet_add (peer
, s
);
780 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
783 /* Send capability message to the peer. */
785 bgp_capability_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
786 int capability_code
, int action
)
792 /* Convert AFI, SAFI to values for packet. */
793 bgp_map_afi_safi_int2iana (afi
, safi
, &pkt_afi
, &pkt_safi
);
795 s
= stream_new (BGP_MAX_PACKET_SIZE
);
797 /* Make BGP update packet. */
798 bgp_packet_set_marker (s
, BGP_MSG_CAPABILITY
);
800 /* Encode MP_EXT capability. */
801 if (capability_code
== CAPABILITY_CODE_MP
)
803 stream_putc (s
, action
);
804 stream_putc (s
, CAPABILITY_CODE_MP
);
805 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
806 stream_putw (s
, pkt_afi
);
808 stream_putc (s
, pkt_safi
);
810 if (bgp_debug_neighbor_events(peer
))
811 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
812 peer
->host
, action
== CAPABILITY_ACTION_SET
?
813 "Advertising" : "Removing", pkt_afi
, pkt_safi
);
816 /* Set packet size. */
817 (void)bgp_packet_set_size (s
);
819 /* Add packet to the peer. */
820 bgp_packet_add (peer
, s
);
822 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
825 /* RFC1771 6.8 Connection collision detection. */
827 bgp_collision_detect (struct peer
*new, struct in_addr remote_id
)
831 /* Upon receipt of an OPEN message, the local system must examine
832 all of its connections that are in the OpenConfirm state. A BGP
833 speaker may also examine connections in an OpenSent state if it
834 knows the BGP Identifier of the peer by means outside of the
835 protocol. If among these connections there is a connection to a
836 remote BGP speaker whose BGP Identifier equals the one in the
837 OPEN message, then the local system performs the following
838 collision resolution procedure: */
840 if ((peer
= new->doppelganger
) != NULL
)
842 /* Do not accept the new connection in Established or Clearing states.
843 * Note that a peer GR is handled by closing the existing connection
844 * upon receipt of new one.
846 if (peer
->status
== Established
|| peer
->status
== Clearing
)
848 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
849 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
852 else if ((peer
->status
== OpenConfirm
) || (peer
->status
== OpenSent
))
854 /* 1. The BGP Identifier of the local system is compared to
855 the BGP Identifier of the remote system (as specified in
856 the OPEN message). */
858 if (ntohl (peer
->local_id
.s_addr
) < ntohl (remote_id
.s_addr
))
859 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
861 /* 2. If the value of the local BGP Identifier is less
862 than the remote one, the local system closes BGP
863 connection that already exists (the one that is
864 already in the OpenConfirm state), and accepts BGP
865 connection initiated by the remote system. */
866 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
867 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
872 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
873 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
878 /* 3. Otherwise, the local system closes newly created
879 BGP connection (the one associated with the newly
880 received OPEN message), and continues to use the
881 existing one (the one that is already in the
882 OpenConfirm state). */
883 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
885 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
886 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
891 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
892 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
902 bgp_open_receive (struct peer
*peer
, bgp_size_t size
)
908 u_int16_t send_holdtime
;
911 struct in_addr remote_id
;
913 u_int8_t notify_data_remote_as
[2];
914 u_int8_t notify_data_remote_as4
[4];
915 u_int8_t notify_data_remote_id
[4];
916 u_int16_t
*holdtime_ptr
;
918 /* Parse open packet. */
919 version
= stream_getc (peer
->ibuf
);
920 memcpy (notify_data_remote_as
, stream_pnt (peer
->ibuf
), 2);
921 remote_as
= stream_getw (peer
->ibuf
);
922 holdtime_ptr
= (u_int16_t
*)stream_pnt (peer
->ibuf
);
923 holdtime
= stream_getw (peer
->ibuf
);
924 memcpy (notify_data_remote_id
, stream_pnt (peer
->ibuf
), 4);
925 remote_id
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
927 /* Receive OPEN message log */
928 if (bgp_debug_neighbor_events(peer
))
929 zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
930 " holdtime %d, id %s",
931 peer
->host
, version
, remote_as
, holdtime
,
932 inet_ntoa (remote_id
));
934 /* BEGIN to read the capability here, but dont do it yet */
936 optlen
= stream_getc (peer
->ibuf
);
940 /* If not enough bytes, it is an error. */
941 if (STREAM_READABLE(peer
->ibuf
) < optlen
)
943 bgp_notify_send (peer
, BGP_NOTIFY_OPEN_ERR
,
944 BGP_NOTIFY_OPEN_MALFORMED_ATTR
);
948 /* We need the as4 capability value *right now* because
949 * if it is there, we have not got the remote_as yet, and without
950 * that we do not know which peer is connecting to us now.
952 as4
= peek_for_as4_capability (peer
, optlen
);
953 memcpy (notify_data_remote_as4
, &as4
, 4);
956 /* Just in case we have a silly peer who sends AS4 capability set to 0 */
957 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
)
959 zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
961 bgp_notify_send_with_data (peer
,
963 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
964 notify_data_remote_as4
, 4);
968 if (remote_as
== BGP_AS_TRANS
)
970 /* Take the AS4 from the capability. We must have received the
971 * capability now! Otherwise we have a asn16 peer who uses
972 * BGP_AS_TRANS, for some unknown reason.
974 if (as4
== BGP_AS_TRANS
)
976 zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
978 bgp_notify_send_with_data (peer
,
980 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
981 notify_data_remote_as4
, 4);
985 if (!as4
&& BGP_DEBUG (as4
, AS4
))
986 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
987 " Odd, but proceeding.", peer
->host
);
988 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG (as4
, AS4
))
989 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
990 "in 2-bytes, very odd peer.", peer
->host
, as4
);
996 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
997 /* If we have got the capability, peer->as4cap must match remote_as */
998 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
)
1001 /* raise error, log this, close session */
1002 zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
1003 " mismatch with 16bit 'myasn' %u in open",
1004 peer
->host
, as4
, remote_as
);
1005 bgp_notify_send_with_data (peer
,
1006 BGP_NOTIFY_OPEN_ERR
,
1007 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1008 notify_data_remote_as4
, 4);
1013 /* remote router-id check. */
1014 if (remote_id
.s_addr
== 0
1015 || IPV4_CLASS_DE (ntohl (remote_id
.s_addr
))
1016 || ntohl (peer
->local_id
.s_addr
) == ntohl (remote_id
.s_addr
))
1018 if (bgp_debug_neighbor_events(peer
))
1019 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1020 peer
->host
, inet_ntoa (remote_id
));
1021 bgp_notify_send_with_data (peer
,
1022 BGP_NOTIFY_OPEN_ERR
,
1023 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1024 notify_data_remote_id
, 4);
1028 /* Set remote router-id */
1029 peer
->remote_id
= remote_id
;
1031 /* Peer BGP version check. */
1032 if (version
!= BGP_VERSION_4
)
1034 u_int16_t maxver
= htons(BGP_VERSION_4
);
1035 /* XXX this reply may not be correct if version < 4 XXX */
1036 if (bgp_debug_neighbor_events(peer
))
1037 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
1038 peer
->host
, version
, BGP_VERSION_4
);
1039 /* Data must be in network byte order here */
1040 bgp_notify_send_with_data (peer
,
1041 BGP_NOTIFY_OPEN_ERR
,
1042 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1043 (u_int8_t
*) &maxver
, 2);
1047 /* Check neighbor as number. */
1048 if (peer
->as_type
== AS_UNSPECIFIED
)
1050 if (bgp_debug_neighbor_events(peer
))
1051 zlog_debug("%s bad OPEN, remote AS is unspecified currently", peer
->host
);
1052 bgp_notify_send_with_data(peer
,
1053 BGP_NOTIFY_OPEN_ERR
,
1054 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1055 notify_data_remote_as
, 2);
1058 else if (peer
->as_type
== AS_INTERNAL
)
1060 if (remote_as
!= peer
->bgp
->as
)
1062 if (bgp_debug_neighbor_events(peer
))
1063 zlog_debug ("%s bad OPEN, remote AS is %u, internal specified",
1064 peer
->host
, remote_as
);
1065 bgp_notify_send_with_data (peer
,
1066 BGP_NOTIFY_OPEN_ERR
,
1067 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1068 notify_data_remote_as
, 2);
1071 peer
->as
= peer
->local_as
;
1073 else if (peer
->as_type
== AS_EXTERNAL
)
1075 if (remote_as
== peer
->bgp
->as
)
1077 if (bgp_debug_neighbor_events(peer
))
1078 zlog_debug ("%s bad OPEN, remote AS is %u, external specified",
1079 peer
->host
, remote_as
);
1080 bgp_notify_send_with_data (peer
,
1081 BGP_NOTIFY_OPEN_ERR
,
1082 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1083 notify_data_remote_as
, 2);
1086 peer
->as
= remote_as
;
1088 else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
))
1090 if (bgp_debug_neighbor_events(peer
))
1091 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1092 peer
->host
, remote_as
, peer
->as
);
1093 bgp_notify_send_with_data (peer
,
1094 BGP_NOTIFY_OPEN_ERR
,
1095 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1096 notify_data_remote_as
, 2);
1100 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1101 calculate the value of the Hold Timer by using the smaller of its
1102 configured Hold Time and the Hold Time received in the OPEN message.
1103 The Hold Time MUST be either zero or at least three seconds. An
1104 implementation may reject connections on the basis of the Hold Time. */
1106 if (holdtime
< 3 && holdtime
!= 0)
1108 bgp_notify_send_with_data (peer
,
1109 BGP_NOTIFY_OPEN_ERR
,
1110 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1111 (u_char
*)holdtime_ptr
, 2);
1115 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1116 would be one third of the Hold Time interval. KEEPALIVE messages
1117 MUST NOT be sent more frequently than one per second. An
1118 implementation MAY adjust the rate at which it sends KEEPALIVE
1119 messages as a function of the Hold Time interval. */
1121 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1122 send_holdtime
= peer
->holdtime
;
1124 send_holdtime
= peer
->bgp
->default_holdtime
;
1126 if (holdtime
< send_holdtime
)
1127 peer
->v_holdtime
= holdtime
;
1129 peer
->v_holdtime
= send_holdtime
;
1131 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1132 peer
->v_keepalive
= peer
->keepalive
;
1134 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1136 /* Open option part parse. */
1139 if ((ret
= bgp_open_option_parse (peer
, optlen
, &mp_capability
)) < 0)
1144 if (bgp_debug_neighbor_events(peer
))
1145 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
1150 * Assume that the peer supports the locally configured set of
1151 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1152 * capabilities, or if 'override-capability' is configured.
1154 if (! mp_capability
||
1155 CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
1157 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] = peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1158 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1159 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] = peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1160 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1163 /* When collision is detected and this peer is closed. Retrun
1165 ret
= bgp_collision_detect (peer
, remote_id
);
1170 if ((ret
= bgp_getsockname (peer
)) < 0)
1172 zlog_err("%s: bgp_getsockname() failed for peer: %s", __FUNCTION__
,
1177 /* Verify valid local address present based on negotiated address-families. */
1178 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] ||
1179 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] ||
1180 peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
] ||
1181 peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
])
1183 if (!peer
->nexthop
.v4
.s_addr
)
1185 #if defined (HAVE_CUMULUS)
1186 zlog_err ("%s: No local IPv4 addr resetting connection, fd %d",
1187 peer
->host
, peer
->fd
);
1188 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1193 if (peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] ||
1194 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] ||
1195 peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
] ||
1196 peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
])
1198 if (IN6_IS_ADDR_UNSPECIFIED (&peer
->nexthop
.v6_global
))
1200 #if defined (HAVE_CUMULUS)
1201 zlog_err ("%s: No local IPv6 addr resetting connection, fd %d",
1202 peer
->host
, peer
->fd
);
1203 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_SUBCODE_UNSPECIFIC
);
1208 peer
->rtt
= sockopt_tcp_rtt (peer
->fd
);
1210 if ((ret
= bgp_event_update(peer
, Receive_OPEN_message
)) < 0)
1212 zlog_err("%s: BGP event update failed for peer: %s", __FUNCTION__
,
1214 /* DD: bgp send notify and reset state */
1218 peer
->packet_size
= 0;
1220 stream_reset (peer
->ibuf
);
1225 /* Called when there is a change in the EOR(implicit or explicit) status of a peer.
1226 Ends the update-delay if all expected peers are done with EORs. */
1228 bgp_check_update_delay(struct bgp
*bgp
)
1230 struct listnode
*node
, *nnode
;
1231 struct peer
*peer
= NULL
;
1233 if (bgp_debug_neighbor_events(peer
))
1234 zlog_debug ("Checking update delay, T: %d R: %d I:%d E: %d", bgp
->established
,
1235 bgp
->restarted_peers
, bgp
->implicit_eors
, bgp
->explicit_eors
);
1237 if (bgp
->established
<=
1238 bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
)
1240 /* This is an extra sanity check to make sure we wait for all the
1241 eligible configured peers. This check is performed if establish wait
1242 timer is on, or establish wait option is not given with the
1243 update-delay command */
1244 if (bgp
->t_establish_wait
||
1245 (bgp
->v_establish_wait
== bgp
->v_update_delay
))
1246 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
1248 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1249 && !CHECK_FLAG (peer
->flags
, PEER_FLAG_SHUTDOWN
)
1250 && !peer
->update_delay_over
)
1252 if (bgp_debug_neighbor_events(peer
))
1253 zlog_debug (" Peer %s pending, continuing read-only mode",
1259 zlog_info ("Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
1260 bgp
->restarted_peers
, bgp
->implicit_eors
, bgp
->explicit_eors
);
1261 bgp_update_delay_end(bgp
);
1265 /* Called if peer is known to have restarted. The restart-state bit in
1266 Graceful-Restart capability is used for that */
1268 bgp_update_restarted_peers (struct peer
*peer
)
1270 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1271 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1273 if (bgp_debug_neighbor_events(peer
))
1274 zlog_debug ("Peer %s: Checking restarted", peer
->host
);
1276 if (peer
->status
== Established
)
1278 peer
->update_delay_over
= 1;
1279 peer
->bgp
->restarted_peers
++;
1280 bgp_check_update_delay(peer
->bgp
);
1284 /* Called as peer receives a keep-alive. Determines if this occurence can be
1285 taken as an implicit EOR for this peer.
1286 NOTE: The very first keep-alive after the Established state of a peer is
1287 considered implicit EOR for the update-delay purposes */
1289 bgp_update_implicit_eors (struct peer
*peer
)
1291 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1292 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1294 if (bgp_debug_neighbor_events(peer
))
1295 zlog_debug ("Peer %s: Checking implicit EORs", peer
->host
);
1297 if (peer
->status
== Established
)
1299 peer
->update_delay_over
= 1;
1300 peer
->bgp
->implicit_eors
++;
1301 bgp_check_update_delay(peer
->bgp
);
1305 /* Should be called only when there is a change in the EOR_RECEIVED status
1306 for any afi/safi on a peer */
1308 bgp_update_explicit_eors (struct peer
*peer
)
1313 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1314 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1316 if (bgp_debug_neighbor_events(peer
))
1317 zlog_debug ("Peer %s: Checking explicit EORs", peer
->host
);
1319 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1320 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1322 if (peer
->afc_nego
[afi
][safi
] &&
1323 !CHECK_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_RECEIVED
))
1325 if (bgp_debug_neighbor_events(peer
))
1326 zlog_debug (" afi %d safi %d didnt receive EOR", afi
, safi
);
1331 peer
->update_delay_over
= 1;
1332 peer
->bgp
->explicit_eors
++;
1333 bgp_check_update_delay(peer
->bgp
);
1336 /* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers
1337 * mp_withdraw, if set, is used to nullify attr structure on most of the calling safi function
1338 * and for evpn, passed as parameter
1341 bgp_nlri_parse (struct peer
*peer
, struct attr
*attr
, struct bgp_nlri
*packet
, int mp_withdraw
)
1343 switch (packet
->safi
)
1346 case SAFI_MULTICAST
:
1347 return bgp_nlri_parse_ip (peer
, mp_withdraw
?NULL
:attr
, packet
);
1349 return bgp_nlri_parse_vpn (peer
, mp_withdraw
?NULL
:attr
, packet
);
1351 return bgp_nlri_parse_evpn (peer
, attr
, packet
, mp_withdraw
);
1356 /* Parse BGP Update packet and make attribute object. */
1358 bgp_update_receive (struct peer
*peer
, bgp_size_t size
)
1364 struct attr_extra extra
;
1365 bgp_size_t attribute_len
;
1366 bgp_size_t update_len
;
1367 bgp_size_t withdraw_len
;
1376 struct bgp_nlri nlris
[NLRI_TYPE_MAX
];
1378 /* Status must be Established. */
1379 if (peer
->status
!= Established
)
1381 zlog_err ("%s [FSM] Update packet received under status %s",
1382 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1383 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1387 /* Set initial values. */
1388 memset (&attr
, 0, sizeof (struct attr
));
1389 memset (&extra
, 0, sizeof (struct attr_extra
));
1390 memset (&nlris
, 0, sizeof (nlris
));
1391 attr
.extra
= &extra
;
1392 memset (peer
->rcvd_attr_str
, 0, BUFSIZ
);
1393 peer
->rcvd_attr_printed
= 0;
1396 end
= stream_pnt (s
) + size
;
1398 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1399 Length is too large (i.e., if Unfeasible Routes Length + Total
1400 Attribute Length + 23 exceeds the message Length), then the Error
1401 Subcode is set to Malformed Attribute List. */
1402 if (stream_pnt (s
) + 2 > end
)
1404 zlog_err ("%s [Error] Update packet error"
1405 " (packet length is short for unfeasible length)",
1407 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1408 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1412 /* Unfeasible Route Length. */
1413 withdraw_len
= stream_getw (s
);
1415 /* Unfeasible Route Length check. */
1416 if (stream_pnt (s
) + withdraw_len
> end
)
1418 zlog_err ("%s [Error] Update packet error"
1419 " (packet unfeasible length overflow %d)",
1420 peer
->host
, withdraw_len
);
1421 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1422 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1426 /* Unfeasible Route packet format check. */
1427 if (withdraw_len
> 0)
1429 nlris
[NLRI_WITHDRAW
].afi
= AFI_IP
;
1430 nlris
[NLRI_WITHDRAW
].safi
= SAFI_UNICAST
;
1431 nlris
[NLRI_WITHDRAW
].nlri
= stream_pnt (s
);
1432 nlris
[NLRI_WITHDRAW
].length
= withdraw_len
;
1433 stream_forward_getp (s
, withdraw_len
);
1436 /* Attribute total length check. */
1437 if (stream_pnt (s
) + 2 > end
)
1439 zlog_warn ("%s [Error] Packet Error"
1440 " (update packet is short for attribute length)",
1442 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1443 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1447 /* Fetch attribute total length. */
1448 attribute_len
= stream_getw (s
);
1450 /* Attribute length check. */
1451 if (stream_pnt (s
) + attribute_len
> end
)
1453 zlog_warn ("%s [Error] Packet Error"
1454 " (update packet attribute length overflow %d)",
1455 peer
->host
, attribute_len
);
1456 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1457 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1461 /* Certain attribute parsing errors should not be considered bad enough
1462 * to reset the session for, most particularly any partial/optional
1463 * attributes that have 'tunneled' over speakers that don't understand
1464 * them. Instead we withdraw only the prefix concerned.
1466 * Complicates the flow a little though..
1468 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1469 /* This define morphs the update case into a withdraw when lower levels
1470 * have signalled an error condition where this is best.
1472 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1474 /* Parse attribute when it exists. */
1477 attr_parse_ret
= bgp_attr_parse (peer
, &attr
, attribute_len
,
1478 &nlris
[NLRI_MP_UPDATE
], &nlris
[NLRI_MP_WITHDRAW
]);
1479 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
)
1481 bgp_attr_unintern_sub (&attr
);
1486 /* Logging the attribute. */
1487 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
||
1488 BGP_DEBUG (update
, UPDATE_IN
) ||
1489 BGP_DEBUG (update
, UPDATE_PREFIX
))
1491 ret
= bgp_dump_attr (peer
, &attr
, peer
->rcvd_attr_str
, BUFSIZ
);
1493 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1494 zlog_err ("%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1497 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1))
1499 zlog_debug ("%s rcvd UPDATE w/ attr: %s", peer
->host
, peer
->rcvd_attr_str
);
1500 peer
->rcvd_attr_printed
= 1;
1504 /* Network Layer Reachability Information. */
1505 update_len
= end
- stream_pnt (s
);
1509 /* Set NLRI portion to structure. */
1510 nlris
[NLRI_UPDATE
].afi
= AFI_IP
;
1511 nlris
[NLRI_UPDATE
].safi
= SAFI_UNICAST
;
1512 nlris
[NLRI_UPDATE
].nlri
= stream_pnt (s
);
1513 nlris
[NLRI_UPDATE
].length
= update_len
;
1514 stream_forward_getp (s
, update_len
);
1517 if (BGP_DEBUG (update
, UPDATE_IN
))
1518 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1519 peer
->host
, withdraw_len
, attribute_len
, update_len
);
1521 /* Parse any given NLRIs */
1522 for (int i
= NLRI_UPDATE
; i
< NLRI_TYPE_MAX
; i
++)
1527 /* NLRI is processed iff the peer if configured for the specific afi/safi */
1528 if (!peer
->afc
[nlris
[i
].afi
][nlris
[i
].safi
])
1530 zlog_info ("%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1531 peer
->host
, nlris
[i
].afi
, nlris
[i
].safi
);
1535 /* EoR handled later */
1536 if (nlris
[i
].length
== 0)
1542 case NLRI_MP_UPDATE
:
1543 nlri_ret
= bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &nlris
[i
], 0);
1546 case NLRI_MP_WITHDRAW
:
1547 nlri_ret
= bgp_nlri_parse (peer
, &attr
, &nlris
[i
], 1);
1555 zlog_err("%s [Error] Error parsing NLRI", peer
->host
);
1556 if (peer
->status
== Established
)
1557 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1558 i
<= NLRI_WITHDRAW
? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1559 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR
);
1560 bgp_attr_unintern_sub (&attr
);
1567 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1568 * and MP EoR should have only an empty MP_UNREACH
1570 if (!update_len
&& !withdraw_len
1571 && nlris
[NLRI_MP_UPDATE
].length
== 0)
1576 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already checked
1577 * update and withdraw NLRI lengths are 0.
1582 safi
= SAFI_UNICAST
;
1584 else if (attr
.flag
& ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI
)
1585 && nlris
[NLRI_MP_WITHDRAW
].length
== 0)
1587 afi
= nlris
[NLRI_MP_WITHDRAW
].afi
;
1588 safi
= nlris
[NLRI_MP_WITHDRAW
].safi
;
1591 if (afi
&& peer
->afc
[afi
][safi
])
1593 /* End-of-RIB received */
1594 if (!CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_RECEIVED
))
1596 SET_FLAG (peer
->af_sflags
[afi
][safi
],
1597 PEER_STATUS_EOR_RECEIVED
);
1598 bgp_update_explicit_eors(peer
);
1601 /* NSF delete stale route */
1602 if (peer
->nsf
[afi
][safi
])
1603 bgp_clear_stale_route (peer
, afi
, safi
);
1605 if (bgp_debug_neighbor_events(peer
))
1607 zlog_debug ("rcvd End-of-RIB for %s from %s",
1608 afi_safi_print (afi
, safi
), peer
->host
);
1613 /* Everything is done. We unintern temporary structures which
1614 interned in bgp_attr_parse(). */
1615 bgp_attr_unintern_sub (&attr
);
1617 /* If peering is stopped due to some reason, do not generate BGP
1619 if (peer
->status
!= Established
)
1622 /* Increment packet counter. */
1624 peer
->update_time
= bgp_clock ();
1626 /* Rearm holdtime timer */
1627 BGP_TIMER_OFF (peer
->t_holdtime
);
1628 bgp_timer_set (peer
);
1633 /* Notify message treatment function. */
1635 bgp_notify_receive (struct peer
*peer
, bgp_size_t size
)
1637 struct bgp_notify bgp_notify
;
1639 if (peer
->notify
.data
)
1641 XFREE (MTYPE_TMP
, peer
->notify
.data
);
1642 peer
->notify
.data
= NULL
;
1643 peer
->notify
.length
= 0;
1646 bgp_notify
.code
= stream_getc (peer
->ibuf
);
1647 bgp_notify
.subcode
= stream_getc (peer
->ibuf
);
1648 bgp_notify
.length
= size
- 2;
1649 bgp_notify
.data
= NULL
;
1651 /* Preserv notify code and sub code. */
1652 peer
->notify
.code
= bgp_notify
.code
;
1653 peer
->notify
.subcode
= bgp_notify
.subcode
;
1654 /* For further diagnostic record returned Data. */
1655 if (bgp_notify
.length
)
1657 peer
->notify
.length
= size
- 2;
1658 peer
->notify
.data
= XMALLOC (MTYPE_TMP
, size
- 2);
1659 memcpy (peer
->notify
.data
, stream_pnt (peer
->ibuf
), size
- 2);
1668 if (bgp_notify
.length
)
1670 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
1671 for (i
= 0; i
< bgp_notify
.length
; i
++)
1674 sprintf (c
, " %02x", stream_getc (peer
->ibuf
));
1675 strcat (bgp_notify
.data
, c
);
1680 sprintf (c
, "%02x", stream_getc (peer
->ibuf
));
1681 strcpy (bgp_notify
.data
, c
);
1683 bgp_notify
.raw_data
= (u_char
*)peer
->notify
.data
;
1686 bgp_notify_print(peer
, &bgp_notify
, "received");
1687 if (bgp_notify
.data
)
1689 XFREE (MTYPE_TMP
, bgp_notify
.data
);
1690 bgp_notify
.data
= NULL
;
1691 bgp_notify
.length
= 0;
1695 /* peer count update */
1698 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1700 /* We have to check for Notify with Unsupported Optional Parameter.
1701 in that case we fallback to open without the capability option.
1702 But this done in bgp_stop. We just mark it here to avoid changing
1704 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1705 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1706 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1708 BGP_EVENT_ADD (peer
, Receive_NOTIFICATION_message
);
1711 /* Keepalive treatment function -- get keepalive send keepalive */
1713 bgp_keepalive_receive (struct peer
*peer
, bgp_size_t size
)
1715 if (bgp_debug_keepalive(peer
))
1716 zlog_debug ("%s KEEPALIVE rcvd", peer
->host
);
1718 BGP_EVENT_ADD (peer
, Receive_KEEPALIVE_message
);
1721 /* Route refresh message is received. */
1723 bgp_route_refresh_receive (struct peer
*peer
, bgp_size_t size
)
1727 safi_t pkt_safi
, safi
;
1729 struct peer_af
*paf
;
1730 struct update_group
*updgrp
;
1731 struct peer
*updgrp_peer
;
1733 /* If peer does not have the capability, send notification. */
1734 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_ADV
))
1736 zlog_err ("%s [Error] BGP route refresh is not enabled",
1738 bgp_notify_send (peer
,
1739 BGP_NOTIFY_HEADER_ERR
,
1740 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1744 /* Status must be Established. */
1745 if (peer
->status
!= Established
)
1747 zlog_err ("%s [Error] Route refresh packet received under status %s",
1748 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1749 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1756 pkt_afi
= stream_getw (s
);
1757 (void)stream_getc (s
);
1758 pkt_safi
= stream_getc (s
);
1760 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1761 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1762 peer
->host
, pkt_afi
, pkt_safi
);
1764 /* Convert AFI, SAFI to internal values and check. */
1765 if (bgp_map_afi_safi_iana2int (pkt_afi
, pkt_safi
, &afi
, &safi
))
1767 zlog_info ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1768 peer
->host
, pkt_afi
, pkt_safi
);
1772 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1775 u_char when_to_refresh
;
1779 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) < 5)
1781 zlog_info ("%s ORF route refresh length error", peer
->host
);
1782 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1786 when_to_refresh
= stream_getc (s
);
1787 end
= stream_pnt (s
) + (size
- 5);
1789 while ((stream_pnt (s
) + 2) < end
)
1791 orf_type
= stream_getc (s
);
1792 orf_len
= stream_getw (s
);
1794 /* orf_len in bounds? */
1795 if ((stream_pnt (s
) + orf_len
) > end
)
1796 break; /* XXX: Notify instead?? */
1797 if (orf_type
== ORF_TYPE_PREFIX
1798 || orf_type
== ORF_TYPE_PREFIX_OLD
)
1800 uint8_t *p_pnt
= stream_pnt (s
);
1801 uint8_t *p_end
= stream_pnt (s
) + orf_len
;
1802 struct orf_prefix orfp
;
1807 int ret
= CMD_SUCCESS
;
1809 if (bgp_debug_neighbor_events(peer
))
1811 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
1812 peer
->host
, orf_type
, orf_len
);
1815 /* we're going to read at least 1 byte of common ORF header,
1816 * and 7 bytes of ORF Address-filter entry from the stream
1821 /* ORF prefix-list name */
1822 sprintf (name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1824 while (p_pnt
< p_end
)
1826 /* If the ORF entry is malformed, want to read as much of it
1827 * as possible without going beyond the bounds of the entry,
1828 * to maximise debug information.
1831 memset (&orfp
, 0, sizeof (struct orf_prefix
));
1833 /* after ++: p_pnt <= p_end */
1834 if (common
& ORF_COMMON_PART_REMOVE_ALL
)
1836 if (bgp_debug_neighbor_events(peer
))
1837 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer
->host
);
1838 prefix_bgp_orf_remove_all (afi
, name
);
1841 ok
= ((u_int32_t
)(p_end
- p_pnt
) >= sizeof(u_int32_t
)) ;
1844 memcpy (&seq
, p_pnt
, sizeof (u_int32_t
));
1845 p_pnt
+= sizeof (u_int32_t
);
1846 orfp
.seq
= ntohl (seq
);
1851 if ((ok
= (p_pnt
< p_end
)))
1852 orfp
.ge
= *p_pnt
++ ; /* value checked in prefix_bgp_orf_set() */
1853 if ((ok
= (p_pnt
< p_end
)))
1854 orfp
.le
= *p_pnt
++ ; /* value checked in prefix_bgp_orf_set() */
1855 if ((ok
= (p_pnt
< p_end
)))
1856 orfp
.p
.prefixlen
= *p_pnt
++ ;
1857 orfp
.p
.family
= afi2family (afi
); /* afi checked already */
1859 psize
= PSIZE (orfp
.p
.prefixlen
); /* 0 if not ok */
1860 if (psize
> prefix_blen(&orfp
.p
)) /* valid for family ? */
1863 psize
= prefix_blen(&orfp
.p
) ;
1865 if (psize
> (p_end
- p_pnt
)) /* valid for packet ? */
1868 psize
= p_end
- p_pnt
;
1872 memcpy (&orfp
.p
.u
.prefix
, p_pnt
, psize
);
1875 if (bgp_debug_neighbor_events(peer
))
1877 char buf
[INET6_BUFSIZ
];
1879 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
1881 (common
& ORF_COMMON_PART_REMOVE
? "Remove" : "Add"),
1882 (common
& ORF_COMMON_PART_DENY
? "deny" : "permit"),
1884 inet_ntop (orfp
.p
.family
, &orfp
.p
.u
.prefix
, buf
, INET6_BUFSIZ
),
1885 orfp
.p
.prefixlen
, orfp
.ge
, orfp
.le
,
1886 ok
? "" : " MALFORMED");
1890 ret
= prefix_bgp_orf_set (name
, afi
, &orfp
,
1891 (common
& ORF_COMMON_PART_DENY
? 0 : 1 ),
1892 (common
& ORF_COMMON_PART_REMOVE
? 0 : 1));
1894 if (!ok
|| (ok
&& ret
!= CMD_SUCCESS
))
1896 zlog_info ("%s Received misformatted prefixlist ORF."
1897 " Remove All pfxlist", peer
->host
);
1898 prefix_bgp_orf_remove_all (afi
, name
);
1903 peer
->orf_plist
[afi
][safi
] = prefix_bgp_orf_lookup (afi
, name
);
1905 stream_forward_getp (s
, orf_len
);
1907 if (bgp_debug_neighbor_events(peer
))
1908 zlog_debug ("%s rcvd Refresh %s ORF request", peer
->host
,
1909 when_to_refresh
== REFRESH_DEFER
? "Defer" : "Immediate");
1910 if (when_to_refresh
== REFRESH_DEFER
)
1914 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1915 if (CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
))
1916 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
1918 paf
= peer_af_find (peer
, afi
, safi
);
1919 if (paf
&& paf
->subgroup
)
1921 if (peer
->orf_plist
[afi
][safi
])
1923 updgrp
= PAF_UPDGRP(paf
);
1924 updgrp_peer
= UPDGRP_PEER(updgrp
);
1925 updgrp_peer
->orf_plist
[afi
][safi
] = peer
->orf_plist
[afi
][safi
];
1928 /* If the peer is configured for default-originate clear the
1929 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will re-advertise the
1932 if (CHECK_FLAG (paf
->subgroup
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1933 UNSET_FLAG (paf
->subgroup
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1936 /* Perform route refreshment to the peer */
1937 bgp_announce_route (peer
, afi
, safi
);
1941 bgp_capability_msg_parse (struct peer
*peer
, u_char
*pnt
, bgp_size_t length
)
1944 struct capability_mp_data mpc
;
1945 struct capability_header
*hdr
;
1949 safi_t pkt_safi
, safi
;
1955 /* We need at least action, capability code and capability length. */
1958 zlog_info ("%s Capability length error", peer
->host
);
1959 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1963 hdr
= (struct capability_header
*)(pnt
+ 1);
1965 /* Action value check. */
1966 if (action
!= CAPABILITY_ACTION_SET
1967 && action
!= CAPABILITY_ACTION_UNSET
)
1969 zlog_info ("%s Capability Action Value error %d",
1970 peer
->host
, action
);
1971 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1975 if (bgp_debug_neighbor_events(peer
))
1976 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
1977 peer
->host
, action
, hdr
->code
, hdr
->length
);
1979 /* Capability length check. */
1980 if ((pnt
+ hdr
->length
+ 3) > end
)
1982 zlog_info ("%s Capability length error", peer
->host
);
1983 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1987 /* Fetch structure to the byte stream. */
1988 memcpy (&mpc
, pnt
+ 3, sizeof (struct capability_mp_data
));
1990 /* We know MP Capability Code. */
1991 if (hdr
->code
== CAPABILITY_CODE_MP
)
1993 pkt_afi
= ntohs (mpc
.afi
);
1994 pkt_safi
= mpc
.safi
;
1996 /* Ignore capability when override-capability is set. */
1997 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
2000 /* Convert AFI, SAFI to internal values. */
2001 if (bgp_map_afi_safi_iana2int (pkt_afi
, pkt_safi
, &afi
, &safi
))
2003 if (bgp_debug_neighbor_events(peer
))
2004 zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
2005 "(%u/%u)", peer
->host
, pkt_afi
, pkt_safi
);
2009 /* Address family check. */
2010 if (bgp_debug_neighbor_events(peer
))
2011 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2013 action
== CAPABILITY_ACTION_SET
2014 ? "Advertising" : "Removing",
2017 if (action
== CAPABILITY_ACTION_SET
)
2019 peer
->afc_recv
[afi
][safi
] = 1;
2020 if (peer
->afc
[afi
][safi
])
2022 peer
->afc_nego
[afi
][safi
] = 1;
2023 bgp_announce_route (peer
, afi
, safi
);
2028 peer
->afc_recv
[afi
][safi
] = 0;
2029 peer
->afc_nego
[afi
][safi
] = 0;
2031 if (peer_active_nego (peer
))
2032 bgp_clear_route (peer
, afi
, safi
);
2034 BGP_EVENT_ADD (peer
, BGP_Stop
);
2039 zlog_warn ("%s unrecognized capability code: %d - ignored",
2040 peer
->host
, hdr
->code
);
2042 pnt
+= hdr
->length
+ 3;
2047 /* Dynamic Capability is received.
2049 * This is exported for unit-test purposes
2052 bgp_capability_receive (struct peer
*peer
, bgp_size_t size
)
2056 /* Fetch pointer. */
2057 pnt
= stream_pnt (peer
->ibuf
);
2059 if (bgp_debug_neighbor_events(peer
))
2060 zlog_debug ("%s rcv CAPABILITY", peer
->host
);
2062 /* If peer does not have the capability, send notification. */
2063 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_ADV
))
2065 zlog_err ("%s [Error] BGP dynamic capability is not enabled",
2067 bgp_notify_send (peer
,
2068 BGP_NOTIFY_HEADER_ERR
,
2069 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2073 /* Status must be Established. */
2074 if (peer
->status
!= Established
)
2076 zlog_err ("%s [Error] Dynamic capability packet received under status %s",
2077 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
2078 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
2083 return bgp_capability_msg_parse (peer
, pnt
, size
);
2086 /* BGP read utility function. */
2088 bgp_read_packet (struct peer
*peer
)
2093 readsize
= peer
->packet_size
- stream_get_endp (peer
->ibuf
);
2095 /* If size is zero then return. */
2099 /* Read packet from fd. */
2100 nbytes
= stream_read_try (peer
->ibuf
, peer
->fd
, readsize
);
2102 /* If read byte is smaller than zero then error occured. */
2105 /* Transient error should retry */
2109 zlog_err ("%s [Error] bgp_read_packet error: %s",
2110 peer
->host
, safe_strerror (errno
));
2112 if (peer
->status
== Established
)
2114 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
);
2120 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2123 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
2127 /* When read byte is zero : clear bgp peer and return */
2130 if (bgp_debug_neighbor_events(peer
))
2131 zlog_debug ("%s [Event] BGP connection closed fd %d",
2132 peer
->host
, peer
->fd
);
2134 if (peer
->status
== Established
)
2136 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2138 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2139 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2142 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2145 BGP_EVENT_ADD (peer
, TCP_connection_closed
);
2149 /* We read partial packet. */
2150 if (stream_get_endp (peer
->ibuf
) != peer
->packet_size
)
2158 bgp_marker_all_one (struct stream
*s
, int length
)
2162 for (i
= 0; i
< length
; i
++)
2163 if (s
->data
[i
] != 0xff)
2169 /* Starting point of packet process function. */
2171 bgp_read (struct thread
*thread
)
2177 char notify_data_length
[2];
2178 u_int32_t notify_out
;
2180 /* Yes first of all get peer pointer. */
2181 peer
= THREAD_ARG (thread
);
2182 peer
->t_read
= NULL
;
2184 /* Note notify_out so we can check later to see if we sent another one */
2185 notify_out
= peer
->notify_out
;
2187 /* For non-blocking IO check. */
2188 if (peer
->status
== Connect
)
2190 bgp_connect_check (peer
, 1);
2197 zlog_err ("bgp_read peer's fd is negative value %d", peer
->fd
);
2200 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
2203 /* Read packet header to determine type of the packet */
2204 if (peer
->packet_size
== 0)
2205 peer
->packet_size
= BGP_HEADER_SIZE
;
2207 if (stream_get_endp (peer
->ibuf
) < BGP_HEADER_SIZE
)
2209 ret
= bgp_read_packet (peer
);
2211 /* Header read error or partial read packet. */
2215 /* Get size and type. */
2216 stream_forward_getp (peer
->ibuf
, BGP_MARKER_SIZE
);
2217 memcpy (notify_data_length
, stream_pnt (peer
->ibuf
), 2);
2218 size
= stream_getw (peer
->ibuf
);
2219 type
= stream_getc (peer
->ibuf
);
2222 if (((type
== BGP_MSG_OPEN
) || (type
== BGP_MSG_KEEPALIVE
))
2223 && ! bgp_marker_all_one (peer
->ibuf
, BGP_MARKER_SIZE
))
2225 bgp_notify_send (peer
,
2226 BGP_NOTIFY_HEADER_ERR
,
2227 BGP_NOTIFY_HEADER_NOT_SYNC
);
2231 /* BGP type check. */
2232 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
2233 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
2234 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
2235 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
2236 && type
!= BGP_MSG_CAPABILITY
)
2238 if (bgp_debug_neighbor_events(peer
))
2239 zlog_debug ("%s unknown message type 0x%02x",
2241 bgp_notify_send_with_data (peer
,
2242 BGP_NOTIFY_HEADER_ERR
,
2243 BGP_NOTIFY_HEADER_BAD_MESTYPE
,
2247 /* Mimimum packet length check. */
2248 if ((size
< BGP_HEADER_SIZE
)
2249 || (size
> BGP_MAX_PACKET_SIZE
)
2250 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
2251 || (type
== BGP_MSG_UPDATE
&& size
< BGP_MSG_UPDATE_MIN_SIZE
)
2252 || (type
== BGP_MSG_NOTIFY
&& size
< BGP_MSG_NOTIFY_MIN_SIZE
)
2253 || (type
== BGP_MSG_KEEPALIVE
&& size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
2254 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2255 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2256 || (type
== BGP_MSG_CAPABILITY
&& size
< BGP_MSG_CAPABILITY_MIN_SIZE
))
2258 if (bgp_debug_neighbor_events(peer
))
2259 zlog_debug ("%s bad message length - %d for %s",
2261 type
== 128 ? "ROUTE-REFRESH" :
2262 bgp_type_str
[(int) type
]);
2263 bgp_notify_send_with_data (peer
,
2264 BGP_NOTIFY_HEADER_ERR
,
2265 BGP_NOTIFY_HEADER_BAD_MESLEN
,
2266 (u_char
*) notify_data_length
, 2);
2270 /* Adjust size to message length. */
2271 peer
->packet_size
= size
;
2274 ret
= bgp_read_packet (peer
);
2278 /* Get size and type again. */
2279 (void)stream_getw_from (peer
->ibuf
, BGP_MARKER_SIZE
);
2280 type
= stream_getc_from (peer
->ibuf
, BGP_MARKER_SIZE
+ 2);
2282 /* BGP packet dump function. */
2283 bgp_dump_packet (peer
, type
, peer
->ibuf
);
2285 size
= (peer
->packet_size
- BGP_HEADER_SIZE
);
2287 /* Read rest of the packet and call each sort of packet routine */
2292 bgp_open_receive (peer
, size
); /* XXX return value ignored! */
2294 case BGP_MSG_UPDATE
:
2295 peer
->readtime
= monotime (NULL
);
2296 bgp_update_receive (peer
, size
);
2298 case BGP_MSG_NOTIFY
:
2299 bgp_notify_receive (peer
, size
);
2301 case BGP_MSG_KEEPALIVE
:
2302 peer
->readtime
= monotime (NULL
);
2303 bgp_keepalive_receive (peer
, size
);
2305 case BGP_MSG_ROUTE_REFRESH_NEW
:
2306 case BGP_MSG_ROUTE_REFRESH_OLD
:
2308 bgp_route_refresh_receive (peer
, size
);
2310 case BGP_MSG_CAPABILITY
:
2311 peer
->dynamic_cap_in
++;
2312 bgp_capability_receive (peer
, size
);
2316 /* If reading this packet caused us to send a NOTIFICATION then store a copy
2317 * of the packet for troubleshooting purposes
2319 if (notify_out
< peer
->notify_out
)
2321 memcpy(peer
->last_reset_cause
, peer
->ibuf
->data
, peer
->packet_size
);
2322 peer
->last_reset_cause_size
= peer
->packet_size
;
2323 notify_out
= peer
->notify_out
;
2326 /* Clear input buffer. */
2327 peer
->packet_size
= 0;
2329 stream_reset (peer
->ibuf
);
2332 /* If reading this packet caused us to send a NOTIFICATION then store a copy
2333 * of the packet for troubleshooting purposes
2335 if (notify_out
< peer
->notify_out
)
2337 memcpy(peer
->last_reset_cause
, peer
->ibuf
->data
, peer
->packet_size
);
2338 peer
->last_reset_cause_size
= peer
->packet_size
;