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 #ifdef DISABLE_BGP_ANNOUNCE
240 #endif /* DISABLE_BGP_ANNOUNCE */
242 if (BGP_DEBUG (normal
, NORMAL
))
243 zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi
, safi
), peer
->host
);
245 s
= stream_new (BGP_MAX_PACKET_SIZE
);
247 /* Make BGP update packet. */
248 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
250 /* Unfeasible Routes Length */
253 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
255 /* Total Path Attribute Length */
260 /* Total Path Attribute Length */
262 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
263 stream_putc (s
, BGP_ATTR_MP_UNREACH_NLRI
);
265 stream_putw (s
, afi
);
266 stream_putc (s
, safi
);
269 bgp_packet_set_size (s
);
270 packet
= stream_dup (s
);
271 bgp_packet_add (peer
, packet
);
276 /* Make BGP withdraw packet. */
277 static struct stream
*
278 bgp_withdraw_packet (struct peer
*peer
, afi_t afi
, safi_t safi
)
281 struct stream
*packet
;
282 struct bgp_adj_out
*adj
;
283 struct bgp_advertise
*adv
;
286 bgp_size_t unfeasible_len
;
287 bgp_size_t total_attr_len
;
293 while ((adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
)) != NULL
)
299 if (STREAM_REMAIN (s
)
300 < (BGP_NLRI_LENGTH
+ BGP_TOTAL_ATTR_LEN
+ PSIZE (rn
->p
.prefixlen
)))
303 if (stream_empty (s
))
305 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
309 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
310 stream_put_prefix (s
, &rn
->p
);
313 struct prefix_rd
*prd
= NULL
;
316 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
317 pos
= stream_get_endp (s
);
320 = bgp_packet_withdraw (peer
, s
, &rn
->p
, afi
, safi
, prd
, NULL
);
322 /* Set total path attribute length. */
323 stream_putw_at (s
, pos
, total_attr_len
);
326 if (BGP_DEBUG (update
, UPDATE_OUT
))
327 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d -- unreachable",
329 inet_ntop (rn
->p
.family
, &(rn
->p
.u
.prefix
), buf
, BUFSIZ
),
332 peer
->scount
[afi
][safi
]--;
334 bgp_adj_out_remove (rn
, adj
, peer
, afi
, safi
);
335 bgp_unlock_node (rn
);
337 if (! (afi
== AFI_IP
&& safi
== SAFI_UNICAST
))
341 if (! stream_empty (s
))
343 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
346 = stream_get_endp (s
) - BGP_HEADER_SIZE
- BGP_UNFEASIBLE_LEN
;
347 stream_putw_at (s
, BGP_HEADER_SIZE
, unfeasible_len
);
350 bgp_packet_set_size (s
);
351 packet
= stream_dup (s
);
352 bgp_packet_add (peer
, packet
);
361 bgp_default_update_send (struct peer
*peer
, struct attr
*attr
,
362 afi_t afi
, safi_t safi
, struct peer
*from
)
365 struct stream
*packet
;
368 bgp_size_t total_attr_len
;
369 char attrstr
[BUFSIZ
];
372 #ifdef DISABLE_BGP_ANNOUNCE
374 #endif /* DISABLE_BGP_ANNOUNCE */
377 str2prefix ("0.0.0.0/0", &p
);
380 str2prefix ("::/0", &p
);
381 #endif /* HAVE_IPV6 */
383 /* Logging the attribute. */
384 if (BGP_DEBUG (update
, UPDATE_OUT
))
386 bgp_dump_attr (peer
, attr
, attrstr
, BUFSIZ
);
387 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d %s",
388 peer
->host
, inet_ntop(p
.family
, &(p
.u
.prefix
), buf
, BUFSIZ
),
389 p
.prefixlen
, attrstr
);
392 s
= stream_new (BGP_MAX_PACKET_SIZE
);
394 /* Make BGP update packet. */
395 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
397 /* Unfeasible Routes Length. */
400 /* Make place for total attribute length. */
401 pos
= stream_get_endp (s
);
403 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
, attr
, &p
, afi
, safi
, from
, NULL
, NULL
);
405 /* Set Total Path Attribute Length. */
406 stream_putw_at (s
, pos
, total_attr_len
);
409 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
)
410 stream_put_prefix (s
, &p
);
413 bgp_packet_set_size (s
);
415 packet
= stream_dup (s
);
418 /* Dump packet if debug option is set. */
420 /* bgp_packet_dump (packet); */
423 /* Add packet to the peer. */
424 bgp_packet_add (peer
, packet
);
426 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
430 bgp_default_withdraw_send (struct peer
*peer
, afi_t afi
, safi_t safi
)
433 struct stream
*packet
;
437 bgp_size_t unfeasible_len
;
438 bgp_size_t total_attr_len
;
441 #ifdef DISABLE_BGP_ANNOUNCE
443 #endif /* DISABLE_BGP_ANNOUNCE */
446 str2prefix ("0.0.0.0/0", &p
);
449 str2prefix ("::/0", &p
);
450 #endif /* HAVE_IPV6 */
455 if (BGP_DEBUG (update
, UPDATE_OUT
))
456 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d -- unreachable",
457 peer
->host
, inet_ntop(p
.family
, &(p
.u
.prefix
), buf
, BUFSIZ
),
460 s
= stream_new (BGP_MAX_PACKET_SIZE
);
462 /* Make BGP update packet. */
463 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
465 /* Unfeasible Routes Length. */;
466 cp
= stream_get_endp (s
);
469 /* Withdrawn Routes. */
470 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
)
472 stream_put_prefix (s
, &p
);
474 unfeasible_len
= stream_get_endp (s
) - cp
- 2;
476 /* Set unfeasible len. */
477 stream_putw_at (s
, cp
, unfeasible_len
);
479 /* Set total path attribute length. */
484 pos
= stream_get_endp (s
);
486 total_attr_len
= bgp_packet_withdraw (peer
, s
, &p
, afi
, safi
, NULL
, NULL
);
488 /* Set total path attribute length. */
489 stream_putw_at (s
, pos
, total_attr_len
);
492 bgp_packet_set_size (s
);
494 packet
= stream_dup (s
);
497 /* Add packet to the peer. */
498 bgp_packet_add (peer
, packet
);
500 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
503 /* Get next packet to be written. */
504 static struct stream
*
505 bgp_write_packet (struct peer
*peer
)
509 struct stream
*s
= NULL
;
510 struct bgp_advertise
*adv
;
512 s
= stream_fifo_head (peer
->obuf
);
516 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
517 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
519 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
);
522 s
= bgp_withdraw_packet (peer
, afi
, safi
);
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
]->update
);
534 if (adv
->binfo
&& adv
->binfo
->uptime
< peer
->synctime
)
536 if (CHECK_FLAG (adv
->binfo
->peer
->cap
, PEER_CAP_RESTART_RCV
)
537 && CHECK_FLAG (adv
->binfo
->peer
->cap
, PEER_CAP_RESTART_ADV
)
538 && ! CHECK_FLAG (adv
->binfo
->flags
, BGP_INFO_STALE
)
539 && safi
!= SAFI_MPLS_VPN
)
541 if (CHECK_FLAG (adv
->binfo
->peer
->af_sflags
[afi
][safi
],
542 PEER_STATUS_EOR_RECEIVED
))
543 s
= bgp_update_packet (peer
, afi
, safi
);
546 s
= bgp_update_packet (peer
, afi
, safi
);
553 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
555 if (peer
->afc_nego
[afi
][safi
] && peer
->synctime
556 && ! CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_SEND
)
557 && safi
!= SAFI_MPLS_VPN
)
559 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_SEND
);
560 return bgp_update_packet_eor (peer
, afi
, safi
);
568 /* Is there partially written packet or updates we can send right
571 bgp_write_proceed (struct peer
*peer
)
575 struct bgp_advertise
*adv
;
577 if (stream_fifo_head (peer
->obuf
))
580 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
581 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
582 if (FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
))
585 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
586 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
587 if ((adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
)) != NULL
)
588 if (adv
->binfo
->uptime
< peer
->synctime
)
594 /* Write packet to the peer. */
596 bgp_write (struct thread
*thread
)
602 unsigned int count
= 0;
605 /* Yes first of all get peer pointer. */
606 peer
= THREAD_ARG (thread
);
607 peer
->t_write
= NULL
;
609 /* For non-blocking IO check. */
610 if (peer
->status
== Connect
)
612 bgp_connect_check (peer
);
616 /* Nonblocking write until TCP output buffer is full. */
622 s
= bgp_write_packet (peer
);
626 /* XXX: FIXME, the socket should be NONBLOCK from the start
627 * status shouldnt need to be toggled on each write
629 val
= fcntl (peer
->fd
, F_GETFL
, 0);
630 fcntl (peer
->fd
, F_SETFL
, val
|O_NONBLOCK
);
632 /* Number of bytes to be sent. */
633 writenum
= stream_get_endp (s
) - stream_get_getp (s
);
635 /* Call write() system call. */
636 num
= write (peer
->fd
, STREAM_PNT (s
), writenum
);
638 fcntl (peer
->fd
, F_SETFL
, val
);
642 if (write_errno
== EWOULDBLOCK
|| write_errno
== EAGAIN
)
645 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
650 stream_forward_getp (s
, num
);
652 if (write_errno
== EAGAIN
)
658 /* Retrieve BGP packet type. */
659 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
660 type
= stream_getc (s
);
672 /* Double start timer. */
675 /* Overflow check. */
676 if (peer
->v_start
>= (60 * 2))
677 peer
->v_start
= (60 * 2);
679 /* Flush any existing events */
680 BGP_EVENT_ADD (peer
, BGP_Stop
);
682 case BGP_MSG_KEEPALIVE
:
683 peer
->keepalive_out
++;
685 case BGP_MSG_ROUTE_REFRESH_NEW
:
686 case BGP_MSG_ROUTE_REFRESH_OLD
:
689 case BGP_MSG_CAPABILITY
:
690 peer
->dynamic_cap_out
++;
694 /* OK we send packet so delete it. */
695 bgp_packet_delete (peer
);
697 if (++count
>= BGP_WRITE_PACKET_MAX
)
701 if (bgp_write_proceed (peer
))
702 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
707 /* This is only for sending NOTIFICATION message to neighbor. */
709 bgp_write_notify (struct peer
*peer
)
715 /* There should be at least one packet. */
716 s
= stream_fifo_head (peer
->obuf
);
719 assert (stream_get_endp (s
) >= BGP_HEADER_SIZE
);
721 /* I'm not sure fd is writable. */
722 ret
= writen (peer
->fd
, STREAM_DATA (s
), stream_get_endp (s
));
725 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
729 /* Retrieve BGP packet type. */
730 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
731 type
= stream_getc (s
);
733 assert (type
== BGP_MSG_NOTIFY
);
735 /* Type should be notify. */
738 /* Double start timer. */
741 /* Overflow check. */
742 if (peer
->v_start
>= (60 * 2))
743 peer
->v_start
= (60 * 2);
745 BGP_EVENT_ADD (peer
, BGP_Stop
);
750 /* Make keepalive packet and send it to the peer. */
752 bgp_keepalive_send (struct peer
*peer
)
757 s
= stream_new (BGP_MAX_PACKET_SIZE
);
759 /* Make keepalive packet. */
760 bgp_packet_set_marker (s
, BGP_MSG_KEEPALIVE
);
762 /* Set packet size. */
763 length
= bgp_packet_set_size (s
);
765 /* Dump packet if debug option is set. */
766 /* bgp_packet_dump (s); */
768 if (BGP_DEBUG (keepalive
, KEEPALIVE
))
769 zlog_debug ("%s sending KEEPALIVE", peer
->host
);
770 if (BGP_DEBUG (normal
, NORMAL
))
771 zlog_debug ("%s send message type %d, length (incl. header) %d",
772 peer
->host
, BGP_MSG_KEEPALIVE
, length
);
774 /* Add packet to the peer. */
775 bgp_packet_add (peer
, s
);
777 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
780 /* Make open packet and send it to the peer. */
782 bgp_open_send (struct peer
*peer
)
786 u_int16_t send_holdtime
;
789 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
790 send_holdtime
= peer
->holdtime
;
792 send_holdtime
= peer
->bgp
->default_holdtime
;
794 /* local-as Change */
795 if (peer
->change_local_as
)
796 local_as
= peer
->change_local_as
;
798 local_as
= peer
->local_as
;
800 s
= stream_new (BGP_MAX_PACKET_SIZE
);
802 /* Make open packet. */
803 bgp_packet_set_marker (s
, BGP_MSG_OPEN
);
805 /* Set open packet values. */
806 stream_putc (s
, BGP_VERSION_4
); /* BGP version */
807 stream_putw (s
, local_as
); /* My Autonomous System*/
808 stream_putw (s
, send_holdtime
); /* Hold Time */
809 stream_put_in_addr (s
, &peer
->local_id
); /* BGP Identifier */
811 /* Set capability code. */
812 bgp_open_capability (s
, peer
);
814 /* Set BGP packet length. */
815 length
= bgp_packet_set_size (s
);
817 if (BGP_DEBUG (normal
, NORMAL
))
818 zlog_debug ("%s sending OPEN, version %d, my as %d, holdtime %d, id %s",
819 peer
->host
, BGP_VERSION_4
, local_as
,
820 send_holdtime
, inet_ntoa (peer
->local_id
));
822 if (BGP_DEBUG (normal
, NORMAL
))
823 zlog_debug ("%s send message type %d, length (incl. header) %d",
824 peer
->host
, BGP_MSG_OPEN
, length
);
826 /* Dump packet if debug option is set. */
827 /* bgp_packet_dump (s); */
829 /* Add packet to the peer. */
830 bgp_packet_add (peer
, s
);
832 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
835 /* Send BGP notify packet with data potion. */
837 bgp_notify_send_with_data (struct peer
*peer
, u_char code
, u_char sub_code
,
838 u_char
*data
, size_t datalen
)
843 /* Allocate new stream. */
844 s
= stream_new (BGP_MAX_PACKET_SIZE
);
846 /* Make nitify packet. */
847 bgp_packet_set_marker (s
, BGP_MSG_NOTIFY
);
849 /* Set notify packet values. */
850 stream_putc (s
, code
); /* BGP notify code */
851 stream_putc (s
, sub_code
); /* BGP notify sub_code */
853 /* If notify data is present. */
855 stream_write (s
, data
, datalen
);
857 /* Set BGP packet length. */
858 length
= bgp_packet_set_size (s
);
860 /* Add packet to the peer. */
861 stream_fifo_clean (peer
->obuf
);
862 bgp_packet_add (peer
, s
);
866 struct bgp_notify bgp_notify
;
871 bgp_notify
.code
= code
;
872 bgp_notify
.subcode
= sub_code
;
873 bgp_notify
.data
= NULL
;
874 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
876 if (bgp_notify
.length
)
878 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
879 for (i
= 0; i
< bgp_notify
.length
; i
++)
882 sprintf (c
, " %02x", data
[i
]);
883 strcat (bgp_notify
.data
, c
);
888 sprintf (c
, "%02x", data
[i
]);
889 strcpy (bgp_notify
.data
, c
);
892 bgp_notify_print (peer
, &bgp_notify
, "sending");
894 XFREE (MTYPE_TMP
, bgp_notify
.data
);
897 if (BGP_DEBUG (normal
, NORMAL
))
898 zlog_debug ("%s send message type %d, length (incl. header) %d",
899 peer
->host
, BGP_MSG_NOTIFY
, length
);
901 /* peer reset cause */
902 if (sub_code
!= BGP_NOTIFY_CEASE_CONFIG_CHANGE
)
904 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
905 peer
->last_reset
= PEER_DOWN_USER_RESET
;
906 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
907 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
909 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
912 /* Call imidiately. */
913 BGP_WRITE_OFF (peer
->t_write
);
915 bgp_write_notify (peer
);
918 /* Send BGP notify packet. */
920 bgp_notify_send (struct peer
*peer
, u_char code
, u_char sub_code
)
922 bgp_notify_send_with_data (peer
, code
, sub_code
, NULL
, 0);
930 else if (afi
== AFI_IP6
)
933 return "Unknown AFI";
937 safi2str (safi_t safi
)
939 if (safi
== SAFI_UNICAST
)
940 return "SAFI_UNICAST";
941 else if (safi
== SAFI_MULTICAST
)
942 return "SAFI_MULTICAST";
943 else if (safi
== SAFI_MPLS_VPN
|| safi
== BGP_SAFI_VPNV4
)
944 return "SAFI_MPLS_VPN";
946 return "Unknown SAFI";
949 /* Send route refresh message to the peer. */
951 bgp_route_refresh_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
952 u_char orf_type
, u_char when_to_refresh
, int remove
)
955 struct stream
*packet
;
957 struct bgp_filter
*filter
;
960 #ifdef DISABLE_BGP_ANNOUNCE
962 #endif /* DISABLE_BGP_ANNOUNCE */
964 filter
= &peer
->filter
[afi
][safi
];
966 /* Adjust safi code. */
967 if (safi
== SAFI_MPLS_VPN
)
968 safi
= BGP_SAFI_VPNV4
;
970 s
= stream_new (BGP_MAX_PACKET_SIZE
);
972 /* Make BGP update packet. */
973 if (CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
974 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_NEW
);
976 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_OLD
);
978 /* Encode Route Refresh message. */
979 stream_putw (s
, afi
);
981 stream_putc (s
, safi
);
983 if (orf_type
== ORF_TYPE_PREFIX
984 || orf_type
== ORF_TYPE_PREFIX_OLD
)
985 if (remove
|| filter
->plist
[FILTER_IN
].plist
)
991 stream_putc (s
, when_to_refresh
);
992 stream_putc (s
, orf_type
);
993 orfp
= stream_get_endp (s
);
998 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
999 stream_putc (s
, ORF_COMMON_PART_REMOVE_ALL
);
1000 if (BGP_DEBUG (normal
, NORMAL
))
1001 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
1002 peer
->host
, orf_type
,
1003 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
1008 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
1009 prefix_bgp_orf_entry (s
, filter
->plist
[FILTER_IN
].plist
,
1010 ORF_COMMON_PART_ADD
, ORF_COMMON_PART_PERMIT
,
1011 ORF_COMMON_PART_DENY
);
1012 if (BGP_DEBUG (normal
, NORMAL
))
1013 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
1014 peer
->host
, orf_type
,
1015 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
1019 /* Total ORF Entry Len. */
1020 orf_len
= stream_get_endp (s
) - orfp
- 2;
1021 stream_putw_at (s
, orfp
, orf_len
);
1024 /* Set packet size. */
1025 length
= bgp_packet_set_size (s
);
1027 if (BGP_DEBUG (normal
, NORMAL
))
1030 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
1031 peer
->host
, afi
, safi
);
1032 zlog_debug ("%s send message type %d, length (incl. header) %d",
1033 peer
->host
, CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
) ?
1034 BGP_MSG_ROUTE_REFRESH_NEW
: BGP_MSG_ROUTE_REFRESH_OLD
, length
);
1037 /* Make real packet. */
1038 packet
= stream_dup (s
);
1041 /* Add packet to the peer. */
1042 bgp_packet_add (peer
, packet
);
1044 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1047 /* Send capability message to the peer. */
1049 bgp_capability_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
1050 int capability_code
, int action
)
1053 struct stream
*packet
;
1056 /* Adjust safi code. */
1057 if (safi
== SAFI_MPLS_VPN
)
1058 safi
= BGP_SAFI_VPNV4
;
1060 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1062 /* Make BGP update packet. */
1063 bgp_packet_set_marker (s
, BGP_MSG_CAPABILITY
);
1065 /* Encode MP_EXT capability. */
1066 if (capability_code
== CAPABILITY_CODE_MP
)
1068 stream_putc (s
, action
);
1069 stream_putc (s
, CAPABILITY_CODE_MP
);
1070 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
1071 stream_putw (s
, afi
);
1073 stream_putc (s
, safi
);
1075 if (BGP_DEBUG (normal
, NORMAL
))
1076 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
1077 peer
->host
, action
== CAPABILITY_ACTION_SET
?
1078 "Advertising" : "Removing", afi
, safi
);
1081 /* Set packet size. */
1082 length
= bgp_packet_set_size (s
);
1084 /* Make real packet. */
1085 packet
= stream_dup (s
);
1088 /* Add packet to the peer. */
1089 bgp_packet_add (peer
, packet
);
1091 if (BGP_DEBUG (normal
, NORMAL
))
1092 zlog_debug ("%s send message type %d, length (incl. header) %d",
1093 peer
->host
, BGP_MSG_CAPABILITY
, length
);
1095 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1098 /* RFC1771 6.8 Connection collision detection. */
1100 bgp_collision_detect (struct peer
*new, struct in_addr remote_id
)
1103 struct listnode
*node
, *nnode
;
1106 bgp
= bgp_get_default ();
1110 /* Upon receipt of an OPEN message, the local system must examine
1111 all of its connections that are in the OpenConfirm state. A BGP
1112 speaker may also examine connections in an OpenSent state if it
1113 knows the BGP Identifier of the peer by means outside of the
1114 protocol. If among these connections there is a connection to a
1115 remote BGP speaker whose BGP Identifier equals the one in the
1116 OPEN message, then the local system performs the following
1117 collision resolution procedure: */
1119 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
1121 /* Under OpenConfirm status, local peer structure already hold
1122 remote router ID. */
1125 && (peer
->status
== OpenConfirm
|| peer
->status
== OpenSent
)
1126 && sockunion_same (&peer
->su
, &new->su
))
1128 /* 1. The BGP Identifier of the local system is compared to
1129 the BGP Identifier of the remote system (as specified in
1130 the OPEN message). */
1132 if (ntohl (peer
->local_id
.s_addr
) < ntohl (remote_id
.s_addr
))
1134 /* 2. If the value of the local BGP Identifier is less
1135 than the remote one, the local system closes BGP
1136 connection that already exists (the one that is
1137 already in the OpenConfirm state), and accepts BGP
1138 connection initiated by the remote system. */
1141 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1146 /* 3. Otherwise, the local system closes newly created
1147 BGP connection (the one associated with the newly
1148 received OPEN message), and continues to use the
1149 existing one (the one that is already in the
1150 OpenConfirm state). */
1153 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
1154 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1163 bgp_open_receive (struct peer
*peer
, bgp_size_t size
)
1169 u_int16_t send_holdtime
;
1171 struct peer
*realpeer
;
1172 struct in_addr remote_id
;
1174 u_int8_t notify_data_remote_as
[2];
1175 u_int8_t notify_data_remote_id
[4];
1179 /* Parse open packet. */
1180 version
= stream_getc (peer
->ibuf
);
1181 memcpy (notify_data_remote_as
, stream_pnt (peer
->ibuf
), 2);
1182 remote_as
= stream_getw (peer
->ibuf
);
1183 holdtime
= stream_getw (peer
->ibuf
);
1184 memcpy (notify_data_remote_id
, stream_pnt (peer
->ibuf
), 4);
1185 remote_id
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
1187 /* Receive OPEN message log */
1188 if (BGP_DEBUG (normal
, NORMAL
))
1189 zlog_debug ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s",
1190 peer
->host
, version
, remote_as
, holdtime
,
1191 inet_ntoa (remote_id
));
1193 /* Lookup peer from Open packet. */
1194 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1198 realpeer
= peer_lookup_with_open (&peer
->su
, remote_as
, &remote_id
, &as
);
1202 /* Peer's source IP address is check in bgp_accept(), so this
1203 must be AS number mismatch or remote-id configuration
1207 if (BGP_DEBUG (normal
, NORMAL
))
1208 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1209 peer
->host
, inet_ntoa (remote_id
));
1210 bgp_notify_send_with_data (peer
, BGP_NOTIFY_OPEN_ERR
,
1211 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1212 notify_data_remote_id
, 4);
1216 if (BGP_DEBUG (normal
, NORMAL
))
1217 zlog_debug ("%s bad OPEN, remote AS is %d, expected %d",
1218 peer
->host
, remote_as
, peer
->as
);
1219 bgp_notify_send_with_data (peer
, BGP_NOTIFY_OPEN_ERR
,
1220 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1221 notify_data_remote_as
, 2);
1227 /* When collision is detected and this peer is closed. Retrun
1229 ret
= bgp_collision_detect (peer
, remote_id
);
1234 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1236 if (realpeer
->status
== Established
1237 && CHECK_FLAG (realpeer
->sflags
, PEER_STATUS_NSF_MODE
))
1239 realpeer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
1240 SET_FLAG (realpeer
->sflags
, PEER_STATUS_NSF_WAIT
);
1242 else if (ret
== 0 && realpeer
->status
!= Active
1243 && realpeer
->status
!= OpenSent
1244 && realpeer
->status
!= OpenConfirm
)
1247 if (BGP_DEBUG (events
, EVENTS
))
1248 zlog_debug ("%s peer status is %s close connection",
1249 realpeer
->host
, LOOKUP (bgp_status_msg
,
1251 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
1252 BGP_NOTIFY_CEASE_CONNECT_REJECT
);
1257 if (BGP_DEBUG (events
, EVENTS
))
1258 zlog_debug ("%s [Event] Transfer temporary BGP peer to existing one",
1261 bgp_stop (realpeer
);
1263 /* Transfer file descriptor. */
1264 realpeer
->fd
= peer
->fd
;
1267 /* Transfer input buffer. */
1268 stream_free (realpeer
->ibuf
);
1269 realpeer
->ibuf
= peer
->ibuf
;
1270 realpeer
->packet_size
= peer
->packet_size
;
1273 /* Transfer status. */
1274 realpeer
->status
= peer
->status
;
1277 /* peer pointer change. Open packet send to neighbor. */
1279 bgp_open_send (peer
);
1282 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1286 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1289 /* remote router-id check. */
1290 if (remote_id
.s_addr
== 0
1291 || ntohl (remote_id
.s_addr
) >= 0xe0000000
1292 || ntohl (peer
->local_id
.s_addr
) == ntohl (remote_id
.s_addr
))
1294 if (BGP_DEBUG (normal
, NORMAL
))
1295 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1296 peer
->host
, inet_ntoa (remote_id
));
1297 bgp_notify_send_with_data (peer
,
1298 BGP_NOTIFY_OPEN_ERR
,
1299 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1300 notify_data_remote_id
, 4);
1304 /* Set remote router-id */
1305 peer
->remote_id
= remote_id
;
1307 /* Peer BGP version check. */
1308 if (version
!= BGP_VERSION_4
)
1310 u_int8_t maxver
= BGP_VERSION_4
;
1311 if (BGP_DEBUG (normal
, NORMAL
))
1312 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
1313 peer
->host
, version
, BGP_VERSION_4
);
1314 bgp_notify_send_with_data (peer
,
1315 BGP_NOTIFY_OPEN_ERR
,
1316 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1321 /* Check neighbor as number. */
1322 if (remote_as
!= peer
->as
)
1324 if (BGP_DEBUG (normal
, NORMAL
))
1325 zlog_debug ("%s bad OPEN, remote AS is %d, expected %d",
1326 peer
->host
, remote_as
, peer
->as
);
1327 bgp_notify_send_with_data (peer
,
1328 BGP_NOTIFY_OPEN_ERR
,
1329 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1330 notify_data_remote_as
, 2);
1334 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1335 calculate the value of the Hold Timer by using the smaller of its
1336 configured Hold Time and the Hold Time received in the OPEN message.
1337 The Hold Time MUST be either zero or at least three seconds. An
1338 implementation may reject connections on the basis of the Hold Time. */
1340 if (holdtime
< 3 && holdtime
!= 0)
1342 bgp_notify_send (peer
,
1343 BGP_NOTIFY_OPEN_ERR
,
1344 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
);
1348 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1349 would be one third of the Hold Time interval. KEEPALIVE messages
1350 MUST NOT be sent more frequently than one per second. An
1351 implementation MAY adjust the rate at which it sends KEEPALIVE
1352 messages as a function of the Hold Time interval. */
1354 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1355 send_holdtime
= peer
->holdtime
;
1357 send_holdtime
= peer
->bgp
->default_holdtime
;
1359 if (holdtime
< send_holdtime
)
1360 peer
->v_holdtime
= holdtime
;
1362 peer
->v_holdtime
= send_holdtime
;
1364 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1366 /* Open option part parse. */
1368 optlen
= stream_getc (peer
->ibuf
);
1371 ret
= bgp_open_option_parse (peer
, optlen
, &capability
);
1377 if (BGP_DEBUG (normal
, NORMAL
))
1378 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
1382 /* Override capability. */
1383 if (! capability
|| CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
1385 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] = peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1386 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1387 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] = peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1388 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1392 bgp_getsockname (peer
);
1394 BGP_EVENT_ADD (peer
, Receive_OPEN_message
);
1396 peer
->packet_size
= 0;
1398 stream_reset (peer
->ibuf
);
1403 /* Parse BGP Update packet and make attribute object. */
1405 bgp_update_receive (struct peer
*peer
, bgp_size_t size
)
1411 bgp_size_t attribute_len
;
1412 bgp_size_t update_len
;
1413 bgp_size_t withdraw_len
;
1414 struct bgp_nlri update
;
1415 struct bgp_nlri withdraw
;
1416 struct bgp_nlri mp_update
;
1417 struct bgp_nlri mp_withdraw
;
1418 char attrstr
[BUFSIZ
] = "";
1420 /* Status must be Established. */
1421 if (peer
->status
!= Established
)
1423 zlog_err ("%s [FSM] Update packet received under status %s",
1424 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1425 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1429 /* Set initial values. */
1430 memset (&attr
, 0, sizeof (struct attr
));
1431 memset (&update
, 0, sizeof (struct bgp_nlri
));
1432 memset (&withdraw
, 0, sizeof (struct bgp_nlri
));
1433 memset (&mp_update
, 0, sizeof (struct bgp_nlri
));
1434 memset (&mp_withdraw
, 0, sizeof (struct bgp_nlri
));
1437 end
= stream_pnt (s
) + size
;
1439 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1440 Length is too large (i.e., if Unfeasible Routes Length + Total
1441 Attribute Length + 23 exceeds the message Length), then the Error
1442 Subcode is set to Malformed Attribute List. */
1443 if (stream_pnt (s
) + 2 > end
)
1445 zlog_err ("%s [Error] Update packet error"
1446 " (packet length is short for unfeasible length)",
1448 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1449 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1453 /* Unfeasible Route Length. */
1454 withdraw_len
= stream_getw (s
);
1456 /* Unfeasible Route Length check. */
1457 if (stream_pnt (s
) + withdraw_len
> end
)
1459 zlog_err ("%s [Error] Update packet error"
1460 " (packet unfeasible length overflow %d)",
1461 peer
->host
, withdraw_len
);
1462 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1463 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1467 /* Unfeasible Route packet format check. */
1468 if (withdraw_len
> 0)
1470 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, stream_pnt (s
), withdraw_len
);
1474 if (BGP_DEBUG (packet
, PACKET_RECV
))
1475 zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer
->host
);
1477 withdraw
.afi
= AFI_IP
;
1478 withdraw
.safi
= SAFI_UNICAST
;
1479 withdraw
.nlri
= stream_pnt (s
);
1480 withdraw
.length
= withdraw_len
;
1481 stream_forward_getp (s
, withdraw_len
);
1484 /* Attribute total length check. */
1485 if (stream_pnt (s
) + 2 > end
)
1487 zlog_warn ("%s [Error] Packet Error"
1488 " (update packet is short for attribute length)",
1490 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1491 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1495 /* Fetch attribute total length. */
1496 attribute_len
= stream_getw (s
);
1498 /* Attribute length check. */
1499 if (stream_pnt (s
) + attribute_len
> end
)
1501 zlog_warn ("%s [Error] Packet Error"
1502 " (update packet attribute length overflow %d)",
1503 peer
->host
, attribute_len
);
1504 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1505 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1509 /* Parse attribute when it exists. */
1512 ret
= bgp_attr_parse (peer
, &attr
, attribute_len
,
1513 &mp_update
, &mp_withdraw
);
1518 /* Logging the attribute. */
1519 if (BGP_DEBUG (update
, UPDATE_IN
))
1521 ret
= bgp_dump_attr (peer
, &attr
, attrstr
, BUFSIZ
);
1524 zlog (peer
->log
, LOG_DEBUG
, "%s rcvd UPDATE w/ attr: %s",
1525 peer
->host
, attrstr
);
1528 /* Network Layer Reachability Information. */
1529 update_len
= end
- stream_pnt (s
);
1533 /* Check NLRI packet format and prefix length. */
1534 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, stream_pnt (s
), update_len
);
1538 /* Set NLRI portion to structure. */
1539 update
.afi
= AFI_IP
;
1540 update
.safi
= SAFI_UNICAST
;
1541 update
.nlri
= stream_pnt (s
);
1542 update
.length
= update_len
;
1543 stream_forward_getp (s
, update_len
);
1546 /* NLRI is processed only when the peer is configured specific
1547 Address Family and Subsequent Address Family. */
1548 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
])
1550 if (withdraw
.length
)
1551 bgp_nlri_parse (peer
, NULL
, &withdraw
);
1555 /* We check well-known attribute only for IPv4 unicast
1557 ret
= bgp_attr_check (peer
, &attr
);
1561 bgp_nlri_parse (peer
, &attr
, &update
);
1564 if (mp_update
.length
1565 && mp_update
.afi
== AFI_IP
1566 && mp_update
.safi
== SAFI_UNICAST
)
1567 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1569 if (mp_withdraw
.length
1570 && mp_withdraw
.afi
== AFI_IP
1571 && mp_withdraw
.safi
== SAFI_UNICAST
)
1572 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1574 if (! attribute_len
&& ! withdraw_len
)
1576 /* End-of-RIB received */
1577 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_UNICAST
],
1578 PEER_STATUS_EOR_RECEIVED
);
1580 /* NSF delete stale route */
1581 if (peer
->nsf
[AFI_IP
][SAFI_UNICAST
])
1582 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_UNICAST
);
1584 if (BGP_DEBUG (normal
, NORMAL
))
1585 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv4 Unicast from %s",
1589 if (peer
->afc
[AFI_IP
][SAFI_MULTICAST
])
1591 if (mp_update
.length
1592 && mp_update
.afi
== AFI_IP
1593 && mp_update
.safi
== SAFI_MULTICAST
)
1594 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1596 if (mp_withdraw
.length
1597 && mp_withdraw
.afi
== AFI_IP
1598 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1599 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1602 && mp_withdraw
.afi
== AFI_IP
1603 && mp_withdraw
.safi
== SAFI_MULTICAST
1604 && mp_withdraw
.length
== 0)
1606 /* End-of-RIB received */
1607 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_MULTICAST
],
1608 PEER_STATUS_EOR_RECEIVED
);
1610 /* NSF delete stale route */
1611 if (peer
->nsf
[AFI_IP
][SAFI_MULTICAST
])
1612 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_MULTICAST
);
1614 if (BGP_DEBUG (normal
, NORMAL
))
1615 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv4 Multicast from %s",
1619 if (peer
->afc
[AFI_IP6
][SAFI_UNICAST
])
1621 if (mp_update
.length
1622 && mp_update
.afi
== AFI_IP6
1623 && mp_update
.safi
== SAFI_UNICAST
)
1624 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1626 if (mp_withdraw
.length
1627 && mp_withdraw
.afi
== AFI_IP6
1628 && mp_withdraw
.safi
== SAFI_UNICAST
)
1629 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1632 && mp_withdraw
.afi
== AFI_IP6
1633 && mp_withdraw
.safi
== SAFI_UNICAST
1634 && mp_withdraw
.length
== 0)
1636 /* End-of-RIB received */
1637 SET_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_UNICAST
], PEER_STATUS_EOR_RECEIVED
);
1639 /* NSF delete stale route */
1640 if (peer
->nsf
[AFI_IP6
][SAFI_UNICAST
])
1641 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_UNICAST
);
1643 if (BGP_DEBUG (normal
, NORMAL
))
1644 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv6 Unicast from %s",
1648 if (peer
->afc
[AFI_IP6
][SAFI_MULTICAST
])
1650 if (mp_update
.length
1651 && mp_update
.afi
== AFI_IP6
1652 && mp_update
.safi
== SAFI_MULTICAST
)
1653 bgp_nlri_parse (peer
, &attr
, &mp_update
);
1655 if (mp_withdraw
.length
1656 && mp_withdraw
.afi
== AFI_IP6
1657 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1658 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1661 && mp_withdraw
.afi
== AFI_IP6
1662 && mp_withdraw
.safi
== SAFI_MULTICAST
1663 && mp_withdraw
.length
== 0)
1665 /* End-of-RIB received */
1667 /* NSF delete stale route */
1668 if (peer
->nsf
[AFI_IP6
][SAFI_MULTICAST
])
1669 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_MULTICAST
);
1671 if (BGP_DEBUG (update
, UPDATE_IN
))
1672 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv6 Multicast from %s",
1676 if (peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
])
1678 if (mp_update
.length
1679 && mp_update
.afi
== AFI_IP
1680 && mp_update
.safi
== BGP_SAFI_VPNV4
)
1681 bgp_nlri_parse_vpnv4 (peer
, &attr
, &mp_update
);
1683 if (mp_withdraw
.length
1684 && mp_withdraw
.afi
== AFI_IP
1685 && mp_withdraw
.safi
== BGP_SAFI_VPNV4
)
1686 bgp_nlri_parse_vpnv4 (peer
, NULL
, &mp_withdraw
);
1689 && mp_withdraw
.afi
== AFI_IP
1690 && mp_withdraw
.safi
== BGP_SAFI_VPNV4
1691 && mp_withdraw
.length
== 0)
1693 /* End-of-RIB received */
1695 if (BGP_DEBUG (update
, UPDATE_IN
))
1696 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for VPNv4 Unicast from %s",
1701 /* Everything is done. We unintern temporary structures which
1702 interned in bgp_attr_parse(). */
1704 aspath_unintern (attr
.aspath
);
1706 community_unintern (attr
.community
);
1709 if (attr
.extra
->ecommunity
)
1710 ecommunity_unintern (attr
.extra
->ecommunity
);
1711 if (attr
.extra
->cluster
)
1712 cluster_unintern (attr
.extra
->cluster
);
1713 if (attr
.extra
->transit
)
1714 transit_unintern (attr
.extra
->transit
);
1715 bgp_attr_extra_free (&attr
);
1718 /* If peering is stopped due to some reason, do not generate BGP
1720 if (peer
->status
!= Established
)
1723 /* Increment packet counter. */
1725 peer
->update_time
= time (NULL
);
1727 /* Generate BGP event. */
1728 BGP_EVENT_ADD (peer
, Receive_UPDATE_message
);
1733 /* Notify message treatment function. */
1735 bgp_notify_receive (struct peer
*peer
, bgp_size_t size
)
1737 struct bgp_notify bgp_notify
;
1739 if (peer
->notify
.data
)
1741 XFREE (MTYPE_TMP
, peer
->notify
.data
);
1742 peer
->notify
.data
= NULL
;
1743 peer
->notify
.length
= 0;
1746 bgp_notify
.code
= stream_getc (peer
->ibuf
);
1747 bgp_notify
.subcode
= stream_getc (peer
->ibuf
);
1748 bgp_notify
.length
= size
- 2;
1749 bgp_notify
.data
= NULL
;
1751 /* Preserv notify code and sub code. */
1752 peer
->notify
.code
= bgp_notify
.code
;
1753 peer
->notify
.subcode
= bgp_notify
.subcode
;
1754 /* For further diagnostic record returned Data. */
1755 if (bgp_notify
.length
)
1757 peer
->notify
.length
= size
- 2;
1758 peer
->notify
.data
= XMALLOC (MTYPE_TMP
, size
- 2);
1759 memcpy (peer
->notify
.data
, stream_pnt (peer
->ibuf
), size
- 2);
1768 if (bgp_notify
.length
)
1770 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
1771 for (i
= 0; i
< bgp_notify
.length
; i
++)
1774 sprintf (c
, " %02x", stream_getc (peer
->ibuf
));
1775 strcat (bgp_notify
.data
, c
);
1780 sprintf (c
, "%02x", stream_getc (peer
->ibuf
));
1781 strcpy (bgp_notify
.data
, c
);
1785 bgp_notify_print(peer
, &bgp_notify
, "received");
1786 if (bgp_notify
.data
)
1787 XFREE (MTYPE_TMP
, bgp_notify
.data
);
1790 /* peer count update */
1793 if (peer
->status
== Established
)
1794 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
1796 /* We have to check for Notify with Unsupported Optional Parameter.
1797 in that case we fallback to open without the capability option.
1798 But this done in bgp_stop. We just mark it here to avoid changing
1800 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1801 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
1802 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1804 /* Also apply to Unsupported Capability until remote router support
1806 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
1807 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_CAPBL
)
1808 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1810 BGP_EVENT_ADD (peer
, Receive_NOTIFICATION_message
);
1813 /* Keepalive treatment function -- get keepalive send keepalive */
1815 bgp_keepalive_receive (struct peer
*peer
, bgp_size_t size
)
1817 if (BGP_DEBUG (keepalive
, KEEPALIVE
))
1818 zlog_debug ("%s KEEPALIVE rcvd", peer
->host
);
1820 BGP_EVENT_ADD (peer
, Receive_KEEPALIVE_message
);
1823 /* Route refresh message is received. */
1825 bgp_route_refresh_receive (struct peer
*peer
, bgp_size_t size
)
1832 /* If peer does not have the capability, send notification. */
1833 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_ADV
))
1835 plog_err (peer
->log
, "%s [Error] BGP route refresh is not enabled",
1837 bgp_notify_send (peer
,
1838 BGP_NOTIFY_HEADER_ERR
,
1839 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
1843 /* Status must be Established. */
1844 if (peer
->status
!= Established
)
1846 plog_err (peer
->log
,
1847 "%s [Error] Route refresh packet received under status %s",
1848 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1849 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1856 afi
= stream_getw (s
);
1857 reserved
= stream_getc (s
);
1858 safi
= stream_getc (s
);
1860 if (BGP_DEBUG (normal
, NORMAL
))
1861 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1862 peer
->host
, afi
, safi
);
1864 /* Check AFI and SAFI. */
1865 if ((afi
!= AFI_IP
&& afi
!= AFI_IP6
)
1866 || (safi
!= SAFI_UNICAST
&& safi
!= SAFI_MULTICAST
1867 && safi
!= BGP_SAFI_VPNV4
))
1869 if (BGP_DEBUG (normal
, NORMAL
))
1871 zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1872 peer
->host
, afi
, safi
);
1877 /* Adjust safi code. */
1878 if (safi
== BGP_SAFI_VPNV4
)
1879 safi
= SAFI_MPLS_VPN
;
1881 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
1884 u_char when_to_refresh
;
1888 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) < 5)
1890 zlog_info ("%s ORF route refresh length error", peer
->host
);
1891 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
1895 when_to_refresh
= stream_getc (s
);
1896 end
= stream_pnt (s
) + (size
- 5);
1898 while (stream_pnt (s
) < end
)
1900 orf_type
= stream_getc (s
);
1901 orf_len
= stream_getw (s
);
1903 if (orf_type
== ORF_TYPE_PREFIX
1904 || orf_type
== ORF_TYPE_PREFIX_OLD
)
1906 u_char
*p_pnt
= stream_pnt (s
);
1907 u_char
*p_end
= stream_pnt (s
) + orf_len
;
1908 struct orf_prefix orfp
;
1916 if (BGP_DEBUG (normal
, NORMAL
))
1918 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
1919 peer
->host
, orf_type
, orf_len
);
1922 /* ORF prefix-list name */
1923 sprintf (name
, "%s.%d.%d", peer
->host
, afi
, safi
);
1925 while (p_pnt
< p_end
)
1927 memset (&orfp
, 0, sizeof (struct orf_prefix
));
1929 if (common
& ORF_COMMON_PART_REMOVE_ALL
)
1931 if (BGP_DEBUG (normal
, NORMAL
))
1932 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer
->host
);
1933 prefix_bgp_orf_remove_all (name
);
1936 memcpy (&seq
, p_pnt
, sizeof (u_int32_t
));
1937 p_pnt
+= sizeof (u_int32_t
);
1938 orfp
.seq
= ntohl (seq
);
1941 orfp
.p
.prefixlen
= *p_pnt
++;
1942 orfp
.p
.family
= afi2family (afi
);
1943 psize
= PSIZE (orfp
.p
.prefixlen
);
1944 memcpy (&orfp
.p
.u
.prefix
, p_pnt
, psize
);
1947 if (BGP_DEBUG (normal
, NORMAL
))
1948 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d",
1950 (common
& ORF_COMMON_PART_REMOVE
? "Remove" : "Add"),
1951 (common
& ORF_COMMON_PART_DENY
? "deny" : "permit"),
1953 inet_ntop (orfp
.p
.family
, &orfp
.p
.u
.prefix
, buf
, BUFSIZ
),
1954 orfp
.p
.prefixlen
, orfp
.ge
, orfp
.le
);
1956 ret
= prefix_bgp_orf_set (name
, afi
, &orfp
,
1957 (common
& ORF_COMMON_PART_DENY
? 0 : 1 ),
1958 (common
& ORF_COMMON_PART_REMOVE
? 0 : 1));
1960 if (ret
!= CMD_SUCCESS
)
1962 if (BGP_DEBUG (normal
, NORMAL
))
1963 zlog_debug ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer
->host
);
1964 prefix_bgp_orf_remove_all (name
);
1968 peer
->orf_plist
[afi
][safi
] =
1969 prefix_list_lookup (AFI_ORF_PREFIX
, name
);
1971 stream_forward_getp (s
, orf_len
);
1973 if (BGP_DEBUG (normal
, NORMAL
))
1974 zlog_debug ("%s rcvd Refresh %s ORF request", peer
->host
,
1975 when_to_refresh
== REFRESH_DEFER
? "Defer" : "Immediate");
1976 if (when_to_refresh
== REFRESH_DEFER
)
1980 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1981 if (CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
))
1982 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
1984 /* Perform route refreshment to the peer */
1985 bgp_announce_route (peer
, afi
, safi
);
1989 bgp_capability_msg_parse (struct peer
*peer
, u_char
*pnt
, bgp_size_t length
)
1992 struct capability_mp_data mpc
;
1993 struct capability_header
*hdr
;
2004 /* We need at least action, capability code and capability length. */
2007 zlog_info ("%s Capability length error", peer
->host
);
2008 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2012 hdr
= (struct capability_header
*)(pnt
+ 1);
2014 /* Action value check. */
2015 if (action
!= CAPABILITY_ACTION_SET
2016 && action
!= CAPABILITY_ACTION_UNSET
)
2018 zlog_info ("%s Capability Action Value error %d",
2019 peer
->host
, action
);
2020 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2024 if (BGP_DEBUG (normal
, NORMAL
))
2025 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
2026 peer
->host
, action
, hdr
->code
, hdr
->length
);
2028 /* Capability length check. */
2029 if ((pnt
+ hdr
->length
+ 3) > end
)
2031 zlog_info ("%s Capability length error", peer
->host
);
2032 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2036 /* Fetch structure to the byte stream. */
2037 memcpy (&mpc
, pnt
+ 3, sizeof (struct capability_mp_data
));
2039 /* We know MP Capability Code. */
2040 if (hdr
->code
== CAPABILITY_CODE_MP
)
2042 afi
= ntohs (mpc
.afi
);
2045 /* Ignore capability when override-capability is set. */
2046 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
2049 if (!bgp_afi_safi_valid_indices (afi
, &safi
))
2051 if (BGP_DEBUG (normal
, NORMAL
))
2052 zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid",
2053 peer
->host
, afi
, safi
);
2057 /* Address family check. */
2058 if (BGP_DEBUG (normal
, NORMAL
))
2059 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2061 action
== CAPABILITY_ACTION_SET
2062 ? "Advertising" : "Removing",
2063 ntohs(mpc
.afi
) , mpc
.safi
);
2065 if (action
== CAPABILITY_ACTION_SET
)
2067 peer
->afc_recv
[afi
][safi
] = 1;
2068 if (peer
->afc
[afi
][safi
])
2070 peer
->afc_nego
[afi
][safi
] = 1;
2071 bgp_announce_route (peer
, afi
, safi
);
2076 peer
->afc_recv
[afi
][safi
] = 0;
2077 peer
->afc_nego
[afi
][safi
] = 0;
2079 if (peer_active_nego (peer
))
2080 bgp_clear_route (peer
, afi
, safi
);
2082 BGP_EVENT_ADD (peer
, BGP_Stop
);
2087 zlog_warn ("%s unrecognized capability code: %d - ignored",
2088 peer
->host
, hdr
->code
);
2090 pnt
+= hdr
->length
+ 3;
2095 /* Dynamic Capability is received. */
2097 bgp_capability_receive (struct peer
*peer
, bgp_size_t size
)
2102 /* Fetch pointer. */
2103 pnt
= stream_pnt (peer
->ibuf
);
2105 if (BGP_DEBUG (normal
, NORMAL
))
2106 zlog_debug ("%s rcv CAPABILITY", peer
->host
);
2108 /* If peer does not have the capability, send notification. */
2109 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_ADV
))
2111 plog_err (peer
->log
, "%s [Error] BGP dynamic capability is not enabled",
2113 bgp_notify_send (peer
,
2114 BGP_NOTIFY_HEADER_ERR
,
2115 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2119 /* Status must be Established. */
2120 if (peer
->status
!= Established
)
2122 plog_err (peer
->log
,
2123 "%s [Error] Dynamic capability packet received under status %s", peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
2124 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
2129 return bgp_capability_msg_parse (peer
, pnt
, size
);
2132 /* BGP read utility function. */
2134 bgp_read_packet (struct peer
*peer
)
2139 readsize
= peer
->packet_size
- stream_get_endp (peer
->ibuf
);
2141 /* If size is zero then return. */
2145 /* Read packet from fd. */
2146 nbytes
= stream_read_unblock (peer
->ibuf
, peer
->fd
, readsize
);
2148 /* If read byte is smaller than zero then error occured. */
2151 if (errno
== EAGAIN
)
2154 plog_err (peer
->log
, "%s [Error] bgp_read_packet error: %s",
2155 peer
->host
, safe_strerror (errno
));
2157 if (peer
->status
== Established
)
2159 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2161 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2162 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2165 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2168 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
2172 /* When read byte is zero : clear bgp peer and return */
2175 if (BGP_DEBUG (events
, EVENTS
))
2176 plog_debug (peer
->log
, "%s [Event] BGP connection closed fd %d",
2177 peer
->host
, peer
->fd
);
2179 if (peer
->status
== Established
)
2181 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2183 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2184 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2187 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2190 BGP_EVENT_ADD (peer
, TCP_connection_closed
);
2194 /* We read partial packet. */
2195 if (stream_get_endp (peer
->ibuf
) != peer
->packet_size
)
2203 bgp_marker_all_one (struct stream
*s
, int length
)
2207 for (i
= 0; i
< length
; i
++)
2208 if (s
->data
[i
] != 0xff)
2214 /* Starting point of packet process function. */
2216 bgp_read (struct thread
*thread
)
2222 char notify_data_length
[2];
2224 /* Yes first of all get peer pointer. */
2225 peer
= THREAD_ARG (thread
);
2226 peer
->t_read
= NULL
;
2228 /* For non-blocking IO check. */
2229 if (peer
->status
== Connect
)
2231 bgp_connect_check (peer
);
2238 zlog_err ("bgp_read peer's fd is negative value %d", peer
->fd
);
2241 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
2244 /* Read packet header to determine type of the packet */
2245 if (peer
->packet_size
== 0)
2246 peer
->packet_size
= BGP_HEADER_SIZE
;
2248 if (stream_get_endp (peer
->ibuf
) < BGP_HEADER_SIZE
)
2250 ret
= bgp_read_packet (peer
);
2252 /* Header read error or partial read packet. */
2256 /* Get size and type. */
2257 stream_forward_getp (peer
->ibuf
, BGP_MARKER_SIZE
);
2258 memcpy (notify_data_length
, stream_pnt (peer
->ibuf
), 2);
2259 size
= stream_getw (peer
->ibuf
);
2260 type
= stream_getc (peer
->ibuf
);
2262 if (BGP_DEBUG (normal
, NORMAL
) && type
!= 2 && type
!= 0)
2263 zlog_debug ("%s rcv message type %d, length (excl. header) %d",
2264 peer
->host
, type
, size
- BGP_HEADER_SIZE
);
2267 if (((type
== BGP_MSG_OPEN
) || (type
== BGP_MSG_KEEPALIVE
))
2268 && ! bgp_marker_all_one (peer
->ibuf
, BGP_MARKER_SIZE
))
2270 bgp_notify_send (peer
,
2271 BGP_NOTIFY_HEADER_ERR
,
2272 BGP_NOTIFY_HEADER_NOT_SYNC
);
2276 /* BGP type check. */
2277 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
2278 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
2279 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
2280 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
2281 && type
!= BGP_MSG_CAPABILITY
)
2283 if (BGP_DEBUG (normal
, NORMAL
))
2284 plog_debug (peer
->log
,
2285 "%s unknown message type 0x%02x",
2287 bgp_notify_send_with_data (peer
,
2288 BGP_NOTIFY_HEADER_ERR
,
2289 BGP_NOTIFY_HEADER_BAD_MESTYPE
,
2293 /* Mimimum packet length check. */
2294 if ((size
< BGP_HEADER_SIZE
)
2295 || (size
> BGP_MAX_PACKET_SIZE
)
2296 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
2297 || (type
== BGP_MSG_UPDATE
&& size
< BGP_MSG_UPDATE_MIN_SIZE
)
2298 || (type
== BGP_MSG_NOTIFY
&& size
< BGP_MSG_NOTIFY_MIN_SIZE
)
2299 || (type
== BGP_MSG_KEEPALIVE
&& size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
2300 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2301 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2302 || (type
== BGP_MSG_CAPABILITY
&& size
< BGP_MSG_CAPABILITY_MIN_SIZE
))
2304 if (BGP_DEBUG (normal
, NORMAL
))
2305 plog_debug (peer
->log
,
2306 "%s bad message length - %d for %s",
2308 type
== 128 ? "ROUTE-REFRESH" :
2309 bgp_type_str
[(int) type
]);
2310 bgp_notify_send_with_data (peer
,
2311 BGP_NOTIFY_HEADER_ERR
,
2312 BGP_NOTIFY_HEADER_BAD_MESLEN
,
2313 (u_char
*) notify_data_length
, 2);
2317 /* Adjust size to message length. */
2318 peer
->packet_size
= size
;
2321 ret
= bgp_read_packet (peer
);
2325 /* Get size and type again. */
2326 size
= stream_getw_from (peer
->ibuf
, BGP_MARKER_SIZE
);
2327 type
= stream_getc_from (peer
->ibuf
, BGP_MARKER_SIZE
+ 2);
2329 /* BGP packet dump function. */
2330 bgp_dump_packet (peer
, type
, peer
->ibuf
);
2332 size
= (peer
->packet_size
- BGP_HEADER_SIZE
);
2334 /* Read rest of the packet and call each sort of packet routine */
2339 bgp_open_receive (peer
, size
); /* XXX return value ignored! */
2341 case BGP_MSG_UPDATE
:
2342 peer
->readtime
= time(NULL
); /* Last read timer reset */
2343 bgp_update_receive (peer
, size
);
2345 case BGP_MSG_NOTIFY
:
2346 bgp_notify_receive (peer
, size
);
2348 case BGP_MSG_KEEPALIVE
:
2349 peer
->readtime
= time(NULL
); /* Last read timer reset */
2350 bgp_keepalive_receive (peer
, size
);
2352 case BGP_MSG_ROUTE_REFRESH_NEW
:
2353 case BGP_MSG_ROUTE_REFRESH_OLD
:
2355 bgp_route_refresh_receive (peer
, size
);
2357 case BGP_MSG_CAPABILITY
:
2358 peer
->dynamic_cap_in
++;
2359 bgp_capability_receive (peer
, size
);
2363 /* Clear input buffer. */
2364 peer
->packet_size
= 0;
2366 stream_reset (peer
->ibuf
);
2369 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
2371 if (BGP_DEBUG (events
, EVENTS
))
2372 zlog_debug ("%s [Event] Accepting BGP peer delete", peer
->host
);