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 | |
Gustavo F. Padovan | cf2f90f | 2011-04-27 18:40:39 -0300 | [diff] [blame] | 33 | static const struct proto_ops l2cap_sock_ops; |
| 34 | |
Gustavo F. Padovan | 6ddc048 | 2011-02-04 03:23:31 -0200 | [diff] [blame] | 35 | /* ---- L2CAP timers ---- */ |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 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 && |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 56 | l2cap_pi(sk)->chan->sec_level != BT_SECURITY_SDP) |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 57 | reason = ECONNREFUSED; |
| 58 | else |
| 59 | reason = ETIMEDOUT; |
| 60 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 61 | __l2cap_chan_close(l2cap_pi(sk)->chan, reason); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 62 | |
| 63 | bh_unlock_sock(sk); |
| 64 | |
| 65 | l2cap_sock_kill(sk); |
| 66 | sock_put(sk); |
| 67 | } |
| 68 | |
Gustavo F. Padovan | 6ddc048 | 2011-02-04 03:23:31 -0200 | [diff] [blame] | 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 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 81 | static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) |
| 82 | { |
| 83 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 84 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 85 | struct sockaddr_l2 la; |
| 86 | int len, err = 0; |
| 87 | |
| 88 | BT_DBG("sk %p", sk); |
| 89 | |
| 90 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | memset(&la, 0, sizeof(la)); |
| 94 | len = min_t(unsigned int, sizeof(la), alen); |
| 95 | memcpy(&la, addr, len); |
| 96 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 97 | if (la.l2_cid && la.l2_psm) |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 98 | return -EINVAL; |
| 99 | |
| 100 | lock_sock(sk); |
| 101 | |
| 102 | if (sk->sk_state != BT_OPEN) { |
| 103 | err = -EBADFD; |
| 104 | goto done; |
| 105 | } |
| 106 | |
| 107 | if (la.l2_psm) { |
| 108 | __u16 psm = __le16_to_cpu(la.l2_psm); |
| 109 | |
| 110 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 111 | if ((psm & 0x0101) != 0x0001) { |
| 112 | err = -EINVAL; |
| 113 | goto done; |
| 114 | } |
| 115 | |
| 116 | /* Restrict usage of well-known PSMs */ |
| 117 | if (psm < 0x1001 && !capable(CAP_NET_BIND_SERVICE)) { |
| 118 | err = -EACCES; |
| 119 | goto done; |
| 120 | } |
| 121 | } |
| 122 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 123 | if (la.l2_cid) |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 124 | err = l2cap_add_scid(chan, la.l2_cid); |
| 125 | else |
| 126 | err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 127 | |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 128 | if (err < 0) |
| 129 | goto done; |
| 130 | |
| 131 | if (__le16_to_cpu(la.l2_psm) == 0x0001 || |
| 132 | __le16_to_cpu(la.l2_psm) == 0x0003) |
| 133 | chan->sec_level = BT_SECURITY_SDP; |
| 134 | |
| 135 | bacpy(&bt_sk(sk)->src, &la.l2_bdaddr); |
| 136 | sk->sk_state = BT_BOUND; |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 137 | |
| 138 | done: |
| 139 | release_sock(sk); |
| 140 | return err; |
| 141 | } |
| 142 | |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 143 | static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 144 | { |
| 145 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 146 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 147 | struct sockaddr_l2 la; |
| 148 | int len, err = 0; |
| 149 | |
| 150 | BT_DBG("sk %p", sk); |
| 151 | |
| 152 | if (!addr || alen < sizeof(addr->sa_family) || |
| 153 | addr->sa_family != AF_BLUETOOTH) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | memset(&la, 0, sizeof(la)); |
| 157 | len = min_t(unsigned int, sizeof(la), alen); |
| 158 | memcpy(&la, addr, len); |
| 159 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 160 | if (la.l2_cid && la.l2_psm) |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 161 | return -EINVAL; |
| 162 | |
| 163 | lock_sock(sk); |
| 164 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame^] | 165 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 166 | && !(la.l2_psm || la.l2_cid)) { |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 167 | err = -EINVAL; |
| 168 | goto done; |
| 169 | } |
| 170 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 171 | switch (chan->mode) { |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 172 | case L2CAP_MODE_BASIC: |
| 173 | break; |
| 174 | case L2CAP_MODE_ERTM: |
| 175 | case L2CAP_MODE_STREAMING: |
| 176 | if (!disable_ertm) |
| 177 | break; |
| 178 | /* fall through */ |
| 179 | default: |
| 180 | err = -ENOTSUPP; |
| 181 | goto done; |
| 182 | } |
| 183 | |
| 184 | switch (sk->sk_state) { |
| 185 | case BT_CONNECT: |
| 186 | case BT_CONNECT2: |
| 187 | case BT_CONFIG: |
| 188 | /* Already connecting */ |
| 189 | goto wait; |
| 190 | |
| 191 | case BT_CONNECTED: |
| 192 | /* Already connected */ |
| 193 | err = -EISCONN; |
| 194 | goto done; |
| 195 | |
| 196 | case BT_OPEN: |
| 197 | case BT_BOUND: |
| 198 | /* Can connect */ |
| 199 | break; |
| 200 | |
| 201 | default: |
| 202 | err = -EBADFD; |
| 203 | goto done; |
| 204 | } |
| 205 | |
| 206 | /* PSM must be odd and lsb of upper byte must be 0 */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame^] | 207 | if ((__le16_to_cpu(la.l2_psm) & 0x0101) != 0x0001 && !la.l2_cid && |
| 208 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 209 | err = -EINVAL; |
| 210 | goto done; |
| 211 | } |
| 212 | |
| 213 | /* Set destination address and psm */ |
| 214 | bacpy(&bt_sk(sk)->dst, &la.l2_bdaddr); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 215 | chan->psm = la.l2_psm; |
| 216 | chan->dcid = la.l2_cid; |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 217 | |
Gustavo F. Padovan | 77a74c7 | 2011-04-12 18:17:14 -0300 | [diff] [blame] | 218 | err = l2cap_chan_connect(l2cap_pi(sk)->chan); |
Gustavo F. Padovan | 4e34c50 | 2011-02-04 02:56:13 -0200 | [diff] [blame] | 219 | if (err) |
| 220 | goto done; |
| 221 | |
| 222 | wait: |
| 223 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
| 224 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
| 225 | done: |
| 226 | release_sock(sk); |
| 227 | return err; |
| 228 | } |
| 229 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 230 | static int l2cap_sock_listen(struct socket *sock, int backlog) |
| 231 | { |
| 232 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 233 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 234 | int err = 0; |
| 235 | |
| 236 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 237 | |
| 238 | lock_sock(sk); |
| 239 | |
| 240 | if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) |
| 241 | || sk->sk_state != BT_BOUND) { |
| 242 | err = -EBADFD; |
| 243 | goto done; |
| 244 | } |
| 245 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 246 | switch (chan->mode) { |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 247 | case L2CAP_MODE_BASIC: |
| 248 | break; |
| 249 | case L2CAP_MODE_ERTM: |
| 250 | case L2CAP_MODE_STREAMING: |
| 251 | if (!disable_ertm) |
| 252 | break; |
| 253 | /* fall through */ |
| 254 | default: |
| 255 | err = -ENOTSUPP; |
| 256 | goto done; |
| 257 | } |
| 258 | |
Gustavo F. Padovan | af6bcd8 | 2011-02-04 02:40:28 -0200 | [diff] [blame] | 259 | sk->sk_max_ack_backlog = backlog; |
| 260 | sk->sk_ack_backlog = 0; |
| 261 | sk->sk_state = BT_LISTEN; |
| 262 | |
| 263 | done: |
| 264 | release_sock(sk); |
| 265 | return err; |
| 266 | } |
| 267 | |
Gustavo F. Padovan | c47b7c7 | 2011-02-04 02:42:23 -0200 | [diff] [blame] | 268 | static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags) |
| 269 | { |
| 270 | DECLARE_WAITQUEUE(wait, current); |
| 271 | struct sock *sk = sock->sk, *nsk; |
| 272 | long timeo; |
| 273 | int err = 0; |
| 274 | |
| 275 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 276 | |
| 277 | if (sk->sk_state != BT_LISTEN) { |
| 278 | err = -EBADFD; |
| 279 | goto done; |
| 280 | } |
| 281 | |
| 282 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 283 | |
| 284 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 285 | |
| 286 | /* Wait for an incoming connection. (wake-one). */ |
| 287 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 288 | while (!(nsk = bt_accept_dequeue(sk, newsock))) { |
| 289 | set_current_state(TASK_INTERRUPTIBLE); |
| 290 | if (!timeo) { |
| 291 | err = -EAGAIN; |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | release_sock(sk); |
| 296 | timeo = schedule_timeout(timeo); |
| 297 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 298 | |
| 299 | if (sk->sk_state != BT_LISTEN) { |
| 300 | err = -EBADFD; |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | if (signal_pending(current)) { |
| 305 | err = sock_intr_errno(timeo); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | set_current_state(TASK_RUNNING); |
| 310 | remove_wait_queue(sk_sleep(sk), &wait); |
| 311 | |
| 312 | if (err) |
| 313 | goto done; |
| 314 | |
| 315 | newsock->state = SS_CONNECTED; |
| 316 | |
| 317 | BT_DBG("new socket %p", nsk); |
| 318 | |
| 319 | done: |
| 320 | release_sock(sk); |
| 321 | return err; |
| 322 | } |
| 323 | |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 324 | static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer) |
| 325 | { |
| 326 | struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr; |
| 327 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 328 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 329 | |
| 330 | BT_DBG("sock %p, sk %p", sock, sk); |
| 331 | |
| 332 | addr->sa_family = AF_BLUETOOTH; |
| 333 | *len = sizeof(struct sockaddr_l2); |
| 334 | |
| 335 | if (peer) { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 336 | la->l2_psm = chan->psm; |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 337 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 338 | la->l2_cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 339 | } else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 340 | la->l2_psm = chan->sport; |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 341 | bacpy(&la->l2_bdaddr, &bt_sk(sk)->src); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 342 | la->l2_cid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | d7175d5 | 2011-02-04 02:43:46 -0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 348 | static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) |
| 349 | { |
| 350 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 351 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 352 | struct l2cap_options opts; |
| 353 | struct l2cap_conninfo cinfo; |
| 354 | int len, err = 0; |
| 355 | u32 opt; |
| 356 | |
| 357 | BT_DBG("sk %p", sk); |
| 358 | |
| 359 | if (get_user(len, optlen)) |
| 360 | return -EFAULT; |
| 361 | |
| 362 | lock_sock(sk); |
| 363 | |
| 364 | switch (optname) { |
| 365 | case L2CAP_OPTIONS: |
Vasiliy Kulikov | e3fb592 | 2011-02-10 20:59:42 +0300 | [diff] [blame] | 366 | memset(&opts, 0, sizeof(opts)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 367 | opts.imtu = chan->imtu; |
| 368 | opts.omtu = chan->omtu; |
| 369 | opts.flush_to = chan->flush_to; |
| 370 | opts.mode = chan->mode; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 371 | opts.fcs = chan->fcs; |
| 372 | opts.max_tx = chan->max_tx; |
| 373 | opts.txwin_size = (__u16)chan->tx_win; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 374 | |
| 375 | len = min_t(unsigned int, len, sizeof(opts)); |
| 376 | if (copy_to_user(optval, (char *) &opts, len)) |
| 377 | err = -EFAULT; |
| 378 | |
| 379 | break; |
| 380 | |
| 381 | case L2CAP_LM: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 382 | switch (chan->sec_level) { |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 383 | case BT_SECURITY_LOW: |
| 384 | opt = L2CAP_LM_AUTH; |
| 385 | break; |
| 386 | case BT_SECURITY_MEDIUM: |
| 387 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT; |
| 388 | break; |
| 389 | case BT_SECURITY_HIGH: |
| 390 | opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | |
| 391 | L2CAP_LM_SECURE; |
| 392 | break; |
| 393 | default: |
| 394 | opt = 0; |
| 395 | break; |
| 396 | } |
| 397 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 398 | if (chan->role_switch) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 399 | opt |= L2CAP_LM_MASTER; |
| 400 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 401 | if (chan->force_reliable) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 402 | opt |= L2CAP_LM_RELIABLE; |
| 403 | |
| 404 | if (put_user(opt, (u32 __user *) optval)) |
| 405 | err = -EFAULT; |
| 406 | break; |
| 407 | |
| 408 | case L2CAP_CONNINFO: |
| 409 | if (sk->sk_state != BT_CONNECTED && |
| 410 | !(sk->sk_state == BT_CONNECT2 && |
| 411 | bt_sk(sk)->defer_setup)) { |
| 412 | err = -ENOTCONN; |
| 413 | break; |
| 414 | } |
| 415 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 416 | cinfo.hci_handle = chan->conn->hcon->handle; |
| 417 | memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3); |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 418 | |
| 419 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 420 | if (copy_to_user(optval, (char *) &cinfo, len)) |
| 421 | err = -EFAULT; |
| 422 | |
| 423 | break; |
| 424 | |
| 425 | default: |
| 426 | err = -ENOPROTOOPT; |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | release_sock(sk); |
| 431 | return err; |
| 432 | } |
| 433 | |
| 434 | static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 435 | { |
| 436 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 437 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 438 | struct bt_security sec; |
| 439 | int len, err = 0; |
| 440 | |
| 441 | BT_DBG("sk %p", sk); |
| 442 | |
| 443 | if (level == SOL_L2CAP) |
| 444 | return l2cap_sock_getsockopt_old(sock, optname, optval, optlen); |
| 445 | |
| 446 | if (level != SOL_BLUETOOTH) |
| 447 | return -ENOPROTOOPT; |
| 448 | |
| 449 | if (get_user(len, optlen)) |
| 450 | return -EFAULT; |
| 451 | |
| 452 | lock_sock(sk); |
| 453 | |
| 454 | switch (optname) { |
| 455 | case BT_SECURITY: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame^] | 456 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && |
| 457 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 458 | err = -EINVAL; |
| 459 | break; |
| 460 | } |
| 461 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 462 | sec.level = chan->sec_level; |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 463 | |
| 464 | len = min_t(unsigned int, len, sizeof(sec)); |
| 465 | if (copy_to_user(optval, (char *) &sec, len)) |
| 466 | err = -EFAULT; |
| 467 | |
| 468 | break; |
| 469 | |
| 470 | case BT_DEFER_SETUP: |
| 471 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 472 | err = -EINVAL; |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | if (put_user(bt_sk(sk)->defer_setup, (u32 __user *) optval)) |
| 477 | err = -EFAULT; |
| 478 | |
| 479 | break; |
| 480 | |
| 481 | case BT_FLUSHABLE: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 482 | if (put_user(chan->flushable, (u32 __user *) optval)) |
Gustavo F. Padovan | 99f4808 | 2011-02-04 02:52:55 -0200 | [diff] [blame] | 483 | err = -EFAULT; |
| 484 | |
| 485 | break; |
| 486 | |
| 487 | default: |
| 488 | err = -ENOPROTOOPT; |
| 489 | break; |
| 490 | } |
| 491 | |
| 492 | release_sock(sk); |
| 493 | return err; |
| 494 | } |
| 495 | |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 496 | static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, char __user *optval, unsigned int optlen) |
| 497 | { |
| 498 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 499 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 500 | struct l2cap_options opts; |
| 501 | int len, err = 0; |
| 502 | u32 opt; |
| 503 | |
| 504 | BT_DBG("sk %p", sk); |
| 505 | |
| 506 | lock_sock(sk); |
| 507 | |
| 508 | switch (optname) { |
| 509 | case L2CAP_OPTIONS: |
| 510 | if (sk->sk_state == BT_CONNECTED) { |
| 511 | err = -EINVAL; |
| 512 | break; |
| 513 | } |
| 514 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 515 | opts.imtu = chan->imtu; |
| 516 | opts.omtu = chan->omtu; |
| 517 | opts.flush_to = chan->flush_to; |
| 518 | opts.mode = chan->mode; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 519 | opts.fcs = chan->fcs; |
| 520 | opts.max_tx = chan->max_tx; |
| 521 | opts.txwin_size = (__u16)chan->tx_win; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 522 | |
| 523 | len = min_t(unsigned int, sizeof(opts), optlen); |
| 524 | if (copy_from_user((char *) &opts, optval, len)) { |
| 525 | err = -EFAULT; |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | if (opts.txwin_size > L2CAP_DEFAULT_TX_WINDOW) { |
| 530 | err = -EINVAL; |
| 531 | break; |
| 532 | } |
| 533 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 534 | chan->mode = opts.mode; |
| 535 | switch (chan->mode) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 536 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 537 | chan->conf_state &= ~L2CAP_CONF_STATE2_DEVICE; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 538 | break; |
| 539 | case L2CAP_MODE_ERTM: |
| 540 | case L2CAP_MODE_STREAMING: |
| 541 | if (!disable_ertm) |
| 542 | break; |
| 543 | /* fall through */ |
| 544 | default: |
| 545 | err = -EINVAL; |
| 546 | break; |
| 547 | } |
| 548 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 549 | chan->imtu = opts.imtu; |
| 550 | chan->omtu = opts.omtu; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 551 | chan->fcs = opts.fcs; |
| 552 | chan->max_tx = opts.max_tx; |
| 553 | chan->tx_win = (__u8)opts.txwin_size; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 554 | break; |
| 555 | |
| 556 | case L2CAP_LM: |
| 557 | if (get_user(opt, (u32 __user *) optval)) { |
| 558 | err = -EFAULT; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | if (opt & L2CAP_LM_AUTH) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 563 | chan->sec_level = BT_SECURITY_LOW; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 564 | if (opt & L2CAP_LM_ENCRYPT) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 565 | chan->sec_level = BT_SECURITY_MEDIUM; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 566 | if (opt & L2CAP_LM_SECURE) |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 567 | chan->sec_level = BT_SECURITY_HIGH; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 568 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 569 | chan->role_switch = (opt & L2CAP_LM_MASTER); |
| 570 | chan->force_reliable = (opt & L2CAP_LM_RELIABLE); |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 571 | break; |
| 572 | |
| 573 | default: |
| 574 | err = -ENOPROTOOPT; |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | release_sock(sk); |
| 579 | return err; |
| 580 | } |
| 581 | |
| 582 | static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
| 583 | { |
| 584 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 585 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 586 | struct bt_security sec; |
| 587 | int len, err = 0; |
| 588 | u32 opt; |
| 589 | |
| 590 | BT_DBG("sk %p", sk); |
| 591 | |
| 592 | if (level == SOL_L2CAP) |
| 593 | return l2cap_sock_setsockopt_old(sock, optname, optval, optlen); |
| 594 | |
| 595 | if (level != SOL_BLUETOOTH) |
| 596 | return -ENOPROTOOPT; |
| 597 | |
| 598 | lock_sock(sk); |
| 599 | |
| 600 | switch (optname) { |
| 601 | case BT_SECURITY: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame^] | 602 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && |
| 603 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 604 | err = -EINVAL; |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | sec.level = BT_SECURITY_LOW; |
| 609 | |
| 610 | len = min_t(unsigned int, sizeof(sec), optlen); |
| 611 | if (copy_from_user((char *) &sec, optval, len)) { |
| 612 | err = -EFAULT; |
| 613 | break; |
| 614 | } |
| 615 | |
| 616 | if (sec.level < BT_SECURITY_LOW || |
| 617 | sec.level > BT_SECURITY_HIGH) { |
| 618 | err = -EINVAL; |
| 619 | break; |
| 620 | } |
| 621 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 622 | chan->sec_level = sec.level; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 623 | break; |
| 624 | |
| 625 | case BT_DEFER_SETUP: |
| 626 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 627 | err = -EINVAL; |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | if (get_user(opt, (u32 __user *) optval)) { |
| 632 | err = -EFAULT; |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | bt_sk(sk)->defer_setup = opt; |
| 637 | break; |
| 638 | |
| 639 | case BT_FLUSHABLE: |
| 640 | if (get_user(opt, (u32 __user *) optval)) { |
| 641 | err = -EFAULT; |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | if (opt > BT_FLUSHABLE_ON) { |
| 646 | err = -EINVAL; |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | if (opt == BT_FLUSHABLE_OFF) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 651 | struct l2cap_conn *conn = chan->conn; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 652 | /* proceed further only when we have l2cap_conn and |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 653 | No Flush support in the LM */ |
| 654 | if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) { |
| 655 | err = -EINVAL; |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 660 | chan->flushable = opt; |
Gustavo F. Padovan | 33575df | 2011-02-04 02:48:48 -0200 | [diff] [blame] | 661 | break; |
| 662 | |
| 663 | default: |
| 664 | err = -ENOPROTOOPT; |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | release_sock(sk); |
| 669 | return err; |
| 670 | } |
| 671 | |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 672 | static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len) |
| 673 | { |
| 674 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 675 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 676 | int err; |
| 677 | |
| 678 | BT_DBG("sock %p, sk %p", sock, sk); |
| 679 | |
| 680 | err = sock_error(sk); |
| 681 | if (err) |
| 682 | return err; |
| 683 | |
| 684 | if (msg->msg_flags & MSG_OOB) |
| 685 | return -EOPNOTSUPP; |
| 686 | |
| 687 | lock_sock(sk); |
| 688 | |
| 689 | if (sk->sk_state != BT_CONNECTED) { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 690 | release_sock(sk); |
| 691 | return -ENOTCONN; |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 692 | } |
| 693 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 694 | err = l2cap_chan_send(chan, msg, len); |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 695 | |
Gustavo F. Padovan | fd83ccd | 2011-02-04 03:20:52 -0200 | [diff] [blame] | 696 | release_sock(sk); |
| 697 | return err; |
| 698 | } |
| 699 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 700 | static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len, int flags) |
| 701 | { |
| 702 | struct sock *sk = sock->sk; |
| 703 | |
| 704 | lock_sock(sk); |
| 705 | |
| 706 | if (sk->sk_state == BT_CONNECT2 && bt_sk(sk)->defer_setup) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 707 | sk->sk_state = BT_CONFIG; |
| 708 | |
| 709 | __l2cap_connect_rsp_defer(l2cap_pi(sk)->chan); |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 710 | release_sock(sk); |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | release_sock(sk); |
| 715 | |
| 716 | if (sock->type == SOCK_STREAM) |
| 717 | return bt_sock_stream_recvmsg(iocb, sock, msg, len, flags); |
| 718 | |
| 719 | return bt_sock_recvmsg(iocb, sock, msg, len, flags); |
| 720 | } |
| 721 | |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 722 | /* Kill socket (only if zapped and orphan) |
| 723 | * Must be called on unlocked socket. |
| 724 | */ |
| 725 | void l2cap_sock_kill(struct sock *sk) |
| 726 | { |
| 727 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 728 | return; |
| 729 | |
| 730 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
| 731 | |
| 732 | /* Kill poor orphan */ |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 733 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 734 | l2cap_chan_destroy(l2cap_pi(sk)->chan); |
Gustavo F. Padovan | 05fc157 | 2011-02-04 03:26:01 -0200 | [diff] [blame] | 735 | sock_set_flag(sk, SOCK_DEAD); |
| 736 | sock_put(sk); |
| 737 | } |
| 738 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 739 | static int l2cap_sock_shutdown(struct socket *sock, int how) |
| 740 | { |
| 741 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 742 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 743 | int err = 0; |
| 744 | |
| 745 | BT_DBG("sock %p, sk %p", sock, sk); |
| 746 | |
| 747 | if (!sk) |
| 748 | return 0; |
| 749 | |
| 750 | lock_sock(sk); |
| 751 | if (!sk->sk_shutdown) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 752 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 753 | err = __l2cap_wait_ack(sk); |
| 754 | |
| 755 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 756 | l2cap_sock_clear_timer(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 757 | __l2cap_chan_close(chan, 0); |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 758 | |
| 759 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 760 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 761 | sk->sk_lingertime); |
| 762 | } |
| 763 | |
| 764 | if (!err && sk->sk_err) |
| 765 | err = -sk->sk_err; |
| 766 | |
| 767 | release_sock(sk); |
| 768 | return err; |
| 769 | } |
| 770 | |
Gustavo F. Padovan | 554f05b | 2011-02-04 02:36:42 -0200 | [diff] [blame] | 771 | static int l2cap_sock_release(struct socket *sock) |
| 772 | { |
| 773 | struct sock *sk = sock->sk; |
| 774 | int err; |
| 775 | |
| 776 | BT_DBG("sock %p, sk %p", sock, sk); |
| 777 | |
| 778 | if (!sk) |
| 779 | return 0; |
| 780 | |
| 781 | err = l2cap_sock_shutdown(sock, 2); |
| 782 | |
| 783 | sock_orphan(sk); |
| 784 | l2cap_sock_kill(sk); |
| 785 | return err; |
| 786 | } |
| 787 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 788 | static void l2cap_sock_destruct(struct sock *sk) |
| 789 | { |
| 790 | BT_DBG("sk %p", sk); |
| 791 | |
| 792 | skb_queue_purge(&sk->sk_receive_queue); |
| 793 | skb_queue_purge(&sk->sk_write_queue); |
| 794 | } |
| 795 | |
| 796 | void l2cap_sock_init(struct sock *sk, struct sock *parent) |
| 797 | { |
| 798 | struct l2cap_pinfo *pi = l2cap_pi(sk); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 799 | struct l2cap_chan *chan = pi->chan; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 800 | |
| 801 | BT_DBG("sk %p", sk); |
| 802 | |
| 803 | if (parent) { |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 804 | struct l2cap_chan *pchan = l2cap_pi(parent)->chan; |
| 805 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 806 | sk->sk_type = parent->sk_type; |
| 807 | bt_sk(sk)->defer_setup = bt_sk(parent)->defer_setup; |
| 808 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame^] | 809 | chan->chan_type = pchan->chan_type; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 810 | chan->imtu = pchan->imtu; |
| 811 | chan->omtu = pchan->omtu; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 812 | chan->conf_state = pchan->conf_state; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 813 | chan->mode = pchan->mode; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 814 | chan->fcs = pchan->fcs; |
| 815 | chan->max_tx = pchan->max_tx; |
| 816 | chan->tx_win = pchan->tx_win; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 817 | chan->sec_level = pchan->sec_level; |
| 818 | chan->role_switch = pchan->role_switch; |
| 819 | chan->force_reliable = pchan->force_reliable; |
| 820 | chan->flushable = pchan->flushable; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 821 | } else { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame^] | 822 | |
| 823 | switch (sk->sk_type) { |
| 824 | case SOCK_RAW: |
| 825 | chan->chan_type = L2CAP_CHAN_RAW; |
| 826 | break; |
| 827 | case SOCK_DGRAM: |
| 828 | chan->chan_type = L2CAP_CHAN_CONN_LESS; |
| 829 | break; |
| 830 | case SOCK_SEQPACKET: |
| 831 | case SOCK_STREAM: |
| 832 | chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; |
| 833 | break; |
| 834 | } |
| 835 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 836 | chan->imtu = L2CAP_DEFAULT_MTU; |
| 837 | chan->omtu = 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 838 | if (!disable_ertm && sk->sk_type == SOCK_STREAM) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 839 | chan->mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 840 | chan->conf_state |= L2CAP_CONF_STATE2_DEVICE; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 841 | } else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 842 | chan->mode = L2CAP_MODE_BASIC; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 843 | } |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 844 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 845 | chan->fcs = L2CAP_FCS_CRC16; |
| 846 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 847 | chan->sec_level = BT_SECURITY_LOW; |
| 848 | chan->role_switch = 0; |
| 849 | chan->force_reliable = 0; |
| 850 | chan->flushable = BT_FLUSHABLE_OFF; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* Default config options */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 854 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | static struct proto l2cap_proto = { |
| 858 | .name = "L2CAP", |
| 859 | .owner = THIS_MODULE, |
| 860 | .obj_size = sizeof(struct l2cap_pinfo) |
| 861 | }; |
| 862 | |
| 863 | struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio) |
| 864 | { |
| 865 | struct sock *sk; |
| 866 | |
| 867 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto); |
| 868 | if (!sk) |
| 869 | return NULL; |
| 870 | |
| 871 | sock_init_data(sock, sk); |
| 872 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 873 | |
| 874 | sk->sk_destruct = l2cap_sock_destruct; |
| 875 | sk->sk_sndtimeo = msecs_to_jiffies(L2CAP_CONN_TIMEOUT); |
| 876 | |
| 877 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 878 | |
| 879 | sk->sk_protocol = proto; |
| 880 | sk->sk_state = BT_OPEN; |
| 881 | |
| 882 | setup_timer(&sk->sk_timer, l2cap_sock_timeout, (unsigned long) sk); |
| 883 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 884 | return sk; |
| 885 | } |
| 886 | |
| 887 | static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, |
| 888 | int kern) |
| 889 | { |
| 890 | struct sock *sk; |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 891 | struct l2cap_chan *chan; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 892 | |
| 893 | BT_DBG("sock %p", sock); |
| 894 | |
| 895 | sock->state = SS_UNCONNECTED; |
| 896 | |
| 897 | if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM && |
| 898 | sock->type != SOCK_DGRAM && sock->type != SOCK_RAW) |
| 899 | return -ESOCKTNOSUPPORT; |
| 900 | |
| 901 | if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) |
| 902 | return -EPERM; |
| 903 | |
| 904 | sock->ops = &l2cap_sock_ops; |
| 905 | |
| 906 | sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
| 907 | if (!sk) |
| 908 | return -ENOMEM; |
| 909 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 910 | chan = l2cap_chan_create(sk); |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 911 | if (!chan) { |
| 912 | l2cap_sock_kill(sk); |
| 913 | return -ENOMEM; |
| 914 | } |
| 915 | |
| 916 | l2cap_pi(sk)->chan = chan; |
| 917 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 918 | l2cap_sock_init(sk, NULL); |
| 919 | return 0; |
| 920 | } |
| 921 | |
Gustavo F. Padovan | cf2f90f | 2011-04-27 18:40:39 -0300 | [diff] [blame] | 922 | static const struct proto_ops l2cap_sock_ops = { |
Gustavo F. Padovan | 6539058 | 2011-02-04 02:33:56 -0200 | [diff] [blame] | 923 | .family = PF_BLUETOOTH, |
| 924 | .owner = THIS_MODULE, |
| 925 | .release = l2cap_sock_release, |
| 926 | .bind = l2cap_sock_bind, |
| 927 | .connect = l2cap_sock_connect, |
| 928 | .listen = l2cap_sock_listen, |
| 929 | .accept = l2cap_sock_accept, |
| 930 | .getname = l2cap_sock_getname, |
| 931 | .sendmsg = l2cap_sock_sendmsg, |
| 932 | .recvmsg = l2cap_sock_recvmsg, |
| 933 | .poll = bt_sock_poll, |
| 934 | .ioctl = bt_sock_ioctl, |
| 935 | .mmap = sock_no_mmap, |
| 936 | .socketpair = sock_no_socketpair, |
| 937 | .shutdown = l2cap_sock_shutdown, |
| 938 | .setsockopt = l2cap_sock_setsockopt, |
| 939 | .getsockopt = l2cap_sock_getsockopt |
| 940 | }; |
| 941 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 942 | static const struct net_proto_family l2cap_sock_family_ops = { |
| 943 | .family = PF_BLUETOOTH, |
| 944 | .owner = THIS_MODULE, |
| 945 | .create = l2cap_sock_create, |
| 946 | }; |
| 947 | |
| 948 | int __init l2cap_init_sockets(void) |
| 949 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 950 | int err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 951 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 952 | err = proto_register(&l2cap_proto, 0); |
| 953 | if (err < 0) |
| 954 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 955 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 956 | err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops); |
| 957 | if (err < 0) |
| 958 | goto error; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 959 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 960 | BT_INFO("L2CAP socket layer initialized"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 961 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 962 | return 0; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 963 | |
| 964 | error: |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 965 | BT_ERR("L2CAP socket registration failed"); |
| 966 | proto_unregister(&l2cap_proto); |
| 967 | return err; |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | void l2cap_cleanup_sockets(void) |
| 971 | { |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 972 | if (bt_sock_unregister(BTPROTO_L2CAP) < 0) |
| 973 | BT_ERR("L2CAP socket unregistration failed"); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 974 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 975 | proto_unregister(&l2cap_proto); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 976 | } |