Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
| 4 | Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> |
| 5 | Copyright (C) 2010 Google Inc. |
| 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> |
| 32 | |
| 33 | static void l2cap_sock_timeout(unsigned long arg) |
| 34 | { |
| 35 | struct sock *sk = (struct sock *) arg; |
| 36 | int reason; |
| 37 | |
| 38 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 39 | |
| 40 | bh_lock_sock(sk); |
| 41 | |
| 42 | if (sock_owned_by_user(sk)) { |
| 43 | /* sk is owned by user. Try again later */ |
| 44 | l2cap_sock_set_timer(sk, HZ / 5); |
| 45 | bh_unlock_sock(sk); |
| 46 | sock_put(sk); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG) |
| 51 | reason = ECONNREFUSED; |
| 52 | else if (sk->sk_state == BT_CONNECT && |
| 53 | l2cap_pi(sk)->sec_level != BT_SECURITY_SDP) |
| 54 | reason = ECONNREFUSED; |
| 55 | else |
| 56 | reason = ETIMEDOUT; |
| 57 | |
| 58 | __l2cap_sock_close(sk, reason); |
| 59 | |
| 60 | bh_unlock_sock(sk); |
| 61 | |
| 62 | l2cap_sock_kill(sk); |
| 63 | sock_put(sk); |
| 64 | } |
| 65 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 66 | static struct sock *__l2cap_get_sock_by_addr(__le16 psm, bdaddr_t *src) |
| 67 | { |
| 68 | struct sock *sk; |
| 69 | struct hlist_node *node; |
| 70 | sk_for_each(sk, node, &l2cap_sk_list.head) |
| 71 | if (l2cap_pi(sk)->sport == psm && !bacmp(&bt_sk(sk)->src, src)) |
| 72 | goto found; |
| 73 | sk = NULL; |
| 74 | found: |
| 75 | return sk; |
| 76 | } |
| 77 | |
| 78 | static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) |
| 79 | { |
| 80 | struct sock *sk = sock->sk; |
| 81 | struct sockaddr_l2 la; |
| 82 | int len, err = 0; |
| 83 | |
| 84 | BT_DBG("sk %p", sk); |
| 85 | |
| 86 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | memset(&la, 0, sizeof(la)); |
| 90 | len = min_t(unsigned int, sizeof(la), alen); |
| 91 | memcpy(&la, addr, len); |
| 92 | |
| 93 | if (la.l2_cid) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | lock_sock(sk); |
| 97 | |
| 98 | if (sk->sk_state != BT_OPEN) { |
| 99 | err = -EBADFD; |
| 100 | goto done; |
| 101 | } |
| 102 | |
| 103 | if (la.l2_psm) { |
| 104 | __u16 psm = __le16_to_cpu(la.l2_psm); |
| 105 | |
| 106 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 107 | if ((psm & 0x0101) != 0x0001) { |
| 108 | err = -EINVAL; |
| 109 | goto done; |
| 110 | } |
| 111 | |
| 112 | /* Restrict usage of well-known PSMs */ |
| 113 | if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) { |
| 114 | err = -EACCES; |
| 115 | goto done; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | write_lock_bh(&l2cap_sk_list.lock); |
| 120 | |
| 121 | if (la.l2_psm && __l2cap_get_sock_by_addr(la.l2_psm, &la.l2_bdaddr)) { |
| 122 | err = -EADDRINUSE; |
| 123 | } else { |
| 124 | /* Save source address */ |
| 125 | bacpy(&bt_sk(sk)->src, &la.l2_bdaddr); |
| 126 | l2cap_pi(sk)->psm = la.l2_psm; |
| 127 | l2cap_pi(sk)->sport = la.l2_psm; |
| 128 | sk->sk_state = BT_BOUND; |
| 129 | |
| 130 | if (__le16_to_cpu(la.l2_psm) == 0x0001 || |
| 131 | __le16_to_cpu(la.l2_psm) == 0x0003) |
| 132 | l2cap_pi(sk)->sec_level = BT_SECURITY_SDP; |
| 133 | } |
| 134 | |
| 135 | write_unlock_bh(&l2cap_sk_list.lock); |
| 136 | |
| 137 | done: |
| 138 | release_sock(sk); |
| 139 | return err; |
| 140 | } |
| 141 | |
| 142 | static int l2cap_sock_listen(struct socket *sock, int backlog) |
| 143 | { |
| 144 | struct sock *sk = sock->sk; |
| 145 | int err = 0; |
| 146 | |
| 147 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 148 | |
| 149 | lock_sock(sk); |
| 150 | |
| 151 | if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) |
| 152 | || sk->sk_state != BT_BOUND) { |
| 153 | err = -EBADFD; |
| 154 | goto done; |
| 155 | } |
| 156 | |
| 157 | switch (l2cap_pi(sk)->mode) { |
| 158 | case L2CAP_MODE_BASIC: |
| 159 | break; |
| 160 | case L2CAP_MODE_ERTM: |
| 161 | case L2CAP_MODE_STREAMING: |
| 162 | if (!disable_ertm) |
| 163 | break; |
| 164 | /* fall through */ |
| 165 | default: |
| 166 | err = -ENOTSUPP; |
| 167 | goto done; |
| 168 | } |
| 169 | |
| 170 | if (!l2cap_pi(sk)->psm) { |
| 171 | bdaddr_t *src = &bt_sk(sk)->src; |
| 172 | u16 psm; |
| 173 | |
| 174 | err = -EINVAL; |
| 175 | |
| 176 | write_lock_bh(&l2cap_sk_list.lock); |
| 177 | |
| 178 | for (psm = 0x1001; psm < 0x1100; psm += 2) |
| 179 | if (!__l2cap_get_sock_by_addr(cpu_to_le16(psm), src)) { |
| 180 | l2cap_pi(sk)->psm = cpu_to_le16(psm); |
| 181 | l2cap_pi(sk)->sport = cpu_to_le16(psm); |
| 182 | err = 0; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | write_unlock_bh(&l2cap_sk_list.lock); |
| 187 | |
| 188 | if (err < 0) |
| 189 | goto done; |
| 190 | } |
| 191 | |
| 192 | sk->sk_max_ack_backlog = backlog; |
| 193 | sk->sk_ack_backlog = 0; |
| 194 | sk->sk_state = BT_LISTEN; |
| 195 | |
| 196 | done: |
| 197 | release_sock(sk); |
| 198 | return err; |
| 199 | } |
| 200 | |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 201 | static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags) |
| 202 | { |
| 203 | DECLARE_WAITQUEUE(wait, current); |
| 204 | struct sock *sk = sock->sk, *nsk; |
| 205 | long timeo; |
| 206 | int err = 0; |
| 207 | |
| 208 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 209 | |
| 210 | if (sk->sk_state != BT_LISTEN) { |
| 211 | err = -EBADFD; |
| 212 | goto done; |
| 213 | } |
| 214 | |
| 215 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 216 | |
| 217 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 218 | |
| 219 | /* Wait for an incoming connection. (wake-one). */ |
| 220 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 221 | while (!(nsk = bt_accept_dequeue(sk, newsock))) { |
| 222 | set_current_state(TASK_INTERRUPTIBLE); |
| 223 | if (!timeo) { |
| 224 | err = -EAGAIN; |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | release_sock(sk); |
| 229 | timeo = schedule_timeout(timeo); |
| 230 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 231 | |
| 232 | if (sk->sk_state != BT_LISTEN) { |
| 233 | err = -EBADFD; |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | if (signal_pending(current)) { |
| 238 | err = sock_intr_errno(timeo); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | set_current_state(TASK_RUNNING); |
| 243 | remove_wait_queue(sk_sleep(sk), &wait); |
| 244 | |
| 245 | if (err) |
| 246 | goto done; |
| 247 | |
| 248 | newsock->state = SS_CONNECTED; |
| 249 | |
| 250 | BT_DBG("new socket %p", nsk); |
| 251 | |
| 252 | done: |
| 253 | release_sock(sk); |
| 254 | return err; |
| 255 | } |
| 256 | |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 257 | static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer) |
| 258 | { |
| 259 | struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr; |
| 260 | struct sock *sk = sock->sk; |
| 261 | |
| 262 | BT_DBG("sock %p, sk %p", sock, sk); |
| 263 | |
| 264 | addr->sa_family = AF_BLUETOOTH; |
| 265 | *len = sizeof(struct sockaddr_l2); |
| 266 | |
| 267 | if (peer) { |
| 268 | la->l2_psm = l2cap_pi(sk)->psm; |
| 269 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst); |
| 270 | la->l2_cid = cpu_to_le16(l2cap_pi(sk)->dcid); |
| 271 | } else { |
| 272 | la->l2_psm = l2cap_pi(sk)->sport; |
| 273 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->src); |
| 274 | la->l2_cid = cpu_to_le16(l2cap_pi(sk)->scid); |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame^] | 280 | static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) |
| 281 | { |
| 282 | struct sock *sk = sock->sk; |
| 283 | struct l2cap_options opts; |
| 284 | struct l2cap_conninfo cinfo; |
| 285 | int len, err = 0; |
| 286 | u32 opt; |
| 287 | |
| 288 | BT_DBG("sk %p", sk); |
| 289 | |
| 290 | if (get_user(len, optlen)) |
| 291 | return -EFAULT; |
| 292 | |
| 293 | lock_sock(sk); |
| 294 | |
| 295 | switch (optname) { |
| 296 | case L2CAP_OPTIONS: |
| 297 | opts.imtu = l2cap_pi(sk)->imtu; |
| 298 | opts.omtu = l2cap_pi(sk)->omtu; |
| 299 | opts.flush_to = l2cap_pi(sk)->flush_to; |
| 300 | opts.mode = l2cap_pi(sk)->mode; |
| 301 | opts.fcs = l2cap_pi(sk)->fcs; |
| 302 | opts.max_tx = l2cap_pi(sk)->max_tx; |
| 303 | opts.txwin_size = (__u16)l2cap_pi(sk)->tx_win; |
| 304 | |
| 305 | len = min_t(unsigned int, len, sizeof(opts)); |
| 306 | if (copy_to_user(optval, (char *) &opts, len)) |
| 307 | err = -EFAULT; |
| 308 | |
| 309 | break; |
| 310 | |
| 311 | case L2CAP_LM: |
| 312 | switch (l2cap_pi(sk)->sec_level) { |
| 313 | case BT_SECURITY_LOW: |
| 314 | opt = L2CAP_LM_AUTH; |
| 315 | break; |
| 316 | case BT_SECURITY_MEDIUM: |
| 317 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT; |
| 318 | break; |
| 319 | case BT_SECURITY_HIGH: |
| 320 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | |
| 321 | L2CAP_LM_SECURE; |
| 322 | break; |
| 323 | default: |
| 324 | opt = 0; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | if (l2cap_pi(sk)->role_switch) |
| 329 | opt |= L2CAP_LM_MASTER; |
| 330 | |
| 331 | if (l2cap_pi(sk)->force_reliable) |
| 332 | opt |= L2CAP_LM_RELIABLE; |
| 333 | |
| 334 | if (put_user(opt, (u32 __user *) optval)) |
| 335 | err = -EFAULT; |
| 336 | break; |
| 337 | |
| 338 | case L2CAP_CONNINFO: |
| 339 | if (sk->sk_state != BT_CONNECTED && |
| 340 | !(sk->sk_state == BT_CONNECT2 && |
| 341 | bt_sk(sk)->defer_setup)) { |
| 342 | err = -ENOTCONN; |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle; |
| 347 | memcpy(cinfo.dev_class, l2cap_pi(sk)->conn->hcon->dev_class, 3); |
| 348 | |
| 349 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 350 | if (copy_to_user(optval, (char *) &cinfo, len)) |
| 351 | err = -EFAULT; |
| 352 | |
| 353 | break; |
| 354 | |
| 355 | default: |
| 356 | err = -ENOPROTOOPT; |
| 357 | break; |
| 358 | } |
| 359 | |
| 360 | release_sock(sk); |
| 361 | return err; |
| 362 | } |
| 363 | |
| 364 | static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 365 | { |
| 366 | struct sock *sk = sock->sk; |
| 367 | struct bt_security sec; |
| 368 | int len, err = 0; |
| 369 | |
| 370 | BT_DBG("sk %p", sk); |
| 371 | |
| 372 | if (level == SOL_L2CAP) |
| 373 | return l2cap_sock_getsockopt_old(sock, optname, optval, optlen); |
| 374 | |
| 375 | if (level != SOL_BLUETOOTH) |
| 376 | return -ENOPROTOOPT; |
| 377 | |
| 378 | if (get_user(len, optlen)) |
| 379 | return -EFAULT; |
| 380 | |
| 381 | lock_sock(sk); |
| 382 | |
| 383 | switch (optname) { |
| 384 | case BT_SECURITY: |
| 385 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM |
| 386 | && sk->sk_type != SOCK_RAW) { |
| 387 | err = -EINVAL; |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | sec.level = l2cap_pi(sk)->sec_level; |
| 392 | |
| 393 | len = min_t(unsigned int, len, sizeof(sec)); |
| 394 | if (copy_to_user(optval, (char *) &sec, len)) |
| 395 | err = -EFAULT; |
| 396 | |
| 397 | break; |
| 398 | |
| 399 | case BT_DEFER_SETUP: |
| 400 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 401 | err = -EINVAL; |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval)) |
| 406 | err = -EFAULT; |
| 407 | |
| 408 | break; |
| 409 | |
| 410 | case BT_FLUSHABLE: |
| 411 | if (put_user(l2cap_pi(sk)->flushable, (u32 __user *) optval)) |
| 412 | err = -EFAULT; |
| 413 | |
| 414 | break; |
| 415 | |
| 416 | default: |
| 417 | err = -ENOPROTOOPT; |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | release_sock(sk); |
| 422 | return err; |
| 423 | } |
| 424 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 425 | static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen) |
| 426 | { |
| 427 | struct sock *sk = sock->sk; |
| 428 | struct l2cap_options opts; |
| 429 | int len, err = 0; |
| 430 | u32 opt; |
| 431 | |
| 432 | BT_DBG("sk %p", sk); |
| 433 | |
| 434 | lock_sock(sk); |
| 435 | |
| 436 | switch (optname) { |
| 437 | case L2CAP_OPTIONS: |
| 438 | if (sk->sk_state == BT_CONNECTED) { |
| 439 | err = -EINVAL; |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | opts.imtu = l2cap_pi(sk)->imtu; |
| 444 | opts.omtu = l2cap_pi(sk)->omtu; |
| 445 | opts.flush_to = l2cap_pi(sk)->flush_to; |
| 446 | opts.mode = l2cap_pi(sk)->mode; |
| 447 | opts.fcs = l2cap_pi(sk)->fcs; |
| 448 | opts.max_tx = l2cap_pi(sk)->max_tx; |
| 449 | opts.txwin_size = (__u16)l2cap_pi(sk)->tx_win; |
| 450 | |
| 451 | len = min_t(unsigned int, sizeof(opts), optlen); |
| 452 | if (copy_from_user((char *) &opts, optval, len)) { |
| 453 | err = -EFAULT; |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) { |
| 458 | err = -EINVAL; |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | l2cap_pi(sk)->mode = opts.mode; |
| 463 | switch (l2cap_pi(sk)->mode) { |
| 464 | case L2CAP_MODE_BASIC: |
| 465 | l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_STATE2_DEVICE; |
| 466 | break; |
| 467 | case L2CAP_MODE_ERTM: |
| 468 | case L2CAP_MODE_STREAMING: |
| 469 | if (!disable_ertm) |
| 470 | break; |
| 471 | /* fall through */ |
| 472 | default: |
| 473 | err = -EINVAL; |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | l2cap_pi(sk)->imtu = opts.imtu; |
| 478 | l2cap_pi(sk)->omtu = opts.omtu; |
| 479 | l2cap_pi(sk)->fcs = opts.fcs; |
| 480 | l2cap_pi(sk)->max_tx = opts.max_tx; |
| 481 | l2cap_pi(sk)->tx_win = (__u8)opts.txwin_size; |
| 482 | break; |
| 483 | |
| 484 | case L2CAP_LM: |
| 485 | if (get_user(opt, (u32 __user *) optval)) { |
| 486 | err = -EFAULT; |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | if (opt & L2CAP_LM_AUTH) |
| 491 | l2cap_pi(sk)->sec_level = BT_SECURITY_LOW; |
| 492 | if (opt & L2CAP_LM_ENCRYPT) |
| 493 | l2cap_pi(sk)->sec_level = BT_SECURITY_MEDIUM; |
| 494 | if (opt & L2CAP_LM_SECURE) |
| 495 | l2cap_pi(sk)->sec_level = BT_SECURITY_HIGH; |
| 496 | |
| 497 | l2cap_pi(sk)->role_switch = (opt & L2CAP_LM_MASTER); |
| 498 | l2cap_pi(sk)->force_reliable = (opt & L2CAP_LM_RELIABLE); |
| 499 | break; |
| 500 | |
| 501 | default: |
| 502 | err = -ENOPROTOOPT; |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | release_sock(sk); |
| 507 | return err; |
| 508 | } |
| 509 | |
| 510 | static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
| 511 | { |
| 512 | struct sock *sk = sock->sk; |
| 513 | struct bt_security sec; |
| 514 | int len, err = 0; |
| 515 | u32 opt; |
| 516 | |
| 517 | BT_DBG("sk %p", sk); |
| 518 | |
| 519 | if (level == SOL_L2CAP) |
| 520 | return l2cap_sock_setsockopt_old(sock, optname, optval, optlen); |
| 521 | |
| 522 | if (level != SOL_BLUETOOTH) |
| 523 | return -ENOPROTOOPT; |
| 524 | |
| 525 | lock_sock(sk); |
| 526 | |
| 527 | switch (optname) { |
| 528 | case BT_SECURITY: |
| 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 | |
| 535 | sec.level = BT_SECURITY_LOW; |
| 536 | |
| 537 | len = min_t(unsigned int, sizeof(sec), optlen); |
| 538 | if (copy_from_user((char *) &sec, optval, len)) { |
| 539 | err = -EFAULT; |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | if (sec.level < BT_SECURITY_LOW || |
| 544 | sec.level > BT_SECURITY_HIGH) { |
| 545 | err = -EINVAL; |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | l2cap_pi(sk)->sec_level = sec.level; |
| 550 | break; |
| 551 | |
| 552 | case BT_DEFER_SETUP: |
| 553 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 554 | err = -EINVAL; |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | if (get_user(opt, (u32 __user *) optval)) { |
| 559 | err = -EFAULT; |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | bt_sk(sk)->defer_setup = opt; |
| 564 | break; |
| 565 | |
| 566 | case BT_FLUSHABLE: |
| 567 | if (get_user(opt, (u32 __user *) optval)) { |
| 568 | err = -EFAULT; |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | if (opt > BT_FLUSHABLE_ON) { |
| 573 | err = -EINVAL; |
| 574 | break; |
| 575 | } |
| 576 | |
| 577 | if (opt == BT_FLUSHABLE_OFF) { |
| 578 | struct l2cap_conn *conn = l2cap_pi(sk)->conn; |
| 579 | /* proceed futher only when we have l2cap_conn and |
| 580 | No Flush support in the LM */ |
| 581 | if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) { |
| 582 | err = -EINVAL; |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | l2cap_pi(sk)->flushable = opt; |
| 588 | break; |
| 589 | |
| 590 | default: |
| 591 | err = -ENOPROTOOPT; |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | release_sock(sk); |
| 596 | return err; |
| 597 | } |
| 598 | |
Gustavo F. Padovan | 554f05b | 2011-02-04 02:36:42 -0200 | [diff] [blame] | 599 | static int l2cap_sock_release(struct socket *sock) |
| 600 | { |
| 601 | struct sock *sk = sock->sk; |
| 602 | int err; |
| 603 | |
| 604 | BT_DBG("sock %p, sk %p", sock, sk); |
| 605 | |
| 606 | if (!sk) |
| 607 | return 0; |
| 608 | |
| 609 | err = l2cap_sock_shutdown(sock, 2); |
| 610 | |
| 611 | sock_orphan(sk); |
| 612 | l2cap_sock_kill(sk); |
| 613 | return err; |
| 614 | } |
| 615 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 616 | static void l2cap_sock_destruct(struct sock *sk) |
| 617 | { |
| 618 | BT_DBG("sk %p", sk); |
| 619 | |
| 620 | skb_queue_purge(&sk->sk_receive_queue); |
| 621 | skb_queue_purge(&sk->sk_write_queue); |
| 622 | } |
| 623 | |
| 624 | void l2cap_sock_init(struct sock *sk, struct sock *parent) |
| 625 | { |
| 626 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
| 627 | |
| 628 | BT_DBG("sk %p", sk); |
| 629 | |
| 630 | if (parent) { |
| 631 | sk->sk_type = parent->sk_type; |
| 632 | bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup; |
| 633 | |
| 634 | pi->imtu = l2cap_pi(parent)->imtu; |
| 635 | pi->omtu = l2cap_pi(parent)->omtu; |
| 636 | pi->conf_state = l2cap_pi(parent)->conf_state; |
| 637 | pi->mode = l2cap_pi(parent)->mode; |
| 638 | pi->fcs = l2cap_pi(parent)->fcs; |
| 639 | pi->max_tx = l2cap_pi(parent)->max_tx; |
| 640 | pi->tx_win = l2cap_pi(parent)->tx_win; |
| 641 | pi->sec_level = l2cap_pi(parent)->sec_level; |
| 642 | pi->role_switch = l2cap_pi(parent)->role_switch; |
| 643 | pi->force_reliable = l2cap_pi(parent)->force_reliable; |
| 644 | pi->flushable = l2cap_pi(parent)->flushable; |
| 645 | } else { |
| 646 | pi->imtu = L2CAP_DEFAULT_MTU; |
| 647 | pi->omtu = 0; |
| 648 | if (!disable_ertm && sk->sk_type == SOCK_STREAM) { |
| 649 | pi->mode = L2CAP_MODE_ERTM; |
| 650 | pi->conf_state |= L2CAP_CONF_STATE2_DEVICE; |
| 651 | } else { |
| 652 | pi->mode = L2CAP_MODE_BASIC; |
| 653 | } |
| 654 | pi->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 655 | pi->fcs = L2CAP_FCS_CRC16; |
| 656 | pi->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 657 | pi->sec_level = BT_SECURITY_LOW; |
| 658 | pi->role_switch = 0; |
| 659 | pi->force_reliable = 0; |
| 660 | pi->flushable = BT_FLUSHABLE_OFF; |
| 661 | } |
| 662 | |
| 663 | /* Default config options */ |
| 664 | pi->conf_len = 0; |
| 665 | pi->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 666 | skb_queue_head_init(TX_QUEUE(sk)); |
| 667 | skb_queue_head_init(SREJ_QUEUE(sk)); |
| 668 | skb_queue_head_init(BUSY_QUEUE(sk)); |
| 669 | INIT_LIST_HEAD(SREJ_LIST(sk)); |
| 670 | } |
| 671 | |
| 672 | static struct proto l2cap_proto = { |
| 673 | .name = "L2CAP", |
| 674 | .owner = THIS_MODULE, |
| 675 | .obj_size = sizeof(struct l2cap_pinfo) |
| 676 | }; |
| 677 | |
| 678 | struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio) |
| 679 | { |
| 680 | struct sock *sk; |
| 681 | |
| 682 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto); |
| 683 | if (!sk) |
| 684 | return NULL; |
| 685 | |
| 686 | sock_init_data(sock, sk); |
| 687 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 688 | |
| 689 | sk->sk_destruct = l2cap_sock_destruct; |
| 690 | sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT); |
| 691 | |
| 692 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 693 | |
| 694 | sk->sk_protocol = proto; |
| 695 | sk->sk_state = BT_OPEN; |
| 696 | |
| 697 | setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk); |
| 698 | |
| 699 | bt_sock_link(&l2cap_sk_list, sk); |
| 700 | return sk; |
| 701 | } |
| 702 | |
| 703 | static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, |
| 704 | int kern) |
| 705 | { |
| 706 | struct sock *sk; |
| 707 | |
| 708 | BT_DBG("sock %p", sock); |
| 709 | |
| 710 | sock->state = SS_UNCONNECTED; |
| 711 | |
| 712 | if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM && |
| 713 | sock->type != SOCK_DGRAM && sock->type != SOCK_RAW) |
| 714 | return -ESOCKTNOSUPPORT; |
| 715 | |
| 716 | if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) |
| 717 | return -EPERM; |
| 718 | |
| 719 | sock->ops = &l2cap_sock_ops; |
| 720 | |
| 721 | sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
| 722 | if (!sk) |
| 723 | return -ENOMEM; |
| 724 | |
| 725 | l2cap_sock_init(sk, NULL); |
| 726 | return 0; |
| 727 | } |
| 728 | |
Gustavo F. Padovan | 6539058 | 2011-02-04 02:33:56 -0200 | [diff] [blame] | 729 | const struct proto_ops l2cap_sock_ops = { |
| 730 | .family = PF_BLUETOOTH, |
| 731 | .owner = THIS_MODULE, |
| 732 | .release = l2cap_sock_release, |
| 733 | .bind = l2cap_sock_bind, |
| 734 | .connect = l2cap_sock_connect, |
| 735 | .listen = l2cap_sock_listen, |
| 736 | .accept = l2cap_sock_accept, |
| 737 | .getname = l2cap_sock_getname, |
| 738 | .sendmsg = l2cap_sock_sendmsg, |
| 739 | .recvmsg = l2cap_sock_recvmsg, |
| 740 | .poll = bt_sock_poll, |
| 741 | .ioctl = bt_sock_ioctl, |
| 742 | .mmap = sock_no_mmap, |
| 743 | .socketpair = sock_no_socketpair, |
| 744 | .shutdown = l2cap_sock_shutdown, |
| 745 | .setsockopt = l2cap_sock_setsockopt, |
| 746 | .getsockopt = l2cap_sock_getsockopt |
| 747 | }; |
| 748 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 749 | static const struct net_proto_family l2cap_sock_family_ops = { |
| 750 | .family = PF_BLUETOOTH, |
| 751 | .owner = THIS_MODULE, |
| 752 | .create = l2cap_sock_create, |
| 753 | }; |
| 754 | |
| 755 | int __init l2cap_init_sockets(void) |
| 756 | { |
| 757 | int err; |
| 758 | |
| 759 | err = proto_register(&l2cap_proto, 0); |
| 760 | if (err < 0) |
| 761 | return err; |
| 762 | |
| 763 | err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops); |
| 764 | if (err < 0) |
| 765 | goto error; |
| 766 | |
| 767 | BT_INFO("L2CAP socket layer initialized"); |
| 768 | |
| 769 | return 0; |
| 770 | |
| 771 | error: |
| 772 | BT_ERR("L2CAP socket registration failed"); |
| 773 | proto_unregister(&l2cap_proto); |
| 774 | return err; |
| 775 | } |
| 776 | |
| 777 | void l2cap_cleanup_sockets(void) |
| 778 | { |
| 779 | if (bt_sock_unregister(BTPROTO_L2CAP) < 0) |
| 780 | BT_ERR("L2CAP socket unregistration failed"); |
| 781 | |
| 782 | proto_unregister(&l2cap_proto); |
| 783 | } |