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