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 stream
*snlri
;
146 struct bgp_adj_out
*adj
;
147 struct bgp_advertise
*adv
;
148 struct stream
*packet
;
149 struct bgp_node
*rn
= NULL
;
150 struct bgp_info
*binfo
= NULL
;
151 bgp_size_t total_attr_len
= 0;
152 unsigned long attrlen_pos
= 0;
153 size_t mpattrlen_pos
= 0;
154 size_t mpattr_pos
= 0;
158 snlri
= peer
->scratch
;
159 stream_reset (snlri
);
161 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
);
171 /* When remaining space can't include NLRI and it's length. */
172 if (STREAM_CONCAT_REMAIN (s
, snlri
, STREAM_SIZE(s
)) <=
173 (BGP_NLRI_LENGTH
+ PSIZE (rn
->p
.prefixlen
)))
176 /* If packet is empty, set attribute. */
177 if (stream_empty (s
))
179 struct peer
*from
= NULL
;
184 /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
185 * one byte message type.
187 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
189 /* 2: withdrawn routes length */
192 /* 3: total attributes length - attrlen_pos stores the position */
193 attrlen_pos
= stream_get_endp (s
);
196 /* 4: if there is MP_REACH_NLRI attribute, that should be the first
197 * attribute, according to draft-ietf-idr-error-handling. Save the
200 mpattr_pos
= stream_get_endp(s
);
202 /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
203 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
,
209 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
210 stream_put_prefix (s
, &rn
->p
);
213 /* Encode the prefix in MP_REACH_NLRI attribute */
214 struct prefix_rd
*prd
= NULL
;
218 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
219 if (binfo
&& binfo
->extra
)
220 tag
= binfo
->extra
->tag
;
222 if (stream_empty(snlri
))
223 mpattrlen_pos
= bgp_packet_mpattr_start(snlri
, afi
, safi
,
225 bgp_packet_mpattr_prefix(snlri
, afi
, safi
, &rn
->p
, prd
, tag
);
227 if (BGP_DEBUG (update
, UPDATE_OUT
))
229 char buf
[INET6_BUFSIZ
];
231 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d",
233 inet_ntop (rn
->p
.family
, &(rn
->p
.u
.prefix
), buf
, INET6_BUFSIZ
),
237 /* Synchnorize attribute. */
239 bgp_attr_unintern (&adj
->attr
);
241 peer
->scount
[afi
][safi
]++;
243 adj
->attr
= bgp_attr_intern (adv
->baa
->attr
);
245 adv
= bgp_advertise_clean (peer
, adj
, afi
, safi
);
248 if (! stream_empty (s
))
250 if (!stream_empty(snlri
))
252 bgp_packet_mpattr_end(snlri
, mpattrlen_pos
);
253 total_attr_len
+= stream_get_endp(snlri
);
256 /* set the total attribute length correctly */
257 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
259 if (!stream_empty(snlri
))
260 packet
= stream_dupcat(s
, snlri
, mpattr_pos
);
262 packet
= stream_dup (s
);
263 bgp_packet_set_size (packet
);
264 bgp_packet_add (peer
, packet
);
265 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
267 stream_reset (snlri
);
273 static struct stream
*
274 bgp_update_packet_eor (struct peer
*peer
, afi_t afi
, safi_t safi
)
277 struct stream
*packet
;
279 if (DISABLE_BGP_ANNOUNCE
)
282 if (BGP_DEBUG (normal
, NORMAL
))
283 zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi
, safi
), peer
->host
);
285 s
= stream_new (BGP_MAX_PACKET_SIZE
);
287 /* Make BGP update packet. */
288 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
290 /* Unfeasible Routes Length */
293 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
295 /* Total Path Attribute Length */
300 /* Total Path Attribute Length */
302 stream_putc (s
, BGP_ATTR_FLAG_OPTIONAL
);
303 stream_putc (s
, BGP_ATTR_MP_UNREACH_NLRI
);
305 stream_putw (s
, afi
);
306 stream_putc (s
, safi
);
309 bgp_packet_set_size (s
);
310 packet
= stream_dup (s
);
311 bgp_packet_add (peer
, packet
);
316 /* Make BGP withdraw packet. */
318 16-octet marker | 2-octet length | 1-octet type |
319 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
321 /* For other afi/safis:
322 16-octet marker | 2-octet length | 1-octet type |
323 2-octet withdrawn route length (=0) | 2-octet attrlen |
324 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
326 static struct stream
*
327 bgp_withdraw_packet (struct peer
*peer
, afi_t afi
, safi_t safi
)
330 struct stream
*packet
;
331 struct bgp_adj_out
*adj
;
332 struct bgp_advertise
*adv
;
335 bgp_size_t unfeasible_len
;
336 bgp_size_t total_attr_len
;
338 size_t attrlen_pos
= 0;
339 size_t mplen_pos
= 0;
340 u_char first_time
= 1;
345 while ((adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
)) != NULL
)
351 if (STREAM_REMAIN (s
)
352 < (BGP_NLRI_LENGTH
+ BGP_TOTAL_ATTR_LEN
+ PSIZE (rn
->p
.prefixlen
)))
355 if (stream_empty (s
))
357 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
358 stream_putw (s
, 0); /* unfeasible routes length */
363 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
364 stream_put_prefix (s
, &rn
->p
);
367 struct prefix_rd
*prd
= NULL
;
370 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
372 /* If first time, format the MP_UNREACH header */
375 attrlen_pos
= stream_get_endp (s
);
376 /* total attr length = 0 for now. reevaluate later */
378 mp_start
= stream_get_endp (s
);
379 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
, safi
);
382 bgp_packet_mpunreach_prefix(s
, &rn
->p
, afi
, safi
, prd
, NULL
);
385 if (BGP_DEBUG (update
, UPDATE_OUT
))
387 char buf
[INET6_BUFSIZ
];
389 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d -- unreachable",
391 inet_ntop (rn
->p
.family
, &(rn
->p
.u
.prefix
), buf
, INET6_BUFSIZ
),
395 peer
->scount
[afi
][safi
]--;
397 bgp_adj_out_remove (rn
, adj
, peer
, afi
, safi
);
398 bgp_unlock_node (rn
);
401 if (! stream_empty (s
))
403 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
406 = stream_get_endp (s
) - BGP_HEADER_SIZE
- BGP_UNFEASIBLE_LEN
;
407 stream_putw_at (s
, BGP_HEADER_SIZE
, unfeasible_len
);
412 /* Set the mp_unreach attr's length */
413 bgp_packet_mpunreach_end(s
, mplen_pos
);
415 /* Set total path attribute length. */
416 total_attr_len
= stream_get_endp(s
) - mp_start
;
417 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
419 bgp_packet_set_size (s
);
420 packet
= stream_dup (s
);
421 bgp_packet_add (peer
, packet
);
430 bgp_default_update_send (struct peer
*peer
, struct attr
*attr
,
431 afi_t afi
, safi_t safi
, struct peer
*from
)
434 struct stream
*packet
;
437 bgp_size_t total_attr_len
;
439 if (DISABLE_BGP_ANNOUNCE
)
443 str2prefix ("0.0.0.0/0", &p
);
446 str2prefix ("::/0", &p
);
447 #endif /* HAVE_IPV6 */
449 /* Logging the attribute. */
450 if (BGP_DEBUG (update
, UPDATE_OUT
))
452 char attrstr
[BUFSIZ
];
453 char buf
[INET6_BUFSIZ
];
456 bgp_dump_attr (peer
, attr
, attrstr
, BUFSIZ
);
457 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d %s",
458 peer
->host
, inet_ntop(p
.family
, &(p
.u
.prefix
), buf
, INET6_BUFSIZ
),
459 p
.prefixlen
, attrstr
);
462 s
= stream_new (BGP_MAX_PACKET_SIZE
);
464 /* Make BGP update packet. */
465 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
467 /* Unfeasible Routes Length. */
470 /* Make place for total attribute length. */
471 pos
= stream_get_endp (s
);
473 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
, attr
, &p
, afi
, safi
, from
, NULL
, NULL
);
475 /* Set Total Path Attribute Length. */
476 stream_putw_at (s
, pos
, total_attr_len
);
479 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
)
480 stream_put_prefix (s
, &p
);
483 bgp_packet_set_size (s
);
485 packet
= stream_dup (s
);
488 /* Dump packet if debug option is set. */
490 /* bgp_packet_dump (packet); */
493 /* Add packet to the peer. */
494 bgp_packet_add (peer
, packet
);
496 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
500 bgp_default_withdraw_send (struct peer
*peer
, afi_t afi
, safi_t safi
)
503 struct stream
*packet
;
505 unsigned long attrlen_pos
= 0;
507 bgp_size_t unfeasible_len
;
508 bgp_size_t total_attr_len
;
510 size_t mplen_pos
= 0;
512 if (DISABLE_BGP_ANNOUNCE
)
516 str2prefix ("0.0.0.0/0", &p
);
519 str2prefix ("::/0", &p
);
520 #endif /* HAVE_IPV6 */
524 if (BGP_DEBUG (update
, UPDATE_OUT
))
526 char buf
[INET6_BUFSIZ
];
528 zlog (peer
->log
, LOG_DEBUG
, "%s send UPDATE %s/%d -- unreachable",
529 peer
->host
, inet_ntop(p
.family
, &(p
.u
.prefix
), buf
, INET6_BUFSIZ
),
533 s
= stream_new (BGP_MAX_PACKET_SIZE
);
535 /* Make BGP update packet. */
536 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
538 /* Unfeasible Routes Length. */;
539 cp
= stream_get_endp (s
);
542 /* Withdrawn Routes. */
543 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
)
545 stream_put_prefix (s
, &p
);
547 unfeasible_len
= stream_get_endp (s
) - cp
- 2;
549 /* Set unfeasible len. */
550 stream_putw_at (s
, cp
, unfeasible_len
);
552 /* Set total path attribute length. */
557 attrlen_pos
= stream_get_endp (s
);
559 mp_start
= stream_get_endp (s
);
560 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
, safi
);
561 bgp_packet_mpunreach_prefix(s
, &p
, afi
, safi
, NULL
, NULL
);
563 /* Set the mp_unreach attr's length */
564 bgp_packet_mpunreach_end(s
, mplen_pos
);
566 /* Set total path attribute length. */
567 total_attr_len
= stream_get_endp(s
) - mp_start
;
568 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
571 bgp_packet_set_size (s
);
573 packet
= stream_dup (s
);
576 /* Add packet to the peer. */
577 bgp_packet_add (peer
, packet
);
579 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
582 /* Get next packet to be written. */
583 static struct stream
*
584 bgp_write_packet (struct peer
*peer
)
588 struct stream
*s
= NULL
;
589 struct bgp_advertise
*adv
;
591 s
= stream_fifo_head (peer
->obuf
);
595 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
596 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
598 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
);
601 s
= bgp_withdraw_packet (peer
, afi
, safi
);
607 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
608 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
610 adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
);
613 if (adv
->binfo
&& adv
->binfo
->uptime
< peer
->synctime
)
615 if (CHECK_FLAG (adv
->binfo
->peer
->cap
, PEER_CAP_RESTART_RCV
)
616 && CHECK_FLAG (adv
->binfo
->peer
->cap
, PEER_CAP_RESTART_ADV
)
617 && ! CHECK_FLAG (adv
->binfo
->flags
, BGP_INFO_STALE
)
618 && safi
!= SAFI_MPLS_VPN
)
620 if (CHECK_FLAG (adv
->binfo
->peer
->af_sflags
[afi
][safi
],
621 PEER_STATUS_EOR_RECEIVED
))
622 s
= bgp_update_packet (peer
, afi
, safi
);
625 s
= bgp_update_packet (peer
, afi
, safi
);
632 if (CHECK_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
))
634 if (peer
->afc_nego
[afi
][safi
] && peer
->synctime
635 && ! CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_SEND
)
636 && safi
!= SAFI_MPLS_VPN
)
638 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_EOR_SEND
);
639 return bgp_update_packet_eor (peer
, afi
, safi
);
647 /* Is there partially written packet or updates we can send right
650 bgp_write_proceed (struct peer
*peer
)
654 struct bgp_advertise
*adv
;
656 if (stream_fifo_head (peer
->obuf
))
659 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
660 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
661 if (FIFO_HEAD (&peer
->sync
[afi
][safi
]->withdraw
))
664 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
665 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
666 if ((adv
= FIFO_HEAD (&peer
->sync
[afi
][safi
]->update
)) != NULL
)
667 if (adv
->binfo
->uptime
< peer
->synctime
)
673 /* Write packet to the peer. */
675 bgp_write (struct thread
*thread
)
681 unsigned int count
= 0;
683 /* Yes first of all get peer pointer. */
684 peer
= THREAD_ARG (thread
);
685 peer
->t_write
= NULL
;
687 /* For non-blocking IO check. */
688 if (peer
->status
== Connect
)
690 bgp_connect_check (peer
);
694 s
= bgp_write_packet (peer
);
696 return 0; /* nothing to send */
698 sockopt_cork (peer
->fd
, 1);
700 /* Nonblocking write until TCP output buffer is full. */
705 /* Number of bytes to be sent. */
706 writenum
= stream_get_endp (s
) - stream_get_getp (s
);
708 /* Call write() system call. */
709 num
= write (peer
->fd
, STREAM_PNT (s
), writenum
);
712 /* write failed either retry needed or error */
713 if (ERRNO_IO_RETRY(errno
))
716 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
723 stream_forward_getp (s
, num
);
727 /* Retrieve BGP packet type. */
728 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
729 type
= stream_getc (s
);
741 /* Double start timer. */
744 /* Overflow check. */
745 if (peer
->v_start
>= (60 * 2))
746 peer
->v_start
= (60 * 2);
748 /* Flush any existing events */
749 BGP_EVENT_ADD (peer
, BGP_Stop
);
752 case BGP_MSG_KEEPALIVE
:
753 peer
->keepalive_out
++;
755 case BGP_MSG_ROUTE_REFRESH_NEW
:
756 case BGP_MSG_ROUTE_REFRESH_OLD
:
759 case BGP_MSG_CAPABILITY
:
760 peer
->dynamic_cap_out
++;
764 /* OK we send packet so delete it. */
765 bgp_packet_delete (peer
);
767 while (++count
< BGP_WRITE_PACKET_MAX
&&
768 (s
= bgp_write_packet (peer
)) != NULL
);
770 if (bgp_write_proceed (peer
))
771 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
774 sockopt_cork (peer
->fd
, 0);
778 /* This is only for sending NOTIFICATION message to neighbor. */
780 bgp_write_notify (struct peer
*peer
)
786 /* There should be at least one packet. */
787 s
= stream_fifo_head (peer
->obuf
);
790 assert (stream_get_endp (s
) >= BGP_HEADER_SIZE
);
792 /* Stop collecting data within the socket */
793 sockopt_cork (peer
->fd
, 0);
795 /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
796 * we only care about getting a clean shutdown at this point. */
797 ret
= write (peer
->fd
, STREAM_DATA (s
), stream_get_endp (s
));
799 /* only connection reset/close gets counted as TCP_fatal_error, failure
800 * to write the entire NOTIFY doesn't get different FSM treatment */
803 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
807 /* Disable Nagle, make NOTIFY packet go out right away */
809 (void) setsockopt (peer
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
810 (char *) &val
, sizeof (val
));
812 /* Retrieve BGP packet type. */
813 stream_set_getp (s
, BGP_MARKER_SIZE
+ 2);
814 type
= stream_getc (s
);
816 assert (type
== BGP_MSG_NOTIFY
);
818 /* Type should be notify. */
821 /* Double start timer. */
824 /* Overflow check. */
825 if (peer
->v_start
>= (60 * 2))
826 peer
->v_start
= (60 * 2);
828 BGP_EVENT_ADD (peer
, BGP_Stop
);
833 /* Make keepalive packet and send it to the peer. */
835 bgp_keepalive_send (struct peer
*peer
)
840 s
= stream_new (BGP_MAX_PACKET_SIZE
);
842 /* Make keepalive packet. */
843 bgp_packet_set_marker (s
, BGP_MSG_KEEPALIVE
);
845 /* Set packet size. */
846 length
= bgp_packet_set_size (s
);
848 /* Dump packet if debug option is set. */
849 /* bgp_packet_dump (s); */
851 if (BGP_DEBUG (keepalive
, KEEPALIVE
))
852 zlog_debug ("%s sending KEEPALIVE", peer
->host
);
853 if (BGP_DEBUG (normal
, NORMAL
))
854 zlog_debug ("%s send message type %d, length (incl. header) %d",
855 peer
->host
, BGP_MSG_KEEPALIVE
, length
);
857 /* Add packet to the peer. */
858 bgp_packet_add (peer
, s
);
860 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
863 /* Make open packet and send it to the peer. */
865 bgp_open_send (struct peer
*peer
)
869 u_int16_t send_holdtime
;
872 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
873 send_holdtime
= peer
->holdtime
;
875 send_holdtime
= peer
->bgp
->default_holdtime
;
877 /* local-as Change */
878 if (peer
->change_local_as
)
879 local_as
= peer
->change_local_as
;
881 local_as
= peer
->local_as
;
883 s
= stream_new (BGP_MAX_PACKET_SIZE
);
885 /* Make open packet. */
886 bgp_packet_set_marker (s
, BGP_MSG_OPEN
);
888 /* Set open packet values. */
889 stream_putc (s
, BGP_VERSION_4
); /* BGP version */
890 stream_putw (s
, (local_as
<= BGP_AS_MAX
) ? (u_int16_t
) local_as
892 stream_putw (s
, send_holdtime
); /* Hold Time */
893 stream_put_in_addr (s
, &peer
->local_id
); /* BGP Identifier */
895 /* Set capability code. */
896 bgp_open_capability (s
, peer
);
898 /* Set BGP packet length. */
899 length
= bgp_packet_set_size (s
);
901 if (BGP_DEBUG (normal
, NORMAL
))
902 zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
903 peer
->host
, BGP_VERSION_4
, local_as
,
904 send_holdtime
, inet_ntoa (peer
->local_id
));
906 if (BGP_DEBUG (normal
, NORMAL
))
907 zlog_debug ("%s send message type %d, length (incl. header) %d",
908 peer
->host
, BGP_MSG_OPEN
, length
);
910 /* Dump packet if debug option is set. */
911 /* bgp_packet_dump (s); */
913 /* Add packet to the peer. */
914 bgp_packet_add (peer
, s
);
916 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
919 /* Send BGP notify packet with data potion. */
921 bgp_notify_send_with_data (struct peer
*peer
, u_char code
, u_char sub_code
,
922 u_char
*data
, size_t datalen
)
927 /* Allocate new stream. */
928 s
= stream_new (BGP_MAX_PACKET_SIZE
);
930 /* Make nitify packet. */
931 bgp_packet_set_marker (s
, BGP_MSG_NOTIFY
);
933 /* Set notify packet values. */
934 stream_putc (s
, code
); /* BGP notify code */
935 stream_putc (s
, sub_code
); /* BGP notify sub_code */
937 /* If notify data is present. */
939 stream_write (s
, data
, datalen
);
941 /* Set BGP packet length. */
942 length
= bgp_packet_set_size (s
);
944 /* Add packet to the peer. */
945 stream_fifo_clean (peer
->obuf
);
946 bgp_packet_add (peer
, s
);
950 struct bgp_notify bgp_notify
;
955 bgp_notify
.code
= code
;
956 bgp_notify
.subcode
= sub_code
;
957 bgp_notify
.data
= NULL
;
958 bgp_notify
.length
= length
- BGP_MSG_NOTIFY_MIN_SIZE
;
960 if (bgp_notify
.length
)
962 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
963 for (i
= 0; i
< bgp_notify
.length
; i
++)
966 sprintf (c
, " %02x", data
[i
]);
967 strcat (bgp_notify
.data
, c
);
972 sprintf (c
, "%02x", data
[i
]);
973 strcpy (bgp_notify
.data
, c
);
976 bgp_notify_print (peer
, &bgp_notify
, "sending");
978 XFREE (MTYPE_TMP
, bgp_notify
.data
);
981 if (BGP_DEBUG (normal
, NORMAL
))
982 zlog_debug ("%s send message type %d, length (incl. header) %d",
983 peer
->host
, BGP_MSG_NOTIFY
, length
);
985 /* peer reset cause */
986 if (sub_code
!= BGP_NOTIFY_CEASE_CONFIG_CHANGE
)
988 if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_RESET
)
990 peer
->last_reset
= PEER_DOWN_USER_RESET
;
991 zlog_info ("Notification sent to neighbor %s: User reset", peer
->host
);
993 else if (sub_code
== BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
)
995 peer
->last_reset
= PEER_DOWN_USER_SHUTDOWN
;
996 zlog_info ("Notification sent to neighbor %s: shutdown", peer
->host
);
1000 peer
->last_reset
= PEER_DOWN_NOTIFY_SEND
;
1001 zlog_info ("Notification sent to neighbor %s: type %u/%u",
1002 peer
->host
, code
, sub_code
);
1006 zlog_info ("Notification sent to neighbor %s: configuration change",
1009 /* Call immediately. */
1010 BGP_WRITE_OFF (peer
->t_write
);
1012 bgp_write_notify (peer
);
1015 /* Send BGP notify packet. */
1017 bgp_notify_send (struct peer
*peer
, u_char code
, u_char sub_code
)
1019 bgp_notify_send_with_data (peer
, code
, sub_code
, NULL
, 0);
1022 /* Send route refresh message to the peer. */
1024 bgp_route_refresh_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
1025 u_char orf_type
, u_char when_to_refresh
, int remove
)
1028 struct stream
*packet
;
1030 struct bgp_filter
*filter
;
1031 int orf_refresh
= 0;
1033 if (DISABLE_BGP_ANNOUNCE
)
1036 filter
= &peer
->filter
[afi
][safi
];
1038 /* Adjust safi code. */
1039 if (safi
== SAFI_MPLS_VPN
)
1040 safi
= SAFI_MPLS_LABELED_VPN
;
1042 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1044 /* Make BGP update packet. */
1045 if (CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
1046 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_NEW
);
1048 bgp_packet_set_marker (s
, BGP_MSG_ROUTE_REFRESH_OLD
);
1050 /* Encode Route Refresh message. */
1051 stream_putw (s
, afi
);
1053 stream_putc (s
, safi
);
1055 if (orf_type
== ORF_TYPE_PREFIX
1056 || orf_type
== ORF_TYPE_PREFIX_OLD
)
1057 if (remove
|| filter
->plist
[FILTER_IN
].plist
)
1063 stream_putc (s
, when_to_refresh
);
1064 stream_putc (s
, orf_type
);
1065 orfp
= stream_get_endp (s
);
1070 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
1071 stream_putc (s
, ORF_COMMON_PART_REMOVE_ALL
);
1072 if (BGP_DEBUG (normal
, NORMAL
))
1073 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
1074 peer
->host
, orf_type
,
1075 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
1080 SET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_PREFIX_SEND
);
1081 prefix_bgp_orf_entry (s
, filter
->plist
[FILTER_IN
].plist
,
1082 ORF_COMMON_PART_ADD
, ORF_COMMON_PART_PERMIT
,
1083 ORF_COMMON_PART_DENY
);
1084 if (BGP_DEBUG (normal
, NORMAL
))
1085 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
1086 peer
->host
, orf_type
,
1087 (when_to_refresh
== REFRESH_DEFER
? "defer" : "immediate"),
1091 /* Total ORF Entry Len. */
1092 orf_len
= stream_get_endp (s
) - orfp
- 2;
1093 stream_putw_at (s
, orfp
, orf_len
);
1096 /* Set packet size. */
1097 length
= bgp_packet_set_size (s
);
1099 if (BGP_DEBUG (normal
, NORMAL
))
1102 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
1103 peer
->host
, afi
, safi
);
1104 zlog_debug ("%s send message type %d, length (incl. header) %d",
1105 peer
->host
, CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
) ?
1106 BGP_MSG_ROUTE_REFRESH_NEW
: BGP_MSG_ROUTE_REFRESH_OLD
, length
);
1109 /* Make real packet. */
1110 packet
= stream_dup (s
);
1113 /* Add packet to the peer. */
1114 bgp_packet_add (peer
, packet
);
1116 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1119 /* Send capability message to the peer. */
1121 bgp_capability_send (struct peer
*peer
, afi_t afi
, safi_t safi
,
1122 int capability_code
, int action
)
1125 struct stream
*packet
;
1128 /* Adjust safi code. */
1129 if (safi
== SAFI_MPLS_VPN
)
1130 safi
= SAFI_MPLS_LABELED_VPN
;
1132 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1134 /* Make BGP update packet. */
1135 bgp_packet_set_marker (s
, BGP_MSG_CAPABILITY
);
1137 /* Encode MP_EXT capability. */
1138 if (capability_code
== CAPABILITY_CODE_MP
)
1140 stream_putc (s
, action
);
1141 stream_putc (s
, CAPABILITY_CODE_MP
);
1142 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
1143 stream_putw (s
, afi
);
1145 stream_putc (s
, safi
);
1147 if (BGP_DEBUG (normal
, NORMAL
))
1148 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
1149 peer
->host
, action
== CAPABILITY_ACTION_SET
?
1150 "Advertising" : "Removing", afi
, safi
);
1153 /* Set packet size. */
1154 length
= bgp_packet_set_size (s
);
1156 /* Make real packet. */
1157 packet
= stream_dup (s
);
1160 /* Add packet to the peer. */
1161 bgp_packet_add (peer
, packet
);
1163 if (BGP_DEBUG (normal
, NORMAL
))
1164 zlog_debug ("%s send message type %d, length (incl. header) %d",
1165 peer
->host
, BGP_MSG_CAPABILITY
, length
);
1167 BGP_WRITE_ON (peer
->t_write
, bgp_write
, peer
->fd
);
1170 /* RFC1771 6.8 Connection collision detection. */
1172 bgp_collision_detect (struct peer
*new, struct in_addr remote_id
)
1175 struct listnode
*node
, *nnode
;
1178 bgp
= bgp_get_default ();
1182 /* Upon receipt of an OPEN message, the local system must examine
1183 all of its connections that are in the OpenConfirm state. A BGP
1184 speaker may also examine connections in an OpenSent state if it
1185 knows the BGP Identifier of the peer by means outside of the
1186 protocol. If among these connections there is a connection to a
1187 remote BGP speaker whose BGP Identifier equals the one in the
1188 OPEN message, then the local system performs the following
1189 collision resolution procedure: */
1191 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
1193 /* Under OpenConfirm status, local peer structure already hold
1194 remote router ID. */
1197 && (peer
->status
== OpenConfirm
|| peer
->status
== OpenSent
)
1198 && sockunion_same (&peer
->su
, &new->su
))
1200 /* 1. The BGP Identifier of the local system is compared to
1201 the BGP Identifier of the remote system (as specified in
1202 the OPEN message). */
1204 if (ntohl (peer
->local_id
.s_addr
) < ntohl (remote_id
.s_addr
))
1206 /* 2. If the value of the local BGP Identifier is less
1207 than the remote one, the local system closes BGP
1208 connection that already exists (the one that is
1209 already in the OpenConfirm state), and accepts BGP
1210 connection initiated by the remote system. */
1213 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1218 /* 3. Otherwise, the local system closes newly created
1219 BGP connection (the one associated with the newly
1220 received OPEN message), and continues to use the
1221 existing one (the one that is already in the
1222 OpenConfirm state). */
1225 bgp_notify_send (new, BGP_NOTIFY_CEASE
,
1226 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION
);
1235 bgp_open_receive (struct peer
*peer
, bgp_size_t size
)
1241 u_int16_t send_holdtime
;
1244 struct peer
*realpeer
;
1245 struct in_addr remote_id
;
1247 u_int8_t notify_data_remote_as
[2];
1248 u_int8_t notify_data_remote_id
[4];
1252 /* Parse open packet. */
1253 version
= stream_getc (peer
->ibuf
);
1254 memcpy (notify_data_remote_as
, stream_pnt (peer
->ibuf
), 2);
1255 remote_as
= stream_getw (peer
->ibuf
);
1256 holdtime
= stream_getw (peer
->ibuf
);
1257 memcpy (notify_data_remote_id
, stream_pnt (peer
->ibuf
), 4);
1258 remote_id
.s_addr
= stream_get_ipv4 (peer
->ibuf
);
1260 /* Receive OPEN message log */
1261 if (BGP_DEBUG (normal
, NORMAL
))
1262 zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
1263 " holdtime %d, id %s",
1264 peer
->host
, version
, remote_as
, holdtime
,
1265 inet_ntoa (remote_id
));
1267 /* BEGIN to read the capability here, but dont do it yet */
1269 optlen
= stream_getc (peer
->ibuf
);
1273 /* We need the as4 capability value *right now* because
1274 * if it is there, we have not got the remote_as yet, and without
1275 * that we do not know which peer is connecting to us now.
1277 as4
= peek_for_as4_capability (peer
, optlen
);
1280 /* Just in case we have a silly peer who sends AS4 capability set to 0 */
1281 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
) && !as4
)
1283 zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
1285 bgp_notify_send (peer
, BGP_NOTIFY_OPEN_ERR
,
1286 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1290 if (remote_as
== BGP_AS_TRANS
)
1292 /* Take the AS4 from the capability. We must have received the
1293 * capability now! Otherwise we have a asn16 peer who uses
1294 * BGP_AS_TRANS, for some unknown reason.
1296 if (as4
== BGP_AS_TRANS
)
1298 zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1300 bgp_notify_send (peer
, BGP_NOTIFY_OPEN_ERR
,
1301 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1305 if (!as4
&& BGP_DEBUG (as4
, AS4
))
1306 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
1307 " Odd, but proceeding.", peer
->host
);
1308 else if (as4
< BGP_AS_MAX
&& BGP_DEBUG (as4
, AS4
))
1309 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
1310 "in 2-bytes, very odd peer.", peer
->host
, as4
);
1316 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
1317 /* If we have got the capability, peer->as4cap must match remote_as */
1318 if (CHECK_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
)
1319 && as4
!= remote_as
)
1321 /* raise error, log this, close session */
1322 zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
1323 " mismatch with 16bit 'myasn' %u in open",
1324 peer
->host
, as4
, remote_as
);
1325 bgp_notify_send (peer
, BGP_NOTIFY_OPEN_ERR
,
1326 BGP_NOTIFY_OPEN_BAD_PEER_AS
);
1331 /* Lookup peer from Open packet. */
1332 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1336 realpeer
= peer_lookup_with_open (&peer
->su
, remote_as
, &remote_id
, &as
);
1340 /* Peer's source IP address is check in bgp_accept(), so this
1341 must be AS number mismatch or remote-id configuration
1345 if (BGP_DEBUG (normal
, NORMAL
))
1346 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1347 peer
->host
, inet_ntoa (remote_id
));
1348 bgp_notify_send_with_data (peer
, BGP_NOTIFY_OPEN_ERR
,
1349 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1350 notify_data_remote_id
, 4);
1354 if (BGP_DEBUG (normal
, NORMAL
))
1355 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1356 peer
->host
, remote_as
, peer
->as
);
1357 bgp_notify_send_with_data (peer
, BGP_NOTIFY_OPEN_ERR
,
1358 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1359 notify_data_remote_as
, 2);
1365 /* When collision is detected and this peer is closed. Retrun
1367 ret
= bgp_collision_detect (peer
, remote_id
);
1372 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
1374 if (realpeer
->status
== Established
1375 && CHECK_FLAG (realpeer
->sflags
, PEER_STATUS_NSF_MODE
))
1377 realpeer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
1378 SET_FLAG (realpeer
->sflags
, PEER_STATUS_NSF_WAIT
);
1380 else if (ret
== 0 && realpeer
->status
!= Active
1381 && realpeer
->status
!= OpenSent
1382 && realpeer
->status
!= OpenConfirm
1383 && realpeer
->status
!= Connect
)
1385 /* XXX: This is an awful problem..
1387 * According to the RFC we should just let this connection (of the
1388 * accepted 'peer') continue on to Established if the other
1389 * connection (the 'realpeer' one) is in state Connect, and deal
1390 * with the more larval FSM as/when it gets far enough to receive
1391 * an Open. We don't do that though, we instead close the (more
1392 * developed) accepted connection.
1394 * This means there's a race, which if hit, can loop:
1396 * FSM for A FSM for B
1397 * realpeer accept-peer realpeer accept-peer
1413 * If both sides are Quagga, they're almost certain to wait for
1414 * the same amount of time of course (which doesn't preclude other
1415 * implementations also waiting for same time). The race is
1416 * exacerbated by high-latency (in bgpd and/or the network).
1418 * The reason we do this is because our FSM is tied to our peer
1419 * structure, which carries our configuration information, etc.
1420 * I.e. we can't let the accepted-peer FSM continue on as it is,
1421 * cause it's not associated with any actual peer configuration -
1422 * it's just a dummy.
1424 * It's possible we could hack-fix this by just bgp_stop'ing the
1425 * realpeer and continueing on with the 'transfer FSM' below.
1426 * Ideally, we need to seperate FSMs from struct peer.
1428 * Setting one side to passive avoids the race, as a workaround.
1430 if (BGP_DEBUG (events
, EVENTS
))
1431 zlog_debug ("%s peer status is %s close connection",
1432 realpeer
->host
, LOOKUP (bgp_status_msg
,
1434 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
,
1435 BGP_NOTIFY_CEASE_CONNECT_REJECT
);
1440 if (BGP_DEBUG (events
, EVENTS
))
1441 zlog_debug ("%s [Event] Transfer accept BGP peer to real (state %s)",
1443 LOOKUP (bgp_status_msg
, realpeer
->status
));
1445 bgp_stop (realpeer
);
1447 /* Transfer file descriptor. */
1448 realpeer
->fd
= peer
->fd
;
1451 /* Transfer input buffer. */
1452 stream_free (realpeer
->ibuf
);
1453 realpeer
->ibuf
= peer
->ibuf
;
1454 realpeer
->packet_size
= peer
->packet_size
;
1457 /* Transfer status. */
1458 realpeer
->status
= peer
->status
;
1461 /* peer pointer change. Open packet send to neighbor. */
1463 bgp_open_send (peer
);
1466 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1470 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
1473 /* remote router-id check. */
1474 if (remote_id
.s_addr
== 0
1475 || IPV4_CLASS_DE (ntohl (remote_id
.s_addr
))
1476 || ntohl (peer
->local_id
.s_addr
) == ntohl (remote_id
.s_addr
))
1478 if (BGP_DEBUG (normal
, NORMAL
))
1479 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1480 peer
->host
, inet_ntoa (remote_id
));
1481 bgp_notify_send_with_data (peer
,
1482 BGP_NOTIFY_OPEN_ERR
,
1483 BGP_NOTIFY_OPEN_BAD_BGP_IDENT
,
1484 notify_data_remote_id
, 4);
1488 /* Set remote router-id */
1489 peer
->remote_id
= remote_id
;
1491 /* Peer BGP version check. */
1492 if (version
!= BGP_VERSION_4
)
1494 u_int16_t maxver
= htons(BGP_VERSION_4
);
1495 /* XXX this reply may not be correct if version < 4 XXX */
1496 if (BGP_DEBUG (normal
, NORMAL
))
1497 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
1498 peer
->host
, version
, BGP_VERSION_4
);
1499 /* Data must be in network byte order here */
1500 bgp_notify_send_with_data (peer
,
1501 BGP_NOTIFY_OPEN_ERR
,
1502 BGP_NOTIFY_OPEN_UNSUP_VERSION
,
1503 (u_int8_t
*) &maxver
, 2);
1507 /* Check neighbor as number. */
1508 if (remote_as
!= peer
->as
)
1510 if (BGP_DEBUG (normal
, NORMAL
))
1511 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1512 peer
->host
, remote_as
, peer
->as
);
1513 bgp_notify_send_with_data (peer
,
1514 BGP_NOTIFY_OPEN_ERR
,
1515 BGP_NOTIFY_OPEN_BAD_PEER_AS
,
1516 notify_data_remote_as
, 2);
1520 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1521 calculate the value of the Hold Timer by using the smaller of its
1522 configured Hold Time and the Hold Time received in the OPEN message.
1523 The Hold Time MUST be either zero or at least three seconds. An
1524 implementation may reject connections on the basis of the Hold Time. */
1526 if (holdtime
< 3 && holdtime
!= 0)
1528 bgp_notify_send (peer
,
1529 BGP_NOTIFY_OPEN_ERR
,
1530 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
);
1534 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1535 would be one third of the Hold Time interval. KEEPALIVE messages
1536 MUST NOT be sent more frequently than one per second. An
1537 implementation MAY adjust the rate at which it sends KEEPALIVE
1538 messages as a function of the Hold Time interval. */
1540 if (CHECK_FLAG (peer
->config
, PEER_CONFIG_TIMER
))
1541 send_holdtime
= peer
->holdtime
;
1543 send_holdtime
= peer
->bgp
->default_holdtime
;
1545 if (holdtime
< send_holdtime
)
1546 peer
->v_holdtime
= holdtime
;
1548 peer
->v_holdtime
= send_holdtime
;
1550 peer
->v_keepalive
= peer
->v_holdtime
/ 3;
1552 /* Open option part parse. */
1555 if ((ret
= bgp_open_option_parse (peer
, optlen
, &mp_capability
)) < 0)
1557 bgp_notify_send (peer
,
1558 BGP_NOTIFY_OPEN_ERR
,
1559 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME
);
1565 if (BGP_DEBUG (normal
, NORMAL
))
1566 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
1571 * Assume that the peer supports the locally configured set of
1572 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1573 * capabilities, or if 'override-capability' is configured.
1575 if (! mp_capability
||
1576 CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
1578 peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
] = peer
->afc
[AFI_IP
][SAFI_UNICAST
];
1579 peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP
][SAFI_MULTICAST
];
1580 peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
] = peer
->afc
[AFI_IP6
][SAFI_UNICAST
];
1581 peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
] = peer
->afc
[AFI_IP6
][SAFI_MULTICAST
];
1585 bgp_getsockname (peer
);
1587 BGP_EVENT_ADD (peer
, Receive_OPEN_message
);
1589 peer
->packet_size
= 0;
1591 stream_reset (peer
->ibuf
);
1596 /* Parse BGP Update packet and make attribute object. */
1598 bgp_update_receive (struct peer
*peer
, bgp_size_t size
)
1604 struct attr_extra extra
;
1605 bgp_size_t attribute_len
;
1606 bgp_size_t update_len
;
1607 bgp_size_t withdraw_len
;
1608 struct bgp_nlri update
;
1609 struct bgp_nlri withdraw
;
1610 struct bgp_nlri mp_update
;
1611 struct bgp_nlri mp_withdraw
;
1613 /* Status must be Established. */
1614 if (peer
->status
!= Established
)
1616 zlog_err ("%s [FSM] Update packet received under status %s",
1617 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
1618 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
1622 /* Set initial values. */
1623 memset (&attr
, 0, sizeof (struct attr
));
1624 memset (&extra
, 0, sizeof (struct attr_extra
));
1625 memset (&update
, 0, sizeof (struct bgp_nlri
));
1626 memset (&withdraw
, 0, sizeof (struct bgp_nlri
));
1627 memset (&mp_update
, 0, sizeof (struct bgp_nlri
));
1628 memset (&mp_withdraw
, 0, sizeof (struct bgp_nlri
));
1629 attr
.extra
= &extra
;
1632 end
= stream_pnt (s
) + size
;
1634 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1635 Length is too large (i.e., if Unfeasible Routes Length + Total
1636 Attribute Length + 23 exceeds the message Length), then the Error
1637 Subcode is set to Malformed Attribute List. */
1638 if (stream_pnt (s
) + 2 > end
)
1640 zlog_err ("%s [Error] Update packet error"
1641 " (packet length is short for unfeasible length)",
1643 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1644 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1648 /* Unfeasible Route Length. */
1649 withdraw_len
= stream_getw (s
);
1651 /* Unfeasible Route Length check. */
1652 if (stream_pnt (s
) + withdraw_len
> end
)
1654 zlog_err ("%s [Error] Update packet error"
1655 " (packet unfeasible length overflow %d)",
1656 peer
->host
, withdraw_len
);
1657 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1658 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1662 /* Unfeasible Route packet format check. */
1663 if (withdraw_len
> 0)
1665 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, stream_pnt (s
), withdraw_len
);
1669 if (BGP_DEBUG (packet
, PACKET_RECV
))
1670 zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer
->host
);
1672 withdraw
.afi
= AFI_IP
;
1673 withdraw
.safi
= SAFI_UNICAST
;
1674 withdraw
.nlri
= stream_pnt (s
);
1675 withdraw
.length
= withdraw_len
;
1676 stream_forward_getp (s
, withdraw_len
);
1679 /* Attribute total length check. */
1680 if (stream_pnt (s
) + 2 > end
)
1682 zlog_warn ("%s [Error] Packet Error"
1683 " (update packet is short for attribute length)",
1685 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1686 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1690 /* Fetch attribute total length. */
1691 attribute_len
= stream_getw (s
);
1693 /* Attribute length check. */
1694 if (stream_pnt (s
) + attribute_len
> end
)
1696 zlog_warn ("%s [Error] Packet Error"
1697 " (update packet attribute length overflow %d)",
1698 peer
->host
, attribute_len
);
1699 bgp_notify_send (peer
, BGP_NOTIFY_UPDATE_ERR
,
1700 BGP_NOTIFY_UPDATE_MAL_ATTR
);
1704 /* Certain attribute parsing errors should not be considered bad enough
1705 * to reset the session for, most particularly any partial/optional
1706 * attributes that have 'tunneled' over speakers that don't understand
1707 * them. Instead we withdraw only the prefix concerned.
1709 * Complicates the flow a little though..
1711 bgp_attr_parse_ret_t attr_parse_ret
= BGP_ATTR_PARSE_PROCEED
;
1712 /* This define morphs the update case into a withdraw when lower levels
1713 * have signalled an error condition where this is best.
1715 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1717 /* Parse attribute when it exists. */
1720 attr_parse_ret
= bgp_attr_parse (peer
, &attr
, attribute_len
,
1721 &mp_update
, &mp_withdraw
);
1722 if (attr_parse_ret
== BGP_ATTR_PARSE_ERROR
)
1726 /* Logging the attribute. */
1727 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
1728 || BGP_DEBUG (update
, UPDATE_IN
))
1730 char attrstr
[BUFSIZ
];
1733 ret
= bgp_dump_attr (peer
, &attr
, attrstr
, BUFSIZ
);
1734 int lvl
= (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1735 ? LOG_ERR
: LOG_DEBUG
;
1737 if (attr_parse_ret
== BGP_ATTR_PARSE_WITHDRAW
)
1738 zlog (peer
->log
, LOG_ERR
,
1739 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1743 zlog (peer
->log
, lvl
, "%s rcvd UPDATE w/ attr: %s",
1744 peer
->host
, attrstr
);
1747 /* Network Layer Reachability Information. */
1748 update_len
= end
- stream_pnt (s
);
1752 /* Check NLRI packet format and prefix length. */
1753 ret
= bgp_nlri_sanity_check (peer
, AFI_IP
, stream_pnt (s
), update_len
);
1756 bgp_attr_unintern_sub (&attr
);
1760 /* Set NLRI portion to structure. */
1761 update
.afi
= AFI_IP
;
1762 update
.safi
= SAFI_UNICAST
;
1763 update
.nlri
= stream_pnt (s
);
1764 update
.length
= update_len
;
1765 stream_forward_getp (s
, update_len
);
1768 /* NLRI is processed only when the peer is configured specific
1769 Address Family and Subsequent Address Family. */
1770 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
])
1772 if (withdraw
.length
)
1773 bgp_nlri_parse (peer
, NULL
, &withdraw
);
1777 /* We check well-known attribute only for IPv4 unicast
1779 ret
= bgp_attr_check (peer
, &attr
);
1782 bgp_attr_unintern_sub (&attr
);
1786 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &update
);
1789 if (mp_update
.length
1790 && mp_update
.afi
== AFI_IP
1791 && mp_update
.safi
== SAFI_UNICAST
)
1792 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &mp_update
);
1794 if (mp_withdraw
.length
1795 && mp_withdraw
.afi
== AFI_IP
1796 && mp_withdraw
.safi
== SAFI_UNICAST
)
1797 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1799 if (! attribute_len
&& ! withdraw_len
)
1801 /* End-of-RIB received */
1802 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_UNICAST
],
1803 PEER_STATUS_EOR_RECEIVED
);
1805 /* NSF delete stale route */
1806 if (peer
->nsf
[AFI_IP
][SAFI_UNICAST
])
1807 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_UNICAST
);
1809 if (BGP_DEBUG (normal
, NORMAL
))
1810 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv4 Unicast from %s",
1814 if (peer
->afc
[AFI_IP
][SAFI_MULTICAST
])
1816 if (mp_update
.length
1817 && mp_update
.afi
== AFI_IP
1818 && mp_update
.safi
== SAFI_MULTICAST
)
1819 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &mp_update
);
1821 if (mp_withdraw
.length
1822 && mp_withdraw
.afi
== AFI_IP
1823 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1824 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1827 && mp_withdraw
.afi
== AFI_IP
1828 && mp_withdraw
.safi
== SAFI_MULTICAST
1829 && mp_withdraw
.length
== 0)
1831 /* End-of-RIB received */
1832 SET_FLAG (peer
->af_sflags
[AFI_IP
][SAFI_MULTICAST
],
1833 PEER_STATUS_EOR_RECEIVED
);
1835 /* NSF delete stale route */
1836 if (peer
->nsf
[AFI_IP
][SAFI_MULTICAST
])
1837 bgp_clear_stale_route (peer
, AFI_IP
, SAFI_MULTICAST
);
1839 if (BGP_DEBUG (normal
, NORMAL
))
1840 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv4 Multicast from %s",
1844 if (peer
->afc
[AFI_IP6
][SAFI_UNICAST
])
1846 if (mp_update
.length
1847 && mp_update
.afi
== AFI_IP6
1848 && mp_update
.safi
== SAFI_UNICAST
)
1849 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &mp_update
);
1851 if (mp_withdraw
.length
1852 && mp_withdraw
.afi
== AFI_IP6
1853 && mp_withdraw
.safi
== SAFI_UNICAST
)
1854 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1857 && mp_withdraw
.afi
== AFI_IP6
1858 && mp_withdraw
.safi
== SAFI_UNICAST
1859 && mp_withdraw
.length
== 0)
1861 /* End-of-RIB received */
1862 SET_FLAG (peer
->af_sflags
[AFI_IP6
][SAFI_UNICAST
], PEER_STATUS_EOR_RECEIVED
);
1864 /* NSF delete stale route */
1865 if (peer
->nsf
[AFI_IP6
][SAFI_UNICAST
])
1866 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_UNICAST
);
1868 if (BGP_DEBUG (normal
, NORMAL
))
1869 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv6 Unicast from %s",
1873 if (peer
->afc
[AFI_IP6
][SAFI_MULTICAST
])
1875 if (mp_update
.length
1876 && mp_update
.afi
== AFI_IP6
1877 && mp_update
.safi
== SAFI_MULTICAST
)
1878 bgp_nlri_parse (peer
, NLRI_ATTR_ARG
, &mp_update
);
1880 if (mp_withdraw
.length
1881 && mp_withdraw
.afi
== AFI_IP6
1882 && mp_withdraw
.safi
== SAFI_MULTICAST
)
1883 bgp_nlri_parse (peer
, NULL
, &mp_withdraw
);
1886 && mp_withdraw
.afi
== AFI_IP6
1887 && mp_withdraw
.safi
== SAFI_MULTICAST
1888 && mp_withdraw
.length
== 0)
1890 /* End-of-RIB received */
1892 /* NSF delete stale route */
1893 if (peer
->nsf
[AFI_IP6
][SAFI_MULTICAST
])
1894 bgp_clear_stale_route (peer
, AFI_IP6
, SAFI_MULTICAST
);
1896 if (BGP_DEBUG (update
, UPDATE_IN
))
1897 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for IPv6 Multicast from %s",
1901 if (peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
])
1903 if (mp_update
.length
1904 && mp_update
.afi
== AFI_IP
1905 && mp_update
.safi
== SAFI_MPLS_LABELED_VPN
)
1906 bgp_nlri_parse_vpnv4 (peer
, NLRI_ATTR_ARG
, &mp_update
);
1908 if (mp_withdraw
.length
1909 && mp_withdraw
.afi
== AFI_IP
1910 && mp_withdraw
.safi
== SAFI_MPLS_LABELED_VPN
)
1911 bgp_nlri_parse_vpnv4 (peer
, NULL
, &mp_withdraw
);
1914 && mp_withdraw
.afi
== AFI_IP
1915 && mp_withdraw
.safi
== SAFI_MPLS_LABELED_VPN
1916 && mp_withdraw
.length
== 0)
1918 /* End-of-RIB received */
1920 if (BGP_DEBUG (update
, UPDATE_IN
))
1921 zlog (peer
->log
, LOG_DEBUG
, "rcvd End-of-RIB for VPNv4 Unicast from %s",
1926 /* Everything is done. We unintern temporary structures which
1927 interned in bgp_attr_parse(). */
1928 bgp_attr_unintern_sub (&attr
);
1930 /* If peering is stopped due to some reason, do not generate BGP
1932 if (peer
->status
!= Established
)
1935 /* Increment packet counter. */
1937 peer
->update_time
= bgp_clock ();
1939 /* Rearm holdtime timer */
1940 BGP_TIMER_OFF (peer
->t_holdtime
);
1941 bgp_timer_set (peer
);
1946 /* Notify message treatment function. */
1948 bgp_notify_receive (struct peer
*peer
, bgp_size_t size
)
1950 struct bgp_notify bgp_notify
;
1952 if (peer
->notify
.data
)
1954 XFREE (MTYPE_TMP
, peer
->notify
.data
);
1955 peer
->notify
.data
= NULL
;
1956 peer
->notify
.length
= 0;
1959 bgp_notify
.code
= stream_getc (peer
->ibuf
);
1960 bgp_notify
.subcode
= stream_getc (peer
->ibuf
);
1961 bgp_notify
.length
= size
- 2;
1962 bgp_notify
.data
= NULL
;
1964 /* Preserv notify code and sub code. */
1965 peer
->notify
.code
= bgp_notify
.code
;
1966 peer
->notify
.subcode
= bgp_notify
.subcode
;
1967 /* For further diagnostic record returned Data. */
1968 if (bgp_notify
.length
)
1970 peer
->notify
.length
= size
- 2;
1971 peer
->notify
.data
= XMALLOC (MTYPE_TMP
, size
- 2);
1972 memcpy (peer
->notify
.data
, stream_pnt (peer
->ibuf
), size
- 2);
1981 if (bgp_notify
.length
)
1983 bgp_notify
.data
= XMALLOC (MTYPE_TMP
, bgp_notify
.length
* 3);
1984 for (i
= 0; i
< bgp_notify
.length
; i
++)
1987 sprintf (c
, " %02x", stream_getc (peer
->ibuf
));
1988 strcat (bgp_notify
.data
, c
);
1993 sprintf (c
, "%02x", stream_getc (peer
->ibuf
));
1994 strcpy (bgp_notify
.data
, c
);
1998 bgp_notify_print(peer
, &bgp_notify
, "received");
1999 if (bgp_notify
.data
)
2000 XFREE (MTYPE_TMP
, bgp_notify
.data
);
2003 /* peer count update */
2006 if (peer
->status
== Established
)
2007 peer
->last_reset
= PEER_DOWN_NOTIFY_RECEIVED
;
2009 /* We have to check for Notify with Unsupported Optional Parameter.
2010 in that case we fallback to open without the capability option.
2011 But this done in bgp_stop. We just mark it here to avoid changing
2013 if (bgp_notify
.code
== BGP_NOTIFY_OPEN_ERR
&&
2014 bgp_notify
.subcode
== BGP_NOTIFY_OPEN_UNSUP_PARAM
)
2015 UNSET_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
2017 BGP_EVENT_ADD (peer
, Receive_NOTIFICATION_message
);
2020 /* Keepalive treatment function -- get keepalive send keepalive */
2022 bgp_keepalive_receive (struct peer
*peer
, bgp_size_t size
)
2024 if (BGP_DEBUG (keepalive
, KEEPALIVE
))
2025 zlog_debug ("%s KEEPALIVE rcvd", peer
->host
);
2027 BGP_EVENT_ADD (peer
, Receive_KEEPALIVE_message
);
2030 /* Route refresh message is received. */
2032 bgp_route_refresh_receive (struct peer
*peer
, bgp_size_t size
)
2039 /* If peer does not have the capability, send notification. */
2040 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_REFRESH_ADV
))
2042 plog_err (peer
->log
, "%s [Error] BGP route refresh is not enabled",
2044 bgp_notify_send (peer
,
2045 BGP_NOTIFY_HEADER_ERR
,
2046 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2050 /* Status must be Established. */
2051 if (peer
->status
!= Established
)
2053 plog_err (peer
->log
,
2054 "%s [Error] Route refresh packet received under status %s",
2055 peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
2056 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
2063 afi
= stream_getw (s
);
2064 reserved
= stream_getc (s
);
2065 safi
= stream_getc (s
);
2067 if (BGP_DEBUG (normal
, NORMAL
))
2068 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
2069 peer
->host
, afi
, safi
);
2071 /* Check AFI and SAFI. */
2072 if ((afi
!= AFI_IP
&& afi
!= AFI_IP6
)
2073 || (safi
!= SAFI_UNICAST
&& safi
!= SAFI_MULTICAST
2074 && safi
!= SAFI_MPLS_LABELED_VPN
))
2076 if (BGP_DEBUG (normal
, NORMAL
))
2078 zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
2079 peer
->host
, afi
, safi
);
2084 /* Adjust safi code. */
2085 if (safi
== SAFI_MPLS_LABELED_VPN
)
2086 safi
= SAFI_MPLS_VPN
;
2088 if (size
!= BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
)
2091 u_char when_to_refresh
;
2095 if (size
- (BGP_MSG_ROUTE_REFRESH_MIN_SIZE
- BGP_HEADER_SIZE
) < 5)
2097 zlog_info ("%s ORF route refresh length error", peer
->host
);
2098 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2102 when_to_refresh
= stream_getc (s
);
2103 end
= stream_pnt (s
) + (size
- 5);
2105 while ((stream_pnt (s
) + 2) < end
)
2107 orf_type
= stream_getc (s
);
2108 orf_len
= stream_getw (s
);
2110 /* orf_len in bounds? */
2111 if ((stream_pnt (s
) + orf_len
) > end
)
2112 break; /* XXX: Notify instead?? */
2113 if (orf_type
== ORF_TYPE_PREFIX
2114 || orf_type
== ORF_TYPE_PREFIX_OLD
)
2116 u_char
*p_pnt
= stream_pnt (s
);
2117 u_char
*p_end
= stream_pnt (s
) + orf_len
;
2118 struct orf_prefix orfp
;
2125 if (BGP_DEBUG (normal
, NORMAL
))
2127 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
2128 peer
->host
, orf_type
, orf_len
);
2131 /* we're going to read at least 1 byte of common ORF header,
2132 * and 7 bytes of ORF Address-filter entry from the stream
2137 /* ORF prefix-list name */
2138 sprintf (name
, "%s.%d.%d", peer
->host
, afi
, safi
);
2140 while (p_pnt
< p_end
)
2142 /* If the ORF entry is malformed, want to read as much of it
2143 * as possible without going beyond the bounds of the entry,
2144 * to maximise debug information.
2147 memset (&orfp
, 0, sizeof (struct orf_prefix
));
2149 /* after ++: p_pnt <= p_end */
2150 if (common
& ORF_COMMON_PART_REMOVE_ALL
)
2152 if (BGP_DEBUG (normal
, NORMAL
))
2153 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer
->host
);
2154 prefix_bgp_orf_remove_all (name
);
2157 ok
= ((p_end
- p_pnt
) >= sizeof(u_int32_t
)) ;
2160 memcpy (&seq
, p_pnt
, sizeof (u_int32_t
));
2161 p_pnt
+= sizeof (u_int32_t
);
2162 orfp
.seq
= ntohl (seq
);
2167 if ((ok
= (p_pnt
< p_end
)))
2168 orfp
.ge
= *p_pnt
++ ; /* value checked in prefix_bgp_orf_set() */
2169 if ((ok
= (p_pnt
< p_end
)))
2170 orfp
.le
= *p_pnt
++ ; /* value checked in prefix_bgp_orf_set() */
2171 if ((ok
= (p_pnt
< p_end
)))
2172 orfp
.p
.prefixlen
= *p_pnt
++ ;
2173 orfp
.p
.family
= afi2family (afi
); /* afi checked already */
2175 psize
= PSIZE (orfp
.p
.prefixlen
); /* 0 if not ok */
2176 if (psize
> prefix_blen(&orfp
.p
)) /* valid for family ? */
2179 psize
= prefix_blen(&orfp
.p
) ;
2181 if (psize
> (p_end
- p_pnt
)) /* valid for packet ? */
2184 psize
= p_end
- p_pnt
;
2188 memcpy (&orfp
.p
.u
.prefix
, p_pnt
, psize
);
2191 if (BGP_DEBUG (normal
, NORMAL
))
2193 char buf
[INET6_BUFSIZ
];
2195 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
2197 (common
& ORF_COMMON_PART_REMOVE
? "Remove" : "Add"),
2198 (common
& ORF_COMMON_PART_DENY
? "deny" : "permit"),
2200 inet_ntop (orfp
.p
.family
, &orfp
.p
.u
.prefix
, buf
, INET6_BUFSIZ
),
2201 orfp
.p
.prefixlen
, orfp
.ge
, orfp
.le
,
2202 ok
? "" : " MALFORMED");
2206 ret
= prefix_bgp_orf_set (name
, afi
, &orfp
,
2207 (common
& ORF_COMMON_PART_DENY
? 0 : 1 ),
2208 (common
& ORF_COMMON_PART_REMOVE
? 0 : 1));
2210 if (!ok
|| (ret
!= CMD_SUCCESS
))
2212 if (BGP_DEBUG (normal
, NORMAL
))
2213 zlog_debug ("%s Received misformatted prefixlist ORF."
2214 " Remove All pfxlist", peer
->host
);
2215 prefix_bgp_orf_remove_all (name
);
2219 peer
->orf_plist
[afi
][safi
] =
2220 prefix_list_lookup (AFI_ORF_PREFIX
, name
);
2222 stream_forward_getp (s
, orf_len
);
2224 if (BGP_DEBUG (normal
, NORMAL
))
2225 zlog_debug ("%s rcvd Refresh %s ORF request", peer
->host
,
2226 when_to_refresh
== REFRESH_DEFER
? "Defer" : "Immediate");
2227 if (when_to_refresh
== REFRESH_DEFER
)
2231 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2232 if (CHECK_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
))
2233 UNSET_FLAG (peer
->af_sflags
[afi
][safi
], PEER_STATUS_ORF_WAIT_REFRESH
);
2235 /* Perform route refreshment to the peer */
2236 bgp_announce_route (peer
, afi
, safi
);
2240 bgp_capability_msg_parse (struct peer
*peer
, u_char
*pnt
, bgp_size_t length
)
2243 struct capability_mp_data mpc
;
2244 struct capability_header
*hdr
;
2255 /* We need at least action, capability code and capability length. */
2258 zlog_info ("%s Capability length error", peer
->host
);
2259 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2263 hdr
= (struct capability_header
*)(pnt
+ 1);
2265 /* Action value check. */
2266 if (action
!= CAPABILITY_ACTION_SET
2267 && action
!= CAPABILITY_ACTION_UNSET
)
2269 zlog_info ("%s Capability Action Value error %d",
2270 peer
->host
, action
);
2271 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2275 if (BGP_DEBUG (normal
, NORMAL
))
2276 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
2277 peer
->host
, action
, hdr
->code
, hdr
->length
);
2279 /* Capability length check. */
2280 if ((pnt
+ hdr
->length
+ 3) > end
)
2282 zlog_info ("%s Capability length error", peer
->host
);
2283 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
2287 /* Fetch structure to the byte stream. */
2288 memcpy (&mpc
, pnt
+ 3, sizeof (struct capability_mp_data
));
2290 /* We know MP Capability Code. */
2291 if (hdr
->code
== CAPABILITY_CODE_MP
)
2293 afi
= ntohs (mpc
.afi
);
2296 /* Ignore capability when override-capability is set. */
2297 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
2300 if (!bgp_afi_safi_valid_indices (afi
, &safi
))
2302 if (BGP_DEBUG (normal
, NORMAL
))
2303 zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
2304 "(%u/%u)", peer
->host
, afi
, safi
);
2308 /* Address family check. */
2309 if (BGP_DEBUG (normal
, NORMAL
))
2310 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2312 action
== CAPABILITY_ACTION_SET
2313 ? "Advertising" : "Removing",
2314 ntohs(mpc
.afi
) , mpc
.safi
);
2316 if (action
== CAPABILITY_ACTION_SET
)
2318 peer
->afc_recv
[afi
][safi
] = 1;
2319 if (peer
->afc
[afi
][safi
])
2321 peer
->afc_nego
[afi
][safi
] = 1;
2322 bgp_announce_route (peer
, afi
, safi
);
2327 peer
->afc_recv
[afi
][safi
] = 0;
2328 peer
->afc_nego
[afi
][safi
] = 0;
2330 if (peer_active_nego (peer
))
2331 bgp_clear_route (peer
, afi
, safi
, BGP_CLEAR_ROUTE_NORMAL
);
2333 BGP_EVENT_ADD (peer
, BGP_Stop
);
2338 zlog_warn ("%s unrecognized capability code: %d - ignored",
2339 peer
->host
, hdr
->code
);
2341 pnt
+= hdr
->length
+ 3;
2346 /* Dynamic Capability is received.
2348 * This is exported for unit-test purposes
2351 bgp_capability_receive (struct peer
*peer
, bgp_size_t size
)
2355 /* Fetch pointer. */
2356 pnt
= stream_pnt (peer
->ibuf
);
2358 if (BGP_DEBUG (normal
, NORMAL
))
2359 zlog_debug ("%s rcv CAPABILITY", peer
->host
);
2361 /* If peer does not have the capability, send notification. */
2362 if (! CHECK_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_ADV
))
2364 plog_err (peer
->log
, "%s [Error] BGP dynamic capability is not enabled",
2366 bgp_notify_send (peer
,
2367 BGP_NOTIFY_HEADER_ERR
,
2368 BGP_NOTIFY_HEADER_BAD_MESTYPE
);
2372 /* Status must be Established. */
2373 if (peer
->status
!= Established
)
2375 plog_err (peer
->log
,
2376 "%s [Error] Dynamic capability packet received under status %s", peer
->host
, LOOKUP (bgp_status_msg
, peer
->status
));
2377 bgp_notify_send (peer
, BGP_NOTIFY_FSM_ERR
, 0);
2382 return bgp_capability_msg_parse (peer
, pnt
, size
);
2385 /* BGP read utility function. */
2387 bgp_read_packet (struct peer
*peer
)
2392 readsize
= peer
->packet_size
- stream_get_endp (peer
->ibuf
);
2394 /* If size is zero then return. */
2398 /* Read packet from fd. */
2399 nbytes
= stream_read_try (peer
->ibuf
, peer
->fd
, readsize
);
2401 /* If read byte is smaller than zero then error occured. */
2404 /* Transient error should retry */
2408 plog_err (peer
->log
, "%s [Error] bgp_read_packet error: %s",
2409 peer
->host
, safe_strerror (errno
));
2411 if (peer
->status
== Established
)
2413 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2415 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2416 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2419 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2422 BGP_EVENT_ADD (peer
, TCP_fatal_error
);
2426 /* When read byte is zero : clear bgp peer and return */
2429 if (BGP_DEBUG (events
, EVENTS
))
2430 plog_debug (peer
->log
, "%s [Event] BGP connection closed fd %d",
2431 peer
->host
, peer
->fd
);
2433 if (peer
->status
== Established
)
2435 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_NSF_MODE
))
2437 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
2438 SET_FLAG (peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2441 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
2444 BGP_EVENT_ADD (peer
, TCP_connection_closed
);
2448 /* We read partial packet. */
2449 if (stream_get_endp (peer
->ibuf
) != peer
->packet_size
)
2457 bgp_marker_all_one (struct stream
*s
, int length
)
2461 for (i
= 0; i
< length
; i
++)
2462 if (s
->data
[i
] != 0xff)
2468 /* Recent thread time.
2469 On same clock base as bgp_clock (MONOTONIC)
2470 but can be time of last context switch to bgp_read thread. */
2472 bgp_recent_clock (void)
2474 return recent_relative_time().tv_sec
;
2477 /* Starting point of packet process function. */
2479 bgp_read (struct thread
*thread
)
2485 char notify_data_length
[2];
2487 /* Yes first of all get peer pointer. */
2488 peer
= THREAD_ARG (thread
);
2489 peer
->t_read
= NULL
;
2491 /* For non-blocking IO check. */
2492 if (peer
->status
== Connect
)
2494 bgp_connect_check (peer
);
2501 zlog_err ("bgp_read peer's fd is negative value %d", peer
->fd
);
2504 BGP_READ_ON (peer
->t_read
, bgp_read
, peer
->fd
);
2507 /* Read packet header to determine type of the packet */
2508 if (peer
->packet_size
== 0)
2509 peer
->packet_size
= BGP_HEADER_SIZE
;
2511 if (stream_get_endp (peer
->ibuf
) < BGP_HEADER_SIZE
)
2513 ret
= bgp_read_packet (peer
);
2515 /* Header read error or partial read packet. */
2519 /* Get size and type. */
2520 stream_forward_getp (peer
->ibuf
, BGP_MARKER_SIZE
);
2521 memcpy (notify_data_length
, stream_pnt (peer
->ibuf
), 2);
2522 size
= stream_getw (peer
->ibuf
);
2523 type
= stream_getc (peer
->ibuf
);
2525 if (BGP_DEBUG (normal
, NORMAL
) && type
!= 2 && type
!= 0)
2526 zlog_debug ("%s rcv message type %d, length (excl. header) %d",
2527 peer
->host
, type
, size
- BGP_HEADER_SIZE
);
2530 if (((type
== BGP_MSG_OPEN
) || (type
== BGP_MSG_KEEPALIVE
))
2531 && ! bgp_marker_all_one (peer
->ibuf
, BGP_MARKER_SIZE
))
2533 bgp_notify_send (peer
,
2534 BGP_NOTIFY_HEADER_ERR
,
2535 BGP_NOTIFY_HEADER_NOT_SYNC
);
2539 /* BGP type check. */
2540 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
2541 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
2542 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
2543 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
2544 && type
!= BGP_MSG_CAPABILITY
)
2546 if (BGP_DEBUG (normal
, NORMAL
))
2547 plog_debug (peer
->log
,
2548 "%s unknown message type 0x%02x",
2550 bgp_notify_send_with_data (peer
,
2551 BGP_NOTIFY_HEADER_ERR
,
2552 BGP_NOTIFY_HEADER_BAD_MESTYPE
,
2556 /* Mimimum packet length check. */
2557 if ((size
< BGP_HEADER_SIZE
)
2558 || (size
> BGP_MAX_PACKET_SIZE
)
2559 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
2560 || (type
== BGP_MSG_UPDATE
&& size
< BGP_MSG_UPDATE_MIN_SIZE
)
2561 || (type
== BGP_MSG_NOTIFY
&& size
< BGP_MSG_NOTIFY_MIN_SIZE
)
2562 || (type
== BGP_MSG_KEEPALIVE
&& size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
2563 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2564 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
&& size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
2565 || (type
== BGP_MSG_CAPABILITY
&& size
< BGP_MSG_CAPABILITY_MIN_SIZE
))
2567 if (BGP_DEBUG (normal
, NORMAL
))
2568 plog_debug (peer
->log
,
2569 "%s bad message length - %d for %s",
2571 type
== 128 ? "ROUTE-REFRESH" :
2572 bgp_type_str
[(int) type
]);
2573 bgp_notify_send_with_data (peer
,
2574 BGP_NOTIFY_HEADER_ERR
,
2575 BGP_NOTIFY_HEADER_BAD_MESLEN
,
2576 (u_char
*) notify_data_length
, 2);
2580 /* Adjust size to message length. */
2581 peer
->packet_size
= size
;
2584 ret
= bgp_read_packet (peer
);
2588 /* Get size and type again. */
2589 size
= stream_getw_from (peer
->ibuf
, BGP_MARKER_SIZE
);
2590 type
= stream_getc_from (peer
->ibuf
, BGP_MARKER_SIZE
+ 2);
2592 /* BGP packet dump function. */
2593 bgp_dump_packet (peer
, type
, peer
->ibuf
);
2595 size
= (peer
->packet_size
- BGP_HEADER_SIZE
);
2597 /* Read rest of the packet and call each sort of packet routine */
2602 bgp_open_receive (peer
, size
); /* XXX return value ignored! */
2604 case BGP_MSG_UPDATE
:
2605 peer
->readtime
= bgp_recent_clock ();
2606 bgp_update_receive (peer
, size
);
2608 case BGP_MSG_NOTIFY
:
2609 bgp_notify_receive (peer
, size
);
2611 case BGP_MSG_KEEPALIVE
:
2612 peer
->readtime
= bgp_recent_clock ();
2613 bgp_keepalive_receive (peer
, size
);
2615 case BGP_MSG_ROUTE_REFRESH_NEW
:
2616 case BGP_MSG_ROUTE_REFRESH_OLD
:
2618 bgp_route_refresh_receive (peer
, size
);
2620 case BGP_MSG_CAPABILITY
:
2621 peer
->dynamic_cap_in
++;
2622 bgp_capability_receive (peer
, size
);
2626 /* Clear input buffer. */
2627 peer
->packet_size
= 0;
2629 stream_reset (peer
->ibuf
);
2632 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_ACCEPT_PEER
))
2634 if (BGP_DEBUG (events
, EVENTS
))
2635 zlog_debug ("%s [Event] Accepting BGP peer delete", peer
->host
);