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