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