K2.6 patches and update.
[tomato.git] / release / src / router / zebra / bgpd / bgp_packet.c
bloba1274d109783feb9814f52136dda2b4474f858e5
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
9 later version.
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
19 02111-1307, USA. */
21 #include <zebra.h>
23 #include "thread.h"
24 #include "stream.h"
25 #include "network.h"
26 #include "prefix.h"
27 #include "command.h"
28 #include "log.h"
29 #include "memory.h"
30 #include "sockunion.h" /* for inet_ntop () */
31 #include "linklist.h"
32 #include "plist.h"
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. */
54 static int
55 bgp_packet_set_marker (struct stream *s, u_char type)
57 int i;
59 /* Fill in marker. */
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. */
64 stream_putw (s, 0);
66 /* BGP packet type. */
67 stream_putc (s, type);
69 /* Return current stream size. */
70 return stream_get_putp (s);
73 /* Set BGP packet header size entry. If size is zero then use current
74 stream size. */
75 static int
76 bgp_packet_set_size (struct stream *s)
78 int cp;
80 /* Preserve current pointer. */
81 cp = stream_get_putp (s);
82 stream_set_putp (s, BGP_MARKER_SIZE);
83 stream_putw (s, cp);
85 /* Write back current pointer. */
86 stream_set_putp (s, cp);
88 return cp;
91 /* Add new packet to the peer. */
92 void
93 bgp_packet_add (struct peer *peer, struct stream *s)
95 /* Add packet to the end of list. */
96 stream_fifo_push (peer->obuf, s);
99 /* Free first packet. */
100 void
101 bgp_packet_delete (struct peer *peer)
103 stream_free (stream_fifo_pop (peer->obuf));
106 /* Duplicate packet. */
107 struct stream *
108 bgp_packet_dup (struct stream *s)
110 struct stream *new;
112 new = stream_new (stream_get_endp (s));
114 new->endp = s->endp;
115 new->putp = s->putp;
116 new->getp = s->getp;
118 memcpy (new->data, s->data, stream_get_endp (s));
120 return new;
123 /* Check file descriptor whether connect is established. */
124 static void
125 bgp_connect_check (struct peer *peer)
127 int status;
128 int slen;
129 int ret;
131 /* Anyway I have to reset read and write thread. */
132 BGP_READ_OFF (peer->t_read);
133 BGP_WRITE_OFF (peer->t_write);
135 /* Check file descriptor. */
136 slen = sizeof (status);
137 ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen);
139 /* If getsockopt is fail, this is fatal error. */
140 if (ret < 0)
142 zlog (peer->log, LOG_INFO, "can't get sockopt for nonblocking connect");
143 BGP_EVENT_ADD (peer, TCP_fatal_error);
144 return;
147 /* When status is 0 then TCP connection is established. */
148 if (status == 0)
150 BGP_EVENT_ADD (peer, TCP_connection_open);
152 else
154 if (BGP_DEBUG (events, EVENTS))
155 plog_info (peer->log, "%s [Event] Connect failed (%s)",
156 peer->host, strerror (errno));
157 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
161 /* Make BGP update packet. */
162 struct stream *
163 bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi)
165 struct stream *s;
166 struct bgp_adj_out *adj;
167 struct bgp_advertise *adv;
168 struct stream *packet;
169 struct bgp_node *rn = NULL;
170 struct bgp_info *binfo = NULL;
171 bgp_size_t total_attr_len = 0;
172 unsigned long pos;
173 char buf[BUFSIZ];
174 struct prefix_rd *prd = NULL;
175 char *tag = NULL;
177 s = peer->work;
178 stream_reset (s);
180 adv = FIFO_HEAD (&peer->sync[afi][safi]->update);
182 while (adv)
184 if (adv->rn)
185 rn = adv->rn;
186 adj = adv->adj;
187 if (adv->binfo)
188 binfo = adv->binfo;
189 #ifdef MPLS_VPN
190 if (rn)
191 prd = (struct prefix_rd *) &rn->prn->p;
192 if (binfo)
193 tag = binfo->tag;
194 #endif /* MPLS_VPN */
196 /* When remaining space can't include NLRI and it's length. */
197 if (rn && STREAM_REMAIN (s) <= BGP_NLRI_LENGTH + PSIZE (rn->p.prefixlen))
198 break;
200 /* If packet is empty, set attribute. */
201 if (stream_empty (s))
203 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
204 stream_putw (s, 0);
205 pos = stream_get_putp (s);
206 stream_putw (s, 0);
207 total_attr_len = bgp_packet_attribute (NULL, peer, s,
208 adv->baa->attr,
209 &rn->p, afi, safi,
210 binfo->peer, prd, tag);
211 stream_putw_at (s, pos, total_attr_len);
214 if (afi == AFI_IP && safi == SAFI_UNICAST)
215 stream_put_prefix (s, &rn->p);
217 if (BGP_DEBUG (update, UPDATE_OUT))
218 zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d",
219 peer->host,
220 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ),
221 rn->p.prefixlen);
223 /* Synchnorize attribute. */
224 if (adj->attr)
225 bgp_attr_unintern (adj->attr);
226 else
227 peer->scount[afi][safi]++;
229 adj->attr = bgp_attr_intern (adv->baa->attr);
231 adv = bgp_advertise_clean (peer, adj, afi, safi);
233 if (! (afi == AFI_IP && safi == SAFI_UNICAST))
234 break;
237 if (! stream_empty (s))
239 bgp_packet_set_size (s);
240 packet = bgp_packet_dup (s);
241 bgp_packet_add (peer, packet);
242 stream_reset (s);
243 return packet;
245 return NULL;
248 struct stream *
249 bgp_update_packet_eor (struct peer *peer, afi_t afi, safi_t safi)
251 struct stream *s;
252 struct stream *packet;
254 if (BGP_DEBUG (normal, NORMAL))
255 zlog_info ("Send End-of-RIB for AF %s to neighbor %s",
256 afi_safi_print(afi, safi), peer->host);
258 s = stream_new (BGP_MAX_PACKET_SIZE);
260 /* Make BGP update packet. */
261 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
263 /* Unfeasible Routes Length */
264 stream_putw (s, 0);
266 if (afi == AFI_IP && safi == SAFI_UNICAST)
268 /* Total Path Attribute Length */
269 stream_putw (s, 0);
271 else
273 /* Total Path Attribute Length */
274 stream_putw (s, 6);
275 stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
276 stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI);
277 stream_putc (s, 3);
278 stream_putw (s, afi);
279 stream_putc (s, safi);
282 bgp_packet_set_size (s);
283 packet = bgp_packet_dup (s);
284 bgp_packet_add (peer, packet);
285 stream_free (s);
286 return packet;
289 /* Make BGP withdraw packet. */
290 struct stream *
291 bgp_withdraw_packet (struct peer *peer, afi_t afi, safi_t safi)
293 struct stream *s;
294 struct stream *packet;
295 struct bgp_adj_out *adj;
296 struct bgp_advertise *adv;
297 struct bgp_node *rn;
298 unsigned long pos;
299 bgp_size_t unfeasible_len;
300 bgp_size_t total_attr_len;
301 char buf[BUFSIZ];
302 struct prefix_rd *prd = NULL;
304 s = peer->work;
305 stream_reset (s);
307 while ((adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL)
309 adj = adv->adj;
310 rn = adv->rn;
311 #ifdef MPLS_VPN
312 prd = (struct prefix_rd *) &rn->prn->p;
313 #endif /* MPLS_VPN */
315 if (STREAM_REMAIN (s)
316 < (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN + PSIZE (rn->p.prefixlen)))
317 break;
319 if (stream_empty (s))
321 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
322 stream_putw (s, 0);
325 if (afi == AFI_IP && safi == SAFI_UNICAST)
326 stream_put_prefix (s, &rn->p);
327 else
329 pos = stream_get_putp (s);
330 stream_putw (s, 0);
331 total_attr_len
332 = bgp_packet_withdraw (peer, s, &rn->p, afi, safi, prd, NULL);
334 /* Set total path attribute length. */
335 stream_putw_at (s, pos, total_attr_len);
338 if (BGP_DEBUG (update, UPDATE_OUT))
339 zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d -- unreachable",
340 peer->host,
341 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ),
342 rn->p.prefixlen);
344 peer->scount[afi][safi]--;
346 bgp_adj_out_remove (rn, adj, peer, afi, safi);
347 bgp_unlock_node (rn);
349 if (! (afi == AFI_IP && safi == SAFI_UNICAST))
350 break;
353 if (! stream_empty (s))
355 if (afi == AFI_IP && safi == SAFI_UNICAST)
357 unfeasible_len
358 = stream_get_putp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
359 stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
360 stream_putw (s, 0);
362 bgp_packet_set_size (s);
363 packet = bgp_packet_dup (s);
364 bgp_packet_add (peer, packet);
365 stream_reset (s);
366 return packet;
369 return NULL;
372 void
373 bgp_default_update_send (struct peer *peer, struct attr *attr,
374 afi_t afi, safi_t safi, struct peer *from)
376 struct stream *s;
377 struct stream *packet;
378 struct prefix p;
379 unsigned long pos;
380 bgp_size_t total_attr_len;
381 char attrstr[BUFSIZ];
382 char buf[BUFSIZ];
384 #ifdef DISABLE_BGP_ANNOUNCE
385 return;
386 #endif /* DISABLE_BGP_ANNOUNCE */
388 if (afi == AFI_IP)
389 str2prefix ("0.0.0.0/0", &p);
390 #ifdef HAVE_IPV6
391 else
392 str2prefix ("::/0", &p);
393 #endif /* HAVE_IPV6 */
395 /* Logging the attribute. */
396 if (BGP_DEBUG (update, UPDATE_OUT))
398 bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
399 zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d %s",
400 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ),
401 p.prefixlen, attrstr);
404 s = stream_new (BGP_MAX_PACKET_SIZE);
406 /* Make BGP update packet. */
407 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
409 /* Unfeasible Routes Length. */
410 stream_putw (s, 0);
412 /* Make place for total attribute length. */
413 pos = stream_get_putp (s);
414 stream_putw (s, 0);
415 total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &p, afi, safi, from, NULL, NULL);
417 /* Set Total Path Attribute Length. */
418 stream_putw_at (s, pos, total_attr_len);
420 /* NLRI set. */
421 if (p.family == AF_INET && safi == SAFI_UNICAST)
422 stream_put_prefix (s, &p);
424 /* Set size. */
425 bgp_packet_set_size (s);
427 packet = bgp_packet_dup (s);
428 stream_free (s);
430 /* Dump packet if debug option is set. */
431 #ifdef DEBUG
432 bgp_packet_dump (packet);
433 #endif /* DEBUG */
435 /* Add packet to the peer. */
436 bgp_packet_add (peer, packet);
438 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
441 void
442 bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi)
444 struct stream *s;
445 struct stream *packet;
446 struct prefix p;
447 unsigned long pos;
448 unsigned long cp;
449 bgp_size_t unfeasible_len;
450 bgp_size_t total_attr_len;
451 char buf[BUFSIZ];
453 #ifdef DISABLE_BGP_ANNOUNCE
454 return;
455 #endif /* DISABLE_BGP_ANNOUNCE */
457 if (afi == AFI_IP)
458 str2prefix ("0.0.0.0/0", &p);
459 #ifdef HAVE_IPV6
460 else
461 str2prefix ("::/0", &p);
462 #endif /* HAVE_IPV6 */
464 total_attr_len = 0;
465 pos = 0;
467 if (BGP_DEBUG (update, UPDATE_OUT))
468 zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d -- unreachable",
469 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ),
470 p.prefixlen);
472 s = stream_new (BGP_MAX_PACKET_SIZE);
474 /* Make BGP update packet. */
475 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
477 /* Unfeasible Routes Length. */;
478 cp = stream_get_putp (s);
479 stream_putw (s, 0);
481 /* Withdrawn Routes. */
482 if (p.family == AF_INET && safi == SAFI_UNICAST)
484 stream_put_prefix (s, &p);
486 unfeasible_len = stream_get_putp (s) - cp - 2;
488 /* Set unfeasible len. */
489 stream_putw_at (s, cp, unfeasible_len);
491 /* Set total path attribute length. */
492 stream_putw (s, 0);
494 else
496 pos = stream_get_putp (s);
497 stream_putw (s, 0);
498 total_attr_len = bgp_packet_withdraw (peer, s, &p, afi, safi, NULL, NULL);
500 /* Set total path attribute length. */
501 stream_putw_at (s, pos, total_attr_len);
504 bgp_packet_set_size (s);
506 packet = bgp_packet_dup (s);
507 stream_free (s);
509 /* Add packet to the peer. */
510 bgp_packet_add (peer, packet);
512 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
515 /* Get next packet to be written. */
516 struct stream *
517 bgp_write_packet (struct peer *peer)
519 afi_t afi;
520 safi_t safi;
521 struct stream *s = NULL;
522 struct bgp_advertise *adv;
524 s = stream_fifo_head (peer->obuf);
525 if (s)
526 return s;
528 for (afi = AFI_IP; afi < AFI_MAX; afi++)
529 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
531 adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw);
532 if (adv)
534 s = bgp_withdraw_packet (peer, afi, safi);
535 if (s)
536 return s;
540 for (afi = AFI_IP; afi < AFI_MAX; afi++)
541 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
543 adv = FIFO_HEAD (&peer->sync[afi][safi]->update);
544 if (adv)
546 if (adv->binfo && adv->binfo->uptime < peer->synctime[afi][safi])
548 if (CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_RCV)
549 && CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_ADV)
550 && ! CHECK_FLAG (adv->binfo->flags, BGP_INFO_STALE)
551 && safi != SAFI_MPLS_VPN)
553 if (CHECK_FLAG (adv->binfo->peer->af_sflags[afi][safi],
554 PEER_STATUS_EOR_RECEIVED))
555 s = bgp_update_packet (peer, afi, safi);
557 else
558 s = bgp_update_packet (peer, afi, safi);
561 if (s)
562 return s;
565 if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV))
567 if (peer->afc_nego[afi][safi] && peer->synctime[afi][safi]
568 && ! CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND)
569 && safi != SAFI_MPLS_VPN)
571 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND);
572 return bgp_update_packet_eor (peer, afi, safi);
577 return NULL;
580 /* Is there partially written packet or updates we can send right
581 now. */
583 bgp_write_proceed (struct peer *peer)
585 afi_t afi;
586 safi_t safi;
587 struct bgp_advertise *adv;
589 if (stream_fifo_head (peer->obuf))
590 return 1;
592 for (afi = AFI_IP; afi < AFI_MAX; afi++)
593 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
594 if (FIFO_HEAD (&peer->sync[afi][safi]->withdraw))
595 return 1;
597 for (afi = AFI_IP; afi < AFI_MAX; afi++)
598 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
599 if ((adv = FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL)
600 if (adv->binfo->uptime < peer->synctime[afi][safi])
601 return 1;
603 return 0;
606 /* Write packet to the peer. */
608 bgp_write (struct thread *thread)
610 struct peer *peer;
611 u_char type;
612 struct stream *s;
613 int num;
614 int count = 0;
615 int write_errno;
617 /* Yes first of all get peer pointer. */
618 peer = THREAD_ARG (thread);
619 peer->t_write = NULL;
621 /* For non-blocking IO check. */
622 if (peer->status == Connect)
624 bgp_connect_check (peer);
625 return 0;
628 /* Nonblocking write until TCP output buffer is full. */
629 while (1)
631 int writenum;
633 s = bgp_write_packet (peer);
634 if (! s)
635 return 0;
637 /* Number of bytes to be sent. */
638 writenum = stream_get_endp (s) - stream_get_getp (s);
640 /* Call write() system call. */
641 num = write (peer->fd, STREAM_PNT (s), writenum);
642 write_errno = errno;
643 if (num <= 0)
645 /* Partial write. */
646 if (write_errno == EWOULDBLOCK || write_errno == EAGAIN)
647 break;
649 bgp_stop (peer);
650 peer->status = Idle;
651 bgp_timer_set (peer);
652 return 0;
654 if (num != writenum)
656 stream_forward (s, num);
658 if (write_errno == EAGAIN)
659 break;
661 continue;
664 /* Retrieve BGP packet type. */
665 stream_set_getp (s, BGP_MARKER_SIZE + 2);
666 type = stream_getc (s);
668 switch (type)
670 case BGP_MSG_OPEN:
671 peer->open_out++;
672 break;
673 case BGP_MSG_UPDATE:
674 peer->update_out++;
675 break;
676 case BGP_MSG_NOTIFY:
677 peer->notify_out++;
679 /* BGP_EVENT_ADD (peer, BGP_Stop); */
680 bgp_stop_with_error (peer);
681 bgp_fsm_change_status (peer, Idle);
682 bgp_timer_set (peer);
683 return 0;
684 break;
685 case BGP_MSG_KEEPALIVE:
686 peer->keepalive_out++;
687 break;
688 case BGP_MSG_ROUTE_REFRESH_NEW:
689 case BGP_MSG_ROUTE_REFRESH_OLD:
690 peer->refresh_out++;
691 break;
692 case BGP_MSG_CAPABILITY:
693 peer->dynamic_cap_out++;
694 break;
697 /* OK we send packet so delete it. */
698 bgp_packet_delete (peer);
700 if (++count >= BGP_WRITE_PACKET_MAX)
701 break;
704 if (bgp_write_proceed (peer))
705 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
707 return 0;
710 /* This is only for sending NOTIFICATION message to neighbor. */
712 bgp_write_notify (struct peer *peer)
714 int ret;
715 u_char type;
716 struct stream *s;
718 /* There should be at least one packet. */
719 s = stream_fifo_head (peer->obuf);
720 if (!s)
721 return 0;
722 assert (stream_get_endp (s) >= BGP_HEADER_SIZE);
724 /* I'm not sure fd is writable. */
725 ret = writen (peer->fd, STREAM_DATA (s), stream_get_endp (s));
726 if (ret <= 0)
728 bgp_stop (peer);
729 peer->status = Idle;
730 bgp_timer_set (peer);
731 return 0;
734 /* Retrieve BGP packet type. */
735 stream_set_getp (s, BGP_MARKER_SIZE + 2);
736 type = stream_getc (s);
738 assert (type == BGP_MSG_NOTIFY);
740 /* Type should be notify. */
741 peer->notify_out++;
743 /* We don't call event manager at here for avoiding other events. */
744 if (peer->status == Established)
745 bgp_stop (peer);
746 else
748 bgp_stop_with_error (peer);
749 bgp_fsm_change_status (peer, Idle);
751 bgp_timer_set (peer);
753 return 0;
756 /* Make keepalive packet and send it to the peer. */
757 void
758 bgp_keepalive_send (struct peer *peer)
760 struct stream *s;
761 int length;
763 s = stream_new (BGP_MAX_PACKET_SIZE);
765 /* Make keepalive packet. */
766 bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE);
768 /* Set packet size. */
769 length = bgp_packet_set_size (s);
771 /* Dump packet if debug option is set. */
772 /* bgp_packet_dump (s); */
774 if (BGP_DEBUG (keepalive, KEEPALIVE))
775 zlog_info ("%s sending KEEPALIVE", peer->host);
777 /* Add packet to the peer. */
778 bgp_packet_add (peer, s);
780 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
783 /* Make open packet and send it to the peer. */
784 void
785 bgp_open_send (struct peer *peer)
787 struct stream *s;
788 int length;
789 u_int16_t send_holdtime;
790 as_t local_as;
792 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
793 send_holdtime = peer->holdtime;
794 else
795 send_holdtime = peer->bgp->default_holdtime;
797 /* local-as Change */
798 if (peer->change_local_as)
799 local_as = peer->change_local_as;
800 else
801 local_as = peer->local_as;
803 s = stream_new (BGP_MAX_PACKET_SIZE);
805 /* Make open packet. */
806 bgp_packet_set_marker (s, BGP_MSG_OPEN);
808 /* Set open packet values. */
809 stream_putc (s, BGP_VERSION_4); /* BGP version */
810 stream_putw (s, local_as); /* My Autonomous System*/
811 stream_putw (s, send_holdtime); /* Hold Time */
812 stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */
814 /* Set capability code. */
815 bgp_open_capability (s, peer);
817 /* Set BGP packet length. */
818 length = bgp_packet_set_size (s);
820 if (BGP_DEBUG (normal, NORMAL))
821 zlog_info ("%s sending OPEN, version %d, my as %d, holdtime %d, id %s",
822 peer->host, BGP_VERSION_4, local_as,
823 send_holdtime, inet_ntoa (peer->local_id));
825 if (BGP_DEBUG (normal, NORMAL))
826 zlog_info ("%s send message type %d, length (incl. header) %d",
827 peer->host, BGP_MSG_OPEN, length);
829 /* Dump packet if debug option is set. */
830 /* bgp_packet_dump (s); */
832 /* Add packet to the peer. */
833 bgp_packet_add (peer, s);
835 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
838 /* Send BGP notify packet with data potion. */
839 void
840 bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code,
841 u_char *data, size_t datalen)
843 struct stream *s;
844 int length;
846 /* Allocate new stream. */
847 s = stream_new (BGP_MAX_PACKET_SIZE);
849 /* Make nitify packet. */
850 bgp_packet_set_marker (s, BGP_MSG_NOTIFY);
852 /* Set notify packet values. */
853 stream_putc (s, code); /* BGP notify code */
854 stream_putc (s, sub_code); /* BGP notify sub_code */
856 /* If notify data is present. */
857 if (data)
858 stream_write (s, data, datalen);
860 /* Set BGP packet length. */
861 length = bgp_packet_set_size (s);
863 /* Add packet to the peer. */
864 stream_fifo_clean (peer->obuf);
865 bgp_packet_add (peer, s);
867 /* For debug */
869 struct bgp_notify bgp_notify;
870 int first = 0;
871 int i;
872 char c[4];
874 bgp_notify.code = code;
875 bgp_notify.subcode = sub_code;
876 bgp_notify.data = NULL;
877 bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE;
879 if (bgp_notify.length)
881 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
882 for (i = 0; i < bgp_notify.length; i++)
883 if (first)
885 sprintf (c, " %02x", data[i]);
886 strcat (bgp_notify.data, c);
888 else
890 first = 1;
891 sprintf (c, "%02x", data[i]);
892 strcpy (bgp_notify.data, c);
895 bgp_notify_print (peer, &bgp_notify, "sending");
896 if (bgp_notify.data)
897 XFREE (MTYPE_TMP, bgp_notify.data);
900 if (BGP_DEBUG (normal, NORMAL))
901 zlog_info ("%s send message type %d, length (incl. header) %d",
902 peer->host, BGP_MSG_NOTIFY, length);
904 /* peer reset cause */
905 if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE)
907 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
908 peer->last_reset = PEER_DOWN_USER_RESET;
909 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
910 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
911 else if (sub_code == BGP_NOTIFY_CEASE_PEER_UNCONFIG)
912 peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE;
913 else
914 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
916 /* Call imidiately. */
917 BGP_WRITE_OFF (peer->t_write);
919 bgp_write_notify (peer);
922 /* Send BGP notify packet. */
923 void
924 bgp_notify_send (struct peer *peer, u_char code, u_char sub_code)
926 bgp_notify_send_with_data (peer, code, sub_code, NULL, 0);
929 char *
930 afi2str (afi_t afi)
932 if (afi == AFI_IP)
933 return "AFI_IP";
934 else if (afi == AFI_IP6)
935 return "AFI_IP6";
936 else
937 return "Unknown AFI";
940 char *
941 safi2str (safi_t safi)
943 if (safi == SAFI_UNICAST)
944 return "SAFI_UNICAST";
945 else if (safi == SAFI_MULTICAST)
946 return "SAFI_MULTICAST";
947 else if (safi == SAFI_MPLS_VPN || safi == BGP_SAFI_VPNV4)
948 return "SAFI_MPLS_VPN";
949 else
950 return "Unknown SAFI";
953 /* Send route refresh message to the peer. */
954 void
955 bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi,
956 u_char orf_type, u_char when_to_refresh, int remove)
958 struct stream *s;
959 struct stream *packet;
960 int length;
961 struct bgp_filter *filter;
962 int orf_refresh = 0;
963 int msg_type;
965 #ifdef DISABLE_BGP_ANNOUNCE
966 return;
967 #endif /* DISABLE_BGP_ANNOUNCE */
969 if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
970 msg_type = BGP_MSG_ROUTE_REFRESH_NEW;
971 else
972 msg_type = BGP_MSG_ROUTE_REFRESH_OLD;
973 filter = &peer->filter[afi][safi];
975 /* Adjust safi code. */
976 if (safi == SAFI_MPLS_VPN)
977 safi = BGP_SAFI_VPNV4;
979 s = stream_new (BGP_MAX_PACKET_SIZE);
981 /* Make BGP update packet. */
982 bgp_packet_set_marker (s, msg_type);
984 /* Encode Route Refresh message. */
985 stream_putw (s, afi);
986 stream_putc (s, 0);
987 stream_putc (s, safi);
989 if (orf_type == ORF_TYPE_PREFIX
990 || orf_type == ORF_TYPE_PREFIX_OLD)
991 if (remove || filter->plist[FILTER_IN].plist)
993 u_int16_t orf_len;
994 unsigned long orfp;
996 orf_refresh = 1;
997 stream_putc (s, when_to_refresh);
998 stream_putc (s, orf_type);
999 orfp = stream_get_putp (s);
1000 stream_putw (s, 0);
1002 if (remove)
1004 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
1005 stream_putc (s, ORF_COMMON_PART_REMOVE_ALL);
1006 if (BGP_DEBUG (normal, NORMAL))
1007 zlog_info ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
1008 peer->host, orf_type,
1009 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
1010 afi, safi);
1012 else
1014 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
1015 prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist,
1016 ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT,
1017 ORF_COMMON_PART_DENY);
1018 if (BGP_DEBUG (normal, NORMAL))
1019 zlog_info ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
1020 peer->host, orf_type,
1021 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
1022 afi, safi);
1025 /* Total ORF Entry Len. */
1026 orf_len = stream_get_putp (s) - orfp - 2;
1027 stream_putw_at (s, orfp, orf_len);
1030 /* Set packet size. */
1031 length = bgp_packet_set_size (s);
1033 if (BGP_DEBUG (normal, NORMAL))
1035 if (! orf_refresh)
1036 zlog_info ("%s sending REFRESH_REQ(%d) for afi/safi: %d/%d",
1037 peer->host, msg_type, afi, safi);
1038 zlog_info ("%s send message type %d, length (incl. header) %d",
1039 peer->host, msg_type, length);
1042 /* Make real packet. */
1043 packet = bgp_packet_dup (s);
1044 stream_free (s);
1046 /* Add packet to the peer. */
1047 bgp_packet_add (peer, packet);
1049 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
1052 /* Send capability message to the peer. */
1053 void
1054 bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi,
1055 int capability_code, int action)
1057 struct stream *s;
1058 struct stream *packet;
1059 int length;
1061 /* Adjust safi code. */
1062 if (safi == SAFI_MPLS_VPN)
1063 safi = BGP_SAFI_VPNV4;
1065 s = stream_new (BGP_MAX_PACKET_SIZE);
1067 /* Make BGP update packet. */
1068 bgp_packet_set_marker (s, BGP_MSG_CAPABILITY);
1070 /* Encode MP_EXT capability. */
1071 if (capability_code == CAPABILITY_CODE_MP)
1073 stream_putc (s, action);
1074 stream_putc (s, CAPABILITY_CODE_MP);
1075 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1076 stream_putw (s, afi);
1077 stream_putc (s, 0);
1078 stream_putc (s, safi);
1080 if (BGP_DEBUG (normal, NORMAL))
1081 zlog_info ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
1082 peer->host, action == CAPABILITY_ACTION_SET ?
1083 "Advertising" : "Removing", afi, safi);
1086 /* Set packet size. */
1087 length = bgp_packet_set_size (s);
1089 /* Make real packet. */
1090 packet = bgp_packet_dup (s);
1091 stream_free (s);
1093 /* Add packet to the peer. */
1094 bgp_packet_add (peer, packet);
1096 if (BGP_DEBUG (normal, NORMAL))
1097 zlog_info ("%s send message type %d, length (incl. header) %d",
1098 peer->host, BGP_MSG_CAPABILITY, length);
1100 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
1103 /* RFC1771 6.8 Connection collision detection. */
1105 bgp_collision_detect (struct peer *new, struct in_addr remote_id)
1107 struct peer *peer;
1108 struct listnode *nn;
1109 struct bgp *bgp;
1111 bgp = bgp_get_default ();
1112 if (! bgp)
1113 return 0;
1115 /* Upon receipt of an OPEN message, the local system must examine
1116 all of its connections that are in the OpenConfirm state. A BGP
1117 speaker may also examine connections in an OpenSent state if it
1118 knows the BGP Identifier of the peer by means outside of the
1119 protocol. If among these connections there is a connection to a
1120 remote BGP speaker whose BGP Identifier equals the one in the
1121 OPEN message, then the local system performs the following
1122 collision resolution procedure: */
1124 LIST_LOOP (bgp->peer, peer, nn)
1126 /* Under OpenConfirm status, local peer structure already hold
1127 remote router ID. */
1129 if (peer != new
1130 && (peer->status == OpenConfirm || peer->status == OpenSent)
1131 && sockunion_same (&peer->su, &new->su))
1133 /* 1. The BGP Identifier of the local system is compared to
1134 the BGP Identifier of the remote system (as specified in
1135 the OPEN message). */
1137 if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr))
1139 /* 2. If the value of the local BGP Identifier is less
1140 than the remote one, the local system closes BGP
1141 connection that already exists (the one that is
1142 already in the OpenConfirm state), and accepts BGP
1143 connection initiated by the remote system. */
1145 if (peer->fd >= 0)
1146 bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONNECT_COLLISION);
1147 return 1;
1149 else
1151 /* 3. Otherwise, the local system closes newly created
1152 BGP connection (the one associated with the newly
1153 received OPEN message), and continues to use the
1154 existing one (the one that is already in the
1155 OpenConfirm state). */
1157 if (new->fd >= 0)
1158 bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONNECT_COLLISION);
1159 return -1;
1163 return 0;
1167 bgp_open_receive (struct peer *peer, bgp_size_t size)
1169 int ret;
1170 u_char version;
1171 u_char optlen;
1172 u_int16_t holdtime;
1173 u_int16_t send_holdtime;
1174 as_t remote_as;
1175 struct peer *realpeer;
1176 struct in_addr remote_id;
1177 int capability;
1178 char notify_data_remote_as[2];
1179 char notify_data_remote_id[4];
1181 realpeer = NULL;
1183 /* Parse open packet. */
1184 version = stream_getc (peer->ibuf);
1185 memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2);
1186 remote_as = stream_getw (peer->ibuf);
1187 holdtime = stream_getw (peer->ibuf);
1188 memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
1189 remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
1191 /* Receive OPEN message log */
1192 if (BGP_DEBUG (normal, NORMAL))
1193 zlog_info ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s",
1194 peer->host, version, remote_as, holdtime,
1195 inet_ntoa (remote_id));
1197 /* Lookup peer from Open packet. */
1198 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1200 int as = 0;
1202 realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as);
1204 if (! realpeer)
1206 /* Peer's source IP address is check in bgp_accept(), so this
1207 must be AS number mismatch or remote-id configuration
1208 mismatch. */
1209 if (as)
1211 if (BGP_DEBUG (normal, NORMAL))
1212 zlog_info ("%s bad OPEN, wrong router identifier %s",
1213 peer->host, inet_ntoa (remote_id));
1214 bgp_notify_send_with_data (peer,
1215 BGP_NOTIFY_OPEN_ERR,
1216 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1217 notify_data_remote_id, 4);
1219 else
1221 if (BGP_DEBUG (normal, NORMAL))
1222 zlog_info ("%s bad OPEN, remote AS is %d, expected %d",
1223 peer->host, remote_as, peer->as);
1224 bgp_notify_send_with_data (peer,
1225 BGP_NOTIFY_OPEN_ERR,
1226 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1227 notify_data_remote_as, 2);
1229 return -1;
1233 /* When collision is detected and this peer is closed. Retrun
1234 immidiately. */
1235 ret = bgp_collision_detect (peer, remote_id);
1236 if (ret < 0)
1237 return ret;
1239 /* Hack part. */
1240 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1242 if (CHECK_FLAG (realpeer->flags, PEER_FLAG_CONNECT_MODE_ACTIVE))
1244 if (BGP_DEBUG (normal, NORMAL))
1245 zlog_info ("%s passive open failed - TCP session must be opened actively",
1246 realpeer->host);
1247 bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONNECT_REJECT);
1248 return -1;
1251 if (realpeer->status == Established
1252 && CHECK_FLAG (realpeer->sflags, PEER_STATUS_NSF_MODE))
1254 realpeer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
1255 SET_FLAG (realpeer->sflags, PEER_STATUS_NSF_WAIT);
1257 else if (ret == 0 && realpeer->status != Active
1258 && realpeer->status != OpenSent
1259 && realpeer->status != OpenConfirm)
1261 if (BGP_DEBUG (events, EVENTS))
1262 zlog_info ("%s peer status is %s close connection",
1263 realpeer->host, LOOKUP (bgp_status_msg, realpeer->status));
1264 bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONNECT_REJECT);
1265 return -1;
1268 if (BGP_DEBUG (events, EVENTS))
1269 zlog_info ("%s [Event] Transfer temporary BGP peer to existing one",
1270 peer->host);
1272 bgp_stop (realpeer);
1274 /* Transfer file descriptor. */
1275 realpeer->fd = peer->fd;
1276 peer->fd = -1;
1278 /* Transfer input buffer. */
1279 stream_free (realpeer->ibuf);
1280 realpeer->ibuf = peer->ibuf;
1281 realpeer->packet_size = peer->packet_size;
1282 peer->ibuf = NULL;
1284 /* Transfer status. */
1285 bgp_fsm_change_status (realpeer, peer->status);
1286 bgp_stop (peer);
1288 /* peer pointer change. Open packet send to neighbor. */
1289 peer = realpeer;
1290 bgp_open_send (peer);
1291 if (peer->fd < 0)
1293 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1294 peer->fd);
1295 return -1;
1297 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
1300 /* remote router-id check. */
1301 if (remote_id.s_addr == 0
1302 || ntohl (remote_id.s_addr) >= 0xe0000000
1303 || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
1305 if (BGP_DEBUG (normal, NORMAL))
1306 zlog_info ("%s bad OPEN, wrong router identifier %s",
1307 peer->host, inet_ntoa (remote_id));
1308 bgp_notify_send_with_data (peer,
1309 BGP_NOTIFY_OPEN_ERR,
1310 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1311 notify_data_remote_id, 4);
1312 return -1;
1315 /* Set remote router-id */
1316 peer->remote_id = remote_id;
1318 /* Peer BGP version check. */
1319 if (version != BGP_VERSION_4)
1321 if (BGP_DEBUG (normal, NORMAL))
1322 zlog_info ("%s bad protocol version, remote requested %d, local request %d",
1323 peer->host, version, BGP_VERSION_4);
1324 bgp_notify_send_with_data (peer,
1325 BGP_NOTIFY_OPEN_ERR,
1326 BGP_NOTIFY_OPEN_UNSUP_VERSION,
1327 "\x04", 1);
1328 return -1;
1331 /* Check neighbor as number. */
1332 if (remote_as != peer->as)
1334 if (BGP_DEBUG (normal, NORMAL))
1335 zlog_info ("%s bad OPEN, remote AS is %d, expected %d",
1336 peer->host, remote_as, peer->as);
1337 bgp_notify_send_with_data (peer,
1338 BGP_NOTIFY_OPEN_ERR,
1339 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1340 notify_data_remote_as, 2);
1341 return -1;
1344 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1345 calculate the value of the Hold Timer by using the smaller of its
1346 configured Hold Time and the Hold Time received in the OPEN message.
1347 The Hold Time MUST be either zero or at least three seconds. An
1348 implementation may reject connections on the basis of the Hold Time. */
1350 if (holdtime < 3 && holdtime != 0)
1352 bgp_notify_send (peer,
1353 BGP_NOTIFY_OPEN_ERR,
1354 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
1355 return -1;
1358 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1359 would be one third of the Hold Time interval. KEEPALIVE messages
1360 MUST NOT be sent more frequently than one per second. An
1361 implementation MAY adjust the rate at which it sends KEEPALIVE
1362 messages as a function of the Hold Time interval. */
1364 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
1365 send_holdtime = peer->holdtime;
1366 else
1367 send_holdtime = peer->bgp->default_holdtime;
1369 if (holdtime < send_holdtime)
1370 peer->v_holdtime = holdtime;
1371 else
1372 peer->v_holdtime = send_holdtime;
1374 peer->v_keepalive = peer->v_holdtime / 3;
1376 /* Open option part parse. */
1377 capability = 0;
1378 optlen = stream_getc (peer->ibuf);
1379 if (optlen != 0)
1381 ret = bgp_open_option_parse (peer, optlen, &capability);
1382 if (ret < 0)
1383 return ret;
1385 stream_forward (peer->ibuf, optlen);
1387 else
1389 if (BGP_DEBUG (normal, NORMAL))
1390 zlog_info ("%s rcvd OPEN w/ OPTION parameter len: 0",
1391 peer->host);
1394 /* Override capability. */
1395 if (! capability || CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1397 peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
1398 peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
1399 peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
1400 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
1403 /* Get sockname. */
1404 bgp_getsockname (peer);
1406 BGP_EVENT_ADD (peer, Receive_OPEN_message);
1408 peer->packet_size = 0;
1409 if (peer->ibuf)
1410 stream_reset (peer->ibuf);
1412 return 0;
1415 /* Parse BGP Update packet and make attribute object. */
1417 bgp_update_receive (struct peer *peer, bgp_size_t size)
1419 int ret;
1420 u_char *end;
1421 struct stream *s;
1422 struct attr attr;
1423 bgp_size_t attribute_len;
1424 bgp_size_t update_len;
1425 bgp_size_t withdraw_len;
1426 struct bgp_nlri update;
1427 struct bgp_nlri withdraw;
1428 struct bgp_nlri mp_update;
1429 struct bgp_nlri mp_withdraw;
1430 char attrstr[BUFSIZ] = "";
1432 /* Status must be Established. */
1433 if (peer->status != Established)
1435 zlog_err ("%s [FSM] Update packet received under status %s",
1436 peer->host, LOOKUP (bgp_status_msg, peer->status));
1437 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1438 return -1;
1441 /* Set initial values. */
1442 memset (&attr, 0, sizeof (struct attr));
1443 memset (&update, 0, sizeof (struct bgp_nlri));
1444 memset (&withdraw, 0, sizeof (struct bgp_nlri));
1445 memset (&mp_update, 0, sizeof (struct bgp_nlri));
1446 memset (&mp_withdraw, 0, sizeof (struct bgp_nlri));
1448 s = peer->ibuf;
1449 end = stream_pnt (s) + size;
1451 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1452 Length is too large (i.e., if Unfeasible Routes Length + Total
1453 Attribute Length + 23 exceeds the message Length), then the Error
1454 Subcode is set to Malformed Attribute List. */
1455 if (stream_pnt (s) + 2 > end)
1457 zlog_err ("%s [Error] Update packet error"
1458 " (packet length is short for unfeasible length)",
1459 peer->host);
1460 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1461 BGP_NOTIFY_UPDATE_MAL_ATTR);
1462 return -1;
1465 /* Unfeasible Route Length. */
1466 withdraw_len = stream_getw (s);
1468 /* Unfeasible Route Length check. */
1469 if (stream_pnt (s) + withdraw_len > end)
1471 zlog_err ("%s [Error] Update packet error"
1472 " (packet unfeasible length overflow %d)",
1473 peer->host, withdraw_len);
1474 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1475 BGP_NOTIFY_UPDATE_MAL_ATTR);
1476 return -1;
1479 /* Unfeasible Route packet format check. */
1480 if (withdraw_len > 0)
1482 ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len);
1483 if (ret < 0)
1484 return -1;
1486 if (BGP_DEBUG (packet, PACKET_RECV))
1487 zlog_info ("%s [Update:RECV] Unfeasible NLRI received", peer->host);
1489 withdraw.afi = AFI_IP;
1490 withdraw.safi = SAFI_UNICAST;
1491 withdraw.nlri = stream_pnt (s);
1492 withdraw.length = withdraw_len;
1493 stream_forward (s, withdraw_len);
1496 /* Attribute total length check. */
1497 if (stream_pnt (s) + 2 > end)
1499 zlog_warn ("%s [Error] Packet Error"
1500 " (update packet is short for attribute length)",
1501 peer->host);
1502 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1503 BGP_NOTIFY_UPDATE_MAL_ATTR);
1504 return -1;
1507 /* Fetch attribute total length. */
1508 attribute_len = stream_getw (s);
1510 /* Attribute length check. */
1511 if (stream_pnt (s) + attribute_len > end)
1513 zlog_warn ("%s [Error] Packet Error"
1514 " (update packet attribute length overflow %d)",
1515 peer->host, attribute_len);
1516 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1517 BGP_NOTIFY_UPDATE_MAL_ATTR);
1518 return -1;
1521 /* Parse attribute when it exists. */
1522 if (attribute_len)
1524 ret = bgp_attr_parse (peer, &attr, attribute_len,
1525 &mp_update, &mp_withdraw);
1526 if (ret < 0)
1527 return -1;
1530 /* Logging the attribute. */
1531 if (BGP_DEBUG (update, UPDATE_IN))
1533 ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
1535 if (ret)
1536 zlog (peer->log, LOG_INFO, "%s rcvd UPDATE w/ attr: %s",
1537 peer->host, attrstr);
1540 /* Network Layer Reachability Information. */
1541 update_len = end - stream_pnt (s);
1543 if (update_len)
1545 /* Check NLRI packet format and prefix length. */
1546 ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len);
1547 if (ret < 0)
1548 return -1;
1550 /* Set NLRI portion to structure. */
1551 update.afi = AFI_IP;
1552 update.safi = SAFI_UNICAST;
1553 update.nlri = stream_pnt (s);
1554 update.length = update_len;
1555 stream_forward (s, update_len);
1558 /* NLRI is processed only when the peer is configured specific
1559 Address Family and Subsequent Address Family. */
1560 if (peer->afc[AFI_IP][SAFI_UNICAST])
1562 if (withdraw.length)
1563 bgp_nlri_parse (peer, NULL, &withdraw);
1565 if (update.length)
1567 /* We check well-known attribute only for IPv4 unicast
1568 update. */
1569 ret = bgp_attr_check (peer, &attr);
1570 if (ret < 0)
1571 return -1;
1573 bgp_nlri_parse (peer, &attr, &update);
1576 if (mp_update.length
1577 && mp_update.afi == AFI_IP
1578 && mp_update.safi == SAFI_UNICAST)
1579 bgp_nlri_parse (peer, &attr, &mp_update);
1581 if (mp_withdraw.length
1582 && mp_withdraw.afi == AFI_IP
1583 && mp_withdraw.safi == SAFI_UNICAST)
1584 bgp_nlri_parse (peer, NULL, &mp_withdraw);
1586 if (! attribute_len && ! withdraw_len)
1588 /* End-of-RIB received */
1589 SET_FLAG (peer->af_sflags[AFI_IP][SAFI_UNICAST], PEER_STATUS_EOR_RECEIVED);
1591 if (BGP_DEBUG (normal, NORMAL))
1592 zlog_info ("neighbor %s sent End-of-RIB marker for IPv4 Unicast",
1593 peer->host);
1595 /* NSF delete stale route */
1596 if (peer->nsf[AFI_IP][SAFI_UNICAST])
1597 bgp_clear_stale_route (peer, AFI_IP, SAFI_UNICAST);
1600 if (peer->afc[AFI_IP][SAFI_MULTICAST])
1602 if (mp_update.length
1603 && mp_update.afi == AFI_IP
1604 && mp_update.safi == SAFI_MULTICAST)
1605 bgp_nlri_parse (peer, &attr, &mp_update);
1607 if (mp_withdraw.length
1608 && mp_withdraw.afi == AFI_IP
1609 && mp_withdraw.safi == SAFI_MULTICAST)
1610 bgp_nlri_parse (peer, NULL, &mp_withdraw);
1612 if (! withdraw_len
1613 && mp_withdraw.afi == AFI_IP
1614 && mp_withdraw.safi == SAFI_MULTICAST
1615 && mp_withdraw.length == 0)
1617 /* End-of-RIB received */
1618 SET_FLAG (peer->af_sflags[AFI_IP][SAFI_MULTICAST], PEER_STATUS_EOR_RECEIVED);
1620 if (BGP_DEBUG (normal, NORMAL))
1621 zlog_info ("neighbor %s sent End-of-RIB marker for IPv4 Multicast",
1622 peer->host);
1624 /* NSF delete stale route */
1625 if (peer->nsf[AFI_IP][SAFI_MULTICAST])
1626 bgp_clear_stale_route (peer, AFI_IP, SAFI_MULTICAST);
1629 if (peer->afc[AFI_IP6][SAFI_UNICAST])
1631 if (mp_update.length
1632 && mp_update.afi == AFI_IP6
1633 && mp_update.safi == SAFI_UNICAST)
1634 bgp_nlri_parse (peer, &attr, &mp_update);
1636 if (mp_withdraw.length
1637 && mp_withdraw.afi == AFI_IP6
1638 && mp_withdraw.safi == SAFI_UNICAST)
1639 bgp_nlri_parse (peer, NULL, &mp_withdraw);
1641 if (! withdraw_len
1642 && mp_withdraw.afi == AFI_IP6
1643 && mp_withdraw.safi == SAFI_UNICAST
1644 && mp_withdraw.length == 0)
1646 /* End-of-RIB received */
1647 SET_FLAG (peer->af_sflags[AFI_IP6][SAFI_UNICAST], PEER_STATUS_EOR_RECEIVED);
1649 if (BGP_DEBUG (normal, NORMAL))
1650 zlog_info ("neighbor %s sent End-of-RIB marker for IPv6 Unicast",
1651 peer->host);
1653 /* NSF delete stale route */
1654 if (peer->nsf[AFI_IP6][SAFI_UNICAST])
1655 bgp_clear_stale_route (peer, AFI_IP6, SAFI_UNICAST);
1658 if (peer->afc[AFI_IP6][SAFI_MULTICAST])
1660 if (mp_update.length
1661 && mp_update.afi == AFI_IP6
1662 && mp_update.safi == SAFI_MULTICAST)
1663 bgp_nlri_parse (peer, &attr, &mp_update);
1665 if (mp_withdraw.length
1666 && mp_withdraw.afi == AFI_IP6
1667 && mp_withdraw.safi == SAFI_MULTICAST)
1668 bgp_nlri_parse (peer, NULL, &mp_withdraw);
1670 if (! withdraw_len
1671 && mp_withdraw.afi == AFI_IP6
1672 && mp_withdraw.safi == SAFI_MULTICAST
1673 && mp_withdraw.length == 0)
1675 /* End-of-RIB received */
1676 SET_FLAG (peer->af_sflags[AFI_IP6][SAFI_MULTICAST], PEER_STATUS_EOR_RECEIVED);
1678 if (BGP_DEBUG (normal, NORMAL))
1679 zlog_info ("neighbor %s sent End-of-RIB marker for IPv6 Multicast",
1680 peer->host);
1682 /* NSF delete stale route */
1683 if (peer->nsf[AFI_IP6][SAFI_MULTICAST])
1684 bgp_clear_stale_route (peer, AFI_IP6, SAFI_MULTICAST);
1687 if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
1689 if (mp_update.length
1690 && mp_update.afi == AFI_IP
1691 && mp_update.safi == BGP_SAFI_VPNV4)
1692 bgp_nlri_parse_vpnv4 (peer, &attr, &mp_update);
1694 if (mp_withdraw.length
1695 && mp_withdraw.afi == AFI_IP
1696 && mp_withdraw.safi == BGP_SAFI_VPNV4)
1697 bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw);
1699 if (! withdraw_len
1700 && mp_withdraw.afi == AFI_IP
1701 && mp_withdraw.safi == BGP_SAFI_VPNV4
1702 && mp_withdraw.length == 0)
1704 /* End-of-RIB received */
1706 if (BGP_DEBUG (normal, NORMAL))
1707 zlog_info ("neighbor %s sent End-of-RIB marker for VPNv4 Unicast",
1708 peer->host);
1712 /* Everything is done. We unintern temporary structures which
1713 interned in bgp_attr_parse(). */
1714 if (attr.aspath)
1715 aspath_unintern (attr.aspath);
1716 if (attr.community)
1717 community_unintern (attr.community);
1718 if (attr.ecommunity)
1719 ecommunity_unintern (attr.ecommunity);
1720 if (attr.cluster)
1721 cluster_unintern (attr.cluster);
1722 if (attr.transit)
1723 transit_unintern (attr.transit);
1725 /* If peering is stopped due to some reason, do not generate BGP
1726 event. */
1727 if (peer->status != Established)
1728 return 0;
1730 /* Increment packet counter. */
1731 peer->update_in++;
1732 peer->update_time = time (NULL);
1734 /* Generate BGP event. */
1735 BGP_EVENT_ADD (peer, Receive_UPDATE_message);
1737 return 0;
1740 /* Notify message treatment function. */
1741 void
1742 bgp_notify_receive (struct peer *peer, bgp_size_t size)
1744 struct bgp_notify bgp_notify;
1746 if (peer->notify.data)
1748 XFREE (MTYPE_TMP, peer->notify.data);
1749 peer->notify.data = NULL;
1750 peer->notify.length = 0;
1753 bgp_notify.code = stream_getc (peer->ibuf);
1754 bgp_notify.subcode = stream_getc (peer->ibuf);
1755 bgp_notify.length = size - 2;
1756 bgp_notify.data = NULL;
1758 /* Preserv notify code and sub code. */
1759 peer->notify.code = bgp_notify.code;
1760 peer->notify.subcode = bgp_notify.subcode;
1761 /* For further diagnostic record returned Data. */
1762 if (bgp_notify.length)
1764 peer->notify.length = size - 2;
1765 peer->notify.data = XMALLOC (MTYPE_TMP, size - 2);
1766 memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2);
1769 /* For debug */
1771 int i;
1772 int first = 0;
1773 char c[4];
1775 if (bgp_notify.length)
1777 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
1778 for (i = 0; i < bgp_notify.length; i++)
1779 if (first)
1781 sprintf (c, " %02x", stream_getc (peer->ibuf));
1782 strcat (bgp_notify.data, c);
1784 else
1786 first = 1;
1787 sprintf (c, "%02x", stream_getc (peer->ibuf));
1788 strcpy (bgp_notify.data, c);
1792 bgp_notify_print(peer, &bgp_notify, "received");
1793 if (bgp_notify.data)
1794 XFREE (MTYPE_TMP, bgp_notify.data);
1797 /* peer count update */
1798 peer->notify_in++;
1800 if (peer->status == Established)
1801 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
1802 /* We have to check for Notify with Unsupported Optional Parameter.
1803 in that case we fallback to open without the capability option.
1804 But this done in bgp_stop. We just mark it here to avoid changing
1805 the fsm tables. */
1806 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
1807 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM )
1808 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1810 /* Also apply to Unsupported Capability until remote router support
1811 capability. */
1812 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
1813 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_CAPBL)
1814 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1816 BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message);
1819 /* Keepalive treatment function -- get keepalive send keepalive */
1820 void
1821 bgp_keepalive_receive (struct peer *peer, bgp_size_t size)
1823 if (BGP_DEBUG (keepalive, KEEPALIVE))
1824 zlog_info ("%s received KEEPALIVE, length (excl. header) %d", peer->host, size);
1826 BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message);
1829 /* Route refresh message is received. */
1830 void
1831 bgp_route_refresh_receive (struct peer *peer, bgp_size_t size)
1833 afi_t afi;
1834 safi_t safi;
1835 u_char reserved;
1836 struct stream *s;
1838 /* If peer does not have the capability, send notification. */
1839 if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV))
1841 plog_err (peer->log, "%s [Error] BGP route refresh is not enabled",
1842 peer->host);
1843 bgp_notify_send (peer,
1844 BGP_NOTIFY_HEADER_ERR,
1845 BGP_NOTIFY_HEADER_BAD_MESTYPE);
1846 return;
1849 /* Status must be Established. */
1850 if (peer->status != Established)
1852 plog_err (peer->log,
1853 "%s [Error] Route refresh packet received under status %s",
1854 peer->host, LOOKUP (bgp_status_msg, peer->status));
1855 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1856 return;
1859 s = peer->ibuf;
1861 /* Parse packet. */
1862 afi = stream_getw (s);
1863 reserved = stream_getc (s);
1864 safi = stream_getc (s);
1866 if (BGP_DEBUG (normal, NORMAL))
1867 zlog_info ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1868 peer->host, afi, safi);
1870 /* Check AFI and SAFI. */
1871 if ((afi != AFI_IP && afi != AFI_IP6)
1872 || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
1873 && safi != BGP_SAFI_VPNV4))
1875 if (BGP_DEBUG (normal, NORMAL))
1877 zlog_info ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1878 peer->host, afi, safi);
1880 return;
1883 /* Adjust safi code. */
1884 if (safi == BGP_SAFI_VPNV4)
1885 safi = SAFI_MPLS_VPN;
1887 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
1889 u_char *end;
1890 u_char when_to_refresh;
1891 u_char orf_type;
1892 u_int16_t orf_len;
1894 if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5)
1896 zlog_info ("%s ORF route refresh length error", peer->host);
1897 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1898 return;
1901 when_to_refresh = stream_getc (s);
1902 end = stream_pnt (s) + (size - 5);
1904 while (stream_pnt (s) < end)
1906 orf_type = stream_getc (s);
1907 orf_len = stream_getw (s);
1909 if (orf_type == ORF_TYPE_PREFIX
1910 || orf_type == ORF_TYPE_PREFIX_OLD)
1912 u_char *p_pnt = stream_pnt (s);
1913 u_char *p_end = stream_pnt (s) + orf_len;
1914 struct orf_prefix orfp;
1915 u_char common = 0;
1916 u_int32_t seq;
1917 int psize;
1918 char name[BUFSIZ];
1919 char buf[BUFSIZ];
1920 int ret;
1922 if (BGP_DEBUG (normal, NORMAL))
1924 zlog_info ("%s rcvd Prefixlist ORF(%d) length %d",
1925 peer->host, orf_type, orf_len);
1928 /* ORF prefix-list name */
1929 sprintf (name, "%s.%d.%d", peer->host, afi, safi);
1931 while (p_pnt < p_end)
1933 memset (&orfp, 0, sizeof (struct orf_prefix));
1934 common = *p_pnt++;
1935 if (common & ORF_COMMON_PART_REMOVE_ALL)
1937 if (BGP_DEBUG (normal, NORMAL))
1938 zlog_info ("%s rcvd Remove-All pfxlist ORF request", peer->host);
1939 prefix_bgp_orf_remove_all (name);
1940 break;
1942 memcpy (&seq, p_pnt, sizeof (u_int32_t));
1943 p_pnt += sizeof (u_int32_t);
1944 orfp.seq = ntohl (seq);
1945 orfp.ge = *p_pnt++;
1946 orfp.le = *p_pnt++;
1947 orfp.p.prefixlen = *p_pnt++;
1948 orfp.p.family = afi2family (afi);
1949 psize = PSIZE (orfp.p.prefixlen);
1950 memcpy (&orfp.p.u.prefix, p_pnt, psize);
1951 p_pnt += psize;
1953 if (BGP_DEBUG (normal, NORMAL))
1954 zlog_info ("%s rcvd %s %s seq %u %s/%d ge %d le %d",
1955 peer->host,
1956 (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
1957 (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
1958 orfp.seq,
1959 inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, BUFSIZ),
1960 orfp.p.prefixlen, orfp.ge, orfp.le);
1962 ret = prefix_bgp_orf_set (name, afi, &orfp,
1963 (common & ORF_COMMON_PART_DENY ? 0 : 1 ),
1964 (common & ORF_COMMON_PART_REMOVE ? 0 : 1));
1966 if (ret != CMD_SUCCESS)
1968 if (BGP_DEBUG (normal, NORMAL))
1969 zlog_info ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer->host);
1970 prefix_bgp_orf_remove_all (name);
1971 break;
1974 peer->orf_plist[afi][safi] =
1975 prefix_list_lookup (AFI_ORF_PREFIX, name);
1977 stream_forward (s, orf_len);
1979 if (BGP_DEBUG (normal, NORMAL))
1980 zlog_info ("%s rcvd Refresh %s ORF request", peer->host,
1981 when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate");
1982 if (when_to_refresh == REFRESH_DEFER)
1983 return;
1986 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1987 if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
1988 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
1990 /* Perform route refreshment to the peer */
1991 bgp_announce_route (peer, afi, safi);
1995 bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length)
1997 u_char *end;
1998 struct capability cap;
1999 u_char action;
2000 struct bgp *bgp;
2001 afi_t afi;
2002 safi_t safi;
2004 bgp = peer->bgp;
2005 end = pnt + length;
2007 while (pnt < end)
2009 /* We need at least action, capability code and capability length. */
2010 if (pnt + 3 > end)
2012 zlog_info ("%s Capability length error", peer->host);
2013 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2014 return -1;
2017 action = *pnt;
2019 /* Fetch structure to the byte stream. */
2020 memcpy (&cap, pnt + 1, sizeof (struct capability));
2022 /* Action value check. */
2023 if (action != CAPABILITY_ACTION_SET
2024 && action != CAPABILITY_ACTION_UNSET)
2026 zlog_info ("%s Capability Action Value error %d",
2027 peer->host, action);
2028 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2029 return -1;
2032 if (BGP_DEBUG (normal, NORMAL))
2033 zlog_info ("%s CAPABILITY has action: %d, code: %u, length %u",
2034 peer->host, action, cap.code, cap.length);
2036 /* Capability length check. */
2037 if (pnt + (cap.length + 3) > end)
2039 zlog_info ("%s Capability length error", peer->host);
2040 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2041 return -1;
2044 /* We know MP Capability Code. */
2045 if (cap.code == CAPABILITY_CODE_MP)
2047 afi = ntohs (cap.mpc.afi);
2048 safi = cap.mpc.safi;
2050 /* Ignore capability when override-capability is set. */
2051 if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
2052 continue;
2054 /* Address family check. */
2055 if ((afi == AFI_IP
2056 || afi == AFI_IP6)
2057 && (safi == SAFI_UNICAST
2058 || safi == SAFI_MULTICAST
2059 || safi == BGP_SAFI_VPNV4))
2061 if (BGP_DEBUG (normal, NORMAL))
2062 zlog_info ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2063 peer->host,
2064 action == CAPABILITY_ACTION_SET
2065 ? "Advertising" : "Removing",
2066 ntohs(cap.mpc.afi) , cap.mpc.safi);
2068 /* Adjust safi code. */
2069 if (safi == BGP_SAFI_VPNV4)
2070 safi = SAFI_MPLS_VPN;
2072 if (action == CAPABILITY_ACTION_SET)
2074 peer->afc_recv[afi][safi] = 1;
2075 if (peer->afc[afi][safi])
2077 peer->afc_nego[afi][safi] = 1;
2078 bgp_announce_route (peer, afi, safi);
2081 else
2083 peer->afc_recv[afi][safi] = 0;
2084 peer->afc_nego[afi][safi] = 0;
2086 if (peer_active_nego (peer))
2088 bgp_clear_route (peer, afi, safi);
2089 peer->synctime[afi][safi] = 0;
2090 BGP_TIMER_OFF (peer->t_routeadv[afi][safi]);
2091 peer->af_sflags[afi][safi] = 0;
2093 else
2094 BGP_EVENT_ADD (peer, BGP_Stop);
2098 else
2100 zlog_warn ("%s unrecognized capability code: %d - ignored",
2101 peer->host, cap.code);
2103 pnt += cap.length + 3;
2105 return 0;
2108 /* Dynamic Capability is received. */
2109 void
2110 bgp_capability_receive (struct peer *peer, bgp_size_t size)
2112 u_char *pnt;
2113 int ret;
2115 /* Fetch pointer. */
2116 pnt = stream_pnt (peer->ibuf);
2118 if (BGP_DEBUG (normal, NORMAL))
2119 zlog_info ("%s rcv CAPABILITY", peer->host);
2121 /* If peer does not have the capability, send notification. */
2122 if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV))
2124 plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled",
2125 peer->host);
2126 bgp_notify_send (peer,
2127 BGP_NOTIFY_HEADER_ERR,
2128 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2129 return;
2132 /* Status must be Established. */
2133 if (peer->status != Established)
2135 plog_err (peer->log,
2136 "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status));
2137 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
2138 return;
2141 /* Parse packet. */
2142 ret = bgp_capability_msg_parse (peer, pnt, size);
2145 /* BGP read utility function. */
2147 bgp_read_packet (struct peer *peer)
2149 int nbytes;
2150 int readsize;
2152 readsize = peer->packet_size - peer->ibuf->putp;
2154 /* If size is zero then return. */
2155 if (! readsize)
2156 return 0;
2158 /* Read packet from fd. */
2159 nbytes = stream_read_unblock (peer->ibuf, peer->fd, readsize);
2161 /* If read byte is smaller than zero then error occured. */
2162 if (nbytes < 0)
2164 if (errno == EAGAIN)
2165 return -1;
2167 plog_err (peer->log, "%s [Error] bgp_read_packet error: %s",
2168 peer->host, strerror (errno));
2170 if (peer->status == Established)
2172 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
2174 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2175 SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
2177 else
2178 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2181 BGP_EVENT_ADD (peer, TCP_fatal_error);
2182 return -1;
2185 /* When read byte is zero : clear bgp peer and return */
2186 if (nbytes == 0)
2188 if (BGP_DEBUG (events, EVENTS))
2189 plog_info (peer->log, "%s [Event] BGP connection closed fd %d",
2190 peer->host, peer->fd);
2192 if (peer->status == Established)
2194 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
2196 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2197 SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
2199 else
2200 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2203 BGP_EVENT_ADD (peer, TCP_connection_closed);
2204 return -1;
2207 /* We read partial packet. */
2208 if (peer->ibuf->putp != peer->packet_size)
2209 return -1;
2211 return 0;
2214 /* Marker check. */
2216 bgp_marker_all_one (struct stream *s, int length)
2218 int i;
2220 for (i = 0; i < length; i++)
2221 if (s->data[i] != 0xff)
2222 return 0;
2224 return 1;
2227 /* Starting point of packet process function. */
2229 bgp_read (struct thread *thread)
2231 int ret;
2232 u_char type = 0;
2233 struct peer *peer;
2234 bgp_size_t size;
2235 char notify_data_length[2];
2237 /* Yes first of all get peer pointer. */
2238 peer = THREAD_ARG (thread);
2239 peer->t_read = NULL;
2241 /* For non-blocking IO check. */
2242 if (peer->status == Connect)
2244 bgp_connect_check (peer);
2245 goto done;
2247 else
2249 if (peer->fd < 0)
2251 zlog_err ("bgp_read peer's fd is negative value %d", peer->fd);
2252 return -1;
2254 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
2257 /* Read packet header to determine type of the packet */
2258 if (peer->packet_size == 0)
2259 peer->packet_size = BGP_HEADER_SIZE;
2261 if (peer->ibuf->putp < BGP_HEADER_SIZE)
2263 ret = bgp_read_packet (peer);
2265 /* Header read error or partial read packet. */
2266 if (ret < 0)
2267 goto done;
2269 /* Get size and type. */
2270 stream_forward (peer->ibuf, BGP_MARKER_SIZE);
2271 memcpy (notify_data_length, stream_pnt (peer->ibuf), 2);
2272 size = stream_getw (peer->ibuf);
2273 type = stream_getc (peer->ibuf);
2275 if (BGP_DEBUG (normal, NORMAL))
2276 if (type && type != BGP_MSG_UPDATE && type != BGP_MSG_KEEPALIVE)
2277 zlog_info ("%s rcv message type %d, length (excl. header) %d",
2278 peer->host, type, size - BGP_HEADER_SIZE);
2280 /* Marker check */
2281 if (type == BGP_MSG_OPEN
2282 && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE))
2284 bgp_notify_send (peer,
2285 BGP_NOTIFY_HEADER_ERR,
2286 BGP_NOTIFY_HEADER_NOT_SYNC);
2287 goto done;
2290 /* BGP type check. */
2291 if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
2292 && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
2293 && type != BGP_MSG_ROUTE_REFRESH_NEW
2294 && type != BGP_MSG_ROUTE_REFRESH_OLD
2295 && type != BGP_MSG_CAPABILITY)
2297 if (BGP_DEBUG (normal, NORMAL))
2298 plog_err (peer->log,
2299 "%s unknown message type 0x%02x",
2300 peer->host, type);
2301 bgp_notify_send_with_data (peer,
2302 BGP_NOTIFY_HEADER_ERR,
2303 BGP_NOTIFY_HEADER_BAD_MESTYPE,
2304 &type, 1);
2305 goto done;
2307 /* Mimimum packet length check. */
2308 if ((size < BGP_HEADER_SIZE)
2309 || (size > BGP_MAX_PACKET_SIZE)
2310 || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
2311 || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE)
2312 || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE)
2313 || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
2314 || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2315 || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2316 || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE))
2318 if (BGP_DEBUG (normal, NORMAL))
2319 plog_err (peer->log,
2320 "%s bad message length - %d for %s",
2321 peer->host, size,
2322 type == 128 ? "ROUTE-REFRESH" :
2323 bgp_type_str[(int) type]);
2324 bgp_notify_send_with_data (peer,
2325 BGP_NOTIFY_HEADER_ERR,
2326 BGP_NOTIFY_HEADER_BAD_MESLEN,
2327 notify_data_length, 2);
2328 goto done;
2331 /* Adjust size to message length. */
2332 peer->packet_size = size;
2335 ret = bgp_read_packet (peer);
2336 if (ret < 0)
2337 goto done;
2339 /* Get size and type again. */
2340 size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE);
2341 type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2);
2343 /* BGP packet dump function. */
2344 bgp_dump_packet (peer, type, peer->ibuf);
2346 size = (peer->packet_size - BGP_HEADER_SIZE);
2348 /* Read rest of the packet and call each sort of packet routine */
2349 switch (type)
2351 case BGP_MSG_OPEN:
2352 peer->open_in++;
2353 bgp_open_receive (peer, size);
2354 break;
2355 case BGP_MSG_UPDATE:
2356 peer->readtime = time(NULL); /* Last read timer reset */
2357 bgp_update_receive (peer, size);
2358 break;
2359 case BGP_MSG_NOTIFY:
2360 bgp_notify_receive (peer, size);
2361 break;
2362 case BGP_MSG_KEEPALIVE:
2363 peer->readtime = time(NULL); /* Last read timer reset */
2364 bgp_keepalive_receive (peer, size);
2365 break;
2366 case BGP_MSG_ROUTE_REFRESH_NEW:
2367 case BGP_MSG_ROUTE_REFRESH_OLD:
2368 peer->refresh_in++;
2369 bgp_route_refresh_receive (peer, size);
2370 break;
2371 case BGP_MSG_CAPABILITY:
2372 peer->dynamic_cap_in++;
2373 bgp_capability_receive (peer, size);
2374 break;
2377 /* Clear input buffer. */
2378 peer->packet_size = 0;
2379 if (peer->ibuf)
2380 stream_reset (peer->ibuf);
2382 done:
2383 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
2385 if (BGP_DEBUG (events, EVENTS))
2386 zlog_info ("%s [Event] Accepting BGP peer delete", peer->host);
2387 peer_delete (peer);
2389 return 0;