Skip to content

Networking And Sockets

Imported from _research/manual-study-linux/networking-sockets.md.

Networking And Sockets

Status: implemented source-backed volume.

Source Surface

  • net/socket.c: socket syscalls, descriptor/file conversion, socket allocation/release, send/receive dispatch, bind/listen/accept/connect.
  • include/linux/net.h: struct socket, struct proto_ops, and socket API declarations used by protocol implementations.
  • include/linux/socket.h: syscall-facing declarations.

Entry Points

net/socket.c exposes the socket syscall family. __sys_socket() appears at line 1788 and SYSCALL_DEFINE3(socket) at line 1805. Related entry points include bind around lines 1912-1954, listen around lines 1964-1992, accept around lines 2001-2094, and connect beginning around line 2118.

The socket object is then attached to the ordinary file descriptor model: sock_alloc_file() at line 525 creates a file-backed socket handle, sock_map_fd() at line 553 maps it into the process descriptor table, and sockfd_lookup() at line 601 converts a descriptor back into a socket.

Core Data Model

Linux treats sockets as file-like endpoints with protocol-specific behavior. include/linux/net.h defines struct socket around line 137 and stores a protocol operation table pointer around line 146. struct proto_ops begins around line 181 and includes operations such as release, bind, connect, listen, accept, sendmsg, recvmsg, mmap, splice, and peek length.

This is the same architectural move as the VFS: generic syscall and descriptor code stays stable while protocol families plug in their own behavior.

Control Flow

Socket creation has two layers:

  1. __sock_create() at line 1580 selects and constructs the protocol endpoint.
  2. __sys_socket() converts that socket into a user-visible descriptor through file allocation and descriptor-table installation.

Send and receive follow operation dispatch. sock_sendmsg() at line 801 and sock_recvmsg() at line 1144 validate and normalize the request before it reaches the protocol implementation.

Release is equally explicit. __sock_release() at line 701 and sock_release() at line 736 undo the protocol/file/socket lifetime stack.

Concurrency And Authority

Sockets combine descriptor lifetime, file lifetime, protocol state, network namespace, credentials, and LSM hooks. The generic layer must avoid assuming TCP-like behavior because UNIX sockets, raw sockets, netlink sockets, packet sockets, and protocol modules all use the same outer interface.

Rust Translation

A Rust equivalent should make the protocol table a trait:

  • SocketOps for bind/connect/listen/accept/send/recv.
  • Socket as a typed state object with explicit namespace and credential context.
  • SocketFile as the descriptor-facing adapter.
  • State types for unbound, bound, listening, connected, and closed sockets where practical.

The goal is to make invalid socket states unrepresentable at the API boundary while still allowing dynamic protocol families behind trait objects.

AI-Native Translation

For AI systems, sockets are a model for network tools: protocol adapters behind a stable capability membrane. Agents should request explicit connect/listen/send capabilities, and observability should attach to the generic socket layer rather than every protocol implementation separately.

  • file-notes/linux__net__socket.c.md