net/socket.c
Source file repositories/reference/linux-study-clean/net/socket.c
File Facts
- System
- Linux kernel
- Corpus path
net/socket.c- Extension
.c- Size
- 95868 bytes
- Lines
- 3823
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bpf-cgroup.hlinux/ethtool.hlinux/mm.hlinux/socket.hlinux/file.hlinux/splice.hlinux/net.hlinux/interrupt.hlinux/thread_info.hlinux/rcupdate.hlinux/netdevice.hlinux/proc_fs.hlinux/seq_file.hlinux/mutex.hlinux/if_bridge.hlinux/if_vlan.hlinux/ptp_classify.hlinux/init.hlinux/poll.hlinux/cache.hlinux/module.hlinux/highmem.hlinux/mount.hlinux/pseudo_fs.hlinux/security.hlinux/uio.hlinux/syscalls.hlinux/compat.hlinux/kmod.hlinux/audit.hlinux/wireless.hlinux/nsproxy.h
Detected Declarations
syscall socketsyscall socketpairsyscall bindsyscall listensyscall accept4syscall acceptsyscall connectsyscall getsocknamesyscall getpeernamesyscall sendtosyscall sendsyscall recvfromsyscall recvsyscall setsockoptsyscall getsockoptsyscall shutdownsyscall sendmsgsyscall sendmmsgsyscall recvmsgsyscall recvmmsgsyscall recvmmsg_time32syscall socketcallstruct sockfs_inodestruct socket_allocstruct used_addressfunction sock_show_fdinfofunction move_addr_to_kernelfunction move_addr_to_userfunction scoped_user_rw_access_sizefunction sock_evict_inodefunction sock_free_inodefunction init_oncefunction init_inodecachefunction sockfs_dnamefunction sockfs_xattr_getfunction sockfs_security_xattr_setfunction sockfs_user_xattr_getfunction sockfs_user_xattr_setfunction sockfs_init_fs_contextfunction sock_map_fdfunction sockfs_listxattrfunction sockfs_setattrfunction __sock_releasefunction sock_releasefunction __sock_tx_timestampfunction call_trace_sock_send_lengthfunction sock_sendmsg_nosecfunction __sock_sendmsg
Annotated Snippet
SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
{
return __sys_socket(family, type, protocol);
}
/*
* Create a pair of connected sockets.
*/
int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
{
struct socket *sock1, *sock2;
int fd1, fd2, err;
struct file *newfile1, *newfile2;
int flags;
flags = type & ~SOCK_TYPE_MASK;
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
return -EINVAL;
type &= SOCK_TYPE_MASK;
if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
/*
* reserve descriptors and make sure we won't fail
* to return them to userland.
*/
fd1 = get_unused_fd_flags(flags);
if (unlikely(fd1 < 0))
return fd1;
fd2 = get_unused_fd_flags(flags);
if (unlikely(fd2 < 0)) {
put_unused_fd(fd1);
return fd2;
}
err = put_user(fd1, &usockvec[0]);
if (err)
goto out;
err = put_user(fd2, &usockvec[1]);
if (err)
goto out;
/*
* Obtain the first socket and check if the underlying protocol
* supports the socketpair call.
*/
err = sock_create(family, type, protocol, &sock1);
if (unlikely(err < 0))
goto out;
err = sock_create(family, type, protocol, &sock2);
if (unlikely(err < 0)) {
sock_release(sock1);
goto out;
}
err = security_socket_socketpair(sock1, sock2);
if (unlikely(err)) {
sock_release(sock2);
sock_release(sock1);
goto out;
}
err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2);
if (unlikely(err < 0)) {
sock_release(sock2);
sock_release(sock1);
goto out;
}
newfile1 = sock_alloc_file(sock1, flags, NULL);
if (IS_ERR(newfile1)) {
err = PTR_ERR(newfile1);
sock_release(sock2);
goto out;
}
newfile2 = sock_alloc_file(sock2, flags, NULL);
if (IS_ERR(newfile2)) {
err = PTR_ERR(newfile2);
fput(newfile1);
goto out;
}
audit_fd_pair(fd1, fd2);
Annotation
- Immediate include surface: `linux/bpf-cgroup.h`, `linux/ethtool.h`, `linux/mm.h`, `linux/socket.h`, `linux/file.h`, `linux/splice.h`, `linux/net.h`, `linux/interrupt.h`.
- Detected declarations: `syscall socket`, `syscall socketpair`, `syscall bind`, `syscall listen`, `syscall accept4`, `syscall accept`, `syscall connect`, `syscall getsockname`, `syscall getpeername`, `syscall sendto`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: core implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.