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 / tests / net_test.c
| 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 | /* Host tests for the M6-A protocol layer: the Rust reference's own vectors, |
| 18 | * malformed-packet negatives, build/parse round trips, and a seeded |
| 19 | * randomized hostile-input sweep. Usage: net_test [seed] [iterations] */ |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include "../aienos_net.h" |
| 25 | #include "../aienos_virtio_pci.h" |
| 26 | |
| 27 | static int failures, checks; |
| 28 | #define CHECK(c) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0) |
| 29 | #define EQ(a, b) CHECK((long long)(a) == (long long)(b)) |
| 30 | |
| 31 | static uint64_t rng; |
| 32 | static uint64_t next(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return rng; } |
| 33 | |
| 34 | static void fix_ip_csum(uint8_t *h, size_t hl) |
| 35 | { |
| 36 | h[10] = h[11] = 0; |
| 37 | uint16_t c = net_checksum(h, hl); |
| 38 | h[10] = (uint8_t)(c >> 8); h[11] = (uint8_t)c; |
| 39 | } |
| 40 | |
| 41 | /* ---------------- vectors ported from net.rs #[cfg(test)] ---------------- */ |
| 42 | static void test_rust_vectors(void) |
| 43 | { |
| 44 | const uint8_t v[] = {0, 1, 0xf2, 3, 0xf4, 0xf5, 0xf6, 0xf7}; |
| 45 | EQ(net_checksum(v, sizeof v), 0x220d); |
| 46 | const uint8_t one[] = {1}; |
| 47 | EQ(net_checksum(one, 1), 0xfeff); |
| 48 | |
| 49 | const uint8_t req[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x52, 0x54, 0, 0x12, 0x34, 0x56, |
| 50 | 0x08, 0x06, 0, 1, 8, 0, 6, 4, 0, 1, 0x52, 0x54, 0, 0x12, 0x34, 0x56, |
| 51 | 192, 168, 1, 10, 0, 0, 0, 0, 0, 0, 192, 168, 1, 1}; |
| 52 | net_eth_frame f; |
| 53 | EQ(net_eth_parse(req, sizeof req, &f), NET_OK); |
| 54 | EQ(f.ethertype, NET_ETHERTYPE_ARP); |
| 55 | net_arp_packet a; |
| 56 | EQ(net_arp_parse(f.payload, f.payload_len, &a), NET_OK); |
| 57 | EQ(a.operation, NET_ARP_REQUEST); |
| 58 | uint8_t out[64]; size_t w = 0; |
| 59 | EQ(net_arp_build(&a, out, 28, &w), NET_OK); |
| 60 | EQ(w, 28); |
| 61 | CHECK(memcmp(out, req + 14, 28) == 0); |
| 62 | EQ(net_eth_build(&f, out, sizeof out, &w), NET_OK); |
| 63 | EQ(w, sizeof req); |
| 64 | CHECK(memcmp(out, req, sizeof req) == 0); |
| 65 | |
| 66 | const uint8_t p[] = {0x45, 0, 0, 0x1c, 0, 1, 0, 0, 64, 1, 0x8e, 0xa9, 192, 0, 2, 1, |
| 67 | 198, 51, 100, 2, 8, 0, 0xe5, 0xca, 0x12, 0x34, 0, 1}; |
| 68 | net_ipv4_header h; const uint8_t *body; size_t blen; |
| 69 | EQ(net_ipv4_parse(p, sizeof p, &h, &body, &blen), NET_OK); |
| 70 | EQ(h.protocol, 1); |
| 71 | net_icmp_echo e; |
| 72 | EQ(net_icmp_echo_parse(body, blen, &e), NET_OK); |
| 73 | EQ(e.identifier, 0x1234); |
| 74 | EQ(e.reply, 0); |
| 75 | EQ(net_ipv4_build(&h, body, blen, out, 28, &w), NET_OK); |
| 76 | EQ(w, 28); |
| 77 | CHECK(memcmp(out, p, 28) == 0); |
| 78 | uint8_t ic[8]; |
| 79 | EQ(net_icmp_echo_build(&e, NULL, 0, ic, sizeof ic, &w), NET_OK); |
| 80 | CHECK(memcmp(ic, p + 20, 8) == 0); |
| 81 | |
| 82 | net_ipv4 src = {{192, 0, 2, 1}}, dst = {{198, 51, 100, 2}}; |
| 83 | net_udp_header u = {1234, 53}; |
| 84 | uint8_t ub[11]; |
| 85 | EQ(net_udp_build(&u, (const uint8_t *)"abc", 3, src, dst, ub, sizeof ub, &w), NET_OK); |
| 86 | EQ(ub[6], 0x4a); EQ(ub[7], 0x37); |
| 87 | net_udp_header u2; const uint8_t *up; size_t ul; |
| 88 | EQ(net_udp_parse(ub, sizeof ub, src, dst, &u2, &up, &ul), NET_OK); |
| 89 | EQ(ul, 3); CHECK(memcmp(up, "abc", 3) == 0); |
| 90 | EQ(u2.source_port, 1234); EQ(u2.destination_port, 53); |
| 91 | |
| 92 | EQ(net_eth_parse(NULL, 0, &f), NET_ERR_TRUNCATED); |
| 93 | uint8_t z[27] = {0}; |
| 94 | EQ(net_arp_parse(z, 27, &a), NET_ERR_TRUNCATED); |
| 95 | uint8_t z20[20] = {0}; |
| 96 | EQ(net_ipv4_parse(z20, 20, &h, &body, &blen), NET_ERR_INVALID); |
| 97 | uint8_t fr[20] = {0x45, 0, 0, 20, 0, 0, 0x20, 0, 64, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 98 | fix_ip_csum(fr, 20); |
| 99 | EQ(net_ipv4_parse(fr, 20, &h, &body, &blen), NET_ERR_UNSUPPORTED); |
| 100 | |
| 101 | uint8_t rs[20] = {0x45, 0, 0, 20, 0x12, 0x34, 0x80, 0, 64, 17, 0, 0, 10, 0, 0, 1, 10, 0, 0, 2}; |
| 102 | fix_ip_csum(rs, 20); |
| 103 | EQ(net_ipv4_parse(rs, 20, &h, &body, &blen), NET_ERR_INVALID); |
| 104 | rs[6] = 0x40; /* don't-fragment alone stays valid */ |
| 105 | fix_ip_csum(rs, 20); |
| 106 | EQ(net_ipv4_parse(rs, 20, &h, &body, &blen), NET_OK); |
| 107 | } |
| 108 | |
| 109 | static void test_arp_cache(void) |
| 110 | { |
| 111 | net_arp_cache c; |
| 112 | EQ(net_arp_cache_init(&c, 0), NET_ERR_CAPACITY); |
| 113 | EQ(net_arp_cache_init(&c, NET_ARP_CACHE_MAX + 1), NET_ERR_CAPACITY); |
| 114 | EQ(net_arp_cache_init(&c, 1), NET_OK); |
| 115 | net_arp_packet p = {2, {{1, 1, 1, 1, 1, 1}}, {{1, 1, 1, 1}}, {{0}}, {{2, 2, 2, 2}}}; |
| 116 | net_mac m; |
| 117 | EQ(net_arp_cache_learn(&c, &p, 0, 0, 0, 5), NET_ERR_INVALID); /* unsolicited refused */ |
| 118 | EQ(net_arp_cache_learn(&c, &p, 1, 0, 0, 5), NET_OK); |
| 119 | EQ(net_arp_cache_lookup(&c, p.sender_ip, 4, &m), 1); |
| 120 | CHECK(memcmp(m.b, p.sender_mac.b, 6) == 0); |
| 121 | EQ(net_arp_cache_lookup(&c, p.sender_ip, 5, &m), 0); /* expired at exactly ttl */ |
| 122 | /* full cache: a different sender is refused until expiry frees the slot */ |
| 123 | EQ(net_arp_cache_learn(&c, &p, 1, 0, 10, 5), NET_OK); |
| 124 | net_arp_packet q = p; q.sender_ip.b[3] = 9; |
| 125 | EQ(net_arp_cache_learn(&c, &q, 1, 0, 11, 5), NET_ERR_CAPACITY); |
| 126 | EQ(net_arp_cache_learn(&c, &q, 1, 0, 15, 5), NET_OK); /* expired slot is reclaimed by learn */ |
| 127 | EQ(net_arp_cache_lookup(&c, q.sender_ip, 16, &m), 1); |
| 128 | EQ(net_arp_cache_lookup(&c, p.sender_ip, 16, &m), 0); |
| 129 | EQ(net_arp_cache_learn(&c, &p, 1, 0, 20, 5), NET_OK); /* q expired at 20 */ |
| 130 | /* same IP re-learn replaces in place (spoofed reply would overwrite: caller must gate) */ |
| 131 | p.sender_mac.b[0] = 7; |
| 132 | EQ(net_arp_cache_learn(&c, &p, 1, 0, 12, 5), NET_OK); |
| 133 | EQ(net_arp_cache_lookup(&c, p.sender_ip, 13, &m), 1); |
| 134 | EQ(m.b[0], 7); |
| 135 | /* ttl saturates instead of wrapping */ |
| 136 | EQ(net_arp_cache_learn(&c, &p, 0, 1, UINT64_MAX - 1, 100), NET_OK); |
| 137 | EQ(net_arp_cache_lookup(&c, p.sender_ip, UINT64_MAX - 1, &m), 1); |
| 138 | } |
| 139 | |
| 140 | /* ---------------- malformed-packet negatives ---------------- */ |
| 141 | static void test_malformed(void) |
| 142 | { |
| 143 | net_eth_frame f; net_arp_packet a; net_ipv4_header h; const uint8_t *pl; size_t pll; |
| 144 | net_icmp_echo e; net_udp_header u; |
| 145 | uint8_t b[128]; |
| 146 | for (size_t n = 0; n < 14; n++) EQ(net_eth_parse(b, n, &f), NET_ERR_TRUNCATED); |
| 147 | /* ARP: every truncation, bad htype/ptype/hlen/plen, bad op */ |
| 148 | uint8_t arp[28] = {0, 1, 8, 0, 6, 4, 0, 2}; |
| 149 | for (size_t n = 0; n < 28; n++) EQ(net_arp_parse(arp, n, &a), NET_ERR_TRUNCATED); |
| 150 | EQ(net_arp_parse(arp, 28, &a), NET_OK); |
| 151 | for (int i = 0; i < 6; i++) { |
| 152 | uint8_t x[28]; memcpy(x, arp, 28); x[i] ^= 0x10; |
| 153 | EQ(net_arp_parse(x, 28, &a), NET_ERR_INVALID); |
| 154 | } |
| 155 | uint8_t ops[] = {0, 3, 0xff}; |
| 156 | for (size_t i = 0; i < sizeof ops; i++) { |
| 157 | uint8_t x[28]; memcpy(x, arp, 28); x[7] = ops[i]; |
| 158 | EQ(net_arp_parse(x, 28, &a), NET_ERR_UNSUPPORTED); |
| 159 | } |
| 160 | /* IPv4 */ |
| 161 | uint8_t ip[40] = {0x45, 0, 0, 28, 0, 7, 0, 0, 64, 17, 0, 0, 10, 0, 0, 1, 10, 0, 0, 2}; |
| 162 | fix_ip_csum(ip, 20); |
| 163 | EQ(net_ipv4_parse(ip, 28, &h, &pl, &pll), NET_OK); |
| 164 | EQ(pll, 8); |
| 165 | for (size_t n = 0; n < 20; n++) EQ(net_ipv4_parse(ip, n, &h, &pl, &pll), NET_ERR_TRUNCATED); |
| 166 | EQ(net_ipv4_parse(ip, 27, &h, &pl, &pll), NET_ERR_TRUNCATED); /* total > buffer */ |
| 167 | memcpy(b, ip, 40); b[3] = 19; fix_ip_csum(b, 20); /* total < ihl */ |
| 168 | EQ(net_ipv4_parse(b, 40, &h, &pl, &pll), NET_ERR_TRUNCATED); |
| 169 | memcpy(b, ip, 40); b[0] = 0x65; fix_ip_csum(b, 20); /* version 6 */ |
| 170 | EQ(net_ipv4_parse(b, 40, &h, &pl, &pll), NET_ERR_INVALID); |
| 171 | for (uint8_t ihl = 0; ihl < 5; ihl++) { /* ihl < 5 */ |
| 172 | memcpy(b, ip, 40); b[0] = (uint8_t)(0x40 | ihl); |
| 173 | EQ(net_ipv4_parse(b, 40, &h, &pl, &pll), NET_ERR_INVALID); |
| 174 | } |
| 175 | memcpy(b, ip, 40); b[0] = 0x4f; /* ihl 60 > buffer */ |
| 176 | EQ(net_ipv4_parse(b, 40, &h, &pl, &pll), NET_ERR_INVALID); |
| 177 | memcpy(b, ip, 40); b[8] = 0; fix_ip_csum(b, 20); /* ttl 0 */ |
| 178 | EQ(net_ipv4_parse(b, 40, &h, &pl, &pll), NET_ERR_INVALID); |
| 179 | memcpy(b, ip, 40); b[11] ^= 1; /* bad checksum */ |
| 180 | EQ(net_ipv4_parse(b, 40, &h, &pl, &pll), NET_ERR_INVALID); |
| 181 | memcpy(b, ip, 40); b[7] = 1; fix_ip_csum(b, 20); /* fragment offset */ |
| 182 | EQ(net_ipv4_parse(b, 40, &h, &pl, &pll), NET_ERR_UNSUPPORTED); |
| 183 | /* options (ihl 6) are accepted and skipped; payload stops at total length */ |
| 184 | uint8_t o[32] = {0x46, 0, 0, 30, 0, 0, 0x40, 0, 1, 17, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 1, 1, 0}; |
| 185 | fix_ip_csum(o, 24); |
| 186 | EQ(net_ipv4_parse(o, 32, &h, &pl, &pll), NET_OK); |
| 187 | CHECK(pl == o + 24 && pll == 6); |
| 188 | /* oversize builds */ |
| 189 | static uint8_t big[70000]; |
| 190 | size_t w; |
| 191 | net_ipv4_header hh = {0, 0, 64, 17, {{0}}, {{0}}}; |
| 192 | EQ(net_ipv4_build(&hh, big, 65535 - 19, big, sizeof big, &w), NET_ERR_TRUNCATED); |
| 193 | EQ(net_ipv4_build(&hh, NULL, 0, b, 19, &w), NET_ERR_TRUNCATED); |
| 194 | net_udp_header uh = {1, 2}; |
| 195 | net_ipv4 s = {{1, 2, 3, 4}}, d = {{5, 6, 7, 8}}; |
| 196 | EQ(net_udp_build(&uh, big, 65535 - 7, s, d, big, sizeof big, &w), NET_ERR_TRUNCATED); |
| 197 | EQ(net_udp_build(&uh, big + 100, 65535 - 8, s, d, big, sizeof big, &w), NET_OK); |
| 198 | EQ(w, 65535); |
| 199 | /* ICMP */ |
| 200 | uint8_t ic[16]; |
| 201 | net_icmp_echo ee = {1, 9, 9, 0}; |
| 202 | EQ(net_icmp_echo_build(&ee, (const uint8_t *)"12345678", 8, ic, sizeof ic, &w), NET_OK); |
| 203 | EQ(net_icmp_echo_parse(ic, 16, &e), NET_OK); |
| 204 | for (size_t n = 0; n < 8; n++) EQ(net_icmp_echo_parse(ic, n, &e), NET_ERR_TRUNCATED); |
| 205 | EQ(net_icmp_echo_parse(ic, 15, &e), NET_ERR_INVALID); /* length lie breaks checksum */ |
| 206 | memcpy(b, ic, 16); b[1] = 1; |
| 207 | EQ(net_icmp_echo_parse(b, 16, &e), NET_ERR_UNSUPPORTED); |
| 208 | memcpy(b, ic, 16); b[0] = 3; |
| 209 | EQ(net_icmp_echo_parse(b, 16, &e), NET_ERR_UNSUPPORTED); |
| 210 | memcpy(b, ic, 16); b[12] ^= 0x80; |
| 211 | EQ(net_icmp_echo_parse(b, 16, &e), NET_ERR_INVALID); |
| 212 | /* UDP */ |
| 213 | uint8_t ud[16]; |
| 214 | EQ(net_udp_build(&uh, (const uint8_t *)"hostile", 7, s, d, ud, sizeof ud, &w), NET_OK); |
| 215 | EQ(w, 15); |
| 216 | for (size_t n = 0; n < 8; n++) EQ(net_udp_parse(ud, n, s, d, &u, &pl, &pll), NET_ERR_TRUNCATED); |
| 217 | EQ(net_udp_parse(ud, 14, s, d, &u, &pl, &pll), NET_ERR_INVALID); /* length field > buffer */ |
| 218 | memcpy(b, ud, 15); b[5] = 7; /* length < 8 */ |
| 219 | EQ(net_udp_parse(b, 15, s, d, &u, &pl, &pll), NET_ERR_INVALID); |
| 220 | memcpy(b, ud, 15); b[14] ^= 1; /* payload corrupted */ |
| 221 | EQ(net_udp_parse(b, 15, s, d, &u, &pl, &pll), NET_ERR_INVALID); |
| 222 | net_ipv4 s2 = {{1, 2, 3, 5}}; /* wrong pseudo-header */ |
| 223 | EQ(net_udp_parse(ud, 15, s2, d, &u, &pl, &pll), NET_ERR_INVALID); |
| 224 | memcpy(b, ud, 15); b[6] = b[7] = 0; /* checksum 0 = absent */ |
| 225 | EQ(net_udp_parse(b, 15, s2, d, &u, &pl, &pll), NET_OK); |
| 226 | memcpy(b, ud, 15); memcpy(b + 15, "XYZ", 3); /* trailing bytes ignored */ |
| 227 | EQ(net_udp_parse(b, 18, s, d, &u, &pl, &pll), NET_OK); |
| 228 | EQ(pll, 7); |
| 229 | /* null/argument misuse never dereferences */ |
| 230 | EQ(net_eth_parse(NULL, 5, &f), NET_ERR_INVALID); |
| 231 | EQ(net_ipv4_parse(ip, 28, NULL, &pl, &pll), NET_ERR_INVALID); |
| 232 | EQ(net_eth_build(NULL, b, 10, &w), NET_ERR_INVALID); |
| 233 | EQ(net_checksum(NULL, 99), 0xffff); |
| 234 | } |
| 235 | |
| 236 | /* ---------------- virtio PCI capability walk ---------------- */ |
| 237 | static void put32(uint8_t *c, size_t o, uint32_t v) { c[o] = (uint8_t)v; c[o + 1] = (uint8_t)(v >> 8); c[o + 2] = (uint8_t)(v >> 16); c[o + 3] = (uint8_t)(v >> 24); } |
| 238 | static void put_cap(uint8_t *c, size_t at, uint8_t nx, uint8_t kind, uint8_t bar, uint32_t off, uint32_t len) |
| 239 | { |
| 240 | size_t cl = kind == 2 ? 20 : 16; |
| 241 | memset(c + at, 0, cl); |
| 242 | c[at] = 9; c[at + 1] = nx; c[at + 2] = (uint8_t)cl; c[at + 3] = kind; c[at + 4] = bar; |
| 243 | put32(c, at + 8, off); put32(c, at + 12, len); |
| 244 | if (kind == 2) put32(c, at + 16, 4); |
| 245 | } |
| 246 | static void test_virtio(void) |
| 247 | { |
| 248 | uint8_t c[256] = {0}; |
| 249 | virtio_pci_caps r; |
| 250 | c[6] = 1 << 4; c[0x34] = 0x40; |
| 251 | put_cap(c, 0x40, 0x50, 1, 0, 0x1000, 0x100); |
| 252 | put_cap(c, 0x50, 0x64, 2, 2, 0x2000, 0x100); |
| 253 | put_cap(c, 0x64, 0x74, 3, 0, 0x3000, 1); |
| 254 | put_cap(c, 0x74, 0, 4, 0, 0x4000, 0x80); |
| 255 | EQ(virtio_pci_parse_caps(c, 256, &r), VIRTIO_PCI_OK); |
| 256 | CHECK(r.common_cfg.present && r.common_cfg.bar == 0 && r.common_cfg.offset == 0x1000 && r.common_cfg.length == 0x100); |
| 257 | CHECK(r.notify_cfg.present && r.notify_cfg.bar == 2 && r.notify_cfg.offset == 0x2000); |
| 258 | CHECK(r.has_notify_off_multiplier && r.notify_off_multiplier == 4); |
| 259 | CHECK(r.isr_cfg.present && r.isr_cfg.length == 1); |
| 260 | CHECK(r.device_cfg.present && r.device_cfg.offset == 0x4000 && r.device_cfg.length == 0x80); |
| 261 | uint8_t big[4096] = {0}; |
| 262 | memcpy(big, c, 256); |
| 263 | EQ(virtio_pci_parse_caps(big, 4096, &r), VIRTIO_PCI_OK); |
| 264 | EQ(virtio_pci_parse_caps(c, 255, &r), VIRTIO_PCI_INVALID_CONFIG_LENGTH); |
| 265 | EQ(virtio_pci_parse_caps(NULL, 256, &r), VIRTIO_PCI_INVALID_CONFIG_LENGTH); |
| 266 | |
| 267 | uint8_t x[256]; |
| 268 | memcpy(x, c, 256); x[6] = 0; |
| 269 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_CAPABILITIES_NOT_PRESENT); |
| 270 | memcpy(x, c, 256); x[0x34] = 0x3c; /* pointer into header */ |
| 271 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_POINTER); |
| 272 | memcpy(x, c, 256); x[0x41] = 0x40; /* self loop */ |
| 273 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_CAPABILITY_LOOP); |
| 274 | memcpy(x, c, 256); x[0x75] = 0x50; /* long loop back to 0x50 */ |
| 275 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_CAPABILITY_LOOP); |
| 276 | memcpy(x, c, 256); x[0x41] = 0; x[0x42] = 8; /* short cap (Rust vector) */ |
| 277 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_SHORT_CAPABILITY); |
| 278 | memcpy(x, c, 256); x[0x52] = 16; /* notify cap shorter than 20 */ |
| 279 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_SHORT_CAPABILITY); |
| 280 | memset(x, 0, 256); x[6] = 16; x[0x34] = 0xf8; x[0xf8] = 9; /* cap runs off the end */ |
| 281 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_SHORT_CAPABILITY); |
| 282 | memset(x, 0, 256); x[6] = 16; x[0x34] = 0xfc; x[0xfc] = 1; /* non-vendor cap at end is fine */ |
| 283 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_OK); |
| 284 | memcpy(x, c, 256); x[0x44] = 6; /* bar > 5 */ |
| 285 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); |
| 286 | memcpy(x, c, 256); put32(x, 0x4c, 0); /* zero length */ |
| 287 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); |
| 288 | memcpy(x, c, 256); put32(x, 0x48, 0xffffff00); /* offset + length overflows */ |
| 289 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); |
| 290 | memcpy(x, c, 256); x[0x77] = 1; /* second common cfg */ |
| 291 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_DUPLICATE_CAPABILITY); |
| 292 | /* the longest loop-free chain: all 48 aligned slots 0x40..0xfc. The 48-step |
| 293 | bound in the reference is therefore unreachable without a revisit. */ |
| 294 | memset(x, 0, 256); x[6] = 16; x[0x34] = 0x40; |
| 295 | for (int i = 0; i < 48; i++) { size_t at = 0x40 + 4 * (size_t)i; x[at] = 1; x[at + 1] = (uint8_t)(i == 47 ? 0 : at + 4); } |
| 296 | EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_OK); |
| 297 | } |
| 298 | |
| 299 | /* ---------------- round trips ---------------- */ |
| 300 | static void test_roundtrips(unsigned iters) |
| 301 | { |
| 302 | static uint8_t data[2000], pkt[2100], frame[2200]; |
| 303 | for (unsigned it = 0; it < iters; it++) { |
| 304 | size_t dl = next() % 1400; |
| 305 | for (size_t i = 0; i < dl; i++) data[i] = (uint8_t)next(); |
| 306 | net_ipv4 s = {{(uint8_t)next(), (uint8_t)next(), (uint8_t)next(), (uint8_t)next()}}; |
| 307 | net_ipv4 d = {{(uint8_t)next(), (uint8_t)next(), (uint8_t)next(), (uint8_t)next()}}; |
| 308 | net_udp_header u = {(uint16_t)next(), (uint16_t)next()}; |
| 309 | size_t ul, il, fl; |
| 310 | EQ(net_udp_build(&u, data, dl, s, d, pkt + 20, sizeof pkt - 20, &ul), NET_OK); |
| 311 | net_ipv4_header h = {(uint8_t)next(), (uint16_t)next(), (uint8_t)(1 + next() % 255), NET_IPPROTO_UDP, s, d}; |
| 312 | EQ(net_ipv4_build(&h, pkt + 20, ul, pkt, sizeof pkt, &il), NET_OK); |
| 313 | net_eth_frame f = {{{1, 2, 3, 4, 5, 6}}, {{6, 5, 4, 3, 2, 1}}, NET_ETHERTYPE_IPV4, pkt, il}; |
| 314 | EQ(net_eth_build(&f, frame, sizeof frame, &fl), NET_OK); |
| 315 | net_eth_frame f2; net_ipv4_header h2; net_udp_header u2; |
| 316 | const uint8_t *ip, *up; size_t ipl, upl; |
| 317 | EQ(net_eth_parse(frame, fl, &f2), NET_OK); |
| 318 | EQ(net_ipv4_parse(f2.payload, f2.payload_len, &h2, &ip, &ipl), NET_OK); |
| 319 | CHECK(memcmp(&h, &h2, sizeof h) == 0); |
| 320 | EQ(net_udp_parse(ip, ipl, h2.source, h2.destination, &u2, &up, &upl), NET_OK); |
| 321 | CHECK(upl == dl && (dl == 0 || memcmp(up, data, dl) == 0)); |
| 322 | CHECK(u2.source_port == u.source_port && u2.destination_port == u.destination_port); |
| 323 | /* every single-bit flip in the IP header or UDP datagram is caught or changes nothing visible */ |
| 324 | size_t bit = next() % ((fl - 14) * 8); |
| 325 | frame[14 + bit / 8] ^= (uint8_t)(1u << (bit % 8)); |
| 326 | net_err e = net_ipv4_parse(frame + 14, fl - 14, &h2, &ip, &ipl); |
| 327 | if (e == NET_OK) e = net_udp_parse(ip, ipl, h2.source, h2.destination, &u2, &up, &upl); |
| 328 | CHECK(e != NET_OK || (ip[6] == 0 && ip[7] == 0)); /* only a flip to "no checksum" passes */ |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* ---------------- randomized hostile inputs: no crash, views stay in bounds ---------------- */ |
| 333 | static void test_hostile(unsigned iters) |
| 334 | { |
| 335 | static uint8_t b[4096]; |
| 336 | for (unsigned it = 0; it < iters; it++) { |
| 337 | size_t n = next() % 1600; |
| 338 | for (size_t i = 0; i < n; i++) b[i] = (uint8_t)next(); |
| 339 | if (n > 20 && (it & 1)) { /* make it look like IPv4 more often */ |
| 340 | b[0] = (uint8_t)(0x45 + (next() % 3 == 0 ? next() % 11 : 0)); |
| 341 | if (next() & 1) { b[2] = (uint8_t)(n >> 8); b[3] = (uint8_t)n; } |
| 342 | b[6] &= 0x40; if (!b[8]) b[8] = 1; |
| 343 | if (next() & 1) fix_ip_csum(b, (size_t)(b[0] & 15) * 4 <= n ? (size_t)(b[0] & 15) * 4 : 20); |
| 344 | } |
| 345 | net_eth_frame f; net_arp_packet a; net_ipv4_header h; net_icmp_echo e; net_udp_header u; |
| 346 | memset(&h, 0, sizeof h); |
| 347 | const uint8_t *p; size_t pl; |
| 348 | if (net_eth_parse(b, n, &f) == NET_OK) CHECK(f.payload == b + 14 && f.payload_len == n - 14); |
| 349 | (void)net_arp_parse(b, n, &a); |
| 350 | if (net_ipv4_parse(b, n, &h, &p, &pl) == NET_OK) { |
| 351 | CHECK(p >= b + 20 && p + pl <= b + n); |
| 352 | net_ipv4 s = h.source, d = h.destination; |
| 353 | if (net_udp_parse(p, pl, s, d, &u, &p, &pl) == NET_OK) CHECK(p + pl <= b + n); |
| 354 | } |
| 355 | if (net_icmp_echo_parse(b, n, &e) == NET_OK) CHECK(e.data_len == n - 8); |
| 356 | if (net_udp_parse(b, n, h.source, h.destination, &u, &p, &pl) == NET_OK) CHECK(p == b + 8 && p + pl <= b + n); |
| 357 | virtio_pci_caps r; |
| 358 | b[6] |= 16; |
| 359 | (void)virtio_pci_parse_caps(b, 256, &r); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | int main(int argc, char **argv) |
| 364 | { |
| 365 | unsigned long long seed = argc > 1 ? strtoull(argv[1], NULL, 0) : 0x6a5e11ULL; |
| 366 | unsigned iters = argc > 2 ? (unsigned)strtoul(argv[2], NULL, 0) : 200000; |
| 367 | rng = seed ? seed : 1; |
| 368 | test_rust_vectors(); |
| 369 | test_arp_cache(); |
| 370 | test_malformed(); |
| 371 | test_virtio(); |
| 372 | test_roundtrips(iters / 10); |
| 373 | test_hostile(iters); |
| 374 | printf("net_test seed=0x%llx iters=%u checks=%d failures=%d\n", seed, iters, checks, failures); |
| 375 | printf("NET_PROTOCOL_HOST: %s\n", failures ? "FAIL" : "PASS"); |
| 376 | return failures ? 1 : 0; |
| 377 | } |