]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_packet.c
Merge pull request #11745 from mjstapp/fix_a_dot_py
[mirror_frr.git] / bgpd / bgp_packet.c
CommitLineData
718e3744 1/* BGP packet management routine.
56257a44
QY
2 * Contains utility functions for constructing and consuming BGP messages.
3 * Copyright (C) 2017 Cumulus Networks
896014f4
DL
4 * Copyright (C) 1999 Kunihiro Ishiguro
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
718e3744 22
23#include <zebra.h>
d3ecc69e 24#include <sys/time.h>
718e3744 25
26#include "thread.h"
27#include "stream.h"
28#include "network.h"
29#include "prefix.h"
30#include "command.h"
31#include "log.h"
32#include "memory.h"
d62a17ae 33#include "sockunion.h" /* for inet_ntop () */
baa376fc 34#include "sockopt.h"
718e3744 35#include "linklist.h"
36#include "plist.h"
3f9c7369 37#include "queue.h"
039f3a34 38#include "filter.h"
02705213 39#include "lib_errors.h"
718e3744 40
41#include "bgpd/bgpd.h"
42#include "bgpd/bgp_table.h"
43#include "bgpd/bgp_dump.h"
6c29258c 44#include "bgpd/bgp_bmp.h"
718e3744 45#include "bgpd/bgp_attr.h"
46#include "bgpd/bgp_debug.h"
14454c9f 47#include "bgpd/bgp_errors.h"
718e3744 48#include "bgpd/bgp_fsm.h"
49#include "bgpd/bgp_route.h"
50#include "bgpd/bgp_packet.h"
51#include "bgpd/bgp_open.h"
52#include "bgpd/bgp_aspath.h"
53#include "bgpd/bgp_community.h"
54#include "bgpd/bgp_ecommunity.h"
57d187bc 55#include "bgpd/bgp_lcommunity.h"
718e3744 56#include "bgpd/bgp_network.h"
57#include "bgpd/bgp_mplsvpn.h"
7ef5a232 58#include "bgpd/bgp_evpn.h"
718e3744 59#include "bgpd/bgp_advertise.h"
93406d87 60#include "bgpd/bgp_vty.h"
3f9c7369 61#include "bgpd/bgp_updgrp.h"
cd1964ff 62#include "bgpd/bgp_label.h"
56257a44 63#include "bgpd/bgp_io.h"
934af458 64#include "bgpd/bgp_keepalives.h"
7c40bf39 65#include "bgpd/bgp_flowspec.h"
d9a03c57 66#include "bgpd/bgp_trace.h"
718e3744 67
584470fb
DL
68DEFINE_HOOK(bgp_packet_dump,
69 (struct peer *peer, uint8_t type, bgp_size_t size,
70 struct stream *s),
8451921b 71 (peer, type, size, s));
584470fb 72
6fd04594
DL
73DEFINE_HOOK(bgp_packet_send,
74 (struct peer *peer, uint8_t type, bgp_size_t size,
75 struct stream *s),
8451921b 76 (peer, type, size, s));
6fd04594 77
d8151687
QY
78/**
79 * Sets marker and type fields for a BGP message.
80 *
81 * @param s the stream containing the packet
82 * @param type the packet type
83 * @return the size of the stream
84 */
d7c0a89a 85int bgp_packet_set_marker(struct stream *s, uint8_t type)
718e3744 86{
d62a17ae 87 int i;
718e3744 88
d62a17ae 89 /* Fill in marker. */
90 for (i = 0; i < BGP_MARKER_SIZE; i++)
91 stream_putc(s, 0xff);
718e3744 92
d62a17ae 93 /* Dummy total length. This field is should be filled in later on. */
94 stream_putw(s, 0);
718e3744 95
d62a17ae 96 /* BGP packet type. */
97 stream_putc(s, type);
718e3744 98
d62a17ae 99 /* Return current stream size. */
100 return stream_get_endp(s);
718e3744 101}
102
d8151687
QY
103/**
104 * Sets size field for a BGP message.
105 *
106 * Size field is set to the size of the stream passed.
107 *
108 * @param s the stream containing the packet
d8151687 109 */
65baedca 110void bgp_packet_set_size(struct stream *s)
718e3744 111{
d62a17ae 112 int cp;
718e3744 113
d62a17ae 114 /* Preserve current pointer. */
115 cp = stream_get_endp(s);
116 stream_putw_at(s, BGP_MARKER_SIZE, cp);
718e3744 117}
118
d3ecc69e
QY
119/*
120 * Push a packet onto the beginning of the peer's output queue.
121 * This function acquires the peer's write mutex before proceeding.
122 */
123static void bgp_packet_add(struct peer *peer, struct stream *s)
124{
bd9fb6f3
DL
125 intmax_t delta;
126 uint32_t holdtime;
127
cb1991af 128 frr_with_mutex (&peer->io_mtx) {
bd9fb6f3
DL
129 /* if the queue is empty, reset the "last OK" timestamp to
130 * now, otherwise if we write another packet immediately
131 * after it'll get confused
132 */
133 if (!stream_fifo_count_safe(peer->obuf))
134 peer->last_sendq_ok = bgp_clock();
135
00dffa8c 136 stream_fifo_push(peer->obuf, s);
bd9fb6f3
DL
137
138 delta = bgp_clock() - peer->last_sendq_ok;
139 holdtime = atomic_load_explicit(&peer->holdtime,
140 memory_order_relaxed);
141
142 /* Note that when we're here, we're adding some packet to the
143 * OutQ. That includes keepalives when there is nothing to
144 * do, so there's a guarantee we pass by here once in a while.
145 *
146 * That implies there is no need to go set up another separate
147 * timer that ticks down SendHoldTime, as we'll be here sooner
148 * or later anyway and will see the checks below failing.
149 */
382268f4
DL
150 if (!holdtime) {
151 /* no holdtime, do nothing. */
152 } else if (delta > 2 * (intmax_t)holdtime) {
bd9fb6f3
DL
153 flog_err(
154 EC_BGP_SENDQ_STUCK_PROPER,
155 "%s has not made any SendQ progress for 2 holdtimes, terminating session",
156 peer->host);
157 BGP_EVENT_ADD(peer, TCP_fatal_error);
158 } else if (delta > (intmax_t)holdtime &&
159 bgp_clock() - peer->last_sendq_warn > 5) {
160 flog_warn(
161 EC_BGP_SENDQ_STUCK_WARN,
162 "%s has not made any SendQ progress for 1 holdtime, peer overloaded?",
163 peer->host);
164 peer->last_sendq_warn = bgp_clock();
165 }
00dffa8c 166 }
718e3744 167}
168
d62a17ae 169static struct stream *bgp_update_packet_eor(struct peer *peer, afi_t afi,
170 safi_t safi)
93406d87 171{
d62a17ae 172 struct stream *s;
617975d1
DS
173 iana_afi_t pkt_afi = IANA_AFI_IPV4;
174 iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
d62a17ae 175
176 if (DISABLE_BGP_ANNOUNCE)
177 return NULL;
178
179 if (bgp_debug_neighbor_events(peer))
180 zlog_debug("send End-of-RIB for %s to %s",
5cb5f4d0 181 get_afi_safi_str(afi, safi, false), peer->host);
d62a17ae 182
ef56aee4 183 s = stream_new(peer->max_packet_size);
d62a17ae 184
185 /* Make BGP update packet. */
186 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
187
188 /* Unfeasible Routes Length */
189 stream_putw(s, 0);
190
191 if (afi == AFI_IP && safi == SAFI_UNICAST) {
192 /* Total Path Attribute Length */
193 stream_putw(s, 0);
194 } else {
195 /* Convert AFI, SAFI to values for packet. */
196 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
197
198 /* Total Path Attribute Length */
199 stream_putw(s, 6);
200 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
201 stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI);
202 stream_putc(s, 3);
203 stream_putw(s, pkt_afi);
204 stream_putc(s, pkt_safi);
205 }
206
207 bgp_packet_set_size(s);
d62a17ae 208 return s;
718e3744 209}
210
d8151687
QY
211/* Called when there is a change in the EOR(implicit or explicit) status of a
212 * peer. Ends the update-delay if all expected peers are done with EORs. */
213void bgp_check_update_delay(struct bgp *bgp)
214{
215 struct listnode *node, *nnode;
216 struct peer *peer = NULL;
217
218 if (bgp_debug_neighbor_events(peer))
219 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
220 bgp->established, bgp->restarted_peers,
221 bgp->implicit_eors, bgp->explicit_eors);
222
223 if (bgp->established
224 <= bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) {
becedef6
QY
225 /*
226 * This is an extra sanity check to make sure we wait for all
227 * the eligible configured peers. This check is performed if
228 * establish wait timer is on, or establish wait option is not
229 * given with the update-delay command
230 */
d8151687
QY
231 if (bgp->t_establish_wait
232 || (bgp->v_establish_wait == bgp->v_update_delay))
233 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
234 if (CHECK_FLAG(peer->flags,
235 PEER_FLAG_CONFIG_NODE)
236 && !CHECK_FLAG(peer->flags,
237 PEER_FLAG_SHUTDOWN)
cb9196e7
DS
238 && !CHECK_FLAG(peer->bgp->flags,
239 BGP_FLAG_SHUTDOWN)
d8151687
QY
240 && !peer->update_delay_over) {
241 if (bgp_debug_neighbor_events(peer))
242 zlog_debug(
243 " Peer %s pending, continuing read-only mode",
244 peer->host);
245 return;
246 }
247 }
248
249 zlog_info(
250 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
251 bgp->restarted_peers, bgp->implicit_eors,
252 bgp->explicit_eors);
253 bgp_update_delay_end(bgp);
254 }
255}
256
becedef6
QY
257/*
258 * Called if peer is known to have restarted. The restart-state bit in
259 * Graceful-Restart capability is used for that
260 */
d8151687
QY
261void bgp_update_restarted_peers(struct peer *peer)
262{
263 if (!bgp_update_delay_active(peer->bgp))
264 return; /* BGP update delay has ended */
265 if (peer->update_delay_over)
266 return; /* This peer has already been considered */
267
268 if (bgp_debug_neighbor_events(peer))
269 zlog_debug("Peer %s: Checking restarted", peer->host);
270
feb17238 271 if (peer_established(peer)) {
d8151687
QY
272 peer->update_delay_over = 1;
273 peer->bgp->restarted_peers++;
274 bgp_check_update_delay(peer->bgp);
275 }
276}
277
becedef6
QY
278/*
279 * Called as peer receives a keep-alive. Determines if this occurence can be
280 * taken as an implicit EOR for this peer.
281 * NOTE: The very first keep-alive after the Established state of a peer is
282 * considered implicit EOR for the update-delay purposes
283 */
d8151687
QY
284void bgp_update_implicit_eors(struct peer *peer)
285{
286 if (!bgp_update_delay_active(peer->bgp))
287 return; /* BGP update delay has ended */
288 if (peer->update_delay_over)
289 return; /* This peer has already been considered */
290
291 if (bgp_debug_neighbor_events(peer))
292 zlog_debug("Peer %s: Checking implicit EORs", peer->host);
293
feb17238 294 if (peer_established(peer)) {
d8151687
QY
295 peer->update_delay_over = 1;
296 peer->bgp->implicit_eors++;
297 bgp_check_update_delay(peer->bgp);
298 }
299}
300
becedef6
QY
301/*
302 * Should be called only when there is a change in the EOR_RECEIVED status
303 * for any afi/safi on a peer.
304 */
d8151687
QY
305static void bgp_update_explicit_eors(struct peer *peer)
306{
307 afi_t afi;
308 safi_t safi;
309
310 if (!bgp_update_delay_active(peer->bgp))
311 return; /* BGP update delay has ended */
312 if (peer->update_delay_over)
313 return; /* This peer has already been considered */
314
315 if (bgp_debug_neighbor_events(peer))
316 zlog_debug("Peer %s: Checking explicit EORs", peer->host);
317
f18ba3cd
DS
318 FOREACH_AFI_SAFI (afi, safi) {
319 if (peer->afc_nego[afi][safi]
320 && !CHECK_FLAG(peer->af_sflags[afi][safi],
321 PEER_STATUS_EOR_RECEIVED)) {
322 if (bgp_debug_neighbor_events(peer))
323 zlog_debug(
324 " afi %d safi %d didn't receive EOR",
325 afi, safi);
326 return;
d8151687 327 }
f18ba3cd 328 }
d8151687
QY
329
330 peer->update_delay_over = 1;
331 peer->bgp->explicit_eors++;
332 bgp_check_update_delay(peer->bgp);
333}
334
335/**
336 * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
337 *
338 * mp_withdraw, if set, is used to nullify attr structure on most of the
339 * calling safi function and for evpn, passed as parameter
340 */
341int bgp_nlri_parse(struct peer *peer, struct attr *attr,
342 struct bgp_nlri *packet, int mp_withdraw)
343{
344 switch (packet->safi) {
345 case SAFI_UNICAST:
346 case SAFI_MULTICAST:
347 return bgp_nlri_parse_ip(peer, mp_withdraw ? NULL : attr,
348 packet);
349 case SAFI_LABELED_UNICAST:
350 return bgp_nlri_parse_label(peer, mp_withdraw ? NULL : attr,
351 packet);
352 case SAFI_MPLS_VPN:
353 return bgp_nlri_parse_vpn(peer, mp_withdraw ? NULL : attr,
354 packet);
355 case SAFI_EVPN:
356 return bgp_nlri_parse_evpn(peer, attr, packet, mp_withdraw);
7c40bf39 357 case SAFI_FLOWSPEC:
358 return bgp_nlri_parse_flowspec(peer, attr, packet, mp_withdraw);
d8151687 359 }
513386b5 360 return BGP_NLRI_PARSE_ERROR;
d8151687
QY
361}
362
6ec98a2f
QY
363/*
364 * Checks a variety of conditions to determine whether the peer needs to be
365 * rescheduled for packet generation again, and does so if necessary.
366 *
367 * @param peer to check for rescheduling
368 */
af1e1dc6
QY
369static void bgp_write_proceed_actions(struct peer *peer)
370{
371 afi_t afi;
372 safi_t safi;
373 struct peer_af *paf;
374 struct bpacket *next_pkt;
375 struct update_subgroup *subgrp;
0e5cdd59 376 enum bgp_af_index index;
af1e1dc6 377
0e5cdd59
DS
378 for (index = BGP_AF_START; index < BGP_AF_MAX; index++) {
379 paf = peer->peer_af_array[index];
c58b0f46
QY
380 if (!paf)
381 continue;
0e5cdd59 382
c58b0f46
QY
383 subgrp = paf->subgroup;
384 if (!subgrp)
385 continue;
386
387 next_pkt = paf->next_pkt_to_send;
388 if (next_pkt && next_pkt->buffer) {
389 BGP_TIMER_ON(peer->t_generate_updgrp_packets,
390 bgp_generate_updgrp_packets, 0);
391 return;
392 }
af1e1dc6 393
c58b0f46
QY
394 /* No packets readily available for AFI/SAFI, are there
395 * subgroup packets
396 * that need to be generated? */
397 if (bpacket_queue_is_full(SUBGRP_INST(subgrp),
398 SUBGRP_PKTQ(subgrp))
399 || subgroup_packets_to_build(subgrp)) {
400 BGP_TIMER_ON(peer->t_generate_updgrp_packets,
401 bgp_generate_updgrp_packets, 0);
402 return;
403 }
af1e1dc6 404
0e5cdd59
DS
405 afi = paf->afi;
406 safi = paf->safi;
407
c58b0f46
QY
408 /* No packets to send, see if EOR is pending */
409 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)) {
410 if (!subgrp->t_coalesce && peer->afc_nego[afi][safi]
411 && peer->synctime
412 && !CHECK_FLAG(peer->af_sflags[afi][safi],
413 PEER_STATUS_EOR_SEND)
414 && safi != SAFI_MPLS_VPN) {
af1e1dc6
QY
415 BGP_TIMER_ON(peer->t_generate_updgrp_packets,
416 bgp_generate_updgrp_packets, 0);
417 return;
418 }
af1e1dc6 419 }
c58b0f46 420 }
af1e1dc6
QY
421}
422
6ec98a2f
QY
423/*
424 * Generate advertisement information (withdraws, updates, EOR) from each
425 * update group a peer belongs to, encode this information into packets, and
426 * enqueue the packets onto the peer's output buffer.
56257a44 427 */
cc9f21da 428void bgp_generate_updgrp_packets(struct thread *thread)
718e3744 429{
56257a44 430 struct peer *peer = THREAD_ARG(thread);
56257a44
QY
431
432 struct stream *s;
d62a17ae 433 struct peer_af *paf;
434 struct bpacket *next_pkt;
d0ad6d8e
QY
435 uint32_t wpq;
436 uint32_t generated = 0;
d62a17ae 437 afi_t afi;
438 safi_t safi;
439
d0ad6d8e
QY
440 wpq = atomic_load_explicit(&peer->bgp->wpkt_quanta,
441 memory_order_relaxed);
442
d62a17ae 443 /*
444 * The code beyond this part deals with update packets, proceed only
445 * if peer is Established and updates are not on hold (as part of
f4d2dd84 446 * update-delay processing).
3f9c7369 447 */
feb17238 448 if (!peer_established(peer))
cc9f21da 449 return;
d62a17ae 450
f4d2dd84
DS
451 if ((peer->bgp->main_peers_update_hold)
452 || bgp_update_delay_active(peer->bgp))
cc9f21da 453 return;
d62a17ae 454
b10b6d52 455 if (peer->t_routeadv)
cc9f21da 456 return;
b10b6d52 457
56257a44 458 do {
0e5cdd59
DS
459 enum bgp_af_index index;
460
56257a44 461 s = NULL;
0e5cdd59
DS
462 for (index = BGP_AF_START; index < BGP_AF_MAX; index++) {
463 paf = peer->peer_af_array[index];
c58b0f46
QY
464 if (!paf || !PAF_SUBGRP(paf))
465 continue;
0e5cdd59
DS
466
467 afi = paf->afi;
468 safi = paf->safi;
c58b0f46 469 next_pkt = paf->next_pkt_to_send;
80bd61c4 470
c58b0f46
QY
471 /*
472 * Try to generate a packet for the peer if we are at
473 * the end of the list. Always try to push out
474 * WITHDRAWs first.
475 */
476 if (!next_pkt || !next_pkt->buffer) {
477 next_pkt = subgroup_withdraw_packet(
478 PAF_SUBGRP(paf));
479 if (!next_pkt || !next_pkt->buffer)
480 subgroup_update_packet(PAF_SUBGRP(paf));
481 next_pkt = paf->next_pkt_to_send;
482 }
80bd61c4 483
c58b0f46
QY
484 /*
485 * If we still don't have a packet to send to the peer,
486 * then try to find out out if we have to send eor or
487 * if not, skip to the next AFI, SAFI. Don't send the
488 * EOR prematurely; if the subgroup's coalesce timer is
489 * running, the adjacency-out structure is not created
490 * yet.
491 */
492 if (!next_pkt || !next_pkt->buffer) {
9af52ccf
DA
493 if (!paf->t_announce_route) {
494 /* Make sure we supress BGP UPDATES
495 * for normal processing later again.
496 */
2adac256
DA
497 UNSET_FLAG(paf->subgroup->sflags,
498 SUBGRP_STATUS_FORCE_UPDATES);
499
9af52ccf
DA
500 /* If route-refresh BoRR message was
501 * already sent and we are done with
502 * re-announcing tables for a decent
503 * afi/safi, we ready to send
504 * EoRR request.
505 */
506 if (CHECK_FLAG(
507 peer->af_sflags[afi][safi],
508 PEER_STATUS_BORR_SEND)) {
509 bgp_route_refresh_send(
510 peer, afi, safi, 0, 0,
511 0,
512 BGP_ROUTE_REFRESH_EORR);
513
514 SET_FLAG(peer->af_sflags[afi]
515 [safi],
516 PEER_STATUS_EORR_SEND);
517 UNSET_FLAG(
518 peer->af_sflags[afi]
519 [safi],
520 PEER_STATUS_BORR_SEND);
521
522 if (bgp_debug_neighbor_events(
523 peer))
524 zlog_debug(
f70c91dc
DA
525 "%pBP sending route-refresh (EoRR) for %s/%s",
526 peer,
9af52ccf
DA
527 afi2str(afi),
528 safi2str(safi));
529 }
530 }
531
c58b0f46 532 if (CHECK_FLAG(peer->cap,
36235319 533 PEER_CAP_RESTART_RCV)) {
c58b0f46 534 if (!(PAF_SUBGRP(paf))->t_coalesce
36235319
QY
535 && peer->afc_nego[afi][safi]
536 && peer->synctime
537 && !CHECK_FLAG(
538 peer->af_sflags[afi][safi],
539 PEER_STATUS_EOR_SEND)) {
d6e3c15b 540 /* If EOR is disabled,
541 * the message is not sent
542 */
36235319
QY
543 if (BGP_SEND_EOR(peer->bgp, afi,
544 safi)) {
d6e3c15b 545 SET_FLAG(
36235319
QY
546 peer->af_sflags
547 [afi]
548 [safi],
549 PEER_STATUS_EOR_SEND);
d6e3c15b 550
9e3b51a7 551 /* Update EOR
552 * send time
553 */
36235319
QY
554 peer->eor_stime[afi]
555 [safi] =
556 monotime(NULL);
9e3b51a7 557
d6e3c15b 558 BGP_UPDATE_EOR_PKT(
36235319
QY
559 peer, afi, safi,
560 s);
56257a44 561 }
80bd61c4 562 }
d62a17ae 563 }
c58b0f46
QY
564 continue;
565 }
9e3b51a7 566
567 /* Update packet send time */
568 peer->pkt_stime[afi][safi] = monotime(NULL);
569
c58b0f46
QY
570 /* Found a packet template to send, overwrite
571 * packet with appropriate attributes from peer
572 * and advance peer */
573 s = bpacket_reformat_for_peer(next_pkt, paf);
574 bgp_packet_add(peer, s);
c58b0f46
QY
575 bpacket_queue_advance_peer(paf);
576 }
d0ad6d8e 577 } while (s && (++generated < wpq));
80bd61c4 578
6ec98a2f
QY
579 if (generated)
580 bgp_writes_on(peer);
581
af1e1dc6 582 bgp_write_proceed_actions(peer);
80bd61c4
QY
583}
584
d3ecc69e
QY
585/*
586 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
587 */
d62a17ae 588void bgp_keepalive_send(struct peer *peer)
718e3744 589{
d62a17ae 590 struct stream *s;
591
556beacf 592 s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
718e3744 593
d62a17ae 594 /* Make keepalive packet. */
595 bgp_packet_set_marker(s, BGP_MSG_KEEPALIVE);
718e3744 596
d62a17ae 597 /* Set packet size. */
65baedca 598 bgp_packet_set_size(s);
718e3744 599
d62a17ae 600 /* Dump packet if debug option is set. */
601 /* bgp_packet_dump (s); */
718e3744 602
d62a17ae 603 if (bgp_debug_keepalive(peer))
604 zlog_debug("%s sending KEEPALIVE", peer->host);
718e3744 605
d62a17ae 606 /* Add packet to the peer. */
607 bgp_packet_add(peer, s);
424ab01d
QY
608
609 bgp_writes_on(peer);
718e3744 610}
611
d3ecc69e
QY
612/*
613 * Creates a BGP Open packet and appends it to the peer's output queue.
614 * Sets capabilities as necessary.
615 */
d62a17ae 616void bgp_open_send(struct peer *peer)
718e3744 617{
d62a17ae 618 struct stream *s;
d7c0a89a 619 uint16_t send_holdtime;
d62a17ae 620 as_t local_as;
718e3744 621
b90a8e13 622 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
d62a17ae 623 send_holdtime = peer->holdtime;
624 else
625 send_holdtime = peer->bgp->default_holdtime;
718e3744 626
d62a17ae 627 /* local-as Change */
628 if (peer->change_local_as)
629 local_as = peer->change_local_as;
630 else
631 local_as = peer->local_as;
718e3744 632
556beacf 633 s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
718e3744 634
d62a17ae 635 /* Make open packet. */
636 bgp_packet_set_marker(s, BGP_MSG_OPEN);
718e3744 637
d62a17ae 638 /* Set open packet values. */
639 stream_putc(s, BGP_VERSION_4); /* BGP version */
d7c0a89a
QY
640 stream_putw(s, (local_as <= BGP_AS_MAX) ? (uint16_t)local_as
641 : BGP_AS_TRANS);
d62a17ae 642 stream_putw(s, send_holdtime); /* Hold Time */
643 stream_put_in_addr(s, &peer->local_id); /* BGP Identifier */
718e3744 644
d08c0c80
DA
645 /* Set capabilities */
646 if (CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
647 (void)bgp_open_capability(s, peer, true);
648 } else {
649 struct stream *tmp = stream_new(STREAM_SIZE(s));
650
651 stream_copy(tmp, s);
652 if (bgp_open_capability(tmp, peer, false)
653 > BGP_OPEN_NON_EXT_OPT_LEN) {
654 stream_free(tmp);
655 (void)bgp_open_capability(s, peer, true);
656 } else {
657 stream_copy(s, tmp);
658 stream_free(tmp);
659 }
660 }
718e3744 661
d62a17ae 662 /* Set BGP packet length. */
65baedca 663 bgp_packet_set_size(s);
718e3744 664
d62a17ae 665 if (bgp_debug_neighbor_events(peer))
666 zlog_debug(
23d0a753 667 "%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4",
d62a17ae 668 peer->host, BGP_VERSION_4, local_as, send_holdtime,
23d0a753 669 &peer->local_id);
718e3744 670
d62a17ae 671 /* Dump packet if debug option is set. */
672 /* bgp_packet_dump (s); */
6fd04594 673 hook_call(bgp_packet_send, peer, BGP_MSG_OPEN, stream_get_endp(s), s);
718e3744 674
d62a17ae 675 /* Add packet to the peer. */
676 bgp_packet_add(peer, s);
424ab01d
QY
677
678 bgp_writes_on(peer);
679}
680
a127f33b
QY
681/*
682 * Writes NOTIFICATION message directly to a peer socket without waiting for
683 * the I/O thread.
684 *
685 * There must be exactly one stream on the peer->obuf FIFO, and the data within
686 * this stream must match the format of a BGP NOTIFICATION message.
687 * Transmission is best-effort.
688 *
689 * @requires peer->io_mtx
690 * @param peer
691 * @return 0
692 */
3dc339cd 693static void bgp_write_notify(struct peer *peer)
424ab01d
QY
694{
695 int ret, val;
d7c0a89a 696 uint8_t type;
424ab01d
QY
697 struct stream *s;
698
a127f33b
QY
699 /* There should be at least one packet. */
700 s = stream_fifo_pop(peer->obuf);
424ab01d 701
8ec586b0 702 if (!s)
3dc339cd 703 return;
8ec586b0
QY
704
705 assert(stream_get_endp(s) >= BGP_HEADER_SIZE);
706
becedef6
QY
707 /*
708 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
709 * we only care about getting a clean shutdown at this point.
710 */
424ab01d
QY
711 ret = write(peer->fd, STREAM_DATA(s), stream_get_endp(s));
712
becedef6
QY
713 /*
714 * only connection reset/close gets counted as TCP_fatal_error, failure
715 * to write the entire NOTIFY doesn't get different FSM treatment
716 */
424ab01d 717 if (ret <= 0) {
3735936b 718 stream_free(s);
424ab01d 719 BGP_EVENT_ADD(peer, TCP_fatal_error);
3dc339cd 720 return;
424ab01d
QY
721 }
722
723 /* Disable Nagle, make NOTIFY packet go out right away */
724 val = 1;
725 (void)setsockopt(peer->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val,
726 sizeof(val));
727
728 /* Retrieve BGP packet type. */
729 stream_set_getp(s, BGP_MARKER_SIZE + 2);
730 type = stream_getc(s);
731
732 assert(type == BGP_MSG_NOTIFY);
733
734 /* Type should be notify. */
0112e9e0 735 atomic_fetch_add_explicit(&peer->notify_out, 1, memory_order_relaxed);
424ab01d
QY
736
737 /* Double start timer. */
738 peer->v_start *= 2;
739
740 /* Overflow check. */
741 if (peer->v_start >= (60 * 2))
742 peer->v_start = (60 * 2);
743
becedef6
QY
744 /*
745 * Handle Graceful Restart case where the state changes to
746 * Connect instead of Idle
747 */
424ab01d
QY
748 BGP_EVENT_ADD(peer, BGP_Stop);
749
3735936b 750 stream_free(s);
718e3744 751}
752
eea685b6
DA
753/*
754 * Encapsulate an original BGP CEASE Notification into Hard Reset
755 */
756static uint8_t *bgp_notify_encapsulate_hard_reset(uint8_t code, uint8_t subcode,
757 uint8_t *data, size_t datalen)
758{
759 uint8_t *message = XCALLOC(MTYPE_BGP_NOTIFICATION, datalen + 2);
760
761 /* ErrCode */
762 message[0] = code;
763 /* Subcode */
764 message[1] = subcode;
765 /* Data */
766 if (datalen)
767 memcpy(message + 2, data, datalen);
768
769 return message;
770}
771
772/*
773 * Decapsulate an original BGP CEASE Notification from Hard Reset
774 */
775struct bgp_notify bgp_notify_decapsulate_hard_reset(struct bgp_notify *notify)
776{
777 struct bgp_notify bn = {};
778
779 bn.code = notify->raw_data[0];
780 bn.subcode = notify->raw_data[1];
781 bn.length = notify->length - 2;
782
c73d2363 783 bn.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, bn.length);
eea685b6
DA
784 memcpy(bn.raw_data, notify->raw_data + 2, bn.length);
785
786 return bn;
787}
788
20170775
DA
789/* Check if Graceful-Restart N-bit is exchanged */
790bool bgp_has_graceful_restart_notification(struct peer *peer)
791{
792 return CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV) &&
793 CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV);
794}
795
eea685b6
DA
796/*
797 * Check if to send BGP CEASE Notification/Hard Reset?
798 */
1ae314be
DA
799bool bgp_notify_send_hard_reset(struct peer *peer, uint8_t code,
800 uint8_t subcode)
eea685b6
DA
801{
802 /* When the "N" bit has been exchanged, a Hard Reset message is used to
803 * indicate to the peer that the session is to be fully terminated.
804 */
20170775 805 if (!bgp_has_graceful_restart_notification(peer))
eea685b6
DA
806 return false;
807
808 /*
809 * https://datatracker.ietf.org/doc/html/rfc8538#section-5.1
810 */
1ae314be 811 if (code == BGP_NOTIFY_CEASE) {
eea685b6
DA
812 switch (subcode) {
813 case BGP_NOTIFY_CEASE_MAX_PREFIX:
814 case BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN:
815 case BGP_NOTIFY_CEASE_PEER_UNCONFIG:
816 case BGP_NOTIFY_CEASE_HARD_RESET:
aebe2e37 817 case BGP_NOTIFY_CEASE_BFD_DOWN:
eea685b6 818 return true;
1ae314be
DA
819 case BGP_NOTIFY_CEASE_ADMIN_RESET:
820 /* Provide user control:
821 * `bgp hard-adminstrative-reset`
822 */
823 if (CHECK_FLAG(peer->bgp->flags,
824 BGP_FLAG_HARD_ADMIN_RESET))
825 return true;
826 else
827 return false;
eea685b6
DA
828 default:
829 break;
830 }
831 }
832
833 return false;
834}
835
1ae314be
DA
836/*
837 * Check if received BGP CEASE Notification/Hard Reset?
838 */
839bool bgp_notify_received_hard_reset(struct peer *peer, uint8_t code,
840 uint8_t subcode)
841{
842 /* When the "N" bit has been exchanged, a Hard Reset message is used to
843 * indicate to the peer that the session is to be fully terminated.
844 */
20170775 845 if (!bgp_has_graceful_restart_notification(peer))
1ae314be
DA
846 return false;
847
848 if (code == BGP_NOTIFY_CEASE && subcode == BGP_NOTIFY_CEASE_HARD_RESET)
849 return true;
850
851 return false;
852}
853
d3ecc69e
QY
854/*
855 * Creates a BGP Notify and appends it to the peer's output queue.
856 *
becedef6
QY
857 * This function attempts to write the packet from the thread it is called
858 * from, to ensure the packet gets out ASAP.
d3ecc69e 859 *
a127f33b
QY
860 * This function may be called from multiple threads. Since the function
861 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
862 * call to prevent tampering from other threads.
863 *
864 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
865 * return, the peer structure *must* be reset; no assumptions about session
866 * state are valid.
867 *
d3ecc69e
QY
868 * @param peer
869 * @param code BGP error code
870 * @param sub_code BGP error subcode
871 * @param data Data portion
872 * @param datalen length of data portion
873 */
d7c0a89a
QY
874void bgp_notify_send_with_data(struct peer *peer, uint8_t code,
875 uint8_t sub_code, uint8_t *data, size_t datalen)
718e3744 876{
d62a17ae 877 struct stream *s;
1ae314be 878 bool hard_reset = bgp_notify_send_hard_reset(peer, code, sub_code);
d62a17ae 879
a127f33b 880 /* Lock I/O mutex to prevent other threads from pushing packets */
00dffa8c 881 frr_mutex_lock_autounlock(&peer->io_mtx);
a127f33b
QY
882 /* ============================================== */
883
d62a17ae 884 /* Allocate new stream. */
ef56aee4 885 s = stream_new(peer->max_packet_size);
d62a17ae 886
d3ecc69e 887 /* Make notify packet. */
d62a17ae 888 bgp_packet_set_marker(s, BGP_MSG_NOTIFY);
889
eea685b6
DA
890 /* Check if we should send Hard Reset Notification or not */
891 if (hard_reset) {
892 uint8_t *hard_reset_message = bgp_notify_encapsulate_hard_reset(
893 code, sub_code, data, datalen);
d62a17ae 894
eea685b6
DA
895 /* Hard Reset encapsulates another NOTIFICATION message
896 * in its data portion.
897 */
898 stream_putc(s, BGP_NOTIFY_CEASE);
899 stream_putc(s, BGP_NOTIFY_CEASE_HARD_RESET);
900 stream_write(s, hard_reset_message, datalen + 2);
901
902 XFREE(MTYPE_BGP_NOTIFICATION, hard_reset_message);
903 } else {
904 stream_putc(s, code);
905 stream_putc(s, sub_code);
906 if (data)
907 stream_write(s, data, datalen);
908 }
d62a17ae 909
910 /* Set BGP packet length. */
bd6b2706 911 bgp_packet_set_size(s);
d62a17ae 912
424ab01d 913 /* wipe output buffer */
a127f33b 914 stream_fifo_clean(peer->obuf);
d62a17ae 915
becedef6
QY
916 /*
917 * If possible, store last packet for debugging purposes. This check is
918 * in place because we are sometimes called with a doppelganger peer,
919 * who tends to have a plethora of fields nulled out.
920 */
1a1f4534 921 if (peer->curr) {
d8151687 922 size_t packetsize = stream_get_endp(peer->curr);
556beacf 923 assert(packetsize <= peer->max_packet_size);
d8151687
QY
924 memcpy(peer->last_reset_cause, peer->curr->data, packetsize);
925 peer->last_reset_cause_size = packetsize;
926 }
927
d62a17ae 928 /* For debug */
929 {
930 struct bgp_notify bgp_notify;
931 int first = 0;
932 int i;
933 char c[4];
934
935 bgp_notify.code = code;
936 bgp_notify.subcode = sub_code;
937 bgp_notify.data = NULL;
e0981960 938 bgp_notify.length = datalen;
d62a17ae 939 bgp_notify.raw_data = data;
940
941 peer->notify.code = bgp_notify.code;
942 peer->notify.subcode = bgp_notify.subcode;
943
e0981960 944 if (bgp_notify.length && data) {
d62a17ae 945 bgp_notify.data =
946 XMALLOC(MTYPE_TMP, bgp_notify.length * 3);
947 for (i = 0; i < bgp_notify.length; i++)
948 if (first) {
552d6491
QY
949 snprintf(c, sizeof(c), " %02x",
950 data[i]);
f009ff26 951
552d6491 952 strlcat(bgp_notify.data, c,
2ba1fe69 953 bgp_notify.length);
f009ff26 954
d62a17ae 955 } else {
956 first = 1;
552d6491 957 snprintf(c, sizeof(c), "%02x", data[i]);
f009ff26 958
552d6491 959 strlcpy(bgp_notify.data, c,
2ba1fe69 960 bgp_notify.length);
d62a17ae 961 }
962 }
eea685b6 963 bgp_notify_print(peer, &bgp_notify, "sending", hard_reset);
d62a17ae 964
965 if (bgp_notify.data) {
966 XFREE(MTYPE_TMP, bgp_notify.data);
d62a17ae 967 bgp_notify.length = 0;
968 }
969 }
970
971 /* peer reset cause */
972 if (code == BGP_NOTIFY_CEASE) {
973 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
974 peer->last_reset = PEER_DOWN_USER_RESET;
975 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
976 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
977 else
978 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
979 } else
980 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
981
d3ecc69e 982 /* Add packet to peer's output queue */
a127f33b 983 stream_fifo_push(peer->obuf, s);
424ab01d 984
5cce3f05 985 bgp_peer_gr_flags_update(peer);
36235319
QY
986 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
987 peer->bgp->peer);
5cce3f05 988
424ab01d 989 bgp_write_notify(peer);
718e3744 990}
991
d3ecc69e
QY
992/*
993 * Creates a BGP Notify and appends it to the peer's output queue.
994 *
becedef6
QY
995 * This function attempts to write the packet from the thread it is called
996 * from, to ensure the packet gets out ASAP.
d3ecc69e
QY
997 *
998 * @param peer
999 * @param code BGP error code
1000 * @param sub_code BGP error subcode
1001 */
d7c0a89a 1002void bgp_notify_send(struct peer *peer, uint8_t code, uint8_t sub_code)
718e3744 1003{
d62a17ae 1004 bgp_notify_send_with_data(peer, code, sub_code, NULL, 0);
718e3744 1005}
1006
d3ecc69e
QY
1007/*
1008 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
1009 *
1010 * @param peer
1011 * @param afi Address Family Identifier
1012 * @param safi Subsequent Address Family Identifier
1013 * @param orf_type Outbound Route Filtering type
1014 * @param when_to_refresh Whether to refresh immediately or defer
1015 * @param remove Whether to remove ORF for specified AFI/SAFI
1016 */
d62a17ae 1017void bgp_route_refresh_send(struct peer *peer, afi_t afi, safi_t safi,
d7c0a89a 1018 uint8_t orf_type, uint8_t when_to_refresh,
9af52ccf 1019 int remove, uint8_t subtype)
718e3744 1020{
d62a17ae 1021 struct stream *s;
1022 struct bgp_filter *filter;
1023 int orf_refresh = 0;
617975d1
DS
1024 iana_afi_t pkt_afi = IANA_AFI_IPV4;
1025 iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
d62a17ae 1026
1027 if (DISABLE_BGP_ANNOUNCE)
1028 return;
1029
1030 filter = &peer->filter[afi][safi];
1031
1032 /* Convert AFI, SAFI to values for packet. */
1033 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
1034
ef56aee4 1035 s = stream_new(peer->max_packet_size);
d62a17ae 1036
1037 /* Make BGP update packet. */
1038 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
1039 bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_NEW);
718e3744 1040 else
d62a17ae 1041 bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_OLD);
1042
1043 /* Encode Route Refresh message. */
1044 stream_putw(s, pkt_afi);
9af52ccf
DA
1045 if (subtype)
1046 stream_putc(s, subtype);
1047 else
1048 stream_putc(s, 0);
d62a17ae 1049 stream_putc(s, pkt_safi);
1050
1051 if (orf_type == ORF_TYPE_PREFIX || orf_type == ORF_TYPE_PREFIX_OLD)
1052 if (remove || filter->plist[FILTER_IN].plist) {
d7c0a89a 1053 uint16_t orf_len;
d62a17ae 1054 unsigned long orfp;
1055
1056 orf_refresh = 1;
1057 stream_putc(s, when_to_refresh);
1058 stream_putc(s, orf_type);
1059 orfp = stream_get_endp(s);
1060 stream_putw(s, 0);
1061
1062 if (remove) {
1063 UNSET_FLAG(peer->af_sflags[afi][safi],
1064 PEER_STATUS_ORF_PREFIX_SEND);
1065 stream_putc(s, ORF_COMMON_PART_REMOVE_ALL);
1066 if (bgp_debug_neighbor_events(peer))
1067 zlog_debug(
f70c91dc
DA
1068 "%pBP sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s",
1069 peer, orf_type,
a7d91a8c
DA
1070 (when_to_refresh ==
1071 REFRESH_DEFER
d62a17ae 1072 ? "defer"
1073 : "immediate"),
748a041f
DS
1074 iana_afi2str(pkt_afi),
1075 iana_safi2str(pkt_safi));
d62a17ae 1076 } else {
1077 SET_FLAG(peer->af_sflags[afi][safi],
1078 PEER_STATUS_ORF_PREFIX_SEND);
1079 prefix_bgp_orf_entry(
1080 s, filter->plist[FILTER_IN].plist,
1081 ORF_COMMON_PART_ADD,
1082 ORF_COMMON_PART_PERMIT,
1083 ORF_COMMON_PART_DENY);
1084 if (bgp_debug_neighbor_events(peer))
1085 zlog_debug(
f70c91dc
DA
1086 "%pBP sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s",
1087 peer, orf_type,
a7d91a8c
DA
1088 (when_to_refresh ==
1089 REFRESH_DEFER
d62a17ae 1090 ? "defer"
1091 : "immediate"),
748a041f
DS
1092 iana_afi2str(pkt_afi),
1093 iana_safi2str(pkt_safi));
d62a17ae 1094 }
1095
1096 /* Total ORF Entry Len. */
1097 orf_len = stream_get_endp(s) - orfp - 2;
1098 stream_putw_at(s, orfp, orf_len);
1099 }
1100
1101 /* Set packet size. */
65baedca 1102 bgp_packet_set_size(s);
d62a17ae 1103
1104 if (bgp_debug_neighbor_events(peer)) {
1105 if (!orf_refresh)
a7d91a8c 1106 zlog_debug(
f70c91dc
DA
1107 "%pBP sending REFRESH_REQ for afi/safi: %s/%s",
1108 peer, iana_afi2str(pkt_afi),
1109 iana_safi2str(pkt_safi));
d62a17ae 1110 }
1111
1112 /* Add packet to the peer. */
1113 bgp_packet_add(peer, s);
424ab01d
QY
1114
1115 bgp_writes_on(peer);
718e3744 1116}
1117
d3ecc69e
QY
1118/*
1119 * Create a BGP Capability packet and append it to the peer's output queue.
1120 *
1121 * @param peer
1122 * @param afi Address Family Identifier
1123 * @param safi Subsequent Address Family Identifier
1124 * @param capability_code BGP Capability Code
1125 * @param action Set or Remove capability
1126 */
d62a17ae 1127void bgp_capability_send(struct peer *peer, afi_t afi, safi_t safi,
1128 int capability_code, int action)
718e3744 1129{
d62a17ae 1130 struct stream *s;
617975d1
DS
1131 iana_afi_t pkt_afi = IANA_AFI_IPV4;
1132 iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
d62a17ae 1133
1134 /* Convert AFI, SAFI to values for packet. */
1135 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
1136
ef56aee4 1137 s = stream_new(peer->max_packet_size);
d62a17ae 1138
1139 /* Make BGP update packet. */
1140 bgp_packet_set_marker(s, BGP_MSG_CAPABILITY);
1141
1142 /* Encode MP_EXT capability. */
1143 if (capability_code == CAPABILITY_CODE_MP) {
1144 stream_putc(s, action);
1145 stream_putc(s, CAPABILITY_CODE_MP);
1146 stream_putc(s, CAPABILITY_CODE_MP_LEN);
1147 stream_putw(s, pkt_afi);
1148 stream_putc(s, 0);
1149 stream_putc(s, pkt_safi);
1150
1151 if (bgp_debug_neighbor_events(peer))
1152 zlog_debug(
f70c91dc
DA
1153 "%pBP sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
1154 peer,
d62a17ae 1155 action == CAPABILITY_ACTION_SET ? "Advertising"
1156 : "Removing",
748a041f 1157 iana_afi2str(pkt_afi), iana_safi2str(pkt_safi));
d62a17ae 1158 }
1159
1160 /* Set packet size. */
65baedca 1161 bgp_packet_set_size(s);
718e3744 1162
d62a17ae 1163 /* Add packet to the peer. */
1164 bgp_packet_add(peer, s);
424ab01d
QY
1165
1166 bgp_writes_on(peer);
d62a17ae 1167}
718e3744 1168
d62a17ae 1169/* RFC1771 6.8 Connection collision detection. */
1170static int bgp_collision_detect(struct peer *new, struct in_addr remote_id)
1171{
1172 struct peer *peer;
1173
f88221f3
DS
1174 /*
1175 * Upon receipt of an OPEN message, the local system must examine
1176 * all of its connections that are in the OpenConfirm state. A BGP
1177 * speaker may also examine connections in an OpenSent state if it
1178 * knows the BGP Identifier of the peer by means outside of the
1179 * protocol. If among these connections there is a connection to a
1180 * remote BGP speaker whose BGP Identifier equals the one in the
1181 * OPEN message, then the local system performs the following
1182 * collision resolution procedure:
1183 */
1184 peer = new->doppelganger;
1185 if (peer == NULL)
1186 return 0;
1187
1188 /*
1189 * Do not accept the new connection in Established or Clearing
1190 * states. Note that a peer GR is handled by closing the existing
1191 * connection upon receipt of new one.
1192 */
feb17238 1193 if (peer_established(peer) || peer->status == Clearing) {
f88221f3
DS
1194 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1195 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1196 return -1;
1197 }
1198
1199 if ((peer->status != OpenConfirm) && (peer->status != OpenSent))
1200 return 0;
1201
1202 /*
1203 * 1. The BGP Identifier of the local system is
1204 * compared to the BGP Identifier of the remote
1205 * system (as specified in the OPEN message).
1206 *
1207 * If the BGP Identifiers of the peers
1208 * involved in the connection collision
1209 * are identical, then the connection
1210 * initiated by the BGP speaker with the
1211 * larger AS number is preserved.
1212 */
1213 if (ntohl(peer->local_id.s_addr) < ntohl(remote_id.s_addr)
1214 || (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)
1215 && peer->local_as < peer->as))
1216 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
1217 /*
1218 * 2. If the value of the local BGP
1219 * Identifier is less than the remote one,
1220 * the local system closes BGP connection
1221 * that already exists (the one that is
1222 * already in the OpenConfirm state),
1223 * and accepts BGP connection initiated by
1224 * the remote system.
1225 */
1226 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
1227 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1228 return 1;
1229 } else {
1230 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1231 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1232 return -1;
1233 }
1234 else {
1235 if (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)
1236 && peer->local_as == peer->as)
1237 flog_err(EC_BGP_ROUTER_ID_SAME,
1238 "Peer's router-id %pI4 is the same as ours",
1239 &remote_id);
1240
1241 /*
1242 * 3. Otherwise, the local system closes newly
1243 * created BGP connection (the one associated with the
1244 * newly received OPEN message), and continues to use
1245 * the existing one (the one that is already in the
1246 * OpenConfirm state).
d62a17ae 1247 */
f88221f3
DS
1248 if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
1249 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
1250 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1251 return 1;
1252 } else {
d62a17ae 1253 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1254 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
95f7965d 1255 return -1;
d62a17ae 1256 }
1257 }
d62a17ae 1258}
718e3744 1259
d8151687
QY
1260/* Packet processing routines ---------------------------------------------- */
1261/*
1262 * This is a family of functions designed to be called from
1263 * bgp_process_packet(). These functions all share similar behavior and should
1264 * adhere to the following invariants and restrictions:
1265 *
1266 * Return codes
1267 * ------------
1268 * The return code of any one of those functions should be one of the FSM event
1269 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1270 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1271 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1272 * error and Receive_OPEN_message otherwise.
1273 *
1274 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1275 * defined below.
1276 *
1277 * Side effects
1278 * ------------
1279 * - May send NOTIFY messages
1280 * - May not modify peer->status
1281 * - May not call bgp_event_update()
1282 */
1283
1284#define BGP_PACKET_NOOP 0
1285
1286/**
1287 * Process BGP OPEN message for peer.
1288 *
1289 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1290 * and returns BGP_Stop.
1291 *
1292 * @param peer
1293 * @param size size of the packet
1294 * @return as in summary
1295 */
d62a17ae 1296static int bgp_open_receive(struct peer *peer, bgp_size_t size)
1297{
1298 int ret;
d7c0a89a 1299 uint8_t version;
d08c0c80 1300 uint16_t optlen;
d7c0a89a
QY
1301 uint16_t holdtime;
1302 uint16_t send_holdtime;
d62a17ae 1303 as_t remote_as;
6dcef54c 1304 as_t as4 = 0, as4_be;
d62a17ae 1305 struct in_addr remote_id;
1306 int mp_capability;
d7c0a89a
QY
1307 uint8_t notify_data_remote_as[2];
1308 uint8_t notify_data_remote_as4[4];
1309 uint8_t notify_data_remote_id[4];
1310 uint16_t *holdtime_ptr;
d62a17ae 1311
1312 /* Parse open packet. */
424ab01d
QY
1313 version = stream_getc(peer->curr);
1314 memcpy(notify_data_remote_as, stream_pnt(peer->curr), 2);
1315 remote_as = stream_getw(peer->curr);
d7c0a89a 1316 holdtime_ptr = (uint16_t *)stream_pnt(peer->curr);
424ab01d
QY
1317 holdtime = stream_getw(peer->curr);
1318 memcpy(notify_data_remote_id, stream_pnt(peer->curr), 4);
1319 remote_id.s_addr = stream_get_ipv4(peer->curr);
d62a17ae 1320
d62a17ae 1321 /* BEGIN to read the capability here, but dont do it yet */
1322 mp_capability = 0;
424ab01d 1323 optlen = stream_getc(peer->curr);
d62a17ae 1324
d08c0c80
DA
1325 /* Extended Optional Parameters Length for BGP OPEN Message */
1326 if (optlen == BGP_OPEN_NON_EXT_OPT_LEN
1327 || CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
1328 uint8_t opttype;
1329
1330 opttype = stream_getc(peer->curr);
1331 if (opttype == BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH) {
1332 optlen = stream_getw(peer->curr);
1333 SET_FLAG(peer->sflags,
1334 PEER_STATUS_EXT_OPT_PARAMS_LENGTH);
1335 }
1336 }
1337
1338 /* Receive OPEN message log */
1339 if (bgp_debug_neighbor_events(peer))
1340 zlog_debug(
1341 "%s rcv OPEN%s, version %d, remote-as (in open) %u, holdtime %d, id %pI4",
1342 peer->host,
1343 CHECK_FLAG(peer->sflags,
1344 PEER_STATUS_EXT_OPT_PARAMS_LENGTH)
1345 ? " (Extended)"
1346 : "",
1347 version, remote_as, holdtime, &remote_id);
1348
d62a17ae 1349 if (optlen != 0) {
1350 /* If not enough bytes, it is an error. */
424ab01d 1351 if (STREAM_READABLE(peer->curr) < optlen) {
d08c0c80
DA
1352 flog_err(EC_BGP_PKT_OPEN,
1353 "%s: stream has not enough bytes (%u)",
1354 peer->host, optlen);
d62a17ae 1355 bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR,
1356 BGP_NOTIFY_OPEN_MALFORMED_ATTR);
d8151687 1357 return BGP_Stop;
d62a17ae 1358 }
718e3744 1359
d62a17ae 1360 /* We need the as4 capability value *right now* because
1361 * if it is there, we have not got the remote_as yet, and
1362 * without
1363 * that we do not know which peer is connecting to us now.
1364 */
1365 as4 = peek_for_as4_capability(peer, optlen);
d62a17ae 1366 }
718e3744 1367
6dcef54c
DL
1368 as4_be = htonl(as4);
1369 memcpy(notify_data_remote_as4, &as4_be, 4);
1370
d62a17ae 1371 /* Just in case we have a silly peer who sends AS4 capability set to 0
1372 */
1373 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) && !as4) {
e50f7cfd 1374 flog_err(EC_BGP_PKT_OPEN,
1c50c1c0
QY
1375 "%s bad OPEN, got AS4 capability, but AS4 set to 0",
1376 peer->host);
d62a17ae 1377 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1378 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1379 notify_data_remote_as4, 4);
d8151687 1380 return BGP_Stop;
d62a17ae 1381 }
718e3744 1382
33d022bc
DA
1383 /* Codification of AS 0 Processing */
1384 if (remote_as == BGP_AS_ZERO) {
1385 flog_err(EC_BGP_PKT_OPEN, "%s bad OPEN, got AS set to 0",
1386 peer->host);
1387 bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR,
1388 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1389 return BGP_Stop;
1390 }
1391
d62a17ae 1392 if (remote_as == BGP_AS_TRANS) {
1393 /* Take the AS4 from the capability. We must have received the
1394 * capability now! Otherwise we have a asn16 peer who uses
1395 * BGP_AS_TRANS, for some unknown reason.
1396 */
1397 if (as4 == BGP_AS_TRANS) {
af4c2728 1398 flog_err(
e50f7cfd 1399 EC_BGP_PKT_OPEN,
d62a17ae 1400 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1401 peer->host);
1402 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1403 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1404 notify_data_remote_as4, 4);
d8151687 1405 return BGP_Stop;
d62a17ae 1406 }
718e3744 1407
d62a17ae 1408 if (!as4 && BGP_DEBUG(as4, AS4))
1409 zlog_debug(
3efd0893 1410 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.",
d62a17ae 1411 peer->host);
1412 else if (as4 < BGP_AS_MAX && BGP_DEBUG(as4, AS4))
1413 zlog_debug(
3efd0893 1414 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.",
d62a17ae 1415 peer->host, as4);
1416 if (as4)
1417 remote_as = as4;
1418 } else {
1419 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1420 */
1421 /* If we have got the capability, peer->as4cap must match
1422 * remote_as */
1423 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV)
1424 && as4 != remote_as) {
1425 /* raise error, log this, close session */
af4c2728 1426 flog_err(
e50f7cfd 1427 EC_BGP_PKT_OPEN,
3efd0893 1428 "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
d62a17ae 1429 peer->host, as4, remote_as);
1430 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1431 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1432 notify_data_remote_as4, 4);
d8151687 1433 return BGP_Stop;
d62a17ae 1434 }
1435 }
6b0655a2 1436
787c3020
DA
1437 /* rfc6286:
1438 * If the BGP Identifier field of the OPEN message
1439 * is zero, or if it is the same as the BGP Identifier
1440 * of the local BGP speaker and the message is from an
1441 * internal peer, then the Error Subcode is set to
1442 * "Bad BGP Identifier".
1443 */
975a328e 1444 if (remote_id.s_addr == INADDR_ANY
787c3020
DA
1445 || (peer->sort == BGP_PEER_IBGP
1446 && ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr))) {
d62a17ae 1447 if (bgp_debug_neighbor_events(peer))
23d0a753
DA
1448 zlog_debug("%s bad OPEN, wrong router identifier %pI4",
1449 peer->host, &remote_id);
d62a17ae 1450 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1451 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1452 notify_data_remote_id, 4);
d8151687 1453 return BGP_Stop;
d62a17ae 1454 }
1455
d62a17ae 1456 /* Peer BGP version check. */
1457 if (version != BGP_VERSION_4) {
d7c0a89a 1458 uint16_t maxver = htons(BGP_VERSION_4);
d62a17ae 1459 /* XXX this reply may not be correct if version < 4 XXX */
1460 if (bgp_debug_neighbor_events(peer))
1461 zlog_debug(
1462 "%s bad protocol version, remote requested %d, local request %d",
1463 peer->host, version, BGP_VERSION_4);
1464 /* Data must be in network byte order here */
1465 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1466 BGP_NOTIFY_OPEN_UNSUP_VERSION,
d7c0a89a 1467 (uint8_t *)&maxver, 2);
d8151687 1468 return BGP_Stop;
d62a17ae 1469 }
1470
1471 /* Check neighbor as number. */
1472 if (peer->as_type == AS_UNSPECIFIED) {
1473 if (bgp_debug_neighbor_events(peer))
1474 zlog_debug(
1475 "%s bad OPEN, remote AS is unspecified currently",
1476 peer->host);
1477 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1478 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1479 notify_data_remote_as, 2);
d8151687 1480 return BGP_Stop;
d62a17ae 1481 } else if (peer->as_type == AS_INTERNAL) {
1482 if (remote_as != peer->bgp->as) {
1483 if (bgp_debug_neighbor_events(peer))
1484 zlog_debug(
1485 "%s bad OPEN, remote AS is %u, internal specified",
1486 peer->host, remote_as);
1487 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1488 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1489 notify_data_remote_as, 2);
d8151687 1490 return BGP_Stop;
1ff9a340 1491 }
d62a17ae 1492 peer->as = peer->local_as;
1493 } else if (peer->as_type == AS_EXTERNAL) {
1494 if (remote_as == peer->bgp->as) {
1495 if (bgp_debug_neighbor_events(peer))
1496 zlog_debug(
1497 "%s bad OPEN, remote AS is %u, external specified",
1498 peer->host, remote_as);
1499 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1500 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1501 notify_data_remote_as, 2);
d8151687 1502 return BGP_Stop;
1ff9a340 1503 }
d62a17ae 1504 peer->as = remote_as;
1505 } else if ((peer->as_type == AS_SPECIFIED) && (remote_as != peer->as)) {
1506 if (bgp_debug_neighbor_events(peer))
1507 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1508 peer->host, remote_as, peer->as);
1509 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1510 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1511 notify_data_remote_as, 2);
d8151687 1512 return BGP_Stop;
eb821189 1513 }
718e3744 1514
7a75470f
DS
1515 /*
1516 * When collision is detected and this peer is closed.
1517 * Return immediately.
1518 */
1519 ret = bgp_collision_detect(peer, remote_id);
1520 if (ret < 0)
1521 return BGP_Stop;
1522
1523 /* Get sockname. */
1524 if (bgp_getsockname(peer) < 0) {
1525 flog_err_sys(EC_LIB_SOCKET,
1526 "%s: bgp_getsockname() failed for peer: %s",
1527 __func__, peer->host);
1528 return BGP_Stop;
1529 }
1530
1531 /* Set remote router-id */
1532 peer->remote_id = remote_id;
1533
d62a17ae 1534 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1535 calculate the value of the Hold Timer by using the smaller of its
1536 configured Hold Time and the Hold Time received in the OPEN message.
1537 The Hold Time MUST be either zero or at least three seconds. An
1538 implementation may reject connections on the basis of the Hold Time.
0b2aa3a0 1539 */
d62a17ae 1540
1541 if (holdtime < 3 && holdtime != 0) {
b042667a
TI
1542 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1543 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
1544 (uint8_t *)holdtime_ptr, 2);
1545 return BGP_Stop;
1546 }
1547
1548 /* Send notification message when Hold Time received in the OPEN message
1549 * is smaller than configured minimum Hold Time. */
1550 if (holdtime < peer->bgp->default_min_holdtime
1551 && peer->bgp->default_min_holdtime != 0) {
d62a17ae 1552 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1553 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
d7c0a89a 1554 (uint8_t *)holdtime_ptr, 2);
d8151687 1555 return BGP_Stop;
0b2aa3a0 1556 }
d62a17ae 1557
1558 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1559 would be one third of the Hold Time interval. KEEPALIVE messages
1560 MUST NOT be sent more frequently than one per second. An
1561 implementation MAY adjust the rate at which it sends KEEPALIVE
1562 messages as a function of the Hold Time interval. */
1563
b90a8e13 1564 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
d62a17ae 1565 send_holdtime = peer->holdtime;
1566 else
1567 send_holdtime = peer->bgp->default_holdtime;
1568
1569 if (holdtime < send_holdtime)
1570 peer->v_holdtime = holdtime;
1571 else
1572 peer->v_holdtime = send_holdtime;
1573
7aa4fd5b
TA
1574 /* Set effective keepalive to 1/3 the effective holdtime.
1575 * Use configured keeplive when < effective keepalive.
1576 */
1577 peer->v_keepalive = peer->v_holdtime / 3;
1578 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) {
1579 if (peer->keepalive && peer->keepalive < peer->v_keepalive)
1580 peer->v_keepalive = peer->keepalive;
1581 } else {
1582 if (peer->bgp->default_keepalive
1583 && peer->bgp->default_keepalive < peer->v_keepalive)
1584 peer->v_keepalive = peer->bgp->default_keepalive;
1585 }
d62a17ae 1586
1587 /* Open option part parse. */
1588 if (optlen != 0) {
1bb379bf 1589 if (bgp_open_option_parse(peer, optlen, &mp_capability) < 0)
d8151687 1590 return BGP_Stop;
d62a17ae 1591 } else {
1592 if (bgp_debug_neighbor_events(peer))
1593 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1594 peer->host);
0299c004 1595 }
d62a17ae 1596
1597 /*
1598 * Assume that the peer supports the locally configured set of
1599 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1600 * capabilities, or if 'override-capability' is configured.
1601 */
1602 if (!mp_capability
1603 || CHECK_FLAG(peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) {
1604 peer->afc_nego[AFI_IP][SAFI_UNICAST] =
1605 peer->afc[AFI_IP][SAFI_UNICAST];
1606 peer->afc_nego[AFI_IP][SAFI_MULTICAST] =
1607 peer->afc[AFI_IP][SAFI_MULTICAST];
1608 peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] =
1609 peer->afc[AFI_IP][SAFI_LABELED_UNICAST];
7c40bf39 1610 peer->afc_nego[AFI_IP][SAFI_FLOWSPEC] =
1611 peer->afc[AFI_IP][SAFI_FLOWSPEC];
d62a17ae 1612 peer->afc_nego[AFI_IP6][SAFI_UNICAST] =
1613 peer->afc[AFI_IP6][SAFI_UNICAST];
1614 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] =
1615 peer->afc[AFI_IP6][SAFI_MULTICAST];
1616 peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] =
1617 peer->afc[AFI_IP6][SAFI_LABELED_UNICAST];
1618 peer->afc_nego[AFI_L2VPN][SAFI_EVPN] =
1619 peer->afc[AFI_L2VPN][SAFI_EVPN];
7c40bf39 1620 peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC] =
1621 peer->afc[AFI_IP6][SAFI_FLOWSPEC];
0299c004 1622 }
d62a17ae 1623
d62a17ae 1624 /* Verify valid local address present based on negotiated
1625 * address-families. */
1626 if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
e5f22b30 1627 || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST]
d62a17ae 1628 || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
1629 || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
1630 || peer->afc_nego[AFI_IP][SAFI_ENCAP]) {
975a328e 1631 if (peer->nexthop.v4.s_addr == INADDR_ANY) {
d62a17ae 1632#if defined(HAVE_CUMULUS)
50121ac0
DS
1633 zlog_warn("%s: No local IPv4 addr, BGP routing may not work",
1634 peer->host);
1d808091 1635#endif
d62a17ae 1636 }
1637 }
1638 if (peer->afc_nego[AFI_IP6][SAFI_UNICAST]
e5f22b30 1639 || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST]
d62a17ae 1640 || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
1641 || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
1642 || peer->afc_nego[AFI_IP6][SAFI_ENCAP]) {
1643 if (IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_global)) {
1644#if defined(HAVE_CUMULUS)
50121ac0
DS
1645 zlog_warn("%s: No local IPv6 address, BGP routing may not work",
1646 peer->host);
1d808091 1647#endif
d62a17ae 1648 }
1649 }
1650 peer->rtt = sockopt_tcp_rtt(peer->fd);
1651
d8151687 1652 return Receive_OPEN_message;
718e3744 1653}
1654
d8151687
QY
1655/**
1656 * Process BGP KEEPALIVE message for peer.
1657 *
1658 * @param peer
1659 * @param size size of the packet
1660 * @return as in summary
1661 */
1662static int bgp_keepalive_receive(struct peer *peer, bgp_size_t size)
f188f2c4 1663{
d8151687
QY
1664 if (bgp_debug_keepalive(peer))
1665 zlog_debug("%s KEEPALIVE rcvd", peer->host);
d62a17ae 1666
d8151687 1667 bgp_update_implicit_eors(peer);
d62a17ae 1668
e410d563
DA
1669 peer->rtt = sockopt_tcp_rtt(peer->fd);
1670
8336c896
DA
1671 /* If the peer's RTT is higher than expected, shutdown
1672 * the peer automatically.
1673 */
1674 if (CHECK_FLAG(peer->flags, PEER_FLAG_RTT_SHUTDOWN)
1675 && peer->rtt > peer->rtt_expected) {
1676
1677 peer->rtt_keepalive_rcv++;
1678
1679 if (peer->rtt_keepalive_rcv > peer->rtt_keepalive_conf) {
1680 zlog_warn(
1681 "%s shutdown due to high round-trip-time (%dms > %dms)",
1682 peer->host, peer->rtt, peer->rtt_expected);
1683 peer_flag_set(peer, PEER_FLAG_SHUTDOWN);
1684 }
1685 } else {
1686 if (peer->rtt_keepalive_rcv)
1687 peer->rtt_keepalive_rcv--;
1688 }
1689
d8151687 1690 return Receive_KEEPALIVE_message;
f188f2c4
DS
1691}
1692
cc9f21da 1693static void bgp_refresh_stalepath_timer_expire(struct thread *thread)
9af52ccf
DA
1694{
1695 struct peer_af *paf;
1696
1697 paf = THREAD_ARG(thread);
1698
1699 afi_t afi = paf->afi;
1700 safi_t safi = paf->safi;
1701 struct peer *peer = paf->peer;
1702
1703 peer->t_refresh_stalepath = NULL;
1704
1705 if (peer->nsf[afi][safi])
1706 bgp_clear_stale_route(peer, afi, safi);
1707
1708 if (bgp_debug_neighbor_events(peer))
a7d91a8c 1709 zlog_debug(
f70c91dc
DA
1710 "%pBP route-refresh (BoRR) timer expired for afi/safi: %d/%d",
1711 peer, afi, safi);
9af52ccf
DA
1712
1713 bgp_timer_set(peer);
9af52ccf 1714}
d62a17ae 1715
d8151687
QY
1716/**
1717 * Process BGP UPDATE message for peer.
1718 *
1719 * Parses UPDATE and creates attribute object.
1720 *
1721 * @param peer
1722 * @param size size of the packet
1723 * @return as in summary
7ef5a232 1724 */
d62a17ae 1725static int bgp_update_receive(struct peer *peer, bgp_size_t size)
718e3744 1726{
d62a17ae 1727 int ret, nlri_ret;
d7c0a89a 1728 uint8_t *end;
d62a17ae 1729 struct stream *s;
1730 struct attr attr;
1731 bgp_size_t attribute_len;
1732 bgp_size_t update_len;
1733 bgp_size_t withdraw_len;
f009ff26 1734 bool restart = false;
d62a17ae 1735
1736 enum NLRI_TYPES {
1737 NLRI_UPDATE,
1738 NLRI_WITHDRAW,
1739 NLRI_MP_UPDATE,
1740 NLRI_MP_WITHDRAW,
1741 NLRI_TYPE_MAX
1742 };
1743 struct bgp_nlri nlris[NLRI_TYPE_MAX];
1744
1745 /* Status must be Established. */
feb17238 1746 if (!peer_established(peer)) {
e50f7cfd 1747 flog_err(EC_BGP_INVALID_STATUS,
1c50c1c0
QY
1748 "%s [FSM] Update packet received under status %s",
1749 peer->host,
1750 lookup_msg(bgp_status_msg, peer->status, NULL));
0e35025e 1751 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
3893aeee 1752 bgp_fsm_error_subcode(peer->status));
d8151687 1753 return BGP_Stop;
d62a17ae 1754 }
1755
1756 /* Set initial values. */
6006b807 1757 memset(&attr, 0, sizeof(attr));
d62a17ae 1758 attr.label_index = BGP_INVALID_LABEL_INDEX;
1759 attr.label = MPLS_INVALID_LABEL;
1760 memset(&nlris, 0, sizeof(nlris));
1761 memset(peer->rcvd_attr_str, 0, BUFSIZ);
1762 peer->rcvd_attr_printed = 0;
1763
424ab01d 1764 s = peer->curr;
d62a17ae 1765 end = stream_pnt(s) + size;
1766
1767 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1768 Length is too large (i.e., if Unfeasible Routes Length + Total
1769 Attribute Length + 23 exceeds the message Length), then the Error
1770 Subcode is set to Malformed Attribute List. */
1771 if (stream_pnt(s) + 2 > end) {
e50f7cfd 1772 flog_err(EC_BGP_UPDATE_RCV,
3efd0893 1773 "%s [Error] Update packet error (packet length is short for unfeasible length)",
1c50c1c0 1774 peer->host);
d62a17ae 1775 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1776 BGP_NOTIFY_UPDATE_MAL_ATTR);
d8151687 1777 return BGP_Stop;
d62a17ae 1778 }
1779
1780 /* Unfeasible Route Length. */
1781 withdraw_len = stream_getw(s);
1782
1783 /* Unfeasible Route Length check. */
1784 if (stream_pnt(s) + withdraw_len > end) {
e50f7cfd 1785 flog_err(EC_BGP_UPDATE_RCV,
3efd0893 1786 "%s [Error] Update packet error (packet unfeasible length overflow %d)",
1c50c1c0 1787 peer->host, withdraw_len);
d62a17ae 1788 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1789 BGP_NOTIFY_UPDATE_MAL_ATTR);
d8151687 1790 return BGP_Stop;
d62a17ae 1791 }
1792
1793 /* Unfeasible Route packet format check. */
1794 if (withdraw_len > 0) {
1795 nlris[NLRI_WITHDRAW].afi = AFI_IP;
1796 nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST;
1797 nlris[NLRI_WITHDRAW].nlri = stream_pnt(s);
1798 nlris[NLRI_WITHDRAW].length = withdraw_len;
1799 stream_forward_getp(s, withdraw_len);
1800 }
1801
1802 /* Attribute total length check. */
1803 if (stream_pnt(s) + 2 > end) {
ade6974d 1804 flog_warn(
e50f7cfd 1805 EC_BGP_UPDATE_PACKET_SHORT,
ade6974d
QY
1806 "%s [Error] Packet Error (update packet is short for attribute length)",
1807 peer->host);
d62a17ae 1808 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1809 BGP_NOTIFY_UPDATE_MAL_ATTR);
d8151687 1810 return BGP_Stop;
d62a17ae 1811 }
1812
1813 /* Fetch attribute total length. */
1814 attribute_len = stream_getw(s);
1815
1816 /* Attribute length check. */
1817 if (stream_pnt(s) + attribute_len > end) {
ade6974d 1818 flog_warn(
e50f7cfd 1819 EC_BGP_UPDATE_PACKET_LONG,
ade6974d
QY
1820 "%s [Error] Packet Error (update packet attribute length overflow %d)",
1821 peer->host, attribute_len);
d62a17ae 1822 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1823 BGP_NOTIFY_UPDATE_MAL_ATTR);
d8151687 1824 return BGP_Stop;
d62a17ae 1825 }
1826
1827 /* Certain attribute parsing errors should not be considered bad enough
1828 * to reset the session for, most particularly any partial/optional
1829 * attributes that have 'tunneled' over speakers that don't understand
1830 * them. Instead we withdraw only the prefix concerned.
1831 *
1832 * Complicates the flow a little though..
1833 */
79288e4c 1834 enum bgp_attr_parse_ret attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
d62a17ae 1835/* This define morphs the update case into a withdraw when lower levels
1836 * have signalled an error condition where this is best.
1837 */
b881c707 1838#define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
718e3744 1839
d62a17ae 1840 /* Parse attribute when it exists. */
1841 if (attribute_len) {
1842 attr_parse_ret = bgp_attr_parse(peer, &attr, attribute_len,
1843 &nlris[NLRI_MP_UPDATE],
1844 &nlris[NLRI_MP_WITHDRAW]);
1845 if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) {
1846 bgp_attr_unintern_sub(&attr);
d8151687 1847 return BGP_Stop;
d62a17ae 1848 }
1849 }
1850
1851 /* Logging the attribute. */
1852 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
1853 || BGP_DEBUG(update, UPDATE_IN)
1854 || BGP_DEBUG(update, UPDATE_PREFIX)) {
5022c833
DA
1855 ret = bgp_dump_attr(&attr, peer->rcvd_attr_str,
1856 sizeof(peer->rcvd_attr_str));
d62a17ae 1857
b4d46cc9
DL
1858 peer->stat_upd_7606++;
1859
d62a17ae 1860 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
af4c2728 1861 flog_err(
e50f7cfd 1862 EC_BGP_UPDATE_RCV,
f70c91dc
DA
1863 "%pBP rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1864 peer);
d62a17ae 1865
1866 if (ret && bgp_debug_update(peer, NULL, NULL, 1)) {
f70c91dc 1867 zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer,
d62a17ae 1868 peer->rcvd_attr_str);
1869 peer->rcvd_attr_printed = 1;
1870 }
1871 }
1872
1873 /* Network Layer Reachability Information. */
1874 update_len = end - stream_pnt(s);
1875
1876 if (update_len) {
1877 /* Set NLRI portion to structure. */
1878 nlris[NLRI_UPDATE].afi = AFI_IP;
1879 nlris[NLRI_UPDATE].safi = SAFI_UNICAST;
1880 nlris[NLRI_UPDATE].nlri = stream_pnt(s);
1881 nlris[NLRI_UPDATE].length = update_len;
1882 stream_forward_getp(s, update_len);
9738e9aa 1883
1884 if (CHECK_FLAG(attr.flag, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI))) {
1885 /*
1886 * We skipped nexthop attribute validation earlier so
1887 * validate the nexthop now.
1888 */
1889 if (bgp_attr_nexthop_valid(peer, &attr) < 0) {
1890 bgp_attr_unintern_sub(&attr);
1891 return BGP_Stop;
1892 }
1893 }
d62a17ae 1894 }
1895
1896 if (BGP_DEBUG(update, UPDATE_IN))
f70c91dc
DA
1897 zlog_debug("%pBP rcvd UPDATE wlen %d attrlen %d alen %d", peer,
1898 withdraw_len, attribute_len, update_len);
d62a17ae 1899
1900 /* Parse any given NLRIs */
1901 for (int i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++) {
1902 if (!nlris[i].nlri)
1903 continue;
1904
1905 /* NLRI is processed iff the peer if configured for the specific
1906 * afi/safi */
1907 if (!peer->afc[nlris[i].afi][nlris[i].safi]) {
1908 zlog_info(
1909 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1910 peer->host, nlris[i].afi, nlris[i].safi);
1911 continue;
1912 }
1913
1914 /* EoR handled later */
1915 if (nlris[i].length == 0)
1916 continue;
1917
1918 switch (i) {
1919 case NLRI_UPDATE:
1920 case NLRI_MP_UPDATE:
1921 nlri_ret = bgp_nlri_parse(peer, NLRI_ATTR_ARG,
1922 &nlris[i], 0);
1923 break;
1924 case NLRI_WITHDRAW:
1925 case NLRI_MP_WITHDRAW:
1926 nlri_ret = bgp_nlri_parse(peer, &attr, &nlris[i], 1);
1927 break;
1928 default:
513386b5 1929 nlri_ret = BGP_NLRI_PARSE_ERROR;
d62a17ae 1930 }
1931
513386b5
DA
1932 if (nlri_ret < BGP_NLRI_PARSE_OK
1933 && nlri_ret != BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW) {
e50f7cfd 1934 flog_err(EC_BGP_UPDATE_RCV,
1c50c1c0 1935 "%s [Error] Error parsing NLRI", peer->host);
feb17238 1936 if (peer_established(peer))
d62a17ae 1937 bgp_notify_send(
1938 peer, BGP_NOTIFY_UPDATE_ERR,
1939 i <= NLRI_WITHDRAW
1940 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1941 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR);
1942 bgp_attr_unintern_sub(&attr);
d8151687 1943 return BGP_Stop;
d62a17ae 1944 }
1945 }
1946
1947 /* EoR checks
1948 *
1949 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1950 * and MP EoR should have only an empty MP_UNREACH
1951 */
996c9314
LB
1952 if ((!update_len && !withdraw_len && nlris[NLRI_MP_UPDATE].length == 0)
1953 || (attr_parse_ret == BGP_ATTR_PARSE_EOR)) {
d62a17ae 1954 afi_t afi = 0;
1955 safi_t safi;
f009ff26 1956 struct graceful_restart_info *gr_info;
1957
1958 /* Restarting router */
36235319
QY
1959 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)
1960 && BGP_PEER_RESTARTING_MODE(peer))
f009ff26 1961 restart = true;
d62a17ae 1962
1963 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1964 * checked
1965 * update and withdraw NLRI lengths are 0.
1966 */
1967 if (!attribute_len) {
1968 afi = AFI_IP;
1969 safi = SAFI_UNICAST;
1970 } else if (attr.flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI)
1971 && nlris[NLRI_MP_WITHDRAW].length == 0) {
1972 afi = nlris[NLRI_MP_WITHDRAW].afi;
1973 safi = nlris[NLRI_MP_WITHDRAW].safi;
9b9df989
DS
1974 } else if (attr_parse_ret == BGP_ATTR_PARSE_EOR) {
1975 afi = nlris[NLRI_MP_UPDATE].afi;
1976 safi = nlris[NLRI_MP_UPDATE].safi;
d62a17ae 1977 }
1978
1979 if (afi && peer->afc[afi][safi]) {
e82d19a3
DS
1980 struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
1981
d62a17ae 1982 /* End-of-RIB received */
1983 if (!CHECK_FLAG(peer->af_sflags[afi][safi],
1984 PEER_STATUS_EOR_RECEIVED)) {
1985 SET_FLAG(peer->af_sflags[afi][safi],
1986 PEER_STATUS_EOR_RECEIVED);
1987 bgp_update_explicit_eors(peer);
f009ff26 1988 /* Update graceful restart information */
1989 gr_info = &(peer->bgp->gr_info[afi][safi]);
1990 if (restart)
1991 gr_info->eor_received++;
1992 /* If EOR received from all peers and selection
1993 * deferral timer is running, cancel the timer
1994 * and invoke the best path calculation
1995 */
36235319
QY
1996 if (gr_info->eor_required
1997 == gr_info->eor_received) {
1998 if (bgp_debug_neighbor_events(peer))
1999 zlog_debug(
2000 "%s %d, %s %d",
f009ff26 2001 "EOR REQ",
2002 gr_info->eor_required,
2003 "EOR RCV",
2004 gr_info->eor_received);
fa5806c3 2005 THREAD_OFF(gr_info->t_select_deferral);
f009ff26 2006 gr_info->eor_required = 0;
2007 gr_info->eor_received = 0;
2008 /* Best path selection */
42c93837
DA
2009 bgp_best_path_select_defer(peer->bgp,
2010 afi, safi);
f009ff26 2011 }
d62a17ae 2012 }
2013
2014 /* NSF delete stale route */
2015 if (peer->nsf[afi][safi])
2016 bgp_clear_stale_route(peer, afi, safi);
2017
1479ed2f
DA
2018 zlog_info(
2019 "%s: rcvd End-of-RIB for %s from %s in vrf %s",
2020 __func__, get_afi_safi_str(afi, safi, false),
2021 peer->host, vrf ? vrf->name : VRF_DEFAULT_NAME);
2022 }
f80f838b 2023 }
d62a17ae 2024
2025 /* Everything is done. We unintern temporary structures which
2026 interned in bgp_attr_parse(). */
2027 bgp_attr_unintern_sub(&attr);
2028
d62a17ae 2029 peer->update_time = bgp_clock();
2030
c385f82a
MK
2031 /* Notify BGP Conditional advertisement scanner process */
2032 peer->advmap_table_change = true;
2033
d8151687 2034 return Receive_UPDATE_message;
718e3744 2035}
2036
d8151687
QY
2037/**
2038 * Process BGP NOTIFY message for peer.
2039 *
2040 * @param peer
2041 * @param size size of the packet
2042 * @return as in summary
2043 */
2044static int bgp_notify_receive(struct peer *peer, bgp_size_t size)
718e3744 2045{
10d476d4
DA
2046 struct bgp_notify outer = {};
2047 struct bgp_notify inner = {};
eea685b6 2048 bool hard_reset = false;
d62a17ae 2049
2050 if (peer->notify.data) {
eea685b6 2051 XFREE(MTYPE_BGP_NOTIFICATION, peer->notify.data);
d62a17ae 2052 peer->notify.length = 0;
e7ce634f 2053 peer->notify.hard_reset = false;
d62a17ae 2054 }
2055
eea685b6
DA
2056 outer.code = stream_getc(peer->curr);
2057 outer.subcode = stream_getc(peer->curr);
2058 outer.length = size - 2;
2059 outer.data = NULL;
2060 outer.raw_data = NULL;
2061 if (outer.length) {
2062 outer.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, outer.length);
2063 memcpy(outer.raw_data, stream_pnt(peer->curr), outer.length);
2064 }
2065
1ae314be
DA
2066 hard_reset =
2067 bgp_notify_received_hard_reset(peer, outer.code, outer.subcode);
eea685b6
DA
2068 if (hard_reset && outer.length) {
2069 inner = bgp_notify_decapsulate_hard_reset(&outer);
2070 peer->notify.hard_reset = true;
2071 } else {
2072 inner = outer;
2073 }
d62a17ae 2074
2075 /* Preserv notify code and sub code. */
eea685b6
DA
2076 peer->notify.code = inner.code;
2077 peer->notify.subcode = inner.subcode;
d62a17ae 2078 /* For further diagnostic record returned Data. */
eea685b6
DA
2079 if (inner.length) {
2080 peer->notify.length = inner.length;
2081 peer->notify.data =
2082 XMALLOC(MTYPE_BGP_NOTIFICATION, inner.length);
2083 memcpy(peer->notify.data, inner.raw_data, inner.length);
d62a17ae 2084 }
2085
2086 /* For debug */
2087 {
2088 int i;
2089 int first = 0;
2090 char c[4];
2091
eea685b6
DA
2092 if (inner.length) {
2093 inner.data = XMALLOC(MTYPE_BGP_NOTIFICATION,
2094 inner.length * 3);
2095 for (i = 0; i < inner.length; i++)
d62a17ae 2096 if (first) {
552d6491 2097 snprintf(c, sizeof(c), " %02x",
424ab01d 2098 stream_getc(peer->curr));
f009ff26 2099
eea685b6
DA
2100 strlcat(inner.data, c,
2101 inner.length * 3);
f009ff26 2102
d62a17ae 2103 } else {
2104 first = 1;
552d6491
QY
2105 snprintf(c, sizeof(c), "%02x",
2106 stream_getc(peer->curr));
f009ff26 2107
eea685b6
DA
2108 strlcpy(inner.data, c,
2109 inner.length * 3);
d62a17ae 2110 }
d62a17ae 2111 }
2112
eea685b6 2113 bgp_notify_print(peer, &inner, "received", hard_reset);
10d476d4 2114 if (inner.length) {
eea685b6
DA
2115 XFREE(MTYPE_BGP_NOTIFICATION, inner.data);
2116 inner.length = 0;
2117 }
2118 if (outer.length) {
2119 XFREE(MTYPE_BGP_NOTIFICATION, outer.data);
10d476d4 2120 XFREE(MTYPE_BGP_NOTIFICATION, outer.raw_data);
c73d2363
DA
2121
2122 /* If this is a Hard Reset notification, we MUST free
2123 * the inner (encapsulated) notification too.
2124 */
2125 if (hard_reset)
2126 XFREE(MTYPE_BGP_NOTIFICATION, inner.raw_data);
eea685b6 2127 outer.length = 0;
d62a17ae 2128 }
2129 }
2130
2131 /* peer count update */
0112e9e0 2132 atomic_fetch_add_explicit(&peer->notify_in, 1, memory_order_relaxed);
d62a17ae 2133
2134 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
2135
2136 /* We have to check for Notify with Unsupported Optional Parameter.
2137 in that case we fallback to open without the capability option.
2138 But this done in bgp_stop. We just mark it here to avoid changing
2139 the fsm tables. */
eea685b6
DA
2140 if (inner.code == BGP_NOTIFY_OPEN_ERR &&
2141 inner.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM)
d62a17ae 2142 UNSET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
2143
20170775
DA
2144 /* If Graceful-Restart N-bit (Notification) is exchanged,
2145 * and it's not a Hard Reset, let's retain the routes.
2146 */
2147 if (bgp_has_graceful_restart_notification(peer) && !hard_reset &&
2148 CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE))
2149 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2150
5cce3f05 2151 bgp_peer_gr_flags_update(peer);
36235319
QY
2152 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
2153 peer->bgp->peer);
5cce3f05 2154
d8151687 2155 return Receive_NOTIFICATION_message;
718e3744 2156}
2157
d8151687
QY
2158/**
2159 * Process BGP ROUTEREFRESH message for peer.
2160 *
2161 * @param peer
2162 * @param size size of the packet
2163 * @return as in summary
2164 */
2165static int bgp_route_refresh_receive(struct peer *peer, bgp_size_t size)
718e3744 2166{
d62a17ae 2167 iana_afi_t pkt_afi;
2168 afi_t afi;
5c525538
RW
2169 iana_safi_t pkt_safi;
2170 safi_t safi;
d62a17ae 2171 struct stream *s;
2172 struct peer_af *paf;
2173 struct update_group *updgrp;
2174 struct peer *updgrp_peer;
9af52ccf 2175 uint8_t subtype;
e1a32ec1 2176 bool force_update = false;
9af52ccf
DA
2177 bgp_size_t msg_length =
2178 size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE);
d62a17ae 2179
2180 /* If peer does not have the capability, send notification. */
2181 if (!CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)) {
e50f7cfd 2182 flog_err(EC_BGP_NO_CAP,
1c50c1c0
QY
2183 "%s [Error] BGP route refresh is not enabled",
2184 peer->host);
d62a17ae 2185 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2186 BGP_NOTIFY_HEADER_BAD_MESTYPE);
d8151687 2187 return BGP_Stop;
d62a17ae 2188 }
2189
2190 /* Status must be Established. */
feb17238 2191 if (!peer_established(peer)) {
af4c2728 2192 flog_err(
e50f7cfd 2193 EC_BGP_INVALID_STATUS,
d62a17ae 2194 "%s [Error] Route refresh packet received under status %s",
2195 peer->host,
2196 lookup_msg(bgp_status_msg, peer->status, NULL));
0e35025e 2197 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
3893aeee 2198 bgp_fsm_error_subcode(peer->status));
d8151687 2199 return BGP_Stop;
d62a17ae 2200 }
2201
424ab01d 2202 s = peer->curr;
d62a17ae 2203
2204 /* Parse packet. */
2205 pkt_afi = stream_getw(s);
9af52ccf 2206 subtype = stream_getc(s);
d62a17ae 2207 pkt_safi = stream_getc(s);
2208
d62a17ae 2209 /* Convert AFI, SAFI to internal values and check. */
2210 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) {
2211 zlog_info(
748a041f
DS
2212 "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
2213 peer->host, iana_afi2str(pkt_afi),
2214 iana_safi2str(pkt_safi));
d8151687 2215 return BGP_PACKET_NOOP;
d62a17ae 2216 }
2217
2218 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) {
d7c0a89a
QY
2219 uint8_t *end;
2220 uint8_t when_to_refresh;
2221 uint8_t orf_type;
2222 uint16_t orf_len;
d62a17ae 2223
9af52ccf
DA
2224 if (subtype) {
2225 /* If the length, excluding the fixed-size message
2226 * header, of the received ROUTE-REFRESH message with
2227 * Message Subtype 1 and 2 is not 4, then the BGP
2228 * speaker MUST send a NOTIFICATION message with the
2229 * Error Code of "ROUTE-REFRESH Message Error" and the
2230 * subcode of "Invalid Message Length".
2231 */
2232 if (msg_length != 4) {
2233 zlog_err(
2234 "%s Enhanced Route Refresh message length error",
2235 peer->host);
2236 bgp_notify_send(
2237 peer, BGP_NOTIFY_ROUTE_REFRESH_ERR,
2238 BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN);
2239 }
2240
2241 /* When the BGP speaker receives a ROUTE-REFRESH message
2242 * with a "Message Subtype" field other than 0, 1, or 2,
2243 * it MUST ignore the received ROUTE-REFRESH message.
2244 */
2245 if (subtype > 2)
2246 zlog_err(
2247 "%s Enhanced Route Refresh invalid subtype",
2248 peer->host);
2249 }
2250
2251 if (msg_length < 5) {
d62a17ae 2252 zlog_info("%s ORF route refresh length error",
2253 peer->host);
0e35025e
DA
2254 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2255 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
d8151687 2256 return BGP_Stop;
718e3744 2257 }
2258
d62a17ae 2259 when_to_refresh = stream_getc(s);
2260 end = stream_pnt(s) + (size - 5);
2261
2262 while ((stream_pnt(s) + 2) < end) {
2263 orf_type = stream_getc(s);
2264 orf_len = stream_getw(s);
2265
2266 /* orf_len in bounds? */
2267 if ((stream_pnt(s) + orf_len) > end)
2268 break; /* XXX: Notify instead?? */
2269 if (orf_type == ORF_TYPE_PREFIX
2270 || orf_type == ORF_TYPE_PREFIX_OLD) {
2271 uint8_t *p_pnt = stream_pnt(s);
2272 uint8_t *p_end = stream_pnt(s) + orf_len;
2273 struct orf_prefix orfp;
d7c0a89a
QY
2274 uint8_t common = 0;
2275 uint32_t seq;
d62a17ae 2276 int psize;
2277 char name[BUFSIZ];
2278 int ret = CMD_SUCCESS;
2279
2280 if (bgp_debug_neighbor_events(peer)) {
2281 zlog_debug(
f70c91dc
DA
2282 "%pBP rcvd Prefixlist ORF(%d) length %d",
2283 peer, orf_type, orf_len);
d62a17ae 2284 }
2285
2286 /* we're going to read at least 1 byte of common
2287 * ORF header,
2288 * and 7 bytes of ORF Address-filter entry from
2289 * the stream
2290 */
2291 if (orf_len < 7)
2292 break;
2293
2294 /* ORF prefix-list name */
772270f3
QY
2295 snprintf(name, sizeof(name), "%s.%d.%d",
2296 peer->host, afi, safi);
d62a17ae 2297
2298 while (p_pnt < p_end) {
2299 /* If the ORF entry is malformed, want
2300 * to read as much of it
2301 * as possible without going beyond the
2302 * bounds of the entry,
2303 * to maximise debug information.
2304 */
2305 int ok;
6006b807 2306 memset(&orfp, 0, sizeof(orfp));
d62a17ae 2307 common = *p_pnt++;
2308 /* after ++: p_pnt <= p_end */
2309 if (common
2310 & ORF_COMMON_PART_REMOVE_ALL) {
2311 if (bgp_debug_neighbor_events(
2312 peer))
2313 zlog_debug(
f70c91dc
DA
2314 "%pBP rcvd Remove-All pfxlist ORF request",
2315 peer);
d62a17ae 2316 prefix_bgp_orf_remove_all(afi,
2317 name);
2318 break;
2319 }
d7c0a89a
QY
2320 ok = ((uint32_t)(p_end - p_pnt)
2321 >= sizeof(uint32_t));
d62a17ae 2322 if (ok) {
2323 memcpy(&seq, p_pnt,
d7c0a89a
QY
2324 sizeof(uint32_t));
2325 p_pnt += sizeof(uint32_t);
d62a17ae 2326 orfp.seq = ntohl(seq);
2327 } else
2328 p_pnt = p_end;
2329
5ca840a3 2330 /* val checked in prefix_bgp_orf_set */
1bb379bf 2331 if (p_pnt < p_end)
5ca840a3
DS
2332 orfp.ge = *p_pnt++;
2333
2334 /* val checked in prefix_bgp_orf_set */
1bb379bf 2335 if (p_pnt < p_end)
5ca840a3
DS
2336 orfp.le = *p_pnt++;
2337
d62a17ae 2338 if ((ok = (p_pnt < p_end)))
2339 orfp.p.prefixlen = *p_pnt++;
5ca840a3
DS
2340
2341 /* afi checked already */
2342 orfp.p.family = afi2family(afi);
2343
2344 /* 0 if not ok */
2345 psize = PSIZE(orfp.p.prefixlen);
2346 /* valid for family ? */
2347 if (psize > prefix_blen(&orfp.p)) {
d62a17ae 2348 ok = 0;
2349 psize = prefix_blen(&orfp.p);
2350 }
5ca840a3
DS
2351 /* valid for packet ? */
2352 if (psize > (p_end - p_pnt)) {
d62a17ae 2353 ok = 0;
2354 psize = p_end - p_pnt;
2355 }
2356
2357 if (psize > 0)
2358 memcpy(&orfp.p.u.prefix, p_pnt,
2359 psize);
2360 p_pnt += psize;
2361
2362 if (bgp_debug_neighbor_events(peer)) {
2363 char buf[INET6_BUFSIZ];
2364
2365 zlog_debug(
f70c91dc
DA
2366 "%pBP rcvd %s %s seq %u %s/%d ge %d le %d%s",
2367 peer,
d62a17ae 2368 (common & ORF_COMMON_PART_REMOVE
2369 ? "Remove"
2370 : "Add"),
2371 (common & ORF_COMMON_PART_DENY
2372 ? "deny"
2373 : "permit"),
2374 orfp.seq,
2375 inet_ntop(
2376 orfp.p.family,
2377 &orfp.p.u.prefix,
2378 buf,
2379 INET6_BUFSIZ),
2380 orfp.p.prefixlen,
2381 orfp.ge, orfp.le,
2382 ok ? "" : " MALFORMED");
2383 }
2384
2385 if (ok)
2386 ret = prefix_bgp_orf_set(
2387 name, afi, &orfp,
2388 (common & ORF_COMMON_PART_DENY
2389 ? 0
2390 : 1),
2391 (common & ORF_COMMON_PART_REMOVE
2392 ? 0
2393 : 1));
2394
2395 if (!ok || (ok && ret != CMD_SUCCESS)) {
2396 zlog_info(
f70c91dc
DA
2397 "%pBP Received misformatted prefixlist ORF. Remove All pfxlist",
2398 peer);
d62a17ae 2399 prefix_bgp_orf_remove_all(afi,
2400 name);
2401 break;
2402 }
2403 }
2404
2405 peer->orf_plist[afi][safi] =
2406 prefix_bgp_orf_lookup(afi, name);
2407 }
2408 stream_forward_getp(s, orf_len);
718e3744 2409 }
d62a17ae 2410 if (bgp_debug_neighbor_events(peer))
f70c91dc 2411 zlog_debug("%pBP rcvd Refresh %s ORF request", peer,
d62a17ae 2412 when_to_refresh == REFRESH_DEFER
2413 ? "Defer"
2414 : "Immediate");
2415 if (when_to_refresh == REFRESH_DEFER)
d8151687 2416 return BGP_PACKET_NOOP;
d62a17ae 2417 }
40d2700d 2418
d62a17ae 2419 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2420 if (CHECK_FLAG(peer->af_sflags[afi][safi],
2421 PEER_STATUS_ORF_WAIT_REFRESH))
2422 UNSET_FLAG(peer->af_sflags[afi][safi],
2423 PEER_STATUS_ORF_WAIT_REFRESH);
2424
2425 paf = peer_af_find(peer, afi, safi);
2426 if (paf && paf->subgroup) {
2427 if (peer->orf_plist[afi][safi]) {
2428 updgrp = PAF_UPDGRP(paf);
2429 updgrp_peer = UPDGRP_PEER(updgrp);
2430 updgrp_peer->orf_plist[afi][safi] =
2431 peer->orf_plist[afi][safi];
2432 }
2433
2adac256
DA
2434 /* Avoid supressing duplicate routes later
2435 * when processing in subgroup_announce_table().
2436 */
e1a32ec1 2437 force_update = true;
2adac256 2438
d62a17ae 2439 /* If the peer is configured for default-originate clear the
2440 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
2441 * re-advertise the
2442 * default
2443 */
2444 if (CHECK_FLAG(paf->subgroup->sflags,
2445 SUBGRP_STATUS_DEFAULT_ORIGINATE))
2446 UNSET_FLAG(paf->subgroup->sflags,
2447 SUBGRP_STATUS_DEFAULT_ORIGINATE);
718e3744 2448 }
d62a17ae 2449
9af52ccf
DA
2450 if (subtype == BGP_ROUTE_REFRESH_BORR) {
2451 /* A BGP speaker that has received the Graceful Restart
2452 * Capability from its neighbor MUST ignore any BoRRs for
2453 * an <AFI, SAFI> from the neighbor before the speaker
2454 * receives the EoR for the given <AFI, SAFI> from the
2455 * neighbor.
2456 */
2457 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)
2458 && !CHECK_FLAG(peer->af_sflags[afi][safi],
2459 PEER_STATUS_EOR_RECEIVED)) {
2460 if (bgp_debug_neighbor_events(peer))
2461 zlog_debug(
f70c91dc
DA
2462 "%pBP rcvd route-refresh (BoRR) for %s/%s before EoR",
2463 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2464 return BGP_PACKET_NOOP;
2465 }
2466
2467 if (peer->t_refresh_stalepath) {
2468 if (bgp_debug_neighbor_events(peer))
2469 zlog_debug(
f70c91dc
DA
2470 "%pBP rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
2471 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2472 return BGP_PACKET_NOOP;
2473 }
2474
2475 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_BORR_RECEIVED);
2476 UNSET_FLAG(peer->af_sflags[afi][safi],
2477 PEER_STATUS_EORR_RECEIVED);
2478
2479 /* When a BGP speaker receives a BoRR message from
2480 * a peer, it MUST mark all the routes with the given
2481 * Address Family Identifier and Subsequent Address
2482 * Family Identifier, <AFI, SAFI> [RFC2918], from
2483 * that peer as stale.
2484 */
2485 if (peer_active_nego(peer)) {
2486 SET_FLAG(peer->af_sflags[afi][safi],
2487 PEER_STATUS_ENHANCED_REFRESH);
2488 bgp_set_stale_route(peer, afi, safi);
2489 }
2490
feb17238 2491 if (peer_established(peer))
9af52ccf
DA
2492 thread_add_timer(bm->master,
2493 bgp_refresh_stalepath_timer_expire,
2494 paf, peer->bgp->stalepath_time,
2495 &peer->t_refresh_stalepath);
2496
2497 if (bgp_debug_neighbor_events(peer))
2498 zlog_debug(
f70c91dc
DA
2499 "%pBP rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
2500 peer, afi2str(afi), safi2str(safi),
9af52ccf
DA
2501 peer->bgp->stalepath_time);
2502 } else if (subtype == BGP_ROUTE_REFRESH_EORR) {
2503 if (!peer->t_refresh_stalepath) {
2504 zlog_err(
f70c91dc
DA
2505 "%pBP rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
2506 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2507 return BGP_PACKET_NOOP;
2508 }
2509
fa5806c3 2510 THREAD_OFF(peer->t_refresh_stalepath);
9af52ccf
DA
2511
2512 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EORR_RECEIVED);
2513 UNSET_FLAG(peer->af_sflags[afi][safi],
2514 PEER_STATUS_BORR_RECEIVED);
2515
2516 if (bgp_debug_neighbor_events(peer))
2517 zlog_debug(
f70c91dc
DA
2518 "%pBP rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
2519 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2520
2521 if (peer->nsf[afi][safi])
2522 bgp_clear_stale_route(peer, afi, safi);
2523 } else {
bcbeb3f9 2524 if (bgp_debug_neighbor_events(peer))
a7d91a8c 2525 zlog_debug(
f70c91dc
DA
2526 "%pBP rcvd route-refresh (REQUEST) for %s/%s",
2527 peer, afi2str(afi), safi2str(safi));
bcbeb3f9 2528
9af52ccf
DA
2529 /* In response to a "normal route refresh request" from the
2530 * peer, the speaker MUST send a BoRR message.
2531 */
2532 if (CHECK_FLAG(peer->cap, PEER_CAP_ENHANCED_RR_RCV)) {
2533 /* For a BGP speaker that supports the BGP Graceful
2534 * Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
2535 * to a neighbor before it sends the EoR for the
2536 * <AFI, SAFI> to the neighbor.
2537 */
2538 if (!CHECK_FLAG(peer->af_sflags[afi][safi],
2539 PEER_STATUS_EOR_SEND)) {
2540 if (bgp_debug_neighbor_events(peer))
2541 zlog_debug(
f70c91dc
DA
2542 "%pBP rcvd route-refresh (REQUEST) for %s/%s before EoR",
2543 peer, afi2str(afi),
2544 safi2str(safi));
9af52ccf
DA
2545 return BGP_PACKET_NOOP;
2546 }
2547
2548 bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
2549 BGP_ROUTE_REFRESH_BORR);
2550
2551 if (bgp_debug_neighbor_events(peer))
2552 zlog_debug(
f70c91dc
DA
2553 "%pBP sending route-refresh (BoRR) for %s/%s",
2554 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2555
2556 /* Set flag Ready-To-Send to know when we can send EoRR
2557 * message.
2558 */
2559 SET_FLAG(peer->af_sflags[afi][safi],
2560 PEER_STATUS_BORR_SEND);
2561 UNSET_FLAG(peer->af_sflags[afi][safi],
2562 PEER_STATUS_EORR_SEND);
2563 }
2564 }
2565
d62a17ae 2566 /* Perform route refreshment to the peer */
e1a32ec1 2567 bgp_announce_route(peer, afi, safi, force_update);
d8151687
QY
2568
2569 /* No FSM action necessary */
2570 return BGP_PACKET_NOOP;
718e3744 2571}
2572
d8151687
QY
2573/**
2574 * Parse BGP CAPABILITY message for peer.
2575 *
2576 * @param peer
2577 * @param size size of the packet
2578 * @return as in summary
2579 */
d7c0a89a 2580static int bgp_capability_msg_parse(struct peer *peer, uint8_t *pnt,
d62a17ae 2581 bgp_size_t length)
718e3744 2582{
d7c0a89a 2583 uint8_t *end;
d62a17ae 2584 struct capability_mp_data mpc;
2585 struct capability_header *hdr;
d7c0a89a 2586 uint8_t action;
d62a17ae 2587 iana_afi_t pkt_afi;
2588 afi_t afi;
5c525538
RW
2589 iana_safi_t pkt_safi;
2590 safi_t safi;
d62a17ae 2591
2592 end = pnt + length;
2593
2594 while (pnt < end) {
2595 /* We need at least action, capability code and capability
2596 * length. */
2597 if (pnt + 3 > end) {
2598 zlog_info("%s Capability length error", peer->host);
0e35025e
DA
2599 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2600 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
d8151687 2601 return BGP_Stop;
d62a17ae 2602 }
2603 action = *pnt;
2604 hdr = (struct capability_header *)(pnt + 1);
2605
2606 /* Action value check. */
2607 if (action != CAPABILITY_ACTION_SET
2608 && action != CAPABILITY_ACTION_UNSET) {
2609 zlog_info("%s Capability Action Value error %d",
2610 peer->host, action);
0e35025e
DA
2611 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2612 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
d8151687 2613 return BGP_Stop;
d62a17ae 2614 }
2615
2616 if (bgp_debug_neighbor_events(peer))
2617 zlog_debug(
2618 "%s CAPABILITY has action: %d, code: %u, length %u",
2619 peer->host, action, hdr->code, hdr->length);
2620
ff6db102
DS
2621 if (hdr->length < sizeof(struct capability_mp_data)) {
2622 zlog_info(
2623 "%pBP Capability structure is not properly filled out, expected at least %zu bytes but header length specified is %d",
2624 peer, sizeof(struct capability_mp_data),
2625 hdr->length);
2626 return BGP_Stop;
2627 }
2628
d62a17ae 2629 /* Capability length check. */
2630 if ((pnt + hdr->length + 3) > end) {
2631 zlog_info("%s Capability length error", peer->host);
0e35025e
DA
2632 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2633 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
d8151687 2634 return BGP_Stop;
d62a17ae 2635 }
2636
2637 /* Fetch structure to the byte stream. */
2638 memcpy(&mpc, pnt + 3, sizeof(struct capability_mp_data));
d2b6417b 2639 pnt += hdr->length + 3;
d62a17ae 2640
2641 /* We know MP Capability Code. */
2642 if (hdr->code == CAPABILITY_CODE_MP) {
2643 pkt_afi = ntohs(mpc.afi);
2644 pkt_safi = mpc.safi;
2645
2646 /* Ignore capability when override-capability is set. */
2647 if (CHECK_FLAG(peer->flags,
2648 PEER_FLAG_OVERRIDE_CAPABILITY))
2649 continue;
2650
2651 /* Convert AFI, SAFI to internal values. */
2652 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi,
2653 &safi)) {
2654 if (bgp_debug_neighbor_events(peer))
2655 zlog_debug(
3efd0893 2656 "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)",
748a041f
DS
2657 peer->host,
2658 iana_afi2str(pkt_afi),
2659 iana_safi2str(pkt_safi));
d62a17ae 2660 continue;
2661 }
2662
2663 /* Address family check. */
2664 if (bgp_debug_neighbor_events(peer))
2665 zlog_debug(
c386cdd8 2666 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
d62a17ae 2667 peer->host,
2668 action == CAPABILITY_ACTION_SET
2669 ? "Advertising"
2670 : "Removing",
c386cdd8
DA
2671 iana_afi2str(pkt_afi),
2672 iana_safi2str(pkt_safi));
d62a17ae 2673
2674 if (action == CAPABILITY_ACTION_SET) {
2675 peer->afc_recv[afi][safi] = 1;
2676 if (peer->afc[afi][safi]) {
2677 peer->afc_nego[afi][safi] = 1;
e1a32ec1
DS
2678 bgp_announce_route(peer, afi, safi,
2679 false);
d62a17ae 2680 }
2681 } else {
2682 peer->afc_recv[afi][safi] = 0;
2683 peer->afc_nego[afi][safi] = 0;
2684
2685 if (peer_active_nego(peer))
2686 bgp_clear_route(peer, afi, safi);
2687 else
d8151687 2688 return BGP_Stop;
d62a17ae 2689 }
2690 } else {
ade6974d 2691 flog_warn(
e50f7cfd 2692 EC_BGP_UNRECOGNIZED_CAPABILITY,
ade6974d
QY
2693 "%s unrecognized capability code: %d - ignored",
2694 peer->host, hdr->code);
d62a17ae 2695 }
d62a17ae 2696 }
d8151687
QY
2697
2698 /* No FSM action necessary */
2699 return BGP_PACKET_NOOP;
718e3744 2700}
2701
d8151687
QY
2702/**
2703 * Parse BGP CAPABILITY message for peer.
01b7ce2d 2704 *
d8151687
QY
2705 * Exported for unit testing.
2706 *
2707 * @param peer
2708 * @param size size of the packet
2709 * @return as in summary
01b7ce2d 2710 */
d62a17ae 2711int bgp_capability_receive(struct peer *peer, bgp_size_t size)
718e3744 2712{
d7c0a89a 2713 uint8_t *pnt;
d62a17ae 2714
2715 /* Fetch pointer. */
424ab01d 2716 pnt = stream_pnt(peer->curr);
d62a17ae 2717
2718 if (bgp_debug_neighbor_events(peer))
2719 zlog_debug("%s rcv CAPABILITY", peer->host);
2720
2721 /* If peer does not have the capability, send notification. */
2722 if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV)) {
e50f7cfd 2723 flog_err(EC_BGP_NO_CAP,
1c50c1c0
QY
2724 "%s [Error] BGP dynamic capability is not enabled",
2725 peer->host);
d62a17ae 2726 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2727 BGP_NOTIFY_HEADER_BAD_MESTYPE);
d8151687 2728 return BGP_Stop;
d62a17ae 2729 }
2730
2731 /* Status must be Established. */
feb17238 2732 if (!peer_established(peer)) {
af4c2728 2733 flog_err(
e50f7cfd 2734 EC_BGP_NO_CAP,
d62a17ae 2735 "%s [Error] Dynamic capability packet received under status %s",
2736 peer->host,
2737 lookup_msg(bgp_status_msg, peer->status, NULL));
0e35025e 2738 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
3893aeee 2739 bgp_fsm_error_subcode(peer->status));
d8151687 2740 return BGP_Stop;
d62a17ae 2741 }
2742
2743 /* Parse packet. */
2744 return bgp_capability_msg_parse(peer, pnt, size);
718e3744 2745}
6b0655a2 2746
d8151687
QY
2747/**
2748 * Processes a peer's input buffer.
2749 *
2750 * This function sidesteps the event loop and directly calls bgp_event_update()
2751 * after processing each BGP message. This is necessary to ensure proper
2752 * ordering of FSM events and unifies the behavior that was present previously,
2753 * whereby some of the packet handling functions would update the FSM and some
2754 * would not, making event flow difficult to understand. Please think twice
2755 * before hacking this.
2756 *
2757 * Thread type: THREAD_EVENT
2758 * @param thread
2759 * @return 0
2760 */
cc9f21da 2761void bgp_process_packet(struct thread *thread)
718e3744 2762{
424ab01d 2763 /* Yes first of all get peer pointer. */
d8151687
QY
2764 struct peer *peer; // peer
2765 uint32_t rpkt_quanta_old; // how many packets to read
2766 int fsm_update_result; // return code of bgp_event_update()
2767 int mprc; // message processing return code
555e09d4 2768
424ab01d 2769 peer = THREAD_ARG(thread);
555e09d4
QY
2770 rpkt_quanta_old = atomic_load_explicit(&peer->bgp->rpkt_quanta,
2771 memory_order_relaxed);
e3c7270d 2772 fsm_update_result = 0;
555e09d4 2773
424ab01d 2774 /* Guard against scheduled events that occur after peer deletion. */
9eb217ff 2775 if (peer->status == Deleted || peer->status == Clearing)
cc9f21da 2776 return;
718e3744 2777
555e09d4 2778 unsigned int processed = 0;
d62a17ae 2779
555e09d4 2780 while (processed < rpkt_quanta_old) {
d7c0a89a 2781 uint8_t type = 0;
9eb217ff
QY
2782 bgp_size_t size;
2783 char notify_data_length[2];
d62a17ae 2784
cb1991af 2785 frr_with_mutex (&peer->io_mtx) {
9eb217ff
QY
2786 peer->curr = stream_fifo_pop(peer->ibuf);
2787 }
d62a17ae 2788
9eb217ff 2789 if (peer->curr == NULL) // no packets to process, hmm...
cc9f21da 2790 return;
d62a17ae 2791
9eb217ff
QY
2792 /* skip the marker and copy the packet length */
2793 stream_forward_getp(peer->curr, BGP_MARKER_SIZE);
2794 memcpy(notify_data_length, stream_pnt(peer->curr), 2);
2795
2796 /* read in the packet length and type */
2797 size = stream_getw(peer->curr);
2798 type = stream_getc(peer->curr);
2799
584470fb 2800 hook_call(bgp_packet_dump, peer, type, size, peer->curr);
9eb217ff
QY
2801
2802 /* adjust size to exclude the marker + length + type */
2803 size -= BGP_HEADER_SIZE;
2804
2805 /* Read rest of the packet and call each sort of packet routine
2806 */
2807 switch (type) {
2808 case BGP_MSG_OPEN:
c7bb4f00 2809 frrtrace(2, frr_bgp, open_process, peer, size);
0112e9e0
QY
2810 atomic_fetch_add_explicit(&peer->open_in, 1,
2811 memory_order_relaxed);
d8151687
QY
2812 mprc = bgp_open_receive(peer, size);
2813 if (mprc == BGP_Stop)
af4c2728 2814 flog_err(
e50f7cfd 2815 EC_BGP_PKT_OPEN,
d8151687 2816 "%s: BGP OPEN receipt failed for peer: %s",
0767b4f3 2817 __func__, peer->host);
9eb217ff
QY
2818 break;
2819 case BGP_MSG_UPDATE:
c7bb4f00 2820 frrtrace(2, frr_bgp, update_process, peer, size);
0112e9e0
QY
2821 atomic_fetch_add_explicit(&peer->update_in, 1,
2822 memory_order_relaxed);
9eb217ff 2823 peer->readtime = monotime(NULL);
d8151687
QY
2824 mprc = bgp_update_receive(peer, size);
2825 if (mprc == BGP_Stop)
af4c2728 2826 flog_err(
e50f7cfd 2827 EC_BGP_UPDATE_RCV,
d8151687 2828 "%s: BGP UPDATE receipt failed for peer: %s",
0767b4f3 2829 __func__, peer->host);
9eb217ff
QY
2830 break;
2831 case BGP_MSG_NOTIFY:
c7bb4f00 2832 frrtrace(2, frr_bgp, notification_process, peer, size);
0112e9e0
QY
2833 atomic_fetch_add_explicit(&peer->notify_in, 1,
2834 memory_order_relaxed);
d8151687
QY
2835 mprc = bgp_notify_receive(peer, size);
2836 if (mprc == BGP_Stop)
af4c2728 2837 flog_err(
e50f7cfd 2838 EC_BGP_NOTIFY_RCV,
d8151687 2839 "%s: BGP NOTIFY receipt failed for peer: %s",
0767b4f3 2840 __func__, peer->host);
9eb217ff
QY
2841 break;
2842 case BGP_MSG_KEEPALIVE:
c7bb4f00 2843 frrtrace(2, frr_bgp, keepalive_process, peer, size);
9eb217ff 2844 peer->readtime = monotime(NULL);
0112e9e0
QY
2845 atomic_fetch_add_explicit(&peer->keepalive_in, 1,
2846 memory_order_relaxed);
d8151687
QY
2847 mprc = bgp_keepalive_receive(peer, size);
2848 if (mprc == BGP_Stop)
af4c2728 2849 flog_err(
e50f7cfd 2850 EC_BGP_KEEP_RCV,
d8151687 2851 "%s: BGP KEEPALIVE receipt failed for peer: %s",
0767b4f3 2852 __func__, peer->host);
9eb217ff
QY
2853 break;
2854 case BGP_MSG_ROUTE_REFRESH_NEW:
2855 case BGP_MSG_ROUTE_REFRESH_OLD:
c7bb4f00 2856 frrtrace(2, frr_bgp, refresh_process, peer, size);
0112e9e0
QY
2857 atomic_fetch_add_explicit(&peer->refresh_in, 1,
2858 memory_order_relaxed);
d8151687
QY
2859 mprc = bgp_route_refresh_receive(peer, size);
2860 if (mprc == BGP_Stop)
af4c2728 2861 flog_err(
e50f7cfd 2862 EC_BGP_RFSH_RCV,
d8151687 2863 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
0767b4f3 2864 __func__, peer->host);
9eb217ff
QY
2865 break;
2866 case BGP_MSG_CAPABILITY:
c7bb4f00 2867 frrtrace(2, frr_bgp, capability_process, peer, size);
0112e9e0
QY
2868 atomic_fetch_add_explicit(&peer->dynamic_cap_in, 1,
2869 memory_order_relaxed);
d8151687
QY
2870 mprc = bgp_capability_receive(peer, size);
2871 if (mprc == BGP_Stop)
af4c2728 2872 flog_err(
e50f7cfd 2873 EC_BGP_CAP_RCV,
d8151687 2874 "%s: BGP CAPABILITY receipt failed for peer: %s",
0767b4f3 2875 __func__, peer->host);
9eb217ff 2876 break;
e3c7270d 2877 default:
db878db0
QY
2878 /* Suppress uninitialized variable warning */
2879 mprc = 0;
5041dc4f 2880 (void)mprc;
becedef6
QY
2881 /*
2882 * The message type should have been sanitized before
2883 * we ever got here. Receipt of a message with an
2884 * invalid header at this point is indicative of a
2885 * security issue.
2886 */
e3c7270d 2887 assert (!"Message of invalid type received during input processing");
9eb217ff
QY
2888 }
2889
d8151687
QY
2890 /* delete processed packet */
2891 stream_free(peer->curr);
2892 peer->curr = NULL;
2893 processed++;
9eb217ff 2894
d8151687
QY
2895 /* Update FSM */
2896 if (mprc != BGP_PACKET_NOOP)
2897 fsm_update_result = bgp_event_update(peer, mprc);
e3c7270d
QY
2898 else
2899 continue;
d8151687 2900
becedef6
QY
2901 /*
2902 * If peer was deleted, do not process any more packets. This
2903 * is usually due to executing BGP_Stop or a stub deletion.
2904 */
d8151687
QY
2905 if (fsm_update_result == FSM_PEER_TRANSFERRED
2906 || fsm_update_result == FSM_PEER_STOPPED)
2907 break;
d62a17ae 2908 }
2909
d8151687
QY
2910 if (fsm_update_result != FSM_PEER_TRANSFERRED
2911 && fsm_update_result != FSM_PEER_STOPPED) {
cb1991af 2912 frr_with_mutex (&peer->io_mtx) {
becedef6
QY
2913 // more work to do, come back later
2914 if (peer->ibuf->count > 0)
e0d550df 2915 thread_add_event(
4af76660
QY
2916 bm->master, bgp_process_packet, peer, 0,
2917 &peer->t_process_packet);
d8151687 2918 }
718e3744 2919 }
718e3744 2920}
9e3b51a7 2921
2922/* Send EOR when routes are processed by selection deferral timer */
2923void bgp_send_delayed_eor(struct bgp *bgp)
2924{
2925 struct peer *peer;
2926 struct listnode *node, *nnode;
2927
2928 /* EOR message sent in bgp_write_proceed_actions */
2929 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
2930 bgp_write_proceed_actions(peer);
2931}
6af96fa3
MS
2932
2933/*
2934 * Task callback to handle socket error encountered in the io pthread. We avoid
2935 * having the io pthread try to enqueue fsm events or mess with the peer
2936 * struct.
2937 */
cc9f21da 2938void bgp_packet_process_error(struct thread *thread)
6af96fa3
MS
2939{
2940 struct peer *peer;
2941 int code;
2942
2943 peer = THREAD_ARG(thread);
2944 code = THREAD_VAL(thread);
2945
2946 if (bgp_debug_neighbor_events(peer))
2947 zlog_debug("%s [Event] BGP error %d on fd %d",
046bb347 2948 peer->host, code, peer->fd);
6af96fa3
MS
2949
2950 /* Closed connection or error on the socket */
feb17238 2951 if (peer_established(peer)) {
6af96fa3
MS
2952 if ((CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART)
2953 || CHECK_FLAG(peer->flags,
2954 PEER_FLAG_GRACEFUL_RESTART_HELPER))
2955 && CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) {
2956 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2957 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2958 } else
2959 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2960 }
2961
2962 bgp_event_update(peer, code);
6af96fa3 2963}