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