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 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 448 | static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen) |
| 449 | { |
| 450 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 451 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 452 | struct l2cap_options opts; |
| 453 | int len, err = 0; |
| 454 | u32 opt; |
| 455 | |
| 456 | BT_DBG("sk %p", sk); |
| 457 | |
| 458 | lock_sock(sk); |
| 459 | |
| 460 | switch (optname) { |
| 461 | case L2CAP_OPTIONS: |
| 462 | if (sk->sk_state == BT_CONNECTED) { |
| 463 | err = -EINVAL; |
| 464 | break; |
| 465 | } |
| 466 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 467 | opts.imtu = chan->imtu; |
| 468 | opts.omtu = chan->omtu; |
| 469 | opts.flush_to = chan->flush_to; |
| 470 | opts.mode = chan->mode; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 471 | opts.fcs = chan->fcs; |
| 472 | opts.max_tx = chan->max_tx; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 473 | opts.txwin_size = chan->tx_win; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 474 | |
| 475 | len = min_t(unsigned int, sizeof(opts), optlen); |
| 476 | if (copy_from_user((char *) &opts, optval, len)) { |
| 477 | err = -EFAULT; |
| 478 | break; |
| 479 | } |
| 480 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 481 | if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 482 | err = -EINVAL; |
| 483 | break; |
| 484 | } |
| 485 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 486 | chan->mode = opts.mode; |
| 487 | switch (chan->mode) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 488 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 489 | clear_bit(CONF_STATE2_DEVICE, &chan->conf_state); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 490 | break; |
| 491 | case L2CAP_MODE_ERTM: |
| 492 | case L2CAP_MODE_STREAMING: |
| 493 | if (!disable_ertm) |
| 494 | break; |
| 495 | /* fall through */ |
| 496 | default: |
| 497 | err = -EINVAL; |
| 498 | break; |
| 499 | } |
| 500 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 501 | chan->imtu = opts.imtu; |
| 502 | chan->omtu = opts.omtu; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 503 | chan->fcs = opts.fcs; |
| 504 | chan->max_tx = opts.max_tx; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 505 | chan->tx_win = opts.txwin_size; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 506 | break; |
| 507 | |
| 508 | case L2CAP_LM: |
| 509 | if (get_user(opt, (u32 __user *) optval)) { |
| 510 | err = -EFAULT; |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | if (opt & L2CAP_LM_AUTH) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 515 | chan->sec_level = BT_SECURITY_LOW; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 516 | if (opt & L2CAP_LM_ENCRYPT) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 517 | chan->sec_level = BT_SECURITY_MEDIUM; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 518 | if (opt & L2CAP_LM_SECURE) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 519 | chan->sec_level = BT_SECURITY_HIGH; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 520 | |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 521 | if (opt & L2CAP_LM_MASTER) |
| 522 | set_bit(FLAG_ROLE_SWITCH, &chan->flags); |
| 523 | else |
| 524 | clear_bit(FLAG_ROLE_SWITCH, &chan->flags); |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 525 | |
| 526 | if (opt & L2CAP_LM_RELIABLE) |
| 527 | set_bit(FLAG_FORCE_RELIABLE, &chan->flags); |
| 528 | else |
| 529 | clear_bit(FLAG_FORCE_RELIABLE, &chan->flags); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 530 | break; |
| 531 | |
| 532 | default: |
| 533 | err = -ENOPROTOOPT; |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | release_sock(sk); |
| 538 | return err; |
| 539 | } |
| 540 | |
| 541 | static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
| 542 | { |
| 543 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 544 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 545 | struct bt_security sec; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 546 | struct bt_power pwr; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 547 | struct l2cap_conn *conn; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 548 | int len, err = 0; |
| 549 | u32 opt; |
| 550 | |
| 551 | BT_DBG("sk %p", sk); |
| 552 | |
| 553 | if (level == SOL_L2CAP) |
| 554 | return l2cap_sock_setsockopt_old(sock, optname, optval, optlen); |
| 555 | |
| 556 | if (level != SOL_BLUETOOTH) |
| 557 | return -ENOPROTOOPT; |
| 558 | |
| 559 | lock_sock(sk); |
| 560 | |
| 561 | switch (optname) { |
| 562 | case BT_SECURITY: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 563 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && |
| 564 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 565 | err = -EINVAL; |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | sec.level = BT_SECURITY_LOW; |
| 570 | |
| 571 | len = min_t(unsigned int, sizeof(sec), optlen); |
| 572 | if (copy_from_user((char *) &sec, optval, len)) { |
| 573 | err = -EFAULT; |
| 574 | break; |
| 575 | } |
| 576 | |
| 577 | if (sec.level < BT_SECURITY_LOW || |
| 578 | sec.level > BT_SECURITY_HIGH) { |
| 579 | err = -EINVAL; |
| 580 | break; |
| 581 | } |
| 582 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 583 | chan->sec_level = sec.level; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 584 | |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 585 | if (!chan->conn) |
| 586 | break; |
| 587 | |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 588 | conn = chan->conn; |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 589 | |
| 590 | /*change security for LE channels */ |
| 591 | if (chan->scid == L2CAP_CID_LE_DATA) { |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 592 | if (!conn->hcon->out) { |
| 593 | err = -EINVAL; |
| 594 | break; |
| 595 | } |
| 596 | |
| 597 | if (smp_conn_security(conn, sec.level)) |
| 598 | break; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 599 | sk->sk_state = BT_CONFIG; |
Gustavo F. Padovan | 3542b854 | 2011-12-28 13:54:17 -0200 | [diff] [blame] | 600 | chan->state = BT_CONFIG; |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 601 | |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 602 | /* or for ACL link */ |
| 603 | } else if ((sk->sk_state == BT_CONNECT2 && |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 604 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 605 | sk->sk_state == BT_CONNECTED) { |
| 606 | if (!l2cap_chan_check_security(chan)) |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 607 | set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 608 | else |
| 609 | sk->sk_state_change(sk); |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 610 | } else { |
| 611 | err = -EINVAL; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 612 | } |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 613 | break; |
| 614 | |
| 615 | case BT_DEFER_SETUP: |
| 616 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 617 | err = -EINVAL; |
| 618 | break; |
| 619 | } |
| 620 | |
| 621 | if (get_user(opt, (u32 __user *) optval)) { |
| 622 | err = -EFAULT; |
| 623 | break; |
| 624 | } |
| 625 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 626 | if (opt) |
| 627 | set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 628 | else |
| 629 | clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 630 | break; |
| 631 | |
| 632 | case BT_FLUSHABLE: |
| 633 | if (get_user(opt, (u32 __user *) optval)) { |
| 634 | err = -EFAULT; |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | if (opt > BT_FLUSHABLE_ON) { |
| 639 | err = -EINVAL; |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | if (opt == BT_FLUSHABLE_OFF) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 644 | struct l2cap_conn *conn = chan->conn; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 645 | /* proceed further only when we have l2cap_conn and |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 646 | No Flush support in the LM */ |
| 647 | if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) { |
| 648 | err = -EINVAL; |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
Andrei Emeltchenko | d57b0e8 | 2011-10-11 14:04:31 +0300 | [diff] [blame] | 653 | if (opt) |
| 654 | set_bit(FLAG_FLUSHABLE, &chan->flags); |
| 655 | else |
| 656 | clear_bit(FLAG_FLUSHABLE, &chan->flags); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 657 | break; |
| 658 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 659 | case BT_POWER: |
| 660 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && |
| 661 | chan->chan_type != L2CAP_CHAN_RAW) { |
| 662 | err = -EINVAL; |
| 663 | break; |
| 664 | } |
| 665 | |
| 666 | pwr.force_active = BT_POWER_FORCE_ACTIVE_ON; |
| 667 | |
| 668 | len = min_t(unsigned int, sizeof(pwr), optlen); |
| 669 | if (copy_from_user((char *) &pwr, optval, len)) { |
| 670 | err = -EFAULT; |
| 671 | break; |
| 672 | } |
Andrei Emeltchenko | 15770b1 | 2011-10-11 14:04:33 +0300 | [diff] [blame] | 673 | |
| 674 | if (pwr.force_active) |
| 675 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 676 | else |
| 677 | clear_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 678 | break; |
| 679 | |
Mat Martineau | 2ea6648 | 2011-11-02 16:18:30 -0700 | [diff] [blame] | 680 | case BT_CHANNEL_POLICY: |
| 681 | if (!enable_hs) { |
| 682 | err = -ENOPROTOOPT; |
| 683 | break; |
| 684 | } |
| 685 | |
| 686 | if (get_user(opt, (u32 __user *) optval)) { |
| 687 | err = -EFAULT; |
| 688 | break; |
| 689 | } |
| 690 | |
| 691 | if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) { |
| 692 | err = -EINVAL; |
| 693 | break; |
| 694 | } |
| 695 | |
| 696 | if (chan->mode != L2CAP_MODE_ERTM && |
| 697 | chan->mode != L2CAP_MODE_STREAMING) { |
| 698 | err = -EOPNOTSUPP; |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | chan->chan_policy = (u8) opt; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 703 | break; |
| 704 | |
| 705 | default: |
| 706 | err = -ENOPROTOOPT; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | release_sock(sk); |
| 711 | return err; |
| 712 | } |
| 713 | |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 714 | static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len) |
| 715 | { |
| 716 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 717 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 718 | int err; |
| 719 | |
| 720 | BT_DBG("sock %p, sk %p", sock, sk); |
| 721 | |
| 722 | err = sock_error(sk); |
| 723 | if (err) |
| 724 | return err; |
| 725 | |
| 726 | if (msg->msg_flags & MSG_OOB) |
| 727 | return -EOPNOTSUPP; |
| 728 | |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 729 | if (sk->sk_state != BT_CONNECTED) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 730 | return -ENOTCONN; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 731 | |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 732 | l2cap_chan_lock(chan); |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 733 | err = l2cap_chan_send(chan, msg, len, sk->sk_priority); |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 734 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 735 | |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 736 | return err; |
| 737 | } |
| 738 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 739 | static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags) |
| 740 | { |
| 741 | struct sock *sk = sock->sk; |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 742 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
| 743 | int err; |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 744 | |
| 745 | lock_sock(sk); |
| 746 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 747 | if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP, |
| 748 | &bt_sk(sk)->flags)) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 749 | sk->sk_state = BT_CONFIG; |
Gustavo F. Padovan | 3542b854 | 2011-12-28 13:54:17 -0200 | [diff] [blame] | 750 | pi->chan->state = BT_CONFIG; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 751 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 752 | __l2cap_connect_rsp_defer(pi->chan); |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 753 | release_sock(sk); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | release_sock(sk); |
| 758 | |
| 759 | if (sock->type == SOCK_STREAM) |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 760 | err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags); |
| 761 | else |
| 762 | err = bt_sock_recvmsg(iocb, sock, msg, len, flags); |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 763 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 764 | if (pi->chan->mode != L2CAP_MODE_ERTM) |
| 765 | return err; |
| 766 | |
| 767 | /* Attempt to put pending rx data in the socket buffer */ |
| 768 | |
| 769 | lock_sock(sk); |
| 770 | |
| 771 | if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state)) |
| 772 | goto done; |
| 773 | |
| 774 | if (pi->rx_busy_skb) { |
| 775 | if (!sock_queue_rcv_skb(sk, pi->rx_busy_skb)) |
| 776 | pi->rx_busy_skb = NULL; |
| 777 | else |
| 778 | goto done; |
| 779 | } |
| 780 | |
| 781 | /* Restore data flow when half of the receive buffer is |
| 782 | * available. This avoids resending large numbers of |
| 783 | * frames. |
| 784 | */ |
| 785 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1) |
| 786 | l2cap_chan_busy(pi->chan, 0); |
| 787 | |
| 788 | done: |
| 789 | release_sock(sk); |
| 790 | return err; |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 791 | } |
| 792 | |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 793 | /* Kill socket (only if zapped and orphan) |
| 794 | * Must be called on unlocked socket. |
| 795 | */ |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 796 | static void l2cap_sock_kill(struct sock *sk) |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 797 | { |
| 798 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 799 | return; |
| 800 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 801 | 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] | 802 | |
| 803 | /* Kill poor orphan */ |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 804 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 805 | l2cap_chan_destroy(l2cap_pi(sk)->chan); |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 806 | sock_set_flag(sk, SOCK_DEAD); |
| 807 | sock_put(sk); |
| 808 | } |
| 809 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 810 | static int l2cap_sock_shutdown(struct socket *sock, int how) |
| 811 | { |
| 812 | struct sock *sk = sock->sk; |
Andrei Emeltchenko | 7ddb6e0 | 2012-02-14 15:12:57 +0200 | [diff] [blame] | 813 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 814 | struct l2cap_conn *conn; |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 815 | int err = 0; |
| 816 | |
| 817 | BT_DBG("sock %p, sk %p", sock, sk); |
| 818 | |
| 819 | if (!sk) |
| 820 | return 0; |
| 821 | |
Andrei Emeltchenko | 7ddb6e0 | 2012-02-14 15:12:57 +0200 | [diff] [blame] | 822 | chan = l2cap_pi(sk)->chan; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 823 | conn = chan->conn; |
| 824 | |
| 825 | if (conn) |
| 826 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 7ddb6e0 | 2012-02-14 15:12:57 +0200 | [diff] [blame] | 827 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 828 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 829 | lock_sock(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 830 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 831 | if (!sk->sk_shutdown) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 832 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 833 | err = __l2cap_wait_ack(sk); |
| 834 | |
| 835 | sk->sk_shutdown = SHUTDOWN_MASK; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 836 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 837 | release_sock(sk); |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 838 | l2cap_chan_close(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 839 | lock_sock(sk); |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 840 | |
| 841 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 842 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 843 | sk->sk_lingertime); |
| 844 | } |
| 845 | |
| 846 | if (!err && sk->sk_err) |
| 847 | err = -sk->sk_err; |
| 848 | |
| 849 | release_sock(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 850 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 851 | |
| 852 | if (conn) |
| 853 | mutex_unlock(&conn->chan_lock); |
| 854 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 855 | return err; |
| 856 | } |
| 857 | |
Gustavo F. Padovan | 554f05b | 2011-02-04 02:36:42 -0200 | [diff] [blame] | 858 | static int l2cap_sock_release(struct socket *sock) |
| 859 | { |
| 860 | struct sock *sk = sock->sk; |
| 861 | int err; |
| 862 | |
| 863 | BT_DBG("sock %p, sk %p", sock, sk); |
| 864 | |
| 865 | if (!sk) |
| 866 | return 0; |
| 867 | |
| 868 | err = l2cap_sock_shutdown(sock, 2); |
| 869 | |
| 870 | sock_orphan(sk); |
| 871 | l2cap_sock_kill(sk); |
| 872 | return err; |
| 873 | } |
| 874 | |
Andrei Emeltchenko | c0df7f6 | 2012-05-27 22:27:52 -0300 | [diff] [blame] | 875 | static void l2cap_sock_cleanup_listen(struct sock *parent) |
| 876 | { |
| 877 | struct sock *sk; |
| 878 | |
| 879 | BT_DBG("parent %p", parent); |
| 880 | |
| 881 | /* Close not yet accepted channels */ |
| 882 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
| 883 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
| 884 | |
| 885 | l2cap_chan_lock(chan); |
| 886 | __clear_chan_timer(chan); |
| 887 | l2cap_chan_close(chan, ECONNRESET); |
| 888 | l2cap_chan_unlock(chan); |
| 889 | |
| 890 | l2cap_sock_kill(sk); |
| 891 | } |
| 892 | } |
| 893 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 894 | 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] | 895 | { |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 896 | struct sock *sk, *parent = chan->data; |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 897 | |
| 898 | sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, |
| 899 | GFP_ATOMIC); |
| 900 | if (!sk) |
| 901 | return NULL; |
| 902 | |
Octavian Purdila | d22015a | 2012-01-22 00:28:34 +0200 | [diff] [blame] | 903 | bt_sock_reclassify_lock(sk, BTPROTO_L2CAP); |
| 904 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 905 | l2cap_sock_init(sk, parent); |
| 906 | |
| 907 | return l2cap_pi(sk)->chan; |
| 908 | } |
| 909 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 910 | 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] | 911 | { |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 912 | int err; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 913 | struct sock *sk = chan->data; |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 914 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 915 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 916 | lock_sock(sk); |
| 917 | |
| 918 | if (pi->rx_busy_skb) { |
| 919 | err = -ENOMEM; |
| 920 | goto done; |
| 921 | } |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 922 | |
| 923 | err = sock_queue_rcv_skb(sk, skb); |
| 924 | |
| 925 | /* For ERTM, handle one skb that doesn't fit into the recv |
| 926 | * buffer. This is important to do because the data frames |
| 927 | * have already been acked, so the skb cannot be discarded. |
| 928 | * |
| 929 | * Notify the l2cap core that the buffer is full, so the |
| 930 | * LOCAL_BUSY state is entered and no more frames are |
| 931 | * acked and reassembled until there is buffer space |
| 932 | * available. |
| 933 | */ |
| 934 | if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) { |
| 935 | pi->rx_busy_skb = skb; |
| 936 | l2cap_chan_busy(pi->chan, 1); |
| 937 | err = 0; |
| 938 | } |
| 939 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 940 | done: |
| 941 | release_sock(sk); |
| 942 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 943 | return err; |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 944 | } |
| 945 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 946 | static void l2cap_sock_close_cb(struct l2cap_chan *chan) |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 947 | { |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 948 | struct sock *sk = chan->data; |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 949 | |
| 950 | l2cap_sock_kill(sk); |
| 951 | } |
| 952 | |
Andrei Emeltchenko | c0df7f6 | 2012-05-27 22:27:52 -0300 | [diff] [blame] | 953 | static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) |
| 954 | { |
| 955 | struct sock *sk = chan->data; |
| 956 | struct sock *parent; |
| 957 | |
| 958 | lock_sock(sk); |
| 959 | |
| 960 | parent = bt_sk(sk)->parent; |
| 961 | |
| 962 | sock_set_flag(sk, SOCK_ZAPPED); |
| 963 | |
| 964 | switch (chan->state) { |
| 965 | case BT_OPEN: |
| 966 | case BT_BOUND: |
| 967 | case BT_CLOSED: |
| 968 | break; |
| 969 | case BT_LISTEN: |
| 970 | l2cap_sock_cleanup_listen(sk); |
| 971 | sk->sk_state = BT_CLOSED; |
| 972 | chan->state = BT_CLOSED; |
| 973 | |
| 974 | break; |
| 975 | default: |
| 976 | sk->sk_state = BT_CLOSED; |
| 977 | chan->state = BT_CLOSED; |
| 978 | |
| 979 | sk->sk_err = err; |
| 980 | |
| 981 | if (parent) { |
| 982 | bt_accept_unlink(sk); |
| 983 | parent->sk_data_ready(parent, 0); |
| 984 | } else { |
| 985 | sk->sk_state_change(sk); |
| 986 | } |
| 987 | |
| 988 | break; |
| 989 | } |
| 990 | |
| 991 | release_sock(sk); |
| 992 | } |
| 993 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 994 | 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] | 995 | { |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 996 | struct sock *sk = chan->data; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 997 | |
| 998 | sk->sk_state = state; |
| 999 | } |
| 1000 | |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1001 | 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] | 1002 | unsigned long len, int nb) |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1003 | { |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 1004 | struct sk_buff *skb; |
| 1005 | int err; |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1006 | |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 1007 | l2cap_chan_unlock(chan); |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 1008 | skb = bt_skb_send_alloc(chan->sk, len, nb, &err); |
Mat Martineau | a6a5568 | 2012-05-04 14:20:31 -0700 | [diff] [blame] | 1009 | l2cap_chan_lock(chan); |
| 1010 | |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 1011 | if (!skb) |
| 1012 | return ERR_PTR(err); |
| 1013 | |
| 1014 | return skb; |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame^] | 1017 | static void l2cap_sock_ready_cb(struct l2cap_chan *chan) |
| 1018 | { |
| 1019 | struct sock *sk = chan->data; |
| 1020 | struct sock *parent; |
| 1021 | |
| 1022 | lock_sock(sk); |
| 1023 | |
| 1024 | parent = bt_sk(sk)->parent; |
| 1025 | |
| 1026 | BT_DBG("sk %p, parent %p", sk, parent); |
| 1027 | |
| 1028 | sk->sk_state = BT_CONNECTED; |
| 1029 | sk->sk_state_change(sk); |
| 1030 | |
| 1031 | if (parent) |
| 1032 | parent->sk_data_ready(parent, 0); |
| 1033 | |
| 1034 | release_sock(sk); |
| 1035 | } |
| 1036 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1037 | static struct l2cap_ops l2cap_chan_ops = { |
| 1038 | .name = "L2CAP Socket Interface", |
| 1039 | .new_connection = l2cap_sock_new_connection_cb, |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 1040 | .recv = l2cap_sock_recv_cb, |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 1041 | .close = l2cap_sock_close_cb, |
Andrei Emeltchenko | c0df7f6 | 2012-05-27 22:27:52 -0300 | [diff] [blame] | 1042 | .teardown = l2cap_sock_teardown_cb, |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1043 | .state_change = l2cap_sock_state_change_cb, |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame^] | 1044 | .ready = l2cap_sock_ready_cb, |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1045 | .alloc_skb = l2cap_sock_alloc_skb_cb, |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1046 | }; |
| 1047 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1048 | static void l2cap_sock_destruct(struct sock *sk) |
| 1049 | { |
| 1050 | BT_DBG("sk %p", sk); |
| 1051 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1052 | l2cap_chan_put(l2cap_pi(sk)->chan); |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 1053 | if (l2cap_pi(sk)->rx_busy_skb) { |
| 1054 | kfree_skb(l2cap_pi(sk)->rx_busy_skb); |
| 1055 | l2cap_pi(sk)->rx_busy_skb = NULL; |
| 1056 | } |
| 1057 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1058 | skb_queue_purge(&sk->sk_receive_queue); |
| 1059 | skb_queue_purge(&sk->sk_write_queue); |
| 1060 | } |
| 1061 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1062 | static void l2cap_sock_init(struct sock *sk, struct sock *parent) |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1063 | { |
| 1064 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1065 | struct l2cap_chan *chan = pi->chan; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1066 | |
| 1067 | BT_DBG("sk %p", sk); |
| 1068 | |
| 1069 | if (parent) { |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1070 | struct l2cap_chan *pchan = l2cap_pi(parent)->chan; |
| 1071 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1072 | sk->sk_type = parent->sk_type; |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 1073 | bt_sk(sk)->flags = bt_sk(parent)->flags; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1074 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1075 | chan->chan_type = pchan->chan_type; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1076 | chan->imtu = pchan->imtu; |
| 1077 | chan->omtu = pchan->omtu; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1078 | chan->conf_state = pchan->conf_state; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1079 | chan->mode = pchan->mode; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1080 | chan->fcs = pchan->fcs; |
| 1081 | chan->max_tx = pchan->max_tx; |
| 1082 | chan->tx_win = pchan->tx_win; |
Andrei Emeltchenko | 6b3c710 | 2011-11-02 09:57:10 +0200 | [diff] [blame] | 1083 | chan->tx_win_max = pchan->tx_win_max; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1084 | chan->sec_level = pchan->sec_level; |
Andrei Emeltchenko | d57b0e8 | 2011-10-11 14:04:31 +0300 | [diff] [blame] | 1085 | chan->flags = pchan->flags; |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 1086 | |
| 1087 | security_sk_clone(parent, sk); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1088 | } else { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1089 | |
| 1090 | switch (sk->sk_type) { |
| 1091 | case SOCK_RAW: |
| 1092 | chan->chan_type = L2CAP_CHAN_RAW; |
| 1093 | break; |
| 1094 | case SOCK_DGRAM: |
| 1095 | chan->chan_type = L2CAP_CHAN_CONN_LESS; |
| 1096 | break; |
| 1097 | case SOCK_SEQPACKET: |
| 1098 | case SOCK_STREAM: |
| 1099 | chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; |
| 1100 | break; |
| 1101 | } |
| 1102 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1103 | chan->imtu = L2CAP_DEFAULT_MTU; |
| 1104 | chan->omtu = 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1105 | if (!disable_ertm && sk->sk_type == SOCK_STREAM) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1106 | chan->mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1107 | set_bit(CONF_STATE2_DEVICE, &chan->conf_state); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1108 | } else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1109 | chan->mode = L2CAP_MODE_BASIC; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1110 | } |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 1111 | |
| 1112 | l2cap_chan_set_defaults(chan); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /* Default config options */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1116 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1117 | |
| 1118 | chan->data = sk; |
| 1119 | chan->ops = &l2cap_chan_ops; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | static struct proto l2cap_proto = { |
| 1123 | .name = "L2CAP", |
| 1124 | .owner = THIS_MODULE, |
| 1125 | .obj_size = sizeof(struct l2cap_pinfo) |
| 1126 | }; |
| 1127 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1128 | 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] | 1129 | { |
| 1130 | struct sock *sk; |
Gustavo F. Padovan | dc50a06 | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1131 | struct l2cap_chan *chan; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1132 | |
| 1133 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto); |
| 1134 | if (!sk) |
| 1135 | return NULL; |
| 1136 | |
| 1137 | sock_init_data(sock, sk); |
| 1138 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 1139 | |
| 1140 | sk->sk_destruct = l2cap_sock_destruct; |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 1141 | sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1142 | |
| 1143 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1144 | |
| 1145 | sk->sk_protocol = proto; |
| 1146 | sk->sk_state = BT_OPEN; |
| 1147 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 1148 | chan = l2cap_chan_create(); |
Gustavo F. Padovan | dc50a06 | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1149 | if (!chan) { |
| 1150 | l2cap_sock_kill(sk); |
| 1151 | return NULL; |
| 1152 | } |
| 1153 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1154 | l2cap_chan_hold(chan); |
| 1155 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 1156 | chan->sk = sk; |
| 1157 | |
Gustavo F. Padovan | dc50a06 | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1158 | l2cap_pi(sk)->chan = chan; |
| 1159 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1160 | return sk; |
| 1161 | } |
| 1162 | |
| 1163 | static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, |
| 1164 | int kern) |
| 1165 | { |
| 1166 | struct sock *sk; |
| 1167 | |
| 1168 | BT_DBG("sock %p", sock); |
| 1169 | |
| 1170 | sock->state = SS_UNCONNECTED; |
| 1171 | |
| 1172 | if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM && |
| 1173 | sock->type != SOCK_DGRAM && sock->type != SOCK_RAW) |
| 1174 | return -ESOCKTNOSUPPORT; |
| 1175 | |
| 1176 | if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) |
| 1177 | return -EPERM; |
| 1178 | |
| 1179 | sock->ops = &l2cap_sock_ops; |
| 1180 | |
| 1181 | sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
| 1182 | if (!sk) |
| 1183 | return -ENOMEM; |
| 1184 | |
| 1185 | l2cap_sock_init(sk, NULL); |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Gustavo F. Padovan | cf2f90f | 2011-04-27 18:40:39 -0300 | [diff] [blame] | 1189 | static const struct proto_ops l2cap_sock_ops = { |
Gustavo F. Padovan | 6539058 | 2011-02-04 02:33:56 -0200 | [diff] [blame] | 1190 | .family = PF_BLUETOOTH, |
| 1191 | .owner = THIS_MODULE, |
| 1192 | .release = l2cap_sock_release, |
| 1193 | .bind = l2cap_sock_bind, |
| 1194 | .connect = l2cap_sock_connect, |
| 1195 | .listen = l2cap_sock_listen, |
| 1196 | .accept = l2cap_sock_accept, |
| 1197 | .getname = l2cap_sock_getname, |
| 1198 | .sendmsg = l2cap_sock_sendmsg, |
| 1199 | .recvmsg = l2cap_sock_recvmsg, |
| 1200 | .poll = bt_sock_poll, |
| 1201 | .ioctl = bt_sock_ioctl, |
| 1202 | .mmap = sock_no_mmap, |
| 1203 | .socketpair = sock_no_socketpair, |
| 1204 | .shutdown = l2cap_sock_shutdown, |
| 1205 | .setsockopt = l2cap_sock_setsockopt, |
| 1206 | .getsockopt = l2cap_sock_getsockopt |
| 1207 | }; |
| 1208 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1209 | static const struct net_proto_family l2cap_sock_family_ops = { |
| 1210 | .family = PF_BLUETOOTH, |
| 1211 | .owner = THIS_MODULE, |
| 1212 | .create = l2cap_sock_create, |
| 1213 | }; |
| 1214 | |
| 1215 | int __init l2cap_init_sockets(void) |
| 1216 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1217 | int err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1218 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1219 | err = proto_register(&l2cap_proto, 0); |
| 1220 | if (err < 0) |
| 1221 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1222 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1223 | err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops); |
| 1224 | if (err < 0) |
| 1225 | goto error; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1226 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1227 | BT_INFO("L2CAP socket layer initialized"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1228 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1229 | return 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1230 | |
| 1231 | error: |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1232 | BT_ERR("L2CAP socket registration failed"); |
| 1233 | proto_unregister(&l2cap_proto); |
| 1234 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | void l2cap_cleanup_sockets(void) |
| 1238 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1239 | if (bt_sock_unregister(BTPROTO_L2CAP) < 0) |
| 1240 | BT_ERR("L2CAP socket unregistration failed"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1241 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1242 | proto_unregister(&l2cap_proto); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1243 | } |