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 | |
| 81 | pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family); |
| 82 | |
| 83 | if (!addr || addr->sa_family != AF_NFC) |
| 84 | return -EINVAL; |
| 85 | |
| 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; |
| 114 | llcp_sock->local = local; |
| 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); |
| 124 | if (llcp_sock->ssap == LLCP_MAX_SAP) |
| 125 | goto put_dev; |
| 126 | |
| 127 | local->sockets[llcp_sock->ssap] = llcp_sock; |
| 128 | |
| 129 | pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap); |
| 130 | |
| 131 | sk->sk_state = LLCP_BOUND; |
| 132 | |
| 133 | put_dev: |
| 134 | nfc_put_device(dev); |
| 135 | |
| 136 | error: |
| 137 | release_sock(sk); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static int llcp_sock_listen(struct socket *sock, int backlog) |
| 142 | { |
| 143 | struct sock *sk = sock->sk; |
| 144 | int ret = 0; |
| 145 | |
| 146 | pr_debug("sk %p backlog %d\n", sk, backlog); |
| 147 | |
| 148 | lock_sock(sk); |
| 149 | |
| 150 | if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 151 | || sk->sk_state != LLCP_BOUND) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 152 | ret = -EBADFD; |
| 153 | goto error; |
| 154 | } |
| 155 | |
| 156 | sk->sk_max_ack_backlog = backlog; |
| 157 | sk->sk_ack_backlog = 0; |
| 158 | |
| 159 | pr_debug("Socket listening\n"); |
| 160 | sk->sk_state = LLCP_LISTEN; |
| 161 | |
| 162 | error: |
| 163 | release_sock(sk); |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | void nfc_llcp_accept_unlink(struct sock *sk) |
| 169 | { |
| 170 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 171 | |
| 172 | pr_debug("state %d\n", sk->sk_state); |
| 173 | |
| 174 | list_del_init(&llcp_sock->accept_queue); |
| 175 | sk_acceptq_removed(llcp_sock->parent); |
| 176 | llcp_sock->parent = NULL; |
| 177 | |
| 178 | sock_put(sk); |
| 179 | } |
| 180 | |
| 181 | void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk) |
| 182 | { |
| 183 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 184 | struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent); |
| 185 | |
| 186 | /* Lock will be free from unlink */ |
| 187 | sock_hold(sk); |
| 188 | |
| 189 | list_add_tail(&llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 190 | &llcp_sock_parent->accept_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 191 | llcp_sock->parent = parent; |
| 192 | sk_acceptq_added(parent); |
| 193 | } |
| 194 | |
| 195 | struct sock *nfc_llcp_accept_dequeue(struct sock *parent, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 196 | struct socket *newsock) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 197 | { |
| 198 | struct nfc_llcp_sock *lsk, *n, *llcp_parent; |
| 199 | struct sock *sk; |
| 200 | |
| 201 | llcp_parent = nfc_llcp_sock(parent); |
| 202 | |
| 203 | list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 204 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 205 | sk = &lsk->sk; |
| 206 | lock_sock(sk); |
| 207 | |
| 208 | if (sk->sk_state == LLCP_CLOSED) { |
| 209 | release_sock(sk); |
| 210 | nfc_llcp_accept_unlink(sk); |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | if (sk->sk_state == LLCP_CONNECTED || !newsock) { |
| 215 | nfc_llcp_accept_unlink(sk); |
| 216 | if (newsock) |
| 217 | sock_graft(sk, newsock); |
| 218 | |
| 219 | release_sock(sk); |
| 220 | |
| 221 | pr_debug("Returning sk state %d\n", sk->sk_state); |
| 222 | |
| 223 | return sk; |
| 224 | } |
| 225 | |
| 226 | release_sock(sk); |
| 227 | } |
| 228 | |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | static int llcp_sock_accept(struct socket *sock, struct socket *newsock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 233 | int flags) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 234 | { |
| 235 | DECLARE_WAITQUEUE(wait, current); |
| 236 | struct sock *sk = sock->sk, *new_sk; |
| 237 | long timeo; |
| 238 | int ret = 0; |
| 239 | |
| 240 | pr_debug("parent %p\n", sk); |
| 241 | |
| 242 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 243 | |
| 244 | if (sk->sk_state != LLCP_LISTEN) { |
| 245 | ret = -EBADFD; |
| 246 | goto error; |
| 247 | } |
| 248 | |
| 249 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 250 | |
| 251 | /* Wait for an incoming connection. */ |
| 252 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 253 | while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) { |
| 254 | set_current_state(TASK_INTERRUPTIBLE); |
| 255 | |
| 256 | if (!timeo) { |
| 257 | ret = -EAGAIN; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | if (signal_pending(current)) { |
| 262 | ret = sock_intr_errno(timeo); |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | release_sock(sk); |
| 267 | timeo = schedule_timeout(timeo); |
| 268 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 269 | } |
| 270 | __set_current_state(TASK_RUNNING); |
| 271 | remove_wait_queue(sk_sleep(sk), &wait); |
| 272 | |
| 273 | if (ret) |
| 274 | goto error; |
| 275 | |
| 276 | newsock->state = SS_CONNECTED; |
| 277 | |
| 278 | pr_debug("new socket %p\n", new_sk); |
| 279 | |
| 280 | error: |
| 281 | release_sock(sk); |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static int llcp_sock_getname(struct socket *sock, struct sockaddr *addr, |
| 287 | int *len, int peer) |
| 288 | { |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 289 | struct sockaddr_nfc_llcp *llcp_addr = (struct sockaddr_nfc_llcp *)addr; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 290 | struct sock *sk = sock->sk; |
| 291 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 292 | |
| 293 | pr_debug("%p\n", sk); |
| 294 | |
Sasha Levin | 58d1eab | 2012-06-06 23:02:55 +0200 | [diff] [blame] | 295 | if (llcp_sock == NULL) |
| 296 | return -EBADFD; |
| 297 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 298 | addr->sa_family = AF_NFC; |
| 299 | *len = sizeof(struct sockaddr_nfc_llcp); |
| 300 | |
| 301 | llcp_addr->dev_idx = llcp_sock->dev->idx; |
| 302 | llcp_addr->dsap = llcp_sock->dsap; |
| 303 | llcp_addr->ssap = llcp_sock->ssap; |
| 304 | llcp_addr->service_name_len = llcp_sock->service_name_len; |
| 305 | memcpy(llcp_addr->service_name, llcp_sock->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 306 | llcp_addr->service_name_len); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static inline unsigned int llcp_accept_poll(struct sock *parent) |
| 312 | { |
| 313 | struct nfc_llcp_sock *llcp_sock, *n, *parent_sock; |
| 314 | struct sock *sk; |
| 315 | |
| 316 | parent_sock = nfc_llcp_sock(parent); |
| 317 | |
| 318 | list_for_each_entry_safe(llcp_sock, n, &parent_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 319 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 320 | sk = &llcp_sock->sk; |
| 321 | |
| 322 | if (sk->sk_state == LLCP_CONNECTED) |
| 323 | return POLLIN | POLLRDNORM; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static unsigned int llcp_sock_poll(struct file *file, struct socket *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 330 | poll_table *wait) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 331 | { |
| 332 | struct sock *sk = sock->sk; |
| 333 | unsigned int mask = 0; |
| 334 | |
| 335 | pr_debug("%p\n", sk); |
| 336 | |
| 337 | sock_poll_wait(file, sk_sleep(sk), wait); |
| 338 | |
| 339 | if (sk->sk_state == LLCP_LISTEN) |
| 340 | return llcp_accept_poll(sk); |
| 341 | |
| 342 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
| 343 | mask |= POLLERR; |
| 344 | |
| 345 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 346 | mask |= POLLIN | POLLRDNORM; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 347 | |
| 348 | if (sk->sk_state == LLCP_CLOSED) |
| 349 | mask |= POLLHUP; |
| 350 | |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 351 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 352 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
| 353 | |
| 354 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 355 | mask |= POLLHUP; |
| 356 | |
| 357 | if (sock_writeable(sk)) |
| 358 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 359 | else |
| 360 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
| 361 | |
| 362 | pr_debug("mask 0x%x\n", mask); |
| 363 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 364 | return mask; |
| 365 | } |
| 366 | |
| 367 | static int llcp_sock_release(struct socket *sock) |
| 368 | { |
| 369 | struct sock *sk = sock->sk; |
| 370 | struct nfc_llcp_local *local; |
| 371 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 372 | int err = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 373 | |
| 374 | if (!sk) |
| 375 | return 0; |
| 376 | |
| 377 | pr_debug("%p\n", sk); |
| 378 | |
| 379 | local = llcp_sock->local; |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 380 | if (local == NULL) { |
| 381 | err = -ENODEV; |
| 382 | goto out; |
| 383 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 384 | |
| 385 | mutex_lock(&local->socket_lock); |
| 386 | |
Samuel Ortiz | 0c31835 | 2012-03-05 01:03:48 +0100 | [diff] [blame] | 387 | if (llcp_sock == local->sockets[llcp_sock->ssap]) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 388 | local->sockets[llcp_sock->ssap] = NULL; |
Samuel Ortiz | 0c31835 | 2012-03-05 01:03:48 +0100 | [diff] [blame] | 389 | else |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 390 | list_del_init(&llcp_sock->list); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 391 | |
| 392 | mutex_unlock(&local->socket_lock); |
| 393 | |
| 394 | lock_sock(sk); |
| 395 | |
| 396 | /* Send a DISC */ |
| 397 | if (sk->sk_state == LLCP_CONNECTED) |
| 398 | nfc_llcp_disconnect(llcp_sock); |
| 399 | |
| 400 | if (sk->sk_state == LLCP_LISTEN) { |
| 401 | struct nfc_llcp_sock *lsk, *n; |
| 402 | struct sock *accept_sk; |
| 403 | |
| 404 | list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 405 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 406 | accept_sk = &lsk->sk; |
| 407 | lock_sock(accept_sk); |
| 408 | |
| 409 | nfc_llcp_disconnect(lsk); |
| 410 | nfc_llcp_accept_unlink(accept_sk); |
| 411 | |
| 412 | release_sock(accept_sk); |
| 413 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 414 | sock_orphan(accept_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | /* Freeing the SAP */ |
| 419 | if ((sk->sk_state == LLCP_CONNECTED |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 420 | && llcp_sock->ssap > LLCP_LOCAL_SAP_OFFSET) || |
| 421 | sk->sk_state == LLCP_BOUND || sk->sk_state == LLCP_LISTEN) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 422 | nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap); |
| 423 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 424 | release_sock(sk); |
| 425 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 426 | out: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 427 | sock_orphan(sk); |
| 428 | sock_put(sk); |
| 429 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 430 | return err; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 434 | int len, int flags) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 435 | { |
| 436 | struct sock *sk = sock->sk; |
| 437 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 438 | struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr; |
| 439 | struct nfc_dev *dev; |
| 440 | struct nfc_llcp_local *local; |
| 441 | int ret = 0; |
| 442 | |
| 443 | pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags); |
| 444 | |
| 445 | if (!addr || len < sizeof(struct sockaddr_nfc) || |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 446 | addr->sa_family != AF_NFC) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 447 | pr_err("Invalid socket\n"); |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | |
| 451 | if (addr->service_name_len == 0 && addr->dsap == 0) { |
| 452 | pr_err("Missing service name or dsap\n"); |
| 453 | return -EINVAL; |
| 454 | } |
| 455 | |
| 456 | 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] | 457 | addr->target_idx, addr->nfc_protocol); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 458 | |
| 459 | lock_sock(sk); |
| 460 | |
| 461 | if (sk->sk_state == LLCP_CONNECTED) { |
| 462 | ret = -EISCONN; |
| 463 | goto error; |
| 464 | } |
| 465 | |
| 466 | dev = nfc_get_device(addr->dev_idx); |
| 467 | if (dev == NULL) { |
| 468 | ret = -ENODEV; |
| 469 | goto error; |
| 470 | } |
| 471 | |
| 472 | local = nfc_llcp_find_local(dev); |
| 473 | if (local == NULL) { |
| 474 | ret = -ENODEV; |
| 475 | goto put_dev; |
| 476 | } |
| 477 | |
| 478 | device_lock(&dev->dev); |
| 479 | if (dev->dep_link_up == false) { |
| 480 | ret = -ENOLINK; |
| 481 | device_unlock(&dev->dev); |
| 482 | goto put_dev; |
| 483 | } |
| 484 | device_unlock(&dev->dev); |
| 485 | |
| 486 | if (local->rf_mode == NFC_RF_INITIATOR && |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 487 | addr->target_idx != local->target_idx) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 488 | ret = -ENOLINK; |
| 489 | goto put_dev; |
| 490 | } |
| 491 | |
| 492 | llcp_sock->dev = dev; |
| 493 | llcp_sock->local = local; |
| 494 | llcp_sock->ssap = nfc_llcp_get_local_ssap(local); |
| 495 | if (llcp_sock->ssap == LLCP_SAP_MAX) { |
| 496 | ret = -ENOMEM; |
| 497 | goto put_dev; |
| 498 | } |
| 499 | if (addr->service_name_len == 0) |
| 500 | llcp_sock->dsap = addr->dsap; |
| 501 | else |
| 502 | llcp_sock->dsap = LLCP_SAP_SDP; |
| 503 | llcp_sock->nfc_protocol = addr->nfc_protocol; |
| 504 | llcp_sock->service_name_len = min_t(unsigned int, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 505 | addr->service_name_len, |
| 506 | NFC_LLCP_MAX_SERVICE_NAME); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 507 | llcp_sock->service_name = kmemdup(addr->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 508 | llcp_sock->service_name_len, |
| 509 | GFP_KERNEL); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 510 | |
| 511 | local->sockets[llcp_sock->ssap] = llcp_sock; |
| 512 | |
| 513 | ret = nfc_llcp_send_connect(llcp_sock); |
| 514 | if (ret) |
| 515 | goto put_dev; |
| 516 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 517 | ret = sock_wait_state(sk, LLCP_CONNECTED, |
| 518 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
| 519 | if (ret) |
| 520 | goto put_dev; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 521 | |
| 522 | release_sock(sk); |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 523 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 524 | return 0; |
| 525 | |
| 526 | put_dev: |
| 527 | nfc_put_device(dev); |
| 528 | |
| 529 | error: |
| 530 | release_sock(sk); |
| 531 | return ret; |
| 532 | } |
| 533 | |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 534 | static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 535 | struct msghdr *msg, size_t len) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 536 | { |
| 537 | struct sock *sk = sock->sk; |
| 538 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 539 | int ret; |
| 540 | |
| 541 | pr_debug("sock %p sk %p", sock, sk); |
| 542 | |
| 543 | ret = sock_error(sk); |
| 544 | if (ret) |
| 545 | return ret; |
| 546 | |
| 547 | if (msg->msg_flags & MSG_OOB) |
| 548 | return -EOPNOTSUPP; |
| 549 | |
| 550 | lock_sock(sk); |
| 551 | |
| 552 | if (sk->sk_state != LLCP_CONNECTED) { |
| 553 | release_sock(sk); |
| 554 | return -ENOTCONN; |
| 555 | } |
| 556 | |
| 557 | release_sock(sk); |
| 558 | |
| 559 | return nfc_llcp_send_i_frame(llcp_sock, msg, len); |
| 560 | } |
| 561 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 562 | static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 563 | struct msghdr *msg, size_t len, int flags) |
| 564 | { |
| 565 | int noblock = flags & MSG_DONTWAIT; |
| 566 | struct sock *sk = sock->sk; |
| 567 | unsigned int copied, rlen; |
| 568 | struct sk_buff *skb, *cskb; |
| 569 | int err = 0; |
| 570 | |
| 571 | pr_debug("%p %zu\n", sk, len); |
| 572 | |
| 573 | lock_sock(sk); |
| 574 | |
| 575 | if (sk->sk_state == LLCP_CLOSED && |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 576 | skb_queue_empty(&sk->sk_receive_queue)) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 577 | release_sock(sk); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | release_sock(sk); |
| 582 | |
| 583 | if (flags & (MSG_OOB)) |
| 584 | return -EOPNOTSUPP; |
| 585 | |
| 586 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 587 | if (!skb) { |
| 588 | pr_err("Recv datagram failed state %d %d %d", |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 589 | sk->sk_state, err, sock_error(sk)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 590 | |
| 591 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 592 | return 0; |
| 593 | |
| 594 | return err; |
| 595 | } |
| 596 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 597 | rlen = skb->len; /* real length of skb */ |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 598 | copied = min_t(unsigned int, rlen, len); |
| 599 | |
| 600 | cskb = skb; |
| 601 | if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) { |
| 602 | if (!(flags & MSG_PEEK)) |
| 603 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 604 | return -EFAULT; |
| 605 | } |
| 606 | |
| 607 | /* Mark read part of skb as used */ |
| 608 | if (!(flags & MSG_PEEK)) { |
| 609 | |
| 610 | /* SOCK_STREAM: re-queue skb if it contains unreceived data */ |
| 611 | if (sk->sk_type == SOCK_STREAM) { |
| 612 | skb_pull(skb, copied); |
| 613 | if (skb->len) { |
| 614 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 615 | goto done; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | kfree_skb(skb); |
| 620 | } |
| 621 | |
| 622 | /* XXX Queue backlogged skbs */ |
| 623 | |
| 624 | done: |
| 625 | /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */ |
| 626 | if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC)) |
| 627 | copied = rlen; |
| 628 | |
| 629 | return copied; |
| 630 | } |
| 631 | |
| 632 | static const struct proto_ops llcp_sock_ops = { |
| 633 | .family = PF_NFC, |
| 634 | .owner = THIS_MODULE, |
| 635 | .bind = llcp_sock_bind, |
| 636 | .connect = llcp_sock_connect, |
| 637 | .release = llcp_sock_release, |
| 638 | .socketpair = sock_no_socketpair, |
| 639 | .accept = llcp_sock_accept, |
| 640 | .getname = llcp_sock_getname, |
| 641 | .poll = llcp_sock_poll, |
| 642 | .ioctl = sock_no_ioctl, |
| 643 | .listen = llcp_sock_listen, |
| 644 | .shutdown = sock_no_shutdown, |
| 645 | .setsockopt = sock_no_setsockopt, |
| 646 | .getsockopt = sock_no_getsockopt, |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 647 | .sendmsg = llcp_sock_sendmsg, |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 648 | .recvmsg = llcp_sock_recvmsg, |
| 649 | .mmap = sock_no_mmap, |
| 650 | }; |
| 651 | |
| 652 | static void llcp_sock_destruct(struct sock *sk) |
| 653 | { |
| 654 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 655 | |
| 656 | pr_debug("%p\n", sk); |
| 657 | |
| 658 | if (sk->sk_state == LLCP_CONNECTED) |
| 659 | nfc_put_device(llcp_sock->dev); |
| 660 | |
| 661 | skb_queue_purge(&sk->sk_receive_queue); |
| 662 | |
| 663 | nfc_llcp_sock_free(llcp_sock); |
| 664 | |
| 665 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 666 | pr_err("Freeing alive NFC LLCP socket %p\n", sk); |
| 667 | return; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp) |
| 672 | { |
| 673 | struct sock *sk; |
| 674 | struct nfc_llcp_sock *llcp_sock; |
| 675 | |
| 676 | sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto); |
| 677 | if (!sk) |
| 678 | return NULL; |
| 679 | |
| 680 | llcp_sock = nfc_llcp_sock(sk); |
| 681 | |
| 682 | sock_init_data(sock, sk); |
| 683 | sk->sk_state = LLCP_CLOSED; |
| 684 | sk->sk_protocol = NFC_SOCKPROTO_LLCP; |
| 685 | sk->sk_type = type; |
| 686 | sk->sk_destruct = llcp_sock_destruct; |
| 687 | |
| 688 | llcp_sock->ssap = 0; |
| 689 | llcp_sock->dsap = LLCP_SAP_SDP; |
| 690 | llcp_sock->send_n = llcp_sock->send_ack_n = 0; |
| 691 | llcp_sock->recv_n = llcp_sock->recv_ack_n = 0; |
| 692 | llcp_sock->remote_ready = 1; |
| 693 | skb_queue_head_init(&llcp_sock->tx_queue); |
| 694 | skb_queue_head_init(&llcp_sock->tx_pending_queue); |
| 695 | skb_queue_head_init(&llcp_sock->tx_backlog_queue); |
| 696 | INIT_LIST_HEAD(&llcp_sock->list); |
| 697 | INIT_LIST_HEAD(&llcp_sock->accept_queue); |
| 698 | |
| 699 | if (sock != NULL) |
| 700 | sock->state = SS_UNCONNECTED; |
| 701 | |
| 702 | return sk; |
| 703 | } |
| 704 | |
| 705 | void nfc_llcp_sock_free(struct nfc_llcp_sock *sock) |
| 706 | { |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 707 | struct nfc_llcp_local *local = sock->local; |
| 708 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 709 | kfree(sock->service_name); |
| 710 | |
| 711 | skb_queue_purge(&sock->tx_queue); |
| 712 | skb_queue_purge(&sock->tx_pending_queue); |
| 713 | skb_queue_purge(&sock->tx_backlog_queue); |
| 714 | |
| 715 | list_del_init(&sock->accept_queue); |
| 716 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 717 | if (local != NULL && sock == local->sockets[sock->ssap]) |
| 718 | local->sockets[sock->ssap] = NULL; |
| 719 | else |
| 720 | list_del_init(&sock->list); |
| 721 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 722 | sock->parent = NULL; |
| 723 | } |
| 724 | |
| 725 | static int llcp_sock_create(struct net *net, struct socket *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 726 | const struct nfc_protocol *nfc_proto) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 727 | { |
| 728 | struct sock *sk; |
| 729 | |
| 730 | pr_debug("%p\n", sock); |
| 731 | |
| 732 | if (sock->type != SOCK_STREAM && sock->type != SOCK_DGRAM) |
| 733 | return -ESOCKTNOSUPPORT; |
| 734 | |
| 735 | sock->ops = &llcp_sock_ops; |
| 736 | |
| 737 | sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC); |
| 738 | if (sk == NULL) |
| 739 | return -ENOMEM; |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static const struct nfc_protocol llcp_nfc_proto = { |
| 745 | .id = NFC_SOCKPROTO_LLCP, |
| 746 | .proto = &llcp_sock_proto, |
| 747 | .owner = THIS_MODULE, |
| 748 | .create = llcp_sock_create |
| 749 | }; |
| 750 | |
| 751 | int __init nfc_llcp_sock_init(void) |
| 752 | { |
| 753 | return nfc_proto_register(&llcp_nfc_proto); |
| 754 | } |
| 755 | |
| 756 | void nfc_llcp_sock_exit(void) |
| 757 | { |
| 758 | nfc_proto_unregister(&llcp_nfc_proto); |
| 759 | } |