]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_packet.c
Merge pull request #11731 from opensourcerouting/feature/show_bgp_access-list
[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 */
2009 if (bgp_best_path_select_defer(
36235319
QY
2010 peer->bgp, afi, safi)
2011 < 0)
f009ff26 2012 return BGP_Stop;
2013 }
d62a17ae 2014 }
2015
2016 /* NSF delete stale route */
2017 if (peer->nsf[afi][safi])
2018 bgp_clear_stale_route(peer, afi, safi);
2019
1479ed2f
DA
2020 zlog_info(
2021 "%s: rcvd End-of-RIB for %s from %s in vrf %s",
2022 __func__, get_afi_safi_str(afi, safi, false),
2023 peer->host, vrf ? vrf->name : VRF_DEFAULT_NAME);
2024 }
f80f838b 2025 }
d62a17ae 2026
2027 /* Everything is done. We unintern temporary structures which
2028 interned in bgp_attr_parse(). */
2029 bgp_attr_unintern_sub(&attr);
2030
d62a17ae 2031 peer->update_time = bgp_clock();
2032
c385f82a
MK
2033 /* Notify BGP Conditional advertisement scanner process */
2034 peer->advmap_table_change = true;
2035
d8151687 2036 return Receive_UPDATE_message;
718e3744 2037}
2038
d8151687
QY
2039/**
2040 * Process BGP NOTIFY message for peer.
2041 *
2042 * @param peer
2043 * @param size size of the packet
2044 * @return as in summary
2045 */
2046static int bgp_notify_receive(struct peer *peer, bgp_size_t size)
718e3744 2047{
10d476d4
DA
2048 struct bgp_notify outer = {};
2049 struct bgp_notify inner = {};
eea685b6 2050 bool hard_reset = false;
d62a17ae 2051
2052 if (peer->notify.data) {
eea685b6 2053 XFREE(MTYPE_BGP_NOTIFICATION, peer->notify.data);
d62a17ae 2054 peer->notify.length = 0;
e7ce634f 2055 peer->notify.hard_reset = false;
d62a17ae 2056 }
2057
eea685b6
DA
2058 outer.code = stream_getc(peer->curr);
2059 outer.subcode = stream_getc(peer->curr);
2060 outer.length = size - 2;
2061 outer.data = NULL;
2062 outer.raw_data = NULL;
2063 if (outer.length) {
2064 outer.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, outer.length);
2065 memcpy(outer.raw_data, stream_pnt(peer->curr), outer.length);
2066 }
2067
1ae314be
DA
2068 hard_reset =
2069 bgp_notify_received_hard_reset(peer, outer.code, outer.subcode);
eea685b6
DA
2070 if (hard_reset && outer.length) {
2071 inner = bgp_notify_decapsulate_hard_reset(&outer);
2072 peer->notify.hard_reset = true;
2073 } else {
2074 inner = outer;
2075 }
d62a17ae 2076
2077 /* Preserv notify code and sub code. */
eea685b6
DA
2078 peer->notify.code = inner.code;
2079 peer->notify.subcode = inner.subcode;
d62a17ae 2080 /* For further diagnostic record returned Data. */
eea685b6
DA
2081 if (inner.length) {
2082 peer->notify.length = inner.length;
2083 peer->notify.data =
2084 XMALLOC(MTYPE_BGP_NOTIFICATION, inner.length);
2085 memcpy(peer->notify.data, inner.raw_data, inner.length);
d62a17ae 2086 }
2087
2088 /* For debug */
2089 {
2090 int i;
2091 int first = 0;
2092 char c[4];
2093
eea685b6
DA
2094 if (inner.length) {
2095 inner.data = XMALLOC(MTYPE_BGP_NOTIFICATION,
2096 inner.length * 3);
2097 for (i = 0; i < inner.length; i++)
d62a17ae 2098 if (first) {
552d6491 2099 snprintf(c, sizeof(c), " %02x",
424ab01d 2100 stream_getc(peer->curr));
f009ff26 2101
eea685b6
DA
2102 strlcat(inner.data, c,
2103 inner.length * 3);
f009ff26 2104
d62a17ae 2105 } else {
2106 first = 1;
552d6491
QY
2107 snprintf(c, sizeof(c), "%02x",
2108 stream_getc(peer->curr));
f009ff26 2109
eea685b6
DA
2110 strlcpy(inner.data, c,
2111 inner.length * 3);
d62a17ae 2112 }
d62a17ae 2113 }
2114
eea685b6 2115 bgp_notify_print(peer, &inner, "received", hard_reset);
10d476d4 2116 if (inner.length) {
eea685b6
DA
2117 XFREE(MTYPE_BGP_NOTIFICATION, inner.data);
2118 inner.length = 0;
2119 }
2120 if (outer.length) {
2121 XFREE(MTYPE_BGP_NOTIFICATION, outer.data);
10d476d4 2122 XFREE(MTYPE_BGP_NOTIFICATION, outer.raw_data);
c73d2363
DA
2123
2124 /* If this is a Hard Reset notification, we MUST free
2125 * the inner (encapsulated) notification too.
2126 */
2127 if (hard_reset)
2128 XFREE(MTYPE_BGP_NOTIFICATION, inner.raw_data);
eea685b6 2129 outer.length = 0;
d62a17ae 2130 }
2131 }
2132
2133 /* peer count update */
0112e9e0 2134 atomic_fetch_add_explicit(&peer->notify_in, 1, memory_order_relaxed);
d62a17ae 2135
2136 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
2137
2138 /* We have to check for Notify with Unsupported Optional Parameter.
2139 in that case we fallback to open without the capability option.
2140 But this done in bgp_stop. We just mark it here to avoid changing
2141 the fsm tables. */
eea685b6
DA
2142 if (inner.code == BGP_NOTIFY_OPEN_ERR &&
2143 inner.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM)
d62a17ae 2144 UNSET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
2145
20170775
DA
2146 /* If Graceful-Restart N-bit (Notification) is exchanged,
2147 * and it's not a Hard Reset, let's retain the routes.
2148 */
2149 if (bgp_has_graceful_restart_notification(peer) && !hard_reset &&
2150 CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE))
2151 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2152
5cce3f05 2153 bgp_peer_gr_flags_update(peer);
36235319
QY
2154 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
2155 peer->bgp->peer);
5cce3f05 2156
d8151687 2157 return Receive_NOTIFICATION_message;
718e3744 2158}
2159
d8151687
QY
2160/**
2161 * Process BGP ROUTEREFRESH message for peer.
2162 *
2163 * @param peer
2164 * @param size size of the packet
2165 * @return as in summary
2166 */
2167static int bgp_route_refresh_receive(struct peer *peer, bgp_size_t size)
718e3744 2168{
d62a17ae 2169 iana_afi_t pkt_afi;
2170 afi_t afi;
5c525538
RW
2171 iana_safi_t pkt_safi;
2172 safi_t safi;
d62a17ae 2173 struct stream *s;
2174 struct peer_af *paf;
2175 struct update_group *updgrp;
2176 struct peer *updgrp_peer;
9af52ccf 2177 uint8_t subtype;
e1a32ec1 2178 bool force_update = false;
9af52ccf
DA
2179 bgp_size_t msg_length =
2180 size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE);
d62a17ae 2181
2182 /* If peer does not have the capability, send notification. */
2183 if (!CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)) {
e50f7cfd 2184 flog_err(EC_BGP_NO_CAP,
1c50c1c0
QY
2185 "%s [Error] BGP route refresh is not enabled",
2186 peer->host);
d62a17ae 2187 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2188 BGP_NOTIFY_HEADER_BAD_MESTYPE);
d8151687 2189 return BGP_Stop;
d62a17ae 2190 }
2191
2192 /* Status must be Established. */
feb17238 2193 if (!peer_established(peer)) {
af4c2728 2194 flog_err(
e50f7cfd 2195 EC_BGP_INVALID_STATUS,
d62a17ae 2196 "%s [Error] Route refresh packet received under status %s",
2197 peer->host,
2198 lookup_msg(bgp_status_msg, peer->status, NULL));
0e35025e 2199 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
3893aeee 2200 bgp_fsm_error_subcode(peer->status));
d8151687 2201 return BGP_Stop;
d62a17ae 2202 }
2203
424ab01d 2204 s = peer->curr;
d62a17ae 2205
2206 /* Parse packet. */
2207 pkt_afi = stream_getw(s);
9af52ccf 2208 subtype = stream_getc(s);
d62a17ae 2209 pkt_safi = stream_getc(s);
2210
d62a17ae 2211 /* Convert AFI, SAFI to internal values and check. */
2212 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) {
2213 zlog_info(
748a041f
DS
2214 "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
2215 peer->host, iana_afi2str(pkt_afi),
2216 iana_safi2str(pkt_safi));
d8151687 2217 return BGP_PACKET_NOOP;
d62a17ae 2218 }
2219
2220 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) {
d7c0a89a
QY
2221 uint8_t *end;
2222 uint8_t when_to_refresh;
2223 uint8_t orf_type;
2224 uint16_t orf_len;
d62a17ae 2225
9af52ccf
DA
2226 if (subtype) {
2227 /* If the length, excluding the fixed-size message
2228 * header, of the received ROUTE-REFRESH message with
2229 * Message Subtype 1 and 2 is not 4, then the BGP
2230 * speaker MUST send a NOTIFICATION message with the
2231 * Error Code of "ROUTE-REFRESH Message Error" and the
2232 * subcode of "Invalid Message Length".
2233 */
2234 if (msg_length != 4) {
2235 zlog_err(
2236 "%s Enhanced Route Refresh message length error",
2237 peer->host);
2238 bgp_notify_send(
2239 peer, BGP_NOTIFY_ROUTE_REFRESH_ERR,
2240 BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN);
2241 }
2242
2243 /* When the BGP speaker receives a ROUTE-REFRESH message
2244 * with a "Message Subtype" field other than 0, 1, or 2,
2245 * it MUST ignore the received ROUTE-REFRESH message.
2246 */
2247 if (subtype > 2)
2248 zlog_err(
2249 "%s Enhanced Route Refresh invalid subtype",
2250 peer->host);
2251 }
2252
2253 if (msg_length < 5) {
d62a17ae 2254 zlog_info("%s ORF route refresh length error",
2255 peer->host);
0e35025e
DA
2256 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2257 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
d8151687 2258 return BGP_Stop;
718e3744 2259 }
2260
d62a17ae 2261 when_to_refresh = stream_getc(s);
2262 end = stream_pnt(s) + (size - 5);
2263
2264 while ((stream_pnt(s) + 2) < end) {
2265 orf_type = stream_getc(s);
2266 orf_len = stream_getw(s);
2267
2268 /* orf_len in bounds? */
2269 if ((stream_pnt(s) + orf_len) > end)
2270 break; /* XXX: Notify instead?? */
2271 if (orf_type == ORF_TYPE_PREFIX
2272 || orf_type == ORF_TYPE_PREFIX_OLD) {
2273 uint8_t *p_pnt = stream_pnt(s);
2274 uint8_t *p_end = stream_pnt(s) + orf_len;
2275 struct orf_prefix orfp;
d7c0a89a
QY
2276 uint8_t common = 0;
2277 uint32_t seq;
d62a17ae 2278 int psize;
2279 char name[BUFSIZ];
2280 int ret = CMD_SUCCESS;
2281
2282 if (bgp_debug_neighbor_events(peer)) {
2283 zlog_debug(
f70c91dc
DA
2284 "%pBP rcvd Prefixlist ORF(%d) length %d",
2285 peer, orf_type, orf_len);
d62a17ae 2286 }
2287
2288 /* we're going to read at least 1 byte of common
2289 * ORF header,
2290 * and 7 bytes of ORF Address-filter entry from
2291 * the stream
2292 */
2293 if (orf_len < 7)
2294 break;
2295
2296 /* ORF prefix-list name */
772270f3
QY
2297 snprintf(name, sizeof(name), "%s.%d.%d",
2298 peer->host, afi, safi);
d62a17ae 2299
2300 while (p_pnt < p_end) {
2301 /* If the ORF entry is malformed, want
2302 * to read as much of it
2303 * as possible without going beyond the
2304 * bounds of the entry,
2305 * to maximise debug information.
2306 */
2307 int ok;
6006b807 2308 memset(&orfp, 0, sizeof(orfp));
d62a17ae 2309 common = *p_pnt++;
2310 /* after ++: p_pnt <= p_end */
2311 if (common
2312 & ORF_COMMON_PART_REMOVE_ALL) {
2313 if (bgp_debug_neighbor_events(
2314 peer))
2315 zlog_debug(
f70c91dc
DA
2316 "%pBP rcvd Remove-All pfxlist ORF request",
2317 peer);
d62a17ae 2318 prefix_bgp_orf_remove_all(afi,
2319 name);
2320 break;
2321 }
d7c0a89a
QY
2322 ok = ((uint32_t)(p_end - p_pnt)
2323 >= sizeof(uint32_t));
d62a17ae 2324 if (ok) {
2325 memcpy(&seq, p_pnt,
d7c0a89a
QY
2326 sizeof(uint32_t));
2327 p_pnt += sizeof(uint32_t);
d62a17ae 2328 orfp.seq = ntohl(seq);
2329 } else
2330 p_pnt = p_end;
2331
5ca840a3 2332 /* val checked in prefix_bgp_orf_set */
1bb379bf 2333 if (p_pnt < p_end)
5ca840a3
DS
2334 orfp.ge = *p_pnt++;
2335
2336 /* val checked in prefix_bgp_orf_set */
1bb379bf 2337 if (p_pnt < p_end)
5ca840a3
DS
2338 orfp.le = *p_pnt++;
2339
d62a17ae 2340 if ((ok = (p_pnt < p_end)))
2341 orfp.p.prefixlen = *p_pnt++;
5ca840a3
DS
2342
2343 /* afi checked already */
2344 orfp.p.family = afi2family(afi);
2345
2346 /* 0 if not ok */
2347 psize = PSIZE(orfp.p.prefixlen);
2348 /* valid for family ? */
2349 if (psize > prefix_blen(&orfp.p)) {
d62a17ae 2350 ok = 0;
2351 psize = prefix_blen(&orfp.p);
2352 }
5ca840a3
DS
2353 /* valid for packet ? */
2354 if (psize > (p_end - p_pnt)) {
d62a17ae 2355 ok = 0;
2356 psize = p_end - p_pnt;
2357 }
2358
2359 if (psize > 0)
2360 memcpy(&orfp.p.u.prefix, p_pnt,
2361 psize);
2362 p_pnt += psize;
2363
2364 if (bgp_debug_neighbor_events(peer)) {
2365 char buf[INET6_BUFSIZ];
2366
2367 zlog_debug(
f70c91dc
DA
2368 "%pBP rcvd %s %s seq %u %s/%d ge %d le %d%s",
2369 peer,
d62a17ae 2370 (common & ORF_COMMON_PART_REMOVE
2371 ? "Remove"
2372 : "Add"),
2373 (common & ORF_COMMON_PART_DENY
2374 ? "deny"
2375 : "permit"),
2376 orfp.seq,
2377 inet_ntop(
2378 orfp.p.family,
2379 &orfp.p.u.prefix,
2380 buf,
2381 INET6_BUFSIZ),
2382 orfp.p.prefixlen,
2383 orfp.ge, orfp.le,
2384 ok ? "" : " MALFORMED");
2385 }
2386
2387 if (ok)
2388 ret = prefix_bgp_orf_set(
2389 name, afi, &orfp,
2390 (common & ORF_COMMON_PART_DENY
2391 ? 0
2392 : 1),
2393 (common & ORF_COMMON_PART_REMOVE
2394 ? 0
2395 : 1));
2396
2397 if (!ok || (ok && ret != CMD_SUCCESS)) {
2398 zlog_info(
f70c91dc
DA
2399 "%pBP Received misformatted prefixlist ORF. Remove All pfxlist",
2400 peer);
d62a17ae 2401 prefix_bgp_orf_remove_all(afi,
2402 name);
2403 break;
2404 }
2405 }
2406
2407 peer->orf_plist[afi][safi] =
2408 prefix_bgp_orf_lookup(afi, name);
2409 }
2410 stream_forward_getp(s, orf_len);
718e3744 2411 }
d62a17ae 2412 if (bgp_debug_neighbor_events(peer))
f70c91dc 2413 zlog_debug("%pBP rcvd Refresh %s ORF request", peer,
d62a17ae 2414 when_to_refresh == REFRESH_DEFER
2415 ? "Defer"
2416 : "Immediate");
2417 if (when_to_refresh == REFRESH_DEFER)
d8151687 2418 return BGP_PACKET_NOOP;
d62a17ae 2419 }
40d2700d 2420
d62a17ae 2421 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2422 if (CHECK_FLAG(peer->af_sflags[afi][safi],
2423 PEER_STATUS_ORF_WAIT_REFRESH))
2424 UNSET_FLAG(peer->af_sflags[afi][safi],
2425 PEER_STATUS_ORF_WAIT_REFRESH);
2426
2427 paf = peer_af_find(peer, afi, safi);
2428 if (paf && paf->subgroup) {
2429 if (peer->orf_plist[afi][safi]) {
2430 updgrp = PAF_UPDGRP(paf);
2431 updgrp_peer = UPDGRP_PEER(updgrp);
2432 updgrp_peer->orf_plist[afi][safi] =
2433 peer->orf_plist[afi][safi];
2434 }
2435
2adac256
DA
2436 /* Avoid supressing duplicate routes later
2437 * when processing in subgroup_announce_table().
2438 */
e1a32ec1 2439 force_update = true;
2adac256 2440
d62a17ae 2441 /* If the peer is configured for default-originate clear the
2442 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
2443 * re-advertise the
2444 * default
2445 */
2446 if (CHECK_FLAG(paf->subgroup->sflags,
2447 SUBGRP_STATUS_DEFAULT_ORIGINATE))
2448 UNSET_FLAG(paf->subgroup->sflags,
2449 SUBGRP_STATUS_DEFAULT_ORIGINATE);
718e3744 2450 }
d62a17ae 2451
9af52ccf
DA
2452 if (subtype == BGP_ROUTE_REFRESH_BORR) {
2453 /* A BGP speaker that has received the Graceful Restart
2454 * Capability from its neighbor MUST ignore any BoRRs for
2455 * an <AFI, SAFI> from the neighbor before the speaker
2456 * receives the EoR for the given <AFI, SAFI> from the
2457 * neighbor.
2458 */
2459 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)
2460 && !CHECK_FLAG(peer->af_sflags[afi][safi],
2461 PEER_STATUS_EOR_RECEIVED)) {
2462 if (bgp_debug_neighbor_events(peer))
2463 zlog_debug(
f70c91dc
DA
2464 "%pBP rcvd route-refresh (BoRR) for %s/%s before EoR",
2465 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2466 return BGP_PACKET_NOOP;
2467 }
2468
2469 if (peer->t_refresh_stalepath) {
2470 if (bgp_debug_neighbor_events(peer))
2471 zlog_debug(
f70c91dc
DA
2472 "%pBP rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
2473 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2474 return BGP_PACKET_NOOP;
2475 }
2476
2477 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_BORR_RECEIVED);
2478 UNSET_FLAG(peer->af_sflags[afi][safi],
2479 PEER_STATUS_EORR_RECEIVED);
2480
2481 /* When a BGP speaker receives a BoRR message from
2482 * a peer, it MUST mark all the routes with the given
2483 * Address Family Identifier and Subsequent Address
2484 * Family Identifier, <AFI, SAFI> [RFC2918], from
2485 * that peer as stale.
2486 */
2487 if (peer_active_nego(peer)) {
2488 SET_FLAG(peer->af_sflags[afi][safi],
2489 PEER_STATUS_ENHANCED_REFRESH);
2490 bgp_set_stale_route(peer, afi, safi);
2491 }
2492
feb17238 2493 if (peer_established(peer))
9af52ccf
DA
2494 thread_add_timer(bm->master,
2495 bgp_refresh_stalepath_timer_expire,
2496 paf, peer->bgp->stalepath_time,
2497 &peer->t_refresh_stalepath);
2498
2499 if (bgp_debug_neighbor_events(peer))
2500 zlog_debug(
f70c91dc
DA
2501 "%pBP rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
2502 peer, afi2str(afi), safi2str(safi),
9af52ccf
DA
2503 peer->bgp->stalepath_time);
2504 } else if (subtype == BGP_ROUTE_REFRESH_EORR) {
2505 if (!peer->t_refresh_stalepath) {
2506 zlog_err(
f70c91dc
DA
2507 "%pBP rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
2508 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2509 return BGP_PACKET_NOOP;
2510 }
2511
fa5806c3 2512 THREAD_OFF(peer->t_refresh_stalepath);
9af52ccf
DA
2513
2514 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EORR_RECEIVED);
2515 UNSET_FLAG(peer->af_sflags[afi][safi],
2516 PEER_STATUS_BORR_RECEIVED);
2517
2518 if (bgp_debug_neighbor_events(peer))
2519 zlog_debug(
f70c91dc
DA
2520 "%pBP rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
2521 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2522
2523 if (peer->nsf[afi][safi])
2524 bgp_clear_stale_route(peer, afi, safi);
2525 } else {
bcbeb3f9 2526 if (bgp_debug_neighbor_events(peer))
a7d91a8c 2527 zlog_debug(
f70c91dc
DA
2528 "%pBP rcvd route-refresh (REQUEST) for %s/%s",
2529 peer, afi2str(afi), safi2str(safi));
bcbeb3f9 2530
9af52ccf
DA
2531 /* In response to a "normal route refresh request" from the
2532 * peer, the speaker MUST send a BoRR message.
2533 */
2534 if (CHECK_FLAG(peer->cap, PEER_CAP_ENHANCED_RR_RCV)) {
2535 /* For a BGP speaker that supports the BGP Graceful
2536 * Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
2537 * to a neighbor before it sends the EoR for the
2538 * <AFI, SAFI> to the neighbor.
2539 */
2540 if (!CHECK_FLAG(peer->af_sflags[afi][safi],
2541 PEER_STATUS_EOR_SEND)) {
2542 if (bgp_debug_neighbor_events(peer))
2543 zlog_debug(
f70c91dc
DA
2544 "%pBP rcvd route-refresh (REQUEST) for %s/%s before EoR",
2545 peer, afi2str(afi),
2546 safi2str(safi));
9af52ccf
DA
2547 return BGP_PACKET_NOOP;
2548 }
2549
2550 bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
2551 BGP_ROUTE_REFRESH_BORR);
2552
2553 if (bgp_debug_neighbor_events(peer))
2554 zlog_debug(
f70c91dc
DA
2555 "%pBP sending route-refresh (BoRR) for %s/%s",
2556 peer, afi2str(afi), safi2str(safi));
9af52ccf
DA
2557
2558 /* Set flag Ready-To-Send to know when we can send EoRR
2559 * message.
2560 */
2561 SET_FLAG(peer->af_sflags[afi][safi],
2562 PEER_STATUS_BORR_SEND);
2563 UNSET_FLAG(peer->af_sflags[afi][safi],
2564 PEER_STATUS_EORR_SEND);
2565 }
2566 }
2567
d62a17ae 2568 /* Perform route refreshment to the peer */
e1a32ec1 2569 bgp_announce_route(peer, afi, safi, force_update);
d8151687
QY
2570
2571 /* No FSM action necessary */
2572 return BGP_PACKET_NOOP;
718e3744 2573}
2574
d8151687
QY
2575/**
2576 * Parse BGP CAPABILITY message for peer.
2577 *
2578 * @param peer
2579 * @param size size of the packet
2580 * @return as in summary
2581 */
d7c0a89a 2582static int bgp_capability_msg_parse(struct peer *peer, uint8_t *pnt,
d62a17ae 2583 bgp_size_t length)
718e3744 2584{
d7c0a89a 2585 uint8_t *end;
d62a17ae 2586 struct capability_mp_data mpc;
2587 struct capability_header *hdr;
d7c0a89a 2588 uint8_t action;
d62a17ae 2589 iana_afi_t pkt_afi;
2590 afi_t afi;
5c525538
RW
2591 iana_safi_t pkt_safi;
2592 safi_t safi;
d62a17ae 2593
2594 end = pnt + length;
2595
2596 while (pnt < end) {
2597 /* We need at least action, capability code and capability
2598 * length. */
2599 if (pnt + 3 > end) {
2600 zlog_info("%s Capability length error", peer->host);
0e35025e
DA
2601 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2602 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
d8151687 2603 return BGP_Stop;
d62a17ae 2604 }
2605 action = *pnt;
2606 hdr = (struct capability_header *)(pnt + 1);
2607
2608 /* Action value check. */
2609 if (action != CAPABILITY_ACTION_SET
2610 && action != CAPABILITY_ACTION_UNSET) {
2611 zlog_info("%s Capability Action Value error %d",
2612 peer->host, action);
0e35025e
DA
2613 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2614 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
d8151687 2615 return BGP_Stop;
d62a17ae 2616 }
2617
2618 if (bgp_debug_neighbor_events(peer))
2619 zlog_debug(
2620 "%s CAPABILITY has action: %d, code: %u, length %u",
2621 peer->host, action, hdr->code, hdr->length);
2622
ff6db102
DS
2623 if (hdr->length < sizeof(struct capability_mp_data)) {
2624 zlog_info(
2625 "%pBP Capability structure is not properly filled out, expected at least %zu bytes but header length specified is %d",
2626 peer, sizeof(struct capability_mp_data),
2627 hdr->length);
2628 return BGP_Stop;
2629 }
2630
d62a17ae 2631 /* Capability length check. */
2632 if ((pnt + hdr->length + 3) > end) {
2633 zlog_info("%s Capability length error", peer->host);
0e35025e
DA
2634 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2635 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
d8151687 2636 return BGP_Stop;
d62a17ae 2637 }
2638
2639 /* Fetch structure to the byte stream. */
2640 memcpy(&mpc, pnt + 3, sizeof(struct capability_mp_data));
d2b6417b 2641 pnt += hdr->length + 3;
d62a17ae 2642
2643 /* We know MP Capability Code. */
2644 if (hdr->code == CAPABILITY_CODE_MP) {
2645 pkt_afi = ntohs(mpc.afi);
2646 pkt_safi = mpc.safi;
2647
2648 /* Ignore capability when override-capability is set. */
2649 if (CHECK_FLAG(peer->flags,
2650 PEER_FLAG_OVERRIDE_CAPABILITY))
2651 continue;
2652
2653 /* Convert AFI, SAFI to internal values. */
2654 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi,
2655 &safi)) {
2656 if (bgp_debug_neighbor_events(peer))
2657 zlog_debug(
3efd0893 2658 "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)",
748a041f
DS
2659 peer->host,
2660 iana_afi2str(pkt_afi),
2661 iana_safi2str(pkt_safi));
d62a17ae 2662 continue;
2663 }
2664
2665 /* Address family check. */
2666 if (bgp_debug_neighbor_events(peer))
2667 zlog_debug(
c386cdd8 2668 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
d62a17ae 2669 peer->host,
2670 action == CAPABILITY_ACTION_SET
2671 ? "Advertising"
2672 : "Removing",
c386cdd8
DA
2673 iana_afi2str(pkt_afi),
2674 iana_safi2str(pkt_safi));
d62a17ae 2675
2676 if (action == CAPABILITY_ACTION_SET) {
2677 peer->afc_recv[afi][safi] = 1;
2678 if (peer->afc[afi][safi]) {
2679 peer->afc_nego[afi][safi] = 1;
e1a32ec1
DS
2680 bgp_announce_route(peer, afi, safi,
2681 false);
d62a17ae 2682 }
2683 } else {
2684 peer->afc_recv[afi][safi] = 0;
2685 peer->afc_nego[afi][safi] = 0;
2686
2687 if (peer_active_nego(peer))
2688 bgp_clear_route(peer, afi, safi);
2689 else
d8151687 2690 return BGP_Stop;
d62a17ae 2691 }
2692 } else {
ade6974d 2693 flog_warn(
e50f7cfd 2694 EC_BGP_UNRECOGNIZED_CAPABILITY,
ade6974d
QY
2695 "%s unrecognized capability code: %d - ignored",
2696 peer->host, hdr->code);
d62a17ae 2697 }
d62a17ae 2698 }
d8151687
QY
2699
2700 /* No FSM action necessary */
2701 return BGP_PACKET_NOOP;
718e3744 2702}
2703
d8151687
QY
2704/**
2705 * Parse BGP CAPABILITY message for peer.
01b7ce2d 2706 *
d8151687
QY
2707 * Exported for unit testing.
2708 *
2709 * @param peer
2710 * @param size size of the packet
2711 * @return as in summary
01b7ce2d 2712 */
d62a17ae 2713int bgp_capability_receive(struct peer *peer, bgp_size_t size)
718e3744 2714{
d7c0a89a 2715 uint8_t *pnt;
d62a17ae 2716
2717 /* Fetch pointer. */
424ab01d 2718 pnt = stream_pnt(peer->curr);
d62a17ae 2719
2720 if (bgp_debug_neighbor_events(peer))
2721 zlog_debug("%s rcv CAPABILITY", peer->host);
2722
2723 /* If peer does not have the capability, send notification. */
2724 if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV)) {
e50f7cfd 2725 flog_err(EC_BGP_NO_CAP,
1c50c1c0
QY
2726 "%s [Error] BGP dynamic capability is not enabled",
2727 peer->host);
d62a17ae 2728 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2729 BGP_NOTIFY_HEADER_BAD_MESTYPE);
d8151687 2730 return BGP_Stop;
d62a17ae 2731 }
2732
2733 /* Status must be Established. */
feb17238 2734 if (!peer_established(peer)) {
af4c2728 2735 flog_err(
e50f7cfd 2736 EC_BGP_NO_CAP,
d62a17ae 2737 "%s [Error] Dynamic capability packet received under status %s",
2738 peer->host,
2739 lookup_msg(bgp_status_msg, peer->status, NULL));
0e35025e 2740 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
3893aeee 2741 bgp_fsm_error_subcode(peer->status));
d8151687 2742 return BGP_Stop;
d62a17ae 2743 }
2744
2745 /* Parse packet. */
2746 return bgp_capability_msg_parse(peer, pnt, size);
718e3744 2747}
6b0655a2 2748
d8151687
QY
2749/**
2750 * Processes a peer's input buffer.
2751 *
2752 * This function sidesteps the event loop and directly calls bgp_event_update()
2753 * after processing each BGP message. This is necessary to ensure proper
2754 * ordering of FSM events and unifies the behavior that was present previously,
2755 * whereby some of the packet handling functions would update the FSM and some
2756 * would not, making event flow difficult to understand. Please think twice
2757 * before hacking this.
2758 *
2759 * Thread type: THREAD_EVENT
2760 * @param thread
2761 * @return 0
2762 */
cc9f21da 2763void bgp_process_packet(struct thread *thread)
718e3744 2764{
424ab01d 2765 /* Yes first of all get peer pointer. */
d8151687
QY
2766 struct peer *peer; // peer
2767 uint32_t rpkt_quanta_old; // how many packets to read
2768 int fsm_update_result; // return code of bgp_event_update()
2769 int mprc; // message processing return code
555e09d4 2770
424ab01d 2771 peer = THREAD_ARG(thread);
555e09d4
QY
2772 rpkt_quanta_old = atomic_load_explicit(&peer->bgp->rpkt_quanta,
2773 memory_order_relaxed);
e3c7270d 2774 fsm_update_result = 0;
555e09d4 2775
424ab01d 2776 /* Guard against scheduled events that occur after peer deletion. */
9eb217ff 2777 if (peer->status == Deleted || peer->status == Clearing)
cc9f21da 2778 return;
718e3744 2779
555e09d4 2780 unsigned int processed = 0;
d62a17ae 2781
555e09d4 2782 while (processed < rpkt_quanta_old) {
d7c0a89a 2783 uint8_t type = 0;
9eb217ff
QY
2784 bgp_size_t size;
2785 char notify_data_length[2];
d62a17ae 2786
cb1991af 2787 frr_with_mutex (&peer->io_mtx) {
9eb217ff
QY
2788 peer->curr = stream_fifo_pop(peer->ibuf);
2789 }
d62a17ae 2790
9eb217ff 2791 if (peer->curr == NULL) // no packets to process, hmm...
cc9f21da 2792 return;
d62a17ae 2793
9eb217ff
QY
2794 /* skip the marker and copy the packet length */
2795 stream_forward_getp(peer->curr, BGP_MARKER_SIZE);
2796 memcpy(notify_data_length, stream_pnt(peer->curr), 2);
2797
2798 /* read in the packet length and type */
2799 size = stream_getw(peer->curr);
2800 type = stream_getc(peer->curr);
2801
584470fb 2802 hook_call(bgp_packet_dump, peer, type, size, peer->curr);
9eb217ff
QY
2803
2804 /* adjust size to exclude the marker + length + type */
2805 size -= BGP_HEADER_SIZE;
2806
2807 /* Read rest of the packet and call each sort of packet routine
2808 */
2809 switch (type) {
2810 case BGP_MSG_OPEN:
c7bb4f00 2811 frrtrace(2, frr_bgp, open_process, peer, size);
0112e9e0
QY
2812 atomic_fetch_add_explicit(&peer->open_in, 1,
2813 memory_order_relaxed);
d8151687
QY
2814 mprc = bgp_open_receive(peer, size);
2815 if (mprc == BGP_Stop)
af4c2728 2816 flog_err(
e50f7cfd 2817 EC_BGP_PKT_OPEN,
d8151687 2818 "%s: BGP OPEN receipt failed for peer: %s",
0767b4f3 2819 __func__, peer->host);
9eb217ff
QY
2820 break;
2821 case BGP_MSG_UPDATE:
c7bb4f00 2822 frrtrace(2, frr_bgp, update_process, peer, size);
0112e9e0
QY
2823 atomic_fetch_add_explicit(&peer->update_in, 1,
2824 memory_order_relaxed);
9eb217ff 2825 peer->readtime = monotime(NULL);
d8151687
QY
2826 mprc = bgp_update_receive(peer, size);
2827 if (mprc == BGP_Stop)
af4c2728 2828 flog_err(
e50f7cfd 2829 EC_BGP_UPDATE_RCV,
d8151687 2830 "%s: BGP UPDATE receipt failed for peer: %s",
0767b4f3 2831 __func__, peer->host);
9eb217ff
QY
2832 break;
2833 case BGP_MSG_NOTIFY:
c7bb4f00 2834 frrtrace(2, frr_bgp, notification_process, peer, size);
0112e9e0
QY
2835 atomic_fetch_add_explicit(&peer->notify_in, 1,
2836 memory_order_relaxed);
d8151687
QY
2837 mprc = bgp_notify_receive(peer, size);
2838 if (mprc == BGP_Stop)
af4c2728 2839 flog_err(
e50f7cfd 2840 EC_BGP_NOTIFY_RCV,
d8151687 2841 "%s: BGP NOTIFY receipt failed for peer: %s",
0767b4f3 2842 __func__, peer->host);
9eb217ff
QY
2843 break;
2844 case BGP_MSG_KEEPALIVE:
c7bb4f00 2845 frrtrace(2, frr_bgp, keepalive_process, peer, size);
9eb217ff 2846 peer->readtime = monotime(NULL);
0112e9e0
QY
2847 atomic_fetch_add_explicit(&peer->keepalive_in, 1,
2848 memory_order_relaxed);
d8151687
QY
2849 mprc = bgp_keepalive_receive(peer, size);
2850 if (mprc == BGP_Stop)
af4c2728 2851 flog_err(
e50f7cfd 2852 EC_BGP_KEEP_RCV,
d8151687 2853 "%s: BGP KEEPALIVE receipt failed for peer: %s",
0767b4f3 2854 __func__, peer->host);
9eb217ff
QY
2855 break;
2856 case BGP_MSG_ROUTE_REFRESH_NEW:
2857 case BGP_MSG_ROUTE_REFRESH_OLD:
c7bb4f00 2858 frrtrace(2, frr_bgp, refresh_process, peer, size);
0112e9e0
QY
2859 atomic_fetch_add_explicit(&peer->refresh_in, 1,
2860 memory_order_relaxed);
d8151687
QY
2861 mprc = bgp_route_refresh_receive(peer, size);
2862 if (mprc == BGP_Stop)
af4c2728 2863 flog_err(
e50f7cfd 2864 EC_BGP_RFSH_RCV,
d8151687 2865 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
0767b4f3 2866 __func__, peer->host);
9eb217ff
QY
2867 break;
2868 case BGP_MSG_CAPABILITY:
c7bb4f00 2869 frrtrace(2, frr_bgp, capability_process, peer, size);
0112e9e0
QY
2870 atomic_fetch_add_explicit(&peer->dynamic_cap_in, 1,
2871 memory_order_relaxed);
d8151687
QY
2872 mprc = bgp_capability_receive(peer, size);
2873 if (mprc == BGP_Stop)
af4c2728 2874 flog_err(
e50f7cfd 2875 EC_BGP_CAP_RCV,
d8151687 2876 "%s: BGP CAPABILITY receipt failed for peer: %s",
0767b4f3 2877 __func__, peer->host);
9eb217ff 2878 break;
e3c7270d 2879 default:
db878db0
QY
2880 /* Suppress uninitialized variable warning */
2881 mprc = 0;
5041dc4f 2882 (void)mprc;
becedef6
QY
2883 /*
2884 * The message type should have been sanitized before
2885 * we ever got here. Receipt of a message with an
2886 * invalid header at this point is indicative of a
2887 * security issue.
2888 */
e3c7270d 2889 assert (!"Message of invalid type received during input processing");
9eb217ff
QY
2890 }
2891
d8151687
QY
2892 /* delete processed packet */
2893 stream_free(peer->curr);
2894 peer->curr = NULL;
2895 processed++;
9eb217ff 2896
d8151687
QY
2897 /* Update FSM */
2898 if (mprc != BGP_PACKET_NOOP)
2899 fsm_update_result = bgp_event_update(peer, mprc);
e3c7270d
QY
2900 else
2901 continue;
d8151687 2902
becedef6
QY
2903 /*
2904 * If peer was deleted, do not process any more packets. This
2905 * is usually due to executing BGP_Stop or a stub deletion.
2906 */
d8151687
QY
2907 if (fsm_update_result == FSM_PEER_TRANSFERRED
2908 || fsm_update_result == FSM_PEER_STOPPED)
2909 break;
d62a17ae 2910 }
2911
d8151687
QY
2912 if (fsm_update_result != FSM_PEER_TRANSFERRED
2913 && fsm_update_result != FSM_PEER_STOPPED) {
cb1991af 2914 frr_with_mutex (&peer->io_mtx) {
becedef6
QY
2915 // more work to do, come back later
2916 if (peer->ibuf->count > 0)
e0d550df 2917 thread_add_event(
4af76660
QY
2918 bm->master, bgp_process_packet, peer, 0,
2919 &peer->t_process_packet);
d8151687 2920 }
718e3744 2921 }
718e3744 2922}
9e3b51a7 2923
2924/* Send EOR when routes are processed by selection deferral timer */
2925void bgp_send_delayed_eor(struct bgp *bgp)
2926{
2927 struct peer *peer;
2928 struct listnode *node, *nnode;
2929
2930 /* EOR message sent in bgp_write_proceed_actions */
2931 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
2932 bgp_write_proceed_actions(peer);
2933}
6af96fa3
MS
2934
2935/*
2936 * Task callback to handle socket error encountered in the io pthread. We avoid
2937 * having the io pthread try to enqueue fsm events or mess with the peer
2938 * struct.
2939 */
cc9f21da 2940void bgp_packet_process_error(struct thread *thread)
6af96fa3
MS
2941{
2942 struct peer *peer;
2943 int code;
2944
2945 peer = THREAD_ARG(thread);
2946 code = THREAD_VAL(thread);
2947
2948 if (bgp_debug_neighbor_events(peer))
2949 zlog_debug("%s [Event] BGP error %d on fd %d",
046bb347 2950 peer->host, code, peer->fd);
6af96fa3
MS
2951
2952 /* Closed connection or error on the socket */
feb17238 2953 if (peer_established(peer)) {
6af96fa3
MS
2954 if ((CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART)
2955 || CHECK_FLAG(peer->flags,
2956 PEER_FLAG_GRACEFUL_RESTART_HELPER))
2957 && CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) {
2958 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2959 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2960 } else
2961 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2962 }
2963
2964 bgp_event_update(peer, code);
6af96fa3 2965}