-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfragmenter.c
executable file
·2809 lines (2485 loc) · 87.1 KB
/
fragmenter.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
/*
* (c) 2018 - 2022 - idlab - UGent - imec
*
* Bart Moons
*
* This file is part of the SCHC stack implementation
*
*/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "fragmenter.h"
#include "bit_operations.h"
uint8_t ATTEMPTS = 0; // for debugging
#if CLICK
#include <click/config.h>
#endif
// keep track of the active connections
static uint8_t FRAGMENTATION_BUF[MAX_MTU_LENGTH] = { 0 };
#if DYNAMIC_MEMORY
struct schc_fragmentation_t *schc_rx_conns;
struct schc_fragmentation_t *schc_tx_conns;
#else
struct schc_fragmentation_t schc_rx_conns[SCHC_CONF_RX_CONNS];
struct schc_fragmentation_t schc_tx_conns[SCHC_CONF_TX_CONNS];
static uint32_t buf_ptr = 0;
uint8_t schc_buf[STATIC_MEMORY_BUFFER_LENGTH] = { 0 };
static struct schc_mbuf_t MBUF_POOL[SCHC_CONF_MBUF_POOL_LEN];
#endif
static schc_fragmentation_t default_conn;
/**
* get the FCN value
*
* @param fragment a pointer to the fragment to retrieve the FCN from
*
* @return FCN the FCN as indicated by the fragment
*
* @note only FCN values up to 16 bits are currently supported
*
*/
static uint16_t get_fcn_value(uint8_t* fragment, schc_fragmentation_t* conn) {
uint8_t offset = conn->device->profile->RULE_ID_SIZE + conn->device->profile->DTAG_SIZE + conn->fragmentation_rule->WINDOW_SIZE;
return (uint16_t) get_bits(fragment, offset, conn->fragmentation_rule->FCN_SIZE);
}
/**
* get the ALL-1 FCN value
*
* @return FCN the all-1 fcn value
*
* @note only FCN values up to 16 bits are currently supported
*
*/
static uint16_t get_max_fcn_value(schc_fragmentation_t* conn) {
uint8_t fcn[2] = { 0 };
set_bits(fcn, 0, conn->fragmentation_rule->FCN_SIZE);
return (uint16_t) get_bits(fcn, 0, conn->fragmentation_rule->FCN_SIZE);
}
/**
* get the DTag value
*
* @param fragment a pointer to the fragment to retrieve the DTag from
* @return DTag the DTag as indicated by the fragment
*
* @note only DTag values up to 8 bits are currently supported
*
*/
static int16_t get_dtag_value(uint8_t* fragment, struct schc_device* device) {
uint8_t offset = device->profile->RULE_ID_SIZE;
if(device->profile->DTAG_SIZE) {
return (uint8_t) get_bits(fragment, offset, device->profile->DTAG_SIZE);
} else {
return SCHC_INIT;
}
}
/**
* get the Sender-Abort tile size
*
* @param conn a pointer to the connection
* @return Sender-Abort size
*
*/
static uint8_t get_sender_abort_ack_req_size(schc_fragmentation_t* conn) {
return (BITS_TO_BYTES(conn->device->profile->RULE_ID_SIZE + conn->device->profile->DTAG_SIZE +
conn->fragmentation_rule->WINDOW_SIZE + conn->fragmentation_rule->FCN_SIZE));
}
/**
* Returns the number of bits the current header exists off
*
* @param mbuf the mbuf to find the offset for
*
* @return length the length of the header
*
*/
static uint8_t get_fragmentation_header_length(schc_mbuf_t *mbuf, schc_fragmentation_t* conn) {
uint8_t offset = conn->device->profile->RULE_ID_SIZE + conn->device->profile->DTAG_SIZE
+ conn->fragmentation_rule->WINDOW_SIZE + conn->fragmentation_rule->FCN_SIZE;
uint8_t fcn = get_fcn_value(mbuf->ptr, conn);
if (fcn == get_max_fcn_value(conn)) {
offset += BYTES_TO_BITS(conn->fragmentation_rule->RCS_SIZE_BYTES);
}
return offset;
}
/**
* print the complete mbuf chain
*
* @param head the head of the list
*
*/
static void mbuf_print(schc_mbuf_t *head) {
uint8_t i = 0; uint8_t j;
schc_mbuf_t *curr = head;
while (curr != NULL) {
DEBUG_PRINTF("%d: %p\n", curr->frag_cnt, curr->ptr);
for (j = 0; j < curr->len; j++) {
DEBUG_PRINTF("0x%02X ", curr->ptr[j]);
}
DEBUG_PRINTF("\n");
curr = curr->next;
i++;
}
}
static schc_mbuf_t *mbuf_alloc(void)
{
#if !DYNAMIC_MEMORY
uint32_t i;
for(i = 0; i < SCHC_CONF_MBUF_POOL_LEN; i++) {
if(MBUF_POOL[i].len == 0 && MBUF_POOL[i].ptr == NULL) {
DEBUG_PRINTF("mbuf_alloc(): selected mbuf slot %d \n", (int) i);
return &MBUF_POOL[i];
}
}
return NULL;
#else
schc_mbuf_t *res = malloc(sizeof(schc_mbuf_t));
*res = (schc_mbuf_t){ .len = 0, .ptr = NULL };
return res;
#endif
}
/**
* add an item to the end of the mbuf list
* if head is NULL, the first item of the list
* will be set
*
* @param head the head of the list
* @param data a pointer to the data pointer
* @param len the length of the data
*
* @return -1 no free mbuf slot was found
* 0 ok
*/
static int8_t mbuf_push(schc_mbuf_t **head, uint8_t* data, uint16_t len) {
// scroll to next free mbuf slot
schc_mbuf_t *mbuf = mbuf_alloc();
if(mbuf == NULL) {
DEBUG_PRINTF("mbuf_push(): no free mbuf slots found \n");
return SCHC_FAILURE;
}
// check if this is a new connection
if(*head == NULL) {
*head = mbuf;
(*head)->len = len;
(*head)->ptr = (uint8_t*) (data);
(*head)->next = NULL;
return SCHC_SUCCESS;
}
mbuf->next = NULL;
mbuf->len = len;
mbuf->ptr = (uint8_t*) (data);
// find the last mbuf in the chain
schc_mbuf_t *curr = *head;
while (curr->next != NULL) {
curr = curr->next;
}
// set next in chain
curr->next = mbuf;
return SCHC_SUCCESS;
}
/**
* returns the last chain in the mbuf linked list
*
* @param head the head of the list
* @param mbuf the mbuf to find the previous mbuf for
*
* @return prev the previous mbuf
*/
static schc_mbuf_t* get_prev_mbuf(schc_mbuf_t *head, schc_mbuf_t *mbuf) {
schc_mbuf_t *curr = head;
while (curr->next != mbuf && curr->next != NULL) {
DEBUG_PRINTF(
"head is %p, looking for %p with curr %p, next is %p \n",
(void*) head, (void*) mbuf, (void*) curr, (void*) curr->next);
curr = curr->next;
}
return curr;
}
/**
* delete a mbuf from the chain
*
* @param head the head of the list
* @param mbuf the mbuf to delete
*
*/
static void mbuf_delete(schc_mbuf_t **head, schc_mbuf_t *mbuf) {
schc_mbuf_t *prev = NULL;
if (!(*head) || !mbuf) {
return;
}
if(mbuf->next != NULL) {
if(mbuf == *head) {
DEBUG_PRINTF("mbuf_delete(): set head \n");
(*head) = mbuf->next;
}
} else {
if(mbuf == *head) { // head is last fragment
DEBUG_PRINTF("mbuf_delete(): mbuf is head, delete head \n");
(*head) = NULL;
} else {
DEBUG_PRINTF("mbuf_delete(): chain next to prev \n");
prev = get_prev_mbuf(*head, mbuf);
prev->next = mbuf->next;
}
}
#if DYNAMIC_MEMORY
DEBUG_PRINTF("mbuf_delete(): free %p \n", (void *)mbuf);
free(mbuf->ptr);
free(mbuf);
#else
DEBUG_PRINTF("mbuf_delete(): clear slot %li in mbuf pool \n", mbuf - MBUF_POOL);
memset(mbuf->ptr, 0, mbuf->len);
mbuf->next = NULL;
mbuf->frag_cnt = 0;
mbuf->len = 0;
mbuf->ptr = NULL;
#endif
}
/**
* returns the total length of the mbuf without padding
*
* @param head the head of the list
*
* @return len the total length of the fragment
*/
uint16_t get_mbuf_len(schc_fragmentation_t *conn) {
schc_mbuf_t *curr = conn->head; uint32_t total_len = 0;
if (conn->bit_arr) {
/* we return a bit array without padding from the fragmenter */
conn->bit_arr->padding = 0;
}
if(conn->fragmentation_rule == NULL)
return curr->len;
if(conn->fragmentation_rule->mode == NOT_FRAGMENTED)
return curr->len;
while (curr != NULL) {
total_len += ((curr->len * 8) - get_fragmentation_header_length(curr, conn));
curr = curr->next;
}
return (uint16_t) ( (total_len) / 8 );
}
/**
* returns the last chain in the mbuf linked list
*
* @param head the head of the list
*
* @return tail the last mbuf in the linked list
*/
static schc_mbuf_t* get_mbuf_tail(schc_mbuf_t *head) {
schc_mbuf_t *curr = head;
if(head == NULL) {
return NULL;
}
while (curr->next != NULL) {
curr = curr->next;
}
return curr;
}
static uint8_t mbuf_get_byte(schc_mbuf_t *prev, schc_mbuf_t *curr, schc_fragmentation_t* conn, uint32_t* offset) {
uint32_t mbuf_bit_len = (curr->len * 8);
uint8_t byte_arr[1] = { 0 };
uint8_t start_offset = 0;
if(prev == NULL && (*offset) < get_fragmentation_header_length(curr, conn)) { // cope with fragmentation header from first packet
(*offset) = get_fragmentation_header_length(curr, conn);
}
int32_t remaining_bits = mbuf_bit_len - (*offset);
// DEBUG_PRINTF("total length %d, remaining bits %d, current offset %d: ", mbuf_bit_len, remaining_bits, *offset);
if (remaining_bits > 8) {
copy_bits(byte_arr, start_offset, curr->ptr, (*offset), (8 - start_offset));
*offset += (8 - start_offset);
} else if (curr->next != NULL) { // copy remainig bits from next mbuf and set offset accordingly
copy_bits(byte_arr, 0, curr->ptr, (*offset), remaining_bits);
copy_bits(byte_arr, remaining_bits, curr->next->ptr,
get_fragmentation_header_length(curr->next, conn),
(8 - remaining_bits));
*offset = (8 - remaining_bits) + get_fragmentation_header_length(curr->next, conn);
} else { // final byte
copy_bits(byte_arr, 0, curr->ptr, (*offset), remaining_bits);
*offset = remaining_bits;
}
// DEBUG_PRINTF("0x%02X \n", byte_arr[0]);
return byte_arr[0];
}
/**
* copy the byte alligned contents of the mbuf chain to
* the passed pointer
*
* @param head the head of the list
* @param ptr the pointer to copy the contents to
*/
void mbuf_copy(schc_fragmentation_t *conn, uint8_t* ptr) {
schc_mbuf_t *curr = conn->head;
schc_mbuf_t *prev = NULL;
uint8_t index = 0; uint32_t curr_bit_offset = 0;
if ( (!conn) || (!conn->fragmentation_rule) ||
(conn->fragmentation_rule->mode == NOT_FRAGMENTED) ) {
int i;
for (i = 0; i < curr->len; i++) {
ptr[i] = curr->ptr[i];
}
return;
}
while (curr != NULL) {
uint32_t temp_offset = curr_bit_offset;
ptr[index] = mbuf_get_byte(prev, curr, conn, &curr_bit_offset);
if (curr_bit_offset < temp_offset) { // partially included bits of next mbuf
prev = curr;
curr = curr->next;
}
index++;
}
}
/**
* delete all fragments chained in an mbuf
*
* @param head the head of the list
*/
void mbuf_clean(schc_mbuf_t **head) {
schc_mbuf_t *curr = *head;
schc_mbuf_t *temp = NULL;
while (curr != NULL) {
temp = curr->next;
mbuf_delete(head, curr);
curr = temp;
}
}
/**
* sort the complete mbuf chain based on fragment counter (fcn)
* note: some packets will arrive out of order, as they
* were part of a retransmission, and consequently
* arrive out of order, but carry the same fcn
*
* @param head double pointer to the head of the list
*
*/
static void mbuf_sort(schc_mbuf_t **head) {
schc_mbuf_t *hd = *head;
*head = NULL;
while (hd != NULL) {
schc_mbuf_t **curr = &hd;
schc_mbuf_t **next = &hd->next;
uint8_t swapped = 0;
while (*next != NULL) {
if ((*next)->frag_cnt < (*curr)->frag_cnt) { // swap pointers for curr and curr->next
schc_mbuf_t *temp = *curr;
*curr = *next;
*next = (schc_mbuf_t*) temp;
temp = (*curr)->next;
(*curr)->next = (*next)->next;
(*next)->next = (schc_mbuf_t*) temp;
curr = &(*curr)->next;
swapped = 1;
} else { // no swap. advance both pointer-pointers
curr = next;
next = &(*next)->next;
}
}
*next = *head;
if (swapped) {
*head = *curr;
*curr = NULL;
} else {
*head = hd;
break;
}
}
}
/**
* Calculates the Message Integrity Check (MIC) over an unformatted mbuf chain
* without formatting the mbuf chain, as the last window might contain corrupted fragments
*
* this is the 8- 16- or 32- bit Cyclic Redundancy Check (CRC)
*
* @param head the head of the list
*
* @return checksum the computed checksum
*
*/
static unsigned int mbuf_compute_rcs(schc_fragmentation_t *conn) {
schc_mbuf_t *curr = conn->head;
schc_mbuf_t *prev = NULL;
uint32_t crc, crc_mask; int8_t k = 0;
uint8_t byte = 0; uint32_t curr_bit_offset = 0;
crc = 0xFFFFFFFF;
while (curr != NULL) {
uint32_t temp_offset = curr_bit_offset;
byte = mbuf_get_byte(prev, curr, conn, &curr_bit_offset);
/*if (curr->next->next == NULL
&& curr_bit_offset >= ((curr->next->len * 8) - 8)) { // last byte always contains payload + padding
// rare case where
curr = curr->next->next;
}*/
// todo
// when mtu is very small (e.g. 6 or 7), final mbuf > second last mbuf
// and curr_bit_offset will never be larger than temp_offset
// resulting in an endless loop
if ( (curr_bit_offset < temp_offset) ) { // partially included bits of next mbuf
prev = curr;
curr = curr->next;
}
crc = crc ^ byte;
for (k = 7; k >= 0; k--) { // do eight times.
crc_mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & crc_mask);
}
// printf("0x%02X ", byte);
}
// printf("\n");
crc = ~crc;
uint8_t mic[MAX_RCS_SIZE_BYTES] = { ((crc & 0xFF000000) >> 24),
((crc & 0xFF0000) >> 16), ((crc & 0xFF00) >> 8), ((crc & 0xFF)) };
memcpy((uint8_t *) conn->rcs, mic, conn->fragmentation_rule->RCS_SIZE_BYTES);
DEBUG_PRINTF("mbuf_compute_rcs(): RCS is %02X%02X%02X%02X \n", mic[0], mic[1], mic[2],
mic[3]);
return crc;
}
////////////////////////////////////////////////////////////////////////////////////
// LOCAL FUNCIONS //
////////////////////////////////////////////////////////////////////////////////////
static uint8_t calculate_byte_padding(uint32_t total_bits) {
return (8U - (total_bits % 8U)) % 8U;
}
/**
* Calculates the Message Integrity Check (MIC)
* which is the 8- 16- or 32- bit Cyclic Redundancy Check (CRC)
*
* @param conn pointer to the connection
*
* @return checksum the computed checksum
*
*/
static unsigned int compute_rcs(schc_fragmentation_t *conn, uint8_t last_tile_padding) {
int i, j; uint8_t byte;
unsigned int crc, mask;
i = 0;
crc = 0xFFFFFFFF;
// the MIC is computed over the complete, compressed packet
// + padding of the last tile, which may result in a non-byte aligned packet
// so, extra padding might be added before computing the MIC
uint8_t extra_padding = calculate_byte_padding((conn->bit_arr->len * 8) + last_tile_padding);
DEBUG_PRINTF(
"compute_rcs(): original packet length %d bits, last tile padding %d bits, extra padding %d bits \n",
(int) conn->bit_arr->len * 8, last_tile_padding, extra_padding);
uint16_t padded_length = (((conn->bit_arr->len * 8) + last_tile_padding + extra_padding) / 8);
while (i < padded_length) {
if (i < conn->bit_arr->len) {
byte = conn->bit_arr->ptr[i];
}
else {
byte = 0U;
}
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i++;
// printf("0x%02X ", byte);
}
// printf("\n");
crc = ~crc;
uint8_t mic[MAX_RCS_SIZE_BYTES] = { ((crc & 0xFF000000) >> 24), ((crc & 0xFF0000) >> 16),
((crc & 0xFF00) >> 8), ((crc & 0xFF)) };
memcpy((uint8_t *) conn->rcs, mic, conn->fragmentation_rule->RCS_SIZE_BYTES);
DEBUG_PRINTF("compute_rcs(): RCS for device %d is %02X%02X%02X%02X \n",
(int) conn->device->device_id, mic[0], mic[1], mic[2], mic[3]);
return crc;
}
/**
* get the window bit
*
* @param fragment a pointer to the fragment to retrieve the window number from
*
* @return window the window number as indicated by the fragment
*
*/
static uint8_t get_window_bit(uint8_t* fragment, schc_fragmentation_t* conn) {
uint8_t offset = conn->device->profile->RULE_ID_SIZE + conn->device->profile->DTAG_SIZE;
return (uint8_t) get_bits(fragment, offset, conn->fragmentation_rule->WINDOW_SIZE);
}
/**
* get the MIC value
*
* @param fragment a pointer to the fragment to retrieve the MIC from
* @param mic
*
*/
static void get_received_rcs(uint8_t* fragment, uint8_t mic[],
schc_fragmentation_t* conn) {
uint8_t offset = conn->device->profile->RULE_ID_SIZE + conn->device->profile->DTAG_SIZE
+ conn->fragmentation_rule->WINDOW_SIZE + conn->fragmentation_rule->FCN_SIZE;
copy_bits(mic, 0, fragment, offset, BYTES_TO_BITS(conn->fragmentation_rule->RCS_SIZE_BYTES));
}
/**
* set the fragmentation counter of the current connection
* which is the inverse of the fcn value
*
* @param conn a pointer to the connection
* @param fcn the fcn value
*
*/
static uint8_t get_frag_cnt(schc_fragmentation_t* conn, uint8_t fcn, uint8_t window) {
uint8_t value = (get_max_fcn_value(conn) * (window + 1)) - fcn;
if(fcn == get_max_fcn_value(conn)) {
value = (window + 1) * get_max_fcn_value(conn);
}
return value;
}
/*
* Find a rule with the correct reliability mode
*
* @param mode the mode for which a rule should be found
* @param device_id the device to find a rule for
*
* @return fragmentation_rule the rule that was found
* NULL if no rule was found
*
*/
struct schc_fragmentation_rule_t* get_fragmentation_rule_by_reliability_mode(reliability_mode mode,
uint32_t device_id) {
struct schc_device *device = get_device_by_id(device_id);
if (device == NULL) {
DEBUG_PRINTF(
"get_schc_rule(): no device was found for the id: %d\n", (int) device_id);
return NULL;
}
int i;
for (i = 0; i < device->fragmentation_rule_count; i++) {
const struct schc_fragmentation_rule_t* curr_rule = (*device->fragmentation_context)[i];
if (curr_rule->mode == mode) {
return (struct schc_fragmentation_rule_t*) (curr_rule);
}
}
DEBUG_PRINTF("get_schc_rule(): no fragmentation rule was found for device with id=%d\n",
(int ) device_id);
return NULL;
}
// todo move to schc.c generic function for both this method and get_compression_rule_by_rule_id
/*
* Find a SCHC rule entry for a device
*
* @param rule_arr the rule id in uint8_t array
* @param device_id the device to find a rule for
*
* @return schc_rule the rule that was found
* NULL if no rule was found
*
*/
static struct schc_fragmentation_rule_t* get_fragmentation_rule_by_rule_id(uint8_t* rule_arr, struct schc_device *device) {
int i;
for (i = 0; i < device->fragmentation_rule_count; i++) {
struct schc_fragmentation_rule_t* curr_rule = (struct schc_fragmentation_rule_t*) (*device->fragmentation_context)[i];
uint8_t curr_rule_pos = get_position_in_first_byte(device->profile->RULE_ID_SIZE);
uint8_t rule_id[4] = { 0 };
little_end_uint8_from_uint32(rule_id, curr_rule->rule_id); /* copy the uint32_t to a uint8_t array */
if( compare_bits_aligned(rule_id, curr_rule_pos, rule_arr, 0, device->profile->RULE_ID_SIZE)) {
DEBUG_PRINTF("get_fragmentation_rule(): curr rule %p \n", (void*) curr_rule);
return curr_rule;
}
}
return NULL;
}
static int16_t get_next_available_dtag(schc_fragmentation_t* conn) {
int16_t count = 0;
#if DYNAMIC_MEMORY
schc_fragmentation_t *ptr = schc_tx_conns;
while(ptr) {
if(ptr != conn && ptr->fragmentation_rule) {
if ( (ptr->fragmentation_rule->rule_id == conn->fragmentation_rule->rule_id) && (ptr->TX_STATE != INIT_TX)) {
/* rule in use by another active connection */
count++;
}
}
ptr = ptr->next;
}
#else
uint32_t i;
for(i = 0; i < SCHC_CONF_TX_CONNS; i++) {
if(&schc_tx_conns[i] != conn && schc_tx_conns[i].fragmentation_rule) { /* not all connections are initialized yet */
if( (schc_tx_conns[i].fragmentation_rule->rule_id == conn->fragmentation_rule->rule_id) && (schc_tx_conns[i].TX_STATE != INIT_TX)) {
/* rule in use by another active connection */
count++;
}
}
}
#endif
if(conn->device->profile->DTAG_SIZE > 0) {
if(get_required_number_of_bits(count) <= conn->device->profile->DTAG_SIZE) { /* check if dtag value fits the available space */
DEBUG_PRINTF("get_next_available_dtag(): tx connection=%p, dtag=%d\n", conn, count);
return count;
} else {
return SCHC_FAILURE;
}
} else {
if(count > 0) {
return SCHC_INIT;
} else {
return 0;
}
}
}
/**
* initializes a new tx transmission for a device:
* set the starting and ending point of the packet
* calculate the MIC over the complete SCHC packet
*
* @param conn a pointer to the connection to initialize
*
* @return 1 on success
* 0 on error
* -1 if no fragmentation is needed
*
*/
static int8_t init_tx_connection(schc_fragmentation_t* conn) {
if (!conn->bit_arr->ptr) {
DEBUG_PRINTF(
"init_connection(): no pointer to compressed packet given \n");
return 0;
}
if (!conn->bit_arr->len) {
DEBUG_PRINTF("init_connection(): packet_length not specified \n");
return 0;
}
if (conn->send == NULL) {
DEBUG_PRINTF("init_connection(): no send function specified \n");
return 0;
}
if (conn->post_timer_task == NULL) {
DEBUG_PRINTF("init_connection(): no timer function specified \n");
return 0;
}
if(conn->fragmentation_rule == NULL) {
DEBUG_PRINTF("init_connection(): SCHC fragmentation rule not specified \n");
return 0;
}
if((conn->tile_size * 8) < (conn->device->profile->RULE_ID_SIZE + conn->device->profile->DTAG_SIZE + conn->fragmentation_rule->WINDOW_SIZE
+ conn->fragmentation_rule->FCN_SIZE + (conn->fragmentation_rule->RCS_SIZE_BYTES * 8)) ) {
DEBUG_PRINTF(
"init_connection(): conn->tile_size should be larger than last tile's header length \n");
return 0;
}
conn->tail_ptr = (uint8_t*) (conn->bit_arr->ptr + conn->bit_arr->len); // set end of packet
conn->window = 0;
conn->frag_cnt = 0;
conn->attempts = 0;
conn->sync = 0;
conn->all1_window = 0;
conn->total_transmissions = 0;
if (conn->bit_arr->len < conn->tile_size
&& conn->fragmentation_rule->mode != NOT_FRAGMENTED) { // should not fragment; change rule
DEBUG_PRINTF(
"init_connection(): changing rule to NOT FRAGMENTED mode \n");
conn->fragmentation_rule = get_fragmentation_rule_by_reliability_mode(
NOT_FRAGMENTED, conn->device->device_id);
if (conn->fragmentation_rule == NULL) {
DEBUG_PRINTF(
"init_connection(): no matching rule found for mode specified");
return 0;
}
}
if(!conn->fragmentation_rule->inactivity_timer_ms) {
DEBUG_PRINTF("init_connection(): inactivity timer should be set in fragmentation rule \n");
return 0;
}
if(!conn->fragmentation_rule->retransmission_timer_ms) {
DEBUG_PRINTF("init_connection(): retransmission timer should be set in fragmentation rule\n");
return 0;
}
uint8_t fcn[1] = { 0 };
uint8_t offset = 8 - (conn->fragmentation_rule->FCN_SIZE % 8);
set_bits(fcn, offset, conn->fragmentation_rule->FCN_SIZE);
if( (fcn[0] - 1) != conn->fragmentation_rule->MAX_WND_FCN) {
DEBUG_PRINTF("init_connection(): MAX_WND_FCN should be set according to the window size and FCN size\n");
return 0;
}
if(conn->fragmentation_rule->mode == ACK_ON_ERROR) {
conn->tile_size = conn->fragmentation_rule->tile_size;
DEBUG_PRINTF("init_connection(): tile size=%d \n", conn->tile_size);
}
if(conn->tile_size > MAX_MTU_LENGTH || conn->tile_size <= 0) {
DEBUG_PRINTF("init_connection(): tile size (%d) should be set between 0 and the maximum allowed MTU (%d)\n", conn->tile_size, MAX_MTU_LENGTH);
return 0;
}
uint32_rule_id_to_uint8_buf(conn->fragmentation_rule->rule_id,
conn->rule_id, conn->device->profile->RULE_ID_SIZE);
if(conn->fragmentation_rule->mode != NOT_FRAGMENTED) {
if(conn->fragmentation_rule->MAX_WND_FCN >= get_max_fcn_value(conn)) {
DEBUG_PRINTF("init_connection(): MAX_WIND_FCN must be smaller than all-1 \n");
return 0;
}
}
if (get_number_of_bytes_from_bits(conn->fragmentation_rule->MAX_WND_FCN) > BITMAP_SIZE_BYTES) {
DEBUG_PRINTF(
"init_connection(): BITMAP_SIZE_BYTES must support MAX_WND_FCN (%d bytes required!) \n", get_number_of_bytes_from_bits(conn->fragmentation_rule->MAX_WND_FCN));
return 0;
}
if(conn->fragmentation_rule->MAX_WND_FCN > MAX_WINDOW_SIZE) {
DEBUG_PRINTF(
"init_connection(): MAX_WINDOW_SIZE must match MAX_WND_FCN \n");
return 0;
}
memset(conn->window_tiles, 0, sizeof(conn->window_tiles));
conn->fcn = conn->fragmentation_rule->MAX_WND_FCN;
memset(conn->bitmap[conn->window], 0, BITMAP_SIZE_BYTES); // clear bitmap
/* check the list of tx connections in order to set an appropriate dtag value */
int16_t dtag = get_next_available_dtag(conn);
if(dtag == SCHC_FAILURE) {
DEBUG_PRINTF("init_connection(): no more free dtag values available\n");
return 0;
} else {
conn->dtag = dtag;
}
if(conn->fragmentation_rule->mode == NOT_FRAGMENTED) {
return SCHC_NO_FRAGMENTATION;
} else {
return 1;
}
}
/**
* reset a connection
*
* @param conn a pointer to the connection to reset
*
*/
void schc_reset(schc_fragmentation_t* conn) {
/* reset connection variables */
if (conn->remove_timer_entry) {
conn->remove_timer_entry(conn);
}
#if DYNAMIC_MEMORY
conn->next = NULL;
#endif
conn->device = NULL;
conn->tail_ptr = 0;
conn->dc = 0;
conn->fcn = 0;
conn->dtag = -1;
conn->frag_cnt = 0;
conn->fragmentation_rule = NULL;
memset(conn->bitmap, 0, BITMAP_SIZE_BYTES * MAX_WINDOWS);
conn->attempts = 0;
conn->sync = 0;
conn->TX_STATE = INIT_TX;
conn->RX_STATE = RECV_WINDOW;
conn->window = 0;
conn->timer_flag = 0;
conn->input = 0;
memset(conn->rcs, 0, MAX_RCS_SIZE_BYTES);
/* reset ack structure */
memset(conn->ack.rule_id, 0, 4); /* rule id can be maximum of 4 bytes */
memset(conn->ack.bitmap, 0, BITMAP_SIZE_BYTES);
memset(conn->ack.window, 0, 1);
memset(conn->ack.dtag, 0, 1);
conn->ack.mic = 0;
conn->ack.fcn = 0;
conn->all1_window = 0;
conn->total_transmissions = 0;
memset(conn->window_tiles, 0, sizeof(conn->window_tiles));
if(conn->head != NULL ){
mbuf_clean(&conn->head);
}
conn->head = NULL;
}
/**
* check if a connection has more fragments to deliver
*
* @param conn a pointer to the connection
*
* @return 0 the connection still has fragments to send
* total_bits the total number of packet bits already transmitted
*
*/
static uint32_t has_no_more_fragments(schc_fragmentation_t* conn) {
// uint32_t total_bits_to_transmit = BYTES_TO_BITS(conn->bit_arr->len); /* effective payload bits */
// uint16_t header_size = (conn->device->profile->RULE_ID_SIZE + conn->device->profile->DTAG_SIZE
// + conn->fragmentation_rule->WINDOW_SIZE + conn->fragmentation_rule->FCN_SIZE);
// uint16_t prev_header_bits = header_size * (conn->frag_cnt - 1); /* previous fragmentation overhead */
// uint32_t total_mtu_bits = BYTES_TO_BITS(conn->tile_size); // (header + packet) bits already transfered
// for(int i = 0; i < conn->frag_cnt - 1; i++) {
// total_mtu_bits += BYTES_TO_BITS(conn->window_tiles[i]);
// }
// if ( (total_mtu_bits - prev_header_bits) > total_bits_to_transmit) { // last fragment
// uint16_t mic_included_bits = (total_bits_to_transmit + prev_header_bits)
// + (header_size + BYTES_TO_BITS(conn->fragmentation_rule->RCS_SIZE_BYTES));
// if (mic_included_bits <= total_mtu_bits) { // return the number of bits transmitted,
// // return the total number of bits transmitted if the RCS does not create an extra fragment
// uint32_t already_transmitted = total_mtu_bits - BYTES_TO_BITS(conn->tile_size) - prev_header_bits;
// return already_transmitted;
// }
// }
uint32_t total_bits_to_transmit = (conn->bit_arr->len * 8); // effective payload bits
uint16_t header_size = (conn->device->profile->RULE_ID_SIZE + conn->device->profile->DTAG_SIZE + conn->fragmentation_rule->WINDOW_SIZE + conn->fragmentation_rule->FCN_SIZE);
uint16_t prev_header_bits = header_size * (conn->frag_cnt - 1); // previous fragmentation overhead
uint32_t total_mtu_bits = BYTES_TO_BITS(conn->tile_size) * (conn->frag_cnt); // (header + packet) bits already transfered
if ((total_bits_to_transmit + prev_header_bits) < total_mtu_bits) { // last fragment
uint16_t mic_included_bits = (total_bits_to_transmit + prev_header_bits) + (header_size + BYTES_TO_BITS(conn->fragmentation_rule->RCS_SIZE_BYTES));
if (mic_included_bits <= total_mtu_bits) { // return the number of bits transmitted,
// if the RCS does not create an extra fragment
uint32_t already_transmitted = ((BYTES_TO_BITS(conn->tile_size) * (conn->frag_cnt - 1)) - prev_header_bits);
return already_transmitted;
}
}
return 0;
}
static uint8_t set_bare_fragmentation_header(schc_fragmentation_t* conn, uint8_t window, uint8_t* fragmentation_buffer) {
uint8_t bit_offset = conn->device->profile->RULE_ID_SIZE;
// set rule id
uint8_t src_pos = get_position_in_first_byte(conn->device->profile->RULE_ID_SIZE);
uint8_t fragmenter_id[4] = { 0 };
little_end_uint8_from_uint32(fragmenter_id, conn->fragmentation_rule->rule_id); /* copy the uint32_t to a uint8_t array */
copy_bits(fragmentation_buffer, 0, fragmenter_id, src_pos, bit_offset);
// set dtag field
uint8_t dtag[1] = { conn->dtag << (8 - conn->device->profile->DTAG_SIZE) };
copy_bits(fragmentation_buffer, bit_offset, dtag, 0, conn->device->profile->DTAG_SIZE); // right after rule id
bit_offset += conn->device->profile->DTAG_SIZE;
// set window bit
uint8_t w[1] = { window << (8 - conn->fragmentation_rule->WINDOW_SIZE) };
copy_bits(fragmentation_buffer, bit_offset, w, 0, conn->fragmentation_rule->WINDOW_SIZE); // right after dtag
bit_offset += conn->fragmentation_rule->WINDOW_SIZE;
// set fcn value
uint8_t fcn[1] = { conn->fcn << (8 - conn->fragmentation_rule->FCN_SIZE) };
copy_bits(fragmentation_buffer, bit_offset, fcn, 0, conn->fragmentation_rule->FCN_SIZE); // right after window bits
bit_offset += conn->fragmentation_rule->FCN_SIZE;
return bit_offset;
}
/**
* set the fragmentation header and compute RCS if this is the final fragment
*
* @param conn a pointer to the connection
* @param buffer a pointer to the buffer to set the header
*
* @return bit_offset the number of bits added to the front of the fragment
*
*/
static uint16_t set_complete_fragmentation_header(schc_fragmentation_t* conn, uint8_t window,
uint8_t* fragmentation_buffer) {
uint8_t bit_offset = set_bare_fragmentation_header(conn, window, fragmentation_buffer);