blob: 4eb7050c0fb2811ba9eed506bab58e7a78a4f215 [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;
paul5228ad22004-06-04 17:58:18 +0000127 socklen_t slen;
paul718e3742002-12-13 20:15:29 +0000128 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);
pauleb821182004-05-01 08:44:08 +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))
ajs6b514742004-12-08 21:03:23 +0000154 plog_debug (peer->log, "%s [Event] Connect failed (%s)",
ajs6099b3b2004-11-20 02:06:59 +0000155 peer->host, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000156 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);
paul5228ad22004-06-04 17:58:18 +0000206 total_attr_len = bgp_packet_attribute (NULL, peer, s,
207 adv->baa->attr,
208 &rn->p, afi, safi,
209 binfo->peer, prd, tag);
paul718e3742002-12-13 20:15:29 +0000210 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))
ajs6b514742004-12-08 21:03:23 +0000217 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d",
paul718e3742002-12-13 20:15:29 +0000218 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);
pauleb821182004-05-01 08:44:08 +0000241 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000242 stream_reset (s);
243 return packet;
244 }
245 return NULL;
246
247}
248
249/* Make BGP withdraw packet. */
250struct stream *
251bgp_withdraw_packet (struct peer *peer, afi_t afi, safi_t safi)
252{
253 struct stream *s;
254 struct stream *packet;
255 struct bgp_adj_out *adj;
256 struct bgp_advertise *adv;
257 struct bgp_node *rn;
258 unsigned long pos;
259 bgp_size_t unfeasible_len;
260 bgp_size_t total_attr_len;
261 char buf[BUFSIZ];
262 struct prefix_rd *prd = NULL;
263
264 s = peer->work;
265 stream_reset (s);
266
267 while ((adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL)
268 {
269 adj = adv->adj;
270 rn = adv->rn;
271#ifdef MPLS_VPN
272 prd = (struct prefix_rd *) &rn->prn->p;
273#endif /* MPLS_VPN */
274
275 if (STREAM_REMAIN (s)
hasso4372df72004-05-20 10:20:02 +0000276 < (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN + PSIZE (rn->p.prefixlen)))
paul718e3742002-12-13 20:15:29 +0000277 break;
278
279 if (stream_empty (s))
280 {
281 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
282 stream_putw (s, 0);
283 }
284
285 if (afi == AFI_IP && safi == SAFI_UNICAST)
286 stream_put_prefix (s, &rn->p);
287 else
288 {
289 pos = stream_get_putp (s);
290 stream_putw (s, 0);
291 total_attr_len
292 = bgp_packet_withdraw (peer, s, &rn->p, afi, safi, prd, NULL);
293
294 /* Set total path attribute length. */
295 stream_putw_at (s, pos, total_attr_len);
296 }
297
298 if (BGP_DEBUG (update, UPDATE_OUT))
ajs6b514742004-12-08 21:03:23 +0000299 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
paul718e3742002-12-13 20:15:29 +0000300 peer->host,
301 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ),
302 rn->p.prefixlen);
303
304 peer->scount[afi][safi]--;
305
306 bgp_adj_out_remove (rn, adj, peer, afi, safi);
307 bgp_unlock_node (rn);
308
309 if (! (afi == AFI_IP && safi == SAFI_UNICAST))
310 break;
311 }
312
313 if (! stream_empty (s))
314 {
315 if (afi == AFI_IP && safi == SAFI_UNICAST)
316 {
317 unfeasible_len
318 = stream_get_putp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
319 stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
320 stream_putw (s, 0);
321 }
322 bgp_packet_set_size (s);
323 packet = bgp_packet_dup (s);
324 bgp_packet_add (peer, packet);
325 stream_reset (s);
326 return packet;
327 }
328
329 return NULL;
330}
331
332void
333bgp_default_update_send (struct peer *peer, struct attr *attr,
334 afi_t afi, safi_t safi, struct peer *from)
335{
336 struct stream *s;
337 struct stream *packet;
338 struct prefix p;
339 unsigned long pos;
340 bgp_size_t total_attr_len;
341 char attrstr[BUFSIZ];
342 char buf[BUFSIZ];
343
344#ifdef DISABLE_BGP_ANNOUNCE
345 return;
346#endif /* DISABLE_BGP_ANNOUNCE */
347
348 if (afi == AFI_IP)
349 str2prefix ("0.0.0.0/0", &p);
350#ifdef HAVE_IPV6
351 else
352 str2prefix ("::/0", &p);
353#endif /* HAVE_IPV6 */
354
355 /* Logging the attribute. */
356 if (BGP_DEBUG (update, UPDATE_OUT))
357 {
358 bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
ajs6b514742004-12-08 21:03:23 +0000359 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d %s",
paul718e3742002-12-13 20:15:29 +0000360 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ),
361 p.prefixlen, attrstr);
362 }
363
364 s = stream_new (BGP_MAX_PACKET_SIZE);
365
366 /* Make BGP update packet. */
367 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
368
369 /* Unfeasible Routes Length. */
370 stream_putw (s, 0);
371
372 /* Make place for total attribute length. */
373 pos = stream_get_putp (s);
374 stream_putw (s, 0);
375 total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &p, afi, safi, from, NULL, NULL);
376
377 /* Set Total Path Attribute Length. */
378 stream_putw_at (s, pos, total_attr_len);
379
380 /* NLRI set. */
381 if (p.family == AF_INET && safi == SAFI_UNICAST)
382 stream_put_prefix (s, &p);
383
384 /* Set size. */
385 bgp_packet_set_size (s);
386
387 packet = bgp_packet_dup (s);
388 stream_free (s);
389
390 /* Dump packet if debug option is set. */
391#ifdef DEBUG
392 bgp_packet_dump (packet);
393#endif /* DEBUG */
394
395 /* Add packet to the peer. */
396 bgp_packet_add (peer, packet);
397
pauleb821182004-05-01 08:44:08 +0000398 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000399}
400
401void
402bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi)
403{
404 struct stream *s;
405 struct stream *packet;
406 struct prefix p;
407 unsigned long pos;
408 unsigned long cp;
409 bgp_size_t unfeasible_len;
410 bgp_size_t total_attr_len;
411 char buf[BUFSIZ];
412
413#ifdef DISABLE_BGP_ANNOUNCE
414 return;
415#endif /* DISABLE_BGP_ANNOUNCE */
416
417 if (afi == AFI_IP)
418 str2prefix ("0.0.0.0/0", &p);
419#ifdef HAVE_IPV6
420 else
421 str2prefix ("::/0", &p);
422#endif /* HAVE_IPV6 */
423
424 total_attr_len = 0;
425 pos = 0;
426
427 if (BGP_DEBUG (update, UPDATE_OUT))
ajs6b514742004-12-08 21:03:23 +0000428 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
paul718e3742002-12-13 20:15:29 +0000429 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ),
430 p.prefixlen);
431
432 s = stream_new (BGP_MAX_PACKET_SIZE);
433
434 /* Make BGP update packet. */
435 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
436
437 /* Unfeasible Routes Length. */;
438 cp = stream_get_putp (s);
439 stream_putw (s, 0);
440
441 /* Withdrawn Routes. */
442 if (p.family == AF_INET && safi == SAFI_UNICAST)
443 {
444 stream_put_prefix (s, &p);
445
446 unfeasible_len = stream_get_putp (s) - cp - 2;
447
448 /* Set unfeasible len. */
449 stream_putw_at (s, cp, unfeasible_len);
450
451 /* Set total path attribute length. */
452 stream_putw (s, 0);
453 }
454 else
455 {
456 pos = stream_get_putp (s);
457 stream_putw (s, 0);
458 total_attr_len = bgp_packet_withdraw (peer, s, &p, afi, safi, NULL, NULL);
459
460 /* Set total path attribute length. */
461 stream_putw_at (s, pos, total_attr_len);
462 }
463
464 bgp_packet_set_size (s);
465
466 packet = bgp_packet_dup (s);
467 stream_free (s);
468
469 /* Add packet to the peer. */
470 bgp_packet_add (peer, packet);
471
pauleb821182004-05-01 08:44:08 +0000472 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000473}
474
475/* Get next packet to be written. */
476struct stream *
477bgp_write_packet (struct peer *peer)
478{
479 afi_t afi;
480 safi_t safi;
481 struct stream *s = NULL;
482 struct bgp_advertise *adv;
483
484 s = stream_fifo_head (peer->obuf);
485 if (s)
486 return s;
487
488 for (afi = AFI_IP; afi < AFI_MAX; afi++)
489 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
490 {
491 adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw);
492 if (adv)
493 {
494 s = bgp_withdraw_packet (peer, afi, safi);
495 if (s)
496 return s;
497 }
498 }
499
500 for (afi = AFI_IP; afi < AFI_MAX; afi++)
501 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
502 {
503 adv = FIFO_HEAD (&peer->sync[afi][safi]->update);
504 if (adv)
505 {
506 if (adv->binfo && adv->binfo->uptime < peer->synctime)
507 s = bgp_update_packet (peer, afi, safi);
508
509 if (s)
510 return s;
511 }
512 }
513
514 return NULL;
515}
516
517/* Is there partially written packet or updates we can send right
518 now. */
519int
520bgp_write_proceed (struct peer *peer)
521{
522 afi_t afi;
523 safi_t safi;
524 struct bgp_advertise *adv;
525
526 if (stream_fifo_head (peer->obuf))
527 return 1;
528
529 for (afi = AFI_IP; afi < AFI_MAX; afi++)
530 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
531 if (FIFO_HEAD (&peer->sync[afi][safi]->withdraw))
532 return 1;
533
534 for (afi = AFI_IP; afi < AFI_MAX; afi++)
535 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
536 if ((adv = FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL)
537 if (adv->binfo->uptime < peer->synctime)
538 return 1;
539
540 return 0;
541}
542
543/* Write packet to the peer. */
544int
545bgp_write (struct thread *thread)
546{
547 struct peer *peer;
548 u_char type;
549 struct stream *s;
550 int num;
paulfd79ac92004-10-13 05:06:08 +0000551 unsigned int count = 0;
paul718e3742002-12-13 20:15:29 +0000552 int write_errno;
553
554 /* Yes first of all get peer pointer. */
555 peer = THREAD_ARG (thread);
556 peer->t_write = NULL;
557
558 /* For non-blocking IO check. */
559 if (peer->status == Connect)
560 {
561 bgp_connect_check (peer);
562 return 0;
563 }
564
565 /* Nonblocking write until TCP output buffer is full. */
566 while (1)
567 {
568 int writenum;
569
570 s = bgp_write_packet (peer);
571 if (! s)
572 return 0;
573
574 /* Number of bytes to be sent. */
575 writenum = stream_get_endp (s) - stream_get_getp (s);
576
577 /* Call write() system call. */
pauleb821182004-05-01 08:44:08 +0000578 num = write (peer->fd, STREAM_PNT (s), writenum);
paul718e3742002-12-13 20:15:29 +0000579 write_errno = errno;
580 if (num <= 0)
581 {
582 /* Partial write. */
583 if (write_errno == EWOULDBLOCK || write_errno == EAGAIN)
584 break;
585
586 bgp_stop (peer);
587 peer->status = Idle;
588 bgp_timer_set (peer);
589 return 0;
590 }
591 if (num != writenum)
592 {
593 stream_forward (s, num);
594
595 if (write_errno == EAGAIN)
596 break;
597
598 continue;
599 }
600
601 /* Retrieve BGP packet type. */
602 stream_set_getp (s, BGP_MARKER_SIZE + 2);
603 type = stream_getc (s);
604
605 switch (type)
606 {
607 case BGP_MSG_OPEN:
608 peer->open_out++;
609 break;
610 case BGP_MSG_UPDATE:
611 peer->update_out++;
612 break;
613 case BGP_MSG_NOTIFY:
614 peer->notify_out++;
615 /* Double start timer. */
616 peer->v_start *= 2;
617
618 /* Overflow check. */
619 if (peer->v_start >= (60 * 2))
620 peer->v_start = (60 * 2);
621
622 /* BGP_EVENT_ADD (peer, BGP_Stop); */
623 bgp_stop (peer);
624 peer->status = Idle;
625 bgp_timer_set (peer);
626 return 0;
627 break;
628 case BGP_MSG_KEEPALIVE:
629 peer->keepalive_out++;
630 break;
631 case BGP_MSG_ROUTE_REFRESH_NEW:
632 case BGP_MSG_ROUTE_REFRESH_OLD:
633 peer->refresh_out++;
634 break;
635 case BGP_MSG_CAPABILITY:
636 peer->dynamic_cap_out++;
637 break;
638 }
639
640 /* OK we send packet so delete it. */
641 bgp_packet_delete (peer);
642
643 if (++count >= BGP_WRITE_PACKET_MAX)
644 break;
645 }
646
647 if (bgp_write_proceed (peer))
pauleb821182004-05-01 08:44:08 +0000648 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000649
650 return 0;
651}
652
653/* This is only for sending NOTIFICATION message to neighbor. */
654int
655bgp_write_notify (struct peer *peer)
656{
657 int ret;
658 u_char type;
659 struct stream *s;
660
661 /* There should be at least one packet. */
662 s = stream_fifo_head (peer->obuf);
663 if (!s)
664 return 0;
665 assert (stream_get_endp (s) >= BGP_HEADER_SIZE);
666
667 /* I'm not sure fd is writable. */
pauleb821182004-05-01 08:44:08 +0000668 ret = writen (peer->fd, STREAM_DATA (s), stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000669 if (ret <= 0)
670 {
671 bgp_stop (peer);
672 peer->status = Idle;
673 bgp_timer_set (peer);
674 return 0;
675 }
676
677 /* Retrieve BGP packet type. */
678 stream_set_getp (s, BGP_MARKER_SIZE + 2);
679 type = stream_getc (s);
680
681 assert (type == BGP_MSG_NOTIFY);
682
683 /* Type should be notify. */
684 peer->notify_out++;
685
686 /* Double start timer. */
687 peer->v_start *= 2;
688
689 /* Overflow check. */
690 if (peer->v_start >= (60 * 2))
691 peer->v_start = (60 * 2);
692
693 /* We don't call event manager at here for avoiding other events. */
694 bgp_stop (peer);
695 peer->status = Idle;
696 bgp_timer_set (peer);
697
698 return 0;
699}
700
701/* Make keepalive packet and send it to the peer. */
702void
703bgp_keepalive_send (struct peer *peer)
704{
705 struct stream *s;
706 int length;
707
708 s = stream_new (BGP_MAX_PACKET_SIZE);
709
710 /* Make keepalive packet. */
711 bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE);
712
713 /* Set packet size. */
714 length = bgp_packet_set_size (s);
715
716 /* Dump packet if debug option is set. */
717 /* bgp_packet_dump (s); */
718
719 if (BGP_DEBUG (keepalive, KEEPALIVE))
ajs6b514742004-12-08 21:03:23 +0000720 zlog_debug ("%s sending KEEPALIVE", peer->host);
paul718e3742002-12-13 20:15:29 +0000721 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000722 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000723 peer->host, BGP_MSG_KEEPALIVE, length);
724
725 /* Add packet to the peer. */
726 bgp_packet_add (peer, s);
727
pauleb821182004-05-01 08:44:08 +0000728 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000729}
730
731/* Make open packet and send it to the peer. */
732void
733bgp_open_send (struct peer *peer)
734{
735 struct stream *s;
736 int length;
737 u_int16_t send_holdtime;
738 as_t local_as;
739
740 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
741 send_holdtime = peer->holdtime;
742 else
743 send_holdtime = peer->bgp->default_holdtime;
744
745 /* local-as Change */
746 if (peer->change_local_as)
747 local_as = peer->change_local_as;
748 else
749 local_as = peer->local_as;
750
751 s = stream_new (BGP_MAX_PACKET_SIZE);
752
753 /* Make open packet. */
754 bgp_packet_set_marker (s, BGP_MSG_OPEN);
755
756 /* Set open packet values. */
757 stream_putc (s, BGP_VERSION_4); /* BGP version */
758 stream_putw (s, local_as); /* My Autonomous System*/
759 stream_putw (s, send_holdtime); /* Hold Time */
760 stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */
761
762 /* Set capability code. */
763 bgp_open_capability (s, peer);
764
765 /* Set BGP packet length. */
766 length = bgp_packet_set_size (s);
767
768 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000769 zlog_debug ("%s sending OPEN, version %d, my as %d, holdtime %d, id %s",
paul718e3742002-12-13 20:15:29 +0000770 peer->host, BGP_VERSION_4, local_as,
771 send_holdtime, inet_ntoa (peer->local_id));
772
773 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000774 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000775 peer->host, BGP_MSG_OPEN, length);
776
777 /* Dump packet if debug option is set. */
778 /* bgp_packet_dump (s); */
779
780 /* Add packet to the peer. */
781 bgp_packet_add (peer, s);
782
pauleb821182004-05-01 08:44:08 +0000783 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000784}
785
786/* Send BGP notify packet with data potion. */
787void
788bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code,
789 u_char *data, size_t datalen)
790{
791 struct stream *s;
792 int length;
793
794 /* Allocate new stream. */
795 s = stream_new (BGP_MAX_PACKET_SIZE);
796
797 /* Make nitify packet. */
798 bgp_packet_set_marker (s, BGP_MSG_NOTIFY);
799
800 /* Set notify packet values. */
801 stream_putc (s, code); /* BGP notify code */
802 stream_putc (s, sub_code); /* BGP notify sub_code */
803
804 /* If notify data is present. */
805 if (data)
806 stream_write (s, data, datalen);
807
808 /* Set BGP packet length. */
809 length = bgp_packet_set_size (s);
810
811 /* Add packet to the peer. */
812 stream_fifo_clean (peer->obuf);
813 bgp_packet_add (peer, s);
814
815 /* For debug */
816 {
817 struct bgp_notify bgp_notify;
818 int first = 0;
819 int i;
820 char c[4];
821
822 bgp_notify.code = code;
823 bgp_notify.subcode = sub_code;
824 bgp_notify.data = NULL;
825 bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE;
826
827 if (bgp_notify.length)
828 {
829 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
830 for (i = 0; i < bgp_notify.length; i++)
831 if (first)
832 {
833 sprintf (c, " %02x", data[i]);
834 strcat (bgp_notify.data, c);
835 }
836 else
837 {
838 first = 1;
839 sprintf (c, "%02x", data[i]);
840 strcpy (bgp_notify.data, c);
841 }
842 }
843 bgp_notify_print (peer, &bgp_notify, "sending");
844 if (bgp_notify.data)
845 XFREE (MTYPE_TMP, bgp_notify.data);
846 }
847
848 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000849 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000850 peer->host, BGP_MSG_NOTIFY, length);
851
hassoe0701b72004-05-20 09:19:34 +0000852 /* peer reset cause */
853 if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE)
854 {
855 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
856 peer->last_reset = PEER_DOWN_USER_RESET;
857 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
858 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
859 else
860 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
861 }
862
paul718e3742002-12-13 20:15:29 +0000863 /* Call imidiately. */
864 BGP_WRITE_OFF (peer->t_write);
865
866 bgp_write_notify (peer);
867}
868
869/* Send BGP notify packet. */
870void
871bgp_notify_send (struct peer *peer, u_char code, u_char sub_code)
872{
873 bgp_notify_send_with_data (peer, code, sub_code, NULL, 0);
874}
875
paulfd79ac92004-10-13 05:06:08 +0000876const char *
paul718e3742002-12-13 20:15:29 +0000877afi2str (afi_t afi)
878{
879 if (afi == AFI_IP)
880 return "AFI_IP";
881 else if (afi == AFI_IP6)
882 return "AFI_IP6";
883 else
884 return "Unknown AFI";
885}
886
paulfd79ac92004-10-13 05:06:08 +0000887const char *
paul718e3742002-12-13 20:15:29 +0000888safi2str (safi_t safi)
889{
890 if (safi == SAFI_UNICAST)
891 return "SAFI_UNICAST";
892 else if (safi == SAFI_MULTICAST)
893 return "SAFI_MULTICAST";
894 else if (safi == SAFI_MPLS_VPN || safi == BGP_SAFI_VPNV4)
895 return "SAFI_MPLS_VPN";
896 else
897 return "Unknown SAFI";
898}
899
900/* Send route refresh message to the peer. */
901void
902bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi,
903 u_char orf_type, u_char when_to_refresh, int remove)
904{
905 struct stream *s;
906 struct stream *packet;
907 int length;
908 struct bgp_filter *filter;
909 int orf_refresh = 0;
910
911#ifdef DISABLE_BGP_ANNOUNCE
912 return;
913#endif /* DISABLE_BGP_ANNOUNCE */
914
915 filter = &peer->filter[afi][safi];
916
917 /* Adjust safi code. */
918 if (safi == SAFI_MPLS_VPN)
919 safi = BGP_SAFI_VPNV4;
920
921 s = stream_new (BGP_MAX_PACKET_SIZE);
922
923 /* Make BGP update packet. */
924 if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
925 bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW);
926 else
927 bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD);
928
929 /* Encode Route Refresh message. */
930 stream_putw (s, afi);
931 stream_putc (s, 0);
932 stream_putc (s, safi);
933
934 if (orf_type == ORF_TYPE_PREFIX
935 || orf_type == ORF_TYPE_PREFIX_OLD)
936 if (remove || filter->plist[FILTER_IN].plist)
937 {
938 u_int16_t orf_len;
939 unsigned long orfp;
940
941 orf_refresh = 1;
942 stream_putc (s, when_to_refresh);
943 stream_putc (s, orf_type);
944 orfp = stream_get_putp (s);
945 stream_putw (s, 0);
946
947 if (remove)
948 {
949 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
950 stream_putc (s, ORF_COMMON_PART_REMOVE_ALL);
951 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000952 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +0000953 peer->host, orf_type,
954 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
955 afi, safi);
956 }
957 else
958 {
959 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
960 prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist,
961 ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT,
962 ORF_COMMON_PART_DENY);
963 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000964 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +0000965 peer->host, orf_type,
966 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
967 afi, safi);
968 }
969
970 /* Total ORF Entry Len. */
971 orf_len = stream_get_putp (s) - orfp - 2;
972 stream_putw_at (s, orfp, orf_len);
973 }
974
975 /* Set packet size. */
976 length = bgp_packet_set_size (s);
977
978 if (BGP_DEBUG (normal, NORMAL))
979 {
980 if (! orf_refresh)
ajs6b514742004-12-08 21:03:23 +0000981 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +0000982 peer->host, afi, safi);
ajs6b514742004-12-08 21:03:23 +0000983 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000984 peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ?
985 BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length);
986 }
987
988 /* Make real packet. */
989 packet = bgp_packet_dup (s);
990 stream_free (s);
991
992 /* Add packet to the peer. */
993 bgp_packet_add (peer, packet);
994
pauleb821182004-05-01 08:44:08 +0000995 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000996}
997
998/* Send capability message to the peer. */
999void
1000bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi,
1001 int capability_code, int action)
1002{
1003 struct stream *s;
1004 struct stream *packet;
1005 int length;
1006
1007 /* Adjust safi code. */
1008 if (safi == SAFI_MPLS_VPN)
1009 safi = BGP_SAFI_VPNV4;
1010
1011 s = stream_new (BGP_MAX_PACKET_SIZE);
1012
1013 /* Make BGP update packet. */
1014 bgp_packet_set_marker (s, BGP_MSG_CAPABILITY);
1015
1016 /* Encode MP_EXT capability. */
1017 if (capability_code == CAPABILITY_CODE_MP)
1018 {
1019 stream_putc (s, action);
1020 stream_putc (s, CAPABILITY_CODE_MP);
1021 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1022 stream_putw (s, afi);
1023 stream_putc (s, 0);
1024 stream_putc (s, safi);
1025
1026 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001027 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00001028 peer->host, action == CAPABILITY_ACTION_SET ?
1029 "Advertising" : "Removing", afi, safi);
1030 }
1031
1032 /* Encode Route Refresh capability. */
1033 if (capability_code == CAPABILITY_CODE_REFRESH)
1034 {
1035 stream_putc (s, action);
1036 stream_putc (s, CAPABILITY_CODE_REFRESH);
1037 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
1038 stream_putc (s, action);
1039 stream_putc (s, CAPABILITY_CODE_REFRESH_OLD);
1040 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
1041
1042 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001043 zlog_debug ("%s sending CAPABILITY has %s ROUTE-REFRESH capability",
paul718e3742002-12-13 20:15:29 +00001044 peer->host, action == CAPABILITY_ACTION_SET ?
1045 "Advertising" : "Removing");
1046 }
1047
1048 /* Set packet size. */
1049 length = bgp_packet_set_size (s);
1050
1051 /* Make real packet. */
1052 packet = bgp_packet_dup (s);
1053 stream_free (s);
1054
1055 /* Add packet to the peer. */
1056 bgp_packet_add (peer, packet);
1057
1058 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001059 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +00001060 peer->host, BGP_MSG_CAPABILITY, length);
1061
pauleb821182004-05-01 08:44:08 +00001062 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +00001063}
1064
1065/* RFC1771 6.8 Connection collision detection. */
1066int
pauleb821182004-05-01 08:44:08 +00001067bgp_collision_detect (struct peer *new, struct in_addr remote_id)
paul718e3742002-12-13 20:15:29 +00001068{
pauleb821182004-05-01 08:44:08 +00001069 struct peer *peer;
paul718e3742002-12-13 20:15:29 +00001070 struct listnode *nn;
1071 struct bgp *bgp;
1072
1073 bgp = bgp_get_default ();
1074 if (! bgp)
1075 return 0;
1076
1077 /* Upon receipt of an OPEN message, the local system must examine
1078 all of its connections that are in the OpenConfirm state. A BGP
1079 speaker may also examine connections in an OpenSent state if it
1080 knows the BGP Identifier of the peer by means outside of the
1081 protocol. If among these connections there is a connection to a
1082 remote BGP speaker whose BGP Identifier equals the one in the
1083 OPEN message, then the local system performs the following
1084 collision resolution procedure: */
1085
1086 LIST_LOOP (bgp->peer, peer, nn)
1087 {
1088 /* Under OpenConfirm status, local peer structure already hold
1089 remote router ID. */
pauleb821182004-05-01 08:44:08 +00001090
1091 if (peer != new
1092 && (peer->status == OpenConfirm || peer->status == OpenSent)
1093 && sockunion_same (&peer->su, &new->su))
1094 {
paul718e3742002-12-13 20:15:29 +00001095 /* 1. The BGP Identifier of the local system is compared to
1096 the BGP Identifier of the remote system (as specified in
1097 the OPEN message). */
1098
1099 if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr))
1100 {
1101 /* 2. If the value of the local BGP Identifier is less
1102 than the remote one, the local system closes BGP
1103 connection that already exists (the one that is
1104 already in the OpenConfirm state), and accepts BGP
1105 connection initiated by the remote system. */
1106
pauleb821182004-05-01 08:44:08 +00001107 if (peer->fd >= 0)
hassoe0701b72004-05-20 09:19:34 +00001108 bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
paul718e3742002-12-13 20:15:29 +00001109 return 1;
1110 }
1111 else
1112 {
1113 /* 3. Otherwise, the local system closes newly created
1114 BGP connection (the one associated with the newly
1115 received OPEN message), and continues to use the
1116 existing one (the one that is already in the
1117 OpenConfirm state). */
1118
pauleb821182004-05-01 08:44:08 +00001119 if (new->fd >= 0)
paulf5ba3872004-07-09 12:11:31 +00001120 bgp_notify_send (new, BGP_NOTIFY_CEASE,
1121 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
paul718e3742002-12-13 20:15:29 +00001122 return -1;
1123 }
pauleb821182004-05-01 08:44:08 +00001124 }
1125 }
paul718e3742002-12-13 20:15:29 +00001126 return 0;
1127}
1128
1129int
1130bgp_open_receive (struct peer *peer, bgp_size_t size)
1131{
1132 int ret;
1133 u_char version;
1134 u_char optlen;
1135 u_int16_t holdtime;
1136 u_int16_t send_holdtime;
1137 as_t remote_as;
1138 struct peer *realpeer;
1139 struct in_addr remote_id;
1140 int capability;
paul5228ad22004-06-04 17:58:18 +00001141 u_int8_t notify_data_remote_as[2];
1142 u_int8_t notify_data_remote_id[4];
paul718e3742002-12-13 20:15:29 +00001143
1144 realpeer = NULL;
1145
1146 /* Parse open packet. */
1147 version = stream_getc (peer->ibuf);
1148 memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2);
1149 remote_as = stream_getw (peer->ibuf);
1150 holdtime = stream_getw (peer->ibuf);
1151 memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
1152 remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
1153
1154 /* Receive OPEN message log */
1155 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001156 zlog_debug ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s",
paul718e3742002-12-13 20:15:29 +00001157 peer->host, version, remote_as, holdtime,
1158 inet_ntoa (remote_id));
1159
1160 /* Lookup peer from Open packet. */
1161 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1162 {
1163 int as = 0;
1164
1165 realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as);
1166
1167 if (! realpeer)
1168 {
1169 /* Peer's source IP address is check in bgp_accept(), so this
1170 must be AS number mismatch or remote-id configuration
1171 mismatch. */
1172 if (as)
1173 {
1174 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001175 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1176 peer->host, inet_ntoa (remote_id));
1177 bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1178 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1179 notify_data_remote_id, 4);
paul718e3742002-12-13 20:15:29 +00001180 }
1181 else
1182 {
1183 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001184 zlog_debug ("%s bad OPEN, remote AS is %d, expected %d",
1185 peer->host, remote_as, peer->as);
1186 bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1187 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1188 notify_data_remote_as, 2);
paul718e3742002-12-13 20:15:29 +00001189 }
1190 return -1;
1191 }
1192 }
1193
1194 /* When collision is detected and this peer is closed. Retrun
1195 immidiately. */
1196 ret = bgp_collision_detect (peer, remote_id);
1197 if (ret < 0)
1198 return ret;
1199
pauleb821182004-05-01 08:44:08 +00001200 /* Hack part. */
1201 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1202 {
1203 if (ret == 0 && realpeer->status != Active
1204 && realpeer->status != OpenSent
1205 && realpeer->status != OpenConfirm)
1206 {
1207 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00001208 zlog_debug ("%s [Event] peer's status is %s close connection",
pauleb821182004-05-01 08:44:08 +00001209 realpeer->host, LOOKUP (bgp_status_msg, peer->status));
1210 return -1;
1211 }
1212
1213 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00001214 zlog_debug ("%s [Event] Transfer temporary BGP peer to existing one",
pauleb821182004-05-01 08:44:08 +00001215 peer->host);
1216
1217 bgp_stop (realpeer);
1218
1219 /* Transfer file descriptor. */
1220 realpeer->fd = peer->fd;
1221 peer->fd = -1;
1222
1223 /* Transfer input buffer. */
1224 stream_free (realpeer->ibuf);
1225 realpeer->ibuf = peer->ibuf;
1226 realpeer->packet_size = peer->packet_size;
1227 peer->ibuf = NULL;
1228
1229 /* Transfer status. */
1230 realpeer->status = peer->status;
1231 bgp_stop (peer);
1232
1233 /* peer pointer change. Open packet send to neighbor. */
1234 peer = realpeer;
1235 bgp_open_send (peer);
1236 if (peer->fd < 0)
1237 {
1238 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1239 peer->fd);
1240 return -1;
1241 }
1242 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
1243 }
1244
paul718e3742002-12-13 20:15:29 +00001245 /* remote router-id check. */
1246 if (remote_id.s_addr == 0
1247 || ntohl (remote_id.s_addr) >= 0xe0000000
1248 || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
1249 {
1250 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001251 zlog_debug ("%s bad OPEN, wrong router identifier %s",
paul718e3742002-12-13 20:15:29 +00001252 peer->host, inet_ntoa (remote_id));
1253 bgp_notify_send_with_data (peer,
1254 BGP_NOTIFY_OPEN_ERR,
1255 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1256 notify_data_remote_id, 4);
1257 return -1;
1258 }
1259
1260 /* Set remote router-id */
1261 peer->remote_id = remote_id;
1262
1263 /* Peer BGP version check. */
1264 if (version != BGP_VERSION_4)
1265 {
paul5228ad22004-06-04 17:58:18 +00001266 u_int8_t maxver = BGP_VERSION_4;
paul718e3742002-12-13 20:15:29 +00001267 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001268 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
paul718e3742002-12-13 20:15:29 +00001269 peer->host, version, BGP_VERSION_4);
1270 bgp_notify_send_with_data (peer,
1271 BGP_NOTIFY_OPEN_ERR,
1272 BGP_NOTIFY_OPEN_UNSUP_VERSION,
paul5228ad22004-06-04 17:58:18 +00001273 &maxver, 1);
paul718e3742002-12-13 20:15:29 +00001274 return -1;
1275 }
1276
1277 /* Check neighbor as number. */
1278 if (remote_as != peer->as)
1279 {
1280 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001281 zlog_debug ("%s bad OPEN, remote AS is %d, expected %d",
paul718e3742002-12-13 20:15:29 +00001282 peer->host, remote_as, peer->as);
1283 bgp_notify_send_with_data (peer,
1284 BGP_NOTIFY_OPEN_ERR,
1285 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1286 notify_data_remote_as, 2);
1287 return -1;
1288 }
1289
1290 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1291 calculate the value of the Hold Timer by using the smaller of its
1292 configured Hold Time and the Hold Time received in the OPEN message.
1293 The Hold Time MUST be either zero or at least three seconds. An
1294 implementation may reject connections on the basis of the Hold Time. */
1295
1296 if (holdtime < 3 && holdtime != 0)
1297 {
1298 bgp_notify_send (peer,
1299 BGP_NOTIFY_OPEN_ERR,
1300 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
1301 return -1;
1302 }
1303
1304 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1305 would be one third of the Hold Time interval. KEEPALIVE messages
1306 MUST NOT be sent more frequently than one per second. An
1307 implementation MAY adjust the rate at which it sends KEEPALIVE
1308 messages as a function of the Hold Time interval. */
1309
1310 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
1311 send_holdtime = peer->holdtime;
1312 else
1313 send_holdtime = peer->bgp->default_holdtime;
1314
1315 if (holdtime < send_holdtime)
1316 peer->v_holdtime = holdtime;
1317 else
1318 peer->v_holdtime = send_holdtime;
1319
1320 peer->v_keepalive = peer->v_holdtime / 3;
1321
1322 /* Open option part parse. */
1323 capability = 0;
1324 optlen = stream_getc (peer->ibuf);
1325 if (optlen != 0)
1326 {
1327 ret = bgp_open_option_parse (peer, optlen, &capability);
1328 if (ret < 0)
1329 return ret;
1330
1331 stream_forward (peer->ibuf, optlen);
1332 }
1333 else
1334 {
1335 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001336 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
paul718e3742002-12-13 20:15:29 +00001337 peer->host);
1338 }
1339
1340 /* Override capability. */
1341 if (! capability || CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1342 {
1343 peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
1344 peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
1345 peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
1346 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
1347 }
1348
1349 /* Get sockname. */
1350 bgp_getsockname (peer);
1351
1352 BGP_EVENT_ADD (peer, Receive_OPEN_message);
1353
1354 peer->packet_size = 0;
1355 if (peer->ibuf)
1356 stream_reset (peer->ibuf);
1357
1358 return 0;
1359}
1360
1361/* Parse BGP Update packet and make attribute object. */
1362int
1363bgp_update_receive (struct peer *peer, bgp_size_t size)
1364{
1365 int ret;
1366 u_char *end;
1367 struct stream *s;
1368 struct attr attr;
1369 bgp_size_t attribute_len;
1370 bgp_size_t update_len;
1371 bgp_size_t withdraw_len;
1372 struct bgp_nlri update;
1373 struct bgp_nlri withdraw;
1374 struct bgp_nlri mp_update;
1375 struct bgp_nlri mp_withdraw;
paule01f9cb2004-07-09 17:48:53 +00001376 char attrstr[BUFSIZ] = "";
paul718e3742002-12-13 20:15:29 +00001377
1378 /* Status must be Established. */
1379 if (peer->status != Established)
1380 {
1381 zlog_err ("%s [FSM] Update packet received under status %s",
1382 peer->host, LOOKUP (bgp_status_msg, peer->status));
1383 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1384 return -1;
1385 }
1386
1387 /* Set initial values. */
1388 memset (&attr, 0, sizeof (struct attr));
1389 memset (&update, 0, sizeof (struct bgp_nlri));
1390 memset (&withdraw, 0, sizeof (struct bgp_nlri));
1391 memset (&mp_update, 0, sizeof (struct bgp_nlri));
1392 memset (&mp_withdraw, 0, sizeof (struct bgp_nlri));
1393
1394 s = peer->ibuf;
1395 end = stream_pnt (s) + size;
1396
1397 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1398 Length is too large (i.e., if Unfeasible Routes Length + Total
1399 Attribute Length + 23 exceeds the message Length), then the Error
1400 Subcode is set to Malformed Attribute List. */
1401 if (stream_pnt (s) + 2 > end)
1402 {
1403 zlog_err ("%s [Error] Update packet error"
1404 " (packet length is short for unfeasible length)",
1405 peer->host);
1406 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1407 BGP_NOTIFY_UPDATE_MAL_ATTR);
1408 return -1;
1409 }
1410
1411 /* Unfeasible Route Length. */
1412 withdraw_len = stream_getw (s);
1413
1414 /* Unfeasible Route Length check. */
1415 if (stream_pnt (s) + withdraw_len > end)
1416 {
1417 zlog_err ("%s [Error] Update packet error"
1418 " (packet unfeasible length overflow %d)",
1419 peer->host, withdraw_len);
1420 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1421 BGP_NOTIFY_UPDATE_MAL_ATTR);
1422 return -1;
1423 }
1424
1425 /* Unfeasible Route packet format check. */
1426 if (withdraw_len > 0)
1427 {
1428 ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len);
1429 if (ret < 0)
1430 return -1;
1431
1432 if (BGP_DEBUG (packet, PACKET_RECV))
ajs6b514742004-12-08 21:03:23 +00001433 zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer->host);
paul718e3742002-12-13 20:15:29 +00001434
1435 withdraw.afi = AFI_IP;
1436 withdraw.safi = SAFI_UNICAST;
1437 withdraw.nlri = stream_pnt (s);
1438 withdraw.length = withdraw_len;
1439 stream_forward (s, withdraw_len);
1440 }
1441
1442 /* Attribute total length check. */
1443 if (stream_pnt (s) + 2 > end)
1444 {
1445 zlog_warn ("%s [Error] Packet Error"
1446 " (update packet is short for attribute length)",
1447 peer->host);
1448 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1449 BGP_NOTIFY_UPDATE_MAL_ATTR);
1450 return -1;
1451 }
1452
1453 /* Fetch attribute total length. */
1454 attribute_len = stream_getw (s);
1455
1456 /* Attribute length check. */
1457 if (stream_pnt (s) + attribute_len > end)
1458 {
1459 zlog_warn ("%s [Error] Packet Error"
1460 " (update packet attribute length overflow %d)",
1461 peer->host, attribute_len);
1462 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1463 BGP_NOTIFY_UPDATE_MAL_ATTR);
1464 return -1;
1465 }
1466
1467 /* Parse attribute when it exists. */
1468 if (attribute_len)
1469 {
1470 ret = bgp_attr_parse (peer, &attr, attribute_len,
1471 &mp_update, &mp_withdraw);
1472 if (ret < 0)
1473 return -1;
1474 }
1475
1476 /* Logging the attribute. */
1477 if (BGP_DEBUG (update, UPDATE_IN))
1478 {
paule01f9cb2004-07-09 17:48:53 +00001479 ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
1480
1481 if (ret)
ajs6b514742004-12-08 21:03:23 +00001482 zlog (peer->log, LOG_DEBUG, "%s rcvd UPDATE w/ attr: %s",
paule01f9cb2004-07-09 17:48:53 +00001483 peer->host, attrstr);
paul718e3742002-12-13 20:15:29 +00001484 }
1485
1486 /* Network Layer Reachability Information. */
1487 update_len = end - stream_pnt (s);
1488
1489 if (update_len)
1490 {
1491 /* Check NLRI packet format and prefix length. */
1492 ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len);
1493 if (ret < 0)
1494 return -1;
1495
1496 /* Set NLRI portion to structure. */
1497 update.afi = AFI_IP;
1498 update.safi = SAFI_UNICAST;
1499 update.nlri = stream_pnt (s);
1500 update.length = update_len;
1501 stream_forward (s, update_len);
1502 }
1503
1504 /* NLRI is processed only when the peer is configured specific
1505 Address Family and Subsequent Address Family. */
1506 if (peer->afc[AFI_IP][SAFI_UNICAST])
1507 {
1508 if (withdraw.length)
1509 bgp_nlri_parse (peer, NULL, &withdraw);
1510
1511 if (update.length)
1512 {
1513 /* We check well-known attribute only for IPv4 unicast
1514 update. */
1515 ret = bgp_attr_check (peer, &attr);
1516 if (ret < 0)
1517 return -1;
1518
1519 bgp_nlri_parse (peer, &attr, &update);
1520 }
paule01f9cb2004-07-09 17:48:53 +00001521
1522 if (! attribute_len && ! withdraw_len)
1523 {
1524 /* End-of-RIB received */
1525
1526 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001527 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Unicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001528 peer->host);
1529 }
paul718e3742002-12-13 20:15:29 +00001530 }
1531 if (peer->afc[AFI_IP][SAFI_MULTICAST])
1532 {
1533 if (mp_update.length
1534 && mp_update.afi == AFI_IP
1535 && mp_update.safi == SAFI_MULTICAST)
1536 bgp_nlri_parse (peer, &attr, &mp_update);
1537
1538 if (mp_withdraw.length
1539 && mp_withdraw.afi == AFI_IP
1540 && mp_withdraw.safi == SAFI_MULTICAST)
1541 bgp_nlri_parse (peer, NULL, &mp_withdraw);
paule01f9cb2004-07-09 17:48:53 +00001542
1543 if (attribute_len == 6 && ! withdraw_len
1544 && mp_withdraw.afi == AFI_IP
1545 && mp_withdraw.safi == SAFI_MULTICAST
1546 && mp_withdraw.length == 0)
1547 {
1548 /* End-of-RIB received */
1549
1550 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001551 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Multicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001552 peer->host);
1553 }
paul718e3742002-12-13 20:15:29 +00001554 }
1555 if (peer->afc[AFI_IP6][SAFI_UNICAST])
1556 {
1557 if (mp_update.length
1558 && mp_update.afi == AFI_IP6
1559 && mp_update.safi == SAFI_UNICAST)
1560 bgp_nlri_parse (peer, &attr, &mp_update);
1561
1562 if (mp_withdraw.length
1563 && mp_withdraw.afi == AFI_IP6
1564 && mp_withdraw.safi == SAFI_UNICAST)
1565 bgp_nlri_parse (peer, NULL, &mp_withdraw);
paule01f9cb2004-07-09 17:48:53 +00001566
1567 if (attribute_len == 6 && ! withdraw_len
1568 && mp_withdraw.afi == AFI_IP6
1569 && mp_withdraw.safi == SAFI_UNICAST
1570 && mp_withdraw.length == 0)
1571 {
1572 /* End-of-RIB received */
1573
1574 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001575 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Unicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001576 peer->host);
1577 }
paul718e3742002-12-13 20:15:29 +00001578 }
1579 if (peer->afc[AFI_IP6][SAFI_MULTICAST])
1580 {
1581 if (mp_update.length
1582 && mp_update.afi == AFI_IP6
1583 && mp_update.safi == SAFI_MULTICAST)
1584 bgp_nlri_parse (peer, &attr, &mp_update);
1585
1586 if (mp_withdraw.length
1587 && mp_withdraw.afi == AFI_IP6
1588 && mp_withdraw.safi == SAFI_MULTICAST)
1589 bgp_nlri_parse (peer, NULL, &mp_withdraw);
paule01f9cb2004-07-09 17:48:53 +00001590
1591 if (attribute_len == 6 && ! withdraw_len
1592 && mp_withdraw.afi == AFI_IP6
1593 && mp_withdraw.safi == SAFI_MULTICAST
1594 && mp_withdraw.length == 0)
1595 {
1596 /* End-of-RIB received */
1597
1598 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001599 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Multicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001600 peer->host);
1601 }
paul718e3742002-12-13 20:15:29 +00001602 }
1603 if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
1604 {
1605 if (mp_update.length
1606 && mp_update.afi == AFI_IP
1607 && mp_update.safi == BGP_SAFI_VPNV4)
1608 bgp_nlri_parse_vpnv4 (peer, &attr, &mp_update);
1609
1610 if (mp_withdraw.length
1611 && mp_withdraw.afi == AFI_IP
1612 && mp_withdraw.safi == BGP_SAFI_VPNV4)
1613 bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw);
paule01f9cb2004-07-09 17:48:53 +00001614
1615 if (attribute_len == 6 && ! withdraw_len
1616 && mp_withdraw.afi == AFI_IP
1617 && mp_withdraw.safi == BGP_SAFI_VPNV4
1618 && mp_withdraw.length == 0)
1619 {
1620 /* End-of-RIB received */
1621
1622 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001623 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for VPNv4 Unicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001624 peer->host);
1625 }
paul718e3742002-12-13 20:15:29 +00001626 }
1627
1628 /* Everything is done. We unintern temporary structures which
1629 interned in bgp_attr_parse(). */
1630 if (attr.aspath)
1631 aspath_unintern (attr.aspath);
1632 if (attr.community)
1633 community_unintern (attr.community);
1634 if (attr.ecommunity)
1635 ecommunity_unintern (attr.ecommunity);
1636 if (attr.cluster)
1637 cluster_unintern (attr.cluster);
1638 if (attr.transit)
1639 transit_unintern (attr.transit);
1640
1641 /* If peering is stopped due to some reason, do not generate BGP
1642 event. */
1643 if (peer->status != Established)
1644 return 0;
1645
1646 /* Increment packet counter. */
1647 peer->update_in++;
1648 peer->update_time = time (NULL);
1649
1650 /* Generate BGP event. */
1651 BGP_EVENT_ADD (peer, Receive_UPDATE_message);
1652
1653 return 0;
1654}
1655
1656/* Notify message treatment function. */
1657void
1658bgp_notify_receive (struct peer *peer, bgp_size_t size)
1659{
1660 struct bgp_notify bgp_notify;
1661
1662 if (peer->notify.data)
1663 {
1664 XFREE (MTYPE_TMP, peer->notify.data);
1665 peer->notify.data = NULL;
1666 peer->notify.length = 0;
1667 }
1668
1669 bgp_notify.code = stream_getc (peer->ibuf);
1670 bgp_notify.subcode = stream_getc (peer->ibuf);
1671 bgp_notify.length = size - 2;
1672 bgp_notify.data = NULL;
1673
1674 /* Preserv notify code and sub code. */
1675 peer->notify.code = bgp_notify.code;
1676 peer->notify.subcode = bgp_notify.subcode;
1677 /* For further diagnostic record returned Data. */
1678 if (bgp_notify.length)
1679 {
1680 peer->notify.length = size - 2;
1681 peer->notify.data = XMALLOC (MTYPE_TMP, size - 2);
1682 memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2);
1683 }
1684
1685 /* For debug */
1686 {
1687 int i;
1688 int first = 0;
1689 char c[4];
1690
1691 if (bgp_notify.length)
1692 {
1693 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
1694 for (i = 0; i < bgp_notify.length; i++)
1695 if (first)
1696 {
1697 sprintf (c, " %02x", stream_getc (peer->ibuf));
1698 strcat (bgp_notify.data, c);
1699 }
1700 else
1701 {
1702 first = 1;
1703 sprintf (c, "%02x", stream_getc (peer->ibuf));
1704 strcpy (bgp_notify.data, c);
1705 }
1706 }
1707
1708 bgp_notify_print(peer, &bgp_notify, "received");
1709 if (bgp_notify.data)
1710 XFREE (MTYPE_TMP, bgp_notify.data);
1711 }
1712
1713 /* peer count update */
1714 peer->notify_in++;
1715
hassoe0701b72004-05-20 09:19:34 +00001716 if (peer->status == Established)
1717 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
1718
paul718e3742002-12-13 20:15:29 +00001719 /* We have to check for Notify with Unsupported Optional Parameter.
1720 in that case we fallback to open without the capability option.
1721 But this done in bgp_stop. We just mark it here to avoid changing
1722 the fsm tables. */
1723 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
1724 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM )
1725 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1726
1727 /* Also apply to Unsupported Capability until remote router support
1728 capability. */
1729 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
1730 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_CAPBL)
1731 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1732
1733 BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message);
1734}
1735
1736/* Keepalive treatment function -- get keepalive send keepalive */
1737void
1738bgp_keepalive_receive (struct peer *peer, bgp_size_t size)
1739{
1740 if (BGP_DEBUG (keepalive, KEEPALIVE))
ajs6b514742004-12-08 21:03:23 +00001741 zlog_debug ("%s KEEPALIVE rcvd", peer->host);
paul718e3742002-12-13 20:15:29 +00001742
1743 BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message);
1744}
1745
1746/* Route refresh message is received. */
1747void
1748bgp_route_refresh_receive (struct peer *peer, bgp_size_t size)
1749{
1750 afi_t afi;
1751 safi_t safi;
1752 u_char reserved;
1753 struct stream *s;
1754
1755 /* If peer does not have the capability, send notification. */
1756 if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV))
1757 {
1758 plog_err (peer->log, "%s [Error] BGP route refresh is not enabled",
1759 peer->host);
1760 bgp_notify_send (peer,
1761 BGP_NOTIFY_HEADER_ERR,
1762 BGP_NOTIFY_HEADER_BAD_MESTYPE);
1763 return;
1764 }
1765
1766 /* Status must be Established. */
1767 if (peer->status != Established)
1768 {
1769 plog_err (peer->log,
1770 "%s [Error] Route refresh packet received under status %s",
1771 peer->host, LOOKUP (bgp_status_msg, peer->status));
1772 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1773 return;
1774 }
1775
1776 s = peer->ibuf;
1777
1778 /* Parse packet. */
1779 afi = stream_getw (s);
1780 reserved = stream_getc (s);
1781 safi = stream_getc (s);
1782
1783 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001784 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00001785 peer->host, afi, safi);
1786
1787 /* Check AFI and SAFI. */
1788 if ((afi != AFI_IP && afi != AFI_IP6)
1789 || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
1790 && safi != BGP_SAFI_VPNV4))
1791 {
1792 if (BGP_DEBUG (normal, NORMAL))
1793 {
ajs6b514742004-12-08 21:03:23 +00001794 zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
paul718e3742002-12-13 20:15:29 +00001795 peer->host, afi, safi);
1796 }
1797 return;
1798 }
1799
1800 /* Adjust safi code. */
1801 if (safi == BGP_SAFI_VPNV4)
1802 safi = SAFI_MPLS_VPN;
1803
1804 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
1805 {
1806 u_char *end;
1807 u_char when_to_refresh;
1808 u_char orf_type;
1809 u_int16_t orf_len;
1810
1811 if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5)
1812 {
1813 zlog_info ("%s ORF route refresh length error", peer->host);
1814 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1815 return;
1816 }
1817
1818 when_to_refresh = stream_getc (s);
1819 end = stream_pnt (s) + (size - 5);
1820
1821 while (stream_pnt (s) < end)
1822 {
1823 orf_type = stream_getc (s);
1824 orf_len = stream_getw (s);
1825
1826 if (orf_type == ORF_TYPE_PREFIX
1827 || orf_type == ORF_TYPE_PREFIX_OLD)
1828 {
1829 u_char *p_pnt = stream_pnt (s);
1830 u_char *p_end = stream_pnt (s) + orf_len;
1831 struct orf_prefix orfp;
1832 u_char common = 0;
1833 u_int32_t seq;
1834 int psize;
1835 char name[BUFSIZ];
1836 char buf[BUFSIZ];
1837 int ret;
1838
1839 if (BGP_DEBUG (normal, NORMAL))
1840 {
ajs6b514742004-12-08 21:03:23 +00001841 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
paul718e3742002-12-13 20:15:29 +00001842 peer->host, orf_type, orf_len);
1843 }
1844
1845 /* ORF prefix-list name */
1846 sprintf (name, "%s.%d.%d", peer->host, afi, safi);
1847
1848 while (p_pnt < p_end)
1849 {
1850 memset (&orfp, 0, sizeof (struct orf_prefix));
1851 common = *p_pnt++;
1852 if (common & ORF_COMMON_PART_REMOVE_ALL)
1853 {
1854 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001855 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host);
paul718e3742002-12-13 20:15:29 +00001856 prefix_bgp_orf_remove_all (name);
1857 break;
1858 }
1859 memcpy (&seq, p_pnt, sizeof (u_int32_t));
1860 p_pnt += sizeof (u_int32_t);
1861 orfp.seq = ntohl (seq);
1862 orfp.ge = *p_pnt++;
1863 orfp.le = *p_pnt++;
1864 orfp.p.prefixlen = *p_pnt++;
1865 orfp.p.family = afi2family (afi);
1866 psize = PSIZE (orfp.p.prefixlen);
1867 memcpy (&orfp.p.u.prefix, p_pnt, psize);
1868 p_pnt += psize;
1869
1870 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001871 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d",
paul718e3742002-12-13 20:15:29 +00001872 peer->host,
1873 (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
1874 (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
1875 orfp.seq,
1876 inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, BUFSIZ),
1877 orfp.p.prefixlen, orfp.ge, orfp.le);
1878
1879 ret = prefix_bgp_orf_set (name, afi, &orfp,
1880 (common & ORF_COMMON_PART_DENY ? 0 : 1 ),
1881 (common & ORF_COMMON_PART_REMOVE ? 0 : 1));
1882
1883 if (ret != CMD_SUCCESS)
1884 {
1885 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001886 zlog_debug ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer->host);
paul718e3742002-12-13 20:15:29 +00001887 prefix_bgp_orf_remove_all (name);
1888 break;
1889 }
1890 }
1891 peer->orf_plist[afi][safi] =
1892 prefix_list_lookup (AFI_ORF_PREFIX, name);
1893 }
1894 stream_forward (s, orf_len);
1895 }
1896 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001897 zlog_debug ("%s rcvd Refresh %s ORF request", peer->host,
paul718e3742002-12-13 20:15:29 +00001898 when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate");
1899 if (when_to_refresh == REFRESH_DEFER)
1900 return;
1901 }
1902
1903 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1904 if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
1905 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
1906
1907 /* Perform route refreshment to the peer */
1908 bgp_announce_route (peer, afi, safi);
1909}
1910
1911int
1912bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length)
1913{
1914 u_char *end;
1915 struct capability cap;
1916 u_char action;
1917 struct bgp *bgp;
1918 afi_t afi;
1919 safi_t safi;
1920
1921 bgp = peer->bgp;
1922 end = pnt + length;
1923
1924 while (pnt < end)
1925 {
1926 /* We need at least action, capability code and capability length. */
1927 if (pnt + 3 > end)
1928 {
1929 zlog_info ("%s Capability length error", peer->host);
1930 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1931 return -1;
1932 }
1933
1934 action = *pnt;
1935
1936 /* Fetch structure to the byte stream. */
1937 memcpy (&cap, pnt + 1, sizeof (struct capability));
1938
1939 /* Action value check. */
1940 if (action != CAPABILITY_ACTION_SET
1941 && action != CAPABILITY_ACTION_UNSET)
1942 {
1943 zlog_info ("%s Capability Action Value error %d",
1944 peer->host, action);
1945 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1946 return -1;
1947 }
1948
1949 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001950 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
paul718e3742002-12-13 20:15:29 +00001951 peer->host, action, cap.code, cap.length);
1952
1953 /* Capability length check. */
1954 if (pnt + (cap.length + 3) > end)
1955 {
1956 zlog_info ("%s Capability length error", peer->host);
1957 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1958 return -1;
1959 }
1960
1961 /* We know MP Capability Code. */
1962 if (cap.code == CAPABILITY_CODE_MP)
1963 {
1964 afi = ntohs (cap.mpc.afi);
1965 safi = cap.mpc.safi;
1966
1967 /* Ignore capability when override-capability is set. */
1968 if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1969 continue;
1970
1971 /* Address family check. */
1972 if ((afi == AFI_IP
1973 || afi == AFI_IP6)
1974 && (safi == SAFI_UNICAST
1975 || safi == SAFI_MULTICAST
1976 || safi == BGP_SAFI_VPNV4))
1977 {
1978 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001979 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
paul718e3742002-12-13 20:15:29 +00001980 peer->host,
1981 action == CAPABILITY_ACTION_SET
1982 ? "Advertising" : "Removing",
1983 ntohs(cap.mpc.afi) , cap.mpc.safi);
1984
1985 /* Adjust safi code. */
1986 if (safi == BGP_SAFI_VPNV4)
1987 safi = SAFI_MPLS_VPN;
1988
1989 if (action == CAPABILITY_ACTION_SET)
1990 {
1991 peer->afc_recv[afi][safi] = 1;
1992 if (peer->afc[afi][safi])
1993 {
1994 peer->afc_nego[afi][safi] = 1;
1995 bgp_announce_route (peer, afi, safi);
1996 }
1997 }
1998 else
1999 {
2000 peer->afc_recv[afi][safi] = 0;
2001 peer->afc_nego[afi][safi] = 0;
2002
2003 if (peer_active_nego (peer))
2004 bgp_clear_route (peer, afi, safi);
2005 else
2006 BGP_EVENT_ADD (peer, BGP_Stop);
2007 }
2008 }
2009 }
2010 else if (cap.code == CAPABILITY_CODE_REFRESH
2011 || cap.code == CAPABILITY_CODE_REFRESH_OLD)
2012 {
2013 /* Check length. */
2014 if (cap.length != 0)
2015 {
2016 zlog_info ("%s Route Refresh Capability length error %d",
2017 peer->host, cap.length);
2018 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2019 return -1;
2020 }
2021
2022 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002023 zlog_debug ("%s CAPABILITY has %s ROUTE-REFRESH capability(%s) for all address-families",
paul718e3742002-12-13 20:15:29 +00002024 peer->host,
2025 action == CAPABILITY_ACTION_SET
2026 ? "Advertising" : "Removing",
2027 cap.code == CAPABILITY_CODE_REFRESH_OLD
2028 ? "old" : "new");
2029
2030 /* BGP refresh capability */
2031 if (action == CAPABILITY_ACTION_SET)
2032 {
2033 if (cap.code == CAPABILITY_CODE_REFRESH_OLD)
2034 SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
2035 else
2036 SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV);
2037 }
2038 else
2039 {
2040 if (cap.code == CAPABILITY_CODE_REFRESH_OLD)
2041 UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
2042 else
2043 UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV);
2044 }
2045 }
2046 else
2047 {
2048 zlog_warn ("%s unrecognized capability code: %d - ignored",
2049 peer->host, cap.code);
2050 }
2051 pnt += cap.length + 3;
2052 }
2053 return 0;
2054}
2055
2056/* Dynamic Capability is received. */
2057void
2058bgp_capability_receive (struct peer *peer, bgp_size_t size)
2059{
2060 u_char *pnt;
2061 int ret;
2062
2063 /* Fetch pointer. */
2064 pnt = stream_pnt (peer->ibuf);
2065
2066 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002067 zlog_debug ("%s rcv CAPABILITY", peer->host);
paul718e3742002-12-13 20:15:29 +00002068
2069 /* If peer does not have the capability, send notification. */
2070 if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV))
2071 {
2072 plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled",
2073 peer->host);
2074 bgp_notify_send (peer,
2075 BGP_NOTIFY_HEADER_ERR,
2076 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2077 return;
2078 }
2079
2080 /* Status must be Established. */
2081 if (peer->status != Established)
2082 {
2083 plog_err (peer->log,
2084 "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status));
2085 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
2086 return;
2087 }
2088
2089 /* Parse packet. */
2090 ret = bgp_capability_msg_parse (peer, pnt, size);
2091}
2092
2093/* BGP read utility function. */
2094int
2095bgp_read_packet (struct peer *peer)
2096{
2097 int nbytes;
2098 int readsize;
2099
2100 readsize = peer->packet_size - peer->ibuf->putp;
2101
2102 /* If size is zero then return. */
2103 if (! readsize)
2104 return 0;
2105
2106 /* Read packet from fd. */
pauleb821182004-05-01 08:44:08 +00002107 nbytes = stream_read_unblock (peer->ibuf, peer->fd, readsize);
paul718e3742002-12-13 20:15:29 +00002108
2109 /* If read byte is smaller than zero then error occured. */
2110 if (nbytes < 0)
2111 {
2112 if (errno == EAGAIN)
2113 return -1;
2114
2115 plog_err (peer->log, "%s [Error] bgp_read_packet error: %s",
ajs6099b3b2004-11-20 02:06:59 +00002116 peer->host, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00002117 BGP_EVENT_ADD (peer, TCP_fatal_error);
2118 return -1;
2119 }
2120
2121 /* When read byte is zero : clear bgp peer and return */
2122 if (nbytes == 0)
2123 {
2124 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00002125 plog_debug (peer->log, "%s [Event] BGP connection closed fd %d",
pauleb821182004-05-01 08:44:08 +00002126 peer->host, peer->fd);
hassoe0701b72004-05-20 09:19:34 +00002127
2128 if (peer->status == Established)
2129 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2130
paul718e3742002-12-13 20:15:29 +00002131 BGP_EVENT_ADD (peer, TCP_connection_closed);
2132 return -1;
2133 }
2134
2135 /* We read partial packet. */
2136 if (peer->ibuf->putp != peer->packet_size)
2137 return -1;
2138
2139 return 0;
2140}
2141
2142/* Marker check. */
2143int
2144bgp_marker_all_one (struct stream *s, int length)
2145{
2146 int i;
2147
2148 for (i = 0; i < length; i++)
2149 if (s->data[i] != 0xff)
2150 return 0;
2151
2152 return 1;
2153}
2154
2155/* Starting point of packet process function. */
2156int
2157bgp_read (struct thread *thread)
2158{
2159 int ret;
2160 u_char type = 0;
2161 struct peer *peer;
2162 bgp_size_t size;
2163 char notify_data_length[2];
2164
2165 /* Yes first of all get peer pointer. */
2166 peer = THREAD_ARG (thread);
2167 peer->t_read = NULL;
2168
2169 /* For non-blocking IO check. */
2170 if (peer->status == Connect)
2171 {
2172 bgp_connect_check (peer);
2173 goto done;
2174 }
2175 else
2176 {
pauleb821182004-05-01 08:44:08 +00002177 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +00002178 {
pauleb821182004-05-01 08:44:08 +00002179 zlog_err ("bgp_read peer's fd is negative value %d", peer->fd);
paul718e3742002-12-13 20:15:29 +00002180 return -1;
2181 }
pauleb821182004-05-01 08:44:08 +00002182 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
paul718e3742002-12-13 20:15:29 +00002183 }
2184
2185 /* Read packet header to determine type of the packet */
2186 if (peer->packet_size == 0)
2187 peer->packet_size = BGP_HEADER_SIZE;
2188
2189 if (peer->ibuf->putp < BGP_HEADER_SIZE)
2190 {
2191 ret = bgp_read_packet (peer);
2192
2193 /* Header read error or partial read packet. */
2194 if (ret < 0)
2195 goto done;
2196
2197 /* Get size and type. */
2198 stream_forward (peer->ibuf, BGP_MARKER_SIZE);
2199 memcpy (notify_data_length, stream_pnt (peer->ibuf), 2);
2200 size = stream_getw (peer->ibuf);
2201 type = stream_getc (peer->ibuf);
2202
2203 if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0)
ajs6b514742004-12-08 21:03:23 +00002204 zlog_debug ("%s rcv message type %d, length (excl. header) %d",
paul718e3742002-12-13 20:15:29 +00002205 peer->host, type, size - BGP_HEADER_SIZE);
2206
2207 /* Marker check */
paulf5ba3872004-07-09 12:11:31 +00002208 if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE))
paul718e3742002-12-13 20:15:29 +00002209 && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE))
2210 {
2211 bgp_notify_send (peer,
2212 BGP_NOTIFY_HEADER_ERR,
2213 BGP_NOTIFY_HEADER_NOT_SYNC);
2214 goto done;
2215 }
2216
2217 /* BGP type check. */
2218 if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
2219 && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
2220 && type != BGP_MSG_ROUTE_REFRESH_NEW
2221 && type != BGP_MSG_ROUTE_REFRESH_OLD
2222 && type != BGP_MSG_CAPABILITY)
2223 {
2224 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002225 plog_debug (peer->log,
paul718e3742002-12-13 20:15:29 +00002226 "%s unknown message type 0x%02x",
2227 peer->host, type);
2228 bgp_notify_send_with_data (peer,
2229 BGP_NOTIFY_HEADER_ERR,
2230 BGP_NOTIFY_HEADER_BAD_MESTYPE,
2231 &type, 1);
2232 goto done;
2233 }
2234 /* Mimimum packet length check. */
2235 if ((size < BGP_HEADER_SIZE)
2236 || (size > BGP_MAX_PACKET_SIZE)
2237 || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
2238 || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE)
2239 || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE)
2240 || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
2241 || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2242 || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2243 || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE))
2244 {
2245 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002246 plog_debug (peer->log,
paul718e3742002-12-13 20:15:29 +00002247 "%s bad message length - %d for %s",
2248 peer->host, size,
2249 type == 128 ? "ROUTE-REFRESH" :
2250 bgp_type_str[(int) type]);
2251 bgp_notify_send_with_data (peer,
2252 BGP_NOTIFY_HEADER_ERR,
2253 BGP_NOTIFY_HEADER_BAD_MESLEN,
hassoc9e52be2004-09-26 16:09:34 +00002254 (u_char *) notify_data_length, 2);
paul718e3742002-12-13 20:15:29 +00002255 goto done;
2256 }
2257
2258 /* Adjust size to message length. */
2259 peer->packet_size = size;
2260 }
2261
2262 ret = bgp_read_packet (peer);
2263 if (ret < 0)
2264 goto done;
2265
2266 /* Get size and type again. */
2267 size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE);
2268 type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2);
2269
2270 /* BGP packet dump function. */
2271 bgp_dump_packet (peer, type, peer->ibuf);
2272
2273 size = (peer->packet_size - BGP_HEADER_SIZE);
2274
2275 /* Read rest of the packet and call each sort of packet routine */
2276 switch (type)
2277 {
2278 case BGP_MSG_OPEN:
2279 peer->open_in++;
paulf5ba3872004-07-09 12:11:31 +00002280 bgp_open_receive (peer, size); /* XXX return value ignored! */
paul718e3742002-12-13 20:15:29 +00002281 break;
2282 case BGP_MSG_UPDATE:
2283 peer->readtime = time(NULL); /* Last read timer reset */
2284 bgp_update_receive (peer, size);
2285 break;
2286 case BGP_MSG_NOTIFY:
2287 bgp_notify_receive (peer, size);
2288 break;
2289 case BGP_MSG_KEEPALIVE:
2290 peer->readtime = time(NULL); /* Last read timer reset */
2291 bgp_keepalive_receive (peer, size);
2292 break;
2293 case BGP_MSG_ROUTE_REFRESH_NEW:
2294 case BGP_MSG_ROUTE_REFRESH_OLD:
2295 peer->refresh_in++;
2296 bgp_route_refresh_receive (peer, size);
2297 break;
2298 case BGP_MSG_CAPABILITY:
2299 peer->dynamic_cap_in++;
2300 bgp_capability_receive (peer, size);
2301 break;
2302 }
2303
2304 /* Clear input buffer. */
2305 peer->packet_size = 0;
2306 if (peer->ibuf)
2307 stream_reset (peer->ibuf);
2308
2309 done:
2310 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
2311 {
2312 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00002313 zlog_debug ("%s [Event] Accepting BGP peer delete", peer->host);
paul718e3742002-12-13 20:15:29 +00002314 peer_delete (peer);
2315 }
2316 return 0;
2317}