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