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 | |
Brian Gix | 27a795c | 2012-04-19 11:05:06 -0700 | [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; |
Brian Gix | 27a795c | 2012-04-19 11:05:06 -0700 | [diff] [blame] | 510 | sec.level = l2cap_pi(sk)->conn->hcon->sec_level; |
| 511 | } |
Vinicius Costa Gomes | 89daa34 | 2011-07-08 18:31:46 -0300 | [diff] [blame] | 512 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 513 | len = min_t(unsigned int, len, sizeof(sec)); |
| 514 | if (copy_to_user(optval, (char *) &sec, len)) |
| 515 | err = -EFAULT; |
| 516 | |
| 517 | break; |
| 518 | |
| 519 | case BT_DEFER_SETUP: |
| 520 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 521 | err = -EINVAL; |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval)) |
| 526 | err = -EFAULT; |
| 527 | |
| 528 | break; |
| 529 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 530 | case BT_POWER: |
| 531 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 532 | && sk->sk_type != SOCK_RAW) { |
| 533 | err = -EINVAL; |
| 534 | break; |
| 535 | } |
| 536 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 537 | pwr.force_active = l2cap_pi(sk)->force_active; |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 538 | |
| 539 | len = min_t(unsigned int, len, sizeof(pwr)); |
| 540 | if (copy_to_user(optval, (char *) &pwr, len)) |
| 541 | err = -EFAULT; |
| 542 | |
| 543 | break; |
| 544 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 545 | case BT_AMP_POLICY: |
| 546 | if (put_user(l2cap_pi(sk)->amp_pref, (u32 __user *) optval)) |
| 547 | err = -EFAULT; |
| 548 | break; |
| 549 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 550 | default: |
| 551 | err = -ENOPROTOOPT; |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | release_sock(sk); |
| 556 | return err; |
| 557 | } |
| 558 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 559 | static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen) |
| 560 | { |
| 561 | struct sock *sk = sock->sk; |
| 562 | struct l2cap_options opts; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 563 | int len, le_sock, err = 0; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 564 | u32 opt; |
| 565 | |
| 566 | BT_DBG("sk %p", sk); |
| 567 | |
| 568 | lock_sock(sk); |
| 569 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 570 | le_sock = l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA; |
| 571 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 572 | switch (optname) { |
| 573 | case L2CAP_OPTIONS: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 574 | if (sk->sk_state == BT_CONNECTED && !le_sock) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 575 | err = -EINVAL; |
| 576 | break; |
| 577 | } |
| 578 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 579 | opts.imtu = l2cap_pi(sk)->imtu; |
| 580 | opts.omtu = l2cap_pi(sk)->omtu; |
| 581 | opts.flush_to = l2cap_pi(sk)->flush_to; |
| 582 | opts.mode = l2cap_pi(sk)->mode; |
| 583 | opts.fcs = l2cap_pi(sk)->fcs; |
| 584 | opts.max_tx = l2cap_pi(sk)->max_tx; |
| 585 | opts.txwin_size = l2cap_pi(sk)->tx_win; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 586 | |
| 587 | len = min_t(unsigned int, sizeof(opts), optlen); |
| 588 | if (copy_from_user((char *) &opts, optval, len)) { |
| 589 | err = -EFAULT; |
| 590 | break; |
| 591 | } |
| 592 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 593 | if ((opts.imtu || opts.omtu) && le_sock && |
| 594 | (sk->sk_state == BT_CONNECTED)) { |
| 595 | if (opts.imtu >= L2CAP_LE_DEFAULT_MTU) |
| 596 | l2cap_pi(sk)->imtu = opts.imtu; |
| 597 | if (opts.omtu >= L2CAP_LE_DEFAULT_MTU) |
| 598 | l2cap_pi(sk)->omtu = opts.omtu; |
| 599 | if (opts.imtu < L2CAP_LE_DEFAULT_MTU || |
| 600 | opts.omtu < L2CAP_LE_DEFAULT_MTU) |
| 601 | err = -EINVAL; |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | if (opts.txwin_size < 1 || |
| 606 | opts.txwin_size > L2CAP_TX_WIN_MAX_EXTENDED) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 607 | err = -EINVAL; |
| 608 | break; |
| 609 | } |
| 610 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 611 | l2cap_pi(sk)->mode = opts.mode; |
| 612 | switch (l2cap_pi(sk)->mode) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 613 | case L2CAP_MODE_BASIC: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 614 | l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_STATE2_DEVICE; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 615 | break; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 616 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 0ac1436 | 2012-02-03 16:24:42 -0800 | [diff] [blame] | 617 | if (!disable_ertm) { |
| 618 | /* No fallback to ERTM or Basic mode */ |
| 619 | l2cap_pi(sk)->conf_state |= |
| 620 | L2CAP_CONF_STATE2_DEVICE; |
| 621 | break; |
| 622 | } |
| 623 | err = -EINVAL; |
| 624 | break; |
| 625 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 626 | if (!disable_ertm) |
| 627 | break; |
| 628 | /* fall through */ |
| 629 | default: |
| 630 | err = -EINVAL; |
| 631 | break; |
| 632 | } |
| 633 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 634 | l2cap_pi(sk)->imtu = opts.imtu; |
| 635 | l2cap_pi(sk)->omtu = opts.omtu; |
| 636 | l2cap_pi(sk)->fcs = opts.fcs; |
| 637 | l2cap_pi(sk)->max_tx = opts.max_tx; |
| 638 | l2cap_pi(sk)->tx_win = opts.txwin_size; |
Srinivas Krovvidi | 1073419 | 2011-12-29 07:29:11 +0530 | [diff] [blame] | 639 | l2cap_pi(sk)->flush_to = opts.flush_to; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 640 | break; |
| 641 | |
| 642 | case L2CAP_LM: |
| 643 | if (get_user(opt, (u32 __user *) optval)) { |
| 644 | err = -EFAULT; |
| 645 | break; |
| 646 | } |
| 647 | |
| 648 | if (opt & L2CAP_LM_AUTH) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 649 | l2cap_pi(sk)->sec_level = BT_SECURITY_LOW; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 650 | if (opt & L2CAP_LM_ENCRYPT) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 651 | l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 652 | if (opt & L2CAP_LM_SECURE) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 653 | l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 654 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 655 | l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER); |
| 656 | l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE); |
| 657 | l2cap_pi(sk)->flushable = (opt & L2CAP_LM_FLUSHABLE); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 658 | break; |
| 659 | |
| 660 | default: |
| 661 | err = -ENOPROTOOPT; |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | release_sock(sk); |
| 666 | return err; |
| 667 | } |
| 668 | |
| 669 | static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
| 670 | { |
| 671 | struct sock *sk = sock->sk; |
| 672 | struct bt_security sec; |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 673 | struct bt_power pwr; |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 674 | struct l2cap_conn *conn; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 675 | int len, err = 0; |
| 676 | u32 opt; |
| 677 | |
| 678 | BT_DBG("sk %p", sk); |
| 679 | |
| 680 | if (level == SOL_L2CAP) |
| 681 | return l2cap_sock_setsockopt_old(sock, optname, optval, optlen); |
| 682 | |
| 683 | if (level != SOL_BLUETOOTH) |
| 684 | return -ENOPROTOOPT; |
| 685 | |
| 686 | lock_sock(sk); |
| 687 | |
| 688 | switch (optname) { |
| 689 | case BT_SECURITY: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 690 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 691 | && sk->sk_type != SOCK_RAW) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 692 | err = -EINVAL; |
| 693 | break; |
| 694 | } |
| 695 | |
| 696 | sec.level = BT_SECURITY_LOW; |
| 697 | |
| 698 | len = min_t(unsigned int, sizeof(sec), optlen); |
| 699 | if (copy_from_user((char *) &sec, optval, len)) { |
| 700 | err = -EFAULT; |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | if (sec.level < BT_SECURITY_LOW || |
| 705 | sec.level > BT_SECURITY_HIGH) { |
| 706 | err = -EINVAL; |
| 707 | break; |
| 708 | } |
| 709 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 710 | l2cap_pi(sk)->sec_level = sec.level; |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 711 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 712 | conn = l2cap_pi(sk)->conn; |
| 713 | if (conn && l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA) { |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 714 | if (!conn->hcon->out) { |
| 715 | err = -EINVAL; |
| 716 | break; |
| 717 | } |
| 718 | |
| 719 | if (smp_conn_security(conn, sec.level)) |
| 720 | break; |
| 721 | |
| 722 | err = 0; |
| 723 | sk->sk_state = BT_CONFIG; |
| 724 | } |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 725 | break; |
| 726 | |
| 727 | case BT_DEFER_SETUP: |
| 728 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 729 | err = -EINVAL; |
| 730 | break; |
| 731 | } |
| 732 | |
| 733 | if (get_user(opt, (u32 __user *) optval)) { |
| 734 | err = -EFAULT; |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | bt_sk(sk)->defer_setup = opt; |
| 739 | break; |
| 740 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 741 | case BT_POWER: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 742 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 743 | && sk->sk_type != SOCK_RAW) { |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 744 | err = -EINVAL; |
| 745 | break; |
| 746 | } |
| 747 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 748 | pwr.force_active = 1; |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 749 | |
| 750 | len = min_t(unsigned int, sizeof(pwr), optlen); |
| 751 | if (copy_from_user((char *) &pwr, optval, len)) { |
| 752 | err = -EFAULT; |
| 753 | break; |
| 754 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 755 | l2cap_pi(sk)->force_active = pwr.force_active; |
| 756 | break; |
| 757 | |
| 758 | case BT_AMP_POLICY: |
| 759 | if (get_user(opt, (u32 __user *) optval)) { |
| 760 | err = -EFAULT; |
| 761 | break; |
| 762 | } |
| 763 | |
Mat Martineau | f00ffac | 2011-10-19 15:07:32 -0700 | [diff] [blame] | 764 | if ((opt > BT_AMP_POLICY_PREFER_AMP) || |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 765 | ((l2cap_pi(sk)->mode != L2CAP_MODE_ERTM) && |
| 766 | (l2cap_pi(sk)->mode != L2CAP_MODE_STREAMING))) { |
| 767 | err = -EINVAL; |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | l2cap_pi(sk)->amp_pref = (u8) opt; |
| 772 | BT_DBG("BT_AMP_POLICY now %d", opt); |
| 773 | |
| 774 | if ((sk->sk_state == BT_CONNECTED) && |
Peter Krystad | c446d21 | 2011-09-20 15:35:50 -0700 | [diff] [blame] | 775 | (l2cap_pi(sk)->amp_move_role == L2CAP_AMP_MOVE_NONE)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 776 | l2cap_amp_move_init(sk); |
| 777 | |
Jaikumar Ganesh | 514abe6 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 778 | break; |
| 779 | |
Srinivas Krovvidi | 1073419 | 2011-12-29 07:29:11 +0530 | [diff] [blame] | 780 | case BT_FLUSHABLE: |
| 781 | if (get_user(opt, (u32 __user *) optval)) { |
| 782 | err = -EFAULT; |
| 783 | break; |
| 784 | } |
| 785 | l2cap_pi(sk)->flushable = opt; |
| 786 | |
| 787 | break; |
| 788 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 789 | default: |
| 790 | err = -ENOPROTOOPT; |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | release_sock(sk); |
| 795 | return err; |
| 796 | } |
| 797 | |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 798 | static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len) |
| 799 | { |
| 800 | struct sock *sk = sock->sk; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 801 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
| 802 | struct sk_buff *skb; |
| 803 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 804 | int err; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 805 | u8 amp_id; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 806 | |
| 807 | BT_DBG("sock %p, sk %p", sock, sk); |
| 808 | |
| 809 | err = sock_error(sk); |
| 810 | if (err) |
| 811 | return err; |
| 812 | |
| 813 | if (msg->msg_flags & MSG_OOB) |
| 814 | return -EOPNOTSUPP; |
| 815 | |
| 816 | lock_sock(sk); |
| 817 | |
| 818 | if (sk->sk_state != BT_CONNECTED) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 819 | err = -ENOTCONN; |
| 820 | goto done; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 821 | } |
| 822 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 823 | /* Connectionless channel */ |
| 824 | if (sk->sk_type == SOCK_DGRAM) { |
| 825 | skb = l2cap_create_connless_pdu(sk, msg, len); |
| 826 | if (IS_ERR(skb)) { |
| 827 | err = PTR_ERR(skb); |
| 828 | } else { |
| 829 | l2cap_do_send(sk, skb); |
| 830 | err = len; |
| 831 | } |
| 832 | goto done; |
| 833 | } |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 834 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 835 | switch (pi->mode) { |
| 836 | case L2CAP_MODE_BASIC: |
| 837 | /* Check outgoing MTU */ |
| 838 | if (len > pi->omtu) { |
| 839 | err = -EMSGSIZE; |
| 840 | goto done; |
| 841 | } |
| 842 | |
| 843 | /* Create a basic PDU */ |
| 844 | skb = l2cap_create_basic_pdu(sk, msg, len); |
| 845 | if (IS_ERR(skb)) { |
| 846 | err = PTR_ERR(skb); |
| 847 | goto done; |
| 848 | } |
| 849 | |
| 850 | l2cap_do_send(sk, skb); |
| 851 | err = len; |
| 852 | break; |
| 853 | |
| 854 | case L2CAP_MODE_ERTM: |
| 855 | case L2CAP_MODE_STREAMING: |
| 856 | |
| 857 | /* Check outgoing MTU */ |
| 858 | if (len > pi->omtu) { |
| 859 | err = -EMSGSIZE; |
| 860 | goto done; |
| 861 | } |
| 862 | |
| 863 | __skb_queue_head_init(&seg_queue); |
| 864 | |
| 865 | /* Do segmentation before calling in to the state machine, |
| 866 | * since it's possible to block while waiting for memory |
| 867 | * allocation. |
| 868 | */ |
| 869 | amp_id = pi->amp_id; |
| 870 | err = l2cap_segment_sdu(sk, &seg_queue, msg, len, 0); |
| 871 | |
| 872 | /* The socket lock is released while segmenting, so check |
| 873 | * that the socket is still connected |
| 874 | */ |
| 875 | if (sk->sk_state != BT_CONNECTED) { |
| 876 | __skb_queue_purge(&seg_queue); |
| 877 | err = -ENOTCONN; |
| 878 | } |
| 879 | |
| 880 | if (err) { |
| 881 | BT_DBG("Error %d, sk_sndbuf %d, sk_wmem_alloc %d", |
| 882 | err, sk->sk_sndbuf, |
| 883 | atomic_read(&sk->sk_wmem_alloc)); |
| 884 | break; |
| 885 | } |
| 886 | |
| 887 | if (pi->amp_id != amp_id) { |
| 888 | /* Channel moved while unlocked. Resegment. */ |
| 889 | err = l2cap_resegment_queue(sk, &seg_queue); |
| 890 | |
| 891 | if (err) |
| 892 | break; |
| 893 | } |
| 894 | |
| 895 | if (pi->mode != L2CAP_MODE_STREAMING) |
| 896 | err = l2cap_ertm_tx(sk, 0, &seg_queue, |
| 897 | L2CAP_ERTM_EVENT_DATA_REQUEST); |
| 898 | else |
| 899 | err = l2cap_strm_tx(sk, &seg_queue); |
| 900 | if (!err) |
| 901 | err = len; |
| 902 | |
| 903 | /* If the skbs were not queued for sending, they'll still be in |
| 904 | * seg_queue and need to be purged. |
| 905 | */ |
| 906 | __skb_queue_purge(&seg_queue); |
| 907 | break; |
| 908 | |
| 909 | default: |
| 910 | BT_DBG("bad state %1.1x", pi->mode); |
| 911 | err = -EBADFD; |
| 912 | } |
| 913 | |
| 914 | done: |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 915 | release_sock(sk); |
| 916 | return err; |
| 917 | } |
| 918 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 919 | static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags) |
| 920 | { |
| 921 | struct sock *sk = sock->sk; |
Mat Martineau | c0675b8 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 922 | int err; |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 923 | |
| 924 | lock_sock(sk); |
| 925 | |
| 926 | if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 927 | struct l2cap_conn_rsp rsp; |
| 928 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; |
| 929 | u8 buf[128]; |
| 930 | |
| 931 | if (l2cap_pi(sk)->amp_id) { |
| 932 | /* Physical link must be brought up before connection |
| 933 | * completes. |
| 934 | */ |
| 935 | amp_accept_physical(conn, l2cap_pi(sk)->amp_id, sk); |
| 936 | release_sock(sk); |
| 937 | return 0; |
| 938 | } |
| 939 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 940 | sk->sk_state = BT_CONFIG; |
| 941 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 942 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); |
| 943 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); |
| 944 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 945 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 946 | l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident, |
| 947 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 948 | |
| 949 | if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) { |
| 950 | release_sock(sk); |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; |
| 955 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
| 956 | l2cap_build_conf_req(sk, buf), buf); |
| 957 | l2cap_pi(sk)->num_conf_req++; |
| 958 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 959 | release_sock(sk); |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | release_sock(sk); |
| 964 | |
| 965 | if (sock->type == SOCK_STREAM) |
Mat Martineau | c0675b8 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 966 | err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags); |
| 967 | else |
| 968 | err = bt_sock_recvmsg(iocb, sock, msg, len, flags); |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 969 | |
Mat Martineau | 28eb3fa | 2012-02-09 16:06:12 -0800 | [diff] [blame] | 970 | if (err >= 0) |
| 971 | l2cap_ertm_recv_done(sk); |
Mat Martineau | c0675b8 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 972 | |
Mat Martineau | c0675b8 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 973 | return err; |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 974 | } |
| 975 | |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 976 | /* Kill socket (only if zapped and orphan) |
| 977 | * Must be called on unlocked socket. |
| 978 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 979 | void l2cap_sock_kill(struct sock *sk) |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 980 | { |
| 981 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 982 | return; |
| 983 | |
| 984 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
| 985 | |
| 986 | /* Kill poor orphan */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 987 | bt_sock_unlink(&l2cap_sk_list, sk); |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 988 | sock_set_flag(sk, SOCK_DEAD); |
| 989 | sock_put(sk); |
| 990 | } |
| 991 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 992 | /* Must be called on unlocked socket. */ |
| 993 | static void l2cap_sock_close(struct sock *sk) |
| 994 | { |
| 995 | l2cap_sock_clear_timer(sk); |
| 996 | lock_sock(sk); |
| 997 | __l2cap_sock_close(sk, ECONNRESET); |
| 998 | release_sock(sk); |
| 999 | l2cap_sock_kill(sk); |
| 1000 | } |
| 1001 | |
| 1002 | static void l2cap_sock_cleanup_listen(struct sock *parent) |
| 1003 | { |
| 1004 | struct sock *sk; |
| 1005 | |
| 1006 | BT_DBG("parent %p", parent); |
| 1007 | |
| 1008 | /* Close not yet accepted channels */ |
| 1009 | while ((sk = bt_accept_dequeue(parent, NULL))) |
| 1010 | l2cap_sock_close(sk); |
| 1011 | |
| 1012 | parent->sk_state = BT_CLOSED; |
| 1013 | sock_set_flag(parent, SOCK_ZAPPED); |
| 1014 | } |
| 1015 | |
| 1016 | void __l2cap_sock_close(struct sock *sk, int reason) |
| 1017 | { |
| 1018 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; |
| 1019 | |
| 1020 | BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket); |
| 1021 | |
| 1022 | switch (sk->sk_state) { |
| 1023 | case BT_LISTEN: |
| 1024 | l2cap_sock_cleanup_listen(sk); |
| 1025 | break; |
| 1026 | |
| 1027 | case BT_CONNECTED: |
| 1028 | case BT_CONFIG: |
| 1029 | if ((sk->sk_type == SOCK_SEQPACKET || |
| 1030 | sk->sk_type == SOCK_STREAM) && |
| 1031 | conn->hcon->type == ACL_LINK) { |
| 1032 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 1033 | l2cap_send_disconn_req(conn, sk, reason); |
| 1034 | } else |
| 1035 | l2cap_chan_del(sk, reason); |
| 1036 | break; |
| 1037 | |
| 1038 | case BT_CONNECT2: |
| 1039 | if ((sk->sk_type == SOCK_SEQPACKET || |
| 1040 | sk->sk_type == SOCK_STREAM) && |
| 1041 | conn->hcon->type == ACL_LINK) { |
| 1042 | struct l2cap_conn_rsp rsp; |
| 1043 | __u16 result; |
| 1044 | |
| 1045 | if (bt_sk(sk)->defer_setup) |
| 1046 | result = L2CAP_CR_SEC_BLOCK; |
| 1047 | else |
| 1048 | result = L2CAP_CR_BAD_PSM; |
| 1049 | sk->sk_state = BT_DISCONN; |
| 1050 | |
| 1051 | rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); |
| 1052 | rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); |
| 1053 | rsp.result = cpu_to_le16(result); |
| 1054 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 1055 | l2cap_send_cmd(conn, l2cap_pi(sk)->ident, |
| 1056 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 1057 | } |
| 1058 | |
| 1059 | l2cap_chan_del(sk, reason); |
| 1060 | break; |
| 1061 | |
| 1062 | case BT_CONNECT: |
| 1063 | case BT_DISCONN: |
| 1064 | l2cap_chan_del(sk, reason); |
| 1065 | break; |
| 1066 | |
| 1067 | default: |
| 1068 | sock_set_flag(sk, SOCK_ZAPPED); |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1073 | static int l2cap_sock_shutdown(struct socket *sock, int how) |
| 1074 | { |
| 1075 | struct sock *sk = sock->sk; |
| 1076 | int err = 0; |
| 1077 | |
| 1078 | BT_DBG("sock %p, sk %p", sock, sk); |
| 1079 | |
| 1080 | if (!sk) |
| 1081 | return 0; |
| 1082 | |
| 1083 | lock_sock(sk); |
| 1084 | if (!sk->sk_shutdown) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1085 | |
| 1086 | if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1087 | err = __l2cap_wait_ack(sk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1088 | l2cap_ertm_shutdown(sk); |
| 1089 | } |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1090 | |
| 1091 | sk->sk_shutdown = SHUTDOWN_MASK; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1092 | l2cap_sock_clear_timer(sk); |
| 1093 | __l2cap_sock_close(sk, 0); |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1094 | |
| 1095 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 1096 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 1097 | sk->sk_lingertime); |
| 1098 | } |
| 1099 | |
| 1100 | if (!err && sk->sk_err) |
| 1101 | err = -sk->sk_err; |
| 1102 | |
| 1103 | release_sock(sk); |
| 1104 | return err; |
| 1105 | } |
| 1106 | |
Gustavo F. Padovan | 554f05b | 2011-02-04 02:36:42 -0200 | [diff] [blame] | 1107 | static int l2cap_sock_release(struct socket *sock) |
| 1108 | { |
| 1109 | struct sock *sk = sock->sk; |
Brian Gix | 20de7cf | 2012-02-02 14:56:51 -0800 | [diff] [blame] | 1110 | struct sock *srv_sk = NULL; |
Gustavo F. Padovan | 554f05b | 2011-02-04 02:36:42 -0200 | [diff] [blame] | 1111 | int err; |
| 1112 | |
| 1113 | BT_DBG("sock %p, sk %p", sock, sk); |
| 1114 | |
| 1115 | if (!sk) |
| 1116 | return 0; |
| 1117 | |
Brian Gix | 20de7cf | 2012-02-02 14:56:51 -0800 | [diff] [blame] | 1118 | /* If this is an ATT Client socket, find the matching Server */ |
| 1119 | if (l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA && !l2cap_pi(sk)->incoming) |
| 1120 | srv_sk = l2cap_find_sock_by_fixed_cid_and_dir(L2CAP_CID_LE_DATA, |
| 1121 | &bt_sk(sk)->src, &bt_sk(sk)->dst, 1); |
| 1122 | |
| 1123 | /* If server socket found, request tear down */ |
| 1124 | BT_DBG("client:%p server:%p", sk, srv_sk); |
| 1125 | if (srv_sk) |
| 1126 | l2cap_sock_set_timer(srv_sk, 1); |
| 1127 | |
Gustavo F. Padovan | 554f05b | 2011-02-04 02:36:42 -0200 | [diff] [blame] | 1128 | err = l2cap_sock_shutdown(sock, 2); |
| 1129 | |
| 1130 | sock_orphan(sk); |
| 1131 | l2cap_sock_kill(sk); |
| 1132 | return err; |
| 1133 | } |
| 1134 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1135 | static void l2cap_sock_destruct(struct sock *sk) |
| 1136 | { |
| 1137 | BT_DBG("sk %p", sk); |
| 1138 | |
| 1139 | skb_queue_purge(&sk->sk_receive_queue); |
| 1140 | skb_queue_purge(&sk->sk_write_queue); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1141 | |
| 1142 | l2cap_ertm_destruct(sk); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1143 | } |
| 1144 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1145 | static void set_default_config(struct l2cap_conf_prm *conf_prm) |
| 1146 | { |
| 1147 | conf_prm->fcs = L2CAP_FCS_CRC16; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1148 | conf_prm->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 1149 | } |
| 1150 | |
| 1151 | void l2cap_sock_init(struct sock *sk, struct sock *parent) |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1152 | { |
| 1153 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
| 1154 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1155 | BT_DBG("sk %p parent %p", sk, parent); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1156 | |
| 1157 | if (parent) { |
| 1158 | sk->sk_type = parent->sk_type; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1159 | sk->sk_rcvbuf = parent->sk_rcvbuf; |
| 1160 | sk->sk_sndbuf = parent->sk_sndbuf; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1161 | bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup; |
| 1162 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1163 | pi->imtu = l2cap_pi(parent)->imtu; |
| 1164 | pi->omtu = l2cap_pi(parent)->omtu; |
| 1165 | pi->conf_state = l2cap_pi(parent)->conf_state; |
| 1166 | pi->mode = l2cap_pi(parent)->mode; |
| 1167 | pi->fcs = l2cap_pi(parent)->fcs; |
| 1168 | pi->max_tx = l2cap_pi(parent)->max_tx; |
| 1169 | pi->tx_win = l2cap_pi(parent)->tx_win; |
| 1170 | pi->sec_level = l2cap_pi(parent)->sec_level; |
| 1171 | pi->role_switch = l2cap_pi(parent)->role_switch; |
| 1172 | pi->force_reliable = l2cap_pi(parent)->force_reliable; |
| 1173 | pi->flushable = l2cap_pi(parent)->flushable; |
| 1174 | pi->force_active = l2cap_pi(parent)->force_active; |
| 1175 | pi->amp_pref = l2cap_pi(parent)->amp_pref; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1176 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1177 | pi->imtu = L2CAP_DEFAULT_MTU; |
| 1178 | pi->omtu = 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1179 | if (!disable_ertm && sk->sk_type == SOCK_STREAM) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1180 | pi->mode = L2CAP_MODE_ERTM; |
| 1181 | pi->conf_state |= L2CAP_CONF_STATE2_DEVICE; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1182 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1183 | pi->mode = L2CAP_MODE_BASIC; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1184 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1185 | pi->reconf_state = L2CAP_RECONF_NONE; |
| 1186 | pi->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 1187 | pi->fcs = L2CAP_FCS_CRC16; |
| 1188 | pi->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 1189 | pi->sec_level = BT_SECURITY_LOW; |
| 1190 | pi->role_switch = 0; |
| 1191 | pi->force_reliable = 0; |
| 1192 | pi->flushable = 0; |
| 1193 | pi->force_active = 1; |
| 1194 | pi->amp_pref = BT_AMP_POLICY_REQUIRE_BR_EDR; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* Default config options */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1198 | sk->sk_backlog_rcv = l2cap_data_channel; |
| 1199 | pi->ampcon = NULL; |
| 1200 | pi->ampchan = NULL; |
| 1201 | pi->conf_len = 0; |
| 1202 | pi->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 1203 | pi->scid = 0; |
| 1204 | pi->dcid = 0; |
| 1205 | pi->tx_win_max = L2CAP_TX_WIN_MAX_ENHANCED; |
| 1206 | pi->extended_control = 0; |
Gustavo F. Padovan | b090c4b | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1207 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1208 | pi->local_conf.fcs = pi->fcs; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1209 | pi->local_conf.flush_to = pi->flush_to; |
| 1210 | |
| 1211 | set_default_config(&pi->remote_conf); |
| 1212 | |
| 1213 | skb_queue_head_init(TX_QUEUE(sk)); |
| 1214 | skb_queue_head_init(SREJ_QUEUE(sk)); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | static struct proto l2cap_proto = { |
| 1218 | .name = "L2CAP", |
| 1219 | .owner = THIS_MODULE, |
| 1220 | .obj_size = sizeof(struct l2cap_pinfo) |
| 1221 | }; |
| 1222 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1223 | 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] | 1224 | { |
| 1225 | struct sock *sk; |
| 1226 | |
| 1227 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto); |
| 1228 | if (!sk) |
| 1229 | return NULL; |
| 1230 | |
| 1231 | sock_init_data(sock, sk); |
| 1232 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 1233 | |
| 1234 | sk->sk_destruct = l2cap_sock_destruct; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1235 | sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1236 | |
| 1237 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1238 | |
| 1239 | sk->sk_protocol = proto; |
| 1240 | sk->sk_state = BT_OPEN; |
| 1241 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1242 | setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk); |
Gustavo F. Padovan | b87bf5b | 2011-05-16 16:42:01 -0300 | [diff] [blame] | 1243 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1244 | bt_sock_link(&l2cap_sk_list, sk); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1245 | return sk; |
| 1246 | } |
| 1247 | |
| 1248 | static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, |
| 1249 | int kern) |
| 1250 | { |
| 1251 | struct sock *sk; |
| 1252 | |
| 1253 | BT_DBG("sock %p", sock); |
| 1254 | |
| 1255 | sock->state = SS_UNCONNECTED; |
| 1256 | |
| 1257 | if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM && |
| 1258 | sock->type != SOCK_DGRAM && sock->type != SOCK_RAW) |
| 1259 | return -ESOCKTNOSUPPORT; |
| 1260 | |
| 1261 | if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) |
| 1262 | return -EPERM; |
| 1263 | |
| 1264 | sock->ops = &l2cap_sock_ops; |
| 1265 | |
| 1266 | sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
| 1267 | if (!sk) |
| 1268 | return -ENOMEM; |
| 1269 | |
| 1270 | l2cap_sock_init(sk, NULL); |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1274 | const struct proto_ops l2cap_sock_ops = { |
Gustavo F. Padovan | 6539058 | 2011-02-04 02:33:56 -0200 | [diff] [blame] | 1275 | .family = PF_BLUETOOTH, |
| 1276 | .owner = THIS_MODULE, |
| 1277 | .release = l2cap_sock_release, |
| 1278 | .bind = l2cap_sock_bind, |
| 1279 | .connect = l2cap_sock_connect, |
| 1280 | .listen = l2cap_sock_listen, |
| 1281 | .accept = l2cap_sock_accept, |
| 1282 | .getname = l2cap_sock_getname, |
| 1283 | .sendmsg = l2cap_sock_sendmsg, |
| 1284 | .recvmsg = l2cap_sock_recvmsg, |
| 1285 | .poll = bt_sock_poll, |
| 1286 | .ioctl = bt_sock_ioctl, |
| 1287 | .mmap = sock_no_mmap, |
| 1288 | .socketpair = sock_no_socketpair, |
| 1289 | .shutdown = l2cap_sock_shutdown, |
| 1290 | .setsockopt = l2cap_sock_setsockopt, |
| 1291 | .getsockopt = l2cap_sock_getsockopt |
| 1292 | }; |
| 1293 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1294 | static const struct net_proto_family l2cap_sock_family_ops = { |
| 1295 | .family = PF_BLUETOOTH, |
| 1296 | .owner = THIS_MODULE, |
| 1297 | .create = l2cap_sock_create, |
| 1298 | }; |
| 1299 | |
| 1300 | int __init l2cap_init_sockets(void) |
| 1301 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1302 | int err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1303 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1304 | err = proto_register(&l2cap_proto, 0); |
| 1305 | if (err < 0) |
| 1306 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1307 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1308 | err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops); |
| 1309 | if (err < 0) |
| 1310 | goto error; |
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 | BT_INFO("L2CAP socket layer initialized"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1313 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1314 | return 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1315 | |
| 1316 | error: |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1317 | BT_ERR("L2CAP socket registration failed"); |
| 1318 | proto_unregister(&l2cap_proto); |
| 1319 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | void l2cap_cleanup_sockets(void) |
| 1323 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1324 | if (bt_sock_unregister(BTPROTO_L2CAP) < 0) |
| 1325 | BT_ERR("L2CAP socket unregistration failed"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1326 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 1327 | proto_unregister(&l2cap_proto); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1328 | } |