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 | 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> |
| 5 | |
| 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; |
| 9 | |
| 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 |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 19 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 20 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | SOFTWARE IS DISCLAIMED. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * RFCOMM sockets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 28 | #include <linux/export.h> |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 29 | #include <linux/debugfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <net/bluetooth/bluetooth.h> |
| 32 | #include <net/bluetooth/hci_core.h> |
| 33 | #include <net/bluetooth/l2cap.h> |
| 34 | #include <net/bluetooth/rfcomm.h> |
| 35 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 36 | static const struct proto_ops rfcomm_sock_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | static struct bt_sock_list rfcomm_sk_list = { |
Robert P. J. Day | d5fb296 | 2008-03-28 16:17:38 -0700 | [diff] [blame] | 39 | .lock = __RW_LOCK_UNLOCKED(rfcomm_sk_list.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static void rfcomm_sock_close(struct sock *sk); |
| 43 | static void rfcomm_sock_kill(struct sock *sk); |
| 44 | |
| 45 | /* ---- DLC callbacks ---- |
| 46 | * |
| 47 | * called under rfcomm_dlc_lock() |
| 48 | */ |
| 49 | static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb) |
| 50 | { |
| 51 | struct sock *sk = d->owner; |
| 52 | if (!sk) |
| 53 | return; |
| 54 | |
| 55 | atomic_add(skb->len, &sk->sk_rmem_alloc); |
| 56 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 57 | sk->sk_data_ready(sk, skb->len); |
| 58 | |
| 59 | if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) |
| 60 | rfcomm_dlc_throttle(d); |
| 61 | } |
| 62 | |
| 63 | static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err) |
| 64 | { |
| 65 | struct sock *sk = d->owner, *parent; |
Gustavo F. Padovan | fad003b | 2010-08-14 00:48:07 -0300 | [diff] [blame] | 66 | unsigned long flags; |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (!sk) |
| 69 | return; |
| 70 | |
| 71 | BT_DBG("dlc %p state %ld err %d", d, d->state, err); |
| 72 | |
Gustavo F. Padovan | fad003b | 2010-08-14 00:48:07 -0300 | [diff] [blame] | 73 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | bh_lock_sock(sk); |
| 75 | |
| 76 | if (err) |
| 77 | sk->sk_err = err; |
| 78 | |
| 79 | sk->sk_state = d->state; |
| 80 | |
| 81 | parent = bt_sk(sk)->parent; |
| 82 | if (parent) { |
| 83 | if (d->state == BT_CLOSED) { |
| 84 | sock_set_flag(sk, SOCK_ZAPPED); |
| 85 | bt_accept_unlink(sk); |
| 86 | } |
| 87 | parent->sk_data_ready(parent, 0); |
| 88 | } else { |
| 89 | if (d->state == BT_CONNECTED) |
| 90 | rfcomm_session_getaddr(d->session, &bt_sk(sk)->src, NULL); |
| 91 | sk->sk_state_change(sk); |
| 92 | } |
| 93 | |
| 94 | bh_unlock_sock(sk); |
Gustavo F. Padovan | fad003b | 2010-08-14 00:48:07 -0300 | [diff] [blame] | 95 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | if (parent && sock_flag(sk, SOCK_ZAPPED)) { |
| 98 | /* We have to drop DLC lock here, otherwise |
| 99 | * rfcomm_sock_destruct() will dead lock. */ |
| 100 | rfcomm_dlc_unlock(d); |
| 101 | rfcomm_sock_kill(sk); |
| 102 | rfcomm_dlc_lock(d); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* ---- Socket functions ---- */ |
| 107 | static struct sock *__rfcomm_get_sock_by_addr(u8 channel, bdaddr_t *src) |
| 108 | { |
| 109 | struct sock *sk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 111 | sk_for_each(sk, &rfcomm_sk_list.head) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 112 | if (rfcomm_pi(sk)->channel == channel && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | !bacmp(&bt_sk(sk)->src, src)) |
| 114 | break; |
| 115 | } |
| 116 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 117 | return sk ? sk : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Find socket with channel and source bdaddr. |
| 121 | * Returns closest match. |
| 122 | */ |
Gustavo F. Padovan | eeb3665 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 123 | static struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | struct sock *sk = NULL, *sk1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Gustavo F. Padovan | eeb3665 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 127 | read_lock(&rfcomm_sk_list.lock); |
| 128 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 129 | sk_for_each(sk, &rfcomm_sk_list.head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (state && sk->sk_state != state) |
| 131 | continue; |
| 132 | |
| 133 | if (rfcomm_pi(sk)->channel == channel) { |
| 134 | /* Exact match. */ |
| 135 | if (!bacmp(&bt_sk(sk)->src, src)) |
| 136 | break; |
| 137 | |
| 138 | /* Closest match */ |
| 139 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
| 140 | sk1 = sk; |
| 141 | } |
| 142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | read_unlock(&rfcomm_sk_list.lock); |
Gustavo F. Padovan | eeb3665 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 145 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 146 | return sk ? sk : sk1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void rfcomm_sock_destruct(struct sock *sk) |
| 150 | { |
| 151 | struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc; |
| 152 | |
| 153 | BT_DBG("sk %p dlc %p", sk, d); |
| 154 | |
| 155 | skb_queue_purge(&sk->sk_receive_queue); |
| 156 | skb_queue_purge(&sk->sk_write_queue); |
| 157 | |
| 158 | rfcomm_dlc_lock(d); |
| 159 | rfcomm_pi(sk)->dlc = NULL; |
| 160 | |
| 161 | /* Detach DLC if it's owned by this socket */ |
| 162 | if (d->owner == sk) |
| 163 | d->owner = NULL; |
| 164 | rfcomm_dlc_unlock(d); |
| 165 | |
| 166 | rfcomm_dlc_put(d); |
| 167 | } |
| 168 | |
| 169 | static void rfcomm_sock_cleanup_listen(struct sock *parent) |
| 170 | { |
| 171 | struct sock *sk; |
| 172 | |
| 173 | BT_DBG("parent %p", parent); |
| 174 | |
| 175 | /* Close not yet accepted dlcs */ |
| 176 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
| 177 | rfcomm_sock_close(sk); |
| 178 | rfcomm_sock_kill(sk); |
| 179 | } |
| 180 | |
| 181 | parent->sk_state = BT_CLOSED; |
| 182 | sock_set_flag(parent, SOCK_ZAPPED); |
| 183 | } |
| 184 | |
| 185 | /* Kill socket (only if zapped and orphan) |
| 186 | * Must be called on unlocked socket. |
| 187 | */ |
| 188 | static void rfcomm_sock_kill(struct sock *sk) |
| 189 | { |
| 190 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 191 | return; |
| 192 | |
| 193 | BT_DBG("sk %p state %d refcnt %d", sk, sk->sk_state, atomic_read(&sk->sk_refcnt)); |
| 194 | |
| 195 | /* Kill poor orphan */ |
| 196 | bt_sock_unlink(&rfcomm_sk_list, sk); |
| 197 | sock_set_flag(sk, SOCK_DEAD); |
| 198 | sock_put(sk); |
| 199 | } |
| 200 | |
| 201 | static void __rfcomm_sock_close(struct sock *sk) |
| 202 | { |
| 203 | struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc; |
| 204 | |
| 205 | BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket); |
| 206 | |
| 207 | switch (sk->sk_state) { |
| 208 | case BT_LISTEN: |
| 209 | rfcomm_sock_cleanup_listen(sk); |
| 210 | break; |
| 211 | |
| 212 | case BT_CONNECT: |
| 213 | case BT_CONNECT2: |
| 214 | case BT_CONFIG: |
| 215 | case BT_CONNECTED: |
| 216 | rfcomm_dlc_close(d, 0); |
| 217 | |
| 218 | default: |
| 219 | sock_set_flag(sk, SOCK_ZAPPED); |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* Close socket. |
| 225 | * Must be called on unlocked socket. |
| 226 | */ |
| 227 | static void rfcomm_sock_close(struct sock *sk) |
| 228 | { |
| 229 | lock_sock(sk); |
| 230 | __rfcomm_sock_close(sk); |
| 231 | release_sock(sk); |
| 232 | } |
| 233 | |
| 234 | static void rfcomm_sock_init(struct sock *sk, struct sock *parent) |
| 235 | { |
| 236 | struct rfcomm_pinfo *pi = rfcomm_pi(sk); |
| 237 | |
| 238 | BT_DBG("sk %p", sk); |
| 239 | |
| 240 | if (parent) { |
| 241 | sk->sk_type = parent->sk_type; |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 242 | pi->dlc->defer_setup = test_bit(BT_SK_DEFER_SETUP, |
| 243 | &bt_sk(parent)->flags); |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 244 | |
| 245 | pi->sec_level = rfcomm_pi(parent)->sec_level; |
| 246 | pi->role_switch = rfcomm_pi(parent)->role_switch; |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 247 | |
| 248 | security_sk_clone(parent, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } else { |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 250 | pi->dlc->defer_setup = 0; |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 251 | |
| 252 | pi->sec_level = BT_SECURITY_LOW; |
| 253 | pi->role_switch = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 256 | pi->dlc->sec_level = pi->sec_level; |
| 257 | pi->dlc->role_switch = pi->role_switch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static struct proto rfcomm_proto = { |
| 261 | .name = "RFCOMM", |
| 262 | .owner = THIS_MODULE, |
| 263 | .obj_size = sizeof(struct rfcomm_pinfo) |
| 264 | }; |
| 265 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 266 | static struct sock *rfcomm_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] | 267 | { |
| 268 | struct rfcomm_dlc *d; |
| 269 | struct sock *sk; |
| 270 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 271 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &rfcomm_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (!sk) |
| 273 | return NULL; |
| 274 | |
| 275 | sock_init_data(sock, sk); |
| 276 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 277 | |
| 278 | d = rfcomm_dlc_alloc(prio); |
| 279 | if (!d) { |
| 280 | sk_free(sk); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | d->data_ready = rfcomm_sk_data_ready; |
| 285 | d->state_change = rfcomm_sk_state_change; |
| 286 | |
| 287 | rfcomm_pi(sk)->dlc = d; |
| 288 | d->owner = sk; |
| 289 | |
| 290 | sk->sk_destruct = rfcomm_sock_destruct; |
| 291 | sk->sk_sndtimeo = RFCOMM_CONN_TIMEOUT; |
| 292 | |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 293 | sk->sk_sndbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10; |
| 294 | sk->sk_rcvbuf = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 297 | |
| 298 | sk->sk_protocol = proto; |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 299 | sk->sk_state = BT_OPEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | bt_sock_link(&rfcomm_sk_list, sk); |
| 302 | |
| 303 | BT_DBG("sk %p", sk); |
| 304 | return sk; |
| 305 | } |
| 306 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 307 | static int rfcomm_sock_create(struct net *net, struct socket *sock, |
| 308 | int protocol, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
| 310 | struct sock *sk; |
| 311 | |
| 312 | BT_DBG("sock %p", sock); |
| 313 | |
| 314 | sock->state = SS_UNCONNECTED; |
| 315 | |
| 316 | if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW) |
| 317 | return -ESOCKTNOSUPPORT; |
| 318 | |
| 319 | sock->ops = &rfcomm_sock_ops; |
| 320 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 321 | sk = rfcomm_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
Marcel Holtmann | 74da626 | 2006-10-15 17:31:14 +0200 | [diff] [blame] | 322 | if (!sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | return -ENOMEM; |
| 324 | |
| 325 | rfcomm_sock_init(sk, NULL); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 330 | { |
| 331 | struct sockaddr_rc *sa = (struct sockaddr_rc *) addr; |
| 332 | struct sock *sk = sock->sk; |
| 333 | int err = 0; |
| 334 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 335 | BT_DBG("sk %p %pMR", sk, &sa->rc_bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
| 338 | return -EINVAL; |
| 339 | |
| 340 | lock_sock(sk); |
| 341 | |
| 342 | if (sk->sk_state != BT_OPEN) { |
| 343 | err = -EBADFD; |
| 344 | goto done; |
| 345 | } |
| 346 | |
Marcel Holtmann | 354d28d | 2005-09-13 01:32:31 +0200 | [diff] [blame] | 347 | if (sk->sk_type != SOCK_STREAM) { |
| 348 | err = -EINVAL; |
| 349 | goto done; |
| 350 | } |
| 351 | |
Gustavo F. Padovan | 95ca83f | 2011-12-27 15:28:44 -0200 | [diff] [blame] | 352 | write_lock(&rfcomm_sk_list.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | if (sa->rc_channel && __rfcomm_get_sock_by_addr(sa->rc_channel, &sa->rc_bdaddr)) { |
| 355 | err = -EADDRINUSE; |
| 356 | } else { |
| 357 | /* Save source address */ |
| 358 | bacpy(&bt_sk(sk)->src, &sa->rc_bdaddr); |
| 359 | rfcomm_pi(sk)->channel = sa->rc_channel; |
| 360 | sk->sk_state = BT_BOUND; |
| 361 | } |
| 362 | |
Gustavo F. Padovan | 95ca83f | 2011-12-27 15:28:44 -0200 | [diff] [blame] | 363 | write_unlock(&rfcomm_sk_list.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | done: |
| 366 | release_sock(sk); |
| 367 | return err; |
| 368 | } |
| 369 | |
| 370 | static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 371 | { |
| 372 | struct sockaddr_rc *sa = (struct sockaddr_rc *) addr; |
| 373 | struct sock *sk = sock->sk; |
| 374 | struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc; |
| 375 | int err = 0; |
| 376 | |
| 377 | BT_DBG("sk %p", sk); |
| 378 | |
Changli Gao | 6503d96 | 2010-03-31 22:58:26 +0000 | [diff] [blame] | 379 | if (alen < sizeof(struct sockaddr_rc) || |
| 380 | addr->sa_family != AF_BLUETOOTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | return -EINVAL; |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | lock_sock(sk); |
| 384 | |
Marcel Holtmann | 354d28d | 2005-09-13 01:32:31 +0200 | [diff] [blame] | 385 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) { |
| 386 | err = -EBADFD; |
| 387 | goto done; |
| 388 | } |
| 389 | |
| 390 | if (sk->sk_type != SOCK_STREAM) { |
| 391 | err = -EINVAL; |
| 392 | goto done; |
| 393 | } |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | sk->sk_state = BT_CONNECT; |
| 396 | bacpy(&bt_sk(sk)->dst, &sa->rc_bdaddr); |
| 397 | rfcomm_pi(sk)->channel = sa->rc_channel; |
| 398 | |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 399 | d->sec_level = rfcomm_pi(sk)->sec_level; |
| 400 | d->role_switch = rfcomm_pi(sk)->role_switch; |
Marcel Holtmann | 77db198 | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | err = rfcomm_dlc_open(d, &bt_sk(sk)->src, &sa->rc_bdaddr, sa->rc_channel); |
| 403 | if (!err) |
| 404 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
| 405 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
| 406 | |
Marcel Holtmann | 354d28d | 2005-09-13 01:32:31 +0200 | [diff] [blame] | 407 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | release_sock(sk); |
| 409 | return err; |
| 410 | } |
| 411 | |
| 412 | static int rfcomm_sock_listen(struct socket *sock, int backlog) |
| 413 | { |
| 414 | struct sock *sk = sock->sk; |
| 415 | int err = 0; |
| 416 | |
| 417 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 418 | |
| 419 | lock_sock(sk); |
| 420 | |
| 421 | if (sk->sk_state != BT_BOUND) { |
| 422 | err = -EBADFD; |
| 423 | goto done; |
| 424 | } |
| 425 | |
Marcel Holtmann | 354d28d | 2005-09-13 01:32:31 +0200 | [diff] [blame] | 426 | if (sk->sk_type != SOCK_STREAM) { |
| 427 | err = -EINVAL; |
| 428 | goto done; |
| 429 | } |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (!rfcomm_pi(sk)->channel) { |
| 432 | bdaddr_t *src = &bt_sk(sk)->src; |
| 433 | u8 channel; |
| 434 | |
| 435 | err = -EINVAL; |
| 436 | |
Gustavo F. Padovan | 95ca83f | 2011-12-27 15:28:44 -0200 | [diff] [blame] | 437 | write_lock(&rfcomm_sk_list.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
| 439 | for (channel = 1; channel < 31; channel++) |
| 440 | if (!__rfcomm_get_sock_by_addr(channel, src)) { |
| 441 | rfcomm_pi(sk)->channel = channel; |
| 442 | err = 0; |
| 443 | break; |
| 444 | } |
| 445 | |
Gustavo F. Padovan | 95ca83f | 2011-12-27 15:28:44 -0200 | [diff] [blame] | 446 | write_unlock(&rfcomm_sk_list.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | if (err < 0) |
| 449 | goto done; |
| 450 | } |
| 451 | |
| 452 | sk->sk_max_ack_backlog = backlog; |
| 453 | sk->sk_ack_backlog = 0; |
| 454 | sk->sk_state = BT_LISTEN; |
| 455 | |
| 456 | done: |
| 457 | release_sock(sk); |
| 458 | return err; |
| 459 | } |
| 460 | |
| 461 | static int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags) |
| 462 | { |
| 463 | DECLARE_WAITQUEUE(wait, current); |
| 464 | struct sock *sk = sock->sk, *nsk; |
| 465 | long timeo; |
| 466 | int err = 0; |
| 467 | |
Gustavo Padovan | dc2a0e2 | 2012-11-20 23:25:54 -0200 | [diff] [blame] | 468 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Marcel Holtmann | 354d28d | 2005-09-13 01:32:31 +0200 | [diff] [blame] | 470 | if (sk->sk_type != SOCK_STREAM) { |
| 471 | err = -EINVAL; |
| 472 | goto done; |
| 473 | } |
| 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 476 | |
| 477 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 478 | |
| 479 | /* Wait for an incoming connection. (wake-one). */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 480 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Peter Hurley | 950e2d5 | 2011-07-24 00:10:41 -0400 | [diff] [blame] | 481 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | set_current_state(TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | if (sk->sk_state != BT_LISTEN) { |
| 485 | err = -EBADFD; |
| 486 | break; |
| 487 | } |
| 488 | |
Peter Hurley | 950e2d5 | 2011-07-24 00:10:41 -0400 | [diff] [blame] | 489 | nsk = bt_accept_dequeue(sk, newsock); |
| 490 | if (nsk) |
| 491 | break; |
| 492 | |
| 493 | if (!timeo) { |
| 494 | err = -EAGAIN; |
| 495 | break; |
| 496 | } |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | if (signal_pending(current)) { |
| 499 | err = sock_intr_errno(timeo); |
| 500 | break; |
| 501 | } |
Peter Hurley | 950e2d5 | 2011-07-24 00:10:41 -0400 | [diff] [blame] | 502 | |
| 503 | release_sock(sk); |
| 504 | timeo = schedule_timeout(timeo); |
Gustavo Padovan | dc2a0e2 | 2012-11-20 23:25:54 -0200 | [diff] [blame] | 505 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
Peter Hurley | 950e2d5 | 2011-07-24 00:10:41 -0400 | [diff] [blame] | 507 | __set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 508 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
| 510 | if (err) |
| 511 | goto done; |
| 512 | |
| 513 | newsock->state = SS_CONNECTED; |
| 514 | |
| 515 | BT_DBG("new socket %p", nsk); |
| 516 | |
| 517 | done: |
| 518 | release_sock(sk); |
| 519 | return err; |
| 520 | } |
| 521 | |
| 522 | static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer) |
| 523 | { |
| 524 | struct sockaddr_rc *sa = (struct sockaddr_rc *) addr; |
| 525 | struct sock *sk = sock->sk; |
| 526 | |
| 527 | BT_DBG("sock %p, sk %p", sock, sk); |
| 528 | |
Mathias Krause | 9344a97 | 2012-08-15 11:31:50 +0000 | [diff] [blame] | 529 | memset(sa, 0, sizeof(*sa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | sa->rc_family = AF_BLUETOOTH; |
| 531 | sa->rc_channel = rfcomm_pi(sk)->channel; |
| 532 | if (peer) |
| 533 | bacpy(&sa->rc_bdaddr, &bt_sk(sk)->dst); |
| 534 | else |
| 535 | bacpy(&sa->rc_bdaddr, &bt_sk(sk)->src); |
| 536 | |
| 537 | *len = sizeof(struct sockaddr_rc); |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 542 | struct msghdr *msg, size_t len) |
| 543 | { |
| 544 | struct sock *sk = sock->sk; |
| 545 | struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc; |
| 546 | struct sk_buff *skb; |
Johan Hedberg | e793dcf | 2013-09-16 13:05:19 +0300 | [diff] [blame^] | 547 | int sent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 549 | if (test_bit(RFCOMM_DEFER_SETUP, &d->flags)) |
| 550 | return -ENOTCONN; |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (msg->msg_flags & MSG_OOB) |
| 553 | return -EOPNOTSUPP; |
| 554 | |
| 555 | if (sk->sk_shutdown & SEND_SHUTDOWN) |
| 556 | return -EPIPE; |
| 557 | |
| 558 | BT_DBG("sock %p, sk %p", sock, sk); |
| 559 | |
| 560 | lock_sock(sk); |
| 561 | |
Johan Hedberg | e793dcf | 2013-09-16 13:05:19 +0300 | [diff] [blame^] | 562 | sent = bt_sock_wait_ready(sk, msg->msg_flags); |
| 563 | if (sent) |
| 564 | goto done; |
| 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | while (len) { |
| 567 | size_t size = min_t(size_t, len, d->mtu); |
Marcel Holtmann | 4d6a218 | 2007-01-08 02:16:31 +0100 | [diff] [blame] | 568 | int err; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE, |
| 571 | msg->msg_flags & MSG_DONTWAIT, &err); |
Victor Shcherbatyuk | 91aa35a | 2009-01-15 21:52:12 +0100 | [diff] [blame] | 572 | if (!skb) { |
| 573 | if (sent == 0) |
| 574 | sent = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | break; |
Victor Shcherbatyuk | 91aa35a | 2009-01-15 21:52:12 +0100 | [diff] [blame] | 576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE); |
| 578 | |
| 579 | err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size); |
| 580 | if (err) { |
| 581 | kfree_skb(skb); |
Marcel Holtmann | 4d6a218 | 2007-01-08 02:16:31 +0100 | [diff] [blame] | 582 | if (sent == 0) |
| 583 | sent = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | |
Luiz Augusto von Dentz | 262038f | 2011-11-01 10:58:58 +0200 | [diff] [blame] | 587 | skb->priority = sk->sk_priority; |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | err = rfcomm_dlc_send(d, skb); |
| 590 | if (err < 0) { |
| 591 | kfree_skb(skb); |
Marcel Holtmann | 4d6a218 | 2007-01-08 02:16:31 +0100 | [diff] [blame] | 592 | if (sent == 0) |
| 593 | sent = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | break; |
| 595 | } |
| 596 | |
| 597 | sent += size; |
| 598 | len -= size; |
| 599 | } |
| 600 | |
Johan Hedberg | e793dcf | 2013-09-16 13:05:19 +0300 | [diff] [blame^] | 601 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | release_sock(sk); |
| 603 | |
Marcel Holtmann | 4d6a218 | 2007-01-08 02:16:31 +0100 | [diff] [blame] | 604 | return sent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 608 | struct msghdr *msg, size_t size, int flags) |
| 609 | { |
| 610 | struct sock *sk = sock->sk; |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 611 | struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc; |
Mat Martineau | 3d7d01d | 2010-09-08 10:05:28 -0700 | [diff] [blame] | 612 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 614 | if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) { |
| 615 | rfcomm_dlc_accept(d); |
Mathias Krause | e11e045 | 2013-04-07 01:51:50 +0000 | [diff] [blame] | 616 | msg->msg_namelen = 0; |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
Mat Martineau | 3d7d01d | 2010-09-08 10:05:28 -0700 | [diff] [blame] | 620 | len = bt_sock_stream_recvmsg(iocb, sock, msg, size, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | lock_sock(sk); |
Mat Martineau | 3d7d01d | 2010-09-08 10:05:28 -0700 | [diff] [blame] | 623 | if (!(flags & MSG_PEEK) && len > 0) |
| 624 | atomic_sub(len, &sk->sk_rmem_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | if (atomic_read(&sk->sk_rmem_alloc) <= (sk->sk_rcvbuf >> 2)) |
| 627 | rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | release_sock(sk); |
Mat Martineau | 3d7d01d | 2010-09-08 10:05:28 -0700 | [diff] [blame] | 629 | |
| 630 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 633 | static int rfcomm_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
| 635 | struct sock *sk = sock->sk; |
| 636 | int err = 0; |
| 637 | u32 opt; |
| 638 | |
| 639 | BT_DBG("sk %p", sk); |
| 640 | |
| 641 | lock_sock(sk); |
| 642 | |
| 643 | switch (optname) { |
| 644 | case RFCOMM_LM: |
| 645 | if (get_user(opt, (u32 __user *) optval)) { |
| 646 | err = -EFAULT; |
| 647 | break; |
| 648 | } |
| 649 | |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 650 | if (opt & RFCOMM_LM_AUTH) |
| 651 | rfcomm_pi(sk)->sec_level = BT_SECURITY_LOW; |
| 652 | if (opt & RFCOMM_LM_ENCRYPT) |
| 653 | rfcomm_pi(sk)->sec_level = BT_SECURITY_MEDIUM; |
| 654 | if (opt & RFCOMM_LM_SECURE) |
| 655 | rfcomm_pi(sk)->sec_level = BT_SECURITY_HIGH; |
| 656 | |
| 657 | rfcomm_pi(sk)->role_switch = (opt & RFCOMM_LM_MASTER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | break; |
| 659 | |
| 660 | default: |
| 661 | err = -ENOPROTOOPT; |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | release_sock(sk); |
| 666 | return err; |
| 667 | } |
| 668 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 669 | static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 670 | { |
| 671 | struct sock *sk = sock->sk; |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 672 | struct bt_security sec; |
Stephen Boyd | d0fad89 | 2011-05-12 16:50:09 -0700 | [diff] [blame] | 673 | int err = 0; |
| 674 | size_t len; |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 675 | u32 opt; |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 676 | |
| 677 | BT_DBG("sk %p", sk); |
| 678 | |
| 679 | if (level == SOL_RFCOMM) |
| 680 | return rfcomm_sock_setsockopt_old(sock, optname, optval, optlen); |
| 681 | |
Marcel Holtmann | 0588d94 | 2009-01-16 10:06:13 +0100 | [diff] [blame] | 682 | if (level != SOL_BLUETOOTH) |
| 683 | return -ENOPROTOOPT; |
| 684 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 685 | lock_sock(sk); |
| 686 | |
| 687 | switch (optname) { |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 688 | case BT_SECURITY: |
Marcel Holtmann | 0588d94 | 2009-01-16 10:06:13 +0100 | [diff] [blame] | 689 | if (sk->sk_type != SOCK_STREAM) { |
| 690 | err = -EINVAL; |
| 691 | break; |
| 692 | } |
| 693 | |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 694 | sec.level = BT_SECURITY_LOW; |
| 695 | |
| 696 | len = min_t(unsigned int, sizeof(sec), optlen); |
| 697 | if (copy_from_user((char *) &sec, optval, len)) { |
| 698 | err = -EFAULT; |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | if (sec.level > BT_SECURITY_HIGH) { |
| 703 | err = -EINVAL; |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | rfcomm_pi(sk)->sec_level = sec.level; |
| 708 | break; |
| 709 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 710 | case BT_DEFER_SETUP: |
| 711 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 712 | err = -EINVAL; |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | if (get_user(opt, (u32 __user *) optval)) { |
| 717 | err = -EFAULT; |
| 718 | break; |
| 719 | } |
| 720 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 721 | if (opt) |
| 722 | set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 723 | else |
| 724 | clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 725 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 726 | break; |
| 727 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 728 | default: |
| 729 | err = -ENOPROTOOPT; |
| 730 | break; |
| 731 | } |
| 732 | |
| 733 | release_sock(sk); |
| 734 | return err; |
| 735 | } |
| 736 | |
| 737 | static int rfcomm_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] | 738 | { |
| 739 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | struct rfcomm_conninfo cinfo; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 741 | struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | int len, err = 0; |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 743 | u32 opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
| 745 | BT_DBG("sk %p", sk); |
| 746 | |
| 747 | if (get_user(len, optlen)) |
| 748 | return -EFAULT; |
| 749 | |
| 750 | lock_sock(sk); |
| 751 | |
| 752 | switch (optname) { |
| 753 | case RFCOMM_LM: |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 754 | switch (rfcomm_pi(sk)->sec_level) { |
| 755 | case BT_SECURITY_LOW: |
| 756 | opt = RFCOMM_LM_AUTH; |
| 757 | break; |
| 758 | case BT_SECURITY_MEDIUM: |
| 759 | opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT; |
| 760 | break; |
| 761 | case BT_SECURITY_HIGH: |
| 762 | opt = RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT | |
| 763 | RFCOMM_LM_SECURE; |
| 764 | break; |
| 765 | default: |
| 766 | opt = 0; |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | if (rfcomm_pi(sk)->role_switch) |
| 771 | opt |= RFCOMM_LM_MASTER; |
| 772 | |
| 773 | if (put_user(opt, (u32 __user *) optval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | err = -EFAULT; |
| 775 | break; |
| 776 | |
| 777 | case RFCOMM_CONNINFO: |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 778 | if (sk->sk_state != BT_CONNECTED && |
| 779 | !rfcomm_pi(sk)->dlc->defer_setup) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | err = -ENOTCONN; |
| 781 | break; |
| 782 | } |
| 783 | |
Filip Palian | 8d03e97 | 2011-05-12 19:32:46 +0200 | [diff] [blame] | 784 | memset(&cinfo, 0, sizeof(cinfo)); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 785 | cinfo.hci_handle = conn->hcon->handle; |
| 786 | memcpy(cinfo.dev_class, conn->hcon->dev_class, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
| 788 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 789 | if (copy_to_user(optval, (char *) &cinfo, len)) |
| 790 | err = -EFAULT; |
| 791 | |
| 792 | break; |
| 793 | |
| 794 | default: |
| 795 | err = -ENOPROTOOPT; |
| 796 | break; |
| 797 | } |
| 798 | |
| 799 | release_sock(sk); |
| 800 | return err; |
| 801 | } |
| 802 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 803 | static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 804 | { |
| 805 | struct sock *sk = sock->sk; |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 806 | struct bt_security sec; |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 807 | int len, err = 0; |
| 808 | |
| 809 | BT_DBG("sk %p", sk); |
| 810 | |
| 811 | if (level == SOL_RFCOMM) |
| 812 | return rfcomm_sock_getsockopt_old(sock, optname, optval, optlen); |
| 813 | |
Marcel Holtmann | 0588d94 | 2009-01-16 10:06:13 +0100 | [diff] [blame] | 814 | if (level != SOL_BLUETOOTH) |
| 815 | return -ENOPROTOOPT; |
| 816 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 817 | if (get_user(len, optlen)) |
| 818 | return -EFAULT; |
| 819 | |
| 820 | lock_sock(sk); |
| 821 | |
| 822 | switch (optname) { |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 823 | case BT_SECURITY: |
Marcel Holtmann | 0588d94 | 2009-01-16 10:06:13 +0100 | [diff] [blame] | 824 | if (sk->sk_type != SOCK_STREAM) { |
| 825 | err = -EINVAL; |
| 826 | break; |
| 827 | } |
| 828 | |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 829 | sec.level = rfcomm_pi(sk)->sec_level; |
Mathias Krause | 9ad2de4 | 2012-08-15 11:31:48 +0000 | [diff] [blame] | 830 | sec.key_size = 0; |
Marcel Holtmann | 9f2c8a0 | 2009-01-15 21:58:40 +0100 | [diff] [blame] | 831 | |
| 832 | len = min_t(unsigned int, len, sizeof(sec)); |
| 833 | if (copy_to_user(optval, (char *) &sec, len)) |
| 834 | err = -EFAULT; |
| 835 | |
| 836 | break; |
| 837 | |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 838 | case BT_DEFER_SETUP: |
| 839 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 840 | err = -EINVAL; |
| 841 | break; |
| 842 | } |
| 843 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 844 | if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags), |
| 845 | (u32 __user *) optval)) |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 846 | err = -EFAULT; |
| 847 | |
| 848 | break; |
| 849 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 850 | default: |
| 851 | err = -ENOPROTOOPT; |
| 852 | break; |
| 853 | } |
| 854 | |
| 855 | release_sock(sk); |
| 856 | return err; |
| 857 | } |
| 858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 860 | { |
David S. Miller | e19caae | 2008-12-09 01:04:27 -0800 | [diff] [blame] | 861 | struct sock *sk __maybe_unused = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | int err; |
| 863 | |
David S. Miller | e19caae | 2008-12-09 01:04:27 -0800 | [diff] [blame] | 864 | BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 866 | err = bt_sock_ioctl(sock, cmd, arg); |
| 867 | |
| 868 | if (err == -ENOIOCTLCMD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | #ifdef CONFIG_BT_RFCOMM_TTY |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 870 | lock_sock(sk); |
| 871 | err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg); |
| 872 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | #else |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 874 | err = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | #endif |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | return err; |
| 879 | } |
| 880 | |
| 881 | static int rfcomm_sock_shutdown(struct socket *sock, int how) |
| 882 | { |
| 883 | struct sock *sk = sock->sk; |
| 884 | int err = 0; |
| 885 | |
| 886 | BT_DBG("sock %p, sk %p", sock, sk); |
| 887 | |
Andrei Emeltchenko | 285b4e9 | 2010-12-01 16:58:23 +0200 | [diff] [blame] | 888 | if (!sk) |
| 889 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | |
| 891 | lock_sock(sk); |
| 892 | if (!sk->sk_shutdown) { |
| 893 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 894 | __rfcomm_sock_close(sk); |
| 895 | |
| 896 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 897 | err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); |
| 898 | } |
| 899 | release_sock(sk); |
| 900 | return err; |
| 901 | } |
| 902 | |
| 903 | static int rfcomm_sock_release(struct socket *sock) |
| 904 | { |
| 905 | struct sock *sk = sock->sk; |
| 906 | int err; |
| 907 | |
| 908 | BT_DBG("sock %p, sk %p", sock, sk); |
| 909 | |
| 910 | if (!sk) |
| 911 | return 0; |
| 912 | |
| 913 | err = rfcomm_sock_shutdown(sock, 2); |
| 914 | |
| 915 | sock_orphan(sk); |
| 916 | rfcomm_sock_kill(sk); |
| 917 | return err; |
| 918 | } |
| 919 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 920 | /* ---- RFCOMM core layer callbacks ---- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | * |
| 922 | * called under rfcomm_lock() |
| 923 | */ |
| 924 | int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d) |
| 925 | { |
| 926 | struct sock *sk, *parent; |
| 927 | bdaddr_t src, dst; |
| 928 | int result = 0; |
| 929 | |
| 930 | BT_DBG("session %p channel %d", s, channel); |
| 931 | |
| 932 | rfcomm_session_getaddr(s, &src, &dst); |
| 933 | |
| 934 | /* Check if we have socket listening on channel */ |
| 935 | parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src); |
| 936 | if (!parent) |
| 937 | return 0; |
| 938 | |
Gustavo F. Padovan | eeb3665 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 939 | bh_lock_sock(parent); |
| 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | /* Check for backlog size */ |
| 942 | if (sk_acceptq_is_full(parent)) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 943 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | goto done; |
| 945 | } |
| 946 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 947 | sk = rfcomm_sock_alloc(sock_net(parent), NULL, BTPROTO_RFCOMM, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | if (!sk) |
| 949 | goto done; |
| 950 | |
Octavian Purdila | b5a30dd | 2012-01-22 00:28:34 +0200 | [diff] [blame] | 951 | bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM); |
| 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | rfcomm_sock_init(sk, parent); |
| 954 | bacpy(&bt_sk(sk)->src, &src); |
| 955 | bacpy(&bt_sk(sk)->dst, &dst); |
| 956 | rfcomm_pi(sk)->channel = channel; |
| 957 | |
| 958 | sk->sk_state = BT_CONFIG; |
| 959 | bt_accept_enqueue(parent, sk); |
| 960 | |
| 961 | /* Accept connection and return socket DLC */ |
| 962 | *d = rfcomm_pi(sk)->dlc; |
| 963 | result = 1; |
| 964 | |
| 965 | done: |
| 966 | bh_unlock_sock(parent); |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 967 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 968 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) |
Marcel Holtmann | bb23c0a | 2009-01-15 21:56:48 +0100 | [diff] [blame] | 969 | parent->sk_state_change(parent); |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | return result; |
| 972 | } |
| 973 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 974 | static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | { |
| 976 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
Gustavo F. Padovan | 95ca83f | 2011-12-27 15:28:44 -0200 | [diff] [blame] | 978 | read_lock(&rfcomm_sk_list.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 980 | sk_for_each(sk, &rfcomm_sk_list.head) { |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 981 | seq_printf(f, "%pMR %pMR %d %d\n", |
| 982 | &bt_sk(sk)->src, &bt_sk(sk)->dst, |
| 983 | sk->sk_state, rfcomm_pi(sk)->channel); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 984 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Gustavo F. Padovan | 95ca83f | 2011-12-27 15:28:44 -0200 | [diff] [blame] | 986 | read_unlock(&rfcomm_sk_list.lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 987 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 988 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 991 | static int rfcomm_sock_debugfs_open(struct inode *inode, struct file *file) |
| 992 | { |
| 993 | return single_open(file, rfcomm_sock_debugfs_show, inode->i_private); |
| 994 | } |
| 995 | |
| 996 | static const struct file_operations rfcomm_sock_debugfs_fops = { |
| 997 | .open = rfcomm_sock_debugfs_open, |
| 998 | .read = seq_read, |
| 999 | .llseek = seq_lseek, |
| 1000 | .release = single_release, |
| 1001 | }; |
| 1002 | |
| 1003 | static struct dentry *rfcomm_sock_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1005 | static const struct proto_ops rfcomm_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | .family = PF_BLUETOOTH, |
| 1007 | .owner = THIS_MODULE, |
| 1008 | .release = rfcomm_sock_release, |
| 1009 | .bind = rfcomm_sock_bind, |
| 1010 | .connect = rfcomm_sock_connect, |
| 1011 | .listen = rfcomm_sock_listen, |
| 1012 | .accept = rfcomm_sock_accept, |
| 1013 | .getname = rfcomm_sock_getname, |
| 1014 | .sendmsg = rfcomm_sock_sendmsg, |
| 1015 | .recvmsg = rfcomm_sock_recvmsg, |
| 1016 | .shutdown = rfcomm_sock_shutdown, |
| 1017 | .setsockopt = rfcomm_sock_setsockopt, |
| 1018 | .getsockopt = rfcomm_sock_getsockopt, |
| 1019 | .ioctl = rfcomm_sock_ioctl, |
| 1020 | .poll = bt_sock_poll, |
| 1021 | .socketpair = sock_no_socketpair, |
| 1022 | .mmap = sock_no_mmap |
| 1023 | }; |
| 1024 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1025 | static const struct net_proto_family rfcomm_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | .family = PF_BLUETOOTH, |
| 1027 | .owner = THIS_MODULE, |
| 1028 | .create = rfcomm_sock_create |
| 1029 | }; |
| 1030 | |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1031 | int __init rfcomm_init_sockets(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | { |
| 1033 | int err; |
| 1034 | |
| 1035 | err = proto_register(&rfcomm_proto, 0); |
| 1036 | if (err < 0) |
| 1037 | return err; |
| 1038 | |
| 1039 | err = bt_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops); |
Masatake YAMATO | c6f5df16 | 2012-07-26 01:29:49 +0900 | [diff] [blame] | 1040 | if (err < 0) { |
| 1041 | BT_ERR("RFCOMM socket layer registration failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | goto error; |
Masatake YAMATO | c6f5df16 | 2012-07-26 01:29:49 +0900 | [diff] [blame] | 1043 | } |
| 1044 | |
Al Viro | b031661 | 2013-04-04 19:14:33 -0400 | [diff] [blame] | 1045 | err = bt_procfs_init(&init_net, "rfcomm", &rfcomm_sk_list, NULL); |
Masatake YAMATO | c6f5df16 | 2012-07-26 01:29:49 +0900 | [diff] [blame] | 1046 | if (err < 0) { |
| 1047 | BT_ERR("Failed to create RFCOMM proc file"); |
| 1048 | bt_sock_unregister(BTPROTO_RFCOMM); |
| 1049 | goto error; |
| 1050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1052 | if (bt_debugfs) { |
| 1053 | rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444, |
| 1054 | bt_debugfs, NULL, &rfcomm_sock_debugfs_fops); |
| 1055 | if (!rfcomm_sock_debugfs) |
| 1056 | BT_ERR("Failed to create RFCOMM debug file"); |
| 1057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
| 1059 | BT_INFO("RFCOMM socket layer initialized"); |
| 1060 | |
| 1061 | return 0; |
| 1062 | |
| 1063 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | proto_unregister(&rfcomm_proto); |
| 1065 | return err; |
| 1066 | } |
| 1067 | |
Gustavo F. Padovan | 2f8362a | 2010-07-24 02:04:45 -0300 | [diff] [blame] | 1068 | void __exit rfcomm_cleanup_sockets(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | { |
Masatake YAMATO | c6f5df16 | 2012-07-26 01:29:49 +0900 | [diff] [blame] | 1070 | bt_procfs_cleanup(&init_net, "rfcomm"); |
| 1071 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1072 | debugfs_remove(rfcomm_sock_debugfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
David Herrmann | 5e9d7f8 | 2013-02-24 19:36:51 +0100 | [diff] [blame] | 1074 | bt_sock_unregister(BTPROTO_RFCOMM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
| 1076 | proto_unregister(&rfcomm_proto); |
| 1077 | } |