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