2 RFCOMM implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
39 #include <linux/socket.h>
40 #include <linux/skbuff.h>
41 #include <linux/list.h>
42 #include <linux/device.h>
45 #include <asm/system.h>
46 #include <asm/uaccess.h>
48 #include <net/bluetooth/bluetooth.h>
49 #include <net/bluetooth/hci_core.h>
50 #include <net/bluetooth/l2cap.h>
51 #include <net/bluetooth/rfcomm.h>
53 #ifndef CONFIG_BT_RFCOMM_DEBUG
58 static const struct proto_ops rfcomm_sock_ops;
60 static struct bt_sock_list rfcomm_sk_list = {
61 .lock = __RW_LOCK_UNLOCKED(rfcomm_sk_list.lock)
64 static void rfcomm_sock_close(struct sock *sk);
65 static void rfcomm_sock_kill(struct sock *sk);
67 /* ---- DLC callbacks ----
69 * called under rfcomm_dlc_lock()
71 static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)
73 struct sock *sk = d->owner;
77 atomic_add(skb->len, &sk->sk_rmem_alloc);
78 skb_queue_tail(&sk->sk_receive_queue, skb);
79 sk->sk_data_ready(sk, skb->len);
81 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
82 rfcomm_dlc_throttle(d);
85 static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
87 struct sock *sk = d->owner, *parent;
91 BT_DBG("dlc %p state %ld err %d", d, d->state, err);
98 sk->sk_state = d->state;
100 parent = bt_sk(sk)->parent;
102 if (d->state == BT_CLOSED) {
103 sock_set_flag(sk, SOCK_ZAPPED);
104 bt_accept_unlink(sk);
106 parent->sk_data_ready(parent, 0);
108 if (d->state == BT_CONNECTED)
109 rfcomm_session_getaddr(d->session, &bt_sk(sk)->src, NULL);
110 sk->sk_state_change(sk);
115 if (parent && sock_flag(sk, SOCK_ZAPPED)) {
116 /* We have to drop DLC lock here, otherwise
117 * rfcomm_sock_destruct() will dead lock. */
118 rfcomm_dlc_unlock(d);
119 rfcomm_sock_kill(sk);
124 /* ---- Socket functions ---- */
125 static struct sock *__rfcomm_get_sock_by_addr(u8 channel, bdaddr_t *src)
127 struct sock *sk = NULL;
128 struct hlist_node *node;
130 sk_for_each(sk, node, &rfcomm_sk_list.head) {
131 if (rfcomm_pi(sk)->channel == channel &&
132 !bacmp(&bt_sk(sk)->src, src))
136 return node ? sk : NULL;
139 /* Find socket with channel and source bdaddr.
140 * Returns closest match.
142 static struct sock *__rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
144 struct sock *sk = NULL, *sk1 = NULL;
145 struct hlist_node *node;
147 sk_for_each(sk, node, &rfcomm_sk_list.head) {
148 if (state && sk->sk_state != state)
151 if (rfcomm_pi(sk)->channel == channel) {
153 if (!bacmp(&bt_sk(sk)->src, src))
157 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
161 return node ? sk : sk1;
164 /* Find socket with given address (channel, src).
165 * Returns locked socket */
166 static inline struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
169 read_lock(&rfcomm_sk_list.lock);
170 s = __rfcomm_get_sock_by_channel(state, channel, src);
171 if (s) bh_lock_sock(s);
172 read_unlock(&rfcomm_sk_list.lock);
176 static void rfcomm_sock_destruct(struct sock *sk)
178 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
180 BT_DBG("sk %p dlc %p", sk, d);
182 skb_queue_purge(&sk->sk_receive_queue);
183 skb_queue_purge(&sk->sk_write_queue);
186 rfcomm_pi(sk)->dlc = NULL;
188 /* Detach DLC if it's owned by this socket */
191 rfcomm_dlc_unlock(d);
196 static void rfcomm_sock_cleanup_listen(struct sock *parent)
200 BT_DBG("parent %p", parent);
202 /* Close not yet accepted dlcs */
203 while ((sk = bt_accept_dequeue(parent, NULL))) {
204 rfcomm_sock_close(sk);
205 rfcomm_sock_kill(sk);
208 parent->sk_state = BT_CLOSED;
209 sock_set_flag(parent, SOCK_ZAPPED);
212 /* Kill socket (only if zapped and orphan)
213 * Must be called on unlocked socket.
215 static void rfcomm_sock_kill(struct sock *sk)
217 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
220 BT_DBG("sk %p state %d refcnt %d", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
222 /* Kill poor orphan */
223 bt_sock_unlink(&rfcomm_sk_list, sk);
224 sock_set_flag(sk, SOCK_DEAD);
228 static void __rfcomm_sock_close(struct sock *sk)
230 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
232 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
234 switch (sk->sk_state) {
236 rfcomm_sock_cleanup_listen(sk);
243 rfcomm_dlc_close(d, 0);
246 sock_set_flag(sk, SOCK_ZAPPED);
252 * Must be called on unlocked socket.
254 static void rfcomm_sock_close(struct sock *sk)
257 __rfcomm_sock_close(sk);
261 static void rfcomm_sock_init(struct sock *sk, struct sock *parent)
263 struct rfcomm_pinfo *pi = rfcomm_pi(sk);
268 sk->sk_type = parent->sk_type;
269 pi->link_mode = rfcomm_pi(parent)->link_mode;
274 pi->dlc->link_mode = pi->link_mode;
277 static struct proto rfcomm_proto = {
279 .owner = THIS_MODULE,
280 .obj_size = sizeof(struct rfcomm_pinfo)
283 static struct sock *rfcomm_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
285 struct rfcomm_dlc *d;
288 sk = sk_alloc(net, PF_BLUETOOTH, prio, &rfcomm_proto);
292 sock_init_data(sock, sk);
293 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
295 d = rfcomm_dlc_alloc(prio);
301 d->data_ready = rfcomm_sk_data_ready;
302 d->state_change = rfcomm_sk_state_change;
304 rfcomm_pi(sk)->dlc = d;
307 sk->sk_destruct = rfcomm_sock_destruct;
308 sk->sk_sndtimeo = RFCOMM_CONN_TIMEOUT;
310 sk->sk_sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
311 sk->sk_rcvbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
313 sock_reset_flag(sk, SOCK_ZAPPED);
315 sk->sk_protocol = proto;
316 sk->sk_state = BT_OPEN;
318 bt_sock_link(&rfcomm_sk_list, sk);
324 static int rfcomm_sock_create(struct net *net, struct socket *sock, int protocol)
328 BT_DBG("sock %p", sock);
330 sock->state = SS_UNCONNECTED;
332 if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW)
333 return -ESOCKTNOSUPPORT;
335 sock->ops = &rfcomm_sock_ops;
337 sk = rfcomm_sock_alloc(net, sock, protocol, GFP_ATOMIC);
341 rfcomm_sock_init(sk, NULL);
345 static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
347 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
348 struct sock *sk = sock->sk;
351 BT_DBG("sk %p %s", sk, batostr(&sa->rc_bdaddr));
353 if (!addr || addr->sa_family != AF_BLUETOOTH)
358 if (sk->sk_state != BT_OPEN) {
363 if (sk->sk_type != SOCK_STREAM) {
368 write_lock_bh(&rfcomm_sk_list.lock);
370 if (sa->rc_channel && __rfcomm_get_sock_by_addr(sa->rc_channel, &sa->rc_bdaddr)) {
373 /* Save source address */
374 bacpy(&bt_sk(sk)->src, &sa->rc_bdaddr);
375 rfcomm_pi(sk)->channel = sa->rc_channel;
376 sk->sk_state = BT_BOUND;
379 write_unlock_bh(&rfcomm_sk_list.lock);
386 static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
388 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
389 struct sock *sk = sock->sk;
390 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
395 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_rc))
400 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
405 if (sk->sk_type != SOCK_STREAM) {
410 sk->sk_state = BT_CONNECT;
411 bacpy(&bt_sk(sk)->dst, &sa->rc_bdaddr);
412 rfcomm_pi(sk)->channel = sa->rc_channel;
414 d->link_mode = rfcomm_pi(sk)->link_mode;
416 err = rfcomm_dlc_open(d, &bt_sk(sk)->src, &sa->rc_bdaddr, sa->rc_channel);
418 err = bt_sock_wait_state(sk, BT_CONNECTED,
419 sock_sndtimeo(sk, flags & O_NONBLOCK));
426 static int rfcomm_sock_listen(struct socket *sock, int backlog)
428 struct sock *sk = sock->sk;
431 BT_DBG("sk %p backlog %d", sk, backlog);
435 if (sk->sk_state != BT_BOUND) {
440 if (sk->sk_type != SOCK_STREAM) {
445 if (!rfcomm_pi(sk)->channel) {
446 bdaddr_t *src = &bt_sk(sk)->src;
451 write_lock_bh(&rfcomm_sk_list.lock);
453 for (channel = 1; channel < 31; channel++)
454 if (!__rfcomm_get_sock_by_addr(channel, src)) {
455 rfcomm_pi(sk)->channel = channel;
460 write_unlock_bh(&rfcomm_sk_list.lock);
466 sk->sk_max_ack_backlog = backlog;
467 sk->sk_ack_backlog = 0;
468 sk->sk_state = BT_LISTEN;
475 static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
477 DECLARE_WAITQUEUE(wait, current);
478 struct sock *sk = sock->sk, *nsk;
484 if (sk->sk_state != BT_LISTEN) {
489 if (sk->sk_type != SOCK_STREAM) {
494 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
496 BT_DBG("sk %p timeo %ld", sk, timeo);
498 /* Wait for an incoming connection. (wake-one). */
499 add_wait_queue_exclusive(sk->sk_sleep, &wait);
500 while (!(nsk = bt_accept_dequeue(sk, newsock))) {
501 set_current_state(TASK_INTERRUPTIBLE);
508 timeo = schedule_timeout(timeo);
511 if (sk->sk_state != BT_LISTEN) {
516 if (signal_pending(current)) {
517 err = sock_intr_errno(timeo);
521 set_current_state(TASK_RUNNING);
522 remove_wait_queue(sk->sk_sleep, &wait);
527 newsock->state = SS_CONNECTED;
529 BT_DBG("new socket %p", nsk);
536 static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
538 struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
539 struct sock *sk = sock->sk;
541 BT_DBG("sock %p, sk %p", sock, sk);
543 sa->rc_family = AF_BLUETOOTH;
544 sa->rc_channel = rfcomm_pi(sk)->channel;
546 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->dst);
548 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->src);
550 *len = sizeof(struct sockaddr_rc);
554 static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
555 struct msghdr *msg, size_t len)
557 struct sock *sk = sock->sk;
558 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
562 if (msg->msg_flags & MSG_OOB)
565 if (sk->sk_shutdown & SEND_SHUTDOWN)
568 BT_DBG("sock %p, sk %p", sock, sk);
573 size_t size = min_t(size_t, len, d->mtu);
576 skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
577 msg->msg_flags & MSG_DONTWAIT, &err);
580 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
582 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
590 err = rfcomm_dlc_send(d, skb);
607 static long rfcomm_sock_data_wait(struct sock *sk, long timeo)
609 DECLARE_WAITQUEUE(wait, current);
611 add_wait_queue(sk->sk_sleep, &wait);
613 set_current_state(TASK_INTERRUPTIBLE);
615 if (!skb_queue_empty(&sk->sk_receive_queue) ||
617 (sk->sk_shutdown & RCV_SHUTDOWN) ||
618 signal_pending(current) ||
622 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
624 timeo = schedule_timeout(timeo);
626 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
629 __set_current_state(TASK_RUNNING);
630 remove_wait_queue(sk->sk_sleep, &wait);
634 static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
635 struct msghdr *msg, size_t size, int flags)
637 struct sock *sk = sock->sk;
639 size_t target, copied = 0;
645 msg->msg_namelen = 0;
647 BT_DBG("sk %p size %d", sk, size);
651 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
652 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
658 skb = skb_dequeue(&sk->sk_receive_queue);
660 if (copied >= target)
663 if ((err = sock_error(sk)) != 0)
665 if (sk->sk_shutdown & RCV_SHUTDOWN)
672 timeo = rfcomm_sock_data_wait(sk, timeo);
674 if (signal_pending(current)) {
675 err = sock_intr_errno(timeo);
681 chunk = min_t(unsigned int, skb->len, size);
682 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
683 skb_queue_head(&sk->sk_receive_queue, skb);
691 sock_recv_timestamp(msg, sk, skb);
693 if (!(flags & MSG_PEEK)) {
694 atomic_sub(chunk, &sk->sk_rmem_alloc);
696 skb_pull(skb, chunk);
698 skb_queue_head(&sk->sk_receive_queue, skb);
704 /* put message back and return */
705 skb_queue_head(&sk->sk_receive_queue, skb);
711 if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2))
712 rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
715 return copied ? : err;
718 static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
720 struct sock *sk = sock->sk;
730 if (get_user(opt, (u32 __user *) optval)) {
735 rfcomm_pi(sk)->link_mode = opt;
747 static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
749 struct sock *sk = sock->sk;
750 struct sock *l2cap_sk;
751 struct rfcomm_conninfo cinfo;
756 if (get_user(len, optlen))
763 if (put_user(rfcomm_pi(sk)->link_mode, (u32 __user *) optval))
767 case RFCOMM_CONNINFO:
768 if (sk->sk_state != BT_CONNECTED) {
773 l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
775 cinfo.hci_handle = l2cap_pi(l2cap_sk)->conn->hcon->handle;
776 memcpy(cinfo.dev_class, l2cap_pi(l2cap_sk)->conn->hcon->dev_class, 3);
778 len = min_t(unsigned int, len, sizeof(cinfo));
779 if (copy_to_user(optval, (char *) &cinfo, len))
793 static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
795 #if defined(CONFIG_BT_RFCOMM_TTY) || defined(CONFIG_BT_RFCOMM_DEBUG)
796 struct sock *sk = sock->sk;
800 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
802 err = bt_sock_ioctl(sock, cmd, arg);
804 if (err == -ENOIOCTLCMD) {
805 #ifdef CONFIG_BT_RFCOMM_TTY
807 err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
817 static int rfcomm_sock_shutdown(struct socket *sock, int how)
819 struct sock *sk = sock->sk;
822 BT_DBG("sock %p, sk %p", sock, sk);
827 if (!sk->sk_shutdown) {
828 sk->sk_shutdown = SHUTDOWN_MASK;
829 __rfcomm_sock_close(sk);
831 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
832 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
838 static int rfcomm_sock_release(struct socket *sock)
840 struct sock *sk = sock->sk;
843 BT_DBG("sock %p, sk %p", sock, sk);
848 err = rfcomm_sock_shutdown(sock, 2);
851 rfcomm_sock_kill(sk);
855 /* ---- RFCOMM core layer callbacks ----
857 * called under rfcomm_lock()
859 int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
861 struct sock *sk, *parent;
865 BT_DBG("session %p channel %d", s, channel);
867 rfcomm_session_getaddr(s, &src, &dst);
869 /* Check if we have socket listening on channel */
870 parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
874 /* Check for backlog size */
875 if (sk_acceptq_is_full(parent)) {
876 BT_DBG("backlog full %d", parent->sk_ack_backlog);
880 sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
884 rfcomm_sock_init(sk, parent);
885 bacpy(&bt_sk(sk)->src, &src);
886 bacpy(&bt_sk(sk)->dst, &dst);
887 rfcomm_pi(sk)->channel = channel;
889 sk->sk_state = BT_CONFIG;
890 bt_accept_enqueue(parent, sk);
892 /* Accept connection and return socket DLC */
893 *d = rfcomm_pi(sk)->dlc;
897 bh_unlock_sock(parent);
901 static ssize_t rfcomm_sock_sysfs_show(struct class *dev, char *buf)
904 struct hlist_node *node;
907 read_lock_bh(&rfcomm_sk_list.lock);
909 sk_for_each(sk, node, &rfcomm_sk_list.head) {
910 str += sprintf(str, "%s %s %d %d\n",
911 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
912 sk->sk_state, rfcomm_pi(sk)->channel);
915 read_unlock_bh(&rfcomm_sk_list.lock);
920 static CLASS_ATTR(rfcomm, S_IRUGO, rfcomm_sock_sysfs_show, NULL);
922 static const struct proto_ops rfcomm_sock_ops = {
923 .family = PF_BLUETOOTH,
924 .owner = THIS_MODULE,
925 .release = rfcomm_sock_release,
926 .bind = rfcomm_sock_bind,
927 .connect = rfcomm_sock_connect,
928 .listen = rfcomm_sock_listen,
929 .accept = rfcomm_sock_accept,
930 .getname = rfcomm_sock_getname,
931 .sendmsg = rfcomm_sock_sendmsg,
932 .recvmsg = rfcomm_sock_recvmsg,
933 .shutdown = rfcomm_sock_shutdown,
934 .setsockopt = rfcomm_sock_setsockopt,
935 .getsockopt = rfcomm_sock_getsockopt,
936 .ioctl = rfcomm_sock_ioctl,
937 .poll = bt_sock_poll,
938 .socketpair = sock_no_socketpair,
942 static struct net_proto_family rfcomm_sock_family_ops = {
943 .family = PF_BLUETOOTH,
944 .owner = THIS_MODULE,
945 .create = rfcomm_sock_create
948 int __init rfcomm_init_sockets(void)
952 err = proto_register(&rfcomm_proto, 0);
956 err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops);
960 if (class_create_file(bt_class, &class_attr_rfcomm) < 0)
961 BT_ERR("Failed to create RFCOMM info file");
963 BT_INFO("RFCOMM socket layer initialized");
968 BT_ERR("RFCOMM socket layer registration failed");
969 proto_unregister(&rfcomm_proto);
973 void __exit rfcomm_cleanup_sockets(void)
975 class_remove_file(bt_class, &class_attr_rfcomm);
977 if (bt_sock_unregister(BTPROTO_RFCOMM) < 0)
978 BT_ERR("RFCOMM socket layer unregistration failed");
980 proto_unregister(&rfcomm_proto);