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