mirror of
https://github.com/subhobhai943/SUB-OS.git
synced 2026-10-25 16:52:29 +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
@@ -102,6 +102,15 @@ arp_entry_t* net_get_arp_table(int* count_out);
|
||||
|
||||
uint32_t ip_parse(const char* ip_str);
|
||||
void ip_to_str(uint32_t ip, char* buf);
|
||||
|
||||
// One's-complement checksum, summed a byte at a time so the loads cannot be
|
||||
// reordered around a caller's store into the buffer being summed, and so no
|
||||
// alignment is assumed of a packed header. Words are read big-endian;
|
||||
// net_csum_finish returns the result already in network byte order, ready to
|
||||
// be assigned straight to a header's checksum field.
|
||||
uint32_t net_csum_add(uint32_t sum, const void* data, size_t len);
|
||||
uint16_t net_csum_finish(uint32_t sum);
|
||||
uint16_t net_checksum(const void* data, size_t len);
|
||||
void mac_to_str(const uint8_t* mac, char* buf);
|
||||
|
||||
#endif // _NET_NET_H
|
||||
+55
-2
@@ -34,15 +34,35 @@ typedef struct {
|
||||
uint16_t urgent_pointer;
|
||||
} __attribute__((packed)) tcp_header_t;
|
||||
|
||||
// Which end opened the connection. Server connections are created by an
|
||||
// inbound SYN to a listening service; client connections by tcp_connect().
|
||||
#define TCP_ROLE_SERVER 0
|
||||
#define TCP_ROLE_CLIENT 1
|
||||
|
||||
#define TCP_RX_BUF_SIZE 4096
|
||||
#define TCP_MSS 1460
|
||||
|
||||
typedef struct tcp_conn {
|
||||
uint32_t local_ip;
|
||||
uint32_t remote_ip;
|
||||
uint16_t local_port;
|
||||
uint16_t remote_port;
|
||||
uint8_t state;
|
||||
uint32_t seq_num;
|
||||
uint32_t ack_num;
|
||||
uint32_t seq_num; // next sequence number we will send
|
||||
uint32_t ack_num; // next sequence number we expect to receive
|
||||
bool in_use;
|
||||
|
||||
// --- client / stream state, unused by the server path ----------------
|
||||
uint8_t role;
|
||||
uint32_t iss; // initial send sequence number
|
||||
uint32_t snd_una; // oldest sequence number not yet acknowledged
|
||||
uint8_t* rx_buf; // receive ring, allocated on active open
|
||||
uint16_t rx_head; // read cursor
|
||||
uint16_t rx_count; // bytes available to the reader
|
||||
bool peer_fin; // peer sent FIN: no more data will arrive
|
||||
bool reset; // peer sent RST
|
||||
uint64_t timer; // tick of the last transmission awaiting an ACK
|
||||
uint64_t closed_at; // tick TIME_WAIT began
|
||||
} tcp_conn_t;
|
||||
|
||||
void tcp_init(void);
|
||||
@@ -51,7 +71,40 @@ void tcp_send_packet(uint32_t dst_ip, uint16_t src_port, uint16_t dst_port,
|
||||
uint32_t seq, uint32_t ack, uint8_t flags,
|
||||
const void* payload, uint16_t payload_len);
|
||||
|
||||
// --- Active open (client side) ------------------------------------------
|
||||
//
|
||||
// Every call below is bounded: a silent or unreachable peer produces a
|
||||
// timeout, never a hang. They busy-wait against the timer tick with the CPU
|
||||
// halted between checks, the same way net_ping and the DNS resolver do, so
|
||||
// they must be called from a context where inbound packets can still be
|
||||
// serviced by the NIC interrupt.
|
||||
|
||||
// Perform the three-way handshake with dst_ip:dst_port, retransmitting the
|
||||
// SYN until the handshake completes or timeout_ms elapses. Returns the
|
||||
// connection, or NULL if it could not be established.
|
||||
tcp_conn_t* tcp_connect(uint32_t dst_ip, uint16_t dst_port, uint32_t timeout_ms);
|
||||
|
||||
// Send len bytes, segmented to the MSS. Stop-and-wait: each segment is
|
||||
// retransmitted until acknowledged before the next is sent. Returns the number
|
||||
// of bytes acknowledged, or -1 if the connection is unusable.
|
||||
int tcp_send(tcp_conn_t* c, const void* data, uint16_t len);
|
||||
|
||||
// Read up to len bytes, waiting up to timeout_ms for data to arrive. Returns
|
||||
// the byte count, or -1 on reset. A return of 0 means no data became
|
||||
// available: use tcp_peer_closed() to tell a finished stream from an idle one.
|
||||
int tcp_recv(tcp_conn_t* c, void* buf, uint16_t len, uint32_t timeout_ms);
|
||||
|
||||
// Orderly shutdown, then release the connection. The handle must not be used
|
||||
// afterwards.
|
||||
void tcp_close(tcp_conn_t* c);
|
||||
|
||||
bool tcp_is_established(const tcp_conn_t* c);
|
||||
|
||||
// True once the peer's FIN has been received, so no further data can arrive.
|
||||
bool tcp_peer_closed(const tcp_conn_t* c);
|
||||
|
||||
size_t tcp_get_connections_count(void);
|
||||
const tcp_conn_t* tcp_get_connection(size_t index);
|
||||
const char* tcp_state_name(uint8_t state);
|
||||
|
||||
#endif // _NET_TCP_H
|
||||
Reference in new issue
Block a user