Coverage Report

Created: 2026-01-01 06:18

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