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_putp (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_putp (s
);
82 stream_set_putp (s
, BGP_MARKER_SIZE
);
85 /* Write back current pointer. */
86 stream_set_putp (s
, cp
);
91 /* Add new packet to the peer. */
93 bgp_packet_add (struct peer
*peer
, struct stream
*s
)
95 /* Add packet to the end of list. */
96 stream_fifo_push (peer
->obuf
, s
);
99 /* Free first packet. */
101 bgp_packet_delete (struct peer
*peer
)
103 stream_free (stream_fifo_pop (peer
->obuf
));
106 /* Duplicate packet. */
108 bgp_packet_dup (struct stream
*s
)
112 new = stream_new (stream_get_endp (s
));
118 memcpy (new->data
, s
->data
, stream_get_endp (s
));
123 /* Check file descriptor whether connect is established. */
125 bgp_connect_check (struct peer
*peer
)
131 /* Anyway I have to reset read and write thread. */
132 BGP_READ_OFF (peer
->t_read
);
133 BGP_WRITE_OFF (peer
->t_write
);
135 /* Check file descriptor. */
136 slen
= sizeof (status
);
137 ret
= getsockopt(peer
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &status
, &slen
);
139 /* If getsockopt is fail, this is fatal error. */
142 zlog (peer
->log
, LOG_INFO
, "can't get sockopt for nonblocking connect");
143 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
147 /* When status is 0 then TCP connection is established. */
150 BGP_EVENT_ADD (peer
, TCP_connection_open
);
154 if (BGP_DEBUG (events
, EVENTS
))
155 plog_info (peer
->log
, "%s [Event] Connect failed (%s)",
156 peer
->host
, strerror (errno
));
157 BGP_EVENT_ADD (peer
, TCP_connection_open_failed
);
161 /* Make BGP update packet. */
163 bgp_update_packet (struct peer
*peer
, afi_t afi
, safi_t safi
)
166 struct bgp_adj_out
*adj
;
167 struct bgp_advertise
*adv
;
168 struct stream
*packet
;
169 struct bgp_node
*rn
= NULL
;
170 struct bgp_info
*binfo
= NULL
;
171 bgp_size_t total_attr_len
= 0;
174 struct prefix_rd
*prd
= NULL
;
180 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
);
191 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
194 #endif /* MPLS_VPN */
196 /* When remaining space can't include NLRI and it's length. */
197 if (rn
&& STREAM_REMAIN (s
) <= BGP_NLRI_LENGTH
+ PSIZE (rn
->p
.prefixlen
))
200 /* If packet is empty, set attribute. */
201 if (stream_empty (s
))
203 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
205 pos
= stream_get_putp (s
);
207 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
,
210 binfo
->peer
, prd
, tag
);
211 stream_putw_at (s
, pos
, total_attr_len
);
214 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
215 stream_put_prefix (s
, &rn
->p
);
217 if (BGP_DEBUG (update
, UPDATE_OUT
))
218 zlog (peer
->log
, LOG_INFO
, "%s send UPDATE %s/%d",
220 inet_ntop (rn
->p
.family
, &(rn
->p
.u
.prefix
), buf
, BUFSIZ
),
223 /* Synchnorize attribute. */
225 bgp_attr_unintern (adj
->attr
);
227 peer
->scount
[afi
][safi
]++;
229 adj
->attr
= bgp_attr_intern (adv
->baa
->attr
);
231 adv
= bgp_advertise_clean (peer
, adj
, afi
, safi
);
233 if (! (afi
== AFI_IP
&& safi
== SAFI_UNICAST
))
237 if (! stream_empty (s
))
239 bgp_packet_set_size (s
);
240 packet
= bgp_packet_dup (s
);
241 bgp_packet_add (peer
, packet
);
249 bgp_update_packet_eor (struct peer
*peer
, afi_t afi
, safi_t safi
)
252 struct stream
*packet
;
254 if (BGP_DEBUG (normal
, NORMAL
))
255 zlog_info ("Send End-of-RIB for AF %s to neighbor %s",
256 afi_safi_print(afi
, safi
), peer
->host
);
258 s
= stream_new (BGP_MAX_PACKET_SIZE
);
260 /* Make BGP update packet. */
261 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
263 /* Unfeasible Routes Length */
266 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
268 /* Total Path Attribute Length */
273 /* Total Path Attribute Length */
275 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
276 stream_putc (s
, BGP_ATTR_MP_UNREACH_NLRI
);
278 stream_putw (s
, afi
);
279 stream_putc (s
, safi
);
282 bgp_packet_set_size (s
);
283 packet
= bgp_packet_dup (s
);
284 bgp_packet_add (peer
, packet
);
289 /* Make BGP withdraw packet. */
291 bgp_withdraw_packet (struct peer
*peer
, afi_t afi
, safi_t safi
)
294 struct stream
*packet
;
295 struct bgp_adj_out
*adj
;
296 struct bgp_advertise
*adv
;
299 bgp_size_t unfeasible_len
;
300 bgp_size_t total_attr_len
;
302 struct prefix_rd
*prd
= NULL
;
307 while ((adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
)) != NULL
)
312 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
313 #endif /* MPLS_VPN */
315 if (STREAM_REMAIN (s
)
316 < (BGP_NLRI_LENGTH
+ BGP_TOTAL_ATTR_LEN
+ PSIZE (rn
->p
.prefixlen
)))
319 if (stream_empty (s
))
321 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
325 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
326 stream_put_prefix (s
, &rn
->p
);
329 pos
= stream_get_putp (s
);
332 = bgp_packet_withdraw (peer
, s
, &rn
->p
, afi
, safi
, prd
, NULL
);
334 /* Set total path attribute length. */
335 stream_putw_at (s
, pos
, total_attr_len
);
338 if (BGP_DEBUG (update
, UPDATE_OUT
))
339 zlog (peer
->log
, LOG_INFO
, "%s send UPDATE %s/%d -- unreachable",
341 inet_ntop (rn
->p
.family
, &(rn
->p
.u
.prefix
), buf
, BUFSIZ
),
344 peer
->scount
[afi
][safi
]--;
346 bgp_adj_out_remove (rn
, adj
, peer
, afi
, safi
);
347 bgp_unlock_node (rn
);
349 if (! (afi
== AFI_IP
&& safi
== SAFI_UNICAST
))
353 if (! stream_empty (s
))
355 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
358 = stream_get_putp (s
) - BGP_HEADER_SIZE
- BGP_UNFEASIBLE_LEN
;
359 stream_putw_at (s
, BGP_HEADER_SIZE
, unfeasible_len
);
362 bgp_packet_set_size (s
);
363 packet
= bgp_packet_dup (s
);
364 bgp_packet_add (peer
, packet
);
373 bgp_default_update_send (struct peer
*peer
, struct attr
*attr
,
374 afi_t afi
, safi_t safi
, struct peer
*from
)
377 struct stream
*packet
;
380 bgp_size_t total_attr_len
;
381 char attrstr
[BUFSIZ
];
384 #ifdef DISABLE_BGP_ANNOUNCE
386 #endif /* DISABLE_BGP_ANNOUNCE */
389 str2prefix ("0.0.0.0/0", &p
);
392 str2prefix ("::/0", &p
);
393 #endif /* HAVE_IPV6 */
395 /* Logging the attribute. */
396 if (BGP_DEBUG (update
, UPDATE_OUT
))
398 bgp_dump_attr (peer
, attr
, attrstr
, BUFSIZ
);
399 zlog (peer
->log
, LOG_INFO
, "%s send UPDATE %s/%d %s",
400 peer
->host
, inet_ntop(p
.family
, &(p
.u
.prefix
), buf
, BUFSIZ
),
401 p
.prefixlen
, attrstr
);
404 s
= stream_new (BGP_MAX_PACKET_SIZE
);
406 /* Make BGP update packet. */
407 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
409 /* Unfeasible Routes Length. */
412 /* Make place for total attribute length. */
413 pos
= stream_get_putp (s
);
415 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
, attr
, &p
, afi
, safi
, from
, NULL
, NULL
);
417 /* Set Total Path Attribute Length. */
418 stream_putw_at (s
, pos
, total_attr_len
);
421 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
)
422 stream_put_prefix (s
, &p
);
425 bgp_packet_set_size (s
);
427 packet
= bgp_packet_dup (s
);
430 /* Dump packet if debug option is set. */
432 bgp_packet_dump (packet
);
435 /* Add packet to the peer. */
436 bgp_packet_add (peer
, packet
);
438 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
442 bgp_default_withdraw_send (struct peer
*peer
, afi_t afi
, safi_t safi
)
445 struct stream
*packet
;
449 bgp_size_t unfeasible_len
;
450 bgp_size_t total_attr_len
;
453 #ifdef DISABLE_BGP_ANNOUNCE
455 #endif /* DISABLE_BGP_ANNOUNCE */
458 str2prefix ("0.0.0.0/0", &p
);
461 str2prefix ("::/0", &p
);
462 #endif /* HAVE_IPV6 */
467 if (BGP_DEBUG (update
, UPDATE_OUT
))
468 zlog (peer
->log
, LOG_INFO
, "%s send UPDATE %s/%d -- unreachable",
469 peer
->host
, inet_ntop(p
.family
, &(p
.u
.prefix
), buf
, BUFSIZ
),
472 s
= stream_new (BGP_MAX_PACKET_SIZE
);
474 /* Make BGP update packet. */
475 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
477 /* Unfeasible Routes Length. */;
478 cp
= stream_get_putp (s
);
481 /* Withdrawn Routes. */
482 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
)
484 stream_put_prefix (s
, &p
);
486 unfeasible_len
= stream_get_putp (s
) - cp
- 2;
488 /* Set unfeasible len. */
489 stream_putw_at (s
, cp
, unfeasible_len
);
491 /* Set total path attribute length. */
496 pos
= stream_get_putp (s
);
498 total_attr_len
= bgp_packet_withdraw (peer
, s
, &p
, afi
, safi
, NULL
, NULL
);
500 /* Set total path attribute length. */
501 stream_putw_at (s
, pos
, total_attr_len
);
504 bgp_packet_set_size (s
);
506 packet
= bgp_packet_dup (s
);
509 /* Add packet to the peer. */
510 bgp_packet_add (peer
, packet
);
512 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
515 /* Get next packet to be written. */
517 bgp_write_packet (struct peer
*peer
)
521 struct stream
*s
= NULL
;
522 struct bgp_advertise
*adv
;
524 s
= stream_fifo_head (peer
->obuf
);
528 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
529 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
531 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
);
534 s
= bgp_withdraw_packet (peer
, afi
, safi
);
540 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
541 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
543 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
);
546 if (adv
->binfo
&& adv
->binfo
->uptime
< peer
->synctime
[afi
][safi
])
548 if (CHECK_FLAG (adv
->binfo
->peer
->cap
, PEER_CAP_RESTART_RCV
)
549 && CHECK_FLAG (adv
->binfo
->peer
->cap
, PEER_CAP_RESTART_ADV
)
550 && ! CHECK_FLAG (adv
->binfo
->flags
, BGP_INFO_STALE
)
551 && safi
!= SAFI_MPLS_VPN
)
553 if (CHECK_FLAG (adv
->binfo
->peer
->af_sflags
[afi
][safi
],
554 PEER_STATUS_EOR_RECEIVED
))
555 s
= bgp_update_packet (peer
, afi
, safi
);
558 s
= bgp_update_packet (peer
, afi
, safi
);
565 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
567 if (peer
->afc_nego
[afi
][safi
] && peer
->synctime
[afi
][safi
]
568 && ! CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_SEND
)
569 && safi
!= SAFI_MPLS_VPN
)
571 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_SEND
);
572 return bgp_update_packet_eor (peer
, afi
, safi
);
580 /* Is there partially written packet or updates we can send right
583 bgp_write_proceed (struct peer
*peer
)
587 struct bgp_advertise
*adv
;
589 if (stream_fifo_head (peer
->obuf
))
592 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
593 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
594 if (FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
))
597 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
598 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
599 if ((adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
)) != NULL
)
600 if (adv
->binfo
->uptime
< peer
->synctime
[afi
][safi
])
606 /* Write packet to the peer. */
608 bgp_write (struct thread
*thread
)
617 /* Yes first of all get peer pointer. */
618 peer
= THREAD_ARG (thread
);
619 peer
->t_write
= NULL
;
621 /* For non-blocking IO check. */
622 if (peer
->status
== Connect
)
624 bgp_connect_check (peer
);
628 /* Nonblocking write until TCP output buffer is full. */
633 s
= bgp_write_packet (peer
);
637 /* Number of bytes to be sent. */
638 writenum
= stream_get_endp (s
) - stream_get_getp (s
);
640 /* Call write() system call. */
641 num
= write (peer
->fd
, STREAM_PNT (s
), writenum
);
646 if (write_errno
== EWOULDBLOCK
|| write_errno
== EAGAIN
)
651 bgp_timer_set (peer
);
656 stream_forward (s
, num
);
658 if (write_errno
== EAGAIN
)
664 /* Retrieve BGP packet type. */
665 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
666 type
= stream_getc (s
);
679 /* BGP_EVENT_ADD (peer, BGP_Stop); */
680 bgp_stop_with_error (peer
);
681 bgp_fsm_change_status (peer
, Idle
);
682 bgp_timer_set (peer
);
685 case BGP_MSG_KEEPALIVE
:
686 peer
->keepalive_out
++;
688 case BGP_MSG_ROUTE_REFRESH_NEW
:
689 case BGP_MSG_ROUTE_REFRESH_OLD
:
692 case BGP_MSG_CAPABILITY
:
693 peer
->dynamic_cap_out
++;
697 /* OK we send packet so delete it. */
698 bgp_packet_delete (peer
);
700 if (++count
>= BGP_WRITE_PACKET_MAX
)
704 if (bgp_write_proceed (peer
))
705 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
710 /* This is only for sending NOTIFICATION message to neighbor. */
712 bgp_write_notify (struct peer
*peer
)
718 /* There should be at least one packet. */
719 s
= stream_fifo_head (peer
->obuf
);
722 assert (stream_get_endp (s
) >= BGP_HEADER_SIZE
);
724 /* I'm not sure fd is writable. */
725 ret
= writen (peer
->fd
, STREAM_DATA (s
), stream_get_endp (s
));
730 bgp_timer_set (peer
);
734 /* Retrieve BGP packet type. */
735 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
736 type
= stream_getc (s
);
738 assert (type
== BGP_MSG_NOTIFY
);
740 /* Type should be notify. */
743 /* We don't call event manager at here for avoiding other events. */
744 if (peer
->status
== Established
)
748 bgp_stop_with_error (peer
);
749 bgp_fsm_change_status (peer
, Idle
);
751 bgp_timer_set (peer
);
756 /* Make keepalive packet and send it to the peer. */
758 bgp_keepalive_send (struct peer
*peer
)
763 s
= stream_new (BGP_MAX_PACKET_SIZE
);
765 /* Make keepalive packet. */
766 bgp_packet_set_marker (s
, BGP_MSG_KEEPALIVE
);
768 /* Set packet size. */
769 length
= bgp_packet_set_size (s
);
771 /* Dump packet if debug option is set. */
772 /* bgp_packet_dump (s); */
774 if (BGP_DEBUG (keepalive
, KEEPALIVE
))
775 zlog_info ("%s sending KEEPALIVE", peer
->host
);
777 /* Add packet to the peer. */
778 bgp_packet_add (peer
, s
);
780 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
783 /* Make open packet and send it to the peer. */
785 bgp_open_send (struct peer
*peer
)
789 u_int16_t send_holdtime
;
792 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
793 send_holdtime
= peer
->holdtime
;
795 send_holdtime
= peer
->bgp
->default_holdtime
;
797 /* local-as Change */
798 if (peer
->change_local_as
)
799 local_as
= peer
->change_local_as
;
801 local_as
= peer
->local_as
;
803 s
= stream_new (BGP_MAX_PACKET_SIZE
);
805 /* Make open packet. */
806 bgp_packet_set_marker (s
, BGP_MSG_OPEN
);
808 /* Set open packet values. */
809 stream_putc (s
, BGP_VERSION_4
); /* BGP version */
810 stream_putw (s
, local_as
); /* My Autonomous System*/
811 stream_putw (s
, send_holdtime
); /* Hold Time */
812 stream_put_in_addr (s
, &peer
->local_id
); /* BGP Identifier */
814 /* Set capability code. */
815 bgp_open_capability (s
, peer
);
817 /* Set BGP packet length. */
818 length
= bgp_packet_set_size (s
);
820 if (BGP_DEBUG (normal
, NORMAL
))
821 zlog_info ("%s sending OPEN, version %d, my as %d, holdtime %d, id %s",
822 peer
->host
, BGP_VERSION_4
, local_as
,
823 send_holdtime
, inet_ntoa (peer
->local_id
));
825 if (BGP_DEBUG (normal
, NORMAL
))
826 zlog_info ("%s send message type %d, length (incl. header) %d",
827 peer
->host
, BGP_MSG_OPEN
, length
);
829 /* Dump packet if debug option is set. */
830 /* bgp_packet_dump (s); */
832 /* Add packet to the peer. */
833 bgp_packet_add (peer
, s
);
835 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
838 /* Send BGP notify packet with data potion. */
840 bgp_notify_send_with_data (struct peer
*peer
, u_char code
, u_char sub_code
,
841 u_char
*data
, size_t datalen
)
846 /* Allocate new stream. */
847 s
= stream_new (BGP_MAX_PACKET_SIZE
);
849 /* Make nitify packet. */
850 bgp_packet_set_marker (s
, BGP_MSG_NOTIFY
);
852 /* Set notify packet values. */
853 stream_putc (s
, code
); /* BGP notify code */
854 stream_putc (s
, sub_code
); /* BGP notify sub_code */
856 /* If notify data is present. */
858 stream_write (s
, data
, datalen
);
860 /* Set BGP packet length. */
861 length
= bgp_packet_set_size (s
);
863 /* Add packet to the peer. */
864 stream_fifo_clean (peer
->obuf
);
865 bgp_packet_add (peer
, s
);
869 struct bgp_notify bgp_notify
;
874 bgp_notify
.code
= code
;
875 bgp_notify
.subcode
= sub_code
;
876 bgp_notify
.data
= NULL
;
877 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
879 if (bgp_notify
.length
)
881 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
882 for (i
= 0; i
< bgp_notify
.length
; i
++)
885 sprintf (c
, " %02x", data
[i
]);
886 strcat (bgp_notify
.data
, c
);
891 sprintf (c
, "%02x", data
[i
]);
892 strcpy (bgp_notify
.data
, c
);
895 bgp_notify_print (peer
, &bgp_notify
, "sending");
897 XFREE (MTYPE_TMP
, bgp_notify
.data
);
900 if (BGP_DEBUG (normal
, NORMAL
))
901 zlog_info ("%s send message type %d, length (incl. header) %d",
902 peer
->host
, BGP_MSG_NOTIFY
, length
);
904 /* peer reset cause */
905 if (sub_code
!= BGP_NOTIFY_CEASE_CONFIG_CHANGE
)
907 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
908 peer
->last_reset
= PEER_DOWN_USER_RESET
;
909 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
910 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
911 else if (sub_code
== BGP_NOTIFY_CEASE_PEER_UNCONFIG
)
912 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
914 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
916 /* Call imidiately. */
917 BGP_WRITE_OFF (peer
->t_write
);
919 bgp_write_notify (peer
);
922 /* Send BGP notify packet. */
924 bgp_notify_send (struct peer
*peer
, u_char code
, u_char sub_code
)
926 bgp_notify_send_with_data (peer
, code
, sub_code
, NULL
, 0);
934 else if (afi
== AFI_IP6
)
937 return "Unknown AFI";
941 safi2str (safi_t safi
)
943 if (safi
== SAFI_UNICAST
)
944 return "SAFI_UNICAST";
945 else if (safi
== SAFI_MULTICAST
)
946 return "SAFI_MULTICAST";
947 else if (safi
== SAFI_MPLS_VPN
|| safi
== BGP_SAFI_VPNV4
)
948 return "SAFI_MPLS_VPN";
950 return "Unknown SAFI";
953 /* Send route refresh message to the peer. */
955 bgp_route_refresh_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
956 u_char orf_type
, u_char when_to_refresh
, int remove
)
959 struct stream
*packet
;
961 struct bgp_filter
*filter
;
965 #ifdef DISABLE_BGP_ANNOUNCE
967 #endif /* DISABLE_BGP_ANNOUNCE */
969 if (CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
970 msg_type
= BGP_MSG_ROUTE_REFRESH_NEW
;
972 msg_type
= BGP_MSG_ROUTE_REFRESH_OLD
;
973 filter
= &peer
->filter
[afi
][safi
];
975 /* Adjust safi code. */
976 if (safi
== SAFI_MPLS_VPN
)
977 safi
= BGP_SAFI_VPNV4
;
979 s
= stream_new (BGP_MAX_PACKET_SIZE
);
981 /* Make BGP update packet. */
982 bgp_packet_set_marker (s
, msg_type
);
984 /* Encode Route Refresh message. */
985 stream_putw (s
, afi
);
987 stream_putc (s
, safi
);
989 if (orf_type
== ORF_TYPE_PREFIX
990 || orf_type
== ORF_TYPE_PREFIX_OLD
)
991 if (remove
|| filter
->plist
[FILTER_IN
].plist
)
997 stream_putc (s
, when_to_refresh
);
998 stream_putc (s
, orf_type
);
999 orfp
= stream_get_putp (s
);
1004 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
1005 stream_putc (s
, ORF_COMMON_PART_REMOVE_ALL
);
1006 if (BGP_DEBUG (normal
, NORMAL
))
1007 zlog_info ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
1008 peer
->host
, orf_type
,
1009 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
1014 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
1015 prefix_bgp_orf_entry (s
, filter
->plist
[FILTER_IN
].plist
,
1016 ORF_COMMON_PART_ADD
, ORF_COMMON_PART_PERMIT
,
1017 ORF_COMMON_PART_DENY
);
1018 if (BGP_DEBUG (normal
, NORMAL
))
1019 zlog_info ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
1020 peer
->host
, orf_type
,
1021 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
1025 /* Total ORF Entry Len. */
1026 orf_len
= stream_get_putp (s
) - orfp
- 2;
1027 stream_putw_at (s
, orfp
, orf_len
);
1030 /* Set packet size. */
1031 length
= bgp_packet_set_size (s
);
1033 if (BGP_DEBUG (normal
, NORMAL
))
1036 zlog_info ("%s sending REFRESH_REQ(%d) for afi/safi: %d/%d",
1037 peer
->host
, msg_type
, afi
, safi
);
1038 zlog_info ("%s send message type %d, length (incl. header) %d",
1039 peer
->host
, msg_type
, length
);
1042 /* Make real packet. */
1043 packet
= bgp_packet_dup (s
);
1046 /* Add packet to the peer. */
1047 bgp_packet_add (peer
, packet
);
1049 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1052 /* Send capability message to the peer. */
1054 bgp_capability_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
1055 int capability_code
, int action
)
1058 struct stream
*packet
;
1061 /* Adjust safi code. */
1062 if (safi
== SAFI_MPLS_VPN
)
1063 safi
= BGP_SAFI_VPNV4
;
1065 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1067 /* Make BGP update packet. */
1068 bgp_packet_set_marker (s
, BGP_MSG_CAPABILITY
);
1070 /* Encode MP_EXT capability. */
1071 if (capability_code
== CAPABILITY_CODE_MP
)
1073 stream_putc (s
, action
);
1074 stream_putc (s
, CAPABILITY_CODE_MP
);
1075 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
1076 stream_putw (s
, afi
);
1078 stream_putc (s
, safi
);
1080 if (BGP_DEBUG (normal
, NORMAL
))
1081 zlog_info ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
1082 peer
->host
, action
== CAPABILITY_ACTION_SET
?
1083 "Advertising" : "Removing", afi
, safi
);
1086 /* Set packet size. */
1087 length
= bgp_packet_set_size (s
);
1089 /* Make real packet. */
1090 packet
= bgp_packet_dup (s
);
1093 /* Add packet to the peer. */
1094 bgp_packet_add (peer
, packet
);
1096 if (BGP_DEBUG (normal
, NORMAL
))
1097 zlog_info ("%s send message type %d, length (incl. header) %d",
1098 peer
->host
, BGP_MSG_CAPABILITY
, length
);
1100 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1103 /* RFC1771 6.8 Connection collision detection. */
1105 bgp_collision_detect (struct peer
*new, struct in_addr remote_id
)
1108 struct listnode
*nn
;
1111 bgp
= bgp_get_default ();
1115 /* Upon receipt of an OPEN message, the local system must examine
1116 all of its connections that are in the OpenConfirm state. A BGP
1117 speaker may also examine connections in an OpenSent state if it
1118 knows the BGP Identifier of the peer by means outside of the
1119 protocol. If among these connections there is a connection to a
1120 remote BGP speaker whose BGP Identifier equals the one in the
1121 OPEN message, then the local system performs the following
1122 collision resolution procedure: */
1124 LIST_LOOP (bgp
->peer
, peer
, nn
)
1126 /* Under OpenConfirm status, local peer structure already hold
1127 remote router ID. */
1130 && (peer
->status
== OpenConfirm
|| peer
->status
== OpenSent
)
1131 && sockunion_same (&peer
->su
, &new->su
))
1133 /* 1. The BGP Identifier of the local system is compared to
1134 the BGP Identifier of the remote system (as specified in
1135 the OPEN message). */
1137 if (ntohl (peer
->local_id
.s_addr
) < ntohl (remote_id
.s_addr
))
1139 /* 2. If the value of the local BGP Identifier is less
1140 than the remote one, the local system closes BGP
1141 connection that already exists (the one that is
1142 already in the OpenConfirm state), and accepts BGP
1143 connection initiated by the remote system. */
1146 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_CEASE_CONNECT_COLLISION
);
1151 /* 3. Otherwise, the local system closes newly created
1152 BGP connection (the one associated with the newly
1153 received OPEN message), and continues to use the
1154 existing one (the one that is already in the
1155 OpenConfirm state). */
1158 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_CEASE_CONNECT_COLLISION
);
1167 bgp_open_receive (struct peer
*peer
, bgp_size_t size
)
1173 u_int16_t send_holdtime
;
1175 struct peer
*realpeer
;
1176 struct in_addr remote_id
;
1178 char notify_data_remote_as
[2];
1179 char notify_data_remote_id
[4];
1183 /* Parse open packet. */
1184 version
= stream_getc (peer
->ibuf
);
1185 memcpy (notify_data_remote_as
, stream_pnt (peer
->ibuf
), 2);
1186 remote_as
= stream_getw (peer
->ibuf
);
1187 holdtime
= stream_getw (peer
->ibuf
);
1188 memcpy (notify_data_remote_id
, stream_pnt (peer
->ibuf
), 4);
1189 remote_id
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
1191 /* Receive OPEN message log */
1192 if (BGP_DEBUG (normal
, NORMAL
))
1193 zlog_info ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s",
1194 peer
->host
, version
, remote_as
, holdtime
,
1195 inet_ntoa (remote_id
));
1197 /* Lookup peer from Open packet. */
1198 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1202 realpeer
= peer_lookup_with_open (&peer
->su
, remote_as
, &remote_id
, &as
);
1206 /* Peer's source IP address is check in bgp_accept(), so this
1207 must be AS number mismatch or remote-id configuration
1211 if (BGP_DEBUG (normal
, NORMAL
))
1212 zlog_info ("%s bad OPEN, wrong router identifier %s",
1213 peer
->host
, inet_ntoa (remote_id
));
1214 bgp_notify_send_with_data (peer
,
1215 BGP_NOTIFY_OPEN_ERR
,
1216 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1217 notify_data_remote_id
, 4);
1221 if (BGP_DEBUG (normal
, NORMAL
))
1222 zlog_info ("%s bad OPEN, remote AS is %d, expected %d",
1223 peer
->host
, remote_as
, peer
->as
);
1224 bgp_notify_send_with_data (peer
,
1225 BGP_NOTIFY_OPEN_ERR
,
1226 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1227 notify_data_remote_as
, 2);
1233 /* When collision is detected and this peer is closed. Retrun
1235 ret
= bgp_collision_detect (peer
, remote_id
);
1240 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1242 if (CHECK_FLAG (realpeer
->flags
, PEER_FLAG_CONNECT_MODE_ACTIVE
))
1244 if (BGP_DEBUG (normal
, NORMAL
))
1245 zlog_info ("%s passive open failed - TCP session must be opened actively",
1247 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_CEASE_CONNECT_REJECT
);
1251 if (realpeer
->status
== Established
1252 && CHECK_FLAG (realpeer
->sflags
, PEER_STATUS_NSF_MODE
))
1254 realpeer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
1255 SET_FLAG (realpeer
->sflags
, PEER_STATUS_NSF_WAIT
);
1257 else if (ret
== 0 && realpeer
->status
!= Active
1258 && realpeer
->status
!= OpenSent
1259 && realpeer
->status
!= OpenConfirm
)
1261 if (BGP_DEBUG (events
, EVENTS
))
1262 zlog_info ("%s peer status is %s close connection",
1263 realpeer
->host
, LOOKUP (bgp_status_msg
, realpeer
->status
));
1264 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_CEASE_CONNECT_REJECT
);
1268 if (BGP_DEBUG (events
, EVENTS
))
1269 zlog_info ("%s [Event] Transfer temporary BGP peer to existing one",
1272 bgp_stop (realpeer
);
1274 /* Transfer file descriptor. */
1275 realpeer
->fd
= peer
->fd
;
1278 /* Transfer input buffer. */
1279 stream_free (realpeer
->ibuf
);
1280 realpeer
->ibuf
= peer
->ibuf
;
1281 realpeer
->packet_size
= peer
->packet_size
;
1284 /* Transfer status. */
1285 bgp_fsm_change_status (realpeer
, peer
->status
);
1288 /* peer pointer change. Open packet send to neighbor. */
1290 bgp_open_send (peer
);
1293 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1297 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1300 /* remote router-id check. */
1301 if (remote_id
.s_addr
== 0
1302 || ntohl (remote_id
.s_addr
) >= 0xe0000000
1303 || ntohl (peer
->local_id
.s_addr
) == ntohl (remote_id
.s_addr
))
1305 if (BGP_DEBUG (normal
, NORMAL
))
1306 zlog_info ("%s bad OPEN, wrong router identifier %s",
1307 peer
->host
, inet_ntoa (remote_id
));
1308 bgp_notify_send_with_data (peer
,
1309 BGP_NOTIFY_OPEN_ERR
,
1310 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1311 notify_data_remote_id
, 4);
1315 /* Set remote router-id */
1316 peer
->remote_id
= remote_id
;
1318 /* Peer BGP version check. */
1319 if (version
!= BGP_VERSION_4
)
1321 if (BGP_DEBUG (normal
, NORMAL
))
1322 zlog_info ("%s bad protocol version, remote requested %d, local request %d",
1323 peer
->host
, version
, BGP_VERSION_4
);
1324 bgp_notify_send_with_data (peer
,
1325 BGP_NOTIFY_OPEN_ERR
,
1326 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1331 /* Check neighbor as number. */
1332 if (remote_as
!= peer
->as
)
1334 if (BGP_DEBUG (normal
, NORMAL
))
1335 zlog_info ("%s bad OPEN, remote AS is %d, expected %d",
1336 peer
->host
, remote_as
, peer
->as
);
1337 bgp_notify_send_with_data (peer
,
1338 BGP_NOTIFY_OPEN_ERR
,
1339 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1340 notify_data_remote_as
, 2);
1344 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1345 calculate the value of the Hold Timer by using the smaller of its
1346 configured Hold Time and the Hold Time received in the OPEN message.
1347 The Hold Time MUST be either zero or at least three seconds. An
1348 implementation may reject connections on the basis of the Hold Time. */
1350 if (holdtime
< 3 && holdtime
!= 0)
1352 bgp_notify_send (peer
,
1353 BGP_NOTIFY_OPEN_ERR
,
1354 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
);
1358 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1359 would be one third of the Hold Time interval. KEEPALIVE messages
1360 MUST NOT be sent more frequently than one per second. An
1361 implementation MAY adjust the rate at which it sends KEEPALIVE
1362 messages as a function of the Hold Time interval. */
1364 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1365 send_holdtime
= peer
->holdtime
;
1367 send_holdtime
= peer
->bgp
->default_holdtime
;
1369 if (holdtime
< send_holdtime
)
1370 peer
->v_holdtime
= holdtime
;
1372 peer
->v_holdtime
= send_holdtime
;
1374 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1376 /* Open option part parse. */
1378 optlen
= stream_getc (peer
->ibuf
);
1381 ret
= bgp_open_option_parse (peer
, optlen
, &capability
);
1385 stream_forward (peer
->ibuf
, optlen
);
1389 if (BGP_DEBUG (normal
, NORMAL
))
1390 zlog_info ("%s rcvd OPEN w/ OPTION parameter len: 0",
1394 /* Override capability. */
1395 if (! capability
|| CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
1397 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] = peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1398 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1399 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] = peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1400 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1404 bgp_getsockname (peer
);
1406 BGP_EVENT_ADD (peer
, Receive_OPEN_message
);
1408 peer
->packet_size
= 0;
1410 stream_reset (peer
->ibuf
);
1415 /* Parse BGP Update packet and make attribute object. */
1417 bgp_update_receive (struct peer
*peer
, bgp_size_t size
)
1423 bgp_size_t attribute_len
;
1424 bgp_size_t update_len
;
1425 bgp_size_t withdraw_len
;
1426 struct bgp_nlri update
;
1427 struct bgp_nlri withdraw
;
1428 struct bgp_nlri mp_update
;
1429 struct bgp_nlri mp_withdraw
;
1430 char attrstr
[BUFSIZ
] = "";
1432 /* Status must be Established. */
1433 if (peer
->status
!= Established
)
1435 zlog_err ("%s [FSM] Update packet received under status %s",
1436 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1437 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1441 /* Set initial values. */
1442 memset (&attr
, 0, sizeof (struct attr
));
1443 memset (&update
, 0, sizeof (struct bgp_nlri
));
1444 memset (&withdraw
, 0, sizeof (struct bgp_nlri
));
1445 memset (&mp_update
, 0, sizeof (struct bgp_nlri
));
1446 memset (&mp_withdraw
, 0, sizeof (struct bgp_nlri
));
1449 end
= stream_pnt (s
) + size
;
1451 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1452 Length is too large (i.e., if Unfeasible Routes Length + Total
1453 Attribute Length + 23 exceeds the message Length), then the Error
1454 Subcode is set to Malformed Attribute List. */
1455 if (stream_pnt (s
) + 2 > end
)
1457 zlog_err ("%s [Error] Update packet error"
1458 " (packet length is short for unfeasible length)",
1460 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1461 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1465 /* Unfeasible Route Length. */
1466 withdraw_len
= stream_getw (s
);
1468 /* Unfeasible Route Length check. */
1469 if (stream_pnt (s
) + withdraw_len
> end
)
1471 zlog_err ("%s [Error] Update packet error"
1472 " (packet unfeasible length overflow %d)",
1473 peer
->host
, withdraw_len
);
1474 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1475 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1479 /* Unfeasible Route packet format check. */
1480 if (withdraw_len
> 0)
1482 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, stream_pnt (s
), withdraw_len
);
1486 if (BGP_DEBUG (packet
, PACKET_RECV
))
1487 zlog_info ("%s [Update:RECV] Unfeasible NLRI received", peer
->host
);
1489 withdraw
.afi
= AFI_IP
;
1490 withdraw
.safi
= SAFI_UNICAST
;
1491 withdraw
.nlri
= stream_pnt (s
);
1492 withdraw
.length
= withdraw_len
;
1493 stream_forward (s
, withdraw_len
);
1496 /* Attribute total length check. */
1497 if (stream_pnt (s
) + 2 > end
)
1499 zlog_warn ("%s [Error] Packet Error"
1500 " (update packet is short for attribute length)",
1502 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1503 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1507 /* Fetch attribute total length. */
1508 attribute_len
= stream_getw (s
);
1510 /* Attribute length check. */
1511 if (stream_pnt (s
) + attribute_len
> end
)
1513 zlog_warn ("%s [Error] Packet Error"
1514 " (update packet attribute length overflow %d)",
1515 peer
->host
, attribute_len
);
1516 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1517 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1521 /* Parse attribute when it exists. */
1524 ret
= bgp_attr_parse (peer
, &attr
, attribute_len
,
1525 &mp_update
, &mp_withdraw
);
1530 /* Logging the attribute. */
1531 if (BGP_DEBUG (update
, UPDATE_IN
))
1533 ret
= bgp_dump_attr (peer
, &attr
, attrstr
, BUFSIZ
);
1536 zlog (peer
->log
, LOG_INFO
, "%s rcvd UPDATE w/ attr: %s",
1537 peer
->host
, attrstr
);
1540 /* Network Layer Reachability Information. */
1541 update_len
= end
- stream_pnt (s
);
1545 /* Check NLRI packet format and prefix length. */
1546 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, stream_pnt (s
), update_len
);
1550 /* Set NLRI portion to structure. */
1551 update
.afi
= AFI_IP
;
1552 update
.safi
= SAFI_UNICAST
;
1553 update
.nlri
= stream_pnt (s
);
1554 update
.length
= update_len
;
1555 stream_forward (s
, update_len
);
1558 /* NLRI is processed only when the peer is configured specific
1559 Address Family and Subsequent Address Family. */
1560 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
])
1562 if (withdraw
.length
)
1563 bgp_nlri_parse (peer
, NULL
, &withdraw
);
1567 /* We check well-known attribute only for IPv4 unicast
1569 ret
= bgp_attr_check (peer
, &attr
);
1573 bgp_nlri_parse (peer
, &attr
, &update
);
1576 if (mp_update
.length
1577 && mp_update
.afi
== AFI_IP
1578 && mp_update
.safi
== SAFI_UNICAST
)
1579 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1581 if (mp_withdraw
.length
1582 && mp_withdraw
.afi
== AFI_IP
1583 && mp_withdraw
.safi
== SAFI_UNICAST
)
1584 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1586 if (! attribute_len
&& ! withdraw_len
)
1588 /* End-of-RIB received */
1589 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_UNICAST
], PEER_STATUS_EOR_RECEIVED
);
1591 if (BGP_DEBUG (normal
, NORMAL
))
1592 zlog_info ("neighbor %s sent End-of-RIB marker for IPv4 Unicast",
1595 /* NSF delete stale route */
1596 if (peer
->nsf
[AFI_IP
][SAFI_UNICAST
])
1597 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_UNICAST
);
1600 if (peer
->afc
[AFI_IP
][SAFI_MULTICAST
])
1602 if (mp_update
.length
1603 && mp_update
.afi
== AFI_IP
1604 && mp_update
.safi
== SAFI_MULTICAST
)
1605 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1607 if (mp_withdraw
.length
1608 && mp_withdraw
.afi
== AFI_IP
1609 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1610 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1613 && mp_withdraw
.afi
== AFI_IP
1614 && mp_withdraw
.safi
== SAFI_MULTICAST
1615 && mp_withdraw
.length
== 0)
1617 /* End-of-RIB received */
1618 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_MULTICAST
], PEER_STATUS_EOR_RECEIVED
);
1620 if (BGP_DEBUG (normal
, NORMAL
))
1621 zlog_info ("neighbor %s sent End-of-RIB marker for IPv4 Multicast",
1624 /* NSF delete stale route */
1625 if (peer
->nsf
[AFI_IP
][SAFI_MULTICAST
])
1626 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_MULTICAST
);
1629 if (peer
->afc
[AFI_IP6
][SAFI_UNICAST
])
1631 if (mp_update
.length
1632 && mp_update
.afi
== AFI_IP6
1633 && mp_update
.safi
== SAFI_UNICAST
)
1634 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1636 if (mp_withdraw
.length
1637 && mp_withdraw
.afi
== AFI_IP6
1638 && mp_withdraw
.safi
== SAFI_UNICAST
)
1639 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1642 && mp_withdraw
.afi
== AFI_IP6
1643 && mp_withdraw
.safi
== SAFI_UNICAST
1644 && mp_withdraw
.length
== 0)
1646 /* End-of-RIB received */
1647 SET_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_UNICAST
], PEER_STATUS_EOR_RECEIVED
);
1649 if (BGP_DEBUG (normal
, NORMAL
))
1650 zlog_info ("neighbor %s sent End-of-RIB marker for IPv6 Unicast",
1653 /* NSF delete stale route */
1654 if (peer
->nsf
[AFI_IP6
][SAFI_UNICAST
])
1655 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_UNICAST
);
1658 if (peer
->afc
[AFI_IP6
][SAFI_MULTICAST
])
1660 if (mp_update
.length
1661 && mp_update
.afi
== AFI_IP6
1662 && mp_update
.safi
== SAFI_MULTICAST
)
1663 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1665 if (mp_withdraw
.length
1666 && mp_withdraw
.afi
== AFI_IP6
1667 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1668 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1671 && mp_withdraw
.afi
== AFI_IP6
1672 && mp_withdraw
.safi
== SAFI_MULTICAST
1673 && mp_withdraw
.length
== 0)
1675 /* End-of-RIB received */
1676 SET_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_MULTICAST
], PEER_STATUS_EOR_RECEIVED
);
1678 if (BGP_DEBUG (normal
, NORMAL
))
1679 zlog_info ("neighbor %s sent End-of-RIB marker for IPv6 Multicast",
1682 /* NSF delete stale route */
1683 if (peer
->nsf
[AFI_IP6
][SAFI_MULTICAST
])
1684 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_MULTICAST
);
1687 if (peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
])
1689 if (mp_update
.length
1690 && mp_update
.afi
== AFI_IP
1691 && mp_update
.safi
== BGP_SAFI_VPNV4
)
1692 bgp_nlri_parse_vpnv4 (peer
, &attr
, &mp_update
);
1694 if (mp_withdraw
.length
1695 && mp_withdraw
.afi
== AFI_IP
1696 && mp_withdraw
.safi
== BGP_SAFI_VPNV4
)
1697 bgp_nlri_parse_vpnv4 (peer
, NULL
, &mp_withdraw
);
1700 && mp_withdraw
.afi
== AFI_IP
1701 && mp_withdraw
.safi
== BGP_SAFI_VPNV4
1702 && mp_withdraw
.length
== 0)
1704 /* End-of-RIB received */
1706 if (BGP_DEBUG (normal
, NORMAL
))
1707 zlog_info ("neighbor %s sent End-of-RIB marker for VPNv4 Unicast",
1712 /* Everything is done. We unintern temporary structures which
1713 interned in bgp_attr_parse(). */
1715 aspath_unintern (attr
.aspath
);
1717 community_unintern (attr
.community
);
1718 if (attr
.ecommunity
)
1719 ecommunity_unintern (attr
.ecommunity
);
1721 cluster_unintern (attr
.cluster
);
1723 transit_unintern (attr
.transit
);
1725 /* If peering is stopped due to some reason, do not generate BGP
1727 if (peer
->status
!= Established
)
1730 /* Increment packet counter. */
1732 peer
->update_time
= time (NULL
);
1734 /* Generate BGP event. */
1735 BGP_EVENT_ADD (peer
, Receive_UPDATE_message
);
1740 /* Notify message treatment function. */
1742 bgp_notify_receive (struct peer
*peer
, bgp_size_t size
)
1744 struct bgp_notify bgp_notify
;
1746 if (peer
->notify
.data
)
1748 XFREE (MTYPE_TMP
, peer
->notify
.data
);
1749 peer
->notify
.data
= NULL
;
1750 peer
->notify
.length
= 0;
1753 bgp_notify
.code
= stream_getc (peer
->ibuf
);
1754 bgp_notify
.subcode
= stream_getc (peer
->ibuf
);
1755 bgp_notify
.length
= size
- 2;
1756 bgp_notify
.data
= NULL
;
1758 /* Preserv notify code and sub code. */
1759 peer
->notify
.code
= bgp_notify
.code
;
1760 peer
->notify
.subcode
= bgp_notify
.subcode
;
1761 /* For further diagnostic record returned Data. */
1762 if (bgp_notify
.length
)
1764 peer
->notify
.length
= size
- 2;
1765 peer
->notify
.data
= XMALLOC (MTYPE_TMP
, size
- 2);
1766 memcpy (peer
->notify
.data
, stream_pnt (peer
->ibuf
), size
- 2);
1775 if (bgp_notify
.length
)
1777 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
1778 for (i
= 0; i
< bgp_notify
.length
; i
++)
1781 sprintf (c
, " %02x", stream_getc (peer
->ibuf
));
1782 strcat (bgp_notify
.data
, c
);
1787 sprintf (c
, "%02x", stream_getc (peer
->ibuf
));
1788 strcpy (bgp_notify
.data
, c
);
1792 bgp_notify_print(peer
, &bgp_notify
, "received");
1793 if (bgp_notify
.data
)
1794 XFREE (MTYPE_TMP
, bgp_notify
.data
);
1797 /* peer count update */
1800 if (peer
->status
== Established
)
1801 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1802 /* We have to check for Notify with Unsupported Optional Parameter.
1803 in that case we fallback to open without the capability option.
1804 But this done in bgp_stop. We just mark it here to avoid changing
1806 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1807 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1808 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1810 /* Also apply to Unsupported Capability until remote router support
1812 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1813 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_CAPBL
)
1814 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1816 BGP_EVENT_ADD (peer
, Receive_NOTIFICATION_message
);
1819 /* Keepalive treatment function -- get keepalive send keepalive */
1821 bgp_keepalive_receive (struct peer
*peer
, bgp_size_t size
)
1823 if (BGP_DEBUG (keepalive
, KEEPALIVE
))
1824 zlog_info ("%s received KEEPALIVE, length (excl. header) %d", peer
->host
, size
);
1826 BGP_EVENT_ADD (peer
, Receive_KEEPALIVE_message
);
1829 /* Route refresh message is received. */
1831 bgp_route_refresh_receive (struct peer
*peer
, bgp_size_t size
)
1838 /* If peer does not have the capability, send notification. */
1839 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_ADV
))
1841 plog_err (peer
->log
, "%s [Error] BGP route refresh is not enabled",
1843 bgp_notify_send (peer
,
1844 BGP_NOTIFY_HEADER_ERR
,
1845 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1849 /* Status must be Established. */
1850 if (peer
->status
!= Established
)
1852 plog_err (peer
->log
,
1853 "%s [Error] Route refresh packet received under status %s",
1854 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1855 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1862 afi
= stream_getw (s
);
1863 reserved
= stream_getc (s
);
1864 safi
= stream_getc (s
);
1866 if (BGP_DEBUG (normal
, NORMAL
))
1867 zlog_info ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1868 peer
->host
, afi
, safi
);
1870 /* Check AFI and SAFI. */
1871 if ((afi
!= AFI_IP
&& afi
!= AFI_IP6
)
1872 || (safi
!= SAFI_UNICAST
&& safi
!= SAFI_MULTICAST
1873 && safi
!= BGP_SAFI_VPNV4
))
1875 if (BGP_DEBUG (normal
, NORMAL
))
1877 zlog_info ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1878 peer
->host
, afi
, safi
);
1883 /* Adjust safi code. */
1884 if (safi
== BGP_SAFI_VPNV4
)
1885 safi
= SAFI_MPLS_VPN
;
1887 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1890 u_char when_to_refresh
;
1894 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) < 5)
1896 zlog_info ("%s ORF route refresh length error", peer
->host
);
1897 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1901 when_to_refresh
= stream_getc (s
);
1902 end
= stream_pnt (s
) + (size
- 5);
1904 while (stream_pnt (s
) < end
)
1906 orf_type
= stream_getc (s
);
1907 orf_len
= stream_getw (s
);
1909 if (orf_type
== ORF_TYPE_PREFIX
1910 || orf_type
== ORF_TYPE_PREFIX_OLD
)
1912 u_char
*p_pnt
= stream_pnt (s
);
1913 u_char
*p_end
= stream_pnt (s
) + orf_len
;
1914 struct orf_prefix orfp
;
1922 if (BGP_DEBUG (normal
, NORMAL
))
1924 zlog_info ("%s rcvd Prefixlist ORF(%d) length %d",
1925 peer
->host
, orf_type
, orf_len
);
1928 /* ORF prefix-list name */
1929 sprintf (name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1931 while (p_pnt
< p_end
)
1933 memset (&orfp
, 0, sizeof (struct orf_prefix
));
1935 if (common
& ORF_COMMON_PART_REMOVE_ALL
)
1937 if (BGP_DEBUG (normal
, NORMAL
))
1938 zlog_info ("%s rcvd Remove-All pfxlist ORF request", peer
->host
);
1939 prefix_bgp_orf_remove_all (name
);
1942 memcpy (&seq
, p_pnt
, sizeof (u_int32_t
));
1943 p_pnt
+= sizeof (u_int32_t
);
1944 orfp
.seq
= ntohl (seq
);
1947 orfp
.p
.prefixlen
= *p_pnt
++;
1948 orfp
.p
.family
= afi2family (afi
);
1949 psize
= PSIZE (orfp
.p
.prefixlen
);
1950 memcpy (&orfp
.p
.u
.prefix
, p_pnt
, psize
);
1953 if (BGP_DEBUG (normal
, NORMAL
))
1954 zlog_info ("%s rcvd %s %s seq %u %s/%d ge %d le %d",
1956 (common
& ORF_COMMON_PART_REMOVE
? "Remove" : "Add"),
1957 (common
& ORF_COMMON_PART_DENY
? "deny" : "permit"),
1959 inet_ntop (orfp
.p
.family
, &orfp
.p
.u
.prefix
, buf
, BUFSIZ
),
1960 orfp
.p
.prefixlen
, orfp
.ge
, orfp
.le
);
1962 ret
= prefix_bgp_orf_set (name
, afi
, &orfp
,
1963 (common
& ORF_COMMON_PART_DENY
? 0 : 1 ),
1964 (common
& ORF_COMMON_PART_REMOVE
? 0 : 1));
1966 if (ret
!= CMD_SUCCESS
)
1968 if (BGP_DEBUG (normal
, NORMAL
))
1969 zlog_info ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer
->host
);
1970 prefix_bgp_orf_remove_all (name
);
1974 peer
->orf_plist
[afi
][safi
] =
1975 prefix_list_lookup (AFI_ORF_PREFIX
, name
);
1977 stream_forward (s
, orf_len
);
1979 if (BGP_DEBUG (normal
, NORMAL
))
1980 zlog_info ("%s rcvd Refresh %s ORF request", peer
->host
,
1981 when_to_refresh
== REFRESH_DEFER
? "Defer" : "Immediate");
1982 if (when_to_refresh
== REFRESH_DEFER
)
1986 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1987 if (CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
))
1988 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
1990 /* Perform route refreshment to the peer */
1991 bgp_announce_route (peer
, afi
, safi
);
1995 bgp_capability_msg_parse (struct peer
*peer
, u_char
*pnt
, bgp_size_t length
)
1998 struct capability cap
;
2009 /* We need at least action, capability code and capability length. */
2012 zlog_info ("%s Capability length error", peer
->host
);
2013 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2019 /* Fetch structure to the byte stream. */
2020 memcpy (&cap
, pnt
+ 1, sizeof (struct capability
));
2022 /* Action value check. */
2023 if (action
!= CAPABILITY_ACTION_SET
2024 && action
!= CAPABILITY_ACTION_UNSET
)
2026 zlog_info ("%s Capability Action Value error %d",
2027 peer
->host
, action
);
2028 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2032 if (BGP_DEBUG (normal
, NORMAL
))
2033 zlog_info ("%s CAPABILITY has action: %d, code: %u, length %u",
2034 peer
->host
, action
, cap
.code
, cap
.length
);
2036 /* Capability length check. */
2037 if (pnt
+ (cap
.length
+ 3) > end
)
2039 zlog_info ("%s Capability length error", peer
->host
);
2040 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2044 /* We know MP Capability Code. */
2045 if (cap
.code
== CAPABILITY_CODE_MP
)
2047 afi
= ntohs (cap
.mpc
.afi
);
2048 safi
= cap
.mpc
.safi
;
2050 /* Ignore capability when override-capability is set. */
2051 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
2054 /* Address family check. */
2057 && (safi
== SAFI_UNICAST
2058 || safi
== SAFI_MULTICAST
2059 || safi
== BGP_SAFI_VPNV4
))
2061 if (BGP_DEBUG (normal
, NORMAL
))
2062 zlog_info ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2064 action
== CAPABILITY_ACTION_SET
2065 ? "Advertising" : "Removing",
2066 ntohs(cap
.mpc
.afi
) , cap
.mpc
.safi
);
2068 /* Adjust safi code. */
2069 if (safi
== BGP_SAFI_VPNV4
)
2070 safi
= SAFI_MPLS_VPN
;
2072 if (action
== CAPABILITY_ACTION_SET
)
2074 peer
->afc_recv
[afi
][safi
] = 1;
2075 if (peer
->afc
[afi
][safi
])
2077 peer
->afc_nego
[afi
][safi
] = 1;
2078 bgp_announce_route (peer
, afi
, safi
);
2083 peer
->afc_recv
[afi
][safi
] = 0;
2084 peer
->afc_nego
[afi
][safi
] = 0;
2086 if (peer_active_nego (peer
))
2088 bgp_clear_route (peer
, afi
, safi
);
2089 peer
->synctime
[afi
][safi
] = 0;
2090 BGP_TIMER_OFF (peer
->t_routeadv
[afi
][safi
]);
2091 peer
->af_sflags
[afi
][safi
] = 0;
2094 BGP_EVENT_ADD (peer
, BGP_Stop
);
2100 zlog_warn ("%s unrecognized capability code: %d - ignored",
2101 peer
->host
, cap
.code
);
2103 pnt
+= cap
.length
+ 3;
2108 /* Dynamic Capability is received. */
2110 bgp_capability_receive (struct peer
*peer
, bgp_size_t size
)
2115 /* Fetch pointer. */
2116 pnt
= stream_pnt (peer
->ibuf
);
2118 if (BGP_DEBUG (normal
, NORMAL
))
2119 zlog_info ("%s rcv CAPABILITY", peer
->host
);
2121 /* If peer does not have the capability, send notification. */
2122 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_ADV
))
2124 plog_err (peer
->log
, "%s [Error] BGP dynamic capability is not enabled",
2126 bgp_notify_send (peer
,
2127 BGP_NOTIFY_HEADER_ERR
,
2128 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2132 /* Status must be Established. */
2133 if (peer
->status
!= Established
)
2135 plog_err (peer
->log
,
2136 "%s [Error] Dynamic capability packet received under status %s", peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
2137 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
2142 ret
= bgp_capability_msg_parse (peer
, pnt
, size
);
2145 /* BGP read utility function. */
2147 bgp_read_packet (struct peer
*peer
)
2152 readsize
= peer
->packet_size
- peer
->ibuf
->putp
;
2154 /* If size is zero then return. */
2158 /* Read packet from fd. */
2159 nbytes
= stream_read_unblock (peer
->ibuf
, peer
->fd
, readsize
);
2161 /* If read byte is smaller than zero then error occured. */
2164 if (errno
== EAGAIN
)
2167 plog_err (peer
->log
, "%s [Error] bgp_read_packet error: %s",
2168 peer
->host
, strerror (errno
));
2170 if (peer
->status
== Established
)
2172 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2174 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2175 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2178 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2181 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
2185 /* When read byte is zero : clear bgp peer and return */
2188 if (BGP_DEBUG (events
, EVENTS
))
2189 plog_info (peer
->log
, "%s [Event] BGP connection closed fd %d",
2190 peer
->host
, peer
->fd
);
2192 if (peer
->status
== Established
)
2194 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2196 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2197 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2200 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2203 BGP_EVENT_ADD (peer
, TCP_connection_closed
);
2207 /* We read partial packet. */
2208 if (peer
->ibuf
->putp
!= peer
->packet_size
)
2216 bgp_marker_all_one (struct stream
*s
, int length
)
2220 for (i
= 0; i
< length
; i
++)
2221 if (s
->data
[i
] != 0xff)
2227 /* Starting point of packet process function. */
2229 bgp_read (struct thread
*thread
)
2235 char notify_data_length
[2];
2237 /* Yes first of all get peer pointer. */
2238 peer
= THREAD_ARG (thread
);
2239 peer
->t_read
= NULL
;
2241 /* For non-blocking IO check. */
2242 if (peer
->status
== Connect
)
2244 bgp_connect_check (peer
);
2251 zlog_err ("bgp_read peer's fd is negative value %d", peer
->fd
);
2254 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
2257 /* Read packet header to determine type of the packet */
2258 if (peer
->packet_size
== 0)
2259 peer
->packet_size
= BGP_HEADER_SIZE
;
2261 if (peer
->ibuf
->putp
< BGP_HEADER_SIZE
)
2263 ret
= bgp_read_packet (peer
);
2265 /* Header read error or partial read packet. */
2269 /* Get size and type. */
2270 stream_forward (peer
->ibuf
, BGP_MARKER_SIZE
);
2271 memcpy (notify_data_length
, stream_pnt (peer
->ibuf
), 2);
2272 size
= stream_getw (peer
->ibuf
);
2273 type
= stream_getc (peer
->ibuf
);
2275 if (BGP_DEBUG (normal
, NORMAL
))
2276 if (type
&& type
!= BGP_MSG_UPDATE
&& type
!= BGP_MSG_KEEPALIVE
)
2277 zlog_info ("%s rcv message type %d, length (excl. header) %d",
2278 peer
->host
, type
, size
- BGP_HEADER_SIZE
);
2281 if (type
== BGP_MSG_OPEN
2282 && ! bgp_marker_all_one (peer
->ibuf
, BGP_MARKER_SIZE
))
2284 bgp_notify_send (peer
,
2285 BGP_NOTIFY_HEADER_ERR
,
2286 BGP_NOTIFY_HEADER_NOT_SYNC
);
2290 /* BGP type check. */
2291 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
2292 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
2293 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
2294 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
2295 && type
!= BGP_MSG_CAPABILITY
)
2297 if (BGP_DEBUG (normal
, NORMAL
))
2298 plog_err (peer
->log
,
2299 "%s unknown message type 0x%02x",
2301 bgp_notify_send_with_data (peer
,
2302 BGP_NOTIFY_HEADER_ERR
,
2303 BGP_NOTIFY_HEADER_BAD_MESTYPE
,
2307 /* Mimimum packet length check. */
2308 if ((size
< BGP_HEADER_SIZE
)
2309 || (size
> BGP_MAX_PACKET_SIZE
)
2310 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
2311 || (type
== BGP_MSG_UPDATE
&& size
< BGP_MSG_UPDATE_MIN_SIZE
)
2312 || (type
== BGP_MSG_NOTIFY
&& size
< BGP_MSG_NOTIFY_MIN_SIZE
)
2313 || (type
== BGP_MSG_KEEPALIVE
&& size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
2314 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2315 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2316 || (type
== BGP_MSG_CAPABILITY
&& size
< BGP_MSG_CAPABILITY_MIN_SIZE
))
2318 if (BGP_DEBUG (normal
, NORMAL
))
2319 plog_err (peer
->log
,
2320 "%s bad message length - %d for %s",
2322 type
== 128 ? "ROUTE-REFRESH" :
2323 bgp_type_str
[(int) type
]);
2324 bgp_notify_send_with_data (peer
,
2325 BGP_NOTIFY_HEADER_ERR
,
2326 BGP_NOTIFY_HEADER_BAD_MESLEN
,
2327 notify_data_length
, 2);
2331 /* Adjust size to message length. */
2332 peer
->packet_size
= size
;
2335 ret
= bgp_read_packet (peer
);
2339 /* Get size and type again. */
2340 size
= stream_getw_from (peer
->ibuf
, BGP_MARKER_SIZE
);
2341 type
= stream_getc_from (peer
->ibuf
, BGP_MARKER_SIZE
+ 2);
2343 /* BGP packet dump function. */
2344 bgp_dump_packet (peer
, type
, peer
->ibuf
);
2346 size
= (peer
->packet_size
- BGP_HEADER_SIZE
);
2348 /* Read rest of the packet and call each sort of packet routine */
2353 bgp_open_receive (peer
, size
);
2355 case BGP_MSG_UPDATE
:
2356 peer
->readtime
= time(NULL
); /* Last read timer reset */
2357 bgp_update_receive (peer
, size
);
2359 case BGP_MSG_NOTIFY
:
2360 bgp_notify_receive (peer
, size
);
2362 case BGP_MSG_KEEPALIVE
:
2363 peer
->readtime
= time(NULL
); /* Last read timer reset */
2364 bgp_keepalive_receive (peer
, size
);
2366 case BGP_MSG_ROUTE_REFRESH_NEW
:
2367 case BGP_MSG_ROUTE_REFRESH_OLD
:
2369 bgp_route_refresh_receive (peer
, size
);
2371 case BGP_MSG_CAPABILITY
:
2372 peer
->dynamic_cap_in
++;
2373 bgp_capability_receive (peer
, size
);
2377 /* Clear input buffer. */
2378 peer
->packet_size
= 0;
2380 stream_reset (peer
->ibuf
);
2383 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
2385 if (BGP_DEBUG (events
, EVENTS
))
2386 zlog_info ("%s [Event] Accepting BGP peer delete", peer
->host
);