Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
| 4 | Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> |
| 5 | Copyright (C) 2010 Google Inc. |
Gustavo F. Padovan | 590051d | 2011-12-18 13:39:33 -0200 | [diff] [blame] | 6 | Copyright (C) 2011 ProFUSION Embedded Systems |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7 | |
| 8 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License version 2 as |
| 12 | published by the Free Software Foundation; |
| 13 | |
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 17 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 18 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 21 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 24 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 25 | SOFTWARE IS DISCLAIMED. |
| 26 | */ |
| 27 | |
| 28 | /* Bluetooth L2CAP sockets. */ |
| 29 | |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 30 | #include <linux/export.h> |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 31 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 32 | #include <net/bluetooth/bluetooth.h> |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 33 | #include <net/bluetooth/hci_core.h> |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 34 | #include <net/bluetooth/l2cap.h> |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 35 | #include <net/bluetooth/smp.h> |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 36 | |
Masatake YAMATO | 5b28d95 | 2012-07-26 01:29:25 +0900 | [diff] [blame] | 37 | static struct bt_sock_list l2cap_sk_list = { |
| 38 | .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock) |
| 39 | }; |
| 40 | |
Gustavo F. Padovan | cf2f90f | 2011-04-27 18:40:39 -0300 | [diff] [blame] | 41 | static const struct proto_ops l2cap_sock_ops; |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 42 | static void l2cap_sock_init(struct sock *sk, struct sock *parent); |
| 43 | static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio); |
Gustavo F. Padovan | cf2f90f | 2011-04-27 18:40:39 -0300 | [diff] [blame] | 44 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 45 | static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) |
| 46 | { |
| 47 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 48 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 49 | struct sockaddr_l2 la; |
| 50 | int len, err = 0; |
| 51 | |
| 52 | BT_DBG("sk %p", sk); |
| 53 | |
| 54 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | memset(&la, 0, sizeof(la)); |
| 58 | len = min_t(unsigned int, sizeof(la), alen); |
| 59 | memcpy(&la, addr, len); |
| 60 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 61 | if (la.l2_cid && la.l2_psm) |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 62 | return -EINVAL; |
| 63 | |
| 64 | lock_sock(sk); |
| 65 | |
| 66 | if (sk->sk_state != BT_OPEN) { |
| 67 | err = -EBADFD; |
| 68 | goto done; |
| 69 | } |
| 70 | |
| 71 | if (la.l2_psm) { |
| 72 | __u16 psm = __le16_to_cpu(la.l2_psm); |
| 73 | |
| 74 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 75 | if ((psm & 0x0101) != 0x0001) { |
| 76 | err = -EINVAL; |
| 77 | goto done; |
| 78 | } |
| 79 | |
| 80 | /* Restrict usage of well-known PSMs */ |
| 81 | if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) { |
| 82 | err = -EACCES; |
| 83 | goto done; |
| 84 | } |
| 85 | } |
| 86 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 87 | if (la.l2_cid) |
Santosh Nayak | 6e4aff1 | 2012-03-01 22:46:36 +0530 | [diff] [blame] | 88 | err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid)); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 89 | else |
| 90 | err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 91 | |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 92 | if (err < 0) |
| 93 | goto done; |
| 94 | |
Andrei Emeltchenko | 2983fd6 | 2012-05-24 15:42:50 +0300 | [diff] [blame] | 95 | if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP || |
| 96 | __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM) |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 97 | chan->sec_level = BT_SECURITY_SDP; |
| 98 | |
| 99 | bacpy(&bt_sk(sk)->src, &la.l2_bdaddr); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 100 | |
| 101 | chan->state = BT_BOUND; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 102 | sk->sk_state = BT_BOUND; |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 103 | |
| 104 | done: |
| 105 | release_sock(sk); |
| 106 | return err; |
| 107 | } |
| 108 | |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 109 | static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 110 | { |
| 111 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 112 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 113 | struct sockaddr_l2 la; |
| 114 | int len, err = 0; |
| 115 | |
| 116 | BT_DBG("sk %p", sk); |
| 117 | |
| 118 | if (!addr || alen < sizeof(addr->sa_family) || |
| 119 | addr->sa_family != AF_BLUETOOTH) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | memset(&la, 0, sizeof(la)); |
| 123 | len = min_t(unsigned int, sizeof(la), alen); |
| 124 | memcpy(&la, addr, len); |
| 125 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 126 | if (la.l2_cid && la.l2_psm) |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 127 | return -EINVAL; |
| 128 | |
Santosh Nayak | 6e4aff1 | 2012-03-01 22:46:36 +0530 | [diff] [blame] | 129 | err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid), |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 130 | &la.l2_bdaddr, la.l2_bdaddr_type); |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 131 | if (err) |
Andrei Emeltchenko | b3fb611 | 2012-02-22 17:11:57 +0200 | [diff] [blame] | 132 | return err; |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 133 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 134 | lock_sock(sk); |
| 135 | |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 136 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
| 137 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Andrei Emeltchenko | b3fb611 | 2012-02-22 17:11:57 +0200 | [diff] [blame] | 138 | |
| 139 | release_sock(sk); |
| 140 | |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 141 | return err; |
| 142 | } |
| 143 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 144 | static int l2cap_sock_listen(struct socket *sock, int backlog) |
| 145 | { |
| 146 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 147 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 148 | int err = 0; |
| 149 | |
| 150 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 151 | |
| 152 | lock_sock(sk); |
| 153 | |
Marcel Holtmann | 6b3af73 | 2012-04-19 13:43:51 +0200 | [diff] [blame] | 154 | if (sk->sk_state != BT_BOUND) { |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 155 | err = -EBADFD; |
| 156 | goto done; |
| 157 | } |
| 158 | |
Marcel Holtmann | 6b3af73 | 2012-04-19 13:43:51 +0200 | [diff] [blame] | 159 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) { |
| 160 | err = -EINVAL; |
| 161 | goto done; |
| 162 | } |
| 163 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 164 | switch (chan->mode) { |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 165 | case L2CAP_MODE_BASIC: |
| 166 | break; |
| 167 | case L2CAP_MODE_ERTM: |
| 168 | case L2CAP_MODE_STREAMING: |
| 169 | if (!disable_ertm) |
| 170 | break; |
| 171 | /* fall through */ |
| 172 | default: |
| 173 | err = -ENOTSUPP; |
| 174 | goto done; |
| 175 | } |
| 176 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 177 | sk->sk_max_ack_backlog = backlog; |
| 178 | sk->sk_ack_backlog = 0; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 179 | |
| 180 | chan->state = BT_LISTEN; |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 181 | sk->sk_state = BT_LISTEN; |
| 182 | |
| 183 | done: |
| 184 | release_sock(sk); |
| 185 | return err; |
| 186 | } |
| 187 | |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 188 | static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags) |
| 189 | { |
| 190 | DECLARE_WAITQUEUE(wait, current); |
| 191 | struct sock *sk = sock->sk, *nsk; |
| 192 | long timeo; |
| 193 | int err = 0; |
| 194 | |
| 195 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 196 | |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 197 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 198 | |
| 199 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 200 | |
| 201 | /* Wait for an incoming connection. (wake-one). */ |
| 202 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Peter Hurley | f9a3c20 | 2011-07-24 00:10:52 -0400 | [diff] [blame] | 203 | while (1) { |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 204 | set_current_state(TASK_INTERRUPTIBLE); |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 205 | |
| 206 | if (sk->sk_state != BT_LISTEN) { |
| 207 | err = -EBADFD; |
| 208 | break; |
| 209 | } |
| 210 | |
Peter Hurley | f9a3c20 | 2011-07-24 00:10:52 -0400 | [diff] [blame] | 211 | nsk = bt_accept_dequeue(sk, newsock); |
| 212 | if (nsk) |
| 213 | break; |
| 214 | |
| 215 | if (!timeo) { |
| 216 | err = -EAGAIN; |
| 217 | break; |
| 218 | } |
| 219 | |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 220 | if (signal_pending(current)) { |
| 221 | err = sock_intr_errno(timeo); |
| 222 | break; |
| 223 | } |
Peter Hurley | f9a3c20 | 2011-07-24 00:10:52 -0400 | [diff] [blame] | 224 | |
| 225 | release_sock(sk); |
| 226 | timeo = schedule_timeout(timeo); |
| 227 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 228 | } |
Peter Hurley | f9a3c20 | 2011-07-24 00:10:52 -0400 | [diff] [blame] | 229 | __set_current_state(TASK_RUNNING); |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 230 | remove_wait_queue(sk_sleep(sk), &wait); |
| 231 | |
| 232 | if (err) |
| 233 | goto done; |
| 234 | |
| 235 | newsock->state = SS_CONNECTED; |
| 236 | |
| 237 | BT_DBG("new socket %p", nsk); |
| 238 | |
| 239 | done: |
| 240 | release_sock(sk); |
| 241 | return err; |
| 242 | } |
| 243 | |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 244 | static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer) |
| 245 | { |
| 246 | struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr; |
| 247 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 248 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 249 | |
| 250 | BT_DBG("sock %p, sk %p", sock, sk); |
| 251 | |
| 252 | addr->sa_family = AF_BLUETOOTH; |
| 253 | *len = sizeof(struct sockaddr_l2); |
| 254 | |
| 255 | if (peer) { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 256 | la->l2_psm = chan->psm; |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 257 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 258 | la->l2_cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 259 | } else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 260 | la->l2_psm = chan->sport; |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 261 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->src); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 262 | la->l2_cid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 268 | static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) |
| 269 | { |
| 270 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 271 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 272 | struct l2cap_options opts; |
| 273 | struct l2cap_conninfo cinfo; |
| 274 | int len, err = 0; |
| 275 | u32 opt; |
| 276 | |
| 277 | BT_DBG("sk %p", sk); |
| 278 | |
| 279 | if (get_user(len, optlen)) |
| 280 | return -EFAULT; |
| 281 | |
| 282 | lock_sock(sk); |
| 283 | |
| 284 | switch (optname) { |
| 285 | case L2CAP_OPTIONS: |
Vasiliy Kulikov | e3fb592 | 2011-02-10 20:59:42 +0300 | [diff] [blame] | 286 | memset(&opts, 0, sizeof(opts)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 287 | opts.imtu = chan->imtu; |
| 288 | opts.omtu = chan->omtu; |
| 289 | opts.flush_to = chan->flush_to; |
| 290 | opts.mode = chan->mode; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 291 | opts.fcs = chan->fcs; |
| 292 | opts.max_tx = chan->max_tx; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 293 | opts.txwin_size = chan->tx_win; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 294 | |
| 295 | len = min_t(unsigned int, len, sizeof(opts)); |
| 296 | if (copy_to_user(optval, (char *) &opts, len)) |
| 297 | err = -EFAULT; |
| 298 | |
| 299 | break; |
| 300 | |
| 301 | case L2CAP_LM: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 302 | switch (chan->sec_level) { |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 303 | case BT_SECURITY_LOW: |
| 304 | opt = L2CAP_LM_AUTH; |
| 305 | break; |
| 306 | case BT_SECURITY_MEDIUM: |
| 307 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT; |
| 308 | break; |
| 309 | case BT_SECURITY_HIGH: |
| 310 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | |
| 311 | L2CAP_LM_SECURE; |
| 312 | break; |
| 313 | default: |
| 314 | opt = 0; |
| 315 | break; |
| 316 | } |
| 317 | |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 318 | if (test_bit(FLAG_ROLE_SWITCH, &chan->flags)) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 319 | opt |= L2CAP_LM_MASTER; |
| 320 | |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 321 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 322 | opt |= L2CAP_LM_RELIABLE; |
| 323 | |
| 324 | if (put_user(opt, (u32 __user *) optval)) |
| 325 | err = -EFAULT; |
| 326 | break; |
| 327 | |
| 328 | case L2CAP_CONNINFO: |
| 329 | if (sk->sk_state != BT_CONNECTED && |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 330 | !(sk->sk_state == BT_CONNECT2 && |
| 331 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 332 | err = -ENOTCONN; |
| 333 | break; |
| 334 | } |
| 335 | |
Filip Palian | 8d03e97 | 2011-05-12 19:32:46 +0200 | [diff] [blame] | 336 | memset(&cinfo, 0, sizeof(cinfo)); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 337 | cinfo.hci_handle = chan->conn->hcon->handle; |
| 338 | memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3); |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 339 | |
| 340 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 341 | if (copy_to_user(optval, (char *) &cinfo, len)) |
| 342 | err = -EFAULT; |
| 343 | |
| 344 | break; |
| 345 | |
| 346 | default: |
| 347 | err = -ENOPROTOOPT; |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | release_sock(sk); |
| 352 | return err; |
| 353 | } |
| 354 | |
| 355 | static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 356 | { |
| 357 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 358 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 359 | struct bt_security sec; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 360 | struct bt_power pwr; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 361 | int len, err = 0; |
| 362 | |
| 363 | BT_DBG("sk %p", sk); |
| 364 | |
| 365 | if (level == SOL_L2CAP) |
| 366 | return l2cap_sock_getsockopt_old(sock, optname, optval, optlen); |
| 367 | |
| 368 | if (level != SOL_BLUETOOTH) |
| 369 | return -ENOPROTOOPT; |
| 370 | |
| 371 | if (get_user(len, optlen)) |
| 372 | return -EFAULT; |
| 373 | |
| 374 | lock_sock(sk); |
| 375 | |
| 376 | switch (optname) { |
| 377 | case BT_SECURITY: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 378 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && |
| 379 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 380 | err = -EINVAL; |
| 381 | break; |
| 382 | } |
| 383 | |
Vinicius Costa Gomes | 8f36011 | 2011-07-08 18:31:46 -0300 | [diff] [blame] | 384 | memset(&sec, 0, sizeof(sec)); |
Gustavo Padovan | c6585a4 | 2012-05-07 03:07:26 -0300 | [diff] [blame] | 385 | if (chan->conn) |
| 386 | sec.level = chan->conn->hcon->sec_level; |
| 387 | else |
| 388 | sec.level = chan->sec_level; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 389 | |
Vinicius Costa Gomes | 8f36011 | 2011-07-08 18:31:46 -0300 | [diff] [blame] | 390 | if (sk->sk_state == BT_CONNECTED) |
| 391 | sec.key_size = chan->conn->hcon->enc_key_size; |
| 392 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 393 | len = min_t(unsigned int, len, sizeof(sec)); |
| 394 | if (copy_to_user(optval, (char *) &sec, len)) |
| 395 | err = -EFAULT; |
| 396 | |
| 397 | break; |
| 398 | |
| 399 | case BT_DEFER_SETUP: |
| 400 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 401 | err = -EINVAL; |
| 402 | break; |
| 403 | } |
| 404 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 405 | if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags), |
| 406 | (u32 __user *) optval)) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 407 | err = -EFAULT; |
| 408 | |
| 409 | break; |
| 410 | |
| 411 | case BT_FLUSHABLE: |
Andrei Emeltchenko | d57b0e8 | 2011-10-11 14:04:31 +0300 | [diff] [blame] | 412 | if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags), |
| 413 | (u32 __user *) optval)) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 414 | err = -EFAULT; |
| 415 | |
| 416 | break; |
| 417 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 418 | case BT_POWER: |
| 419 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 420 | && sk->sk_type != SOCK_RAW) { |
| 421 | err = -EINVAL; |
| 422 | break; |
| 423 | } |
| 424 | |
Andrei Emeltchenko | 15770b1 | 2011-10-11 14:04:33 +0300 | [diff] [blame] | 425 | pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 426 | |
| 427 | len = min_t(unsigned int, len, sizeof(pwr)); |
| 428 | if (copy_to_user(optval, (char *) &pwr, len)) |
| 429 | err = -EFAULT; |
| 430 | |
| 431 | break; |
| 432 | |
Mat Martineau | 2ea6648 | 2011-11-02 16:18:30 -0700 | [diff] [blame] | 433 | case BT_CHANNEL_POLICY: |
| 434 | if (!enable_hs) { |
| 435 | err = -ENOPROTOOPT; |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | if (put_user(chan->chan_policy, (u32 __user *) optval)) |
| 440 | err = -EFAULT; |
| 441 | break; |
| 442 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 443 | default: |
| 444 | err = -ENOPROTOOPT; |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | release_sock(sk); |
| 449 | return err; |
| 450 | } |
| 451 | |
Andre Guedes | 682877c | 2012-05-31 17:01:34 -0300 | [diff] [blame] | 452 | static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu) |
| 453 | { |
| 454 | switch (chan->scid) { |
| 455 | case L2CAP_CID_LE_DATA: |
Andre Guedes | 8c3a4f0 | 2012-05-31 17:01:35 -0300 | [diff] [blame] | 456 | if (mtu < L2CAP_LE_MIN_MTU) |
Andre Guedes | 682877c | 2012-05-31 17:01:34 -0300 | [diff] [blame] | 457 | return false; |
| 458 | break; |
| 459 | |
| 460 | default: |
| 461 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | return true; |
| 466 | } |
| 467 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 468 | static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen) |
| 469 | { |
| 470 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 471 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 472 | struct l2cap_options opts; |
| 473 | int len, err = 0; |
| 474 | u32 opt; |
| 475 | |
| 476 | BT_DBG("sk %p", sk); |
| 477 | |
| 478 | lock_sock(sk); |
| 479 | |
| 480 | switch (optname) { |
| 481 | case L2CAP_OPTIONS: |
| 482 | if (sk->sk_state == BT_CONNECTED) { |
| 483 | err = -EINVAL; |
| 484 | break; |
| 485 | } |
| 486 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 487 | opts.imtu = chan->imtu; |
| 488 | opts.omtu = chan->omtu; |
| 489 | opts.flush_to = chan->flush_to; |
| 490 | opts.mode = chan->mode; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 491 | opts.fcs = chan->fcs; |
| 492 | opts.max_tx = chan->max_tx; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 493 | opts.txwin_size = chan->tx_win; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 494 | |
| 495 | len = min_t(unsigned int, sizeof(opts), optlen); |
| 496 | if (copy_from_user((char *) &opts, optval, len)) { |
| 497 | err = -EFAULT; |
| 498 | break; |
| 499 | } |
| 500 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 501 | if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 502 | err = -EINVAL; |
| 503 | break; |
| 504 | } |
| 505 | |
Andre Guedes | 682877c | 2012-05-31 17:01:34 -0300 | [diff] [blame] | 506 | if (!l2cap_valid_mtu(chan, opts.imtu)) { |
| 507 | err = -EINVAL; |
| 508 | break; |
| 509 | } |
| 510 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 511 | chan->mode = opts.mode; |
| 512 | switch (chan->mode) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 513 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 514 | clear_bit(CONF_STATE2_DEVICE, &chan->conf_state); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 515 | break; |
| 516 | case L2CAP_MODE_ERTM: |
| 517 | case L2CAP_MODE_STREAMING: |
| 518 | if (!disable_ertm) |
| 519 | break; |
| 520 | /* fall through */ |
| 521 | default: |
| 522 | err = -EINVAL; |
| 523 | break; |
| 524 | } |
| 525 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 526 | chan->imtu = opts.imtu; |
| 527 | chan->omtu = opts.omtu; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 528 | chan->fcs = opts.fcs; |
| 529 | chan->max_tx = opts.max_tx; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 530 | chan->tx_win = opts.txwin_size; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 531 | break; |
| 532 | |
| 533 | case L2CAP_LM: |
| 534 | if (get_user(opt, (u32 __user *) optval)) { |
| 535 | err = -EFAULT; |
| 536 | break; |
| 537 | } |
| 538 | |
| 539 | if (opt & L2CAP_LM_AUTH) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 540 | chan->sec_level = BT_SECURITY_LOW; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 541 | if (opt & L2CAP_LM_ENCRYPT) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 542 | chan->sec_level = BT_SECURITY_MEDIUM; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 543 | if (opt & L2CAP_LM_SECURE) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 544 | chan->sec_level = BT_SECURITY_HIGH; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 545 | |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 546 | if (opt & L2CAP_LM_MASTER) |
| 547 | set_bit(FLAG_ROLE_SWITCH, &chan->flags); |
| 548 | else |
| 549 | clear_bit(FLAG_ROLE_SWITCH, &chan->flags); |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 550 | |
| 551 | if (opt & L2CAP_LM_RELIABLE) |
| 552 | set_bit(FLAG_FORCE_RELIABLE, &chan->flags); |
| 553 | else |
| 554 | clear_bit(FLAG_FORCE_RELIABLE, &chan->flags); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 555 | break; |
| 556 | |
| 557 | default: |
| 558 | err = -ENOPROTOOPT; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | release_sock(sk); |
| 563 | return err; |
| 564 | } |
| 565 | |
| 566 | static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
| 567 | { |
| 568 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 569 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 570 | struct bt_security sec; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 571 | struct bt_power pwr; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 572 | struct l2cap_conn *conn; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 573 | int len, err = 0; |
| 574 | u32 opt; |
| 575 | |
| 576 | BT_DBG("sk %p", sk); |
| 577 | |
| 578 | if (level == SOL_L2CAP) |
| 579 | return l2cap_sock_setsockopt_old(sock, optname, optval, optlen); |
| 580 | |
| 581 | if (level != SOL_BLUETOOTH) |
| 582 | return -ENOPROTOOPT; |
| 583 | |
| 584 | lock_sock(sk); |
| 585 | |
| 586 | switch (optname) { |
| 587 | case BT_SECURITY: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 588 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && |
| 589 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 590 | err = -EINVAL; |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | sec.level = BT_SECURITY_LOW; |
| 595 | |
| 596 | len = min_t(unsigned int, sizeof(sec), optlen); |
| 597 | if (copy_from_user((char *) &sec, optval, len)) { |
| 598 | err = -EFAULT; |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | if (sec.level < BT_SECURITY_LOW || |
| 603 | sec.level > BT_SECURITY_HIGH) { |
| 604 | err = -EINVAL; |
| 605 | break; |
| 606 | } |
| 607 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 608 | chan->sec_level = sec.level; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 609 | |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 610 | if (!chan->conn) |
| 611 | break; |
| 612 | |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 613 | conn = chan->conn; |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 614 | |
| 615 | /*change security for LE channels */ |
| 616 | if (chan->scid == L2CAP_CID_LE_DATA) { |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 617 | if (!conn->hcon->out) { |
| 618 | err = -EINVAL; |
| 619 | break; |
| 620 | } |
| 621 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 622 | if (smp_conn_security(conn->hcon, sec.level)) |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 623 | break; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 624 | sk->sk_state = BT_CONFIG; |
Gustavo F. Padovan | 3542b854 | 2011-12-28 13:54:17 -0200 | [diff] [blame] | 625 | chan->state = BT_CONFIG; |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 626 | |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 627 | /* or for ACL link */ |
| 628 | } else if ((sk->sk_state == BT_CONNECT2 && |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 629 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 630 | sk->sk_state == BT_CONNECTED) { |
| 631 | if (!l2cap_chan_check_security(chan)) |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 632 | set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 633 | else |
| 634 | sk->sk_state_change(sk); |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 635 | } else { |
| 636 | err = -EINVAL; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 637 | } |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 638 | break; |
| 639 | |
| 640 | case BT_DEFER_SETUP: |
| 641 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 642 | err = -EINVAL; |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | if (get_user(opt, (u32 __user *) optval)) { |
| 647 | err = -EFAULT; |
| 648 | break; |
| 649 | } |
| 650 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 651 | if (opt) |
| 652 | set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 653 | else |
| 654 | clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 655 | break; |
| 656 | |
| 657 | case BT_FLUSHABLE: |
| 658 | if (get_user(opt, (u32 __user *) optval)) { |
| 659 | err = -EFAULT; |
| 660 | break; |
| 661 | } |
| 662 | |
| 663 | if (opt > BT_FLUSHABLE_ON) { |
| 664 | err = -EINVAL; |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | if (opt == BT_FLUSHABLE_OFF) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 669 | struct l2cap_conn *conn = chan->conn; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 670 | /* proceed further only when we have l2cap_conn and |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 671 | No Flush support in the LM */ |
| 672 | if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) { |
| 673 | err = -EINVAL; |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | |
Andrei Emeltchenko | d57b0e8 | 2011-10-11 14:04:31 +0300 | [diff] [blame] | 678 | if (opt) |
| 679 | set_bit(FLAG_FLUSHABLE, &chan->flags); |
| 680 | else |
| 681 | clear_bit(FLAG_FLUSHABLE, &chan->flags); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 682 | break; |
| 683 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 684 | case BT_POWER: |
| 685 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && |
| 686 | chan->chan_type != L2CAP_CHAN_RAW) { |
| 687 | err = -EINVAL; |
| 688 | break; |
| 689 | } |
| 690 | |
| 691 | pwr.force_active = BT_POWER_FORCE_ACTIVE_ON; |
| 692 | |
| 693 | len = min_t(unsigned int, sizeof(pwr), optlen); |
| 694 | if (copy_from_user((char *) &pwr, optval, len)) { |
| 695 | err = -EFAULT; |
| 696 | break; |
| 697 | } |
Andrei Emeltchenko | 15770b1 | 2011-10-11 14:04:33 +0300 | [diff] [blame] | 698 | |
| 699 | if (pwr.force_active) |
| 700 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 701 | else |
| 702 | clear_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 703 | break; |
| 704 | |
Mat Martineau | 2ea6648 | 2011-11-02 16:18:30 -0700 | [diff] [blame] | 705 | case BT_CHANNEL_POLICY: |
| 706 | if (!enable_hs) { |
| 707 | err = -ENOPROTOOPT; |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | if (get_user(opt, (u32 __user *) optval)) { |
| 712 | err = -EFAULT; |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) { |
| 717 | err = -EINVAL; |
| 718 | break; |
| 719 | } |
| 720 | |
| 721 | if (chan->mode != L2CAP_MODE_ERTM && |
| 722 | chan->mode != L2CAP_MODE_STREAMING) { |
| 723 | err = -EOPNOTSUPP; |
| 724 | break; |
| 725 | } |
| 726 | |
| 727 | chan->chan_policy = (u8) opt; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 728 | break; |
| 729 | |
| 730 | default: |
| 731 | err = -ENOPROTOOPT; |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | release_sock(sk); |
| 736 | return err; |
| 737 | } |
| 738 | |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 739 | static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len) |
| 740 | { |
| 741 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 742 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 743 | int err; |
| 744 | |
| 745 | BT_DBG("sock %p, sk %p", sock, sk); |
| 746 | |
| 747 | err = sock_error(sk); |
| 748 | if (err) |
| 749 | return err; |
| 750 | |
| 751 | if (msg->msg_flags & MSG_OOB) |
| 752 | return -EOPNOTSUPP; |
| 753 | |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 754 | if (sk->sk_state != BT_CONNECTED) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 755 | return -ENOTCONN; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 756 | |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 757 | l2cap_chan_lock(chan); |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 758 | err = l2cap_chan_send(chan, msg, len, sk->sk_priority); |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 759 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 760 | |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 761 | return err; |
| 762 | } |
| 763 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 764 | static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags) |
| 765 | { |
| 766 | struct sock *sk = sock->sk; |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 767 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
| 768 | int err; |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 769 | |
| 770 | lock_sock(sk); |
| 771 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 772 | if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP, |
| 773 | &bt_sk(sk)->flags)) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 774 | sk->sk_state = BT_CONFIG; |
Gustavo F. Padovan | 3542b854 | 2011-12-28 13:54:17 -0200 | [diff] [blame] | 775 | pi->chan->state = BT_CONFIG; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 776 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 777 | __l2cap_connect_rsp_defer(pi->chan); |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 778 | release_sock(sk); |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | release_sock(sk); |
| 783 | |
| 784 | if (sock->type == SOCK_STREAM) |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 785 | err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags); |
| 786 | else |
| 787 | err = bt_sock_recvmsg(iocb, sock, msg, len, flags); |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 788 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 789 | if (pi->chan->mode != L2CAP_MODE_ERTM) |
| 790 | return err; |
| 791 | |
| 792 | /* Attempt to put pending rx data in the socket buffer */ |
| 793 | |
| 794 | lock_sock(sk); |
| 795 | |
| 796 | if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state)) |
| 797 | goto done; |
| 798 | |
| 799 | if (pi->rx_busy_skb) { |
| 800 | if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb)) |
| 801 | pi->rx_busy_skb = NULL; |
| 802 | else |
| 803 | goto done; |
| 804 | } |
| 805 | |
| 806 | /* Restore data flow when half of the receive buffer is |
| 807 | * available. This avoids resending large numbers of |
| 808 | * frames. |
| 809 | */ |
| 810 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1) |
| 811 | l2cap_chan_busy(pi->chan, 0); |
| 812 | |
| 813 | done: |
| 814 | release_sock(sk); |
| 815 | return err; |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 816 | } |
| 817 | |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 818 | /* Kill socket (only if zapped and orphan) |
| 819 | * Must be called on unlocked socket. |
| 820 | */ |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 821 | static void l2cap_sock_kill(struct sock *sk) |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 822 | { |
| 823 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 824 | return; |
| 825 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 826 | BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state)); |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 827 | |
| 828 | /* Kill poor orphan */ |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 829 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 830 | l2cap_chan_put(l2cap_pi(sk)->chan); |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 831 | sock_set_flag(sk, SOCK_DEAD); |
| 832 | sock_put(sk); |
| 833 | } |
| 834 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 835 | static int l2cap_sock_shutdown(struct socket *sock, int how) |
| 836 | { |
| 837 | struct sock *sk = sock->sk; |
Andrei Emeltchenko | 7ddb6e0 | 2012-02-14 15:12:57 +0200 | [diff] [blame] | 838 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 839 | struct l2cap_conn *conn; |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 840 | int err = 0; |
| 841 | |
| 842 | BT_DBG("sock %p, sk %p", sock, sk); |
| 843 | |
| 844 | if (!sk) |
| 845 | return 0; |
| 846 | |
Andrei Emeltchenko | 7ddb6e0 | 2012-02-14 15:12:57 +0200 | [diff] [blame] | 847 | chan = l2cap_pi(sk)->chan; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 848 | conn = chan->conn; |
| 849 | |
| 850 | if (conn) |
| 851 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 7ddb6e0 | 2012-02-14 15:12:57 +0200 | [diff] [blame] | 852 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 853 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 854 | lock_sock(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 855 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 856 | if (!sk->sk_shutdown) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 857 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 858 | err = __l2cap_wait_ack(sk); |
| 859 | |
| 860 | sk->sk_shutdown = SHUTDOWN_MASK; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 861 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 862 | release_sock(sk); |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 863 | l2cap_chan_close(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 864 | lock_sock(sk); |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 865 | |
| 866 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 867 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 868 | sk->sk_lingertime); |
| 869 | } |
| 870 | |
| 871 | if (!err && sk->sk_err) |
| 872 | err = -sk->sk_err; |
| 873 | |
| 874 | release_sock(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 875 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 876 | |
| 877 | if (conn) |
| 878 | mutex_unlock(&conn->chan_lock); |
| 879 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 880 | return err; |
| 881 | } |
| 882 | |
Gustavo F. Padovan | 554f05b | 2011-02-04 02:36:42 -0200 | [diff] [blame] | 883 | static int l2cap_sock_release(struct socket *sock) |
| 884 | { |
| 885 | struct sock *sk = sock->sk; |
| 886 | int err; |
| 887 | |
| 888 | BT_DBG("sock %p, sk %p", sock, sk); |
| 889 | |
| 890 | if (!sk) |
| 891 | return 0; |
| 892 | |
Masatake YAMATO | 5b28d95 | 2012-07-26 01:29:25 +0900 | [diff] [blame] | 893 | bt_sock_unlink(&l2cap_sk_list, sk); |
| 894 | |
Gustavo F. Padovan | 554f05b | 2011-02-04 02:36:42 -0200 | [diff] [blame] | 895 | err = l2cap_sock_shutdown(sock, 2); |
| 896 | |
| 897 | sock_orphan(sk); |
| 898 | l2cap_sock_kill(sk); |
| 899 | return err; |
| 900 | } |
| 901 | |
Andrei Emeltchenko | c0df7f6 | 2012-05-27 22:27:52 -0300 | [diff] [blame] | 902 | static void l2cap_sock_cleanup_listen(struct sock *parent) |
| 903 | { |
| 904 | struct sock *sk; |
| 905 | |
| 906 | BT_DBG("parent %p", parent); |
| 907 | |
| 908 | /* Close not yet accepted channels */ |
| 909 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
| 910 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
| 911 | |
| 912 | l2cap_chan_lock(chan); |
| 913 | __clear_chan_timer(chan); |
| 914 | l2cap_chan_close(chan, ECONNRESET); |
| 915 | l2cap_chan_unlock(chan); |
| 916 | |
| 917 | l2cap_sock_kill(sk); |
| 918 | } |
| 919 | } |
| 920 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 921 | static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan) |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 922 | { |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 923 | struct sock *sk, *parent = chan->data; |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 924 | |
Gustavo Padovan | 5382669 | 2012-05-27 22:27:55 -0300 | [diff] [blame] | 925 | /* Check for backlog size */ |
| 926 | if (sk_acceptq_is_full(parent)) { |
| 927 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
| 928 | return NULL; |
| 929 | } |
| 930 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 931 | sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, |
| 932 | GFP_ATOMIC); |
| 933 | if (!sk) |
| 934 | return NULL; |
| 935 | |
Octavian Purdila | d22015a | 2012-01-22 00:28:34 +0200 | [diff] [blame] | 936 | bt_sock_reclassify_lock(sk, BTPROTO_L2CAP); |
| 937 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 938 | l2cap_sock_init(sk, parent); |
| 939 | |
| 940 | return l2cap_pi(sk)->chan; |
| 941 | } |
| 942 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 943 | static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 944 | { |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 945 | int err; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 946 | struct sock *sk = chan->data; |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 947 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 948 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 949 | lock_sock(sk); |
| 950 | |
| 951 | if (pi->rx_busy_skb) { |
| 952 | err = -ENOMEM; |
| 953 | goto done; |
| 954 | } |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 955 | |
| 956 | err = sock_queue_rcv_skb(sk, skb); |
| 957 | |
| 958 | /* For ERTM, handle one skb that doesn't fit into the recv |
| 959 | * buffer. This is important to do because the data frames |
| 960 | * have already been acked, so the skb cannot be discarded. |
| 961 | * |
| 962 | * Notify the l2cap core that the buffer is full, so the |
| 963 | * LOCAL_BUSY state is entered and no more frames are |
| 964 | * acked and reassembled until there is buffer space |
| 965 | * available. |
| 966 | */ |
| 967 | if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) { |
| 968 | pi->rx_busy_skb = skb; |
| 969 | l2cap_chan_busy(pi->chan, 1); |
| 970 | err = 0; |
| 971 | } |
| 972 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 973 | done: |
| 974 | release_sock(sk); |
| 975 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 976 | return err; |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 977 | } |
| 978 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 979 | static void l2cap_sock_close_cb(struct l2cap_chan *chan) |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 980 | { |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 981 | struct sock *sk = chan->data; |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 982 | |
| 983 | l2cap_sock_kill(sk); |
| 984 | } |
| 985 | |
Andrei Emeltchenko | c0df7f6 | 2012-05-27 22:27:52 -0300 | [diff] [blame] | 986 | static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) |
| 987 | { |
| 988 | struct sock *sk = chan->data; |
| 989 | struct sock *parent; |
| 990 | |
| 991 | lock_sock(sk); |
| 992 | |
| 993 | parent = bt_sk(sk)->parent; |
| 994 | |
| 995 | sock_set_flag(sk, SOCK_ZAPPED); |
| 996 | |
| 997 | switch (chan->state) { |
| 998 | case BT_OPEN: |
| 999 | case BT_BOUND: |
| 1000 | case BT_CLOSED: |
| 1001 | break; |
| 1002 | case BT_LISTEN: |
| 1003 | l2cap_sock_cleanup_listen(sk); |
| 1004 | sk->sk_state = BT_CLOSED; |
| 1005 | chan->state = BT_CLOSED; |
| 1006 | |
| 1007 | break; |
| 1008 | default: |
| 1009 | sk->sk_state = BT_CLOSED; |
| 1010 | chan->state = BT_CLOSED; |
| 1011 | |
| 1012 | sk->sk_err = err; |
| 1013 | |
| 1014 | if (parent) { |
| 1015 | bt_accept_unlink(sk); |
| 1016 | parent->sk_data_ready(parent, 0); |
| 1017 | } else { |
| 1018 | sk->sk_state_change(sk); |
| 1019 | } |
| 1020 | |
| 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | release_sock(sk); |
| 1025 | } |
| 1026 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1027 | static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1028 | { |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1029 | struct sock *sk = chan->data; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1030 | |
| 1031 | sk->sk_state = state; |
| 1032 | } |
| 1033 | |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1034 | static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 1035 | unsigned long len, int nb) |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1036 | { |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 1037 | struct sk_buff *skb; |
| 1038 | int err; |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1039 | |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 1040 | l2cap_chan_unlock(chan); |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 1041 | skb = bt_skb_send_alloc(chan->sk, len, nb, &err); |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 1042 | l2cap_chan_lock(chan); |
| 1043 | |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 1044 | if (!skb) |
| 1045 | return ERR_PTR(err); |
| 1046 | |
| 1047 | return skb; |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1050 | static void l2cap_sock_ready_cb(struct l2cap_chan *chan) |
| 1051 | { |
| 1052 | struct sock *sk = chan->data; |
| 1053 | struct sock *parent; |
| 1054 | |
| 1055 | lock_sock(sk); |
| 1056 | |
| 1057 | parent = bt_sk(sk)->parent; |
| 1058 | |
| 1059 | BT_DBG("sk %p, parent %p", sk, parent); |
| 1060 | |
| 1061 | sk->sk_state = BT_CONNECTED; |
| 1062 | sk->sk_state_change(sk); |
| 1063 | |
| 1064 | if (parent) |
| 1065 | parent->sk_data_ready(parent, 0); |
| 1066 | |
| 1067 | release_sock(sk); |
| 1068 | } |
| 1069 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1070 | static struct l2cap_ops l2cap_chan_ops = { |
| 1071 | .name = "L2CAP Socket Interface", |
| 1072 | .new_connection = l2cap_sock_new_connection_cb, |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 1073 | .recv = l2cap_sock_recv_cb, |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 1074 | .close = l2cap_sock_close_cb, |
Andrei Emeltchenko | c0df7f6 | 2012-05-27 22:27:52 -0300 | [diff] [blame] | 1075 | .teardown = l2cap_sock_teardown_cb, |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1076 | .state_change = l2cap_sock_state_change_cb, |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1077 | .ready = l2cap_sock_ready_cb, |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1078 | .alloc_skb = l2cap_sock_alloc_skb_cb, |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1079 | }; |
| 1080 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1081 | static void l2cap_sock_destruct(struct sock *sk) |
| 1082 | { |
| 1083 | BT_DBG("sk %p", sk); |
| 1084 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1085 | l2cap_chan_put(l2cap_pi(sk)->chan); |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 1086 | if (l2cap_pi(sk)->rx_busy_skb) { |
| 1087 | kfree_skb(l2cap_pi(sk)->rx_busy_skb); |
| 1088 | l2cap_pi(sk)->rx_busy_skb = NULL; |
| 1089 | } |
| 1090 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1091 | skb_queue_purge(&sk->sk_receive_queue); |
| 1092 | skb_queue_purge(&sk->sk_write_queue); |
| 1093 | } |
| 1094 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1095 | static void l2cap_sock_init(struct sock *sk, struct sock *parent) |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1096 | { |
| 1097 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1098 | struct l2cap_chan *chan = pi->chan; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1099 | |
| 1100 | BT_DBG("sk %p", sk); |
| 1101 | |
| 1102 | if (parent) { |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1103 | struct l2cap_chan *pchan = l2cap_pi(parent)->chan; |
| 1104 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1105 | sk->sk_type = parent->sk_type; |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 1106 | bt_sk(sk)->flags = bt_sk(parent)->flags; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1107 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1108 | chan->chan_type = pchan->chan_type; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1109 | chan->imtu = pchan->imtu; |
| 1110 | chan->omtu = pchan->omtu; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1111 | chan->conf_state = pchan->conf_state; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1112 | chan->mode = pchan->mode; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1113 | chan->fcs = pchan->fcs; |
| 1114 | chan->max_tx = pchan->max_tx; |
| 1115 | chan->tx_win = pchan->tx_win; |
Andrei Emeltchenko | 6b3c710 | 2011-11-02 09:57:10 +0200 | [diff] [blame] | 1116 | chan->tx_win_max = pchan->tx_win_max; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1117 | chan->sec_level = pchan->sec_level; |
Andrei Emeltchenko | d57b0e8 | 2011-10-11 14:04:31 +0300 | [diff] [blame] | 1118 | chan->flags = pchan->flags; |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 1119 | |
| 1120 | security_sk_clone(parent, sk); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1121 | } else { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1122 | |
| 1123 | switch (sk->sk_type) { |
| 1124 | case SOCK_RAW: |
| 1125 | chan->chan_type = L2CAP_CHAN_RAW; |
| 1126 | break; |
| 1127 | case SOCK_DGRAM: |
| 1128 | chan->chan_type = L2CAP_CHAN_CONN_LESS; |
| 1129 | break; |
| 1130 | case SOCK_SEQPACKET: |
| 1131 | case SOCK_STREAM: |
| 1132 | chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; |
| 1133 | break; |
| 1134 | } |
| 1135 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1136 | chan->imtu = L2CAP_DEFAULT_MTU; |
| 1137 | chan->omtu = 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1138 | if (!disable_ertm && sk->sk_type == SOCK_STREAM) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1139 | chan->mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1140 | set_bit(CONF_STATE2_DEVICE, &chan->conf_state); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1141 | } else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1142 | chan->mode = L2CAP_MODE_BASIC; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1143 | } |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 1144 | |
| 1145 | l2cap_chan_set_defaults(chan); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /* Default config options */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1149 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1150 | |
| 1151 | chan->data = sk; |
| 1152 | chan->ops = &l2cap_chan_ops; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | static struct proto l2cap_proto = { |
| 1156 | .name = "L2CAP", |
| 1157 | .owner = THIS_MODULE, |
| 1158 | .obj_size = sizeof(struct l2cap_pinfo) |
| 1159 | }; |
| 1160 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1161 | static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio) |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1162 | { |
| 1163 | struct sock *sk; |
Gustavo F. Padovan | dc50a06 | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1164 | struct l2cap_chan *chan; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1165 | |
| 1166 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto); |
| 1167 | if (!sk) |
| 1168 | return NULL; |
| 1169 | |
| 1170 | sock_init_data(sock, sk); |
| 1171 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 1172 | |
| 1173 | sk->sk_destruct = l2cap_sock_destruct; |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 1174 | sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1175 | |
| 1176 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1177 | |
| 1178 | sk->sk_protocol = proto; |
| 1179 | sk->sk_state = BT_OPEN; |
| 1180 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 1181 | chan = l2cap_chan_create(); |
Gustavo F. Padovan | dc50a06 | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1182 | if (!chan) { |
Jaganath Kanakkassery | 49dfbb9 | 2012-07-19 12:54:04 +0530 | [diff] [blame] | 1183 | sk_free(sk); |
Gustavo F. Padovan | dc50a06 | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1184 | return NULL; |
| 1185 | } |
| 1186 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1187 | l2cap_chan_hold(chan); |
| 1188 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 1189 | chan->sk = sk; |
| 1190 | |
Gustavo F. Padovan | dc50a06 | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1191 | l2cap_pi(sk)->chan = chan; |
| 1192 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1193 | return sk; |
| 1194 | } |
| 1195 | |
| 1196 | static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, |
| 1197 | int kern) |
| 1198 | { |
| 1199 | struct sock *sk; |
| 1200 | |
| 1201 | BT_DBG("sock %p", sock); |
| 1202 | |
| 1203 | sock->state = SS_UNCONNECTED; |
| 1204 | |
| 1205 | if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM && |
| 1206 | sock->type != SOCK_DGRAM && sock->type != SOCK_RAW) |
| 1207 | return -ESOCKTNOSUPPORT; |
| 1208 | |
| 1209 | if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) |
| 1210 | return -EPERM; |
| 1211 | |
| 1212 | sock->ops = &l2cap_sock_ops; |
| 1213 | |
| 1214 | sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
| 1215 | if (!sk) |
| 1216 | return -ENOMEM; |
| 1217 | |
| 1218 | l2cap_sock_init(sk, NULL); |
Masatake YAMATO | 5b28d95 | 2012-07-26 01:29:25 +0900 | [diff] [blame] | 1219 | bt_sock_link(&l2cap_sk_list, sk); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1220 | return 0; |
| 1221 | } |
| 1222 | |
Gustavo F. Padovan | cf2f90f | 2011-04-27 18:40:39 -0300 | [diff] [blame] | 1223 | static const struct proto_ops l2cap_sock_ops = { |
Gustavo F. Padovan | 6539058 | 2011-02-04 02:33:56 -0200 | [diff] [blame] | 1224 | .family = PF_BLUETOOTH, |
| 1225 | .owner = THIS_MODULE, |
| 1226 | .release = l2cap_sock_release, |
| 1227 | .bind = l2cap_sock_bind, |
| 1228 | .connect = l2cap_sock_connect, |
| 1229 | .listen = l2cap_sock_listen, |
| 1230 | .accept = l2cap_sock_accept, |
| 1231 | .getname = l2cap_sock_getname, |
| 1232 | .sendmsg = l2cap_sock_sendmsg, |
| 1233 | .recvmsg = l2cap_sock_recvmsg, |
| 1234 | .poll = bt_sock_poll, |
| 1235 | .ioctl = bt_sock_ioctl, |
| 1236 | .mmap = sock_no_mmap, |
| 1237 | .socketpair = sock_no_socketpair, |
| 1238 | .shutdown = l2cap_sock_shutdown, |
| 1239 | .setsockopt = l2cap_sock_setsockopt, |
| 1240 | .getsockopt = l2cap_sock_getsockopt |
| 1241 | }; |
| 1242 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1243 | static const struct net_proto_family l2cap_sock_family_ops = { |
| 1244 | .family = PF_BLUETOOTH, |
| 1245 | .owner = THIS_MODULE, |
| 1246 | .create = l2cap_sock_create, |
| 1247 | }; |
| 1248 | |
| 1249 | int __init l2cap_init_sockets(void) |
| 1250 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1251 | int err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1252 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1253 | err = proto_register(&l2cap_proto, 0); |
| 1254 | if (err < 0) |
| 1255 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1256 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1257 | err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops); |
Masatake YAMATO | 5b28d95 | 2012-07-26 01:29:25 +0900 | [diff] [blame] | 1258 | if (err < 0) { |
| 1259 | BT_ERR("L2CAP socket registration failed"); |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1260 | goto error; |
Masatake YAMATO | 5b28d95 | 2012-07-26 01:29:25 +0900 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | err = bt_procfs_init(THIS_MODULE, &init_net, "l2cap", &l2cap_sk_list, NULL); |
| 1264 | if (err < 0) { |
| 1265 | BT_ERR("Failed to create L2CAP proc file"); |
| 1266 | bt_sock_unregister(BTPROTO_L2CAP); |
| 1267 | goto error; |
| 1268 | } |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1269 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1270 | BT_INFO("L2CAP socket layer initialized"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1271 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1272 | return 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1273 | |
| 1274 | error: |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1275 | proto_unregister(&l2cap_proto); |
| 1276 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | void l2cap_cleanup_sockets(void) |
| 1280 | { |
Masatake YAMATO | 5b28d95 | 2012-07-26 01:29:25 +0900 | [diff] [blame] | 1281 | bt_procfs_cleanup(&init_net, "l2cap"); |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1282 | if (bt_sock_unregister(BTPROTO_L2CAP) < 0) |
| 1283 | BT_ERR("L2CAP socket unregistration failed"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1284 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1285 | proto_unregister(&l2cap_proto); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1286 | } |