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