omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / net / core / net.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/net/core/net.om: Pure-Omega AIENOS M6-A network protocol layer and boot report framing. |
| 3 | note Converted from aienux native/net/aienos_net.{h,c} and native/net/aienos_report.{h,c}. |
| 4 | note Compose with: si/directives/om2.om aienux/net/core/net.om ... |
| 5 | |
| 6 | const NET_OK int 0 |
| 7 | const NET_ERR_TRUNCATED int 1 |
| 8 | const NET_ERR_INVALID int 2 |
| 9 | const NET_ERR_UNSUPPORTED int 3 |
| 10 | const NET_ERR_CAPACITY int 4 |
| 11 | |
| 12 | const NET_ETHERTYPE_IPV4 ushort 0x0800 |
| 13 | const NET_ETHERTYPE_ARP ushort 0x0806 |
| 14 | const NET_ARP_REQUEST ushort 1 |
| 15 | const NET_ARP_REPLY ushort 2 |
| 16 | const NET_IPPROTO_ICMP uchar 1 |
| 17 | const NET_IPPROTO_UDP uchar 17 |
| 18 | |
| 19 | const NET_REPORT_PORT ushort 47031 |
| 20 | const NET_REPORT_CHUNK ulong 1024 |
| 21 | const NET_REPORT_MAX ulong 65536 |
| 22 | |
| 23 | struct NetEthHdr dst_hi ulong dst_lo ulong src_hi ulong src_lo ulong ethertype ushort pay_off ulong pay_len ulong end |
| 24 | struct NetArp op ushort smac_hi ulong smac_lo ulong sip uint tmac_hi ulong tmac_lo ulong tip uint end |
| 25 | struct NetIpv4 ihl uchar dscp uchar total_len ushort ident ushort flags_frag ushort ttl uchar proto uchar csum ushort src uint dst uint pay_off ulong pay_len ulong end |
| 26 | struct NetUdp sport ushort dport ushort ulen ushort csum ushort pay_off ulong pay_len ulong end |
| 27 | |
| 28 | const NET_ARP_CAP ulong 32 |
| 29 | global net_arp_ip uint 32 |
| 30 | global net_arp_mac uchar 192 |
| 31 | global net_arp_exp ulong 32 |
| 32 | global net_arp_used uchar 32 |
| 33 | |
| 34 | fn net_be16 buf view uchar * off ulong to ushort |
| 35 | b0 = ushort get buf off |
| 36 | b1 = ushort get buf add off 1 |
| 37 | ret or shl b0 8 b1 |
| 38 | end |
| 39 | |
| 40 | fn net_be32 buf view uchar * off ulong to uint |
| 41 | b0 = shl uint get buf off 24 |
| 42 | b1 = shl uint get buf add off 1 16 |
| 43 | b2 = shl uint get buf add off 2 8 |
| 44 | b3 = uint get buf add off 3 |
| 45 | ret or or b0 b1 or b2 b3 |
| 46 | end |
| 47 | |
| 48 | fn net_put_be16 buf edit uchar * off ulong v ushort to void |
| 49 | put buf off uchar and shr v 8 0xff |
| 50 | put buf add off 1 uchar and v 0xff |
| 51 | end |
| 52 | |
| 53 | fn net_put_be32 buf edit uchar * off ulong v uint to void |
| 54 | put buf off uchar and shr v 24 0xff |
| 55 | put buf add off 1 uchar and shr v 16 0xff |
| 56 | put buf add off 2 uchar and shr v 8 0xff |
| 57 | put buf add off 3 uchar and v 0xff |
| 58 | end |
| 59 | |
| 60 | note RFC 1071 one's-complement checksum (64-bit accumulator, odd final byte as high octet) |
| 61 | fn net_checksum buf view uchar * off ulong n ulong to ushort |
| 62 | acc = ulong 0 |
| 63 | i = ulong 0 |
| 64 | while lt add i 1 n bound 65536 |
| 65 | w = ulong net_be16 buf add off i |
| 66 | acc = add acc w |
| 67 | i = add i 2 |
| 68 | end |
| 69 | if lt i n |
| 70 | acc = add acc shl ulong get buf add off i 8 |
| 71 | end |
| 72 | while gt shr acc 16 0 bound 8 |
| 73 | acc = add and acc 0xffff shr acc 16 |
| 74 | end |
| 75 | ret ushort xor and acc 0xffff 0xffff |
| 76 | end |
| 77 | |
| 78 | note ---- Ethernet II ---- |
| 79 | |
| 80 | fn net_eth_parse buf view uchar * n ulong hdr edit NetEthHdr to int |
| 81 | if lt n 14 |
| 82 | ret NET_ERR_TRUNCATED |
| 83 | end |
| 84 | et = net_be16 buf 12 |
| 85 | if lt et 1536 |
| 86 | ret NET_ERR_UNSUPPORTED |
| 87 | end |
| 88 | dhi = ulong net_be32 buf 0 |
| 89 | dlo = ulong net_be16 buf 4 |
| 90 | shi = ulong net_be32 buf 6 |
| 91 | slo = ulong net_be16 buf 10 |
| 92 | put hdr dst_hi dhi |
| 93 | put hdr dst_lo dlo |
| 94 | put hdr src_hi shi |
| 95 | put hdr src_lo slo |
| 96 | put hdr ethertype et |
| 97 | put hdr pay_off 14 |
| 98 | put hdr pay_len sub n 14 |
| 99 | ret NET_OK |
| 100 | end |
| 101 | |
| 102 | fn net_eth_build macs view uchar 12 et ushort pay view uchar * buf edit uchar * to ulong |
| 103 | need = add 14 len pay |
| 104 | if gt need len buf |
| 105 | ret 0 |
| 106 | end |
| 107 | for i ulong 12 |
| 108 | put buf i get macs i |
| 109 | end |
| 110 | net_put_be16 buf 12 et |
| 111 | for i ulong len pay bound 2048 |
| 112 | put buf add 14 i get pay i |
| 113 | end |
| 114 | ret need |
| 115 | end |
| 116 | |
| 117 | note ---- ARP (Ethernet / IPv4) ---- |
| 118 | |
| 119 | fn net_arp_parse buf view uchar * off ulong n ulong pkt edit NetArp to int |
| 120 | if lt n 28 |
| 121 | ret NET_ERR_TRUNCATED |
| 122 | end |
| 123 | if ne net_be16 buf off 1 |
| 124 | ret NET_ERR_UNSUPPORTED |
| 125 | end |
| 126 | if ne net_be16 buf add off 2 NET_ETHERTYPE_IPV4 |
| 127 | ret NET_ERR_UNSUPPORTED |
| 128 | end |
| 129 | if or ne get buf add off 4 6 ne get buf add off 5 4 |
| 130 | ret NET_ERR_UNSUPPORTED |
| 131 | end |
| 132 | op = net_be16 buf add off 6 |
| 133 | if and ne op NET_ARP_REQUEST ne op NET_ARP_REPLY |
| 134 | ret NET_ERR_UNSUPPORTED |
| 135 | end |
| 136 | put pkt op op |
| 137 | put pkt smac_hi ulong net_be32 buf add off 8 |
| 138 | put pkt smac_lo ulong net_be16 buf add off 12 |
| 139 | put pkt sip net_be32 buf add off 14 |
| 140 | put pkt tmac_hi ulong net_be32 buf add off 18 |
| 141 | put pkt tmac_lo ulong net_be16 buf add off 22 |
| 142 | put pkt tip net_be32 buf add off 24 |
| 143 | ret NET_OK |
| 144 | end |
| 145 | |
| 146 | fn net_arp_cache_reset to void |
| 147 | for i ulong NET_ARP_CAP |
| 148 | put net_arp_used i 0 |
| 149 | put net_arp_ip i 0 |
| 150 | put net_arp_exp i 0 |
| 151 | end |
| 152 | end |
| 153 | |
| 154 | fn net_arp_cache_learn ip uint mac view uchar 6 now ulong ttl ulong to int |
| 155 | exp = add now ttl |
| 156 | slot = ulong NET_ARP_CAP |
| 157 | for i ulong NET_ARP_CAP |
| 158 | if and ne get net_arp_used i 0 eq get net_arp_ip i ip |
| 159 | slot = i |
| 160 | break |
| 161 | end |
| 162 | end |
| 163 | if eq slot NET_ARP_CAP |
| 164 | for i ulong NET_ARP_CAP |
| 165 | if or eq get net_arp_used i 0 le get net_arp_exp i now |
| 166 | slot = i |
| 167 | break |
| 168 | end |
| 169 | end |
| 170 | end |
| 171 | if eq slot NET_ARP_CAP |
| 172 | ret NET_ERR_CAPACITY |
| 173 | end |
| 174 | put net_arp_used slot 1 |
| 175 | put net_arp_ip slot ip |
| 176 | put net_arp_exp slot exp |
| 177 | base = mul slot 6 |
| 178 | for j ulong 6 |
| 179 | put net_arp_mac add base j get mac j |
| 180 | end |
| 181 | ret NET_OK |
| 182 | end |
| 183 | |
| 184 | fn net_arp_cache_lookup ip uint now ulong mac_out edit uchar 6 to bool |
| 185 | for i ulong NET_ARP_CAP |
| 186 | if ne get net_arp_used i 0 |
| 187 | if and eq get net_arp_ip i ip gt get net_arp_exp i now |
| 188 | base = mul i 6 |
| 189 | for j ulong 6 |
| 190 | put mac_out j get net_arp_mac add base j |
| 191 | end |
| 192 | ret true |
| 193 | end |
| 194 | end |
| 195 | end |
| 196 | ret false |
| 197 | end |
| 198 | |
| 199 | note ---- IPv4 ---- |
| 200 | |
| 201 | fn net_ipv4_parse buf view uchar * off ulong n ulong hdr edit NetIpv4 to int |
| 202 | if lt n 20 |
| 203 | ret NET_ERR_TRUNCATED |
| 204 | end |
| 205 | vihl = get buf off |
| 206 | ver = shr vihl 4 |
| 207 | ihl_w = and vihl 0x0f |
| 208 | if ne ver 4 |
| 209 | ret NET_ERR_UNSUPPORTED |
| 210 | end |
| 211 | if lt ihl_w 5 |
| 212 | ret NET_ERR_INVALID |
| 213 | end |
| 214 | hlen = mul ulong ihl_w 4 |
| 215 | if gt hlen n |
| 216 | ret NET_ERR_TRUNCATED |
| 217 | end |
| 218 | tot = ulong net_be16 buf add off 2 |
| 219 | if lt tot hlen |
| 220 | ret NET_ERR_INVALID |
| 221 | end |
| 222 | if gt tot n |
| 223 | ret NET_ERR_TRUNCATED |
| 224 | end |
| 225 | if ne net_checksum buf off hlen 0 |
| 226 | ret NET_ERR_INVALID |
| 227 | end |
| 228 | put hdr ihl uchar hlen |
| 229 | put hdr dscp get buf add off 1 |
| 230 | put hdr total_len ushort tot |
| 231 | put hdr ident net_be16 buf add off 4 |
| 232 | put hdr flags_frag net_be16 buf add off 6 |
| 233 | put hdr ttl get buf add off 8 |
| 234 | put hdr proto get buf add off 9 |
| 235 | put hdr csum net_be16 buf add off 10 |
| 236 | put hdr src net_be32 buf add off 12 |
| 237 | put hdr dst net_be32 buf add off 16 |
| 238 | put hdr pay_off add off hlen |
| 239 | put hdr pay_len sub tot hlen |
| 240 | ret NET_OK |
| 241 | end |
| 242 | |
| 243 | note ip_meta: 0=src 1=dst 2=proto 3=ttl |
| 244 | fn net_ipv4_build ip_meta view uint 4 pay view uchar * buf edit uchar * to ulong |
| 245 | tot = add 20 len pay |
| 246 | if gt tot len buf |
| 247 | ret 0 |
| 248 | end |
| 249 | put buf 0 0x45 |
| 250 | put buf 1 0 |
| 251 | net_put_be16 buf 2 ushort tot |
| 252 | net_put_be16 buf 4 0 |
| 253 | net_put_be16 buf 6 0 |
| 254 | put buf 8 uchar get ip_meta 3 |
| 255 | put buf 9 uchar get ip_meta 2 |
| 256 | net_put_be16 buf 10 0 |
| 257 | net_put_be32 buf 12 get ip_meta 0 |
| 258 | net_put_be32 buf 16 get ip_meta 1 |
| 259 | cs = net_checksum buf 0 20 |
| 260 | net_put_be16 buf 10 cs |
| 261 | for i ulong len pay bound 2048 |
| 262 | put buf add 20 i get pay i |
| 263 | end |
| 264 | ret tot |
| 265 | end |
| 266 | |
| 267 | note ---- UDP ---- |
| 268 | |
| 269 | fn net_udp_parse buf view uchar * off ulong n ulong hdr edit NetUdp to int |
| 270 | if lt n 8 |
| 271 | ret NET_ERR_TRUNCATED |
| 272 | end |
| 273 | ul = ulong net_be16 buf add off 4 |
| 274 | if lt ul 8 |
| 275 | ret NET_ERR_INVALID |
| 276 | end |
| 277 | if gt ul n |
| 278 | ret NET_ERR_TRUNCATED |
| 279 | end |
| 280 | put hdr sport net_be16 buf off |
| 281 | put hdr dport net_be16 buf add off 2 |
| 282 | put hdr ulen ushort ul |
| 283 | put hdr csum net_be16 buf add off 6 |
| 284 | put hdr pay_off add off 8 |
| 285 | put hdr pay_len sub ul 8 |
| 286 | ret NET_OK |
| 287 | end |
| 288 | |
| 289 | fn net_udp_build sport ushort dport ushort pay view uchar * buf edit uchar * to ulong |
| 290 | ul = add 8 len pay |
| 291 | if gt ul len buf |
| 292 | ret 0 |
| 293 | end |
| 294 | net_put_be16 buf 0 sport |
| 295 | net_put_be16 buf 2 dport |
| 296 | net_put_be16 buf 4 ushort ul |
| 297 | net_put_be16 buf 6 0 |
| 298 | for i ulong len pay bound 2048 |
| 299 | put buf add 8 i get pay i |
| 300 | end |
| 301 | ret ul |
| 302 | end |
| 303 | |
| 304 | note ---- Boot Report Framing (aienos_report) ---- |
| 305 | |
| 306 | fn net_report_parts n ulong to ulong |
| 307 | if or eq n 0 gt n NET_REPORT_MAX |
| 308 | ret 0 |
| 309 | end |
| 310 | ret div sub add n NET_REPORT_CHUNK 1 NET_REPORT_CHUNK |
| 311 | end |
| 312 | |
| 313 | fn net_report_wrap_eth mac view uchar 6 ubuf view uchar * dst edit uchar * to ulong |
| 314 | ip_meta = new uint 4 0 |
| 315 | put ip_meta 0 0 |
| 316 | put ip_meta 1 0xffffffff |
| 317 | put ip_meta 2 uint NET_IPPROTO_UDP |
| 318 | put ip_meta 3 1 |
| 319 | ipbuf = new uchar 1152 0 |
| 320 | iplen = net_ipv4_build ip_meta ubuf ipbuf |
| 321 | if eq iplen 0 |
| 322 | ret 0 |
| 323 | end |
| 324 | macs = new uchar 12 0xff |
| 325 | for i ulong 6 |
| 326 | put macs add 6 i get mac i |
| 327 | end |
| 328 | ret net_eth_build macs NET_ETHERTYPE_IPV4 part ipbuf 0 iplen dst |
| 329 | end |
| 330 | |
| 331 | note rmeta: 0=run_id 1=part_idx 2=parts |
| 332 | fn net_report_build_frame mac view uchar 6 rmeta view ulong 3 chunk view uchar * dst edit uchar * to ulong |
| 333 | run_id = get rmeta 0 |
| 334 | part_idx = get rmeta 1 |
| 335 | parts = get rmeta 2 |
| 336 | if or eq parts 0 ge part_idx parts |
| 337 | ret 0 |
| 338 | end |
| 339 | if gt len chunk NET_REPORT_CHUNK |
| 340 | ret 0 |
| 341 | end |
| 342 | udp_pay = new uchar 1088 0 |
| 343 | note Binary/compact header: "ARPT" (4) + run_id (8) + part_idx (4) + parts (4) + chunk_len (4) = 24 bytes |
| 344 | put udp_pay 0 65 |
| 345 | put udp_pay 1 82 |
| 346 | put udp_pay 2 80 |
| 347 | put udp_pay 3 84 |
| 348 | net_put_be32 udp_pay 4 uint shr run_id 32 |
| 349 | net_put_be32 udp_pay 8 uint and run_id 0xffffffff |
| 350 | net_put_be32 udp_pay 12 uint part_idx |
| 351 | net_put_be32 udp_pay 16 uint parts |
| 352 | net_put_be32 udp_pay 20 uint len chunk |
| 353 | for i ulong len chunk bound 1024 |
| 354 | put udp_pay add 24 i get chunk i |
| 355 | end |
| 356 | plen = add 24 len chunk |
| 357 | ubuf = new uchar 1120 0 |
| 358 | ulen = net_udp_build NET_REPORT_PORT NET_REPORT_PORT part udp_pay 0 plen ubuf |
| 359 | if eq ulen 0 |
| 360 | ret 0 |
| 361 | end |
| 362 | ret net_report_wrap_eth mac part ubuf 0 ulen dst |
| 363 | end |
| 364 | |
| 365 | fn net_report_parse_payload frame view uchar * poff ulong plen ulong meta_out edit ulong 4 to int |
| 366 | if lt plen 24 |
| 367 | ret -7 |
| 368 | end |
| 369 | if or ne get frame poff 65 ne get frame add poff 1 82 |
| 370 | ret -8 |
| 371 | end |
| 372 | if or ne get frame add poff 2 80 ne get frame add poff 3 84 |
| 373 | ret -8 |
| 374 | end |
| 375 | rhi = ulong net_be32 frame add poff 4 |
| 376 | rlo = ulong net_be32 frame add poff 8 |
| 377 | pidx = ulong net_be32 frame add poff 12 |
| 378 | ptot = ulong net_be32 frame add poff 16 |
| 379 | clen = ulong net_be32 frame add poff 20 |
| 380 | if or eq ptot 0 ge pidx ptot |
| 381 | ret -9 |
| 382 | end |
| 383 | if ne add 24 clen plen |
| 384 | ret -9 |
| 385 | end |
| 386 | put meta_out 0 or shl rhi 32 rlo |
| 387 | put meta_out 1 pidx |
| 388 | put meta_out 2 ptot |
| 389 | put meta_out 3 clen |
| 390 | ret 0 |
| 391 | end |
| 392 | |
| 393 | fn net_report_parse_frame frame view uchar * n ulong meta_out edit ulong 4 to int |
| 394 | eh = new NetEthHdr 0 0 0 0 0 0 0 |
| 395 | if ne net_eth_parse frame n eh NET_OK |
| 396 | ret -1 |
| 397 | end |
| 398 | if ne get eh ethertype NET_ETHERTYPE_IPV4 |
| 399 | ret -2 |
| 400 | end |
| 401 | ih = new NetIpv4 0 0 0 0 0 0 0 0 0 0 0 0 |
| 402 | if ne net_ipv4_parse frame get eh pay_off get eh pay_len ih NET_OK |
| 403 | ret -3 |
| 404 | end |
| 405 | if ne get ih proto NET_IPPROTO_UDP |
| 406 | ret -4 |
| 407 | end |
| 408 | uh = new NetUdp 0 0 0 0 0 0 |
| 409 | if ne net_udp_parse frame get ih pay_off get ih pay_len uh NET_OK |
| 410 | ret -5 |
| 411 | end |
| 412 | if or ne get uh sport NET_REPORT_PORT ne get uh dport NET_REPORT_PORT |
| 413 | ret -6 |
| 414 | end |
| 415 | ret net_report_parse_payload frame get uh pay_off get uh pay_len meta_out |
| 416 | end |