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