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