-
Notifications
You must be signed in to change notification settings - Fork 74
/
peer.cc
2779 lines (2427 loc) · 70.4 KB
/
peer.cc
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
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
// Copyright (c) 2001-2010 XORP, Inc and Others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2, June
// 1991 as published by the Free Software Foundation. Redistribution
// and/or modification of this program under the terms of any other
// version of the GNU General Public License is not permitted.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
// see the GNU General Public License, Version 2, a copy of which can be
// found in the XORP LICENSE.gpl file.
//
// XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net
// #define DEBUG_LOGGING
// #define DEBUG_PRINT_FUNCTION_NAME
// #define SAVE_PACKETS
#define CHECK_TIME
#include "bgp_module.h"
#include "libxorp/xorp.h"
#include "libxorp/debug.h"
#include "libxorp/xlog.h"
#include "libxorp/timer.hh"
#include "libxorp/timespent.hh"
#include "libcomm/comm_api.h"
#include "libproto/packet.hh"
#include "peer.hh"
#include "bgp.hh"
#include "profile_vars.hh"
#define DEBUG_BGPPeer
uint32_t BGPPeer::_unique_id_allocator = UNIQUE_ID_START;
#if 0
inline void trap_callback(const XrlError& error, const char *comment);
#endif
BGPPeer::BGPPeer(LocalData *ld, BGPPeerData *pd, SocketClient *sock,
BGPMain *m)
: _unique_id(_unique_id_allocator++),
_damping_peer_oscillations(true),
_damp_peer_oscillations(m->eventloop(),
10, /* restart threshold */
5 * 60, /* time period */
2 * 60 /* idle holdtime */)
{
debug_msg("BGPPeer constructor called (1)\n");
_localdata = ld;
_peerdata = pd;
_mainprocess = m;
_state = STATEIDLE;
_SocketClient = sock;
_output_queue_was_busy = false;
_handler = NULL;
_peername = c_format("Peer-%s", peerdata()->iptuple().str().c_str());
zero_stats();
_current_state = _next_state = _activated = false;
}
BGPPeer::~BGPPeer()
{
delete _SocketClient;
delete _peerdata;
list<AcceptSession *>::iterator i;
for (i = _accept_attempt.begin(); i != _accept_attempt.end(); i++)
delete (*i);
_accept_attempt.clear();
}
void
BGPPeer::zero_stats()
{
_in_updates = 0;
_out_updates = 0;
_in_total_messages = 0;
_out_total_messages = 0;
_last_error[0] = 0;
_last_error[1] = 0;
_established_transitions = 0;
_mainprocess->eventloop().current_time(_established_time);
_mainprocess->eventloop().current_time(_in_update_time);
}
void
BGPPeer::clear_last_error()
{
_last_error[0] = 0;
_last_error[1] = 0;
}
/*
* This call is dispatched by the reader after making sure that
* we have a packet with at least a BGP common header and whose
* length matches the one in that header.
* Our job now is to decode the message and dispatch it to the
* state machine.
*/
bool
BGPPeer::get_message(BGPPacket::Status status, const uint8_t *buf,
size_t length, SocketClient *socket_client)
{
XLOG_ASSERT(0 == socket_client || _SocketClient == socket_client);
PROFILE(if (main()->profile().enabled(profile_message_in))
main()->profile().log(profile_message_in,
c_format("message on %s len %u",
str().c_str(),
XORP_UINT_CAST(length))));
TIMESPENT();
switch (status) {
case BGPPacket::GOOD_MESSAGE:
break;
case BGPPacket::ILLEGAL_MESSAGE_LENGTH:
notify_peer_of_error(MSGHEADERERR, BADMESSLEN,
buf + BGPPacket::MARKER_SIZE, 2);
// event_tranfatal();
TIMESPENT_CHECK();
return false;
case BGPPacket::CONNECTION_CLOSED:
// _SocketClient->disconnect();
event_closed();
// XLOG_ASSERT(!is_connected());
TIMESPENT_CHECK();
return false;
}
#ifdef SAVE_PACKETS
// XXX
// This is a horrible hack to try and find a problem before the
// 1.1 Release.
// All packets will be stored in fname, in MRTD format, if they
// arrived on an IPv4 peering.
// The route_btoa program can be used to print the packets.
// TODO after the release:
// 1) Move the structures into a header file in libxorp.
// 2) Add an XRL to enable the saving of incoming packets on a
// per peer basis.
// 3) Handle IPv6 peerings.
// 4) Save all the values that are required by mrt_update.
// 5) Move all this code into a separate method.
// 6) Keep the file open.
// 7) Don't call gettimeofday directly, get the time from the eventloop.
string fname = "/tmp/bgpin.mrtd";
FILE *fp = fopen(fname.c_str(), "a");
if(0 == fp)
XLOG_FATAL("fopen of %s failed: %s", fname.c_str(),
strerror(errno));
struct mrt_header {
uint32_t time;
uint16_t type;
uint16_t subtype;
uint32_t length;
};
struct mrt_update {
uint16_t source_as;
uint16_t dest_as;
uint16_t ifindex;
uint16_t af;
uint32_t source_ip;
uint32_t dest_ip;
};
TimeVal tv;
TimerList::system_gettimeofday(&tv);
mrt_header mrt_header;
mrt_header.time = htonl(tv.sec());
mrt_header.type = htons(16);
mrt_header.subtype = htons(1);
mrt_header.length = htonl(length + sizeof(mrt_update));
if(fwrite(&mrt_header, sizeof(mrt_header), 1, fp) != 1)
XLOG_FATAL("fwrite of %s failed: %s", fname.c_str(), strerror(errno));
mrt_update update;
memset(&update, 0, sizeof(update));
update.af = htons(1); /* IPv4 */
string peer_addr = peerdata()->iptuple().get_peer_addr();
try {
update.source_as = htons(peerdata()->as().as());
update.source_ip = IPv4(peer_addr.c_str()).addr();
if(fwrite(&update, sizeof(update), 1, fp) != 1)
XLOG_FATAL("fwrite of %s failed: %s", fname.c_str(),
strerror(errno));
if(fwrite(buf, length, 1, fp) != 1)
XLOG_FATAL("fwrite of %s failed: %s", fname.c_str(),
strerror(errno));
} catch(InvalidFamily &e) {
XLOG_ERROR("%s might not be an IPv4 address %s", peer_addr.c_str(),
e.str().c_str());
}
fclose(fp);
#endif
_in_total_messages++;
/*
** We should only have a good packet at this point.
** The buffer pointer must be non 0.
*/
XLOG_ASSERT(0 != buf);
const uint8_t* marker = buf + BGPPacket::MARKER_OFFSET;
uint8_t type = extract_8(buf + BGPPacket::TYPE_OFFSET);
try {
/*
** Check the Marker, total waste of time as it never contains
** anything of interest.
*/
if (0 != memcmp(const_cast<uint8_t *>(&BGPPacket::Marker[0]),
marker, BGPPacket::MARKER_SIZE)) {
xorp_throw(CorruptMessage,"Bad Marker", MSGHEADERERR, CONNNOTSYNC);
}
switch (type) {
case MESSAGETYPEOPEN: {
debug_msg("OPEN Packet RECEIVED\n");
OpenPacket pac(buf, length);
PROFILE(XLOG_TRACE(main()->profile().enabled(trace_message_in),
"Peer %s: Receive: %s",
peerdata()->iptuple().str().c_str(),
cstring(pac)));
// All decode errors should throw a CorruptMessage.
debug_msg("%s", pac.str().c_str());
// want unified decode call. now need to get peerdata out.
_peerdata->dump_peer_data();
event_openmess(pac);
TIMESPENT_CHECK();
break;
}
case MESSAGETYPEKEEPALIVE: {
debug_msg("KEEPALIVE Packet RECEIVED %u\n",
XORP_UINT_CAST(length));
// Check that the length is correct or throw an exception
KeepAlivePacket pac(buf, length);
PROFILE(XLOG_TRACE(main()->profile().enabled(trace_message_in),
"Peer %s: Receive: %s",
peerdata()->iptuple().str().c_str(),
cstring(pac)));
// debug_msg("%s", pac.str().c_str());
event_keepmess();
TIMESPENT_CHECK();
break;
}
case MESSAGETYPEUPDATE: {
debug_msg("UPDATE Packet RECEIVED\n");
_in_updates++;
_mainprocess->eventloop().current_time(_in_update_time);
UpdatePacket pac(buf, length, _peerdata, _mainprocess, /*do checks*/true);
PROFILE(XLOG_TRACE(main()->profile().enabled(trace_message_in),
"Peer %s: Receive: %s",
peerdata()->iptuple().str().c_str(),
cstring(pac)));
// All decode errors should throw a CorruptMessage.
debug_msg("%s", pac.str().c_str());
event_recvupdate(pac);
TIMESPENT_CHECK();
if (TIMESPENT_OVERLIMIT()) {
XLOG_WARNING("Processing packet took longer than %u second %s",
XORP_UINT_CAST(TIMESPENT_LIMIT),
pac.str().c_str());
}
break;
}
case MESSAGETYPENOTIFICATION: {
debug_msg("NOTIFICATION Packet RECEIVED\n");
NotificationPacket pac(buf, length);
PROFILE(XLOG_TRACE(main()->profile().enabled(trace_message_in),
"Peer %s: Receive: %s",
peerdata()->iptuple().str().c_str(),
cstring(pac)));
// All decode errors should throw a CorruptMessage.
debug_msg("%s", pac.str().c_str());
event_recvnotify(pac);
TIMESPENT_CHECK();
break;
}
default:
/*
** Send a notification to the peer. This is a bad message type.
*/
XLOG_ERROR("%s Unknown packet type %d",
this->str().c_str(), type);
notify_peer_of_error(MSGHEADERERR, BADMESSTYPE,
buf + BGPPacket::TYPE_OFFSET, 1);
// event_tranfatal();
TIMESPENT_CHECK();
return false;
}
} catch(CorruptMessage& c) {
/*
** This peer has sent us a bad message. Send a notification
** and drop the the peering.
*/
XLOG_WARNING("%s %s %s", this->str().c_str(), c.where().c_str(),
c.why().c_str());
notify_peer_of_error(c.error(), c.subcode(), c.data(), c.len());
// event_tranfatal();
TIMESPENT_CHECK();
return false;
} catch (UnusableMessage& um) {
// the packet wasn't usable for some reason, but also
// wasn't so corrupt we need to send a notification -
// this is a "silent" error.
XLOG_WARNING("%s %s %s", this->str().c_str(), um.where().c_str(),
um.why().c_str());
}
TIMESPENT_CHECK();
/*
** If we are still connected and supposed to be reading.
*/
if (!is_connected() || !still_reading()) {
TIMESPENT_CHECK();
return false;
}
return true;
}
PeerOutputState
BGPPeer::send_message(const BGPPacket& p)
{
debug_msg("%s", p.str().c_str());
PROFILE(XLOG_TRACE(main()->profile().enabled(trace_message_out),
"Peer %s: Send: %s",
peerdata()->iptuple().str().c_str(),
cstring(p)));
uint8_t packet_type = p.type();
if ( packet_type != MESSAGETYPEOPEN &&
packet_type != MESSAGETYPEUPDATE &&
packet_type != MESSAGETYPENOTIFICATION &&
packet_type != MESSAGETYPEKEEPALIVE) {
xorp_throw(InvalidPacket,
c_format("Unknown packet type %d\n", packet_type));
}
_out_total_messages++;
if (packet_type == MESSAGETYPEUPDATE)
_out_updates++;
uint8_t *buf = new uint8_t[BGPPacket::MAXPACKETSIZE];
size_t ccnt = BGPPacket::MAXPACKETSIZE;
/*
** This buffer is dynamically allocated and should be freed.
*/
XLOG_ASSERT(p.encode(buf, ccnt, _peerdata));
debug_msg("Buffer for sent packet is %p\n", buf);
/*
** This write is async. So we can't free the data now,
** we will deal with it in the complete routine.
*/
bool ret = _SocketClient->send_message(buf, ccnt,
callback(this,&BGPPeer::send_message_complete));
if (ret == false)
delete[] buf;
if (ret) {
int size = _SocketClient->output_queue_size();
UNUSED(size);
debug_msg("Output queue size is %d\n", size);
if (_SocketClient->output_queue_busy()) {
_output_queue_was_busy = true;
return PEER_OUTPUT_BUSY;
} else
return PEER_OUTPUT_OK;
} else
return PEER_OUTPUT_FAIL;
}
void
BGPPeer::send_message_complete(SocketClient::Event ev, const uint8_t *buf)
{
TIMESPENT();
debug_msg("Packet sent, queue size now %d\n",
_SocketClient->output_queue_size());
switch (ev) {
case SocketClient::DATA:
debug_msg("event: data\n");
if (_output_queue_was_busy &&
(_SocketClient->output_queue_busy() == false)) {
debug_msg("Peer: output no longer busy\n");
_output_queue_was_busy = false;
if (_handler != NULL)
_handler->output_no_longer_busy();
}
TIMESPENT_CHECK();
/*drop through to next case*/
case SocketClient::FLUSHING:
debug_msg("event: flushing\n");
debug_msg("Freeing Buffer for sent packet: %p\n", buf);
delete[] buf;
TIMESPENT_CHECK();
break;
case SocketClient::ERROR:
// The most likely cause of an error is that the peer closed
// the connection.
debug_msg("event: error\n");
/* Don't free the message here we'll get it in the flush */
// XLOG_ERROR("Writing buffer failed: %s", strerror(errno));
// _SocketClient->disconnect();
event_closed();
// XLOG_ASSERT(!is_connected());
TIMESPENT_CHECK();
}
}
void
BGPPeer::send_notification(const NotificationPacket& p, bool restart,
bool automatic)
{
debug_msg("%s", p.str().c_str());
XLOG_INFO("Sending: %s", cstring(p));
PROFILE(XLOG_TRACE(main()->profile().enabled(trace_message_out),
"Peer %s: Send: %s",
peerdata()->iptuple().str().c_str(),
cstring(p)));
/*
** We need to deal with NOTIFICATION differently from other packets -
** NOTIFICATION is the last packet we send on a connection, and because
** we're using async sends, we need to chain the rest of the
** cleanup on the send complete callback.
*/
/*
* First we need to clear the transmit queue - we no longer care
* about anything that's in it, and we want to make sure that the
* transmit complete callbacks can no longer happen.
*/
flush_transmit_queue();
/*
** Don't read anything more on this connection.
*/
stop_reader();
/*
* This buffer is dynamically allocated and should be freed.
*/
size_t ccnt = BGPPacket::MAXPACKETSIZE;
uint8_t *buf = new uint8_t[BGPPacket::MAXPACKETSIZE];
XLOG_ASSERT(p.encode(buf, ccnt, _peerdata));
debug_msg("Buffer for sent packet is %p\n", buf);
/*
** This write is async. So we can't free the data now,
** we will deal with it in the complete routine. */
bool ret =_SocketClient->send_message(buf, ccnt,
callback(this, &BGPPeer::send_notification_complete,
restart, automatic));
if (!ret) {
delete[] buf;
return;
}
}
void
BGPPeer::send_notification_complete(SocketClient::Event ev,
const uint8_t* buf, bool restart,
bool automatic)
{
TIMESPENT();
switch (ev) {
case SocketClient::DATA:
debug_msg("Notification sent\n");
XLOG_ASSERT(STATESTOPPED == _state);
delete[] buf;
set_state(STATEIDLE, restart);
break;
case SocketClient::FLUSHING:
delete[] buf;
break;
case SocketClient::ERROR:
debug_msg("Notification not sent\n");
XLOG_ASSERT(STATESTOPPED == _state);
set_state(STATEIDLE, restart, automatic);
/* Don't free the message here we'll get it in the flush */
break;
}
/* there doesn't appear to be anything we can do at this point if
status indicates something went wrong */
}
/*
** Timer callback
*/
void
BGPPeer::hook_stopped()
{
XLOG_ASSERT(STATESTOPPED == _state);
XLOG_WARNING("%s Unable to send Notification so taking peer to idle",
this->str().c_str());
/*
** If the original notification was not an error such as sending a
** CEASE. If we arrived here due to a timeout, something has gone
** wrong so unconditionally set the restart to true.
*/
set_state(STATEIDLE);
}
/*
* Here begins the BGP state machine.
* It is split into multiple functions, each one dealing with a
* specific event.
*/
/*
* STATESTOPPED is the state we're in after we've queued a
* CEASE notification, but before it's actually been sent. We
* ignore packet events, but a further STOP event or error
* will cause is to stop waiting for the CEASE to be sent, and
* a START event will cause us to transition to idle, and then
* process the START event as normal
*/
/*
* start event. No data associated with the event.
*/
void
BGPPeer::event_start() // EVENTBGPSTART
{
TIMESPENT();
// Compute the type of this peering.
const_cast<BGPPeerData*>(peerdata())->compute_peer_type();
switch(_state) {
case STATESTOPPED:
flush_transmit_queue(); // ensure callback can't happen
set_state(STATEIDLE, false);// go through STATEIDLE to clear resources
// fallthrough now to process the start event
case STATEIDLE:
// Initalise resources
start_connect_retry_timer();
set_state(STATECONNECT);
connect_to_peer(callback(this, &BGPPeer::connect_to_peer_complete));
break;
// in all other cases, remain in the same state
case STATECONNECT:
case STATEACTIVE:
case STATEOPENSENT:
case STATEOPENCONFIRM:
case STATEESTABLISHED:
break;
}
}
/*
* stop event. No data associated with the event.
*/
void
BGPPeer::event_stop(bool restart, bool automatic) // EVENTBGPSTOP
{
TIMESPENT();
switch(_state) {
case STATEIDLE:
break;
case STATECONNECT:
_SocketClient->connect_break();
clear_connect_retry_timer();
/*FALLTHROUGH*/
case STATEACTIVE:
set_state(STATEIDLE, restart, automatic);
break;
case STATEOPENSENT:
case STATEOPENCONFIRM:
case STATEESTABLISHED: {
// Send Notification Message with error code of CEASE.
NotificationPacket np(CEASE);
send_notification(np, restart, automatic);
set_state(STATESTOPPED, restart, automatic);
break;
}
case STATESTOPPED:
// a second stop will cause us to give up on sending the CEASE
flush_transmit_queue(); // ensure callback can't happen
set_state(STATEIDLE, restart, automatic);
break;
}
}
/*
* EVENTBGPTRANOPEN event. No data associated with the event.
* We only call this when a connect() or an accept() succeeded,
* and we have a valid socket.
* The only states from which we enter here are STATECONNECT and STATEACTIVE
*/
void
BGPPeer::event_open() // EVENTBGPTRANOPEN
{
TIMESPENT();
switch(_state) {
case STATEOPENSENT:
case STATEOPENCONFIRM:
case STATEESTABLISHED:
case STATESTOPPED:
case STATEIDLE:
XLOG_FATAL("%s can't get EVENTBGPTRANOPEN in state %s",
this->str().c_str(),
pretty_print_state(_state));
break;
case STATECONNECT:
case STATEACTIVE: {
if (0 != _peerdata->get_delay_open_time()) {
start_delay_open_timer();
clear_connect_retry_timer();
return;
}
OpenPacket open_packet(_peerdata->my_AS_number(),
_localdata->get_id(),
_peerdata->get_configured_hold_time());
#if 0
ParameterList::const_iterator
pi = _peerdata->parameter_sent_list().begin();
while(pi != _peerdata->parameter_sent_list().end()) {
if ((*pi)->send())
open_packet.add_parameter(*pi);
pi++;
}
#endif
generate_open_message(open_packet);
send_message(open_packet);
clear_connect_retry_timer();
if ((_state == STATEACTIVE) || (_state == STATECONNECT)) {
// Start Holdtimer - four minutes recommended in spec.
_peerdata->set_hold_duration(4 * 60);
start_hold_timer();
}
// Change state to OpenSent
set_state(STATEOPENSENT);
break;
}
}
}
/*
* EVENTBGPTRANCLOSED event. No data associated with the event.
*/
void
BGPPeer::event_closed() // EVENTBGPTRANCLOSED
{
TIMESPENT();
switch(_state) {
case STATEIDLE:
break;
case STATECONNECT:
if (_SocketClient->is_connected())
_SocketClient->connect_break();
clear_connect_retry_timer();
set_state(STATEIDLE);
break;
case STATEACTIVE:
set_state(STATEIDLE);
break;
case STATEOPENSENT:
// Close Connection
_SocketClient->disconnect();
// Restart ConnectRetry Timer
restart_connect_retry_timer();
set_state(STATEACTIVE);
break;
case STATEOPENCONFIRM:
case STATEESTABLISHED:
set_state(STATEIDLE);
break;
case STATESTOPPED:
flush_transmit_queue(); // ensure callback can't happen
set_state(STATEIDLE);
break;
}
}
/*
* EVENTBGPCONNOPENFAIL event. No data associated with the event.
* We can only be in STATECONNECT
*/
void
BGPPeer::event_openfail() // EVENTBGPCONNOPENFAIL
{
TIMESPENT();
switch(_state) {
case STATEIDLE:
case STATEACTIVE:
case STATEOPENSENT:
case STATEOPENCONFIRM:
case STATEESTABLISHED:
case STATESTOPPED:
XLOG_FATAL("%s can't get EVENTBGPCONNOPENFAIL in state %s",
this->str().c_str(),
pretty_print_state(_state));
break;
case STATECONNECT:
if (_peerdata->get_delay_open_time() == 0) {
//
// If the DelayOpenTimer is not running, we have to go first
// through STATEIDLE.
// XXX: Instead of checking the configured DelayOpenTime value,
// we should check whether the corresponding timer is indeed
// not running. However, there is no dedicated DelayOpenTimer
// (the _idle_hold timer is reused instead), hence we check
// the corresponding DelayOpenTime configured value.
//
set_state(STATEIDLE, false);
}
restart_connect_retry_timer();
set_state(STATEACTIVE); // Continue to listen for a connection
break;
}
}
/*
* EVENTBGPTRANFATALERR event. No data associated with the event.
*/
void
BGPPeer::event_tranfatal() // EVENTBGPTRANFATALERR
{
TIMESPENT();
switch(_state) {
case STATEIDLE:
break;
case STATECONNECT:
case STATEACTIVE:
set_state(STATEIDLE);
break;
case STATEOPENSENT:
case STATEOPENCONFIRM:
case STATEESTABLISHED:
set_state(STATEIDLE);
break;
case STATESTOPPED:
// ensure callback can't happen
flush_transmit_queue();
set_state(STATEIDLE);
break;
}
}
/*
* EVENTCONNTIMEEXP event. No data associated with the event.
* This is a timer hook called from a connection_retry_timer expiring
* (it is a oneoff timer)
*/
void
BGPPeer::event_connexp() // EVENTCONNTIMEEXP
{
TIMESPENT();
switch(_state) {
case STATEIDLE:
case STATESTOPPED:
break;
case STATECONNECT:
restart_connect_retry_timer();
_SocketClient->connect_break();
connect_to_peer(callback(this, &BGPPeer::connect_to_peer_complete));
break;
case STATEACTIVE:
restart_connect_retry_timer();
set_state(STATECONNECT);
connect_to_peer(callback(this, &BGPPeer::connect_to_peer_complete));
break;
/*
* if these happen, we failed to properly cancel a timer (?)
*/
case STATEOPENSENT:
case STATEOPENCONFIRM:
case STATEESTABLISHED: {
// Send Notification Message with error code of FSM error.
// XXX this needs to be revised.
XLOG_WARNING("%s FSM received EVENTCONNTIMEEXP in state %s",
this->str().c_str(),
pretty_print_state(_state));
NotificationPacket np(FSMERROR);
send_notification(np);
set_state(STATESTOPPED, true);
break;
}
}
}
/*
* EVENTHOLDTIMEEXP event. No data associated with the event.
* Only called by a hold_timer (oneoff) expiring.
* If it goes off, the connection is over so we do not start the timer again.
*/
void
BGPPeer::event_holdexp() // EVENTHOLDTIMEEXP
{
TIMESPENT();
switch(_state) {
case STATEIDLE:
case STATESTOPPED:
break;
case STATECONNECT:
case STATEACTIVE:
set_state(STATEIDLE);
break;
case STATEOPENSENT:
case STATEOPENCONFIRM:
case STATEESTABLISHED: {
// Send Notification Message with error code of Hold Timer expired.
NotificationPacket np(HOLDTIMEEXP);
send_notification(np);
set_state(STATESTOPPED);
break;
}
}
}
/*
* EVENTKEEPALIVEEXP event.
* A keepalive timer expired.
*/
void
BGPPeer::event_keepexp() // EVENTKEEPALIVEEXP
{
TIMESPENT();
switch(_state) {
case STATEIDLE:
case STATESTOPPED:
case STATECONNECT:
case STATEACTIVE:
case STATEOPENSENT:
XLOG_FATAL("%s FSM received EVENTKEEPALIVEEXP in state %s",
this->str().c_str(),
pretty_print_state(_state));
break;
case STATEOPENCONFIRM:
case STATEESTABLISHED:
start_keepalive_timer();
KeepAlivePacket kp;
send_message(kp);
break;
}
}
void
BGPPeer::event_delay_open_exp()
{
TIMESPENT();
switch(_state) {
case STATEIDLE:
case STATESTOPPED:
case STATEOPENSENT:
case STATEESTABLISHED: {
XLOG_WARNING("%s FSM received EVENTRECOPENMESS in state %s",
this->str().c_str(),
pretty_print_state(_state));
NotificationPacket np(FSMERROR);
send_notification(np);
set_state(STATESTOPPED);
}
break;
case STATECONNECT:
case STATEACTIVE:
case STATEOPENCONFIRM: {
OpenPacket open_packet(_peerdata->my_AS_number(),
_localdata->get_id(),
_peerdata->get_configured_hold_time());
generate_open_message(open_packet);
send_message(open_packet);
if ((_state == STATEACTIVE) || (_state == STATECONNECT)) {
// Start Holdtimer - four minutes recommended in spec.
_peerdata->set_hold_duration(4 * 60);
start_hold_timer();
}
// Change state to OpenSent
set_state(STATEOPENSENT);
}
break;
}
}
void
BGPPeer::event_idle_hold_exp()
{
TIMESPENT();
XLOG_ASSERT(state() == STATEIDLE);
event_start();
}
/*
* EVENTRECOPENMESS event.
* We receive an open packet.
*/
void
BGPPeer::event_openmess(const OpenPacket& p) // EVENTRECOPENMESS
{
TIMESPENT();
switch(_state) {
case STATECONNECT:
case STATEACTIVE: {
// The only way to get here is due to a delayed open.
// Kill the delay open timer and send the open packet.
clear_delay_open_timer();
OpenPacket open_packet(_peerdata->my_AS_number(),
_localdata->get_id(),
_peerdata->get_configured_hold_time());
generate_open_message(open_packet);
send_message(open_packet);
}
/* FALLTHROUGH */
case STATEOPENSENT:
// Process OPEN MESSAGE
try {
check_open_packet(&p);
// We liked the open packet continue, trying to setup session.
KeepAlivePacket kp;
send_message(kp);
// start timers