2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
5 Copyright (C) 2010 Google Inc.
6 Copyright (C) 2011 ProFUSION Embedded Systems
8 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation;
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
18 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25 SOFTWARE IS DISCLAIMED.
28 /* Bluetooth L2CAP sockets. */
30 #include <linux/security.h>
31 #include <linux/export.h>
33 #include <net/bluetooth/bluetooth.h>
34 #include <net/bluetooth/hci_core.h>
35 #include <net/bluetooth/l2cap.h>
36 #include <net/bluetooth/smp.h>
38 static const struct proto_ops l2cap_sock_ops;
39 static void l2cap_sock_init(struct sock *sk, struct sock *parent);
40 static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio);
42 static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
44 struct sock *sk = sock->sk;
45 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
46 struct sockaddr_l2 la;
51 if (!addr || addr->sa_family != AF_BLUETOOTH)
54 memset(&la, 0, sizeof(la));
55 len = min_t(unsigned int, sizeof(la), alen);
56 memcpy(&la, addr, len);
58 if (la.l2_cid && la.l2_psm)
63 if (sk->sk_state != BT_OPEN) {
69 __u16 psm = __le16_to_cpu(la.l2_psm);
71 /* PSM must be odd and lsb of upper byte must be 0 */
72 if ((psm & 0x0101) != 0x0001) {
77 /* Restrict usage of well-known PSMs */
78 if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) {
85 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
87 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
92 if (__le16_to_cpu(la.l2_psm) == 0x0001 ||
93 __le16_to_cpu(la.l2_psm) == 0x0003)
94 chan->sec_level = BT_SECURITY_SDP;
96 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr);
98 chan->state = BT_BOUND;
99 sk->sk_state = BT_BOUND;
106 static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
108 struct sock *sk = sock->sk;
109 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
110 struct sockaddr_l2 la;
115 if (!addr || alen < sizeof(addr->sa_family) ||
116 addr->sa_family != AF_BLUETOOTH)
119 memset(&la, 0, sizeof(la));
120 len = min_t(unsigned int, sizeof(la), alen);
121 memcpy(&la, addr, len);
123 if (la.l2_cid && la.l2_psm)
126 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
133 err = bt_sock_wait_state(sk, BT_CONNECTED,
134 sock_sndtimeo(sk, flags & O_NONBLOCK));
141 static int l2cap_sock_listen(struct socket *sock, int backlog)
143 struct sock *sk = sock->sk;
144 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
147 BT_DBG("sk %p backlog %d", sk, backlog);
151 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
152 || sk->sk_state != BT_BOUND) {
157 switch (chan->mode) {
158 case L2CAP_MODE_BASIC:
160 case L2CAP_MODE_ERTM:
161 case L2CAP_MODE_STREAMING:
170 sk->sk_max_ack_backlog = backlog;
171 sk->sk_ack_backlog = 0;
173 chan->state = BT_LISTEN;
174 sk->sk_state = BT_LISTEN;
181 static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
183 DECLARE_WAITQUEUE(wait, current);
184 struct sock *sk = sock->sk, *nsk;
188 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
190 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
192 BT_DBG("sk %p timeo %ld", sk, timeo);
194 /* Wait for an incoming connection. (wake-one). */
195 add_wait_queue_exclusive(sk_sleep(sk), &wait);
197 set_current_state(TASK_INTERRUPTIBLE);
199 if (sk->sk_state != BT_LISTEN) {
204 nsk = bt_accept_dequeue(sk, newsock);
213 if (signal_pending(current)) {
214 err = sock_intr_errno(timeo);
219 timeo = schedule_timeout(timeo);
220 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
222 __set_current_state(TASK_RUNNING);
223 remove_wait_queue(sk_sleep(sk), &wait);
228 newsock->state = SS_CONNECTED;
230 BT_DBG("new socket %p", nsk);
237 static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
239 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
240 struct sock *sk = sock->sk;
241 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
243 BT_DBG("sock %p, sk %p", sock, sk);
245 addr->sa_family = AF_BLUETOOTH;
246 *len = sizeof(struct sockaddr_l2);
249 la->l2_psm = chan->psm;
250 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst);
251 la->l2_cid = cpu_to_le16(chan->dcid);
253 la->l2_psm = chan->sport;
254 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src);
255 la->l2_cid = cpu_to_le16(chan->scid);
261 static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
263 struct sock *sk = sock->sk;
264 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
265 struct l2cap_options opts;
266 struct l2cap_conninfo cinfo;
272 if (get_user(len, optlen))
279 memset(&opts, 0, sizeof(opts));
280 opts.imtu = chan->imtu;
281 opts.omtu = chan->omtu;
282 opts.flush_to = chan->flush_to;
283 opts.mode = chan->mode;
284 opts.fcs = chan->fcs;
285 opts.max_tx = chan->max_tx;
286 opts.txwin_size = chan->tx_win;
288 len = min_t(unsigned int, len, sizeof(opts));
289 if (copy_to_user(optval, (char *) &opts, len))
295 switch (chan->sec_level) {
296 case BT_SECURITY_LOW:
299 case BT_SECURITY_MEDIUM:
300 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
302 case BT_SECURITY_HIGH:
303 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
311 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
312 opt |= L2CAP_LM_MASTER;
314 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
315 opt |= L2CAP_LM_RELIABLE;
317 if (put_user(opt, (u32 __user *) optval))
322 if (sk->sk_state != BT_CONNECTED &&
323 !(sk->sk_state == BT_CONNECT2 &&
324 bt_sk(sk)->defer_setup)) {
329 memset(&cinfo, 0, sizeof(cinfo));
330 cinfo.hci_handle = chan->conn->hcon->handle;
331 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
333 len = min_t(unsigned int, len, sizeof(cinfo));
334 if (copy_to_user(optval, (char *) &cinfo, len))
348 static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
350 struct sock *sk = sock->sk;
351 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
352 struct bt_security sec;
358 if (level == SOL_L2CAP)
359 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
361 if (level != SOL_BLUETOOTH)
364 if (get_user(len, optlen))
371 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
372 chan->chan_type != L2CAP_CHAN_RAW) {
377 memset(&sec, 0, sizeof(sec));
378 sec.level = chan->sec_level;
380 if (sk->sk_state == BT_CONNECTED)
381 sec.key_size = chan->conn->hcon->enc_key_size;
383 len = min_t(unsigned int, len, sizeof(sec));
384 if (copy_to_user(optval, (char *) &sec, len))
390 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
395 if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval))
401 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
402 (u32 __user *) optval))
408 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
409 && sk->sk_type != SOCK_RAW) {
414 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
416 len = min_t(unsigned int, len, sizeof(pwr));
417 if (copy_to_user(optval, (char *) &pwr, len))
422 case BT_CHANNEL_POLICY:
428 if (put_user(chan->chan_policy, (u32 __user *) optval))
441 static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen)
443 struct sock *sk = sock->sk;
444 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
445 struct l2cap_options opts;
455 if (sk->sk_state == BT_CONNECTED) {
460 opts.imtu = chan->imtu;
461 opts.omtu = chan->omtu;
462 opts.flush_to = chan->flush_to;
463 opts.mode = chan->mode;
464 opts.fcs = chan->fcs;
465 opts.max_tx = chan->max_tx;
466 opts.txwin_size = chan->tx_win;
468 len = min_t(unsigned int, sizeof(opts), optlen);
469 if (copy_from_user((char *) &opts, optval, len)) {
474 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
479 chan->mode = opts.mode;
480 switch (chan->mode) {
481 case L2CAP_MODE_BASIC:
482 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
484 case L2CAP_MODE_ERTM:
485 case L2CAP_MODE_STREAMING:
494 chan->imtu = opts.imtu;
495 chan->omtu = opts.omtu;
496 chan->fcs = opts.fcs;
497 chan->max_tx = opts.max_tx;
498 chan->tx_win = opts.txwin_size;
502 if (get_user(opt, (u32 __user *) optval)) {
507 if (opt & L2CAP_LM_AUTH)
508 chan->sec_level = BT_SECURITY_LOW;
509 if (opt & L2CAP_LM_ENCRYPT)
510 chan->sec_level = BT_SECURITY_MEDIUM;
511 if (opt & L2CAP_LM_SECURE)
512 chan->sec_level = BT_SECURITY_HIGH;
514 if (opt & L2CAP_LM_MASTER)
515 set_bit(FLAG_ROLE_SWITCH, &chan->flags);
517 clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
519 if (opt & L2CAP_LM_RELIABLE)
520 set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
522 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
534 static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
536 struct sock *sk = sock->sk;
537 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
538 struct bt_security sec;
540 struct l2cap_conn *conn;
546 if (level == SOL_L2CAP)
547 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
549 if (level != SOL_BLUETOOTH)
556 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
557 chan->chan_type != L2CAP_CHAN_RAW) {
562 sec.level = BT_SECURITY_LOW;
564 len = min_t(unsigned int, sizeof(sec), optlen);
565 if (copy_from_user((char *) &sec, optval, len)) {
570 if (sec.level < BT_SECURITY_LOW ||
571 sec.level > BT_SECURITY_HIGH) {
576 chan->sec_level = sec.level;
583 /*change security for LE channels */
584 if (chan->scid == L2CAP_CID_LE_DATA) {
585 if (!conn->hcon->out) {
590 if (smp_conn_security(conn, sec.level))
592 sk->sk_state = BT_CONFIG;
593 chan->state = BT_CONFIG;
595 /* or for ACL link, under defer_setup time */
596 } else if (sk->sk_state == BT_CONNECT2 &&
597 bt_sk(sk)->defer_setup) {
598 err = l2cap_chan_check_security(chan);
605 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
610 if (get_user(opt, (u32 __user *) optval)) {
615 bt_sk(sk)->defer_setup = opt;
619 if (get_user(opt, (u32 __user *) optval)) {
624 if (opt > BT_FLUSHABLE_ON) {
629 if (opt == BT_FLUSHABLE_OFF) {
630 struct l2cap_conn *conn = chan->conn;
631 /* proceed further only when we have l2cap_conn and
632 No Flush support in the LM */
633 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
640 set_bit(FLAG_FLUSHABLE, &chan->flags);
642 clear_bit(FLAG_FLUSHABLE, &chan->flags);
646 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
647 chan->chan_type != L2CAP_CHAN_RAW) {
652 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
654 len = min_t(unsigned int, sizeof(pwr), optlen);
655 if (copy_from_user((char *) &pwr, optval, len)) {
660 if (pwr.force_active)
661 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
663 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
666 case BT_CHANNEL_POLICY:
672 if (get_user(opt, (u32 __user *) optval)) {
677 if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
682 if (chan->mode != L2CAP_MODE_ERTM &&
683 chan->mode != L2CAP_MODE_STREAMING) {
688 chan->chan_policy = (u8) opt;
700 static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
702 struct sock *sk = sock->sk;
703 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
706 BT_DBG("sock %p, sk %p", sock, sk);
708 err = sock_error(sk);
712 if (msg->msg_flags & MSG_OOB)
717 if (sk->sk_state != BT_CONNECTED) {
722 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
728 static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags)
730 struct sock *sk = sock->sk;
731 struct l2cap_pinfo *pi = l2cap_pi(sk);
736 if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) {
737 sk->sk_state = BT_CONFIG;
738 pi->chan->state = BT_CONFIG;
740 __l2cap_connect_rsp_defer(pi->chan);
747 if (sock->type == SOCK_STREAM)
748 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags);
750 err = bt_sock_recvmsg(iocb, sock, msg, len, flags);
752 if (pi->chan->mode != L2CAP_MODE_ERTM)
755 /* Attempt to put pending rx data in the socket buffer */
759 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
762 if (pi->rx_busy_skb) {
763 if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb))
764 pi->rx_busy_skb = NULL;
769 /* Restore data flow when half of the receive buffer is
770 * available. This avoids resending large numbers of
773 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
774 l2cap_chan_busy(pi->chan, 0);
781 /* Kill socket (only if zapped and orphan)
782 * Must be called on unlocked socket.
784 static void l2cap_sock_kill(struct sock *sk)
786 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
789 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
791 /* Kill poor orphan */
793 l2cap_chan_destroy(l2cap_pi(sk)->chan);
794 sock_set_flag(sk, SOCK_DEAD);
798 static int l2cap_sock_shutdown(struct socket *sock, int how)
800 struct sock *sk = sock->sk;
801 struct l2cap_chan *chan;
802 struct l2cap_conn *conn;
805 BT_DBG("sock %p, sk %p", sock, sk);
810 chan = l2cap_pi(sk)->chan;
814 mutex_lock(&conn->chan_lock);
816 l2cap_chan_lock(chan);
819 if (!sk->sk_shutdown) {
820 if (chan->mode == L2CAP_MODE_ERTM)
821 err = __l2cap_wait_ack(sk);
823 sk->sk_shutdown = SHUTDOWN_MASK;
826 l2cap_chan_close(chan, 0);
829 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
830 err = bt_sock_wait_state(sk, BT_CLOSED,
834 if (!err && sk->sk_err)
838 l2cap_chan_unlock(chan);
841 mutex_unlock(&conn->chan_lock);
846 static int l2cap_sock_release(struct socket *sock)
848 struct sock *sk = sock->sk;
851 BT_DBG("sock %p, sk %p", sock, sk);
856 err = l2cap_sock_shutdown(sock, 2);
863 static struct l2cap_chan *l2cap_sock_new_connection_cb(void *data)
865 struct sock *sk, *parent = data;
867 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
872 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
874 l2cap_sock_init(sk, parent);
876 return l2cap_pi(sk)->chan;
879 static int l2cap_sock_recv_cb(void *data, struct sk_buff *skb)
882 struct sock *sk = data;
883 struct l2cap_pinfo *pi = l2cap_pi(sk);
887 if (pi->rx_busy_skb) {
892 err = sock_queue_rcv_skb(sk, skb);
894 /* For ERTM, handle one skb that doesn't fit into the recv
895 * buffer. This is important to do because the data frames
896 * have already been acked, so the skb cannot be discarded.
898 * Notify the l2cap core that the buffer is full, so the
899 * LOCAL_BUSY state is entered and no more frames are
900 * acked and reassembled until there is buffer space
903 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) {
904 pi->rx_busy_skb = skb;
905 l2cap_chan_busy(pi->chan, 1);
915 static void l2cap_sock_close_cb(void *data)
917 struct sock *sk = data;
922 static void l2cap_sock_state_change_cb(void *data, int state)
924 struct sock *sk = data;
926 sk->sk_state = state;
929 static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
930 unsigned long len, int nb,
933 struct sock *sk = chan->sk;
935 return bt_skb_send_alloc(sk, len, nb, err);
938 static struct l2cap_ops l2cap_chan_ops = {
939 .name = "L2CAP Socket Interface",
940 .new_connection = l2cap_sock_new_connection_cb,
941 .recv = l2cap_sock_recv_cb,
942 .close = l2cap_sock_close_cb,
943 .state_change = l2cap_sock_state_change_cb,
944 .alloc_skb = l2cap_sock_alloc_skb_cb,
947 static void l2cap_sock_destruct(struct sock *sk)
951 if (l2cap_pi(sk)->rx_busy_skb) {
952 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
953 l2cap_pi(sk)->rx_busy_skb = NULL;
956 skb_queue_purge(&sk->sk_receive_queue);
957 skb_queue_purge(&sk->sk_write_queue);
960 static void l2cap_sock_init(struct sock *sk, struct sock *parent)
962 struct l2cap_pinfo *pi = l2cap_pi(sk);
963 struct l2cap_chan *chan = pi->chan;
968 struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
970 sk->sk_type = parent->sk_type;
971 bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup;
973 chan->chan_type = pchan->chan_type;
974 chan->imtu = pchan->imtu;
975 chan->omtu = pchan->omtu;
976 chan->conf_state = pchan->conf_state;
977 chan->mode = pchan->mode;
978 chan->fcs = pchan->fcs;
979 chan->max_tx = pchan->max_tx;
980 chan->tx_win = pchan->tx_win;
981 chan->tx_win_max = pchan->tx_win_max;
982 chan->sec_level = pchan->sec_level;
983 chan->flags = pchan->flags;
985 security_sk_clone(parent, sk);
988 switch (sk->sk_type) {
990 chan->chan_type = L2CAP_CHAN_RAW;
993 chan->chan_type = L2CAP_CHAN_CONN_LESS;
997 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
1001 chan->imtu = L2CAP_DEFAULT_MTU;
1003 if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
1004 chan->mode = L2CAP_MODE_ERTM;
1005 set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
1007 chan->mode = L2CAP_MODE_BASIC;
1009 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
1010 chan->fcs = L2CAP_FCS_CRC16;
1011 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
1012 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
1013 chan->sec_level = BT_SECURITY_LOW;
1015 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
1018 /* Default config options */
1019 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
1022 chan->ops = &l2cap_chan_ops;
1025 static struct proto l2cap_proto = {
1027 .owner = THIS_MODULE,
1028 .obj_size = sizeof(struct l2cap_pinfo)
1031 static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
1034 struct l2cap_chan *chan;
1036 sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto);
1040 sock_init_data(sock, sk);
1041 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
1043 sk->sk_destruct = l2cap_sock_destruct;
1044 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
1046 sock_reset_flag(sk, SOCK_ZAPPED);
1048 sk->sk_protocol = proto;
1049 sk->sk_state = BT_OPEN;
1051 chan = l2cap_chan_create(sk);
1053 l2cap_sock_kill(sk);
1057 l2cap_pi(sk)->chan = chan;
1062 static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
1067 BT_DBG("sock %p", sock);
1069 sock->state = SS_UNCONNECTED;
1071 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
1072 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1073 return -ESOCKTNOSUPPORT;
1075 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1078 sock->ops = &l2cap_sock_ops;
1080 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC);
1084 l2cap_sock_init(sk, NULL);
1088 static const struct proto_ops l2cap_sock_ops = {
1089 .family = PF_BLUETOOTH,
1090 .owner = THIS_MODULE,
1091 .release = l2cap_sock_release,
1092 .bind = l2cap_sock_bind,
1093 .connect = l2cap_sock_connect,
1094 .listen = l2cap_sock_listen,
1095 .accept = l2cap_sock_accept,
1096 .getname = l2cap_sock_getname,
1097 .sendmsg = l2cap_sock_sendmsg,
1098 .recvmsg = l2cap_sock_recvmsg,
1099 .poll = bt_sock_poll,
1100 .ioctl = bt_sock_ioctl,
1101 .mmap = sock_no_mmap,
1102 .socketpair = sock_no_socketpair,
1103 .shutdown = l2cap_sock_shutdown,
1104 .setsockopt = l2cap_sock_setsockopt,
1105 .getsockopt = l2cap_sock_getsockopt
1108 static const struct net_proto_family l2cap_sock_family_ops = {
1109 .family = PF_BLUETOOTH,
1110 .owner = THIS_MODULE,
1111 .create = l2cap_sock_create,
1114 int __init l2cap_init_sockets(void)
1118 err = proto_register(&l2cap_proto, 0);
1122 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
1126 BT_INFO("L2CAP socket layer initialized");
1131 BT_ERR("L2CAP socket registration failed");
1132 proto_unregister(&l2cap_proto);
1136 void l2cap_cleanup_sockets(void)
1138 if (bt_sock_unregister(BTPROTO_L2CAP) < 0)
1139 BT_ERR("L2CAP socket unregistration failed");
1141 proto_unregister(&l2cap_proto);