forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbgp_packet.c
4175 lines (3653 loc) · 118 KB
/
bgp_packet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP packet management routine.
* Contains utility functions for constructing and consuming BGP messages.
* Copyright (C) 2017 Cumulus Networks
* Copyright (C) 1999 Kunihiro Ishiguro
*/
#include <zebra.h>
#include <sys/time.h>
#include "frrevent.h"
#include "stream.h"
#include "network.h"
#include "prefix.h"
#include "command.h"
#include "log.h"
#include "memory.h"
#include "sockunion.h" /* for inet_ntop () */
#include "sockopt.h"
#include "linklist.h"
#include "plist.h"
#include "queue.h"
#include "filter.h"
#include "lib_errors.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_addpath.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_dump.h"
#include "bgpd/bgp_bmp.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_packet.h"
#include "bgpd/bgp_open.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_community.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_lcommunity.h"
#include "bgpd/bgp_network.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_advertise.h"
#include "bgpd/bgp_vty.h"
#include "bgpd/bgp_updgrp.h"
#include "bgpd/bgp_label.h"
#include "bgpd/bgp_io.h"
#include "bgpd/bgp_keepalives.h"
#include "bgpd/bgp_flowspec.h"
#include "bgpd/bgp_trace.h"
DEFINE_HOOK(bgp_packet_dump,
(struct peer *peer, uint8_t type, bgp_size_t size,
struct stream *s),
(peer, type, size, s));
DEFINE_HOOK(bgp_packet_send,
(struct peer *peer, uint8_t type, bgp_size_t size,
struct stream *s),
(peer, type, size, s));
/**
* Sets marker and type fields for a BGP message.
*
* @param s the stream containing the packet
* @param type the packet type
* @return the size of the stream
*/
int bgp_packet_set_marker(struct stream *s, uint8_t type)
{
int i;
/* Fill in marker. */
for (i = 0; i < BGP_MARKER_SIZE; i++)
stream_putc(s, 0xff);
/* Dummy total length. This field is should be filled in later on. */
stream_putw(s, 0);
/* BGP packet type. */
stream_putc(s, type);
/* Return current stream size. */
return stream_get_endp(s);
}
/**
* Sets size field for a BGP message.
*
* Size field is set to the size of the stream passed.
*
* @param s the stream containing the packet
*/
void bgp_packet_set_size(struct stream *s)
{
int cp;
/* Preserve current pointer. */
cp = stream_get_endp(s);
stream_putw_at(s, BGP_MARKER_SIZE, cp);
}
/*
* Push a packet onto the beginning of the peer's output queue.
* This function acquires the peer's write mutex before proceeding.
*/
static void bgp_packet_add(struct peer_connection *connection,
struct peer *peer, struct stream *s)
{
intmax_t delta;
uint32_t holdtime;
intmax_t sendholdtime;
frr_with_mutex (&connection->io_mtx) {
/* if the queue is empty, reset the "last OK" timestamp to
* now, otherwise if we write another packet immediately
* after it'll get confused
*/
if (!stream_fifo_count_safe(connection->obuf))
peer->last_sendq_ok = monotime(NULL);
stream_fifo_push(connection->obuf, s);
}
delta = monotime(NULL) - peer->last_sendq_ok;
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
holdtime = atomic_load_explicit(&peer->holdtime, memory_order_relaxed);
else
holdtime = peer->bgp->default_holdtime;
sendholdtime = holdtime * 2;
/* Note that when we're here, we're adding some packet to the
* OutQ. That includes keepalives when there is nothing to
* do, so there's a guarantee we pass by here once in a while.
*
* That implies there is no need to go set up another separate
* timer that ticks down SendHoldTime, as we'll be here sooner
* or later anyway and will see the checks below failing.
*/
if (!holdtime) {
/* no holdtime, do nothing. */
} else if (delta > sendholdtime) {
flog_err(EC_BGP_SENDQ_STUCK_PROPER,
"%pBP has not made any SendQ progress for 2 holdtimes (%jds), terminating session",
peer, sendholdtime);
event_add_event(bm->master, bgp_event_stop_with_notify, connection, 0,
&connection->t_stop_with_notify);
} else if (delta > (intmax_t)holdtime && monotime(NULL) - peer->last_sendq_warn > 5) {
flog_warn(EC_BGP_SENDQ_STUCK_WARN,
"%pBP has not made any SendQ progress for 1 holdtime (%us), peer overloaded?",
peer, holdtime);
peer->last_sendq_warn = monotime(NULL);
}
}
static struct stream *bgp_update_packet_eor(struct peer *peer, afi_t afi,
safi_t safi)
{
struct stream *s;
iana_afi_t pkt_afi = IANA_AFI_IPV4;
iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
if (DISABLE_BGP_ANNOUNCE)
return NULL;
if (bgp_debug_neighbor_events(peer))
zlog_debug("send End-of-RIB for %s to %s",
get_afi_safi_str(afi, safi, false), peer->host);
s = stream_new(peer->max_packet_size);
/* Make BGP update packet. */
bgp_packet_set_marker(s, BGP_MSG_UPDATE);
/* Unfeasible Routes Length */
stream_putw(s, 0);
if (afi == AFI_IP && safi == SAFI_UNICAST) {
/* Total Path Attribute Length */
stream_putw(s, 0);
} else {
/* Convert AFI, SAFI to values for packet. */
bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
/* Total Path Attribute Length */
stream_putw(s, 6);
stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI);
stream_putc(s, 3);
stream_putw(s, pkt_afi);
stream_putc(s, pkt_safi);
}
bgp_packet_set_size(s);
return s;
}
/* Called when there is a change in the EOR(implicit or explicit) status of a
* peer. Ends the update-delay if all expected peers are done with EORs. */
void bgp_check_update_delay(struct bgp *bgp)
{
struct listnode *node, *nnode;
struct peer *peer = NULL;
if (bgp_debug_neighbor_events(peer))
zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
bgp->established, bgp->restarted_peers,
bgp->implicit_eors, bgp->explicit_eors);
if (bgp->established
<= bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) {
/*
* This is an extra sanity check to make sure we wait for all
* the eligible configured peers. This check is performed if
* establish wait timer is on, or establish wait option is not
* given with the update-delay command
*/
if (bgp->t_establish_wait
|| (bgp->v_establish_wait == bgp->v_update_delay))
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (CHECK_FLAG(peer->flags,
PEER_FLAG_CONFIG_NODE)
&& !CHECK_FLAG(peer->flags,
PEER_FLAG_SHUTDOWN)
&& !CHECK_FLAG(peer->bgp->flags,
BGP_FLAG_SHUTDOWN)
&& !peer->update_delay_over) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
" Peer %s pending, continuing read-only mode",
peer->host);
return;
}
}
zlog_info(
"Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
bgp->restarted_peers, bgp->implicit_eors,
bgp->explicit_eors);
bgp_update_delay_end(bgp);
}
}
/*
* Called if peer is known to have restarted. The restart-state bit in
* Graceful-Restart capability is used for that
*/
void bgp_update_restarted_peers(struct peer *peer)
{
if (!bgp_update_delay_active(peer->bgp))
return; /* BGP update delay has ended */
if (peer->update_delay_over)
return; /* This peer has already been considered */
if (bgp_debug_neighbor_events(peer))
zlog_debug("Peer %s: Checking restarted", peer->host);
if (peer_established(peer->connection)) {
peer->update_delay_over = 1;
peer->bgp->restarted_peers++;
bgp_check_update_delay(peer->bgp);
}
}
/*
* Called as peer receives a keep-alive. Determines if this occurence can be
* taken as an implicit EOR for this peer.
* NOTE: The very first keep-alive after the Established state of a peer is
* considered implicit EOR for the update-delay purposes
*/
void bgp_update_implicit_eors(struct peer *peer)
{
if (!bgp_update_delay_active(peer->bgp))
return; /* BGP update delay has ended */
if (peer->update_delay_over)
return; /* This peer has already been considered */
if (bgp_debug_neighbor_events(peer))
zlog_debug("Peer %s: Checking implicit EORs", peer->host);
if (peer_established(peer->connection)) {
peer->update_delay_over = 1;
peer->bgp->implicit_eors++;
bgp_check_update_delay(peer->bgp);
}
}
/*
* Should be called only when there is a change in the EOR_RECEIVED status
* for any afi/safi on a peer.
*/
static void bgp_update_explicit_eors(struct peer *peer)
{
afi_t afi;
safi_t safi;
if (!bgp_update_delay_active(peer->bgp))
return; /* BGP update delay has ended */
if (peer->update_delay_over)
return; /* This peer has already been considered */
if (bgp_debug_neighbor_events(peer))
zlog_debug("Peer %s: Checking explicit EORs", peer->host);
FOREACH_AFI_SAFI (afi, safi) {
if (peer->afc_nego[afi][safi]
&& !CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_RECEIVED)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
" afi %d safi %d didn't receive EOR",
afi, safi);
return;
}
}
peer->update_delay_over = 1;
peer->bgp->explicit_eors++;
bgp_check_update_delay(peer->bgp);
}
/**
* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
*
* mp_withdraw, if set, is used to nullify attr structure on most of the
* calling safi function and for evpn, passed as parameter
*/
int bgp_nlri_parse(struct peer *peer, struct attr *attr,
struct bgp_nlri *packet, bool mp_withdraw)
{
switch (packet->safi) {
case SAFI_UNICAST:
case SAFI_MULTICAST:
return bgp_nlri_parse_ip(peer, mp_withdraw ? NULL : attr,
packet);
case SAFI_LABELED_UNICAST:
return bgp_nlri_parse_label(peer, mp_withdraw ? NULL : attr,
packet);
case SAFI_MPLS_VPN:
return bgp_nlri_parse_vpn(peer, mp_withdraw ? NULL : attr,
packet);
case SAFI_EVPN:
return bgp_nlri_parse_evpn(peer, attr, packet, mp_withdraw);
case SAFI_FLOWSPEC:
return bgp_nlri_parse_flowspec(peer, attr, packet, mp_withdraw);
}
return BGP_NLRI_PARSE_ERROR;
}
/*
* Check if route-refresh request from peer is pending (received before EoR),
* and process it now.
*/
static void bgp_process_pending_refresh(struct peer *peer, afi_t afi,
safi_t safi)
{
if (CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_REFRESH_PENDING)) {
UNSET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_REFRESH_PENDING);
bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
BGP_ROUTE_REFRESH_BORR);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP sending route-refresh (BoRR) for %s/%s (for pending REQUEST)",
peer, afi2str(afi), safi2str(safi));
SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_BORR_SEND);
UNSET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EORR_SEND);
bgp_announce_route(peer, afi, safi, true);
}
}
/*
* Checks a variety of conditions to determine whether the peer needs to be
* rescheduled for packet generation again, and does so if necessary.
*
* @param peer to check for rescheduling
*/
static void bgp_write_proceed_actions(struct peer *peer)
{
afi_t afi;
safi_t safi;
struct peer_af *paf;
struct bpacket *next_pkt;
struct update_subgroup *subgrp;
enum bgp_af_index index;
struct peer_connection *connection = peer->connection;
for (index = BGP_AF_START; index < BGP_AF_MAX; index++) {
paf = peer->peer_af_array[index];
if (!paf)
continue;
subgrp = paf->subgroup;
if (!subgrp)
continue;
next_pkt = paf->next_pkt_to_send;
if (next_pkt && next_pkt->buffer) {
BGP_TIMER_ON(connection->t_generate_updgrp_packets,
bgp_generate_updgrp_packets, 0);
return;
}
/* No packets readily available for AFI/SAFI, are there
* subgroup packets
* that need to be generated? */
if (bpacket_queue_is_full(SUBGRP_INST(subgrp),
SUBGRP_PKTQ(subgrp))
|| subgroup_packets_to_build(subgrp)) {
BGP_TIMER_ON(connection->t_generate_updgrp_packets,
bgp_generate_updgrp_packets, 0);
return;
}
afi = paf->afi;
safi = paf->safi;
/* No packets to send, see if EOR is pending */
if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)) {
if (!subgrp->t_coalesce && peer->afc_nego[afi][safi]
&& peer->synctime
&& !CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_SEND)
&& safi != SAFI_MPLS_VPN) {
BGP_TIMER_ON(connection->t_generate_updgrp_packets,
bgp_generate_updgrp_packets, 0);
return;
}
}
}
}
/*
* Generate advertisement information (withdraws, updates, EOR) from each
* update group a peer belongs to, encode this information into packets, and
* enqueue the packets onto the peer's output buffer.
*/
void bgp_generate_updgrp_packets(struct event *thread)
{
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
struct stream *s;
struct peer_af *paf;
struct bpacket *next_pkt;
uint32_t wpq;
uint32_t generated = 0;
afi_t afi;
safi_t safi;
wpq = atomic_load_explicit(&peer->bgp->wpkt_quanta,
memory_order_relaxed);
/*
* The code beyond this part deals with update packets, proceed only
* if peer is Established and updates are not on hold (as part of
* update-delay processing).
*/
if (!peer_established(peer->connection))
return;
if ((peer->bgp->main_peers_update_hold)
|| bgp_update_delay_active(peer->bgp))
return;
if (peer->connection->t_routeadv)
return;
/*
* Since the following is a do while loop
* let's stop adding to the outq if we are
* already at the limit.
*/
if (connection->obuf->count >= bm->outq_limit) {
bgp_write_proceed_actions(peer);
return;
}
do {
enum bgp_af_index index;
s = NULL;
for (index = BGP_AF_START; index < BGP_AF_MAX; index++) {
paf = peer->peer_af_array[index];
if (!paf || !PAF_SUBGRP(paf))
continue;
afi = paf->afi;
safi = paf->safi;
next_pkt = paf->next_pkt_to_send;
/*
* Try to generate a packet for the peer if we are at
* the end of the list. Always try to push out
* WITHDRAWs first.
*/
if (!next_pkt || !next_pkt->buffer) {
next_pkt = subgroup_withdraw_packet(
PAF_SUBGRP(paf));
if (!next_pkt || !next_pkt->buffer)
subgroup_update_packet(PAF_SUBGRP(paf));
next_pkt = paf->next_pkt_to_send;
}
/*
* If we still don't have a packet to send to the peer,
* then try to find out out if we have to send eor or
* if not, skip to the next AFI, SAFI. Don't send the
* EOR prematurely; if the subgroup's coalesce timer is
* running, the adjacency-out structure is not created
* yet.
*/
if (!next_pkt || !next_pkt->buffer) {
if (!paf->t_announce_route) {
/* Make sure we supress BGP UPDATES
* for normal processing later again.
*/
UNSET_FLAG(paf->subgroup->sflags,
SUBGRP_STATUS_FORCE_UPDATES);
/* If route-refresh BoRR message was
* already sent and we are done with
* re-announcing tables for a decent
* afi/safi, we ready to send
* EoRR request.
*/
if (CHECK_FLAG(
peer->af_sflags[afi][safi],
PEER_STATUS_BORR_SEND)) {
bgp_route_refresh_send(
peer, afi, safi, 0, 0,
0,
BGP_ROUTE_REFRESH_EORR);
SET_FLAG(peer->af_sflags[afi]
[safi],
PEER_STATUS_EORR_SEND);
UNSET_FLAG(
peer->af_sflags[afi]
[safi],
PEER_STATUS_BORR_SEND);
if (bgp_debug_neighbor_events(
peer))
zlog_debug(
"%pBP sending route-refresh (EoRR) for %s/%s",
peer,
afi2str(afi),
safi2str(safi));
}
}
/* rfc4724 says:
* Although the End-of-RIB marker is
* specified for the purpose of BGP
* graceful restart, it is noted that
* the generation of such a marker upon
* completion of the initial update would
* be useful for routing convergence in
* general, and thus the practice is
* recommended.
*/
if (!(PAF_SUBGRP(paf))->t_coalesce &&
peer->afc_nego[afi][safi] &&
peer->synctime &&
!CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_SEND)) {
/* If EOR is disabled, the message is
* not sent.
*/
if (!BGP_SEND_EOR(peer->bgp, afi, safi))
continue;
SET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_SEND);
/* Update EOR send time */
peer->eor_stime[afi][safi] =
monotime(NULL);
BGP_UPDATE_EOR_PKT(peer, afi, safi, s);
bgp_process_pending_refresh(peer, afi,
safi);
}
continue;
}
/* Update packet send time */
peer->pkt_stime[afi][safi] = monotime(NULL);
/* Found a packet template to send, overwrite
* packet with appropriate attributes from peer
* and advance peer */
s = bpacket_reformat_for_peer(next_pkt, paf);
bgp_packet_add(connection, peer, s);
bpacket_queue_advance_peer(paf);
}
} while (s && (++generated < wpq) &&
(connection->obuf->count <= bm->outq_limit));
if (generated)
bgp_writes_on(connection);
bgp_write_proceed_actions(peer);
}
/*
* Creates a BGP Keepalive packet and appends it to the peer's output queue.
*/
void bgp_keepalive_send(struct peer *peer)
{
struct stream *s;
s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
/* Make keepalive packet. */
bgp_packet_set_marker(s, BGP_MSG_KEEPALIVE);
/* Set packet size. */
bgp_packet_set_size(s);
/* Dump packet if debug option is set. */
/* bgp_packet_dump (s); */
if (bgp_debug_keepalive(peer))
zlog_debug("%s sending KEEPALIVE", peer->host);
/* Add packet to the peer. */
bgp_packet_add(peer->connection, peer, s);
bgp_writes_on(peer->connection);
}
struct stream *bgp_open_make(struct peer *peer, uint16_t send_holdtime, as_t local_as,
struct in_addr *id)
{
struct stream *s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
bool ext_opt_params = false;
/* Make open packet. */
bgp_packet_set_marker(s, BGP_MSG_OPEN);
/* Set open packet values. */
stream_putc(s, BGP_VERSION_4); /* BGP version */
stream_putw(s, (local_as <= BGP_AS_MAX) ? (uint16_t)local_as
: BGP_AS_TRANS);
stream_putw(s, send_holdtime); /* Hold Time */
stream_put_in_addr(s, id); /* BGP Identifier */
/* Set capabilities */
if (CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
ext_opt_params = true;
(void)bgp_open_capability(s, peer, ext_opt_params);
} else {
struct stream *tmp = stream_new(STREAM_SIZE(s));
stream_copy(tmp, s);
if (bgp_open_capability(tmp, peer, ext_opt_params) >
BGP_OPEN_NON_EXT_OPT_LEN) {
stream_free(tmp);
ext_opt_params = true;
(void)bgp_open_capability(s, peer, ext_opt_params);
} else {
stream_copy(s, tmp);
stream_free(tmp);
}
}
/* Set BGP packet length. */
bgp_packet_set_size(s);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP fd %d sending OPEN%s, version %d, my as %u, holdtime %d, id %pI4",
peer, peer->connection->fd,
ext_opt_params ? " (Extended)" : "", BGP_VERSION_4,
local_as, send_holdtime, &peer->local_id);
return s;
}
/*
* Creates a BGP Open packet and appends it to the peer's output queue.
* Sets capabilities as necessary.
*/
void bgp_open_send(struct peer_connection *connection)
{
struct stream *s;
uint16_t send_holdtime;
as_t local_as;
struct peer *peer = connection->peer;
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
send_holdtime = peer->holdtime;
else
send_holdtime = peer->bgp->default_holdtime;
/* local-as Change */
if (peer->change_local_as)
local_as = peer->change_local_as;
else
local_as = peer->local_as;
s = bgp_open_make(peer, send_holdtime, local_as, &peer->local_id);
/* Dump packet if debug option is set. */
/* bgp_packet_dump (s); */
hook_call(bgp_packet_send, peer, BGP_MSG_OPEN, stream_get_endp(s), s);
/* Add packet to the peer. */
bgp_packet_add(connection, peer, s);
bgp_writes_on(connection);
}
/*
* Writes NOTIFICATION message directly to a peer socket without waiting for
* the I/O thread.
*
* There must be exactly one stream on the peer->connection->obuf FIFO, and the
* data within this stream must match the format of a BGP NOTIFICATION message.
* Transmission is best-effort.
*
* @requires peer->connection->io_mtx
* @param peer
* @return 0
*/
static void bgp_write_notify(struct peer_connection *connection,
struct peer *peer)
{
int ret, val;
uint8_t type;
struct stream *s;
/* There should be at least one packet. */
s = stream_fifo_pop(connection->obuf);
if (!s)
return;
assert(stream_get_endp(s) >= BGP_HEADER_SIZE);
/*
* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
* we only care about getting a clean shutdown at this point.
*/
ret = write(connection->fd, STREAM_DATA(s), stream_get_endp(s));
/*
* only connection reset/close gets counted as TCP_fatal_error, failure
* to write the entire NOTIFY doesn't get different FSM treatment
*/
if (ret <= 0) {
stream_free(s);
BGP_EVENT_ADD(connection, TCP_fatal_error);
return;
}
/* Disable Nagle, make NOTIFY packet go out right away */
val = 1;
(void)setsockopt(connection->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val,
sizeof(val));
/* Retrieve BGP packet type. */
stream_set_getp(s, BGP_MARKER_SIZE + 2);
type = stream_getc(s);
assert(type == BGP_MSG_NOTIFY);
/* Type should be notify. */
atomic_fetch_add_explicit(&peer->notify_out, 1, memory_order_relaxed);
/* Double start timer. */
peer->v_start *= 2;
/* Overflow check. */
if (peer->v_start >= (60 * 2))
peer->v_start = (60 * 2);
/*
* Handle Graceful Restart case where the state changes to
* Connect instead of Idle
*/
BGP_EVENT_ADD(connection, BGP_Stop);
stream_free(s);
}
/*
* Encapsulate an original BGP CEASE Notification into Hard Reset
*/
static uint8_t *bgp_notify_encapsulate_hard_reset(uint8_t code, uint8_t subcode,
uint8_t *data, size_t datalen)
{
uint8_t *message = XCALLOC(MTYPE_BGP_NOTIFICATION, datalen + 2);
/* ErrCode */
message[0] = code;
/* Subcode */
message[1] = subcode;
/* Data */
if (datalen)
memcpy(message + 2, data, datalen);
return message;
}
/*
* Decapsulate an original BGP CEASE Notification from Hard Reset
*/
struct bgp_notify bgp_notify_decapsulate_hard_reset(struct bgp_notify *notify)
{
struct bgp_notify bn = {};
bn.code = notify->raw_data[0];
bn.subcode = notify->raw_data[1];
bn.length = notify->length - 2;
bn.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, bn.length);
memcpy(bn.raw_data, notify->raw_data + 2, bn.length);
return bn;
}
/* Check if Graceful-Restart N-bit is exchanged */
bool bgp_has_graceful_restart_notification(struct peer *peer)
{
return CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV) &&
CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV);
}
/*
* Check if to send BGP CEASE Notification/Hard Reset?
*/
bool bgp_notify_send_hard_reset(struct peer *peer, uint8_t code,
uint8_t subcode)
{
/* When the "N" bit has been exchanged, a Hard Reset message is used to
* indicate to the peer that the session is to be fully terminated.
*/
if (!bgp_has_graceful_restart_notification(peer))
return false;
/*
* https://datatracker.ietf.org/doc/html/rfc8538#section-5.1
*/
if (code == BGP_NOTIFY_CEASE) {
switch (subcode) {
case BGP_NOTIFY_CEASE_MAX_PREFIX:
case BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN:
case BGP_NOTIFY_CEASE_PEER_UNCONFIG:
case BGP_NOTIFY_CEASE_HARD_RESET:
case BGP_NOTIFY_CEASE_BFD_DOWN:
return true;
case BGP_NOTIFY_CEASE_ADMIN_RESET:
/* Provide user control:
* `bgp hard-adminstrative-reset`
*/
if (CHECK_FLAG(peer->bgp->flags,
BGP_FLAG_HARD_ADMIN_RESET))
return true;
else
return false;
default:
break;
}
}
return false;
}
/*
* Check if received BGP CEASE Notification/Hard Reset?
*/
bool bgp_notify_received_hard_reset(struct peer *peer, uint8_t code,
uint8_t subcode)
{
/* When the "N" bit has been exchanged, a Hard Reset message is used to
* indicate to the peer that the session is to be fully terminated.
*/
if (!bgp_has_graceful_restart_notification(peer))
return false;
if (code == BGP_NOTIFY_CEASE && subcode == BGP_NOTIFY_CEASE_HARD_RESET)
return true;
return false;
}
/*
* Creates a BGP Notify and appends it to the peer's output queue.
*
* This function attempts to write the packet from the thread it is called
* from, to ensure the packet gets out ASAP.
*
* This function may be called from multiple threads. Since the function
* modifies I/O buffer(s) in the peer, these are locked for the duration of the
* call to prevent tampering from other threads.
*
* Delivery of the NOTIFICATION is attempted once and is best-effort. After
* return, the peer structure *must* be reset; no assumptions about session
* state are valid.
*
* @param peer
* @param code BGP error code
* @param sub_code BGP error subcode
* @param data Data portion
* @param datalen length of data portion
*/
static void bgp_notify_send_internal(struct peer_connection *connection,
uint8_t code, uint8_t sub_code,
uint8_t *data, size_t datalen,
bool use_curr)
{
struct stream *s;
struct peer *peer = connection->peer;
bool hard_reset = bgp_notify_send_hard_reset(peer, code, sub_code);
/* Lock I/O mutex to prevent other threads from pushing packets */
frr_mutex_lock_autounlock(&connection->io_mtx);
/* ============================================== */
/* Allocate new stream. */
s = stream_new(peer->max_packet_size);
/* Make notify packet. */
bgp_packet_set_marker(s, BGP_MSG_NOTIFY);
/* Check if we should send Hard Reset Notification or not */
if (hard_reset) {
uint8_t *hard_reset_message = bgp_notify_encapsulate_hard_reset(
code, sub_code, data, datalen);
/* Hard Reset encapsulates another NOTIFICATION message
* in its data portion.
*/
stream_putc(s, BGP_NOTIFY_CEASE);
stream_putc(s, BGP_NOTIFY_CEASE_HARD_RESET);
stream_write(s, hard_reset_message, datalen + 2);
XFREE(MTYPE_BGP_NOTIFICATION, hard_reset_message);
} else {
stream_putc(s, code);
stream_putc(s, sub_code);
if (data)
stream_write(s, data, datalen);
}
/* Set BGP packet length. */
bgp_packet_set_size(s);
/* wipe output buffer */
stream_fifo_clean(connection->obuf);
/*
* If possible, store last packet for debugging purposes. This check is
* in place because we are sometimes called with a doppelganger peer,
* who tends to have a plethora of fields nulled out.
*
* Some callers should not attempt this - the io pthread for example
* should not touch internals of the peer struct.
*/
if (use_curr && peer->curr) {
size_t packetsize = stream_get_endp(peer->curr);
assert(packetsize <= peer->max_packet_size);
if (peer->last_reset_cause)
stream_free(peer->last_reset_cause);
peer->last_reset_cause = stream_dup(peer->curr);
}
/* For debug */
{
struct bgp_notify bgp_notify;
int first = 0;
int i;
char c[4];
bgp_notify.code = code;
bgp_notify.subcode = sub_code;
bgp_notify.data = NULL;
bgp_notify.length = datalen;
bgp_notify.raw_data = data;
peer->notify.code = bgp_notify.code;
peer->notify.subcode = bgp_notify.subcode;
peer->notify.length = bgp_notify.length;
peer->notify.hard_reset = hard_reset;
if (bgp_notify.length && data) {
bgp_notify.data = XMALLOC(MTYPE_BGP_NOTIFICATION,
bgp_notify.length * 3);
for (i = 0; i < bgp_notify.length; i++)
if (first) {
snprintf(c, sizeof(c), " %02x",
data[i]);