-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
proto_bfd.c
1860 lines (1476 loc) · 42.4 KB
/
proto_bfd.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
/*
* proto_bfd.c BFD processing.
*
* Version: $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright (C) 2012 Network RADIUS SARL <info@networkradius.com>
*/
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/protocol.h>
#include <freeradius-devel/modules.h>
#include <freeradius-devel/rad_assert.h>
#include <freeradius-devel/event.h>
#include <freeradius-devel/md5.h>
#include <freeradius-devel/sha1.h>
#define USEC (1000000)
#define BFD_MAX_SECRET_LENGTH 20
typedef enum bfd_session_state_t {
BFD_STATE_ADMIN_DOWN = 0,
BFD_STATE_DOWN,
BFD_STATE_INIT,
BFD_STATE_UP
} bfd_session_state_t;
typedef enum bfd_diag_t {
BFD_DIAG_NONE = 0,
BFD_CTRL_EXPIRED,
BFD_ECHO_FAILED,
BFD_NEIGHBOR_DOWN,
BFD_FORWARD_PLANE_RESET,
BFD_PATH_DOWN,
BFD_CONCATENATED_PATH_DOWN,
BFD_ADMIN_DOWN,
BFD_REVERSE_CONCAT_PATH_DOWN
} bfd_diag_t;
typedef enum bfd_auth_type_t {
BFD_AUTH_RESERVED = 0,
BFD_AUTH_SIMPLE,
BFD_AUTH_KEYED_MD5,
BFD_AUTH_MET_KEYED_MD5,
BFD_AUTH_KEYED_SHA1,
BFD_AUTH_MET_KEYED_SHA1,
} bfd_auth_type_t;
#define BFD_AUTH_INVALID (BFD_AUTH_MET_KEYED_SHA1 + 1)
typedef struct bfd_state_t {
int number;
int sockfd;
fr_event_list_t *el;
const char *server;
CONF_SECTION *unlang;
bool blocked;
int pipefd[2];
pthread_t pthread_id;
bfd_auth_type_t auth_type;
uint8_t secret[BFD_MAX_SECRET_LENGTH];
size_t secret_len;
fr_ipaddr_t local_ipaddr;
fr_ipaddr_t remote_ipaddr;
uint16_t local_port;
uint16_t remote_port;
/*
* To simplify sending the packets.
*/
struct sockaddr_storage remote_sockaddr;
socklen_t salen;
fr_event_timer_t *ev_timeout;
fr_event_timer_t *ev_packet;
struct timeval last_recv;
struct timeval next_recv;
struct timeval last_sent;
bfd_session_state_t session_state;
bfd_session_state_t remote_session_state;
uint32_t local_disc;
uint32_t remote_disc;
bfd_diag_t local_diag;
uint32_t desired_min_tx_interval; /* in usec */
uint32_t required_min_rx_interval;
uint32_t remote_min_rx_interval;
uint32_t remote_min_echo_rx_interval;
uint32_t next_min_tx_interval;
bool demand_mode;
bool remote_demand_mode;
int detect_multi;
uint32_t recv_auth_seq;
uint32_t xmit_auth_seq;
int auth_seq_known;
int doing_poll;
uint32_t my_min_echo_rx_interval;
uint32_t detection_time;
int detection_timeouts;
int passive;
} bfd_state_t;
typedef struct bfd_auth_basic_t {
uint8_t auth_type;
uint8_t auth_len;
uint8_t key_id;
} __attribute__ ((packed)) bfd_auth_basic_t;
typedef struct bfd_auth_simple_t {
uint8_t auth_type;
uint8_t auth_len;
uint8_t key_id;
uint8_t password[16];
} __attribute__ ((packed)) bfd_auth_simple_t;
typedef struct bfd_auth_md5_t {
uint8_t auth_type;
uint8_t auth_len;
uint8_t key_id;
uint8_t reserved;
uint32_t sequence_no;
uint8_t digest[MD5_DIGEST_LENGTH];
} __attribute__ ((packed)) bfd_auth_md5_t;
typedef struct bfd_auth_sha1_t {
uint8_t auth_type;
uint8_t auth_len;
uint8_t key_id;
uint8_t reserved;
uint32_t sequence_no;
uint8_t digest[SHA1_DIGEST_LENGTH];
} __attribute__ ((packed)) bfd_auth_sha1_t;
typedef union bfd_auth_t {
bfd_auth_basic_t basic;
bfd_auth_simple_t password;
bfd_auth_md5_t md5;
bfd_auth_sha1_t sha1;
} __attribute__ ((packed)) bfd_auth_t;
/*
* A packet
*/
typedef struct bfd_packet_t {
#ifdef WORDS_BIGENDIAN
unsigned int version : 3;
unsigned int diag : 5;
unsigned int state : 2;
unsigned int poll : 1;
unsigned int final : 1;
unsigned int control_plane_independent : 1;
unsigned int auth_present : 1;
unsigned int demand : 1;
unsigned int multipoint : 1;
#else
unsigned int diag : 5;
unsigned int version : 3;
unsigned int multipoint : 1;
unsigned int demand : 1;
unsigned int auth_present : 1;
unsigned int control_plane_independent : 1;
unsigned int final : 1;
unsigned int poll : 1;
unsigned int state : 2;
#endif
uint8_t detect_multi;
uint8_t length;
uint32_t my_disc;
uint32_t your_disc;
uint32_t desired_min_tx_interval;
uint32_t required_min_rx_interval;
uint32_t min_echo_rx_interval;
bfd_auth_t auth;
} __attribute__ ((packed)) bfd_packet_t;
typedef struct bfd_socket_t {
fr_ipaddr_t my_ipaddr;
uint16_t my_port;
char const *interface;
int number;
const char *server;
CONF_SECTION *unlang;
uint32_t min_tx_interval;
uint32_t min_rx_interval;
uint32_t max_timeouts;
bool demand;
bfd_auth_type_t auth_type;
uint8_t secret[BFD_MAX_SECRET_LENGTH];
size_t secret_len;
rbtree_t *session_tree;
} bfd_socket_t;
static int bfd_start_packets(bfd_state_t *session);
static int bfd_start_control(bfd_state_t *session);
static int bfd_stop_control(bfd_state_t *session);
static void bfd_detection_timeout(UNUSED fr_event_list_t *eel, struct timeval *now, void *ctx);
static int bfd_process(bfd_state_t *session, bfd_packet_t *bfd);
static fr_event_list_t *el = NULL; /* don't ask */
void bfd_init(fr_event_list_t *xel);
void bfd_init(fr_event_list_t *xel)
{
el = xel;
}
static void bfd_pthread_free(bfd_state_t *session)
{
session->blocked = true;
close(session->pipefd[0]);
close(session->pipefd[1]);
talloc_free(session->el);
session->el = NULL;
session->pipefd[0] = session->pipefd[1] = -1;
session->blocked = false;
}
/*
* A child thread reads the packet from a pipe, and processes it.
*/
static void bfd_pipe_recv(UNUSED fr_event_list_t *xel, int fd, void *ctx)
{
ssize_t num;
bfd_state_t *session = ctx;
bfd_packet_t bfd;
if (session->blocked) return;
/*
* Read the header
*/
num = read(fd, &bfd, 4);
if ((num < 4) || (bfd.length < 4)) {
fail:
ERROR("BFD Failed reading from pipe!");
session->blocked = true;
return;
}
/*
* This is already checked in the caller, but what the heck...
*/
if (bfd.length > sizeof(bfd)) goto fail;
/*
* Read the rest of the packet.
*/
num = read(fd, ((uint8_t *) &bfd) + 4, bfd.length - 4);
if ((num < 0) || ((num + 4) != bfd.length)) goto fail;
bfd_process(session, &bfd);
}
/*
* Do nothing more than read from the sockets and process the
* timers.
*/
static void *bfd_child_thread(void *ctx)
{
bfd_state_t *session = ctx;
DEBUG("BFD %d starting child thread", session->number);
bfd_start_control(session);
fr_event_loop(session->el);
bfd_pthread_free(session);
return NULL;
}
static int bfd_pthread_create(bfd_state_t *session)
{
int rcode;
pthread_attr_t attr;
if (pipe(session->pipefd) < 0) {
ERROR("Failed opening pipe: %s", fr_syserror(errno));
return 0;
}
session->el = fr_event_list_alloc(session, NULL, NULL);
if (!session->el) {
ERROR("Failed creating event list");
close_pipes:
close(session->pipefd[0]);
close(session->pipefd[1]);
session->pipefd[0] = session->pipefd[1] = -1;
return 0;
}
#ifdef O_NONBLOCK
fcntl(session->pipefd[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC);
fcntl(session->pipefd[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC);
#endif
if (fr_event_fd_insert(session->el, session->pipefd[0], bfd_pipe_recv, NULL, NULL, session) < 0) {
PERROR("Failed inserting file descriptor into event list");
goto close_pipes;
}
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
/*
* Create the thread detached, so that it cleans up it's
* own memory when it exits.
*
* Note that the function returns non-zero on error, NOT
* -1. The return code is the error, and errno isn't set.
*/
rcode = pthread_create(&session->pthread_id, &attr,
bfd_child_thread, session);
if (rcode != 0) {
talloc_free(session->el);
session->el = NULL;
ERROR("Thread create failed: %s", fr_syserror(rcode));
goto close_pipes;
}
pthread_attr_destroy(&attr);
return 1;
}
static const char *bfd_state[] = {
"admin-down",
"down",
"init",
"up"
};
static void bfd_request(bfd_state_t *session, REQUEST *request,
RADIUS_PACKET *packet)
{
memset(request, 0, sizeof(*request));
memset(packet, 0, sizeof(*packet));
request->packet = packet;
request->server = session->server;
packet->src_ipaddr = session->local_ipaddr;
packet->src_port = session->local_port;
packet->dst_ipaddr = session->remote_ipaddr;
packet->dst_port = session->remote_port;
/* request->heap_offset = -1; */
}
static void bfd_trigger(bfd_state_t *session)
{
RADIUS_PACKET packet;
REQUEST request;
char buffer[256];
snprintf(buffer, sizeof(buffer), "server.bfd.%s",
bfd_state[session->session_state]);
bfd_request(session, &request, &packet);
trigger_exec(&request, NULL, buffer, false, NULL);
}
static void bfd_session_free(void *ctx)
{
bfd_state_t *session = ctx;
if (el != session->el) {
/*
* FIXME: this isn't particularly safe.
*/
bfd_pthread_free(session);
}
talloc_free(session);
}
static ssize_t bfd_parse_secret(CONF_SECTION *cs, uint8_t secret[BFD_MAX_SECRET_LENGTH])
{
int rcode;
size_t len;
char const *value = NULL;
rcode = cf_pair_parse(NULL, cs, "secret", FR_ITEM_POINTER(PW_TYPE_STRING, &value), NULL, T_INVALID);
if (rcode != 0) return 0;
len = strlen(value);
if ((value[0] == '0') && (value[1] == 'x')) {
if (len > 42) {
cf_log_err(cf_section_to_item(cs), "Secret is too long");
return -1;
}
if ((len & 0x01) != 0) {
cf_log_err(cf_section_to_item(cs), "Invalid hex length");
return -1;
}
return fr_hex2bin(secret, BFD_MAX_SECRET_LENGTH, value + 2, (len - 2));
}
if (len >= 20) {
cf_log_err(cf_section_to_item(cs), "Secret is too long");
return -1;
}
memset(secret, 0, BFD_MAX_SECRET_LENGTH);
memcpy(secret, value, len);
return len;
}
/*
* Create a new session.
*/
static bfd_state_t *bfd_new_session(bfd_socket_t *sock, int sockfd,
CONF_SECTION *cs,
const fr_ipaddr_t *ipaddr, uint16_t port)
{
int rcode;
bool flag;
uint32_t number;
bfd_state_t *session;
session = talloc_zero(sock, bfd_state_t);
/*
* Initialize according to RFC.
*/
session->number = sock->number++;
session->sockfd = sockfd;
session->session_state = BFD_STATE_DOWN;
session->server = sock->server;
session->unlang = sock->unlang;
session->local_disc = fr_rand();
session->remote_disc = 0;
session->local_diag = BFD_DIAG_NONE;
session->desired_min_tx_interval = sock->min_tx_interval * 1000;
session->required_min_rx_interval = sock->min_rx_interval * 1000;
session->remote_min_rx_interval = 1;
session->demand_mode = sock->demand;
session->remote_demand_mode = false;
session->detect_multi = sock->max_timeouts;
session->auth_type = BFD_AUTH_RESERVED;
session->recv_auth_seq = 0;
session->xmit_auth_seq = fr_rand();
session->auth_seq_known = 0;
/*
* Allow over-riding of variables per session.
*/
rcode = cf_pair_parse(NULL, cs, "demand", FR_ITEM_POINTER(PW_TYPE_BOOLEAN, &flag), NULL, T_INVALID);
if (rcode == 0) {
session->demand_mode = flag;
}
rcode = cf_pair_parse(NULL, cs, "min_transmit_interval", FR_ITEM_POINTER(PW_TYPE_INTEGER, &number), NULL, T_INVALID);
if (rcode == 0) {
if (number < 100) number = 100;
if (number > 10000) number = 10000;
session->desired_min_tx_interval = number * 1000;
}
rcode = cf_pair_parse(NULL, cs, "min_receive_interval", FR_ITEM_POINTER(PW_TYPE_INTEGER, &number), NULL, T_INVALID);
if (rcode == 0) {
if (number < 100) number = 100;
if (number > 10000) number = 10000;
session->required_min_rx_interval = number * 1000;
}
rcode = cf_pair_parse(NULL, cs, "max_timeouts", FR_ITEM_POINTER(PW_TYPE_INTEGER, &number), NULL, T_INVALID);
if (rcode == 0) {
if (number == 0) number = 1;
if (number > 10) number = 10;
session->detect_multi = number;
}
session->auth_type = sock->auth_type;
/*
* Parse / over-ride the secrets.
*/
session->secret_len = bfd_parse_secret(cs, session->secret);
if ((session->secret_len == 0) &&
(session->auth_type != BFD_AUTH_RESERVED)) {
if (sock->secret_len == 0) {
cf_log_err(cf_section_to_item(cs), "auth_type requires a secret");
talloc_free(session);
return NULL;
}
session->secret_len = sock->secret_len;
memcpy(session->secret, sock->secret, sizeof(session->secret));
}
/*
* Initialize the detection time.
*/
if (!session->demand_mode) {
session->detection_time = session->required_min_rx_interval;
} else {
session->detection_time = session->desired_min_tx_interval;
}
session->detection_time *= session->detect_multi;
/*
* And finally remember the session.
*/
session->remote_ipaddr = *ipaddr;
session->remote_port = port;
session->local_ipaddr = sock->my_ipaddr;
session->local_port = sock->my_port;
fr_ipaddr_to_sockaddr(ipaddr, port,
&session->remote_sockaddr, &session->salen);
if (!rbtree_insert(sock->session_tree, session)) {
ERROR("FAILED creating new session!");
talloc_free(session);
return NULL;
}
bfd_trigger(session);
/*
* Check for threaded / non-threaded operation.
*/
if (el) {
session->el = el;
bfd_start_control(session);
session->pipefd[0] = session->pipefd[1] = -1;
session->pthread_id = pthread_self();
} else {
if (!bfd_pthread_create(session)) {
rbtree_deletebydata(sock->session_tree, session);
talloc_free(session);
return NULL;
}
}
return session;
}
static int bfd_verify_sequence(bfd_state_t *session, uint32_t sequence_no,
int keyed)
{
uint32_t start, stop;
start = session->recv_auth_seq;
if (keyed) {
start++;
}
stop = start + 3 * session->detect_multi;
if (start < stop) {
if ((sequence_no < start) ||
(sequence_no > stop)) {
return 0;
}
} else { /* start is ~2^32, stop is ~10 */
if ((sequence_no > start) &&
(sequence_no < stop)) {
return 0;
}
}
return 1;
}
static void bfd_calc_md5(bfd_state_t *session, bfd_packet_t *bfd)
{
FR_MD5_CTX ctx;
bfd_auth_md5_t *md5 = &bfd->auth.md5;
rad_assert(session->secret_len <= sizeof(md5->digest));
rad_assert(md5->auth_len == sizeof(*md5));
memset(md5->digest, 0, sizeof(md5->digest));
memcpy(md5->digest, session->secret, session->secret_len);
fr_md5_init(&ctx);
fr_md5_update(&ctx, (const uint8_t *) bfd, bfd->length);
fr_md5_final(md5->digest, &ctx);
}
static void bfd_auth_md5(bfd_state_t *session, bfd_packet_t *bfd)
{
bfd_auth_md5_t *md5 = &bfd->auth.md5;
md5->auth_type = session->auth_type;
md5->auth_len = sizeof(*md5);
bfd->length += md5->auth_len;
md5->key_id = 0;
md5->sequence_no = session->xmit_auth_seq++;
bfd_calc_md5(session, bfd);
}
static int bfd_verify_md5(bfd_state_t *session, bfd_packet_t *bfd)
{
int rcode;
bfd_auth_md5_t *md5 = &bfd->auth.md5;
uint8_t digest[sizeof(md5->digest)];
if (md5->auth_len != sizeof(*md5)) return 0;
if (md5->key_id != 0) return 0;
memcpy(digest, md5->digest, sizeof(digest));
bfd_calc_md5(session, bfd);
rcode = fr_radius_digest_cmp(digest, md5->digest, sizeof(digest));
memcpy(md5->digest, digest, sizeof(md5->digest)); /* pedantic */
if (rcode != 0) {
DEBUG("BFD %d MD5 Digest failed: **** RE-ENTER THE SECRET ON BOTH ENDS ****", session->number);
return 0;
}
/*
* Do this AFTER the authentication instead of before!
*/
if (!session->auth_seq_known) {
session->auth_seq_known = 1;
} else if (!bfd_verify_sequence(session, md5->sequence_no,
(md5->auth_type == BFD_AUTH_MET_KEYED_MD5))) {
DEBUG("MD5 sequence out of window");
return 0;
}
session->recv_auth_seq = md5->sequence_no;
return 1;
}
static void bfd_calc_sha1(bfd_state_t *session, bfd_packet_t *bfd)
{
fr_sha1_ctx ctx;
bfd_auth_sha1_t *sha1 = &bfd->auth.sha1;
rad_assert(session->secret_len <= sizeof(sha1->digest));
rad_assert(sha1->auth_len == sizeof(*sha1));
memset(sha1->digest, 0, sizeof(sha1->digest));
memcpy(sha1->digest, session->secret, session->secret_len);
fr_sha1_init(&ctx);
fr_sha1_update(&ctx, (const uint8_t *) bfd, bfd->length);
fr_sha1_final(sha1->digest, &ctx);
}
static void bfd_auth_sha1(bfd_state_t *session, bfd_packet_t *bfd)
{
bfd_auth_sha1_t *sha1 = &bfd->auth.sha1;
sha1->auth_type = session->auth_type;
sha1->auth_len = sizeof(*sha1);
bfd->length += sha1->auth_len;
sha1->key_id = 0;
sha1->sequence_no = session->xmit_auth_seq++;
bfd_calc_sha1(session, bfd);
}
static int bfd_verify_sha1(bfd_state_t *session, bfd_packet_t *bfd)
{
int rcode;
bfd_auth_sha1_t *sha1 = &bfd->auth.sha1;
uint8_t digest[sizeof(sha1->digest)];
if (sha1->auth_len != sizeof(*sha1)) return 0;
if (sha1->key_id != 0) return 0;
memcpy(digest, sha1->digest, sizeof(digest));
bfd_calc_sha1(session, bfd);
rcode = fr_radius_digest_cmp(digest, sha1->digest, sizeof(digest));
memcpy(sha1->digest, digest, sizeof(sha1->digest)); /* pedantic */
if (rcode != 0) {
DEBUG("BFD %d SHA1 Digest failed: **** RE-ENTER THE SECRET ON BOTH ENDS ****", session->number);
return 0;
}
/*
* Do this AFTER the authentication instead of before!
*/
if (!session->auth_seq_known) {
session->auth_seq_known = 1;
} else if (!bfd_verify_sequence(session, sha1->sequence_no,
(sha1->auth_type == BFD_AUTH_MET_KEYED_SHA1))) {
DEBUG("SHA1 sequence out of window");
return 0;
}
session->recv_auth_seq = sha1->sequence_no;
return 1;
}
static int bfd_authenticate(bfd_state_t *session, bfd_packet_t *bfd)
{
switch (bfd->auth.basic.auth_type) {
case BFD_AUTH_RESERVED:
return 0;
case BFD_AUTH_SIMPLE:
break;
case BFD_AUTH_KEYED_MD5:
case BFD_AUTH_MET_KEYED_MD5:
return bfd_verify_md5(session, bfd);
case BFD_AUTH_KEYED_SHA1:
case BFD_AUTH_MET_KEYED_SHA1:
return bfd_verify_sha1(session, bfd);
}
return 0;
}
static void bfd_control_packet_init(bfd_state_t *session,
bfd_packet_t *bfd)
{
memset(bfd, 0, sizeof(*bfd));
bfd->version = 1;
bfd->diag = session->local_diag;
bfd->state = session->session_state;
bfd->poll = 0; /* fixed by poll response */
bfd->final = 0; /* fixed by poll response */
bfd->control_plane_independent = 0;
if (session->auth_type == BFD_AUTH_RESERVED) {
bfd->auth_present = 0;
} else {
bfd->auth_present = 1;
}
/*
* If we're UP / UP, signal that we've entered demand
* mode, and stop sending packets.
*/
if (session->demand_mode &&
(session->session_state == BFD_STATE_UP) &&
(session->remote_session_state == BFD_STATE_UP)) {
bfd->demand = true;
DEBUG("BFD %d demand mode UP / UP, sending ACK and done.",
session->number);
bfd_stop_control(session);
} else {
bfd->demand = false;
}
bfd->multipoint = 0;
bfd->detect_multi = session->detect_multi;
bfd->length = 24; /* auth types add to this later */
bfd->my_disc = session->local_disc;
bfd->your_disc = session->remote_disc;
bfd->desired_min_tx_interval = session->desired_min_tx_interval;
bfd->required_min_rx_interval = session->required_min_rx_interval;
bfd->min_echo_rx_interval = session->my_min_echo_rx_interval;
}
static void bfd_sign(bfd_state_t *session, bfd_packet_t *bfd)
{
if (bfd->auth_present) {
switch (session->auth_type) {
case BFD_AUTH_RESERVED:
break;
case BFD_AUTH_SIMPLE:
break;
case BFD_AUTH_KEYED_MD5:
case BFD_AUTH_MET_KEYED_MD5:
bfd_auth_md5(session, bfd);
break;
case BFD_AUTH_KEYED_SHA1:
case BFD_AUTH_MET_KEYED_SHA1:
bfd_auth_sha1(session, bfd);
break;
}
}
}
/*
* Send a packet.
*/
static void bfd_send_packet(UNUSED fr_event_list_t *eel, UNUSED struct timeval *now, void *ctx)
{
bfd_state_t *session = ctx;
bfd_packet_t bfd;
bfd_control_packet_init(session, &bfd);
if (session->doing_poll) {
bfd.poll = 1;
}
if (!bfd.demand) {
bfd_start_packets(session);
}
bfd_sign(session, &bfd);
DEBUG("BFD %d sending packet state %s",
session->number, bfd_state[session->session_state]);
if (sendto(session->sockfd, &bfd, bfd.length, 0,
(struct sockaddr *) &session->remote_sockaddr,
session->salen) < 0) {
ERROR("Failed sending packet: %s", fr_syserror(errno));
}
}
static int bfd_start_packets(bfd_state_t *session)
{
uint32_t interval, base;
uint64_t jitter;
struct timeval now;
/*
* Reset the timers.
*/
fr_event_timer_delete(session->el, &session->ev_packet);
gettimeofday(&session->last_sent, NULL);
now = session->last_sent;
if (session->desired_min_tx_interval >= session->remote_min_rx_interval) {
interval = session->desired_min_tx_interval;
} else {
interval = session->remote_min_rx_interval;
}
base = (interval * 3) / 4;
jitter = fr_rand(); /* 32-bit number */
if (session->detect_multi == 1) {
jitter *= 644245094; /* 15% of 2^32 */
} else {
jitter *= (1 << 30); /* 25% of 2^32 */
}
jitter >>= 32;
jitter *= interval;
jitter >>= 32;
interval = base;
interval += jitter;
if (interval >= USEC) {
now.tv_sec += interval / USEC;
}
now.tv_usec += interval % USEC;
if (now.tv_usec >= USEC) {
now.tv_sec++;
now.tv_usec -= USEC;
}
if (fr_event_timer_insert(session->el, bfd_send_packet, session, &now,
&session->ev_packet) < 0) {
rad_assert("Failed to insert event" == NULL);
}
return 0;
}
static void bfd_set_timeout(bfd_state_t *session, struct timeval *when)
{
struct timeval now = *when;
fr_event_timer_delete(session->el, &session->ev_timeout);
if (session->detection_time >= USEC) {
now.tv_sec += session->detection_time / USEC;
}
now.tv_usec += session->detection_time % USEC;
if (now.tv_usec >= USEC) {
now.tv_sec++;
now.tv_usec -= USEC;
}
if (session->detect_multi >= 2) {
uint32_t delay;
session->next_recv = *when;
delay = session->detection_time / session->detect_multi;
delay += delay / 2;
if (delay > USEC) {
session->next_recv.tv_sec += delay / USEC;
}
session->next_recv.tv_usec += delay % USEC;
if (session->next_recv.tv_usec >= USEC) {
session->next_recv.tv_sec++;
session->next_recv.tv_usec -= USEC;
}
}
if (fr_event_timer_insert(session->el, bfd_detection_timeout, session, &now,
&session->ev_timeout) < 0) {
rad_assert("Failed to insert event" == NULL);
}
}
static int bfd_start_control(bfd_state_t *session)
{
if (session->remote_min_rx_interval == 0) return 0;
if ((session->remote_disc == 0) && session->passive) return 0;
if (session->remote_demand_mode &&
(session->session_state == BFD_STATE_UP) &&
(session->remote_session_state == BFD_STATE_UP) &&
!session->doing_poll) {
DEBUG("BFD %d warning: asked to start UP / UP ?",
session->number);
rad_assert(0 == 1);
bfd_stop_control(session);
return 0;
}
bfd_set_timeout(session, &session->last_recv);
if (session->ev_packet) return 0;
return bfd_start_packets(session);
}
static int bfd_stop_control(bfd_state_t *session)
{