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 | 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 | |
| 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 | |
| 226 | void nfc_llcp_accept_unlink(struct sock *sk) |
| 227 | { |
| 228 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 229 | |
| 230 | pr_debug("state %d\n", sk->sk_state); |
| 231 | |
| 232 | list_del_init(&llcp_sock->accept_queue); |
| 233 | sk_acceptq_removed(llcp_sock->parent); |
| 234 | llcp_sock->parent = NULL; |
| 235 | |
| 236 | sock_put(sk); |
| 237 | } |
| 238 | |
| 239 | void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk) |
| 240 | { |
| 241 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 242 | struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent); |
| 243 | |
| 244 | /* Lock will be free from unlink */ |
| 245 | sock_hold(sk); |
| 246 | |
| 247 | list_add_tail(&llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 248 | &llcp_sock_parent->accept_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 249 | llcp_sock->parent = parent; |
| 250 | sk_acceptq_added(parent); |
| 251 | } |
| 252 | |
| 253 | struct sock *nfc_llcp_accept_dequeue(struct sock *parent, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 254 | struct socket *newsock) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 255 | { |
| 256 | struct nfc_llcp_sock *lsk, *n, *llcp_parent; |
| 257 | struct sock *sk; |
| 258 | |
| 259 | llcp_parent = nfc_llcp_sock(parent); |
| 260 | |
| 261 | list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 262 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 263 | sk = &lsk->sk; |
| 264 | lock_sock(sk); |
| 265 | |
| 266 | if (sk->sk_state == LLCP_CLOSED) { |
| 267 | release_sock(sk); |
| 268 | nfc_llcp_accept_unlink(sk); |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | if (sk->sk_state == LLCP_CONNECTED || !newsock) { |
| 273 | nfc_llcp_accept_unlink(sk); |
| 274 | if (newsock) |
| 275 | sock_graft(sk, newsock); |
| 276 | |
| 277 | release_sock(sk); |
| 278 | |
| 279 | pr_debug("Returning sk state %d\n", sk->sk_state); |
| 280 | |
| 281 | return sk; |
| 282 | } |
| 283 | |
| 284 | release_sock(sk); |
| 285 | } |
| 286 | |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | static int llcp_sock_accept(struct socket *sock, struct socket *newsock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 291 | int flags) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 292 | { |
| 293 | DECLARE_WAITQUEUE(wait, current); |
| 294 | struct sock *sk = sock->sk, *new_sk; |
| 295 | long timeo; |
| 296 | int ret = 0; |
| 297 | |
| 298 | pr_debug("parent %p\n", sk); |
| 299 | |
| 300 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 301 | |
| 302 | if (sk->sk_state != LLCP_LISTEN) { |
| 303 | ret = -EBADFD; |
| 304 | goto error; |
| 305 | } |
| 306 | |
| 307 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 308 | |
| 309 | /* Wait for an incoming connection. */ |
| 310 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 311 | while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) { |
| 312 | set_current_state(TASK_INTERRUPTIBLE); |
| 313 | |
| 314 | if (!timeo) { |
| 315 | ret = -EAGAIN; |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | if (signal_pending(current)) { |
| 320 | ret = sock_intr_errno(timeo); |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | release_sock(sk); |
| 325 | timeo = schedule_timeout(timeo); |
| 326 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 327 | } |
| 328 | __set_current_state(TASK_RUNNING); |
| 329 | remove_wait_queue(sk_sleep(sk), &wait); |
| 330 | |
| 331 | if (ret) |
| 332 | goto error; |
| 333 | |
| 334 | newsock->state = SS_CONNECTED; |
| 335 | |
| 336 | pr_debug("new socket %p\n", new_sk); |
| 337 | |
| 338 | error: |
| 339 | release_sock(sk); |
| 340 | |
| 341 | return ret; |
| 342 | } |
| 343 | |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 344 | static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr, |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 345 | int *len, int peer) |
| 346 | { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 347 | struct sock *sk = sock->sk; |
| 348 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 349 | DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 350 | |
Samuel Ortiz | fe3c094 | 2012-07-05 17:43:08 +0200 | [diff] [blame] | 351 | if (llcp_sock == NULL || llcp_sock->dev == NULL) |
| 352 | return -EBADFD; |
| 353 | |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 354 | pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx, |
| 355 | llcp_sock->dsap, llcp_sock->ssap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 356 | |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 357 | uaddr->sa_family = AF_NFC; |
| 358 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 359 | *len = sizeof(struct sockaddr_nfc_llcp); |
| 360 | |
| 361 | llcp_addr->dev_idx = llcp_sock->dev->idx; |
Samuel Ortiz | 12e5bdf | 2012-06-21 17:41:42 +0200 | [diff] [blame] | 362 | llcp_addr->target_idx = llcp_sock->target_idx; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 363 | llcp_addr->dsap = llcp_sock->dsap; |
| 364 | llcp_addr->ssap = llcp_sock->ssap; |
| 365 | llcp_addr->service_name_len = llcp_sock->service_name_len; |
| 366 | memcpy(llcp_addr->service_name, llcp_sock->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 367 | llcp_addr->service_name_len); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static inline unsigned int llcp_accept_poll(struct sock *parent) |
| 373 | { |
| 374 | struct nfc_llcp_sock *llcp_sock, *n, *parent_sock; |
| 375 | struct sock *sk; |
| 376 | |
| 377 | parent_sock = nfc_llcp_sock(parent); |
| 378 | |
| 379 | list_for_each_entry_safe(llcp_sock, n, &parent_sock->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 = &llcp_sock->sk; |
| 382 | |
| 383 | if (sk->sk_state == LLCP_CONNECTED) |
| 384 | return POLLIN | POLLRDNORM; |
| 385 | } |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static unsigned int llcp_sock_poll(struct file *file, struct socket *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 391 | poll_table *wait) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 392 | { |
| 393 | struct sock *sk = sock->sk; |
| 394 | unsigned int mask = 0; |
| 395 | |
| 396 | pr_debug("%p\n", sk); |
| 397 | |
| 398 | sock_poll_wait(file, sk_sleep(sk), wait); |
| 399 | |
| 400 | if (sk->sk_state == LLCP_LISTEN) |
| 401 | return llcp_accept_poll(sk); |
| 402 | |
| 403 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
| 404 | mask |= POLLERR; |
| 405 | |
| 406 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 407 | mask |= POLLIN | POLLRDNORM; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 408 | |
| 409 | if (sk->sk_state == LLCP_CLOSED) |
| 410 | mask |= POLLHUP; |
| 411 | |
Samuel Ortiz | 4260c13 | 2012-05-07 12:31:20 +0200 | [diff] [blame] | 412 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 413 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
| 414 | |
| 415 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 416 | mask |= POLLHUP; |
| 417 | |
| 418 | if (sock_writeable(sk)) |
| 419 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 420 | else |
| 421 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
| 422 | |
| 423 | pr_debug("mask 0x%x\n", mask); |
| 424 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 425 | return mask; |
| 426 | } |
| 427 | |
| 428 | static int llcp_sock_release(struct socket *sock) |
| 429 | { |
| 430 | struct sock *sk = sock->sk; |
| 431 | struct nfc_llcp_local *local; |
| 432 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 433 | int err = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 434 | |
| 435 | if (!sk) |
| 436 | return 0; |
| 437 | |
| 438 | pr_debug("%p\n", sk); |
| 439 | |
| 440 | local = llcp_sock->local; |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 441 | if (local == NULL) { |
| 442 | err = -ENODEV; |
| 443 | goto out; |
| 444 | } |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 445 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 446 | lock_sock(sk); |
| 447 | |
| 448 | /* Send a DISC */ |
| 449 | if (sk->sk_state == LLCP_CONNECTED) |
| 450 | nfc_llcp_disconnect(llcp_sock); |
| 451 | |
| 452 | if (sk->sk_state == LLCP_LISTEN) { |
| 453 | struct nfc_llcp_sock *lsk, *n; |
| 454 | struct sock *accept_sk; |
| 455 | |
| 456 | list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 457 | accept_queue) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 458 | accept_sk = &lsk->sk; |
| 459 | lock_sock(accept_sk); |
| 460 | |
| 461 | nfc_llcp_disconnect(lsk); |
| 462 | nfc_llcp_accept_unlink(accept_sk); |
| 463 | |
| 464 | release_sock(accept_sk); |
| 465 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 466 | sock_orphan(accept_sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 470 | if (llcp_sock->reserved_ssap < LLCP_SAP_MAX) |
| 471 | nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 472 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 473 | release_sock(sk); |
| 474 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 475 | if (sock->type == SOCK_RAW) |
| 476 | nfc_llcp_sock_unlink(&local->raw_sockets, sk); |
| 477 | else |
| 478 | nfc_llcp_sock_unlink(&local->sockets, sk); |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 479 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 480 | out: |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 481 | sock_orphan(sk); |
| 482 | sock_put(sk); |
| 483 | |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 484 | return err; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 488 | int len, int flags) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 489 | { |
| 490 | struct sock *sk = sock->sk; |
| 491 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 492 | struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr; |
| 493 | struct nfc_dev *dev; |
| 494 | struct nfc_llcp_local *local; |
| 495 | int ret = 0; |
| 496 | |
| 497 | pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags); |
| 498 | |
| 499 | if (!addr || len < sizeof(struct sockaddr_nfc) || |
Dave Jones | 32418cf | 2012-10-04 09:51:11 +0000 | [diff] [blame] | 500 | addr->sa_family != AF_NFC) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 501 | return -EINVAL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 502 | |
Dave Jones | 32418cf | 2012-10-04 09:51:11 +0000 | [diff] [blame] | 503 | if (addr->service_name_len == 0 && addr->dsap == 0) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 504 | return -EINVAL; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 505 | |
| 506 | 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] | 507 | addr->target_idx, addr->nfc_protocol); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 508 | |
| 509 | lock_sock(sk); |
| 510 | |
| 511 | if (sk->sk_state == LLCP_CONNECTED) { |
| 512 | ret = -EISCONN; |
| 513 | goto error; |
| 514 | } |
| 515 | |
| 516 | dev = nfc_get_device(addr->dev_idx); |
| 517 | if (dev == NULL) { |
| 518 | ret = -ENODEV; |
| 519 | goto error; |
| 520 | } |
| 521 | |
| 522 | local = nfc_llcp_find_local(dev); |
| 523 | if (local == NULL) { |
| 524 | ret = -ENODEV; |
| 525 | goto put_dev; |
| 526 | } |
| 527 | |
| 528 | device_lock(&dev->dev); |
| 529 | if (dev->dep_link_up == false) { |
| 530 | ret = -ENOLINK; |
| 531 | device_unlock(&dev->dev); |
| 532 | goto put_dev; |
| 533 | } |
| 534 | device_unlock(&dev->dev); |
| 535 | |
| 536 | if (local->rf_mode == NFC_RF_INITIATOR && |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 537 | addr->target_idx != local->target_idx) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 538 | ret = -ENOLINK; |
| 539 | goto put_dev; |
| 540 | } |
| 541 | |
| 542 | llcp_sock->dev = dev; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 543 | llcp_sock->local = nfc_llcp_local_get(local); |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 544 | llcp_sock->miu = llcp_sock->local->remote_miu; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 545 | llcp_sock->ssap = nfc_llcp_get_local_ssap(local); |
| 546 | if (llcp_sock->ssap == LLCP_SAP_MAX) { |
| 547 | ret = -ENOMEM; |
| 548 | goto put_dev; |
| 549 | } |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 550 | |
| 551 | llcp_sock->reserved_ssap = llcp_sock->ssap; |
| 552 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 553 | if (addr->service_name_len == 0) |
| 554 | llcp_sock->dsap = addr->dsap; |
| 555 | else |
| 556 | llcp_sock->dsap = LLCP_SAP_SDP; |
| 557 | llcp_sock->nfc_protocol = addr->nfc_protocol; |
| 558 | llcp_sock->service_name_len = min_t(unsigned int, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 559 | addr->service_name_len, |
| 560 | NFC_LLCP_MAX_SERVICE_NAME); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 561 | llcp_sock->service_name = kmemdup(addr->service_name, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 562 | llcp_sock->service_name_len, |
| 563 | GFP_KERNEL); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 564 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 565 | nfc_llcp_sock_link(&local->connecting_sockets, sk); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 566 | |
| 567 | ret = nfc_llcp_send_connect(llcp_sock); |
| 568 | if (ret) |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 569 | goto sock_unlink; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 570 | |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 571 | ret = sock_wait_state(sk, LLCP_CONNECTED, |
| 572 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
| 573 | if (ret) |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 574 | goto sock_unlink; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 575 | |
| 576 | release_sock(sk); |
Samuel Ortiz | ff353d8 | 2012-05-07 12:31:19 +0200 | [diff] [blame] | 577 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 578 | return 0; |
| 579 | |
Samuel Ortiz | a69f32a | 2012-05-04 17:04:19 +0200 | [diff] [blame] | 580 | sock_unlink: |
| 581 | nfc_llcp_put_ssap(local, llcp_sock->ssap); |
| 582 | |
| 583 | nfc_llcp_sock_unlink(&local->connecting_sockets, sk); |
| 584 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 585 | put_dev: |
| 586 | nfc_put_device(dev); |
| 587 | |
| 588 | error: |
| 589 | release_sock(sk); |
| 590 | return ret; |
| 591 | } |
| 592 | |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 593 | static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 594 | struct msghdr *msg, size_t len) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 595 | { |
| 596 | struct sock *sk = sock->sk; |
| 597 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 598 | int ret; |
| 599 | |
| 600 | pr_debug("sock %p sk %p", sock, sk); |
| 601 | |
| 602 | ret = sock_error(sk); |
| 603 | if (ret) |
| 604 | return ret; |
| 605 | |
| 606 | if (msg->msg_flags & MSG_OOB) |
| 607 | return -EOPNOTSUPP; |
| 608 | |
| 609 | lock_sock(sk); |
| 610 | |
| 611 | if (sk->sk_state != LLCP_CONNECTED) { |
| 612 | release_sock(sk); |
| 613 | return -ENOTCONN; |
| 614 | } |
| 615 | |
| 616 | release_sock(sk); |
| 617 | |
| 618 | return nfc_llcp_send_i_frame(llcp_sock, msg, len); |
| 619 | } |
| 620 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 621 | static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 622 | struct msghdr *msg, size_t len, int flags) |
| 623 | { |
| 624 | int noblock = flags & MSG_DONTWAIT; |
| 625 | struct sock *sk = sock->sk; |
| 626 | unsigned int copied, rlen; |
| 627 | struct sk_buff *skb, *cskb; |
| 628 | int err = 0; |
| 629 | |
| 630 | pr_debug("%p %zu\n", sk, len); |
| 631 | |
| 632 | lock_sock(sk); |
| 633 | |
| 634 | if (sk->sk_state == LLCP_CLOSED && |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 635 | skb_queue_empty(&sk->sk_receive_queue)) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 636 | release_sock(sk); |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | release_sock(sk); |
| 641 | |
| 642 | if (flags & (MSG_OOB)) |
| 643 | return -EOPNOTSUPP; |
| 644 | |
| 645 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 646 | if (!skb) { |
| 647 | pr_err("Recv datagram failed state %d %d %d", |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 648 | sk->sk_state, err, sock_error(sk)); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 649 | |
| 650 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 651 | return 0; |
| 652 | |
| 653 | return err; |
| 654 | } |
| 655 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 656 | rlen = skb->len; /* real length of skb */ |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 657 | copied = min_t(unsigned int, rlen, len); |
| 658 | |
| 659 | cskb = skb; |
| 660 | if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) { |
| 661 | if (!(flags & MSG_PEEK)) |
| 662 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 663 | return -EFAULT; |
| 664 | } |
| 665 | |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame^] | 666 | if (sk->sk_type == SOCK_DGRAM && msg->msg_name) { |
| 667 | struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb); |
| 668 | struct sockaddr_nfc_llcp sockaddr; |
| 669 | |
| 670 | pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap); |
| 671 | |
| 672 | sockaddr.sa_family = AF_NFC; |
| 673 | sockaddr.nfc_protocol = NFC_PROTO_NFC_DEP; |
| 674 | sockaddr.dsap = ui_cb->dsap; |
| 675 | sockaddr.ssap = ui_cb->ssap; |
| 676 | |
| 677 | memcpy(msg->msg_name, &sockaddr, sizeof(sockaddr)); |
| 678 | msg->msg_namelen = sizeof(sockaddr); |
| 679 | } |
| 680 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 681 | /* Mark read part of skb as used */ |
| 682 | if (!(flags & MSG_PEEK)) { |
| 683 | |
| 684 | /* SOCK_STREAM: re-queue skb if it contains unreceived data */ |
Samuel Ortiz | 31ca61a | 2012-10-15 17:44:44 +0200 | [diff] [blame^] | 685 | if (sk->sk_type == SOCK_STREAM || |
| 686 | sk->sk_type == SOCK_DGRAM || |
| 687 | sk->sk_type == SOCK_RAW) { |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 688 | skb_pull(skb, copied); |
| 689 | if (skb->len) { |
| 690 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 691 | goto done; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | kfree_skb(skb); |
| 696 | } |
| 697 | |
| 698 | /* XXX Queue backlogged skbs */ |
| 699 | |
| 700 | done: |
| 701 | /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */ |
| 702 | if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC)) |
| 703 | copied = rlen; |
| 704 | |
| 705 | return copied; |
| 706 | } |
| 707 | |
| 708 | static const struct proto_ops llcp_sock_ops = { |
| 709 | .family = PF_NFC, |
| 710 | .owner = THIS_MODULE, |
| 711 | .bind = llcp_sock_bind, |
| 712 | .connect = llcp_sock_connect, |
| 713 | .release = llcp_sock_release, |
| 714 | .socketpair = sock_no_socketpair, |
| 715 | .accept = llcp_sock_accept, |
| 716 | .getname = llcp_sock_getname, |
| 717 | .poll = llcp_sock_poll, |
| 718 | .ioctl = sock_no_ioctl, |
| 719 | .listen = llcp_sock_listen, |
| 720 | .shutdown = sock_no_shutdown, |
| 721 | .setsockopt = sock_no_setsockopt, |
| 722 | .getsockopt = sock_no_getsockopt, |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 723 | .sendmsg = llcp_sock_sendmsg, |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 724 | .recvmsg = llcp_sock_recvmsg, |
| 725 | .mmap = sock_no_mmap, |
| 726 | }; |
| 727 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 728 | static const struct proto_ops llcp_rawsock_ops = { |
| 729 | .family = PF_NFC, |
| 730 | .owner = THIS_MODULE, |
| 731 | .bind = llcp_raw_sock_bind, |
| 732 | .connect = sock_no_connect, |
| 733 | .release = llcp_sock_release, |
| 734 | .socketpair = sock_no_socketpair, |
| 735 | .accept = sock_no_accept, |
| 736 | .getname = llcp_sock_getname, |
| 737 | .poll = llcp_sock_poll, |
| 738 | .ioctl = sock_no_ioctl, |
| 739 | .listen = sock_no_listen, |
| 740 | .shutdown = sock_no_shutdown, |
| 741 | .setsockopt = sock_no_setsockopt, |
| 742 | .getsockopt = sock_no_getsockopt, |
| 743 | .sendmsg = sock_no_sendmsg, |
| 744 | .recvmsg = llcp_sock_recvmsg, |
| 745 | .mmap = sock_no_mmap, |
| 746 | }; |
| 747 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 748 | static void llcp_sock_destruct(struct sock *sk) |
| 749 | { |
| 750 | struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); |
| 751 | |
| 752 | pr_debug("%p\n", sk); |
| 753 | |
| 754 | if (sk->sk_state == LLCP_CONNECTED) |
| 755 | nfc_put_device(llcp_sock->dev); |
| 756 | |
| 757 | skb_queue_purge(&sk->sk_receive_queue); |
| 758 | |
| 759 | nfc_llcp_sock_free(llcp_sock); |
| 760 | |
| 761 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 762 | pr_err("Freeing alive NFC LLCP socket %p\n", sk); |
| 763 | return; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp) |
| 768 | { |
| 769 | struct sock *sk; |
| 770 | struct nfc_llcp_sock *llcp_sock; |
| 771 | |
| 772 | sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto); |
| 773 | if (!sk) |
| 774 | return NULL; |
| 775 | |
| 776 | llcp_sock = nfc_llcp_sock(sk); |
| 777 | |
| 778 | sock_init_data(sock, sk); |
| 779 | sk->sk_state = LLCP_CLOSED; |
| 780 | sk->sk_protocol = NFC_SOCKPROTO_LLCP; |
| 781 | sk->sk_type = type; |
| 782 | sk->sk_destruct = llcp_sock_destruct; |
| 783 | |
| 784 | llcp_sock->ssap = 0; |
| 785 | llcp_sock->dsap = LLCP_SAP_SDP; |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 786 | llcp_sock->rw = LLCP_DEFAULT_RW; |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame] | 787 | llcp_sock->miu = LLCP_DEFAULT_MIU; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 788 | llcp_sock->send_n = llcp_sock->send_ack_n = 0; |
| 789 | llcp_sock->recv_n = llcp_sock->recv_ack_n = 0; |
| 790 | llcp_sock->remote_ready = 1; |
Samuel Ortiz | cbbf472 | 2012-06-22 14:48:11 +0200 | [diff] [blame] | 791 | llcp_sock->reserved_ssap = LLCP_SAP_MAX; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 792 | skb_queue_head_init(&llcp_sock->tx_queue); |
| 793 | skb_queue_head_init(&llcp_sock->tx_pending_queue); |
| 794 | skb_queue_head_init(&llcp_sock->tx_backlog_queue); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 795 | INIT_LIST_HEAD(&llcp_sock->accept_queue); |
| 796 | |
| 797 | if (sock != NULL) |
| 798 | sock->state = SS_UNCONNECTED; |
| 799 | |
| 800 | return sk; |
| 801 | } |
| 802 | |
| 803 | void nfc_llcp_sock_free(struct nfc_llcp_sock *sock) |
| 804 | { |
| 805 | kfree(sock->service_name); |
| 806 | |
| 807 | skb_queue_purge(&sock->tx_queue); |
| 808 | skb_queue_purge(&sock->tx_pending_queue); |
| 809 | skb_queue_purge(&sock->tx_backlog_queue); |
| 810 | |
| 811 | list_del_init(&sock->accept_queue); |
Samuel Ortiz | 40c75f8 | 2012-03-05 01:03:51 +0100 | [diff] [blame] | 812 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 813 | sock->parent = NULL; |
Samuel Ortiz | c7aa122 | 2012-05-04 11:24:16 +0200 | [diff] [blame] | 814 | |
| 815 | nfc_llcp_local_put(sock->local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static int llcp_sock_create(struct net *net, struct socket *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 819 | const struct nfc_protocol *nfc_proto) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 820 | { |
| 821 | struct sock *sk; |
| 822 | |
| 823 | pr_debug("%p\n", sock); |
| 824 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 825 | if (sock->type != SOCK_STREAM && |
| 826 | sock->type != SOCK_DGRAM && |
| 827 | sock->type != SOCK_RAW) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 828 | return -ESOCKTNOSUPPORT; |
| 829 | |
Thierry Escande | 4463523 | 2012-09-26 18:16:44 +0200 | [diff] [blame] | 830 | if (sock->type == SOCK_RAW) |
| 831 | sock->ops = &llcp_rawsock_ops; |
| 832 | else |
| 833 | sock->ops = &llcp_sock_ops; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 834 | |
| 835 | sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC); |
| 836 | if (sk == NULL) |
| 837 | return -ENOMEM; |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static const struct nfc_protocol llcp_nfc_proto = { |
| 843 | .id = NFC_SOCKPROTO_LLCP, |
| 844 | .proto = &llcp_sock_proto, |
| 845 | .owner = THIS_MODULE, |
| 846 | .create = llcp_sock_create |
| 847 | }; |
| 848 | |
| 849 | int __init nfc_llcp_sock_init(void) |
| 850 | { |
| 851 | return nfc_proto_register(&llcp_nfc_proto); |
| 852 | } |
| 853 | |
| 854 | void nfc_llcp_sock_exit(void) |
| 855 | { |
| 856 | nfc_proto_unregister(&llcp_nfc_proto); |
| 857 | } |