-
Notifications
You must be signed in to change notification settings - Fork 15
/
udpst_control.c
2021 lines (1926 loc) · 92.3 KB
/
udpst_control.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
/*
* Copyright (c) 2020, Broadband Forum
* Copyright (c) 2020, AT&T Communications
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
* UDP Speed Test - udpst_control.c
*
* This file handles the control message processing needed to setup and
* activate test sessions. This includes allocating connections and managing
* the associated sockets.
*
* Author Date Comments
* -------------------- ---------- ----------------------------------
* Len Ciavattone 01/16/2019 Created
* Len Ciavattone 10/18/2019 Add param for load sample period
* Len Ciavattone 11/04/2019 Add minimum delays to summary
* Len Ciavattone 06/16/2020 Add dual-stack (IPv6) support
* Len Ciavattone 07/02/2020 Added (HMAC-SHA256) authentication
* Len Ciavattone 08/04/2020 Rearranged source files
* Len Ciavattone 09/03/2020 Added __linux__ conditionals
* Len Ciavattone 11/10/2020 Add option to ignore OoO/Dup
* Len Ciavattone 10/13/2021 Refresh with clang-format
* Add TR-181 fields in JSON
* Add interface traffic rate support
* Len Ciavattone 11/18/2021 Add backward compat. protocol version
* Add bandwidth management support
* Len Ciavattone 12/08/2021 Add starting sending rate
* Len Ciavattone 12/17/2021 Add payload randomization
* Len Ciavattone 02/02/2022 Add rate adj. algo. selection
* Len Ciavattone 01/01/2023 Add timer to prevent client from
* processing rogue load PDUs forever
* Len Ciavattone 01/14/2023 Add multi-connection support
* Len Ciavattone 02/07/2023 Randomize start of send intervals
* Len Ciavattone 02/14/2023 Add per-server port selection
* Len Ciavattone 03/05/2023 Fix server setup error messages
* Len Ciavattone 03/22/2023 Add GSO and GRO optimizations
* Len Ciavattone 03/25/2023 GRO replaced w/recvmmsg+truncation
* Len Ciavattone 05/24/2023 Add data output (export) capability
* Len Ciavattone 10/01/2023 Updated ErrorStatus values
* Len Ciavattone 12/18/2023 Add server msg for invalid setup req
* Len Ciavattone 02/23/2024 Add status feedback loss to export
* Len Ciavattone 03/03/2024 Add multi-key support
* Len Ciavattone 04/12/2024 Enhanced control PDU integrity checks
* Len Ciavattone 06/24/2024 Add interface Mbps to export
* Len Ciavattone 07/02/2024 Preset dir for bw dealloc on timeout
*
*/
#define UDPST_CONTROL
#if __linux__
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <time.h>
#include <netdb.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <sys/file.h>
#ifdef AUTH_KEY_ENABLE
#include <openssl/hmac.h>
#include <openssl/x509.h>
#endif
#else
#include "../udpst_control_alt1.h"
#endif
//
#include "cJSON.h"
#include "udpst_common.h"
#include "udpst_protocol.h"
#include "udpst.h"
#include "udpst_control.h"
#include "udpst_data.h"
#ifndef __linux__
#include "../udpst_control_alt2.h"
#endif
//----------------------------------------------------------------------------
//
// Internal function prototypes
//
int timeout_testinit(int);
int service_actreq(int);
int service_actresp(int);
int sock_connect(int);
int connected(int);
int open_outputfile(int);
BOOL validate_auth(void);
BOOL verify_ctrlpdu(int, struct controlHdrSR *, struct controlHdrTA *, char *, char *);
//----------------------------------------------------------------------------
//
// External data
//
extern int errConn, monConn, aggConn;
extern char scratch[STRING_SIZE];
extern struct configuration conf;
extern struct repository repo;
extern struct connection *conn;
extern char *boolText[];
extern char *rateAdjAlgo[];
//
extern cJSON *json_top, *json_output;
//----------------------------------------------------------------------------
//
// Global data
//
#define SRAUTO_TEXT "<Auto>"
#define OWD_TEXT "OWD"
#define RTT_TEXT "RTT"
#define ZERO_TEXT "zeroes"
#define RAND_TEXT "random"
#define TESTHDR_LINE1 \
"%s%s Test Int(sec): %d, DelayVar Thresh(ms): %d-%d [%s], Trial Int(ms): %d, Ignore OoO/Dup: %s, Payload: %s,\n"
#define TESTHDR_LINE2 " ID: %d, SR Index: %s, Cong. Thresh: %d, HS Delta: %d, SeqErr Thresh: %d, Algo: %s, Conn: %d, "
static char *testHdrV4 = TESTHDR_LINE1 TESTHDR_LINE2 "IPv4 ToS: %d%s\n";
static char *testHdrV6 = TESTHDR_LINE1 TESTHDR_LINE2 "IPv6 TClass: %d%s\n";
//----------------------------------------------------------------------------
// Function definitions
//----------------------------------------------------------------------------
//
// Initialize a connection structure
//
void init_conn(int connindex, BOOL cleanup) {
register struct connection *c = &conn[connindex];
int i;
//
// Cleanup prior to clear and init
//
if (cleanup) {
if (connindex == repo.maxConnIndex) {
for (i = connindex - 1; i >= 0; i--) {
if (conn[i].fd == -1)
continue;
repo.maxConnIndex = i;
break;
}
}
if (c->fd >= 0) {
#ifdef __linux__
// Event needed to be non-null before kernel version 2.6.9
epoll_ctl(repo.epollFD, EPOLL_CTL_DEL, c->fd, NULL);
#endif
close(c->fd);
}
if (c->outputFPtr != NULL)
fclose(c->outputFPtr);
}
//
// Clear structure
//
memset(&conn[connindex], 0, sizeof(struct connection));
//
// Initialize non-zero values
//
c->fd = -1;
c->priAction = &null_action;
c->secAction = &null_action;
c->timer1Action = &null_action;
c->timer2Action = &null_action;
c->timer3Action = &null_action;
return;
}
//----------------------------------------------------------------------------
//
// Null action routine
//
int null_action(int connindex) {
(void) (connindex);
return 0;
}
//----------------------------------------------------------------------------
//
// Client function to send setup request to server's control port
//
// A setup response is expected back from the server
//
int send_setupreq(int connindex, int mcIndex, int serverIndex) {
register struct connection *c = &conn[connindex], *a;
int var;
struct timespec tspecvar;
char addrstr[INET6_ADDR_STRLEN], portstr[8], intfpath[IFNAMSIZ + 64];
struct controlHdrSR *cHdrSR = (struct controlHdrSR *) repo.defBuffer;
#ifdef AUTH_KEY_ENABLE
char *key;
unsigned int uvar;
#endif
//
// Additional initialization on first setup request
//
if (c->mcIndex == 0) {
//
// Open local sysfs interface statistics
//
if (*conf.intfName) {
var = sprintf(intfpath, "/sys/class/net/%s/statistics/", conf.intfName);
if (conf.usTesting)
strcat(&intfpath[var], "tx_bytes");
else
strcat(&intfpath[var], "rx_bytes");
if ((repo.intfFD = open(intfpath, O_RDONLY)) < 0) {
var = sprintf(scratch, "OPEN ERROR: %s (%s)\n", strerror(errno), intfpath);
send_proc(errConn, scratch, var);
return -1;
}
}
//
// Init aggregate connection and aggregate query timer
//
a = &conn[aggConn]; // Aggregate connection pointer
if (conf.usTesting) {
a->testType = TEST_TYPE_US;
} else {
a->testType = TEST_TYPE_DS;
}
tspecvar.tv_sec = 0;
tspecvar.tv_nsec = AGG_QUERY_TIME * NSECINMSEC;
tspecplus(&repo.systemClock, &tspecvar, &a->timer1Thresh);
a->timer1Action = &agg_query_proc;
a->state = S_DATA; // Allow for data timer processing
}
repo.actConnCount++; // Increment active test connection count
//
// Build setup request PDU
//
memset(cHdrSR, 0, CHSR_SIZE_CVER);
cHdrSR->controlId = htons(CHSR_ID);
c->protocolVer = PROTOCOL_VER; // Client always uses current version
cHdrSR->protocolVer = htons((uint16_t) c->protocolVer);
c->mcIndex = mcIndex; // Multi-connection index of this connection
cHdrSR->mcIndex = (uint8_t) c->mcIndex;
c->mcCount = conf.maxConnCount; // Configured maximum multi-connection count
cHdrSR->mcCount = (uint8_t) c->mcCount;
if (repo.mcIdent == 0) {
repo.mcIdent = getuniform(1, UINT16_MAX); // Random (non-zero) multi-connection identifier
}
c->mcIdent = repo.mcIdent;
cHdrSR->mcIdent = htons((uint16_t) c->mcIdent);
cHdrSR->cmdRequest = CHSR_CREQ_SETUPREQ;
cHdrSR->cmdResponse = CHSR_CRSP_NONE;
if (conf.maxBandwidth > 0) {
// Each connection requests 1/Nth the total bandwidth
if ((c->maxBandwidth = conf.maxBandwidth / conf.maxConnCount) < MIN_REQUIRED_BW)
c->maxBandwidth = MIN_REQUIRED_BW;
var = c->maxBandwidth;
if (conf.usTesting)
var |= CHSR_USDIR_BIT; // Set upstream bit of max bandwidth being transmitted
cHdrSR->maxBandwidth = htons((uint16_t) var);
}
if (conf.jumboStatus) {
cHdrSR->modifierBitmap |= CHSR_JUMBO_STATUS;
}
if (conf.traditionalMTU) {
cHdrSR->modifierBitmap |= CHSR_TRADITIONAL_MTU;
}
if (*conf.authKey == '\0' && conf.keyFile == NULL) {
cHdrSR->authMode = AUTHMODE_NONE;
cHdrSR->authUnixTime = 0;
cHdrSR->keyId = 0;
#ifdef AUTH_KEY_ENABLE
} else {
cHdrSR->authMode = AUTHMODE_SHA256;
cHdrSR->authUnixTime = htonl((uint32_t) repo.systemClock.tv_sec);
cHdrSR->keyId = (uint8_t) conf.keyId;
if (*conf.authKey != '\0') {
key = conf.authKey;
} else {
key = repo.key[repo.keyIndex].key;
}
HMAC(EVP_sha256(), key, strlen(key), (const unsigned char *) cHdrSR, CHSR_SIZE_CVER, cHdrSR->authDigest, &uvar);
#endif
}
#ifdef ADD_HEADER_CSUM
cHdrSR->checkSum = checksum(cHdrSR, CHSR_SIZE_CVER); // Added after HMAC (server MUST clear before HMAC validation)
#endif
//
// Update global address info for subsequent send
//
c->serverIndex = serverIndex;
if ((var = sock_mgmt(connindex, repo.server[serverIndex].ip, repo.server[serverIndex].port, NULL, SMA_UPDATE)) != 0) {
send_proc(errConn, scratch, var);
return -1;
}
//
// Send setup request PDU (socket not yet connected)
//
var = CHSR_SIZE_CVER;
if (send_proc(connindex, (char *) cHdrSR, var) != var)
return -1;
if (conf.verbose) {
getnameinfo((struct sockaddr *) &repo.remSas, repo.remSasLen, addrstr, INET6_ADDR_STRLEN, portstr, sizeof(portstr),
NI_NUMERICHOST | NI_NUMERICSERV);
var = sprintf(scratch, "[%d]Setup request (%d.%d) sent from %s:%d to %s:%s\n", connindex, c->mcIndex, c->mcIdent,
c->locAddr, c->locPort, addrstr, portstr);
send_proc(monConn, scratch, var);
}
//
// Set timeout timer awaiting test initiation
//
tspecvar.tv_sec = TIMEOUT_NOTRAFFIC;
tspecvar.tv_nsec = 0;
tspecplus(&repo.systemClock, &tspecvar, &c->timer3Thresh);
c->timer3Action = &timeout_testinit;
return 0;
}
//----------------------------------------------------------------------------
//
// Client function to process timeout awaiting test initiation
//
int timeout_testinit(int connindex) {
register struct connection *c = &conn[connindex];
int var;
//
// Clear timeout timer
//
tspecclear(&c->timer3Thresh);
c->timer3Action = &null_action;
//
// Notify user and set immediate end time
//
var = sprintf(scratch, "WARNING: Timeout awaiting response from server %s:%d\n", repo.server[c->serverIndex].ip,
repo.server[c->serverIndex].port);
send_proc(errConn, scratch, var);
repo.endTimeStatus = STATUS_WARNBASE + WARN_SRV_TIMEOUT; // ErrorStatus
tspeccpy(&c->endTime, &repo.systemClock);
return 0;
}
//----------------------------------------------------------------------------
//
// Server function to service client setup request received on control port
//
// A new test connection is allocated and a setup response is sent back
//
int service_setupreq(int connindex) {
register struct connection *c = &conn[connindex];
int i = -1, var, pver, mbw = 0, currbw = repo.dsBandwidth;
BOOL usbw = FALSE;
struct timespec tspecvar;
char addrstr[INET6_ADDR_STRLEN], portstr[8];
struct controlHdrSR *cHdrSR = (struct controlHdrSR *) repo.defBuffer;
//
// Verify PDU
//
getnameinfo((struct sockaddr *) &repo.remSas, repo.remSasLen, addrstr, INET6_ADDR_STRLEN, portstr, sizeof(portstr),
NI_NUMERICHOST | NI_NUMERICSERV);
if (!verify_ctrlpdu(connindex, cHdrSR, NULL, addrstr, portstr)) {
return 0; // Ignore bad PDU
}
//
// Check specifics of setup request from client
//
var = 0; // Used for error indication throughout next section
pver = (int) ntohs(cHdrSR->protocolVer);
mbw = (int) (ntohs(cHdrSR->maxBandwidth) & ~CHSR_USDIR_BIT); // Obtain max bandwidth while ignoring upstream bit
if (ntohs(cHdrSR->maxBandwidth) & CHSR_USDIR_BIT) {
usbw = TRUE; // Max bandwidth is for upstream
currbw = repo.usBandwidth;
}
if (pver < PROTOCOL_MIN || pver > PROTOCOL_VER) {
var = sprintf(scratch, "ERROR: Invalid version (%d) in setup request from", pver);
cHdrSR->protocolVer = htons(PROTOCOL_VER); // Send back expected version
cHdrSR->cmdResponse = CHSR_CRSP_BADVER;
} else if (cHdrSR->mcCount == 0 || cHdrSR->mcCount > MAX_MC_COUNT || cHdrSR->mcIndex >= cHdrSR->mcCount) {
var = sprintf(scratch, "ERROR: Invalid multi-connection parameters (%d,%d) in setup request from", cHdrSR->mcIndex,
cHdrSR->mcCount);
cHdrSR->cmdResponse = CHSR_CRSP_MCINVPAR;
} else if (((cHdrSR->modifierBitmap & CHSR_JUMBO_STATUS) && !conf.jumboStatus) ||
(!(cHdrSR->modifierBitmap & CHSR_JUMBO_STATUS) && conf.jumboStatus)) {
var = sprintf(scratch, "ERROR: Invalid jumbo datagram option in setup request from");
cHdrSR->cmdResponse = CHSR_CRSP_BADJS;
} else if (((cHdrSR->modifierBitmap & CHSR_TRADITIONAL_MTU) && !conf.traditionalMTU) ||
(!(cHdrSR->modifierBitmap & CHSR_TRADITIONAL_MTU) && conf.traditionalMTU)) {
var = sprintf(scratch, "ERROR: Invalid traditional MTU option in setup request from");
cHdrSR->cmdResponse = CHSR_CRSP_BADTMTU;
} else if (conf.maxBandwidth > 0 && mbw == 0) {
var = sprintf(scratch, "ERROR: Required bandwidth not specified in setup request from");
cHdrSR->cmdResponse = CHSR_CRSP_NOMAXBW;
} else if (conf.maxBandwidth > 0 && currbw + mbw > conf.maxBandwidth) {
var = sprintf(scratch, "ERROR: Capacity exceeded (%d.%d) by required bandwidth (%d) in setup request from",
cHdrSR->mcIndex, (int) ntohs(cHdrSR->mcIdent), mbw);
cHdrSR->cmdResponse = CHSR_CRSP_CAPEXC;
} else if (cHdrSR->authMode != AUTHMODE_NONE && *conf.authKey == '\0' && conf.keyFile == NULL) {
var = sprintf(scratch, "ERROR: Unexpected authentication in setup request from");
cHdrSR->cmdResponse = CHSR_CRSP_AUTHNC;
#ifdef AUTH_KEY_ENABLE
#ifndef AUTH_IS_OPTIONAL
} else if (cHdrSR->authMode == AUTHMODE_NONE && (*conf.authKey != '\0' || conf.keyFile != NULL)) {
var = sprintf(scratch, "ERROR: Authentication missing in setup request from");
cHdrSR->cmdResponse = CHSR_CRSP_AUTHREQ;
#endif // AUTH_IS_OPTIONAL
} else if (cHdrSR->authMode != AUTHMODE_NONE && cHdrSR->authMode != AUTHMODE_SHA256) {
var = sprintf(scratch, "ERROR: Invalid authentication method in setup request from");
cHdrSR->cmdResponse = CHSR_CRSP_AUTHINV;
} else if (cHdrSR->authMode == AUTHMODE_SHA256 && (*conf.authKey != '\0' || conf.keyFile != NULL)) {
//
// Validate authentication digest (leave zeroed for response) and check time window if enforced
//
if (pver >= CHECKSUM_PVER)
cHdrSR->checkSum = 0; // MUST be cleared before HMAC validation
if (validate_auth()) {
var = sprintf(scratch, "ERROR: Authentication failure of setup request from");
cHdrSR->cmdResponse = CHSR_CRSP_AUTHFAIL;
} else if (AUTH_ENFORCE_TIME) {
tspecvar.tv_sec = (time_t) ntohl(cHdrSR->authUnixTime);
if (tspecvar.tv_sec < repo.systemClock.tv_sec - AUTH_TIME_WINDOW ||
tspecvar.tv_sec > repo.systemClock.tv_sec + AUTH_TIME_WINDOW) {
var = sprintf(scratch, "ERROR: Authentication time invalid in setup request from");
cHdrSR->cmdResponse = CHSR_CRSP_AUTHTIME;
}
}
#endif // AUTH_KEY_ENABLE
}
if (cHdrSR->cmdResponse == CHSR_CRSP_NONE) {
if (conf.verbose) {
if (pver < MULTIKEY_PVER) {
var = DEF_KEY_ID; // Use default key ID for older protocol versions
} else {
var = (int) cHdrSR->keyId; // Else obtain key ID specified by client
}
var = sprintf(scratch, "[%d]Setup request (%d.%d, Ver: %d, MaxBW: %d, KeyID: %d) received from %s:%s\n",
connindex, (int) cHdrSR->mcIndex, (int) ntohs(cHdrSR->mcIdent), pver, mbw, var, addrstr,
portstr);
send_proc(monConn, scratch, var);
}
//
// Obtain new test connection for this client
//
if ((i = new_conn(-1, repo.server[0].ip, 0, T_UDP, &recv_proc, &service_actreq)) < 0) {
var = 0; // Error message already output as part of allocation failure
cHdrSR->cmdResponse = CHSR_CRSP_CONNFAIL;
}
}
cHdrSR->cmdRequest = CHSR_CREQ_SETUPRSP; // Convert setup request to setup response
if (cHdrSR->cmdResponse != CHSR_CRSP_NONE) {
//
// Output error message if needed (append source info), send back setup response, and exit
//
if (var > 0) {
var += sprintf(&scratch[var], " %s:%s\n", addrstr, portstr);
send_proc(errConn, scratch, var);
}
if (pver >= CHECKSUM_PVER) {
cHdrSR->checkSum = 0;
#ifdef ADD_HEADER_CSUM
cHdrSR->checkSum = checksum(cHdrSR, repo.rcvDataSize);
#endif
}
send_proc(connindex, (char *) cHdrSR, repo.rcvDataSize);
return 0;
}
//
// Initialize new test connection obtained above as 'i'
//
conn[i].protocolVer = pver;
conn[i].mcIndex = (int) cHdrSR->mcIndex;
conn[i].mcCount = (int) cHdrSR->mcCount;
conn[i].mcIdent = (int) ntohs(cHdrSR->mcIdent);
if (conf.maxBandwidth > 0) {
conn[i].maxBandwidth = mbw; // Save bandwidth for adjustment at end of test
if (usbw) {
conn[i].testType = TEST_TYPE_US; // Preset direction to allow for bandwidth deallocation on timeout
repo.usBandwidth += mbw; // Update current upstream bandwidth
} else {
conn[i].testType = TEST_TYPE_DS; // Preset direction to allow for bandwidth deallocation on timeout
repo.dsBandwidth += mbw; // Update current downstream bandwidth
}
if (conf.verbose && mbw > 0) {
var = sprintf(scratch, "[%d]Bandwidth of %d allocated (New USBW: %d, DSBW: %d)\n", i, mbw, repo.usBandwidth,
repo.dsBandwidth);
send_proc(monConn, scratch, var);
}
}
//
// Set end time (used as watchdog) in case client goes quiet
//
tspecvar.tv_sec = TIMEOUT_NOTRAFFIC;
tspecvar.tv_nsec = 0;
tspecplus(&repo.systemClock, &tspecvar, &conn[i].endTime);
//
// Send setup response to client with port number of new test connection
//
cHdrSR->cmdResponse = CHSR_CRSP_ACKOK;
cHdrSR->testPort = htons((uint16_t) conn[i].locPort);
if (pver >= CHECKSUM_PVER) {
cHdrSR->checkSum = 0;
#ifdef ADD_HEADER_CSUM
cHdrSR->checkSum = checksum(cHdrSR, repo.rcvDataSize);
#endif
}
var = repo.rcvDataSize;
if (send_proc(connindex, (char *) cHdrSR, var) != var)
return 0;
if (conf.verbose) {
var = sprintf(scratch, "[%d]Setup response (%d.%d) sent from %s:%d to %s:%s\n", connindex, conn[i].mcIndex,
conn[i].mcIdent, c->locAddr, c->locPort, addrstr, portstr);
send_proc(monConn, scratch, var);
}
return 0;
}
//----------------------------------------------------------------------------
//
// Client function to service setup response received from server
//
// Send test activation request to server for the new test connection
//
int service_setupresp(int connindex) {
register struct connection *c = &conn[connindex];
int var;
char addrstr[INET6_ADDR_STRLEN], portstr[8];
struct controlHdrSR *cHdrSR = (struct controlHdrSR *) repo.defBuffer;
struct controlHdrTA *cHdrTA = (struct controlHdrTA *) repo.defBuffer;
//
// Verify PDU
//
if (!verify_ctrlpdu(connindex, cHdrSR, NULL, NULL, NULL)) {
return 0; // Ignore bad PDU
}
//
// Process any setup response errors
//
if (cHdrSR->cmdResponse != CHSR_CRSP_ACKOK) {
var = 0;
repo.endTimeStatus = CHSR_CRSP_ERRBASE + cHdrSR->cmdResponse; // ErrorStatus
switch (cHdrSR->cmdResponse) {
case CHSR_CRSP_BADVER:
var = sprintf(scratch, "ERROR: Client protocol version (%u) not accepted by server (%u)", PROTOCOL_VER,
ntohs(cHdrSR->protocolVer));
break;
case CHSR_CRSP_BADJS:
var = sprintf(scratch, "ERROR: Client jumbo datagram size option does not match server");
break;
case CHSR_CRSP_AUTHNC:
var = sprintf(scratch, "ERROR: Authentication not configured on server");
break;
case CHSR_CRSP_AUTHREQ:
var = sprintf(scratch, "ERROR: Authentication required by server");
break;
case CHSR_CRSP_AUTHINV:
var = sprintf(scratch, "ERROR: Authentication method does not match server");
break;
case CHSR_CRSP_AUTHFAIL:
var = sprintf(scratch, "ERROR: Authentication verification failed at server");
break;
case CHSR_CRSP_AUTHTIME:
var = sprintf(scratch, "ERROR: Authentication time outside time window of server");
break;
case CHSR_CRSP_NOMAXBW:
var = sprintf(scratch, "ERROR: Max bandwidth option required by server");
break;
case CHSR_CRSP_CAPEXC:
var = sprintf(scratch, "ERROR: Required max bandwidth exceeds available capacity on server");
break;
case CHSR_CRSP_BADTMTU:
var = sprintf(scratch, "ERROR: Client traditional MTU option does not match server");
break;
case CHSR_CRSP_MCINVPAR:
var = sprintf(scratch, "ERROR: Multi-connection parameters rejected by server");
break;
case CHSR_CRSP_CONNFAIL:
var = sprintf(scratch, "ERROR: Connection allocation failure on server");
break;
default:
repo.endTimeStatus = CHSR_CRSP_ERRBASE; // Unexpected values use only error base for ErrorStatus
var = sprintf(scratch, "ERROR: Unexpected CRSP (%u) in setup response from server", cHdrSR->cmdResponse);
}
if (var > 0) {
var += sprintf(&scratch[var], " %s:%d\n", repo.server[c->serverIndex].ip, repo.server[c->serverIndex].port);
send_proc(errConn, scratch, var);
}
tspeccpy(&c->endTime, &repo.systemClock); // Set for immediate close/exit
return 0;
}
//
// Obtain IP address and port number of sender
//
getnameinfo((struct sockaddr *) &repo.remSas, repo.remSasLen, addrstr, INET6_ADDR_STRLEN, portstr, sizeof(portstr),
NI_NUMERICHOST | NI_NUMERICSERV);
if (conf.verbose) {
var = sprintf(scratch, "[%d]Setup response (%d.%d) received from %s:%s\n", connindex, c->mcIndex, c->mcIdent,
addrstr, portstr);
send_proc(monConn, scratch, var);
}
//
// Update global address info with new server specified address/port number and connect socket
//
var = (int) ntohs(cHdrSR->testPort);
if ((var = sock_mgmt(connindex, addrstr, var, NULL, SMA_UPDATE)) != 0) {
send_proc(errConn, scratch, var);
return 0;
}
if (sock_connect(connindex) < 0)
return 0;
//
// Build test activation PDU
//
memset(cHdrTA, 0, CHTA_SIZE_CVER);
cHdrTA->controlId = htons(CHTA_ID);
cHdrTA->protocolVer = htons((uint16_t) c->protocolVer);
if (conf.usTesting) {
c->testType = TEST_TYPE_US;
cHdrTA->cmdRequest = CHTA_CREQ_TESTACTUS;
} else {
c->testType = TEST_TYPE_DS;
cHdrTA->cmdRequest = CHTA_CREQ_TESTACTDS;
}
cHdrTA->cmdResponse = CHTA_CRSP_NONE;
//
// Save configured parameters in connection and copy to test activation request
//
c->lowThresh = conf.lowThresh;
cHdrTA->lowThresh = htons((uint16_t) c->lowThresh);
c->upperThresh = conf.upperThresh;
cHdrTA->upperThresh = htons((uint16_t) c->upperThresh);
c->trialInt = conf.trialInt;
cHdrTA->trialInt = htons((uint16_t) c->trialInt);
c->testIntTime = conf.testIntTime;
cHdrTA->testIntTime = htons((uint16_t) c->testIntTime);
c->subIntPeriod = conf.subIntPeriod;
cHdrTA->subIntPeriod = (uint8_t) c->subIntPeriod;
c->ipTosByte = conf.ipTosByte;
cHdrTA->ipTosByte = (uint8_t) c->ipTosByte;
c->srIndexConf = conf.srIndexConf;
cHdrTA->srIndexConf = htons((uint16_t) c->srIndexConf);
c->useOwDelVar = (BOOL) conf.useOwDelVar;
cHdrTA->useOwDelVar = (uint8_t) c->useOwDelVar;
c->highSpeedDelta = conf.highSpeedDelta;
cHdrTA->highSpeedDelta = (uint8_t) c->highSpeedDelta;
c->slowAdjThresh = conf.slowAdjThresh;
cHdrTA->slowAdjThresh = htons((uint16_t) c->slowAdjThresh);
c->seqErrThresh = conf.seqErrThresh;
cHdrTA->seqErrThresh = htons((uint16_t) c->seqErrThresh);
c->ignoreOooDup = (BOOL) conf.ignoreOooDup;
cHdrTA->ignoreOooDup = (uint8_t) c->ignoreOooDup;
if (conf.srIndexIsStart) {
c->srIndexIsStart = TRUE; // Designate configured value as starting point
cHdrTA->modifierBitmap |= CHTA_SRIDX_ISSTART;
}
if (conf.randPayload) {
c->randPayload = TRUE;
cHdrTA->modifierBitmap |= CHTA_RAND_PAYLOAD;
}
c->rateAdjAlgo = conf.rateAdjAlgo;
cHdrTA->rateAdjAlgo = (uint8_t) c->rateAdjAlgo;
//
// Send test activation request
//
c->secAction = &service_actresp; // Set service handler for response
#ifdef ADD_HEADER_CSUM
cHdrTA->checkSum = checksum(cHdrTA, CHTA_SIZE_CVER);
#endif
var = CHTA_SIZE_CVER;
if (send_proc(connindex, (char *) cHdrTA, var) != var)
return 0;
if (conf.verbose) {
var = sprintf(scratch, "[%d]Test activation request (%d.%d) sent from %s:%d to %s:%d\n", connindex, c->mcIndex,
c->mcIdent, c->locAddr, c->locPort, c->remAddr, c->remPort);
send_proc(monConn, scratch, var);
}
return 0;
}
//----------------------------------------------------------------------------
//
// Server function to service test activation request received on new test connection
//
// Send test activation response back to client, connection is ready for testing
//
int service_actreq(int connindex) {
register struct connection *c = &conn[connindex];
int var;
char addrstr[INET6_ADDR_STRLEN], portstr[8];
struct sendingRate *sr = repo.sendingRates; // Set to first row of table
struct timespec tspecvar;
struct controlHdrTA *cHdrTA = (struct controlHdrTA *) repo.defBuffer;
//
// Verify PDU
//
getnameinfo((struct sockaddr *) &repo.remSas, repo.remSasLen, addrstr, INET6_ADDR_STRLEN, portstr, sizeof(portstr),
NI_NUMERICHOST | NI_NUMERICSERV);
if (!verify_ctrlpdu(connindex, NULL, cHdrTA, addrstr, portstr)) {
return 0; // Ignore bad PDU
}
//
// Update global address info with client address/port number and connect socket
//
if (conf.verbose) {
var = sprintf(scratch, "[%d]Test activation request (%d.%d) received from %s:%s\n", connindex, c->mcIndex,
c->mcIdent, addrstr, portstr);
send_proc(monConn, scratch, var);
}
var = atoi(portstr);
if ((var = sock_mgmt(connindex, addrstr, var, NULL, SMA_UPDATE)) != 0) {
send_proc(errConn, scratch, var);
return 0;
}
if (sock_connect(connindex) < 0)
return 0;
// ===================================================================
// Accept (but police) most test parameters as is and enforce server configured
// maximums where applicable. Update modified values for communication back to client.
// If the request needs to be rejected use command response value CHTA_CRSP_BADPARAM.
//
cHdrTA->cmdResponse = CHTA_CRSP_ACKOK; // Initialize to request accepted
//
// Low and upper delay variation thresholds
//
c->lowThresh = (int) ntohs(cHdrTA->lowThresh);
if (c->lowThresh < MIN_LOW_THRESH || c->lowThresh > MAX_LOW_THRESH) {
c->lowThresh = DEF_LOW_THRESH;
cHdrTA->lowThresh = htons((uint16_t) c->lowThresh);
}
c->upperThresh = (int) ntohs(cHdrTA->upperThresh);
if (c->upperThresh < MIN_UPPER_THRESH || c->upperThresh > MAX_UPPER_THRESH) {
c->upperThresh = DEF_UPPER_THRESH;
cHdrTA->upperThresh = htons((uint16_t) c->upperThresh);
}
if (c->lowThresh > c->upperThresh) { // Check for invalid relationship
c->lowThresh = DEF_LOW_THRESH;
cHdrTA->lowThresh = htons((uint16_t) c->lowThresh);
c->upperThresh = DEF_UPPER_THRESH;
cHdrTA->upperThresh = htons((uint16_t) c->upperThresh);
}
//
// Trial interval
//
c->trialInt = (int) ntohs(cHdrTA->trialInt);
if (c->trialInt < MIN_TRIAL_INT || c->trialInt > MAX_TRIAL_INT) {
c->trialInt = DEF_TRIAL_INT;
cHdrTA->trialInt = htons((uint16_t) c->trialInt);
}
//
// Test interval time and sub-interval period
//
c->testIntTime = (int) ntohs(cHdrTA->testIntTime);
if (c->testIntTime < MIN_TESTINT_TIME || c->testIntTime > MAX_TESTINT_TIME) {
c->testIntTime = DEF_TESTINT_TIME;
cHdrTA->testIntTime = htons((uint16_t) c->testIntTime);
} else if (c->testIntTime > conf.testIntTime) { // Enforce server maximum
c->testIntTime = conf.testIntTime;
cHdrTA->testIntTime = htons((uint16_t) c->testIntTime);
}
c->subIntPeriod = (int) cHdrTA->subIntPeriod;
if (c->subIntPeriod < MIN_SUBINT_PERIOD || c->subIntPeriod > MAX_SUBINT_PERIOD) {
c->subIntPeriod = DEF_SUBINT_PERIOD;
cHdrTA->subIntPeriod = (uint8_t) c->subIntPeriod;
}
if (c->subIntPeriod > c->testIntTime) { // Check for invalid relationship
c->testIntTime = DEF_TESTINT_TIME;
cHdrTA->testIntTime = htons((uint16_t) c->testIntTime);
c->subIntPeriod = DEF_SUBINT_PERIOD;
cHdrTA->subIntPeriod = (uint8_t) c->subIntPeriod;
}
//
// IP ToS/TClass byte (also set socket option)
//
c->ipTosByte = (int) cHdrTA->ipTosByte;
if (c->ipTosByte < MIN_IPTOS_BYTE || c->ipTosByte > MAX_IPTOS_BYTE) {
c->ipTosByte = DEF_IPTOS_BYTE;
cHdrTA->ipTosByte = (uint8_t) c->ipTosByte;
} else if (c->ipTosByte > conf.ipTosByte) { // Enforce server maximum
c->ipTosByte = conf.ipTosByte;
cHdrTA->ipTosByte = (uint8_t) c->ipTosByte;
}
if (c->ipTosByte != 0) {
if (c->ipProtocol == IPPROTO_IPV6)
var = IPV6_TCLASS;
else
var = IP_TOS;
if (setsockopt(c->fd, c->ipProtocol, var, (const void *) &c->ipTosByte, sizeof(c->ipTosByte)) < 0) {
c->ipTosByte = 0;
cHdrTA->ipTosByte = (uint8_t) c->ipTosByte;
}
}
//
// Static or starting sending rate index (special case <Auto>, which is the default but greater than max)
//
c->srIndexConf = (int) ntohs(cHdrTA->srIndexConf);
if (c->srIndexConf != DEF_SRINDEX_CONF) {
if (c->srIndexConf < MIN_SRINDEX_CONF || c->srIndexConf > MAX_SRINDEX_CONF) {
c->srIndexConf = DEF_SRINDEX_CONF;
cHdrTA->srIndexConf = htons((uint16_t) c->srIndexConf);
} else if (c->srIndexConf > conf.srIndexConf) { // Enforce server maximum
c->srIndexConf = conf.srIndexConf;
cHdrTA->srIndexConf = htons((uint16_t) c->srIndexConf);
}
if (cHdrTA->modifierBitmap & CHTA_SRIDX_ISSTART) {
c->srIndexIsStart = TRUE; // Designate configured value as starting point
c->srIndex = c->srIndexConf; // Set starting point from configured value
}
if (c->srIndexConf != DEF_SRINDEX_CONF)
sr = &repo.sendingRates[c->srIndexConf]; // Select starting SR table row
}
//
// Use one-way delay flag
//
c->useOwDelVar = (BOOL) cHdrTA->useOwDelVar;
if (c->useOwDelVar != TRUE && c->useOwDelVar != FALSE) { // Enforce C boolean
c->useOwDelVar = DEF_USE_OWDELVAR;
cHdrTA->useOwDelVar = (uint8_t) c->useOwDelVar;
}
//
// High-speed delta
//
c->highSpeedDelta = (int) cHdrTA->highSpeedDelta;
if (c->highSpeedDelta < MIN_HS_DELTA || c->highSpeedDelta > MAX_HS_DELTA) {
c->highSpeedDelta = DEF_HS_DELTA;
cHdrTA->highSpeedDelta = (uint8_t) c->highSpeedDelta;
}
//
// Slow rate adjustment threshold
//
c->slowAdjThresh = (int) ntohs(cHdrTA->slowAdjThresh);
if (c->slowAdjThresh < MIN_SLOW_ADJ_TH || c->slowAdjThresh > MAX_SLOW_ADJ_TH) {
c->slowAdjThresh = DEF_SLOW_ADJ_TH;
cHdrTA->slowAdjThresh = htons((uint16_t) c->slowAdjThresh);
}
//
// Sequence error threshold
//
c->seqErrThresh = (int) ntohs(cHdrTA->seqErrThresh);
if (c->seqErrThresh < MIN_SEQ_ERR_TH || c->seqErrThresh > MAX_SEQ_ERR_TH) {
c->seqErrThresh = DEF_SEQ_ERR_TH;
cHdrTA->seqErrThresh = htons((uint16_t) c->seqErrThresh);
}
//
// Ignore Out-of-Order/Duplicate flag
//
c->ignoreOooDup = (BOOL) cHdrTA->ignoreOooDup;
if (c->ignoreOooDup != TRUE && c->ignoreOooDup != FALSE) { // Enforce C boolean
c->ignoreOooDup = DEF_IGNORE_OOODUP;
cHdrTA->ignoreOooDup = (uint8_t) c->ignoreOooDup;
}
//
// Payload randomization (only allow if also configured on server)
//
if (cHdrTA->modifierBitmap & CHTA_RAND_PAYLOAD) {
if (conf.randPayload) {
c->randPayload = TRUE;
} else {
cHdrTA->modifierBitmap &= ~CHTA_RAND_PAYLOAD; // Reset bit for return
}
}
//
// Rate adjustment algorithm
//
c->rateAdjAlgo = (int) cHdrTA->rateAdjAlgo;
if (c->rateAdjAlgo < CHTA_RA_ALGO_MIN || c->rateAdjAlgo > CHTA_RA_ALGO_MAX) {
c->rateAdjAlgo = DEF_RA_ALGO;
cHdrTA->rateAdjAlgo = (uint8_t) c->rateAdjAlgo;
}
//
// If upstream test, send back initial sending rate transmission parameters
//
if (cHdrTA->cmdRequest == CHTA_CREQ_TESTACTUS) {
sr_copy(sr, &cHdrTA->srStruct, TRUE);
} else {
memset(&cHdrTA->srStruct, 0, sizeof(struct sendingRate));
}
// ===================================================================
//
// Continue updating connection if test activation is NOT being rejected
//
if (cHdrTA->cmdResponse == CHTA_CRSP_ACKOK) {
//
// Set connection test action as testing and initialize PDU received time
//
c->testAction = TEST_ACT_TEST;
tspeccpy(&c->pduRxTime, &repo.systemClock);
//
// Finalize connection for testing based on test type
//
if (cHdrTA->cmdRequest == CHTA_CREQ_TESTACTUS) {
//
// Upstream
// Setup to receive load PDUs and send status PDUs
//
c->testType = TEST_TYPE_US;
c->rttMinimum = INITIAL_MIN_DELAY;
c->rttSample = INITIAL_MIN_DELAY;
#ifdef HAVE_RECVMMSG
c->secAction = &service_recvmmsg;
#else
c->secAction = &service_loadpdu;
#endif
//
c->delayVarMin = INITIAL_MIN_DELAY;
tspeccpy(&c->trialIntClock, &repo.systemClock);
tspecvar.tv_sec = 0;
tspecvar.tv_nsec = (long) (c->trialInt * NSECINMSEC);
tspecplus(&repo.systemClock, &tspecvar, &c->timer1Thresh);
c->timer1Action = &send_statuspdu;
} else {
//
// Downstream
// Setup to receive status PDUs and send load PDUs
//
c->testType = TEST_TYPE_DS;
c->secAction = &service_statuspdu;
//
if (sr->txInterval1 > 0) {
var = getuniform(MIN_RANDOM_START * USECINMSEC, MAX_RANDOM_START * USECINMSEC);
tspecvar.tv_sec = 0;
tspecvar.tv_nsec = (long) (var * NSECINUSEC);
tspecplus(&repo.systemClock, &tspecvar, &c->timer1Thresh);
}
c->timer1Action = &send1_loadpdu;
if (sr->txInterval2 > 0) {
var = getuniform(MIN_RANDOM_START * USECINMSEC, MAX_RANDOM_START * USECINMSEC);
tspecvar.tv_sec = 0;
tspecvar.tv_nsec = (long) (var * NSECINUSEC);
tspecplus(&repo.systemClock, &tspecvar, &c->timer2Thresh);
}
c->timer2Action = &send2_loadpdu;
}
}
//
// Send test activation response to client
//
if (c->protocolVer >= CHECKSUM_PVER) {
cHdrTA->checkSum = 0;
#ifdef ADD_HEADER_CSUM
cHdrTA->checkSum = checksum(cHdrTA, repo.rcvDataSize);
#endif
}
var = repo.rcvDataSize;
if (send_proc(connindex, (char *) cHdrTA, var) != var)
return 0;
if (conf.verbose) {
var = sprintf(scratch, "[%d]Test activation response (%d.%d) sent from %s:%d to %s:%d\n", connindex, c->mcIndex,