blob: 211cb234f7a39288738211fec9779b7ceeccfdc9 [file] [log] [blame]
Samuel Ortizd6469602011-12-14 16:43:12 +01001/*
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 Ortizff353d82012-05-07 12:31:19 +020030static 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 Ortizd6469602011-12-14 16:43:12 +010066static struct proto llcp_sock_proto = {
67 .name = "NFC_LLCP",
68 .owner = THIS_MODULE,
69 .obj_size = sizeof(struct nfc_llcp_sock),
70};
71
72static 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;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200114 llcp_sock->local = nfc_llcp_local_get(local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100115 llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
116 llcp_sock->service_name_len = min_t(unsigned int,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100117 llcp_addr.service_name_len,
118 NFC_LLCP_MAX_SERVICE_NAME);
Samuel Ortizd6469602011-12-14 16:43:12 +0100119 llcp_sock->service_name = kmemdup(llcp_addr.service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100120 llcp_sock->service_name_len,
121 GFP_KERNEL);
Samuel Ortizd6469602011-12-14 16:43:12 +0100122
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
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200127 llcp_sock->reserved_ssap = llcp_sock->ssap;
128
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200129 nfc_llcp_sock_link(&local->sockets, sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100130
131 pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap);
132
133 sk->sk_state = LLCP_BOUND;
134
135put_dev:
136 nfc_put_device(dev);
137
138error:
139 release_sock(sk);
140 return ret;
141}
142
143static int llcp_sock_listen(struct socket *sock, int backlog)
144{
145 struct sock *sk = sock->sk;
146 int ret = 0;
147
148 pr_debug("sk %p backlog %d\n", sk, backlog);
149
150 lock_sock(sk);
151
152 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM)
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100153 || sk->sk_state != LLCP_BOUND) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100154 ret = -EBADFD;
155 goto error;
156 }
157
158 sk->sk_max_ack_backlog = backlog;
159 sk->sk_ack_backlog = 0;
160
161 pr_debug("Socket listening\n");
162 sk->sk_state = LLCP_LISTEN;
163
164error:
165 release_sock(sk);
166
167 return ret;
168}
169
170void nfc_llcp_accept_unlink(struct sock *sk)
171{
172 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
173
174 pr_debug("state %d\n", sk->sk_state);
175
176 list_del_init(&llcp_sock->accept_queue);
177 sk_acceptq_removed(llcp_sock->parent);
178 llcp_sock->parent = NULL;
179
180 sock_put(sk);
181}
182
183void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk)
184{
185 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
186 struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent);
187
188 /* Lock will be free from unlink */
189 sock_hold(sk);
190
191 list_add_tail(&llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100192 &llcp_sock_parent->accept_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100193 llcp_sock->parent = parent;
194 sk_acceptq_added(parent);
195}
196
197struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100198 struct socket *newsock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100199{
200 struct nfc_llcp_sock *lsk, *n, *llcp_parent;
201 struct sock *sk;
202
203 llcp_parent = nfc_llcp_sock(parent);
204
205 list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100206 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100207 sk = &lsk->sk;
208 lock_sock(sk);
209
210 if (sk->sk_state == LLCP_CLOSED) {
211 release_sock(sk);
212 nfc_llcp_accept_unlink(sk);
213 continue;
214 }
215
216 if (sk->sk_state == LLCP_CONNECTED || !newsock) {
217 nfc_llcp_accept_unlink(sk);
218 if (newsock)
219 sock_graft(sk, newsock);
220
221 release_sock(sk);
222
223 pr_debug("Returning sk state %d\n", sk->sk_state);
224
225 return sk;
226 }
227
228 release_sock(sk);
229 }
230
231 return NULL;
232}
233
234static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100235 int flags)
Samuel Ortizd6469602011-12-14 16:43:12 +0100236{
237 DECLARE_WAITQUEUE(wait, current);
238 struct sock *sk = sock->sk, *new_sk;
239 long timeo;
240 int ret = 0;
241
242 pr_debug("parent %p\n", sk);
243
244 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
245
246 if (sk->sk_state != LLCP_LISTEN) {
247 ret = -EBADFD;
248 goto error;
249 }
250
251 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
252
253 /* Wait for an incoming connection. */
254 add_wait_queue_exclusive(sk_sleep(sk), &wait);
255 while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
256 set_current_state(TASK_INTERRUPTIBLE);
257
258 if (!timeo) {
259 ret = -EAGAIN;
260 break;
261 }
262
263 if (signal_pending(current)) {
264 ret = sock_intr_errno(timeo);
265 break;
266 }
267
268 release_sock(sk);
269 timeo = schedule_timeout(timeo);
270 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
271 }
272 __set_current_state(TASK_RUNNING);
273 remove_wait_queue(sk_sleep(sk), &wait);
274
275 if (ret)
276 goto error;
277
278 newsock->state = SS_CONNECTED;
279
280 pr_debug("new socket %p\n", new_sk);
281
282error:
283 release_sock(sk);
284
285 return ret;
286}
287
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200288static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
Samuel Ortizd6469602011-12-14 16:43:12 +0100289 int *len, int peer)
290{
Samuel Ortizd6469602011-12-14 16:43:12 +0100291 struct sock *sk = sock->sk;
292 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200293 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr);
Samuel Ortizd6469602011-12-14 16:43:12 +0100294
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200295 pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx,
296 llcp_sock->dsap, llcp_sock->ssap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100297
Sasha Levin147f20e2012-06-30 11:56:47 +0200298 if (llcp_sock == NULL || llcp_sock->dev == NULL)
Sasha Levin58d1eab2012-06-06 23:02:55 +0200299 return -EBADFD;
300
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200301 uaddr->sa_family = AF_NFC;
302
Samuel Ortizd6469602011-12-14 16:43:12 +0100303 *len = sizeof(struct sockaddr_nfc_llcp);
304
305 llcp_addr->dev_idx = llcp_sock->dev->idx;
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200306 llcp_addr->target_idx = llcp_sock->target_idx;
Samuel Ortizd6469602011-12-14 16:43:12 +0100307 llcp_addr->dsap = llcp_sock->dsap;
308 llcp_addr->ssap = llcp_sock->ssap;
309 llcp_addr->service_name_len = llcp_sock->service_name_len;
310 memcpy(llcp_addr->service_name, llcp_sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100311 llcp_addr->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100312
313 return 0;
314}
315
316static inline unsigned int llcp_accept_poll(struct sock *parent)
317{
318 struct nfc_llcp_sock *llcp_sock, *n, *parent_sock;
319 struct sock *sk;
320
321 parent_sock = nfc_llcp_sock(parent);
322
323 list_for_each_entry_safe(llcp_sock, n, &parent_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100324 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100325 sk = &llcp_sock->sk;
326
327 if (sk->sk_state == LLCP_CONNECTED)
328 return POLLIN | POLLRDNORM;
329 }
330
331 return 0;
332}
333
334static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100335 poll_table *wait)
Samuel Ortizd6469602011-12-14 16:43:12 +0100336{
337 struct sock *sk = sock->sk;
338 unsigned int mask = 0;
339
340 pr_debug("%p\n", sk);
341
342 sock_poll_wait(file, sk_sleep(sk), wait);
343
344 if (sk->sk_state == LLCP_LISTEN)
345 return llcp_accept_poll(sk);
346
347 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
348 mask |= POLLERR;
349
350 if (!skb_queue_empty(&sk->sk_receive_queue))
Samuel Ortiz4260c132012-05-07 12:31:20 +0200351 mask |= POLLIN | POLLRDNORM;
Samuel Ortizd6469602011-12-14 16:43:12 +0100352
353 if (sk->sk_state == LLCP_CLOSED)
354 mask |= POLLHUP;
355
Samuel Ortiz4260c132012-05-07 12:31:20 +0200356 if (sk->sk_shutdown & RCV_SHUTDOWN)
357 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
358
359 if (sk->sk_shutdown == SHUTDOWN_MASK)
360 mask |= POLLHUP;
361
362 if (sock_writeable(sk))
363 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
364 else
365 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
366
367 pr_debug("mask 0x%x\n", mask);
368
Samuel Ortizd6469602011-12-14 16:43:12 +0100369 return mask;
370}
371
372static int llcp_sock_release(struct socket *sock)
373{
374 struct sock *sk = sock->sk;
375 struct nfc_llcp_local *local;
376 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100377 int err = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100378
379 if (!sk)
380 return 0;
381
382 pr_debug("%p\n", sk);
383
384 local = llcp_sock->local;
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100385 if (local == NULL) {
386 err = -ENODEV;
387 goto out;
388 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100389
Samuel Ortizd6469602011-12-14 16:43:12 +0100390 lock_sock(sk);
391
392 /* Send a DISC */
393 if (sk->sk_state == LLCP_CONNECTED)
394 nfc_llcp_disconnect(llcp_sock);
395
396 if (sk->sk_state == LLCP_LISTEN) {
397 struct nfc_llcp_sock *lsk, *n;
398 struct sock *accept_sk;
399
400 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100401 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100402 accept_sk = &lsk->sk;
403 lock_sock(accept_sk);
404
405 nfc_llcp_disconnect(lsk);
406 nfc_llcp_accept_unlink(accept_sk);
407
408 release_sock(accept_sk);
409
Samuel Ortizd6469602011-12-14 16:43:12 +0100410 sock_orphan(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100411 }
412 }
413
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200414 if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
415 nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100416
Samuel Ortizd6469602011-12-14 16:43:12 +0100417 release_sock(sk);
418
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200419 nfc_llcp_sock_unlink(&local->sockets, sk);
420
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100421out:
Samuel Ortizd6469602011-12-14 16:43:12 +0100422 sock_orphan(sk);
423 sock_put(sk);
424
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100425 return err;
Samuel Ortizd6469602011-12-14 16:43:12 +0100426}
427
428static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100429 int len, int flags)
Samuel Ortizd6469602011-12-14 16:43:12 +0100430{
431 struct sock *sk = sock->sk;
432 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
433 struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
434 struct nfc_dev *dev;
435 struct nfc_llcp_local *local;
436 int ret = 0;
437
438 pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
439
440 if (!addr || len < sizeof(struct sockaddr_nfc) ||
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100441 addr->sa_family != AF_NFC) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100442 pr_err("Invalid socket\n");
443 return -EINVAL;
444 }
445
446 if (addr->service_name_len == 0 && addr->dsap == 0) {
447 pr_err("Missing service name or dsap\n");
448 return -EINVAL;
449 }
450
451 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100452 addr->target_idx, addr->nfc_protocol);
Samuel Ortizd6469602011-12-14 16:43:12 +0100453
454 lock_sock(sk);
455
456 if (sk->sk_state == LLCP_CONNECTED) {
457 ret = -EISCONN;
458 goto error;
459 }
460
461 dev = nfc_get_device(addr->dev_idx);
462 if (dev == NULL) {
463 ret = -ENODEV;
464 goto error;
465 }
466
467 local = nfc_llcp_find_local(dev);
468 if (local == NULL) {
469 ret = -ENODEV;
470 goto put_dev;
471 }
472
473 device_lock(&dev->dev);
474 if (dev->dep_link_up == false) {
475 ret = -ENOLINK;
476 device_unlock(&dev->dev);
477 goto put_dev;
478 }
479 device_unlock(&dev->dev);
480
481 if (local->rf_mode == NFC_RF_INITIATOR &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100482 addr->target_idx != local->target_idx) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100483 ret = -ENOLINK;
484 goto put_dev;
485 }
486
487 llcp_sock->dev = dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200488 llcp_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200489 llcp_sock->miu = llcp_sock->local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100490 llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
491 if (llcp_sock->ssap == LLCP_SAP_MAX) {
492 ret = -ENOMEM;
493 goto put_dev;
494 }
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200495
496 llcp_sock->reserved_ssap = llcp_sock->ssap;
497
Samuel Ortizd6469602011-12-14 16:43:12 +0100498 if (addr->service_name_len == 0)
499 llcp_sock->dsap = addr->dsap;
500 else
501 llcp_sock->dsap = LLCP_SAP_SDP;
502 llcp_sock->nfc_protocol = addr->nfc_protocol;
503 llcp_sock->service_name_len = min_t(unsigned int,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100504 addr->service_name_len,
505 NFC_LLCP_MAX_SERVICE_NAME);
Samuel Ortizd6469602011-12-14 16:43:12 +0100506 llcp_sock->service_name = kmemdup(addr->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100507 llcp_sock->service_name_len,
508 GFP_KERNEL);
Samuel Ortizd6469602011-12-14 16:43:12 +0100509
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200510 nfc_llcp_sock_link(&local->connecting_sockets, sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100511
512 ret = nfc_llcp_send_connect(llcp_sock);
513 if (ret)
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200514 goto sock_unlink;
Samuel Ortizd6469602011-12-14 16:43:12 +0100515
Samuel Ortizff353d82012-05-07 12:31:19 +0200516 ret = sock_wait_state(sk, LLCP_CONNECTED,
517 sock_sndtimeo(sk, flags & O_NONBLOCK));
518 if (ret)
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200519 goto sock_unlink;
Samuel Ortizd6469602011-12-14 16:43:12 +0100520
521 release_sock(sk);
Samuel Ortizff353d82012-05-07 12:31:19 +0200522
Samuel Ortizd6469602011-12-14 16:43:12 +0100523 return 0;
524
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200525sock_unlink:
526 nfc_llcp_put_ssap(local, llcp_sock->ssap);
527
528 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
529
Samuel Ortizd6469602011-12-14 16:43:12 +0100530put_dev:
531 nfc_put_device(dev);
532
533error:
534 release_sock(sk);
535 return ret;
536}
537
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100538static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100539 struct msghdr *msg, size_t len)
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100540{
541 struct sock *sk = sock->sk;
542 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
543 int ret;
544
545 pr_debug("sock %p sk %p", sock, sk);
546
547 ret = sock_error(sk);
548 if (ret)
549 return ret;
550
551 if (msg->msg_flags & MSG_OOB)
552 return -EOPNOTSUPP;
553
554 lock_sock(sk);
555
556 if (sk->sk_state != LLCP_CONNECTED) {
557 release_sock(sk);
558 return -ENOTCONN;
559 }
560
561 release_sock(sk);
562
563 return nfc_llcp_send_i_frame(llcp_sock, msg, len);
564}
565
Samuel Ortizd6469602011-12-14 16:43:12 +0100566static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
567 struct msghdr *msg, size_t len, int flags)
568{
569 int noblock = flags & MSG_DONTWAIT;
570 struct sock *sk = sock->sk;
571 unsigned int copied, rlen;
572 struct sk_buff *skb, *cskb;
573 int err = 0;
574
575 pr_debug("%p %zu\n", sk, len);
576
577 lock_sock(sk);
578
579 if (sk->sk_state == LLCP_CLOSED &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100580 skb_queue_empty(&sk->sk_receive_queue)) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100581 release_sock(sk);
582 return 0;
583 }
584
585 release_sock(sk);
586
587 if (flags & (MSG_OOB))
588 return -EOPNOTSUPP;
589
590 skb = skb_recv_datagram(sk, flags, noblock, &err);
591 if (!skb) {
592 pr_err("Recv datagram failed state %d %d %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100593 sk->sk_state, err, sock_error(sk));
Samuel Ortizd6469602011-12-14 16:43:12 +0100594
595 if (sk->sk_shutdown & RCV_SHUTDOWN)
596 return 0;
597
598 return err;
599 }
600
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100601 rlen = skb->len; /* real length of skb */
Samuel Ortizd6469602011-12-14 16:43:12 +0100602 copied = min_t(unsigned int, rlen, len);
603
604 cskb = skb;
605 if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
606 if (!(flags & MSG_PEEK))
607 skb_queue_head(&sk->sk_receive_queue, skb);
608 return -EFAULT;
609 }
610
611 /* Mark read part of skb as used */
612 if (!(flags & MSG_PEEK)) {
613
614 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
615 if (sk->sk_type == SOCK_STREAM) {
616 skb_pull(skb, copied);
617 if (skb->len) {
618 skb_queue_head(&sk->sk_receive_queue, skb);
619 goto done;
620 }
621 }
622
623 kfree_skb(skb);
624 }
625
626 /* XXX Queue backlogged skbs */
627
628done:
629 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
630 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
631 copied = rlen;
632
633 return copied;
634}
635
636static const struct proto_ops llcp_sock_ops = {
637 .family = PF_NFC,
638 .owner = THIS_MODULE,
639 .bind = llcp_sock_bind,
640 .connect = llcp_sock_connect,
641 .release = llcp_sock_release,
642 .socketpair = sock_no_socketpair,
643 .accept = llcp_sock_accept,
644 .getname = llcp_sock_getname,
645 .poll = llcp_sock_poll,
646 .ioctl = sock_no_ioctl,
647 .listen = llcp_sock_listen,
648 .shutdown = sock_no_shutdown,
649 .setsockopt = sock_no_setsockopt,
650 .getsockopt = sock_no_getsockopt,
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100651 .sendmsg = llcp_sock_sendmsg,
Samuel Ortizd6469602011-12-14 16:43:12 +0100652 .recvmsg = llcp_sock_recvmsg,
653 .mmap = sock_no_mmap,
654};
655
656static void llcp_sock_destruct(struct sock *sk)
657{
658 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
659
660 pr_debug("%p\n", sk);
661
662 if (sk->sk_state == LLCP_CONNECTED)
663 nfc_put_device(llcp_sock->dev);
664
665 skb_queue_purge(&sk->sk_receive_queue);
666
667 nfc_llcp_sock_free(llcp_sock);
668
669 if (!sock_flag(sk, SOCK_DEAD)) {
670 pr_err("Freeing alive NFC LLCP socket %p\n", sk);
671 return;
672 }
673}
674
675struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp)
676{
677 struct sock *sk;
678 struct nfc_llcp_sock *llcp_sock;
679
680 sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto);
681 if (!sk)
682 return NULL;
683
684 llcp_sock = nfc_llcp_sock(sk);
685
686 sock_init_data(sock, sk);
687 sk->sk_state = LLCP_CLOSED;
688 sk->sk_protocol = NFC_SOCKPROTO_LLCP;
689 sk->sk_type = type;
690 sk->sk_destruct = llcp_sock_destruct;
691
692 llcp_sock->ssap = 0;
693 llcp_sock->dsap = LLCP_SAP_SDP;
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200694 llcp_sock->rw = LLCP_DEFAULT_RW;
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200695 llcp_sock->miu = LLCP_DEFAULT_MIU;
Samuel Ortizd6469602011-12-14 16:43:12 +0100696 llcp_sock->send_n = llcp_sock->send_ack_n = 0;
697 llcp_sock->recv_n = llcp_sock->recv_ack_n = 0;
698 llcp_sock->remote_ready = 1;
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200699 llcp_sock->reserved_ssap = LLCP_SAP_MAX;
Samuel Ortizd6469602011-12-14 16:43:12 +0100700 skb_queue_head_init(&llcp_sock->tx_queue);
701 skb_queue_head_init(&llcp_sock->tx_pending_queue);
702 skb_queue_head_init(&llcp_sock->tx_backlog_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100703 INIT_LIST_HEAD(&llcp_sock->accept_queue);
704
705 if (sock != NULL)
706 sock->state = SS_UNCONNECTED;
707
708 return sk;
709}
710
711void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
712{
713 kfree(sock->service_name);
714
715 skb_queue_purge(&sock->tx_queue);
716 skb_queue_purge(&sock->tx_pending_queue);
717 skb_queue_purge(&sock->tx_backlog_queue);
718
719 list_del_init(&sock->accept_queue);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100720
Samuel Ortizd6469602011-12-14 16:43:12 +0100721 sock->parent = NULL;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200722
723 nfc_llcp_local_put(sock->local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100724}
725
726static int llcp_sock_create(struct net *net, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100727 const struct nfc_protocol *nfc_proto)
Samuel Ortizd6469602011-12-14 16:43:12 +0100728{
729 struct sock *sk;
730
731 pr_debug("%p\n", sock);
732
733 if (sock->type != SOCK_STREAM && sock->type != SOCK_DGRAM)
734 return -ESOCKTNOSUPPORT;
735
736 sock->ops = &llcp_sock_ops;
737
738 sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC);
739 if (sk == NULL)
740 return -ENOMEM;
741
742 return 0;
743}
744
745static const struct nfc_protocol llcp_nfc_proto = {
746 .id = NFC_SOCKPROTO_LLCP,
747 .proto = &llcp_sock_proto,
748 .owner = THIS_MODULE,
749 .create = llcp_sock_create
750};
751
752int __init nfc_llcp_sock_init(void)
753{
754 return nfc_proto_register(&llcp_nfc_proto);
755}
756
757void nfc_llcp_sock_exit(void)
758{
759 nfc_proto_unregister(&llcp_nfc_proto);
760}