aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / net / tcp / link.om
| 1 | note link.om: seeded impairment relay for the L232 QEMU gate. Seeded Omega. |
| 2 | note It joins two QEMU dgram netdevs over AF_UNIX datagram sockets: side A is the |
| 3 | note guest NIC, side B is the QEMU hub that holds the slirp network. Every frame is |
| 4 | note one datagram. Per direction it drops, holds back (reorders) and duplicates |
| 5 | note frames with per-mille rates from a seeded xorshift64 stream, so a run is |
| 6 | note reproducible from its arguments up to timing. Toward the guest it also |
| 7 | note injects hostile variants of real frames (see lmutate). |
| 8 | note Arguments: ABIND APEER BBIND BPEER SEED DROP REORDER DUP HOSTILE IDLE |
| 9 | note (rates per mille; IDLE seconds of silence after traffic ends the run). |
| 10 | note Output: one LINK line of counters, one LINKH line of injected kinds. |
| 11 | global Lfd ulong 2 |
| 12 | global Lcf ulong 8 |
| 13 | global Lsa uchar 128 |
| 14 | global Larg uchar 256 |
| 15 | global Lfr uchar 2048 |
| 16 | global Lmu uchar 2048 |
| 17 | global Lhb uchar 16384 |
| 18 | global Lhl ulong 8 |
| 19 | global Lhc ulong 8 |
| 20 | global Lhd ulong 8 |
| 21 | global Lpf uchar 16 |
| 22 | note Lst: per direction d (A to B d=0, B to A d=1) base 8d: 0 in, 1 out, 2 drop, |
| 23 | note 3 held, 4 dup, 5 send errors. 16 hostile injected, 17 hostile send errors. |
| 24 | global Lst ulong 24 |
| 25 | global Lhk ulong 16 |
| 26 | global Lnum uchar 32 |
| 27 | text Mlink "LINK" |
| 28 | text Mlinkh "LINKH" |
| 29 | text Msp " " |
| 30 | text Mnl "\n" |
| 31 | fn lrnd to ulong |
| 32 | x = get Lcf 0 |
| 33 | x = xor x shl x 13 |
| 34 | x = xor x shr x 7 |
| 35 | x = xor x shl x 17 |
| 36 | put Lcf 0 x |
| 37 | ret x |
| 38 | end |
| 39 | fn lpm to ulong |
| 40 | ret rem lrnd 1000 |
| 41 | end |
| 42 | fn lst k ulong to void |
| 43 | put Lst k add get Lst k 1 |
| 44 | end |
| 45 | fn lnum v ulong to void |
| 46 | i = ulong 32 |
| 47 | while true bound 21 |
| 48 | i = sub i 1 |
| 49 | put Lnum i uchar add 48 rem v 10 |
| 50 | v = div v 10 |
| 51 | if eq v 0 |
| 52 | break |
| 53 | end |
| 54 | end |
| 55 | k = write 1 Lnum i sub 32 i |
| 56 | end |
| 57 | fn lsp to void |
| 58 | k = write 1 Msp 0 len Msp |
| 59 | end |
| 60 | fn larg i ulong to ulong |
| 61 | if ge long i argcnt |
| 62 | exit 2 |
| 63 | end |
| 64 | k = argread i Larg 0 255 |
| 65 | if lt k 1 |
| 66 | exit 2 |
| 67 | end |
| 68 | ret ulong k |
| 69 | end |
| 70 | fn ldec n ulong to ulong |
| 71 | v = ulong 0 |
| 72 | for i ulong n bound 20 |
| 73 | c = ulong get Larg i |
| 74 | if or lt c 48 gt c 57 |
| 75 | exit 2 |
| 76 | end |
| 77 | v = add mul v 10 sub c 48 |
| 78 | end |
| 79 | ret v |
| 80 | end |
| 81 | note sockaddr_un from Larg[0..n) into Lsa; returns its length. |
| 82 | fn lsun n ulong to ulong |
| 83 | if gt n 100 |
| 84 | exit 2 |
| 85 | end |
| 86 | put Lsa 0 1 |
| 87 | put Lsa 1 0 |
| 88 | for i ulong n bound 100 |
| 89 | put Lsa add 2 i get Larg i |
| 90 | end |
| 91 | put Lsa add 2 n 0 |
| 92 | ret add 3 n |
| 93 | end |
| 94 | fn lnow to ulong |
| 95 | t = monons |
| 96 | if lt t 0 |
| 97 | exit 3 |
| 98 | end |
| 99 | ret div ulong t 1000000 |
| 100 | end |
| 101 | fn lopen side ulong to void |
| 102 | s = socknew 1 2 0 |
| 103 | if lt s 0 |
| 104 | exit 4 |
| 105 | end |
| 106 | n = lsun larg add 1 mul side 2 |
| 107 | if ne bind int s Lsa 0 n 0 |
| 108 | exit 4 |
| 109 | end |
| 110 | put Lfd side ulong s |
| 111 | end |
| 112 | fn lconnect side ulong to void |
| 113 | n = lsun larg add 2 mul side 2 |
| 114 | for t ulong 3000 |
| 115 | if eq connect int get Lfd side Lsa 0 n 0 |
| 116 | ret |
| 117 | end |
| 118 | z = sleepns 10000000 |
| 119 | end |
| 120 | exit 5 |
| 121 | end |
| 122 | fn lsend d ulong n ulong to void |
| 123 | note Lfr frame of n bytes in direction d. |
| 124 | k = send int get Lfd sub 1 d Lfr 0 n 0 |
| 125 | if eq k long n |
| 126 | lst add mul d 8 1 |
| 127 | else |
| 128 | lst add mul d 8 5 |
| 129 | end |
| 130 | end |
| 131 | fn lsendh d ulong slot ulong to void |
| 132 | b = mul add mul d 4 slot 2048 |
| 133 | n = get Lhl add mul d 4 slot |
| 134 | k = send int get Lfd sub 1 d Lhb b n 0 |
| 135 | if eq k long n |
| 136 | lst add mul d 8 1 |
| 137 | else |
| 138 | lst add mul d 8 5 |
| 139 | end |
| 140 | put Lhl add mul d 4 slot 0 |
| 141 | end |
| 142 | note Count down held frames of direction d; release at zero or past the deadline. |
| 143 | fn ltick d ulong frame bool to void |
| 144 | now = lnow |
| 145 | for s ulong 4 |
| 146 | j = add mul d 4 s |
| 147 | if ne get Lhl j 0 |
| 148 | if frame |
| 149 | put Lhc j sub get Lhc j 1 |
| 150 | end |
| 151 | if or eq get Lhc j 0 ge now get Lhd j |
| 152 | lsendh d s |
| 153 | end |
| 154 | end |
| 155 | end |
| 156 | end |
| 157 | fn lhold d ulong n ulong to bool |
| 158 | for s ulong 4 |
| 159 | j = add mul d 4 s |
| 160 | if eq get Lhl j 0 |
| 161 | b = mul j 2048 |
| 162 | for i ulong n bound 2048 |
| 163 | put Lhb add b i get Lfr i |
| 164 | end |
| 165 | put Lhl j n |
| 166 | put Lhc j add 1 rem lrnd 3 |
| 167 | put Lhd j add lnow 40 |
| 168 | ret true |
| 169 | end |
| 170 | end |
| 171 | ret false |
| 172 | end |
| 173 | fn lr16 p ulong to ulong |
| 174 | ret add shl ulong get Lmu p 8 ulong get Lmu add p 1 |
| 175 | end |
| 176 | fn lw16 p ulong v ulong to void |
| 177 | put Lmu p uchar and shr v 8 255 |
| 178 | put Lmu add p 1 uchar and v 255 |
| 179 | end |
| 180 | fn lw32 p ulong v ulong to void |
| 181 | lw16 p and shr v 16 65535 |
| 182 | lw16 add p 2 and v 65535 |
| 183 | end |
| 184 | fn lr32 p ulong to ulong |
| 185 | ret add shl lr16 p 16 lr16 add p 2 |
| 186 | end |
| 187 | fn lsum p ulong n ulong to ulong |
| 188 | s = ulong 0 |
| 189 | while ge n 2 bound 1024 |
| 190 | s = add s lr16 p |
| 191 | p = add p 2 |
| 192 | n = sub n 2 |
| 193 | end |
| 194 | if eq n 1 |
| 195 | s = add s shl ulong get Lmu p 8 |
| 196 | end |
| 197 | ret s |
| 198 | end |
| 199 | fn lfold s ulong to ulong |
| 200 | while gt s 65535 bound 8 |
| 201 | s = add and s 65535 shr s 16 |
| 202 | end |
| 203 | ret xor s 65535 |
| 204 | end |
| 205 | note Recompute the IPv4 header and TCP checksums of the frame in Lmu (IHL 5). |
| 206 | fn lfix to void |
| 207 | lw16 24 0 |
| 208 | lw16 24 lfold lsum 14 20 |
| 209 | t = sub lr16 16 20 |
| 210 | lw16 50 0 |
| 211 | s = add lsum 26 4 lsum 30 4 |
| 212 | s = add add s 6 t |
| 213 | lw16 50 lfold add s lsum 34 t |
| 214 | end |
| 215 | note True when Lmu[0..n) is IPv4 with IHL 5 carrying a whole TCP segment. |
| 216 | fn ltcp n ulong to bool |
| 217 | if lt n 54 |
| 218 | ret false |
| 219 | end |
| 220 | if ne lr16 12 2048 |
| 221 | ret false |
| 222 | end |
| 223 | if ne get Lmu 14 69 |
| 224 | ret false |
| 225 | end |
| 226 | if ne get Lmu 23 6 |
| 227 | ret false |
| 228 | end |
| 229 | t = lr16 16 |
| 230 | if or lt t 40 gt add t 14 n |
| 231 | ret false |
| 232 | end |
| 233 | ret true |
| 234 | end |
| 235 | fn lxdata n ulong to void |
| 236 | t = add 14 lr16 16 |
| 237 | h = add 34 mul ulong shr get Lmu 46 4 4 |
| 238 | for i ulong sub t h bound 2048 |
| 239 | p = add h i |
| 240 | put Lmu p uchar xor get Lmu p 90 |
| 241 | end |
| 242 | end |
| 243 | note Hostile kinds: 0 bit flip, 1 truncation, 2 IPv4 length too long, 3 bad |
| 244 | note version or IHL, 4 fragment bits, 5 bad TCP data offset, 6 SYN added, |
| 245 | note 7 RST outside the window, 8 altered payload with an ACK of unsent data, |
| 246 | note 9 wrong destination port, 10 garbage IPv4 frame, 11 garbage ARP frame, |
| 247 | note 12 random flags outside the window, 13 altered payload beyond the window, |
| 248 | note 14 altered payload before the window. Kind 2 carries a correct IPv4 header |
| 249 | note checksum and kinds 3..9 and 12..14 carry correct checksums, so the stack must |
| 250 | note refuse them on protocol grounds. Kinds 6..8 and 12..14 are only valid |
| 251 | note forgeries against an established sequence space: on a handshake segment (SYN |
| 252 | note set or ACK clear) a RST with an acceptable ACK or a SYN with a foreign ISS is |
| 253 | note legal, so those frames use kinds 0..5 only. Non-TCP frames (ARP has no |
| 254 | note checksum) get only the garbage kinds 10 and 11. |
| 255 | fn lmutate n ulong to ulong |
| 256 | for i ulong n bound 2048 |
| 257 | put Lmu i get Lfr i |
| 258 | end |
| 259 | k = rem lrnd 15 |
| 260 | if ltcp n |
| 261 | f = ulong get Lmu 47 |
| 262 | if or ne and f 2 0 eq and f 16 0 |
| 263 | k = rem lrnd 6 |
| 264 | end |
| 265 | else |
| 266 | k = add 10 rem lrnd 2 |
| 267 | end |
| 268 | put Lhk k add get Lhk k 1 |
| 269 | if eq k 0 |
| 270 | p = add 14 rem lrnd sub n 14 |
| 271 | put Lmu p uchar xor ulong get Lmu p shl 1 rem lrnd 8 |
| 272 | ret n |
| 273 | end |
| 274 | if eq k 1 |
| 275 | ret add 14 rem lrnd sub n 14 |
| 276 | end |
| 277 | if eq k 2 |
| 278 | lw16 16 add lr16 16 add 1 rem lrnd 100 |
| 279 | lw16 24 0 |
| 280 | lw16 24 lfold lsum 14 20 |
| 281 | ret n |
| 282 | end |
| 283 | if eq k 3 |
| 284 | v = ulong 68 |
| 285 | if eq rem lrnd 2 0 |
| 286 | v = ulong 101 |
| 287 | end |
| 288 | put Lmu 14 uchar v |
| 289 | lw16 24 0 |
| 290 | lw16 24 lfold lsum 14 20 |
| 291 | ret n |
| 292 | end |
| 293 | if eq k 4 |
| 294 | lw16 20 or 8192 rem lrnd 8192 |
| 295 | lw16 24 0 |
| 296 | lw16 24 lfold lsum 14 20 |
| 297 | ret n |
| 298 | end |
| 299 | ret lmut2 k n |
| 300 | end |
| 301 | fn lmut2 k ulong n ulong to ulong |
| 302 | if eq k 5 |
| 303 | note Offsets 0..4 are below the 20-byte minimum header: invalid at any length. |
| 304 | o = rem lrnd 5 |
| 305 | put Lmu 46 uchar or shl o 4 and ulong get Lmu 46 15 |
| 306 | lfix |
| 307 | ret n |
| 308 | end |
| 309 | if eq k 6 |
| 310 | put Lmu 47 uchar or get Lmu 47 2 |
| 311 | lfix |
| 312 | ret n |
| 313 | end |
| 314 | if eq k 7 |
| 315 | put Lmu 47 uchar or get Lmu 47 4 |
| 316 | lw32 38 and add lr32 38 2147483648 4294967295 |
| 317 | lfix |
| 318 | ret n |
| 319 | end |
| 320 | if eq k 8 |
| 321 | lw32 42 and add lr32 42 1073741824 4294967295 |
| 322 | lxdata n |
| 323 | lfix |
| 324 | ret n |
| 325 | end |
| 326 | if eq k 9 |
| 327 | lw16 36 xor lr16 36 add 1 rem lrnd 1000 |
| 328 | lfix |
| 329 | ret n |
| 330 | end |
| 331 | ret lmut3 k n |
| 332 | end |
| 333 | fn lmut3 k ulong n ulong to ulong |
| 334 | if eq k 10 |
| 335 | m = add 14 rem lrnd sub n 13 |
| 336 | for i ulong sub m 14 bound 2048 |
| 337 | put Lmu add 14 i uchar and lrnd 255 |
| 338 | end |
| 339 | lw16 12 2048 |
| 340 | ret m |
| 341 | end |
| 342 | if eq k 11 |
| 343 | m = add 14 rem lrnd 60 |
| 344 | for i ulong sub m 14 bound 64 |
| 345 | put Lmu add 14 i uchar and lrnd 255 |
| 346 | end |
| 347 | lw16 12 2054 |
| 348 | ret m |
| 349 | end |
| 350 | if eq k 12 |
| 351 | put Lmu 47 uchar and lrnd 255 |
| 352 | lw32 38 and add lr32 38 2147483648 4294967295 |
| 353 | lfix |
| 354 | ret n |
| 355 | end |
| 356 | if eq k 13 |
| 357 | note 2^30 away from a real sequence number is outside any 65535-byte window. |
| 358 | lw32 38 and add lr32 38 1073741824 4294967295 |
| 359 | lxdata n |
| 360 | lfix |
| 361 | ret n |
| 362 | end |
| 363 | lw32 38 and sub add lr32 38 4294967296 1073741824 4294967295 |
| 364 | lxdata n |
| 365 | lfix |
| 366 | ret n |
| 367 | end |
| 368 | fn linject n ulong to void |
| 369 | m = lmutate n |
| 370 | k = send int get Lfd 0 Lmu 0 m 0 |
| 371 | if eq k long m |
| 372 | lst 16 |
| 373 | else |
| 374 | lst 17 |
| 375 | end |
| 376 | end |
| 377 | note True when Lfr[0..n) is an IPv4 (IHL 5) TCP segment with SYN set. |
| 378 | fn lsyn n ulong to bool |
| 379 | if lt n 54 |
| 380 | ret false |
| 381 | end |
| 382 | if or ne get Lfr 12 8 ne get Lfr 13 0 |
| 383 | ret false |
| 384 | end |
| 385 | if or ne get Lfr 14 69 ne get Lfr 23 6 |
| 386 | ret false |
| 387 | end |
| 388 | ret ne and ulong get Lfr 47 2 0 |
| 389 | end |
| 390 | note libslirp 4.7.0 (also master in 2026) runs tcp_dooptions on every segment |
| 391 | note outside LISTEN; an MSS option on a SYN calls tcp_mss, whose sbreserve |
| 392 | note resets the socket send buffer. A SYN that reaches slirp after it has |
| 393 | note buffered host data therefore discards that data. SYN segments toward slirp |
| 394 | note (d = 0) are never held back or duplicated; they may still be dropped. |
| 395 | fn lframe d ulong n ulong to void |
| 396 | lst mul d 8 |
| 397 | v = and eq d 0 lsyn n |
| 398 | note Hostile variants go ahead of the real frame, so a stack that accepted one |
| 399 | note would keep its bytes (first copy wins) and fail the end-to-end digest. |
| 400 | if eq d 1 |
| 401 | if lt lpm get Lcf 4 |
| 402 | linject n |
| 403 | end |
| 404 | end |
| 405 | if lt lpm get Lcf 1 |
| 406 | lst add mul d 8 2 |
| 407 | ltick d true |
| 408 | ret |
| 409 | end |
| 410 | if lt lpm get Lcf 2 |
| 411 | if not v |
| 412 | if lhold d n |
| 413 | lst add mul d 8 3 |
| 414 | ret |
| 415 | end |
| 416 | end |
| 417 | end |
| 418 | lsend d n |
| 419 | if lt lpm get Lcf 3 |
| 420 | if not v |
| 421 | lst add mul d 8 4 |
| 422 | lsend d n |
| 423 | end |
| 424 | end |
| 425 | ltick d true |
| 426 | end |
| 427 | fn lreport to void |
| 428 | k = write 1 Mlink 0 len Mlink |
| 429 | for i ulong 18 |
| 430 | lsp |
| 431 | lnum get Lst i |
| 432 | end |
| 433 | k = write 1 Mnl 0 len Mnl |
| 434 | k = write 1 Mlinkh 0 len Mlinkh |
| 435 | for i ulong 15 |
| 436 | lsp |
| 437 | lnum get Lhk i |
| 438 | end |
| 439 | k = write 1 Mnl 0 len Mnl |
| 440 | end |
| 441 | fn lpoll to ulong |
| 442 | for d ulong 2 |
| 443 | s = get Lfd d |
| 444 | put Lpf mul d 8 uchar and s 255 |
| 445 | put Lpf add mul d 8 1 uchar and shr s 8 255 |
| 446 | put Lpf add mul d 8 2 0 |
| 447 | put Lpf add mul d 8 3 0 |
| 448 | put Lpf add mul d 8 4 1 |
| 449 | put Lpf add mul d 8 5 0 |
| 450 | put Lpf add mul d 8 6 0 |
| 451 | put Lpf add mul d 8 7 0 |
| 452 | end |
| 453 | r = pollfds Lpf 0 16 5000000 |
| 454 | if lt r 0 |
| 455 | exit 6 |
| 456 | end |
| 457 | ret ulong r |
| 458 | end |
| 459 | fn lpump d ulong to ulong |
| 460 | c = ulong 0 |
| 461 | for i ulong 64 |
| 462 | k = recv int get Lfd d Lfr 0 2048 64 |
| 463 | if le k 0 |
| 464 | ret c |
| 465 | end |
| 466 | lframe d ulong k |
| 467 | c = add c 1 |
| 468 | end |
| 469 | ret c |
| 470 | end |
| 471 | fn main to int |
| 472 | lopen 0 |
| 473 | lopen 1 |
| 474 | put Lcf 0 ldec larg 5 |
| 475 | if eq get Lcf 0 0 |
| 476 | put Lcf 0 1 |
| 477 | end |
| 478 | put Lcf 1 ldec larg 6 |
| 479 | put Lcf 2 ldec larg 7 |
| 480 | put Lcf 3 ldec larg 8 |
| 481 | put Lcf 4 ldec larg 9 |
| 482 | idle = mul ldec larg 10 1000 |
| 483 | lconnect 0 |
| 484 | lconnect 1 |
| 485 | last = lnow |
| 486 | seen = false |
| 487 | while true bound 1000000000 |
| 488 | r = lpoll |
| 489 | c = add lpump 0 lpump 1 |
| 490 | now = lnow |
| 491 | if gt c 0 |
| 492 | last = now |
| 493 | seen = true |
| 494 | end |
| 495 | ltick 0 false |
| 496 | ltick 1 false |
| 497 | if seen |
| 498 | if gt sub now last idle |
| 499 | lreport |
| 500 | ret 0 |
| 501 | end |
| 502 | end |
| 503 | end |
| 504 | ret 1 |
| 505 | end |