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 () */
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_table.h"
36 #include "bgpd/bgp_dump.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_debug.h"
39 #include "bgpd/bgp_fsm.h"
40 #include "bgpd/bgp_route.h"
41 #include "bgpd/bgp_packet.h"
42 #include "bgpd/bgp_open.h"
43 #include "bgpd/bgp_aspath.h"
44 #include "bgpd/bgp_community.h"
45 #include "bgpd/bgp_ecommunity.h"
46 #include "bgpd/bgp_network.h"
47 #include "bgpd/bgp_mplsvpn.h"
48 #include "bgpd/bgp_advertise.h"
49 #include "bgpd/bgp_vty.h"
51 int stream_put_prefix (struct stream
*, struct prefix
*);
53 /* Set up BGP packet marker and packet type. */
55 bgp_packet_set_marker (struct stream
*s
, u_char type
)
60 for (i
= 0; i
< BGP_MARKER_SIZE
; i
++)
61 stream_putc (s
, 0xff);
63 /* Dummy total length. This field is should be filled in later on. */
66 /* BGP packet type. */
67 stream_putc (s
, type
);
69 /* Return current stream size. */
70 return stream_get_endp (s
);
73 /* Set BGP packet header size entry. If size is zero then use current
76 bgp_packet_set_size (struct stream
*s
)
80 /* Preserve current pointer. */
81 cp
= stream_get_endp (s
);
82 stream_putw_at (s
, BGP_MARKER_SIZE
, cp
);
87 /* Add new packet to the peer. */
89 bgp_packet_add (struct peer
*peer
, struct stream
*s
)
91 /* Add packet to the end of list. */
92 stream_fifo_push (peer
->obuf
, s
);
95 /* Free first packet. */
97 bgp_packet_delete (struct peer
*peer
)
99 stream_free (stream_fifo_pop (peer
->obuf
));
102 /* Check file descriptor whether connect is established. */
104 bgp_connect_check (struct peer
*peer
)
110 /* Anyway I have to reset read and write thread. */
111 BGP_READ_OFF (peer
->t_read
);
112 BGP_WRITE_OFF (peer
->t_write
);
114 /* Check file descriptor. */
115 slen
= sizeof (status
);
116 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &status
, &slen
);
118 /* If getsockopt is fail, this is fatal error. */
121 zlog (peer
->log
, LOG_INFO
, "can't get sockopt for nonblocking connect");
122 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
126 /* When status is 0 then TCP connection is established. */
129 BGP_EVENT_ADD (peer
, TCP_connection_open
);
133 if (BGP_DEBUG (events
, EVENTS
))
134 plog_debug (peer
->log
, "%s [Event] Connect failed (%s)",
135 peer
->host
, safe_strerror (errno
));
136 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
140 /* Make BGP update packet. */
141 static struct stream
*
142 bgp_update_packet (struct peer
*peer
, afi_t afi
, safi_t safi
)
145 struct bgp_adj_out
*adj
;
146 struct bgp_advertise
*adv
;
147 struct stream
*packet
;
148 struct bgp_node
*rn
= NULL
;
149 struct bgp_info
*binfo
= NULL
;
150 bgp_size_t total_attr_len
= 0;
157 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
);
167 /* When remaining space can't include NLRI and it's length. */
168 if (STREAM_REMAIN (s
) <= BGP_NLRI_LENGTH
+ PSIZE (rn
->p
.prefixlen
))
171 /* If packet is empty, set attribute. */
172 if (stream_empty (s
))
174 struct prefix_rd
*prd
= NULL
;
176 struct peer
*from
= NULL
;
179 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
180 if (binfo
&& binfo
->extra
)
182 tag
= binfo
->extra
->tag
;
186 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
188 pos
= stream_get_endp (s
);
190 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
,
194 stream_putw_at (s
, pos
, total_attr_len
);
197 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
198 stream_put_prefix (s
, &rn
->p
);
200 if (BGP_DEBUG (update
, UPDATE_OUT
))
201 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d",
203 inet_ntop (rn
->p
.family
, &(rn
->p
.u
.prefix
), buf
, BUFSIZ
),
206 /* Synchnorize attribute. */
208 bgp_attr_unintern (adj
->attr
);
210 peer
->scount
[afi
][safi
]++;
212 adj
->attr
= bgp_attr_intern (adv
->baa
->attr
);
214 adv
= bgp_advertise_clean (peer
, adj
, afi
, safi
);
216 if (! (afi
== AFI_IP
&& safi
== SAFI_UNICAST
))
220 if (! stream_empty (s
))
222 bgp_packet_set_size (s
);
223 packet
= stream_dup (s
);
224 bgp_packet_add (peer
, packet
);
225 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
232 static struct stream
*
233 bgp_update_packet_eor (struct peer
*peer
, afi_t afi
, safi_t safi
)
236 struct stream
*packet
;
238 if (DISABLE_BGP_ANNOUNCE
)
241 if (BGP_DEBUG (normal
, NORMAL
))
242 zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi
, safi
), peer
->host
);
244 s
= stream_new (BGP_MAX_PACKET_SIZE
);
246 /* Make BGP update packet. */
247 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
249 /* Unfeasible Routes Length */
252 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
254 /* Total Path Attribute Length */
259 /* Total Path Attribute Length */
261 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
262 stream_putc (s
, BGP_ATTR_MP_UNREACH_NLRI
);
264 stream_putw (s
, afi
);
265 stream_putc (s
, safi
);
268 bgp_packet_set_size (s
);
269 packet
= stream_dup (s
);
270 bgp_packet_add (peer
, packet
);
275 /* Make BGP withdraw packet. */
276 static struct stream
*
277 bgp_withdraw_packet (struct peer
*peer
, afi_t afi
, safi_t safi
)
280 struct stream
*packet
;
281 struct bgp_adj_out
*adj
;
282 struct bgp_advertise
*adv
;
285 bgp_size_t unfeasible_len
;
286 bgp_size_t total_attr_len
;
292 while ((adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
)) != NULL
)
298 if (STREAM_REMAIN (s
)
299 < (BGP_NLRI_LENGTH
+ BGP_TOTAL_ATTR_LEN
+ PSIZE (rn
->p
.prefixlen
)))
302 if (stream_empty (s
))
304 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
308 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
309 stream_put_prefix (s
, &rn
->p
);
312 struct prefix_rd
*prd
= NULL
;
315 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
316 pos
= stream_get_endp (s
);
319 = bgp_packet_withdraw (peer
, s
, &rn
->p
, afi
, safi
, prd
, NULL
);
321 /* Set total path attribute length. */
322 stream_putw_at (s
, pos
, total_attr_len
);
325 if (BGP_DEBUG (update
, UPDATE_OUT
))
326 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d -- unreachable",
328 inet_ntop (rn
->p
.family
, &(rn
->p
.u
.prefix
), buf
, BUFSIZ
),
331 peer
->scount
[afi
][safi
]--;
333 bgp_adj_out_remove (rn
, adj
, peer
, afi
, safi
);
334 bgp_unlock_node (rn
);
336 if (! (afi
== AFI_IP
&& safi
== SAFI_UNICAST
))
340 if (! stream_empty (s
))
342 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
345 = stream_get_endp (s
) - BGP_HEADER_SIZE
- BGP_UNFEASIBLE_LEN
;
346 stream_putw_at (s
, BGP_HEADER_SIZE
, unfeasible_len
);
349 bgp_packet_set_size (s
);
350 packet
= stream_dup (s
);
351 bgp_packet_add (peer
, packet
);
360 bgp_default_update_send (struct peer
*peer
, struct attr
*attr
,
361 afi_t afi
, safi_t safi
, struct peer
*from
)
364 struct stream
*packet
;
367 bgp_size_t total_attr_len
;
368 char attrstr
[BUFSIZ
];
371 if (DISABLE_BGP_ANNOUNCE
)
375 str2prefix ("0.0.0.0/0", &p
);
378 str2prefix ("::/0", &p
);
379 #endif /* HAVE_IPV6 */
381 /* Logging the attribute. */
382 if (BGP_DEBUG (update
, UPDATE_OUT
))
384 bgp_dump_attr (peer
, attr
, attrstr
, BUFSIZ
);
385 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d %s",
386 peer
->host
, inet_ntop(p
.family
, &(p
.u
.prefix
), buf
, BUFSIZ
),
387 p
.prefixlen
, attrstr
);
390 s
= stream_new (BGP_MAX_PACKET_SIZE
);
392 /* Make BGP update packet. */
393 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
395 /* Unfeasible Routes Length. */
398 /* Make place for total attribute length. */
399 pos
= stream_get_endp (s
);
401 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
, attr
, &p
, afi
, safi
, from
, NULL
, NULL
);
403 /* Set Total Path Attribute Length. */
404 stream_putw_at (s
, pos
, total_attr_len
);
407 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
)
408 stream_put_prefix (s
, &p
);
411 bgp_packet_set_size (s
);
413 packet
= stream_dup (s
);
416 /* Dump packet if debug option is set. */
418 /* bgp_packet_dump (packet); */
421 /* Add packet to the peer. */
422 bgp_packet_add (peer
, packet
);
424 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
428 bgp_default_withdraw_send (struct peer
*peer
, afi_t afi
, safi_t safi
)
431 struct stream
*packet
;
435 bgp_size_t unfeasible_len
;
436 bgp_size_t total_attr_len
;
439 if (DISABLE_BGP_ANNOUNCE
)
443 str2prefix ("0.0.0.0/0", &p
);
446 str2prefix ("::/0", &p
);
447 #endif /* HAVE_IPV6 */
452 if (BGP_DEBUG (update
, UPDATE_OUT
))
453 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d -- unreachable",
454 peer
->host
, inet_ntop(p
.family
, &(p
.u
.prefix
), buf
, BUFSIZ
),
457 s
= stream_new (BGP_MAX_PACKET_SIZE
);
459 /* Make BGP update packet. */
460 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
462 /* Unfeasible Routes Length. */;
463 cp
= stream_get_endp (s
);
466 /* Withdrawn Routes. */
467 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
)
469 stream_put_prefix (s
, &p
);
471 unfeasible_len
= stream_get_endp (s
) - cp
- 2;
473 /* Set unfeasible len. */
474 stream_putw_at (s
, cp
, unfeasible_len
);
476 /* Set total path attribute length. */
481 pos
= stream_get_endp (s
);
483 total_attr_len
= bgp_packet_withdraw (peer
, s
, &p
, afi
, safi
, NULL
, NULL
);
485 /* Set total path attribute length. */
486 stream_putw_at (s
, pos
, total_attr_len
);
489 bgp_packet_set_size (s
);
491 packet
= stream_dup (s
);
494 /* Add packet to the peer. */
495 bgp_packet_add (peer
, packet
);
497 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
500 /* Get next packet to be written. */
501 static struct stream
*
502 bgp_write_packet (struct peer
*peer
)
506 struct stream
*s
= NULL
;
507 struct bgp_advertise
*adv
;
509 s
= stream_fifo_head (peer
->obuf
);
513 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
514 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
516 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
);
519 s
= bgp_withdraw_packet (peer
, afi
, safi
);
525 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
526 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
528 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
);
531 if (adv
->binfo
&& adv
->binfo
->uptime
< peer
->synctime
)
533 if (CHECK_FLAG (adv
->binfo
->peer
->cap
, PEER_CAP_RESTART_RCV
)
534 && CHECK_FLAG (adv
->binfo
->peer
->cap
, PEER_CAP_RESTART_ADV
)
535 && ! CHECK_FLAG (adv
->binfo
->flags
, BGP_INFO_STALE
)
536 && safi
!= SAFI_MPLS_VPN
)
538 if (CHECK_FLAG (adv
->binfo
->peer
->af_sflags
[afi
][safi
],
539 PEER_STATUS_EOR_RECEIVED
))
540 s
= bgp_update_packet (peer
, afi
, safi
);
543 s
= bgp_update_packet (peer
, afi
, safi
);
550 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
552 if (peer
->afc_nego
[afi
][safi
] && peer
->synctime
553 && ! CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_SEND
)
554 && safi
!= SAFI_MPLS_VPN
)
556 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_SEND
);
557 return bgp_update_packet_eor (peer
, afi
, safi
);
565 /* Is there partially written packet or updates we can send right
568 bgp_write_proceed (struct peer
*peer
)
572 struct bgp_advertise
*adv
;
574 if (stream_fifo_head (peer
->obuf
))
577 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
578 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
579 if (FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
))
582 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
583 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
584 if ((adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
)) != NULL
)
585 if (adv
->binfo
->uptime
< peer
->synctime
)
591 /* Write packet to the peer. */
593 bgp_write (struct thread
*thread
)
599 unsigned int count
= 0;
602 /* Yes first of all get peer pointer. */
603 peer
= THREAD_ARG (thread
);
604 peer
->t_write
= NULL
;
606 /* For non-blocking IO check. */
607 if (peer
->status
== Connect
)
609 bgp_connect_check (peer
);
613 /* Nonblocking write until TCP output buffer is full. */
619 s
= bgp_write_packet (peer
);
623 /* XXX: FIXME, the socket should be NONBLOCK from the start
624 * status shouldnt need to be toggled on each write
626 val
= fcntl (peer
->fd
, F_GETFL
, 0);
627 fcntl (peer
->fd
, F_SETFL
, val
|O_NONBLOCK
);
629 /* Number of bytes to be sent. */
630 writenum
= stream_get_endp (s
) - stream_get_getp (s
);
632 /* Call write() system call. */
633 num
= write (peer
->fd
, STREAM_PNT (s
), writenum
);
635 fcntl (peer
->fd
, F_SETFL
, val
);
639 if (write_errno
== EWOULDBLOCK
|| write_errno
== EAGAIN
)
642 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
647 stream_forward_getp (s
, num
);
649 if (write_errno
== EAGAIN
)
655 /* Retrieve BGP packet type. */
656 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
657 type
= stream_getc (s
);
669 /* Double start timer. */
672 /* Overflow check. */
673 if (peer
->v_start
>= (60 * 2))
674 peer
->v_start
= (60 * 2);
676 /* Flush any existing events */
677 BGP_EVENT_ADD (peer
, BGP_Stop
);
679 case BGP_MSG_KEEPALIVE
:
680 peer
->keepalive_out
++;
682 case BGP_MSG_ROUTE_REFRESH_NEW
:
683 case BGP_MSG_ROUTE_REFRESH_OLD
:
686 case BGP_MSG_CAPABILITY
:
687 peer
->dynamic_cap_out
++;
691 /* OK we send packet so delete it. */
692 bgp_packet_delete (peer
);
694 if (++count
>= BGP_WRITE_PACKET_MAX
)
698 if (bgp_write_proceed (peer
))
699 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
704 /* This is only for sending NOTIFICATION message to neighbor. */
706 bgp_write_notify (struct peer
*peer
)
712 /* There should be at least one packet. */
713 s
= stream_fifo_head (peer
->obuf
);
716 assert (stream_get_endp (s
) >= BGP_HEADER_SIZE
);
718 /* I'm not sure fd is writable. */
719 ret
= writen (peer
->fd
, STREAM_DATA (s
), stream_get_endp (s
));
722 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
726 /* Retrieve BGP packet type. */
727 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
728 type
= stream_getc (s
);
730 assert (type
== BGP_MSG_NOTIFY
);
732 /* Type should be notify. */
735 /* Double start timer. */
738 /* Overflow check. */
739 if (peer
->v_start
>= (60 * 2))
740 peer
->v_start
= (60 * 2);
742 BGP_EVENT_ADD (peer
, BGP_Stop
);
747 /* Make keepalive packet and send it to the peer. */
749 bgp_keepalive_send (struct peer
*peer
)
754 s
= stream_new (BGP_MAX_PACKET_SIZE
);
756 /* Make keepalive packet. */
757 bgp_packet_set_marker (s
, BGP_MSG_KEEPALIVE
);
759 /* Set packet size. */
760 length
= bgp_packet_set_size (s
);
762 /* Dump packet if debug option is set. */
763 /* bgp_packet_dump (s); */
765 if (BGP_DEBUG (keepalive
, KEEPALIVE
))
766 zlog_debug ("%s sending KEEPALIVE", peer
->host
);
767 if (BGP_DEBUG (normal
, NORMAL
))
768 zlog_debug ("%s send message type %d, length (incl. header) %d",
769 peer
->host
, BGP_MSG_KEEPALIVE
, length
);
771 /* Add packet to the peer. */
772 bgp_packet_add (peer
, s
);
774 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
777 /* Make open packet and send it to the peer. */
779 bgp_open_send (struct peer
*peer
)
783 u_int16_t send_holdtime
;
786 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
787 send_holdtime
= peer
->holdtime
;
789 send_holdtime
= peer
->bgp
->default_holdtime
;
791 /* local-as Change */
792 if (peer
->change_local_as
)
793 local_as
= peer
->change_local_as
;
795 local_as
= peer
->local_as
;
797 s
= stream_new (BGP_MAX_PACKET_SIZE
);
799 /* Make open packet. */
800 bgp_packet_set_marker (s
, BGP_MSG_OPEN
);
802 /* Set open packet values. */
803 stream_putc (s
, BGP_VERSION_4
); /* BGP version */
804 stream_putw (s
, (local_as
<= BGP_AS_MAX
) ? (u_int16_t
) local_as
806 stream_putw (s
, send_holdtime
); /* Hold Time */
807 stream_put_in_addr (s
, &peer
->local_id
); /* BGP Identifier */
809 /* Set capability code. */
810 bgp_open_capability (s
, peer
);
812 /* Set BGP packet length. */
813 length
= bgp_packet_set_size (s
);
815 if (BGP_DEBUG (normal
, NORMAL
))
816 zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
817 peer
->host
, BGP_VERSION_4
, local_as
,
818 send_holdtime
, inet_ntoa (peer
->local_id
));
820 if (BGP_DEBUG (normal
, NORMAL
))
821 zlog_debug ("%s send message type %d, length (incl. header) %d",
822 peer
->host
, BGP_MSG_OPEN
, length
);
824 /* Dump packet if debug option is set. */
825 /* bgp_packet_dump (s); */
827 /* Add packet to the peer. */
828 bgp_packet_add (peer
, s
);
830 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
833 /* Send BGP notify packet with data potion. */
835 bgp_notify_send_with_data (struct peer
*peer
, u_char code
, u_char sub_code
,
836 u_char
*data
, size_t datalen
)
841 /* Allocate new stream. */
842 s
= stream_new (BGP_MAX_PACKET_SIZE
);
844 /* Make nitify packet. */
845 bgp_packet_set_marker (s
, BGP_MSG_NOTIFY
);
847 /* Set notify packet values. */
848 stream_putc (s
, code
); /* BGP notify code */
849 stream_putc (s
, sub_code
); /* BGP notify sub_code */
851 /* If notify data is present. */
853 stream_write (s
, data
, datalen
);
855 /* Set BGP packet length. */
856 length
= bgp_packet_set_size (s
);
858 /* Add packet to the peer. */
859 stream_fifo_clean (peer
->obuf
);
860 bgp_packet_add (peer
, s
);
864 struct bgp_notify bgp_notify
;
869 bgp_notify
.code
= code
;
870 bgp_notify
.subcode
= sub_code
;
871 bgp_notify
.data
= NULL
;
872 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
874 if (bgp_notify
.length
)
876 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
877 for (i
= 0; i
< bgp_notify
.length
; i
++)
880 sprintf (c
, " %02x", data
[i
]);
881 strcat (bgp_notify
.data
, c
);
886 sprintf (c
, "%02x", data
[i
]);
887 strcpy (bgp_notify
.data
, c
);
890 bgp_notify_print (peer
, &bgp_notify
, "sending");
892 XFREE (MTYPE_TMP
, bgp_notify
.data
);
895 if (BGP_DEBUG (normal
, NORMAL
))
896 zlog_debug ("%s send message type %d, length (incl. header) %d",
897 peer
->host
, BGP_MSG_NOTIFY
, length
);
899 /* peer reset cause */
900 if (sub_code
!= BGP_NOTIFY_CEASE_CONFIG_CHANGE
)
902 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
903 peer
->last_reset
= PEER_DOWN_USER_RESET
;
904 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
905 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
907 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
910 /* Call imidiately. */
911 BGP_WRITE_OFF (peer
->t_write
);
913 bgp_write_notify (peer
);
916 /* Send BGP notify packet. */
918 bgp_notify_send (struct peer
*peer
, u_char code
, u_char sub_code
)
920 bgp_notify_send_with_data (peer
, code
, sub_code
, NULL
, 0);
928 else if (afi
== AFI_IP6
)
931 return "Unknown AFI";
935 safi2str (safi_t safi
)
937 if (safi
== SAFI_UNICAST
)
938 return "SAFI_UNICAST";
939 else if (safi
== SAFI_MULTICAST
)
940 return "SAFI_MULTICAST";
941 else if (safi
== SAFI_MPLS_VPN
|| safi
== BGP_SAFI_VPNV4
)
942 return "SAFI_MPLS_VPN";
944 return "Unknown SAFI";
947 /* Send route refresh message to the peer. */
949 bgp_route_refresh_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
950 u_char orf_type
, u_char when_to_refresh
, int remove
)
953 struct stream
*packet
;
955 struct bgp_filter
*filter
;
958 if (DISABLE_BGP_ANNOUNCE
)
961 filter
= &peer
->filter
[afi
][safi
];
963 /* Adjust safi code. */
964 if (safi
== SAFI_MPLS_VPN
)
965 safi
= BGP_SAFI_VPNV4
;
967 s
= stream_new (BGP_MAX_PACKET_SIZE
);
969 /* Make BGP update packet. */
970 if (CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
971 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_NEW
);
973 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_OLD
);
975 /* Encode Route Refresh message. */
976 stream_putw (s
, afi
);
978 stream_putc (s
, safi
);
980 if (orf_type
== ORF_TYPE_PREFIX
981 || orf_type
== ORF_TYPE_PREFIX_OLD
)
982 if (remove
|| filter
->plist
[FILTER_IN
].plist
)
988 stream_putc (s
, when_to_refresh
);
989 stream_putc (s
, orf_type
);
990 orfp
= stream_get_endp (s
);
995 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
996 stream_putc (s
, ORF_COMMON_PART_REMOVE_ALL
);
997 if (BGP_DEBUG (normal
, NORMAL
))
998 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
999 peer
->host
, orf_type
,
1000 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
1005 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
1006 prefix_bgp_orf_entry (s
, filter
->plist
[FILTER_IN
].plist
,
1007 ORF_COMMON_PART_ADD
, ORF_COMMON_PART_PERMIT
,
1008 ORF_COMMON_PART_DENY
);
1009 if (BGP_DEBUG (normal
, NORMAL
))
1010 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
1011 peer
->host
, orf_type
,
1012 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
1016 /* Total ORF Entry Len. */
1017 orf_len
= stream_get_endp (s
) - orfp
- 2;
1018 stream_putw_at (s
, orfp
, orf_len
);
1021 /* Set packet size. */
1022 length
= bgp_packet_set_size (s
);
1024 if (BGP_DEBUG (normal
, NORMAL
))
1027 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
1028 peer
->host
, afi
, safi
);
1029 zlog_debug ("%s send message type %d, length (incl. header) %d",
1030 peer
->host
, CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
) ?
1031 BGP_MSG_ROUTE_REFRESH_NEW
: BGP_MSG_ROUTE_REFRESH_OLD
, length
);
1034 /* Make real packet. */
1035 packet
= stream_dup (s
);
1038 /* Add packet to the peer. */
1039 bgp_packet_add (peer
, packet
);
1041 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1044 /* Send capability message to the peer. */
1046 bgp_capability_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
1047 int capability_code
, int action
)
1050 struct stream
*packet
;
1053 /* Adjust safi code. */
1054 if (safi
== SAFI_MPLS_VPN
)
1055 safi
= BGP_SAFI_VPNV4
;
1057 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1059 /* Make BGP update packet. */
1060 bgp_packet_set_marker (s
, BGP_MSG_CAPABILITY
);
1062 /* Encode MP_EXT capability. */
1063 if (capability_code
== CAPABILITY_CODE_MP
)
1065 stream_putc (s
, action
);
1066 stream_putc (s
, CAPABILITY_CODE_MP
);
1067 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
1068 stream_putw (s
, afi
);
1070 stream_putc (s
, safi
);
1072 if (BGP_DEBUG (normal
, NORMAL
))
1073 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
1074 peer
->host
, action
== CAPABILITY_ACTION_SET
?
1075 "Advertising" : "Removing", afi
, safi
);
1078 /* Set packet size. */
1079 length
= bgp_packet_set_size (s
);
1081 /* Make real packet. */
1082 packet
= stream_dup (s
);
1085 /* Add packet to the peer. */
1086 bgp_packet_add (peer
, packet
);
1088 if (BGP_DEBUG (normal
, NORMAL
))
1089 zlog_debug ("%s send message type %d, length (incl. header) %d",
1090 peer
->host
, BGP_MSG_CAPABILITY
, length
);
1092 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1095 /* RFC1771 6.8 Connection collision detection. */
1097 bgp_collision_detect (struct peer
*new, struct in_addr remote_id
)
1100 struct listnode
*node
, *nnode
;
1103 bgp
= bgp_get_default ();
1107 /* Upon receipt of an OPEN message, the local system must examine
1108 all of its connections that are in the OpenConfirm state. A BGP
1109 speaker may also examine connections in an OpenSent state if it
1110 knows the BGP Identifier of the peer by means outside of the
1111 protocol. If among these connections there is a connection to a
1112 remote BGP speaker whose BGP Identifier equals the one in the
1113 OPEN message, then the local system performs the following
1114 collision resolution procedure: */
1116 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
1118 /* Under OpenConfirm status, local peer structure already hold
1119 remote router ID. */
1122 && (peer
->status
== OpenConfirm
|| peer
->status
== OpenSent
)
1123 && sockunion_same (&peer
->su
, &new->su
))
1125 /* 1. The BGP Identifier of the local system is compared to
1126 the BGP Identifier of the remote system (as specified in
1127 the OPEN message). */
1129 if (ntohl (peer
->local_id
.s_addr
) < ntohl (remote_id
.s_addr
))
1131 /* 2. If the value of the local BGP Identifier is less
1132 than the remote one, the local system closes BGP
1133 connection that already exists (the one that is
1134 already in the OpenConfirm state), and accepts BGP
1135 connection initiated by the remote system. */
1138 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1143 /* 3. Otherwise, the local system closes newly created
1144 BGP connection (the one associated with the newly
1145 received OPEN message), and continues to use the
1146 existing one (the one that is already in the
1147 OpenConfirm state). */
1150 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
1151 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1160 bgp_open_receive (struct peer
*peer
, bgp_size_t size
)
1166 u_int16_t send_holdtime
;
1169 struct peer
*realpeer
;
1170 struct in_addr remote_id
;
1172 u_int8_t notify_data_remote_as
[2];
1173 u_int8_t notify_data_remote_id
[4];
1177 /* Parse open packet. */
1178 version
= stream_getc (peer
->ibuf
);
1179 memcpy (notify_data_remote_as
, stream_pnt (peer
->ibuf
), 2);
1180 remote_as
= stream_getw (peer
->ibuf
);
1181 holdtime
= stream_getw (peer
->ibuf
);
1182 memcpy (notify_data_remote_id
, stream_pnt (peer
->ibuf
), 4);
1183 remote_id
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
1185 /* Receive OPEN message log */
1186 if (BGP_DEBUG (normal
, NORMAL
))
1187 zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
1188 " holdtime %d, id %s",
1189 peer
->host
, version
, remote_as
, holdtime
,
1190 inet_ntoa (remote_id
));
1192 /* BEGIN to read the capability here, but dont do it yet */
1194 optlen
= stream_getc (peer
->ibuf
);
1198 /* We need the as4 capability value *right now* because
1199 * if it is there, we have not got the remote_as yet, and without
1200 * that we do not know which peer is connecting to us now.
1202 as4
= peek_for_as4_capability (peer
, optlen
);
1205 /* Just in case we have a silly peer who sends AS4 capability set to 0 */
1206 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
)
1208 zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
1210 bgp_notify_send (peer
, BGP_NOTIFY_OPEN_ERR
,
1211 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1215 if (remote_as
== BGP_AS_TRANS
)
1217 /* Take the AS4 from the capability. We must have received the
1218 * capability now! Otherwise we have a asn16 peer who uses
1219 * BGP_AS_TRANS, for some unknown reason.
1221 if (as4
== BGP_AS_TRANS
)
1223 zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1225 bgp_notify_send (peer
, BGP_NOTIFY_OPEN_ERR
,
1226 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1230 if (!as4
&& BGP_DEBUG (as4
, AS4
))
1231 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
1232 " Odd, but proceeding.", peer
->host
);
1233 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG (as4
, AS4
))
1234 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
1235 "in 2-bytes, very odd peer.", peer
->host
, as4
);
1241 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
1242 /* If we have got the capability, peer->as4cap must match remote_as */
1243 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
)
1244 && as4
!= remote_as
)
1246 /* raise error, log this, close session */
1247 zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
1248 " mismatch with 16bit 'myasn' %u in open",
1249 peer
->host
, as4
, remote_as
);
1250 bgp_notify_send (peer
, BGP_NOTIFY_OPEN_ERR
,
1251 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1256 /* Lookup peer from Open packet. */
1257 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1261 realpeer
= peer_lookup_with_open (&peer
->su
, remote_as
, &remote_id
, &as
);
1265 /* Peer's source IP address is check in bgp_accept(), so this
1266 must be AS number mismatch or remote-id configuration
1270 if (BGP_DEBUG (normal
, NORMAL
))
1271 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1272 peer
->host
, inet_ntoa (remote_id
));
1273 bgp_notify_send_with_data (peer
, BGP_NOTIFY_OPEN_ERR
,
1274 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1275 notify_data_remote_id
, 4);
1279 if (BGP_DEBUG (normal
, NORMAL
))
1280 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1281 peer
->host
, remote_as
, peer
->as
);
1282 bgp_notify_send_with_data (peer
, BGP_NOTIFY_OPEN_ERR
,
1283 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1284 notify_data_remote_as
, 2);
1290 /* When collision is detected and this peer is closed. Retrun
1292 ret
= bgp_collision_detect (peer
, remote_id
);
1297 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1299 if (realpeer
->status
== Established
1300 && CHECK_FLAG (realpeer
->sflags
, PEER_STATUS_NSF_MODE
))
1302 realpeer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
1303 SET_FLAG (realpeer
->sflags
, PEER_STATUS_NSF_WAIT
);
1305 else if (ret
== 0 && realpeer
->status
!= Active
1306 && realpeer
->status
!= OpenSent
1307 && realpeer
->status
!= OpenConfirm
1308 && realpeer
->status
!= Connect
)
1310 /* XXX: This is an awful problem..
1312 * According to the RFC we should just let this connection (of the
1313 * accepted 'peer') continue on to Established if the other
1314 * connection (the 'realpeer' one) is in state Connect, and deal
1315 * with the more larval FSM as/when it gets far enough to receive
1316 * an Open. We don't do that though, we instead close the (more
1317 * developed) accepted connection.
1319 * This means there's a race, which if hit, can loop:
1321 * FSM for A FSM for B
1322 * realpeer accept-peer realpeer accept-peer
1338 * If both sides are Quagga, they're almost certain to wait for
1339 * the same amount of time of course (which doesn't preclude other
1340 * implementations also waiting for same time). The race is
1341 * exacerbated by high-latency (in bgpd and/or the network).
1343 * The reason we do this is because our FSM is tied to our peer
1344 * structure, which carries our configuration information, etc.
1345 * I.e. we can't let the accepted-peer FSM continue on as it is,
1346 * cause it's not associated with any actual peer configuration -
1347 * it's just a dummy.
1349 * It's possible we could hack-fix this by just bgp_stop'ing the
1350 * realpeer and continueing on with the 'transfer FSM' below.
1351 * Ideally, we need to seperate FSMs from struct peer.
1353 * Setting one side to passive avoids the race, as a workaround.
1355 if (BGP_DEBUG (events
, EVENTS
))
1356 zlog_debug ("%s peer status is %s close connection",
1357 realpeer
->host
, LOOKUP (bgp_status_msg
,
1359 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
1360 BGP_NOTIFY_CEASE_CONNECT_REJECT
);
1365 if (BGP_DEBUG (events
, EVENTS
))
1366 zlog_debug ("%s [Event] Transfer accept BGP peer to real (state %s)",
1368 LOOKUP (bgp_status_msg
, realpeer
->status
));
1370 bgp_stop (realpeer
);
1372 /* Transfer file descriptor. */
1373 realpeer
->fd
= peer
->fd
;
1376 /* Transfer input buffer. */
1377 stream_free (realpeer
->ibuf
);
1378 realpeer
->ibuf
= peer
->ibuf
;
1379 realpeer
->packet_size
= peer
->packet_size
;
1382 /* Transfer status. */
1383 realpeer
->status
= peer
->status
;
1386 /* peer pointer change. Open packet send to neighbor. */
1388 bgp_open_send (peer
);
1391 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1395 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1398 /* remote router-id check. */
1399 if (remote_id
.s_addr
== 0
1400 || ntohl (remote_id
.s_addr
) >= 0xe0000000
1401 || ntohl (peer
->local_id
.s_addr
) == ntohl (remote_id
.s_addr
))
1403 if (BGP_DEBUG (normal
, NORMAL
))
1404 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1405 peer
->host
, inet_ntoa (remote_id
));
1406 bgp_notify_send_with_data (peer
,
1407 BGP_NOTIFY_OPEN_ERR
,
1408 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1409 notify_data_remote_id
, 4);
1413 /* Set remote router-id */
1414 peer
->remote_id
= remote_id
;
1416 /* Peer BGP version check. */
1417 if (version
!= BGP_VERSION_4
)
1419 u_int8_t maxver
= BGP_VERSION_4
;
1420 if (BGP_DEBUG (normal
, NORMAL
))
1421 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
1422 peer
->host
, version
, BGP_VERSION_4
);
1423 bgp_notify_send_with_data (peer
,
1424 BGP_NOTIFY_OPEN_ERR
,
1425 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1430 /* Check neighbor as number. */
1431 if (remote_as
!= peer
->as
)
1433 if (BGP_DEBUG (normal
, NORMAL
))
1434 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1435 peer
->host
, remote_as
, peer
->as
);
1436 bgp_notify_send_with_data (peer
,
1437 BGP_NOTIFY_OPEN_ERR
,
1438 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1439 notify_data_remote_as
, 2);
1443 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1444 calculate the value of the Hold Timer by using the smaller of its
1445 configured Hold Time and the Hold Time received in the OPEN message.
1446 The Hold Time MUST be either zero or at least three seconds. An
1447 implementation may reject connections on the basis of the Hold Time. */
1449 if (holdtime
< 3 && holdtime
!= 0)
1451 bgp_notify_send (peer
,
1452 BGP_NOTIFY_OPEN_ERR
,
1453 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
);
1457 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1458 would be one third of the Hold Time interval. KEEPALIVE messages
1459 MUST NOT be sent more frequently than one per second. An
1460 implementation MAY adjust the rate at which it sends KEEPALIVE
1461 messages as a function of the Hold Time interval. */
1463 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1464 send_holdtime
= peer
->holdtime
;
1466 send_holdtime
= peer
->bgp
->default_holdtime
;
1468 if (holdtime
< send_holdtime
)
1469 peer
->v_holdtime
= holdtime
;
1471 peer
->v_holdtime
= send_holdtime
;
1473 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1475 /* Open option part parse. */
1478 ret
= bgp_open_option_parse (peer
, optlen
, &capability
);
1484 if (BGP_DEBUG (normal
, NORMAL
))
1485 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
1489 /* Override capability. */
1490 if (! capability
|| CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
1492 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] = peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1493 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1494 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] = peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1495 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1499 bgp_getsockname (peer
);
1501 BGP_EVENT_ADD (peer
, Receive_OPEN_message
);
1503 peer
->packet_size
= 0;
1505 stream_reset (peer
->ibuf
);
1510 /* Parse BGP Update packet and make attribute object. */
1512 bgp_update_receive (struct peer
*peer
, bgp_size_t size
)
1518 bgp_size_t attribute_len
;
1519 bgp_size_t update_len
;
1520 bgp_size_t withdraw_len
;
1521 struct bgp_nlri update
;
1522 struct bgp_nlri withdraw
;
1523 struct bgp_nlri mp_update
;
1524 struct bgp_nlri mp_withdraw
;
1525 char attrstr
[BUFSIZ
] = "";
1527 /* Status must be Established. */
1528 if (peer
->status
!= Established
)
1530 zlog_err ("%s [FSM] Update packet received under status %s",
1531 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1532 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1536 /* Set initial values. */
1537 memset (&attr
, 0, sizeof (struct attr
));
1538 memset (&update
, 0, sizeof (struct bgp_nlri
));
1539 memset (&withdraw
, 0, sizeof (struct bgp_nlri
));
1540 memset (&mp_update
, 0, sizeof (struct bgp_nlri
));
1541 memset (&mp_withdraw
, 0, sizeof (struct bgp_nlri
));
1544 end
= stream_pnt (s
) + size
;
1546 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1547 Length is too large (i.e., if Unfeasible Routes Length + Total
1548 Attribute Length + 23 exceeds the message Length), then the Error
1549 Subcode is set to Malformed Attribute List. */
1550 if (stream_pnt (s
) + 2 > end
)
1552 zlog_err ("%s [Error] Update packet error"
1553 " (packet length is short for unfeasible length)",
1555 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1556 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1560 /* Unfeasible Route Length. */
1561 withdraw_len
= stream_getw (s
);
1563 /* Unfeasible Route Length check. */
1564 if (stream_pnt (s
) + withdraw_len
> end
)
1566 zlog_err ("%s [Error] Update packet error"
1567 " (packet unfeasible length overflow %d)",
1568 peer
->host
, withdraw_len
);
1569 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1570 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1574 /* Unfeasible Route packet format check. */
1575 if (withdraw_len
> 0)
1577 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, stream_pnt (s
), withdraw_len
);
1581 if (BGP_DEBUG (packet
, PACKET_RECV
))
1582 zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer
->host
);
1584 withdraw
.afi
= AFI_IP
;
1585 withdraw
.safi
= SAFI_UNICAST
;
1586 withdraw
.nlri
= stream_pnt (s
);
1587 withdraw
.length
= withdraw_len
;
1588 stream_forward_getp (s
, withdraw_len
);
1591 /* Attribute total length check. */
1592 if (stream_pnt (s
) + 2 > end
)
1594 zlog_warn ("%s [Error] Packet Error"
1595 " (update packet is short for attribute length)",
1597 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1598 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1602 /* Fetch attribute total length. */
1603 attribute_len
= stream_getw (s
);
1605 /* Attribute length check. */
1606 if (stream_pnt (s
) + attribute_len
> end
)
1608 zlog_warn ("%s [Error] Packet Error"
1609 " (update packet attribute length overflow %d)",
1610 peer
->host
, attribute_len
);
1611 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1612 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1616 /* Parse attribute when it exists. */
1619 ret
= bgp_attr_parse (peer
, &attr
, attribute_len
,
1620 &mp_update
, &mp_withdraw
);
1625 /* Logging the attribute. */
1626 if (BGP_DEBUG (update
, UPDATE_IN
))
1628 ret
= bgp_dump_attr (peer
, &attr
, attrstr
, BUFSIZ
);
1631 zlog (peer
->log
, LOG_DEBUG
, "%s rcvd UPDATE w/ attr: %s",
1632 peer
->host
, attrstr
);
1635 /* Network Layer Reachability Information. */
1636 update_len
= end
- stream_pnt (s
);
1640 /* Check NLRI packet format and prefix length. */
1641 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, stream_pnt (s
), update_len
);
1645 /* Set NLRI portion to structure. */
1646 update
.afi
= AFI_IP
;
1647 update
.safi
= SAFI_UNICAST
;
1648 update
.nlri
= stream_pnt (s
);
1649 update
.length
= update_len
;
1650 stream_forward_getp (s
, update_len
);
1653 /* NLRI is processed only when the peer is configured specific
1654 Address Family and Subsequent Address Family. */
1655 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
])
1657 if (withdraw
.length
)
1658 bgp_nlri_parse (peer
, NULL
, &withdraw
);
1662 /* We check well-known attribute only for IPv4 unicast
1664 ret
= bgp_attr_check (peer
, &attr
);
1668 bgp_nlri_parse (peer
, &attr
, &update
);
1671 if (mp_update
.length
1672 && mp_update
.afi
== AFI_IP
1673 && mp_update
.safi
== SAFI_UNICAST
)
1674 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1676 if (mp_withdraw
.length
1677 && mp_withdraw
.afi
== AFI_IP
1678 && mp_withdraw
.safi
== SAFI_UNICAST
)
1679 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1681 if (! attribute_len
&& ! withdraw_len
)
1683 /* End-of-RIB received */
1684 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_UNICAST
],
1685 PEER_STATUS_EOR_RECEIVED
);
1687 /* NSF delete stale route */
1688 if (peer
->nsf
[AFI_IP
][SAFI_UNICAST
])
1689 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_UNICAST
);
1691 if (BGP_DEBUG (normal
, NORMAL
))
1692 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv4 Unicast from %s",
1696 if (peer
->afc
[AFI_IP
][SAFI_MULTICAST
])
1698 if (mp_update
.length
1699 && mp_update
.afi
== AFI_IP
1700 && mp_update
.safi
== SAFI_MULTICAST
)
1701 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1703 if (mp_withdraw
.length
1704 && mp_withdraw
.afi
== AFI_IP
1705 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1706 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1709 && mp_withdraw
.afi
== AFI_IP
1710 && mp_withdraw
.safi
== SAFI_MULTICAST
1711 && mp_withdraw
.length
== 0)
1713 /* End-of-RIB received */
1714 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_MULTICAST
],
1715 PEER_STATUS_EOR_RECEIVED
);
1717 /* NSF delete stale route */
1718 if (peer
->nsf
[AFI_IP
][SAFI_MULTICAST
])
1719 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_MULTICAST
);
1721 if (BGP_DEBUG (normal
, NORMAL
))
1722 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv4 Multicast from %s",
1726 if (peer
->afc
[AFI_IP6
][SAFI_UNICAST
])
1728 if (mp_update
.length
1729 && mp_update
.afi
== AFI_IP6
1730 && mp_update
.safi
== SAFI_UNICAST
)
1731 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1733 if (mp_withdraw
.length
1734 && mp_withdraw
.afi
== AFI_IP6
1735 && mp_withdraw
.safi
== SAFI_UNICAST
)
1736 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1739 && mp_withdraw
.afi
== AFI_IP6
1740 && mp_withdraw
.safi
== SAFI_UNICAST
1741 && mp_withdraw
.length
== 0)
1743 /* End-of-RIB received */
1744 SET_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_UNICAST
], PEER_STATUS_EOR_RECEIVED
);
1746 /* NSF delete stale route */
1747 if (peer
->nsf
[AFI_IP6
][SAFI_UNICAST
])
1748 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_UNICAST
);
1750 if (BGP_DEBUG (normal
, NORMAL
))
1751 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv6 Unicast from %s",
1755 if (peer
->afc
[AFI_IP6
][SAFI_MULTICAST
])
1757 if (mp_update
.length
1758 && mp_update
.afi
== AFI_IP6
1759 && mp_update
.safi
== SAFI_MULTICAST
)
1760 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1762 if (mp_withdraw
.length
1763 && mp_withdraw
.afi
== AFI_IP6
1764 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1765 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1768 && mp_withdraw
.afi
== AFI_IP6
1769 && mp_withdraw
.safi
== SAFI_MULTICAST
1770 && mp_withdraw
.length
== 0)
1772 /* End-of-RIB received */
1774 /* NSF delete stale route */
1775 if (peer
->nsf
[AFI_IP6
][SAFI_MULTICAST
])
1776 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_MULTICAST
);
1778 if (BGP_DEBUG (update
, UPDATE_IN
))
1779 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv6 Multicast from %s",
1783 if (peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
])
1785 if (mp_update
.length
1786 && mp_update
.afi
== AFI_IP
1787 && mp_update
.safi
== BGP_SAFI_VPNV4
)
1788 bgp_nlri_parse_vpnv4 (peer
, &attr
, &mp_update
);
1790 if (mp_withdraw
.length
1791 && mp_withdraw
.afi
== AFI_IP
1792 && mp_withdraw
.safi
== BGP_SAFI_VPNV4
)
1793 bgp_nlri_parse_vpnv4 (peer
, NULL
, &mp_withdraw
);
1796 && mp_withdraw
.afi
== AFI_IP
1797 && mp_withdraw
.safi
== BGP_SAFI_VPNV4
1798 && mp_withdraw
.length
== 0)
1800 /* End-of-RIB received */
1802 if (BGP_DEBUG (update
, UPDATE_IN
))
1803 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for VPNv4 Unicast from %s",
1808 /* Everything is done. We unintern temporary structures which
1809 interned in bgp_attr_parse(). */
1811 aspath_unintern (attr
.aspath
);
1813 community_unintern (attr
.community
);
1816 if (attr
.extra
->ecommunity
)
1817 ecommunity_unintern (attr
.extra
->ecommunity
);
1818 if (attr
.extra
->cluster
)
1819 cluster_unintern (attr
.extra
->cluster
);
1820 if (attr
.extra
->transit
)
1821 transit_unintern (attr
.extra
->transit
);
1822 bgp_attr_extra_free (&attr
);
1825 /* If peering is stopped due to some reason, do not generate BGP
1827 if (peer
->status
!= Established
)
1830 /* Increment packet counter. */
1832 peer
->update_time
= time (NULL
);
1834 /* Generate BGP event. */
1835 BGP_EVENT_ADD (peer
, Receive_UPDATE_message
);
1840 /* Notify message treatment function. */
1842 bgp_notify_receive (struct peer
*peer
, bgp_size_t size
)
1844 struct bgp_notify bgp_notify
;
1846 if (peer
->notify
.data
)
1848 XFREE (MTYPE_TMP
, peer
->notify
.data
);
1849 peer
->notify
.data
= NULL
;
1850 peer
->notify
.length
= 0;
1853 bgp_notify
.code
= stream_getc (peer
->ibuf
);
1854 bgp_notify
.subcode
= stream_getc (peer
->ibuf
);
1855 bgp_notify
.length
= size
- 2;
1856 bgp_notify
.data
= NULL
;
1858 /* Preserv notify code and sub code. */
1859 peer
->notify
.code
= bgp_notify
.code
;
1860 peer
->notify
.subcode
= bgp_notify
.subcode
;
1861 /* For further diagnostic record returned Data. */
1862 if (bgp_notify
.length
)
1864 peer
->notify
.length
= size
- 2;
1865 peer
->notify
.data
= XMALLOC (MTYPE_TMP
, size
- 2);
1866 memcpy (peer
->notify
.data
, stream_pnt (peer
->ibuf
), size
- 2);
1875 if (bgp_notify
.length
)
1877 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
1878 for (i
= 0; i
< bgp_notify
.length
; i
++)
1881 sprintf (c
, " %02x", stream_getc (peer
->ibuf
));
1882 strcat (bgp_notify
.data
, c
);
1887 sprintf (c
, "%02x", stream_getc (peer
->ibuf
));
1888 strcpy (bgp_notify
.data
, c
);
1892 bgp_notify_print(peer
, &bgp_notify
, "received");
1893 if (bgp_notify
.data
)
1894 XFREE (MTYPE_TMP
, bgp_notify
.data
);
1897 /* peer count update */
1900 if (peer
->status
== Established
)
1901 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1903 /* We have to check for Notify with Unsupported Optional Parameter.
1904 in that case we fallback to open without the capability option.
1905 But this done in bgp_stop. We just mark it here to avoid changing
1907 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1908 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1909 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1911 /* Also apply to Unsupported Capability until remote router support
1913 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1914 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_CAPBL
)
1915 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1917 BGP_EVENT_ADD (peer
, Receive_NOTIFICATION_message
);
1920 /* Keepalive treatment function -- get keepalive send keepalive */
1922 bgp_keepalive_receive (struct peer
*peer
, bgp_size_t size
)
1924 if (BGP_DEBUG (keepalive
, KEEPALIVE
))
1925 zlog_debug ("%s KEEPALIVE rcvd", peer
->host
);
1927 BGP_EVENT_ADD (peer
, Receive_KEEPALIVE_message
);
1930 /* Route refresh message is received. */
1932 bgp_route_refresh_receive (struct peer
*peer
, bgp_size_t size
)
1939 /* If peer does not have the capability, send notification. */
1940 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_ADV
))
1942 plog_err (peer
->log
, "%s [Error] BGP route refresh is not enabled",
1944 bgp_notify_send (peer
,
1945 BGP_NOTIFY_HEADER_ERR
,
1946 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1950 /* Status must be Established. */
1951 if (peer
->status
!= Established
)
1953 plog_err (peer
->log
,
1954 "%s [Error] Route refresh packet received under status %s",
1955 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1956 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1963 afi
= stream_getw (s
);
1964 reserved
= stream_getc (s
);
1965 safi
= stream_getc (s
);
1967 if (BGP_DEBUG (normal
, NORMAL
))
1968 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1969 peer
->host
, afi
, safi
);
1971 /* Check AFI and SAFI. */
1972 if ((afi
!= AFI_IP
&& afi
!= AFI_IP6
)
1973 || (safi
!= SAFI_UNICAST
&& safi
!= SAFI_MULTICAST
1974 && safi
!= BGP_SAFI_VPNV4
))
1976 if (BGP_DEBUG (normal
, NORMAL
))
1978 zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1979 peer
->host
, afi
, safi
);
1984 /* Adjust safi code. */
1985 if (safi
== BGP_SAFI_VPNV4
)
1986 safi
= SAFI_MPLS_VPN
;
1988 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1991 u_char when_to_refresh
;
1995 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) < 5)
1997 zlog_info ("%s ORF route refresh length error", peer
->host
);
1998 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2002 when_to_refresh
= stream_getc (s
);
2003 end
= stream_pnt (s
) + (size
- 5);
2005 while ((stream_pnt (s
) + 2) < end
)
2007 orf_type
= stream_getc (s
);
2008 orf_len
= stream_getw (s
);
2010 /* orf_len in bounds? */
2011 if ((stream_pnt (s
) + orf_len
) > end
)
2012 break; /* XXX: Notify instead?? */
2013 if (orf_type
== ORF_TYPE_PREFIX
2014 || orf_type
== ORF_TYPE_PREFIX_OLD
)
2016 u_char
*p_pnt
= stream_pnt (s
);
2017 u_char
*p_end
= stream_pnt (s
) + orf_len
;
2018 struct orf_prefix orfp
;
2026 if (BGP_DEBUG (normal
, NORMAL
))
2028 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
2029 peer
->host
, orf_type
, orf_len
);
2032 /* we're going to read at least 1 byte of common ORF header,
2033 * and 7 bytes of ORF Address-filter entry from the stream
2038 /* ORF prefix-list name */
2039 sprintf (name
, "%s.%d.%d", peer
->host
, afi
, safi
);
2041 while (p_pnt
< p_end
)
2043 memset (&orfp
, 0, sizeof (struct orf_prefix
));
2045 if (common
& ORF_COMMON_PART_REMOVE_ALL
)
2047 if (BGP_DEBUG (normal
, NORMAL
))
2048 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer
->host
);
2049 prefix_bgp_orf_remove_all (name
);
2052 memcpy (&seq
, p_pnt
, sizeof (u_int32_t
));
2053 p_pnt
+= sizeof (u_int32_t
);
2054 orfp
.seq
= ntohl (seq
);
2057 orfp
.p
.prefixlen
= *p_pnt
++;
2058 orfp
.p
.family
= afi2family (afi
);
2059 psize
= PSIZE (orfp
.p
.prefixlen
);
2060 memcpy (&orfp
.p
.u
.prefix
, p_pnt
, psize
);
2063 if (BGP_DEBUG (normal
, NORMAL
))
2064 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d",
2066 (common
& ORF_COMMON_PART_REMOVE
? "Remove" : "Add"),
2067 (common
& ORF_COMMON_PART_DENY
? "deny" : "permit"),
2069 inet_ntop (orfp
.p
.family
, &orfp
.p
.u
.prefix
, buf
, BUFSIZ
),
2070 orfp
.p
.prefixlen
, orfp
.ge
, orfp
.le
);
2072 ret
= prefix_bgp_orf_set (name
, afi
, &orfp
,
2073 (common
& ORF_COMMON_PART_DENY
? 0 : 1 ),
2074 (common
& ORF_COMMON_PART_REMOVE
? 0 : 1));
2076 if (ret
!= CMD_SUCCESS
)
2078 if (BGP_DEBUG (normal
, NORMAL
))
2079 zlog_debug ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer
->host
);
2080 prefix_bgp_orf_remove_all (name
);
2084 peer
->orf_plist
[afi
][safi
] =
2085 prefix_list_lookup (AFI_ORF_PREFIX
, name
);
2087 stream_forward_getp (s
, orf_len
);
2089 if (BGP_DEBUG (normal
, NORMAL
))
2090 zlog_debug ("%s rcvd Refresh %s ORF request", peer
->host
,
2091 when_to_refresh
== REFRESH_DEFER
? "Defer" : "Immediate");
2092 if (when_to_refresh
== REFRESH_DEFER
)
2096 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2097 if (CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
))
2098 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
2100 /* Perform route refreshment to the peer */
2101 bgp_announce_route (peer
, afi
, safi
);
2105 bgp_capability_msg_parse (struct peer
*peer
, u_char
*pnt
, bgp_size_t length
)
2108 struct capability_mp_data mpc
;
2109 struct capability_header
*hdr
;
2120 /* We need at least action, capability code and capability length. */
2123 zlog_info ("%s Capability length error", peer
->host
);
2124 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2128 hdr
= (struct capability_header
*)(pnt
+ 1);
2130 /* Action value check. */
2131 if (action
!= CAPABILITY_ACTION_SET
2132 && action
!= CAPABILITY_ACTION_UNSET
)
2134 zlog_info ("%s Capability Action Value error %d",
2135 peer
->host
, action
);
2136 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2140 if (BGP_DEBUG (normal
, NORMAL
))
2141 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
2142 peer
->host
, action
, hdr
->code
, hdr
->length
);
2144 /* Capability length check. */
2145 if ((pnt
+ hdr
->length
+ 3) > end
)
2147 zlog_info ("%s Capability length error", peer
->host
);
2148 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2152 /* Fetch structure to the byte stream. */
2153 memcpy (&mpc
, pnt
+ 3, sizeof (struct capability_mp_data
));
2155 /* We know MP Capability Code. */
2156 if (hdr
->code
== CAPABILITY_CODE_MP
)
2158 afi
= ntohs (mpc
.afi
);
2161 /* Ignore capability when override-capability is set. */
2162 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
2165 if (!bgp_afi_safi_valid_indices (afi
, &safi
))
2167 if (BGP_DEBUG (normal
, NORMAL
))
2168 zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
2169 "(%u/%u)", peer
->host
, afi
, safi
);
2173 /* Address family check. */
2174 if (BGP_DEBUG (normal
, NORMAL
))
2175 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2177 action
== CAPABILITY_ACTION_SET
2178 ? "Advertising" : "Removing",
2179 ntohs(mpc
.afi
) , mpc
.safi
);
2181 if (action
== CAPABILITY_ACTION_SET
)
2183 peer
->afc_recv
[afi
][safi
] = 1;
2184 if (peer
->afc
[afi
][safi
])
2186 peer
->afc_nego
[afi
][safi
] = 1;
2187 bgp_announce_route (peer
, afi
, safi
);
2192 peer
->afc_recv
[afi
][safi
] = 0;
2193 peer
->afc_nego
[afi
][safi
] = 0;
2195 if (peer_active_nego (peer
))
2196 bgp_clear_route (peer
, afi
, safi
, BGP_CLEAR_ROUTE_NORMAL
);
2198 BGP_EVENT_ADD (peer
, BGP_Stop
);
2203 zlog_warn ("%s unrecognized capability code: %d - ignored",
2204 peer
->host
, hdr
->code
);
2206 pnt
+= hdr
->length
+ 3;
2211 /* Dynamic Capability is received.
2213 * This is exported for unit-test purposes
2216 bgp_capability_receive (struct peer
*peer
, bgp_size_t size
)
2220 /* Fetch pointer. */
2221 pnt
= stream_pnt (peer
->ibuf
);
2223 if (BGP_DEBUG (normal
, NORMAL
))
2224 zlog_debug ("%s rcv CAPABILITY", peer
->host
);
2226 /* If peer does not have the capability, send notification. */
2227 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_ADV
))
2229 plog_err (peer
->log
, "%s [Error] BGP dynamic capability is not enabled",
2231 bgp_notify_send (peer
,
2232 BGP_NOTIFY_HEADER_ERR
,
2233 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2237 /* Status must be Established. */
2238 if (peer
->status
!= Established
)
2240 plog_err (peer
->log
,
2241 "%s [Error] Dynamic capability packet received under status %s", peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
2242 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
2247 return bgp_capability_msg_parse (peer
, pnt
, size
);
2250 /* BGP read utility function. */
2252 bgp_read_packet (struct peer
*peer
)
2257 readsize
= peer
->packet_size
- stream_get_endp (peer
->ibuf
);
2259 /* If size is zero then return. */
2263 /* Read packet from fd. */
2264 nbytes
= stream_read_unblock (peer
->ibuf
, peer
->fd
, readsize
);
2266 /* If read byte is smaller than zero then error occured. */
2269 if (errno
== EAGAIN
)
2272 plog_err (peer
->log
, "%s [Error] bgp_read_packet error: %s",
2273 peer
->host
, safe_strerror (errno
));
2275 if (peer
->status
== Established
)
2277 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2279 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2280 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2283 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2286 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
2290 /* When read byte is zero : clear bgp peer and return */
2293 if (BGP_DEBUG (events
, EVENTS
))
2294 plog_debug (peer
->log
, "%s [Event] BGP connection closed fd %d",
2295 peer
->host
, peer
->fd
);
2297 if (peer
->status
== Established
)
2299 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2301 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2302 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2305 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2308 BGP_EVENT_ADD (peer
, TCP_connection_closed
);
2312 /* We read partial packet. */
2313 if (stream_get_endp (peer
->ibuf
) != peer
->packet_size
)
2321 bgp_marker_all_one (struct stream
*s
, int length
)
2325 for (i
= 0; i
< length
; i
++)
2326 if (s
->data
[i
] != 0xff)
2332 /* Starting point of packet process function. */
2334 bgp_read (struct thread
*thread
)
2340 char notify_data_length
[2];
2342 /* Yes first of all get peer pointer. */
2343 peer
= THREAD_ARG (thread
);
2344 peer
->t_read
= NULL
;
2346 /* For non-blocking IO check. */
2347 if (peer
->status
== Connect
)
2349 bgp_connect_check (peer
);
2356 zlog_err ("bgp_read peer's fd is negative value %d", peer
->fd
);
2359 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
2362 /* Read packet header to determine type of the packet */
2363 if (peer
->packet_size
== 0)
2364 peer
->packet_size
= BGP_HEADER_SIZE
;
2366 if (stream_get_endp (peer
->ibuf
) < BGP_HEADER_SIZE
)
2368 ret
= bgp_read_packet (peer
);
2370 /* Header read error or partial read packet. */
2374 /* Get size and type. */
2375 stream_forward_getp (peer
->ibuf
, BGP_MARKER_SIZE
);
2376 memcpy (notify_data_length
, stream_pnt (peer
->ibuf
), 2);
2377 size
= stream_getw (peer
->ibuf
);
2378 type
= stream_getc (peer
->ibuf
);
2380 if (BGP_DEBUG (normal
, NORMAL
) && type
!= 2 && type
!= 0)
2381 zlog_debug ("%s rcv message type %d, length (excl. header) %d",
2382 peer
->host
, type
, size
- BGP_HEADER_SIZE
);
2385 if (((type
== BGP_MSG_OPEN
) || (type
== BGP_MSG_KEEPALIVE
))
2386 && ! bgp_marker_all_one (peer
->ibuf
, BGP_MARKER_SIZE
))
2388 bgp_notify_send (peer
,
2389 BGP_NOTIFY_HEADER_ERR
,
2390 BGP_NOTIFY_HEADER_NOT_SYNC
);
2394 /* BGP type check. */
2395 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
2396 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
2397 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
2398 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
2399 && type
!= BGP_MSG_CAPABILITY
)
2401 if (BGP_DEBUG (normal
, NORMAL
))
2402 plog_debug (peer
->log
,
2403 "%s unknown message type 0x%02x",
2405 bgp_notify_send_with_data (peer
,
2406 BGP_NOTIFY_HEADER_ERR
,
2407 BGP_NOTIFY_HEADER_BAD_MESTYPE
,
2411 /* Mimimum packet length check. */
2412 if ((size
< BGP_HEADER_SIZE
)
2413 || (size
> BGP_MAX_PACKET_SIZE
)
2414 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
2415 || (type
== BGP_MSG_UPDATE
&& size
< BGP_MSG_UPDATE_MIN_SIZE
)
2416 || (type
== BGP_MSG_NOTIFY
&& size
< BGP_MSG_NOTIFY_MIN_SIZE
)
2417 || (type
== BGP_MSG_KEEPALIVE
&& size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
2418 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2419 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2420 || (type
== BGP_MSG_CAPABILITY
&& size
< BGP_MSG_CAPABILITY_MIN_SIZE
))
2422 if (BGP_DEBUG (normal
, NORMAL
))
2423 plog_debug (peer
->log
,
2424 "%s bad message length - %d for %s",
2426 type
== 128 ? "ROUTE-REFRESH" :
2427 bgp_type_str
[(int) type
]);
2428 bgp_notify_send_with_data (peer
,
2429 BGP_NOTIFY_HEADER_ERR
,
2430 BGP_NOTIFY_HEADER_BAD_MESLEN
,
2431 (u_char
*) notify_data_length
, 2);
2435 /* Adjust size to message length. */
2436 peer
->packet_size
= size
;
2439 ret
= bgp_read_packet (peer
);
2443 /* Get size and type again. */
2444 size
= stream_getw_from (peer
->ibuf
, BGP_MARKER_SIZE
);
2445 type
= stream_getc_from (peer
->ibuf
, BGP_MARKER_SIZE
+ 2);
2447 /* BGP packet dump function. */
2448 bgp_dump_packet (peer
, type
, peer
->ibuf
);
2450 size
= (peer
->packet_size
- BGP_HEADER_SIZE
);
2452 /* Read rest of the packet and call each sort of packet routine */
2457 bgp_open_receive (peer
, size
); /* XXX return value ignored! */
2459 case BGP_MSG_UPDATE
:
2460 peer
->readtime
= time(NULL
); /* Last read timer reset */
2461 bgp_update_receive (peer
, size
);
2463 case BGP_MSG_NOTIFY
:
2464 bgp_notify_receive (peer
, size
);
2466 case BGP_MSG_KEEPALIVE
:
2467 peer
->readtime
= time(NULL
); /* Last read timer reset */
2468 bgp_keepalive_receive (peer
, size
);
2470 case BGP_MSG_ROUTE_REFRESH_NEW
:
2471 case BGP_MSG_ROUTE_REFRESH_OLD
:
2473 bgp_route_refresh_receive (peer
, size
);
2475 case BGP_MSG_CAPABILITY
:
2476 peer
->dynamic_cap_in
++;
2477 bgp_capability_receive (peer
, size
);
2481 /* Clear input buffer. */
2482 peer
->packet_size
= 0;
2484 stream_reset (peer
->ibuf
);
2487 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
2489 if (BGP_DEBUG (events
, EVENTS
))
2490 zlog_debug ("%s [Event] Accepting BGP peer delete", peer
->host
);