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 / aienos_ctl.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* AIENOS M6-A framed control-transport STUB, TEST identity only. |
| 18 | * |
| 19 | * There is no Rust reference for this layer; this file is its spec. It is a |
| 20 | * reliable, ordered, authenticated message channel carried in UDP payloads, |
| 21 | * driven entirely by the caller: the caller supplies the clock (ticks), puts |
| 22 | * frames on the wire and hands received frames back. No heap, clock or I/O. |
| 23 | * |
| 24 | * Identity: ONLY the published TEST identity exists here. Its key is derived |
| 25 | * from a public label, so it authenticates nothing against a real attacker; it |
| 26 | * exercises framing, loss recovery and refusal paths. Production identity and |
| 27 | * keys wait for M5 (key hierarchy), and native binding waits for a C kernel. |
| 28 | * |
| 29 | * Frame (big-endian), total = 24 + payload_len + 16: |
| 30 | * 0 magic "ACT1" 4 version (1) 5 type (1 DATA, 2 ACK) |
| 31 | * 6 sender role (0/1) 7 identity kind (0x54 TEST) |
| 32 | * 8 session u32 12 seq u32 16 ack u32 (next seq expected) |
| 33 | * 20 payload_len u16 22 reserved u16 (0) |
| 34 | * 24 payload .. tag: HMAC-SHA256(key, frame[0..24+len])[0..16] |
| 35 | * ACK frames carry seq 0 and no payload. DATA frames piggyback ack. |
| 36 | * Sender: up to CT_WINDOW unacknowledged messages, per-message retransmit: |
| 37 | * the first after exactly rto ticks, each later one after rto plus a |
| 38 | * deterministic jitter in [0, rto] (from session, role, seq, try count) so a |
| 39 | * periodic loss pattern cannot lock onto one frame. Link declared down after |
| 40 | * max_retries retransmissions. |
| 41 | * Receiver: delivers only the next expected seq (in order, exactly once); |
| 42 | * duplicates and gaps are not delivered and trigger a cumulative ACK. |
| 43 | */ |
| 44 | #ifndef AIENOS_CTL_H |
| 45 | #define AIENOS_CTL_H |
| 46 | |
| 47 | #include <stddef.h> |
| 48 | #include <stdint.h> |
| 49 | |
| 50 | #define CT_HEADER_LEN 24u |
| 51 | #define CT_TAG_LEN 16u |
| 52 | #define CT_MAX_PAYLOAD 1024u |
| 53 | #define CT_MAX_FRAME (CT_HEADER_LEN + CT_MAX_PAYLOAD + CT_TAG_LEN) |
| 54 | #define CT_WINDOW 4u |
| 55 | #define CT_VERSION 1u |
| 56 | #define CT_TYPE_DATA 1u |
| 57 | #define CT_TYPE_ACK 2u |
| 58 | #define CT_IDENTITY_TEST 0x54u |
| 59 | |
| 60 | typedef enum { |
| 61 | CT_OK = 0, /* frame accepted (ack, duplicate or gap) / frame produced */ |
| 62 | CT_DELIVERED = 1, /* receive: one in-order message copied out */ |
| 63 | CT_IDLE = 2, /* poll: nothing to send now */ |
| 64 | CT_ERR_ARG = -1, |
| 65 | CT_ERR_MALFORMED = -2, /* bad magic/version/type/lengths/reserved fields */ |
| 66 | CT_ERR_BAD_TAG = -3, /* authentication tag mismatch */ |
| 67 | CT_ERR_REFUSED = -4, /* wrong identity kind, session or sender role */ |
| 68 | CT_ERR_BAD_ACK = -5, /* ack outside the sent range */ |
| 69 | CT_ERR_CAPACITY = -6, /* output buffer too small */ |
| 70 | CT_ERR_TOO_LARGE = -7, /* message longer than CT_MAX_PAYLOAD */ |
| 71 | CT_ERR_WINDOW_FULL = -8, |
| 72 | CT_ERR_LINK_DOWN = -9, /* max retries exhausted; endpoint is dead */ |
| 73 | CT_ERR_SEQ_EXHAUSTED = -10, /* sequence space used up; needs a new session */ |
| 74 | } ct_status; |
| 75 | |
| 76 | typedef struct { |
| 77 | uint8_t kind; /* always CT_IDENTITY_TEST */ |
| 78 | uint8_t key[32]; |
| 79 | } ct_identity; |
| 80 | |
| 81 | typedef struct { |
| 82 | uint8_t used, sent; |
| 83 | uint16_t len; |
| 84 | uint32_t seq; |
| 85 | uint32_t tries; |
| 86 | uint64_t last_tx; |
| 87 | uint8_t data[CT_MAX_PAYLOAD]; |
| 88 | } ct_slot; |
| 89 | |
| 90 | typedef struct { |
| 91 | uint64_t tx_frames, tx_retransmits, tx_acks; |
| 92 | uint64_t rx_delivered, rx_duplicate, rx_gap, rx_ack; |
| 93 | uint64_t rx_malformed, rx_bad_tag, rx_refused, rx_bad_ack; |
| 94 | } ct_counters; |
| 95 | |
| 96 | typedef struct { |
| 97 | uint8_t key[32]; |
| 98 | uint8_t role; |
| 99 | uint8_t link_down; |
| 100 | uint8_t ack_pending; |
| 101 | uint32_t session; |
| 102 | uint32_t rto; |
| 103 | uint32_t max_retries; |
| 104 | uint32_t next_seq; /* sender: next seq to assign */ |
| 105 | uint32_t base; /* sender: oldest unacknowledged seq */ |
| 106 | uint32_t expected; /* receiver: next seq to deliver */ |
| 107 | ct_slot slots[CT_WINDOW]; |
| 108 | ct_counters c; |
| 109 | } ct_endpoint; |
| 110 | |
| 111 | /* Derive the public TEST identity for a label. Never use for production. */ |
| 112 | ct_status ct_test_identity(ct_identity *id, const char *label); |
| 113 | |
| 114 | ct_status ct_init(ct_endpoint *ep, const ct_identity *id, uint8_t role, uint32_t session, |
| 115 | uint32_t rto, uint32_t max_retries); |
| 116 | /* Queue one message for reliable, ordered delivery. */ |
| 117 | ct_status ct_send(ct_endpoint *ep, const uint8_t *msg, size_t len); |
| 118 | /* Produce the next frame to put on the wire (CT_OK, *written > 0) or CT_IDLE. */ |
| 119 | ct_status ct_poll_tx(ct_endpoint *ep, uint64_t now, uint8_t *out, size_t cap, size_t *written); |
| 120 | /* Process one received frame. CT_DELIVERED copies one message into msg. */ |
| 121 | ct_status ct_receive(ct_endpoint *ep, const uint8_t *frame, size_t len, uint8_t *msg, size_t cap, |
| 122 | size_t *msg_len); |
| 123 | /* Number of queued, unacknowledged messages. */ |
| 124 | uint32_t ct_in_flight(const ct_endpoint *ep); |
| 125 | const char *ct_status_name(ct_status s); |
| 126 | |
| 127 | #endif |