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