1/* BGP packet management routine.
2   Copyright (C) 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING.  If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA.  */
20
21#include <zebra.h>
22
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"
33
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"
50
51int stream_put_prefix (struct stream *, struct prefix *);
52
53/* Set up BGP packet marker and packet type. */
54static int
55bgp_packet_set_marker (struct stream *s, u_char type)
56{
57  int i;
58
59  /* Fill in marker. */
60  for (i = 0; i < BGP_MARKER_SIZE; i++)
61    stream_putc (s, 0xff);
62
63  /* Dummy total length. This field is should be filled in later on. */
64  stream_putw (s, 0);
65
66  /* BGP packet type. */
67  stream_putc (s, type);
68
69  /* Return current stream size. */
70  return stream_get_endp (s);
71}
72
73/* Set BGP packet header size entry.  If size is zero then use current
74   stream size. */
75static int
76bgp_packet_set_size (struct stream *s)
77{
78  int cp;
79
80  /* Preserve current pointer. */
81  cp = stream_get_endp (s);
82  stream_putw_at (s, BGP_MARKER_SIZE, cp);
83
84  return cp;
85}
86
87/* Add new packet to the peer. */
88static void
89bgp_packet_add (struct peer *peer, struct stream *s)
90{
91  /* Add packet to the end of list. */
92  stream_fifo_push (peer->obuf, s);
93}
94
95/* Free first packet. */
96static void
97bgp_packet_delete (struct peer *peer)
98{
99  stream_free (stream_fifo_pop (peer->obuf));
100}
101
102/* Check file descriptor whether connect is established. */
103static void
104bgp_connect_check (struct peer *peer)
105{
106  int status;
107  socklen_t slen;
108  int ret;
109
110  /* Anyway I have to reset read and write thread. */
111  BGP_READ_OFF (peer->t_read);
112  BGP_WRITE_OFF (peer->t_write);
113
114  /* Check file descriptor. */
115  slen = sizeof (status);
116  ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen);
117
118  /* If getsockopt is fail, this is fatal error. */
119  if (ret < 0)
120    {
121      zlog (peer->log, LOG_INFO, "can't get sockopt for nonblocking connect");
122      BGP_EVENT_ADD (peer, TCP_fatal_error);
123      return;
124    }
125
126  /* When status is 0 then TCP connection is established. */
127  if (status == 0)
128    {
129      BGP_EVENT_ADD (peer, TCP_connection_open);
130    }
131  else
132    {
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);
137    }
138}
139
140/* Make BGP update packet.  */
141static struct stream *
142bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi)
143{
144  struct stream *s;
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;
155
156  s = peer->work;
157  stream_reset (s);
158  snlri = peer->scratch;
159  stream_reset (snlri);
160
161  adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update);
162
163  while (adv)
164    {
165      assert (adv->rn);
166      rn = adv->rn;
167      adj = adv->adj;
168      if (adv->binfo)
169        binfo = adv->binfo;
170
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)))
174	break;
175
176      /* If packet is empty, set attribute. */
177      if (stream_empty (s))
178	{
179	  struct peer *from = NULL;
180
181          if (binfo)
182	    from = binfo->peer;
183
184	  /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
185	   * one byte message type.
186	   */
187	  bgp_packet_set_marker (s, BGP_MSG_UPDATE);
188
189	  /* 2: withdrawn routes length */
190	  stream_putw (s, 0);
191
192	  /* 3: total attributes length - attrlen_pos stores the position */
193	  attrlen_pos = stream_get_endp (s);
194	  stream_putw (s, 0);
195
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
198	   * position.
199	   */
200	  mpattr_pos = stream_get_endp(s);
201
202	  /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
203	  total_attr_len = bgp_packet_attribute (NULL, peer, s,
204	                                         adv->baa->attr,
205	                                         NULL, afi, safi,
206	                                         from, NULL, NULL);
207	}
208
209      if (afi == AFI_IP && safi == SAFI_UNICAST)
210	stream_put_prefix (s, &rn->p);
211      else
212	{
213	  /* Encode the prefix in MP_REACH_NLRI attribute */
214	  struct prefix_rd *prd = NULL;
215	  u_char *tag = NULL;
216
217	  if (rn->prn)
218	    prd = (struct prefix_rd *) &rn->prn->p;
219	  if (binfo && binfo->extra)
220	    tag = binfo->extra->tag;
221
222	  if (stream_empty(snlri))
223	    mpattrlen_pos = bgp_packet_mpattr_start(snlri, afi, safi,
224						    adv->baa->attr);
225	  bgp_packet_mpattr_prefix(snlri, afi, safi, &rn->p, prd, tag);
226	}
227      if (BGP_DEBUG (update, UPDATE_OUT))
228        {
229          char buf[INET6_BUFSIZ];
230
231          zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d",
232                peer->host,
233                inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ),
234                rn->p.prefixlen);
235        }
236
237      /* Synchnorize attribute.  */
238      if (adj->attr)
239	bgp_attr_unintern (&adj->attr);
240      else
241	peer->scount[afi][safi]++;
242
243      adj->attr = bgp_attr_intern (adv->baa->attr);
244
245      adv = bgp_advertise_clean (peer, adj, afi, safi);
246    }
247
248  if (! stream_empty (s))
249    {
250      if (!stream_empty(snlri))
251	{
252	  bgp_packet_mpattr_end(snlri, mpattrlen_pos);
253	  total_attr_len += stream_get_endp(snlri);
254	}
255
256      /* set the total attribute length correctly */
257      stream_putw_at (s, attrlen_pos, total_attr_len);
258
259      if (!stream_empty(snlri))
260	packet = stream_dupcat(s, snlri, mpattr_pos);
261      else
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);
266      stream_reset (s);
267      stream_reset (snlri);
268      return packet;
269    }
270  return NULL;
271}
272
273static struct stream *
274bgp_update_packet_eor (struct peer *peer, afi_t afi, safi_t safi)
275{
276  struct stream *s;
277  struct stream *packet;
278
279  if (DISABLE_BGP_ANNOUNCE)
280    return NULL;
281
282  if (BGP_DEBUG (normal, NORMAL))
283    zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi, safi), peer->host);
284
285  s = stream_new (BGP_MAX_PACKET_SIZE);
286
287  /* Make BGP update packet. */
288  bgp_packet_set_marker (s, BGP_MSG_UPDATE);
289
290  /* Unfeasible Routes Length */
291  stream_putw (s, 0);
292
293  if (afi == AFI_IP && safi == SAFI_UNICAST)
294    {
295      /* Total Path Attribute Length */
296      stream_putw (s, 0);
297    }
298  else
299    {
300      /* Total Path Attribute Length */
301      stream_putw (s, 6);
302      stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
303      stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI);
304      stream_putc (s, 3);
305      stream_putw (s, afi);
306      stream_putc (s, safi);
307    }
308
309  bgp_packet_set_size (s);
310  packet = stream_dup (s);
311  bgp_packet_add (peer, packet);
312  stream_free (s);
313  return packet;
314}
315
316/* Make BGP withdraw packet.  */
317/* For ipv4 unicast:
318   16-octet marker | 2-octet length | 1-octet type |
319    2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
320*/
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
325*/
326static struct stream *
327bgp_withdraw_packet (struct peer *peer, afi_t afi, safi_t safi)
328{
329  struct stream *s;
330  struct stream *packet;
331  struct bgp_adj_out *adj;
332  struct bgp_advertise *adv;
333  struct bgp_node *rn;
334  bgp_size_t unfeasible_len;
335  bgp_size_t total_attr_len;
336  size_t mp_start = 0;
337  size_t attrlen_pos = 0;
338  size_t mplen_pos = 0;
339  u_char first_time = 1;
340
341  s = peer->work;
342  stream_reset (s);
343
344  while ((adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL)
345    {
346      assert (adv->rn);
347      adj = adv->adj;
348      rn = adv->rn;
349
350      if (STREAM_REMAIN (s)
351	  < (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN + PSIZE (rn->p.prefixlen)))
352	break;
353
354      if (stream_empty (s))
355	{
356	  bgp_packet_set_marker (s, BGP_MSG_UPDATE);
357	  stream_putw (s, 0); /* unfeasible routes length */
358	}
359      else
360	first_time = 0;
361
362      if (afi == AFI_IP && safi == SAFI_UNICAST)
363	stream_put_prefix (s, &rn->p);
364      else
365	{
366	  struct prefix_rd *prd = NULL;
367
368	  if (rn->prn)
369	    prd = (struct prefix_rd *) &rn->prn->p;
370
371	  /* If first time, format the MP_UNREACH header */
372	  if (first_time)
373	    {
374	      attrlen_pos = stream_get_endp (s);
375	      /* total attr length = 0 for now. reevaluate later */
376	      stream_putw (s, 0);
377	      mp_start = stream_get_endp (s);
378	      mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
379	    }
380
381	  bgp_packet_mpunreach_prefix(s, &rn->p, afi, safi, prd, NULL);
382	}
383
384      if (BGP_DEBUG (update, UPDATE_OUT))
385        {
386          char buf[INET6_BUFSIZ];
387
388          zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
389                peer->host,
390                inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ),
391                rn->p.prefixlen);
392        }
393
394      peer->scount[afi][safi]--;
395
396      bgp_adj_out_remove (rn, adj, peer, afi, safi);
397      bgp_unlock_node (rn);
398    }
399
400  if (! stream_empty (s))
401    {
402      if (afi == AFI_IP && safi == SAFI_UNICAST)
403	{
404	  unfeasible_len
405	    = stream_get_endp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
406	  stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
407	  stream_putw (s, 0);
408	}
409      else
410	{
411	  /* Set the mp_unreach attr's length */
412	  bgp_packet_mpunreach_end(s, mplen_pos);
413
414	  /* Set total path attribute length. */
415	  total_attr_len = stream_get_endp(s) - mp_start;
416	  stream_putw_at (s, attrlen_pos, total_attr_len);
417	}
418      bgp_packet_set_size (s);
419      packet = stream_dup (s);
420      bgp_packet_add (peer, packet);
421      stream_reset (s);
422      return packet;
423    }
424
425  return NULL;
426}
427
428void
429bgp_default_update_send (struct peer *peer, struct attr *attr,
430			 afi_t afi, safi_t safi, struct peer *from)
431{
432  struct stream *s;
433  struct stream *packet;
434  struct prefix p;
435  unsigned long pos;
436  bgp_size_t total_attr_len;
437
438  if (DISABLE_BGP_ANNOUNCE)
439    return;
440
441  if (afi == AFI_IP)
442    str2prefix ("0.0.0.0/0", &p);
443#ifdef HAVE_IPV6
444  else
445    str2prefix ("::/0", &p);
446#endif /* HAVE_IPV6 */
447
448  /* Logging the attribute. */
449  if (BGP_DEBUG (update, UPDATE_OUT))
450    {
451      char attrstr[BUFSIZ];
452      char buf[INET6_BUFSIZ];
453      attrstr[0] = '\0';
454
455      bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
456      zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d %s",
457	    peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
458	    p.prefixlen, attrstr);
459    }
460
461  s = stream_new (BGP_MAX_PACKET_SIZE);
462
463  /* Make BGP update packet. */
464  bgp_packet_set_marker (s, BGP_MSG_UPDATE);
465
466  /* Unfeasible Routes Length. */
467  stream_putw (s, 0);
468
469  /* Make place for total attribute length.  */
470  pos = stream_get_endp (s);
471  stream_putw (s, 0);
472  total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &p, afi, safi, from, NULL, NULL);
473
474  /* Set Total Path Attribute Length. */
475  stream_putw_at (s, pos, total_attr_len);
476
477  /* NLRI set. */
478  if (p.family == AF_INET && safi == SAFI_UNICAST)
479    stream_put_prefix (s, &p);
480
481  /* Set size. */
482  bgp_packet_set_size (s);
483
484  packet = stream_dup (s);
485  stream_free (s);
486
487  /* Dump packet if debug option is set. */
488#ifdef DEBUG
489  /* bgp_packet_dump (packet); */
490#endif /* DEBUG */
491
492  /* Add packet to the peer. */
493  bgp_packet_add (peer, packet);
494
495  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
496}
497
498void
499bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi)
500{
501  struct stream *s;
502  struct stream *packet;
503  struct prefix p;
504  unsigned long attrlen_pos = 0;
505  unsigned long cp;
506  bgp_size_t unfeasible_len;
507  bgp_size_t total_attr_len;
508  size_t mp_start = 0;
509  size_t mplen_pos = 0;
510
511  if (DISABLE_BGP_ANNOUNCE)
512    return;
513
514  if (afi == AFI_IP)
515    str2prefix ("0.0.0.0/0", &p);
516#ifdef HAVE_IPV6
517  else
518    str2prefix ("::/0", &p);
519#endif /* HAVE_IPV6 */
520
521  total_attr_len = 0;
522
523  if (BGP_DEBUG (update, UPDATE_OUT))
524    {
525      char buf[INET6_BUFSIZ];
526
527      zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
528            peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
529            p.prefixlen);
530    }
531
532  s = stream_new (BGP_MAX_PACKET_SIZE);
533
534  /* Make BGP update packet. */
535  bgp_packet_set_marker (s, BGP_MSG_UPDATE);
536
537  /* Unfeasible Routes Length. */;
538  cp = stream_get_endp (s);
539  stream_putw (s, 0);
540
541  /* Withdrawn Routes. */
542  if (p.family == AF_INET && safi == SAFI_UNICAST)
543    {
544      stream_put_prefix (s, &p);
545
546      unfeasible_len = stream_get_endp (s) - cp - 2;
547
548      /* Set unfeasible len.  */
549      stream_putw_at (s, cp, unfeasible_len);
550
551      /* Set total path attribute length. */
552      stream_putw (s, 0);
553    }
554  else
555    {
556      attrlen_pos = stream_get_endp (s);
557      stream_putw (s, 0);
558      mp_start = stream_get_endp (s);
559      mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
560      bgp_packet_mpunreach_prefix(s, &p, afi, safi, NULL, NULL);
561
562      /* Set the mp_unreach attr's length */
563      bgp_packet_mpunreach_end(s, mplen_pos);
564
565      /* Set total path attribute length. */
566      total_attr_len = stream_get_endp(s) - mp_start;
567      stream_putw_at (s, attrlen_pos, total_attr_len);
568    }
569
570  bgp_packet_set_size (s);
571
572  packet = stream_dup (s);
573  stream_free (s);
574
575  /* Add packet to the peer. */
576  bgp_packet_add (peer, packet);
577
578  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
579}
580
581/* Get next packet to be written.  */
582static struct stream *
583bgp_write_packet (struct peer *peer)
584{
585  afi_t afi;
586  safi_t safi;
587  struct stream *s = NULL;
588  struct bgp_advertise *adv;
589
590  s = stream_fifo_head (peer->obuf);
591  if (s)
592    return s;
593
594  for (afi = AFI_IP; afi < AFI_MAX; afi++)
595    for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
596      {
597	adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->withdraw);
598	if (adv)
599	  {
600	    s = bgp_withdraw_packet (peer, afi, safi);
601	    if (s)
602	      return s;
603	  }
604      }
605
606  for (afi = AFI_IP; afi < AFI_MAX; afi++)
607    for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
608      {
609	adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update);
610	if (adv)
611	  {
612            if (adv->binfo && adv->binfo->uptime < peer->synctime)
613	      {
614		if (CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_RCV)
615		    && CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_ADV)
616		    && ! (CHECK_FLAG (adv->binfo->peer->cap,
617                                      PEER_CAP_RESTART_BIT_RCV) &&
618		          CHECK_FLAG (adv->binfo->peer->cap,
619                                      PEER_CAP_RESTART_BIT_ADV))
620		    && ! CHECK_FLAG (adv->binfo->flags, BGP_INFO_STALE)
621		    && safi != SAFI_MPLS_VPN)
622		  {
623		    if (CHECK_FLAG (adv->binfo->peer->af_sflags[afi][safi],
624			PEER_STATUS_EOR_RECEIVED))
625		      s = bgp_update_packet (peer, afi, safi);
626		  }
627		else
628		  s = bgp_update_packet (peer, afi, safi);
629	      }
630
631	    if (s)
632	      return s;
633	  }
634
635	if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV))
636	  {
637	    if (peer->afc_nego[afi][safi] && peer->synctime
638		&& ! CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND)
639		&& safi != SAFI_MPLS_VPN)
640	      {
641		SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND);
642		return bgp_update_packet_eor (peer, afi, safi);
643	      }
644	  }
645      }
646
647  return NULL;
648}
649
650/* Is there partially written packet or updates we can send right
651   now.  */
652static int
653bgp_write_proceed (struct peer *peer)
654{
655  afi_t afi;
656  safi_t safi;
657  struct bgp_advertise *adv;
658
659  if (stream_fifo_head (peer->obuf))
660    return 1;
661
662  for (afi = AFI_IP; afi < AFI_MAX; afi++)
663    for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
664      if (FIFO_HEAD (&peer->sync[afi][safi]->withdraw))
665	return 1;
666
667  for (afi = AFI_IP; afi < AFI_MAX; afi++)
668    for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
669      if ((adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL)
670	if (adv->binfo->uptime < peer->synctime)
671	  return 1;
672
673  return 0;
674}
675
676/* Write packet to the peer. */
677int
678bgp_write (struct thread *thread)
679{
680  struct peer *peer;
681  u_char type;
682  struct stream *s;
683  int num;
684  unsigned int count = 0;
685
686  /* Yes first of all get peer pointer. */
687  peer = THREAD_ARG (thread);
688  peer->t_write = NULL;
689
690  /* For non-blocking IO check. */
691  if (peer->status == Connect)
692    {
693      bgp_connect_check (peer);
694      return 0;
695    }
696
697  s = bgp_write_packet (peer);
698  if (!s)
699    return 0;	/* nothing to send */
700
701  sockopt_cork (peer->fd, 1);
702
703  /* Nonblocking write until TCP output buffer is full.  */
704  do
705    {
706      int writenum;
707
708      /* Number of bytes to be sent.  */
709      writenum = stream_get_endp (s) - stream_get_getp (s);
710
711      /* Call write() system call.  */
712      num = write (peer->fd, STREAM_PNT (s), writenum);
713      if (num < 0)
714	{
715	  /* write failed either retry needed or error */
716	  if (ERRNO_IO_RETRY(errno))
717		break;
718
719          BGP_EVENT_ADD (peer, TCP_fatal_error);
720	  return 0;
721	}
722
723      if (num != writenum)
724	{
725	  /* Partial write */
726	  stream_forward_getp (s, num);
727	  break;
728	}
729
730      /* Retrieve BGP packet type. */
731      stream_set_getp (s, BGP_MARKER_SIZE + 2);
732      type = stream_getc (s);
733
734      switch (type)
735	{
736	case BGP_MSG_OPEN:
737	  peer->open_out++;
738	  break;
739	case BGP_MSG_UPDATE:
740	  peer->update_out++;
741	  break;
742	case BGP_MSG_NOTIFY:
743	  peer->notify_out++;
744	  /* Double start timer. */
745	  peer->v_start *= 2;
746
747	  /* Overflow check. */
748	  if (peer->v_start >= (60 * 2))
749	    peer->v_start = (60 * 2);
750
751	  /* Flush any existing events */
752	  BGP_EVENT_ADD (peer, BGP_Stop);
753	  goto done;
754
755	case BGP_MSG_KEEPALIVE:
756	  peer->keepalive_out++;
757	  break;
758	case BGP_MSG_ROUTE_REFRESH_NEW:
759	case BGP_MSG_ROUTE_REFRESH_OLD:
760	  peer->refresh_out++;
761	  break;
762	case BGP_MSG_CAPABILITY:
763	  peer->dynamic_cap_out++;
764	  break;
765	}
766
767      /* OK we send packet so delete it. */
768      bgp_packet_delete (peer);
769    }
770  while (++count < BGP_WRITE_PACKET_MAX &&
771	 (s = bgp_write_packet (peer)) != NULL);
772
773  if (bgp_write_proceed (peer))
774    BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
775
776 done:
777  sockopt_cork (peer->fd, 0);
778  return 0;
779}
780
781/* This is only for sending NOTIFICATION message to neighbor. */
782static int
783bgp_write_notify (struct peer *peer)
784{
785  int ret, val;
786  u_char type;
787  struct stream *s;
788
789  /* There should be at least one packet. */
790  s = stream_fifo_head (peer->obuf);
791  if (!s)
792    return 0;
793  assert (stream_get_endp (s) >= BGP_HEADER_SIZE);
794
795  /* Stop collecting data within the socket */
796  sockopt_cork (peer->fd, 0);
797
798  /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
799   * we only care about getting a clean shutdown at this point. */
800  ret = write (peer->fd, STREAM_DATA (s), stream_get_endp (s));
801
802  /* only connection reset/close gets counted as TCP_fatal_error, failure
803   * to write the entire NOTIFY doesn't get different FSM treatment */
804  if (ret <= 0)
805    {
806      BGP_EVENT_ADD (peer, TCP_fatal_error);
807      return 0;
808    }
809
810  /* Disable Nagle, make NOTIFY packet go out right away */
811  val = 1;
812  (void) setsockopt (peer->fd, IPPROTO_TCP, TCP_NODELAY,
813                            (char *) &val, sizeof (val));
814
815  /* Retrieve BGP packet type. */
816  stream_set_getp (s, BGP_MARKER_SIZE + 2);
817  type = stream_getc (s);
818
819  assert (type == BGP_MSG_NOTIFY);
820
821  /* Type should be notify. */
822  peer->notify_out++;
823
824  /* Double start timer. */
825  peer->v_start *= 2;
826
827  /* Overflow check. */
828  if (peer->v_start >= (60 * 2))
829    peer->v_start = (60 * 2);
830
831  BGP_EVENT_ADD (peer, BGP_Stop);
832
833  return 0;
834}
835
836/* Make keepalive packet and send it to the peer. */
837void
838bgp_keepalive_send (struct peer *peer)
839{
840  struct stream *s;
841  int length;
842
843  s = stream_new (BGP_MAX_PACKET_SIZE);
844
845  /* Make keepalive packet. */
846  bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE);
847
848  /* Set packet size. */
849  length = bgp_packet_set_size (s);
850
851  /* Dump packet if debug option is set. */
852  /* bgp_packet_dump (s); */
853
854  if (BGP_DEBUG (keepalive, KEEPALIVE))
855    zlog_debug ("%s sending KEEPALIVE", peer->host);
856  if (BGP_DEBUG (normal, NORMAL))
857    zlog_debug ("%s send message type %d, length (incl. header) %d",
858               peer->host, BGP_MSG_KEEPALIVE, length);
859
860  /* Add packet to the peer. */
861  bgp_packet_add (peer, s);
862
863  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
864}
865
866/* Make open packet and send it to the peer. */
867void
868bgp_open_send (struct peer *peer)
869{
870  struct stream *s;
871  int length;
872  u_int16_t send_holdtime;
873  as_t local_as;
874
875  if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
876    send_holdtime = peer->holdtime;
877  else
878    send_holdtime = peer->bgp->default_holdtime;
879
880  /* local-as Change */
881  if (peer->change_local_as)
882    local_as = peer->change_local_as;
883  else
884    local_as = peer->local_as;
885
886  s = stream_new (BGP_MAX_PACKET_SIZE);
887
888  /* Make open packet. */
889  bgp_packet_set_marker (s, BGP_MSG_OPEN);
890
891  /* Set open packet values. */
892  stream_putc (s, BGP_VERSION_4);        /* BGP version */
893  stream_putw (s, (local_as <= BGP_AS_MAX) ? (u_int16_t) local_as
894                                           : BGP_AS_TRANS);
895  stream_putw (s, send_holdtime);     	 /* Hold Time */
896  stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */
897
898  /* Set capability code. */
899  bgp_open_capability (s, peer);
900
901  /* Set BGP packet length. */
902  length = bgp_packet_set_size (s);
903
904  if (BGP_DEBUG (normal, NORMAL))
905    zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
906	       peer->host, BGP_VERSION_4, local_as,
907	       send_holdtime, inet_ntoa (peer->local_id));
908
909  if (BGP_DEBUG (normal, NORMAL))
910    zlog_debug ("%s send message type %d, length (incl. header) %d",
911	       peer->host, BGP_MSG_OPEN, length);
912
913  /* Dump packet if debug option is set. */
914  /* bgp_packet_dump (s); */
915
916  /* Add packet to the peer. */
917  bgp_packet_add (peer, s);
918
919  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
920}
921
922/* Send BGP notify packet with data potion. */
923void
924bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code,
925			   u_char *data, size_t datalen)
926{
927  struct stream *s;
928  int length;
929
930  /* Allocate new stream. */
931  s = stream_new (BGP_MAX_PACKET_SIZE);
932
933  /* Make nitify packet. */
934  bgp_packet_set_marker (s, BGP_MSG_NOTIFY);
935
936  /* Set notify packet values. */
937  stream_putc (s, code);        /* BGP notify code */
938  stream_putc (s, sub_code);	/* BGP notify sub_code */
939
940  /* If notify data is present. */
941  if (data)
942    stream_write (s, data, datalen);
943
944  /* Set BGP packet length. */
945  length = bgp_packet_set_size (s);
946
947  /* Add packet to the peer. */
948  stream_fifo_clean (peer->obuf);
949  bgp_packet_add (peer, s);
950
951  /* For debug */
952  {
953    struct bgp_notify bgp_notify;
954    int first = 0;
955    int i;
956    char c[4];
957
958    bgp_notify.code = code;
959    bgp_notify.subcode = sub_code;
960    bgp_notify.data = NULL;
961    bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE;
962
963    if (bgp_notify.length)
964      {
965	bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
966	for (i = 0; i < bgp_notify.length; i++)
967	  if (first)
968	    {
969	      sprintf (c, " %02x", data[i]);
970	      strcat (bgp_notify.data, c);
971	    }
972	  else
973	    {
974	      first = 1;
975	      sprintf (c, "%02x", data[i]);
976	      strcpy (bgp_notify.data, c);
977	    }
978      }
979    bgp_notify_print (peer, &bgp_notify, "sending");
980    if (bgp_notify.data)
981      XFREE (MTYPE_TMP, bgp_notify.data);
982  }
983
984  if (BGP_DEBUG (normal, NORMAL))
985    zlog_debug ("%s send message type %d, length (incl. header) %d",
986	       peer->host, BGP_MSG_NOTIFY, length);
987
988  /* peer reset cause */
989  if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE)
990    {
991      if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
992      {
993        peer->last_reset = PEER_DOWN_USER_RESET;
994        zlog_info ("Notification sent to neighbor %s: User reset", peer->host);
995      }
996      else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
997      {
998        peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
999        zlog_info ("Notification sent to neighbor %s: shutdown", peer->host);
1000      }
1001      else
1002      {
1003        peer->last_reset = PEER_DOWN_NOTIFY_SEND;
1004        zlog_info ("Notification sent to neighbor %s: type %u/%u",
1005                   peer->host, code, sub_code);
1006      }
1007    }
1008  else
1009     zlog_info ("Notification sent to neighbor %s: configuration change",
1010                peer->host);
1011
1012  /* Call immediately. */
1013  BGP_WRITE_OFF (peer->t_write);
1014
1015  bgp_write_notify (peer);
1016}
1017
1018/* Send BGP notify packet. */
1019void
1020bgp_notify_send (struct peer *peer, u_char code, u_char sub_code)
1021{
1022  bgp_notify_send_with_data (peer, code, sub_code, NULL, 0);
1023}
1024
1025/* Send route refresh message to the peer. */
1026void
1027bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi,
1028			u_char orf_type, u_char when_to_refresh, int remove)
1029{
1030  struct stream *s;
1031  struct stream *packet;
1032  int length;
1033  struct bgp_filter *filter;
1034  int orf_refresh = 0;
1035
1036  if (DISABLE_BGP_ANNOUNCE)
1037    return;
1038
1039  filter = &peer->filter[afi][safi];
1040
1041  /* Adjust safi code. */
1042  if (safi == SAFI_MPLS_VPN)
1043    safi = SAFI_MPLS_LABELED_VPN;
1044
1045  s = stream_new (BGP_MAX_PACKET_SIZE);
1046
1047  /* Make BGP update packet. */
1048  if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
1049    bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW);
1050  else
1051    bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD);
1052
1053  /* Encode Route Refresh message. */
1054  stream_putw (s, afi);
1055  stream_putc (s, 0);
1056  stream_putc (s, safi);
1057
1058  if (orf_type == ORF_TYPE_PREFIX
1059      || orf_type == ORF_TYPE_PREFIX_OLD)
1060    if (remove || filter->plist[FILTER_IN].plist)
1061      {
1062	u_int16_t orf_len;
1063	unsigned long orfp;
1064
1065	orf_refresh = 1;
1066	stream_putc (s, when_to_refresh);
1067	stream_putc (s, orf_type);
1068	orfp = stream_get_endp (s);
1069	stream_putw (s, 0);
1070
1071	if (remove)
1072	  {
1073	    UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
1074	    stream_putc (s, ORF_COMMON_PART_REMOVE_ALL);
1075	    if (BGP_DEBUG (normal, NORMAL))
1076	      zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
1077			 peer->host, orf_type,
1078			 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
1079			 afi, safi);
1080	  }
1081	else
1082	  {
1083	    SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
1084	    prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist,
1085				  ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT,
1086				  ORF_COMMON_PART_DENY);
1087	    if (BGP_DEBUG (normal, NORMAL))
1088	      zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
1089			 peer->host, orf_type,
1090			 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
1091			 afi, safi);
1092	  }
1093
1094	/* Total ORF Entry Len. */
1095	orf_len = stream_get_endp (s) - orfp - 2;
1096	stream_putw_at (s, orfp, orf_len);
1097      }
1098
1099  /* Set packet size. */
1100  length = bgp_packet_set_size (s);
1101
1102  if (BGP_DEBUG (normal, NORMAL))
1103    {
1104      if (! orf_refresh)
1105	zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
1106		   peer->host, afi, safi);
1107      zlog_debug ("%s send message type %d, length (incl. header) %d",
1108		 peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ?
1109		 BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length);
1110    }
1111
1112  /* Make real packet. */
1113  packet = stream_dup (s);
1114  stream_free (s);
1115
1116  /* Add packet to the peer. */
1117  bgp_packet_add (peer, packet);
1118
1119  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
1120}
1121
1122/* Send capability message to the peer. */
1123void
1124bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi,
1125		     int capability_code, int action)
1126{
1127  struct stream *s;
1128  struct stream *packet;
1129  int length;
1130
1131  /* Adjust safi code. */
1132  if (safi == SAFI_MPLS_VPN)
1133    safi = SAFI_MPLS_LABELED_VPN;
1134
1135  s = stream_new (BGP_MAX_PACKET_SIZE);
1136
1137  /* Make BGP update packet. */
1138  bgp_packet_set_marker (s, BGP_MSG_CAPABILITY);
1139
1140  /* Encode MP_EXT capability. */
1141  if (capability_code == CAPABILITY_CODE_MP)
1142    {
1143      stream_putc (s, action);
1144      stream_putc (s, CAPABILITY_CODE_MP);
1145      stream_putc (s, CAPABILITY_CODE_MP_LEN);
1146      stream_putw (s, afi);
1147      stream_putc (s, 0);
1148      stream_putc (s, safi);
1149
1150      if (BGP_DEBUG (normal, NORMAL))
1151        zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
1152		   peer->host, action == CAPABILITY_ACTION_SET ?
1153		   "Advertising" : "Removing", afi, safi);
1154    }
1155
1156  /* Set packet size. */
1157  length = bgp_packet_set_size (s);
1158
1159  /* Make real packet. */
1160  packet = stream_dup (s);
1161  stream_free (s);
1162
1163  /* Add packet to the peer. */
1164  bgp_packet_add (peer, packet);
1165
1166  if (BGP_DEBUG (normal, NORMAL))
1167    zlog_debug ("%s send message type %d, length (incl. header) %d",
1168	       peer->host, BGP_MSG_CAPABILITY, length);
1169
1170  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
1171}
1172
1173/* RFC1771 6.8 Connection collision detection. */
1174static int
1175bgp_collision_detect (struct peer *new, struct in_addr remote_id)
1176{
1177  struct peer *peer;
1178  struct listnode *node, *nnode;
1179  struct bgp *bgp;
1180
1181  bgp = bgp_get_default ();
1182  if (! bgp)
1183    return 0;
1184
1185  /* Upon receipt of an OPEN message, the local system must examine
1186     all of its connections that are in the OpenConfirm state.  A BGP
1187     speaker may also examine connections in an OpenSent state if it
1188     knows the BGP Identifier of the peer by means outside of the
1189     protocol.  If among these connections there is a connection to a
1190     remote BGP speaker whose BGP Identifier equals the one in the
1191     OPEN message, then the local system performs the following
1192     collision resolution procedure: */
1193
1194  for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
1195    {
1196      /* Under OpenConfirm status, local peer structure already hold
1197         remote router ID. */
1198
1199      if (peer != new
1200	  && (peer->status == OpenConfirm || peer->status == OpenSent)
1201	  && sockunion_same (&peer->su, &new->su))
1202	{
1203	  /* 1. The BGP Identifier of the local system is compared to
1204	     the BGP Identifier of the remote system (as specified in
1205	     the OPEN message). */
1206
1207	  if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr))
1208	    {
1209	      /* 2. If the value of the local BGP Identifier is less
1210		 than the remote one, the local system closes BGP
1211		 connection that already exists (the one that is
1212		 already in the OpenConfirm state), and accepts BGP
1213		 connection initiated by the remote system. */
1214
1215	      if (peer->fd >= 0)
1216		bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1217	      return 1;
1218	    }
1219	  else
1220	    {
1221	      /* 3. Otherwise, the local system closes newly created
1222		 BGP connection (the one associated with the newly
1223		 received OPEN message), and continues to use the
1224		 existing one (the one that is already in the
1225		 OpenConfirm state). */
1226
1227	      if (new->fd >= 0)
1228		bgp_notify_send (new, BGP_NOTIFY_CEASE,
1229			         BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1230	      return -1;
1231	    }
1232	}
1233    }
1234  return 0;
1235}
1236
1237static int
1238bgp_open_receive (struct peer *peer, bgp_size_t size)
1239{
1240  int ret;
1241  u_char version;
1242  u_char optlen;
1243  u_int16_t holdtime;
1244  u_int16_t send_holdtime;
1245  as_t remote_as;
1246  as_t as4 = 0;
1247  struct peer *realpeer;
1248  struct in_addr remote_id;
1249  int mp_capability;
1250  u_int8_t notify_data_remote_as[2];
1251  u_int8_t notify_data_remote_id[4];
1252
1253  realpeer = NULL;
1254
1255  /* Parse open packet. */
1256  version = stream_getc (peer->ibuf);
1257  memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2);
1258  remote_as  = stream_getw (peer->ibuf);
1259  holdtime = stream_getw (peer->ibuf);
1260  memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
1261  remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
1262
1263  /* Receive OPEN message log  */
1264  if (BGP_DEBUG (normal, NORMAL))
1265    zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
1266                " holdtime %d, id %s",
1267	        peer->host, version, remote_as, holdtime,
1268	        inet_ntoa (remote_id));
1269
1270  /* BEGIN to read the capability here, but dont do it yet */
1271  mp_capability = 0;
1272  optlen = stream_getc (peer->ibuf);
1273
1274  if (optlen != 0)
1275    {
1276      /* We need the as4 capability value *right now* because
1277       * if it is there, we have not got the remote_as yet, and without
1278       * that we do not know which peer is connecting to us now.
1279       */
1280      as4 = peek_for_as4_capability (peer, optlen);
1281    }
1282
1283  /* Just in case we have a silly peer who sends AS4 capability set to 0 */
1284  if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) && !as4)
1285    {
1286      zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
1287                peer->host);
1288      bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1289                       BGP_NOTIFY_OPEN_BAD_PEER_AS);
1290      return -1;
1291    }
1292
1293  if (remote_as == BGP_AS_TRANS)
1294    {
1295	  /* Take the AS4 from the capability.  We must have received the
1296	   * capability now!  Otherwise we have a asn16 peer who uses
1297	   * BGP_AS_TRANS, for some unknown reason.
1298	   */
1299      if (as4 == BGP_AS_TRANS)
1300        {
1301          zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1302                    peer->host);
1303          bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1304                 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1305          return -1;
1306        }
1307
1308      if (!as4 && BGP_DEBUG (as4, AS4))
1309        zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
1310                    " Odd, but proceeding.", peer->host);
1311      else if (as4 < BGP_AS_MAX && BGP_DEBUG (as4, AS4))
1312        zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
1313                    "in 2-bytes, very odd peer.", peer->host, as4);
1314      if (as4)
1315        remote_as = as4;
1316    }
1317  else
1318    {
1319      /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
1320      /* If we have got the capability, peer->as4cap must match remote_as */
1321      if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)
1322          && as4 != remote_as)
1323        {
1324	  /* raise error, log this, close session */
1325	  zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
1326	            " mismatch with 16bit 'myasn' %u in open",
1327	            peer->host, as4, remote_as);
1328	  bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1329			   BGP_NOTIFY_OPEN_BAD_PEER_AS);
1330	  return -1;
1331	}
1332    }
1333
1334  /* Lookup peer from Open packet. */
1335  if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1336    {
1337      int as = 0;
1338
1339      realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as);
1340
1341      if (! realpeer)
1342	{
1343	  /* Peer's source IP address is check in bgp_accept(), so this
1344	     must be AS number mismatch or remote-id configuration
1345	     mismatch. */
1346	  if (as)
1347	    {
1348	      if (BGP_DEBUG (normal, NORMAL))
1349		zlog_debug ("%s bad OPEN, wrong router identifier %s",
1350			    peer->host, inet_ntoa (remote_id));
1351	      bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1352					 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1353					 notify_data_remote_id, 4);
1354	    }
1355	  else
1356	    {
1357	      if (BGP_DEBUG (normal, NORMAL))
1358		zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1359			    peer->host, remote_as, peer->as);
1360	      bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1361					 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1362					 notify_data_remote_as, 2);
1363	    }
1364	  return -1;
1365	}
1366    }
1367
1368  /* When collision is detected and this peer is closed.  Retrun
1369     immidiately. */
1370  ret = bgp_collision_detect (peer, remote_id);
1371  if (ret < 0)
1372    return ret;
1373
1374  /* Hack part. */
1375  if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1376    {
1377	if (realpeer->status == Established
1378	    && CHECK_FLAG (realpeer->sflags, PEER_STATUS_NSF_MODE))
1379	{
1380	  realpeer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
1381	  SET_FLAG (realpeer->sflags, PEER_STATUS_NSF_WAIT);
1382	}
1383	else if (ret == 0 && realpeer->status != Active
1384	         && realpeer->status != OpenSent
1385		 && realpeer->status != OpenConfirm
1386		 && realpeer->status != Connect)
1387 	{
1388 	  /* XXX: This is an awful problem..
1389 	   *
1390 	   * According to the RFC we should just let this connection (of the
1391 	   * accepted 'peer') continue on to Established if the other
1392 	   * connection (the 'realpeer' one) is in state Connect, and deal
1393 	   * with the more larval FSM as/when it gets far enough to receive
1394 	   * an Open. We don't do that though, we instead close the (more
1395 	   * developed) accepted connection.
1396 	   *
1397 	   * This means there's a race, which if hit, can loop:
1398 	   *
1399 	   *       FSM for A                        FSM for B
1400 	   *  realpeer     accept-peer       realpeer     accept-peer
1401 	   *
1402 	   *  Connect                        Connect
1403 	   *               Active
1404 	   *               OpenSent          OpenSent
1405 	   *               <arrive here,
1406 	   *               Notify, delete>
1407 	   *                                 Idle         Active
1408 	   *   OpenSent                                   OpenSent
1409 	   *                                              <arrive here,
1410 	   *                                              Notify, delete>
1411 	   *   Idle
1412 	   *   <wait>                        <wait>
1413 	   *   Connect                       Connect
1414 	   *
1415           *
1416 	   * If both sides are Quagga, they're almost certain to wait for
1417 	   * the same amount of time of course (which doesn't preclude other
1418 	   * implementations also waiting for same time). The race is
1419 	   * exacerbated by high-latency (in bgpd and/or the network).
1420 	   *
1421 	   * The reason we do this is because our FSM is tied to our peer
1422 	   * structure, which carries our configuration information, etc.
1423 	   * I.e. we can't let the accepted-peer FSM continue on as it is,
1424 	   * cause it's not associated with any actual peer configuration -
1425 	   * it's just a dummy.
1426 	   *
1427 	   * It's possible we could hack-fix this by just bgp_stop'ing the
1428 	   * realpeer and continueing on with the 'transfer FSM' below.
1429 	   * Ideally, we need to seperate FSMs from struct peer.
1430 	   *
1431 	   * Setting one side to passive avoids the race, as a workaround.
1432 	   */
1433 	  if (BGP_DEBUG (events, EVENTS))
1434	    zlog_debug ("%s peer status is %s close connection",
1435			realpeer->host, LOOKUP (bgp_status_msg,
1436			realpeer->status));
1437	  bgp_notify_send (peer, BGP_NOTIFY_CEASE,
1438			   BGP_NOTIFY_CEASE_CONNECT_REJECT);
1439
1440 	  return -1;
1441 	}
1442
1443      if (BGP_DEBUG (events, EVENTS))
1444	zlog_debug ("%s [Event] Transfer accept BGP peer to real (state %s)",
1445		   peer->host,
1446		   LOOKUP (bgp_status_msg, realpeer->status));
1447
1448      bgp_stop (realpeer);
1449
1450      /* Transfer file descriptor. */
1451      realpeer->fd = peer->fd;
1452      peer->fd = -1;
1453
1454      /* Transfer input buffer. */
1455      stream_free (realpeer->ibuf);
1456      realpeer->ibuf = peer->ibuf;
1457      realpeer->packet_size = peer->packet_size;
1458      peer->ibuf = NULL;
1459
1460      /* Transfer status. */
1461      realpeer->status = peer->status;
1462      bgp_stop (peer);
1463
1464      /* peer pointer change. Open packet send to neighbor. */
1465      peer = realpeer;
1466      bgp_open_send (peer);
1467      if (peer->fd < 0)
1468	{
1469	  zlog_err ("bgp_open_receive peer's fd is negative value %d",
1470		    peer->fd);
1471	  return -1;
1472	}
1473      BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
1474    }
1475
1476  /* remote router-id check. */
1477  if (remote_id.s_addr == 0
1478      || IPV4_CLASS_DE (ntohl (remote_id.s_addr))
1479      || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
1480    {
1481      if (BGP_DEBUG (normal, NORMAL))
1482	zlog_debug ("%s bad OPEN, wrong router identifier %s",
1483		   peer->host, inet_ntoa (remote_id));
1484      bgp_notify_send_with_data (peer,
1485				 BGP_NOTIFY_OPEN_ERR,
1486				 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1487				 notify_data_remote_id, 4);
1488      return -1;
1489    }
1490
1491  /* Set remote router-id */
1492  peer->remote_id = remote_id;
1493
1494  /* Peer BGP version check. */
1495  if (version != BGP_VERSION_4)
1496    {
1497      u_int16_t maxver = htons(BGP_VERSION_4);
1498      /* XXX this reply may not be correct if version < 4  XXX */
1499      if (BGP_DEBUG (normal, NORMAL))
1500	zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
1501		   peer->host, version, BGP_VERSION_4);
1502      /* Data must be in network byte order here */
1503      bgp_notify_send_with_data (peer,
1504				 BGP_NOTIFY_OPEN_ERR,
1505				 BGP_NOTIFY_OPEN_UNSUP_VERSION,
1506				 (u_int8_t *) &maxver, 2);
1507      return -1;
1508    }
1509
1510  /* Check neighbor as number. */
1511  if (remote_as != peer->as)
1512    {
1513      if (BGP_DEBUG (normal, NORMAL))
1514	zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
1515		   peer->host, remote_as, peer->as);
1516      bgp_notify_send_with_data (peer,
1517				 BGP_NOTIFY_OPEN_ERR,
1518				 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1519				 notify_data_remote_as, 2);
1520      return -1;
1521    }
1522
1523  /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1524     calculate the value of the Hold Timer by using the smaller of its
1525     configured Hold Time and the Hold Time received in the OPEN message.
1526     The Hold Time MUST be either zero or at least three seconds.  An
1527     implementation may reject connections on the basis of the Hold Time. */
1528
1529  if (holdtime < 3 && holdtime != 0)
1530    {
1531      bgp_notify_send (peer,
1532		       BGP_NOTIFY_OPEN_ERR,
1533		       BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
1534      return -1;
1535    }
1536
1537  /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1538     would be one third of the Hold Time interval.  KEEPALIVE messages
1539     MUST NOT be sent more frequently than one per second.  An
1540     implementation MAY adjust the rate at which it sends KEEPALIVE
1541     messages as a function of the Hold Time interval. */
1542
1543  if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
1544    send_holdtime = peer->holdtime;
1545  else
1546    send_holdtime = peer->bgp->default_holdtime;
1547
1548  if (holdtime < send_holdtime)
1549    peer->v_holdtime = holdtime;
1550  else
1551    peer->v_holdtime = send_holdtime;
1552
1553  peer->v_keepalive = peer->v_holdtime / 3;
1554
1555  /* Open option part parse. */
1556  if (optlen != 0)
1557    {
1558      if ((ret = bgp_open_option_parse (peer, optlen, &mp_capability)) < 0)
1559        {
1560          bgp_notify_send (peer,
1561                 BGP_NOTIFY_OPEN_ERR,
1562                 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
1563	  return ret;
1564        }
1565    }
1566  else
1567    {
1568      if (BGP_DEBUG (normal, NORMAL))
1569	zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
1570		   peer->host);
1571    }
1572
1573  /*
1574   * Assume that the peer supports the locally configured set of
1575   * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1576   * capabilities, or if 'override-capability' is configured.
1577   */
1578  if (! mp_capability ||
1579      CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1580    {
1581      peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
1582      peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
1583      peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
1584      peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
1585    }
1586
1587  /* Get sockname. */
1588  bgp_getsockname (peer);
1589
1590  BGP_EVENT_ADD (peer, Receive_OPEN_message);
1591
1592  peer->packet_size = 0;
1593  if (peer->ibuf)
1594    stream_reset (peer->ibuf);
1595
1596  return 0;
1597}
1598
1599/* Parse BGP Update packet and make attribute object. */
1600static int
1601bgp_update_receive (struct peer *peer, bgp_size_t size)
1602{
1603  int ret;
1604  u_char *end;
1605  struct stream *s;
1606  struct attr attr;
1607  struct attr_extra extra;
1608  bgp_size_t attribute_len;
1609  bgp_size_t update_len;
1610  bgp_size_t withdraw_len;
1611  struct bgp_nlri update;
1612  struct bgp_nlri withdraw;
1613  struct bgp_nlri mp_update;
1614  struct bgp_nlri mp_withdraw;
1615
1616  /* Status must be Established. */
1617  if (peer->status != Established)
1618    {
1619      zlog_err ("%s [FSM] Update packet received under status %s",
1620		peer->host, LOOKUP (bgp_status_msg, peer->status));
1621      bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1622      return -1;
1623    }
1624
1625  /* Set initial values. */
1626  memset (&attr, 0, sizeof (struct attr));
1627  memset (&extra, 0, sizeof (struct attr_extra));
1628  memset (&update, 0, sizeof (struct bgp_nlri));
1629  memset (&withdraw, 0, sizeof (struct bgp_nlri));
1630  memset (&mp_update, 0, sizeof (struct bgp_nlri));
1631  memset (&mp_withdraw, 0, sizeof (struct bgp_nlri));
1632  attr.extra = &extra;
1633
1634  s = peer->ibuf;
1635  end = stream_pnt (s) + size;
1636
1637  /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1638     Length is too large (i.e., if Unfeasible Routes Length + Total
1639     Attribute Length + 23 exceeds the message Length), then the Error
1640     Subcode is set to Malformed Attribute List.  */
1641  if (stream_pnt (s) + 2 > end)
1642    {
1643      zlog_err ("%s [Error] Update packet error"
1644		" (packet length is short for unfeasible length)",
1645		peer->host);
1646      bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1647		       BGP_NOTIFY_UPDATE_MAL_ATTR);
1648      return -1;
1649    }
1650
1651  /* Unfeasible Route Length. */
1652  withdraw_len = stream_getw (s);
1653
1654  /* Unfeasible Route Length check. */
1655  if (stream_pnt (s) + withdraw_len > end)
1656    {
1657      zlog_err ("%s [Error] Update packet error"
1658		" (packet unfeasible length overflow %d)",
1659		peer->host, withdraw_len);
1660      bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1661		       BGP_NOTIFY_UPDATE_MAL_ATTR);
1662      return -1;
1663    }
1664
1665  /* Unfeasible Route packet format check. */
1666  if (withdraw_len > 0)
1667    {
1668      ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len);
1669      if (ret < 0)
1670	return -1;
1671
1672      if (BGP_DEBUG (packet, PACKET_RECV))
1673	zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer->host);
1674
1675      withdraw.afi = AFI_IP;
1676      withdraw.safi = SAFI_UNICAST;
1677      withdraw.nlri = stream_pnt (s);
1678      withdraw.length = withdraw_len;
1679      stream_forward_getp (s, withdraw_len);
1680    }
1681
1682  /* Attribute total length check. */
1683  if (stream_pnt (s) + 2 > end)
1684    {
1685      zlog_warn ("%s [Error] Packet Error"
1686		 " (update packet is short for attribute length)",
1687		 peer->host);
1688      bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1689		       BGP_NOTIFY_UPDATE_MAL_ATTR);
1690      return -1;
1691    }
1692
1693  /* Fetch attribute total length. */
1694  attribute_len = stream_getw (s);
1695
1696  /* Attribute length check. */
1697  if (stream_pnt (s) + attribute_len > end)
1698    {
1699      zlog_warn ("%s [Error] Packet Error"
1700		 " (update packet attribute length overflow %d)",
1701		 peer->host, attribute_len);
1702      bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1703		       BGP_NOTIFY_UPDATE_MAL_ATTR);
1704      return -1;
1705    }
1706
1707  /* Certain attribute parsing errors should not be considered bad enough
1708   * to reset the session for, most particularly any partial/optional
1709   * attributes that have 'tunneled' over speakers that don't understand
1710   * them. Instead we withdraw only the prefix concerned.
1711   *
1712   * Complicates the flow a little though..
1713   */
1714  bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
1715  /* This define morphs the update case into a withdraw when lower levels
1716   * have signalled an error condition where this is best.
1717   */
1718#define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1719
1720  /* Parse attribute when it exists. */
1721  if (attribute_len)
1722    {
1723      attr_parse_ret = bgp_attr_parse (peer, &attr, attribute_len,
1724			    &mp_update, &mp_withdraw);
1725      if (attr_parse_ret == BGP_ATTR_PARSE_ERROR)
1726	{
1727	  bgp_attr_unintern_sub (&attr);
1728	  return -1;
1729	}
1730    }
1731
1732  /* Logging the attribute. */
1733  if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
1734      || BGP_DEBUG (update, UPDATE_IN))
1735    {
1736      char attrstr[BUFSIZ];
1737      attrstr[0] = '\0';
1738
1739      ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
1740      int lvl = (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1741                 ? LOG_ERR : LOG_DEBUG;
1742
1743      if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1744        zlog (peer->log, LOG_ERR,
1745              "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1746              peer->host);
1747
1748      if (ret)
1749	zlog (peer->log, lvl, "%s rcvd UPDATE w/ attr: %s",
1750	      peer->host, attrstr);
1751    }
1752
1753  /* Network Layer Reachability Information. */
1754  update_len = end - stream_pnt (s);
1755
1756  if (update_len)
1757    {
1758      /* Check NLRI packet format and prefix length. */
1759      ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len);
1760      if (ret < 0)
1761        {
1762          bgp_attr_unintern_sub (&attr);
1763	  return -1;
1764	}
1765
1766      /* Set NLRI portion to structure. */
1767      update.afi = AFI_IP;
1768      update.safi = SAFI_UNICAST;
1769      update.nlri = stream_pnt (s);
1770      update.length = update_len;
1771      stream_forward_getp (s, update_len);
1772    }
1773
1774  /* NLRI is processed only when the peer is configured specific
1775     Address Family and Subsequent Address Family. */
1776  if (peer->afc[AFI_IP][SAFI_UNICAST])
1777    {
1778      if (withdraw.length)
1779	bgp_nlri_parse (peer, NULL, &withdraw);
1780
1781      if (update.length)
1782	  bgp_nlri_parse (peer, NLRI_ATTR_ARG, &update);
1783
1784      if (mp_update.length
1785	  && mp_update.afi == AFI_IP
1786	  && mp_update.safi == SAFI_UNICAST)
1787	bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
1788
1789      if (mp_withdraw.length
1790	  && mp_withdraw.afi == AFI_IP
1791	  && mp_withdraw.safi == SAFI_UNICAST)
1792	bgp_nlri_parse (peer, NULL, &mp_withdraw);
1793
1794      if (! attribute_len && ! withdraw_len)
1795	{
1796	  /* End-of-RIB received */
1797	  SET_FLAG (peer->af_sflags[AFI_IP][SAFI_UNICAST],
1798		    PEER_STATUS_EOR_RECEIVED);
1799
1800	  /* NSF delete stale route */
1801	  if (peer->nsf[AFI_IP][SAFI_UNICAST])
1802	    bgp_clear_stale_route (peer, AFI_IP, SAFI_UNICAST);
1803
1804	  if (BGP_DEBUG (normal, NORMAL))
1805	    zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Unicast from %s",
1806		  peer->host);
1807	}
1808    }
1809  if (peer->afc[AFI_IP][SAFI_MULTICAST])
1810    {
1811      if (mp_update.length
1812	  && mp_update.afi == AFI_IP
1813	  && mp_update.safi == SAFI_MULTICAST)
1814	bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
1815
1816      if (mp_withdraw.length
1817	  && mp_withdraw.afi == AFI_IP
1818	  && mp_withdraw.safi == SAFI_MULTICAST)
1819	bgp_nlri_parse (peer, NULL, &mp_withdraw);
1820
1821      if (! withdraw_len
1822	  && mp_withdraw.afi == AFI_IP
1823	  && mp_withdraw.safi == SAFI_MULTICAST
1824	  && mp_withdraw.length == 0)
1825	{
1826	  /* End-of-RIB received */
1827	  SET_FLAG (peer->af_sflags[AFI_IP][SAFI_MULTICAST],
1828		    PEER_STATUS_EOR_RECEIVED);
1829
1830	  /* NSF delete stale route */
1831	  if (peer->nsf[AFI_IP][SAFI_MULTICAST])
1832	    bgp_clear_stale_route (peer, AFI_IP, SAFI_MULTICAST);
1833
1834	  if (BGP_DEBUG (normal, NORMAL))
1835	    zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Multicast from %s",
1836		  peer->host);
1837	}
1838    }
1839  if (peer->afc[AFI_IP6][SAFI_UNICAST])
1840    {
1841      if (mp_update.length
1842	  && mp_update.afi == AFI_IP6
1843	  && mp_update.safi == SAFI_UNICAST)
1844	bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
1845
1846      if (mp_withdraw.length
1847	  && mp_withdraw.afi == AFI_IP6
1848	  && mp_withdraw.safi == SAFI_UNICAST)
1849	bgp_nlri_parse (peer, NULL, &mp_withdraw);
1850
1851      if (! withdraw_len
1852	  && mp_withdraw.afi == AFI_IP6
1853	  && mp_withdraw.safi == SAFI_UNICAST
1854	  && mp_withdraw.length == 0)
1855	{
1856	  /* End-of-RIB received */
1857	  SET_FLAG (peer->af_sflags[AFI_IP6][SAFI_UNICAST], PEER_STATUS_EOR_RECEIVED);
1858
1859	  /* NSF delete stale route */
1860	  if (peer->nsf[AFI_IP6][SAFI_UNICAST])
1861	    bgp_clear_stale_route (peer, AFI_IP6, SAFI_UNICAST);
1862
1863	  if (BGP_DEBUG (normal, NORMAL))
1864	    zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Unicast from %s",
1865		  peer->host);
1866	}
1867    }
1868  if (peer->afc[AFI_IP6][SAFI_MULTICAST])
1869    {
1870      if (mp_update.length
1871	  && mp_update.afi == AFI_IP6
1872	  && mp_update.safi == SAFI_MULTICAST)
1873	bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
1874
1875      if (mp_withdraw.length
1876	  && mp_withdraw.afi == AFI_IP6
1877	  && mp_withdraw.safi == SAFI_MULTICAST)
1878	bgp_nlri_parse (peer, NULL, &mp_withdraw);
1879
1880      if (! withdraw_len
1881	  && mp_withdraw.afi == AFI_IP6
1882	  && mp_withdraw.safi == SAFI_MULTICAST
1883	  && mp_withdraw.length == 0)
1884	{
1885	  /* End-of-RIB received */
1886
1887	  /* NSF delete stale route */
1888	  if (peer->nsf[AFI_IP6][SAFI_MULTICAST])
1889	    bgp_clear_stale_route (peer, AFI_IP6, SAFI_MULTICAST);
1890
1891	  if (BGP_DEBUG (update, UPDATE_IN))
1892	    zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Multicast from %s",
1893		  peer->host);
1894	}
1895    }
1896  if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
1897    {
1898      if (mp_update.length
1899	  && mp_update.afi == AFI_IP
1900	  && mp_update.safi == SAFI_MPLS_LABELED_VPN)
1901	bgp_nlri_parse_vpnv4 (peer, NLRI_ATTR_ARG, &mp_update);
1902
1903      if (mp_withdraw.length
1904	  && mp_withdraw.afi == AFI_IP
1905	  && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN)
1906	bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw);
1907
1908      if (! withdraw_len
1909	  && mp_withdraw.afi == AFI_IP
1910	  && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN
1911	  && mp_withdraw.length == 0)
1912	{
1913	  /* End-of-RIB received */
1914
1915	  if (BGP_DEBUG (update, UPDATE_IN))
1916	    zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for VPNv4 Unicast from %s",
1917		  peer->host);
1918	}
1919    }
1920
1921  /* Everything is done.  We unintern temporary structures which
1922     interned in bgp_attr_parse(). */
1923  bgp_attr_unintern_sub (&attr);
1924
1925  /* If peering is stopped due to some reason, do not generate BGP
1926     event.  */
1927  if (peer->status != Established)
1928    return 0;
1929
1930  /* Increment packet counter. */
1931  peer->update_in++;
1932  peer->update_time = bgp_clock ();
1933
1934  /* Rearm holdtime timer */
1935  BGP_TIMER_OFF (peer->t_holdtime);
1936  bgp_timer_set (peer);
1937
1938  return 0;
1939}
1940
1941/* Notify message treatment function. */
1942static void
1943bgp_notify_receive (struct peer *peer, bgp_size_t size)
1944{
1945  struct bgp_notify bgp_notify;
1946
1947  if (peer->notify.data)
1948    {
1949      XFREE (MTYPE_TMP, peer->notify.data);
1950      peer->notify.data = NULL;
1951      peer->notify.length = 0;
1952    }
1953
1954  bgp_notify.code = stream_getc (peer->ibuf);
1955  bgp_notify.subcode = stream_getc (peer->ibuf);
1956  bgp_notify.length = size - 2;
1957  bgp_notify.data = NULL;
1958
1959  /* Preserv notify code and sub code. */
1960  peer->notify.code = bgp_notify.code;
1961  peer->notify.subcode = bgp_notify.subcode;
1962  /* For further diagnostic record returned Data. */
1963  if (bgp_notify.length)
1964    {
1965      peer->notify.length = size - 2;
1966      peer->notify.data = XMALLOC (MTYPE_TMP, size - 2);
1967      memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2);
1968    }
1969
1970  /* For debug */
1971  {
1972    int i;
1973    int first = 0;
1974    char c[4];
1975
1976    if (bgp_notify.length)
1977      {
1978	bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
1979	for (i = 0; i < bgp_notify.length; i++)
1980	  if (first)
1981	    {
1982	      sprintf (c, " %02x", stream_getc (peer->ibuf));
1983	      strcat (bgp_notify.data, c);
1984	    }
1985	  else
1986	    {
1987	      first = 1;
1988	      sprintf (c, "%02x", stream_getc (peer->ibuf));
1989	      strcpy (bgp_notify.data, c);
1990	    }
1991      }
1992
1993    bgp_notify_print(peer, &bgp_notify, "received");
1994    if (bgp_notify.data)
1995      XFREE (MTYPE_TMP, bgp_notify.data);
1996  }
1997
1998  /* peer count update */
1999  peer->notify_in++;
2000
2001  if (peer->status == Established)
2002    peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
2003
2004  /* We have to check for Notify with Unsupported Optional Parameter.
2005     in that case we fallback to open without the capability option.
2006     But this done in bgp_stop. We just mark it here to avoid changing
2007     the fsm tables.  */
2008  if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
2009      bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM )
2010    UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
2011
2012  BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message);
2013}
2014
2015/* Keepalive treatment function -- get keepalive send keepalive */
2016static void
2017bgp_keepalive_receive (struct peer *peer, bgp_size_t size)
2018{
2019  if (BGP_DEBUG (keepalive, KEEPALIVE))
2020    zlog_debug ("%s KEEPALIVE rcvd", peer->host);
2021
2022  BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message);
2023}
2024
2025/* Route refresh message is received. */
2026static void
2027bgp_route_refresh_receive (struct peer *peer, bgp_size_t size)
2028{
2029  afi_t afi;
2030  safi_t safi;
2031  struct stream *s;
2032
2033  /* If peer does not have the capability, send notification. */
2034  if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV))
2035    {
2036      plog_err (peer->log, "%s [Error] BGP route refresh is not enabled",
2037		peer->host);
2038      bgp_notify_send (peer,
2039		       BGP_NOTIFY_HEADER_ERR,
2040		       BGP_NOTIFY_HEADER_BAD_MESTYPE);
2041      return;
2042    }
2043
2044  /* Status must be Established. */
2045  if (peer->status != Established)
2046    {
2047      plog_err (peer->log,
2048		"%s [Error] Route refresh packet received under status %s",
2049		peer->host, LOOKUP (bgp_status_msg, peer->status));
2050      bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
2051      return;
2052    }
2053
2054  s = peer->ibuf;
2055
2056  /* Parse packet. */
2057  afi = stream_getw (s);
2058  /* reserved byte */
2059  stream_getc (s);
2060  safi = stream_getc (s);
2061
2062  if (BGP_DEBUG (normal, NORMAL))
2063    zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
2064	       peer->host, afi, safi);
2065
2066  /* Check AFI and SAFI. */
2067  if ((afi != AFI_IP && afi != AFI_IP6)
2068      || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
2069	  && safi != SAFI_MPLS_LABELED_VPN))
2070    {
2071      if (BGP_DEBUG (normal, NORMAL))
2072	{
2073	  zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
2074		     peer->host, afi, safi);
2075	}
2076      return;
2077    }
2078
2079  /* Adjust safi code. */
2080  if (safi == SAFI_MPLS_LABELED_VPN)
2081    safi = SAFI_MPLS_VPN;
2082
2083  if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
2084    {
2085      u_char *end;
2086      u_char when_to_refresh;
2087      u_char orf_type;
2088      u_int16_t orf_len;
2089
2090      if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5)
2091        {
2092          zlog_info ("%s ORF route refresh length error", peer->host);
2093          bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2094          return;
2095        }
2096
2097      when_to_refresh = stream_getc (s);
2098      end = stream_pnt (s) + (size - 5);
2099
2100      while ((stream_pnt (s) + 2) < end)
2101	{
2102	  orf_type = stream_getc (s);
2103	  orf_len = stream_getw (s);
2104
2105	  /* orf_len in bounds? */
2106	  if ((stream_pnt (s) + orf_len) > end)
2107	    break; /* XXX: Notify instead?? */
2108	  if (orf_type == ORF_TYPE_PREFIX
2109	      || orf_type == ORF_TYPE_PREFIX_OLD)
2110	    {
2111	      uint8_t *p_pnt = stream_pnt (s);
2112	      uint8_t *p_end = stream_pnt (s) + orf_len;
2113	      struct orf_prefix orfp;
2114	      u_char common = 0;
2115	      u_int32_t seq;
2116	      int psize;
2117	      char name[BUFSIZ];
2118	      int ret;
2119
2120	      if (BGP_DEBUG (normal, NORMAL))
2121		{
2122		  zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
2123			     peer->host, orf_type, orf_len);
2124		}
2125
2126              /* we're going to read at least 1 byte of common ORF header,
2127               * and 7 bytes of ORF Address-filter entry from the stream
2128               */
2129              if (orf_len < 7)
2130                break;
2131
2132	      /* ORF prefix-list name */
2133	      sprintf (name, "%s.%d.%d", peer->host, afi, safi);
2134
2135	      while (p_pnt < p_end)
2136		{
2137                  /* If the ORF entry is malformed, want to read as much of it
2138                   * as possible without going beyond the bounds of the entry,
2139                   * to maximise debug information.
2140                   */
2141		  int ok;
2142		  memset (&orfp, 0, sizeof (struct orf_prefix));
2143		  common = *p_pnt++;
2144		  /* after ++: p_pnt <= p_end */
2145		  if (common & ORF_COMMON_PART_REMOVE_ALL)
2146		    {
2147		      if (BGP_DEBUG (normal, NORMAL))
2148			zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host);
2149		      prefix_bgp_orf_remove_all (name);
2150		      break;
2151		    }
2152		  ok = ((size_t)(p_end - p_pnt) >= sizeof(u_int32_t)) ;
2153		  if (ok)
2154		    {
2155		      memcpy (&seq, p_pnt, sizeof (u_int32_t));
2156                      p_pnt += sizeof (u_int32_t);
2157                      orfp.seq = ntohl (seq);
2158		    }
2159		  else
2160		    p_pnt = p_end ;
2161
2162		  if ((ok = (p_pnt < p_end)))
2163		    orfp.ge = *p_pnt++ ;      /* value checked in prefix_bgp_orf_set() */
2164		  if ((ok = (p_pnt < p_end)))
2165		    orfp.le = *p_pnt++ ;      /* value checked in prefix_bgp_orf_set() */
2166		  if ((ok = (p_pnt < p_end)))
2167		    orfp.p.prefixlen = *p_pnt++ ;
2168		  orfp.p.family = afi2family (afi);   /* afi checked already  */
2169
2170		  psize = PSIZE (orfp.p.prefixlen);   /* 0 if not ok          */
2171		  if (psize > prefix_blen(&orfp.p))   /* valid for family ?   */
2172		    {
2173		      ok = 0 ;
2174		      psize = prefix_blen(&orfp.p) ;
2175		    }
2176		  if (psize > (p_end - p_pnt))        /* valid for packet ?   */
2177		    {
2178		      ok = 0 ;
2179		      psize = p_end - p_pnt ;
2180		    }
2181
2182		  if (psize > 0)
2183		    memcpy (&orfp.p.u.prefix, p_pnt, psize);
2184		  p_pnt += psize;
2185
2186		  if (BGP_DEBUG (normal, NORMAL))
2187		    {
2188		      char buf[INET6_BUFSIZ];
2189
2190		      zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
2191			         peer->host,
2192			         (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
2193			         (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
2194			         orfp.seq,
2195			         inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, INET6_BUFSIZ),
2196			         orfp.p.prefixlen, orfp.ge, orfp.le,
2197			         ok ? "" : " MALFORMED");
2198		    }
2199
2200		  if (ok)
2201		    ret = prefix_bgp_orf_set (name, afi, &orfp,
2202				   (common & ORF_COMMON_PART_DENY ? 0 : 1 ),
2203				   (common & ORF_COMMON_PART_REMOVE ? 0 : 1));
2204
2205		  if (!ok || (ok && ret != CMD_SUCCESS))
2206		    {
2207		      if (BGP_DEBUG (normal, NORMAL))
2208			zlog_debug ("%s Received misformatted prefixlist ORF."
2209			            " Remove All pfxlist", peer->host);
2210		      prefix_bgp_orf_remove_all (name);
2211		      break;
2212		    }
2213		}
2214	      peer->orf_plist[afi][safi] =
2215			 prefix_list_lookup (AFI_ORF_PREFIX, name);
2216	    }
2217	  stream_forward_getp (s, orf_len);
2218	}
2219      if (BGP_DEBUG (normal, NORMAL))
2220	zlog_debug ("%s rcvd Refresh %s ORF request", peer->host,
2221		   when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate");
2222      if (when_to_refresh == REFRESH_DEFER)
2223	return;
2224    }
2225
2226  /* First update is deferred until ORF or ROUTE-REFRESH is received */
2227  if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
2228    UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
2229
2230  /* Perform route refreshment to the peer */
2231  bgp_announce_route (peer, afi, safi);
2232}
2233
2234static int
2235bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length)
2236{
2237  u_char *end;
2238  struct capability_mp_data mpc;
2239  struct capability_header *hdr;
2240  u_char action;
2241  afi_t afi;
2242  safi_t safi;
2243
2244  end = pnt + length;
2245
2246  while (pnt < end)
2247    {
2248      /* We need at least action, capability code and capability length. */
2249      if (pnt + 3 > end)
2250        {
2251          zlog_info ("%s Capability length error", peer->host);
2252          bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2253          return -1;
2254        }
2255      action = *pnt;
2256      hdr = (struct capability_header *)(pnt + 1);
2257
2258      /* Action value check.  */
2259      if (action != CAPABILITY_ACTION_SET
2260	  && action != CAPABILITY_ACTION_UNSET)
2261        {
2262          zlog_info ("%s Capability Action Value error %d",
2263		     peer->host, action);
2264          bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2265          return -1;
2266        }
2267
2268      if (BGP_DEBUG (normal, NORMAL))
2269	zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
2270		   peer->host, action, hdr->code, hdr->length);
2271
2272      /* Capability length check. */
2273      if ((pnt + hdr->length + 3) > end)
2274        {
2275          zlog_info ("%s Capability length error", peer->host);
2276          bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2277          return -1;
2278        }
2279
2280      /* Fetch structure to the byte stream. */
2281      memcpy (&mpc, pnt + 3, sizeof (struct capability_mp_data));
2282
2283      /* We know MP Capability Code. */
2284      if (hdr->code == CAPABILITY_CODE_MP)
2285        {
2286	  afi = ntohs (mpc.afi);
2287	  safi = mpc.safi;
2288
2289          /* Ignore capability when override-capability is set. */
2290          if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
2291	    continue;
2292
2293          if (!bgp_afi_safi_valid_indices (afi, &safi))
2294            {
2295              if (BGP_DEBUG (normal, NORMAL))
2296                zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
2297                            "(%u/%u)", peer->host, afi, safi);
2298              continue;
2299            }
2300
2301	  /* Address family check.  */
2302          if (BGP_DEBUG (normal, NORMAL))
2303            zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2304                       peer->host,
2305                       action == CAPABILITY_ACTION_SET
2306                       ? "Advertising" : "Removing",
2307                       ntohs(mpc.afi) , mpc.safi);
2308
2309          if (action == CAPABILITY_ACTION_SET)
2310            {
2311              peer->afc_recv[afi][safi] = 1;
2312              if (peer->afc[afi][safi])
2313                {
2314                  peer->afc_nego[afi][safi] = 1;
2315                  bgp_announce_route (peer, afi, safi);
2316                }
2317            }
2318          else
2319            {
2320              peer->afc_recv[afi][safi] = 0;
2321              peer->afc_nego[afi][safi] = 0;
2322
2323              if (peer_active_nego (peer))
2324                bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_NORMAL);
2325              else
2326                BGP_EVENT_ADD (peer, BGP_Stop);
2327            }
2328        }
2329      else
2330        {
2331          zlog_warn ("%s unrecognized capability code: %d - ignored",
2332                     peer->host, hdr->code);
2333        }
2334      pnt += hdr->length + 3;
2335    }
2336  return 0;
2337}
2338
2339/* Dynamic Capability is received.
2340 *
2341 * This is exported for unit-test purposes
2342 */
2343int
2344bgp_capability_receive (struct peer *peer, bgp_size_t size)
2345{
2346  u_char *pnt;
2347
2348  /* Fetch pointer. */
2349  pnt = stream_pnt (peer->ibuf);
2350
2351  if (BGP_DEBUG (normal, NORMAL))
2352    zlog_debug ("%s rcv CAPABILITY", peer->host);
2353
2354  /* If peer does not have the capability, send notification. */
2355  if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV))
2356    {
2357      plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled",
2358		peer->host);
2359      bgp_notify_send (peer,
2360		       BGP_NOTIFY_HEADER_ERR,
2361		       BGP_NOTIFY_HEADER_BAD_MESTYPE);
2362      return -1;
2363    }
2364
2365  /* Status must be Established. */
2366  if (peer->status != Established)
2367    {
2368      plog_err (peer->log,
2369		"%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status));
2370      bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
2371      return -1;
2372    }
2373
2374  /* Parse packet. */
2375  return bgp_capability_msg_parse (peer, pnt, size);
2376}
2377
2378/* BGP read utility function. */
2379static int
2380bgp_read_packet (struct peer *peer)
2381{
2382  int nbytes;
2383  int readsize;
2384
2385  readsize = peer->packet_size - stream_get_endp (peer->ibuf);
2386
2387  /* If size is zero then return. */
2388  if (! readsize)
2389    return 0;
2390
2391  /* Read packet from fd. */
2392  nbytes = stream_read_try (peer->ibuf, peer->fd, readsize);
2393
2394  /* If read byte is smaller than zero then error occured. */
2395  if (nbytes < 0)
2396    {
2397      /* Transient error should retry */
2398      if (nbytes == -2)
2399	return -1;
2400
2401      plog_err (peer->log, "%s [Error] bgp_read_packet error: %s",
2402		 peer->host, safe_strerror (errno));
2403
2404      if (peer->status == Established)
2405	{
2406	  if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
2407	    {
2408	      peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2409	      SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
2410	    }
2411	  else
2412	    peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2413	}
2414
2415      BGP_EVENT_ADD (peer, TCP_fatal_error);
2416      return -1;
2417    }
2418
2419  /* When read byte is zero : clear bgp peer and return */
2420  if (nbytes == 0)
2421    {
2422      if (BGP_DEBUG (events, EVENTS))
2423	plog_debug (peer->log, "%s [Event] BGP connection closed fd %d",
2424		   peer->host, peer->fd);
2425
2426      if (peer->status == Established)
2427	{
2428	  if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
2429	    {
2430	      peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2431	      SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
2432	    }
2433	  else
2434	    peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2435	}
2436
2437      BGP_EVENT_ADD (peer, TCP_connection_closed);
2438      return -1;
2439    }
2440
2441  /* We read partial packet. */
2442  if (stream_get_endp (peer->ibuf) != peer->packet_size)
2443    return -1;
2444
2445  return 0;
2446}
2447
2448/* Marker check. */
2449static int
2450bgp_marker_all_one (struct stream *s, int length)
2451{
2452  int i;
2453
2454  for (i = 0; i < length; i++)
2455    if (s->data[i] != 0xff)
2456      return 0;
2457
2458  return 1;
2459}
2460
2461/* Recent thread time.
2462   On same clock base as bgp_clock (MONOTONIC)
2463   but can be time of last context switch to bgp_read thread. */
2464static time_t
2465bgp_recent_clock (void)
2466{
2467  return recent_relative_time().tv_sec;
2468}
2469
2470/* Starting point of packet process function. */
2471int
2472bgp_read (struct thread *thread)
2473{
2474  int ret;
2475  u_char type = 0;
2476  struct peer *peer;
2477  bgp_size_t size;
2478  char notify_data_length[2];
2479
2480  /* Yes first of all get peer pointer. */
2481  peer = THREAD_ARG (thread);
2482  peer->t_read = NULL;
2483
2484  /* For non-blocking IO check. */
2485  if (peer->status == Connect)
2486    {
2487      bgp_connect_check (peer);
2488      goto done;
2489    }
2490  else
2491    {
2492      if (peer->fd < 0)
2493	{
2494	  zlog_err ("bgp_read peer's fd is negative value %d", peer->fd);
2495	  return -1;
2496	}
2497      BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
2498    }
2499
2500  /* Read packet header to determine type of the packet */
2501  if (peer->packet_size == 0)
2502    peer->packet_size = BGP_HEADER_SIZE;
2503
2504  if (stream_get_endp (peer->ibuf) < BGP_HEADER_SIZE)
2505    {
2506      ret = bgp_read_packet (peer);
2507
2508      /* Header read error or partial read packet. */
2509      if (ret < 0)
2510	goto done;
2511
2512      /* Get size and type. */
2513      stream_forward_getp (peer->ibuf, BGP_MARKER_SIZE);
2514      memcpy (notify_data_length, stream_pnt (peer->ibuf), 2);
2515      size = stream_getw (peer->ibuf);
2516      type = stream_getc (peer->ibuf);
2517
2518      if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0)
2519	zlog_debug ("%s rcv message type %d, length (excl. header) %d",
2520		   peer->host, type, size - BGP_HEADER_SIZE);
2521
2522      /* Marker check */
2523      if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE))
2524	  && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE))
2525	{
2526	  bgp_notify_send (peer,
2527			   BGP_NOTIFY_HEADER_ERR,
2528			   BGP_NOTIFY_HEADER_NOT_SYNC);
2529	  goto done;
2530	}
2531
2532      /* BGP type check. */
2533      if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
2534	  && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
2535	  && type != BGP_MSG_ROUTE_REFRESH_NEW
2536	  && type != BGP_MSG_ROUTE_REFRESH_OLD
2537	  && type != BGP_MSG_CAPABILITY)
2538	{
2539	  if (BGP_DEBUG (normal, NORMAL))
2540	    plog_debug (peer->log,
2541		      "%s unknown message type 0x%02x",
2542		      peer->host, type);
2543	  bgp_notify_send_with_data (peer,
2544				     BGP_NOTIFY_HEADER_ERR,
2545			 	     BGP_NOTIFY_HEADER_BAD_MESTYPE,
2546				     &type, 1);
2547	  goto done;
2548	}
2549      /* Mimimum packet length check. */
2550      if ((size < BGP_HEADER_SIZE)
2551	  || (size > BGP_MAX_PACKET_SIZE)
2552	  || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
2553	  || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE)
2554	  || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE)
2555	  || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
2556	  || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2557	  || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2558	  || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE))
2559	{
2560	  if (BGP_DEBUG (normal, NORMAL))
2561	    plog_debug (peer->log,
2562		      "%s bad message length - %d for %s",
2563		      peer->host, size,
2564		      type == 128 ? "ROUTE-REFRESH" :
2565		      bgp_type_str[(int) type]);
2566	  bgp_notify_send_with_data (peer,
2567				     BGP_NOTIFY_HEADER_ERR,
2568			  	     BGP_NOTIFY_HEADER_BAD_MESLEN,
2569				     (u_char *) notify_data_length, 2);
2570	  goto done;
2571	}
2572
2573      /* Adjust size to message length. */
2574      peer->packet_size = size;
2575    }
2576
2577  ret = bgp_read_packet (peer);
2578  if (ret < 0)
2579    goto done;
2580
2581  /* Get size and type again. */
2582  size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE);
2583  type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2);
2584
2585  /* BGP packet dump function. */
2586  bgp_dump_packet (peer, type, peer->ibuf);
2587
2588  size = (peer->packet_size - BGP_HEADER_SIZE);
2589
2590  /* Read rest of the packet and call each sort of packet routine */
2591  switch (type)
2592    {
2593    case BGP_MSG_OPEN:
2594      peer->open_in++;
2595      bgp_open_receive (peer, size); /* XXX return value ignored! */
2596      break;
2597    case BGP_MSG_UPDATE:
2598      peer->readtime = bgp_recent_clock ();
2599      bgp_update_receive (peer, size);
2600      break;
2601    case BGP_MSG_NOTIFY:
2602      bgp_notify_receive (peer, size);
2603      break;
2604    case BGP_MSG_KEEPALIVE:
2605      peer->readtime = bgp_recent_clock ();
2606      bgp_keepalive_receive (peer, size);
2607      break;
2608    case BGP_MSG_ROUTE_REFRESH_NEW:
2609    case BGP_MSG_ROUTE_REFRESH_OLD:
2610      peer->refresh_in++;
2611      bgp_route_refresh_receive (peer, size);
2612      break;
2613    case BGP_MSG_CAPABILITY:
2614      peer->dynamic_cap_in++;
2615      bgp_capability_receive (peer, size);
2616      break;
2617    }
2618
2619  /* Clear input buffer. */
2620  peer->packet_size = 0;
2621  if (peer->ibuf)
2622    stream_reset (peer->ibuf);
2623
2624 done:
2625  if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
2626    {
2627      if (BGP_DEBUG (events, EVENTS))
2628	zlog_debug ("%s [Event] Accepting BGP peer delete", peer->host);
2629      peer_delete (peer);
2630    }
2631  return 0;
2632}
2633