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