mirror of
https://github.com/subhobhai943/SUB-OS.git
synced 2026-10-26 00:52:50 +00:00
feat(net): active-open TCP, and fix two bugs that broke TCP on the wire
Adds the client half of TCP -- tcp_connect/tcp_send/tcp_recv/tcp_close -- and wires SOCK_STREAM sockets to it, so the kernel can originate an outbound stream rather than only answer inbound ones. Inbound segments are now matched against the connection table before being offered to the listeners; previously anything not addressed to a listening port was answered with RST, which would have rejected the SYN-ACK completing our own handshake. Transmission is stop-and-wait: one segment outstanding, retransmitted until acknowledged. Every blocking call is bounded against the timer tick and halts the CPU between checks, so a silent peer costs a timeout rather than a hang. Connections linger in TIME_WAIT so a late duplicate cannot be mistaken for traffic on a new incarnation. Verifying it against a real peer uncovered two pre-existing bugs that had made TCP unusable in both directions. Neither was visible from inside the guest, which is why the servers had looked fine: 1. The TCP and UDP checksums were computed by reading the header a uint16_t at a time, immediately after the caller stored zero into that same header's checksum field. Nothing stops the compiler reordering those word reads around that store, and at -O2 it did: the stale checksum field was folded into its own result. Every TCP segment the kernel had ever sent carried a wrong checksum and was silently dropped by the peer -- no client could complete a handshake with sshd or httpd, and it is the likely reason DHCP and DNS replies never arrived. The checksum is now summed a byte at a time through unsigned char pointers, which may alias any object and so cannot be reordered, and which also drops the alignment assumption that reading a packed header as uint16_t made -- a fault rather than a slowdown on ARM. The helper is shared by IP, TCP and UDP instead of being open-coded four times. 2. net_receive derived the datagram length from the Ethernet frame rather than from the IP header's total_length, so the padding on any frame shorter than 60 bytes was delivered as protocol payload. A bare ACK or FIN appeared to carry six bytes, which pushed the acknowledgement number out of step with the peer and turned every close into a FIN retransmission storm answered with RST. Also adds `tcpconnect` and `httpget` applets, and gives netstat the real per-connection state and direction from the engine's table. Verified on x86_64 against a real TCP stack, checked packet by packet in a capture: `httpget` fetched a 349-byte page from a server on the host -- handshake, 81-byte request, response, orderly four-way close, every sequence number exact, zero bad checksums, no retransmissions. In the other direction curl on the host now fetches a live page from the guest's httpd, which had never worked. Builds clean on x86_64, aarch64 and armv8i. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01Fh5z6V7nGPbHvqVG5p4CXX
This commit is contained in:
1 parent
c9dfa6a1aa
commit
65fc5535c8
7 files changed
+847
-172
No files matched your search
+49
-3
@@ -1,12 +1,15 @@
|
||||
// BSD-style socket layer for SUB-OS.
|
||||
//
|
||||
// Implements connectionless (SOCK_DGRAM) sockets on top of the UDP transport.
|
||||
// Implements connectionless (SOCK_DGRAM) sockets on top of the UDP transport
|
||||
// and connection-oriented (SOCK_STREAM) sockets on top of the TCP engine.
|
||||
//
|
||||
// Binding a datagram socket registers a UDP handler that appends inbound
|
||||
// datagrams to the socket's fixed-depth receive ring; sys_recv/sys_recvfrom pop
|
||||
// the oldest datagram. Stream (SOCK_STREAM) sockets are accepted and tracked so
|
||||
// the API is uniform, but their data path is served by the in-kernel TCP engine.
|
||||
// the oldest datagram. A stream socket instead holds a TCP connection opened by
|
||||
// sys_connect, and its send/recv calls carry a byte stream through that.
|
||||
#include <net/socket.h>
|
||||
#include <net/udp.h>
|
||||
#include <net/tcp.h>
|
||||
#include <net/net.h>
|
||||
#include <lib/string.h>
|
||||
#include <kernel/printk.h>
|
||||
@@ -15,6 +18,9 @@
|
||||
#define RX_RING_DEPTH 8
|
||||
#define DGRAM_MAX 1472 // 1500 MTU - 20 IP - 8 UDP
|
||||
|
||||
#define SOCK_CONNECT_TIMEOUT_MS 4000
|
||||
#define SOCK_RECV_TIMEOUT_MS 2000
|
||||
|
||||
typedef struct {
|
||||
uint32_t src_ip;
|
||||
uint16_t src_port;
|
||||
@@ -29,6 +35,7 @@ typedef struct {
|
||||
int head; // next slot to read
|
||||
int count; // queued datagrams
|
||||
bool bound; // a UDP binding is installed for local_port
|
||||
tcp_conn_t* tcp; // stream sockets only: the connection sys_connect opened
|
||||
} sock_priv_t;
|
||||
|
||||
static socket_t socket_table[MAX_SOCKETS];
|
||||
@@ -125,8 +132,22 @@ int sys_connect(int sockfd, const struct sockaddr_in* addr, size_t addrlen) {
|
||||
(void)addrlen;
|
||||
if (!valid_fd(sockfd) || !addr) return -1;
|
||||
socket_t* s = &socket_table[sockfd];
|
||||
sock_priv_t* pv = &socket_priv[sockfd];
|
||||
s->remote_ip = addr->sin_addr;
|
||||
s->remote_port = ntohs(addr->sin_port);
|
||||
|
||||
// A stream socket really connects: the call returns only once the
|
||||
// three-way handshake has completed, or fails on timeout.
|
||||
if (s->type == SOCK_STREAM) {
|
||||
if (pv->tcp) return 0; // already connected
|
||||
pv->tcp = tcp_connect(s->remote_ip, s->remote_port, SOCK_CONNECT_TIMEOUT_MS);
|
||||
if (!pv->tcp) return -1;
|
||||
s->local_ip = pv->tcp->local_ip;
|
||||
s->local_port = pv->tcp->local_port;
|
||||
s->state = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
s->state = 1; // "connected" (address memorised for send/recv)
|
||||
if (s->type == SOCK_DGRAM) return dgram_ensure_bound(sockfd);
|
||||
return 0;
|
||||
@@ -153,6 +174,18 @@ ssize_t sys_sendto(int sockfd, const void* buf, size_t len, int flags,
|
||||
}
|
||||
|
||||
ssize_t sys_send(int sockfd, const void* buf, size_t len, int flags) {
|
||||
if (!valid_fd(sockfd) || !buf) return -1;
|
||||
socket_t* s = &socket_table[sockfd];
|
||||
|
||||
if (s->type == SOCK_STREAM) {
|
||||
sock_priv_t* pv = &socket_priv[sockfd];
|
||||
if (!pv->tcp) return -1; // not connected
|
||||
if (len > 0xFFFF) len = 0xFFFF;
|
||||
int n = tcp_send(pv->tcp, buf, (uint16_t)len);
|
||||
if (n > 0) stat_tx++;
|
||||
return (n < 0) ? -1 : (ssize_t)n;
|
||||
}
|
||||
|
||||
return sys_sendto(sockfd, buf, len, flags, NULL, 0);
|
||||
}
|
||||
|
||||
@@ -183,6 +216,18 @@ ssize_t sys_recvfrom(int sockfd, void* buf, size_t len, int flags,
|
||||
}
|
||||
|
||||
ssize_t sys_recv(int sockfd, void* buf, size_t len, int flags) {
|
||||
if (!valid_fd(sockfd) || !buf) return -1;
|
||||
socket_t* s = &socket_table[sockfd];
|
||||
|
||||
if (s->type == SOCK_STREAM) {
|
||||
sock_priv_t* pv = &socket_priv[sockfd];
|
||||
if (!pv->tcp) return -1; // not connected
|
||||
if (len > 0xFFFF) len = 0xFFFF;
|
||||
int n = tcp_recv(pv->tcp, buf, (uint16_t)len, SOCK_RECV_TIMEOUT_MS);
|
||||
if (n > 0) stat_rx++;
|
||||
return (n < 0) ? -1 : (ssize_t)n;
|
||||
}
|
||||
|
||||
return sys_recvfrom(sockfd, buf, len, flags, NULL, NULL);
|
||||
}
|
||||
|
||||
@@ -190,6 +235,7 @@ int sys_close_socket(int sockfd) {
|
||||
if (!valid_fd(sockfd)) return -1;
|
||||
socket_t* s = &socket_table[sockfd];
|
||||
sock_priv_t* pv = &socket_priv[sockfd];
|
||||
if (pv->tcp) tcp_close(pv->tcp);
|
||||
if (pv->bound && s->local_port) udp_unbind(s->local_port);
|
||||
memset(s, 0, sizeof(socket_t));
|
||||
memset(pv, 0, sizeof(sock_priv_t));
|
||||
|
||||
Reference in new issue
Block a user