Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Mat Martineau | 966dcfa | 2011-12-12 10:45:31 -0800 | [diff] [blame] | 3 | Copyright (c) 2000-2001, 2011-2012 Code Aurora Forum. All rights reserved. |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4 | Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> |
| 5 | Copyright (C) 2010 Google Inc. |
| 6 | |
| 7 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License version 2 as |
| 11 | published by the Free Software Foundation; |
| 12 | |
| 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 16 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 17 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 18 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 19 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 20 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | |
| 22 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 23 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 24 | SOFTWARE IS DISCLAIMED. |
| 25 | */ |
| 26 | |
| 27 | /* Bluetooth L2CAP sockets. */ |
| 28 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/module.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> |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 35 | #include <net/bluetooth/smp.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 36 | #include <net/bluetooth/amp.h> |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 37 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 38 | /* ---- L2CAP timers ---- */ |
| 39 | static void l2cap_sock_timeout(unsigned long arg) |
| 40 | { |
| 41 | struct sock *sk = (struct sock *) arg; |
| 42 | int reason; |
| 43 | |
| 44 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 45 | |
| 46 | bh_lock_sock(sk); |
| 47 | |
| 48 | if (sock_owned_by_user(sk)) { |
| 49 | /* sk is owned by user. Try again later */ |
| 50 | l2cap_sock_set_timer(sk, HZ / 5); |
| 51 | bh_unlock_sock(sk); |
| 52 | sock_put(sk); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG) |
| 57 | reason = ECONNREFUSED; |
| 58 | else if (sk->sk_state == BT_CONNECT && |
| 59 | l2cap_pi(sk)->sec_level != BT_SECURITY_SDP) |
| 60 | reason = ECONNREFUSED; |
| 61 | else |
| 62 | reason = ETIMEDOUT; |
| 63 | |
| 64 | __l2cap_sock_close(sk, reason); |
| 65 | |
| 66 | bh_unlock_sock(sk); |
| 67 | |
| 68 | l2cap_sock_kill(sk); |
| 69 | sock_put(sk); |
| 70 | } |
| 71 | |
| 72 | void l2cap_sock_set_timer(struct sock *sk, long timeout) |
| 73 | { |
| 74 | BT_DBG("sk %p state %d timeout %ld", sk, sk->sk_state, timeout); |
| 75 | sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout); |
| 76 | } |
| 77 | |
| 78 | void l2cap_sock_clear_timer(struct sock *sk) |
| 79 | { |
| 80 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 81 | sk_stop_timer(sk, &sk->sk_timer); |
| 82 | } |
| 83 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 84 | int l2cap_sock_le_params_valid(struct bt_le_params *le_params) |
| 85 | { |
| 86 | if (!le_params || le_params->latency > BT_LE_LATENCY_MAX || |
| 87 | le_params->scan_window > BT_LE_SCAN_WINDOW_MAX || |
| 88 | le_params->scan_interval < BT_LE_SCAN_INTERVAL_MIN || |
| 89 | le_params->scan_window > le_params->scan_interval || |
| 90 | le_params->interval_min < BT_LE_CONN_INTERVAL_MIN || |
| 91 | le_params->interval_max > BT_LE_CONN_INTERVAL_MAX || |
| 92 | le_params->interval_min > le_params->interval_max || |
| 93 | le_params->supervision_timeout < BT_LE_SUP_TO_MIN || |
| 94 | le_params->supervision_timeout > BT_LE_SUP_TO_MAX) { |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | return 1; |
| 99 | } |
| 100 | |
Archana Ramachandran | 95319af | 2012-08-22 11:46:03 -0700 | [diff] [blame] | 101 | int l2cap_sock_le_conn_update_params_valid(struct bt_le_params *le_params) |
| 102 | { |
| 103 | if (!le_params || le_params->latency > BT_LE_LATENCY_MAX || |
| 104 | le_params->interval_min < BT_LE_CONN_INTERVAL_MIN || |
| 105 | le_params->interval_max > BT_LE_CONN_INTERVAL_MAX || |
| 106 | le_params->interval_min > le_params->interval_max || |
| 107 | le_params->supervision_timeout < BT_LE_SUP_TO_MIN || |
| 108 | le_params->supervision_timeout > BT_LE_SUP_TO_MAX) { |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | return 1; |
| 113 | } |
| 114 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 115 | static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src) |
| 116 | { |
| 117 | struct sock *sk; |
| 118 | struct hlist_node *node; |
| 119 | sk_for_each(sk, node, &l2cap_sk_list.head) |
| 120 | if (l2cap_pi(sk)->sport == psm && !bacmp(&bt_sk(sk)->src, src)) |
| 121 | goto found; |
| 122 | sk = NULL; |
| 123 | found: |
| 124 | return sk; |
| 125 | } |
Gustavo F. Padovan | cf2f90f | 2011-04-27 18:40:39 -0300 | [diff] [blame] | 126 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 127 | static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) |
| 128 | { |
| 129 | struct sock *sk = sock->sk; |
| 130 | struct sockaddr_l2 la; |
| 131 | int len, err = 0; |
| 132 | |
| 133 | BT_DBG("sk %p", sk); |
| 134 | |
| 135 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
| 136 | return -EINVAL; |
| 137 | |
| 138 | memset(&la, 0, sizeof(la)); |
| 139 | len = min_t(unsigned int, sizeof(la), alen); |
| 140 | memcpy(&la, addr, len); |
| 141 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 142 | if (la.l2_cid && la.l2_psm) |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 143 | return -EINVAL; |
| 144 | |
| 145 | lock_sock(sk); |
| 146 | |
| 147 | if (sk->sk_state != BT_OPEN) { |
| 148 | err = -EBADFD; |
| 149 | goto done; |
| 150 | } |
| 151 | |
| 152 | if (la.l2_psm) { |
| 153 | __u16 psm = __le16_to_cpu(la.l2_psm); |
| 154 | |
| 155 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 156 | if ((psm & 0x0101) != 0x0001) { |
| 157 | err = -EINVAL; |
| 158 | goto done; |
| 159 | } |
| 160 | |
| 161 | /* Restrict usage of well-known PSMs */ |
| 162 | if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) { |
| 163 | err = -EACCES; |
| 164 | goto done; |
| 165 | } |
| 166 | } |
| 167 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 168 | write_lock_bh(&l2cap_sk_list.lock); |
| 169 | |
| 170 | if (la.l2_psm && __l2cap_get_sock_by_addr(la.l2_psm, &la.l2_bdaddr)) { |
| 171 | err = -EADDRINUSE; |
| 172 | } else { |
| 173 | /* Save source address */ |
| 174 | bacpy(&bt_sk(sk)->src, &la.l2_bdaddr); |
| 175 | l2cap_pi(sk)->psm = la.l2_psm; |
| 176 | l2cap_pi(sk)->sport = la.l2_psm; |
| 177 | sk->sk_state = BT_BOUND; |
| 178 | |
| 179 | if (__le16_to_cpu(la.l2_psm) == 0x0001 || |
| 180 | __le16_to_cpu(la.l2_psm) == 0x0003) |
| 181 | l2cap_pi(sk)->sec_level = BT_SECURITY_SDP; |
| 182 | } |
| 183 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 184 | if (la.l2_cid) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 185 | l2cap_pi(sk)->scid = la.l2_cid; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 186 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 187 | write_unlock_bh(&l2cap_sk_list.lock); |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 188 | |
| 189 | done: |
| 190 | release_sock(sk); |
| 191 | return err; |
| 192 | } |
| 193 | |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 194 | static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 195 | { |
| 196 | struct sock *sk = sock->sk; |
| 197 | struct sockaddr_l2 la; |
| 198 | int len, err = 0; |
| 199 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 200 | BT_DBG("sk %p type %d mode %d state %d", sk, sk->sk_type, |
| 201 | l2cap_pi(sk)->mode, sk->sk_state); |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 202 | |
| 203 | if (!addr || alen < sizeof(addr->sa_family) || |
| 204 | addr->sa_family != AF_BLUETOOTH) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | memset(&la, 0, sizeof(la)); |
| 208 | len = min_t(unsigned int, sizeof(la), alen); |
| 209 | memcpy(&la, addr, len); |
| 210 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 211 | if (la.l2_cid && la.l2_psm) |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 212 | return -EINVAL; |
| 213 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 214 | lock_sock(sk); |
| 215 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 216 | if ((sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM) |
| 217 | && !(la.l2_psm || la.l2_cid || l2cap_pi(sk)->fixed_channel)) { |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 218 | err = -EINVAL; |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 219 | goto done; |
| 220 | } |
| 221 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 222 | switch (l2cap_pi(sk)->mode) { |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 223 | case L2CAP_MODE_BASIC: |
| 224 | break; |
| 225 | case L2CAP_MODE_ERTM: |
| 226 | case L2CAP_MODE_STREAMING: |
| 227 | if (!disable_ertm) |
| 228 | break; |
| 229 | /* fall through */ |
| 230 | default: |
| 231 | err = -ENOTSUPP; |
| 232 | goto done; |
| 233 | } |
| 234 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 235 | switch (sk->sk_state) { |
| 236 | case BT_CONNECT: |
| 237 | case BT_CONNECT2: |
| 238 | case BT_CONFIG: |
| 239 | /* Already connecting */ |
| 240 | goto wait; |
| 241 | |
| 242 | case BT_CONNECTED: |
| 243 | /* Already connected */ |
| 244 | err = -EISCONN; |
| 245 | goto done; |
| 246 | |
| 247 | case BT_OPEN: |
| 248 | case BT_BOUND: |
| 249 | /* Can connect */ |
| 250 | break; |
| 251 | |
| 252 | default: |
| 253 | err = -EBADFD; |
| 254 | goto done; |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* PSM must be odd and lsb of upper byte must be 0 */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 258 | if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 && |
| 259 | !l2cap_pi(sk)->fixed_channel && |
| 260 | sk->sk_type != SOCK_RAW && !la.l2_cid) { |
| 261 | BT_DBG("Bad PSM 0x%x", (int)__le16_to_cpu(la.l2_psm)); |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 262 | err = -EINVAL; |
| 263 | goto done; |
| 264 | } |
| 265 | |
| 266 | /* Set destination address and psm */ |
| 267 | bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 268 | l2cap_pi(sk)->psm = la.l2_psm; |
| 269 | l2cap_pi(sk)->dcid = la.l2_cid; |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 270 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 271 | err = l2cap_do_connect(sk); |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 272 | if (err) |
| 273 | goto done; |
| 274 | |
| 275 | wait: |
| 276 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
| 277 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
| 278 | done: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 279 | if (err) |
| 280 | BT_ERR("failed %d", err); |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 281 | release_sock(sk); |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | static int l2cap_sock_listen(struct socket *sock, int backlog) |
| 286 | { |
| 287 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 288 | int err = 0; |
| 289 | |
| 290 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 291 | |
| 292 | lock_sock(sk); |
| 293 | |
| 294 | if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) |
| 295 | || sk->sk_state != BT_BOUND) { |
| 296 | err = -EBADFD; |
| 297 | goto done; |
| 298 | } |
| 299 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 300 | switch (l2cap_pi(sk)->mode) { |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 301 | case L2CAP_MODE_BASIC: |
| 302 | break; |
| 303 | case L2CAP_MODE_ERTM: |
| 304 | case L2CAP_MODE_STREAMING: |
| 305 | if (!disable_ertm) |
| 306 | break; |
| 307 | /* fall through */ |
| 308 | default: |
| 309 | err = -ENOTSUPP; |
| 310 | goto done; |
| 311 | } |
| 312 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 313 | if (!l2cap_pi(sk)->psm && !l2cap_pi(sk)->scid) { |
| 314 | bdaddr_t *src = &bt_sk(sk)->src; |
| 315 | u16 psm; |
| 316 | |
| 317 | err = -EINVAL; |
| 318 | |
| 319 | write_lock_bh(&l2cap_sk_list.lock); |
| 320 | |
| 321 | for (psm = 0x1001; psm < 0x1100; psm += 2) |
| 322 | if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm), src)) { |
| 323 | l2cap_pi(sk)->psm = cpu_to_le16(psm); |
| 324 | l2cap_pi(sk)->sport = cpu_to_le16(psm); |
| 325 | err = 0; |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | write_unlock_bh(&l2cap_sk_list.lock); |
| 330 | |
| 331 | if (err < 0) |
| 332 | goto done; |
| 333 | } |
| 334 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 335 | sk->sk_max_ack_backlog = backlog; |
| 336 | sk->sk_ack_backlog = 0; |
| 337 | sk->sk_state = BT_LISTEN; |
| 338 | |
Gustavo F. Padovan | 6539058 | 2011-02-04 02:33:56 -0200 | [diff] [blame] | 339 | done: |
| 340 | release_sock(sk); |
| 341 | return err; |
| 342 | } |
| 343 | |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 344 | static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags) |
| 345 | { |
| 346 | DECLARE_WAITQUEUE(wait, current); |
| 347 | struct sock *sk = sock->sk, *nsk; |
| 348 | long timeo; |
| 349 | int err = 0; |
| 350 | |
| 351 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 352 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 353 | if (sk->sk_state != BT_LISTEN) { |
| 354 | err = -EBADFD; |
| 355 | goto done; |
| 356 | } |
| 357 | |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 358 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 359 | |
| 360 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 361 | |
| 362 | /* Wait for an incoming connection. (wake-one). */ |
| 363 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 364 | while (!(nsk = bt_accept_dequeue(sk, newsock))) { |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 365 | set_current_state(TASK_INTERRUPTIBLE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 366 | if (!timeo) { |
| 367 | err = -EAGAIN; |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 371 | release_sock(sk); |
| 372 | timeo = schedule_timeout(timeo); |
| 373 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Peter Hurley | f9a3c20 | 2011-07-24 00:10:52 -0400 | [diff] [blame] | 374 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 375 | if (sk->sk_state != BT_LISTEN) { |
| 376 | err = -EBADFD; |
Peter Hurley | f9a3c20 | 2011-07-24 00:10:52 -0400 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 380 | if (signal_pending(current)) { |
| 381 | err = sock_intr_errno(timeo); |
| 382 | break; |
| 383 | } |
| 384 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 385 | set_current_state(TASK_RUNNING); |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 386 | remove_wait_queue(sk_sleep(sk), &wait); |
| 387 | |
| 388 | if (err) |
| 389 | goto done; |
| 390 | |
| 391 | newsock->state = SS_CONNECTED; |
| 392 | |
| 393 | BT_DBG("new socket %p", nsk); |
| 394 | |
| 395 | done: |
| 396 | release_sock(sk); |
| 397 | return err; |
| 398 | } |
| 399 | |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 400 | static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer) |
| 401 | { |
| 402 | struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr; |
| 403 | struct sock *sk = sock->sk; |
| 404 | |
| 405 | BT_DBG("sock %p, sk %p", sock, sk); |
| 406 | |
| 407 | addr->sa_family = AF_BLUETOOTH; |
| 408 | *len = sizeof(struct sockaddr_l2); |
| 409 | |
| 410 | if (peer) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 411 | la->l2_psm = l2cap_pi(sk)->psm; |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 412 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 413 | la->l2_cid = cpu_to_le16(l2cap_pi(sk)->dcid); |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 414 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 415 | la->l2_psm = l2cap_pi(sk)->sport; |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 416 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->src); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 417 | la->l2_cid = cpu_to_le16(l2cap_pi(sk)->scid); |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 423 | static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) |
| 424 | { |
| 425 | struct sock *sk = sock->sk; |
| 426 | struct l2cap_options opts; |
| 427 | struct l2cap_conninfo cinfo; |
| 428 | int len, err = 0; |
| 429 | u32 opt; |
| 430 | |
| 431 | BT_DBG("sk %p", sk); |
| 432 | |
| 433 | if (get_user(len, optlen)) |
| 434 | return -EFAULT; |
| 435 | |
| 436 | lock_sock(sk); |
| 437 | |
| 438 | switch (optname) { |
| 439 | case L2CAP_OPTIONS: |
Vasiliy Kulikov | e3fb592 | 2011-02-10 20:59:42 +0300 | [diff] [blame] | 440 | memset(&opts, 0, sizeof(opts)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 441 | opts.imtu = l2cap_pi(sk)->imtu; |
| 442 | opts.omtu = l2cap_pi(sk)->omtu; |
| 443 | opts.flush_to = l2cap_pi(sk)->flush_to; |
| 444 | opts.mode = l2cap_pi(sk)->mode; |
| 445 | opts.fcs = l2cap_pi(sk)->fcs; |
| 446 | opts.max_tx = l2cap_pi(sk)->max_tx; |
| 447 | opts.txwin_size = l2cap_pi(sk)->tx_win; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 448 | |
| 449 | len = min_t(unsigned int, len, sizeof(opts)); |
| 450 | if (copy_to_user(optval, (char *) &opts, len)) |
| 451 | err = -EFAULT; |
| 452 | |
| 453 | break; |
| 454 | |
| 455 | case L2CAP_LM: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 456 | switch (l2cap_pi(sk)->sec_level) { |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 457 | case BT_SECURITY_LOW: |
| 458 | opt = L2CAP_LM_AUTH; |
| 459 | break; |
| 460 | case BT_SECURITY_MEDIUM: |
| 461 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT; |
| 462 | break; |
| 463 | case BT_SECURITY_HIGH: |
| 464 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | |
| 465 | L2CAP_LM_SECURE; |
| 466 | break; |
| 467 | default: |
| 468 | opt = 0; |
| 469 | break; |
| 470 | } |
| 471 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 472 | if (l2cap_pi(sk)->role_switch) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 473 | opt |= L2CAP_LM_MASTER; |
| 474 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 475 | if (l2cap_pi(sk)->force_reliable) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 476 | opt |= L2CAP_LM_RELIABLE; |
| 477 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 478 | if (l2cap_pi(sk)->flushable) |
| 479 | opt |= L2CAP_LM_FLUSHABLE; |
| 480 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 481 | if (put_user(opt, (u32 __user *) optval)) |
| 482 | err = -EFAULT; |
| 483 | break; |
| 484 | |
| 485 | case L2CAP_CONNINFO: |
| 486 | if (sk->sk_state != BT_CONNECTED && |
| 487 | !(sk->sk_state == BT_CONNECT2 && |
| 488 | bt_sk(sk)->defer_setup)) { |
| 489 | err = -ENOTCONN; |
| 490 | break; |
| 491 | } |
| 492 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 493 | cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle; |
| 494 | memcpy(cinfo.dev_class, l2cap_pi(sk)->conn->hcon->dev_class, 3); |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 495 | |
| 496 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 497 | if (copy_to_user(optval, (char *) &cinfo, len)) |
| 498 | err = -EFAULT; |
| 499 | |
| 500 | break; |
| 501 | |
| 502 | default: |
| 503 | err = -ENOPROTOOPT; |
| 504 | break; |
| 505 | } |
| 506 | |
| 507 | release_sock(sk); |
| 508 | return err; |
| 509 | } |
| 510 | |
| 511 | static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 512 | { |
| 513 | struct sock *sk = sock->sk; |
| 514 | struct bt_security sec; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 515 | struct bt_power pwr; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 516 | int len, err = 0; |
| 517 | |
| 518 | BT_DBG("sk %p", sk); |
| 519 | |
| 520 | if (level == SOL_L2CAP) |
| 521 | return l2cap_sock_getsockopt_old(sock, optname, optval, optlen); |
| 522 | |
| 523 | if (level != SOL_BLUETOOTH) |
| 524 | return -ENOPROTOOPT; |
| 525 | |
| 526 | if (get_user(len, optlen)) |
| 527 | return -EFAULT; |
| 528 | |
| 529 | lock_sock(sk); |
| 530 | |
| 531 | switch (optname) { |
| 532 | case BT_SECURITY: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 533 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 534 | && sk->sk_type != SOCK_RAW) { |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 535 | err = -EINVAL; |
| 536 | break; |
| 537 | } |
| 538 | |
Vinicius Costa Gomes | 8f36011 | 2011-07-08 18:31:46 -0300 | [diff] [blame] | 539 | memset(&sec, 0, sizeof(sec)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 540 | sec.level = l2cap_pi(sk)->sec_level; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 541 | |
Brian Gix | 27a795c | 2012-04-19 11:05:06 -0700 | [diff] [blame] | 542 | if (sk->sk_state == BT_CONNECTED) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 543 | sec.key_size = l2cap_pi(sk)->conn->hcon->enc_key_size; |
Brian Gix | 27a795c | 2012-04-19 11:05:06 -0700 | [diff] [blame] | 544 | sec.level = l2cap_pi(sk)->conn->hcon->sec_level; |
| 545 | } |
Vinicius Costa Gomes | 8f36011 | 2011-07-08 18:31:46 -0300 | [diff] [blame] | 546 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 547 | len = min_t(unsigned int, len, sizeof(sec)); |
| 548 | if (copy_to_user(optval, (char *) &sec, len)) |
| 549 | err = -EFAULT; |
| 550 | |
| 551 | break; |
| 552 | |
| 553 | case BT_DEFER_SETUP: |
| 554 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 555 | err = -EINVAL; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval)) |
| 560 | err = -EFAULT; |
| 561 | |
| 562 | break; |
| 563 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 564 | case BT_POWER: |
| 565 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 566 | && sk->sk_type != SOCK_RAW) { |
| 567 | err = -EINVAL; |
| 568 | break; |
| 569 | } |
| 570 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 571 | pwr.force_active = l2cap_pi(sk)->force_active; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 572 | |
| 573 | len = min_t(unsigned int, len, sizeof(pwr)); |
| 574 | if (copy_to_user(optval, (char *) &pwr, len)) |
| 575 | err = -EFAULT; |
| 576 | |
| 577 | break; |
| 578 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 579 | case BT_AMP_POLICY: |
| 580 | if (put_user(l2cap_pi(sk)->amp_pref, (u32 __user *) optval)) |
| 581 | err = -EFAULT; |
| 582 | break; |
| 583 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 584 | case BT_LE_PARAMS: |
| 585 | if (l2cap_pi(sk)->scid != L2CAP_CID_LE_DATA) { |
| 586 | err = -EINVAL; |
Mat Martineau | 2ea6648 | 2011-11-02 16:18:30 -0700 | [diff] [blame] | 587 | break; |
| 588 | } |
| 589 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 590 | if (copy_to_user(optval, (char *) &bt_sk(sk)->le_params, |
| 591 | sizeof(bt_sk(sk)->le_params))) |
Mat Martineau | 2ea6648 | 2011-11-02 16:18:30 -0700 | [diff] [blame] | 592 | err = -EFAULT; |
| 593 | break; |
| 594 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 595 | default: |
| 596 | err = -ENOPROTOOPT; |
| 597 | break; |
| 598 | } |
| 599 | |
| 600 | release_sock(sk); |
| 601 | return err; |
| 602 | } |
| 603 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 604 | static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen) |
| 605 | { |
| 606 | struct sock *sk = sock->sk; |
| 607 | struct l2cap_options opts; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 608 | int len, le_sock, err = 0; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 609 | u32 opt; |
| 610 | |
| 611 | BT_DBG("sk %p", sk); |
| 612 | |
| 613 | lock_sock(sk); |
| 614 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 615 | le_sock = l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA; |
| 616 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 617 | switch (optname) { |
| 618 | case L2CAP_OPTIONS: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 619 | if (sk->sk_state == BT_CONNECTED && !le_sock) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 620 | err = -EINVAL; |
| 621 | break; |
| 622 | } |
| 623 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 624 | opts.imtu = l2cap_pi(sk)->imtu; |
| 625 | opts.omtu = l2cap_pi(sk)->omtu; |
| 626 | opts.flush_to = l2cap_pi(sk)->flush_to; |
| 627 | opts.mode = l2cap_pi(sk)->mode; |
| 628 | opts.fcs = l2cap_pi(sk)->fcs; |
| 629 | opts.max_tx = l2cap_pi(sk)->max_tx; |
| 630 | opts.txwin_size = l2cap_pi(sk)->tx_win; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 631 | |
| 632 | len = min_t(unsigned int, sizeof(opts), optlen); |
| 633 | if (copy_from_user((char *) &opts, optval, len)) { |
| 634 | err = -EFAULT; |
| 635 | break; |
| 636 | } |
| 637 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 638 | if ((opts.imtu || opts.omtu) && le_sock && |
| 639 | (sk->sk_state == BT_CONNECTED)) { |
| 640 | if (opts.imtu >= L2CAP_LE_DEFAULT_MTU) |
| 641 | l2cap_pi(sk)->imtu = opts.imtu; |
| 642 | if (opts.omtu >= L2CAP_LE_DEFAULT_MTU) |
| 643 | l2cap_pi(sk)->omtu = opts.omtu; |
| 644 | if (opts.imtu < L2CAP_LE_DEFAULT_MTU || |
| 645 | opts.omtu < L2CAP_LE_DEFAULT_MTU) |
| 646 | err = -EINVAL; |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | if (opts.txwin_size < 1 || |
| 651 | opts.txwin_size > L2CAP_TX_WIN_MAX_EXTENDED) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 652 | err = -EINVAL; |
| 653 | break; |
| 654 | } |
| 655 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 656 | l2cap_pi(sk)->mode = opts.mode; |
| 657 | switch (l2cap_pi(sk)->mode) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 658 | case L2CAP_MODE_BASIC: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 659 | l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_STATE2_DEVICE; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 660 | break; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 661 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 0ac1436 | 2012-02-03 16:24:42 -0800 | [diff] [blame] | 662 | if (!disable_ertm) { |
| 663 | /* No fallback to ERTM or Basic mode */ |
| 664 | l2cap_pi(sk)->conf_state |= |
| 665 | L2CAP_CONF_STATE2_DEVICE; |
| 666 | break; |
| 667 | } |
| 668 | err = -EINVAL; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 669 | break; |
| 670 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 671 | if (!disable_ertm) |
| 672 | break; |
| 673 | /* fall through */ |
| 674 | default: |
| 675 | err = -EINVAL; |
| 676 | break; |
| 677 | } |
| 678 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 679 | l2cap_pi(sk)->imtu = opts.imtu; |
| 680 | l2cap_pi(sk)->omtu = opts.omtu; |
| 681 | l2cap_pi(sk)->fcs = opts.fcs; |
| 682 | l2cap_pi(sk)->max_tx = opts.max_tx; |
| 683 | l2cap_pi(sk)->tx_win = opts.txwin_size; |
Srinivas Krovvidi | 1073419 | 2011-12-29 07:29:11 +0530 | [diff] [blame] | 684 | l2cap_pi(sk)->flush_to = opts.flush_to; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 685 | break; |
| 686 | |
| 687 | case L2CAP_LM: |
| 688 | if (get_user(opt, (u32 __user *) optval)) { |
| 689 | err = -EFAULT; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | if (opt & L2CAP_LM_AUTH) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 694 | l2cap_pi(sk)->sec_level = BT_SECURITY_LOW; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 695 | if (opt & L2CAP_LM_ENCRYPT) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 696 | l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 697 | if (opt & L2CAP_LM_SECURE) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 698 | l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 699 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 700 | l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER); |
| 701 | l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE); |
| 702 | l2cap_pi(sk)->flushable = (opt & L2CAP_LM_FLUSHABLE); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 703 | break; |
| 704 | |
| 705 | default: |
| 706 | err = -ENOPROTOOPT; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | release_sock(sk); |
| 711 | return err; |
| 712 | } |
| 713 | |
| 714 | static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
| 715 | { |
| 716 | struct sock *sk = sock->sk; |
| 717 | struct bt_security sec; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 718 | struct bt_power pwr; |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 719 | struct bt_le_params le_params; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 720 | struct l2cap_conn *conn; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 721 | int len, err = 0; |
| 722 | u32 opt; |
| 723 | |
| 724 | BT_DBG("sk %p", sk); |
| 725 | |
| 726 | if (level == SOL_L2CAP) |
| 727 | return l2cap_sock_setsockopt_old(sock, optname, optval, optlen); |
| 728 | |
| 729 | if (level != SOL_BLUETOOTH) |
| 730 | return -ENOPROTOOPT; |
| 731 | |
| 732 | lock_sock(sk); |
| 733 | |
| 734 | switch (optname) { |
| 735 | case BT_SECURITY: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 736 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 737 | && sk->sk_type != SOCK_RAW) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 738 | err = -EINVAL; |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | sec.level = BT_SECURITY_LOW; |
| 743 | |
| 744 | len = min_t(unsigned int, sizeof(sec), optlen); |
| 745 | if (copy_from_user((char *) &sec, optval, len)) { |
| 746 | err = -EFAULT; |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | if (sec.level < BT_SECURITY_LOW || |
| 751 | sec.level > BT_SECURITY_HIGH) { |
| 752 | err = -EINVAL; |
| 753 | break; |
| 754 | } |
| 755 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 756 | l2cap_pi(sk)->sec_level = sec.level; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 757 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 758 | conn = l2cap_pi(sk)->conn; |
| 759 | if (conn && l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA) { |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 760 | if (!conn->hcon->out) { |
| 761 | err = -EINVAL; |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | if (smp_conn_security(conn, sec.level)) |
| 766 | break; |
Gustavo F. Padovan | 0bee1d6 | 2011-11-05 19:58:31 -0200 | [diff] [blame] | 767 | |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 768 | err = 0; |
| 769 | sk->sk_state = BT_CONFIG; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 770 | } |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 771 | break; |
| 772 | |
| 773 | case BT_DEFER_SETUP: |
| 774 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 775 | err = -EINVAL; |
| 776 | break; |
| 777 | } |
| 778 | |
| 779 | if (get_user(opt, (u32 __user *) optval)) { |
| 780 | err = -EFAULT; |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | bt_sk(sk)->defer_setup = opt; |
| 785 | break; |
| 786 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 787 | case BT_POWER: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 788 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 789 | && sk->sk_type != SOCK_RAW) { |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 790 | err = -EINVAL; |
| 791 | break; |
| 792 | } |
| 793 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 794 | pwr.force_active = 1; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 795 | |
| 796 | len = min_t(unsigned int, sizeof(pwr), optlen); |
| 797 | if (copy_from_user((char *) &pwr, optval, len)) { |
| 798 | err = -EFAULT; |
| 799 | break; |
| 800 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 801 | l2cap_pi(sk)->force_active = pwr.force_active; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 802 | break; |
| 803 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 804 | case BT_AMP_POLICY: |
Mat Martineau | 2ea6648 | 2011-11-02 16:18:30 -0700 | [diff] [blame] | 805 | if (get_user(opt, (u32 __user *) optval)) { |
| 806 | err = -EFAULT; |
| 807 | break; |
| 808 | } |
| 809 | |
Mat Martineau | f00ffac | 2011-10-19 15:07:32 -0700 | [diff] [blame] | 810 | if ((opt > BT_AMP_POLICY_PREFER_AMP) || |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 811 | ((l2cap_pi(sk)->mode != L2CAP_MODE_ERTM) && |
| 812 | (l2cap_pi(sk)->mode != L2CAP_MODE_STREAMING))) { |
Mat Martineau | 2ea6648 | 2011-11-02 16:18:30 -0700 | [diff] [blame] | 813 | err = -EINVAL; |
| 814 | break; |
| 815 | } |
| 816 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 817 | l2cap_pi(sk)->amp_pref = (u8) opt; |
| 818 | BT_DBG("BT_AMP_POLICY now %d", opt); |
| 819 | |
| 820 | if ((sk->sk_state == BT_CONNECTED) && |
Peter Krystad | c446d21 | 2011-09-20 15:35:50 -0700 | [diff] [blame] | 821 | (l2cap_pi(sk)->amp_move_role == L2CAP_AMP_MOVE_NONE)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 822 | l2cap_amp_move_init(sk); |
| 823 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 824 | break; |
| 825 | |
Srinivas Krovvidi | 1073419 | 2011-12-29 07:29:11 +0530 | [diff] [blame] | 826 | case BT_FLUSHABLE: |
| 827 | if (get_user(opt, (u32 __user *) optval)) { |
| 828 | err = -EFAULT; |
| 829 | break; |
| 830 | } |
| 831 | l2cap_pi(sk)->flushable = opt; |
| 832 | |
| 833 | break; |
| 834 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 835 | case BT_LE_PARAMS: |
| 836 | if (l2cap_pi(sk)->scid != L2CAP_CID_LE_DATA) { |
| 837 | err = -EINVAL; |
Mat Martineau | 2ea6648 | 2011-11-02 16:18:30 -0700 | [diff] [blame] | 838 | break; |
| 839 | } |
| 840 | |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 841 | if (copy_from_user((char *) &le_params, optval, |
| 842 | sizeof(struct bt_le_params))) { |
| 843 | err = -EFAULT; |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | conn = l2cap_pi(sk)->conn; |
| 848 | if (!conn || !conn->hcon || |
| 849 | l2cap_pi(sk)->scid != L2CAP_CID_LE_DATA) { |
| 850 | memcpy(&bt_sk(sk)->le_params, &le_params, |
| 851 | sizeof(le_params)); |
| 852 | break; |
| 853 | } |
| 854 | |
| 855 | if (!conn->hcon->out || |
Archana Ramachandran | 95319af | 2012-08-22 11:46:03 -0700 | [diff] [blame] | 856 | !l2cap_sock_le_conn_update_params_valid( |
| 857 | &le_params)) { |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 858 | err = -EINVAL; |
| 859 | break; |
| 860 | } |
| 861 | |
| 862 | memcpy(&bt_sk(sk)->le_params, &le_params, sizeof(le_params)); |
| 863 | |
| 864 | hci_le_conn_update(conn->hcon, |
| 865 | le_params.interval_min, |
| 866 | le_params.interval_max, |
| 867 | le_params.latency, |
| 868 | le_params.supervision_timeout); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 869 | break; |
| 870 | |
| 871 | default: |
| 872 | err = -ENOPROTOOPT; |
| 873 | break; |
| 874 | } |
| 875 | |
| 876 | release_sock(sk); |
| 877 | return err; |
| 878 | } |
| 879 | |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 880 | static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len) |
| 881 | { |
| 882 | struct sock *sk = sock->sk; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 883 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
| 884 | struct sk_buff *skb; |
| 885 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 886 | int err; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 887 | u8 amp_id; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 888 | |
| 889 | BT_DBG("sock %p, sk %p", sock, sk); |
| 890 | |
| 891 | err = sock_error(sk); |
| 892 | if (err) |
| 893 | return err; |
| 894 | |
| 895 | if (msg->msg_flags & MSG_OOB) |
| 896 | return -EOPNOTSUPP; |
| 897 | |
| 898 | lock_sock(sk); |
| 899 | |
| 900 | if (sk->sk_state != BT_CONNECTED) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 901 | err = -ENOTCONN; |
| 902 | goto done; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 903 | } |
| 904 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 905 | /* Connectionless channel */ |
| 906 | if (sk->sk_type == SOCK_DGRAM) { |
| 907 | skb = l2cap_create_connless_pdu(sk, msg, len); |
| 908 | if (IS_ERR(skb)) { |
| 909 | err = PTR_ERR(skb); |
| 910 | } else { |
| 911 | l2cap_do_send(sk, skb); |
| 912 | err = len; |
| 913 | } |
| 914 | goto done; |
| 915 | } |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 916 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 917 | switch (pi->mode) { |
| 918 | case L2CAP_MODE_BASIC: |
| 919 | /* Check outgoing MTU */ |
| 920 | if (len > pi->omtu) { |
| 921 | err = -EMSGSIZE; |
| 922 | goto done; |
| 923 | } |
| 924 | |
| 925 | /* Create a basic PDU */ |
| 926 | skb = l2cap_create_basic_pdu(sk, msg, len); |
| 927 | if (IS_ERR(skb)) { |
| 928 | err = PTR_ERR(skb); |
| 929 | goto done; |
| 930 | } |
| 931 | |
| 932 | l2cap_do_send(sk, skb); |
| 933 | err = len; |
| 934 | break; |
| 935 | |
| 936 | case L2CAP_MODE_ERTM: |
| 937 | case L2CAP_MODE_STREAMING: |
| 938 | |
| 939 | /* Check outgoing MTU */ |
| 940 | if (len > pi->omtu) { |
| 941 | err = -EMSGSIZE; |
| 942 | goto done; |
| 943 | } |
| 944 | |
| 945 | __skb_queue_head_init(&seg_queue); |
| 946 | |
| 947 | /* Do segmentation before calling in to the state machine, |
| 948 | * since it's possible to block while waiting for memory |
| 949 | * allocation. |
| 950 | */ |
| 951 | amp_id = pi->amp_id; |
| 952 | err = l2cap_segment_sdu(sk, &seg_queue, msg, len, 0); |
| 953 | |
| 954 | /* The socket lock is released while segmenting, so check |
| 955 | * that the socket is still connected |
| 956 | */ |
| 957 | if (sk->sk_state != BT_CONNECTED) { |
| 958 | __skb_queue_purge(&seg_queue); |
| 959 | err = -ENOTCONN; |
| 960 | } |
| 961 | |
| 962 | if (err) { |
| 963 | BT_DBG("Error %d, sk_sndbuf %d, sk_wmem_alloc %d", |
| 964 | err, sk->sk_sndbuf, |
| 965 | atomic_read(&sk->sk_wmem_alloc)); |
| 966 | break; |
| 967 | } |
| 968 | |
| 969 | if (pi->amp_id != amp_id) { |
| 970 | /* Channel moved while unlocked. Resegment. */ |
| 971 | err = l2cap_resegment_queue(sk, &seg_queue); |
| 972 | |
| 973 | if (err) |
| 974 | break; |
| 975 | } |
| 976 | |
| 977 | if (pi->mode != L2CAP_MODE_STREAMING) |
| 978 | err = l2cap_ertm_tx(sk, 0, &seg_queue, |
| 979 | L2CAP_ERTM_EVENT_DATA_REQUEST); |
| 980 | else |
| 981 | err = l2cap_strm_tx(sk, &seg_queue); |
| 982 | if (!err) |
| 983 | err = len; |
| 984 | |
| 985 | /* If the skbs were not queued for sending, they'll still be in |
| 986 | * seg_queue and need to be purged. |
| 987 | */ |
| 988 | __skb_queue_purge(&seg_queue); |
| 989 | break; |
| 990 | |
| 991 | default: |
| 992 | BT_DBG("bad state %1.1x", pi->mode); |
| 993 | err = -EBADFD; |
| 994 | } |
| 995 | |
| 996 | done: |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 997 | release_sock(sk); |
| 998 | return err; |
| 999 | } |
| 1000 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 1001 | static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags) |
| 1002 | { |
| 1003 | struct sock *sk = sock->sk; |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 1004 | int err; |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 1005 | |
| 1006 | lock_sock(sk); |
| 1007 | |
| 1008 | if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1009 | struct l2cap_conn_rsp rsp; |
| 1010 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; |
| 1011 | u8 buf[128]; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1012 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1013 | if (l2cap_pi(sk)->amp_id) { |
| 1014 | /* Physical link must be brought up before connection |
| 1015 | * completes. |
| 1016 | */ |
| 1017 | amp_accept_physical(conn, l2cap_pi(sk)->amp_id, sk); |
| 1018 | release_sock(sk); |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1022 | sk->sk_state = BT_CONFIG; |
| 1023 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1024 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); |
| 1025 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); |
| 1026 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 1027 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 1028 | l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident, |
| 1029 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 1030 | |
| 1031 | if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) { |
| 1032 | release_sock(sk); |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; |
| 1037 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
| 1038 | l2cap_build_conf_req(sk, buf), buf); |
| 1039 | l2cap_pi(sk)->num_conf_req++; |
| 1040 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 1041 | release_sock(sk); |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | release_sock(sk); |
| 1046 | |
| 1047 | if (sock->type == SOCK_STREAM) |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 1048 | err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags); |
| 1049 | else |
| 1050 | err = bt_sock_recvmsg(iocb, sock, msg, len, flags); |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 1051 | |
Mat Martineau | 28eb3fa | 2012-02-09 16:06:12 -0800 | [diff] [blame] | 1052 | if (err >= 0) |
| 1053 | l2cap_ertm_recv_done(sk); |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 1054 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 1055 | return err; |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 1056 | } |
| 1057 | |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 1058 | /* Kill socket (only if zapped and orphan) |
| 1059 | * Must be called on unlocked socket. |
| 1060 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1061 | void l2cap_sock_kill(struct sock *sk) |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 1062 | { |
| 1063 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 1064 | return; |
| 1065 | |
| 1066 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
| 1067 | |
| 1068 | /* Kill poor orphan */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1069 | bt_sock_unlink(&l2cap_sk_list, sk); |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 1070 | sock_set_flag(sk, SOCK_DEAD); |
| 1071 | sock_put(sk); |
| 1072 | } |
| 1073 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1074 | /* Must be called on unlocked socket. */ |
| 1075 | static void l2cap_sock_close(struct sock *sk) |
| 1076 | { |
| 1077 | l2cap_sock_clear_timer(sk); |
| 1078 | lock_sock(sk); |
| 1079 | __l2cap_sock_close(sk, ECONNRESET); |
| 1080 | release_sock(sk); |
| 1081 | l2cap_sock_kill(sk); |
| 1082 | } |
| 1083 | |
| 1084 | static void l2cap_sock_cleanup_listen(struct sock *parent) |
| 1085 | { |
| 1086 | struct sock *sk; |
| 1087 | |
| 1088 | BT_DBG("parent %p", parent); |
| 1089 | |
| 1090 | /* Close not yet accepted channels */ |
| 1091 | while ((sk = bt_accept_dequeue(parent, NULL))) |
| 1092 | l2cap_sock_close(sk); |
| 1093 | |
| 1094 | parent->sk_state = BT_CLOSED; |
| 1095 | sock_set_flag(parent, SOCK_ZAPPED); |
| 1096 | } |
| 1097 | |
| 1098 | void __l2cap_sock_close(struct sock *sk, int reason) |
| 1099 | { |
| 1100 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; |
| 1101 | |
| 1102 | BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket); |
| 1103 | |
| 1104 | switch (sk->sk_state) { |
| 1105 | case BT_LISTEN: |
| 1106 | l2cap_sock_cleanup_listen(sk); |
| 1107 | break; |
| 1108 | |
| 1109 | case BT_CONNECTED: |
| 1110 | case BT_CONFIG: |
| 1111 | if ((sk->sk_type == SOCK_SEQPACKET || |
| 1112 | sk->sk_type == SOCK_STREAM) && |
| 1113 | conn->hcon->type == ACL_LINK) { |
| 1114 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 1115 | l2cap_send_disconn_req(conn, sk, reason); |
| 1116 | } else |
| 1117 | l2cap_chan_del(sk, reason); |
| 1118 | break; |
| 1119 | |
| 1120 | case BT_CONNECT2: |
| 1121 | if ((sk->sk_type == SOCK_SEQPACKET || |
| 1122 | sk->sk_type == SOCK_STREAM) && |
| 1123 | conn->hcon->type == ACL_LINK) { |
| 1124 | struct l2cap_conn_rsp rsp; |
| 1125 | __u16 result; |
| 1126 | |
| 1127 | if (bt_sk(sk)->defer_setup) |
| 1128 | result = L2CAP_CR_SEC_BLOCK; |
| 1129 | else |
| 1130 | result = L2CAP_CR_BAD_PSM; |
| 1131 | sk->sk_state = BT_DISCONN; |
| 1132 | |
| 1133 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); |
| 1134 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); |
| 1135 | rsp.result = cpu_to_le16(result); |
| 1136 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 1137 | l2cap_send_cmd(conn, l2cap_pi(sk)->ident, |
| 1138 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 1139 | } |
| 1140 | |
| 1141 | l2cap_chan_del(sk, reason); |
| 1142 | break; |
| 1143 | |
| 1144 | case BT_CONNECT: |
| 1145 | case BT_DISCONN: |
| 1146 | l2cap_chan_del(sk, reason); |
| 1147 | break; |
| 1148 | |
| 1149 | default: |
| 1150 | sock_set_flag(sk, SOCK_ZAPPED); |
| 1151 | break; |
| 1152 | } |
| 1153 | } |
| 1154 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1155 | static int l2cap_sock_shutdown(struct socket *sock, int how) |
| 1156 | { |
| 1157 | struct sock *sk = sock->sk; |
| 1158 | int err = 0; |
| 1159 | |
| 1160 | BT_DBG("sock %p, sk %p", sock, sk); |
| 1161 | |
| 1162 | if (!sk) |
| 1163 | return 0; |
| 1164 | |
| 1165 | lock_sock(sk); |
| 1166 | if (!sk->sk_shutdown) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1167 | |
| 1168 | if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1169 | err = __l2cap_wait_ack(sk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1170 | l2cap_ertm_shutdown(sk); |
| 1171 | } |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1172 | |
| 1173 | sk->sk_shutdown = SHUTDOWN_MASK; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1174 | l2cap_sock_clear_timer(sk); |
| 1175 | __l2cap_sock_close(sk, 0); |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1176 | |
| 1177 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 1178 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 1179 | sk->sk_lingertime); |
| 1180 | } |
| 1181 | |
| 1182 | if (!err && sk->sk_err) |
| 1183 | err = -sk->sk_err; |
| 1184 | |
| 1185 | release_sock(sk); |
| 1186 | return err; |
| 1187 | } |
| 1188 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1189 | static int l2cap_sock_release(struct socket *sock) |
| 1190 | { |
| 1191 | struct sock *sk = sock->sk; |
Brian Gix | bcdb7e7 | 2012-06-05 13:35:39 -0700 | [diff] [blame] | 1192 | struct sock *sk2 = NULL; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1193 | int err; |
| 1194 | |
| 1195 | BT_DBG("sock %p, sk %p", sock, sk); |
| 1196 | |
| 1197 | if (!sk) |
| 1198 | return 0; |
| 1199 | |
Brian Gix | bcdb7e7 | 2012-06-05 13:35:39 -0700 | [diff] [blame] | 1200 | /* If this is an ATT socket, find it's matching server/client */ |
| 1201 | if (l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA) |
| 1202 | sk2 = l2cap_find_sock_by_fixed_cid_and_dir(L2CAP_CID_LE_DATA, |
| 1203 | &bt_sk(sk)->src, &bt_sk(sk)->dst, |
| 1204 | l2cap_pi(sk)->incoming ? 0 : 1); |
Brian Gix | 20de7cf | 2012-02-02 14:56:51 -0800 | [diff] [blame] | 1205 | |
Brian Gix | bcdb7e7 | 2012-06-05 13:35:39 -0700 | [diff] [blame] | 1206 | /* If matching socket found, request tear down */ |
| 1207 | BT_DBG("sock:%p companion:%p", sk, sk2); |
| 1208 | if (sk2) |
| 1209 | l2cap_sock_set_timer(sk2, 1); |
Brian Gix | 20de7cf | 2012-02-02 14:56:51 -0800 | [diff] [blame] | 1210 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1211 | err = l2cap_sock_shutdown(sock, 2); |
| 1212 | |
| 1213 | sock_orphan(sk); |
| 1214 | l2cap_sock_kill(sk); |
| 1215 | return err; |
| 1216 | } |
| 1217 | |
| 1218 | static void l2cap_sock_destruct(struct sock *sk) |
| 1219 | { |
| 1220 | BT_DBG("sk %p", sk); |
| 1221 | |
| 1222 | skb_queue_purge(&sk->sk_receive_queue); |
| 1223 | skb_queue_purge(&sk->sk_write_queue); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1224 | |
| 1225 | l2cap_ertm_destruct(sk); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1226 | } |
| 1227 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1228 | static void set_default_config(struct l2cap_conf_prm *conf_prm) |
| 1229 | { |
| 1230 | conf_prm->fcs = L2CAP_FCS_CRC16; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1231 | conf_prm->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 1232 | } |
| 1233 | |
| 1234 | void l2cap_sock_init(struct sock *sk, struct sock *parent) |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1235 | { |
| 1236 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
| 1237 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1238 | BT_DBG("sk %p parent %p", sk, parent); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1239 | |
| 1240 | if (parent) { |
| 1241 | sk->sk_type = parent->sk_type; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1242 | sk->sk_rcvbuf = parent->sk_rcvbuf; |
| 1243 | sk->sk_sndbuf = parent->sk_sndbuf; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1244 | bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup; |
| 1245 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1246 | pi->imtu = l2cap_pi(parent)->imtu; |
| 1247 | pi->omtu = l2cap_pi(parent)->omtu; |
| 1248 | pi->conf_state = l2cap_pi(parent)->conf_state; |
| 1249 | pi->mode = l2cap_pi(parent)->mode; |
| 1250 | pi->fcs = l2cap_pi(parent)->fcs; |
| 1251 | pi->max_tx = l2cap_pi(parent)->max_tx; |
| 1252 | pi->tx_win = l2cap_pi(parent)->tx_win; |
| 1253 | pi->sec_level = l2cap_pi(parent)->sec_level; |
| 1254 | pi->role_switch = l2cap_pi(parent)->role_switch; |
| 1255 | pi->force_reliable = l2cap_pi(parent)->force_reliable; |
| 1256 | pi->flushable = l2cap_pi(parent)->flushable; |
| 1257 | pi->force_active = l2cap_pi(parent)->force_active; |
| 1258 | pi->amp_pref = l2cap_pi(parent)->amp_pref; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1259 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1260 | pi->imtu = L2CAP_DEFAULT_MTU; |
| 1261 | pi->omtu = 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1262 | if (!disable_ertm && sk->sk_type == SOCK_STREAM) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1263 | pi->mode = L2CAP_MODE_ERTM; |
| 1264 | pi->conf_state |= L2CAP_CONF_STATE2_DEVICE; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1265 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1266 | pi->mode = L2CAP_MODE_BASIC; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1267 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1268 | pi->reconf_state = L2CAP_RECONF_NONE; |
| 1269 | pi->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 1270 | pi->fcs = L2CAP_FCS_CRC16; |
| 1271 | pi->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 1272 | pi->sec_level = BT_SECURITY_LOW; |
| 1273 | pi->role_switch = 0; |
| 1274 | pi->force_reliable = 0; |
| 1275 | pi->flushable = 0; |
| 1276 | pi->force_active = 1; |
| 1277 | pi->amp_pref = BT_AMP_POLICY_REQUIRE_BR_EDR; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | /* Default config options */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1281 | sk->sk_backlog_rcv = l2cap_data_channel; |
| 1282 | pi->ampcon = NULL; |
| 1283 | pi->ampchan = NULL; |
| 1284 | pi->conf_len = 0; |
| 1285 | pi->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 1286 | pi->scid = 0; |
| 1287 | pi->dcid = 0; |
| 1288 | pi->tx_win_max = L2CAP_TX_WIN_MAX_ENHANCED; |
Mat Martineau | da31824 | 2012-06-22 11:36:18 -0700 | [diff] [blame] | 1289 | pi->ack_win = pi->tx_win; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1290 | pi->extended_control = 0; |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1291 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1292 | pi->local_conf.fcs = pi->fcs; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1293 | pi->local_conf.flush_to = pi->flush_to; |
| 1294 | |
| 1295 | set_default_config(&pi->remote_conf); |
| 1296 | |
| 1297 | skb_queue_head_init(TX_QUEUE(sk)); |
| 1298 | skb_queue_head_init(SREJ_QUEUE(sk)); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | static struct proto l2cap_proto = { |
| 1302 | .name = "L2CAP", |
| 1303 | .owner = THIS_MODULE, |
| 1304 | .obj_size = sizeof(struct l2cap_pinfo) |
| 1305 | }; |
| 1306 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1307 | struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio) |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1308 | { |
| 1309 | struct sock *sk; |
| 1310 | |
| 1311 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto); |
| 1312 | if (!sk) |
| 1313 | return NULL; |
| 1314 | |
| 1315 | sock_init_data(sock, sk); |
| 1316 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 1317 | |
| 1318 | sk->sk_destruct = l2cap_sock_destruct; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1319 | sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1320 | |
| 1321 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1322 | |
| 1323 | sk->sk_protocol = proto; |
| 1324 | sk->sk_state = BT_OPEN; |
| 1325 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1326 | setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk); |
Gustavo F. Padovan | dc50a06 | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1327 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1328 | bt_sock_link(&l2cap_sk_list, sk); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1329 | return sk; |
| 1330 | } |
| 1331 | |
| 1332 | static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, |
| 1333 | int kern) |
| 1334 | { |
| 1335 | struct sock *sk; |
| 1336 | |
| 1337 | BT_DBG("sock %p", sock); |
| 1338 | |
| 1339 | sock->state = SS_UNCONNECTED; |
| 1340 | |
| 1341 | if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM && |
| 1342 | sock->type != SOCK_DGRAM && sock->type != SOCK_RAW) |
| 1343 | return -ESOCKTNOSUPPORT; |
| 1344 | |
| 1345 | if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) |
| 1346 | return -EPERM; |
| 1347 | |
| 1348 | sock->ops = &l2cap_sock_ops; |
| 1349 | |
| 1350 | sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
| 1351 | if (!sk) |
| 1352 | return -ENOMEM; |
| 1353 | |
| 1354 | l2cap_sock_init(sk, NULL); |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1358 | const struct proto_ops l2cap_sock_ops = { |
Gustavo F. Padovan | 6539058 | 2011-02-04 02:33:56 -0200 | [diff] [blame] | 1359 | .family = PF_BLUETOOTH, |
| 1360 | .owner = THIS_MODULE, |
| 1361 | .release = l2cap_sock_release, |
| 1362 | .bind = l2cap_sock_bind, |
| 1363 | .connect = l2cap_sock_connect, |
| 1364 | .listen = l2cap_sock_listen, |
| 1365 | .accept = l2cap_sock_accept, |
| 1366 | .getname = l2cap_sock_getname, |
| 1367 | .sendmsg = l2cap_sock_sendmsg, |
| 1368 | .recvmsg = l2cap_sock_recvmsg, |
| 1369 | .poll = bt_sock_poll, |
| 1370 | .ioctl = bt_sock_ioctl, |
| 1371 | .mmap = sock_no_mmap, |
| 1372 | .socketpair = sock_no_socketpair, |
| 1373 | .shutdown = l2cap_sock_shutdown, |
| 1374 | .setsockopt = l2cap_sock_setsockopt, |
| 1375 | .getsockopt = l2cap_sock_getsockopt |
| 1376 | }; |
| 1377 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1378 | static const struct net_proto_family l2cap_sock_family_ops = { |
| 1379 | .family = PF_BLUETOOTH, |
| 1380 | .owner = THIS_MODULE, |
| 1381 | .create = l2cap_sock_create, |
| 1382 | }; |
| 1383 | |
| 1384 | int __init l2cap_init_sockets(void) |
| 1385 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1386 | int err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1387 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1388 | err = proto_register(&l2cap_proto, 0); |
| 1389 | if (err < 0) |
| 1390 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1391 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1392 | err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops); |
| 1393 | if (err < 0) |
| 1394 | goto error; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1395 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1396 | BT_INFO("L2CAP socket layer initialized"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1397 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1398 | return 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1399 | |
| 1400 | error: |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1401 | BT_ERR("L2CAP socket registration failed"); |
| 1402 | proto_unregister(&l2cap_proto); |
| 1403 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | void l2cap_cleanup_sockets(void) |
| 1407 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1408 | if (bt_sock_unregister(BTPROTO_L2CAP) < 0) |
| 1409 | BT_ERR("L2CAP socket unregistration failed"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1410 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1411 | proto_unregister(&l2cap_proto); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1412 | } |