blob: 7d921bed80a6d8ad1c839ea298ca626baa28ff70 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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
50int stream_put_prefix (struct stream *, struct prefix *);
51
52/* Set up BGP packet marker and packet type. */
53static int
54bgp_packet_set_marker (struct stream *s, u_char type)
55{
56 int i;
57
58 /* Fill in marker. */
59 for (i = 0; i < BGP_MARKER_SIZE; i++)
60 stream_putc (s, 0xff);
61
62 /* Dummy total length. This field is should be filled in later on. */
63 stream_putw (s, 0);
64
65 /* BGP packet type. */
66 stream_putc (s, type);
67
68 /* Return current stream size. */
69 return stream_get_putp (s);
70}
71
72/* Set BGP packet header size entry. If size is zero then use current
73 stream size. */
74static int
75bgp_packet_set_size (struct stream *s)
76{
77 int cp;
78
79 /* Preserve current pointer. */
80 cp = stream_get_putp (s);
81 stream_set_putp (s, BGP_MARKER_SIZE);
82 stream_putw (s, cp);
83
84 /* Write back current pointer. */
85 stream_set_putp (s, cp);
86
87 return cp;
88}
89
90/* Add new packet to the peer. */
91void
92bgp_packet_add (struct peer *peer, struct stream *s)
93{
94 /* Add packet to the end of list. */
95 stream_fifo_push (peer->obuf, s);
96}
97
98/* Free first packet. */
99void
100bgp_packet_delete (struct peer *peer)
101{
102 stream_free (stream_fifo_pop (peer->obuf));
103}
104
105/* Duplicate packet. */
106struct stream *
107bgp_packet_dup (struct stream *s)
108{
109 struct stream *new;
110
111 new = stream_new (stream_get_endp (s));
112
113 new->endp = s->endp;
114 new->putp = s->putp;
115 new->getp = s->getp;
116
117 memcpy (new->data, s->data, stream_get_endp (s));
118
119 return new;
120}
121
122/* Check file descriptor whether connect is established. */
123static void
124bgp_connect_check (struct peer *peer)
125{
126 int status;
127 int slen;
128 int ret;
129
130 /* Anyway I have to reset read and write thread. */
131 BGP_READ_OFF (peer->t_read);
132 BGP_WRITE_OFF (peer->t_write);
133
134 /* Check file descriptor. */
135 slen = sizeof (status);
paul6ad23f02004-02-17 19:45:10 +0000136 ret = getsockopt(*peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen);
paul718e3742002-12-13 20:15:29 +0000137
138 /* If getsockopt is fail, this is fatal error. */
139 if (ret < 0)
140 {
141 zlog (peer->log, LOG_INFO, "can't get sockopt for nonblocking connect");
142 BGP_EVENT_ADD (peer, TCP_fatal_error);
143 return;
144 }
145
146 /* When status is 0 then TCP connection is established. */
147 if (status == 0)
148 {
149 BGP_EVENT_ADD (peer, TCP_connection_open);
150 }
151 else
152 {
153 if (BGP_DEBUG (events, EVENTS))
154 plog_info (peer->log, "%s [Event] Connect failed (%s)",
155 peer->host, strerror (errno));
156 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
157 }
158}
159
160/* Make BGP update packet. */
161struct stream *
162bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi)
163{
164 struct stream *s;
165 struct bgp_adj_out *adj;
166 struct bgp_advertise *adv;
167 struct stream *packet;
168 struct bgp_node *rn = NULL;
169 struct bgp_info *binfo = NULL;
170 bgp_size_t total_attr_len = 0;
171 unsigned long pos;
172 char buf[BUFSIZ];
173 struct prefix_rd *prd = NULL;
174 char *tag = NULL;
175
176 s = peer->work;
177 stream_reset (s);
178
179 adv = FIFO_HEAD (&peer->sync[afi][safi]->update);
180
181 while (adv)
182 {
183 if (adv->rn)
184 rn = adv->rn;
185 adj = adv->adj;
186 if (adv->binfo)
187 binfo = adv->binfo;
188#ifdef MPLS_VPN
189 if (rn)
190 prd = (struct prefix_rd *) &rn->prn->p;
191 if (binfo)
192 tag = binfo->tag;
193#endif /* MPLS_VPN */
194
195 /* When remaining space can't include NLRI and it's length. */
196 if (rn && STREAM_REMAIN (s) <= BGP_NLRI_LENGTH + PSIZE (rn->p.prefixlen))
197 break;
198
199 /* If packet is empty, set attribute. */
200 if (stream_empty (s))
201 {
202 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
203 stream_putw (s, 0);
204 pos = stream_get_putp (s);
205 stream_putw (s, 0);
206 total_attr_len = bgp_packet_attribute (NULL, peer, s,
207 adv->baa->attr,
208 &rn->p, afi, safi,
209 binfo->peer, prd, tag);
210 stream_putw_at (s, pos, total_attr_len);
211 }
212
213 if (afi == AFI_IP && safi == SAFI_UNICAST)
214 stream_put_prefix (s, &rn->p);
215
216 if (BGP_DEBUG (update, UPDATE_OUT))
217 zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d",
218 peer->host,
219 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ),
220 rn->p.prefixlen);
221
222 /* Synchnorize attribute. */
223 if (adj->attr)
224 bgp_attr_unintern (adj->attr);
225 else
226 peer->scount[afi][safi]++;
227
228 adj->attr = bgp_attr_intern (adv->baa->attr);
229
230 adv = bgp_advertise_clean (peer, adj, afi, safi);
231
232 if (! (afi == AFI_IP && safi == SAFI_UNICAST))
233 break;
234 }
235
236 if (! stream_empty (s))
237 {
238 bgp_packet_set_size (s);
239 packet = bgp_packet_dup (s);
240 bgp_packet_add (peer, packet);
paul718e3742002-12-13 20:15:29 +0000241 stream_reset (s);
242 return packet;
243 }
244 return NULL;
245
246}
247
248/* Make BGP withdraw packet. */
249struct stream *
250bgp_withdraw_packet (struct peer *peer, afi_t afi, safi_t safi)
251{
252 struct stream *s;
253 struct stream *packet;
254 struct bgp_adj_out *adj;
255 struct bgp_advertise *adv;
256 struct bgp_node *rn;
257 unsigned long pos;
258 bgp_size_t unfeasible_len;
259 bgp_size_t total_attr_len;
260 char buf[BUFSIZ];
261 struct prefix_rd *prd = NULL;
262
263 s = peer->work;
264 stream_reset (s);
265
266 while ((adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL)
267 {
268 adj = adv->adj;
269 rn = adv->rn;
270#ifdef MPLS_VPN
271 prd = (struct prefix_rd *) &rn->prn->p;
272#endif /* MPLS_VPN */
273
274 if (STREAM_REMAIN (s)
275 <= (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN + PSIZE (rn->p.prefixlen)))
276 break;
277
278 if (stream_empty (s))
279 {
280 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
281 stream_putw (s, 0);
282 }
283
284 if (afi == AFI_IP && safi == SAFI_UNICAST)
285 stream_put_prefix (s, &rn->p);
286 else
287 {
288 pos = stream_get_putp (s);
289 stream_putw (s, 0);
290 total_attr_len
291 = bgp_packet_withdraw (peer, s, &rn->p, afi, safi, prd, NULL);
292
293 /* Set total path attribute length. */
294 stream_putw_at (s, pos, total_attr_len);
295 }
296
297 if (BGP_DEBUG (update, UPDATE_OUT))
298 zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d -- unreachable",
299 peer->host,
300 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ),
301 rn->p.prefixlen);
302
303 peer->scount[afi][safi]--;
304
305 bgp_adj_out_remove (rn, adj, peer, afi, safi);
306 bgp_unlock_node (rn);
307
308 if (! (afi == AFI_IP && safi == SAFI_UNICAST))
309 break;
310 }
311
312 if (! stream_empty (s))
313 {
314 if (afi == AFI_IP && safi == SAFI_UNICAST)
315 {
316 unfeasible_len
317 = stream_get_putp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
318 stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
319 stream_putw (s, 0);
320 }
321 bgp_packet_set_size (s);
322 packet = bgp_packet_dup (s);
323 bgp_packet_add (peer, packet);
324 stream_reset (s);
325 return packet;
326 }
327
328 return NULL;
329}
330
331void
332bgp_default_update_send (struct peer *peer, struct attr *attr,
333 afi_t afi, safi_t safi, struct peer *from)
334{
335 struct stream *s;
336 struct stream *packet;
337 struct prefix p;
338 unsigned long pos;
339 bgp_size_t total_attr_len;
340 char attrstr[BUFSIZ];
341 char buf[BUFSIZ];
342
343#ifdef DISABLE_BGP_ANNOUNCE
344 return;
345#endif /* DISABLE_BGP_ANNOUNCE */
346
347 if (afi == AFI_IP)
348 str2prefix ("0.0.0.0/0", &p);
349#ifdef HAVE_IPV6
350 else
351 str2prefix ("::/0", &p);
352#endif /* HAVE_IPV6 */
353
354 /* Logging the attribute. */
355 if (BGP_DEBUG (update, UPDATE_OUT))
356 {
357 bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
358 zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d %s",
359 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ),
360 p.prefixlen, attrstr);
361 }
362
363 s = stream_new (BGP_MAX_PACKET_SIZE);
364
365 /* Make BGP update packet. */
366 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
367
368 /* Unfeasible Routes Length. */
369 stream_putw (s, 0);
370
371 /* Make place for total attribute length. */
372 pos = stream_get_putp (s);
373 stream_putw (s, 0);
374 total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &p, afi, safi, from, NULL, NULL);
375
376 /* Set Total Path Attribute Length. */
377 stream_putw_at (s, pos, total_attr_len);
378
379 /* NLRI set. */
380 if (p.family == AF_INET && safi == SAFI_UNICAST)
381 stream_put_prefix (s, &p);
382
383 /* Set size. */
384 bgp_packet_set_size (s);
385
386 packet = bgp_packet_dup (s);
387 stream_free (s);
388
389 /* Dump packet if debug option is set. */
390#ifdef DEBUG
391 bgp_packet_dump (packet);
392#endif /* DEBUG */
393
394 /* Add packet to the peer. */
395 bgp_packet_add (peer, packet);
396
paul6ad23f02004-02-17 19:45:10 +0000397 BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
paul718e3742002-12-13 20:15:29 +0000398}
399
400void
401bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi)
402{
403 struct stream *s;
404 struct stream *packet;
405 struct prefix p;
406 unsigned long pos;
407 unsigned long cp;
408 bgp_size_t unfeasible_len;
409 bgp_size_t total_attr_len;
410 char buf[BUFSIZ];
411
412#ifdef DISABLE_BGP_ANNOUNCE
413 return;
414#endif /* DISABLE_BGP_ANNOUNCE */
415
416 if (afi == AFI_IP)
417 str2prefix ("0.0.0.0/0", &p);
418#ifdef HAVE_IPV6
419 else
420 str2prefix ("::/0", &p);
421#endif /* HAVE_IPV6 */
422
423 total_attr_len = 0;
424 pos = 0;
425
426 if (BGP_DEBUG (update, UPDATE_OUT))
427 zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d -- unreachable",
428 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ),
429 p.prefixlen);
430
431 s = stream_new (BGP_MAX_PACKET_SIZE);
432
433 /* Make BGP update packet. */
434 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
435
436 /* Unfeasible Routes Length. */;
437 cp = stream_get_putp (s);
438 stream_putw (s, 0);
439
440 /* Withdrawn Routes. */
441 if (p.family == AF_INET && safi == SAFI_UNICAST)
442 {
443 stream_put_prefix (s, &p);
444
445 unfeasible_len = stream_get_putp (s) - cp - 2;
446
447 /* Set unfeasible len. */
448 stream_putw_at (s, cp, unfeasible_len);
449
450 /* Set total path attribute length. */
451 stream_putw (s, 0);
452 }
453 else
454 {
455 pos = stream_get_putp (s);
456 stream_putw (s, 0);
457 total_attr_len = bgp_packet_withdraw (peer, s, &p, afi, safi, NULL, NULL);
458
459 /* Set total path attribute length. */
460 stream_putw_at (s, pos, total_attr_len);
461 }
462
463 bgp_packet_set_size (s);
464
465 packet = bgp_packet_dup (s);
466 stream_free (s);
467
468 /* Add packet to the peer. */
469 bgp_packet_add (peer, packet);
470
paul6ad23f02004-02-17 19:45:10 +0000471 BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
paul718e3742002-12-13 20:15:29 +0000472}
473
474/* Get next packet to be written. */
475struct stream *
476bgp_write_packet (struct peer *peer)
477{
478 afi_t afi;
479 safi_t safi;
480 struct stream *s = NULL;
481 struct bgp_advertise *adv;
482
483 s = stream_fifo_head (peer->obuf);
484 if (s)
485 return s;
486
487 for (afi = AFI_IP; afi < AFI_MAX; afi++)
488 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
489 {
490 adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw);
491 if (adv)
492 {
493 s = bgp_withdraw_packet (peer, afi, safi);
494 if (s)
495 return s;
496 }
497 }
498
499 for (afi = AFI_IP; afi < AFI_MAX; afi++)
500 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
501 {
502 adv = FIFO_HEAD (&peer->sync[afi][safi]->update);
503 if (adv)
504 {
505 if (adv->binfo && adv->binfo->uptime < peer->synctime)
506 s = bgp_update_packet (peer, afi, safi);
507
508 if (s)
509 return s;
510 }
511 }
512
513 return NULL;
514}
515
516/* Is there partially written packet or updates we can send right
517 now. */
518int
519bgp_write_proceed (struct peer *peer)
520{
521 afi_t afi;
522 safi_t safi;
523 struct bgp_advertise *adv;
524
525 if (stream_fifo_head (peer->obuf))
526 return 1;
527
528 for (afi = AFI_IP; afi < AFI_MAX; afi++)
529 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
530 if (FIFO_HEAD (&peer->sync[afi][safi]->withdraw))
531 return 1;
532
533 for (afi = AFI_IP; afi < AFI_MAX; afi++)
534 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
535 if ((adv = FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL)
536 if (adv->binfo->uptime < peer->synctime)
537 return 1;
538
539 return 0;
540}
541
542/* Write packet to the peer. */
543int
544bgp_write (struct thread *thread)
545{
546 struct peer *peer;
547 u_char type;
548 struct stream *s;
549 int num;
550 int count = 0;
551 int write_errno;
552
553 /* Yes first of all get peer pointer. */
554 peer = THREAD_ARG (thread);
555 peer->t_write = NULL;
556
557 /* For non-blocking IO check. */
558 if (peer->status == Connect)
559 {
560 bgp_connect_check (peer);
561 return 0;
562 }
563
564 /* Nonblocking write until TCP output buffer is full. */
565 while (1)
566 {
567 int writenum;
568
569 s = bgp_write_packet (peer);
570 if (! s)
571 return 0;
572
573 /* Number of bytes to be sent. */
574 writenum = stream_get_endp (s) - stream_get_getp (s);
575
576 /* Call write() system call. */
paul6ad23f02004-02-17 19:45:10 +0000577 num = write (*peer->fd, STREAM_PNT (s), writenum);
paul718e3742002-12-13 20:15:29 +0000578 write_errno = errno;
579 if (num <= 0)
580 {
581 /* Partial write. */
582 if (write_errno == EWOULDBLOCK || write_errno == EAGAIN)
583 break;
584
585 bgp_stop (peer);
586 peer->status = Idle;
587 bgp_timer_set (peer);
588 return 0;
589 }
590 if (num != writenum)
591 {
592 stream_forward (s, num);
593
594 if (write_errno == EAGAIN)
595 break;
596
597 continue;
598 }
599
600 /* Retrieve BGP packet type. */
601 stream_set_getp (s, BGP_MARKER_SIZE + 2);
602 type = stream_getc (s);
603
604 switch (type)
605 {
606 case BGP_MSG_OPEN:
607 peer->open_out++;
608 break;
609 case BGP_MSG_UPDATE:
610 peer->update_out++;
611 break;
612 case BGP_MSG_NOTIFY:
613 peer->notify_out++;
614 /* Double start timer. */
615 peer->v_start *= 2;
616
617 /* Overflow check. */
618 if (peer->v_start >= (60 * 2))
619 peer->v_start = (60 * 2);
620
621 /* BGP_EVENT_ADD (peer, BGP_Stop); */
622 bgp_stop (peer);
623 peer->status = Idle;
624 bgp_timer_set (peer);
625 return 0;
626 break;
627 case BGP_MSG_KEEPALIVE:
628 peer->keepalive_out++;
629 break;
630 case BGP_MSG_ROUTE_REFRESH_NEW:
631 case BGP_MSG_ROUTE_REFRESH_OLD:
632 peer->refresh_out++;
633 break;
634 case BGP_MSG_CAPABILITY:
635 peer->dynamic_cap_out++;
636 break;
637 }
638
639 /* OK we send packet so delete it. */
640 bgp_packet_delete (peer);
641
642 if (++count >= BGP_WRITE_PACKET_MAX)
643 break;
644 }
645
646 if (bgp_write_proceed (peer))
paul6ad23f02004-02-17 19:45:10 +0000647 BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
paul718e3742002-12-13 20:15:29 +0000648
649 return 0;
650}
651
652/* This is only for sending NOTIFICATION message to neighbor. */
653int
654bgp_write_notify (struct peer *peer)
655{
656 int ret;
657 u_char type;
658 struct stream *s;
659
660 /* There should be at least one packet. */
661 s = stream_fifo_head (peer->obuf);
662 if (!s)
663 return 0;
664 assert (stream_get_endp (s) >= BGP_HEADER_SIZE);
665
666 /* I'm not sure fd is writable. */
paul6ad23f02004-02-17 19:45:10 +0000667 ret = writen (*peer->fd, STREAM_DATA (s), stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000668 if (ret <= 0)
669 {
670 bgp_stop (peer);
671 peer->status = Idle;
672 bgp_timer_set (peer);
673 return 0;
674 }
675
676 /* Retrieve BGP packet type. */
677 stream_set_getp (s, BGP_MARKER_SIZE + 2);
678 type = stream_getc (s);
679
680 assert (type == BGP_MSG_NOTIFY);
681
682 /* Type should be notify. */
683 peer->notify_out++;
684
685 /* Double start timer. */
686 peer->v_start *= 2;
687
688 /* Overflow check. */
689 if (peer->v_start >= (60 * 2))
690 peer->v_start = (60 * 2);
691
692 /* We don't call event manager at here for avoiding other events. */
693 bgp_stop (peer);
694 peer->status = Idle;
695 bgp_timer_set (peer);
696
697 return 0;
698}
699
700/* Make keepalive packet and send it to the peer. */
701void
702bgp_keepalive_send (struct peer *peer)
703{
704 struct stream *s;
705 int length;
706
707 s = stream_new (BGP_MAX_PACKET_SIZE);
708
709 /* Make keepalive packet. */
710 bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE);
711
712 /* Set packet size. */
713 length = bgp_packet_set_size (s);
714
715 /* Dump packet if debug option is set. */
716 /* bgp_packet_dump (s); */
717
718 if (BGP_DEBUG (keepalive, KEEPALIVE))
719 zlog_info ("%s sending KEEPALIVE", peer->host);
720 if (BGP_DEBUG (normal, NORMAL))
721 zlog_info ("%s send message type %d, length (incl. header) %d",
722 peer->host, BGP_MSG_KEEPALIVE, length);
723
724 /* Add packet to the peer. */
725 bgp_packet_add (peer, s);
726
paul6ad23f02004-02-17 19:45:10 +0000727 BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
paul718e3742002-12-13 20:15:29 +0000728}
729
730/* Make open packet and send it to the peer. */
731void
732bgp_open_send (struct peer *peer)
733{
734 struct stream *s;
735 int length;
736 u_int16_t send_holdtime;
737 as_t local_as;
738
739 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
740 send_holdtime = peer->holdtime;
741 else
742 send_holdtime = peer->bgp->default_holdtime;
743
744 /* local-as Change */
745 if (peer->change_local_as)
746 local_as = peer->change_local_as;
747 else
748 local_as = peer->local_as;
749
750 s = stream_new (BGP_MAX_PACKET_SIZE);
751
752 /* Make open packet. */
753 bgp_packet_set_marker (s, BGP_MSG_OPEN);
754
755 /* Set open packet values. */
756 stream_putc (s, BGP_VERSION_4); /* BGP version */
757 stream_putw (s, local_as); /* My Autonomous System*/
758 stream_putw (s, send_holdtime); /* Hold Time */
759 stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */
760
761 /* Set capability code. */
762 bgp_open_capability (s, peer);
763
764 /* Set BGP packet length. */
765 length = bgp_packet_set_size (s);
766
767 if (BGP_DEBUG (normal, NORMAL))
768 zlog_info ("%s sending OPEN, version %d, my as %d, holdtime %d, id %s",
769 peer->host, BGP_VERSION_4, local_as,
770 send_holdtime, inet_ntoa (peer->local_id));
771
772 if (BGP_DEBUG (normal, NORMAL))
773 zlog_info ("%s send message type %d, length (incl. header) %d",
774 peer->host, BGP_MSG_OPEN, length);
775
776 /* Dump packet if debug option is set. */
777 /* bgp_packet_dump (s); */
778
779 /* Add packet to the peer. */
780 bgp_packet_add (peer, s);
781
paul6ad23f02004-02-17 19:45:10 +0000782 BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
paul718e3742002-12-13 20:15:29 +0000783}
784
785/* Send BGP notify packet with data potion. */
786void
787bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code,
788 u_char *data, size_t datalen)
789{
790 struct stream *s;
791 int length;
792
793 /* Allocate new stream. */
794 s = stream_new (BGP_MAX_PACKET_SIZE);
795
796 /* Make nitify packet. */
797 bgp_packet_set_marker (s, BGP_MSG_NOTIFY);
798
799 /* Set notify packet values. */
800 stream_putc (s, code); /* BGP notify code */
801 stream_putc (s, sub_code); /* BGP notify sub_code */
802
803 /* If notify data is present. */
804 if (data)
805 stream_write (s, data, datalen);
806
807 /* Set BGP packet length. */
808 length = bgp_packet_set_size (s);
809
810 /* Add packet to the peer. */
811 stream_fifo_clean (peer->obuf);
812 bgp_packet_add (peer, s);
813
814 /* For debug */
815 {
816 struct bgp_notify bgp_notify;
817 int first = 0;
818 int i;
819 char c[4];
820
821 bgp_notify.code = code;
822 bgp_notify.subcode = sub_code;
823 bgp_notify.data = NULL;
824 bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE;
825
826 if (bgp_notify.length)
827 {
828 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
829 for (i = 0; i < bgp_notify.length; i++)
830 if (first)
831 {
832 sprintf (c, " %02x", data[i]);
833 strcat (bgp_notify.data, c);
834 }
835 else
836 {
837 first = 1;
838 sprintf (c, "%02x", data[i]);
839 strcpy (bgp_notify.data, c);
840 }
841 }
842 bgp_notify_print (peer, &bgp_notify, "sending");
843 if (bgp_notify.data)
844 XFREE (MTYPE_TMP, bgp_notify.data);
845 }
846
847 if (BGP_DEBUG (normal, NORMAL))
848 zlog_info ("%s send message type %d, length (incl. header) %d",
849 peer->host, BGP_MSG_NOTIFY, length);
850
851 /* Call imidiately. */
852 BGP_WRITE_OFF (peer->t_write);
853
854 bgp_write_notify (peer);
855}
856
857/* Send BGP notify packet. */
858void
859bgp_notify_send (struct peer *peer, u_char code, u_char sub_code)
860{
861 bgp_notify_send_with_data (peer, code, sub_code, NULL, 0);
862}
863
864char *
865afi2str (afi_t afi)
866{
867 if (afi == AFI_IP)
868 return "AFI_IP";
869 else if (afi == AFI_IP6)
870 return "AFI_IP6";
871 else
872 return "Unknown AFI";
873}
874
875char *
876safi2str (safi_t safi)
877{
878 if (safi == SAFI_UNICAST)
879 return "SAFI_UNICAST";
880 else if (safi == SAFI_MULTICAST)
881 return "SAFI_MULTICAST";
882 else if (safi == SAFI_MPLS_VPN || safi == BGP_SAFI_VPNV4)
883 return "SAFI_MPLS_VPN";
884 else
885 return "Unknown SAFI";
886}
887
888/* Send route refresh message to the peer. */
889void
890bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi,
891 u_char orf_type, u_char when_to_refresh, int remove)
892{
893 struct stream *s;
894 struct stream *packet;
895 int length;
896 struct bgp_filter *filter;
897 int orf_refresh = 0;
898
899#ifdef DISABLE_BGP_ANNOUNCE
900 return;
901#endif /* DISABLE_BGP_ANNOUNCE */
902
903 filter = &peer->filter[afi][safi];
904
905 /* Adjust safi code. */
906 if (safi == SAFI_MPLS_VPN)
907 safi = BGP_SAFI_VPNV4;
908
909 s = stream_new (BGP_MAX_PACKET_SIZE);
910
911 /* Make BGP update packet. */
912 if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
913 bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW);
914 else
915 bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD);
916
917 /* Encode Route Refresh message. */
918 stream_putw (s, afi);
919 stream_putc (s, 0);
920 stream_putc (s, safi);
921
922 if (orf_type == ORF_TYPE_PREFIX
923 || orf_type == ORF_TYPE_PREFIX_OLD)
924 if (remove || filter->plist[FILTER_IN].plist)
925 {
926 u_int16_t orf_len;
927 unsigned long orfp;
928
929 orf_refresh = 1;
930 stream_putc (s, when_to_refresh);
931 stream_putc (s, orf_type);
932 orfp = stream_get_putp (s);
933 stream_putw (s, 0);
934
935 if (remove)
936 {
937 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
938 stream_putc (s, ORF_COMMON_PART_REMOVE_ALL);
939 if (BGP_DEBUG (normal, NORMAL))
940 zlog_info ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
941 peer->host, orf_type,
942 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
943 afi, safi);
944 }
945 else
946 {
947 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
948 prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist,
949 ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT,
950 ORF_COMMON_PART_DENY);
951 if (BGP_DEBUG (normal, NORMAL))
952 zlog_info ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
953 peer->host, orf_type,
954 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
955 afi, safi);
956 }
957
958 /* Total ORF Entry Len. */
959 orf_len = stream_get_putp (s) - orfp - 2;
960 stream_putw_at (s, orfp, orf_len);
961 }
962
963 /* Set packet size. */
964 length = bgp_packet_set_size (s);
965
966 if (BGP_DEBUG (normal, NORMAL))
967 {
968 if (! orf_refresh)
969 zlog_info ("%s sending REFRESH_REQ for afi/safi: %d/%d",
970 peer->host, afi, safi);
971 zlog_info ("%s send message type %d, length (incl. header) %d",
972 peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ?
973 BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length);
974 }
975
976 /* Make real packet. */
977 packet = bgp_packet_dup (s);
978 stream_free (s);
979
980 /* Add packet to the peer. */
981 bgp_packet_add (peer, packet);
982
paul6ad23f02004-02-17 19:45:10 +0000983 BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
paul718e3742002-12-13 20:15:29 +0000984}
985
986/* Send capability message to the peer. */
987void
988bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi,
989 int capability_code, int action)
990{
991 struct stream *s;
992 struct stream *packet;
993 int length;
994
995 /* Adjust safi code. */
996 if (safi == SAFI_MPLS_VPN)
997 safi = BGP_SAFI_VPNV4;
998
999 s = stream_new (BGP_MAX_PACKET_SIZE);
1000
1001 /* Make BGP update packet. */
1002 bgp_packet_set_marker (s, BGP_MSG_CAPABILITY);
1003
1004 /* Encode MP_EXT capability. */
1005 if (capability_code == CAPABILITY_CODE_MP)
1006 {
1007 stream_putc (s, action);
1008 stream_putc (s, CAPABILITY_CODE_MP);
1009 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1010 stream_putw (s, afi);
1011 stream_putc (s, 0);
1012 stream_putc (s, safi);
1013
1014 if (BGP_DEBUG (normal, NORMAL))
1015 zlog_info ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
1016 peer->host, action == CAPABILITY_ACTION_SET ?
1017 "Advertising" : "Removing", afi, safi);
1018 }
1019
1020 /* Encode Route Refresh capability. */
1021 if (capability_code == CAPABILITY_CODE_REFRESH)
1022 {
1023 stream_putc (s, action);
1024 stream_putc (s, CAPABILITY_CODE_REFRESH);
1025 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
1026 stream_putc (s, action);
1027 stream_putc (s, CAPABILITY_CODE_REFRESH_OLD);
1028 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
1029
1030 if (BGP_DEBUG (normal, NORMAL))
1031 zlog_info ("%s sending CAPABILITY has %s ROUTE-REFRESH capability",
1032 peer->host, action == CAPABILITY_ACTION_SET ?
1033 "Advertising" : "Removing");
1034 }
1035
1036 /* Set packet size. */
1037 length = bgp_packet_set_size (s);
1038
1039 /* Make real packet. */
1040 packet = bgp_packet_dup (s);
1041 stream_free (s);
1042
1043 /* Add packet to the peer. */
1044 bgp_packet_add (peer, packet);
1045
1046 if (BGP_DEBUG (normal, NORMAL))
1047 zlog_info ("%s send message type %d, length (incl. header) %d",
1048 peer->host, BGP_MSG_CAPABILITY, length);
1049
paul6ad23f02004-02-17 19:45:10 +00001050 BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
paul718e3742002-12-13 20:15:29 +00001051}
1052
1053/* RFC1771 6.8 Connection collision detection. */
1054int
paul6ad23f02004-02-17 19:45:10 +00001055bgp_collision_detect (struct peer *peer, struct in_addr remote_id)
paul718e3742002-12-13 20:15:29 +00001056{
paul718e3742002-12-13 20:15:29 +00001057 struct listnode *nn;
1058 struct bgp *bgp;
1059
1060 bgp = bgp_get_default ();
1061 if (! bgp)
1062 return 0;
1063
1064 /* Upon receipt of an OPEN message, the local system must examine
1065 all of its connections that are in the OpenConfirm state. A BGP
1066 speaker may also examine connections in an OpenSent state if it
1067 knows the BGP Identifier of the peer by means outside of the
1068 protocol. If among these connections there is a connection to a
1069 remote BGP speaker whose BGP Identifier equals the one in the
1070 OPEN message, then the local system performs the following
1071 collision resolution procedure: */
1072
1073 LIST_LOOP (bgp->peer, peer, nn)
1074 {
1075 /* Under OpenConfirm status, local peer structure already hold
1076 remote router ID. */
paul6ad23f02004-02-17 19:45:10 +00001077 if ((peer->status == OpenConfirm)
1078 && ( ntohl(peer->remote_id.s_addr) == ntohl(remote_id.s_addr) )
1079 )
1080 {
paul718e3742002-12-13 20:15:29 +00001081 /* 1. The BGP Identifier of the local system is compared to
1082 the BGP Identifier of the remote system (as specified in
1083 the OPEN message). */
1084
1085 if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr))
1086 {
1087 /* 2. If the value of the local BGP Identifier is less
1088 than the remote one, the local system closes BGP
1089 connection that already exists (the one that is
1090 already in the OpenConfirm state), and accepts BGP
1091 connection initiated by the remote system. */
1092
paul6ad23f02004-02-17 19:45:10 +00001093 if (peer->fd_local >= 0)
1094 {
1095 BGP_WRITE_OFF (peer->t_write);
1096 BGP_READ_OFF (peer->t_read);
1097 close (peer->fd_local);
1098 }
1099 peer->fd_local = -1;
1100 peer->fd = &peer->fd_accept;
1101
1102 BGP_READ_ON (peer->t_read, bgp_read, *peer->fd);
paul718e3742002-12-13 20:15:29 +00001103 return 1;
1104 }
1105 else
1106 {
1107 /* 3. Otherwise, the local system closes newly created
1108 BGP connection (the one associated with the newly
1109 received OPEN message), and continues to use the
1110 existing one (the one that is already in the
1111 OpenConfirm state). */
1112
paul6ad23f02004-02-17 19:45:10 +00001113 if (peer->fd_accept >= 0)
1114 {
1115 close (peer->fd_accept);
1116 peer->fd_accept = -1;
1117 }
paul718e3742002-12-13 20:15:29 +00001118 return -1;
1119 }
paul6ad23f02004-02-17 19:45:10 +00001120 }
1121 }
paul718e3742002-12-13 20:15:29 +00001122 return 0;
1123}
1124
1125int
1126bgp_open_receive (struct peer *peer, bgp_size_t size)
1127{
1128 int ret;
1129 u_char version;
1130 u_char optlen;
1131 u_int16_t holdtime;
1132 u_int16_t send_holdtime;
1133 as_t remote_as;
1134 struct peer *realpeer;
1135 struct in_addr remote_id;
1136 int capability;
1137 char notify_data_remote_as[2];
1138 char notify_data_remote_id[4];
1139
1140 realpeer = NULL;
1141
1142 /* Parse open packet. */
1143 version = stream_getc (peer->ibuf);
1144 memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2);
1145 remote_as = stream_getw (peer->ibuf);
1146 holdtime = stream_getw (peer->ibuf);
1147 memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
1148 remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
1149
1150 /* Receive OPEN message log */
1151 if (BGP_DEBUG (normal, NORMAL))
1152 zlog_info ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s",
1153 peer->host, version, remote_as, holdtime,
1154 inet_ntoa (remote_id));
1155
1156 /* Lookup peer from Open packet. */
1157 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1158 {
1159 int as = 0;
1160
1161 realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as);
1162
1163 if (! realpeer)
1164 {
1165 /* Peer's source IP address is check in bgp_accept(), so this
1166 must be AS number mismatch or remote-id configuration
1167 mismatch. */
1168 if (as)
1169 {
1170 if (BGP_DEBUG (normal, NORMAL))
1171 zlog_info ("%s bad OPEN, wrong router identifier %s",
1172 peer->host, inet_ntoa (remote_id));
1173 bgp_notify_send_with_data (peer,
1174 BGP_NOTIFY_OPEN_ERR,
1175 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1176 notify_data_remote_id, 4);
1177 }
1178 else
1179 {
1180 if (BGP_DEBUG (normal, NORMAL))
1181 zlog_info ("%s bad OPEN, remote AS is %d, expected %d",
1182 peer->host, remote_as, peer->as);
1183 bgp_notify_send_with_data (peer,
1184 BGP_NOTIFY_OPEN_ERR,
1185 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1186 notify_data_remote_as, 2);
1187 }
1188 return -1;
1189 }
1190 }
1191
1192 /* When collision is detected and this peer is closed. Retrun
1193 immidiately. */
1194 ret = bgp_collision_detect (peer, remote_id);
1195 if (ret < 0)
1196 return ret;
1197
paul718e3742002-12-13 20:15:29 +00001198 /* remote router-id check. */
1199 if (remote_id.s_addr == 0
1200 || ntohl (remote_id.s_addr) >= 0xe0000000
1201 || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
1202 {
1203 if (BGP_DEBUG (normal, NORMAL))
1204 zlog_info ("%s bad OPEN, wrong router identifier %s",
1205 peer->host, inet_ntoa (remote_id));
1206 bgp_notify_send_with_data (peer,
1207 BGP_NOTIFY_OPEN_ERR,
1208 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1209 notify_data_remote_id, 4);
1210 return -1;
1211 }
1212
1213 /* Set remote router-id */
1214 peer->remote_id = remote_id;
1215
1216 /* Peer BGP version check. */
1217 if (version != BGP_VERSION_4)
1218 {
1219 if (BGP_DEBUG (normal, NORMAL))
1220 zlog_info ("%s bad protocol version, remote requested %d, local request %d",
1221 peer->host, version, BGP_VERSION_4);
1222 bgp_notify_send_with_data (peer,
1223 BGP_NOTIFY_OPEN_ERR,
1224 BGP_NOTIFY_OPEN_UNSUP_VERSION,
1225 "\x04", 1);
1226 return -1;
1227 }
1228
1229 /* Check neighbor as number. */
1230 if (remote_as != peer->as)
1231 {
1232 if (BGP_DEBUG (normal, NORMAL))
1233 zlog_info ("%s bad OPEN, remote AS is %d, expected %d",
1234 peer->host, remote_as, peer->as);
1235 bgp_notify_send_with_data (peer,
1236 BGP_NOTIFY_OPEN_ERR,
1237 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1238 notify_data_remote_as, 2);
1239 return -1;
1240 }
1241
1242 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1243 calculate the value of the Hold Timer by using the smaller of its
1244 configured Hold Time and the Hold Time received in the OPEN message.
1245 The Hold Time MUST be either zero or at least three seconds. An
1246 implementation may reject connections on the basis of the Hold Time. */
1247
1248 if (holdtime < 3 && holdtime != 0)
1249 {
1250 bgp_notify_send (peer,
1251 BGP_NOTIFY_OPEN_ERR,
1252 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
1253 return -1;
1254 }
1255
1256 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1257 would be one third of the Hold Time interval. KEEPALIVE messages
1258 MUST NOT be sent more frequently than one per second. An
1259 implementation MAY adjust the rate at which it sends KEEPALIVE
1260 messages as a function of the Hold Time interval. */
1261
1262 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
1263 send_holdtime = peer->holdtime;
1264 else
1265 send_holdtime = peer->bgp->default_holdtime;
1266
1267 if (holdtime < send_holdtime)
1268 peer->v_holdtime = holdtime;
1269 else
1270 peer->v_holdtime = send_holdtime;
1271
1272 peer->v_keepalive = peer->v_holdtime / 3;
1273
1274 /* Open option part parse. */
1275 capability = 0;
1276 optlen = stream_getc (peer->ibuf);
1277 if (optlen != 0)
1278 {
1279 ret = bgp_open_option_parse (peer, optlen, &capability);
1280 if (ret < 0)
1281 return ret;
1282
1283 stream_forward (peer->ibuf, optlen);
1284 }
1285 else
1286 {
1287 if (BGP_DEBUG (normal, NORMAL))
1288 zlog_info ("%s rcvd OPEN w/ OPTION parameter len: 0",
1289 peer->host);
1290 }
1291
1292 /* Override capability. */
1293 if (! capability || CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1294 {
1295 peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
1296 peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
1297 peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
1298 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
1299 }
1300
1301 /* Get sockname. */
1302 bgp_getsockname (peer);
1303
1304 BGP_EVENT_ADD (peer, Receive_OPEN_message);
1305
1306 peer->packet_size = 0;
1307 if (peer->ibuf)
1308 stream_reset (peer->ibuf);
1309
1310 return 0;
1311}
1312
1313/* Parse BGP Update packet and make attribute object. */
1314int
1315bgp_update_receive (struct peer *peer, bgp_size_t size)
1316{
1317 int ret;
1318 u_char *end;
1319 struct stream *s;
1320 struct attr attr;
1321 bgp_size_t attribute_len;
1322 bgp_size_t update_len;
1323 bgp_size_t withdraw_len;
1324 struct bgp_nlri update;
1325 struct bgp_nlri withdraw;
1326 struct bgp_nlri mp_update;
1327 struct bgp_nlri mp_withdraw;
1328 char attrstr[BUFSIZ];
1329
1330 /* Status must be Established. */
1331 if (peer->status != Established)
1332 {
1333 zlog_err ("%s [FSM] Update packet received under status %s",
1334 peer->host, LOOKUP (bgp_status_msg, peer->status));
1335 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1336 return -1;
1337 }
1338
1339 /* Set initial values. */
1340 memset (&attr, 0, sizeof (struct attr));
1341 memset (&update, 0, sizeof (struct bgp_nlri));
1342 memset (&withdraw, 0, sizeof (struct bgp_nlri));
1343 memset (&mp_update, 0, sizeof (struct bgp_nlri));
1344 memset (&mp_withdraw, 0, sizeof (struct bgp_nlri));
1345
1346 s = peer->ibuf;
1347 end = stream_pnt (s) + size;
1348
1349 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1350 Length is too large (i.e., if Unfeasible Routes Length + Total
1351 Attribute Length + 23 exceeds the message Length), then the Error
1352 Subcode is set to Malformed Attribute List. */
1353 if (stream_pnt (s) + 2 > end)
1354 {
1355 zlog_err ("%s [Error] Update packet error"
1356 " (packet length is short for unfeasible length)",
1357 peer->host);
1358 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1359 BGP_NOTIFY_UPDATE_MAL_ATTR);
1360 return -1;
1361 }
1362
1363 /* Unfeasible Route Length. */
1364 withdraw_len = stream_getw (s);
1365
1366 /* Unfeasible Route Length check. */
1367 if (stream_pnt (s) + withdraw_len > end)
1368 {
1369 zlog_err ("%s [Error] Update packet error"
1370 " (packet unfeasible length overflow %d)",
1371 peer->host, withdraw_len);
1372 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1373 BGP_NOTIFY_UPDATE_MAL_ATTR);
1374 return -1;
1375 }
1376
1377 /* Unfeasible Route packet format check. */
1378 if (withdraw_len > 0)
1379 {
1380 ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len);
1381 if (ret < 0)
1382 return -1;
1383
1384 if (BGP_DEBUG (packet, PACKET_RECV))
1385 zlog_info ("%s [Update:RECV] Unfeasible NLRI received", peer->host);
1386
1387 withdraw.afi = AFI_IP;
1388 withdraw.safi = SAFI_UNICAST;
1389 withdraw.nlri = stream_pnt (s);
1390 withdraw.length = withdraw_len;
1391 stream_forward (s, withdraw_len);
1392 }
1393
1394 /* Attribute total length check. */
1395 if (stream_pnt (s) + 2 > end)
1396 {
1397 zlog_warn ("%s [Error] Packet Error"
1398 " (update packet is short for attribute length)",
1399 peer->host);
1400 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1401 BGP_NOTIFY_UPDATE_MAL_ATTR);
1402 return -1;
1403 }
1404
1405 /* Fetch attribute total length. */
1406 attribute_len = stream_getw (s);
1407
1408 /* Attribute length check. */
1409 if (stream_pnt (s) + attribute_len > end)
1410 {
1411 zlog_warn ("%s [Error] Packet Error"
1412 " (update packet attribute length overflow %d)",
1413 peer->host, attribute_len);
1414 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1415 BGP_NOTIFY_UPDATE_MAL_ATTR);
1416 return -1;
1417 }
1418
1419 /* Parse attribute when it exists. */
1420 if (attribute_len)
1421 {
1422 ret = bgp_attr_parse (peer, &attr, attribute_len,
1423 &mp_update, &mp_withdraw);
1424 if (ret < 0)
1425 return -1;
1426 }
1427
1428 /* Logging the attribute. */
1429 if (BGP_DEBUG (update, UPDATE_IN))
1430 {
1431 bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
1432 zlog (peer->log, LOG_INFO, "%s rcvd UPDATE w/ attr: %s",
1433 peer->host, attrstr);
1434 }
1435
1436 /* Network Layer Reachability Information. */
1437 update_len = end - stream_pnt (s);
1438
1439 if (update_len)
1440 {
1441 /* Check NLRI packet format and prefix length. */
1442 ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len);
1443 if (ret < 0)
1444 return -1;
1445
1446 /* Set NLRI portion to structure. */
1447 update.afi = AFI_IP;
1448 update.safi = SAFI_UNICAST;
1449 update.nlri = stream_pnt (s);
1450 update.length = update_len;
1451 stream_forward (s, update_len);
1452 }
1453
1454 /* NLRI is processed only when the peer is configured specific
1455 Address Family and Subsequent Address Family. */
1456 if (peer->afc[AFI_IP][SAFI_UNICAST])
1457 {
1458 if (withdraw.length)
1459 bgp_nlri_parse (peer, NULL, &withdraw);
1460
1461 if (update.length)
1462 {
1463 /* We check well-known attribute only for IPv4 unicast
1464 update. */
1465 ret = bgp_attr_check (peer, &attr);
1466 if (ret < 0)
1467 return -1;
1468
1469 bgp_nlri_parse (peer, &attr, &update);
1470 }
1471 }
1472 if (peer->afc[AFI_IP][SAFI_MULTICAST])
1473 {
1474 if (mp_update.length
1475 && mp_update.afi == AFI_IP
1476 && mp_update.safi == SAFI_MULTICAST)
1477 bgp_nlri_parse (peer, &attr, &mp_update);
1478
1479 if (mp_withdraw.length
1480 && mp_withdraw.afi == AFI_IP
1481 && mp_withdraw.safi == SAFI_MULTICAST)
1482 bgp_nlri_parse (peer, NULL, &mp_withdraw);
1483 }
1484 if (peer->afc[AFI_IP6][SAFI_UNICAST])
1485 {
1486 if (mp_update.length
1487 && mp_update.afi == AFI_IP6
1488 && mp_update.safi == SAFI_UNICAST)
1489 bgp_nlri_parse (peer, &attr, &mp_update);
1490
1491 if (mp_withdraw.length
1492 && mp_withdraw.afi == AFI_IP6
1493 && mp_withdraw.safi == SAFI_UNICAST)
1494 bgp_nlri_parse (peer, NULL, &mp_withdraw);
1495 }
1496 if (peer->afc[AFI_IP6][SAFI_MULTICAST])
1497 {
1498 if (mp_update.length
1499 && mp_update.afi == AFI_IP6
1500 && mp_update.safi == SAFI_MULTICAST)
1501 bgp_nlri_parse (peer, &attr, &mp_update);
1502
1503 if (mp_withdraw.length
1504 && mp_withdraw.afi == AFI_IP6
1505 && mp_withdraw.safi == SAFI_MULTICAST)
1506 bgp_nlri_parse (peer, NULL, &mp_withdraw);
1507 }
1508 if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
1509 {
1510 if (mp_update.length
1511 && mp_update.afi == AFI_IP
1512 && mp_update.safi == BGP_SAFI_VPNV4)
1513 bgp_nlri_parse_vpnv4 (peer, &attr, &mp_update);
1514
1515 if (mp_withdraw.length
1516 && mp_withdraw.afi == AFI_IP
1517 && mp_withdraw.safi == BGP_SAFI_VPNV4)
1518 bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw);
1519 }
1520
1521 /* Everything is done. We unintern temporary structures which
1522 interned in bgp_attr_parse(). */
1523 if (attr.aspath)
1524 aspath_unintern (attr.aspath);
1525 if (attr.community)
1526 community_unintern (attr.community);
1527 if (attr.ecommunity)
1528 ecommunity_unintern (attr.ecommunity);
1529 if (attr.cluster)
1530 cluster_unintern (attr.cluster);
1531 if (attr.transit)
1532 transit_unintern (attr.transit);
1533
1534 /* If peering is stopped due to some reason, do not generate BGP
1535 event. */
1536 if (peer->status != Established)
1537 return 0;
1538
1539 /* Increment packet counter. */
1540 peer->update_in++;
1541 peer->update_time = time (NULL);
1542
1543 /* Generate BGP event. */
1544 BGP_EVENT_ADD (peer, Receive_UPDATE_message);
1545
1546 return 0;
1547}
1548
1549/* Notify message treatment function. */
1550void
1551bgp_notify_receive (struct peer *peer, bgp_size_t size)
1552{
1553 struct bgp_notify bgp_notify;
1554
1555 if (peer->notify.data)
1556 {
1557 XFREE (MTYPE_TMP, peer->notify.data);
1558 peer->notify.data = NULL;
1559 peer->notify.length = 0;
1560 }
1561
1562 bgp_notify.code = stream_getc (peer->ibuf);
1563 bgp_notify.subcode = stream_getc (peer->ibuf);
1564 bgp_notify.length = size - 2;
1565 bgp_notify.data = NULL;
1566
1567 /* Preserv notify code and sub code. */
1568 peer->notify.code = bgp_notify.code;
1569 peer->notify.subcode = bgp_notify.subcode;
1570 /* For further diagnostic record returned Data. */
1571 if (bgp_notify.length)
1572 {
1573 peer->notify.length = size - 2;
1574 peer->notify.data = XMALLOC (MTYPE_TMP, size - 2);
1575 memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2);
1576 }
1577
1578 /* For debug */
1579 {
1580 int i;
1581 int first = 0;
1582 char c[4];
1583
1584 if (bgp_notify.length)
1585 {
1586 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
1587 for (i = 0; i < bgp_notify.length; i++)
1588 if (first)
1589 {
1590 sprintf (c, " %02x", stream_getc (peer->ibuf));
1591 strcat (bgp_notify.data, c);
1592 }
1593 else
1594 {
1595 first = 1;
1596 sprintf (c, "%02x", stream_getc (peer->ibuf));
1597 strcpy (bgp_notify.data, c);
1598 }
1599 }
1600
1601 bgp_notify_print(peer, &bgp_notify, "received");
1602 if (bgp_notify.data)
1603 XFREE (MTYPE_TMP, bgp_notify.data);
1604 }
1605
1606 /* peer count update */
1607 peer->notify_in++;
1608
1609 /* We have to check for Notify with Unsupported Optional Parameter.
1610 in that case we fallback to open without the capability option.
1611 But this done in bgp_stop. We just mark it here to avoid changing
1612 the fsm tables. */
1613 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
1614 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM )
1615 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1616
1617 /* Also apply to Unsupported Capability until remote router support
1618 capability. */
1619 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
1620 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_CAPBL)
1621 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1622
1623 BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message);
1624}
1625
1626/* Keepalive treatment function -- get keepalive send keepalive */
1627void
1628bgp_keepalive_receive (struct peer *peer, bgp_size_t size)
1629{
1630 if (BGP_DEBUG (keepalive, KEEPALIVE))
1631 zlog_info ("%s KEEPALIVE rcvd", peer->host);
1632
1633 BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message);
1634}
1635
1636/* Route refresh message is received. */
1637void
1638bgp_route_refresh_receive (struct peer *peer, bgp_size_t size)
1639{
1640 afi_t afi;
1641 safi_t safi;
1642 u_char reserved;
1643 struct stream *s;
1644
1645 /* If peer does not have the capability, send notification. */
1646 if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV))
1647 {
1648 plog_err (peer->log, "%s [Error] BGP route refresh is not enabled",
1649 peer->host);
1650 bgp_notify_send (peer,
1651 BGP_NOTIFY_HEADER_ERR,
1652 BGP_NOTIFY_HEADER_BAD_MESTYPE);
1653 return;
1654 }
1655
1656 /* Status must be Established. */
1657 if (peer->status != Established)
1658 {
1659 plog_err (peer->log,
1660 "%s [Error] Route refresh packet received under status %s",
1661 peer->host, LOOKUP (bgp_status_msg, peer->status));
1662 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1663 return;
1664 }
1665
1666 s = peer->ibuf;
1667
1668 /* Parse packet. */
1669 afi = stream_getw (s);
1670 reserved = stream_getc (s);
1671 safi = stream_getc (s);
1672
1673 if (BGP_DEBUG (normal, NORMAL))
1674 zlog_info ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1675 peer->host, afi, safi);
1676
1677 /* Check AFI and SAFI. */
1678 if ((afi != AFI_IP && afi != AFI_IP6)
1679 || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
1680 && safi != BGP_SAFI_VPNV4))
1681 {
1682 if (BGP_DEBUG (normal, NORMAL))
1683 {
1684 zlog_info ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1685 peer->host, afi, safi);
1686 }
1687 return;
1688 }
1689
1690 /* Adjust safi code. */
1691 if (safi == BGP_SAFI_VPNV4)
1692 safi = SAFI_MPLS_VPN;
1693
1694 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
1695 {
1696 u_char *end;
1697 u_char when_to_refresh;
1698 u_char orf_type;
1699 u_int16_t orf_len;
1700
1701 if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5)
1702 {
1703 zlog_info ("%s ORF route refresh length error", peer->host);
1704 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1705 return;
1706 }
1707
1708 when_to_refresh = stream_getc (s);
1709 end = stream_pnt (s) + (size - 5);
1710
1711 while (stream_pnt (s) < end)
1712 {
1713 orf_type = stream_getc (s);
1714 orf_len = stream_getw (s);
1715
1716 if (orf_type == ORF_TYPE_PREFIX
1717 || orf_type == ORF_TYPE_PREFIX_OLD)
1718 {
1719 u_char *p_pnt = stream_pnt (s);
1720 u_char *p_end = stream_pnt (s) + orf_len;
1721 struct orf_prefix orfp;
1722 u_char common = 0;
1723 u_int32_t seq;
1724 int psize;
1725 char name[BUFSIZ];
1726 char buf[BUFSIZ];
1727 int ret;
1728
1729 if (BGP_DEBUG (normal, NORMAL))
1730 {
1731 zlog_info ("%s rcvd Prefixlist ORF(%d) length %d",
1732 peer->host, orf_type, orf_len);
1733 }
1734
1735 /* ORF prefix-list name */
1736 sprintf (name, "%s.%d.%d", peer->host, afi, safi);
1737
1738 while (p_pnt < p_end)
1739 {
1740 memset (&orfp, 0, sizeof (struct orf_prefix));
1741 common = *p_pnt++;
1742 if (common & ORF_COMMON_PART_REMOVE_ALL)
1743 {
1744 if (BGP_DEBUG (normal, NORMAL))
1745 zlog_info ("%s rcvd Remove-All pfxlist ORF request", peer->host);
1746 prefix_bgp_orf_remove_all (name);
1747 break;
1748 }
1749 memcpy (&seq, p_pnt, sizeof (u_int32_t));
1750 p_pnt += sizeof (u_int32_t);
1751 orfp.seq = ntohl (seq);
1752 orfp.ge = *p_pnt++;
1753 orfp.le = *p_pnt++;
1754 orfp.p.prefixlen = *p_pnt++;
1755 orfp.p.family = afi2family (afi);
1756 psize = PSIZE (orfp.p.prefixlen);
1757 memcpy (&orfp.p.u.prefix, p_pnt, psize);
1758 p_pnt += psize;
1759
1760 if (BGP_DEBUG (normal, NORMAL))
1761 zlog_info ("%s rcvd %s %s seq %u %s/%d ge %d le %d",
1762 peer->host,
1763 (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
1764 (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
1765 orfp.seq,
1766 inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, BUFSIZ),
1767 orfp.p.prefixlen, orfp.ge, orfp.le);
1768
1769 ret = prefix_bgp_orf_set (name, afi, &orfp,
1770 (common & ORF_COMMON_PART_DENY ? 0 : 1 ),
1771 (common & ORF_COMMON_PART_REMOVE ? 0 : 1));
1772
1773 if (ret != CMD_SUCCESS)
1774 {
1775 if (BGP_DEBUG (normal, NORMAL))
1776 zlog_info ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer->host);
1777 prefix_bgp_orf_remove_all (name);
1778 break;
1779 }
1780 }
1781 peer->orf_plist[afi][safi] =
1782 prefix_list_lookup (AFI_ORF_PREFIX, name);
1783 }
1784 stream_forward (s, orf_len);
1785 }
1786 if (BGP_DEBUG (normal, NORMAL))
1787 zlog_info ("%s rcvd Refresh %s ORF request", peer->host,
1788 when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate");
1789 if (when_to_refresh == REFRESH_DEFER)
1790 return;
1791 }
1792
1793 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1794 if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
1795 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
1796
1797 /* Perform route refreshment to the peer */
1798 bgp_announce_route (peer, afi, safi);
1799}
1800
1801int
1802bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length)
1803{
1804 u_char *end;
1805 struct capability cap;
1806 u_char action;
1807 struct bgp *bgp;
1808 afi_t afi;
1809 safi_t safi;
1810
1811 bgp = peer->bgp;
1812 end = pnt + length;
1813
1814 while (pnt < end)
1815 {
1816 /* We need at least action, capability code and capability length. */
1817 if (pnt + 3 > end)
1818 {
1819 zlog_info ("%s Capability length error", peer->host);
1820 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1821 return -1;
1822 }
1823
1824 action = *pnt;
1825
1826 /* Fetch structure to the byte stream. */
1827 memcpy (&cap, pnt + 1, sizeof (struct capability));
1828
1829 /* Action value check. */
1830 if (action != CAPABILITY_ACTION_SET
1831 && action != CAPABILITY_ACTION_UNSET)
1832 {
1833 zlog_info ("%s Capability Action Value error %d",
1834 peer->host, action);
1835 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1836 return -1;
1837 }
1838
1839 if (BGP_DEBUG (normal, NORMAL))
1840 zlog_info ("%s CAPABILITY has action: %d, code: %u, length %u",
1841 peer->host, action, cap.code, cap.length);
1842
1843 /* Capability length check. */
1844 if (pnt + (cap.length + 3) > end)
1845 {
1846 zlog_info ("%s Capability length error", peer->host);
1847 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1848 return -1;
1849 }
1850
1851 /* We know MP Capability Code. */
1852 if (cap.code == CAPABILITY_CODE_MP)
1853 {
1854 afi = ntohs (cap.mpc.afi);
1855 safi = cap.mpc.safi;
1856
1857 /* Ignore capability when override-capability is set. */
1858 if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1859 continue;
1860
1861 /* Address family check. */
1862 if ((afi == AFI_IP
1863 || afi == AFI_IP6)
1864 && (safi == SAFI_UNICAST
1865 || safi == SAFI_MULTICAST
1866 || safi == BGP_SAFI_VPNV4))
1867 {
1868 if (BGP_DEBUG (normal, NORMAL))
1869 zlog_info ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
1870 peer->host,
1871 action == CAPABILITY_ACTION_SET
1872 ? "Advertising" : "Removing",
1873 ntohs(cap.mpc.afi) , cap.mpc.safi);
1874
1875 /* Adjust safi code. */
1876 if (safi == BGP_SAFI_VPNV4)
1877 safi = SAFI_MPLS_VPN;
1878
1879 if (action == CAPABILITY_ACTION_SET)
1880 {
1881 peer->afc_recv[afi][safi] = 1;
1882 if (peer->afc[afi][safi])
1883 {
1884 peer->afc_nego[afi][safi] = 1;
1885 bgp_announce_route (peer, afi, safi);
1886 }
1887 }
1888 else
1889 {
1890 peer->afc_recv[afi][safi] = 0;
1891 peer->afc_nego[afi][safi] = 0;
1892
1893 if (peer_active_nego (peer))
1894 bgp_clear_route (peer, afi, safi);
1895 else
1896 BGP_EVENT_ADD (peer, BGP_Stop);
1897 }
1898 }
1899 }
1900 else if (cap.code == CAPABILITY_CODE_REFRESH
1901 || cap.code == CAPABILITY_CODE_REFRESH_OLD)
1902 {
1903 /* Check length. */
1904 if (cap.length != 0)
1905 {
1906 zlog_info ("%s Route Refresh Capability length error %d",
1907 peer->host, cap.length);
1908 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1909 return -1;
1910 }
1911
1912 if (BGP_DEBUG (normal, NORMAL))
1913 zlog_info ("%s CAPABILITY has %s ROUTE-REFRESH capability(%s) for all address-families",
1914 peer->host,
1915 action == CAPABILITY_ACTION_SET
1916 ? "Advertising" : "Removing",
1917 cap.code == CAPABILITY_CODE_REFRESH_OLD
1918 ? "old" : "new");
1919
1920 /* BGP refresh capability */
1921 if (action == CAPABILITY_ACTION_SET)
1922 {
1923 if (cap.code == CAPABILITY_CODE_REFRESH_OLD)
1924 SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
1925 else
1926 SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV);
1927 }
1928 else
1929 {
1930 if (cap.code == CAPABILITY_CODE_REFRESH_OLD)
1931 UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
1932 else
1933 UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV);
1934 }
1935 }
1936 else
1937 {
1938 zlog_warn ("%s unrecognized capability code: %d - ignored",
1939 peer->host, cap.code);
1940 }
1941 pnt += cap.length + 3;
1942 }
1943 return 0;
1944}
1945
1946/* Dynamic Capability is received. */
1947void
1948bgp_capability_receive (struct peer *peer, bgp_size_t size)
1949{
1950 u_char *pnt;
1951 int ret;
1952
1953 /* Fetch pointer. */
1954 pnt = stream_pnt (peer->ibuf);
1955
1956 if (BGP_DEBUG (normal, NORMAL))
1957 zlog_info ("%s rcv CAPABILITY", peer->host);
1958
1959 /* If peer does not have the capability, send notification. */
1960 if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV))
1961 {
1962 plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled",
1963 peer->host);
1964 bgp_notify_send (peer,
1965 BGP_NOTIFY_HEADER_ERR,
1966 BGP_NOTIFY_HEADER_BAD_MESTYPE);
1967 return;
1968 }
1969
1970 /* Status must be Established. */
1971 if (peer->status != Established)
1972 {
1973 plog_err (peer->log,
1974 "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status));
1975 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1976 return;
1977 }
1978
1979 /* Parse packet. */
1980 ret = bgp_capability_msg_parse (peer, pnt, size);
1981}
1982
1983/* BGP read utility function. */
1984int
1985bgp_read_packet (struct peer *peer)
1986{
1987 int nbytes;
1988 int readsize;
1989
1990 readsize = peer->packet_size - peer->ibuf->putp;
1991
1992 /* If size is zero then return. */
1993 if (! readsize)
1994 return 0;
1995
1996 /* Read packet from fd. */
paul6ad23f02004-02-17 19:45:10 +00001997 nbytes = stream_read_unblock (peer->ibuf, *peer->fd, readsize);
paul718e3742002-12-13 20:15:29 +00001998
1999 /* If read byte is smaller than zero then error occured. */
2000 if (nbytes < 0)
2001 {
2002 if (errno == EAGAIN)
2003 return -1;
2004
2005 plog_err (peer->log, "%s [Error] bgp_read_packet error: %s",
2006 peer->host, strerror (errno));
2007 BGP_EVENT_ADD (peer, TCP_fatal_error);
2008 return -1;
2009 }
2010
2011 /* When read byte is zero : clear bgp peer and return */
2012 if (nbytes == 0)
2013 {
2014 if (BGP_DEBUG (events, EVENTS))
2015 plog_info (peer->log, "%s [Event] BGP connection closed fd %d",
paul6ad23f02004-02-17 19:45:10 +00002016 peer->host, *peer->fd);
paul718e3742002-12-13 20:15:29 +00002017 BGP_EVENT_ADD (peer, TCP_connection_closed);
2018 return -1;
2019 }
2020
2021 /* We read partial packet. */
2022 if (peer->ibuf->putp != peer->packet_size)
2023 return -1;
2024
2025 return 0;
2026}
2027
2028/* Marker check. */
2029int
2030bgp_marker_all_one (struct stream *s, int length)
2031{
2032 int i;
2033
2034 for (i = 0; i < length; i++)
2035 if (s->data[i] != 0xff)
2036 return 0;
2037
2038 return 1;
2039}
2040
2041/* Starting point of packet process function. */
2042int
2043bgp_read (struct thread *thread)
2044{
2045 int ret;
2046 u_char type = 0;
2047 struct peer *peer;
2048 bgp_size_t size;
2049 char notify_data_length[2];
2050
2051 /* Yes first of all get peer pointer. */
2052 peer = THREAD_ARG (thread);
2053 peer->t_read = NULL;
2054
2055 /* For non-blocking IO check. */
2056 if (peer->status == Connect)
2057 {
2058 bgp_connect_check (peer);
2059 goto done;
2060 }
2061 else
2062 {
paul6ad23f02004-02-17 19:45:10 +00002063 if (*peer->fd < 0)
paul718e3742002-12-13 20:15:29 +00002064 {
paul6ad23f02004-02-17 19:45:10 +00002065 zlog_err ("bgp_read peer's fd is negative value %d", *peer->fd);
paul718e3742002-12-13 20:15:29 +00002066 return -1;
2067 }
paul6ad23f02004-02-17 19:45:10 +00002068 BGP_READ_ON (peer->t_read, bgp_read, *peer->fd);
paul718e3742002-12-13 20:15:29 +00002069 }
2070
2071 /* Read packet header to determine type of the packet */
2072 if (peer->packet_size == 0)
2073 peer->packet_size = BGP_HEADER_SIZE;
2074
2075 if (peer->ibuf->putp < BGP_HEADER_SIZE)
2076 {
2077 ret = bgp_read_packet (peer);
2078
2079 /* Header read error or partial read packet. */
2080 if (ret < 0)
2081 goto done;
2082
2083 /* Get size and type. */
2084 stream_forward (peer->ibuf, BGP_MARKER_SIZE);
2085 memcpy (notify_data_length, stream_pnt (peer->ibuf), 2);
2086 size = stream_getw (peer->ibuf);
2087 type = stream_getc (peer->ibuf);
2088
2089 if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0)
2090 zlog_info ("%s rcv message type %d, length (excl. header) %d",
2091 peer->host, type, size - BGP_HEADER_SIZE);
2092
2093 /* Marker check */
2094 if (type == BGP_MSG_OPEN
2095 && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE))
2096 {
2097 bgp_notify_send (peer,
2098 BGP_NOTIFY_HEADER_ERR,
2099 BGP_NOTIFY_HEADER_NOT_SYNC);
2100 goto done;
2101 }
2102
2103 /* BGP type check. */
2104 if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
2105 && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
2106 && type != BGP_MSG_ROUTE_REFRESH_NEW
2107 && type != BGP_MSG_ROUTE_REFRESH_OLD
2108 && type != BGP_MSG_CAPABILITY)
2109 {
2110 if (BGP_DEBUG (normal, NORMAL))
2111 plog_err (peer->log,
2112 "%s unknown message type 0x%02x",
2113 peer->host, type);
2114 bgp_notify_send_with_data (peer,
2115 BGP_NOTIFY_HEADER_ERR,
2116 BGP_NOTIFY_HEADER_BAD_MESTYPE,
2117 &type, 1);
2118 goto done;
2119 }
2120 /* Mimimum packet length check. */
2121 if ((size < BGP_HEADER_SIZE)
2122 || (size > BGP_MAX_PACKET_SIZE)
2123 || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
2124 || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE)
2125 || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE)
2126 || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
2127 || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2128 || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2129 || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE))
2130 {
2131 if (BGP_DEBUG (normal, NORMAL))
2132 plog_err (peer->log,
2133 "%s bad message length - %d for %s",
2134 peer->host, size,
2135 type == 128 ? "ROUTE-REFRESH" :
2136 bgp_type_str[(int) type]);
2137 bgp_notify_send_with_data (peer,
2138 BGP_NOTIFY_HEADER_ERR,
2139 BGP_NOTIFY_HEADER_BAD_MESLEN,
2140 notify_data_length, 2);
2141 goto done;
2142 }
2143
2144 /* Adjust size to message length. */
2145 peer->packet_size = size;
2146 }
2147
2148 ret = bgp_read_packet (peer);
2149 if (ret < 0)
2150 goto done;
2151
2152 /* Get size and type again. */
2153 size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE);
2154 type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2);
2155
2156 /* BGP packet dump function. */
2157 bgp_dump_packet (peer, type, peer->ibuf);
2158
2159 size = (peer->packet_size - BGP_HEADER_SIZE);
2160
2161 /* Read rest of the packet and call each sort of packet routine */
2162 switch (type)
2163 {
2164 case BGP_MSG_OPEN:
2165 peer->open_in++;
2166 bgp_open_receive (peer, size);
2167 break;
2168 case BGP_MSG_UPDATE:
2169 peer->readtime = time(NULL); /* Last read timer reset */
2170 bgp_update_receive (peer, size);
2171 break;
2172 case BGP_MSG_NOTIFY:
2173 bgp_notify_receive (peer, size);
2174 break;
2175 case BGP_MSG_KEEPALIVE:
2176 peer->readtime = time(NULL); /* Last read timer reset */
2177 bgp_keepalive_receive (peer, size);
2178 break;
2179 case BGP_MSG_ROUTE_REFRESH_NEW:
2180 case BGP_MSG_ROUTE_REFRESH_OLD:
2181 peer->refresh_in++;
2182 bgp_route_refresh_receive (peer, size);
2183 break;
2184 case BGP_MSG_CAPABILITY:
2185 peer->dynamic_cap_in++;
2186 bgp_capability_receive (peer, size);
2187 break;
2188 }
2189
2190 /* Clear input buffer. */
2191 peer->packet_size = 0;
2192 if (peer->ibuf)
2193 stream_reset (peer->ibuf);
2194
2195 done:
2196 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
2197 {
2198 if (BGP_DEBUG (events, EVENTS))
2199 zlog_info ("%s [Event] Accepting BGP peer delete", peer->host);
2200 peer_delete (peer);
2201 }
2202 return 0;
2203}