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