Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #define pr_fmt(fmt) "llcp: %s: " fmt, __func__ |
| 19 | |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/nfc.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 24 | #include <linux/sched/signal.h> |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 25 | |
Samuel Ortiz | 30cc458 | 2013-04-26 11:49:40 +0200 | [diff] [blame] | 26 | #include "nfc.h" |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 27 | #include "llcp.h" |
| 28 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 29 | static int sock_wait_state(struct sock *sk, int state, unsigned long timeo) |
| 30 | { |
| 31 | DECLARE_WAITQUEUE(wait, current); |
| 32 | int err = 0; |
| 33 | |
| 34 | pr_debug("sk %p", sk); |
| 35 | |
| 36 | add_wait_queue(sk_sleep(sk), &wait); |
| 37 | set_current_state(TASK_INTERRUPTIBLE); |
| 38 | |
| 39 | while (sk->sk_state != state) { |
| 40 | if (!timeo) { |
| 41 | err = -EINPROGRESS; |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | if (signal_pending(current)) { |
| 46 | err = sock_intr_errno(timeo); |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | release_sock(sk); |
| 51 | timeo = schedule_timeout(timeo); |
| 52 | lock_sock(sk); |
| 53 | set_current_state(TASK_INTERRUPTIBLE); |
| 54 | |
| 55 | err = sock_error(sk); |
| 56 | if (err) |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | __set_current_state(TASK_RUNNING); |
| 61 | remove_wait_queue(sk_sleep(sk), &wait); |
| 62 | return err; |
| 63 | } |
| 64 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 65 | static struct proto llcp_sock_proto = { |
| 66 | .name = "NFC_LLCP", |
| 67 | .owner = THIS_MODULE, |
| 68 | .obj_size = sizeof(struct nfc_llcp_sock), |
| 69 | }; |
| 70 | |
| 71 | static int llcp_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) |
| 72 | { |
| 73 | struct sock *sk = sock->sk; |
| 74 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 75 | struct nfc_llcp_local *local; |
| 76 | struct nfc_dev *dev; |
| 77 | struct sockaddr_nfc_llcp llcp_addr; |
| 78 | int len, ret = 0; |
| 79 | |
Mateusz Jurczyk | f6a5885 | 2017-06-13 18:44:28 +0200 | [diff] [blame] | 80 | if (!addr || alen < offsetofend(struct sockaddr, sa_family) || |
| 81 | addr->sa_family != AF_NFC) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 82 | return -EINVAL; |
| 83 | |
Samuel Ortiz | c66433d | 2012-06-29 12:03:55 +0200 | [diff] [blame] | 84 | pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family); |
| 85 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 86 | memset(&llcp_addr, 0, sizeof(llcp_addr)); |
| 87 | len = min_t(unsigned int, sizeof(llcp_addr), alen); |
| 88 | memcpy(&llcp_addr, addr, len); |
| 89 | |
| 90 | /* This is going to be a listening socket, dsap must be 0 */ |
| 91 | if (llcp_addr.dsap != 0) |
| 92 | return -EINVAL; |
| 93 | |
| 94 | lock_sock(sk); |
| 95 | |
| 96 | if (sk->sk_state != LLCP_CLOSED) { |
| 97 | ret = -EBADFD; |
| 98 | goto error; |
| 99 | } |
| 100 | |
| 101 | dev = nfc_get_device(llcp_addr.dev_idx); |
| 102 | if (dev == NULL) { |
| 103 | ret = -ENODEV; |
| 104 | goto error; |
| 105 | } |
| 106 | |
| 107 | local = nfc_llcp_find_local(dev); |
| 108 | if (local == NULL) { |
| 109 | ret = -ENODEV; |
| 110 | goto put_dev; |
| 111 | } |
| 112 | |
| 113 | llcp_sock->dev = dev; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 114 | llcp_sock->local = nfc_llcp_local_get(local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 115 | llcp_sock->nfc_protocol = llcp_addr.nfc_protocol; |
| 116 | llcp_sock->service_name_len = min_t(unsigned int, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 117 | llcp_addr.service_name_len, |
| 118 | NFC_LLCP_MAX_SERVICE_NAME); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 119 | llcp_sock->service_name = kmemdup(llcp_addr.service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 120 | llcp_sock->service_name_len, |
| 121 | GFP_KERNEL); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 122 | |
| 123 | llcp_sock->ssap = nfc_llcp_get_sdp_ssap(local, llcp_sock); |
Samuel Ortiz | 8b7e8ed | 2012-06-22 15:32:20 +0200 | [diff] [blame] | 124 | if (llcp_sock->ssap == LLCP_SAP_MAX) { |
| 125 | ret = -EADDRINUSE; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 126 | goto put_dev; |
Samuel Ortiz | 8b7e8ed | 2012-06-22 15:32:20 +0200 | [diff] [blame] | 127 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 128 | |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 129 | llcp_sock->reserved_ssap = llcp_sock->ssap; |
| 130 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 131 | nfc_llcp_sock_link(&local->sockets, sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 132 | |
| 133 | pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap); |
| 134 | |
| 135 | sk->sk_state = LLCP_BOUND; |
| 136 | |
| 137 | put_dev: |
| 138 | nfc_put_device(dev); |
| 139 | |
| 140 | error: |
| 141 | release_sock(sk); |
| 142 | return ret; |
| 143 | } |
| 144 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 145 | static int llcp_raw_sock_bind(struct socket *sock, struct sockaddr *addr, |
| 146 | int alen) |
| 147 | { |
| 148 | struct sock *sk = sock->sk; |
| 149 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 150 | struct nfc_llcp_local *local; |
| 151 | struct nfc_dev *dev; |
| 152 | struct sockaddr_nfc_llcp llcp_addr; |
| 153 | int len, ret = 0; |
| 154 | |
Mateusz Jurczyk | f6a5885 | 2017-06-13 18:44:28 +0200 | [diff] [blame] | 155 | if (!addr || alen < offsetofend(struct sockaddr, sa_family) || |
| 156 | addr->sa_family != AF_NFC) |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 157 | return -EINVAL; |
| 158 | |
| 159 | pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family); |
| 160 | |
| 161 | memset(&llcp_addr, 0, sizeof(llcp_addr)); |
| 162 | len = min_t(unsigned int, sizeof(llcp_addr), alen); |
| 163 | memcpy(&llcp_addr, addr, len); |
| 164 | |
| 165 | lock_sock(sk); |
| 166 | |
| 167 | if (sk->sk_state != LLCP_CLOSED) { |
| 168 | ret = -EBADFD; |
| 169 | goto error; |
| 170 | } |
| 171 | |
| 172 | dev = nfc_get_device(llcp_addr.dev_idx); |
| 173 | if (dev == NULL) { |
| 174 | ret = -ENODEV; |
| 175 | goto error; |
| 176 | } |
| 177 | |
| 178 | local = nfc_llcp_find_local(dev); |
| 179 | if (local == NULL) { |
| 180 | ret = -ENODEV; |
| 181 | goto put_dev; |
| 182 | } |
| 183 | |
| 184 | llcp_sock->dev = dev; |
| 185 | llcp_sock->local = nfc_llcp_local_get(local); |
| 186 | llcp_sock->nfc_protocol = llcp_addr.nfc_protocol; |
| 187 | |
| 188 | nfc_llcp_sock_link(&local->raw_sockets, sk); |
| 189 | |
| 190 | sk->sk_state = LLCP_BOUND; |
| 191 | |
| 192 | put_dev: |
| 193 | nfc_put_device(dev); |
| 194 | |
| 195 | error: |
| 196 | release_sock(sk); |
| 197 | return ret; |
| 198 | } |
| 199 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 200 | static int llcp_sock_listen(struct socket *sock, int backlog) |
| 201 | { |
| 202 | struct sock *sk = sock->sk; |
| 203 | int ret = 0; |
| 204 | |
| 205 | pr_debug("sk %p backlog %d\n", sk, backlog); |
| 206 | |
| 207 | lock_sock(sk); |
| 208 | |
Szymon Janc | 874934f | 2012-10-04 15:15:51 +0200 | [diff] [blame] | 209 | if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) || |
| 210 | sk->sk_state != LLCP_BOUND) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 211 | ret = -EBADFD; |
| 212 | goto error; |
| 213 | } |
| 214 | |
| 215 | sk->sk_max_ack_backlog = backlog; |
| 216 | sk->sk_ack_backlog = 0; |
| 217 | |
| 218 | pr_debug("Socket listening\n"); |
| 219 | sk->sk_state = LLCP_LISTEN; |
| 220 | |
| 221 | error: |
| 222 | release_sock(sk); |
| 223 | |
| 224 | return ret; |
| 225 | } |
| 226 | |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 227 | static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname, |
| 228 | char __user *optval, unsigned int optlen) |
| 229 | { |
| 230 | struct sock *sk = sock->sk; |
| 231 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 232 | u32 opt; |
| 233 | int err = 0; |
| 234 | |
| 235 | pr_debug("%p optname %d\n", sk, optname); |
| 236 | |
| 237 | if (level != SOL_NFC) |
| 238 | return -ENOPROTOOPT; |
| 239 | |
| 240 | lock_sock(sk); |
| 241 | |
| 242 | switch (optname) { |
| 243 | case NFC_LLCP_RW: |
| 244 | if (sk->sk_state == LLCP_CONNECTED || |
| 245 | sk->sk_state == LLCP_BOUND || |
| 246 | sk->sk_state == LLCP_LISTEN) { |
| 247 | err = -EINVAL; |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | if (get_user(opt, (u32 __user *) optval)) { |
| 252 | err = -EFAULT; |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | if (opt > LLCP_MAX_RW) { |
| 257 | err = -EINVAL; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | llcp_sock->rw = (u8) opt; |
| 262 | |
| 263 | break; |
| 264 | |
| 265 | case NFC_LLCP_MIUX: |
| 266 | if (sk->sk_state == LLCP_CONNECTED || |
| 267 | sk->sk_state == LLCP_BOUND || |
| 268 | sk->sk_state == LLCP_LISTEN) { |
| 269 | err = -EINVAL; |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | if (get_user(opt, (u32 __user *) optval)) { |
| 274 | err = -EFAULT; |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | if (opt > LLCP_MAX_MIUX) { |
| 279 | err = -EINVAL; |
| 280 | break; |
| 281 | } |
| 282 | |
Samuel Ortiz | 5eef666 | 2013-03-20 16:06:12 +0100 | [diff] [blame] | 283 | llcp_sock->miux = cpu_to_be16((u16) opt); |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 284 | |
| 285 | break; |
| 286 | |
| 287 | default: |
| 288 | err = -ENOPROTOOPT; |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | release_sock(sk); |
| 293 | |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 294 | pr_debug("%p rw %d miux %d\n", llcp_sock, |
| 295 | llcp_sock->rw, llcp_sock->miux); |
| 296 | |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 297 | return err; |
| 298 | } |
| 299 | |
| 300 | static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname, |
| 301 | char __user *optval, int __user *optlen) |
| 302 | { |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 303 | struct nfc_llcp_local *local; |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 304 | struct sock *sk = sock->sk; |
| 305 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 306 | int len, err = 0; |
Thierry Escande | 064f370 | 2013-04-02 10:25:16 +0200 | [diff] [blame] | 307 | u16 miux, remote_miu; |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 308 | u8 rw; |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 309 | |
| 310 | pr_debug("%p optname %d\n", sk, optname); |
| 311 | |
| 312 | if (level != SOL_NFC) |
| 313 | return -ENOPROTOOPT; |
| 314 | |
| 315 | if (get_user(len, optlen)) |
| 316 | return -EFAULT; |
| 317 | |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 318 | local = llcp_sock->local; |
| 319 | if (!local) |
| 320 | return -ENODEV; |
| 321 | |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 322 | len = min_t(u32, len, sizeof(u32)); |
| 323 | |
| 324 | lock_sock(sk); |
| 325 | |
| 326 | switch (optname) { |
| 327 | case NFC_LLCP_RW: |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 328 | rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw; |
| 329 | if (put_user(rw, (u32 __user *) optval)) |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 330 | err = -EFAULT; |
| 331 | |
| 332 | break; |
| 333 | |
| 334 | case NFC_LLCP_MIUX: |
Samuel Ortiz | 00e856d | 2013-03-20 16:36:13 +0100 | [diff] [blame] | 335 | miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ? |
| 336 | be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux); |
| 337 | |
| 338 | if (put_user(miux, (u32 __user *) optval)) |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 339 | err = -EFAULT; |
| 340 | |
| 341 | break; |
| 342 | |
Thierry Escande | 064f370 | 2013-04-02 10:25:16 +0200 | [diff] [blame] | 343 | case NFC_LLCP_REMOTE_MIU: |
| 344 | remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ? |
| 345 | local->remote_miu : llcp_sock->remote_miu; |
| 346 | |
| 347 | if (put_user(remote_miu, (u32 __user *) optval)) |
| 348 | err = -EFAULT; |
| 349 | |
| 350 | break; |
| 351 | |
| 352 | case NFC_LLCP_REMOTE_LTO: |
| 353 | if (put_user(local->remote_lto / 10, (u32 __user *) optval)) |
| 354 | err = -EFAULT; |
| 355 | |
| 356 | break; |
| 357 | |
| 358 | case NFC_LLCP_REMOTE_RW: |
| 359 | if (put_user(llcp_sock->remote_rw, (u32 __user *) optval)) |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 360 | err = -EFAULT; |
| 361 | |
| 362 | break; |
| 363 | |
| 364 | default: |
| 365 | err = -ENOPROTOOPT; |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | release_sock(sk); |
| 370 | |
| 371 | if (put_user(len, optlen)) |
| 372 | return -EFAULT; |
| 373 | |
| 374 | return err; |
| 375 | } |
| 376 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 377 | void nfc_llcp_accept_unlink(struct sock *sk) |
| 378 | { |
| 379 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 380 | |
| 381 | pr_debug("state %d\n", sk->sk_state); |
| 382 | |
| 383 | list_del_init(&llcp_sock->accept_queue); |
| 384 | sk_acceptq_removed(llcp_sock->parent); |
| 385 | llcp_sock->parent = NULL; |
| 386 | |
| 387 | sock_put(sk); |
| 388 | } |
| 389 | |
| 390 | void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk) |
| 391 | { |
| 392 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 393 | struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent); |
| 394 | |
| 395 | /* Lock will be free from unlink */ |
| 396 | sock_hold(sk); |
| 397 | |
| 398 | list_add_tail(&llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 399 | &llcp_sock_parent->accept_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 400 | llcp_sock->parent = parent; |
| 401 | sk_acceptq_added(parent); |
| 402 | } |
| 403 | |
| 404 | struct sock *nfc_llcp_accept_dequeue(struct sock *parent, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 405 | struct socket *newsock) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 406 | { |
| 407 | struct nfc_llcp_sock *lsk, *n, *llcp_parent; |
| 408 | struct sock *sk; |
| 409 | |
| 410 | llcp_parent = nfc_llcp_sock(parent); |
| 411 | |
| 412 | list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 413 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 414 | sk = &lsk->sk; |
| 415 | lock_sock(sk); |
| 416 | |
| 417 | if (sk->sk_state == LLCP_CLOSED) { |
| 418 | release_sock(sk); |
| 419 | nfc_llcp_accept_unlink(sk); |
| 420 | continue; |
| 421 | } |
| 422 | |
| 423 | if (sk->sk_state == LLCP_CONNECTED || !newsock) { |
Samuel Ortiz | 39a352a | 2013-03-26 14:35:57 +0100 | [diff] [blame] | 424 | list_del_init(&lsk->accept_queue); |
| 425 | sock_put(sk); |
| 426 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 427 | if (newsock) |
| 428 | sock_graft(sk, newsock); |
| 429 | |
| 430 | release_sock(sk); |
| 431 | |
| 432 | pr_debug("Returning sk state %d\n", sk->sk_state); |
| 433 | |
Samuel Ortiz | b141e81 | 2013-02-21 11:04:45 +0100 | [diff] [blame] | 434 | sk_acceptq_removed(parent); |
| 435 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 436 | return sk; |
| 437 | } |
| 438 | |
| 439 | release_sock(sk); |
| 440 | } |
| 441 | |
| 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | static int llcp_sock_accept(struct socket *sock, struct socket *newsock, |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 446 | int flags, bool kern) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 447 | { |
| 448 | DECLARE_WAITQUEUE(wait, current); |
| 449 | struct sock *sk = sock->sk, *new_sk; |
| 450 | long timeo; |
| 451 | int ret = 0; |
| 452 | |
| 453 | pr_debug("parent %p\n", sk); |
| 454 | |
| 455 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 456 | |
| 457 | if (sk->sk_state != LLCP_LISTEN) { |
| 458 | ret = -EBADFD; |
| 459 | goto error; |
| 460 | } |
| 461 | |
| 462 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 463 | |
| 464 | /* Wait for an incoming connection. */ |
| 465 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 466 | while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) { |
| 467 | set_current_state(TASK_INTERRUPTIBLE); |
| 468 | |
| 469 | if (!timeo) { |
| 470 | ret = -EAGAIN; |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | if (signal_pending(current)) { |
| 475 | ret = sock_intr_errno(timeo); |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | release_sock(sk); |
| 480 | timeo = schedule_timeout(timeo); |
| 481 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 482 | } |
| 483 | __set_current_state(TASK_RUNNING); |
| 484 | remove_wait_queue(sk_sleep(sk), &wait); |
| 485 | |
| 486 | if (ret) |
| 487 | goto error; |
| 488 | |
| 489 | newsock->state = SS_CONNECTED; |
| 490 | |
| 491 | pr_debug("new socket %p\n", new_sk); |
| 492 | |
| 493 | error: |
| 494 | release_sock(sk); |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 499 | static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr, |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 500 | int *len, int peer) |
| 501 | { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 502 | struct sock *sk = sock->sk; |
| 503 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 504 | DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 505 | |
Samuel Ortiz | fe3c094 | 2012-07-05 17:43:08 +0200 | [diff] [blame] | 506 | if (llcp_sock == NULL || llcp_sock->dev == NULL) |
| 507 | return -EBADFD; |
| 508 | |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 509 | pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx, |
| 510 | llcp_sock->dsap, llcp_sock->ssap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 511 | |
Dan Carpenter | 5ffedc6 | 2013-04-22 20:24:52 +0000 | [diff] [blame] | 512 | memset(llcp_addr, 0, sizeof(*llcp_addr)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 513 | *len = sizeof(struct sockaddr_nfc_llcp); |
| 514 | |
Cong Wang | 03c0535 | 2016-01-29 11:37:40 -0800 | [diff] [blame] | 515 | lock_sock(sk); |
| 516 | if (!llcp_sock->dev) { |
| 517 | release_sock(sk); |
| 518 | return -EBADFD; |
| 519 | } |
Dan Carpenter | 5ffedc6 | 2013-04-22 20:24:52 +0000 | [diff] [blame] | 520 | llcp_addr->sa_family = AF_NFC; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 521 | llcp_addr->dev_idx = llcp_sock->dev->idx; |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 522 | llcp_addr->target_idx = llcp_sock->target_idx; |
Dan Carpenter | 5ffedc6 | 2013-04-22 20:24:52 +0000 | [diff] [blame] | 523 | llcp_addr->nfc_protocol = llcp_sock->nfc_protocol; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 524 | llcp_addr->dsap = llcp_sock->dsap; |
| 525 | llcp_addr->ssap = llcp_sock->ssap; |
| 526 | llcp_addr->service_name_len = llcp_sock->service_name_len; |
| 527 | memcpy(llcp_addr->service_name, llcp_sock->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 528 | llcp_addr->service_name_len); |
Cong Wang | 03c0535 | 2016-01-29 11:37:40 -0800 | [diff] [blame] | 529 | release_sock(sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static inline unsigned int llcp_accept_poll(struct sock *parent) |
| 535 | { |
Axel Lin | 413df10 | 2014-11-06 18:20:41 +0800 | [diff] [blame] | 536 | struct nfc_llcp_sock *llcp_sock, *parent_sock; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 537 | struct sock *sk; |
| 538 | |
| 539 | parent_sock = nfc_llcp_sock(parent); |
| 540 | |
Axel Lin | 413df10 | 2014-11-06 18:20:41 +0800 | [diff] [blame] | 541 | list_for_each_entry(llcp_sock, &parent_sock->accept_queue, |
| 542 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 543 | sk = &llcp_sock->sk; |
| 544 | |
| 545 | if (sk->sk_state == LLCP_CONNECTED) |
| 546 | return POLLIN | POLLRDNORM; |
| 547 | } |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static unsigned int llcp_sock_poll(struct file *file, struct socket *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 553 | poll_table *wait) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 554 | { |
| 555 | struct sock *sk = sock->sk; |
| 556 | unsigned int mask = 0; |
| 557 | |
| 558 | pr_debug("%p\n", sk); |
| 559 | |
| 560 | sock_poll_wait(file, sk_sleep(sk), wait); |
| 561 | |
| 562 | if (sk->sk_state == LLCP_LISTEN) |
| 563 | return llcp_accept_poll(sk); |
| 564 | |
| 565 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
Keller, Jacob E | 7d4c04f | 2013-03-28 11:19:25 +0000 | [diff] [blame] | 566 | mask |= POLLERR | |
Jacob Keller | 8facd5f | 2013-04-02 13:55:40 -0700 | [diff] [blame] | 567 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 568 | |
| 569 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 570 | mask |= POLLIN | POLLRDNORM; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 571 | |
| 572 | if (sk->sk_state == LLCP_CLOSED) |
| 573 | mask |= POLLHUP; |
| 574 | |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 575 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 576 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
| 577 | |
| 578 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 579 | mask |= POLLHUP; |
| 580 | |
Samuel Ortiz | b401123 | 2013-05-03 18:29:30 +0200 | [diff] [blame] | 581 | if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED) |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 582 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 583 | else |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 584 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 585 | |
| 586 | pr_debug("mask 0x%x\n", mask); |
| 587 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 588 | return mask; |
| 589 | } |
| 590 | |
| 591 | static int llcp_sock_release(struct socket *sock) |
| 592 | { |
| 593 | struct sock *sk = sock->sk; |
| 594 | struct nfc_llcp_local *local; |
| 595 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 596 | int err = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 597 | |
| 598 | if (!sk) |
| 599 | return 0; |
| 600 | |
| 601 | pr_debug("%p\n", sk); |
| 602 | |
| 603 | local = llcp_sock->local; |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 604 | if (local == NULL) { |
| 605 | err = -ENODEV; |
| 606 | goto out; |
| 607 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 608 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 609 | lock_sock(sk); |
| 610 | |
| 611 | /* Send a DISC */ |
| 612 | if (sk->sk_state == LLCP_CONNECTED) |
Thierry Escande | 58e3dd1 | 2013-06-04 11:34:50 +0200 | [diff] [blame] | 613 | nfc_llcp_send_disconnect(llcp_sock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 614 | |
| 615 | if (sk->sk_state == LLCP_LISTEN) { |
| 616 | struct nfc_llcp_sock *lsk, *n; |
| 617 | struct sock *accept_sk; |
| 618 | |
| 619 | list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 620 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 621 | accept_sk = &lsk->sk; |
| 622 | lock_sock(accept_sk); |
| 623 | |
Thierry Escande | 58e3dd1 | 2013-06-04 11:34:50 +0200 | [diff] [blame] | 624 | nfc_llcp_send_disconnect(lsk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 625 | nfc_llcp_accept_unlink(accept_sk); |
| 626 | |
| 627 | release_sock(accept_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 631 | if (llcp_sock->reserved_ssap < LLCP_SAP_MAX) |
| 632 | nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 633 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 634 | release_sock(sk); |
| 635 | |
Thierry Escande | 17f7ae16 | 2013-06-04 11:34:51 +0200 | [diff] [blame] | 636 | /* Keep this sock alive and therefore do not remove it from the sockets |
| 637 | * list until the DISC PDU has been actually sent. Otherwise we would |
| 638 | * reply with DM PDUs before sending the DISC one. |
| 639 | */ |
| 640 | if (sk->sk_state == LLCP_DISCONNECTING) |
| 641 | return err; |
| 642 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 643 | if (sock->type == SOCK_RAW) |
| 644 | nfc_llcp_sock_unlink(&local->raw_sockets, sk); |
| 645 | else |
| 646 | nfc_llcp_sock_unlink(&local->sockets, sk); |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 647 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 648 | out: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 649 | sock_orphan(sk); |
| 650 | sock_put(sk); |
| 651 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 652 | return err; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 656 | int len, int flags) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 657 | { |
| 658 | struct sock *sk = sock->sk; |
| 659 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 660 | struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr; |
| 661 | struct nfc_dev *dev; |
| 662 | struct nfc_llcp_local *local; |
| 663 | int ret = 0; |
| 664 | |
| 665 | pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags); |
| 666 | |
Mateusz Jurczyk | 608c4ad | 2017-05-24 12:26:20 +0200 | [diff] [blame] | 667 | if (!addr || len < sizeof(*addr) || addr->sa_family != AF_NFC) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 668 | return -EINVAL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 669 | |
Dave Jones | 32418cf | 2012-10-04 09:51:11 +0000 | [diff] [blame] | 670 | if (addr->service_name_len == 0 && addr->dsap == 0) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 671 | return -EINVAL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 672 | |
| 673 | pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 674 | addr->target_idx, addr->nfc_protocol); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 675 | |
| 676 | lock_sock(sk); |
| 677 | |
| 678 | if (sk->sk_state == LLCP_CONNECTED) { |
| 679 | ret = -EISCONN; |
| 680 | goto error; |
| 681 | } |
| 682 | |
| 683 | dev = nfc_get_device(addr->dev_idx); |
| 684 | if (dev == NULL) { |
| 685 | ret = -ENODEV; |
| 686 | goto error; |
| 687 | } |
| 688 | |
| 689 | local = nfc_llcp_find_local(dev); |
| 690 | if (local == NULL) { |
| 691 | ret = -ENODEV; |
| 692 | goto put_dev; |
| 693 | } |
| 694 | |
| 695 | device_lock(&dev->dev); |
| 696 | if (dev->dep_link_up == false) { |
| 697 | ret = -ENOLINK; |
| 698 | device_unlock(&dev->dev); |
| 699 | goto put_dev; |
| 700 | } |
| 701 | device_unlock(&dev->dev); |
| 702 | |
| 703 | if (local->rf_mode == NFC_RF_INITIATOR && |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 704 | addr->target_idx != local->target_idx) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 705 | ret = -ENOLINK; |
| 706 | goto put_dev; |
| 707 | } |
| 708 | |
| 709 | llcp_sock->dev = dev; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 710 | llcp_sock->local = nfc_llcp_local_get(local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 711 | llcp_sock->ssap = nfc_llcp_get_local_ssap(local); |
| 712 | if (llcp_sock->ssap == LLCP_SAP_MAX) { |
| 713 | ret = -ENOMEM; |
| 714 | goto put_dev; |
| 715 | } |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 716 | |
| 717 | llcp_sock->reserved_ssap = llcp_sock->ssap; |
| 718 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 719 | if (addr->service_name_len == 0) |
| 720 | llcp_sock->dsap = addr->dsap; |
| 721 | else |
| 722 | llcp_sock->dsap = LLCP_SAP_SDP; |
| 723 | llcp_sock->nfc_protocol = addr->nfc_protocol; |
| 724 | llcp_sock->service_name_len = min_t(unsigned int, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 725 | addr->service_name_len, |
| 726 | NFC_LLCP_MAX_SERVICE_NAME); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 727 | llcp_sock->service_name = kmemdup(addr->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 728 | llcp_sock->service_name_len, |
| 729 | GFP_KERNEL); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 730 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 731 | nfc_llcp_sock_link(&local->connecting_sockets, sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 732 | |
| 733 | ret = nfc_llcp_send_connect(llcp_sock); |
| 734 | if (ret) |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 735 | goto sock_unlink; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 736 | |
Samuel Ortiz | b401123 | 2013-05-03 18:29:30 +0200 | [diff] [blame] | 737 | sk->sk_state = LLCP_CONNECTING; |
| 738 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 739 | ret = sock_wait_state(sk, LLCP_CONNECTED, |
| 740 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Samuel Ortiz | b401123 | 2013-05-03 18:29:30 +0200 | [diff] [blame] | 741 | if (ret && ret != -EINPROGRESS) |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 742 | goto sock_unlink; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 743 | |
| 744 | release_sock(sk); |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 745 | |
Samuel Ortiz | b401123 | 2013-05-03 18:29:30 +0200 | [diff] [blame] | 746 | return ret; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 747 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 748 | sock_unlink: |
| 749 | nfc_llcp_put_ssap(local, llcp_sock->ssap); |
| 750 | |
| 751 | nfc_llcp_sock_unlink(&local->connecting_sockets, sk); |
| 752 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 753 | put_dev: |
| 754 | nfc_put_device(dev); |
| 755 | |
| 756 | error: |
| 757 | release_sock(sk); |
| 758 | return ret; |
| 759 | } |
| 760 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 761 | static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg, |
| 762 | size_t len) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 763 | { |
| 764 | struct sock *sk = sock->sk; |
| 765 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 766 | int ret; |
| 767 | |
| 768 | pr_debug("sock %p sk %p", sock, sk); |
| 769 | |
| 770 | ret = sock_error(sk); |
| 771 | if (ret) |
| 772 | return ret; |
| 773 | |
| 774 | if (msg->msg_flags & MSG_OOB) |
| 775 | return -EOPNOTSUPP; |
| 776 | |
| 777 | lock_sock(sk); |
| 778 | |
Samuel Ortiz | b874dec | 2012-10-16 15:04:10 +0200 | [diff] [blame] | 779 | if (sk->sk_type == SOCK_DGRAM) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 780 | DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr, |
| 781 | msg->msg_name); |
Samuel Ortiz | b874dec | 2012-10-16 15:04:10 +0200 | [diff] [blame] | 782 | |
| 783 | if (msg->msg_namelen < sizeof(*addr)) { |
| 784 | release_sock(sk); |
Samuel Ortiz | b874dec | 2012-10-16 15:04:10 +0200 | [diff] [blame] | 785 | return -EINVAL; |
| 786 | } |
| 787 | |
| 788 | release_sock(sk); |
| 789 | |
| 790 | return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap, |
| 791 | msg, len); |
| 792 | } |
| 793 | |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 794 | if (sk->sk_state != LLCP_CONNECTED) { |
| 795 | release_sock(sk); |
| 796 | return -ENOTCONN; |
| 797 | } |
| 798 | |
| 799 | release_sock(sk); |
| 800 | |
| 801 | return nfc_llcp_send_i_frame(llcp_sock, msg, len); |
| 802 | } |
| 803 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 804 | static int llcp_sock_recvmsg(struct socket *sock, struct msghdr *msg, |
| 805 | size_t len, int flags) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 806 | { |
| 807 | int noblock = flags & MSG_DONTWAIT; |
| 808 | struct sock *sk = sock->sk; |
| 809 | unsigned int copied, rlen; |
| 810 | struct sk_buff *skb, *cskb; |
| 811 | int err = 0; |
| 812 | |
| 813 | pr_debug("%p %zu\n", sk, len); |
| 814 | |
| 815 | lock_sock(sk); |
| 816 | |
| 817 | if (sk->sk_state == LLCP_CLOSED && |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 818 | skb_queue_empty(&sk->sk_receive_queue)) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 819 | release_sock(sk); |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | release_sock(sk); |
| 824 | |
| 825 | if (flags & (MSG_OOB)) |
| 826 | return -EOPNOTSUPP; |
| 827 | |
| 828 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 829 | if (!skb) { |
| 830 | pr_err("Recv datagram failed state %d %d %d", |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 831 | sk->sk_state, err, sock_error(sk)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 832 | |
| 833 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 834 | return 0; |
| 835 | |
| 836 | return err; |
| 837 | } |
| 838 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 839 | rlen = skb->len; /* real length of skb */ |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 840 | copied = min_t(unsigned int, rlen, len); |
| 841 | |
| 842 | cskb = skb; |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 843 | if (skb_copy_datagram_msg(cskb, 0, msg, copied)) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 844 | if (!(flags & MSG_PEEK)) |
| 845 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 846 | return -EFAULT; |
| 847 | } |
| 848 | |
Thierry Escande | 2c2d45b | 2012-11-27 15:44:24 +0100 | [diff] [blame] | 849 | sock_recv_timestamp(msg, sk, skb); |
| 850 | |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame] | 851 | if (sk->sk_type == SOCK_DGRAM && msg->msg_name) { |
| 852 | struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb); |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 853 | DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, sockaddr, |
| 854 | msg->msg_name); |
Samuel Ortiz | fad2e37 | 2013-01-11 14:48:48 +0100 | [diff] [blame] | 855 | |
| 856 | msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp); |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame] | 857 | |
| 858 | pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap); |
| 859 | |
Mathias Krause | d26d650 | 2013-04-07 01:51:58 +0000 | [diff] [blame] | 860 | memset(sockaddr, 0, sizeof(*sockaddr)); |
Samuel Ortiz | fad2e37 | 2013-01-11 14:48:48 +0100 | [diff] [blame] | 861 | sockaddr->sa_family = AF_NFC; |
| 862 | sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP; |
| 863 | sockaddr->dsap = ui_cb->dsap; |
| 864 | sockaddr->ssap = ui_cb->ssap; |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame] | 865 | } |
| 866 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 867 | /* Mark read part of skb as used */ |
| 868 | if (!(flags & MSG_PEEK)) { |
| 869 | |
| 870 | /* SOCK_STREAM: re-queue skb if it contains unreceived data */ |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame] | 871 | if (sk->sk_type == SOCK_STREAM || |
| 872 | sk->sk_type == SOCK_DGRAM || |
| 873 | sk->sk_type == SOCK_RAW) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 874 | skb_pull(skb, copied); |
| 875 | if (skb->len) { |
| 876 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 877 | goto done; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | kfree_skb(skb); |
| 882 | } |
| 883 | |
| 884 | /* XXX Queue backlogged skbs */ |
| 885 | |
| 886 | done: |
| 887 | /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */ |
| 888 | if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC)) |
| 889 | copied = rlen; |
| 890 | |
| 891 | return copied; |
| 892 | } |
| 893 | |
| 894 | static const struct proto_ops llcp_sock_ops = { |
| 895 | .family = PF_NFC, |
| 896 | .owner = THIS_MODULE, |
| 897 | .bind = llcp_sock_bind, |
| 898 | .connect = llcp_sock_connect, |
| 899 | .release = llcp_sock_release, |
| 900 | .socketpair = sock_no_socketpair, |
| 901 | .accept = llcp_sock_accept, |
| 902 | .getname = llcp_sock_getname, |
| 903 | .poll = llcp_sock_poll, |
| 904 | .ioctl = sock_no_ioctl, |
| 905 | .listen = llcp_sock_listen, |
| 906 | .shutdown = sock_no_shutdown, |
Samuel Ortiz | 26fd76c | 2013-02-22 10:53:25 +0100 | [diff] [blame] | 907 | .setsockopt = nfc_llcp_setsockopt, |
| 908 | .getsockopt = nfc_llcp_getsockopt, |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 909 | .sendmsg = llcp_sock_sendmsg, |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 910 | .recvmsg = llcp_sock_recvmsg, |
| 911 | .mmap = sock_no_mmap, |
| 912 | }; |
| 913 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 914 | static const struct proto_ops llcp_rawsock_ops = { |
| 915 | .family = PF_NFC, |
| 916 | .owner = THIS_MODULE, |
| 917 | .bind = llcp_raw_sock_bind, |
| 918 | .connect = sock_no_connect, |
| 919 | .release = llcp_sock_release, |
| 920 | .socketpair = sock_no_socketpair, |
| 921 | .accept = sock_no_accept, |
| 922 | .getname = llcp_sock_getname, |
| 923 | .poll = llcp_sock_poll, |
| 924 | .ioctl = sock_no_ioctl, |
| 925 | .listen = sock_no_listen, |
| 926 | .shutdown = sock_no_shutdown, |
| 927 | .setsockopt = sock_no_setsockopt, |
| 928 | .getsockopt = sock_no_getsockopt, |
| 929 | .sendmsg = sock_no_sendmsg, |
| 930 | .recvmsg = llcp_sock_recvmsg, |
| 931 | .mmap = sock_no_mmap, |
| 932 | }; |
| 933 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 934 | static void llcp_sock_destruct(struct sock *sk) |
| 935 | { |
| 936 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 937 | |
| 938 | pr_debug("%p\n", sk); |
| 939 | |
| 940 | if (sk->sk_state == LLCP_CONNECTED) |
| 941 | nfc_put_device(llcp_sock->dev); |
| 942 | |
| 943 | skb_queue_purge(&sk->sk_receive_queue); |
| 944 | |
| 945 | nfc_llcp_sock_free(llcp_sock); |
| 946 | |
| 947 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 948 | pr_err("Freeing alive NFC LLCP socket %p\n", sk); |
| 949 | return; |
| 950 | } |
| 951 | } |
| 952 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 953 | struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp, int kern) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 954 | { |
| 955 | struct sock *sk; |
| 956 | struct nfc_llcp_sock *llcp_sock; |
| 957 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 958 | sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto, kern); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 959 | if (!sk) |
| 960 | return NULL; |
| 961 | |
| 962 | llcp_sock = nfc_llcp_sock(sk); |
| 963 | |
| 964 | sock_init_data(sock, sk); |
| 965 | sk->sk_state = LLCP_CLOSED; |
| 966 | sk->sk_protocol = NFC_SOCKPROTO_LLCP; |
| 967 | sk->sk_type = type; |
| 968 | sk->sk_destruct = llcp_sock_destruct; |
| 969 | |
| 970 | llcp_sock->ssap = 0; |
| 971 | llcp_sock->dsap = LLCP_SAP_SDP; |
Samuel Ortiz | 06d44f8 | 2013-02-22 11:38:05 +0100 | [diff] [blame] | 972 | llcp_sock->rw = LLCP_MAX_RW + 1; |
Samuel Ortiz | 5eef666 | 2013-03-20 16:06:12 +0100 | [diff] [blame] | 973 | llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 974 | llcp_sock->send_n = llcp_sock->send_ack_n = 0; |
| 975 | llcp_sock->recv_n = llcp_sock->recv_ack_n = 0; |
| 976 | llcp_sock->remote_ready = 1; |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 977 | llcp_sock->reserved_ssap = LLCP_SAP_MAX; |
Thierry Escande | abd18d4 | 2013-04-02 10:25:15 +0200 | [diff] [blame] | 978 | nfc_llcp_socket_remote_param_init(llcp_sock); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 979 | skb_queue_head_init(&llcp_sock->tx_queue); |
| 980 | skb_queue_head_init(&llcp_sock->tx_pending_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 981 | INIT_LIST_HEAD(&llcp_sock->accept_queue); |
| 982 | |
| 983 | if (sock != NULL) |
| 984 | sock->state = SS_UNCONNECTED; |
| 985 | |
| 986 | return sk; |
| 987 | } |
| 988 | |
| 989 | void nfc_llcp_sock_free(struct nfc_llcp_sock *sock) |
| 990 | { |
| 991 | kfree(sock->service_name); |
| 992 | |
| 993 | skb_queue_purge(&sock->tx_queue); |
| 994 | skb_queue_purge(&sock->tx_pending_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 995 | |
| 996 | list_del_init(&sock->accept_queue); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 997 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 998 | sock->parent = NULL; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 999 | |
| 1000 | nfc_llcp_local_put(sock->local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | static int llcp_sock_create(struct net *net, struct socket *sock, |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1004 | const struct nfc_protocol *nfc_proto, int kern) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1005 | { |
| 1006 | struct sock *sk; |
| 1007 | |
| 1008 | pr_debug("%p\n", sock); |
| 1009 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1010 | if (sock->type != SOCK_STREAM && |
| 1011 | sock->type != SOCK_DGRAM && |
| 1012 | sock->type != SOCK_RAW) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1013 | return -ESOCKTNOSUPPORT; |
| 1014 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 1015 | if (sock->type == SOCK_RAW) |
| 1016 | sock->ops = &llcp_rawsock_ops; |
| 1017 | else |
| 1018 | sock->ops = &llcp_sock_ops; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1019 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1020 | sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC, kern); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1021 | if (sk == NULL) |
| 1022 | return -ENOMEM; |
| 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | static const struct nfc_protocol llcp_nfc_proto = { |
| 1028 | .id = NFC_SOCKPROTO_LLCP, |
| 1029 | .proto = &llcp_sock_proto, |
| 1030 | .owner = THIS_MODULE, |
| 1031 | .create = llcp_sock_create |
| 1032 | }; |
| 1033 | |
| 1034 | int __init nfc_llcp_sock_init(void) |
| 1035 | { |
| 1036 | return nfc_proto_register(&llcp_nfc_proto); |
| 1037 | } |
| 1038 | |
| 1039 | void nfc_llcp_sock_exit(void) |
| 1040 | { |
| 1041 | nfc_proto_unregister(&llcp_nfc_proto); |
| 1042 | } |