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