forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.c
1525 lines (1451 loc) · 45.6 KB
/
tcp.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include "platform.h"
#include "util.h"
#include "net.h"
#include "ip.h"
#include "ip6.h"
#include "tcp.h"
#include "sock.h"
struct tcp_pcb {
int state;
int mode; /* user command mode */
struct ip_endpoint local;
struct ip_endpoint foreign;
struct {
uint32_t nxt;
uint32_t una;
uint16_t wnd;
uint16_t up;
uint32_t wl1;
uint32_t wl2;
} snd;
uint32_t iss;
struct {
uint32_t nxt;
uint16_t wnd;
uint16_t up;
} rcv;
uint32_t irs;
uint16_t mtu;
uint16_t mss;
uint8_t buf[65535]; /* receive buffer */
struct sched_ctx ctx;
struct queue_head queue; /* retransmit queue */
struct timeval tw_timer;
struct tcp_pcb *parent;
struct queue_head backlog;
};
static mutex_t mutex = MUTEX_INITIALIZER;
static struct tcp_pcb pcbs[TCP_PCB_SIZE];
static ssize_t
tcp_output_segment(uint32_t seq, uint32_t ack, uint8_t flg, uint16_t wnd, uint8_t *data, size_t len, struct ip_endpoint *local, struct ip_endpoint *foreign);
char *
tcp_flg_ntoa(uint8_t flg)
{
static char str[9];
snprintf(str, sizeof(str), "--%c%c%c%c%c%c",
TCP_FLG_ISSET(flg, TCP_FLG_URG) ? 'U' : '-',
TCP_FLG_ISSET(flg, TCP_FLG_ACK) ? 'A' : '-',
TCP_FLG_ISSET(flg, TCP_FLG_PSH) ? 'P' : '-',
TCP_FLG_ISSET(flg, TCP_FLG_RST) ? 'R' : '-',
TCP_FLG_ISSET(flg, TCP_FLG_SYN) ? 'S' : '-',
TCP_FLG_ISSET(flg, TCP_FLG_FIN) ? 'F' : '-');
return str;
}
void
tcp_dump(const uint8_t *data, size_t len)
{
struct tcp_hdr *hdr;
flockfile(stderr);
hdr = (struct tcp_hdr *)data;
fprintf(stderr, " src: %u\n", ntoh16(hdr->src));
fprintf(stderr, " dst: %u\n", ntoh16(hdr->dst));
fprintf(stderr, " seq: %u\n", ntoh32(hdr->seq));
fprintf(stderr, " ack: %u\n", ntoh32(hdr->ack));
fprintf(stderr, " off: 0x%02x (%d)\n", hdr->off, (hdr->off >> 4) << 2);
fprintf(stderr, " flg: 0x%02x (%s)\n", hdr->flg, tcp_flg_ntoa(hdr->flg));
fprintf(stderr, " wnd: %u\n", ntoh16(hdr->wnd));
fprintf(stderr, " sum: 0x%04x\n", ntoh16(hdr->sum));
fprintf(stderr, " up: %u\n", ntoh16(hdr->up));
#ifdef HEXDUMP
hexdump(stderr, data, len);
#endif
funlockfile(stderr);
}
/*
* TCP Protocol Control Block (PCB)
*
* NOTE: TCP PCB functions must be called after mutex locked
*/
static struct tcp_pcb *
tcp_pcb_alloc(void)
{
struct tcp_pcb *pcb;
for (pcb = pcbs; pcb < tailof(pcbs); pcb++) {
if (pcb->state == TCP_PCB_STATE_FREE) {
pcb->state = TCP_PCB_STATE_CLOSED;
sched_ctx_init(&pcb->ctx);
return pcb;
}
}
return NULL;
}
static void
tcp_pcb_release(struct tcp_pcb *pcb)
{
struct queue_entry *entry;
struct tcp_pcb *est;
char ep1[IPV6_ENDPOINT_STR_LEN];
char ep2[IPV6_ENDPOINT_STR_LEN];
if (sched_ctx_destroy(&pcb->ctx) == -1) {
sched_wakeup(&pcb->ctx);
return;
}
while ((entry = queue_pop(&pcb->queue)) != NULL) {
memory_free(entry);
}
while ((est = queue_pop(&pcb->backlog)) != NULL) {
tcp_pcb_release(est);
}
debugf("released, local=%s, foreign=%s",
ip_endpoint_ntop(&pcb->local, ep1, sizeof(ep1)), ip_endpoint_ntop(&pcb->foreign, ep2, sizeof(ep2))); // TODO: foreignにfamily情報が格納されていない
memset(pcb, 0, sizeof(*pcb));
}
static struct tcp_pcb *
tcp_pcb_select(struct ip_endpoint *local, struct ip_endpoint *foreign)
{
struct tcp_pcb *pcb, *listen_pcb = NULL;
for (pcb = pcbs; pcb < tailof(pcbs); pcb++) {
switch (local->addr.family) {
case AF_INET:
if ((pcb->local.addr.s_addr4 == IP_ADDR_ANY || pcb->local.addr.s_addr4 == local->addr.s_addr4) && pcb->local.port == local->port) {
if (!foreign) {
return pcb;
}
if (pcb->foreign.addr.s_addr4 == foreign->addr.s_addr4 && pcb->foreign.port == foreign->port) {
return pcb;
}
if (pcb->state == TCP_PCB_STATE_LISTEN) {
if (pcb->foreign.addr.s_addr4 == IP_ADDR_ANY && pcb->foreign.port == 0) {
/* LISTENed with wildcard foreign address/port */
listen_pcb = pcb;
}
}
}
break;
case AF_INET6:
if ((IPV6_ADDR_EQUAL(&pcb->local.addr.s_addr6, &IPV6_UNSPECIFIED_ADDR) || IPV6_ADDR_EQUAL(&pcb->local.addr.s_addr6, &local->addr.s_addr6))
&& pcb->local.port == local->port) {
if (!foreign) {
return pcb;
}
if (IPV6_ADDR_EQUAL(&pcb->foreign.addr.s_addr6, &foreign->addr.s_addr6) && pcb->foreign.port == foreign->port) {
return pcb;
}
if (pcb->state == TCP_PCB_STATE_LISTEN) {
if (IPV6_ADDR_EQUAL(&pcb->foreign.addr.s_addr6, &IPV6_UNSPECIFIED_ADDR) && pcb->foreign.port == 0) {
/* LISTENed with wildcard foreign address/port */
listen_pcb = pcb;
}
}
}
break;
default:
break;
}
}
return listen_pcb;
}
static struct tcp_pcb *
tcp_pcb_get(int id)
{
struct tcp_pcb *pcb;
if (id < 0 || id >= (int)countof(pcbs)) {
/* out of range */
return NULL;
}
pcb = &pcbs[id];
if (pcb->state == TCP_PCB_STATE_FREE) {
return NULL;
}
return pcb;
}
static int
tcp_pcb_id(struct tcp_pcb *pcb)
{
return indexof(pcbs, pcb);
}
/*
* TCP Retransmit
*
* NOTE: TCP Retransmit functions must be called after mutex locked
*/
static int
tcp_retransmit_queue_add(struct tcp_pcb *pcb, uint32_t seq, uint8_t flg, uint8_t *data, size_t len)
{
struct tcp_queue_entry *entry;
entry = memory_alloc(sizeof(*entry) + len);
if (!entry) {
errorf("memory_alloc() failure");
return -1;
}
entry->rto = TCP_DEFAULT_RTO;
entry->seq = seq;
entry->flg = flg;
entry->len = len;
memcpy(entry + 1, data, entry->len);
gettimeofday(&entry->first, NULL);
entry->last = entry->first;
if (!queue_push(&pcb->queue, entry)) {
errorf("queue_push() failure");
memory_free(entry);
return -1;
}
return 0;
}
static void
tcp_retransmit_queue_cleanup(struct tcp_pcb *pcb)
{
struct tcp_queue_entry *entry;
while ((entry = queue_peek(&pcb->queue))) {
if (entry->seq >= pcb->snd.una) {
break;
}
entry = queue_pop(&pcb->queue);
debugf("remove, seq=%u, flags=%s, len=%zu", entry->seq, tcp_flg_ntoa(entry->flg), entry->len);
memory_free(entry);
}
return;
}
static void
tcp_retransmit_queue_emit(void *arg, void *data)
{
struct tcp_pcb *pcb;
struct tcp_queue_entry *entry;
struct timeval now, diff, timeout;
pcb = (struct tcp_pcb *)arg;
entry = (struct tcp_queue_entry *)data;
gettimeofday(&now, NULL);
timersub(&now, &entry->first, &diff);
if (diff.tv_sec >= TCP_RETRANSMIT_DEADLINE) {
pcb->state = TCP_PCB_STATE_CLOSED;
sched_wakeup(&pcb->ctx);
return;
}
timeout = entry->last;
timeval_add_usec(&timeout, entry->rto);
if (timercmp(&now, &timeout, >)) {
tcp_output_segment(entry->seq, pcb->rcv.nxt, entry->flg, pcb->rcv.wnd, (uint8_t *)(entry+1), entry->len, &pcb->local, &pcb->foreign);
entry->last = now;
entry->rto *= 2;
}
}
static void
tcp_set_timewait_timer(struct tcp_pcb *pcb)
{
gettimeofday(&pcb->tw_timer, NULL);
pcb->tw_timer.tv_sec += TCP_TIMEWAIT_SEC;
debugf("start time_wait timer: %d seconds", TCP_TIMEWAIT_SEC);
}
static ssize_t
tcp_output_segment(uint32_t seq, uint32_t ack, uint8_t flg, uint16_t wnd, uint8_t *data, size_t len, struct ip_endpoint *local, struct ip_endpoint *foreign)
{
uint8_t buf1[IP_PAYLOAD_SIZE_MAX] = {};
uint8_t buf2[IPV6_PAYLOAD_SIZE_MAX] = {};
struct tcp_hdr *hdr;
struct ip_pseudo_hdr pseudo;
struct ip6_pseudo_hdr pseudo6;
uint16_t psum;
uint16_t total;
char ep1[IP_ENDPOINT_STR_LEN];
char ep2[IP_ENDPOINT_STR_LEN];
char ep3[IPV6_ENDPOINT_STR_LEN];
char ep4[IPV6_ENDPOINT_STR_LEN];
switch (foreign->addr.family) {
case AF_INET:
hdr = (struct tcp_hdr *)buf1;
hdr->src = local->port;
hdr->dst = foreign->port;
hdr->seq = hton32(seq);
hdr->ack = hton32(ack);
hdr->off = (sizeof(*hdr) >> 2) << 4;
hdr->flg = flg;
hdr->wnd = hton16(wnd);
hdr->sum = 0;
hdr->up = 0;
memcpy(hdr + 1, data, len);
pseudo.src = local->addr.s_addr4;
pseudo.dst = foreign->addr.s_addr4;
pseudo.zero = 0;
pseudo.protocol = PROTOCOL_TCP;
total = sizeof(*hdr) + len;
pseudo.len = hton16(total);
psum = ~cksum16((uint16_t *)&pseudo, sizeof(pseudo), 0);
hdr->sum = cksum16((uint16_t *)hdr, total, psum);
debugf("%s => %s, len=%u (payload=%zu)",
ip_endpoint_ntop(local, ep1, sizeof(ep1)), ip_endpoint_ntop(foreign, ep2, sizeof(ep2)), total, len);
tcp_dump((uint8_t *)hdr, total);
if (ip_output(PROTOCOL_TCP, (uint8_t *)hdr, total, local->addr.s_addr4, foreign->addr.s_addr4) == -1) {
return -1;
}
return len;
case AF_INET6:
hdr = (struct tcp_hdr *)buf2;
hdr->src = local->port;
hdr->dst = foreign->port;
hdr->seq = hton32(seq);
hdr->ack = hton32(ack);
hdr->off = (sizeof(*hdr) >> 2) << 4;
hdr->flg = flg;
hdr->wnd = hton16(wnd);
hdr->sum = 0;
hdr->up = 0;
memcpy(hdr + 1, data, len);
IPV6_ADDR_COPY(&pseudo6.src, &local->addr.s_addr6, IPV6_ADDR_LEN);
IPV6_ADDR_COPY(&pseudo6.dst, &foreign->addr.s_addr6, IPV6_ADDR_LEN);
pseudo6.zero[0] = pseudo6.zero[1] = pseudo6.zero[2] = 0;
pseudo6.nxt = PROTOCOL_TCP;
total = sizeof(*hdr) + len;
pseudo6.len = hton16(total);
psum = ~cksum16((uint16_t *)&pseudo6, sizeof(pseudo6), 0);
hdr->sum = cksum16((uint16_t *)hdr, total, psum);
debugf("%s => %s, len=%u (payload=%zu)",
ip_endpoint_ntop(local, ep3, sizeof(ep3)), ip_endpoint_ntop(foreign, ep4, sizeof(ep4)), total, len);
tcp_dump((uint8_t *)hdr, total);
if (ip6_output(PROTOCOL_TCP, (uint8_t *)hdr, total, local->addr.s_addr6, foreign->addr.s_addr6) == -1) {
return -1;
}
return len;
default:
errorf("not supported address family");
break;
}
return -1;
}
static ssize_t
tcp_output(struct tcp_pcb *pcb, uint8_t flg, uint8_t *data, size_t len)
{
uint32_t seq;
seq = pcb->snd.nxt;
if (TCP_FLG_ISSET(flg, TCP_FLG_SYN)) {
seq = pcb->iss;
}
if (TCP_FLG_ISSET(flg, TCP_FLG_SYN | TCP_FLG_FIN) || len) {
tcp_retransmit_queue_add(pcb, seq, flg, data, len);
}
return tcp_output_segment(seq, pcb->rcv.nxt, flg, pcb->rcv.wnd, data, len, &pcb->local, &pcb->foreign);
}
/* rfc793 - section 3.9 [Event Processing > SEGMENT ARRIVES] */
static void
tcp_segment_arrives(struct tcp_segment_info *seg, uint8_t flags, uint8_t *data, size_t len, struct ip_endpoint *local, struct ip_endpoint *foreign)
{
struct tcp_pcb *pcb, *new_pcb;
int acceptable = 0;
pcb = tcp_pcb_select(local, foreign);
if (!pcb || pcb->state == TCP_PCB_STATE_CLOSED) {
if (TCP_FLG_ISSET(flags, TCP_FLG_RST)) {
return;
}
if (!TCP_FLG_ISSET(flags, TCP_FLG_ACK)) {
tcp_output_segment(0, seg->seq + seg->len, TCP_FLG_RST | TCP_FLG_ACK, 0, NULL, 0, local, foreign);
} else {
tcp_output_segment(seg->ack, 0, TCP_FLG_RST, 0, NULL, 0, local, foreign);
}
return;
}
switch(pcb->state) {
case TCP_PCB_STATE_LISTEN:
/*
* first check for an RST
*/
if (TCP_FLG_ISSET(flags, TCP_FLG_RST)) {
return;
}
/*
* second check for an ACK
*/
if (TCP_FLG_ISSET(flags, TCP_FLG_ACK)) {
tcp_output_segment(seg->ack, 0, TCP_FLG_RST, 0, NULL, 0, local, foreign);
return;
}
/*
* third check for an SYN
*/
if (TCP_FLG_ISSET(flags, TCP_FLG_SYN)) {
/* ignore: security/compartment check */
/* ignore: precedence check */
if (pcb->mode == TCP_PCB_MODE_SOCKET) {
new_pcb = tcp_pcb_alloc();
if (!new_pcb) {
errorf("tcp_pcb_alloc() failure");
return;
}
new_pcb->mode = TCP_PCB_MODE_SOCKET;
new_pcb->parent = pcb;
pcb = new_pcb;
}
pcb->local = *local;
pcb->foreign = *foreign;
pcb->rcv.wnd = sizeof(pcb->buf);
pcb->rcv.nxt = seg->seq + 1;
pcb->irs = seg->seq;
pcb->iss = random();
tcp_output(pcb, TCP_FLG_SYN | TCP_FLG_ACK, NULL, 0);
pcb->snd.nxt = pcb->iss + 1;
pcb->snd.una = pcb->iss;
pcb->state = TCP_PCB_STATE_SYN_RECEIVED;
/* ignore: Note that any other incoming control or data (combined with SYN) will be processed
in the SYN-RECEIVED state, but processing of SYN and ACK should not be repeated */
return;
}
/*
* fourth other text or control
*/
/* drop segment */
return;
case TCP_PCB_STATE_SYN_SENT:
/*
* first check the ACK bit
*/
if (TCP_FLG_ISSET(flags, TCP_FLG_ACK)) {
if (seg->ack <= pcb->iss || seg->ack > pcb->snd.nxt) {
tcp_output_segment(seg->ack, 0, TCP_FLG_RST, 0, NULL, 0, local, foreign);
return;
}
if (pcb->snd.una <= seg->ack && seg->ack <= pcb->snd.nxt) {
acceptable = 1;
}
}
/*
* second check the RST bit
*/
if (TCP_FLG_ISSET(flags, TCP_FLG_RST)) {
if (acceptable) {
errorf("connection reset");
pcb->state = TCP_PCB_STATE_CLOSED;
tcp_pcb_release(pcb);
}
/* drop segment */
return;
}
/*
* ignore: third check security and precedence
*/
/*
* fourth check the SYN bit
*/
if (TCP_FLG_ISSET(flags, TCP_FLG_SYN)) {
pcb->rcv.nxt = seg->seq + 1;
pcb->irs = seg->seq;
if (acceptable) {
pcb->snd.una = seg->ack;
tcp_retransmit_queue_cleanup(pcb);
}
if (pcb->snd.una > pcb->iss) {
pcb->state = TCP_PCB_STATE_ESTABLISHED;
tcp_output(pcb, TCP_FLG_ACK, NULL, 0);
/* NOTE: not specified in the RFC793, but send window initialization required */
pcb->snd.wnd = seg->wnd;
pcb->snd.wl1 = seg->seq;
pcb->snd.wl2 = seg->ack;
sched_wakeup(&pcb->ctx);
/* ignore: continue processing at the sixth step below where the URG bit is checked */
return;
} else {
pcb->state = TCP_PCB_STATE_SYN_RECEIVED;
tcp_output(pcb, TCP_FLG_SYN | TCP_FLG_ACK, NULL, 0);
/* ignore: If there are other controls or text in the segment, queue them for processing after the ESTABLISHED state has been reached */
return;
}
}
/*
* fifth, if neither of the SYN or RST bits is set then drop the segment and return
*/
/* drop segment */
return;
}
/*
* Otherwise
*/
/*
* first check sequence number
*/
switch (pcb->state) {
case TCP_PCB_STATE_SYN_RECEIVED:
case TCP_PCB_STATE_ESTABLISHED:
case TCP_PCB_STATE_FIN_WAIT1:
case TCP_PCB_STATE_FIN_WAIT2:
case TCP_PCB_STATE_CLOSE_WAIT:
case TCP_PCB_STATE_CLOSING:
case TCP_PCB_STATE_LAST_ACK:
case TCP_PCB_STATE_TIME_WAIT:
if (!seg->len) {
if (!pcb->rcv.wnd) {
if (seg->seq == pcb->rcv.nxt) {
acceptable = 1;
}
} else {
if (pcb->rcv.nxt <= seg->seq && seg->seq < pcb->rcv.nxt + pcb->rcv.wnd) {
acceptable = 1;
}
}
} else {
if (!pcb->rcv.wnd) {
/* not acceptable */
} else {
if ((pcb->rcv.nxt <= seg->seq && seg->seq < pcb->rcv.nxt + pcb->rcv.wnd) ||
(pcb->rcv.nxt <= seg->seq + seg->len - 1 && seg->seq + seg->len - 1 < pcb->rcv.nxt + pcb->rcv.wnd)) {
acceptable = 1;
}
}
}
if (!acceptable) {
if (!TCP_FLG_ISSET(flags, TCP_FLG_RST)) {
tcp_output(pcb, TCP_FLG_ACK, NULL, 0);
}
return;
}
/*
* In the following it is assumed that the segment is the idealized
* segment that begins at RCV.NXT and does not exceed the window.
* One could tailor actual segments to fit this assumption by
* trimming off any portions that lie outside the window (including
* SYN and FIN), and only processing further if the segment then
* begins at RCV.NXT. Segments with higher begining sequence
* numbers may be held for later processing.
*/
}
/*
* second check the RST bit
*/
switch (pcb->state) {
case TCP_PCB_STATE_SYN_RECEIVED:
if (TCP_FLG_ISSET(flags, TCP_FLG_RST)) {
pcb->state = TCP_PCB_STATE_CLOSED;
tcp_pcb_release(pcb);
return;
}
break;
case TCP_PCB_STATE_ESTABLISHED:
case TCP_PCB_STATE_FIN_WAIT1:
case TCP_PCB_STATE_FIN_WAIT2:
case TCP_PCB_STATE_CLOSE_WAIT:
if (TCP_FLG_ISSET(flags, TCP_FLG_RST)) {
errorf("connection reset");
pcb->state = TCP_PCB_STATE_CLOSED;
tcp_pcb_release(pcb);
return;
}
break;
case TCP_PCB_STATE_CLOSING:
case TCP_PCB_STATE_LAST_ACK:
case TCP_PCB_STATE_TIME_WAIT:
if (TCP_FLG_ISSET(flags, TCP_FLG_RST)) {
pcb->state = TCP_PCB_STATE_CLOSED;
tcp_pcb_release(pcb);
return;
}
break;
}
/*
* ignore: third check security and precedence
*/
/*
* fourth check the SYN bit
*/
switch (pcb->state) {
case TCP_PCB_STATE_SYN_RECEIVED:
case TCP_PCB_STATE_ESTABLISHED:
case TCP_PCB_STATE_FIN_WAIT1:
case TCP_PCB_STATE_FIN_WAIT2:
case TCP_PCB_STATE_CLOSE_WAIT:
case TCP_PCB_STATE_CLOSING:
case TCP_PCB_STATE_LAST_ACK:
case TCP_PCB_STATE_TIME_WAIT:
if (TCP_FLG_ISSET(flags, TCP_FLG_SYN)) {
tcp_output(pcb, TCP_FLG_RST, NULL, 0);
errorf("connection reset");
pcb->state = TCP_PCB_STATE_CLOSED;
tcp_pcb_release(pcb);
return;
}
}
/*
* fifth check the ACK field
*/
if (!TCP_FLG_ISSET(flags, TCP_FLG_ACK)) {
/* drop segment */
return;
}
switch (pcb->state) {
case TCP_PCB_STATE_SYN_RECEIVED:
if (pcb->snd.una <= seg->ack && seg->ack <= pcb->snd.nxt) {
pcb->state = TCP_PCB_STATE_ESTABLISHED;
sched_wakeup(&pcb->ctx);
if (pcb->parent) {
queue_push(&pcb->parent->backlog, pcb);
sched_wakeup(&pcb->parent->ctx);
}
} else {
tcp_output_segment(seg->ack, 0, TCP_FLG_RST, 0, NULL, 0, local, foreign);
return;
}
/* fall through */
case TCP_PCB_STATE_ESTABLISHED:
case TCP_PCB_STATE_FIN_WAIT1:
case TCP_PCB_STATE_FIN_WAIT2:
case TCP_PCB_STATE_CLOSE_WAIT:
case TCP_PCB_STATE_CLOSING:
if (pcb->snd.una < seg->ack && seg->ack <= pcb->snd.nxt) {
pcb->snd.una = seg->ack;
tcp_retransmit_queue_cleanup(pcb);
/* ignore: Users should receive positive acknowledgments for buffers
which have been SENT and fully acknowledged (i.e., SEND buffer should be returned with "ok" response) */
if (pcb->snd.wl1 < seg->seq || (pcb->snd.wl1 == seg->seq && pcb->snd.wl2 <= seg->ack)) {
pcb->snd.wnd = seg->wnd;
pcb->snd.wl1 = seg->seq;
pcb->snd.wl2 = seg->ack;
}
} else if (seg->ack < pcb->snd.una) {
/* ignore */
} else if (seg->ack > pcb->snd.nxt) {
tcp_output(pcb, TCP_FLG_ACK, NULL, 0);
return;
}
switch (pcb->state) {
case TCP_PCB_STATE_FIN_WAIT1:
if (seg->ack == pcb->snd.nxt) {
pcb->state = TCP_PCB_STATE_FIN_WAIT2;
}
break;
case TCP_PCB_STATE_FIN_WAIT2:
/* do not delete the TCB */
break;
case TCP_PCB_STATE_CLOSE_WAIT:
/* do nothing */
break;
case TCP_PCB_STATE_CLOSING:
if (seg->ack == pcb->snd.nxt) {
pcb->state = TCP_PCB_STATE_TIME_WAIT;
/* NOTE: set 2MSL timer, although it is not explicitly stated in the RFC */
tcp_set_timewait_timer(pcb);
sched_wakeup(&pcb->ctx);
}
break;
}
break;
case TCP_PCB_STATE_LAST_ACK:
if (seg->ack == pcb->snd.nxt) {
pcb->state = TCP_PCB_STATE_CLOSED;
tcp_pcb_release(pcb);
}
return;
case TCP_PCB_STATE_TIME_WAIT:
if (TCP_FLG_ISSET(flags, TCP_FLG_FIN)) {
tcp_set_timewait_timer(pcb); /* restart time-wait timer */
}
break;
}
/*
* ignore: sixth, check the URG bit
*/
/*
* seventh, process the segment text
*/
switch (pcb->state) {
case TCP_PCB_STATE_ESTABLISHED:
case TCP_PCB_STATE_FIN_WAIT1:
case TCP_PCB_STATE_FIN_WAIT2:
if (len) {
memcpy(pcb->buf + (sizeof(pcb->buf) - pcb->rcv.wnd), data, len);
pcb->rcv.nxt = seg->seq + seg->len;
pcb->rcv.wnd -= len;
tcp_output(pcb, TCP_FLG_ACK, NULL, 0);
sched_wakeup(&pcb->ctx);
}
break;
case TCP_PCB_STATE_CLOSE_WAIT:
case TCP_PCB_STATE_CLOSING:
case TCP_PCB_STATE_LAST_ACK:
case TCP_PCB_STATE_TIME_WAIT:
/* ignore segment text */
break;
}
/*
* eighth, check the FIN bit
*/
if (TCP_FLG_ISSET(flags, TCP_FLG_FIN)) {
switch (pcb->state) {
case TCP_PCB_STATE_CLOSED:
case TCP_PCB_STATE_LISTEN:
case TCP_PCB_STATE_SYN_SENT:
/* drop segment */
return;
}
pcb->rcv.nxt = seg->seq + 1;
tcp_output(pcb, TCP_FLG_ACK, NULL, 0);
switch (pcb->state) {
case TCP_PCB_STATE_SYN_RECEIVED:
case TCP_PCB_STATE_ESTABLISHED:
pcb->state = TCP_PCB_STATE_CLOSE_WAIT;
sched_wakeup(&pcb->ctx);
break;
case TCP_PCB_STATE_FIN_WAIT1:
if (seg->ack == pcb->snd.nxt) {
pcb->state = TCP_PCB_STATE_TIME_WAIT;
tcp_set_timewait_timer(pcb);
} else {
pcb->state = TCP_PCB_STATE_CLOSING;
}
break;
case TCP_PCB_STATE_FIN_WAIT2:
pcb->state = TCP_PCB_STATE_TIME_WAIT;
tcp_set_timewait_timer(pcb);
break;
case TCP_PCB_STATE_CLOSE_WAIT:
/* Remain in the CLOSE-WAIT state */
break;
case TCP_PCB_STATE_CLOSING:
/* Remain in the CLOSING state */
break;
case TCP_PCB_STATE_LAST_ACK:
/* Remain in the LAST-ACK state */
break;
case TCP_PCB_STATE_TIME_WAIT:
/* Remain in the TIME-WAIT state */
tcp_set_timewait_timer(pcb); /* restart time-wait timer */
break;
}
}
return;
}
static void
tcp_input(const uint8_t *data, size_t len, ip_addr_t src, ip_addr_t dst, struct ip_iface *iface)
{
struct tcp_hdr *hdr;
struct ip_pseudo_hdr pseudo;
uint16_t psum, hlen;
char addr1[IP_ADDR_STR_LEN];
char addr2[IP_ADDR_STR_LEN];
struct ip_endpoint local, foreign;
struct tcp_segment_info seg;
ip_addr_storage tcp4_src, tcp4_dst;
if (len < sizeof(*hdr)) {
errorf("too short");
return;
}
hdr = (struct tcp_hdr *)data;
tcp4_src.s_addr4 = src;
tcp4_src.family = AF_INET;
tcp4_dst.s_addr4 = dst;
tcp4_dst.family = AF_INET;
/* verify checksum value */
pseudo.src = src;
pseudo.dst = dst;
pseudo.zero = 0;
pseudo.protocol = PROTOCOL_TCP;
pseudo.len = hton16(len);
psum = ~cksum16((uint16_t *)&pseudo, sizeof(pseudo), 0);
if (cksum16((uint16_t *)hdr, len, psum) != 0) {
errorf("checksum error: sum=0x%04x, verify=0x%04x", ntoh16(hdr->sum), ntoh16(cksum16((uint16_t *)hdr, len, -hdr->sum + psum)));
return;
}
if (src == IP_ADDR_BROADCAST || src == iface->broadcast || dst == IP_ADDR_BROADCAST || dst == iface->broadcast) {
errorf("only supports unicast, src=%s, dst=%s",
ip_addr_ntop(src, addr1, sizeof(addr1)), ip_addr_ntop(dst, addr2, sizeof(addr2)));
return;
}
debugf("%s:%d => %s:%d, len=%zu (payload=%zu)",
ip_addr_ntop(src, addr1, sizeof(addr1)), ntoh16(hdr->src),
ip_addr_ntop(dst, addr2, sizeof(addr2)), ntoh16(hdr->dst),
len, len - sizeof(*hdr));
tcp_dump(data, len);
local.addr = tcp4_dst;
local.port = hdr->dst;
foreign.addr = tcp4_src;
foreign.port = hdr->src;
hlen = (hdr->off >> 4) << 2;
seg.seq = ntoh32(hdr->seq);
seg.ack = ntoh32(hdr->ack);
seg.len = len - hlen;
if (TCP_FLG_ISSET(hdr->flg, TCP_FLG_SYN)) {
seg.len++; /* SYN flag consumes one sequence number */
}
if (TCP_FLG_ISSET(hdr->flg, TCP_FLG_FIN)) {
seg.len++; /* FIN flag consumes one sequence number */
}
seg.wnd = ntoh16(hdr->wnd);
seg.up = ntoh16(hdr->up);
mutex_lock(&mutex);
tcp_segment_arrives(&seg, hdr->flg, (uint8_t *)hdr + hlen, len - hlen, &local, &foreign);
mutex_unlock(&mutex);
return;
}
static void
tcp6_input(const uint8_t *data, size_t len, ip6_addr_t src, ip6_addr_t dst, struct ip6_iface *iface)
{
struct tcp_hdr *hdr;
struct ip6_pseudo_hdr pseudo;
uint16_t psum, hlen;
char addr1[IPV6_ADDR_STR_LEN];
char addr2[IPV6_ADDR_STR_LEN];
struct ip_endpoint local, foreign;
struct tcp_segment_info seg;
ip_addr_storage tcp6_src, tcp6_dst;
if (len < sizeof(*hdr)) {
errorf("too short");
return;
}
hdr = (struct tcp_hdr *)data;
tcp6_src.s_addr6 = src;
tcp6_src.family = AF_INET6;
tcp6_dst.s_addr6 = dst;
tcp6_dst.family = AF_INET6;
/* verify checksum value */
pseudo.src = src;
pseudo.dst = dst;
pseudo.len = hton16(len);
pseudo.zero[0] = pseudo.zero[1] = pseudo.zero[2] = 0;
pseudo.nxt = PROTOCOL_TCP;
psum = ~cksum16((uint16_t *)&pseudo, sizeof(pseudo), 0);
if (cksum16((uint16_t *)hdr, len, psum) != 0) {
errorf("checksum error: sum=0x%04x, verify=0x%04x", ntoh16(hdr->sum), ntoh16(cksum16((uint16_t *)hdr, len, -hdr->sum + psum)));
return;
}
if (IPV6_ADDR_IS_MULTICAST(&src) || IPV6_ADDR_IS_MULTICAST(&dst)) {
errorf("only supports unicast, src=%s, dst=%s",
ip6_addr_ntop(src, addr1, sizeof(addr1)), ip6_addr_ntop(dst, addr2, sizeof(addr2)));
return;
}
debugf("[%s]%d => [%s]%d, len=%zu (payload=%zu)",
ip6_addr_ntop(src, addr1, sizeof(addr1)), ntoh16(hdr->src),
ip6_addr_ntop(dst, addr2, sizeof(addr2)), ntoh16(hdr->dst),
len, len - sizeof(*hdr));
#ifdef HDRDUMP
tcp_dump(data, len);
#endif
local.addr = tcp6_dst;
local.port = hdr->dst;
foreign.addr = tcp6_src;
foreign.port = hdr->src;
hlen = (hdr->off >> 4) << 2;
seg.seq = ntoh32(hdr->seq);
seg.ack = ntoh32(hdr->ack);
seg.len = len - hlen;
if (TCP_FLG_ISSET(hdr->flg, TCP_FLG_SYN)) {
seg.len++; /* SYN flag consumes one sequence number */
}
if (TCP_FLG_ISSET(hdr->flg, TCP_FLG_FIN)) {
seg.len++; /* FIN flag consumes one sequence number */
}
seg.wnd = ntoh16(hdr->wnd);
seg.up = ntoh16(hdr->up);
mutex_lock(&mutex);
tcp_segment_arrives(&seg, hdr->flg, (uint8_t *)hdr + hlen, len - hlen, &local, &foreign);
mutex_unlock(&mutex);
return;
}
static void
tcp_timer(void)
{
struct tcp_pcb *pcb;
struct timeval now;
char ep1[IP_ENDPOINT_STR_LEN];
char ep2[IP_ENDPOINT_STR_LEN];
mutex_lock(&mutex);
gettimeofday(&now, NULL);
for (pcb = pcbs; pcb < tailof(pcbs); pcb++) {
if (pcb->state == TCP_PCB_STATE_FREE) {
continue;
}
if (pcb->state == TCP_PCB_STATE_TIME_WAIT) {
if (timercmp(&now, &pcb->tw_timer, >) != 0) {
debugf("timewait has elapsed, local=%s, foreign=%s",
ip_endpoint_ntop(&pcb->local, ep1, sizeof(ep1)), ip_endpoint_ntop(&pcb->foreign, ep2, sizeof(ep2)));
tcp_pcb_release(pcb);
continue;
}
}
queue_foreach(&pcb->queue, tcp_retransmit_queue_emit, pcb);
}
mutex_unlock(&mutex);
}
static void
event_handler(void *arg)
{
struct tcp_pcb *pcb;
mutex_lock(&mutex);
for (pcb = pcbs; pcb < tailof(pcbs); pcb++) {
if (pcb->state != TCP_PCB_STATE_FREE) {
sched_interrupt(&pcb->ctx);
}
}
mutex_unlock(&mutex);
}
int
tcp_init(void)
{
struct timeval interval = {0,100000};
if (ip_protocol_register("TCP", PROTOCOL_TCP, tcp_input) == -1) {
errorf("ip_protocol_register() failure");
return -1;
}
if (ip6_protocol_register("TCP", PROTOCOL_TCP, tcp6_input) == -1) {
errorf("ip6_protocol_register() failure");
return -1;
}
if (net_timer_register("TCP Timer", interval, tcp_timer) == -1) {
errorf("net_timer_register() failure");
return -1;
}
net_event_subscribe(event_handler, NULL);
return 0;
}
/*
* TCP User Command (RFC793)
*/
int
tcp_open_rfc793(struct ip_endpoint *local, struct ip_endpoint *foreign, int active)
{
struct tcp_pcb *pcb;
char ep1[IP_ENDPOINT_STR_LEN];
char ep2[IP_ENDPOINT_STR_LEN];
int state, id;
mutex_lock(&mutex);
pcb = tcp_pcb_alloc();
if (!pcb) {
errorf("tcp_pcb_alloc() failure");
mutex_unlock(&mutex);
return -1;
}
pcb->mode = TCP_PCB_MODE_RFC793;
if (!active) {
debugf("passive open: local=%s, waiting for connection...", ip_endpoint_ntop(local, ep1, sizeof(ep1)));
pcb->local = *local;
if (foreign) {
pcb->foreign = *foreign;
}
pcb->state = TCP_PCB_STATE_LISTEN;
} else {
debugf("active open: local=%s, foreign=%s, connecting...",
ip_endpoint_ntop(local, ep1, sizeof(ep1)), ip_endpoint_ntop(foreign, ep2, sizeof(ep2)));
pcb->local = *local;
pcb->foreign = *foreign;
pcb->rcv.wnd = sizeof(pcb->buf);
pcb->iss = random();
if (tcp_output(pcb, TCP_FLG_SYN, NULL, 0) == -1) {
errorf("tcp_output() failure");
pcb->state = TCP_PCB_STATE_CLOSED;
tcp_pcb_release(pcb);
mutex_unlock(&mutex);
return -1;