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 / link.om

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