blob: 854b32cdf9dd83358a89639c16261e3213829bd3 [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;
paula24a7e12005-01-05 08:14:13 +0000569 int val;
paul718e3742002-12-13 20:15:29 +0000570
571 s = bgp_write_packet (peer);
572 if (! s)
573 return 0;
paula24a7e12005-01-05 08:14:13 +0000574
575 /* XXX: FIXME, the socket should be NONBLOCK from the start
576 * status shouldnt need to be toggled on each write
577 */
578 val = fcntl (peer->fd, F_GETFL, 0);
579 fcntl (peer->fd, F_SETFL, val|O_NONBLOCK);
paul718e3742002-12-13 20:15:29 +0000580
581 /* Number of bytes to be sent. */
582 writenum = stream_get_endp (s) - stream_get_getp (s);
583
584 /* Call write() system call. */
pauleb821182004-05-01 08:44:08 +0000585 num = write (peer->fd, STREAM_PNT (s), writenum);
paul718e3742002-12-13 20:15:29 +0000586 write_errno = errno;
paula24a7e12005-01-05 08:14:13 +0000587 fcntl (peer->fd, F_SETFL, val);
paul718e3742002-12-13 20:15:29 +0000588 if (num <= 0)
589 {
590 /* Partial write. */
591 if (write_errno == EWOULDBLOCK || write_errno == EAGAIN)
592 break;
593
594 bgp_stop (peer);
595 peer->status = Idle;
596 bgp_timer_set (peer);
597 return 0;
598 }
599 if (num != writenum)
600 {
601 stream_forward (s, num);
602
603 if (write_errno == EAGAIN)
604 break;
605
606 continue;
607 }
608
609 /* Retrieve BGP packet type. */
610 stream_set_getp (s, BGP_MARKER_SIZE + 2);
611 type = stream_getc (s);
612
613 switch (type)
614 {
615 case BGP_MSG_OPEN:
616 peer->open_out++;
617 break;
618 case BGP_MSG_UPDATE:
619 peer->update_out++;
620 break;
621 case BGP_MSG_NOTIFY:
622 peer->notify_out++;
623 /* Double start timer. */
624 peer->v_start *= 2;
625
626 /* Overflow check. */
627 if (peer->v_start >= (60 * 2))
628 peer->v_start = (60 * 2);
629
630 /* BGP_EVENT_ADD (peer, BGP_Stop); */
631 bgp_stop (peer);
632 peer->status = Idle;
633 bgp_timer_set (peer);
634 return 0;
635 break;
636 case BGP_MSG_KEEPALIVE:
637 peer->keepalive_out++;
638 break;
639 case BGP_MSG_ROUTE_REFRESH_NEW:
640 case BGP_MSG_ROUTE_REFRESH_OLD:
641 peer->refresh_out++;
642 break;
643 case BGP_MSG_CAPABILITY:
644 peer->dynamic_cap_out++;
645 break;
646 }
647
648 /* OK we send packet so delete it. */
649 bgp_packet_delete (peer);
650
651 if (++count >= BGP_WRITE_PACKET_MAX)
652 break;
653 }
654
655 if (bgp_write_proceed (peer))
pauleb821182004-05-01 08:44:08 +0000656 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000657
658 return 0;
659}
660
661/* This is only for sending NOTIFICATION message to neighbor. */
662int
663bgp_write_notify (struct peer *peer)
664{
665 int ret;
666 u_char type;
667 struct stream *s;
668
669 /* There should be at least one packet. */
670 s = stream_fifo_head (peer->obuf);
671 if (!s)
672 return 0;
673 assert (stream_get_endp (s) >= BGP_HEADER_SIZE);
674
675 /* I'm not sure fd is writable. */
pauleb821182004-05-01 08:44:08 +0000676 ret = writen (peer->fd, STREAM_DATA (s), stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000677 if (ret <= 0)
678 {
679 bgp_stop (peer);
680 peer->status = Idle;
681 bgp_timer_set (peer);
682 return 0;
683 }
684
685 /* Retrieve BGP packet type. */
686 stream_set_getp (s, BGP_MARKER_SIZE + 2);
687 type = stream_getc (s);
688
689 assert (type == BGP_MSG_NOTIFY);
690
691 /* Type should be notify. */
692 peer->notify_out++;
693
694 /* Double start timer. */
695 peer->v_start *= 2;
696
697 /* Overflow check. */
698 if (peer->v_start >= (60 * 2))
699 peer->v_start = (60 * 2);
700
701 /* We don't call event manager at here for avoiding other events. */
702 bgp_stop (peer);
703 peer->status = Idle;
704 bgp_timer_set (peer);
705
706 return 0;
707}
708
709/* Make keepalive packet and send it to the peer. */
710void
711bgp_keepalive_send (struct peer *peer)
712{
713 struct stream *s;
714 int length;
715
716 s = stream_new (BGP_MAX_PACKET_SIZE);
717
718 /* Make keepalive packet. */
719 bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE);
720
721 /* Set packet size. */
722 length = bgp_packet_set_size (s);
723
724 /* Dump packet if debug option is set. */
725 /* bgp_packet_dump (s); */
726
727 if (BGP_DEBUG (keepalive, KEEPALIVE))
ajs6b514742004-12-08 21:03:23 +0000728 zlog_debug ("%s sending KEEPALIVE", peer->host);
paul718e3742002-12-13 20:15:29 +0000729 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000730 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000731 peer->host, BGP_MSG_KEEPALIVE, length);
732
733 /* Add packet to the peer. */
734 bgp_packet_add (peer, s);
735
pauleb821182004-05-01 08:44:08 +0000736 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000737}
738
739/* Make open packet and send it to the peer. */
740void
741bgp_open_send (struct peer *peer)
742{
743 struct stream *s;
744 int length;
745 u_int16_t send_holdtime;
746 as_t local_as;
747
748 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
749 send_holdtime = peer->holdtime;
750 else
751 send_holdtime = peer->bgp->default_holdtime;
752
753 /* local-as Change */
754 if (peer->change_local_as)
755 local_as = peer->change_local_as;
756 else
757 local_as = peer->local_as;
758
759 s = stream_new (BGP_MAX_PACKET_SIZE);
760
761 /* Make open packet. */
762 bgp_packet_set_marker (s, BGP_MSG_OPEN);
763
764 /* Set open packet values. */
765 stream_putc (s, BGP_VERSION_4); /* BGP version */
766 stream_putw (s, local_as); /* My Autonomous System*/
767 stream_putw (s, send_holdtime); /* Hold Time */
768 stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */
769
770 /* Set capability code. */
771 bgp_open_capability (s, peer);
772
773 /* Set BGP packet length. */
774 length = bgp_packet_set_size (s);
775
776 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000777 zlog_debug ("%s sending OPEN, version %d, my as %d, holdtime %d, id %s",
paul718e3742002-12-13 20:15:29 +0000778 peer->host, BGP_VERSION_4, local_as,
779 send_holdtime, inet_ntoa (peer->local_id));
780
781 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000782 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000783 peer->host, BGP_MSG_OPEN, length);
784
785 /* Dump packet if debug option is set. */
786 /* bgp_packet_dump (s); */
787
788 /* Add packet to the peer. */
789 bgp_packet_add (peer, s);
790
pauleb821182004-05-01 08:44:08 +0000791 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000792}
793
794/* Send BGP notify packet with data potion. */
795void
796bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code,
797 u_char *data, size_t datalen)
798{
799 struct stream *s;
800 int length;
801
802 /* Allocate new stream. */
803 s = stream_new (BGP_MAX_PACKET_SIZE);
804
805 /* Make nitify packet. */
806 bgp_packet_set_marker (s, BGP_MSG_NOTIFY);
807
808 /* Set notify packet values. */
809 stream_putc (s, code); /* BGP notify code */
810 stream_putc (s, sub_code); /* BGP notify sub_code */
811
812 /* If notify data is present. */
813 if (data)
814 stream_write (s, data, datalen);
815
816 /* Set BGP packet length. */
817 length = bgp_packet_set_size (s);
818
819 /* Add packet to the peer. */
820 stream_fifo_clean (peer->obuf);
821 bgp_packet_add (peer, s);
822
823 /* For debug */
824 {
825 struct bgp_notify bgp_notify;
826 int first = 0;
827 int i;
828 char c[4];
829
830 bgp_notify.code = code;
831 bgp_notify.subcode = sub_code;
832 bgp_notify.data = NULL;
833 bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE;
834
835 if (bgp_notify.length)
836 {
837 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
838 for (i = 0; i < bgp_notify.length; i++)
839 if (first)
840 {
841 sprintf (c, " %02x", data[i]);
842 strcat (bgp_notify.data, c);
843 }
844 else
845 {
846 first = 1;
847 sprintf (c, "%02x", data[i]);
848 strcpy (bgp_notify.data, c);
849 }
850 }
851 bgp_notify_print (peer, &bgp_notify, "sending");
852 if (bgp_notify.data)
853 XFREE (MTYPE_TMP, bgp_notify.data);
854 }
855
856 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000857 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000858 peer->host, BGP_MSG_NOTIFY, length);
859
hassoe0701b72004-05-20 09:19:34 +0000860 /* peer reset cause */
861 if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE)
862 {
863 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
864 peer->last_reset = PEER_DOWN_USER_RESET;
865 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
866 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
867 else
868 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
869 }
870
paul718e3742002-12-13 20:15:29 +0000871 /* Call imidiately. */
872 BGP_WRITE_OFF (peer->t_write);
873
874 bgp_write_notify (peer);
875}
876
877/* Send BGP notify packet. */
878void
879bgp_notify_send (struct peer *peer, u_char code, u_char sub_code)
880{
881 bgp_notify_send_with_data (peer, code, sub_code, NULL, 0);
882}
883
paulfd79ac92004-10-13 05:06:08 +0000884const char *
paul718e3742002-12-13 20:15:29 +0000885afi2str (afi_t afi)
886{
887 if (afi == AFI_IP)
888 return "AFI_IP";
889 else if (afi == AFI_IP6)
890 return "AFI_IP6";
891 else
892 return "Unknown AFI";
893}
894
paulfd79ac92004-10-13 05:06:08 +0000895const char *
paul718e3742002-12-13 20:15:29 +0000896safi2str (safi_t safi)
897{
898 if (safi == SAFI_UNICAST)
899 return "SAFI_UNICAST";
900 else if (safi == SAFI_MULTICAST)
901 return "SAFI_MULTICAST";
902 else if (safi == SAFI_MPLS_VPN || safi == BGP_SAFI_VPNV4)
903 return "SAFI_MPLS_VPN";
904 else
905 return "Unknown SAFI";
906}
907
908/* Send route refresh message to the peer. */
909void
910bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi,
911 u_char orf_type, u_char when_to_refresh, int remove)
912{
913 struct stream *s;
914 struct stream *packet;
915 int length;
916 struct bgp_filter *filter;
917 int orf_refresh = 0;
918
919#ifdef DISABLE_BGP_ANNOUNCE
920 return;
921#endif /* DISABLE_BGP_ANNOUNCE */
922
923 filter = &peer->filter[afi][safi];
924
925 /* Adjust safi code. */
926 if (safi == SAFI_MPLS_VPN)
927 safi = BGP_SAFI_VPNV4;
928
929 s = stream_new (BGP_MAX_PACKET_SIZE);
930
931 /* Make BGP update packet. */
932 if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
933 bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW);
934 else
935 bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD);
936
937 /* Encode Route Refresh message. */
938 stream_putw (s, afi);
939 stream_putc (s, 0);
940 stream_putc (s, safi);
941
942 if (orf_type == ORF_TYPE_PREFIX
943 || orf_type == ORF_TYPE_PREFIX_OLD)
944 if (remove || filter->plist[FILTER_IN].plist)
945 {
946 u_int16_t orf_len;
947 unsigned long orfp;
948
949 orf_refresh = 1;
950 stream_putc (s, when_to_refresh);
951 stream_putc (s, orf_type);
952 orfp = stream_get_putp (s);
953 stream_putw (s, 0);
954
955 if (remove)
956 {
957 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
958 stream_putc (s, ORF_COMMON_PART_REMOVE_ALL);
959 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000960 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +0000961 peer->host, orf_type,
962 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
963 afi, safi);
964 }
965 else
966 {
967 SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
968 prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist,
969 ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT,
970 ORF_COMMON_PART_DENY);
971 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000972 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +0000973 peer->host, orf_type,
974 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
975 afi, safi);
976 }
977
978 /* Total ORF Entry Len. */
979 orf_len = stream_get_putp (s) - orfp - 2;
980 stream_putw_at (s, orfp, orf_len);
981 }
982
983 /* Set packet size. */
984 length = bgp_packet_set_size (s);
985
986 if (BGP_DEBUG (normal, NORMAL))
987 {
988 if (! orf_refresh)
ajs6b514742004-12-08 21:03:23 +0000989 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +0000990 peer->host, afi, safi);
ajs6b514742004-12-08 21:03:23 +0000991 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000992 peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ?
993 BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length);
994 }
995
996 /* Make real packet. */
997 packet = bgp_packet_dup (s);
998 stream_free (s);
999
1000 /* Add packet to the peer. */
1001 bgp_packet_add (peer, packet);
1002
pauleb821182004-05-01 08:44:08 +00001003 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +00001004}
1005
1006/* Send capability message to the peer. */
1007void
1008bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi,
1009 int capability_code, int action)
1010{
1011 struct stream *s;
1012 struct stream *packet;
1013 int length;
1014
1015 /* Adjust safi code. */
1016 if (safi == SAFI_MPLS_VPN)
1017 safi = BGP_SAFI_VPNV4;
1018
1019 s = stream_new (BGP_MAX_PACKET_SIZE);
1020
1021 /* Make BGP update packet. */
1022 bgp_packet_set_marker (s, BGP_MSG_CAPABILITY);
1023
1024 /* Encode MP_EXT capability. */
1025 if (capability_code == CAPABILITY_CODE_MP)
1026 {
1027 stream_putc (s, action);
1028 stream_putc (s, CAPABILITY_CODE_MP);
1029 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1030 stream_putw (s, afi);
1031 stream_putc (s, 0);
1032 stream_putc (s, safi);
1033
1034 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001035 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00001036 peer->host, action == CAPABILITY_ACTION_SET ?
1037 "Advertising" : "Removing", afi, safi);
1038 }
1039
paul718e3742002-12-13 20:15:29 +00001040 /* Set packet size. */
1041 length = bgp_packet_set_size (s);
1042
1043 /* Make real packet. */
1044 packet = bgp_packet_dup (s);
1045 stream_free (s);
1046
1047 /* Add packet to the peer. */
1048 bgp_packet_add (peer, packet);
1049
1050 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001051 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +00001052 peer->host, BGP_MSG_CAPABILITY, length);
1053
pauleb821182004-05-01 08:44:08 +00001054 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +00001055}
1056
1057/* RFC1771 6.8 Connection collision detection. */
1058int
pauleb821182004-05-01 08:44:08 +00001059bgp_collision_detect (struct peer *new, struct in_addr remote_id)
paul718e3742002-12-13 20:15:29 +00001060{
pauleb821182004-05-01 08:44:08 +00001061 struct peer *peer;
paul718e3742002-12-13 20:15:29 +00001062 struct listnode *nn;
1063 struct bgp *bgp;
1064
1065 bgp = bgp_get_default ();
1066 if (! bgp)
1067 return 0;
1068
1069 /* Upon receipt of an OPEN message, the local system must examine
1070 all of its connections that are in the OpenConfirm state. A BGP
1071 speaker may also examine connections in an OpenSent state if it
1072 knows the BGP Identifier of the peer by means outside of the
1073 protocol. If among these connections there is a connection to a
1074 remote BGP speaker whose BGP Identifier equals the one in the
1075 OPEN message, then the local system performs the following
1076 collision resolution procedure: */
1077
1078 LIST_LOOP (bgp->peer, peer, nn)
1079 {
1080 /* Under OpenConfirm status, local peer structure already hold
1081 remote router ID. */
pauleb821182004-05-01 08:44:08 +00001082
1083 if (peer != new
1084 && (peer->status == OpenConfirm || peer->status == OpenSent)
1085 && sockunion_same (&peer->su, &new->su))
1086 {
paul718e3742002-12-13 20:15:29 +00001087 /* 1. The BGP Identifier of the local system is compared to
1088 the BGP Identifier of the remote system (as specified in
1089 the OPEN message). */
1090
1091 if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr))
1092 {
1093 /* 2. If the value of the local BGP Identifier is less
1094 than the remote one, the local system closes BGP
1095 connection that already exists (the one that is
1096 already in the OpenConfirm state), and accepts BGP
1097 connection initiated by the remote system. */
1098
pauleb821182004-05-01 08:44:08 +00001099 if (peer->fd >= 0)
hassoe0701b72004-05-20 09:19:34 +00001100 bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
paul718e3742002-12-13 20:15:29 +00001101 return 1;
1102 }
1103 else
1104 {
1105 /* 3. Otherwise, the local system closes newly created
1106 BGP connection (the one associated with the newly
1107 received OPEN message), and continues to use the
1108 existing one (the one that is already in the
1109 OpenConfirm state). */
1110
pauleb821182004-05-01 08:44:08 +00001111 if (new->fd >= 0)
paulf5ba3872004-07-09 12:11:31 +00001112 bgp_notify_send (new, BGP_NOTIFY_CEASE,
1113 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
paul718e3742002-12-13 20:15:29 +00001114 return -1;
1115 }
pauleb821182004-05-01 08:44:08 +00001116 }
1117 }
paul718e3742002-12-13 20:15:29 +00001118 return 0;
1119}
1120
1121int
1122bgp_open_receive (struct peer *peer, bgp_size_t size)
1123{
1124 int ret;
1125 u_char version;
1126 u_char optlen;
1127 u_int16_t holdtime;
1128 u_int16_t send_holdtime;
1129 as_t remote_as;
1130 struct peer *realpeer;
1131 struct in_addr remote_id;
1132 int capability;
paul5228ad22004-06-04 17:58:18 +00001133 u_int8_t notify_data_remote_as[2];
1134 u_int8_t notify_data_remote_id[4];
paul718e3742002-12-13 20:15:29 +00001135
1136 realpeer = NULL;
1137
1138 /* Parse open packet. */
1139 version = stream_getc (peer->ibuf);
1140 memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2);
1141 remote_as = stream_getw (peer->ibuf);
1142 holdtime = stream_getw (peer->ibuf);
1143 memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
1144 remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
1145
1146 /* Receive OPEN message log */
1147 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001148 zlog_debug ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s",
paul718e3742002-12-13 20:15:29 +00001149 peer->host, version, remote_as, holdtime,
1150 inet_ntoa (remote_id));
1151
1152 /* Lookup peer from Open packet. */
1153 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1154 {
1155 int as = 0;
1156
1157 realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as);
1158
1159 if (! realpeer)
1160 {
1161 /* Peer's source IP address is check in bgp_accept(), so this
1162 must be AS number mismatch or remote-id configuration
1163 mismatch. */
1164 if (as)
1165 {
1166 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001167 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1168 peer->host, inet_ntoa (remote_id));
1169 bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1170 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1171 notify_data_remote_id, 4);
paul718e3742002-12-13 20:15:29 +00001172 }
1173 else
1174 {
1175 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001176 zlog_debug ("%s bad OPEN, remote AS is %d, expected %d",
1177 peer->host, remote_as, peer->as);
1178 bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1179 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1180 notify_data_remote_as, 2);
paul718e3742002-12-13 20:15:29 +00001181 }
1182 return -1;
1183 }
1184 }
1185
1186 /* When collision is detected and this peer is closed. Retrun
1187 immidiately. */
1188 ret = bgp_collision_detect (peer, remote_id);
1189 if (ret < 0)
1190 return ret;
1191
pauleb821182004-05-01 08:44:08 +00001192 /* Hack part. */
1193 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1194 {
1195 if (ret == 0 && realpeer->status != Active
1196 && realpeer->status != OpenSent
1197 && realpeer->status != OpenConfirm)
1198 {
1199 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00001200 zlog_debug ("%s [Event] peer's status is %s close connection",
pauleb821182004-05-01 08:44:08 +00001201 realpeer->host, LOOKUP (bgp_status_msg, peer->status));
1202 return -1;
1203 }
1204
1205 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00001206 zlog_debug ("%s [Event] Transfer temporary BGP peer to existing one",
pauleb821182004-05-01 08:44:08 +00001207 peer->host);
1208
1209 bgp_stop (realpeer);
1210
1211 /* Transfer file descriptor. */
1212 realpeer->fd = peer->fd;
1213 peer->fd = -1;
1214
1215 /* Transfer input buffer. */
1216 stream_free (realpeer->ibuf);
1217 realpeer->ibuf = peer->ibuf;
1218 realpeer->packet_size = peer->packet_size;
1219 peer->ibuf = NULL;
1220
1221 /* Transfer status. */
1222 realpeer->status = peer->status;
1223 bgp_stop (peer);
1224
1225 /* peer pointer change. Open packet send to neighbor. */
1226 peer = realpeer;
1227 bgp_open_send (peer);
1228 if (peer->fd < 0)
1229 {
1230 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1231 peer->fd);
1232 return -1;
1233 }
1234 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
1235 }
1236
paul718e3742002-12-13 20:15:29 +00001237 /* remote router-id check. */
1238 if (remote_id.s_addr == 0
1239 || ntohl (remote_id.s_addr) >= 0xe0000000
1240 || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
1241 {
1242 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001243 zlog_debug ("%s bad OPEN, wrong router identifier %s",
paul718e3742002-12-13 20:15:29 +00001244 peer->host, inet_ntoa (remote_id));
1245 bgp_notify_send_with_data (peer,
1246 BGP_NOTIFY_OPEN_ERR,
1247 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1248 notify_data_remote_id, 4);
1249 return -1;
1250 }
1251
1252 /* Set remote router-id */
1253 peer->remote_id = remote_id;
1254
1255 /* Peer BGP version check. */
1256 if (version != BGP_VERSION_4)
1257 {
paul5228ad22004-06-04 17:58:18 +00001258 u_int8_t maxver = BGP_VERSION_4;
paul718e3742002-12-13 20:15:29 +00001259 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001260 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
paul718e3742002-12-13 20:15:29 +00001261 peer->host, version, BGP_VERSION_4);
1262 bgp_notify_send_with_data (peer,
1263 BGP_NOTIFY_OPEN_ERR,
1264 BGP_NOTIFY_OPEN_UNSUP_VERSION,
paul5228ad22004-06-04 17:58:18 +00001265 &maxver, 1);
paul718e3742002-12-13 20:15:29 +00001266 return -1;
1267 }
1268
1269 /* Check neighbor as number. */
1270 if (remote_as != peer->as)
1271 {
1272 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001273 zlog_debug ("%s bad OPEN, remote AS is %d, expected %d",
paul718e3742002-12-13 20:15:29 +00001274 peer->host, remote_as, peer->as);
1275 bgp_notify_send_with_data (peer,
1276 BGP_NOTIFY_OPEN_ERR,
1277 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1278 notify_data_remote_as, 2);
1279 return -1;
1280 }
1281
1282 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1283 calculate the value of the Hold Timer by using the smaller of its
1284 configured Hold Time and the Hold Time received in the OPEN message.
1285 The Hold Time MUST be either zero or at least three seconds. An
1286 implementation may reject connections on the basis of the Hold Time. */
1287
1288 if (holdtime < 3 && holdtime != 0)
1289 {
1290 bgp_notify_send (peer,
1291 BGP_NOTIFY_OPEN_ERR,
1292 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
1293 return -1;
1294 }
1295
1296 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1297 would be one third of the Hold Time interval. KEEPALIVE messages
1298 MUST NOT be sent more frequently than one per second. An
1299 implementation MAY adjust the rate at which it sends KEEPALIVE
1300 messages as a function of the Hold Time interval. */
1301
1302 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
1303 send_holdtime = peer->holdtime;
1304 else
1305 send_holdtime = peer->bgp->default_holdtime;
1306
1307 if (holdtime < send_holdtime)
1308 peer->v_holdtime = holdtime;
1309 else
1310 peer->v_holdtime = send_holdtime;
1311
1312 peer->v_keepalive = peer->v_holdtime / 3;
1313
1314 /* Open option part parse. */
1315 capability = 0;
1316 optlen = stream_getc (peer->ibuf);
1317 if (optlen != 0)
1318 {
1319 ret = bgp_open_option_parse (peer, optlen, &capability);
1320 if (ret < 0)
1321 return ret;
1322
1323 stream_forward (peer->ibuf, optlen);
1324 }
1325 else
1326 {
1327 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001328 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
paul718e3742002-12-13 20:15:29 +00001329 peer->host);
1330 }
1331
1332 /* Override capability. */
1333 if (! capability || CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1334 {
1335 peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
1336 peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
1337 peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
1338 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
1339 }
1340
1341 /* Get sockname. */
1342 bgp_getsockname (peer);
1343
1344 BGP_EVENT_ADD (peer, Receive_OPEN_message);
1345
1346 peer->packet_size = 0;
1347 if (peer->ibuf)
1348 stream_reset (peer->ibuf);
1349
1350 return 0;
1351}
1352
1353/* Parse BGP Update packet and make attribute object. */
1354int
1355bgp_update_receive (struct peer *peer, bgp_size_t size)
1356{
1357 int ret;
1358 u_char *end;
1359 struct stream *s;
1360 struct attr attr;
1361 bgp_size_t attribute_len;
1362 bgp_size_t update_len;
1363 bgp_size_t withdraw_len;
1364 struct bgp_nlri update;
1365 struct bgp_nlri withdraw;
1366 struct bgp_nlri mp_update;
1367 struct bgp_nlri mp_withdraw;
paule01f9cb2004-07-09 17:48:53 +00001368 char attrstr[BUFSIZ] = "";
paul718e3742002-12-13 20:15:29 +00001369
1370 /* Status must be Established. */
1371 if (peer->status != Established)
1372 {
1373 zlog_err ("%s [FSM] Update packet received under status %s",
1374 peer->host, LOOKUP (bgp_status_msg, peer->status));
1375 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1376 return -1;
1377 }
1378
1379 /* Set initial values. */
1380 memset (&attr, 0, sizeof (struct attr));
1381 memset (&update, 0, sizeof (struct bgp_nlri));
1382 memset (&withdraw, 0, sizeof (struct bgp_nlri));
1383 memset (&mp_update, 0, sizeof (struct bgp_nlri));
1384 memset (&mp_withdraw, 0, sizeof (struct bgp_nlri));
1385
1386 s = peer->ibuf;
1387 end = stream_pnt (s) + size;
1388
1389 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1390 Length is too large (i.e., if Unfeasible Routes Length + Total
1391 Attribute Length + 23 exceeds the message Length), then the Error
1392 Subcode is set to Malformed Attribute List. */
1393 if (stream_pnt (s) + 2 > end)
1394 {
1395 zlog_err ("%s [Error] Update packet error"
1396 " (packet length is short for unfeasible length)",
1397 peer->host);
1398 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1399 BGP_NOTIFY_UPDATE_MAL_ATTR);
1400 return -1;
1401 }
1402
1403 /* Unfeasible Route Length. */
1404 withdraw_len = stream_getw (s);
1405
1406 /* Unfeasible Route Length check. */
1407 if (stream_pnt (s) + withdraw_len > end)
1408 {
1409 zlog_err ("%s [Error] Update packet error"
1410 " (packet unfeasible length overflow %d)",
1411 peer->host, withdraw_len);
1412 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1413 BGP_NOTIFY_UPDATE_MAL_ATTR);
1414 return -1;
1415 }
1416
1417 /* Unfeasible Route packet format check. */
1418 if (withdraw_len > 0)
1419 {
1420 ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len);
1421 if (ret < 0)
1422 return -1;
1423
1424 if (BGP_DEBUG (packet, PACKET_RECV))
ajs6b514742004-12-08 21:03:23 +00001425 zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer->host);
paul718e3742002-12-13 20:15:29 +00001426
1427 withdraw.afi = AFI_IP;
1428 withdraw.safi = SAFI_UNICAST;
1429 withdraw.nlri = stream_pnt (s);
1430 withdraw.length = withdraw_len;
1431 stream_forward (s, withdraw_len);
1432 }
1433
1434 /* Attribute total length check. */
1435 if (stream_pnt (s) + 2 > end)
1436 {
1437 zlog_warn ("%s [Error] Packet Error"
1438 " (update packet is short for attribute length)",
1439 peer->host);
1440 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1441 BGP_NOTIFY_UPDATE_MAL_ATTR);
1442 return -1;
1443 }
1444
1445 /* Fetch attribute total length. */
1446 attribute_len = stream_getw (s);
1447
1448 /* Attribute length check. */
1449 if (stream_pnt (s) + attribute_len > end)
1450 {
1451 zlog_warn ("%s [Error] Packet Error"
1452 " (update packet attribute length overflow %d)",
1453 peer->host, attribute_len);
1454 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1455 BGP_NOTIFY_UPDATE_MAL_ATTR);
1456 return -1;
1457 }
1458
1459 /* Parse attribute when it exists. */
1460 if (attribute_len)
1461 {
1462 ret = bgp_attr_parse (peer, &attr, attribute_len,
1463 &mp_update, &mp_withdraw);
1464 if (ret < 0)
1465 return -1;
1466 }
1467
1468 /* Logging the attribute. */
1469 if (BGP_DEBUG (update, UPDATE_IN))
1470 {
paule01f9cb2004-07-09 17:48:53 +00001471 ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
1472
1473 if (ret)
ajs6b514742004-12-08 21:03:23 +00001474 zlog (peer->log, LOG_DEBUG, "%s rcvd UPDATE w/ attr: %s",
paule01f9cb2004-07-09 17:48:53 +00001475 peer->host, attrstr);
paul718e3742002-12-13 20:15:29 +00001476 }
1477
1478 /* Network Layer Reachability Information. */
1479 update_len = end - stream_pnt (s);
1480
1481 if (update_len)
1482 {
1483 /* Check NLRI packet format and prefix length. */
1484 ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len);
1485 if (ret < 0)
1486 return -1;
1487
1488 /* Set NLRI portion to structure. */
1489 update.afi = AFI_IP;
1490 update.safi = SAFI_UNICAST;
1491 update.nlri = stream_pnt (s);
1492 update.length = update_len;
1493 stream_forward (s, update_len);
1494 }
1495
1496 /* NLRI is processed only when the peer is configured specific
1497 Address Family and Subsequent Address Family. */
1498 if (peer->afc[AFI_IP][SAFI_UNICAST])
1499 {
1500 if (withdraw.length)
1501 bgp_nlri_parse (peer, NULL, &withdraw);
1502
1503 if (update.length)
1504 {
1505 /* We check well-known attribute only for IPv4 unicast
1506 update. */
1507 ret = bgp_attr_check (peer, &attr);
1508 if (ret < 0)
1509 return -1;
1510
1511 bgp_nlri_parse (peer, &attr, &update);
1512 }
paule01f9cb2004-07-09 17:48:53 +00001513
hassof4184462005-02-01 20:13:16 +00001514 if (mp_update.length
1515 && mp_update.afi == AFI_IP
1516 && mp_update.safi == SAFI_UNICAST)
1517 bgp_nlri_parse (peer, &attr, &mp_update);
1518
1519 if (mp_withdraw.length
1520 && mp_withdraw.afi == AFI_IP
1521 && mp_withdraw.safi == SAFI_UNICAST)
1522 bgp_nlri_parse (peer, NULL, &mp_withdraw);
1523
paule01f9cb2004-07-09 17:48:53 +00001524 if (! attribute_len && ! withdraw_len)
1525 {
1526 /* End-of-RIB received */
1527
1528 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001529 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Unicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001530 peer->host);
1531 }
paul718e3742002-12-13 20:15:29 +00001532 }
1533 if (peer->afc[AFI_IP][SAFI_MULTICAST])
1534 {
1535 if (mp_update.length
1536 && mp_update.afi == AFI_IP
1537 && mp_update.safi == SAFI_MULTICAST)
1538 bgp_nlri_parse (peer, &attr, &mp_update);
1539
1540 if (mp_withdraw.length
1541 && mp_withdraw.afi == AFI_IP
1542 && mp_withdraw.safi == SAFI_MULTICAST)
1543 bgp_nlri_parse (peer, NULL, &mp_withdraw);
paule01f9cb2004-07-09 17:48:53 +00001544
1545 if (attribute_len == 6 && ! withdraw_len
1546 && mp_withdraw.afi == AFI_IP
1547 && mp_withdraw.safi == SAFI_MULTICAST
1548 && mp_withdraw.length == 0)
1549 {
1550 /* End-of-RIB received */
1551
1552 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001553 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Multicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001554 peer->host);
1555 }
paul718e3742002-12-13 20:15:29 +00001556 }
1557 if (peer->afc[AFI_IP6][SAFI_UNICAST])
1558 {
1559 if (mp_update.length
1560 && mp_update.afi == AFI_IP6
1561 && mp_update.safi == SAFI_UNICAST)
1562 bgp_nlri_parse (peer, &attr, &mp_update);
1563
1564 if (mp_withdraw.length
1565 && mp_withdraw.afi == AFI_IP6
1566 && mp_withdraw.safi == SAFI_UNICAST)
1567 bgp_nlri_parse (peer, NULL, &mp_withdraw);
paule01f9cb2004-07-09 17:48:53 +00001568
1569 if (attribute_len == 6 && ! withdraw_len
1570 && mp_withdraw.afi == AFI_IP6
1571 && mp_withdraw.safi == SAFI_UNICAST
1572 && mp_withdraw.length == 0)
1573 {
1574 /* End-of-RIB received */
1575
1576 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001577 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Unicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001578 peer->host);
1579 }
paul718e3742002-12-13 20:15:29 +00001580 }
1581 if (peer->afc[AFI_IP6][SAFI_MULTICAST])
1582 {
1583 if (mp_update.length
1584 && mp_update.afi == AFI_IP6
1585 && mp_update.safi == SAFI_MULTICAST)
1586 bgp_nlri_parse (peer, &attr, &mp_update);
1587
1588 if (mp_withdraw.length
1589 && mp_withdraw.afi == AFI_IP6
1590 && mp_withdraw.safi == SAFI_MULTICAST)
1591 bgp_nlri_parse (peer, NULL, &mp_withdraw);
paule01f9cb2004-07-09 17:48:53 +00001592
1593 if (attribute_len == 6 && ! withdraw_len
1594 && mp_withdraw.afi == AFI_IP6
1595 && mp_withdraw.safi == SAFI_MULTICAST
1596 && mp_withdraw.length == 0)
1597 {
1598 /* End-of-RIB received */
1599
1600 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001601 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Multicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001602 peer->host);
1603 }
paul718e3742002-12-13 20:15:29 +00001604 }
1605 if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
1606 {
1607 if (mp_update.length
1608 && mp_update.afi == AFI_IP
1609 && mp_update.safi == BGP_SAFI_VPNV4)
1610 bgp_nlri_parse_vpnv4 (peer, &attr, &mp_update);
1611
1612 if (mp_withdraw.length
1613 && mp_withdraw.afi == AFI_IP
1614 && mp_withdraw.safi == BGP_SAFI_VPNV4)
1615 bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw);
paule01f9cb2004-07-09 17:48:53 +00001616
1617 if (attribute_len == 6 && ! withdraw_len
1618 && mp_withdraw.afi == AFI_IP
1619 && mp_withdraw.safi == BGP_SAFI_VPNV4
1620 && mp_withdraw.length == 0)
1621 {
1622 /* End-of-RIB received */
1623
1624 if (BGP_DEBUG (update, UPDATE_IN))
ajs6b514742004-12-08 21:03:23 +00001625 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for VPNv4 Unicast from %s",
paule01f9cb2004-07-09 17:48:53 +00001626 peer->host);
1627 }
paul718e3742002-12-13 20:15:29 +00001628 }
1629
1630 /* Everything is done. We unintern temporary structures which
1631 interned in bgp_attr_parse(). */
1632 if (attr.aspath)
1633 aspath_unintern (attr.aspath);
1634 if (attr.community)
1635 community_unintern (attr.community);
1636 if (attr.ecommunity)
1637 ecommunity_unintern (attr.ecommunity);
1638 if (attr.cluster)
1639 cluster_unintern (attr.cluster);
1640 if (attr.transit)
1641 transit_unintern (attr.transit);
1642
1643 /* If peering is stopped due to some reason, do not generate BGP
1644 event. */
1645 if (peer->status != Established)
1646 return 0;
1647
1648 /* Increment packet counter. */
1649 peer->update_in++;
1650 peer->update_time = time (NULL);
1651
1652 /* Generate BGP event. */
1653 BGP_EVENT_ADD (peer, Receive_UPDATE_message);
1654
1655 return 0;
1656}
1657
1658/* Notify message treatment function. */
1659void
1660bgp_notify_receive (struct peer *peer, bgp_size_t size)
1661{
1662 struct bgp_notify bgp_notify;
1663
1664 if (peer->notify.data)
1665 {
1666 XFREE (MTYPE_TMP, peer->notify.data);
1667 peer->notify.data = NULL;
1668 peer->notify.length = 0;
1669 }
1670
1671 bgp_notify.code = stream_getc (peer->ibuf);
1672 bgp_notify.subcode = stream_getc (peer->ibuf);
1673 bgp_notify.length = size - 2;
1674 bgp_notify.data = NULL;
1675
1676 /* Preserv notify code and sub code. */
1677 peer->notify.code = bgp_notify.code;
1678 peer->notify.subcode = bgp_notify.subcode;
1679 /* For further diagnostic record returned Data. */
1680 if (bgp_notify.length)
1681 {
1682 peer->notify.length = size - 2;
1683 peer->notify.data = XMALLOC (MTYPE_TMP, size - 2);
1684 memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2);
1685 }
1686
1687 /* For debug */
1688 {
1689 int i;
1690 int first = 0;
1691 char c[4];
1692
1693 if (bgp_notify.length)
1694 {
1695 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
1696 for (i = 0; i < bgp_notify.length; i++)
1697 if (first)
1698 {
1699 sprintf (c, " %02x", stream_getc (peer->ibuf));
1700 strcat (bgp_notify.data, c);
1701 }
1702 else
1703 {
1704 first = 1;
1705 sprintf (c, "%02x", stream_getc (peer->ibuf));
1706 strcpy (bgp_notify.data, c);
1707 }
1708 }
1709
1710 bgp_notify_print(peer, &bgp_notify, "received");
1711 if (bgp_notify.data)
1712 XFREE (MTYPE_TMP, bgp_notify.data);
1713 }
1714
1715 /* peer count update */
1716 peer->notify_in++;
1717
hassoe0701b72004-05-20 09:19:34 +00001718 if (peer->status == Established)
1719 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
1720
paul718e3742002-12-13 20:15:29 +00001721 /* We have to check for Notify with Unsupported Optional Parameter.
1722 in that case we fallback to open without the capability option.
1723 But this done in bgp_stop. We just mark it here to avoid changing
1724 the fsm tables. */
1725 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
1726 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM )
1727 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1728
1729 /* Also apply to Unsupported Capability until remote router support
1730 capability. */
1731 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
1732 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_CAPBL)
1733 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1734
1735 BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message);
1736}
1737
1738/* Keepalive treatment function -- get keepalive send keepalive */
1739void
1740bgp_keepalive_receive (struct peer *peer, bgp_size_t size)
1741{
1742 if (BGP_DEBUG (keepalive, KEEPALIVE))
ajs6b514742004-12-08 21:03:23 +00001743 zlog_debug ("%s KEEPALIVE rcvd", peer->host);
paul718e3742002-12-13 20:15:29 +00001744
1745 BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message);
1746}
1747
1748/* Route refresh message is received. */
1749void
1750bgp_route_refresh_receive (struct peer *peer, bgp_size_t size)
1751{
1752 afi_t afi;
1753 safi_t safi;
1754 u_char reserved;
1755 struct stream *s;
1756
1757 /* If peer does not have the capability, send notification. */
1758 if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV))
1759 {
1760 plog_err (peer->log, "%s [Error] BGP route refresh is not enabled",
1761 peer->host);
1762 bgp_notify_send (peer,
1763 BGP_NOTIFY_HEADER_ERR,
1764 BGP_NOTIFY_HEADER_BAD_MESTYPE);
1765 return;
1766 }
1767
1768 /* Status must be Established. */
1769 if (peer->status != Established)
1770 {
1771 plog_err (peer->log,
1772 "%s [Error] Route refresh packet received under status %s",
1773 peer->host, LOOKUP (bgp_status_msg, peer->status));
1774 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1775 return;
1776 }
1777
1778 s = peer->ibuf;
1779
1780 /* Parse packet. */
1781 afi = stream_getw (s);
1782 reserved = stream_getc (s);
1783 safi = stream_getc (s);
1784
1785 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001786 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00001787 peer->host, afi, safi);
1788
1789 /* Check AFI and SAFI. */
1790 if ((afi != AFI_IP && afi != AFI_IP6)
1791 || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
1792 && safi != BGP_SAFI_VPNV4))
1793 {
1794 if (BGP_DEBUG (normal, NORMAL))
1795 {
ajs6b514742004-12-08 21:03:23 +00001796 zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
paul718e3742002-12-13 20:15:29 +00001797 peer->host, afi, safi);
1798 }
1799 return;
1800 }
1801
1802 /* Adjust safi code. */
1803 if (safi == BGP_SAFI_VPNV4)
1804 safi = SAFI_MPLS_VPN;
1805
1806 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
1807 {
1808 u_char *end;
1809 u_char when_to_refresh;
1810 u_char orf_type;
1811 u_int16_t orf_len;
1812
1813 if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5)
1814 {
1815 zlog_info ("%s ORF route refresh length error", peer->host);
1816 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1817 return;
1818 }
1819
1820 when_to_refresh = stream_getc (s);
1821 end = stream_pnt (s) + (size - 5);
1822
1823 while (stream_pnt (s) < end)
1824 {
1825 orf_type = stream_getc (s);
1826 orf_len = stream_getw (s);
1827
1828 if (orf_type == ORF_TYPE_PREFIX
1829 || orf_type == ORF_TYPE_PREFIX_OLD)
1830 {
1831 u_char *p_pnt = stream_pnt (s);
1832 u_char *p_end = stream_pnt (s) + orf_len;
1833 struct orf_prefix orfp;
1834 u_char common = 0;
1835 u_int32_t seq;
1836 int psize;
1837 char name[BUFSIZ];
1838 char buf[BUFSIZ];
1839 int ret;
1840
1841 if (BGP_DEBUG (normal, NORMAL))
1842 {
ajs6b514742004-12-08 21:03:23 +00001843 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
paul718e3742002-12-13 20:15:29 +00001844 peer->host, orf_type, orf_len);
1845 }
1846
1847 /* ORF prefix-list name */
1848 sprintf (name, "%s.%d.%d", peer->host, afi, safi);
1849
1850 while (p_pnt < p_end)
1851 {
1852 memset (&orfp, 0, sizeof (struct orf_prefix));
1853 common = *p_pnt++;
1854 if (common & ORF_COMMON_PART_REMOVE_ALL)
1855 {
1856 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001857 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host);
paul718e3742002-12-13 20:15:29 +00001858 prefix_bgp_orf_remove_all (name);
1859 break;
1860 }
1861 memcpy (&seq, p_pnt, sizeof (u_int32_t));
1862 p_pnt += sizeof (u_int32_t);
1863 orfp.seq = ntohl (seq);
1864 orfp.ge = *p_pnt++;
1865 orfp.le = *p_pnt++;
1866 orfp.p.prefixlen = *p_pnt++;
1867 orfp.p.family = afi2family (afi);
1868 psize = PSIZE (orfp.p.prefixlen);
1869 memcpy (&orfp.p.u.prefix, p_pnt, psize);
1870 p_pnt += psize;
1871
1872 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001873 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d",
paul718e3742002-12-13 20:15:29 +00001874 peer->host,
1875 (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
1876 (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
1877 orfp.seq,
1878 inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, BUFSIZ),
1879 orfp.p.prefixlen, orfp.ge, orfp.le);
1880
1881 ret = prefix_bgp_orf_set (name, afi, &orfp,
1882 (common & ORF_COMMON_PART_DENY ? 0 : 1 ),
1883 (common & ORF_COMMON_PART_REMOVE ? 0 : 1));
1884
1885 if (ret != CMD_SUCCESS)
1886 {
1887 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001888 zlog_debug ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer->host);
paul718e3742002-12-13 20:15:29 +00001889 prefix_bgp_orf_remove_all (name);
1890 break;
1891 }
1892 }
1893 peer->orf_plist[afi][safi] =
1894 prefix_list_lookup (AFI_ORF_PREFIX, name);
1895 }
1896 stream_forward (s, orf_len);
1897 }
1898 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001899 zlog_debug ("%s rcvd Refresh %s ORF request", peer->host,
paul718e3742002-12-13 20:15:29 +00001900 when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate");
1901 if (when_to_refresh == REFRESH_DEFER)
1902 return;
1903 }
1904
1905 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1906 if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
1907 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
1908
1909 /* Perform route refreshment to the peer */
1910 bgp_announce_route (peer, afi, safi);
1911}
1912
1913int
1914bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length)
1915{
1916 u_char *end;
1917 struct capability cap;
1918 u_char action;
1919 struct bgp *bgp;
1920 afi_t afi;
1921 safi_t safi;
1922
1923 bgp = peer->bgp;
1924 end = pnt + length;
1925
1926 while (pnt < end)
1927 {
1928 /* We need at least action, capability code and capability length. */
1929 if (pnt + 3 > end)
1930 {
1931 zlog_info ("%s Capability length error", peer->host);
1932 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1933 return -1;
1934 }
1935
1936 action = *pnt;
1937
1938 /* Fetch structure to the byte stream. */
1939 memcpy (&cap, pnt + 1, sizeof (struct capability));
1940
1941 /* Action value check. */
1942 if (action != CAPABILITY_ACTION_SET
1943 && action != CAPABILITY_ACTION_UNSET)
1944 {
1945 zlog_info ("%s Capability Action Value error %d",
1946 peer->host, action);
1947 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1948 return -1;
1949 }
1950
1951 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001952 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
paul718e3742002-12-13 20:15:29 +00001953 peer->host, action, cap.code, cap.length);
1954
1955 /* Capability length check. */
1956 if (pnt + (cap.length + 3) > end)
1957 {
1958 zlog_info ("%s Capability length error", peer->host);
1959 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
1960 return -1;
1961 }
1962
1963 /* We know MP Capability Code. */
1964 if (cap.code == CAPABILITY_CODE_MP)
1965 {
1966 afi = ntohs (cap.mpc.afi);
1967 safi = cap.mpc.safi;
1968
1969 /* Ignore capability when override-capability is set. */
1970 if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
1971 continue;
1972
1973 /* Address family check. */
1974 if ((afi == AFI_IP
1975 || afi == AFI_IP6)
1976 && (safi == SAFI_UNICAST
1977 || safi == SAFI_MULTICAST
1978 || safi == BGP_SAFI_VPNV4))
1979 {
1980 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001981 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
paul718e3742002-12-13 20:15:29 +00001982 peer->host,
1983 action == CAPABILITY_ACTION_SET
1984 ? "Advertising" : "Removing",
1985 ntohs(cap.mpc.afi) , cap.mpc.safi);
1986
1987 /* Adjust safi code. */
1988 if (safi == BGP_SAFI_VPNV4)
1989 safi = SAFI_MPLS_VPN;
1990
1991 if (action == CAPABILITY_ACTION_SET)
1992 {
1993 peer->afc_recv[afi][safi] = 1;
1994 if (peer->afc[afi][safi])
1995 {
1996 peer->afc_nego[afi][safi] = 1;
1997 bgp_announce_route (peer, afi, safi);
1998 }
1999 }
2000 else
2001 {
2002 peer->afc_recv[afi][safi] = 0;
2003 peer->afc_nego[afi][safi] = 0;
2004
2005 if (peer_active_nego (peer))
2006 bgp_clear_route (peer, afi, safi);
2007 else
2008 BGP_EVENT_ADD (peer, BGP_Stop);
2009 }
2010 }
2011 }
paul718e3742002-12-13 20:15:29 +00002012 else
2013 {
2014 zlog_warn ("%s unrecognized capability code: %d - ignored",
2015 peer->host, cap.code);
2016 }
2017 pnt += cap.length + 3;
2018 }
2019 return 0;
2020}
2021
2022/* Dynamic Capability is received. */
2023void
2024bgp_capability_receive (struct peer *peer, bgp_size_t size)
2025{
2026 u_char *pnt;
2027 int ret;
2028
2029 /* Fetch pointer. */
2030 pnt = stream_pnt (peer->ibuf);
2031
2032 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002033 zlog_debug ("%s rcv CAPABILITY", peer->host);
paul718e3742002-12-13 20:15:29 +00002034
2035 /* If peer does not have the capability, send notification. */
2036 if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV))
2037 {
2038 plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled",
2039 peer->host);
2040 bgp_notify_send (peer,
2041 BGP_NOTIFY_HEADER_ERR,
2042 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2043 return;
2044 }
2045
2046 /* Status must be Established. */
2047 if (peer->status != Established)
2048 {
2049 plog_err (peer->log,
2050 "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status));
2051 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
2052 return;
2053 }
2054
2055 /* Parse packet. */
2056 ret = bgp_capability_msg_parse (peer, pnt, size);
2057}
2058
2059/* BGP read utility function. */
2060int
2061bgp_read_packet (struct peer *peer)
2062{
2063 int nbytes;
2064 int readsize;
2065
2066 readsize = peer->packet_size - peer->ibuf->putp;
2067
2068 /* If size is zero then return. */
2069 if (! readsize)
2070 return 0;
2071
2072 /* Read packet from fd. */
pauleb821182004-05-01 08:44:08 +00002073 nbytes = stream_read_unblock (peer->ibuf, peer->fd, readsize);
paul718e3742002-12-13 20:15:29 +00002074
2075 /* If read byte is smaller than zero then error occured. */
2076 if (nbytes < 0)
2077 {
2078 if (errno == EAGAIN)
2079 return -1;
2080
2081 plog_err (peer->log, "%s [Error] bgp_read_packet error: %s",
ajs6099b3b2004-11-20 02:06:59 +00002082 peer->host, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00002083 BGP_EVENT_ADD (peer, TCP_fatal_error);
2084 return -1;
2085 }
2086
2087 /* When read byte is zero : clear bgp peer and return */
2088 if (nbytes == 0)
2089 {
2090 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00002091 plog_debug (peer->log, "%s [Event] BGP connection closed fd %d",
pauleb821182004-05-01 08:44:08 +00002092 peer->host, peer->fd);
hassoe0701b72004-05-20 09:19:34 +00002093
2094 if (peer->status == Established)
2095 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2096
paul718e3742002-12-13 20:15:29 +00002097 BGP_EVENT_ADD (peer, TCP_connection_closed);
2098 return -1;
2099 }
2100
2101 /* We read partial packet. */
2102 if (peer->ibuf->putp != peer->packet_size)
2103 return -1;
2104
2105 return 0;
2106}
2107
2108/* Marker check. */
2109int
2110bgp_marker_all_one (struct stream *s, int length)
2111{
2112 int i;
2113
2114 for (i = 0; i < length; i++)
2115 if (s->data[i] != 0xff)
2116 return 0;
2117
2118 return 1;
2119}
2120
2121/* Starting point of packet process function. */
2122int
2123bgp_read (struct thread *thread)
2124{
2125 int ret;
2126 u_char type = 0;
2127 struct peer *peer;
2128 bgp_size_t size;
2129 char notify_data_length[2];
2130
2131 /* Yes first of all get peer pointer. */
2132 peer = THREAD_ARG (thread);
2133 peer->t_read = NULL;
2134
2135 /* For non-blocking IO check. */
2136 if (peer->status == Connect)
2137 {
2138 bgp_connect_check (peer);
2139 goto done;
2140 }
2141 else
2142 {
pauleb821182004-05-01 08:44:08 +00002143 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +00002144 {
pauleb821182004-05-01 08:44:08 +00002145 zlog_err ("bgp_read peer's fd is negative value %d", peer->fd);
paul718e3742002-12-13 20:15:29 +00002146 return -1;
2147 }
pauleb821182004-05-01 08:44:08 +00002148 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
paul718e3742002-12-13 20:15:29 +00002149 }
2150
2151 /* Read packet header to determine type of the packet */
2152 if (peer->packet_size == 0)
2153 peer->packet_size = BGP_HEADER_SIZE;
2154
2155 if (peer->ibuf->putp < BGP_HEADER_SIZE)
2156 {
2157 ret = bgp_read_packet (peer);
2158
2159 /* Header read error or partial read packet. */
2160 if (ret < 0)
2161 goto done;
2162
2163 /* Get size and type. */
2164 stream_forward (peer->ibuf, BGP_MARKER_SIZE);
2165 memcpy (notify_data_length, stream_pnt (peer->ibuf), 2);
2166 size = stream_getw (peer->ibuf);
2167 type = stream_getc (peer->ibuf);
2168
2169 if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0)
ajs6b514742004-12-08 21:03:23 +00002170 zlog_debug ("%s rcv message type %d, length (excl. header) %d",
paul718e3742002-12-13 20:15:29 +00002171 peer->host, type, size - BGP_HEADER_SIZE);
2172
2173 /* Marker check */
paulf5ba3872004-07-09 12:11:31 +00002174 if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE))
paul718e3742002-12-13 20:15:29 +00002175 && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE))
2176 {
2177 bgp_notify_send (peer,
2178 BGP_NOTIFY_HEADER_ERR,
2179 BGP_NOTIFY_HEADER_NOT_SYNC);
2180 goto done;
2181 }
2182
2183 /* BGP type check. */
2184 if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
2185 && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
2186 && type != BGP_MSG_ROUTE_REFRESH_NEW
2187 && type != BGP_MSG_ROUTE_REFRESH_OLD
2188 && type != BGP_MSG_CAPABILITY)
2189 {
2190 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002191 plog_debug (peer->log,
paul718e3742002-12-13 20:15:29 +00002192 "%s unknown message type 0x%02x",
2193 peer->host, type);
2194 bgp_notify_send_with_data (peer,
2195 BGP_NOTIFY_HEADER_ERR,
2196 BGP_NOTIFY_HEADER_BAD_MESTYPE,
2197 &type, 1);
2198 goto done;
2199 }
2200 /* Mimimum packet length check. */
2201 if ((size < BGP_HEADER_SIZE)
2202 || (size > BGP_MAX_PACKET_SIZE)
2203 || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
2204 || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE)
2205 || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE)
2206 || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
2207 || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2208 || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2209 || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE))
2210 {
2211 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002212 plog_debug (peer->log,
paul718e3742002-12-13 20:15:29 +00002213 "%s bad message length - %d for %s",
2214 peer->host, size,
2215 type == 128 ? "ROUTE-REFRESH" :
2216 bgp_type_str[(int) type]);
2217 bgp_notify_send_with_data (peer,
2218 BGP_NOTIFY_HEADER_ERR,
2219 BGP_NOTIFY_HEADER_BAD_MESLEN,
hassoc9e52be2004-09-26 16:09:34 +00002220 (u_char *) notify_data_length, 2);
paul718e3742002-12-13 20:15:29 +00002221 goto done;
2222 }
2223
2224 /* Adjust size to message length. */
2225 peer->packet_size = size;
2226 }
2227
2228 ret = bgp_read_packet (peer);
2229 if (ret < 0)
2230 goto done;
2231
2232 /* Get size and type again. */
2233 size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE);
2234 type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2);
2235
2236 /* BGP packet dump function. */
2237 bgp_dump_packet (peer, type, peer->ibuf);
2238
2239 size = (peer->packet_size - BGP_HEADER_SIZE);
2240
2241 /* Read rest of the packet and call each sort of packet routine */
2242 switch (type)
2243 {
2244 case BGP_MSG_OPEN:
2245 peer->open_in++;
paulf5ba3872004-07-09 12:11:31 +00002246 bgp_open_receive (peer, size); /* XXX return value ignored! */
paul718e3742002-12-13 20:15:29 +00002247 break;
2248 case BGP_MSG_UPDATE:
2249 peer->readtime = time(NULL); /* Last read timer reset */
2250 bgp_update_receive (peer, size);
2251 break;
2252 case BGP_MSG_NOTIFY:
2253 bgp_notify_receive (peer, size);
2254 break;
2255 case BGP_MSG_KEEPALIVE:
2256 peer->readtime = time(NULL); /* Last read timer reset */
2257 bgp_keepalive_receive (peer, size);
2258 break;
2259 case BGP_MSG_ROUTE_REFRESH_NEW:
2260 case BGP_MSG_ROUTE_REFRESH_OLD:
2261 peer->refresh_in++;
2262 bgp_route_refresh_receive (peer, size);
2263 break;
2264 case BGP_MSG_CAPABILITY:
2265 peer->dynamic_cap_in++;
2266 bgp_capability_receive (peer, size);
2267 break;
2268 }
2269
2270 /* Clear input buffer. */
2271 peer->packet_size = 0;
2272 if (peer->ibuf)
2273 stream_reset (peer->ibuf);
2274
2275 done:
2276 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
2277 {
2278 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00002279 zlog_debug ("%s [Event] Accepting BGP peer delete", peer->host);
paul718e3742002-12-13 20:15:29 +00002280 peer_delete (peer);
2281 }
2282 return 0;
2283}