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 () */
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_dump.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_debug.h"
40 #include "bgpd/bgp_fsm.h"
41 #include "bgpd/bgp_route.h"
42 #include "bgpd/bgp_packet.h"
43 #include "bgpd/bgp_open.h"
44 #include "bgpd/bgp_aspath.h"
45 #include "bgpd/bgp_community.h"
46 #include "bgpd/bgp_ecommunity.h"
47 #include "bgpd/bgp_network.h"
48 #include "bgpd/bgp_mplsvpn.h"
49 #include "bgpd/bgp_advertise.h"
50 #include "bgpd/bgp_vty.h"
51 #include "bgpd/bgp_updgrp.h"
53 int stream_put_prefix (struct stream
*, struct prefix
*);
55 /* Set up BGP packet marker and packet type. */
57 bgp_packet_set_marker (struct stream
*s
, u_char type
)
62 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
63 stream_putc (s
, 0xff);
65 /* Dummy total length. This field is should be filled in later on. */
68 /* BGP packet type. */
69 stream_putc (s
, type
);
71 /* Return current stream size. */
72 return stream_get_endp (s
);
75 /* Set BGP packet header size entry. If size is zero then use current
78 bgp_packet_set_size (struct stream
*s
)
82 /* Preserve current pointer. */
83 cp
= stream_get_endp (s
);
84 stream_putw_at (s
, BGP_MARKER_SIZE
, cp
);
89 /* Add new packet to the peer. */
91 bgp_packet_add (struct peer
*peer
, struct stream
*s
)
93 /* Add packet to the end of list. */
94 stream_fifo_push (peer
->obuf
, s
);
97 /* Free first packet. */
99 bgp_packet_delete (struct peer
*peer
)
101 stream_free (stream_fifo_pop (peer
->obuf
));
104 /* Check file descriptor whether connect is established. */
106 bgp_connect_check (struct peer
*peer
, int change_state
)
112 /* Anyway I have to reset read and write thread. */
113 BGP_READ_OFF (peer
->t_read
);
114 BGP_WRITE_OFF (peer
->t_write
);
116 /* Check file descriptor. */
117 slen
= sizeof (status
);
118 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &status
, &slen
);
120 /* If getsockopt is fail, this is fatal error. */
123 zlog_info ("can't get sockopt for nonblocking connect");
124 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
128 /* When status is 0 then TCP connection is established. */
131 BGP_EVENT_ADD (peer
, TCP_connection_open
);
136 if (bgp_debug_neighbor_events(peer
))
137 zlog_debug ("%s [Event] Connect failed (%s)",
138 peer
->host
, safe_strerror (errno
));
140 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
145 static struct stream
*
146 bgp_update_packet_eor (struct peer
*peer
, afi_t afi
, safi_t safi
)
149 struct stream
*packet
;
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 packet
= stream_dup (s
);
183 bgp_packet_add (peer
, packet
);
188 /* Get next packet to be written. */
189 static struct stream
*
190 bgp_write_packet (struct peer
*peer
)
192 struct stream
*s
= NULL
;
194 struct bpacket
*next_pkt
;
198 s
= stream_fifo_head (peer
->obuf
);
203 * The code beyond this part deals with update packets, proceed only
204 * if peer is Established and updates are not on hold (as part of
205 * update-delay post processing).
207 if (peer
->status
!= Established
)
210 if (peer
->bgp
&& (peer
->bgp
->main_peers_update_hold
||
211 peer
->bgp
->rsclient_peers_update_hold
))
214 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
215 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
217 paf
= peer_af_find (peer
, afi
, safi
);
218 if (!paf
|| !PAF_SUBGRP(paf
))
220 next_pkt
= paf
->next_pkt_to_send
;
222 /* Try to generate a packet for the peer if we are at the end of
223 * the list. Always try to push out WITHDRAWs first. */
224 if (!next_pkt
|| !next_pkt
->buffer
)
226 next_pkt
= subgroup_withdraw_packet(PAF_SUBGRP(paf
));
227 if (!next_pkt
|| !next_pkt
->buffer
)
228 subgroup_update_packet (PAF_SUBGRP(paf
));
229 next_pkt
= paf
->next_pkt_to_send
;
232 /* If we still don't have a packet to send to the peer, then
233 * try to find out out if we have to send eor or if not, skip to
234 * the next AFI, SAFI.
235 * Don't send the EOR prematurely... if the subgroup's coalesce
236 * timer is running, the adjacency-out structure is not created
239 if (!next_pkt
|| !next_pkt
->buffer
)
241 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
243 if (!(PAF_SUBGRP(paf
))->t_coalesce
&&
244 peer
->afc_nego
[afi
][safi
] && peer
->synctime
245 && ! CHECK_FLAG (peer
->af_sflags
[afi
][safi
],
246 PEER_STATUS_EOR_SEND
)
247 && safi
!= SAFI_MPLS_VPN
)
249 SET_FLAG (peer
->af_sflags
[afi
][safi
],
250 PEER_STATUS_EOR_SEND
);
251 return bgp_update_packet_eor (peer
, afi
, safi
);
260 * Found a packet template to send, overwrite packet with appropriate
261 * attributes from peer and advance peer
263 s
= bpacket_reformat_for_peer (next_pkt
, paf
);
264 bpacket_queue_advance_peer (paf
);
265 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
266 zlog_debug ("u%" PRIu64
":s%" PRIu64
"%s send UPDATE len %zu ",
267 PAF_SUBGRP(paf
)->update_group
->id
, PAF_SUBGRP(paf
)->id
,
268 peer
->host
, (stream_get_endp(s
) - stream_get_getp(s
)));
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 unsigned int count
= 0;
354 /* Yes first of all get peer pointer. */
355 peer
= THREAD_ARG (thread
);
356 peer
->t_write
= NULL
;
358 /* For non-blocking IO check. */
359 if (peer
->status
== Connect
)
361 bgp_connect_check (peer
, 1);
365 s
= bgp_write_packet (peer
);
368 bgp_write_proceed_actions (peer
);
372 sockopt_cork (peer
->fd
, 1);
374 oc
= peer
->update_out
;
376 /* Nonblocking write until TCP output buffer is full. */
381 /* Number of bytes to be sent. */
382 writenum
= stream_get_endp (s
) - stream_get_getp (s
);
384 /* Call write() system call. */
385 num
= write (peer
->fd
, STREAM_PNT (s
), writenum
);
388 /* write failed either retry needed or error */
389 if (ERRNO_IO_RETRY(errno
))
392 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
399 stream_forward_getp (s
, num
);
403 /* Retrieve BGP packet type. */
404 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
405 type
= stream_getc (s
);
417 /* Double start timer. */
420 /* Overflow check. */
421 if (peer
->v_start
>= (60 * 2))
422 peer
->v_start
= (60 * 2);
424 /* Flush any existing events */
425 BGP_EVENT_ADD (peer
, BGP_Stop
);
428 case BGP_MSG_KEEPALIVE
:
429 peer
->keepalive_out
++;
431 case BGP_MSG_ROUTE_REFRESH_NEW
:
432 case BGP_MSG_ROUTE_REFRESH_OLD
:
435 case BGP_MSG_CAPABILITY
:
436 peer
->dynamic_cap_out
++;
440 /* OK we send packet so delete it. */
441 bgp_packet_delete (peer
);
443 while (++count
< peer
->bgp
->wpkt_quanta
&&
444 (s
= bgp_write_packet (peer
)) != NULL
);
446 bgp_write_proceed_actions (peer
);
449 /* Update the last write if some updates were written. */
450 if (peer
->update_out
> oc
)
451 peer
->last_write
= bgp_clock ();
453 sockopt_cork (peer
->fd
, 0);
457 /* This is only for sending NOTIFICATION message to neighbor. */
459 bgp_write_notify (struct peer
*peer
)
465 /* There should be at least one packet. */
466 s
= stream_fifo_head (peer
->obuf
);
469 assert (stream_get_endp (s
) >= BGP_HEADER_SIZE
);
471 /* Stop collecting data within the socket */
472 sockopt_cork (peer
->fd
, 0);
474 /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
475 * we only care about getting a clean shutdown at this point. */
476 ret
= write (peer
->fd
, STREAM_DATA (s
), stream_get_endp (s
));
478 /* only connection reset/close gets counted as TCP_fatal_error, failure
479 * to write the entire NOTIFY doesn't get different FSM treatment */
482 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
486 /* Disable Nagle, make NOTIFY packet go out right away */
488 (void) setsockopt (peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
489 (char *) &val
, sizeof (val
));
491 /* Retrieve BGP packet type. */
492 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
493 type
= stream_getc (s
);
495 assert (type
== BGP_MSG_NOTIFY
);
497 /* Type should be notify. */
500 /* Double start timer. */
503 /* Overflow check. */
504 if (peer
->v_start
>= (60 * 2))
505 peer
->v_start
= (60 * 2);
507 /* Handle Graceful Restart case where the state changes to
508 Connect instead of Idle */
509 BGP_EVENT_ADD (peer
, BGP_Stop
);
514 /* Make keepalive packet and send it to the peer. */
516 bgp_keepalive_send (struct peer
*peer
)
520 s
= stream_new (BGP_MAX_PACKET_SIZE
);
522 /* Make keepalive packet. */
523 bgp_packet_set_marker (s
, BGP_MSG_KEEPALIVE
);
525 /* Set packet size. */
526 (void)bgp_packet_set_size (s
);
528 /* Dump packet if debug option is set. */
529 /* bgp_packet_dump (s); */
531 if (bgp_debug_keepalive(peer
))
532 zlog_debug ("%s sending KEEPALIVE", peer
->host
);
534 /* Add packet to the peer. */
535 bgp_packet_add (peer
, s
);
537 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
540 /* Make open packet and send it to the peer. */
542 bgp_open_send (struct peer
*peer
)
545 u_int16_t send_holdtime
;
548 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
549 send_holdtime
= peer
->holdtime
;
551 send_holdtime
= peer
->bgp
->default_holdtime
;
553 /* local-as Change */
554 if (peer
->change_local_as
)
555 local_as
= peer
->change_local_as
;
557 local_as
= peer
->local_as
;
559 s
= stream_new (BGP_MAX_PACKET_SIZE
);
561 /* Make open packet. */
562 bgp_packet_set_marker (s
, BGP_MSG_OPEN
);
564 /* Set open packet values. */
565 stream_putc (s
, BGP_VERSION_4
); /* BGP version */
566 stream_putw (s
, (local_as
<= BGP_AS_MAX
) ? (u_int16_t
) local_as
568 stream_putw (s
, send_holdtime
); /* Hold Time */
569 stream_put_in_addr (s
, &peer
->local_id
); /* BGP Identifier */
571 /* Set capability code. */
572 bgp_open_capability (s
, peer
);
574 /* Set BGP packet length. */
575 (void)bgp_packet_set_size (s
);
577 if (bgp_debug_neighbor_events(peer
))
578 zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
579 peer
->host
, BGP_VERSION_4
, local_as
,
580 send_holdtime
, inet_ntoa (peer
->local_id
));
582 /* Dump packet if debug option is set. */
583 /* bgp_packet_dump (s); */
585 /* Add packet to the peer. */
586 bgp_packet_add (peer
, s
);
588 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
591 /* Send BGP notify packet with data potion. */
593 bgp_notify_send_with_data (struct peer
*peer
, u_char code
, u_char sub_code
,
594 u_char
*data
, size_t datalen
)
599 /* Allocate new stream. */
600 s
= stream_new (BGP_MAX_PACKET_SIZE
);
602 /* Make nitify packet. */
603 bgp_packet_set_marker (s
, BGP_MSG_NOTIFY
);
605 /* Set notify packet values. */
606 stream_putc (s
, code
); /* BGP notify code */
607 stream_putc (s
, sub_code
); /* BGP notify sub_code */
609 /* If notify data is present. */
611 stream_write (s
, data
, datalen
);
613 /* Set BGP packet length. */
614 length
= bgp_packet_set_size (s
);
616 /* Add packet to the peer. */
617 stream_fifo_clean (peer
->obuf
);
618 bgp_packet_add (peer
, s
);
622 struct bgp_notify bgp_notify
;
627 bgp_notify
.code
= code
;
628 bgp_notify
.subcode
= sub_code
;
629 bgp_notify
.data
= NULL
;
630 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
632 if (bgp_notify
.length
)
634 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
635 for (i
= 0; i
< bgp_notify
.length
; i
++)
638 sprintf (c
, " %02x", data
[i
]);
639 strcat (bgp_notify
.data
, c
);
644 sprintf (c
, "%02x", data
[i
]);
645 strcpy (bgp_notify
.data
, c
);
648 bgp_notify_print (peer
, &bgp_notify
, "sending");
650 XFREE (MTYPE_TMP
, bgp_notify
.data
);
653 /* peer reset cause */
654 if (sub_code
!= BGP_NOTIFY_CEASE_CONFIG_CHANGE
)
656 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
657 peer
->last_reset
= PEER_DOWN_USER_RESET
;
658 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
659 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
661 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
664 /* Call immediately. */
665 BGP_WRITE_OFF (peer
->t_write
);
667 bgp_write_notify (peer
);
670 /* Send BGP notify packet. */
672 bgp_notify_send (struct peer
*peer
, u_char code
, u_char sub_code
)
674 bgp_notify_send_with_data (peer
, code
, sub_code
, NULL
, 0);
677 /* Send route refresh message to the peer. */
679 bgp_route_refresh_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
680 u_char orf_type
, u_char when_to_refresh
, int remove
)
683 struct stream
*packet
;
684 struct bgp_filter
*filter
;
687 if (DISABLE_BGP_ANNOUNCE
)
690 filter
= &peer
->filter
[afi
][safi
];
692 /* Adjust safi code. */
693 if (safi
== SAFI_MPLS_VPN
)
694 safi
= SAFI_MPLS_LABELED_VPN
;
696 s
= stream_new (BGP_MAX_PACKET_SIZE
);
698 /* Make BGP update packet. */
699 if (CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
700 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_NEW
);
702 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_OLD
);
704 /* Encode Route Refresh message. */
705 stream_putw (s
, afi
);
707 stream_putc (s
, safi
);
709 if (orf_type
== ORF_TYPE_PREFIX
710 || orf_type
== ORF_TYPE_PREFIX_OLD
)
711 if (remove
|| filter
->plist
[FILTER_IN
].plist
)
717 stream_putc (s
, when_to_refresh
);
718 stream_putc (s
, orf_type
);
719 orfp
= stream_get_endp (s
);
724 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
725 stream_putc (s
, ORF_COMMON_PART_REMOVE_ALL
);
726 if (bgp_debug_neighbor_events(peer
))
727 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
728 peer
->host
, orf_type
,
729 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
734 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
735 prefix_bgp_orf_entry (s
, filter
->plist
[FILTER_IN
].plist
,
736 ORF_COMMON_PART_ADD
, ORF_COMMON_PART_PERMIT
,
737 ORF_COMMON_PART_DENY
);
738 if (bgp_debug_neighbor_events(peer
))
739 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
740 peer
->host
, orf_type
,
741 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
745 /* Total ORF Entry Len. */
746 orf_len
= stream_get_endp (s
) - orfp
- 2;
747 stream_putw_at (s
, orfp
, orf_len
);
750 /* Set packet size. */
751 (void)bgp_packet_set_size (s
);
753 if (bgp_debug_neighbor_events(peer
))
756 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
757 peer
->host
, afi
, safi
);
760 /* Make real packet. */
761 packet
= stream_dup (s
);
764 /* Add packet to the peer. */
765 bgp_packet_add (peer
, packet
);
767 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
770 /* Send capability message to the peer. */
772 bgp_capability_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
773 int capability_code
, int action
)
776 struct stream
*packet
;
778 /* Adjust safi code. */
779 if (safi
== SAFI_MPLS_VPN
)
780 safi
= SAFI_MPLS_LABELED_VPN
;
782 s
= stream_new (BGP_MAX_PACKET_SIZE
);
784 /* Make BGP update packet. */
785 bgp_packet_set_marker (s
, BGP_MSG_CAPABILITY
);
787 /* Encode MP_EXT capability. */
788 if (capability_code
== CAPABILITY_CODE_MP
)
790 stream_putc (s
, action
);
791 stream_putc (s
, CAPABILITY_CODE_MP
);
792 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
793 stream_putw (s
, afi
);
795 stream_putc (s
, safi
);
797 if (bgp_debug_neighbor_events(peer
))
798 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
799 peer
->host
, action
== CAPABILITY_ACTION_SET
?
800 "Advertising" : "Removing", afi
, safi
);
803 /* Set packet size. */
804 (void)bgp_packet_set_size (s
);
806 /* Make real packet. */
807 packet
= stream_dup (s
);
810 /* Add packet to the peer. */
811 bgp_packet_add (peer
, packet
);
813 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
816 /* RFC1771 6.8 Connection collision detection. */
818 bgp_collision_detect (struct peer
*new, struct in_addr remote_id
)
822 /* Upon receipt of an OPEN message, the local system must examine
823 all of its connections that are in the OpenConfirm state. A BGP
824 speaker may also examine connections in an OpenSent state if it
825 knows the BGP Identifier of the peer by means outside of the
826 protocol. If among these connections there is a connection to a
827 remote BGP speaker whose BGP Identifier equals the one in the
828 OPEN message, then the local system performs the following
829 collision resolution procedure: */
831 if ((peer
= new->doppelganger
) != NULL
)
833 /* Do not accept the new connection in Established or Clearing states.
834 * Note that a peer GR is handled by closing the existing connection
835 * upon receipt of new one.
837 if (peer
->status
== Established
|| peer
->status
== Clearing
)
839 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
840 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
843 else if ((peer
->status
== OpenConfirm
) || (peer
->status
== OpenSent
))
845 /* 1. The BGP Identifier of the local system is compared to
846 the BGP Identifier of the remote system (as specified in
847 the OPEN message). */
849 if (ntohl (peer
->local_id
.s_addr
) < ntohl (remote_id
.s_addr
))
850 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
852 /* 2. If the value of the local BGP Identifier is less
853 than the remote one, the local system closes BGP
854 connection that already exists (the one that is
855 already in the OpenConfirm state), and accepts BGP
856 connection initiated by the remote system. */
857 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
858 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
863 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
864 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
869 /* 3. Otherwise, the local system closes newly created
870 BGP connection (the one associated with the newly
871 received OPEN message), and continues to use the
872 existing one (the one that is already in the
873 OpenConfirm state). */
874 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
876 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
877 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
882 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
883 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
893 bgp_open_receive (struct peer
*peer
, bgp_size_t size
)
899 u_int16_t send_holdtime
;
902 struct in_addr remote_id
;
904 u_int8_t notify_data_remote_as
[2];
905 u_int8_t notify_data_remote_as4
[4];
906 u_int8_t notify_data_remote_id
[4];
907 u_int16_t
*holdtime_ptr
;
909 /* Parse open packet. */
910 version
= stream_getc (peer
->ibuf
);
911 memcpy (notify_data_remote_as
, stream_pnt (peer
->ibuf
), 2);
912 remote_as
= stream_getw (peer
->ibuf
);
913 holdtime_ptr
= (u_int16_t
*)stream_pnt (peer
->ibuf
);
914 holdtime
= stream_getw (peer
->ibuf
);
915 memcpy (notify_data_remote_id
, stream_pnt (peer
->ibuf
), 4);
916 remote_id
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
918 /* Receive OPEN message log */
919 if (bgp_debug_neighbor_events(peer
))
920 zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
921 " holdtime %d, id %s",
922 peer
->host
, version
, remote_as
, holdtime
,
923 inet_ntoa (remote_id
));
925 /* BEGIN to read the capability here, but dont do it yet */
927 optlen
= stream_getc (peer
->ibuf
);
931 /* We need the as4 capability value *right now* because
932 * if it is there, we have not got the remote_as yet, and without
933 * that we do not know which peer is connecting to us now.
935 as4
= peek_for_as4_capability (peer
, optlen
);
936 memcpy (notify_data_remote_as4
, &as4
, 4);
939 /* Just in case we have a silly peer who sends AS4 capability set to 0 */
940 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
)
942 zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
944 bgp_notify_send_with_data (peer
,
946 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
947 notify_data_remote_as4
, 4);
951 if (remote_as
== BGP_AS_TRANS
)
953 /* Take the AS4 from the capability. We must have received the
954 * capability now! Otherwise we have a asn16 peer who uses
955 * BGP_AS_TRANS, for some unknown reason.
957 if (as4
== BGP_AS_TRANS
)
959 zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
961 bgp_notify_send_with_data (peer
,
963 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
964 notify_data_remote_as4
, 4);
968 if (!as4
&& BGP_DEBUG (as4
, AS4
))
969 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
970 " Odd, but proceeding.", peer
->host
);
971 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG (as4
, AS4
))
972 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
973 "in 2-bytes, very odd peer.", peer
->host
, as4
);
979 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
980 /* If we have got the capability, peer->as4cap must match remote_as */
981 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
)
984 /* raise error, log this, close session */
985 zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
986 " mismatch with 16bit 'myasn' %u in open",
987 peer
->host
, as4
, remote_as
);
988 bgp_notify_send_with_data (peer
,
990 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
991 notify_data_remote_as4
, 4);
996 /* remote router-id check. */
997 if (remote_id
.s_addr
== 0
998 || IPV4_CLASS_DE (ntohl (remote_id
.s_addr
))
999 || ntohl (peer
->local_id
.s_addr
) == ntohl (remote_id
.s_addr
))
1001 if (bgp_debug_neighbor_events(peer
))
1002 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1003 peer
->host
, inet_ntoa (remote_id
));
1004 bgp_notify_send_with_data (peer
,
1005 BGP_NOTIFY_OPEN_ERR
,
1006 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1007 notify_data_remote_id
, 4);
1011 /* Set remote router-id */
1012 peer
->remote_id
= remote_id
;
1014 /* Peer BGP version check. */
1015 if (version
!= BGP_VERSION_4
)
1017 u_int16_t maxver
= htons(BGP_VERSION_4
);
1018 /* XXX this reply may not be correct if version < 4 XXX */
1019 if (bgp_debug_neighbor_events(peer
))
1020 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
1021 peer
->host
, version
, BGP_VERSION_4
);
1022 /* Data must be in network byte order here */
1023 bgp_notify_send_with_data (peer
,
1024 BGP_NOTIFY_OPEN_ERR
,
1025 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1026 (u_int8_t
*) &maxver
, 2);
1030 /* Check neighbor as number. */
1031 if (peer
->as_type
== AS_INTERNAL
)
1033 if (remote_as
!= peer
->bgp
->as
)
1035 if (bgp_debug_neighbor_events(peer
))
1036 zlog_debug ("%s bad OPEN, remote AS is %u, internal specified",
1037 peer
->host
, remote_as
);
1038 bgp_notify_send_with_data (peer
,
1039 BGP_NOTIFY_OPEN_ERR
,
1040 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1041 notify_data_remote_as
, 2);
1044 peer
->as
= peer
->local_as
;
1046 else if (peer
->as_type
== AS_EXTERNAL
)
1048 if (remote_as
== peer
->bgp
->as
)
1050 if (bgp_debug_neighbor_events(peer
))
1051 zlog_debug ("%s bad OPEN, remote AS is %u, external specified",
1052 peer
->host
, remote_as
);
1053 bgp_notify_send_with_data (peer
,
1054 BGP_NOTIFY_OPEN_ERR
,
1055 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1056 notify_data_remote_as
, 2);
1059 peer
->as
= remote_as
;
1061 else if ((peer
->as_type
== AS_SPECIFIED
) && (remote_as
!= peer
->as
))
1063 if (bgp_debug_neighbor_events(peer
))
1064 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1065 peer
->host
, remote_as
, peer
->as
);
1066 bgp_notify_send_with_data (peer
,
1067 BGP_NOTIFY_OPEN_ERR
,
1068 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1069 notify_data_remote_as
, 2);
1073 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1074 calculate the value of the Hold Timer by using the smaller of its
1075 configured Hold Time and the Hold Time received in the OPEN message.
1076 The Hold Time MUST be either zero or at least three seconds. An
1077 implementation may reject connections on the basis of the Hold Time. */
1079 if (holdtime
< 3 && holdtime
!= 0)
1081 bgp_notify_send_with_data (peer
,
1082 BGP_NOTIFY_OPEN_ERR
,
1083 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
,
1084 (u_char
*)holdtime_ptr
, 2);
1088 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1089 would be one third of the Hold Time interval. KEEPALIVE messages
1090 MUST NOT be sent more frequently than one per second. An
1091 implementation MAY adjust the rate at which it sends KEEPALIVE
1092 messages as a function of the Hold Time interval. */
1094 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1095 send_holdtime
= peer
->holdtime
;
1097 send_holdtime
= peer
->bgp
->default_holdtime
;
1099 if (holdtime
< send_holdtime
)
1100 peer
->v_holdtime
= holdtime
;
1102 peer
->v_holdtime
= send_holdtime
;
1104 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1106 /* Open option part parse. */
1109 if ((ret
= bgp_open_option_parse (peer
, optlen
, &mp_capability
)) < 0)
1111 bgp_notify_send (peer
,
1112 BGP_NOTIFY_OPEN_ERR
,
1113 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
);
1119 if (bgp_debug_neighbor_events(peer
))
1120 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
1125 * Assume that the peer supports the locally configured set of
1126 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1127 * capabilities, or if 'override-capability' is configured.
1129 if (! mp_capability
||
1130 CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
1132 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] = peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1133 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1134 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] = peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1135 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1138 /* When collision is detected and this peer is closed. Retrun
1140 ret
= bgp_collision_detect (peer
, remote_id
);
1145 if ((ret
= bgp_getsockname (peer
)) < 0)
1147 zlog_err("%s: bgp_getsockname() failed for peer: %s", __FUNCTION__
,
1152 if ((ret
= bgp_event_update(peer
, Receive_OPEN_message
)) < 0)
1154 zlog_err("%s: BGP event update failed for peer: %s", __FUNCTION__
,
1156 /* DD: bgp send notify and reset state */
1160 peer
->packet_size
= 0;
1162 stream_reset (peer
->ibuf
);
1167 /* Called when there is a change in the EOR(implicit or explicit) status of a peer.
1168 Ends the update-delay if all expected peers are done with EORs. */
1170 bgp_check_update_delay(struct bgp
*bgp
)
1172 struct listnode
*node
, *nnode
;
1173 struct peer
*peer
= NULL
;
1175 if (bgp_debug_neighbor_events(peer
))
1176 zlog_debug ("Checking update delay, T: %d R: %d I:%d E: %d", bgp
->established
,
1177 bgp
->restarted_peers
, bgp
->implicit_eors
, bgp
->explicit_eors
);
1179 if (bgp
->established
<=
1180 bgp
->restarted_peers
+ bgp
->implicit_eors
+ bgp
->explicit_eors
)
1182 /* This is an extra sanity check to make sure we wait for all the
1183 eligible configured peers. This check is performed if establish wait
1184 timer is on, or establish wait option is not given with the
1185 update-delay command */
1186 if (bgp
->t_establish_wait
||
1187 (bgp
->v_establish_wait
== bgp
->v_update_delay
))
1188 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
1190 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1191 && !CHECK_FLAG (peer
->flags
, PEER_FLAG_SHUTDOWN
)
1192 && !peer
->update_delay_over
)
1194 if (bgp_debug_neighbor_events(peer
))
1195 zlog_debug (" Peer %s pending, continuing read-only mode",
1201 zlog_info ("Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
1202 bgp
->restarted_peers
, bgp
->implicit_eors
, bgp
->explicit_eors
);
1203 bgp_update_delay_end(bgp
);
1207 /* Called if peer is known to have restarted. The restart-state bit in
1208 Graceful-Restart capability is used for that */
1210 bgp_update_restarted_peers (struct peer
*peer
)
1212 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1213 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1215 if (bgp_debug_neighbor_events(peer
))
1216 zlog_debug ("Peer %s: Checking restarted", peer
->host
);
1218 if (peer
->status
== Established
)
1220 peer
->update_delay_over
= 1;
1221 peer
->bgp
->restarted_peers
++;
1222 bgp_check_update_delay(peer
->bgp
);
1226 /* Called as peer receives a keep-alive. Determines if this occurence can be
1227 taken as an implicit EOR for this peer.
1228 NOTE: The very first keep-alive after the Established state of a peer is
1229 considered implicit EOR for the update-delay purposes */
1231 bgp_update_implicit_eors (struct peer
*peer
)
1233 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1234 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1236 if (bgp_debug_neighbor_events(peer
))
1237 zlog_debug ("Peer %s: Checking implicit EORs", peer
->host
);
1239 if (peer
->status
== Established
)
1241 peer
->update_delay_over
= 1;
1242 peer
->bgp
->implicit_eors
++;
1243 bgp_check_update_delay(peer
->bgp
);
1247 /* Should be called only when there is a change in the EOR_RECEIVED status
1248 for any afi/safi on a peer */
1250 bgp_update_explicit_eors (struct peer
*peer
)
1255 if (!bgp_update_delay_active(peer
->bgp
)) return; /* BGP update delay has ended */
1256 if (peer
->update_delay_over
) return; /* This peer has already been considered */
1258 if (bgp_debug_neighbor_events(peer
))
1259 zlog_debug ("Peer %s: Checking explicit EORs", peer
->host
);
1261 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1262 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1264 if (peer
->afc_nego
[afi
][safi
] &&
1265 !CHECK_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_RECEIVED
))
1267 if (bgp_debug_neighbor_events(peer
))
1268 zlog_debug (" afi %d safi %d didnt receive EOR", afi
, safi
);
1273 peer
->update_delay_over
= 1;
1274 peer
->bgp
->explicit_eors
++;
1275 bgp_check_update_delay(peer
->bgp
);
1278 /* Parse BGP Update packet and make attribute object. */
1280 bgp_update_receive (struct peer
*peer
, bgp_size_t size
)
1286 struct attr_extra extra
;
1287 bgp_size_t attribute_len
;
1288 bgp_size_t update_len
;
1289 bgp_size_t withdraw_len
;
1290 struct bgp_nlri update
;
1291 struct bgp_nlri withdraw
;
1292 struct bgp_nlri mp_update
;
1293 struct bgp_nlri mp_withdraw
;
1294 int num_pfx_adv
, num_pfx_wd
;
1296 /* Status must be Established. */
1297 if (peer
->status
!= Established
)
1299 zlog_err ("%s [FSM] Update packet received under status %s",
1300 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1301 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1305 /* Set initial values. */
1306 memset (&attr
, 0, sizeof (struct attr
));
1307 memset (&extra
, 0, sizeof (struct attr_extra
));
1308 memset (&update
, 0, sizeof (struct bgp_nlri
));
1309 memset (&withdraw
, 0, sizeof (struct bgp_nlri
));
1310 memset (&mp_update
, 0, sizeof (struct bgp_nlri
));
1311 memset (&mp_withdraw
, 0, sizeof (struct bgp_nlri
));
1312 attr
.extra
= &extra
;
1313 num_pfx_adv
= num_pfx_wd
= 0;
1314 memset (peer
->rcvd_attr_str
, 0, BUFSIZ
);
1315 peer
->rcvd_attr_printed
= 0;
1318 end
= stream_pnt (s
) + size
;
1320 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1321 Length is too large (i.e., if Unfeasible Routes Length + Total
1322 Attribute Length + 23 exceeds the message Length), then the Error
1323 Subcode is set to Malformed Attribute List. */
1324 if (stream_pnt (s
) + 2 > end
)
1326 zlog_err ("%s [Error] Update packet error"
1327 " (packet length is short for unfeasible length)",
1329 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1330 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1334 /* Unfeasible Route Length. */
1335 withdraw_len
= stream_getw (s
);
1337 /* Unfeasible Route Length check. */
1338 if (stream_pnt (s
) + withdraw_len
> end
)
1340 zlog_err ("%s [Error] Update packet error"
1341 " (packet unfeasible length overflow %d)",
1342 peer
->host
, withdraw_len
);
1343 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1344 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1348 /* Unfeasible Route packet format check. */
1349 if (withdraw_len
> 0)
1351 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, SAFI_UNICAST
, stream_pnt (s
),
1352 withdraw_len
, &num_pfx_wd
);
1356 withdraw
.afi
= AFI_IP
;
1357 withdraw
.safi
= SAFI_UNICAST
;
1358 withdraw
.nlri
= stream_pnt (s
);
1359 withdraw
.length
= withdraw_len
;
1360 stream_forward_getp (s
, withdraw_len
);
1363 /* Attribute total length check. */
1364 if (stream_pnt (s
) + 2 > end
)
1366 zlog_warn ("%s [Error] Packet Error"
1367 " (update packet is short for attribute length)",
1369 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1370 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1374 /* Fetch attribute total length. */
1375 attribute_len
= stream_getw (s
);
1377 /* Attribute length check. */
1378 if (stream_pnt (s
) + attribute_len
> end
)
1380 zlog_warn ("%s [Error] Packet Error"
1381 " (update packet attribute length overflow %d)",
1382 peer
->host
, attribute_len
);
1383 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1384 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1388 /* Certain attribute parsing errors should not be considered bad enough
1389 * to reset the session for, most particularly any partial/optional
1390 * attributes that have 'tunneled' over speakers that don't understand
1391 * them. Instead we withdraw only the prefix concerned.
1393 * Complicates the flow a little though..
1395 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1396 /* This define morphs the update case into a withdraw when lower levels
1397 * have signalled an error condition where this is best.
1399 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1401 /* Parse attribute when it exists. */
1404 attr_parse_ret
= bgp_attr_parse (peer
, &attr
, attribute_len
,
1405 &mp_update
, &mp_withdraw
);
1406 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
)
1408 bgp_attr_unintern_sub (&attr
);
1413 /* Logging the attribute. */
1414 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
||
1415 BGP_DEBUG (update
, UPDATE_IN
) ||
1416 BGP_DEBUG (update
, UPDATE_PREFIX
))
1418 ret
= bgp_dump_attr (peer
, &attr
, peer
->rcvd_attr_str
, BUFSIZ
);
1420 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1421 zlog_err ("%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1424 if (ret
&& bgp_debug_update(peer
, NULL
, NULL
, 1))
1426 zlog_debug ("%s rcvd UPDATE w/ attr: %s", peer
->host
, peer
->rcvd_attr_str
);
1427 peer
->rcvd_attr_printed
= 1;
1431 /* Network Layer Reachability Information. */
1432 update_len
= end
- stream_pnt (s
);
1436 /* Check NLRI packet format and prefix length. */
1437 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, SAFI_UNICAST
, stream_pnt (s
),
1438 update_len
, &num_pfx_adv
);
1441 bgp_attr_unintern_sub (&attr
);
1445 /* Set NLRI portion to structure. */
1446 update
.afi
= AFI_IP
;
1447 update
.safi
= SAFI_UNICAST
;
1448 update
.nlri
= stream_pnt (s
);
1449 update
.length
= update_len
;
1450 stream_forward_getp (s
, update_len
);
1453 if (BGP_DEBUG (update
, UPDATE_IN
))
1454 zlog_debug("%s rcvd UPDATE wlen %d wpfx %d attrlen %d alen %d apfx %d",
1455 peer
->host
, withdraw_len
, num_pfx_wd
, attribute_len
,
1456 update_len
, num_pfx_adv
);
1458 /* NLRI is processed only when the peer is configured specific
1459 Address Family and Subsequent Address Family. */
1460 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
])
1462 if (withdraw
.length
)
1463 bgp_nlri_parse (peer
, NULL
, &withdraw
);
1467 /* We check well-known attribute only for IPv4 unicast
1469 ret
= bgp_attr_check (peer
, &attr
);
1472 bgp_attr_unintern_sub (&attr
);
1476 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &update
);
1479 if (mp_update
.length
1480 && mp_update
.afi
== AFI_IP
1481 && mp_update
.safi
== SAFI_UNICAST
)
1482 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &mp_update
);
1484 if (mp_withdraw
.length
1485 && mp_withdraw
.afi
== AFI_IP
1486 && mp_withdraw
.safi
== SAFI_UNICAST
)
1487 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1489 if (! attribute_len
&& ! withdraw_len
)
1491 /* End-of-RIB received */
1492 if (!CHECK_FLAG(peer
->af_sflags
[AFI_IP
][SAFI_UNICAST
],
1493 PEER_STATUS_EOR_RECEIVED
))
1495 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_UNICAST
],
1496 PEER_STATUS_EOR_RECEIVED
);
1497 bgp_update_explicit_eors(peer
);
1500 /* NSF delete stale route */
1501 if (peer
->nsf
[AFI_IP
][SAFI_UNICAST
])
1502 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_UNICAST
);
1504 if (bgp_debug_neighbor_events(peer
))
1505 zlog_debug ("rcvd End-of-RIB for IPv4 Unicast from %s", peer
->host
);
1508 if (peer
->afc
[AFI_IP
][SAFI_MULTICAST
])
1510 if (mp_update
.length
1511 && mp_update
.afi
== AFI_IP
1512 && mp_update
.safi
== SAFI_MULTICAST
)
1513 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &mp_update
);
1515 if (mp_withdraw
.length
1516 && mp_withdraw
.afi
== AFI_IP
1517 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1518 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1521 && mp_withdraw
.afi
== AFI_IP
1522 && mp_withdraw
.safi
== SAFI_MULTICAST
1523 && mp_withdraw
.length
== 0)
1525 /* End-of-RIB received */
1526 if (!CHECK_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_MULTICAST
],
1527 PEER_STATUS_EOR_RECEIVED
))
1529 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_MULTICAST
],
1530 PEER_STATUS_EOR_RECEIVED
);
1531 bgp_update_explicit_eors(peer
);
1534 /* NSF delete stale route */
1535 if (peer
->nsf
[AFI_IP
][SAFI_MULTICAST
])
1536 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_MULTICAST
);
1538 if (bgp_debug_neighbor_events(peer
))
1539 zlog_debug ("rcvd End-of-RIB for IPv4 Multicast from %s", peer
->host
);
1542 if (peer
->afc
[AFI_IP6
][SAFI_UNICAST
])
1544 if (mp_update
.length
1545 && mp_update
.afi
== AFI_IP6
1546 && mp_update
.safi
== SAFI_UNICAST
)
1547 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &mp_update
);
1549 if (mp_withdraw
.length
1550 && mp_withdraw
.afi
== AFI_IP6
1551 && mp_withdraw
.safi
== SAFI_UNICAST
)
1552 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1555 && mp_withdraw
.afi
== AFI_IP6
1556 && mp_withdraw
.safi
== SAFI_UNICAST
1557 && mp_withdraw
.length
== 0)
1559 /* End-of-RIB received */
1560 if (!CHECK_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_UNICAST
],
1561 PEER_STATUS_EOR_RECEIVED
))
1563 SET_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_UNICAST
], PEER_STATUS_EOR_RECEIVED
);
1564 bgp_update_explicit_eors(peer
);
1567 /* NSF delete stale route */
1568 if (peer
->nsf
[AFI_IP6
][SAFI_UNICAST
])
1569 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_UNICAST
);
1571 if (bgp_debug_neighbor_events(peer
))
1572 zlog_debug ("rcvd End-of-RIB for IPv6 Unicast from %s", peer
->host
);
1575 if (peer
->afc
[AFI_IP6
][SAFI_MULTICAST
])
1577 if (mp_update
.length
1578 && mp_update
.afi
== AFI_IP6
1579 && mp_update
.safi
== SAFI_MULTICAST
)
1580 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &mp_update
);
1582 if (mp_withdraw
.length
1583 && mp_withdraw
.afi
== AFI_IP6
1584 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1585 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1588 && mp_withdraw
.afi
== AFI_IP6
1589 && mp_withdraw
.safi
== SAFI_MULTICAST
1590 && mp_withdraw
.length
== 0)
1592 /* End-of-RIB received */
1593 if (!CHECK_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_MULTICAST
],
1594 PEER_STATUS_EOR_RECEIVED
))
1596 SET_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_MULTICAST
], PEER_STATUS_EOR_RECEIVED
);
1597 bgp_update_explicit_eors(peer
);
1601 /* NSF delete stale route */
1602 if (peer
->nsf
[AFI_IP6
][SAFI_MULTICAST
])
1603 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_MULTICAST
);
1605 if (bgp_debug_neighbor_events(peer
))
1606 zlog_debug ("rcvd End-of-RIB for IPv6 Multicast from %s", peer
->host
);
1609 if (peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
])
1611 if (mp_update
.length
1612 && mp_update
.afi
== AFI_IP
1613 && mp_update
.safi
== SAFI_MPLS_LABELED_VPN
)
1614 bgp_nlri_parse_vpnv4 (peer
, NLRI_ATTR_ARG
, &mp_update
);
1616 if (mp_withdraw
.length
1617 && mp_withdraw
.afi
== AFI_IP
1618 && mp_withdraw
.safi
== SAFI_MPLS_LABELED_VPN
)
1619 bgp_nlri_parse_vpnv4 (peer
, NULL
, &mp_withdraw
);
1622 && mp_withdraw
.afi
== AFI_IP
1623 && mp_withdraw
.safi
== SAFI_MPLS_LABELED_VPN
1624 && mp_withdraw
.length
== 0)
1627 /* End-of-RIB received */
1628 if (!CHECK_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_MPLS_VPN
],
1629 PEER_STATUS_EOR_RECEIVED
))
1631 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_MPLS_VPN
], PEER_STATUS_EOR_RECEIVED
);
1632 bgp_update_explicit_eors(peer
);
1635 if (bgp_debug_neighbor_events(peer
))
1636 zlog_debug ("rcvd End-of-RIB for VPNv4 Unicast from %s", peer
->host
);
1640 /* Everything is done. We unintern temporary structures which
1641 interned in bgp_attr_parse(). */
1642 bgp_attr_unintern_sub (&attr
);
1644 /* If peering is stopped due to some reason, do not generate BGP
1646 if (peer
->status
!= Established
)
1649 /* Increment packet counter. */
1651 peer
->update_time
= bgp_clock ();
1653 /* Rearm holdtime timer */
1654 BGP_TIMER_OFF (peer
->t_holdtime
);
1655 bgp_timer_set (peer
);
1660 /* Notify message treatment function. */
1662 bgp_notify_receive (struct peer
*peer
, bgp_size_t size
)
1664 struct bgp_notify bgp_notify
;
1666 if (peer
->notify
.data
)
1668 XFREE (MTYPE_TMP
, peer
->notify
.data
);
1669 peer
->notify
.data
= NULL
;
1670 peer
->notify
.length
= 0;
1673 bgp_notify
.code
= stream_getc (peer
->ibuf
);
1674 bgp_notify
.subcode
= stream_getc (peer
->ibuf
);
1675 bgp_notify
.length
= size
- 2;
1676 bgp_notify
.data
= NULL
;
1678 /* Preserv notify code and sub code. */
1679 peer
->notify
.code
= bgp_notify
.code
;
1680 peer
->notify
.subcode
= bgp_notify
.subcode
;
1681 /* For further diagnostic record returned Data. */
1682 if (bgp_notify
.length
)
1684 peer
->notify
.length
= size
- 2;
1685 peer
->notify
.data
= XMALLOC (MTYPE_TMP
, size
- 2);
1686 memcpy (peer
->notify
.data
, stream_pnt (peer
->ibuf
), size
- 2);
1695 if (bgp_notify
.length
)
1697 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
1698 for (i
= 0; i
< bgp_notify
.length
; i
++)
1701 sprintf (c
, " %02x", stream_getc (peer
->ibuf
));
1702 strcat (bgp_notify
.data
, c
);
1707 sprintf (c
, "%02x", stream_getc (peer
->ibuf
));
1708 strcpy (bgp_notify
.data
, c
);
1712 bgp_notify_print(peer
, &bgp_notify
, "received");
1713 if (bgp_notify
.data
)
1714 XFREE (MTYPE_TMP
, bgp_notify
.data
);
1717 /* peer count update */
1720 if (peer
->status
== Established
)
1721 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1723 /* We have to check for Notify with Unsupported Optional Parameter.
1724 in that case we fallback to open without the capability option.
1725 But this done in bgp_stop. We just mark it here to avoid changing
1727 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1728 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1729 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1731 BGP_EVENT_ADD (peer
, Receive_NOTIFICATION_message
);
1734 /* Keepalive treatment function -- get keepalive send keepalive */
1736 bgp_keepalive_receive (struct peer
*peer
, bgp_size_t size
)
1738 if (bgp_debug_keepalive(peer
))
1739 zlog_debug ("%s KEEPALIVE rcvd", peer
->host
);
1741 BGP_EVENT_ADD (peer
, Receive_KEEPALIVE_message
);
1744 /* Route refresh message is received. */
1746 bgp_route_refresh_receive (struct peer
*peer
, bgp_size_t size
)
1751 struct peer_af
*paf
;
1753 /* If peer does not have the capability, send notification. */
1754 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_ADV
))
1756 zlog_err ("%s [Error] BGP route refresh is not enabled",
1758 bgp_notify_send (peer
,
1759 BGP_NOTIFY_HEADER_ERR
,
1760 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1764 /* Status must be Established. */
1765 if (peer
->status
!= Established
)
1767 zlog_err ("%s [Error] Route refresh packet received under status %s",
1768 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1769 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1776 afi
= stream_getw (s
);
1777 (void)stream_getc (s
);
1778 safi
= stream_getc (s
);
1780 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
1781 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1782 peer
->host
, afi
, safi
);
1784 /* Check AFI and SAFI. */
1785 if ((afi
!= AFI_IP
&& afi
!= AFI_IP6
)
1786 || (safi
!= SAFI_UNICAST
&& safi
!= SAFI_MULTICAST
1787 && safi
!= SAFI_MPLS_LABELED_VPN
))
1789 zlog_info ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1790 peer
->host
, afi
, safi
);
1794 /* Adjust safi code. */
1795 if (safi
== SAFI_MPLS_LABELED_VPN
)
1796 safi
= SAFI_MPLS_VPN
;
1798 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1801 u_char when_to_refresh
;
1805 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) < 5)
1807 zlog_info ("%s ORF route refresh length error", peer
->host
);
1808 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1812 when_to_refresh
= stream_getc (s
);
1813 end
= stream_pnt (s
) + (size
- 5);
1815 while ((stream_pnt (s
) + 2) < end
)
1817 orf_type
= stream_getc (s
);
1818 orf_len
= stream_getw (s
);
1820 /* orf_len in bounds? */
1821 if ((stream_pnt (s
) + orf_len
) > end
)
1822 break; /* XXX: Notify instead?? */
1823 if (orf_type
== ORF_TYPE_PREFIX
1824 || orf_type
== ORF_TYPE_PREFIX_OLD
)
1826 u_char
*p_pnt
= stream_pnt (s
);
1827 u_char
*p_end
= stream_pnt (s
) + orf_len
;
1828 struct orf_prefix orfp
;
1833 int ret
= CMD_SUCCESS
;
1835 if (bgp_debug_neighbor_events(peer
))
1837 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
1838 peer
->host
, orf_type
, orf_len
);
1841 /* we're going to read at least 1 byte of common ORF header,
1842 * and 7 bytes of ORF Address-filter entry from the stream
1847 /* ORF prefix-list name */
1848 sprintf (name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1850 while (p_pnt
< p_end
)
1852 /* If the ORF entry is malformed, want to read as much of it
1853 * as possible without going beyond the bounds of the entry,
1854 * to maximise debug information.
1857 memset (&orfp
, 0, sizeof (struct orf_prefix
));
1859 /* after ++: p_pnt <= p_end */
1860 if (common
& ORF_COMMON_PART_REMOVE_ALL
)
1862 if (bgp_debug_neighbor_events(peer
))
1863 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer
->host
);
1864 prefix_bgp_orf_remove_all (name
);
1867 ok
= ((u_int32_t
)(p_end
- p_pnt
) >= sizeof(u_int32_t
)) ;
1870 memcpy (&seq
, p_pnt
, sizeof (u_int32_t
));
1871 p_pnt
+= sizeof (u_int32_t
);
1872 orfp
.seq
= ntohl (seq
);
1877 if ((ok
= (p_pnt
< p_end
)))
1878 orfp
.ge
= *p_pnt
++ ; /* value checked in prefix_bgp_orf_set() */
1879 if ((ok
= (p_pnt
< p_end
)))
1880 orfp
.le
= *p_pnt
++ ; /* value checked in prefix_bgp_orf_set() */
1881 if ((ok
= (p_pnt
< p_end
)))
1882 orfp
.p
.prefixlen
= *p_pnt
++ ;
1883 orfp
.p
.family
= afi2family (afi
); /* afi checked already */
1885 psize
= PSIZE (orfp
.p
.prefixlen
); /* 0 if not ok */
1886 if (psize
> prefix_blen(&orfp
.p
)) /* valid for family ? */
1889 psize
= prefix_blen(&orfp
.p
) ;
1891 if (psize
> (p_end
- p_pnt
)) /* valid for packet ? */
1894 psize
= p_end
- p_pnt
;
1898 memcpy (&orfp
.p
.u
.prefix
, p_pnt
, psize
);
1901 if (bgp_debug_neighbor_events(peer
))
1903 char buf
[INET6_BUFSIZ
];
1905 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
1907 (common
& ORF_COMMON_PART_REMOVE
? "Remove" : "Add"),
1908 (common
& ORF_COMMON_PART_DENY
? "deny" : "permit"),
1910 inet_ntop (orfp
.p
.family
, &orfp
.p
.u
.prefix
, buf
, INET6_BUFSIZ
),
1911 orfp
.p
.prefixlen
, orfp
.ge
, orfp
.le
,
1912 ok
? "" : " MALFORMED");
1916 ret
= prefix_bgp_orf_set (name
, afi
, &orfp
,
1917 (common
& ORF_COMMON_PART_DENY
? 0 : 1 ),
1918 (common
& ORF_COMMON_PART_REMOVE
? 0 : 1));
1920 if (!ok
|| (ret
!= CMD_SUCCESS
))
1922 zlog_info ("%s Received misformatted prefixlist ORF."
1923 " Remove All pfxlist", peer
->host
);
1924 prefix_bgp_orf_remove_all (name
);
1928 peer
->orf_plist
[afi
][safi
] =
1929 prefix_list_lookup (AFI_ORF_PREFIX
, name
);
1931 stream_forward_getp (s
, orf_len
);
1933 if (bgp_debug_neighbor_events(peer
))
1934 zlog_debug ("%s rcvd Refresh %s ORF request", peer
->host
,
1935 when_to_refresh
== REFRESH_DEFER
? "Defer" : "Immediate");
1936 if (when_to_refresh
== REFRESH_DEFER
)
1940 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1941 if (CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
))
1942 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
1944 /* If the peer is configured for default-originate clear the
1945 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will re-advertise the
1948 paf
= peer_af_find (peer
, afi
, safi
);
1949 if (paf
&& paf
->subgroup
&&
1950 CHECK_FLAG (paf
->subgroup
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1951 UNSET_FLAG (paf
->subgroup
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1953 /* Perform route refreshment to the peer */
1954 bgp_announce_route (peer
, afi
, safi
);
1958 bgp_capability_msg_parse (struct peer
*peer
, u_char
*pnt
, bgp_size_t length
)
1961 struct capability_mp_data mpc
;
1962 struct capability_header
*hdr
;
1971 /* We need at least action, capability code and capability length. */
1974 zlog_info ("%s Capability length error", peer
->host
);
1975 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1979 hdr
= (struct capability_header
*)(pnt
+ 1);
1981 /* Action value check. */
1982 if (action
!= CAPABILITY_ACTION_SET
1983 && action
!= CAPABILITY_ACTION_UNSET
)
1985 zlog_info ("%s Capability Action Value error %d",
1986 peer
->host
, action
);
1987 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1991 if (bgp_debug_neighbor_events(peer
))
1992 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
1993 peer
->host
, action
, hdr
->code
, hdr
->length
);
1995 /* Capability length check. */
1996 if ((pnt
+ hdr
->length
+ 3) > end
)
1998 zlog_info ("%s Capability length error", peer
->host
);
1999 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2003 /* Fetch structure to the byte stream. */
2004 memcpy (&mpc
, pnt
+ 3, sizeof (struct capability_mp_data
));
2006 /* We know MP Capability Code. */
2007 if (hdr
->code
== CAPABILITY_CODE_MP
)
2009 afi
= ntohs (mpc
.afi
);
2012 /* Ignore capability when override-capability is set. */
2013 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
2016 if (!bgp_afi_safi_valid_indices (afi
, &safi
))
2018 if (bgp_debug_neighbor_events(peer
))
2019 zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
2020 "(%u/%u)", peer
->host
, afi
, safi
);
2024 /* Address family check. */
2025 if (bgp_debug_neighbor_events(peer
))
2026 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2028 action
== CAPABILITY_ACTION_SET
2029 ? "Advertising" : "Removing",
2030 ntohs(mpc
.afi
) , mpc
.safi
);
2032 if (action
== CAPABILITY_ACTION_SET
)
2034 peer
->afc_recv
[afi
][safi
] = 1;
2035 if (peer
->afc
[afi
][safi
])
2037 peer
->afc_nego
[afi
][safi
] = 1;
2038 bgp_announce_route (peer
, afi
, safi
);
2043 peer
->afc_recv
[afi
][safi
] = 0;
2044 peer
->afc_nego
[afi
][safi
] = 0;
2046 if (peer_active_nego (peer
))
2047 bgp_clear_route (peer
, afi
, safi
, BGP_CLEAR_ROUTE_NORMAL
);
2049 BGP_EVENT_ADD (peer
, BGP_Stop
);
2054 zlog_warn ("%s unrecognized capability code: %d - ignored",
2055 peer
->host
, hdr
->code
);
2057 pnt
+= hdr
->length
+ 3;
2062 /* Dynamic Capability is received.
2064 * This is exported for unit-test purposes
2067 bgp_capability_receive (struct peer
*peer
, bgp_size_t size
)
2071 /* Fetch pointer. */
2072 pnt
= stream_pnt (peer
->ibuf
);
2074 if (bgp_debug_neighbor_events(peer
))
2075 zlog_debug ("%s rcv CAPABILITY", peer
->host
);
2077 /* If peer does not have the capability, send notification. */
2078 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_ADV
))
2080 zlog_err ("%s [Error] BGP dynamic capability is not enabled",
2082 bgp_notify_send (peer
,
2083 BGP_NOTIFY_HEADER_ERR
,
2084 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2088 /* Status must be Established. */
2089 if (peer
->status
!= Established
)
2091 zlog_err ("%s [Error] Dynamic capability packet received under status %s",
2092 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
2093 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
2098 return bgp_capability_msg_parse (peer
, pnt
, size
);
2101 /* BGP read utility function. */
2103 bgp_read_packet (struct peer
*peer
)
2108 readsize
= peer
->packet_size
- stream_get_endp (peer
->ibuf
);
2110 /* If size is zero then return. */
2114 /* Read packet from fd. */
2115 nbytes
= stream_read_try (peer
->ibuf
, peer
->fd
, readsize
);
2117 /* If read byte is smaller than zero then error occured. */
2120 /* Transient error should retry */
2124 zlog_err ("%s [Error] bgp_read_packet error: %s",
2125 peer
->host
, safe_strerror (errno
));
2127 if (peer
->status
== Established
)
2129 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2131 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2132 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2135 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2138 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
2142 /* When read byte is zero : clear bgp peer and return */
2145 if (bgp_debug_neighbor_events(peer
))
2146 zlog_debug ("%s [Event] BGP connection closed fd %d",
2147 peer
->host
, peer
->fd
);
2149 if (peer
->status
== Established
)
2151 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2153 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2154 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2157 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2160 BGP_EVENT_ADD (peer
, TCP_connection_closed
);
2164 /* We read partial packet. */
2165 if (stream_get_endp (peer
->ibuf
) != peer
->packet_size
)
2173 bgp_marker_all_one (struct stream
*s
, int length
)
2177 for (i
= 0; i
< length
; i
++)
2178 if (s
->data
[i
] != 0xff)
2184 /* Recent thread time.
2185 On same clock base as bgp_clock (MONOTONIC)
2186 but can be time of last context switch to bgp_read thread. */
2188 bgp_recent_clock (void)
2190 return recent_relative_time().tv_sec
;
2193 /* Starting point of packet process function. */
2195 bgp_read (struct thread
*thread
)
2201 char notify_data_length
[2];
2202 u_int32_t notify_out
;
2204 /* Yes first of all get peer pointer. */
2205 peer
= THREAD_ARG (thread
);
2206 peer
->t_read
= NULL
;
2208 /* Note notify_out so we can check later to see if we sent another one */
2209 notify_out
= peer
->notify_out
;
2211 /* For non-blocking IO check. */
2212 if (peer
->status
== Connect
)
2214 bgp_connect_check (peer
, 1);
2221 zlog_err ("bgp_read peer's fd is negative value %d", peer
->fd
);
2224 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
2227 /* Read packet header to determine type of the packet */
2228 if (peer
->packet_size
== 0)
2229 peer
->packet_size
= BGP_HEADER_SIZE
;
2231 if (stream_get_endp (peer
->ibuf
) < BGP_HEADER_SIZE
)
2233 ret
= bgp_read_packet (peer
);
2235 /* Header read error or partial read packet. */
2239 /* Get size and type. */
2240 stream_forward_getp (peer
->ibuf
, BGP_MARKER_SIZE
);
2241 memcpy (notify_data_length
, stream_pnt (peer
->ibuf
), 2);
2242 size
= stream_getw (peer
->ibuf
);
2243 type
= stream_getc (peer
->ibuf
);
2246 if (((type
== BGP_MSG_OPEN
) || (type
== BGP_MSG_KEEPALIVE
))
2247 && ! bgp_marker_all_one (peer
->ibuf
, BGP_MARKER_SIZE
))
2249 bgp_notify_send (peer
,
2250 BGP_NOTIFY_HEADER_ERR
,
2251 BGP_NOTIFY_HEADER_NOT_SYNC
);
2255 /* BGP type check. */
2256 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
2257 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
2258 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
2259 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
2260 && type
!= BGP_MSG_CAPABILITY
)
2262 if (bgp_debug_neighbor_events(peer
))
2263 zlog_debug ("%s unknown message type 0x%02x",
2265 bgp_notify_send_with_data (peer
,
2266 BGP_NOTIFY_HEADER_ERR
,
2267 BGP_NOTIFY_HEADER_BAD_MESTYPE
,
2271 /* Mimimum packet length check. */
2272 if ((size
< BGP_HEADER_SIZE
)
2273 || (size
> BGP_MAX_PACKET_SIZE
)
2274 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
2275 || (type
== BGP_MSG_UPDATE
&& size
< BGP_MSG_UPDATE_MIN_SIZE
)
2276 || (type
== BGP_MSG_NOTIFY
&& size
< BGP_MSG_NOTIFY_MIN_SIZE
)
2277 || (type
== BGP_MSG_KEEPALIVE
&& size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
2278 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2279 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2280 || (type
== BGP_MSG_CAPABILITY
&& size
< BGP_MSG_CAPABILITY_MIN_SIZE
))
2282 if (bgp_debug_neighbor_events(peer
))
2283 zlog_debug ("%s bad message length - %d for %s",
2285 type
== 128 ? "ROUTE-REFRESH" :
2286 bgp_type_str
[(int) type
]);
2287 bgp_notify_send_with_data (peer
,
2288 BGP_NOTIFY_HEADER_ERR
,
2289 BGP_NOTIFY_HEADER_BAD_MESLEN
,
2290 (u_char
*) notify_data_length
, 2);
2294 /* Adjust size to message length. */
2295 peer
->packet_size
= size
;
2298 ret
= bgp_read_packet (peer
);
2302 /* Get size and type again. */
2303 size
= stream_getw_from (peer
->ibuf
, BGP_MARKER_SIZE
);
2304 type
= stream_getc_from (peer
->ibuf
, BGP_MARKER_SIZE
+ 2);
2306 /* BGP packet dump function. */
2307 bgp_dump_packet (peer
, type
, peer
->ibuf
);
2309 size
= (peer
->packet_size
- BGP_HEADER_SIZE
);
2311 /* Read rest of the packet and call each sort of packet routine */
2316 bgp_open_receive (peer
, size
); /* XXX return value ignored! */
2318 case BGP_MSG_UPDATE
:
2319 peer
->readtime
= bgp_recent_clock ();
2320 bgp_update_receive (peer
, size
);
2322 case BGP_MSG_NOTIFY
:
2323 bgp_notify_receive (peer
, size
);
2325 case BGP_MSG_KEEPALIVE
:
2326 peer
->readtime
= bgp_recent_clock ();
2327 bgp_keepalive_receive (peer
, size
);
2329 case BGP_MSG_ROUTE_REFRESH_NEW
:
2330 case BGP_MSG_ROUTE_REFRESH_OLD
:
2332 bgp_route_refresh_receive (peer
, size
);
2334 case BGP_MSG_CAPABILITY
:
2335 peer
->dynamic_cap_in
++;
2336 bgp_capability_receive (peer
, size
);
2340 /* If reading this packet caused us to send a NOTIFICATION then store a copy
2341 * of the packet for troubleshooting purposes
2343 if (notify_out
< peer
->notify_out
)
2345 memcpy(peer
->last_reset_cause
, peer
->ibuf
->data
, peer
->packet_size
);
2346 peer
->last_reset_cause_size
= peer
->packet_size
;
2347 notify_out
= peer
->notify_out
;
2350 /* Clear input buffer. */
2351 peer
->packet_size
= 0;
2353 stream_reset (peer
->ibuf
);
2356 /* If reading this packet caused us to send a NOTIFICATION then store a copy
2357 * of the packet for troubleshooting purposes
2359 if (notify_out
< peer
->notify_out
)
2361 memcpy(peer
->last_reset_cause
, peer
->ibuf
->data
, peer
->packet_size
);
2362 peer
->last_reset_cause_size
= peer
->packet_size
;