YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 4 | Copyright (c) 2011, The Linux Foundation. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License version 2 as |
| 10 | published by the Free Software Foundation; |
| 11 | |
| 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 13 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 15 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 16 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 17 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 18 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 20 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 21 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 22 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | SOFTWARE IS DISCLAIMED. |
| 24 | */ |
| 25 | |
| 26 | /* Bluetooth SCO sockets. */ |
| 27 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | |
| 31 | #include <linux/types.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/sched.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/poll.h> |
| 37 | #include <linux/fcntl.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/socket.h> |
| 41 | #include <linux/skbuff.h> |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 42 | #include <linux/device.h> |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 43 | #include <linux/debugfs.h> |
| 44 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/list.h> |
| 46 | #include <net/sock.h> |
| 47 | |
| 48 | #include <asm/system.h> |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 49 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #include <net/bluetooth/bluetooth.h> |
| 52 | #include <net/bluetooth/hci_core.h> |
| 53 | #include <net/bluetooth/sco.h> |
| 54 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 55 | static bool disable_esco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 57 | static const struct proto_ops sco_sock_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | static struct bt_sock_list sco_sk_list = { |
Robert P. J. Day | d5fb296 | 2008-03-28 16:17:38 -0700 | [diff] [blame] | 60 | .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent); |
| 64 | static void sco_chan_del(struct sock *sk, int err); |
| 65 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 66 | static int sco_conn_del(struct hci_conn *conn, int err, u8 is_process); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | static void sco_sock_close(struct sock *sk); |
| 69 | static void sco_sock_kill(struct sock *sk); |
| 70 | |
| 71 | /* ---- SCO timers ---- */ |
| 72 | static void sco_sock_timeout(unsigned long arg) |
| 73 | { |
| 74 | struct sock *sk = (struct sock *) arg; |
| 75 | |
| 76 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 77 | |
| 78 | bh_lock_sock(sk); |
| 79 | sk->sk_err = ETIMEDOUT; |
| 80 | sk->sk_state_change(sk); |
| 81 | bh_unlock_sock(sk); |
| 82 | |
| 83 | sco_sock_kill(sk); |
| 84 | sock_put(sk); |
| 85 | } |
| 86 | |
| 87 | static void sco_sock_set_timer(struct sock *sk, long timeout) |
| 88 | { |
| 89 | BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout); |
| 90 | sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout); |
| 91 | } |
| 92 | |
| 93 | static void sco_sock_clear_timer(struct sock *sk) |
| 94 | { |
| 95 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 96 | sk_stop_timer(sk, &sk->sk_timer); |
| 97 | } |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* ---- SCO connections ---- */ |
| 100 | static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status) |
| 101 | { |
| 102 | struct hci_dev *hdev = hcon->hdev; |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 103 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 105 | if (conn || status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return conn; |
| 107 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 108 | conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC); |
| 109 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | spin_lock_init(&conn->lock); |
| 113 | |
| 114 | hcon->sco_data = conn; |
| 115 | conn->hcon = hcon; |
| 116 | |
| 117 | conn->src = &hdev->bdaddr; |
| 118 | conn->dst = &hcon->dst; |
| 119 | |
| 120 | if (hdev->sco_mtu > 0) |
| 121 | conn->mtu = hdev->sco_mtu; |
| 122 | else |
| 123 | conn->mtu = 60; |
| 124 | |
| 125 | BT_DBG("hcon %p conn %p", hcon, conn); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return conn; |
| 128 | } |
| 129 | |
| 130 | static inline struct sock *sco_chan_get(struct sco_conn *conn) |
| 131 | { |
| 132 | struct sock *sk = NULL; |
| 133 | sco_conn_lock(conn); |
| 134 | sk = conn->sk; |
| 135 | sco_conn_unlock(conn); |
| 136 | return sk; |
| 137 | } |
| 138 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 139 | static int sco_conn_del(struct hci_conn *hcon, int err, u8 is_process) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 141 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | struct sock *sk; |
| 143 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 144 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return 0; |
| 146 | |
| 147 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 148 | |
| 149 | /* Kill socket */ |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 150 | sk = sco_chan_get(conn); |
| 151 | if (sk) { |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 152 | if (is_process) |
| 153 | lock_sock(sk); |
| 154 | else |
| 155 | bh_lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | sco_sock_clear_timer(sk); |
| 157 | sco_chan_del(sk, err); |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 158 | if (is_process) |
| 159 | release_sock(sk); |
| 160 | else |
| 161 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | sco_sock_kill(sk); |
| 163 | } |
| 164 | |
| 165 | hcon->sco_data = NULL; |
| 166 | kfree(conn); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent) |
| 171 | { |
| 172 | int err = 0; |
| 173 | |
| 174 | sco_conn_lock(conn); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 175 | if (conn->sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | err = -EBUSY; |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 177 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | __sco_chan_add(conn, sk, parent); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | sco_conn_unlock(conn); |
| 181 | return err; |
| 182 | } |
| 183 | |
Kun Han Kim | 15b911f | 2011-07-08 09:30:28 -0700 | [diff] [blame] | 184 | static int sco_connect(struct sock *sk, __s8 is_wbs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
| 186 | bdaddr_t *src = &bt_sk(sk)->src; |
| 187 | bdaddr_t *dst = &bt_sk(sk)->dst; |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 188 | __u16 pkt_type = sco_pi(sk)->pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | struct sco_conn *conn; |
| 190 | struct hci_conn *hcon; |
| 191 | struct hci_dev *hdev; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 192 | int err, type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); |
| 195 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 196 | hdev = hci_get_route(dst, src); |
| 197 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return -EHOSTUNREACH; |
| 199 | |
| 200 | hci_dev_lock_bh(hdev); |
| 201 | |
Kun Han Kim | 15b911f | 2011-07-08 09:30:28 -0700 | [diff] [blame] | 202 | hdev->is_wbs = is_wbs; |
| 203 | |
| 204 | if (lmp_esco_capable(hdev) && !disable_esco) { |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 205 | type = ESCO_LINK; |
Kun Han Kim | 15b911f | 2011-07-08 09:30:28 -0700 | [diff] [blame] | 206 | } else if (is_wbs) { |
| 207 | return -ENAVAIL; |
| 208 | } else { |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 209 | type = SCO_LINK; |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 210 | pkt_type &= SCO_ESCO_MASK; |
| 211 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 212 | |
Kun Han Kim | 15b911f | 2011-07-08 09:30:28 -0700 | [diff] [blame] | 213 | BT_DBG("type: %d, pkt_type: 0x%x", type, pkt_type); |
| 214 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | hcon = hci_connect(hdev, type, pkt_type, dst, |
| 216 | BT_SECURITY_LOW, HCI_AT_NO_BONDING); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 217 | if (IS_ERR(hcon)) { |
| 218 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Kun Han Kim | 15b911f | 2011-07-08 09:30:28 -0700 | [diff] [blame] | 222 | if (is_wbs && (hcon->type != ESCO_LINK)) { |
| 223 | BT_ERR("WBS [ hcon->type: 0x%x, hcon->pkt_type: 0x%x ]", |
| 224 | hcon->type, hcon->pkt_type); |
| 225 | err = -EREMOTEIO; |
| 226 | goto done; |
| 227 | } |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | conn = sco_conn_add(hcon, 0); |
| 230 | if (!conn) { |
| 231 | hci_conn_put(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 232 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | goto done; |
| 234 | } |
| 235 | |
| 236 | /* Update source addr of the socket */ |
| 237 | bacpy(src, conn->src); |
| 238 | |
| 239 | err = sco_chan_add(conn, sk, NULL); |
| 240 | if (err) |
| 241 | goto done; |
| 242 | |
| 243 | if (hcon->state == BT_CONNECTED) { |
| 244 | sco_sock_clear_timer(sk); |
| 245 | sk->sk_state = BT_CONNECTED; |
| 246 | } else { |
| 247 | sk->sk_state = BT_CONNECT; |
| 248 | sco_sock_set_timer(sk, sk->sk_sndtimeo); |
| 249 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | done: |
| 252 | hci_dev_unlock_bh(hdev); |
| 253 | hci_dev_put(hdev); |
| 254 | return err; |
| 255 | } |
| 256 | |
| 257 | static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len) |
| 258 | { |
| 259 | struct sco_conn *conn = sco_pi(sk)->conn; |
| 260 | struct sk_buff *skb; |
| 261 | int err, count; |
| 262 | |
| 263 | /* Check outgoing MTU */ |
| 264 | if (len > conn->mtu) |
| 265 | return -EINVAL; |
| 266 | |
| 267 | BT_DBG("sk %p len %d", sk, len); |
| 268 | |
| 269 | count = min_t(unsigned int, conn->mtu, len); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 270 | skb = bt_skb_send_alloc(sk, count, |
| 271 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 272 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return err; |
| 274 | |
| 275 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) { |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 276 | kfree_skb(skb); |
| 277 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 280 | hci_send_sco(conn->hcon, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb) |
| 286 | { |
| 287 | struct sock *sk = sco_chan_get(conn); |
| 288 | |
| 289 | if (!sk) |
| 290 | goto drop; |
| 291 | |
| 292 | BT_DBG("sk %p len %d", sk, skb->len); |
| 293 | |
| 294 | if (sk->sk_state != BT_CONNECTED) |
| 295 | goto drop; |
| 296 | |
| 297 | if (!sock_queue_rcv_skb(sk, skb)) |
| 298 | return; |
| 299 | |
| 300 | drop: |
| 301 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* -------- Socket interface ---------- */ |
| 305 | static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba) |
| 306 | { |
| 307 | struct sock *sk; |
| 308 | struct hlist_node *node; |
| 309 | |
| 310 | sk_for_each(sk, node, &sco_sk_list.head) |
| 311 | if (!bacmp(&bt_sk(sk)->src, ba)) |
| 312 | goto found; |
| 313 | sk = NULL; |
| 314 | found: |
| 315 | return sk; |
| 316 | } |
| 317 | |
| 318 | /* Find socket listening on source bdaddr. |
| 319 | * Returns closest match. |
| 320 | */ |
| 321 | static struct sock *sco_get_sock_listen(bdaddr_t *src) |
| 322 | { |
| 323 | struct sock *sk = NULL, *sk1 = NULL; |
| 324 | struct hlist_node *node; |
| 325 | |
| 326 | read_lock(&sco_sk_list.lock); |
| 327 | |
| 328 | sk_for_each(sk, node, &sco_sk_list.head) { |
| 329 | if (sk->sk_state != BT_LISTEN) |
| 330 | continue; |
| 331 | |
| 332 | /* Exact match. */ |
| 333 | if (!bacmp(&bt_sk(sk)->src, src)) |
| 334 | break; |
| 335 | |
| 336 | /* Closest match */ |
| 337 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
| 338 | sk1 = sk; |
| 339 | } |
| 340 | |
| 341 | read_unlock(&sco_sk_list.lock); |
| 342 | |
| 343 | return node ? sk : sk1; |
| 344 | } |
| 345 | |
| 346 | static void sco_sock_destruct(struct sock *sk) |
| 347 | { |
| 348 | BT_DBG("sk %p", sk); |
| 349 | |
| 350 | skb_queue_purge(&sk->sk_receive_queue); |
| 351 | skb_queue_purge(&sk->sk_write_queue); |
| 352 | } |
| 353 | |
| 354 | static void sco_sock_cleanup_listen(struct sock *parent) |
| 355 | { |
| 356 | struct sock *sk; |
| 357 | |
| 358 | BT_DBG("parent %p", parent); |
| 359 | |
| 360 | /* Close not yet accepted channels */ |
| 361 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
| 362 | sco_sock_close(sk); |
| 363 | sco_sock_kill(sk); |
| 364 | } |
| 365 | |
| 366 | parent->sk_state = BT_CLOSED; |
| 367 | sock_set_flag(parent, SOCK_ZAPPED); |
| 368 | } |
| 369 | |
| 370 | /* Kill socket (only if zapped and orphan) |
| 371 | * Must be called on unlocked socket. |
| 372 | */ |
| 373 | static void sco_sock_kill(struct sock *sk) |
| 374 | { |
| 375 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 376 | return; |
| 377 | |
| 378 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
| 379 | |
| 380 | /* Kill poor orphan */ |
| 381 | bt_sock_unlink(&sco_sk_list, sk); |
| 382 | sock_set_flag(sk, SOCK_DEAD); |
| 383 | sock_put(sk); |
| 384 | } |
| 385 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 386 | static void __sco_sock_close(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 388 | BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | switch (sk->sk_state) { |
| 391 | case BT_LISTEN: |
| 392 | sco_sock_cleanup_listen(sk); |
| 393 | break; |
| 394 | |
| 395 | case BT_CONNECTED: |
| 396 | case BT_CONFIG: |
Luiz Augusto von Dentz | 2e02b861 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 397 | if (sco_pi(sk)->conn) { |
| 398 | sk->sk_state = BT_DISCONN; |
| 399 | sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT); |
Nitin Srivastava | 744098c | 2012-12-10 14:14:56 +0530 | [diff] [blame] | 400 | if (sco_pi(sk)->conn->hcon != NULL) { |
| 401 | hci_conn_put(sco_pi(sk)->conn->hcon); |
| 402 | sco_pi(sk)->conn->hcon = NULL; |
| 403 | } |
Luiz Augusto von Dentz | 2e02b861 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 404 | } else |
| 405 | sco_chan_del(sk, ECONNRESET); |
| 406 | break; |
| 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | case BT_CONNECT: |
| 409 | case BT_DISCONN: |
| 410 | sco_chan_del(sk, ECONNRESET); |
| 411 | break; |
| 412 | |
| 413 | default: |
| 414 | sock_set_flag(sk, SOCK_ZAPPED); |
| 415 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 416 | } |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 419 | /* Must be called on unlocked socket. */ |
| 420 | static void sco_sock_close(struct sock *sk) |
| 421 | { |
| 422 | sco_sock_clear_timer(sk); |
| 423 | lock_sock(sk); |
| 424 | __sco_sock_close(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | sco_sock_kill(sk); |
| 427 | } |
| 428 | |
| 429 | static void sco_sock_init(struct sock *sk, struct sock *parent) |
| 430 | { |
| 431 | BT_DBG("sk %p", sk); |
| 432 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 433 | if (parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | sk->sk_type = parent->sk_type; |
| 435 | } |
| 436 | |
| 437 | static struct proto sco_proto = { |
| 438 | .name = "SCO", |
| 439 | .owner = THIS_MODULE, |
| 440 | .obj_size = sizeof(struct sco_pinfo) |
| 441 | }; |
| 442 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 443 | static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
| 445 | struct sock *sk; |
| 446 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 447 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | if (!sk) |
| 449 | return NULL; |
| 450 | |
| 451 | sock_init_data(sock, sk); |
| 452 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 453 | |
| 454 | sk->sk_destruct = sco_sock_destruct; |
| 455 | sk->sk_sndtimeo = SCO_CONN_TIMEOUT; |
| 456 | |
| 457 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 458 | |
| 459 | sk->sk_protocol = proto; |
| 460 | sk->sk_state = BT_OPEN; |
| 461 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 462 | setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | bt_sock_link(&sco_sk_list, sk); |
| 465 | return sk; |
| 466 | } |
| 467 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 468 | static int sco_sock_create(struct net *net, struct socket *sock, int protocol, |
| 469 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
| 471 | struct sock *sk; |
| 472 | |
| 473 | BT_DBG("sock %p", sock); |
| 474 | |
| 475 | sock->state = SS_UNCONNECTED; |
| 476 | |
| 477 | if (sock->type != SOCK_SEQPACKET) |
| 478 | return -ESOCKTNOSUPPORT; |
| 479 | |
| 480 | sock->ops = &sco_sock_ops; |
| 481 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 482 | sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
Marcel Holtmann | 74da626 | 2006-10-15 17:31:14 +0200 | [diff] [blame] | 483 | if (!sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | return -ENOMEM; |
| 485 | |
| 486 | sco_sock_init(sk, NULL); |
| 487 | return 0; |
| 488 | } |
| 489 | |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 490 | static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 492 | struct sockaddr_sco sa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | struct sock *sk = sock->sk; |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 494 | bdaddr_t *src = &sa.sco_bdaddr; |
| 495 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 497 | BT_DBG("sk %p %s", sk, batostr(&sa.sco_bdaddr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
| 499 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
| 500 | return -EINVAL; |
| 501 | |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 502 | memset(&sa, 0, sizeof(sa)); |
| 503 | len = min_t(unsigned int, sizeof(sa), alen); |
| 504 | memcpy(&sa, addr, len); |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | lock_sock(sk); |
| 507 | |
| 508 | if (sk->sk_state != BT_OPEN) { |
| 509 | err = -EBADFD; |
| 510 | goto done; |
| 511 | } |
| 512 | |
| 513 | write_lock_bh(&sco_sk_list.lock); |
| 514 | |
| 515 | if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) { |
| 516 | err = -EADDRINUSE; |
| 517 | } else { |
| 518 | /* Save source address */ |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 519 | bacpy(&bt_sk(sk)->src, &sa.sco_bdaddr); |
| 520 | sco_pi(sk)->pkt_type = sa.sco_pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | sk->sk_state = BT_BOUND; |
| 522 | } |
| 523 | |
| 524 | write_unlock_bh(&sco_sk_list.lock); |
| 525 | |
| 526 | done: |
| 527 | release_sock(sk); |
| 528 | return err; |
| 529 | } |
| 530 | |
| 531 | static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 532 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | struct sock *sk = sock->sk; |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 534 | struct sockaddr_sco sa; |
| 535 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | BT_DBG("sk %p", sk); |
| 538 | |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 539 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return -EINVAL; |
| 541 | |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 542 | memset(&sa, 0, sizeof(sa)); |
| 543 | len = min_t(unsigned int, sizeof(sa), alen); |
| 544 | memcpy(&sa, addr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | lock_sock(sk); |
| 547 | |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 548 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 549 | err = -EINVAL; |
| 550 | goto done; |
| 551 | } |
| 552 | |
| 553 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) { |
| 554 | err = -EBADFD; |
| 555 | goto done; |
| 556 | } |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* Set destination address and psm */ |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 559 | bacpy(&bt_sk(sk)->dst, &sa.sco_bdaddr); |
| 560 | sco_pi(sk)->pkt_type = sa.sco_pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Kun Han Kim | 15b911f | 2011-07-08 09:30:28 -0700 | [diff] [blame] | 562 | err = sco_connect(sk, sa.is_wbs); |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 563 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | goto done; |
| 565 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 566 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
| 568 | |
| 569 | done: |
| 570 | release_sock(sk); |
| 571 | return err; |
| 572 | } |
| 573 | |
| 574 | static int sco_sock_listen(struct socket *sock, int backlog) |
| 575 | { |
| 576 | struct sock *sk = sock->sk; |
| 577 | int err = 0; |
| 578 | |
| 579 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 580 | |
| 581 | lock_sock(sk); |
| 582 | |
| 583 | if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) { |
| 584 | err = -EBADFD; |
| 585 | goto done; |
| 586 | } |
| 587 | |
| 588 | sk->sk_max_ack_backlog = backlog; |
| 589 | sk->sk_ack_backlog = 0; |
| 590 | sk->sk_state = BT_LISTEN; |
| 591 | |
| 592 | done: |
| 593 | release_sock(sk); |
| 594 | return err; |
| 595 | } |
| 596 | |
| 597 | static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags) |
| 598 | { |
| 599 | DECLARE_WAITQUEUE(wait, current); |
| 600 | struct sock *sk = sock->sk, *ch; |
| 601 | long timeo; |
| 602 | int err = 0; |
| 603 | |
| 604 | lock_sock(sk); |
| 605 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 606 | if (sk->sk_state != BT_LISTEN) { |
| 607 | err = -EBADFD; |
| 608 | goto done; |
| 609 | } |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 612 | |
| 613 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 614 | |
| 615 | /* Wait for an incoming connection. (wake-one). */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 616 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 617 | while (!(ch = bt_accept_dequeue(sk, newsock))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | set_current_state(TASK_INTERRUPTIBLE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 619 | if (!timeo) { |
| 620 | err = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | break; |
| 622 | } |
| 623 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 624 | release_sock(sk); |
| 625 | timeo = schedule_timeout(timeo); |
| 626 | lock_sock(sk); |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 627 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 628 | if (sk->sk_state != BT_LISTEN) { |
| 629 | err = -EBADFD; |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 630 | break; |
| 631 | } |
| 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if (signal_pending(current)) { |
| 634 | err = sock_intr_errno(timeo); |
| 635 | break; |
| 636 | } |
| 637 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 638 | set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 639 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | if (err) |
| 642 | goto done; |
| 643 | |
| 644 | newsock->state = SS_CONNECTED; |
| 645 | |
| 646 | BT_DBG("new socket %p", ch); |
| 647 | |
| 648 | done: |
| 649 | release_sock(sk); |
| 650 | return err; |
| 651 | } |
| 652 | |
| 653 | static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer) |
| 654 | { |
| 655 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 656 | struct sock *sk = sock->sk; |
| 657 | |
| 658 | BT_DBG("sock %p, sk %p", sock, sk); |
| 659 | |
| 660 | addr->sa_family = AF_BLUETOOTH; |
| 661 | *len = sizeof(struct sockaddr_sco); |
| 662 | |
| 663 | if (peer) |
| 664 | bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst); |
| 665 | else |
| 666 | bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src); |
Nick Pelly | 07021e2 | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 667 | sa->sco_pkt_type = sco_pi(sk)->pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 672 | static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | struct msghdr *msg, size_t len) |
| 674 | { |
| 675 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 676 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | BT_DBG("sock %p, sk %p", sock, sk); |
| 679 | |
Benjamin LaHaise | c1cbe4b | 2005-12-13 23:22:19 -0800 | [diff] [blame] | 680 | err = sock_error(sk); |
| 681 | if (err) |
| 682 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
| 684 | if (msg->msg_flags & MSG_OOB) |
| 685 | return -EOPNOTSUPP; |
| 686 | |
| 687 | lock_sock(sk); |
| 688 | |
| 689 | if (sk->sk_state == BT_CONNECTED) |
| 690 | err = sco_send_frame(sk, msg, len); |
| 691 | else |
| 692 | err = -ENOTCONN; |
| 693 | |
| 694 | release_sock(sk); |
| 695 | return err; |
| 696 | } |
| 697 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 698 | static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | { |
| 700 | struct sock *sk = sock->sk; |
| 701 | int err = 0; |
| 702 | |
| 703 | BT_DBG("sk %p", sk); |
| 704 | |
| 705 | lock_sock(sk); |
| 706 | |
| 707 | switch (optname) { |
| 708 | default: |
| 709 | err = -ENOPROTOOPT; |
| 710 | break; |
| 711 | } |
| 712 | |
| 713 | release_sock(sk); |
| 714 | return err; |
| 715 | } |
| 716 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 717 | static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
| 719 | struct sock *sk = sock->sk; |
| 720 | struct sco_options opts; |
| 721 | struct sco_conninfo cinfo; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 722 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | BT_DBG("sk %p", sk); |
| 725 | |
| 726 | if (get_user(len, optlen)) |
| 727 | return -EFAULT; |
| 728 | |
| 729 | lock_sock(sk); |
| 730 | |
| 731 | switch (optname) { |
| 732 | case SCO_OPTIONS: |
| 733 | if (sk->sk_state != BT_CONNECTED) { |
| 734 | err = -ENOTCONN; |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | opts.mtu = sco_pi(sk)->conn->mtu; |
| 739 | |
| 740 | BT_DBG("mtu %d", opts.mtu); |
| 741 | |
| 742 | len = min_t(unsigned int, len, sizeof(opts)); |
| 743 | if (copy_to_user(optval, (char *)&opts, len)) |
| 744 | err = -EFAULT; |
| 745 | |
| 746 | break; |
| 747 | |
| 748 | case SCO_CONNINFO: |
| 749 | if (sk->sk_state != BT_CONNECTED) { |
| 750 | err = -ENOTCONN; |
| 751 | break; |
| 752 | } |
| 753 | |
Vasiliy Kulikov | c4c896e | 2011-02-14 13:54:26 +0300 | [diff] [blame] | 754 | memset(&cinfo, 0, sizeof(cinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle; |
| 756 | memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3); |
| 757 | |
| 758 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 759 | if (copy_to_user(optval, (char *)&cinfo, len)) |
| 760 | err = -EFAULT; |
| 761 | |
| 762 | break; |
| 763 | |
| 764 | default: |
| 765 | err = -ENOPROTOOPT; |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | release_sock(sk); |
| 770 | return err; |
| 771 | } |
| 772 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 773 | static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 774 | { |
| 775 | struct sock *sk = sock->sk; |
| 776 | int len, err = 0; |
| 777 | |
| 778 | BT_DBG("sk %p", sk); |
| 779 | |
| 780 | if (level == SOL_SCO) |
| 781 | return sco_sock_getsockopt_old(sock, optname, optval, optlen); |
| 782 | |
| 783 | if (get_user(len, optlen)) |
| 784 | return -EFAULT; |
| 785 | |
| 786 | lock_sock(sk); |
| 787 | |
| 788 | switch (optname) { |
| 789 | default: |
| 790 | err = -ENOPROTOOPT; |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | release_sock(sk); |
| 795 | return err; |
| 796 | } |
| 797 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 798 | static int sco_sock_shutdown(struct socket *sock, int how) |
| 799 | { |
| 800 | struct sock *sk = sock->sk; |
| 801 | int err = 0; |
| 802 | |
| 803 | BT_DBG("sock %p, sk %p", sock, sk); |
| 804 | |
| 805 | if (!sk) |
| 806 | return 0; |
| 807 | |
| 808 | lock_sock(sk); |
| 809 | if (!sk->sk_shutdown) { |
| 810 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 811 | sco_sock_clear_timer(sk); |
| 812 | __sco_sock_close(sk); |
| 813 | |
| 814 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 815 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 816 | sk->sk_lingertime); |
Srinivas Krovvidi | f799996 | 2012-08-01 18:21:37 +0530 | [diff] [blame] | 817 | else |
| 818 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 819 | SCO_DISCONN_TIMEOUT); |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 820 | } |
| 821 | release_sock(sk); |
| 822 | return err; |
| 823 | } |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | static int sco_sock_release(struct socket *sock) |
| 826 | { |
| 827 | struct sock *sk = sock->sk; |
| 828 | int err = 0; |
| 829 | |
| 830 | BT_DBG("sock %p, sk %p", sock, sk); |
| 831 | |
| 832 | if (!sk) |
| 833 | return 0; |
| 834 | |
| 835 | sco_sock_close(sk); |
| 836 | |
| 837 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) { |
| 838 | lock_sock(sk); |
| 839 | err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); |
| 840 | release_sock(sk); |
Srinivas Krovvidi | f799996 | 2012-08-01 18:21:37 +0530 | [diff] [blame] | 841 | } else { |
| 842 | lock_sock(sk); |
| 843 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 844 | SCO_DISCONN_TIMEOUT); |
| 845 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | sock_orphan(sk); |
| 849 | sco_sock_kill(sk); |
| 850 | return err; |
| 851 | } |
| 852 | |
| 853 | static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent) |
| 854 | { |
| 855 | BT_DBG("conn %p", conn); |
| 856 | |
| 857 | sco_pi(sk)->conn = conn; |
| 858 | conn->sk = sk; |
| 859 | |
| 860 | if (parent) |
| 861 | bt_accept_enqueue(parent, sk); |
| 862 | } |
| 863 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 864 | /* Delete channel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | * Must be called on the locked socket. */ |
| 866 | static void sco_chan_del(struct sock *sk, int err) |
| 867 | { |
| 868 | struct sco_conn *conn; |
| 869 | |
| 870 | conn = sco_pi(sk)->conn; |
| 871 | |
| 872 | BT_DBG("sk %p, conn %p, err %d", sk, conn, err); |
| 873 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 874 | if (conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | sco_conn_lock(conn); |
| 876 | conn->sk = NULL; |
| 877 | sco_pi(sk)->conn = NULL; |
| 878 | sco_conn_unlock(conn); |
Luiz Augusto von Dentz | 2e02b861 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 879 | |
| 880 | if (conn->hcon) |
| 881 | hci_conn_put(conn->hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | sk->sk_state = BT_CLOSED; |
| 885 | sk->sk_err = err; |
| 886 | sk->sk_state_change(sk); |
| 887 | |
| 888 | sock_set_flag(sk, SOCK_ZAPPED); |
| 889 | } |
| 890 | |
| 891 | static void sco_conn_ready(struct sco_conn *conn) |
| 892 | { |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 893 | struct sock *parent; |
| 894 | struct sock *sk = conn->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
| 896 | BT_DBG("conn %p", conn); |
| 897 | |
| 898 | sco_conn_lock(conn); |
| 899 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 900 | if (sk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | sco_sock_clear_timer(sk); |
| 902 | bh_lock_sock(sk); |
| 903 | sk->sk_state = BT_CONNECTED; |
| 904 | sk->sk_state_change(sk); |
| 905 | bh_unlock_sock(sk); |
| 906 | } else { |
| 907 | parent = sco_get_sock_listen(conn->src); |
| 908 | if (!parent) |
| 909 | goto done; |
| 910 | |
| 911 | bh_lock_sock(parent); |
| 912 | |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 913 | sk = sco_sock_alloc(sock_net(parent), NULL, |
| 914 | BTPROTO_SCO, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | if (!sk) { |
| 916 | bh_unlock_sock(parent); |
| 917 | goto done; |
| 918 | } |
| 919 | |
| 920 | sco_sock_init(sk, parent); |
| 921 | |
| 922 | bacpy(&bt_sk(sk)->src, conn->src); |
| 923 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 924 | |
| 925 | hci_conn_hold(conn->hcon); |
| 926 | __sco_chan_add(conn, sk, parent); |
| 927 | |
| 928 | sk->sk_state = BT_CONNECTED; |
| 929 | |
| 930 | /* Wake up parent */ |
| 931 | parent->sk_data_ready(parent, 1); |
| 932 | |
| 933 | bh_unlock_sock(parent); |
| 934 | } |
| 935 | |
| 936 | done: |
| 937 | sco_conn_unlock(conn); |
| 938 | } |
| 939 | |
| 940 | /* ----- SCO interface with lower layer (HCI) ----- */ |
| 941 | static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type) |
| 942 | { |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 943 | register struct sock *sk; |
| 944 | struct hlist_node *node; |
| 945 | int lm = 0; |
| 946 | |
| 947 | if (type != SCO_LINK && type != ESCO_LINK) |
Mat Martineau | 8b51dd4 | 2012-02-13 10:38:24 -0800 | [diff] [blame] | 948 | return 0; |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); |
| 951 | |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 952 | /* Find listening sockets */ |
| 953 | read_lock(&sco_sk_list.lock); |
| 954 | sk_for_each(sk, node, &sco_sk_list.head) { |
| 955 | if (sk->sk_state != BT_LISTEN) |
| 956 | continue; |
| 957 | |
| 958 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) || |
| 959 | !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 960 | lm |= HCI_LM_ACCEPT; |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | read_unlock(&sco_sk_list.lock); |
| 965 | |
| 966 | return lm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | static int sco_connect_cfm(struct hci_conn *hcon, __u8 status) |
| 970 | { |
| 971 | BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); |
| 972 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 973 | if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK) |
Gustavo F. Padovan | c89ad73 | 2010-11-01 19:08:50 +0000 | [diff] [blame] | 974 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
| 976 | if (!status) { |
| 977 | struct sco_conn *conn; |
| 978 | |
| 979 | conn = sco_conn_add(hcon, status); |
| 980 | if (conn) |
| 981 | sco_conn_ready(conn); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 982 | } else |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 983 | sco_conn_del(hcon, bt_err(status), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 988 | static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason, __u8 is_process) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | { |
| 990 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 991 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 992 | if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK) |
Gustavo F. Padovan | c89ad73 | 2010-11-01 19:08:50 +0000 | [diff] [blame] | 993 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
Mat Martineau | 3b9239a | 2012-02-16 11:54:30 -0800 | [diff] [blame] | 995 | sco_conn_del(hcon, bt_err(reason), is_process); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb) |
| 1001 | { |
| 1002 | struct sco_conn *conn = hcon->sco_data; |
| 1003 | |
| 1004 | if (!conn) |
| 1005 | goto drop; |
| 1006 | |
| 1007 | BT_DBG("conn %p len %d", conn, skb->len); |
| 1008 | |
| 1009 | if (skb->len) { |
| 1010 | sco_recv_frame(conn, skb); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | drop: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1015 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | return 0; |
| 1017 | } |
| 1018 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1019 | static int sco_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | { |
| 1021 | struct sock *sk; |
| 1022 | struct hlist_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | read_lock_bh(&sco_sk_list.lock); |
| 1025 | |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1026 | sk_for_each(sk, node, &sco_sk_list.head) { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1027 | seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src), |
| 1028 | batostr(&bt_sk(sk)->dst), sk->sk_state); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1029 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | read_unlock_bh(&sco_sk_list.lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1032 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1033 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1036 | static int sco_debugfs_open(struct inode *inode, struct file *file) |
| 1037 | { |
| 1038 | return single_open(file, sco_debugfs_show, inode->i_private); |
| 1039 | } |
| 1040 | |
| 1041 | static const struct file_operations sco_debugfs_fops = { |
| 1042 | .open = sco_debugfs_open, |
| 1043 | .read = seq_read, |
| 1044 | .llseek = seq_lseek, |
| 1045 | .release = single_release, |
| 1046 | }; |
| 1047 | |
| 1048 | static struct dentry *sco_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1050 | static const struct proto_ops sco_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | .family = PF_BLUETOOTH, |
| 1052 | .owner = THIS_MODULE, |
| 1053 | .release = sco_sock_release, |
| 1054 | .bind = sco_sock_bind, |
| 1055 | .connect = sco_sock_connect, |
| 1056 | .listen = sco_sock_listen, |
| 1057 | .accept = sco_sock_accept, |
| 1058 | .getname = sco_sock_getname, |
| 1059 | .sendmsg = sco_sock_sendmsg, |
| 1060 | .recvmsg = bt_sock_recvmsg, |
| 1061 | .poll = bt_sock_poll, |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1062 | .ioctl = bt_sock_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | .mmap = sock_no_mmap, |
| 1064 | .socketpair = sock_no_socketpair, |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1065 | .shutdown = sco_sock_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | .setsockopt = sco_sock_setsockopt, |
| 1067 | .getsockopt = sco_sock_getsockopt |
| 1068 | }; |
| 1069 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1070 | static const struct net_proto_family sco_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | .family = PF_BLUETOOTH, |
| 1072 | .owner = THIS_MODULE, |
| 1073 | .create = sco_sock_create, |
| 1074 | }; |
| 1075 | |
| 1076 | static struct hci_proto sco_hci_proto = { |
| 1077 | .name = "SCO", |
| 1078 | .id = HCI_PROTO_SCO, |
| 1079 | .connect_ind = sco_connect_ind, |
| 1080 | .connect_cfm = sco_connect_cfm, |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 1081 | .disconn_cfm = sco_disconn_cfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | .recv_scodata = sco_recv_scodata |
| 1083 | }; |
| 1084 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 1085 | int __init sco_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | { |
| 1087 | int err; |
| 1088 | |
| 1089 | err = proto_register(&sco_proto, 0); |
| 1090 | if (err < 0) |
| 1091 | return err; |
| 1092 | |
| 1093 | err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops); |
| 1094 | if (err < 0) { |
| 1095 | BT_ERR("SCO socket registration failed"); |
| 1096 | goto error; |
| 1097 | } |
| 1098 | |
| 1099 | err = hci_register_proto(&sco_hci_proto); |
| 1100 | if (err < 0) { |
| 1101 | BT_ERR("SCO protocol registration failed"); |
| 1102 | bt_sock_unregister(BTPROTO_SCO); |
| 1103 | goto error; |
| 1104 | } |
| 1105 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1106 | if (bt_debugfs) { |
| 1107 | sco_debugfs = debugfs_create_file("sco", 0444, |
| 1108 | bt_debugfs, NULL, &sco_debugfs_fops); |
| 1109 | if (!sco_debugfs) |
| 1110 | BT_ERR("Failed to create SCO debug file"); |
| 1111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | BT_INFO("SCO socket layer initialized"); |
| 1114 | |
| 1115 | return 0; |
| 1116 | |
| 1117 | error: |
| 1118 | proto_unregister(&sco_proto); |
| 1119 | return err; |
| 1120 | } |
| 1121 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 1122 | void __exit sco_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1124 | debugfs_remove(sco_debugfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (bt_sock_unregister(BTPROTO_SCO) < 0) |
| 1127 | BT_ERR("SCO socket unregistration failed"); |
| 1128 | |
| 1129 | if (hci_unregister_proto(&sco_hci_proto) < 0) |
| 1130 | BT_ERR("SCO protocol unregistration failed"); |
| 1131 | |
| 1132 | proto_unregister(&sco_proto); |
| 1133 | } |
| 1134 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 1135 | module_param(disable_esco, bool, 0644); |
| 1136 | MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation"); |