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