network.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Networking | NebOS Architecture</title>
  7. <meta name="description" content="NebOS networking: the netdev registry and NIC drivers, ARP/ICMP/UDP, DHCP, DNS, NTP, the TCP client and listener, TLS 1.3, sys_net ownership, netconsole and netkexec.">
  8. <link rel="icon" href="favicon.svg" type="image/svg+xml">
  9. <link rel="stylesheet" href="style.css">
  10. </head>
  11. <body>
  12. <a class="skip" href="#main">Skip to content</a>
  13. <header class="site-header">
  14. <a class="brand" href="index.html">
  15. <span class="brand-mark" aria-hidden="true"></span>
  16. <span>NebOS <span class="brand-sub">Architecture</span></span>
  17. </a>
  18. <nav class="site-nav" aria-label="Architecture sections">
  19. <a href="index.html">Overview</a>
  20. <a href="boot.html">Boot</a>
  21. <a href="memory.html">Memory</a>
  22. <a href="cpu.html">CPU</a>
  23. <a href="drivers.html">Drivers</a>
  24. <a href="filesystems.html">Filesystems</a>
  25. <a href="graphics.html">Graphics</a>
  26. <a href="network.html" aria-current="page">Network</a>
  27. <a href="userland.html">Userland</a>
  28. <a href="development.html">Development</a>
  29. </nav>
  30. </header>
  31. <main id="main" class="page">
  32. <div class="page-head">
  33. <p class="kicker">Networking</p>
  34. <h1>A small stack with a shared receive pump.</h1>
  35. <p class="lede">
  36. The NIC and the protocol stack live in the kernel; ring-3 commands
  37. drive them through <code>SYS_NET_*</code> and <code>SYS_TCP_*</code>.
  38. Packet-level commands still take the receiver exclusively through
  39. <code>SYS_NET_ACQUIRE</code>, while up to twelve processes can hold a
  40. TCP client connection at the same time. Every stack syscall drains
  41. the NIC through <code>net_pump()</code>, so downloads overlap without
  42. sharing one connection or one receive queue.
  43. </p>
  44. <div class="meta">
  45. <span class="pill">ARP · ICMP · UDP · TCP</span>
  46. <span class="pill">DHCP · DNS · NTP</span>
  47. <span class="pill">TLS 1.3 · X25519</span>
  48. <span class="pill">netconsole + netkexec</span>
  49. </div>
  50. </div>
  51. <nav class="toc" aria-label="On this page">
  52. <h2>On this page</h2>
  53. <ul>
  54. <li><a href="#stack">Protocol stack</a></li>
  55. <li><a href="#netdev">NIC drivers</a></li>
  56. <li><a href="#core">Link &amp; IP layer</a></li>
  57. <li><a href="#dhcp">DHCP, DNS &amp; NTP</a></li>
  58. <li><a href="#tcp">TCP</a></li>
  59. <li><a href="#tls">TLS</a></li>
  60. <li><a href="#abi">sys_net ABI</a></li>
  61. <li><a href="#console">Netconsole &amp; netkexec</a></li>
  62. <li><a href="#http">HTTP clients</a></li>
  63. <li><a href="#chat">Chat client &amp; OIDC login</a></li>
  64. <li><a href="#security">Security posture</a></li>
  65. <li><a href="#sources">Source map</a></li>
  66. </ul>
  67. </nav>
  68. <section id="stack">
  69. <h2>Protocol stack</h2>
  70. <p>
  71. NebOS does not expose a general socket API. A ring-3 program asks the
  72. kernel for info and configuration, then either acquires the receiver
  73. for raw frame paths or opens one TCP client connection. Each
  74. connection has its own receive queue and is owned by one process;
  75. <code>net_pump()</code> dispatches frames to every active client, the
  76. WebDAV listener, a pending DNS query and the ARP cache. HTTP and TLS
  77. are libraries linked into the command, not kernel services.
  78. </p>
  79. <figure class="figure">
  80. <svg viewBox="0 0 920 380" role="img" aria-labelledby="net-title net-desc">
  81. <title id="net-title">NebOS network stack</title>
  82. <desc id="net-desc">Ring-3 curl, browser and websrv use uhttp and TLS, then sys_net syscalls, then TCP/DNS/DHCP/NTP, IPv4/ARP/ICMP, netdev drivers and the NIC.</desc>
  83. <rect class="svg-box accent" x="70" y="18" width="780" height="58" rx="9"/>
  84. <text class="svg-text" x="92" y="43">curl · browser · websrv · ping · dhcp · settime</text>
  85. <text class="svg-sub" x="92" y="63">ring-3 programs</text>
  86. <path class="svg-line" d="M460 76v20"/>
  87. <path class="svg-line" d="M454 90l6 7 6-7"/>
  88. <rect class="svg-box blue" x="70" y="98" width="380" height="58" rx="9"/>
  89. <text class="svg-text" x="92" y="123">uhttp · tls.c</text>
  90. <text class="svg-sub" x="92" y="143">HTTP/1.1 · redirects · chunked · TLS 1.3</text>
  91. <rect class="svg-box" x="470" y="98" width="380" height="58" rx="9"/>
  92. <text class="svg-text" x="492" y="123">kernel services</text>
  93. <text class="svg-sub" x="492" y="143">sys_fs for WebDAV · netlog · netkexec</text>
  94. <path class="svg-line blue" d="M260 156v18"/>
  95. <path class="svg-line blue" d="M254 168l6 7 6-7"/>
  96. <path class="svg-line" d="M660 156v18"/>
  97. <path class="svg-line" d="M654 168l6 7 6-7"/>
  98. <rect class="svg-box green" x="70" y="176" width="780" height="58" rx="9"/>
  99. <text class="svg-text" x="92" y="201">SYS_NET_* · SYS_TCP_* · SYS_DNS_RESOLVE · SYS_DHCP_RUN · SYS_NTP_SYNC</text>
  100. <text class="svg-sub" x="92" y="221">raw owner for packet commands · shared net_pump for TCP/DNS/ARP</text>
  101. <path class="svg-line green" d="M460 234v20"/>
  102. <path class="svg-line green" d="M454 248l6 7 6-7"/>
  103. <rect class="svg-box" x="70" y="256" width="780" height="58" rx="9"/>
  104. <text class="svg-text" x="92" y="281">tcp.c · dns.c · dhcp.c</text>
  105. <text class="svg-sub" x="92" y="301">TCP client table + listener · one pending A query · DISCOVER/OFFER/REQUEST/ACK</text>
  106. <path class="svg-line" d="M460 314v16"/>
  107. <path class="svg-line" d="M454 324l6 7 6-7"/>
  108. <rect class="svg-box blue" x="70" y="332" width="780" height="44" rx="9"/>
  109. <text class="svg-text" x="92" y="359">IPv4 · ARP · ICMP · UDP · netdev registry · NIC driver</text>
  110. </svg>
  111. <figcaption>
  112. <strong>Thin kernel, thick command.</strong> The kernel owns
  113. addressing, ARP, routing and the shared TCP client table. HTTP,
  114. TLS and WebDAV run in ring 3, so a protocol bug kills a command
  115. rather than the kernel.
  116. </figcaption>
  117. </figure>
  118. </section>
  119. <section id="netdev">
  120. <h2>NIC drivers</h2>
  121. <p>
  122. <span class="file">netdev.c</span> keeps a registry of
  123. <code>struct netdev</code> drivers; the first one whose probe succeeds
  124. becomes active. All drivers are polled and share the same transmit,
  125. receive, register-dump and reset interface. <code>netdev</code> with
  126. no argument shows the active driver; <code>netdev rndis</code> or
  127. <code>netdev asix</code> switches to a named one, leaving the current
  128. driver untouched if the probe fails.
  129. </p>
  130. <p>
  131. The top-bar network icon opens <span class="file">ucmd_netmenu.c</span>.
  132. It asks the kernel for every present driver through
  133. <code>SYS_NET_DEVLIST</code> (111), so an RNDIS phone or a USB
  134. Ethernet dongle that was not the boot-time winner still appears.
  135. Selecting one calls <code>SYS_NET_SELECT</code> (112) and then runs
  136. DHCP. The same window lists scanned WiFi networks; selecting a WPA2
  137. network asks for the passphrase and starts the existing
  138. <code>wifi</code> supplicant, which opens the data port and runs DHCP
  139. itself. Mesh/repeater BSSIDs sharing one SSID are collapsed to the
  140. strongest entry (the active BSSID wins when connected), and hidden
  141. SSIDs are omitted because the menu cannot join them.
  142. </p>
  143. <p>
  144. <span class="file">rndis.c</span> is the USB tethering path, not a
  145. WiFi driver: an Android phone (or QEMU's <code>usb-net</code>) is
  146. parsed for an RNDIS control/data interface pair, initialized with
  147. RNDIS INIT/QUERY/SET messages, and then presented as an ordinary
  148. Ethernet NIC. <code>netdev rndis</code> selects it and
  149. <code>dhcp</code> obtains the phone's tethering address. QEMU has no
  150. phone dependency: <code>make run-rndis</code> boots with
  151. <code>qemu-xhci</code> and <code>usb-net</code> on slirp. RNDIS only;
  152. CDC-ECM/NCM, IPv6 and automatic DHCP after a hotplug are not
  153. implemented.
  154. </p>
  155. <p>
  156. <span class="file">wifi.c</span> is the built-in WiFi of the
  157. Chromebook (Intel 7265D/3165, see the
  158. <a href="drivers.html#wifi">driver page</a>). It is last in the
  159. registry, so the boot probe only makes it active when nothing
  160. wired is present (a later <code>wifi connect</code> can still select
  161. it over a wired NIC whose cable is unplugged), and it reports
  162. <code>link_up() = 0</code> until it is connected: the boot DHCP
  163. checks that optional hook and skips the 4 s attempt. The
  164. <code>wifi</code> command scans, associates, runs the WPA2-PSK
  165. handshake in ring 3 and then calls <code>SYS_WIFI_AUTHORIZE</code>,
  166. which opens the data port and selects the <code>wifi</code> netdev.
  167. Data frames are converted between Ethernet and non-QoS 802.11 +
  168. LLC/SNAP; EAPOL (ethertype <code>0x888e</code>) never reaches
  169. <code>net_pump()</code> and is queued for the supplicant instead.
  170. The top-bar menu starts the same supplicant through
  171. <code>wifi connectfile &lt;path&gt;</code>: it stages the SSID,
  172. passphrase and BSSID in a small <code>/share/netmenu.wifi</code>
  173. record because a <code>SYS_SPAWN</code> argument is limited to
  174. <code>VFS_PATH_MAX</code> (64) bytes. The helper removes the file
  175. after reading it.
  176. </p>
  177. <p>
  178. A successful <code>wifi connect</code> (from the CLI or the menu) also saves
  179. the SSID and passphrase to <code>/data/WIFI.CFG</code> on the
  180. persistent <code>NEBOSDATA</code> partition. After the boot DHCP
  181. attempt <code>kernel_main</code> spawns
  182. <code>wifi autoconnect</code> when that file exists. It samples the
  183. active NIC's <code>link_up()</code> state: a wired cable that is
  184. physically up keeps the Ethernet path, while no wired link lets
  185. WiFi associate and run DHCP. An existing WiFi link is left alone.
  186. <code>wifi forget</code> removes the saved record.
  187. The file is a plaintext 8.3 FAT file with the BSSID zeroed so the
  188. strongest AP advertising that SSID is used.
  189. </p>
  190. <div class="table-wrap">
  191. <table>
  192. <thead>
  193. <tr><th>Driver</th><th>Hardware</th><th>Transport</th><th>Notes</th></tr>
  194. </thead>
  195. <tbody>
  196. <tr><td><span class="file">e1000e.c</span></td><td>Intel 82540EM, 82577LM, 82578, 82579</td><td>PCI MMIO</td><td>PCI INTx disabled before MMIO; PCH D0; skips the global MAC reset while the link is up.</td></tr>
  197. <tr><td><span class="file">r8168.c</span></td><td>RTL8111/8168</td><td>PCIe MMIO</td><td>C+ mode, 64-entry RX ring, polled.</td></tr>
  198. <tr><td><span class="file">ne2000.c</span></td><td>RTL8029</td><td>ISA / QEMU PCI</td><td>Simple DP8390 path used by <code>make run-net</code>.</td></tr>
  199. <tr><td><span class="file">asix.c</span></td><td>AX88772</td><td>USB bulk</td><td>Runs over the USB core; usable on EHCI/xHCI dongles.</td></tr>
  200. <tr><td><span class="file">rndis.c</span></td><td>Android USB tethering, QEMU <code>usb-net</code></td><td>USB control + bulk</td><td>RNDIS INIT/QUERY/packet filter; presents the phone's tethering link as an Ethernet netdev.</td></tr>
  201. <tr><td><span class="file">wifi.c</span></td><td>Intel 7265D / 3165 (Chromebook)</td><td>PCIe (firmware)</td><td>Open and WPA2-PSK/CCMP; firmware loaded on first use; link down until <code>wifi connect</code>.</td></tr>
  202. </tbody>
  203. </table>
  204. </div>
  205. <p>
  206. The management NIC used by netlog and netkexec prefers the ASIX dongle
  207. so a direct debug link survives <code>netdev e1000e</code>; otherwise
  208. it falls back to the active driver. <code>netregs</code> dumps registers
  209. and decoded interrupt status; <code>netloop</code> runs a PHY loopback
  210. self-test; <code>netreset</code> re-initialises the active NIC.
  211. </p>
  212. </section>
  213. <section id="core">
  214. <h2>Link &amp; IP layer</h2>
  215. <ul>
  216. <li><strong>Ethernet decode</strong> recognises IPv4, ARP and IPv6 frame types (IPv6 is not implemented).</li>
  217. <li><strong>ARP</strong> resolves a next hop with three tries, one second apart; <code>net_arp_resolve()</code> polls the active NIC and the caller must own it. Cache entries live five minutes and their age slides on every hit, so a DNS or gateway entry does not expire during a long page load. Stack waits drain the receiver in short bursts so a full driver ring can reset and recover.</li>
  218. <li><strong>Routing</strong> sends off-subnet traffic to the gateway when one is known; <code>net_next_hop()</code> is shared by ping, DNS and TCP.</li>
  219. <li><strong>ICMP echo</strong> backs <code>ping</code>, with ARP resolution and round-trip timing.</li>
  220. <li><strong>UDP</strong> has send and parse helpers; DNS, DHCP, NTP and netlog are its users.</li>
  221. <li><strong>Checksums</strong> use the Internet one's-complement sum, with a TCP pseudo-header variant.</li>
  222. </ul>
  223. <p>
  224. The static default address is <code>192.168.178.240</code>. At boot,
  225. when the active NIC reports a physical link, a DHCP client runs with
  226. a 4-second budget (requesting the current address); if no server
  227. answers, the static address stays, and a cable that is not plugged
  228. in skips the wait so the saved WiFi network can be tried. The idle
  229. WM loop answers ARP/ICMP for the board's IP
  230. even when no command is running, so the board is reachable and
  231. MAC-resolvable.
  232. </p>
  233. </section>
  234. <section id="dhcp">
  235. <h2>DHCP, DNS &amp; NTP</h2>
  236. <h3>DHCP</h3>
  237. <p>
  238. <span class="file">dhcp.c</span> is a minimal client:
  239. DISCOVER → OFFER → REQUEST → ACK. It learns the mask, gateway and DNS
  240. server (option 6), and <code>dhcp</code> re-runs it on demand. The
  241. configuration is retained across kexec through the boot-info
  242. <code>BOOTINFO_FLAG_NET</code> fields.
  243. </p>
  244. <h3>DNS</h3>
  245. <p>
  246. <span class="file">dns.c</span> sends one recursive A query over UDP
  247. and handles name compression in the response. One query is in
  248. flight at a time; parallel stack callers wait for it and then hit a
  249. small five-minute A-record cache, so three browser workers asking
  250. for the same host cause one DNS query, not three. <code>netdns</code>
  251. shows or sets the server; <code>host &lt;name&gt;</code> resolves.
  252. Numeric hosts skip DNS entirely.
  253. </p>
  254. <h3>NTP</h3>
  255. <p>
  256. <span class="file">ntp.c</span> is a minimal SNTP (RFC 4330)
  257. client: one 48-byte request to UDP port 123, three one-second
  258. tries, DNS unless the server is a dotted address, and the shared
  259. ARP/router path. An empty name uses <code>pool.ntp.org</code>.
  260. A reply is accepted only when it comes from the
  261. resolved server to the ephemeral source port, is a server/broadcast
  262. reply with a valid stratum, and echoes the request's origin
  263. timestamp; the transmit timestamp is converted from NTP's 1900 epoch
  264. to a UTC <code>struct sys_datetime</code> (with the 2036 era wrap
  265. handled). <code>SYS_NTP_SYNC</code> returns that value to ring 3;
  266. the Date &amp; Time editor then writes it through
  267. <code>SYS_RTC_SET</code>. The service owns the raw receiver for the
  268. duration, so it reports <code>SYS_NTP_EBUSY</code> while another raw
  269. command or a TCP client is active.
  270. </p>
  271. <div class="warn">
  272. <p><strong>NTP is unauthenticated.</strong> The origin-timestamp
  273. check rejects unrelated datagrams but does not authenticate the
  274. server; anyone who can answer for the configured name can set the
  275. board's clock. There is no NTS, no leap-second handling and no
  276. delay/dispersion estimation — the CMOS RTC only resolves seconds.</p>
  277. </div>
  278. </section>
  279. <section id="tcp">
  280. <h2>TCP</h2>
  281. <p>
  282. <span class="file">tcp.c</span> implements a small client table, not a
  283. general socket layer: each process may hold one connect/send/receive/
  284. close connection, and up to <code>TCP_CLIENT_MAX</code> (twelve)
  285. connections can be active. Every connection owns a receive queue
  286. (64 KB from the frame allocator, with 32/16 KB fallbacks), so one
  287. socket's data can never be read or reset by another.
  288. The window update is sent whenever the application drains the queue,
  289. so a full queue cannot deadlock until the peer's zero-window probe.
  290. </p>
  291. <ul>
  292. <li><strong>Demultiplexing.</strong> <code>net_pump()</code> feeds every received frame to <code>tcp_client_input()</code>, which matches the destination IP and port pair and updates that connection's sequence state.</li>
  293. <li><strong>MSS / window.</strong> Our MSS is 1460 bytes and clients advertise up to a 64 KB window (the connection's receive queue; no window scaling). At a 30 ms round trip that is ~2 MB/s per connection instead of the old 4 KB window's ~133 KB/s.</li>
  294. <li><strong>Close.</strong> <code>tcp_client_close()</code> sends FIN and frees the slot immediately instead of waiting for the peer's FIN; every send is synchronous, so no data is outstanding, and a late FIN matches no client and is dropped. <code>uhttp</code> also skips the TLS close_notify when a redirect leaves the origin, so a redirect hop costs no extra round trips.</li>
  295. <li><strong>Retransmit.</strong> A 500 ms retry timer with a small retry count carries the connection through loss; there is no full congestion-control state machine.</li>
  296. <li><strong>Listener.</strong> The server side has one active connection plus three backlog/closing slots. SYN-ACK advertises a zero window while queued; a queued connection may hold 1 KB of early request, and the window opens when <code>tcp_srv_accept()</code> runs.</li>
  297. <li><strong>Bursts.</strong> Responses go out in up to four-segment bursts with a cumulative ACK, rather than one segment per ACK.</li>
  298. <li><strong>No keep-alive.</strong> The WebDAV listener still has one active connection; an idle persistent connection would block the parallel connections Nautilus opens. Client keep-alive is per helper process and does not hold the raw lease.</li>
  299. </ul>
  300. <p>
  301. New SYNs and data are also handed to <code>tcp_srv_input()</code> from
  302. the active connection's receive/send poll loops, not only between
  303. requests, so parallel gvfs clients make progress.
  304. </p>
  305. </section>
  306. <section id="tls">
  307. <h2>TLS 1.3</h2>
  308. <p>
  309. <span class="file">tls.c</span> is a ring-3 client with a deliberately
  310. narrow cipher suite: <code>TLS_CHACHA20_POLY1305_SHA256</code> and
  311. X25519 only. It is stream-oriented over the existing TCP connection
  312. and prints the leaf SHA-256 fingerprint for manual comparison.
  313. </p>
  314. <ul>
  315. <li><strong>Not verified.</strong> Phase A provides encryption without authentication; a MITM is undetected. <code>curl</code> warns, and the browser marks HTTPS as unverified.</li>
  316. <li><strong>Weak DRBG.</strong> <span class="file">tls_rng.c</span> mixes RDTSC jitter, ticks and the MAC; it is not a hardware-backed CSPRNG.</li>
  317. <li><strong>Host tests.</strong> The crypto files are pure freestanding C with host vectors (<code>make test-crypto</code>, <code>tls_test.c</code>).</li>
  318. </ul>
  319. <div class="warn">
  320. <p><strong>Do not treat HTTPS as secure authentication.</strong> The
  321. cipher suite is real, but without certificate verification the
  322. connection authenticates nothing. Use it for confidentiality on a
  323. trusted network, not for secrets against an active attacker.</p>
  324. </div>
  325. </section>
  326. <section id="abi">
  327. <h2>sys_net ABI &amp; ownership</h2>
  328. <p>
  329. <span class="file">sys_net.c</span> exposes NIC info, MAC/IP/mask/
  330. gateway/DNS getters and setters, raw frame TX/RX, decode, reply,
  331. loopback, register dump, reset, speed and MAC overrides. It also
  332. exposes the DNS, DHCP and NTP clients and the TCP/HTTP helpers.
  333. </p>
  334. <ul>
  335. <li><strong>Raw owner.</strong> <code>SYS_NET_ACQUIRE</code> takes the receiver for a packet-level command; the background kexec watcher pauses while one owns it. TCP clients do not take this lease, so they can run alongside each other.</li>
  336. <li><strong>Mode gates.</strong> A raw acquire fails while any TCP client socket is open, and <code>SYS_TCP_CONNECT</code> fails while a raw owner exists. This keeps packet commands and the shared pump from stealing each other's frames.</li>
  337. <li><strong>Reaper safety.</strong> If a raw owner dies, <code>sys_net_reap</code> force-releases the receiver through <code>netdev_release_all()</code>; sockets and a pending DNS query owned by the dead process are closed separately.</li>
  338. <li><strong>Wait semantics.</strong> Stack waits now yield with <code>process_sleep_ms()</code> and re-enable interrupts for driver TX timeouts; the syscall stub enters with interrupts off.</li>
  339. <li><strong>One connection per process.</strong> The connection state lives in the kernel, keyed by the calling pid. <code>SYS_TCP_CONNECT/SEND/RECV/CLOSE</code> keep their ABI, so uhttp and the TLS layer did not need a socket handle.</li>
  340. </ul>
  341. </section>
  342. <section id="console">
  343. <h2>Netconsole &amp; netkexec</h2>
  344. <p>
  345. <span class="file">netlog.c</span> mirrors the serial console and
  346. terminal output to UDP port 19290. The mirror is on by default and
  347. broadcasts; <code>netconsole host &lt;laptop-ip&gt;</code> narrows it,
  348. <code>netconsole off</code>/<code>on</code> toggles it, and
  349. <code>netlog &lt;text&gt;</code> sends an ad-hoc line. The producer
  350. never blocks: a full buffer drops characters and reports it. The
  351. flush pauses while a raw command owns the receiver or a TCP/stack
  352. operation is active, so its broadcast traffic cannot compete with
  353. DNS/ARP replies.
  354. </p>
  355. <p>
  356. <span class="file">netkexec.c</span> receives a new kernel/rootfs over
  357. UDP port 19288 using <code>KXN2</code>, a runtime command pack using
  358. <code>KXNP</code>, and a persistent install using <code>KXI2</code>.
  359. Datagrams are acknowledged with the next expected offset and verified
  360. with a 32-bit checksum. <code>KXN2</code>/<code>KXNP</code> jump or
  361. swap only in RAM. <code>KXI2</code> carries a 16-byte target name;
  362. the board writes P2, then the P1 kernel, patches the exact
  363. <code>kernel_sectors</code>, verifies the disk, sends
  364. <code>ACK_INSTALLED</code>/<code>ACK_INSTALL_FAIL</code>, and jumps.
  365. It never touches P4 (<code>/data</code>) or the MBR. On the host,
  366. <code>make netmon</code>, <code>make net-dev</code> and
  367. <code>make net-install</code> wrap these protocols.
  368. </p>
  369. </section>
  370. <section id="http">
  371. <h2>HTTP clients</h2>
  372. <ul>
  373. <li><strong>curl</strong> — <code>http://</code> or <code>https://</code> GET with DNS, ARP, redirects (up to five hops), chunked decoding, status/headers/body dump and a TLS warning.</li>
  374. <li><strong>uhttp</strong> — shared ring-3 HTTP/1.1 client used by curl, the browser and chat: URL parsing, redirects (up to eight hops; 1 KB paths and 1.5 KB header lines, enough for OIDC URLs), chunked decoding, timeouts and TLS. Requests and bodies of any size are split into pieces the transport accepts (1024-byte TLS records, one peer MSS for plain TCP). Optional per-request fields set the <code>Accept</code> header, extra header lines and a cookie jar; 1xx/204/304 responses are treated as bodyless so keep-alive does not wait for an idle timeout.</li>
  375. <li><strong>uhttp_cookie</strong> — the cookie jar (<span class="file">uhttp_cookie.c</span>, 24 cookies of up to 768 bytes). Every <code>Set-Cookie</code>, including those on redirect responses, is stored; matching cookies are sent on every request and redirect hop. Cookies are host-only (<code>Domain</code> is ignored), <code>Path</code> is a prefix match, <code>Secure</code> cookies only go over https, and <code>Max-Age&lt;=0</code> or a 1970 <code>Expires</code> deletes a cookie. The jar outlives the connection, so a cross-origin redirect that reconnects keeps the session.</li>
  376. <li><strong>browser</strong> — runs network fetches in up to four headless <code>curl __serve</code> workers, each with its own request/body/meta files under <code>/share/bf&lt;session&gt;_&lt;slot&gt;*</code>. The session id comes from the browser pid, so multiple browser windows do not share queue files. Stylesheet and external-script bodies are downloaded in parallel but parsed/executed in document order; one image starts before the script phase, and the remaining images are prefetched in parallel and decoded as jobs complete, so the UI keeps drawing and scrolling. Each worker keeps its own TCP/TLS connection; idle helpers stay warm across navigations (a cross-origin job just drops the old connection, and only busy helpers are killed), and helpers also exit after ten idle seconds or when the window loses focus. A DNS failure is retried once after tearing the helpers down. The helpers' DNS/TLS log lines go to a private tty that the browser drains and discards from every poll loop; a full tty ring blocks its writers, so an undrained one used to hang every helper after about sixteen TLS connections. The status bar reports the page's total, header and first-byte times.</li>
  377. <li><strong>websrv</strong> — the server side, speaking HTTP/1.1 + WebDAV over the TCP listener; see <a href="filesystems.html#webdav">Filesystems → WebDAV</a>.</li>
  378. </ul>
  379. <p>
  380. The browser's helper requests gzip; inflating a multi-megabyte
  381. page in the UI process uses an inflate progress hook that pumps
  382. the UI while decoding.
  383. </p>
  384. </section>
  385. <section id="chat">
  386. <h2>Chat client &amp; OIDC login</h2>
  387. <p>
  388. <code>chat</code> (<span class="file">ucmd_chat.c</span>) is a native
  389. client for Cloudron Chat built on uhttp, the cookie jar, the
  390. <span class="file">ujson.c</span> arena JSON parser (fixed node pool,
  391. escapes, surrogate pairs) and ngui. The web login is a Vue
  392. ES-module SPA the browser cannot run, so
  393. <span class="file">chat_login.c</span> drives the API underneath:
  394. </p>
  395. <ol>
  396. <li><code>GET &lt;server&gt;/auth/login</code> follows the redirect to the Cloudron OIDC provider's <code>/openid/interaction/&lt;uid&gt;</code> page, collecting the PKCE pre-session cookie on the Chat host and the interaction cookies on the provider.</li>
  397. <li>The page's <code>&lt;script id="cloudron-config"&gt;</code> JSON names a relative <code>submitUrl</code> (a plain <code>"submitUrl":"…"</code> scan is the fallback). No config means external SSO: the client offers the cookie screen.</li>
  398. <li><code>POST submitUrl {"username","password","totpToken"}</code> answers 401 with a message, <code>{"twoFactorRequired":true,"totpRequired":true}</code> (the client asks for the code and reposts), <code>totpRequired:false</code> (passkey-only: cookie screen) or <code>{"redirectTo"}</code>.</li>
  399. <li>Following <code>redirectTo</code> ends at <code>/auth/callback</code> on the Chat host, which sets <code>connect.sid</code>. The consent interaction on the way is a bare auto-submit form (no <code>cloudron-config</code>): its <code>&lt;form action="/openid/interaction/&lt;uid&gt;/confirm"&gt;</code> is posted like the web UI does, and an <code>/abort</code> form or page means the account has no access. <code>GET /api/me</code> validates the session.</li>
  400. </ol>
  401. <p>
  402. The password lives only in the form field and the request buffer;
  403. both are wiped after the attempt. The Chat host's cookie header is
  404. saved with the server address in <code>/share/chat.cfg</code>
  405. (RAM-only; another path can be given as the argument or on the login
  406. screen) and is re-saved when the server rolls the cookie. The saved
  407. cookie is a bearer credential. Passkey and external-SSO accounts
  408. paste a <code>connect.sid</code> cookie from a signed-in browser
  409. instead.
  410. </p>
  411. <p>
  412. There is no WebSocket: the open conversation is polled with
  413. <code>GET /api/channels/:id/messages?after=&lt;newest&gt;</code> every
  414. 2 s (5 s while the window is unfocused) and the directory
  415. (<code>/api/channels</code>, <code>/api/dms</code>) every 12 s (20 s),
  416. and polling waits 1 s after keyboard or pointer input. Errors back
  417. off 2/5/10/30 s. Sends carry a <code>clientId</code> so a retried
  418. POST cannot duplicate a message. An open TCP socket pauses the
  419. board's netkexec watcher, so the client only keeps its keep-alive
  420. connection while its window is focused and the user was active in
  421. the last 30 s; otherwise it closes it after each poll. Other
  422. clients see the NebOS user as offline (presence is WebSocket-based).
  423. </p>
  424. </section>
  425. <section id="security">
  426. <h2>Security posture</h2>
  427. <div class="table-wrap">
  428. <table>
  429. <thead>
  430. <tr><th>Surface</th><th>State</th><th>Consequence</th></tr>
  431. </thead>
  432. <tbody>
  433. <tr><td>TLS certificates</td><td>Not verified</td><td>Encryption without authentication; MITM is undetected.</td></tr>
  434. <tr><td>TLS DRBG</td><td>Weak (RDTSC/ticks/MAC)</td><td>Not suitable for high-value secrets.</td></tr>
  435. <tr><td>WebDAV</td><td>No authentication</td><td>Anyone on the LAN can read mounts and write <code>/share</code>.</td></tr>
  436. <tr><td>Chat session</td><td>Cookie saved in <code>/share/chat.cfg</code></td><td>A bearer credential in RAM until sign-out; the password is sent over unverified TLS and never stored.</td></tr>
  437. <tr><td>WiFi WPA2</td><td>PSK/CCMP, handshake in ring 3</td><td>The SNonce comes from the weak TLS DRBG. The PTK/replay are kept in the kernel (<code>SYS_WIFI_SET_PTK</code>/<code>SYS_WIFI_GET_PTK</code>) and fetched by the <code>wifi __keep</code> helper by connection ID for group rekeys; no WPA3/PMF.</td></tr>
  438. <tr><td>Saved WiFi credentials</td><td>Plaintext <code>/data/WIFI.CFG</code></td><td>The SSID and passphrase are stored in an 8.3 FAT file so <code>wifi autoconnect</code> can reconnect at boot; anyone who can read the disk can read the passphrase. <code>wifi forget</code> removes it.</td></tr>
  439. <tr><td>NTP</td><td>Unauthenticated SNTP</td><td>Anyone who can answer for the server name can set the clock.</td></tr>
  440. <tr><td>NIC ownership</td><td>Raw lease for packet commands; shared TCP client table</td><td>Raw commands are mutually exclusive; up to twelve processes can hold one TCP connection each. A killed owner is force-released.</td></tr>
  441. <tr><td>Kernel/user isolation</td><td>None (identity map)</td><td>A ring-3 bug can reach any mapped kernel page.</td></tr>
  442. </tbody>
  443. </table>
  444. </div>
  445. </section>
  446. <section id="sources">
  447. <h2>Source map</h2>
  448. <ul class="filelist">
  449. <li><code>netdev.c</code>, <code>netdev.h</code> — registry</li>
  450. <li><code>e1000e.c</code>, <code>r8168.c</code>, <code>ne2000.c</code>, <code>asix.c</code>, <code>rndis.c</code></li>
  451. <li><code>wifi.c</code>, <code>iwl_mvm.c</code>, <code>iwl_trans.c</code> — Intel 7265D/3165 WiFi</li>
  452. <li><code>wpa.c</code>, <code>wpa_crypto.c</code>, <code>ucmd_wifi.c</code> — WPA2-PSK supplicant and the <code>wifi</code> command (<code>make test-wpa</code>)</li>
  453. <li><code>ucmd_netmenu.c</code>, <code>wifi_cfg.h</code> — top-bar network menu and its staged WiFi connect request</li>
  454. <li><code>net.c</code>, <code>net.h</code> — ARP cache/ICMP/UDP/IP</li>
  455. <li><code>netcore.c</code>, <code>netcore.h</code> — shared receive pump and IRQ helpers</li>
  456. <li><code>dhcp.c</code>, <code>dns.c</code>, <code>ntp.c</code></li>
  457. <li><code>tcp.c</code>, <code>tcp.h</code> — client table + listener</li>
  458. <li><code>tls.c</code>, <code>tls_*.c</code> — TLS 1.3</li>
  459. <li><code>sys_net.c</code> — ring-3 service</li>
  460. <li><code>netlog.c</code>, <code>netkexec.c</code></li>
  461. <li><code>uhttp.c</code>, <code>ucmd_curl.c</code>, <code>ucmd_websrv.c</code></li>
  462. <li><code>uhttp_cookie.c</code>, <code>ujson.c</code> — cookie jar, JSON parser</li>
  463. <li><code>chat_login.c</code>, <code>ucmd_chat.c</code> — OIDC login driver, chat client</li>
  464. <li><code>tools/mock_chat.py</code>, <code>tools/qmp_chat_test.py</code> — mock server, QEMU end-to-end test</li>
  465. </ul>
  466. </section>
  467. </main>
  468. <footer class="site-footer">
  469. <div class="inner">
  470. <p><strong>NebOS 0.2.0</strong> · Architecture documentation · <a href="index.html">overview</a></p>
  471. <p>Maintain alongside <code>AGENTS.md</code> and the source files listed on each page.</p>
  472. </div>
  473. </footer>
  474. </body>
  475. </html>