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