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