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