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