blob: d308402b67d80c192c948aa6db05c15089cfdbe8 [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
Samuel Ortiz30cc4582013-04-26 11:49:40 +020027#include "nfc.h"
Samuel Ortizd6469602011-12-14 16:43:12 +010028#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
Samuel Ortizd6469602011-12-14 16:43:12 +010081 if (!addr || addr->sa_family != AF_NFC)
82 return -EINVAL;
83
Samuel Ortizc66433d2012-06-29 12:03:55 +020084 pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
85
Samuel Ortizd6469602011-12-14 16:43:12 +010086 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);
Samuel Ortiz8b7e8ed2012-06-22 15:32:20 +0200124 if (llcp_sock->ssap == LLCP_SAP_MAX) {
125 ret = -EADDRINUSE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100126 goto put_dev;
Samuel Ortiz8b7e8ed2012-06-22 15:32:20 +0200127 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100128
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200129 llcp_sock->reserved_ssap = llcp_sock->ssap;
130
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200131 nfc_llcp_sock_link(&local->sockets, sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100132
133 pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap);
134
135 sk->sk_state = LLCP_BOUND;
136
137put_dev:
138 nfc_put_device(dev);
139
140error:
141 release_sock(sk);
142 return ret;
143}
144
Thierry Escande4463523b2012-09-26 18:16:44 +0200145static 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
191put_dev:
192 nfc_put_device(dev);
193
194error:
195 release_sock(sk);
196 return ret;
197}
198
Samuel Ortizd6469602011-12-14 16:43:12 +0100199static 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 Janc874934f2012-10-04 15:15:51 +0200208 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) ||
209 sk->sk_state != LLCP_BOUND) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100210 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
220error:
221 release_sock(sk);
222
223 return ret;
224}
225
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100226static 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
Samuel Ortiz5eef6662013-03-20 16:06:12 +0100282 llcp_sock->miux = cpu_to_be16((u16) opt);
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100283
284 break;
285
286 default:
287 err = -ENOPROTOOPT;
288 break;
289 }
290
291 release_sock(sk);
292
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100293 pr_debug("%p rw %d miux %d\n", llcp_sock,
294 llcp_sock->rw, llcp_sock->miux);
295
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100296 return err;
297}
298
299static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
300 char __user *optval, int __user *optlen)
301{
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100302 struct nfc_llcp_local *local;
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100303 struct sock *sk = sock->sk;
304 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
305 int len, err = 0;
Thierry Escande064f3702013-04-02 10:25:16 +0200306 u16 miux, remote_miu;
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100307 u8 rw;
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100308
309 pr_debug("%p optname %d\n", sk, optname);
310
311 if (level != SOL_NFC)
312 return -ENOPROTOOPT;
313
314 if (get_user(len, optlen))
315 return -EFAULT;
316
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100317 local = llcp_sock->local;
318 if (!local)
319 return -ENODEV;
320
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100321 len = min_t(u32, len, sizeof(u32));
322
323 lock_sock(sk);
324
325 switch (optname) {
326 case NFC_LLCP_RW:
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100327 rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw;
328 if (put_user(rw, (u32 __user *) optval))
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100329 err = -EFAULT;
330
331 break;
332
333 case NFC_LLCP_MIUX:
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100334 miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ?
335 be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux);
336
337 if (put_user(miux, (u32 __user *) optval))
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100338 err = -EFAULT;
339
340 break;
341
Thierry Escande064f3702013-04-02 10:25:16 +0200342 case NFC_LLCP_REMOTE_MIU:
343 remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ?
344 local->remote_miu : llcp_sock->remote_miu;
345
346 if (put_user(remote_miu, (u32 __user *) optval))
347 err = -EFAULT;
348
349 break;
350
351 case NFC_LLCP_REMOTE_LTO:
352 if (put_user(local->remote_lto / 10, (u32 __user *) optval))
353 err = -EFAULT;
354
355 break;
356
357 case NFC_LLCP_REMOTE_RW:
358 if (put_user(llcp_sock->remote_rw, (u32 __user *) optval))
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100359 err = -EFAULT;
360
361 break;
362
363 default:
364 err = -ENOPROTOOPT;
365 break;
366 }
367
368 release_sock(sk);
369
370 if (put_user(len, optlen))
371 return -EFAULT;
372
373 return err;
374}
375
Samuel Ortizd6469602011-12-14 16:43:12 +0100376void nfc_llcp_accept_unlink(struct sock *sk)
377{
378 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
379
380 pr_debug("state %d\n", sk->sk_state);
381
382 list_del_init(&llcp_sock->accept_queue);
383 sk_acceptq_removed(llcp_sock->parent);
384 llcp_sock->parent = NULL;
385
386 sock_put(sk);
387}
388
389void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk)
390{
391 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
392 struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent);
393
394 /* Lock will be free from unlink */
395 sock_hold(sk);
396
397 list_add_tail(&llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100398 &llcp_sock_parent->accept_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100399 llcp_sock->parent = parent;
400 sk_acceptq_added(parent);
401}
402
403struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100404 struct socket *newsock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100405{
406 struct nfc_llcp_sock *lsk, *n, *llcp_parent;
407 struct sock *sk;
408
409 llcp_parent = nfc_llcp_sock(parent);
410
411 list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100412 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100413 sk = &lsk->sk;
414 lock_sock(sk);
415
416 if (sk->sk_state == LLCP_CLOSED) {
417 release_sock(sk);
418 nfc_llcp_accept_unlink(sk);
419 continue;
420 }
421
422 if (sk->sk_state == LLCP_CONNECTED || !newsock) {
Samuel Ortiz39a352a2013-03-26 14:35:57 +0100423 list_del_init(&lsk->accept_queue);
424 sock_put(sk);
425
Samuel Ortizd6469602011-12-14 16:43:12 +0100426 if (newsock)
427 sock_graft(sk, newsock);
428
429 release_sock(sk);
430
431 pr_debug("Returning sk state %d\n", sk->sk_state);
432
Samuel Ortizb141e812013-02-21 11:04:45 +0100433 sk_acceptq_removed(parent);
434
Samuel Ortizd6469602011-12-14 16:43:12 +0100435 return sk;
436 }
437
438 release_sock(sk);
439 }
440
441 return NULL;
442}
443
444static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100445 int flags)
Samuel Ortizd6469602011-12-14 16:43:12 +0100446{
447 DECLARE_WAITQUEUE(wait, current);
448 struct sock *sk = sock->sk, *new_sk;
449 long timeo;
450 int ret = 0;
451
452 pr_debug("parent %p\n", sk);
453
454 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
455
456 if (sk->sk_state != LLCP_LISTEN) {
457 ret = -EBADFD;
458 goto error;
459 }
460
461 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
462
463 /* Wait for an incoming connection. */
464 add_wait_queue_exclusive(sk_sleep(sk), &wait);
465 while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
466 set_current_state(TASK_INTERRUPTIBLE);
467
468 if (!timeo) {
469 ret = -EAGAIN;
470 break;
471 }
472
473 if (signal_pending(current)) {
474 ret = sock_intr_errno(timeo);
475 break;
476 }
477
478 release_sock(sk);
479 timeo = schedule_timeout(timeo);
480 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
481 }
482 __set_current_state(TASK_RUNNING);
483 remove_wait_queue(sk_sleep(sk), &wait);
484
485 if (ret)
486 goto error;
487
488 newsock->state = SS_CONNECTED;
489
490 pr_debug("new socket %p\n", new_sk);
491
492error:
493 release_sock(sk);
494
495 return ret;
496}
497
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200498static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
Samuel Ortizd6469602011-12-14 16:43:12 +0100499 int *len, int peer)
500{
Samuel Ortizd6469602011-12-14 16:43:12 +0100501 struct sock *sk = sock->sk;
502 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200503 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr);
Samuel Ortizd6469602011-12-14 16:43:12 +0100504
Samuel Ortizfe3c0942012-07-05 17:43:08 +0200505 if (llcp_sock == NULL || llcp_sock->dev == NULL)
506 return -EBADFD;
507
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200508 pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx,
509 llcp_sock->dsap, llcp_sock->ssap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100510
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000511 memset(llcp_addr, 0, sizeof(*llcp_addr));
Samuel Ortizd6469602011-12-14 16:43:12 +0100512 *len = sizeof(struct sockaddr_nfc_llcp);
513
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000514 llcp_addr->sa_family = AF_NFC;
Samuel Ortizd6469602011-12-14 16:43:12 +0100515 llcp_addr->dev_idx = llcp_sock->dev->idx;
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200516 llcp_addr->target_idx = llcp_sock->target_idx;
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000517 llcp_addr->nfc_protocol = llcp_sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100518 llcp_addr->dsap = llcp_sock->dsap;
519 llcp_addr->ssap = llcp_sock->ssap;
520 llcp_addr->service_name_len = llcp_sock->service_name_len;
521 memcpy(llcp_addr->service_name, llcp_sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100522 llcp_addr->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100523
524 return 0;
525}
526
527static inline unsigned int llcp_accept_poll(struct sock *parent)
528{
529 struct nfc_llcp_sock *llcp_sock, *n, *parent_sock;
530 struct sock *sk;
531
532 parent_sock = nfc_llcp_sock(parent);
533
534 list_for_each_entry_safe(llcp_sock, n, &parent_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100535 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100536 sk = &llcp_sock->sk;
537
538 if (sk->sk_state == LLCP_CONNECTED)
539 return POLLIN | POLLRDNORM;
540 }
541
542 return 0;
543}
544
545static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100546 poll_table *wait)
Samuel Ortizd6469602011-12-14 16:43:12 +0100547{
548 struct sock *sk = sock->sk;
549 unsigned int mask = 0;
550
551 pr_debug("%p\n", sk);
552
553 sock_poll_wait(file, sk_sleep(sk), wait);
554
555 if (sk->sk_state == LLCP_LISTEN)
556 return llcp_accept_poll(sk);
557
558 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +0000559 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -0700560 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Samuel Ortizd6469602011-12-14 16:43:12 +0100561
562 if (!skb_queue_empty(&sk->sk_receive_queue))
Samuel Ortiz4260c132012-05-07 12:31:20 +0200563 mask |= POLLIN | POLLRDNORM;
Samuel Ortizd6469602011-12-14 16:43:12 +0100564
565 if (sk->sk_state == LLCP_CLOSED)
566 mask |= POLLHUP;
567
Samuel Ortiz4260c132012-05-07 12:31:20 +0200568 if (sk->sk_shutdown & RCV_SHUTDOWN)
569 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
570
571 if (sk->sk_shutdown == SHUTDOWN_MASK)
572 mask |= POLLHUP;
573
Samuel Ortizb4011232013-05-03 18:29:30 +0200574 if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED)
Samuel Ortiz4260c132012-05-07 12:31:20 +0200575 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
576 else
577 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
578
579 pr_debug("mask 0x%x\n", mask);
580
Samuel Ortizd6469602011-12-14 16:43:12 +0100581 return mask;
582}
583
584static int llcp_sock_release(struct socket *sock)
585{
586 struct sock *sk = sock->sk;
587 struct nfc_llcp_local *local;
588 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100589 int err = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100590
591 if (!sk)
592 return 0;
593
594 pr_debug("%p\n", sk);
595
596 local = llcp_sock->local;
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100597 if (local == NULL) {
598 err = -ENODEV;
599 goto out;
600 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100601
Samuel Ortizd6469602011-12-14 16:43:12 +0100602 lock_sock(sk);
603
604 /* Send a DISC */
605 if (sk->sk_state == LLCP_CONNECTED)
Thierry Escande58e3dd12013-06-04 11:34:50 +0200606 nfc_llcp_send_disconnect(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100607
608 if (sk->sk_state == LLCP_LISTEN) {
609 struct nfc_llcp_sock *lsk, *n;
610 struct sock *accept_sk;
611
612 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100613 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100614 accept_sk = &lsk->sk;
615 lock_sock(accept_sk);
616
Thierry Escande58e3dd12013-06-04 11:34:50 +0200617 nfc_llcp_send_disconnect(lsk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100618 nfc_llcp_accept_unlink(accept_sk);
619
620 release_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100621 }
622 }
623
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200624 if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
625 nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100626
Samuel Ortizd6469602011-12-14 16:43:12 +0100627 release_sock(sk);
628
Thierry Escande17f7ae162013-06-04 11:34:51 +0200629 /* Keep this sock alive and therefore do not remove it from the sockets
630 * list until the DISC PDU has been actually sent. Otherwise we would
631 * reply with DM PDUs before sending the DISC one.
632 */
633 if (sk->sk_state == LLCP_DISCONNECTING)
634 return err;
635
Thierry Escande4463523b2012-09-26 18:16:44 +0200636 if (sock->type == SOCK_RAW)
637 nfc_llcp_sock_unlink(&local->raw_sockets, sk);
638 else
639 nfc_llcp_sock_unlink(&local->sockets, sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200640
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100641out:
Samuel Ortizd6469602011-12-14 16:43:12 +0100642 sock_orphan(sk);
643 sock_put(sk);
644
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100645 return err;
Samuel Ortizd6469602011-12-14 16:43:12 +0100646}
647
648static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100649 int len, int flags)
Samuel Ortizd6469602011-12-14 16:43:12 +0100650{
651 struct sock *sk = sock->sk;
652 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
653 struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
654 struct nfc_dev *dev;
655 struct nfc_llcp_local *local;
656 int ret = 0;
657
658 pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
659
660 if (!addr || len < sizeof(struct sockaddr_nfc) ||
Dave Jones32418cf2012-10-04 09:51:11 +0000661 addr->sa_family != AF_NFC)
Samuel Ortizd6469602011-12-14 16:43:12 +0100662 return -EINVAL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100663
Dave Jones32418cf2012-10-04 09:51:11 +0000664 if (addr->service_name_len == 0 && addr->dsap == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100665 return -EINVAL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100666
667 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100668 addr->target_idx, addr->nfc_protocol);
Samuel Ortizd6469602011-12-14 16:43:12 +0100669
670 lock_sock(sk);
671
672 if (sk->sk_state == LLCP_CONNECTED) {
673 ret = -EISCONN;
674 goto error;
675 }
676
677 dev = nfc_get_device(addr->dev_idx);
678 if (dev == NULL) {
679 ret = -ENODEV;
680 goto error;
681 }
682
683 local = nfc_llcp_find_local(dev);
684 if (local == NULL) {
685 ret = -ENODEV;
686 goto put_dev;
687 }
688
689 device_lock(&dev->dev);
690 if (dev->dep_link_up == false) {
691 ret = -ENOLINK;
692 device_unlock(&dev->dev);
693 goto put_dev;
694 }
695 device_unlock(&dev->dev);
696
697 if (local->rf_mode == NFC_RF_INITIATOR &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100698 addr->target_idx != local->target_idx) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100699 ret = -ENOLINK;
700 goto put_dev;
701 }
702
703 llcp_sock->dev = dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200704 llcp_sock->local = nfc_llcp_local_get(local);
Samuel Ortize4306be2013-02-22 01:12:28 +0100705 llcp_sock->remote_miu = llcp_sock->local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100706 llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
707 if (llcp_sock->ssap == LLCP_SAP_MAX) {
708 ret = -ENOMEM;
709 goto put_dev;
710 }
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200711
712 llcp_sock->reserved_ssap = llcp_sock->ssap;
713
Samuel Ortizd6469602011-12-14 16:43:12 +0100714 if (addr->service_name_len == 0)
715 llcp_sock->dsap = addr->dsap;
716 else
717 llcp_sock->dsap = LLCP_SAP_SDP;
718 llcp_sock->nfc_protocol = addr->nfc_protocol;
719 llcp_sock->service_name_len = min_t(unsigned int,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100720 addr->service_name_len,
721 NFC_LLCP_MAX_SERVICE_NAME);
Samuel Ortizd6469602011-12-14 16:43:12 +0100722 llcp_sock->service_name = kmemdup(addr->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100723 llcp_sock->service_name_len,
724 GFP_KERNEL);
Samuel Ortizd6469602011-12-14 16:43:12 +0100725
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200726 nfc_llcp_sock_link(&local->connecting_sockets, sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100727
728 ret = nfc_llcp_send_connect(llcp_sock);
729 if (ret)
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200730 goto sock_unlink;
Samuel Ortizd6469602011-12-14 16:43:12 +0100731
Samuel Ortizb4011232013-05-03 18:29:30 +0200732 sk->sk_state = LLCP_CONNECTING;
733
Samuel Ortizff353d82012-05-07 12:31:19 +0200734 ret = sock_wait_state(sk, LLCP_CONNECTED,
735 sock_sndtimeo(sk, flags & O_NONBLOCK));
Samuel Ortizb4011232013-05-03 18:29:30 +0200736 if (ret && ret != -EINPROGRESS)
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200737 goto sock_unlink;
Samuel Ortizd6469602011-12-14 16:43:12 +0100738
739 release_sock(sk);
Samuel Ortizff353d82012-05-07 12:31:19 +0200740
Samuel Ortizb4011232013-05-03 18:29:30 +0200741 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100742
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200743sock_unlink:
744 nfc_llcp_put_ssap(local, llcp_sock->ssap);
745
746 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
747
Samuel Ortizd6469602011-12-14 16:43:12 +0100748put_dev:
749 nfc_put_device(dev);
750
751error:
752 release_sock(sk);
753 return ret;
754}
755
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100756static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100757 struct msghdr *msg, size_t len)
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100758{
759 struct sock *sk = sock->sk;
760 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
761 int ret;
762
763 pr_debug("sock %p sk %p", sock, sk);
764
765 ret = sock_error(sk);
766 if (ret)
767 return ret;
768
769 if (msg->msg_flags & MSG_OOB)
770 return -EOPNOTSUPP;
771
772 lock_sock(sk);
773
Samuel Ortizb874dec2012-10-16 15:04:10 +0200774 if (sk->sk_type == SOCK_DGRAM) {
775 struct sockaddr_nfc_llcp *addr =
776 (struct sockaddr_nfc_llcp *)msg->msg_name;
777
778 if (msg->msg_namelen < sizeof(*addr)) {
779 release_sock(sk);
Samuel Ortizb874dec2012-10-16 15:04:10 +0200780 return -EINVAL;
781 }
782
783 release_sock(sk);
784
785 return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
786 msg, len);
787 }
788
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100789 if (sk->sk_state != LLCP_CONNECTED) {
790 release_sock(sk);
791 return -ENOTCONN;
792 }
793
794 release_sock(sk);
795
796 return nfc_llcp_send_i_frame(llcp_sock, msg, len);
797}
798
Samuel Ortizd6469602011-12-14 16:43:12 +0100799static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
800 struct msghdr *msg, size_t len, int flags)
801{
802 int noblock = flags & MSG_DONTWAIT;
803 struct sock *sk = sock->sk;
804 unsigned int copied, rlen;
805 struct sk_buff *skb, *cskb;
806 int err = 0;
807
808 pr_debug("%p %zu\n", sk, len);
809
Mathias Kraused26d6502013-04-07 01:51:58 +0000810 msg->msg_namelen = 0;
811
Samuel Ortizd6469602011-12-14 16:43:12 +0100812 lock_sock(sk);
813
814 if (sk->sk_state == LLCP_CLOSED &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100815 skb_queue_empty(&sk->sk_receive_queue)) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100816 release_sock(sk);
817 return 0;
818 }
819
820 release_sock(sk);
821
822 if (flags & (MSG_OOB))
823 return -EOPNOTSUPP;
824
825 skb = skb_recv_datagram(sk, flags, noblock, &err);
826 if (!skb) {
827 pr_err("Recv datagram failed state %d %d %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100828 sk->sk_state, err, sock_error(sk));
Samuel Ortizd6469602011-12-14 16:43:12 +0100829
830 if (sk->sk_shutdown & RCV_SHUTDOWN)
831 return 0;
832
833 return err;
834 }
835
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100836 rlen = skb->len; /* real length of skb */
Samuel Ortizd6469602011-12-14 16:43:12 +0100837 copied = min_t(unsigned int, rlen, len);
838
839 cskb = skb;
Samuel Ortizfb97c3e2013-01-11 14:54:22 +0100840 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100841 if (!(flags & MSG_PEEK))
842 skb_queue_head(&sk->sk_receive_queue, skb);
843 return -EFAULT;
844 }
845
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100846 sock_recv_timestamp(msg, sk, skb);
847
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200848 if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
849 struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
Samuel Ortizfad2e372013-01-11 14:48:48 +0100850 struct sockaddr_nfc_llcp *sockaddr =
851 (struct sockaddr_nfc_llcp *) msg->msg_name;
852
853 msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200854
855 pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
856
Mathias Kraused26d6502013-04-07 01:51:58 +0000857 memset(sockaddr, 0, sizeof(*sockaddr));
Samuel Ortizfad2e372013-01-11 14:48:48 +0100858 sockaddr->sa_family = AF_NFC;
859 sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP;
860 sockaddr->dsap = ui_cb->dsap;
861 sockaddr->ssap = ui_cb->ssap;
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200862 }
863
Samuel Ortizd6469602011-12-14 16:43:12 +0100864 /* Mark read part of skb as used */
865 if (!(flags & MSG_PEEK)) {
866
867 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200868 if (sk->sk_type == SOCK_STREAM ||
869 sk->sk_type == SOCK_DGRAM ||
870 sk->sk_type == SOCK_RAW) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100871 skb_pull(skb, copied);
872 if (skb->len) {
873 skb_queue_head(&sk->sk_receive_queue, skb);
874 goto done;
875 }
876 }
877
878 kfree_skb(skb);
879 }
880
881 /* XXX Queue backlogged skbs */
882
883done:
884 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
885 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
886 copied = rlen;
887
888 return copied;
889}
890
891static const struct proto_ops llcp_sock_ops = {
892 .family = PF_NFC,
893 .owner = THIS_MODULE,
894 .bind = llcp_sock_bind,
895 .connect = llcp_sock_connect,
896 .release = llcp_sock_release,
897 .socketpair = sock_no_socketpair,
898 .accept = llcp_sock_accept,
899 .getname = llcp_sock_getname,
900 .poll = llcp_sock_poll,
901 .ioctl = sock_no_ioctl,
902 .listen = llcp_sock_listen,
903 .shutdown = sock_no_shutdown,
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100904 .setsockopt = nfc_llcp_setsockopt,
905 .getsockopt = nfc_llcp_getsockopt,
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100906 .sendmsg = llcp_sock_sendmsg,
Samuel Ortizd6469602011-12-14 16:43:12 +0100907 .recvmsg = llcp_sock_recvmsg,
908 .mmap = sock_no_mmap,
909};
910
Thierry Escande4463523b2012-09-26 18:16:44 +0200911static const struct proto_ops llcp_rawsock_ops = {
912 .family = PF_NFC,
913 .owner = THIS_MODULE,
914 .bind = llcp_raw_sock_bind,
915 .connect = sock_no_connect,
916 .release = llcp_sock_release,
917 .socketpair = sock_no_socketpair,
918 .accept = sock_no_accept,
919 .getname = llcp_sock_getname,
920 .poll = llcp_sock_poll,
921 .ioctl = sock_no_ioctl,
922 .listen = sock_no_listen,
923 .shutdown = sock_no_shutdown,
924 .setsockopt = sock_no_setsockopt,
925 .getsockopt = sock_no_getsockopt,
926 .sendmsg = sock_no_sendmsg,
927 .recvmsg = llcp_sock_recvmsg,
928 .mmap = sock_no_mmap,
929};
930
Samuel Ortizd6469602011-12-14 16:43:12 +0100931static void llcp_sock_destruct(struct sock *sk)
932{
933 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
934
935 pr_debug("%p\n", sk);
936
937 if (sk->sk_state == LLCP_CONNECTED)
938 nfc_put_device(llcp_sock->dev);
939
940 skb_queue_purge(&sk->sk_receive_queue);
941
942 nfc_llcp_sock_free(llcp_sock);
943
944 if (!sock_flag(sk, SOCK_DEAD)) {
945 pr_err("Freeing alive NFC LLCP socket %p\n", sk);
946 return;
947 }
948}
949
950struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp)
951{
952 struct sock *sk;
953 struct nfc_llcp_sock *llcp_sock;
954
955 sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto);
956 if (!sk)
957 return NULL;
958
959 llcp_sock = nfc_llcp_sock(sk);
960
961 sock_init_data(sock, sk);
962 sk->sk_state = LLCP_CLOSED;
963 sk->sk_protocol = NFC_SOCKPROTO_LLCP;
964 sk->sk_type = type;
965 sk->sk_destruct = llcp_sock_destruct;
966
967 llcp_sock->ssap = 0;
968 llcp_sock->dsap = LLCP_SAP_SDP;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100969 llcp_sock->rw = LLCP_MAX_RW + 1;
Samuel Ortiz5eef6662013-03-20 16:06:12 +0100970 llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1);
Samuel Ortizd6469602011-12-14 16:43:12 +0100971 llcp_sock->send_n = llcp_sock->send_ack_n = 0;
972 llcp_sock->recv_n = llcp_sock->recv_ack_n = 0;
973 llcp_sock->remote_ready = 1;
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200974 llcp_sock->reserved_ssap = LLCP_SAP_MAX;
Thierry Escandeabd18d42013-04-02 10:25:15 +0200975 nfc_llcp_socket_remote_param_init(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100976 skb_queue_head_init(&llcp_sock->tx_queue);
977 skb_queue_head_init(&llcp_sock->tx_pending_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100978 INIT_LIST_HEAD(&llcp_sock->accept_queue);
979
980 if (sock != NULL)
981 sock->state = SS_UNCONNECTED;
982
983 return sk;
984}
985
986void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
987{
988 kfree(sock->service_name);
989
990 skb_queue_purge(&sock->tx_queue);
991 skb_queue_purge(&sock->tx_pending_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100992
993 list_del_init(&sock->accept_queue);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100994
Samuel Ortizd6469602011-12-14 16:43:12 +0100995 sock->parent = NULL;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200996
997 nfc_llcp_local_put(sock->local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100998}
999
1000static int llcp_sock_create(struct net *net, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +01001001 const struct nfc_protocol *nfc_proto)
Samuel Ortizd6469602011-12-14 16:43:12 +01001002{
1003 struct sock *sk;
1004
1005 pr_debug("%p\n", sock);
1006
Thierry Escande4463523b2012-09-26 18:16:44 +02001007 if (sock->type != SOCK_STREAM &&
1008 sock->type != SOCK_DGRAM &&
1009 sock->type != SOCK_RAW)
Samuel Ortizd6469602011-12-14 16:43:12 +01001010 return -ESOCKTNOSUPPORT;
1011
Thierry Escande4463523b2012-09-26 18:16:44 +02001012 if (sock->type == SOCK_RAW)
1013 sock->ops = &llcp_rawsock_ops;
1014 else
1015 sock->ops = &llcp_sock_ops;
Samuel Ortizd6469602011-12-14 16:43:12 +01001016
1017 sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC);
1018 if (sk == NULL)
1019 return -ENOMEM;
1020
1021 return 0;
1022}
1023
1024static const struct nfc_protocol llcp_nfc_proto = {
1025 .id = NFC_SOCKPROTO_LLCP,
1026 .proto = &llcp_sock_proto,
1027 .owner = THIS_MODULE,
1028 .create = llcp_sock_create
1029};
1030
1031int __init nfc_llcp_sock_init(void)
1032{
1033 return nfc_proto_register(&llcp_nfc_proto);
1034}
1035
1036void nfc_llcp_sock_exit(void)
1037{
1038 nfc_proto_unregister(&llcp_nfc_proto);
1039}