30 #include <linux/export.h>
41 static const struct proto_ops l2cap_sock_ops;
42 static void l2cap_sock_init(
struct sock *
sk,
struct sock *parent);
47 struct sock *
sk = sock->
sk;
58 len =
min_t(
unsigned int,
sizeof(
la), alen);
61 if (
la.l2_cid &&
la.l2_psm)
75 if ((psm & 0x0101) != 0x0001) {
109 static int l2cap_sock_connect(
struct socket *sock,
struct sockaddr *addr,
int alen,
int flags)
111 struct sock *sk = sock->
sk;
118 if (!addr || alen <
sizeof(addr->
sa_family) ||
123 len =
min_t(
unsigned int,
sizeof(
la), alen);
126 if (
la.l2_cid &&
la.l2_psm)
130 &
la.l2_bdaddr,
la.l2_bdaddr_type);
144 static int l2cap_sock_listen(
struct socket *sock,
int backlog)
146 struct sock *sk = sock->
sk;
150 BT_DBG(
"sk %p backlog %d", sk, backlog);
164 switch (chan->
mode) {
188 static int l2cap_sock_accept(
struct socket *sock,
struct socket *newsock,
int flags)
191 struct sock *sk = sock->
sk, *nsk;
197 timeo = sock_rcvtimeo(sk, flags &
O_NONBLOCK);
199 BT_DBG(
"sk %p timeo %ld", sk, timeo);
221 err = sock_intr_errno(timeo);
237 BT_DBG(
"new socket %p", nsk);
244 static int l2cap_sock_getname(
struct socket *sock,
struct sockaddr *addr,
int *len,
int peer)
247 struct sock *sk = sock->
sk;
250 BT_DBG(
"sock %p, sk %p", sock, sk);
269 static int l2cap_sock_getsockopt_old(
struct socket *sock,
int optname,
char __user *optval,
int __user *optlen)
271 struct sock *sk = sock->
sk;
296 len =
min_t(
unsigned int, len,
sizeof(
opts));
337 memset(&cinfo, 0,
sizeof(cinfo));
338 cinfo.hci_handle = chan->
conn->hcon->handle;
339 memcpy(cinfo.dev_class, chan->
conn->hcon->dev_class, 3);
341 len =
min_t(
unsigned int, len,
sizeof(cinfo));
356 static int l2cap_sock_getsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
int __user *optlen)
358 struct sock *sk = sock->
sk;
367 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
387 sec.level = chan->
conn->hcon->sec_level;
392 sec.key_size = chan->
conn->hcon->enc_key_size;
394 len =
min_t(
unsigned int, len,
sizeof(
sec));
407 (
u32 __user *) optval))
414 (
u32 __user *) optval))
428 len =
min_t(
unsigned int, len,
sizeof(pwr));
453 static bool l2cap_valid_mtu(
struct l2cap_chan *chan,
u16 mtu)
455 switch (chan->
scid) {
469 static int l2cap_sock_setsockopt_old(
struct socket *sock,
int optname,
char __user *optval,
unsigned int optlen)
471 struct sock *sk = sock->
sk;
496 len =
min_t(
unsigned int,
sizeof(
opts), optlen);
507 if (!l2cap_valid_mtu(chan,
opts.imtu)) {
513 switch (chan->
mode) {
567 static int l2cap_sock_setsockopt(
struct socket *sock,
int level,
int optname,
char __user *optval,
unsigned int optlen)
569 struct sock *sk = sock->
sk;
580 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
597 len =
min_t(
unsigned int,
sizeof(
sec), optlen);
618 if (!conn->
hcon->out) {
694 len =
min_t(
unsigned int,
sizeof(pwr), optlen);
700 if (pwr.force_active)
742 struct sock *sk = sock->
sk;
746 BT_DBG(
"sock %p, sk %p", sock, sk);
748 err = sock_error(sk);
758 l2cap_chan_lock(chan);
760 l2cap_chan_unlock(chan);
765 static int l2cap_sock_recvmsg(
struct kiocb *iocb,
struct socket *sock,
struct msghdr *msg,
size_t len,
int flags)
767 struct sock *sk = sock->
sk;
774 &
bt_sk(sk)->flags)) {
822 static void l2cap_sock_kill(
struct sock *sk)
827 BT_DBG(
"sk %p state %s", sk, state_to_string(sk->sk_state));
836 static int l2cap_sock_shutdown(
struct socket *sock,
int how)
838 struct sock *sk = sock->
sk;
843 BT_DBG(
"sock %p, sk %p", sock, sk);
854 l2cap_chan_lock(chan);
876 l2cap_chan_unlock(chan);
884 static int l2cap_sock_release(
struct socket *sock)
886 struct sock *sk = sock->
sk;
889 BT_DBG(
"sock %p, sk %p", sock, sk);
896 err = l2cap_sock_shutdown(sock, 2);
903 static void l2cap_sock_cleanup_listen(
struct sock *parent)
907 BT_DBG(
"parent %p", parent);
913 l2cap_chan_lock(chan);
916 l2cap_chan_unlock(chan);
924 struct sock *
sk, *parent = chan->
data;
927 if (sk_acceptq_is_full(parent)) {
939 l2cap_sock_init(sk, parent);
947 struct sock *sk = chan->
data;
980 static void l2cap_sock_close_cb(
struct l2cap_chan *chan)
982 struct sock *sk = chan->
data;
987 static void l2cap_sock_teardown_cb(
struct l2cap_chan *chan,
int err)
989 struct sock *sk = chan->
data;
994 parent =
bt_sk(sk)->parent;
998 switch (chan->
state) {
1004 l2cap_sock_cleanup_listen(sk);
1028 static void l2cap_sock_state_change_cb(
struct l2cap_chan *chan,
int state)
1030 struct sock *sk = chan->
data;
1032 sk->sk_state =
state;
1036 unsigned long len,
int nb)
1041 l2cap_chan_unlock(chan);
1042 skb = bt_skb_send_alloc(chan->
sk, len, nb, &err);
1043 l2cap_chan_lock(chan);
1046 return ERR_PTR(err);
1051 static void l2cap_sock_ready_cb(
struct l2cap_chan *chan)
1053 struct sock *sk = chan->
data;
1054 struct sock *parent;
1058 parent =
bt_sk(sk)->parent;
1060 BT_DBG(
"sk %p, parent %p", sk, parent);
1071 static struct l2cap_ops l2cap_chan_ops = {
1072 .name =
"L2CAP Socket Interface",
1073 .new_connection = l2cap_sock_new_connection_cb,
1074 .recv = l2cap_sock_recv_cb,
1075 .close = l2cap_sock_close_cb,
1076 .teardown = l2cap_sock_teardown_cb,
1077 .state_change = l2cap_sock_state_change_cb,
1078 .ready = l2cap_sock_ready_cb,
1079 .alloc_skb = l2cap_sock_alloc_skb_cb,
1082 static void l2cap_sock_destruct(
struct sock *sk)
1096 static void l2cap_sock_init(
struct sock *sk,
struct sock *parent)
1121 security_sk_clone(parent, sk);
1153 chan->
ops = &l2cap_chan_ops;
1156 static struct proto l2cap_proto = {
1172 INIT_LIST_HEAD(&
bt_sk(sk)->accept_q);
1197 static int l2cap_sock_create(
struct net *net,
struct socket *sock,
int protocol,
1213 sock->
ops = &l2cap_sock_ops;
1215 sk = l2cap_sock_alloc(net, sock, protocol,
GFP_ATOMIC);
1219 l2cap_sock_init(sk,
NULL);
1224 static const struct proto_ops l2cap_sock_ops = {
1227 .release = l2cap_sock_release,
1228 .bind = l2cap_sock_bind,
1229 .connect = l2cap_sock_connect,
1230 .listen = l2cap_sock_listen,
1231 .accept = l2cap_sock_accept,
1232 .getname = l2cap_sock_getname,
1233 .sendmsg = l2cap_sock_sendmsg,
1234 .recvmsg = l2cap_sock_recvmsg,
1239 .shutdown = l2cap_sock_shutdown,
1240 .setsockopt = l2cap_sock_setsockopt,
1241 .getsockopt = l2cap_sock_getsockopt
1247 .create = l2cap_sock_create,
1260 BT_ERR(
"L2CAP socket registration failed");
1266 BT_ERR(
"Failed to create L2CAP proc file");
1271 BT_INFO(
"L2CAP socket layer initialized");
1284 BT_ERR(
"L2CAP socket unregistration failed");