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 () */
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_table.h"
38 #include "bgpd/bgp_dump.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_fsm.h"
42 #include "bgpd/bgp_route.h"
43 #include "bgpd/bgp_packet.h"
44 #include "bgpd/bgp_open.h"
45 #include "bgpd/bgp_aspath.h"
46 #include "bgpd/bgp_community.h"
47 #include "bgpd/bgp_ecommunity.h"
48 #include "bgpd/bgp_network.h"
49 #include "bgpd/bgp_mplsvpn.h"
50 #include "bgpd/bgp_encap.h"
51 #include "bgpd/bgp_advertise.h"
52 #include "bgpd/bgp_vty.h"
54 int stream_put_prefix (struct stream *, struct prefix *);
56 /* Set up BGP packet marker and packet type. */
58 bgp_packet_set_marker (struct stream *s, u_char type)
63 for (i = 0; i < BGP_MARKER_SIZE; i++)
64 stream_putc (s, 0xff);
66 /* Dummy total length. This field is should be filled in later on. */
69 /* BGP packet type. */
70 stream_putc (s, type);
72 /* Return current stream size. */
73 return stream_get_endp (s);
76 /* Set BGP packet header size entry. If size is zero then use current
79 bgp_packet_set_size (struct stream *s)
83 /* Preserve current pointer. */
84 cp = stream_get_endp (s);
85 stream_putw_at (s, BGP_MARKER_SIZE, cp);
90 /* Add new packet to the peer. */
92 bgp_packet_add (struct peer *peer, struct stream *s)
94 /* Add packet to the end of list. */
95 stream_fifo_push (peer->obuf, s);
98 /* Free first packet. */
100 bgp_packet_delete (struct peer *peer)
102 stream_free (stream_fifo_pop (peer->obuf));
105 /* Check file descriptor whether connect is established. */
107 bgp_connect_check (struct peer *peer)
113 /* Anyway I have to reset read and write thread. */
114 BGP_READ_OFF (peer->t_read);
115 BGP_WRITE_OFF (peer->t_write);
117 /* Check file descriptor. */
118 slen = sizeof (status);
119 ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen);
121 /* If getsockopt is fail, this is fatal error. */
124 zlog (peer->log, LOG_INFO, "can't get sockopt for nonblocking connect");
125 BGP_EVENT_ADD (peer, TCP_fatal_error);
129 /* When status is 0 then TCP connection is established. */
132 BGP_EVENT_ADD (peer, TCP_connection_open);
136 if (BGP_DEBUG (events, EVENTS))
137 plog_debug (peer->log, "%s [Event] Connect failed (%s)",
138 peer->host, safe_strerror (errno));
139 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
143 /* Make BGP update packet. */
144 static struct stream *
145 bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi)
148 struct stream *snlri;
149 struct bgp_adj_out *adj;
150 struct bgp_advertise *adv;
151 struct stream *packet;
152 struct bgp_node *rn = NULL;
153 struct bgp_info *binfo = NULL;
154 bgp_size_t total_attr_len = 0;
155 unsigned long attrlen_pos = 0;
156 int space_remaining = 0;
157 int space_needed = 0;
158 size_t mpattrlen_pos = 0;
159 size_t mpattr_pos = 0;
163 snlri = peer->scratch;
164 stream_reset (snlri);
166 adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update);
176 space_remaining = STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) -
177 BGP_MAX_PACKET_SIZE_OVERFLOW;
178 space_needed = BGP_NLRI_LENGTH + bgp_packet_mpattr_prefix_size (afi, safi, &rn->p);
180 /* When remaining space can't include NLRI and it's length. */
181 if (space_remaining < space_needed)
184 /* If packet is empty, set attribute. */
185 if (stream_empty (s))
187 struct prefix_rd *prd = NULL;
189 struct peer *from = NULL;
192 prd = (struct prefix_rd *) &rn->prn->p;
197 tag = binfo->extra->tag;
200 /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
201 * one byte message type.
203 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
205 /* 2: withdrawn routes length */
208 /* 3: total attributes length - attrlen_pos stores the position */
209 attrlen_pos = stream_get_endp (s);
212 /* 4: if there is MP_REACH_NLRI attribute, that should be the first
213 * attribute, according to draft-ietf-idr-error-handling. Save the
216 mpattr_pos = stream_get_endp(s);
218 /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
219 total_attr_len = bgp_packet_attribute (NULL, peer, s,
221 ((afi == AFI_IP && safi == SAFI_UNICAST) ?
225 space_remaining = STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) -
226 BGP_MAX_PACKET_SIZE_OVERFLOW;
227 space_needed = BGP_NLRI_LENGTH + bgp_packet_mpattr_prefix_size (afi, safi, &rn->p);;
229 /* If the attributes alone do not leave any room for NLRI then
231 if (space_remaining < space_needed)
233 zlog_err ("%s cannot send UPDATE, the attributes do not leave "
234 "room for NLRI", peer->host);
235 /* Flush the FIFO update queue */
237 adv = bgp_advertise_clean (peer, adv->adj, afi, safi);
243 if (afi == AFI_IP && safi == SAFI_UNICAST)
244 stream_put_prefix (s, &rn->p);
247 /* Encode the prefix in MP_REACH_NLRI attribute */
248 struct prefix_rd *prd = NULL;
252 prd = (struct prefix_rd *) &rn->prn->p;
253 if (binfo && binfo->extra)
254 tag = binfo->extra->tag;
256 if (stream_empty(snlri))
257 mpattrlen_pos = bgp_packet_mpattr_start(snlri, afi, safi,
259 bgp_packet_mpattr_prefix(snlri, afi, safi, &rn->p, prd, tag);
261 if (BGP_DEBUG (update, UPDATE_OUT))
263 char buf[INET6_BUFSIZ];
265 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d",
267 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ),
271 /* Synchnorize attribute. */
273 bgp_attr_unintern (&adj->attr);
275 peer->scount[afi][safi]++;
277 adj->attr = bgp_attr_intern (adv->baa->attr);
279 adv = bgp_advertise_clean (peer, adj, afi, safi);
282 if (! stream_empty (s))
284 if (!stream_empty(snlri))
286 bgp_packet_mpattr_end(snlri, mpattrlen_pos);
287 total_attr_len += stream_get_endp(snlri);
290 /* set the total attribute length correctly */
291 stream_putw_at (s, attrlen_pos, total_attr_len);
293 if (!stream_empty(snlri))
294 packet = stream_dupcat(s, snlri, mpattr_pos);
296 packet = stream_dup (s);
297 bgp_packet_set_size (packet);
298 bgp_packet_add (peer, packet);
299 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
301 stream_reset (snlri);
307 static struct stream *
308 bgp_update_packet_eor (struct peer *peer, afi_t afi, safi_t safi)
312 if (DISABLE_BGP_ANNOUNCE)
315 if (BGP_DEBUG (normal, NORMAL))
316 zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi, safi), peer->host);
318 s = stream_new (BGP_MAX_PACKET_SIZE);
320 /* Make BGP update packet. */
321 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
323 /* Unfeasible Routes Length */
326 if (afi == AFI_IP && safi == SAFI_UNICAST)
328 /* Total Path Attribute Length */
333 /* Total Path Attribute Length */
335 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
336 stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI);
338 stream_putw (s, afi);
339 stream_putc (s, safi);
342 bgp_packet_set_size (s);
343 bgp_packet_add (peer, s);
347 /* Make BGP withdraw packet. */
349 16-octet marker | 2-octet length | 1-octet type |
350 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
352 /* For other afi/safis:
353 16-octet marker | 2-octet length | 1-octet type |
354 2-octet withdrawn route length (=0) | 2-octet attrlen |
355 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
357 static struct stream *
358 bgp_withdraw_packet (struct peer *peer, afi_t afi, safi_t safi)
361 struct stream *packet;
362 struct bgp_adj_out *adj;
363 struct bgp_advertise *adv;
365 bgp_size_t unfeasible_len;
366 bgp_size_t total_attr_len;
368 size_t attrlen_pos = 0;
369 size_t mplen_pos = 0;
370 u_char first_time = 1;
371 int space_remaining = 0;
372 int space_needed = 0;
377 while ((adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL)
383 space_remaining = STREAM_REMAIN (s) -
384 BGP_MAX_PACKET_SIZE_OVERFLOW;
385 space_needed = (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN +
386 bgp_packet_mpattr_prefix_size (afi, safi, &rn->p));
388 if (space_remaining < space_needed)
391 if (stream_empty (s))
393 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
394 stream_putw (s, 0); /* unfeasible routes length */
399 if (afi == AFI_IP && safi == SAFI_UNICAST)
400 stream_put_prefix (s, &rn->p);
403 struct prefix_rd *prd = NULL;
406 prd = (struct prefix_rd *) &rn->prn->p;
408 /* If first time, format the MP_UNREACH header */
411 attrlen_pos = stream_get_endp (s);
412 /* total attr length = 0 for now. reevaluate later */
414 mp_start = stream_get_endp (s);
415 mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
418 bgp_packet_mpunreach_prefix(s, &rn->p, afi, safi, prd, NULL);
421 if (BGP_DEBUG (update, UPDATE_OUT))
423 char buf[INET6_BUFSIZ];
425 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
427 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ),
431 peer->scount[afi][safi]--;
433 bgp_adj_out_remove (rn, adj, peer, afi, safi);
434 bgp_unlock_node (rn);
437 if (! stream_empty (s))
439 if (afi == AFI_IP && safi == SAFI_UNICAST)
442 = stream_get_endp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
443 stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
448 /* Set the mp_unreach attr's length */
449 bgp_packet_mpunreach_end(s, mplen_pos);
451 /* Set total path attribute length. */
452 total_attr_len = stream_get_endp(s) - mp_start;
453 stream_putw_at (s, attrlen_pos, total_attr_len);
455 bgp_packet_set_size (s);
456 packet = stream_dup (s);
457 bgp_packet_add (peer, packet);
466 bgp_default_update_send (struct peer *peer, struct attr *attr,
467 afi_t afi, safi_t safi, struct peer *from)
472 bgp_size_t total_attr_len;
474 if (DISABLE_BGP_ANNOUNCE)
478 str2prefix ("0.0.0.0/0", &p);
480 str2prefix ("::/0", &p);
482 /* Logging the attribute. */
483 if (BGP_DEBUG (update, UPDATE_OUT))
485 char attrstr[BUFSIZ];
486 char buf[INET6_BUFSIZ];
489 bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
490 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d %s",
491 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
492 p.prefixlen, attrstr);
495 s = stream_new (BGP_MAX_PACKET_SIZE);
497 /* Make BGP update packet. */
498 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
500 /* Unfeasible Routes Length. */
503 /* Make place for total attribute length. */
504 pos = stream_get_endp (s);
506 total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &p, afi, safi, from, NULL, NULL);
508 /* Set Total Path Attribute Length. */
509 stream_putw_at (s, pos, total_attr_len);
512 if (p.family == AF_INET && safi == SAFI_UNICAST)
513 stream_put_prefix (s, &p);
516 bgp_packet_set_size (s);
518 /* Dump packet if debug option is set. */
520 /* bgp_packet_dump (packet); */
523 /* Add packet to the peer. */
524 bgp_packet_add (peer, s);
526 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
530 bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi)
534 unsigned long attrlen_pos = 0;
536 bgp_size_t unfeasible_len;
537 bgp_size_t total_attr_len;
539 size_t mplen_pos = 0;
541 if (DISABLE_BGP_ANNOUNCE)
545 str2prefix ("0.0.0.0/0", &p);
547 str2prefix ("::/0", &p);
551 if (BGP_DEBUG (update, UPDATE_OUT))
553 char buf[INET6_BUFSIZ];
555 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
556 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
560 s = stream_new (BGP_MAX_PACKET_SIZE);
562 /* Make BGP update packet. */
563 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
565 /* Unfeasible Routes Length. */;
566 cp = stream_get_endp (s);
569 /* Withdrawn Routes. */
570 if (p.family == AF_INET && safi == SAFI_UNICAST)
572 stream_put_prefix (s, &p);
574 unfeasible_len = stream_get_endp (s) - cp - 2;
576 /* Set unfeasible len. */
577 stream_putw_at (s, cp, unfeasible_len);
579 /* Set total path attribute length. */
584 attrlen_pos = stream_get_endp (s);
586 mp_start = stream_get_endp (s);
587 mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
588 bgp_packet_mpunreach_prefix(s, &p, afi, safi, NULL, NULL);
590 /* Set the mp_unreach attr's length */
591 bgp_packet_mpunreach_end(s, mplen_pos);
593 /* Set total path attribute length. */
594 total_attr_len = stream_get_endp(s) - mp_start;
595 stream_putw_at (s, attrlen_pos, total_attr_len);
598 bgp_packet_set_size (s);
600 /* Add packet to the peer. */
601 bgp_packet_add (peer, s);
603 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
606 /* Get next packet to be written. */
607 static struct stream *
608 bgp_write_packet (struct peer *peer)
612 struct stream *s = NULL;
613 struct bgp_advertise *adv;
615 s = stream_fifo_head (peer->obuf);
619 for (afi = AFI_IP; afi < AFI_MAX; afi++)
620 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
622 adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->withdraw);
625 s = bgp_withdraw_packet (peer, afi, safi);
631 for (afi = AFI_IP; afi < AFI_MAX; afi++)
632 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
634 adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update);
637 if (adv->binfo && adv->binfo->uptime < peer->synctime)
639 if (CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_RCV)
640 && CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_ADV)
641 && ! (CHECK_FLAG (adv->binfo->peer->cap,
642 PEER_CAP_RESTART_BIT_RCV) &&
643 CHECK_FLAG (adv->binfo->peer->cap,
644 PEER_CAP_RESTART_BIT_ADV))
645 && ! CHECK_FLAG (adv->binfo->flags, BGP_INFO_STALE)
646 && safi != SAFI_MPLS_VPN)
648 if (CHECK_FLAG (adv->binfo->peer->af_sflags[afi][safi],
649 PEER_STATUS_EOR_RECEIVED))
650 s = bgp_update_packet (peer, afi, safi);
653 s = bgp_update_packet (peer, afi, safi);
660 if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV))
662 if (peer->afc_nego[afi][safi] && peer->synctime
663 && ! CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND)
664 && safi != SAFI_MPLS_VPN)
666 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND);
667 return bgp_update_packet_eor (peer, afi, safi);
675 /* Is there partially written packet or updates we can send right
678 bgp_write_proceed (struct peer *peer)
682 struct bgp_advertise *adv;
684 if (stream_fifo_head (peer->obuf))
687 for (afi = AFI_IP; afi < AFI_MAX; afi++)
688 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
689 if (FIFO_HEAD (&peer->sync[afi][safi]->withdraw))
692 for (afi = AFI_IP; afi < AFI_MAX; afi++)
693 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
694 if ((adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL)
695 if (adv->binfo->uptime < peer->synctime)
701 /* Write packet to the peer. */
703 bgp_write (struct thread *thread)
709 unsigned int count = 0;
711 /* Yes first of all get peer pointer. */
712 peer = THREAD_ARG (thread);
713 peer->t_write = NULL;
715 /* For non-blocking IO check. */
716 if (peer->status == Connect)
718 bgp_connect_check (peer);
722 s = bgp_write_packet (peer);
724 return 0; /* nothing to send */
726 sockopt_cork (peer->fd, 1);
728 /* Nonblocking write until TCP output buffer is full. */
733 /* Number of bytes to be sent. */
734 writenum = stream_get_endp (s) - stream_get_getp (s);
736 /* Call write() system call. */
737 num = write (peer->fd, STREAM_PNT (s), writenum);
740 /* write failed either retry needed or error */
741 if (ERRNO_IO_RETRY(errno))
744 BGP_EVENT_ADD (peer, TCP_fatal_error);
751 stream_forward_getp (s, num);
755 /* Retrieve BGP packet type. */
756 stream_set_getp (s, BGP_MARKER_SIZE + 2);
757 type = stream_getc (s);
770 /* Flush any existing events */
771 BGP_EVENT_ADD (peer, BGP_Stop_with_error);
774 case BGP_MSG_KEEPALIVE:
775 peer->keepalive_out++;
777 case BGP_MSG_ROUTE_REFRESH_NEW:
778 case BGP_MSG_ROUTE_REFRESH_OLD:
781 case BGP_MSG_CAPABILITY:
782 peer->dynamic_cap_out++;
786 /* OK we send packet so delete it. */
787 bgp_packet_delete (peer);
789 while (++count < BGP_WRITE_PACKET_MAX &&
790 (s = bgp_write_packet (peer)) != NULL);
792 if (bgp_write_proceed (peer))
793 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
796 sockopt_cork (peer->fd, 0);
800 /* This is only for sending NOTIFICATION message to neighbor. */
802 bgp_write_notify (struct peer *peer)
808 /* There should be at least one packet. */
809 s = stream_fifo_head (peer->obuf);
812 assert (stream_get_endp (s) >= BGP_HEADER_SIZE);
814 /* Stop collecting data within the socket */
815 sockopt_cork (peer->fd, 0);
817 /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
818 * we only care about getting a clean shutdown at this point. */
819 ret = write (peer->fd, STREAM_DATA (s), stream_get_endp (s));
821 /* only connection reset/close gets counted as TCP_fatal_error, failure
822 * to write the entire NOTIFY doesn't get different FSM treatment */
825 BGP_EVENT_ADD (peer, TCP_fatal_error);
829 /* Disable Nagle, make NOTIFY packet go out right away */
831 (void) setsockopt (peer->fd, IPPROTO_TCP, TCP_NODELAY,
832 (char *) &val, sizeof (val));
834 /* Retrieve BGP packet type. */
835 stream_set_getp (s, BGP_MARKER_SIZE + 2);
836 type = stream_getc (s);
838 assert (type == BGP_MSG_NOTIFY);
840 /* Type should be notify. */
843 BGP_EVENT_ADD (peer, BGP_Stop_with_error);
848 /* Make keepalive packet and send it to the peer. */
850 bgp_keepalive_send (struct peer *peer)
855 s = stream_new (BGP_MAX_PACKET_SIZE);
857 /* Make keepalive packet. */
858 bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE);
860 /* Set packet size. */
861 length = bgp_packet_set_size (s);
863 /* Dump packet if debug option is set. */
864 /* bgp_packet_dump (s); */
866 if (BGP_DEBUG (keepalive, KEEPALIVE))
867 zlog_debug ("%s sending KEEPALIVE", peer->host);
868 if (BGP_DEBUG (normal, NORMAL))
869 zlog_debug ("%s send message type %d, length (incl. header) %d",
870 peer->host, BGP_MSG_KEEPALIVE, length);
872 /* Add packet to the peer. */
873 bgp_packet_add (peer, s);
875 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
878 /* Make open packet and send it to the peer. */
880 bgp_open_send (struct peer *peer)
884 u_int16_t send_holdtime;
887 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
888 send_holdtime = peer->holdtime;
890 send_holdtime = peer->bgp->default_holdtime;
892 /* local-as Change */
893 if (peer->change_local_as)
894 local_as = peer->change_local_as;
896 local_as = peer->local_as;
898 s = stream_new (BGP_MAX_PACKET_SIZE);
900 /* Make open packet. */
901 bgp_packet_set_marker (s, BGP_MSG_OPEN);
903 /* Set open packet values. */
904 stream_putc (s, BGP_VERSION_4); /* BGP version */
905 stream_putw (s, (local_as <= BGP_AS_MAX) ? (u_int16_t) local_as
907 stream_putw (s, send_holdtime); /* Hold Time */
908 stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */
910 /* Set capability code. */
911 bgp_open_capability (s, peer);
913 /* Set BGP packet length. */
914 length = bgp_packet_set_size (s);
916 if (BGP_DEBUG (normal, NORMAL))
917 zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
918 peer->host, BGP_VERSION_4, local_as,
919 send_holdtime, inet_ntoa (peer->local_id));
921 if (BGP_DEBUG (normal, NORMAL))
922 zlog_debug ("%s send message type %d, length (incl. header) %d",
923 peer->host, BGP_MSG_OPEN, length);
925 /* Dump packet if debug option is set. */
926 /* bgp_packet_dump (s); */
928 /* Add packet to the peer. */
929 bgp_packet_add (peer, s);
931 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
934 /* Send BGP notify packet with data potion. */
936 bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code,
937 u_char *data, size_t datalen)
942 /* Allocate new stream. */
943 s = stream_new (BGP_MAX_PACKET_SIZE);
945 /* Make nitify packet. */
946 bgp_packet_set_marker (s, BGP_MSG_NOTIFY);
948 /* Set notify packet values. */
949 stream_putc (s, code); /* BGP notify code */
950 stream_putc (s, sub_code); /* BGP notify sub_code */
952 /* If notify data is present. */
954 stream_write (s, data, datalen);
956 /* Set BGP packet length. */
957 length = bgp_packet_set_size (s);
959 /* Add packet to the peer. */
960 stream_fifo_clean (peer->obuf);
961 bgp_packet_add (peer, s);
965 struct bgp_notify bgp_notify;
970 bgp_notify.code = code;
971 bgp_notify.subcode = sub_code;
972 bgp_notify.data = NULL;
973 bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE;
975 if (bgp_notify.length)
977 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
978 for (i = 0; i < bgp_notify.length; i++)
981 sprintf (c, " %02x", data[i]);
982 strcat (bgp_notify.data, c);
987 sprintf (c, "%02x", data[i]);
988 strcpy (bgp_notify.data, c);
991 bgp_notify_print (peer, &bgp_notify, "sending");
995 XFREE (MTYPE_TMP, bgp_notify.data);
996 bgp_notify.data = NULL;
997 bgp_notify.length = 0;
1001 if (BGP_DEBUG (normal, NORMAL))
1002 zlog_debug ("%s send message type %d, length (incl. header) %d",
1003 peer->host, BGP_MSG_NOTIFY, length);
1005 /* peer reset cause */
1006 if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE)
1008 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
1010 peer->last_reset = PEER_DOWN_USER_RESET;
1011 zlog_info ("Notification sent to neighbor %s:%u: User reset",
1012 peer->host, sockunion_get_port (&peer->su));
1014 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
1016 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
1017 zlog_info ("Notification sent to neighbor %s:%u shutdown",
1018 peer->host, sockunion_get_port (&peer->su));
1022 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
1023 zlog_info ("Notification sent to neighbor %s:%u: type %u/%u",
1024 peer->host, sockunion_get_port (&peer->su),
1029 zlog_info ("Notification sent to neighbor %s:%u: configuration change",
1030 peer->host, sockunion_get_port (&peer->su));
1032 /* Call immediately. */
1033 BGP_WRITE_OFF (peer->t_write);
1035 bgp_write_notify (peer);
1038 /* Send BGP notify packet. */
1040 bgp_notify_send (struct peer *peer, u_char code, u_char sub_code)
1042 bgp_notify_send_with_data (peer, code, sub_code, NULL, 0);
1045 /* Send route refresh message to the peer. */
1047 bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi,
1048 u_char orf_type, u_char when_to_refresh, int remove)
1052 struct bgp_filter *filter;
1053 int orf_refresh = 0;
1055 if (DISABLE_BGP_ANNOUNCE)
1058 filter = &peer->filter[afi][safi];
1060 /* Adjust safi code. */
1061 if (safi == SAFI_MPLS_VPN)
1062 safi = SAFI_MPLS_LABELED_VPN;
1064 s = stream_new (BGP_MAX_PACKET_SIZE);
1066 /* Make BGP update packet. */
1067 if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
1068 bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW);
1070 bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD);
1072 /* Encode Route Refresh message. */
1073 stream_putw (s, afi);
1075 stream_putc (s, safi);
1077 if (orf_type == ORF_TYPE_PREFIX
1078 || orf_type == ORF_TYPE_PREFIX_OLD)
1079 if (remove || filter->plist[FILTER_IN].plist)
1085 stream_putc (s, when_to_refresh);
1086 stream_putc (s, orf_type);
1087 orfp = stream_get_endp (s);
1092 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
1093 stream_putc (s, ORF_COMMON_PART_REMOVE_ALL);
1094 if (BGP_DEBUG (normal, NORMAL))
1095 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
1096 peer->host, orf_type,
1097 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
1102 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
1103 prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist,
1104 ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT,
1105 ORF_COMMON_PART_DENY);
1106 if (BGP_DEBUG (normal, NORMAL))
1107 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
1108 peer->host, orf_type,
1109 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
1113 /* Total ORF Entry Len. */
1114 orf_len = stream_get_endp (s) - orfp - 2;
1115 stream_putw_at (s, orfp, orf_len);
1118 /* Set packet size. */
1119 length = bgp_packet_set_size (s);
1121 if (BGP_DEBUG (normal, NORMAL))
1124 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
1125 peer->host, afi, safi);
1126 zlog_debug ("%s send message type %d, length (incl. header) %d",
1127 peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ?
1128 BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length);
1131 /* Add packet to the peer. */
1132 bgp_packet_add (peer, s);
1134 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
1137 /* Send capability message to the peer. */
1139 bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi,
1140 int capability_code, int action)
1145 /* Adjust safi code. */
1146 if (safi == SAFI_MPLS_VPN)
1147 safi = SAFI_MPLS_LABELED_VPN;
1149 s = stream_new (BGP_MAX_PACKET_SIZE);
1151 /* Make BGP update packet. */
1152 bgp_packet_set_marker (s, BGP_MSG_CAPABILITY);
1154 /* Encode MP_EXT capability. */
1155 if (capability_code == CAPABILITY_CODE_MP)
1157 stream_putc (s, action);
1158 stream_putc (s, CAPABILITY_CODE_MP);
1159 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1160 stream_putw (s, afi);
1162 stream_putc (s, safi);
1164 if (BGP_DEBUG (normal, NORMAL))
1165 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
1166 peer->host, action == CAPABILITY_ACTION_SET ?
1167 "Advertising" : "Removing", afi, safi);
1170 /* Set packet size. */
1171 length = bgp_packet_set_size (s);
1174 /* Add packet to the peer. */
1175 bgp_packet_add (peer, s);
1177 if (BGP_DEBUG (normal, NORMAL))
1178 zlog_debug ("%s send message type %d, length (incl. header) %d",
1179 peer->host, BGP_MSG_CAPABILITY, length);
1181 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
1184 /* RFC1771 6.8 Connection collision detection. */
1186 bgp_collision_detect (struct peer *new, struct in_addr remote_id)
1189 struct listnode *node, *nnode;
1192 bgp = bgp_get_default ();
1196 /* Upon receipt of an OPEN message, the local system must examine
1197 all of its connections that are in the OpenConfirm state. A BGP
1198 speaker may also examine connections in an OpenSent state if it
1199 knows the BGP Identifier of the peer by means outside of the
1200 protocol. If among these connections there is a connection to a
1201 remote BGP speaker whose BGP Identifier equals the one in the
1202 OPEN message, then the local system performs the following
1203 collision resolution procedure: */
1205 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
1209 if (!sockunion_same (&peer->su, &new->su))
1212 /* Unless allowed via configuration, a connection collision with an
1213 existing BGP connection that is in the Established state causes
1214 closing of the newly created connection. */
1215 if (peer->status == Established)
1217 /* GR may do things slightly differently to classic RFC . Punt to
1218 * open_receive, see below
1220 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
1225 if (BGP_DEBUG (events, EVENTS))
1226 zlog_debug ("%s:%u Existing Established peer, sending NOTIFY",
1227 new->host, sockunion_get_port (&new->su));
1228 bgp_notify_send (new, BGP_NOTIFY_CEASE,
1229 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1234 /* Note: Quagga historically orders explicitly only on the processing
1235 * of the Opens, treating 'new' as the passive, inbound and connection
1236 * and 'peer' as the active outbound connection.
1239 /* The local_id is always set, so we can match the given remote-ID
1240 * from the OPEN against both OpenConfirm and OpenSent peers.
1242 if (peer->status == OpenConfirm || peer->status == OpenSent)
1244 struct peer *out = peer;
1245 struct peer *in = new;
1246 int ret_close_out = 1, ret_close_in = -1;
1248 if (!CHECK_FLAG (new->sflags, PEER_STATUS_ACCEPT_PEER))
1256 /* 1. The BGP Identifier of the local system is compared to
1257 the BGP Identifier of the remote system (as specified in
1258 the OPEN message). */
1260 if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr))
1262 /* 2. If the value of the local BGP Identifier is less
1263 than the remote one, the local system closes BGP
1264 connection that already exists (the one that is
1265 already in the OpenConfirm state), and accepts BGP
1266 connection initiated by the remote system. */
1270 if (BGP_DEBUG (events, EVENTS))
1271 zlog_debug ("%s Collision resolution, remote ID higher,"
1272 " closing outbound", peer->host);
1273 bgp_notify_send (out, BGP_NOTIFY_CEASE,
1274 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1276 return ret_close_out;
1280 /* 3. Otherwise, the local system closes newly created
1281 BGP connection (the one associated with the newly
1282 received OPEN message), and continues to use the
1283 existing one (the one that is already in the
1284 OpenConfirm state). */
1288 if (BGP_DEBUG (events, EVENTS))
1289 zlog_debug ("%s Collision resolution, local ID higher,"
1290 " closing inbound", peer->host);
1292 bgp_notify_send (in, BGP_NOTIFY_CEASE,
1293 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1295 return ret_close_in;
1303 bgp_open_receive (struct peer *peer, bgp_size_t size)
1309 u_int16_t send_holdtime;
1312 struct peer *realpeer;
1313 struct in_addr remote_id;
1315 u_int8_t notify_data_remote_as[2];
1316 u_int8_t notify_data_remote_id[4];
1317 u_int16_t *holdtime_ptr;
1321 /* Parse open packet. */
1322 version = stream_getc (peer->ibuf);
1323 memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2);
1324 remote_as = stream_getw (peer->ibuf);
1325 holdtime_ptr = (u_int16_t *)stream_pnt (peer->ibuf);
1326 holdtime = stream_getw (peer->ibuf);
1327 memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
1328 remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
1330 /* Receive OPEN message log */
1331 if (BGP_DEBUG (normal, NORMAL))
1332 zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
1333 " holdtime %d, id %s, %sbound connection",
1334 peer->host, version, remote_as, holdtime,
1335 inet_ntoa (remote_id),
1336 CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)
1339 /* BEGIN to read the capability here, but dont do it yet */
1341 optlen = stream_getc (peer->ibuf);
1345 /* We need the as4 capability value *right now* because
1346 * if it is there, we have not got the remote_as yet, and without
1347 * that we do not know which peer is connecting to us now.
1349 as4 = peek_for_as4_capability (peer, optlen);
1352 /* Just in case we have a silly peer who sends AS4 capability set to 0 */
1353 if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) && !as4)
1355 zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
1357 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1358 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1362 if (remote_as == BGP_AS_TRANS)
1364 /* Take the AS4 from the capability. We must have received the
1365 * capability now! Otherwise we have a asn16 peer who uses
1366 * BGP_AS_TRANS, for some unknown reason.
1368 if (as4 == BGP_AS_TRANS)
1370 zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1372 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1373 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1377 if (!as4 && BGP_DEBUG (as4, AS4))
1378 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
1379 " Odd, but proceeding.", peer->host);
1380 else if (as4 < BGP_AS_MAX && BGP_DEBUG (as4, AS4))
1381 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
1382 "in 2-bytes, very odd peer.", peer->host, as4);
1388 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
1389 /* If we have got the capability, peer->as4cap must match remote_as */
1390 if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)
1391 && as4 != remote_as)
1393 /* raise error, log this, close session */
1394 zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
1395 " mismatch with 16bit 'myasn' %u in open",
1396 peer->host, as4, remote_as);
1397 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1398 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1403 /* Lookup peer from Open packet. */
1404 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1408 realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as);
1412 /* Peer's source IP address is check in bgp_accept(), so this
1413 must be AS number mismatch or remote-id configuration
1417 if (BGP_DEBUG (normal, NORMAL))
1418 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1419 peer->host, inet_ntoa (remote_id));
1420 bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1421 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1422 notify_data_remote_id, 4);
1426 if (BGP_DEBUG (normal, NORMAL))
1427 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1428 peer->host, remote_as, peer->as);
1429 bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1430 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1431 notify_data_remote_as, 2);
1437 /* When collision is detected and this peer is closed. Retrun
1439 ret = bgp_collision_detect (peer, remote_id);
1444 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1446 /* Connection FSM state is intertwined with our peer configuration
1447 * (the RFC encourages this a bit). At _this_ point we have a
1448 * 'realpeer' which represents the configuration and any earlier FSM
1449 * (outbound, unless the remote side has opened two connections to
1450 * us), and a 'peer' which here represents an inbound connection that
1451 * has not yet been reconciled with a 'realpeer'.
1453 * As 'peer' has just sent an OPEN that reconciliation must now
1454 * happen, as only the 'realpeer' can ever proceed to Established.
1456 * bgp_collision_detect should have resolved any collisions with
1457 * realpeers that are in states OpenSent, OpenConfirm or Established,
1458 * and may have sent a notify on the 'realpeer' connection.
1459 * bgp_accept will have rejected any connections where the 'realpeer'
1460 * is in Idle or >Established (though, that status may have changed
1463 * Need to finish off any reconciliation here, and ensure that
1464 * 'realpeer' is left holding any needed state from the appropriate
1465 * connection (fd, buffers, etc.), and any state from the other
1466 * connection is cleaned up.
1469 /* Is realpeer in some globally-down state, that precludes any and all
1470 * connections (Idle, Clearing, Deleted, etc.)?
1472 if (realpeer->status == Idle || realpeer->status > Established)
1474 if (BGP_DEBUG (events, EVENTS))
1475 zlog_debug ("%s peer status is %s, closing the new connection",
1477 LOOKUP (bgp_status_msg, realpeer->status));
1481 /* GR does things differently, and prefers any new connection attempts
1482 * over an Established one (why not just rely on KEEPALIVE and avoid
1483 * having to special case this?) */
1484 if (realpeer->status == Established
1485 && CHECK_FLAG (realpeer->sflags, PEER_STATUS_NSF_MODE))
1487 realpeer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
1488 SET_FLAG (realpeer->sflags, PEER_STATUS_NSF_WAIT);
1492 /* If we're here, RFC collision-detect did not reconcile the
1493 * connections, and the 'realpeer' is still available. So
1494 * 'realpeer' must be 'Active' or 'Connect'.
1496 * According to the RFC we should just let this connection (of the
1497 * accepted 'peer') continue on to Established if the other
1498 * onnection (the 'realpeer') is in a more larval state, and
1499 * reconcile them when OPEN is sent on the 'realpeer'.
1501 * However, the accepted 'peer' must be reconciled with 'peer' at
1502 * this point, due to the implementation, if 'peer' is to be able
1503 * to proceed. So it should be allowed to go to Established, as
1504 * long as the 'realpeer' was in Active or Connect state - which
1505 * /should/ be the case if we're here.
1507 * So we should only need to sanity check that that is the case
1508 * here, and allow the code to get on with transferring the 'peer'
1509 * connection state over.
1511 if (realpeer->status != Active && realpeer->status != Connect)
1513 if (BGP_DEBUG (events, EVENTS))
1514 zlog_warn ("%s real peer status should be Active or Connect,"
1517 LOOKUP (bgp_status_msg, realpeer->status));
1518 bgp_notify_send (realpeer, BGP_NOTIFY_CEASE,
1519 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1523 if (BGP_DEBUG (events, EVENTS))
1524 zlog_debug ("%s:%u [Event] Transfer accept BGP peer to real (state %s)",
1525 peer->host, sockunion_get_port (&peer->su),
1526 LOOKUP (bgp_status_msg, realpeer->status));
1528 bgp_stop (realpeer);
1530 /* Transfer file descriptor. */
1531 realpeer->fd = peer->fd;
1534 /* Transfer input buffer. */
1535 stream_free (realpeer->ibuf);
1536 realpeer->ibuf = peer->ibuf;
1537 realpeer->packet_size = peer->packet_size;
1540 /* Transfer output buffer, there may be an OPEN queued to send */
1541 stream_fifo_free (realpeer->obuf);
1542 realpeer->obuf = peer->obuf;
1546 = CHECK_FLAG (peer->sflags, PEER_STATUS_OPEN_DEFERRED);
1548 /* Transfer status. */
1549 realpeer->status = peer->status;
1552 /* peer pointer change */
1557 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1561 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
1562 if (stream_fifo_head (peer->obuf))
1563 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
1565 /* hack: we may defer OPEN on accept peers, when there seems to be a
1566 * realpeer in progress, when an accept peer connection is opened. This
1567 * is to avoid interoperability issues, with test/conformance tools
1568 * particularly. See bgp_fsm.c::bgp_connect_success
1570 * If OPEN was deferred there, then we must send it now.
1573 bgp_open_send (peer);
1576 /* remote router-id check. */
1577 if (remote_id.s_addr == 0
1578 || IPV4_CLASS_DE (ntohl (remote_id.s_addr))
1579 || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
1581 if (BGP_DEBUG (normal, NORMAL))
1582 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1583 peer->host, inet_ntoa (remote_id));
1584 bgp_notify_send_with_data (peer,
1585 BGP_NOTIFY_OPEN_ERR,
1586 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1587 notify_data_remote_id, 4);
1591 /* Set remote router-id */
1592 peer->remote_id = remote_id;
1594 /* Peer BGP version check. */
1595 if (version != BGP_VERSION_4)
1597 u_int16_t maxver = htons(BGP_VERSION_4);
1598 /* XXX this reply may not be correct if version < 4 XXX */
1599 if (BGP_DEBUG (normal, NORMAL))
1600 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
1601 peer->host, version, BGP_VERSION_4);
1602 /* Data must be in network byte order here */
1603 bgp_notify_send_with_data (peer,
1604 BGP_NOTIFY_OPEN_ERR,
1605 BGP_NOTIFY_OPEN_UNSUP_VERSION,
1606 (u_int8_t *) &maxver, 2);
1610 /* Check neighbor as number. */
1611 if (remote_as != peer->as)
1613 if (BGP_DEBUG (normal, NORMAL))
1614 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1615 peer->host, remote_as, peer->as);
1616 bgp_notify_send_with_data (peer,
1617 BGP_NOTIFY_OPEN_ERR,
1618 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1619 notify_data_remote_as, 2);
1623 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1624 calculate the value of the Hold Timer by using the smaller of its
1625 configured Hold Time and the Hold Time received in the OPEN message.
1626 The Hold Time MUST be either zero or at least three seconds. An
1627 implementation may reject connections on the basis of the Hold Time. */
1629 if (holdtime < 3 && holdtime != 0)
1631 bgp_notify_send_with_data (peer,
1632 BGP_NOTIFY_OPEN_ERR,
1633 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
1634 (u_int8_t *)holdtime_ptr, 2);
1638 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1639 would be one third of the Hold Time interval. KEEPALIVE messages
1640 MUST NOT be sent more frequently than one per second. An
1641 implementation MAY adjust the rate at which it sends KEEPALIVE
1642 messages as a function of the Hold Time interval. */
1644 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
1645 send_holdtime = peer->holdtime;
1647 send_holdtime = peer->bgp->default_holdtime;
1649 if (holdtime < send_holdtime)
1650 peer->v_holdtime = holdtime;
1652 peer->v_holdtime = send_holdtime;
1654 peer->v_keepalive = peer->v_holdtime / 3;
1656 /* Open option part parse. */
1659 if ((ret = bgp_open_option_parse (peer, optlen, &mp_capability)) < 0)
1661 bgp_notify_send (peer,
1662 BGP_NOTIFY_OPEN_ERR,
1663 BGP_NOTIFY_OPEN_UNSPECIFIC);
1669 if (BGP_DEBUG (normal, NORMAL))
1670 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
1675 * Assume that the peer supports the locally configured set of
1676 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1677 * capabilities, or if 'override-capability' is configured.
1679 if (! mp_capability ||
1680 CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1682 peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
1683 peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
1684 peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
1685 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
1689 bgp_getsockname (peer);
1690 peer->rtt = sockopt_tcp_rtt (peer->fd);
1692 BGP_EVENT_ADD (peer, Receive_OPEN_message);
1694 peer->packet_size = 0;
1696 stream_reset (peer->ibuf);
1701 /* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers */
1703 bgp_nlri_parse (struct peer *peer, struct attr *attr, struct bgp_nlri *packet)
1705 switch (packet->safi)
1708 case SAFI_MULTICAST:
1709 return bgp_nlri_parse_ip (peer, attr, packet);
1711 case SAFI_MPLS_LABELED_VPN:
1712 return bgp_nlri_parse_vpn (peer, attr, packet);
1714 return bgp_nlri_parse_encap (peer, attr, packet);
1719 /* Parse BGP Update packet and make attribute object. */
1721 bgp_update_receive (struct peer *peer, bgp_size_t size)
1727 struct attr_extra extra;
1728 bgp_size_t attribute_len;
1729 bgp_size_t update_len;
1730 bgp_size_t withdraw_len;
1740 struct bgp_nlri nlris[NLRI_TYPE_MAX];
1742 /* Status must be Established. */
1743 if (peer->status != Established)
1745 zlog_err ("%s [FSM] Update packet received under status %s",
1746 peer->host, LOOKUP (bgp_status_msg, peer->status));
1747 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1751 /* Set initial values. */
1752 memset (&attr, 0, sizeof (struct attr));
1753 memset (&extra, 0, sizeof (struct attr_extra));
1754 memset (&nlris, 0, sizeof nlris);
1756 attr.extra = &extra;
1759 end = stream_pnt (s) + size;
1761 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1762 Length is too large (i.e., if Unfeasible Routes Length + Total
1763 Attribute Length + 23 exceeds the message Length), then the Error
1764 Subcode is set to Malformed Attribute List. */
1765 if (stream_pnt (s) + 2 > end)
1767 zlog_err ("%s [Error] Update packet error"
1768 " (packet length is short for unfeasible length)",
1770 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1771 BGP_NOTIFY_UPDATE_MAL_ATTR);
1775 /* Unfeasible Route Length. */
1776 withdraw_len = stream_getw (s);
1778 /* Unfeasible Route Length check. */
1779 if (stream_pnt (s) + withdraw_len > end)
1781 zlog_err ("%s [Error] Update packet error"
1782 " (packet unfeasible length overflow %d)",
1783 peer->host, withdraw_len);
1784 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1785 BGP_NOTIFY_UPDATE_MAL_ATTR);
1789 /* Unfeasible Route packet format check. */
1790 if (withdraw_len > 0)
1792 nlris[NLRI_WITHDRAW].afi = AFI_IP;
1793 nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST;
1794 nlris[NLRI_WITHDRAW].nlri = stream_pnt (s);
1795 nlris[NLRI_WITHDRAW].length = withdraw_len;
1797 if (BGP_DEBUG (packet, PACKET_RECV))
1798 zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer->host);
1800 stream_forward_getp (s, withdraw_len);
1803 /* Attribute total length check. */
1804 if (stream_pnt (s) + 2 > end)
1806 zlog_warn ("%s [Error] Packet Error"
1807 " (update packet is short for attribute length)",
1809 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1810 BGP_NOTIFY_UPDATE_MAL_ATTR);
1814 /* Fetch attribute total length. */
1815 attribute_len = stream_getw (s);
1817 /* Attribute length check. */
1818 if (stream_pnt (s) + attribute_len > end)
1820 zlog_warn ("%s [Error] Packet Error"
1821 " (update packet attribute length overflow %d)",
1822 peer->host, attribute_len);
1823 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1824 BGP_NOTIFY_UPDATE_MAL_ATTR);
1828 /* Certain attribute parsing errors should not be considered bad enough
1829 * to reset the session for, most particularly any partial/optional
1830 * attributes that have 'tunneled' over speakers that don't understand
1831 * them. Instead we withdraw only the prefix concerned.
1833 * Complicates the flow a little though..
1835 bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
1836 /* This define morphs the update case into a withdraw when lower levels
1837 * have signalled an error condition where this is best.
1839 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1841 /* Parse attribute when it exists. */
1844 attr_parse_ret = bgp_attr_parse (peer, &attr, attribute_len,
1845 &nlris[NLRI_MP_UPDATE], &nlris[NLRI_MP_WITHDRAW]);
1846 if (attr_parse_ret == BGP_ATTR_PARSE_ERROR)
1848 bgp_attr_unintern_sub (&attr);
1849 bgp_attr_flush (&attr);
1854 /* Logging the attribute. */
1855 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
1856 || BGP_DEBUG (update, UPDATE_IN))
1858 char attrstr[BUFSIZ];
1861 ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
1862 int lvl = (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1863 ? LOG_ERR : LOG_DEBUG;
1865 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1866 zlog (peer->log, LOG_ERR,
1867 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1871 zlog (peer->log, lvl, "%s rcvd UPDATE w/ attr: %s",
1872 peer->host, attrstr);
1875 /* Network Layer Reachability Information. */
1876 update_len = end - stream_pnt (s);
1880 /* Set NLRI portion to structure. */
1881 nlris[NLRI_UPDATE].afi = AFI_IP;
1882 nlris[NLRI_UPDATE].safi = SAFI_UNICAST;
1883 nlris[NLRI_UPDATE].nlri = stream_pnt (s);
1884 nlris[NLRI_UPDATE].length = update_len;
1886 stream_forward_getp (s, update_len);
1889 /* Parse any given NLRIs */
1890 for (i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++)
1892 if (!nlris[i].nlri) continue;
1894 /* We use afi and safi as indices into tables and what not. It would
1895 * be impossible, at this time, to support unknown afi/safis. And
1896 * anyway, the peer needs to be configured to enable the afi/safi
1897 * explicitly which requires UI support.
1899 * Ignore unknown afi/safi NLRIs.
1901 * Note: this means nlri[x].afi/safi still can not be trusted for
1902 * indexing later in this function!
1904 * Note2: This will also remap the wire code-point for VPN safi to the
1905 * internal safi_t point, as needs be.
1907 if (!bgp_afi_safi_valid_indices (nlris[i].afi, &nlris[i].safi))
1909 plog_info (peer->log,
1910 "%s [Info] UPDATE with unsupported AFI/SAFI %u/%u",
1911 peer->host, nlris[i].afi, nlris[i].safi);
1915 /* NLRI is processed only when the peer is configured specific
1916 Address Family and Subsequent Address Family. */
1917 if (!peer->afc[nlris[i].afi][nlris[i].safi])
1919 plog_info (peer->log,
1920 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1921 peer->host, nlris[i].afi, nlris[i].safi);
1925 /* EoR handled later */
1926 if (nlris[i].length == 0)
1932 case NLRI_MP_UPDATE:
1933 nlri_ret = bgp_nlri_parse (peer, NLRI_ATTR_ARG, &nlris[i]);
1936 case NLRI_MP_WITHDRAW:
1937 nlri_ret = bgp_nlri_parse (peer, NULL, &nlris[i]);
1942 plog_err (peer->log,
1943 "%s [Error] Error parsing NLRI", peer->host);
1944 if (peer->status == Established)
1945 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1947 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1948 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR);
1949 bgp_attr_unintern_sub (&attr);
1956 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1957 * and MP EoR should have only an empty MP_UNREACH
1959 if (!update_len && !withdraw_len
1960 && nlris[NLRI_MP_UPDATE].length == 0)
1965 /* Non-MP IPv4/Unicast is a completely empty UPDATE - already
1966 * checked update and withdraw NLRI lengths are 0.
1971 safi = SAFI_UNICAST;
1973 /* otherwise MP AFI/SAFI is an empty update, other than an empty
1974 * MP_UNREACH_NLRI attr (with an AFI/SAFI we recognise).
1976 else if (attr.flag == BGP_ATTR_MP_UNREACH_NLRI
1977 && nlris[NLRI_MP_WITHDRAW].length == 0
1978 && bgp_afi_safi_valid_indices (nlris[NLRI_MP_WITHDRAW].afi,
1979 &nlris[NLRI_MP_WITHDRAW].safi))
1981 afi = nlris[NLRI_MP_WITHDRAW].afi;
1982 safi = nlris[NLRI_MP_WITHDRAW].safi;
1985 if (afi && peer->afc[afi][safi])
1987 /* End-of-RIB received */
1988 SET_FLAG (peer->af_sflags[afi][safi],
1989 PEER_STATUS_EOR_RECEIVED);
1991 /* NSF delete stale route */
1992 if (peer->nsf[afi][safi])
1993 bgp_clear_stale_route (peer, afi, safi);
1995 if (BGP_DEBUG (normal, NORMAL))
1996 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for %s from %s",
1997 peer->host, afi_safi_print (afi, safi));
2001 /* Everything is done. We unintern temporary structures which
2002 interned in bgp_attr_parse(). */
2003 bgp_attr_unintern_sub (&attr);
2004 bgp_attr_flush (&attr);
2006 /* If peering is stopped due to some reason, do not generate BGP
2008 if (peer->status != Established)
2011 /* Increment packet counter. */
2013 peer->update_time = bgp_clock ();
2015 /* Rearm holdtime timer */
2016 BGP_TIMER_OFF (peer->t_holdtime);
2017 bgp_timer_set (peer);
2022 /* Notify message treatment function. */
2024 bgp_notify_receive (struct peer *peer, bgp_size_t size)
2026 struct bgp_notify bgp_notify;
2028 if (peer->notify.data)
2030 XFREE (MTYPE_TMP, peer->notify.data);
2031 peer->notify.data = NULL;
2032 peer->notify.length = 0;
2035 bgp_notify.code = stream_getc (peer->ibuf);
2036 bgp_notify.subcode = stream_getc (peer->ibuf);
2037 bgp_notify.length = size - 2;
2038 bgp_notify.data = NULL;
2040 /* Preserv notify code and sub code. */
2041 peer->notify.code = bgp_notify.code;
2042 peer->notify.subcode = bgp_notify.subcode;
2043 /* For further diagnostic record returned Data. */
2044 if (bgp_notify.length)
2046 peer->notify.length = size - 2;
2047 peer->notify.data = XMALLOC (MTYPE_TMP, size - 2);
2048 memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2);
2057 if (bgp_notify.length)
2059 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
2060 for (i = 0; i < bgp_notify.length; i++)
2063 sprintf (c, " %02x", stream_getc (peer->ibuf));
2064 strcat (bgp_notify.data, c);
2069 sprintf (c, "%02x", stream_getc (peer->ibuf));
2070 strcpy (bgp_notify.data, c);
2074 bgp_notify_print(peer, &bgp_notify, "received");
2075 if (bgp_notify.data)
2077 XFREE (MTYPE_TMP, bgp_notify.data);
2078 bgp_notify.data = NULL;
2079 bgp_notify.length = 0;
2083 /* peer count update */
2086 if (peer->status == Established)
2087 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
2089 /* We have to check for Notify with Unsupported Optional Parameter.
2090 in that case we fallback to open without the capability option.
2091 But this done in bgp_stop. We just mark it here to avoid changing
2093 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
2094 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM )
2095 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
2097 BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message);
2100 /* Keepalive treatment function -- get keepalive send keepalive */
2102 bgp_keepalive_receive (struct peer *peer, bgp_size_t size)
2104 if (BGP_DEBUG (keepalive, KEEPALIVE))
2105 zlog_debug ("%s KEEPALIVE rcvd", peer->host);
2107 BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message);
2110 /* Route refresh message is received. */
2112 bgp_route_refresh_receive (struct peer *peer, bgp_size_t size)
2118 /* If peer does not have the capability, send notification. */
2119 if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV))
2121 plog_err (peer->log, "%s [Error] BGP route refresh is not enabled",
2123 bgp_notify_send (peer,
2124 BGP_NOTIFY_HEADER_ERR,
2125 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2129 /* Status must be Established. */
2130 if (peer->status != Established)
2132 plog_err (peer->log,
2133 "%s [Error] Route refresh packet received under status %s",
2134 peer->host, LOOKUP (bgp_status_msg, peer->status));
2135 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
2142 afi = stream_getw (s);
2145 safi = stream_getc (s);
2147 if (BGP_DEBUG (normal, NORMAL))
2148 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
2149 peer->host, afi, safi);
2151 /* Check AFI and SAFI. */
2152 if ((afi != AFI_IP && afi != AFI_IP6)
2153 || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
2154 && safi != SAFI_MPLS_LABELED_VPN))
2156 if (BGP_DEBUG (normal, NORMAL))
2158 zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
2159 peer->host, afi, safi);
2164 /* Adjust safi code. */
2165 if (safi == SAFI_MPLS_LABELED_VPN)
2166 safi = SAFI_MPLS_VPN;
2168 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
2171 u_char when_to_refresh;
2175 if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5)
2177 zlog_info ("%s ORF route refresh length error", peer->host);
2178 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2182 when_to_refresh = stream_getc (s);
2183 end = stream_pnt (s) + (size - 5);
2185 while ((stream_pnt (s) + 2) < end)
2187 orf_type = stream_getc (s);
2188 orf_len = stream_getw (s);
2190 /* orf_len in bounds? */
2191 if ((stream_pnt (s) + orf_len) > end)
2192 break; /* XXX: Notify instead?? */
2193 if (orf_type == ORF_TYPE_PREFIX
2194 || orf_type == ORF_TYPE_PREFIX_OLD)
2196 uint8_t *p_pnt = stream_pnt (s);
2197 uint8_t *p_end = stream_pnt (s) + orf_len;
2198 struct orf_prefix orfp;
2205 if (BGP_DEBUG (normal, NORMAL))
2207 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
2208 peer->host, orf_type, orf_len);
2211 /* we're going to read at least 1 byte of common ORF header,
2212 * and 7 bytes of ORF Address-filter entry from the stream
2217 /* ORF prefix-list name */
2218 sprintf (name, "%s.%d.%d", peer->host, afi, safi);
2220 while (p_pnt < p_end)
2222 /* If the ORF entry is malformed, want to read as much of it
2223 * as possible without going beyond the bounds of the entry,
2224 * to maximise debug information.
2227 memset (&orfp, 0, sizeof (struct orf_prefix));
2229 /* after ++: p_pnt <= p_end */
2230 if (common & ORF_COMMON_PART_REMOVE_ALL)
2232 if (BGP_DEBUG (normal, NORMAL))
2233 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host);
2234 prefix_bgp_orf_remove_all (afi, name);
2237 ok = ((size_t)(p_end - p_pnt) >= sizeof(u_int32_t)) ;
2240 memcpy (&seq, p_pnt, sizeof (u_int32_t));
2241 p_pnt += sizeof (u_int32_t);
2242 orfp.seq = ntohl (seq);
2247 if ((ok = (p_pnt < p_end)))
2248 orfp.ge = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */
2249 if ((ok = (p_pnt < p_end)))
2250 orfp.le = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */
2251 if ((ok = (p_pnt < p_end)))
2252 orfp.p.prefixlen = *p_pnt++ ;
2253 orfp.p.family = afi2family (afi); /* afi checked already */
2255 psize = PSIZE (orfp.p.prefixlen); /* 0 if not ok */
2256 if (psize > prefix_blen(&orfp.p)) /* valid for family ? */
2259 psize = prefix_blen(&orfp.p) ;
2261 if (psize > (p_end - p_pnt)) /* valid for packet ? */
2264 psize = p_end - p_pnt ;
2268 memcpy (&orfp.p.u.prefix, p_pnt, psize);
2271 if (BGP_DEBUG (normal, NORMAL))
2273 char buf[INET6_BUFSIZ];
2275 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
2277 (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
2278 (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
2280 inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, INET6_BUFSIZ),
2281 orfp.p.prefixlen, orfp.ge, orfp.le,
2282 ok ? "" : " MALFORMED");
2286 ret = prefix_bgp_orf_set (name, afi, &orfp,
2287 (common & ORF_COMMON_PART_DENY ? 0 : 1 ),
2288 (common & ORF_COMMON_PART_REMOVE ? 0 : 1));
2290 if (!ok || (ok && ret != CMD_SUCCESS))
2292 if (BGP_DEBUG (normal, NORMAL))
2293 zlog_debug ("%s Received misformatted prefixlist ORF."
2294 " Remove All pfxlist", peer->host);
2295 prefix_bgp_orf_remove_all (afi, name);
2299 peer->orf_plist[afi][safi] =
2300 prefix_bgp_orf_lookup (afi, name);
2302 stream_forward_getp (s, orf_len);
2304 if (BGP_DEBUG (normal, NORMAL))
2305 zlog_debug ("%s rcvd Refresh %s ORF request", peer->host,
2306 when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate");
2307 if (when_to_refresh == REFRESH_DEFER)
2311 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2312 if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
2313 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
2315 /* Perform route refreshment to the peer */
2316 bgp_announce_route (peer, afi, safi);
2320 bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length)
2323 struct capability_mp_data mpc;
2324 struct capability_header *hdr;
2333 /* We need at least action, capability code and capability length. */
2336 zlog_info ("%s Capability length error", peer->host);
2337 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2341 hdr = (struct capability_header *)(pnt + 1);
2343 /* Action value check. */
2344 if (action != CAPABILITY_ACTION_SET
2345 && action != CAPABILITY_ACTION_UNSET)
2347 zlog_info ("%s Capability Action Value error %d",
2348 peer->host, action);
2349 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2353 if (BGP_DEBUG (normal, NORMAL))
2354 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
2355 peer->host, action, hdr->code, hdr->length);
2357 /* Capability length check. */
2358 if ((pnt + hdr->length + 3) > end)
2360 zlog_info ("%s Capability length error", peer->host);
2361 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2365 /* Fetch structure to the byte stream. */
2366 memcpy (&mpc, pnt + 3, sizeof (struct capability_mp_data));
2368 /* We know MP Capability Code. */
2369 if (hdr->code == CAPABILITY_CODE_MP)
2371 afi = ntohs (mpc.afi);
2374 /* Ignore capability when override-capability is set. */
2375 if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
2378 if (!bgp_afi_safi_valid_indices (afi, &safi))
2380 if (BGP_DEBUG (normal, NORMAL))
2381 zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
2382 "(%u/%u)", peer->host, afi, safi);
2386 /* Address family check. */
2387 if (BGP_DEBUG (normal, NORMAL))
2388 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2390 action == CAPABILITY_ACTION_SET
2391 ? "Advertising" : "Removing",
2392 ntohs(mpc.afi) , mpc.safi);
2394 if (action == CAPABILITY_ACTION_SET)
2396 peer->afc_recv[afi][safi] = 1;
2397 if (peer->afc[afi][safi])
2399 peer->afc_nego[afi][safi] = 1;
2400 bgp_announce_route (peer, afi, safi);
2405 peer->afc_recv[afi][safi] = 0;
2406 peer->afc_nego[afi][safi] = 0;
2408 if (peer_active_nego (peer))
2409 bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_NORMAL);
2411 BGP_EVENT_ADD (peer, BGP_Stop);
2416 zlog_warn ("%s unrecognized capability code: %d - ignored",
2417 peer->host, hdr->code);
2419 pnt += hdr->length + 3;
2424 /* Dynamic Capability is received.
2426 * This is exported for unit-test purposes
2429 bgp_capability_receive (struct peer *peer, bgp_size_t size)
2433 /* Fetch pointer. */
2434 pnt = stream_pnt (peer->ibuf);
2436 if (BGP_DEBUG (normal, NORMAL))
2437 zlog_debug ("%s rcv CAPABILITY", peer->host);
2439 /* If peer does not have the capability, send notification. */
2440 if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV))
2442 plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled",
2444 bgp_notify_send (peer,
2445 BGP_NOTIFY_HEADER_ERR,
2446 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2450 /* Status must be Established. */
2451 if (peer->status != Established)
2453 plog_err (peer->log,
2454 "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status));
2455 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
2460 return bgp_capability_msg_parse (peer, pnt, size);
2463 /* BGP read utility function. */
2465 bgp_read_packet (struct peer *peer)
2470 readsize = peer->packet_size - stream_get_endp (peer->ibuf);
2472 /* If size is zero then return. */
2476 /* Read packet from fd. */
2477 nbytes = stream_read_try (peer->ibuf, peer->fd, readsize);
2479 /* If read byte is smaller than zero then error occured. */
2482 /* Transient error should retry */
2486 plog_err (peer->log, "%s [Error] bgp_read_packet error: %s",
2487 peer->host, safe_strerror (errno));
2489 if (peer->status == Established)
2491 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
2493 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2494 SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
2497 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2500 BGP_EVENT_ADD (peer, TCP_fatal_error);
2504 /* When read byte is zero : clear bgp peer and return */
2507 if (BGP_DEBUG (events, EVENTS))
2508 plog_debug (peer->log, "%s [Event] BGP connection closed fd %d",
2509 peer->host, peer->fd);
2511 if (peer->status == Established)
2513 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
2515 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2516 SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
2519 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2522 BGP_EVENT_ADD (peer, TCP_connection_closed);
2526 /* We read partial packet. */
2527 if (stream_get_endp (peer->ibuf) != peer->packet_size)
2535 bgp_marker_all_one (struct stream *s, int length)
2539 for (i = 0; i < length; i++)
2540 if (s->data[i] != 0xff)
2546 /* Recent thread time.
2547 On same clock base as bgp_clock (MONOTONIC)
2548 but can be time of last context switch to bgp_read thread. */
2550 bgp_recent_clock (void)
2552 return recent_relative_time().tv_sec;
2555 /* Starting point of packet process function. */
2557 bgp_read (struct thread *thread)
2563 char notify_data_length[2];
2565 /* Yes first of all get peer pointer. */
2566 peer = THREAD_ARG (thread);
2567 peer->t_read = NULL;
2569 /* For non-blocking IO check. */
2570 if (peer->status == Connect)
2572 bgp_connect_check (peer);
2579 zlog_err ("bgp_read peer's fd is negative value %d", peer->fd);
2582 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
2585 /* Read packet header to determine type of the packet */
2586 if (peer->packet_size == 0)
2587 peer->packet_size = BGP_HEADER_SIZE;
2589 if (stream_get_endp (peer->ibuf) < BGP_HEADER_SIZE)
2591 ret = bgp_read_packet (peer);
2593 /* Header read error or partial read packet. */
2597 /* Get size and type. */
2598 stream_forward_getp (peer->ibuf, BGP_MARKER_SIZE);
2599 memcpy (notify_data_length, stream_pnt (peer->ibuf), 2);
2600 size = stream_getw (peer->ibuf);
2601 type = stream_getc (peer->ibuf);
2603 if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0)
2604 zlog_debug ("%s rcv message type %d, length (excl. header) %d",
2605 peer->host, type, size - BGP_HEADER_SIZE);
2608 if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE))
2609 && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE))
2611 bgp_notify_send (peer,
2612 BGP_NOTIFY_HEADER_ERR,
2613 BGP_NOTIFY_HEADER_NOT_SYNC);
2617 /* BGP type check. */
2618 if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
2619 && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
2620 && type != BGP_MSG_ROUTE_REFRESH_NEW
2621 && type != BGP_MSG_ROUTE_REFRESH_OLD
2622 && type != BGP_MSG_CAPABILITY)
2624 if (BGP_DEBUG (normal, NORMAL))
2625 plog_debug (peer->log,
2626 "%s unknown message type 0x%02x",
2628 bgp_notify_send_with_data (peer,
2629 BGP_NOTIFY_HEADER_ERR,
2630 BGP_NOTIFY_HEADER_BAD_MESTYPE,
2634 /* Mimimum packet length check. */
2635 if ((size < BGP_HEADER_SIZE)
2636 || (size > BGP_MAX_PACKET_SIZE)
2637 || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
2638 || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE)
2639 || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE)
2640 || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
2641 || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2642 || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2643 || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE))
2645 if (BGP_DEBUG (normal, NORMAL))
2646 plog_debug (peer->log,
2647 "%s bad message length - %d for %s",
2649 type == 128 ? "ROUTE-REFRESH" :
2650 bgp_type_str[(int) type]);
2651 bgp_notify_send_with_data (peer,
2652 BGP_NOTIFY_HEADER_ERR,
2653 BGP_NOTIFY_HEADER_BAD_MESLEN,
2654 (u_char *) notify_data_length, 2);
2658 /* Adjust size to message length. */
2659 peer->packet_size = size;
2662 ret = bgp_read_packet (peer);
2666 /* Get size and type again. */
2667 size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE);
2668 type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2);
2670 /* BGP packet dump function. */
2671 bgp_dump_packet (peer, type, peer->ibuf);
2673 size = (peer->packet_size - BGP_HEADER_SIZE);
2675 /* Read rest of the packet and call each sort of packet routine */
2680 bgp_open_receive (peer, size); /* XXX return value ignored! */
2682 case BGP_MSG_UPDATE:
2683 peer->readtime = bgp_recent_clock ();
2684 bgp_update_receive (peer, size);
2686 case BGP_MSG_NOTIFY:
2687 bgp_notify_receive (peer, size);
2689 case BGP_MSG_KEEPALIVE:
2690 peer->readtime = bgp_recent_clock ();
2691 bgp_keepalive_receive (peer, size);
2693 case BGP_MSG_ROUTE_REFRESH_NEW:
2694 case BGP_MSG_ROUTE_REFRESH_OLD:
2696 bgp_route_refresh_receive (peer, size);
2698 case BGP_MSG_CAPABILITY:
2699 peer->dynamic_cap_in++;
2700 bgp_capability_receive (peer, size);
2704 /* Clear input buffer. */
2705 peer->packet_size = 0;
2707 stream_reset (peer->ibuf);
2710 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
2712 if (BGP_DEBUG (events, EVENTS))
2713 zlog_debug ("%s [Event] Accepting BGP peer delete", peer->host);