net/bluetooth/l2cap_sock.c
Source file repositories/reference/linux-study-clean/net/bluetooth/l2cap_sock.c
File Facts
- System
- Linux kernel
- Corpus path
net/bluetooth/l2cap_sock.c- Extension
.c- Size
- 46892 bytes
- Lines
- 2100
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: operation-table or driver-model contract
- Status
- pattern 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 an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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/module.hlinux/export.hlinux/filter.hlinux/sched/signal.hlinux/uio.hnet/bluetooth/bluetooth.hnet/bluetooth/hci_core.hnet/bluetooth/l2cap.hsmp.h
Detected Declarations
function l2cap_is_socketfunction l2cap_validate_bredr_psmfunction l2cap_validate_le_psmfunction l2cap_sock_bindfunction l2cap_sock_connectfunction connectfunction l2cap_sock_listenfunction l2cap_sock_acceptfunction l2cap_sock_getnamefunction l2cap_get_modefunction l2cap_sock_getsockopt_oldfunction l2cap_sock_getsockoptfunction l2cap_valid_mtufunction l2cap_sock_setsockopt_oldfunction l2cap_set_modefunction l2cap_sock_setsockoptfunction test_bitfunction l2cap_sock_sendmsgfunction l2cap_publish_rx_availfunction l2cap_sock_recvmsgfunction l2cap_sock_killfunction __l2cap_wait_ackfunction l2cap_sock_shutdownfunction l2cap_sock_releasefunction l2cap_sock_cleanup_listenfunction l2cap_sock_recv_cbfunction l2cap_sock_close_cbfunction l2cap_sock_teardown_cbfunction l2cap_sock_state_change_cbfunction l2cap_sock_ready_cbfunction l2cap_sock_defer_cbfunction l2cap_sock_resume_cbfunction l2cap_sock_set_shutdown_cbfunction l2cap_sock_get_sndtimeo_cbfunction l2cap_sock_suspend_cbfunction l2cap_sock_filterfunction l2cap_sock_destructfunction list_for_each_entry_safefunction l2cap_skb_msg_namefunction l2cap_sock_initfunction l2cap_sock_createfunction l2cap_init_socketsfunction l2cap_cleanup_socketsexport l2cap_is_socket
Annotated Snippet
static const struct proto_ops l2cap_sock_ops;
static void l2cap_sock_init(struct sock *sk, struct sock *parent);
static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
int proto, gfp_t prio, int kern);
static void l2cap_sock_cleanup_listen(struct sock *parent);
bool l2cap_is_socket(struct socket *sock)
{
return sock && sock->ops == &l2cap_sock_ops;
}
EXPORT_SYMBOL(l2cap_is_socket);
static int l2cap_validate_bredr_psm(u16 psm)
{
/* PSM must be odd and lsb of upper byte must be 0 */
if ((psm & 0x0101) != 0x0001)
return -EINVAL;
/* Restrict usage of well-known PSMs */
if (psm < L2CAP_PSM_DYN_START && !capable(CAP_NET_BIND_SERVICE))
return -EACCES;
return 0;
}
static int l2cap_validate_le_psm(u16 psm)
{
/* Valid LE_PSM ranges are defined only until 0x00ff */
if (psm > L2CAP_PSM_LE_DYN_END)
return -EINVAL;
/* Restrict fixed, SIG assigned PSM values to CAP_NET_BIND_SERVICE */
if (psm < L2CAP_PSM_LE_DYN_START && !capable(CAP_NET_BIND_SERVICE))
return -EACCES;
return 0;
}
static int l2cap_sock_bind(struct socket *sock, struct sockaddr_unsized *addr, int alen)
{
struct sock *sk = sock->sk;
struct l2cap_chan *chan = l2cap_pi(sk)->chan;
struct sockaddr_l2 la;
int len, err = 0;
BT_DBG("sk %p", sk);
if (!addr || alen < offsetofend(struct sockaddr, sa_family) ||
addr->sa_family != AF_BLUETOOTH)
return -EINVAL;
memset(&la, 0, sizeof(la));
len = min_t(unsigned int, sizeof(la), alen);
memcpy(&la, addr, len);
if (la.l2_cid && la.l2_psm)
return -EINVAL;
if (!bdaddr_type_is_valid(la.l2_bdaddr_type))
return -EINVAL;
if (bdaddr_type_is_le(la.l2_bdaddr_type)) {
/* We only allow ATT user space socket */
if (la.l2_cid &&
la.l2_cid != cpu_to_le16(L2CAP_CID_ATT))
return -EINVAL;
}
lock_sock(sk);
if (sk->sk_state != BT_OPEN) {
err = -EBADFD;
goto done;
}
if (la.l2_psm) {
__u16 psm = __le16_to_cpu(la.l2_psm);
if (la.l2_bdaddr_type == BDADDR_BREDR)
err = l2cap_validate_bredr_psm(psm);
else
err = l2cap_validate_le_psm(psm);
if (err)
goto done;
}
bacpy(&chan->src, &la.l2_bdaddr);
chan->src_type = la.l2_bdaddr_type;
Annotation
- Immediate include surface: `linux/module.h`, `linux/export.h`, `linux/filter.h`, `linux/sched/signal.h`, `linux/uio.h`, `net/bluetooth/bluetooth.h`, `net/bluetooth/hci_core.h`, `net/bluetooth/l2cap.h`.
- Detected declarations: `function l2cap_is_socket`, `function l2cap_validate_bredr_psm`, `function l2cap_validate_le_psm`, `function l2cap_sock_bind`, `function l2cap_sock_connect`, `function connect`, `function l2cap_sock_listen`, `function l2cap_sock_accept`, `function l2cap_sock_getname`, `function l2cap_get_mode`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: pattern implementation candidate.
- 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.