-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession.c
2811 lines (2502 loc) · 67.7 KB
/
session.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
/* $OpenBSD$ */
/*
* Copyright (c) 2003, 2004, 2005 Henning Brauer <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "bgpd.h"
#include "mrt.h"
#include "session.h"
#define PFD_PIPE_MAIN 0
#define PFD_PIPE_ROUTE 1
#define PFD_PIPE_ROUTE_CTL 2
#define PFD_SOCK_CTL 3
#define PFD_SOCK_RCTL 4
#define PFD_LISTENERS_START 5
void session_sighdlr(int);
int setup_listeners(u_int *);
void init_conf(struct bgpd_config *);
void init_peer(struct peer *);
void start_timer_holdtime(struct peer *);
void start_timer_keepalive(struct peer *);
void session_close_connection(struct peer *);
void change_state(struct peer *, enum session_state, enum session_events);
int session_setup_socket(struct peer *);
void session_accept(int);
int session_connect(struct peer *);
void session_tcp_established(struct peer *);
void session_capa_ann_none(struct peer *);
int session_capa_add(struct buf *, u_int8_t, u_int8_t);
int session_capa_add_mp(struct buf *, u_int8_t);
struct bgp_msg *session_newmsg(enum msg_type, u_int16_t);
int session_sendmsg(struct bgp_msg *, struct peer *);
void session_open(struct peer *);
void session_keepalive(struct peer *);
void session_update(u_int32_t, void *, size_t);
void session_notification(struct peer *, u_int8_t, u_int8_t, void *,
ssize_t);
void session_rrefresh(struct peer *, u_int8_t);
int session_dispatch_msg(struct pollfd *, struct peer *);
int parse_header(struct peer *, u_char *, u_int16_t *, u_int8_t *);
int parse_open(struct peer *);
int parse_update(struct peer *);
int parse_refresh(struct peer *);
int parse_notification(struct peer *);
int parse_capabilities(struct peer *, u_char *, u_int16_t, u_int32_t *);
int capa_neg_calc(struct peer *);
void session_dispatch_imsg(struct imsgbuf *, int, u_int *);
void session_up(struct peer *);
void session_down(struct peer *);
void session_demote(struct peer *, int);
int la_cmp(struct listen_addr *, struct listen_addr *);
struct peer *getpeerbyip(struct sockaddr *);
int session_match_mask(struct peer *, struct bgpd_addr *);
struct peer *getpeerbyid(u_int32_t);
struct bgpd_config *conf, *nconf;
struct bgpd_sysdep sysdep;
struct peer *peers, *npeers;
volatile sig_atomic_t session_quit;
int pending_reconf;
int csock = -1, rcsock = -1;
u_int peer_cnt;
struct imsgbuf *ibuf_rde;
struct imsgbuf *ibuf_rde_ctl;
struct imsgbuf *ibuf_main;
struct mrt_head mrthead;
void
session_sighdlr(int sig)
{
switch (sig) {
case SIGINT:
case SIGTERM:
session_quit = 1;
break;
}
}
int
setup_listeners(u_int *la_cnt)
{
int ttl = 255;
int opt;
struct listen_addr *la;
u_int cnt = 0;
TAILQ_FOREACH(la, conf->listen_addrs, entry) {
la->reconf = RECONF_NONE;
cnt++;
if (la->flags & LISTENER_LISTENING)
continue;
if (la->fd == -1) {
log_warn("cannot establish listener on %s: invalid fd",
log_sockaddr((struct sockaddr *)&la->sa));
continue;
}
opt = 1;
if (setsockopt(la->fd, IPPROTO_TCP, TCP_MD5SIG,
&opt, sizeof(opt)) == -1) {
if (errno == ENOPROTOOPT) { /* system w/o md5sig */
log_warnx("md5sig not available, disabling");
sysdep.no_md5sig = 1;
} else
fatal("setsockopt TCP_MD5SIG");
}
/* set ttl to 255 so that ttl-security works */
if (la->sa.ss_family == AF_INET && setsockopt(la->fd,
IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) == -1) {
log_warn("setup_listeners setsockopt TTL");
continue;
}
session_socket_blockmode(la->fd, BM_NONBLOCK);
if (listen(la->fd, MAX_BACKLOG)) {
close(la->fd);
fatal("listen");
}
la->flags |= LISTENER_LISTENING;
log_info("listening on %s",
log_sockaddr((struct sockaddr *)&la->sa));
}
*la_cnt = cnt;
return (0);
}
pid_t
session_main(int pipe_m2s[2], int pipe_s2r[2], int pipe_m2r[2],
int pipe_s2rctl[2], char *cname, char *rcname)
{
int nfds, timeout;
unsigned int i, j, idx_peers, idx_listeners, idx_mrts;
pid_t pid;
u_int pfd_elms = 0, peer_l_elms = 0, mrt_l_elms = 0;
u_int listener_cnt, ctl_cnt, mrt_cnt;
u_int new_cnt;
u_int32_t ctl_queued;
struct passwd *pw;
struct peer *p, **peer_l = NULL, *last, *next;
struct mrt *m, *xm, **mrt_l = NULL;
struct pollfd *pfd = NULL;
struct ctl_conn *ctl_conn;
struct listen_addr *la;
void *newp;
short events;
switch (pid = fork()) {
case -1:
fatal("cannot fork");
case 0:
break;
default:
return (pid);
}
/* control socket is outside chroot */
if ((csock = control_init(0, cname)) == -1)
fatalx("control socket setup failed");
if (rcname != NULL && (rcsock = control_init(1, rcname)) == -1)
fatalx("control socket setup failed");
if ((pw = getpwnam(BGPD_USER)) == NULL)
fatal(NULL);
if (chroot(pw->pw_dir) == -1)
fatal("chroot");
if (chdir("/") == -1)
fatal("chdir(\"/\")");
setproctitle("session engine");
bgpd_process = PROC_SE;
if (pfkey_init(&sysdep) == -1)
fatalx("pfkey setup failed");
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("can't drop privileges");
signal(SIGTERM, session_sighdlr);
signal(SIGINT, session_sighdlr);
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGALRM, SIG_IGN);
signal(SIGUSR1, SIG_IGN);
close(pipe_m2s[0]);
close(pipe_s2r[1]);
close(pipe_s2rctl[1]);
close(pipe_m2r[0]);
close(pipe_m2r[1]);
if ((ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL ||
(ibuf_rde_ctl = malloc(sizeof(struct imsgbuf))) == NULL ||
(ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
imsg_init(ibuf_rde, pipe_s2r[0]);
imsg_init(ibuf_rde_ctl, pipe_s2rctl[0]);
imsg_init(ibuf_main, pipe_m2s[1]);
TAILQ_INIT(&ctl_conns);
control_listen(csock);
control_listen(rcsock);
LIST_INIT(&mrthead);
listener_cnt = 0;
peer_cnt = 0;
ctl_cnt = 0;
if ((conf = malloc(sizeof(struct bgpd_config))) == NULL)
fatal(NULL);
if ((conf->listen_addrs = calloc(1, sizeof(struct listen_addrs))) ==
NULL)
fatal(NULL);
TAILQ_INIT(conf->listen_addrs);
log_info("session engine ready");
while (session_quit == 0) {
/* check for peers to be initialized or deleted */
last = NULL;
for (p = peers; p != NULL; p = next) {
next = p->next;
if (!pending_reconf) {
/* cloned peer that idled out? */
if (p->state == STATE_IDLE && p->conf.cloned &&
time(NULL) - p->stats.last_updown >=
INTERVAL_HOLD_CLONED)
p->conf.reconf_action = RECONF_DELETE;
/* new peer that needs init? */
if (p->state == STATE_NONE)
init_peer(p);
/* reinit due? */
if (p->conf.reconf_action == RECONF_REINIT) {
session_stop(p, ERR_CEASE_ADMIN_RESET);
if (!p->conf.down)
timer_set(p, Timer_IdleHold, 0);
}
/* deletion due? */
if (p->conf.reconf_action == RECONF_DELETE) {
if (p->demoted)
session_demote(p, -1);
p->conf.demote_group[0] = 0;
session_stop(p, ERR_CEASE_PEER_UNCONF);
log_peer_warnx(&p->conf, "removed");
if (last != NULL)
last->next = next;
else
peers = next;
timer_remove_all(p);
free(p);
peer_cnt--;
continue;
}
p->conf.reconf_action = RECONF_NONE;
}
last = p;
}
if (peer_cnt > peer_l_elms) {
if ((newp = realloc(peer_l, sizeof(struct peer *) *
peer_cnt)) == NULL) {
/* panic for now */
log_warn("could not resize peer_l from %u -> %u"
" entries", peer_l_elms, peer_cnt);
fatalx("exiting");
}
peer_l = newp;
peer_l_elms = peer_cnt;
}
mrt_cnt = 0;
for (m = LIST_FIRST(&mrthead); m != NULL; m = xm) {
xm = LIST_NEXT(m, entry);
if (m->state == MRT_STATE_REMOVE) {
mrt_clean(m);
LIST_REMOVE(m, entry);
free(m);
continue;
}
if (m->wbuf.queued)
mrt_cnt++;
}
if (mrt_cnt > mrt_l_elms) {
if ((newp = realloc(mrt_l, sizeof(struct mrt *) *
mrt_cnt)) == NULL) {
/* panic for now */
log_warn("could not resize mrt_l from %u -> %u"
" entries", mrt_l_elms, mrt_cnt);
fatalx("exiting");
}
mrt_l = newp;
mrt_l_elms = mrt_cnt;
}
new_cnt = PFD_LISTENERS_START + listener_cnt + peer_cnt +
ctl_cnt + mrt_cnt;
if (new_cnt > pfd_elms) {
if ((newp = realloc(pfd, sizeof(struct pollfd) *
new_cnt)) == NULL) {
/* panic for now */
log_warn("could not resize pfd from %u -> %u"
" entries", pfd_elms, new_cnt);
fatalx("exiting");
}
pfd = newp;
pfd_elms = new_cnt;
}
bzero(pfd, sizeof(struct pollfd) * pfd_elms);
pfd[PFD_PIPE_MAIN].fd = ibuf_main->fd;
pfd[PFD_PIPE_MAIN].events = POLLIN;
if (ibuf_main->w.queued > 0)
pfd[PFD_PIPE_MAIN].events |= POLLOUT;
pfd[PFD_PIPE_ROUTE].fd = ibuf_rde->fd;
pfd[PFD_PIPE_ROUTE].events = POLLIN;
if (ibuf_rde->w.queued > 0)
pfd[PFD_PIPE_ROUTE].events |= POLLOUT;
ctl_queued = 0;
TAILQ_FOREACH(ctl_conn, &ctl_conns, entry)
ctl_queued += ctl_conn->ibuf.w.queued;
pfd[PFD_PIPE_ROUTE_CTL].fd = ibuf_rde_ctl->fd;
if (ctl_queued < SESSION_CTL_QUEUE_MAX)
/*
* Do not act as unlimited buffer. Don't read in more
* messages if the ctl sockets are getting full.
*/
pfd[PFD_PIPE_ROUTE_CTL].events = POLLIN;
pfd[PFD_SOCK_CTL].fd = csock;
pfd[PFD_SOCK_CTL].events = POLLIN;
pfd[PFD_SOCK_RCTL].fd = rcsock;
pfd[PFD_SOCK_RCTL].events = POLLIN;
i = PFD_LISTENERS_START;
TAILQ_FOREACH(la, conf->listen_addrs, entry) {
pfd[i].fd = la->fd;
pfd[i].events = POLLIN;
i++;
}
idx_listeners = i;
timeout = 240; /* loop every 240s at least */
for (p = peers; p != NULL; p = p->next) {
time_t nextaction;
struct peer_timer *pt;
/* check timers */
if ((pt = timer_nextisdue(p)) != NULL) {
switch (pt->type) {
case Timer_Hold:
bgp_fsm(p, EVNT_TIMER_HOLDTIME);
break;
case Timer_ConnectRetry:
bgp_fsm(p, EVNT_TIMER_CONNRETRY);
break;
case Timer_Keepalive:
bgp_fsm(p, EVNT_TIMER_KEEPALIVE);
break;
case Timer_IdleHold:
bgp_fsm(p, EVNT_START);
break;
case Timer_IdleHoldReset:
p->IdleHoldTime /= 2;
if (p->IdleHoldTime <=
INTERVAL_IDLE_HOLD_INITIAL) {
p->IdleHoldTime =
INTERVAL_IDLE_HOLD_INITIAL;
timer_stop(p,
Timer_IdleHoldReset);
p->errcnt = 0;
} else
timer_set(p,
Timer_IdleHoldReset,
p->IdleHoldTime);
break;
case Timer_CarpUndemote:
timer_stop(p, Timer_CarpUndemote);
if (p->demoted &&
p->state == STATE_ESTABLISHED)
session_demote(p, -1);
break;
default:
fatalx("King Bula lost in time");
}
}
if ((nextaction = timer_nextduein(p)) != -1 &&
nextaction < timeout)
timeout = nextaction;
/* are we waiting for a write? */
events = POLLIN;
if (p->wbuf.queued > 0 || p->state == STATE_CONNECT)
events |= POLLOUT;
/* poll events */
if (p->fd != -1 && events != 0) {
pfd[i].fd = p->fd;
pfd[i].events = events;
peer_l[i - idx_listeners] = p;
i++;
}
}
idx_peers = i;
LIST_FOREACH(m, &mrthead, entry)
if (m->wbuf.queued) {
pfd[i].fd = m->wbuf.fd;
pfd[i].events = POLLOUT;
mrt_l[i - idx_peers] = m;
i++;
}
idx_mrts = i;
TAILQ_FOREACH(ctl_conn, &ctl_conns, entry) {
pfd[i].fd = ctl_conn->ibuf.fd;
pfd[i].events = POLLIN;
if (ctl_conn->ibuf.w.queued > 0)
pfd[i].events |= POLLOUT;
i++;
}
if (timeout < 0)
timeout = 0;
if ((nfds = poll(pfd, i, timeout * 1000)) == -1)
if (errno != EINTR)
fatal("poll error");
if (nfds > 0 && pfd[PFD_PIPE_MAIN].revents & POLLOUT)
if (msgbuf_write(&ibuf_main->w) < 0)
fatal("pipe write error");
if (nfds > 0 && pfd[PFD_PIPE_MAIN].revents & POLLIN) {
nfds--;
session_dispatch_imsg(ibuf_main, PFD_PIPE_MAIN,
&listener_cnt);
}
if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLOUT)
if (msgbuf_write(&ibuf_rde->w) < 0)
fatal("pipe write error");
if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) {
nfds--;
session_dispatch_imsg(ibuf_rde, PFD_PIPE_ROUTE,
&listener_cnt);
}
if (nfds > 0 && pfd[PFD_PIPE_ROUTE_CTL].revents & POLLIN) {
nfds--;
session_dispatch_imsg(ibuf_rde_ctl, PFD_PIPE_ROUTE_CTL,
&listener_cnt);
}
if (nfds > 0 && pfd[PFD_SOCK_CTL].revents & POLLIN) {
nfds--;
ctl_cnt += control_accept(csock, 0);
}
if (nfds > 0 && pfd[PFD_SOCK_RCTL].revents & POLLIN) {
nfds--;
ctl_cnt += control_accept(rcsock, 1);
}
for (j = PFD_LISTENERS_START; nfds > 0 && j < idx_listeners;
j++)
if (pfd[j].revents & POLLIN) {
nfds--;
session_accept(pfd[j].fd);
}
for (; nfds > 0 && j < idx_peers; j++)
nfds -= session_dispatch_msg(&pfd[j],
peer_l[j - idx_listeners]);
for (; nfds > 0 && j < idx_mrts; j++)
if (pfd[j].revents & POLLOUT) {
nfds--;
mrt_write(mrt_l[j - idx_peers]);
}
for (; nfds > 0 && j < i; j++)
nfds -= control_dispatch_msg(&pfd[j], &ctl_cnt);
}
while ((p = peers) != NULL) {
peers = p->next;
session_stop(p, ERR_CEASE_ADMIN_DOWN);
pfkey_remove(p);
free(p);
}
while ((m = LIST_FIRST(&mrthead)) != NULL) {
mrt_clean(m);
LIST_REMOVE(m, entry);
free(m);
}
while ((la = TAILQ_FIRST(conf->listen_addrs)) != NULL) {
TAILQ_REMOVE(conf->listen_addrs, la, entry);
free(la);
}
free(conf->listen_addrs);
free(peer_l);
free(mrt_l);
free(pfd);
msgbuf_write(&ibuf_rde->w);
msgbuf_clear(&ibuf_rde->w);
free(ibuf_rde);
msgbuf_write(&ibuf_main->w);
msgbuf_clear(&ibuf_main->w);
free(ibuf_main);
control_shutdown(csock);
control_shutdown(rcsock);
log_info("session engine exiting");
_exit(0);
}
void
init_conf(struct bgpd_config *c)
{
if (!c->holdtime)
c->holdtime = INTERVAL_HOLD;
if (!c->connectretry)
c->connectretry = INTERVAL_CONNECTRETRY;
}
void
init_peer(struct peer *p)
{
TAILQ_INIT(&p->timers);
p->fd = p->wbuf.fd = -1;
if (p->conf.if_depend[0])
imsg_compose(ibuf_main, IMSG_IFINFO, 0, 0, -1,
p->conf.if_depend, sizeof(p->conf.if_depend));
else
p->depend_ok = 1;
peer_cnt++;
change_state(p, STATE_IDLE, EVNT_NONE);
if (p->conf.down)
timer_stop(p, Timer_IdleHold); /* no autostart */
else
timer_set(p, Timer_IdleHold, 0); /* start ASAP */
/*
* on startup, demote if requested.
* do not handle new peers. they must reach ESTABLISHED beforehands.
* peers added at runtime have reconf_action set to RECONF_REINIT.
*/
if (p->conf.reconf_action != RECONF_REINIT && p->conf.demote_group[0])
session_demote(p, +1);
}
void
bgp_fsm(struct peer *peer, enum session_events event)
{
switch (peer->state) {
case STATE_NONE:
/* nothing */
break;
case STATE_IDLE:
switch (event) {
case EVNT_START:
timer_stop(peer, Timer_Hold);
timer_stop(peer, Timer_Keepalive);
timer_stop(peer, Timer_IdleHold);
/* allocate read buffer */
peer->rbuf = calloc(1, sizeof(struct buf_read));
if (peer->rbuf == NULL)
fatal(NULL);
peer->rbuf->wpos = 0;
/* init write buffer */
msgbuf_init(&peer->wbuf);
/* init pfkey - remove old if any, load new ones */
pfkey_remove(peer);
if (pfkey_establish(peer) == -1) {
log_peer_warnx(&peer->conf,
"pfkey setup failed");
return;
}
peer->stats.last_sent_errcode = 0;
peer->stats.last_sent_suberr = 0;
if (!peer->depend_ok)
timer_stop(peer, Timer_ConnectRetry);
else if (peer->passive || peer->conf.passive ||
peer->conf.template) {
change_state(peer, STATE_ACTIVE, event);
timer_stop(peer, Timer_ConnectRetry);
} else {
change_state(peer, STATE_CONNECT, event);
timer_set(peer, Timer_ConnectRetry,
conf->connectretry);
session_connect(peer);
}
peer->passive = 0;
break;
default:
/* ignore */
break;
}
break;
case STATE_CONNECT:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_CON_OPEN:
session_tcp_established(peer);
session_open(peer);
timer_stop(peer, Timer_ConnectRetry);
peer->holdtime = INTERVAL_HOLD_INITIAL;
start_timer_holdtime(peer);
change_state(peer, STATE_OPENSENT, event);
break;
case EVNT_CON_OPENFAIL:
timer_set(peer, Timer_ConnectRetry,
conf->connectretry);
session_close_connection(peer);
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_TIMER_CONNRETRY:
timer_set(peer, Timer_ConnectRetry,
conf->connectretry);
session_connect(peer);
break;
default:
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_ACTIVE:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_CON_OPEN:
session_tcp_established(peer);
session_open(peer);
timer_stop(peer, Timer_ConnectRetry);
peer->holdtime = INTERVAL_HOLD_INITIAL;
start_timer_holdtime(peer);
change_state(peer, STATE_OPENSENT, event);
break;
case EVNT_CON_OPENFAIL:
timer_set(peer, Timer_ConnectRetry,
conf->connectretry);
session_close_connection(peer);
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_TIMER_CONNRETRY:
timer_set(peer, Timer_ConnectRetry,
peer->holdtime);
change_state(peer, STATE_CONNECT, event);
session_connect(peer);
break;
default:
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_OPENSENT:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
session_close_connection(peer);
timer_set(peer, Timer_ConnectRetry,
conf->connectretry);
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_RCVD_OPEN:
/* parse_open calls change_state itself on failure */
if (parse_open(peer))
break;
session_keepalive(peer);
change_state(peer, STATE_OPENCONFIRM, event);
break;
case EVNT_RCVD_NOTIFICATION:
if (parse_notification(peer)) {
change_state(peer, STATE_IDLE, event);
/* don't punish, capa negotiation */
timer_set(peer, Timer_IdleHold, 0);
peer->IdleHoldTime /= 2;
} else
change_state(peer, STATE_IDLE, event);
break;
default:
session_notification(peer, ERR_FSM, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_OPENCONFIRM:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_KEEPALIVE:
session_keepalive(peer);
break;
case EVNT_RCVD_KEEPALIVE:
start_timer_holdtime(peer);
change_state(peer, STATE_ESTABLISHED, event);
break;
case EVNT_RCVD_NOTIFICATION:
parse_notification(peer);
change_state(peer, STATE_IDLE, event);
break;
default:
session_notification(peer, ERR_FSM, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_ESTABLISHED:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_KEEPALIVE:
session_keepalive(peer);
break;
case EVNT_RCVD_KEEPALIVE:
start_timer_holdtime(peer);
break;
case EVNT_RCVD_UPDATE:
start_timer_holdtime(peer);
if (parse_update(peer))
change_state(peer, STATE_IDLE, event);
else
start_timer_holdtime(peer);
break;
case EVNT_RCVD_NOTIFICATION:
parse_notification(peer);
change_state(peer, STATE_IDLE, event);
break;
default:
session_notification(peer, ERR_FSM, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
}
break;
}
}
void
start_timer_holdtime(struct peer *peer)
{
if (peer->holdtime > 0)
timer_set(peer, Timer_Hold, peer->holdtime);
else
timer_stop(peer, Timer_Hold);
}
void
start_timer_keepalive(struct peer *peer)
{
if (peer->holdtime > 0)
timer_set(peer, Timer_Keepalive, peer->holdtime / 3);
else
timer_stop(peer, Timer_Keepalive);
}
void
session_close_connection(struct peer *peer)
{
if (peer->fd != -1)
close(peer->fd);
peer->fd = peer->wbuf.fd = -1;
}
void
change_state(struct peer *peer, enum session_state state,
enum session_events event)
{
struct mrt *mrt;
switch (state) {
case STATE_IDLE:
/* carp demotion first. new peers handled in init_peer */
if (peer->state == STATE_ESTABLISHED &&
peer->conf.demote_group[0] && !peer->demoted)
session_demote(peer, +1);
/*
* try to write out what's buffered (maybe a notification),
* don't bother if it fails
*/
if (peer->state >= STATE_OPENSENT && peer->wbuf.queued)
msgbuf_write(&peer->wbuf);
/*
* we must start the timer for the next EVNT_START
* if we are coming here due to an error and the
* session was not established successfully before, the
* starttimerinterval needs to be exponentially increased
*/
if (peer->IdleHoldTime == 0)
peer->IdleHoldTime = INTERVAL_IDLE_HOLD_INITIAL;
peer->holdtime = INTERVAL_HOLD_INITIAL;
timer_stop(peer, Timer_ConnectRetry);
timer_stop(peer, Timer_Keepalive);
timer_stop(peer, Timer_Hold);
timer_stop(peer, Timer_IdleHold);
timer_stop(peer, Timer_IdleHoldReset);
session_close_connection(peer);
msgbuf_clear(&peer->wbuf);
free(peer->rbuf);
peer->rbuf = NULL;
bzero(&peer->capa.peer, sizeof(peer->capa.peer));
if (peer->state == STATE_ESTABLISHED)
session_down(peer);
if (event != EVNT_STOP) {
timer_set(peer, Timer_IdleHold, peer->IdleHoldTime);
if (event != EVNT_NONE &&
peer->IdleHoldTime < MAX_IDLE_HOLD/2)
peer->IdleHoldTime *= 2;
}
if (peer->state == STATE_NONE ||
peer->state == STATE_ESTABLISHED) {
/* initialize capability negotiation structures */
memcpy(&peer->capa.ann, &peer->conf.capabilities,
sizeof(peer->capa.ann));
if (!peer->conf.announce_capa)
session_capa_ann_none(peer);
}
break;
case STATE_CONNECT:
break;
case STATE_ACTIVE:
break;
case STATE_OPENSENT:
break;
case STATE_OPENCONFIRM:
break;
case STATE_ESTABLISHED:
timer_set(peer, Timer_IdleHoldReset, peer->IdleHoldTime);
if (peer->demoted)
timer_set(peer, Timer_CarpUndemote,
INTERVAL_HOLD_DEMOTED);
session_up(peer);
break;
default: /* something seriously fucked */
break;
}
log_statechange(peer, state, event);
LIST_FOREACH(mrt, &mrthead, entry) {
if (!(mrt->type == MRT_ALL_IN || mrt->type == MRT_ALL_OUT))
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == peer->conf.id || (mrt->group_id != 0 &&
mrt->group_id == peer->conf.groupid))
mrt_dump_state(mrt, peer->state, state, peer);
}
peer->prev_state = peer->state;
peer->state = state;
}
void
session_accept(int listenfd)
{
int connfd;
int opt;
socklen_t len;
struct sockaddr_storage cliaddr;
struct peer *p = NULL;
len = sizeof(cliaddr);
if ((connfd = accept(listenfd,
(struct sockaddr *)&cliaddr, &len)) == -1) {
if (errno == EWOULDBLOCK || errno == EINTR)
return;
else
log_warn("accept");
}
p = getpeerbyip((struct sockaddr *)&cliaddr);
if (p != NULL && p->state == STATE_IDLE && p->errcnt < 2) {
if (timer_running(p, Timer_IdleHold, NULL)) {
/* fast reconnect after clear */
p->passive = 1;
bgp_fsm(p, EVNT_START);
}
}
if (p != NULL &&
(p->state == STATE_CONNECT || p->state == STATE_ACTIVE)) {
if (p->fd != -1) {
if (p->state == STATE_CONNECT)
session_close_connection(p);
else {
close(connfd);
return;
}
}