interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / net / tcp / tcp.om

1364 lines · 23223 bytes Raw Blame History
1note tcp.om: native TCP over IPv4 and ARP for native/net (ledger L232). Seeded Omega.
2note RFC 9293 TCP, RFC 6298 RTO, RFC 5681 and RFC 6582 congestion control with
3note NewReno recovery, RFC 5961 RST/SYN/ACK hardening, RFC 826 ARP, RFC 791 IPv4.
4note One connection block (Tcb) plus one passive port. No heap: fixed rings.
5note Frames are Ethernet II without FCS, at most 2048 bytes, in Trx (input) and
6note Ttx (output). The embedding unit defines fn tlinkout n ulong to void, which
7note transmits Ttx[0..n). The caller supplies monotonic milliseconds to tstep.
8note Application bytes cross Tapp: twrite copies Tapp[0..n) into the send ring,
9note tread copies received in-order bytes into Tapp[0..n).
10note Limits: no IPv4 options are sent; received options are skipped; fragments
11note are refused; no window scaling, SACK or timestamps; receive window 65535.
12note ARP is unauthenticated (RFC 826): a frame that claims an address is believed.
13global Trx uchar 2048
14global Ttx uchar 2048
15global Tmac uchar 6
16global Tapp uchar 65536
17global Tsbuf uchar 65536
18global Trbuf uchar 65536
19global Trmap uchar 65536
20note Tcf: 0 now ms, 1 own IPv4, 2 gateway, 3 netmask, 4 next IPv4 id, 5 PRNG state,
21note 6 gateway ARP due, 7 window being sent, 8 2MSL ms, 9 own MSS, 10 minimum RTO,
22note 11 ARP rotation, 12 address being resolved, 13 maximum retries.
23global Tcf ulong 16
24note Tarp: four entries of ip, mac (48 bits), valid.
25global Tarp ulong 12
26note Tpk (parsed input): 0 frame length, 4 source, 5 destination, 6 TCP offset,
27note 7 TCP length, 8 seq, 9 ack, 10 flags, 11 data offset, 12 data length,
28note 13 window, 14 header length.
29global Tpk ulong 16
30global Tcb ulong 48
31note Tst counters: 0 frames, 1 bad ethernet, 2 arp, 3 bad ip header, 4 bad ip sum,
32note 5 fragment, 6 not ours, 7 bad tcp length/offset, 8 bad tcp sum, 9 reset sent,
33note 10 unacceptable seq, 11 rst challenged, 12 syn challenged, 13 ack too new,
34note 14 ack too old, 15 bad options, 16 timeouts, 17 fast retransmits,
35note 18 segments sent, 19 bytes delivered, 20 duplicate or outside bytes,
36note 21 bytes stored, 22 arp requests, 23 unresolved drops, 24 resets accepted,
37note 25 duplicate acks, 26 other protocol, 27 martian source, 28 other ethertype,
38note 29 data refused by state, 30 transmit errors, 31 data beyond FIN.
39global Tst ulong 32
40const kST ulong 0
41const kLP ulong 1
42const kRP ulong 2
43const kRIP ulong 3
44const kISS ulong 4
45const kUNA ulong 5
46const kNXT ulong 6
47const kSWN ulong 7
48const kWL1 ulong 8
49const kWL2 ulong 9
50const kMAX ulong 10
51const kIRS ulong 11
52const kRNX ulong 12
53const kMSS ulong 13
54const kCWN ulong 14
55const kSST ulong 15
56const kDUP ulong 16
57const kRTO ulong 17
58const kSRT ulong 18
59const kRVR ulong 19
60const kRSQ ulong 20
61const kRAT ulong 21
62const kRTX ulong 22
63const kTRY ulong 23
64const kSCN ulong 24
65const kSHD ulong 25
66const kFWN ulong 26
67const kFSN ulong 27
68const kFSQ ulong 28
69const kPFN ulong 29
70const kRHD ulong 30
71const kRCN ulong 31
72const kACK ulong 32
73const kTWA ulong 33
74const kPRS ulong 34
75const kERR ulong 35
76const kREC ulong 36
77const kOFQ ulong 37
78const kOFF ulong 38
79const kSYA ulong 39
80const kLAD ulong 40
81const kFRX ulong 41
82const kEST ulong 42
83note kPAS: the block came from LISTEN, so a reset in syn-received returns to LISTEN.
84const kPAS ulong 43
85note States: 0 closed, 1 listen, 2 syn-sent, 3 syn-received, 4 established,
86note 5 fin-wait-1, 6 fin-wait-2, 7 close-wait, 8 closing, 9 last-ack, 10 time-wait.
87note kERR: 0 none, 1 reset by peer, 2 refused, 3 retransmission limit.
88const tNOMAC ulong 281474976710656
89const tBCAST ulong 281474976710655
90fn tstat k ulong to void
91put Tst k add get Tst k 1
92end
93fn tr16 p ulong to ulong
94ret add shl ulong get Trx p 8 ulong get Trx add p 1
95end
96fn tr32 p ulong to ulong
97ret add shl tr16 p 16 tr16 add p 2
98end
99fn tw16 p ulong v ulong to void
100put Ttx p uchar and shr v 8 255
101put Ttx add p 1 uchar and v 255
102end
103fn tw32 p ulong v ulong to void
104tw16 p and shr v 16 65535
105tw16 add p 2 and v 65535
106end
107fn trmac p ulong to ulong
108ret add shl tr16 p 32 tr32 add p 2
109end
110fn twmac p ulong m ulong to void
111tw16 p and shr m 32 65535
112tw32 add p 2 and m 4294967295
113end
114fn tmymac to ulong
115v = ulong 0
116for i ulong 6
117v = add shl v 8 ulong get Tmac i
118end
119ret v
120end
121note Sequence arithmetic modulo 2^32.
122fn sadd a ulong b ulong to ulong
123ret and add a b 4294967295
124end
125fn sdif a ulong b ulong to ulong
126ret and sub add a 4294967296 and b 4294967295 4294967295
127end
128fn slt a ulong b ulong to bool
129ret gt sdif a b 2147483647
130end
131fn sle a ulong b ulong to bool
132ret not slt b a
133end
134fn umin a ulong b ulong to ulong
135if lt a b
136ret a
137end
138ret b
139end
140fn umax a ulong b ulong to ulong
141if gt a b
142ret a
143end
144ret b
145end
146fn tsumr p ulong n ulong to ulong
147s = ulong 0
148while ge n 2 bound 1024
149s = add s tr16 p
150p = add p 2
151n = sub n 2
152end
153if eq n 1
154s = add s shl ulong get Trx p 8
155end
156ret s
157end
158fn tsumt p ulong n ulong to ulong
159s = ulong 0
160while ge n 2 bound 1024
161s = add s shl ulong get Ttx p 8
162s = add s ulong get Ttx add p 1
163p = add p 2
164n = sub n 2
165end
166if eq n 1
167s = add s shl ulong get Ttx p 8
168end
169ret s
170end
171fn tfold s ulong to ulong
172while gt s 65535 bound 8
173s = add and s 65535 shr s 16
174end
175ret xor s 65535
176end
177fn tpsum a ulong b ulong n ulong to ulong
178s = add shr a 16 and a 65535
179s = add s shr b 16
180s = add s and b 65535
181ret add add s 6 n
182end
183fn trnd to ulong
184x = get Tcf 5
185if eq x 0
186x = ulong 88172645463325252
187end
188x = xor x shl x 13
189x = xor x shr x 7
190x = xor x shl x 17
191put Tcf 5 x
192ret x
193end
194note Unicast, routable source: not 0/8, 127/8, multicast, limited or subnet broadcast.
195fn tipok ip ulong to bool
196x = shr ip 24
197if or eq x 0 eq x 127
198ret false
199end
200if ge x 224
201ret false
202end
203h = and not get Tcf 3 4294967295
204if eq and ip h h
205ret false
206end
207ret ne ip get Tcf 1
208end
209fn tnhop ip ulong to ulong
210m = get Tcf 3
211if eq and ip m and get Tcf 1 m
212ret ip
213end
214ret get Tcf 2
215end
216fn tarpget ip ulong to ulong
217for k ulong 4
218b = mul k 3
219if eq get Tarp b ip
220if ne get Tarp add b 2 0
221ret get Tarp add b 1
222end
223end
224end
225ret tNOMAC
226end
227note Refresh an entry; insert only when force (the frame was addressed to us or answers our request).
228fn tarpset ip ulong mac ulong force bool to void
229for k ulong 4
230b = mul k 3
231if eq get Tarp b ip
232if ne get Tarp add b 2 0
233put Tarp add b 1 mac
234ret
235end
236end
237end
238if not force
239ret
240end
241for k ulong 4
242b = mul k 3
243if eq get Tarp add b 2 0
244put Tarp b ip
245put Tarp add b 1 mac
246put Tarp add b 2 1
247ret
248end
249end
250r = and get Tcf 11 3
251put Tcf 11 add get Tcf 11 1
252b = mul r 3
253put Tarp b ip
254put Tarp add b 1 mac
255put Tarp add b 2 1
256end
257fn teth dst ulong typ ulong to void
258twmac 0 dst
259twmac 6 tmymac
260tw16 12 typ
261end
262fn tpad n ulong to ulong
263if ge n 60
264ret n
265end
266for i ulong sub 60 n bound 60
267put Ttx add n i 0
268end
269ret 60
270end
271fn tarpmsg op ulong tha ulong tpa ulong to void
272tw16 14 1
273tw16 16 2048
274put Ttx 18 6
275put Ttx 19 4
276tw16 20 op
277twmac 22 tmymac
278tw32 28 get Tcf 1
279twmac 32 tha
280tw32 38 tpa
281tlinkout tpad 42
282end
283fn tarpreq ip ulong to void
284teth tBCAST 2054
285put Tcf 12 ip
286tstat 22
287tarpmsg 1 0 ip
288end
289fn tarpin n ulong to void
290tstat 2
291if lt n 42
292ret
293end
294if ne tr16 14 1
295ret
296end
297if ne tr16 16 2048
298ret
299end
300if ne tr16 18 1540
301ret
302end
303op = tr16 20
304sha = trmac 22
305spa = tr32 28
306tpa = tr32 38
307if not tipok spa
308tstat 27
309ret
310end
311if ne and shr sha 40 1 0
312tstat 27
313ret
314end
315if eq tpa get Tcf 1
316tarpset spa sha true
317if eq op 1
318teth sha 2054
319tarpmsg 2 sha spa
320end
321ret
322end
323if eq op 2
324if eq spa get Tcf 12
325tarpset spa sha true
326ret
327end
328end
329tarpset spa sha false
330end
331note Ttx[34..34+n) holds a TCP segment whose checksum field is zero. Adds the
332note IPv4 header, the TCP checksum and Ethernet, then transmits; with no
333note next-hop address it sends an ARP request instead and drops the segment.
334fn tipsend dst ulong n ulong to void
335nh = tnhop dst
336mac = tarpget nh
337if eq mac tNOMAC
338tstat 23
339tarpreq nh
340ret
341end
342me = get Tcf 1
343s = tpsum me dst n
344tw16 50 tfold add s tsumt 34 n
345put Ttx 14 69
346put Ttx 15 0
347tw16 16 add 20 n
348tw16 18 and get Tcf 4 65535
349put Tcf 4 add get Tcf 4 1
350tw16 20 16384
351put Ttx 22 64
352put Ttx 23 6
353tw16 24 0
354tw32 26 me
355tw32 30 dst
356tw16 24 tfold tsumt 14 20
357teth mac 2048
358tstat 18
359tlinkout tpad add 34 n
360end
361fn thdr lp ulong rp ulong seq ulong ack ulong flags ulong opt ulong to void
362tw16 34 lp
363tw16 36 rp
364tw32 38 seq
365tw32 42 ack
366put Ttx 46 uchar shl add 5 shr opt 2 4
367put Ttx 47 uchar and flags 255
368tw16 48 get Tcf 7
369tw16 50 0
370tw16 52 0
371if eq opt 4
372put Ttx 54 2
373put Ttx 55 4
374tw16 56 get Tcf 9
375end
376end
377fn trwnd to ulong
378ret sub 65535 get Tcb kRCN
379end
380note Send one segment of the connection: n data bytes from the send ring at seq.
381fn tsend seq ulong flags ulong n ulong to void
382opt = ulong 0
383if ne and flags 2 0
384opt = ulong 4
385end
386tsetwin
387thdr get Tcb kLP get Tcb kRP seq tackval flags flags opt
388tcopy add 54 opt add get Tcb kSHD sdif seq get Tcb kUNA n
389tipsend get Tcb kRIP add add 20 opt n
390end
391fn tsetwin to void
392w = trwnd
393put Tcf 7 w
394put Tcb kLAD w
395end
396fn tackval flags ulong to ulong
397if eq and flags 16 0
398ret 0
399end
400put Tcb kACK 0
401ret get Tcb kRNX
402end
403fn tcopy h ulong b ulong n ulong to void
404for i ulong n bound 1460
405put Ttx add h i get Tsbuf and add b i 65535
406end
407end
408note RFC 9293 3.10.7.1: answer a segment that has no connection.
409fn trstout to void
410f = get Tpk 10
411if ne and f 4 0
412ret
413end
414p = get Tpk 6
415put Tcf 7 0
416if ne and f 16 0
417thdr tr16 add p 2 tr16 p get Tpk 9 0 4 0
418else
419n = get Tpk 12
420if ne and f 2 0
421n = add n 1
422end
423if ne and f 1 0
424n = add n 1
425end
426thdr tr16 add p 2 tr16 p 0 sadd get Tpk 8 n 20 0
427end
428tstat 9
429tipsend get Tpk 4 20
430end
431fn tclear to void
432for i ulong 48
433put Tcb i 0
434end
435for i ulong 65536
436put Trmap i 0
437end
438end
439fn tinit to void
440tclear
441put Tcf 6 0
442if eq get Tcf 9 0
443put Tcf 9 1460
444end
445note Data segments are copied with a 1460-byte bound (tcopy, tstore); the floor of 64 keeps
446note the tpush loop bound true for any embedder setting.
447put Tcf 9 umax umin get Tcf 9 1460 64
448if eq get Tcf 10 0
449put Tcf 10 200
450end
451if eq get Tcf 8 0
452put Tcf 8 1000
453end
454if eq get Tcf 13 0
455put Tcf 13 12
456end
457end
458fn tfresh lp ulong to void
459tclear
460put Tcb kLP lp
461put Tcb kMSS 536
462put Tcb kRTO 1000
463put Tcb kSST 262144
464put Tcb kCWN 5360
465end
466fn tsetiss to void
467i = and trnd 4294967295
468put Tcb kISS i
469put Tcb kUNA i
470put Tcb kNXT sadd i 1
471put Tcb kMAX sadd i 1
472end
473fn tarm to void
474put Tcb kRTX add get Tcf 0 get Tcb kRTO
475end
476fn tlisten port ulong to void
477tfresh port
478put Tcb kST 1
479end
480fn tconnect ip ulong rport ulong lport ulong to void
481tfresh lport
482put Tcb kRIP ip
483put Tcb kRP rport
484tsetiss
485put Tcb kST 2
486tsend get Tcb kISS 2 0
487tarm
488put Tcb kRSQ sadd get Tcb kISS 1
489put Tcb kRAT umax get Tcf 0 1
490end
491fn tdrop code ulong to void
492if eq get Tcb kERR 0
493put Tcb kERR code
494end
495put Tcb kST 0
496put Tcb kRTX 0
497put Tcb kPRS 0
498end
499fn tclose to void
500s = get Tcb kST
501if le s 2
502tdrop 0
503ret
504end
505put Tcb kFWN 1
506end
507note SYN options: maximum segment size; others skipped; malformed lists refuse the SYN.
508fn topts to bool
509p = add get Tpk 6 20
510e = get Tpk 11
511m = ulong 536
512while lt p e bound 40
513k = get Trx p
514if eq k 0
515break
516end
517if eq k 1
518p = add p 1
519continue
520end
521if ge add p 1 e
522ret false
523end
524o = ulong get Trx add p 1
525if lt o 2
526ret false
527end
528if gt add p o e
529ret false
530end
531if eq k 2
532if ne o 4
533ret false
534end
535m = tr16 add p 2
536end
537p = add p o
538end
539m = umax m 64
540m = umin m get Tcf 9
541put Tcb kMSS m
542put Tcb kCWN mul 10 m
543ret true
544end
545fn tlistin to void
546f = get Tpk 10
547if ne and f 4 0
548ret
549end
550if ne and f 16 0
551trstout
552ret
553end
554if eq and f 2 0
555ret
556end
557if ne and f 1 0
558ret
559end
560if not topts
561tstat 15
562ret
563end
564p = get Tpk 6
565put Tcb kRP tr16 p
566put Tcb kRIP get Tpk 4
567put Tcb kIRS get Tpk 8
568put Tcb kRNX sadd get Tpk 8 1
569put Tcb kSWN get Tpk 13
570put Tcb kWL1 get Tpk 8
571tsetiss
572put Tcb kWL2 get Tcb kISS
573put Tcb kST 3
574put Tcb kPAS 1
575tsend get Tcb kISS 18 0
576tarm
577put Tcb kRSQ sadd get Tcb kISS 1
578put Tcb kRAT umax get Tcf 0 1
579end
580fn tsample r ulong to void
581r = umin r 60000
582if eq get Tcb kSRT 0
583put Tcb kSRT umax mul r 8 1
584put Tcb kRVR mul r 2
585else
586s = div get Tcb kSRT 8
587d = sub r s
588if lt r s
589d = sub s r
590end
591v = get Tcb kRVR
592put Tcb kRVR add sub v div v 4 d
593w = get Tcb kSRT
594put Tcb kSRT umax add sub w div w 8 r 1
595end
596o = add div get Tcb kSRT 8 umax get Tcb kRVR 1
597o = umax o get Tcf 10
598put Tcb kRTO umin o 60000
599end
600fn tgrow n ulong to void
601c = get Tcb kCWN
602m = get Tcb kMSS
603if lt c get Tcb kSST
604c = add c umin n m
605else
606c = add c umax div mul m m c 1
607end
608put Tcb kCWN umin c 262144
609end
610note Retransmit one segment at the oldest unacknowledged sequence (data, else FIN).
611fn tretx1 to void
612n = umin get Tcb kSCN get Tcb kMSS
613u = get Tcb kUNA
614if gt n 0
615tsend u 24 n
616ret
617end
618if ne get Tcb kFSN 0
619if eq u get Tcb kFSQ
620tsend u 17 0
621end
622end
623end
624fn tnewack ack ulong to void
625n = sdif ack get Tcb kUNA
626if eq get Tcb kSYA 0
627put Tcb kSYA 1
628put Tcb kUNA sadd get Tcb kUNA 1
629n = sub n 1
630end
631d = umin n get Tcb kSCN
632put Tcb kSHD and add get Tcb kSHD d 65535
633put Tcb kSCN sub get Tcb kSCN d
634put Tcb kUNA sadd get Tcb kUNA d
635if gt sub n d 0
636put Tcb kUNA sadd get Tcb kUNA sub n d
637end
638if slt get Tcb kNXT get Tcb kUNA
639put Tcb kNXT get Tcb kUNA
640end
641if ne get Tcb kRAT 0
642if sle get Tcb kRSQ ack
643tsample sub get Tcf 0 get Tcb kRAT
644put Tcb kRAT 0
645end
646end
647if ne get Tcb kFRX 0
648if sle get Tcb kREC ack
649put Tcb kFRX 0
650put Tcb kCWN get Tcb kSST
651else
652tretx1
653c = sub get Tcb kCWN umin d get Tcb kCWN
654put Tcb kCWN add c get Tcb kMSS
655end
656else
657tgrow d
658end
659put Tcb kDUP 0
660put Tcb kTRY 0
661if eq get Tcb kUNA get Tcb kMAX
662put Tcb kRTX 0
663else
664tarm
665end
666end
667fn tdupchk to void
668if ne get Tpk 12 0
669ret
670end
671if ne and get Tpk 10 3 0
672ret
673end
674if ne get Tpk 13 get Tcb kSWN
675ret
676end
677if eq get Tcb kUNA get Tcb kMAX
678ret
679end
680tstat 25
681k = add get Tcb kDUP 1
682put Tcb kDUP k
683m = get Tcb kMSS
684if eq k 3
685if eq get Tcb kFRX 0
686f = sdif get Tcb kMAX get Tcb kUNA
687put Tcb kSST umax div f 2 mul 2 m
688put Tcb kREC get Tcb kMAX
689put Tcb kFRX 1
690put Tcb kRAT 0
691tstat 17
692tretx1
693put Tcb kCWN add get Tcb kSST mul 3 m
694end
695ret
696end
697if gt k 3
698if ne get Tcb kFRX 0
699put Tcb kCWN umin add get Tcb kCWN m 262144
700end
701end
702end
703fn twinupd to void
704s = get Tpk 8
705a = get Tpk 9
706w1 = get Tcb kWL1
707if or slt w1 s and eq w1 s sle get Tcb kWL2 a
708put Tcb kSWN get Tpk 13
709put Tcb kWL1 s
710put Tcb kWL2 a
711end
712end
713fn tfinacked to bool
714if eq get Tcb kFSN 0
715ret false
716end
717ret eq get Tcb kUNA sadd get Tcb kFSQ 1
718end
719fn ttimewait to void
720put Tcb kST 10
721put Tcb kRTX 0
722put Tcb kTWA add get Tcf 0 get Tcf 8
723end
724note ACK processing (RFC 9293 3.10.7.4 fifth check, RFC 5961 section 5).
725fn tackin to bool
726st = get Tcb kST
727a = get Tpk 9
728u = get Tcb kUNA
729x = get Tcb kMAX
730if eq st 3
731if or sle a u slt x a
732trstout
733ret false
734end
735put Tcb kST 4
736put Tcb kEST 1
737end
738if slt x a
739put Tcb kACK 1
740tstat 13
741ret false
742end
743if slt a sdif u 65535
744put Tcb kACK 1
745tstat 14
746ret false
747end
748if slt u a
749tnewack a
750else
751if eq a u
752tdupchk
753end
754end
755twinupd
756st = get Tcb kST
757if eq st 5
758if tfinacked
759put Tcb kST 6
760end
761end
762if eq st 8
763if tfinacked
764ttimewait
765end
766end
767if eq st 9
768if tfinacked
769tdrop 0
770ret false
771end
772end
773ret true
774end
775fn tinwin s ulong b ulong w ulong to bool
776ret lt sdif s b w
777end
778note First check of RFC 9293 3.10.7.4. A zero window admits only seq = RCV.NXT.
779fn tseqok to bool
780s = get Tpk 8
781n = get Tpk 12
782f = get Tpk 10
783if ne and f 2 0
784n = add n 1
785end
786if ne and f 1 0
787n = add n 1
788end
789w = trwnd
790r = get Tcb kRNX
791if eq w 0
792ret eq s r
793end
794if eq n 0
795ret tinwin s r w
796end
797if tinwin s r w
798ret true
799end
800ret tinwin sadd s sub n 1 r w
801end
802fn tfinacc to void
803put Tcb kOFF 0
804put Tcb kPFN 1
805put Tcb kRNX sadd get Tcb kRNX 1
806put Tcb kACK 1
807st = get Tcb kST
808if eq st 4
809put Tcb kST 7
810ret
811end
812if eq st 5
813if tfinacked
814ttimewait
815else
816put Tcb kST 8
817end
818ret
819end
820if eq st 6
821ttimewait
822end
823end
824fn tdrain to void
825r = get Tcb kRNX
826c = get Tcb kRCN
827b = and add get Tcb kRHD c 65535
828k = ulong 0
829while lt c 65535 bound 65536
830if eq get Trmap b 0
831break
832end
833put Trmap b 0
834c = add c 1
835k = add k 1
836r = sadd r 1
837b = and add b 1 65535
838end
839put Tcb kRCN c
840put Tcb kRNX r
841put Tst 19 add get Tst 19 k
842if ne get Tcb kOFF 0
843if eq get Tcb kOFQ r
844tfinacc
845end
846end
847end
848fn tdata to void
849st = get Tcb kST
850if or lt st 4 gt st 6
851tstat 29
852ret
853end
854if ne get Tcb kPFN 0
855tstat 29
856ret
857end
858r = get Tcb kRNX
859w = trwnd
860if ne get Tcb kOFF 0
861w = umin w sdif get Tcb kOFQ r
862end
863tstore sdif get Tpk 8 r get Tpk 11 w get Tpk 12
864put Tcb kACK 1
865tdrain
866end
867note Store payload bytes whose offset from RCV.NXT is below w; the first copy wins.
868fn tstore d0 ulong o ulong w ulong n ulong to void
869b = and add get Tcb kRHD get Tcb kRCN 65535
870for i ulong n bound 2048
871d = and add d0 i 4294967295
872if lt d w
873q = and add b d 65535
874if eq get Trmap q 0
875put Trbuf q get Trx add o i
876put Trmap q 1
877tstat 21
878else
879tstat 20
880end
881else
882tstat 20
883end
884end
885end
886fn tfinin to void
887st = get Tcb kST
888if ne get Tcb kPFN 0
889put Tcb kACK 1
890ret
891end
892if or lt st 4 gt st 6
893ret
894end
895f = sadd get Tpk 8 get Tpk 12
896r = get Tcb kRNX
897if ne get Tcb kOFF 0
898if ne f get Tcb kOFQ
899tstat 31
900ret
901end
902end
903if ge sdif f r trwnd
904put Tcb kACK 1
905ret
906end
907put Tcb kOFF 1
908put Tcb kOFQ f
909put Tcb kACK 1
910if eq f r
911tfinacc
912end
913end
914fn tsynsent to void
915f = get Tpk 10
916a = get Tpk 9
917ha = ne and f 16 0
918if ha
919if or sle a get Tcb kISS slt get Tcb kNXT a
920if eq and f 4 0
921trstout
922end
923ret
924end
925end
926if ne and f 4 0
927if ha
928tdrop 2
929end
930ret
931end
932if eq and f 2 0
933ret
934end
935if not topts
936tstat 15
937ret
938end
939s = get Tpk 8
940put Tcb kIRS s
941put Tcb kRNX sadd s 1
942put Tcb kWL1 s
943put Tcb kWL2 a
944put Tcb kSWN get Tpk 13
945if ha
946put Tcb kUNA a
947put Tcb kSYA 1
948put Tcb kST 4
949put Tcb kEST 1
950put Tcb kACK 1
951put Tcb kRTX 0
952put Tcb kTRY 0
953if ne get Tcb kRAT 0
954tsample sub get Tcf 0 get Tcb kRAT
955put Tcb kRAT 0
956end
957ret
958end
959put Tcb kST 3
960tsend get Tcb kISS 18 0
961tarm
962end
963note A duplicate SYN in syn-received is an unacceptable segment and gets the plain
964note ACK of RFC 9293 3.10.7.4; a lost SYN-ACK is recovered by its retransmit timer.
965fn tconn to void
966st = get Tcb kST
967if eq st 2
968tsynsent
969ret
970end
971f = get Tpk 10
972s = get Tpk 8
973if not tseqok
974if eq and f 4 0
975put Tcb kACK 1
976end
977tstat 10
978note RFC 9293 3.10.7.4: with a zero receive window, still process a valid ACK.
979if eq trwnd 0
980if eq and f 22 16
981b = tackin
982end
983end
984note A retransmitted FIN in TIME-WAIT restarts the 2MSL timer (RFC 9293 3.10.7.4).
985if eq st 10
986if eq and f 5 1
987put Tcb kTWA add get Tcf 0 get Tcf 8
988end
989end
990ret
991end
992note RST: TIME-WAIT ignores it (RFC 1337); CLOSING and LAST-ACK close without an
993note error (RFC 9293 3.10.7.4); a passive syn-received block returns to LISTEN
994note and an active one reports refused.
995if ne and f 4 0
996if eq st 10
997ret
998end
999if eq s get Tcb kRNX
1000tstat 24
1001if and eq st 3 ne get Tcb kPAS 0
1002tlisten get Tcb kLP
1003ret
1004end
1005if or eq st 8 eq st 9
1006tdrop 0
1007ret
1008end
1009if eq st 3
1010tdrop 2
1011ret
1012end
1013tdrop 1
1014ret
1015end
1016put Tcb kACK 1
1017tstat 11
1018ret
1019end
1020if ne and f 2 0
1021put Tcb kACK 1
1022tstat 12
1023ret
1024end
1025if eq and f 16 0
1026ret
1027end
1028if not tackin
1029ret
1030end
1031if gt get Tpk 12 0
1032tdata
1033end
1034if ne and f 1 0
1035tfinin
1036end
1037end
1038fn ttcpin to void
1039p = get Tpk 6
1040l = get Tpk 7
1041if lt l 20
1042tstat 7
1043ret
1044end
1045s = tpsum get Tpk 4 get Tpk 5 l
1046if ne tfold add s tsumr p l 0
1047tstat 8
1048ret
1049end
1050h = mul ulong shr get Trx add p 12 4 4
1051if or lt h 20 gt h l
1052tstat 7
1053ret
1054end
1055put Tpk 8 tr32 add p 4
1056put Tpk 9 tr32 add p 8
1057put Tpk 10 ulong get Trx add p 13
1058put Tpk 11 add p h
1059put Tpk 12 sub l h
1060put Tpk 13 tr16 add p 14
1061put Tpk 14 h
1062sp = tr16 p
1063dp = tr16 add p 2
1064if or eq sp 0 eq dp 0
1065tstat 7
1066ret
1067end
1068st = get Tcb kST
1069if gt st 1
1070if eq dp get Tcb kLP
1071if eq sp get Tcb kRP
1072if eq get Tpk 4 get Tcb kRIP
1073tconn
1074ret
1075end
1076end
1077end
1078end
1079if eq st 1
1080if eq dp get Tcb kLP
1081tlistin
1082ret
1083end
1084end
1085trstout
1086end
1087fn tipin n ulong to void
1088if lt n 34
1089tstat 3
1090ret
1091end
1092v = ulong get Trx 14
1093h = mul and v 15 4
1094if or ne shr v 4 4 lt h 20
1095tstat 3
1096ret
1097end
1098t = tr16 16
1099if or lt t h gt t sub n 14
1100tstat 3
1101ret
1102end
1103if ne tfold tsumr 14 h 0
1104tstat 4
1105ret
1106end
1107if ne and tr16 20 16383 0
1108tstat 5
1109ret
1110end
1111if ne tr32 30 get Tcf 1
1112tstat 6
1113ret
1114end
1115src = tr32 26
1116if not tipok src
1117tstat 27
1118ret
1119end
1120if ne get Trx 23 6
1121tstat 26
1122ret
1123end
1124put Tpk 4 src
1125put Tpk 5 tr32 30
1126put Tpk 6 add 14 h
1127put Tpk 7 sub t h
1128ttcpin
1129end
1130fn tinput n ulong to void
1131tstat 0
1132if or lt n 14 gt n 2048
1133tstat 1
1134ret
1135end
1136d = trmac 0
1137if ne d tmymac
1138if ne d tBCAST
1139tstat 1
1140ret
1141end
1142end
1143y = tr16 12
1144put Tpk 0 n
1145if eq y 2054
1146tarpin n
1147ret
1148end
1149if eq y 2048
1150tipin n
1151ret
1152end
1153tstat 28
1154end
1155note Unsent data bytes in the send ring.
1156fn tavail to ulong
1157e = sadd get Tcb kUNA get Tcb kSCN
1158n = get Tcb kNXT
1159if slt n e
1160ret sdif e n
1161end
1162ret 0
1163end
1164fn tcansend to bool
1165s = get Tcb kST
1166if or eq s 4 eq s 5
1167ret true
1168end
1169if or eq s 7 eq s 8
1170ret true
1171end
1172ret eq s 9
1173end
1174fn tpush to void
1175if not tcansend
1176ret
1177end
1178l = umin get Tcb kCWN get Tcb kSWN
1179note l is at most 65535 (16-bit window, no scaling) and tinit/topts keep the MSS at 64 or more,
1180note so a full window takes at most 1024 sending passes (ceil 65535/64), then one pass that
1181note returns on the full window or sends FIN: 1025 passes, and the bound leaves one spare.
1182while true bound 1026
1183v = tavail
1184if gt v 0
1185f = sdif get Tcb kNXT get Tcb kUNA
1186if ge f l
1187ret
1188end
1189n = umin umin v get Tcb kMSS sub l f
1190x = get Tcb kNXT
1191tsend x 24 n
1192if sle get Tcb kMAX x
1193if eq get Tcb kRAT 0
1194put Tcb kRSQ sadd x n
1195put Tcb kRAT umax get Tcf 0 1
1196end
1197end
1198put Tcb kNXT sadd x n
1199if slt get Tcb kMAX get Tcb kNXT
1200put Tcb kMAX get Tcb kNXT
1201end
1202if eq get Tcb kRTX 0
1203tarm
1204end
1205continue
1206end
1207tpushfin
1208ret
1209end
1210end
1211fn tpushfin to void
1212if eq get Tcb kFWN 0
1213ret
1214end
1215x = get Tcb kNXT
1216if ne x sadd get Tcb kUNA get Tcb kSCN
1217ret
1218end
1219if ne get Tcb kFSN 0
1220if ne x get Tcb kFSQ
1221ret
1222end
1223end
1224tsend x 17 0
1225put Tcb kFSQ x
1226put Tcb kFSN 1
1227put Tcb kNXT sadd x 1
1228if slt get Tcb kMAX get Tcb kNXT
1229put Tcb kMAX get Tcb kNXT
1230end
1231if eq get Tcb kRTX 0
1232tarm
1233end
1234s = get Tcb kST
1235if eq s 4
1236put Tcb kST 5
1237end
1238if eq s 7
1239put Tcb kST 9
1240end
1241end
1242fn ttimeout to void
1243put Tcb kTRY add get Tcb kTRY 1
1244if gt get Tcb kTRY get Tcf 13
1245if and eq get Tcb kST 3 ne get Tcb kPAS 0
1246tlisten get Tcb kLP
1247ret
1248end
1249tdrop 3
1250ret
1251end
1252tstat 16
1253put Tcb kRTO umin mul get Tcb kRTO 2 60000
1254put Tcb kRAT 0
1255st = get Tcb kST
1256if eq st 2
1257tsend get Tcb kISS 2 0
1258tarm
1259ret
1260end
1261if eq st 3
1262tsend get Tcb kISS 18 0
1263tarm
1264ret
1265end
1266m = get Tcb kMSS
1267f = sdif get Tcb kNXT get Tcb kUNA
1268put Tcb kSST umax div f 2 mul 2 m
1269put Tcb kCWN m
1270put Tcb kFRX 0
1271put Tcb kDUP 0
1272put Tcb kNXT get Tcb kUNA
1273tarm
1274end
1275note Zero-window probe: a segment one below SND.UNA forces an ACK with the current window.
1276fn tpersist to void
1277if ne get Tcb kSWN 0
1278ret
1279end
1280if not tcansend
1281ret
1282end
1283if eq tavail 0
1284ret
1285end
1286if ne get Tcb kUNA get Tcb kMAX
1287ret
1288end
1289n = get Tcf 0
1290if lt n get Tcb kPRS
1291ret
1292end
1293put Tcb kPRS add n umax get Tcb kRTO 500
1294tsend sdif get Tcb kUNA 1 16 0
1295end
1296fn tstep now ulong to void
1297put Tcf 0 now
1298g = get Tcf 2
1299if eq tarpget g tNOMAC
1300if ge now get Tcf 6
1301put Tcf 6 add now 500
1302tarpreq g
1303end
1304end
1305st = get Tcb kST
1306if eq st 10
1307if ne get Tcb kACK 0
1308tsend get Tcb kNXT 16 0
1309end
1310if ge now get Tcb kTWA
1311tdrop 0
1312end
1313ret
1314end
1315if lt st 2
1316ret
1317end
1318r = get Tcb kRTX
1319if ne r 0
1320if ge now r
1321ttimeout
1322end
1323end
1324tpersist
1325tpush
1326if ne get Tcb kACK 0
1327if ge get Tcb kST 3
1328tsend get Tcb kNXT 16 0
1329end
1330end
1331end
1332note Application interface. twrite queues Tapp[0..n) and returns the count taken.
1333fn twrite n ulong to ulong
1334s = get Tcb kST
1335if or lt s 2 gt s 7
1336ret 0
1337end
1338if or eq s 5 eq s 6
1339ret 0
1340end
1341if ne get Tcb kFWN 0
1342ret 0
1343end
1344n = umin n sub 65535 get Tcb kSCN
1345b = add get Tcb kSHD get Tcb kSCN
1346for i ulong n bound 65536
1347put Tsbuf and add b i 65535 get Tapp i
1348end
1349put Tcb kSCN add get Tcb kSCN n
1350ret n
1351end
1352fn tread cap ulong to ulong
1353n = umin get Tcb kRCN umin cap 65536
1354b = get Tcb kRHD
1355for i ulong n bound 65536
1356put Tapp i get Trbuf and add b i 65535
1357end
1358put Tcb kRHD and add b n 65535
1359put Tcb kRCN sub get Tcb kRCN n
1360if ge trwnd add get Tcb kLAD get Tcf 9
1361put Tcb kACK 1
1362end
1363ret n
1364end