blob: 51f077a92fa9220eb987b7b9185efcc75312328a [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
Jeff Kirsher98b32de2013-12-06 08:56:16 -080015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Samuel Ortizd6469602011-12-14 16:43:12 +010016 */
17
18#define pr_fmt(fmt) "llcp: %s: " fmt, __func__
19
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/nfc.h>
24
Samuel Ortiz30cc4582013-04-26 11:49:40 +020025#include "nfc.h"
Samuel Ortizd6469602011-12-14 16:43:12 +010026#include "llcp.h"
27
Samuel Ortizff353d82012-05-07 12:31:19 +020028static int sock_wait_state(struct sock *sk, int state, unsigned long timeo)
29{
30 DECLARE_WAITQUEUE(wait, current);
31 int err = 0;
32
33 pr_debug("sk %p", sk);
34
35 add_wait_queue(sk_sleep(sk), &wait);
36 set_current_state(TASK_INTERRUPTIBLE);
37
38 while (sk->sk_state != state) {
39 if (!timeo) {
40 err = -EINPROGRESS;
41 break;
42 }
43
44 if (signal_pending(current)) {
45 err = sock_intr_errno(timeo);
46 break;
47 }
48
49 release_sock(sk);
50 timeo = schedule_timeout(timeo);
51 lock_sock(sk);
52 set_current_state(TASK_INTERRUPTIBLE);
53
54 err = sock_error(sk);
55 if (err)
56 break;
57 }
58
59 __set_current_state(TASK_RUNNING);
60 remove_wait_queue(sk_sleep(sk), &wait);
61 return err;
62}
63
Samuel Ortizd6469602011-12-14 16:43:12 +010064static struct proto llcp_sock_proto = {
65 .name = "NFC_LLCP",
66 .owner = THIS_MODULE,
67 .obj_size = sizeof(struct nfc_llcp_sock),
68};
69
70static int llcp_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
71{
72 struct sock *sk = sock->sk;
73 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
74 struct nfc_llcp_local *local;
75 struct nfc_dev *dev;
76 struct sockaddr_nfc_llcp llcp_addr;
77 int len, ret = 0;
78
Samuel Ortizd6469602011-12-14 16:43:12 +010079 if (!addr || addr->sa_family != AF_NFC)
80 return -EINVAL;
81
Samuel Ortizc66433d2012-06-29 12:03:55 +020082 pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
83
Samuel Ortizd6469602011-12-14 16:43:12 +010084 memset(&llcp_addr, 0, sizeof(llcp_addr));
85 len = min_t(unsigned int, sizeof(llcp_addr), alen);
86 memcpy(&llcp_addr, addr, len);
87
88 /* This is going to be a listening socket, dsap must be 0 */
89 if (llcp_addr.dsap != 0)
90 return -EINVAL;
91
92 lock_sock(sk);
93
94 if (sk->sk_state != LLCP_CLOSED) {
95 ret = -EBADFD;
96 goto error;
97 }
98
99 dev = nfc_get_device(llcp_addr.dev_idx);
100 if (dev == NULL) {
101 ret = -ENODEV;
102 goto error;
103 }
104
105 local = nfc_llcp_find_local(dev);
106 if (local == NULL) {
107 ret = -ENODEV;
108 goto put_dev;
109 }
110
111 llcp_sock->dev = dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200112 llcp_sock->local = nfc_llcp_local_get(local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100113 llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
114 llcp_sock->service_name_len = min_t(unsigned int,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100115 llcp_addr.service_name_len,
116 NFC_LLCP_MAX_SERVICE_NAME);
Samuel Ortizd6469602011-12-14 16:43:12 +0100117 llcp_sock->service_name = kmemdup(llcp_addr.service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100118 llcp_sock->service_name_len,
119 GFP_KERNEL);
Samuel Ortizd6469602011-12-14 16:43:12 +0100120
121 llcp_sock->ssap = nfc_llcp_get_sdp_ssap(local, llcp_sock);
Samuel Ortiz8b7e8ed2012-06-22 15:32:20 +0200122 if (llcp_sock->ssap == LLCP_SAP_MAX) {
123 ret = -EADDRINUSE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100124 goto put_dev;
Samuel Ortiz8b7e8ed2012-06-22 15:32:20 +0200125 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100126
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
Thierry Escande44635232012-09-26 18:16:44 +0200143static int llcp_raw_sock_bind(struct socket *sock, struct sockaddr *addr,
144 int alen)
145{
146 struct sock *sk = sock->sk;
147 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
148 struct nfc_llcp_local *local;
149 struct nfc_dev *dev;
150 struct sockaddr_nfc_llcp llcp_addr;
151 int len, ret = 0;
152
153 if (!addr || addr->sa_family != AF_NFC)
154 return -EINVAL;
155
156 pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
157
158 memset(&llcp_addr, 0, sizeof(llcp_addr));
159 len = min_t(unsigned int, sizeof(llcp_addr), alen);
160 memcpy(&llcp_addr, addr, len);
161
162 lock_sock(sk);
163
164 if (sk->sk_state != LLCP_CLOSED) {
165 ret = -EBADFD;
166 goto error;
167 }
168
169 dev = nfc_get_device(llcp_addr.dev_idx);
170 if (dev == NULL) {
171 ret = -ENODEV;
172 goto error;
173 }
174
175 local = nfc_llcp_find_local(dev);
176 if (local == NULL) {
177 ret = -ENODEV;
178 goto put_dev;
179 }
180
181 llcp_sock->dev = dev;
182 llcp_sock->local = nfc_llcp_local_get(local);
183 llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
184
185 nfc_llcp_sock_link(&local->raw_sockets, sk);
186
187 sk->sk_state = LLCP_BOUND;
188
189put_dev:
190 nfc_put_device(dev);
191
192error:
193 release_sock(sk);
194 return ret;
195}
196
Samuel Ortizd6469602011-12-14 16:43:12 +0100197static int llcp_sock_listen(struct socket *sock, int backlog)
198{
199 struct sock *sk = sock->sk;
200 int ret = 0;
201
202 pr_debug("sk %p backlog %d\n", sk, backlog);
203
204 lock_sock(sk);
205
Szymon Janc874934f2012-10-04 15:15:51 +0200206 if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) ||
207 sk->sk_state != LLCP_BOUND) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100208 ret = -EBADFD;
209 goto error;
210 }
211
212 sk->sk_max_ack_backlog = backlog;
213 sk->sk_ack_backlog = 0;
214
215 pr_debug("Socket listening\n");
216 sk->sk_state = LLCP_LISTEN;
217
218error:
219 release_sock(sk);
220
221 return ret;
222}
223
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100224static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname,
225 char __user *optval, unsigned int optlen)
226{
227 struct sock *sk = sock->sk;
228 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
229 u32 opt;
230 int err = 0;
231
232 pr_debug("%p optname %d\n", sk, optname);
233
234 if (level != SOL_NFC)
235 return -ENOPROTOOPT;
236
237 lock_sock(sk);
238
239 switch (optname) {
240 case NFC_LLCP_RW:
241 if (sk->sk_state == LLCP_CONNECTED ||
242 sk->sk_state == LLCP_BOUND ||
243 sk->sk_state == LLCP_LISTEN) {
244 err = -EINVAL;
245 break;
246 }
247
248 if (get_user(opt, (u32 __user *) optval)) {
249 err = -EFAULT;
250 break;
251 }
252
253 if (opt > LLCP_MAX_RW) {
254 err = -EINVAL;
255 break;
256 }
257
258 llcp_sock->rw = (u8) opt;
259
260 break;
261
262 case NFC_LLCP_MIUX:
263 if (sk->sk_state == LLCP_CONNECTED ||
264 sk->sk_state == LLCP_BOUND ||
265 sk->sk_state == LLCP_LISTEN) {
266 err = -EINVAL;
267 break;
268 }
269
270 if (get_user(opt, (u32 __user *) optval)) {
271 err = -EFAULT;
272 break;
273 }
274
275 if (opt > LLCP_MAX_MIUX) {
276 err = -EINVAL;
277 break;
278 }
279
Samuel Ortiz5eef6662013-03-20 16:06:12 +0100280 llcp_sock->miux = cpu_to_be16((u16) opt);
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100281
282 break;
283
284 default:
285 err = -ENOPROTOOPT;
286 break;
287 }
288
289 release_sock(sk);
290
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100291 pr_debug("%p rw %d miux %d\n", llcp_sock,
292 llcp_sock->rw, llcp_sock->miux);
293
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100294 return err;
295}
296
297static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
298 char __user *optval, int __user *optlen)
299{
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100300 struct nfc_llcp_local *local;
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100301 struct sock *sk = sock->sk;
302 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
303 int len, err = 0;
Thierry Escande064f3702013-04-02 10:25:16 +0200304 u16 miux, remote_miu;
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100305 u8 rw;
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100306
307 pr_debug("%p optname %d\n", sk, optname);
308
309 if (level != SOL_NFC)
310 return -ENOPROTOOPT;
311
312 if (get_user(len, optlen))
313 return -EFAULT;
314
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100315 local = llcp_sock->local;
316 if (!local)
317 return -ENODEV;
318
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100319 len = min_t(u32, len, sizeof(u32));
320
321 lock_sock(sk);
322
323 switch (optname) {
324 case NFC_LLCP_RW:
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100325 rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw;
326 if (put_user(rw, (u32 __user *) optval))
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100327 err = -EFAULT;
328
329 break;
330
331 case NFC_LLCP_MIUX:
Samuel Ortiz00e856d2013-03-20 16:36:13 +0100332 miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ?
333 be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux);
334
335 if (put_user(miux, (u32 __user *) optval))
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100336 err = -EFAULT;
337
338 break;
339
Thierry Escande064f3702013-04-02 10:25:16 +0200340 case NFC_LLCP_REMOTE_MIU:
341 remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ?
342 local->remote_miu : llcp_sock->remote_miu;
343
344 if (put_user(remote_miu, (u32 __user *) optval))
345 err = -EFAULT;
346
347 break;
348
349 case NFC_LLCP_REMOTE_LTO:
350 if (put_user(local->remote_lto / 10, (u32 __user *) optval))
351 err = -EFAULT;
352
353 break;
354
355 case NFC_LLCP_REMOTE_RW:
356 if (put_user(llcp_sock->remote_rw, (u32 __user *) optval))
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100357 err = -EFAULT;
358
359 break;
360
361 default:
362 err = -ENOPROTOOPT;
363 break;
364 }
365
366 release_sock(sk);
367
368 if (put_user(len, optlen))
369 return -EFAULT;
370
371 return err;
372}
373
Samuel Ortizd6469602011-12-14 16:43:12 +0100374void nfc_llcp_accept_unlink(struct sock *sk)
375{
376 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
377
378 pr_debug("state %d\n", sk->sk_state);
379
380 list_del_init(&llcp_sock->accept_queue);
381 sk_acceptq_removed(llcp_sock->parent);
382 llcp_sock->parent = NULL;
383
384 sock_put(sk);
385}
386
387void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk)
388{
389 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
390 struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent);
391
392 /* Lock will be free from unlink */
393 sock_hold(sk);
394
395 list_add_tail(&llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100396 &llcp_sock_parent->accept_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100397 llcp_sock->parent = parent;
398 sk_acceptq_added(parent);
399}
400
401struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100402 struct socket *newsock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100403{
404 struct nfc_llcp_sock *lsk, *n, *llcp_parent;
405 struct sock *sk;
406
407 llcp_parent = nfc_llcp_sock(parent);
408
409 list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100410 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100411 sk = &lsk->sk;
412 lock_sock(sk);
413
414 if (sk->sk_state == LLCP_CLOSED) {
415 release_sock(sk);
416 nfc_llcp_accept_unlink(sk);
417 continue;
418 }
419
420 if (sk->sk_state == LLCP_CONNECTED || !newsock) {
Samuel Ortiz39a352a2013-03-26 14:35:57 +0100421 list_del_init(&lsk->accept_queue);
422 sock_put(sk);
423
Samuel Ortizd6469602011-12-14 16:43:12 +0100424 if (newsock)
425 sock_graft(sk, newsock);
426
427 release_sock(sk);
428
429 pr_debug("Returning sk state %d\n", sk->sk_state);
430
Samuel Ortizb141e812013-02-21 11:04:45 +0100431 sk_acceptq_removed(parent);
432
Samuel Ortizd6469602011-12-14 16:43:12 +0100433 return sk;
434 }
435
436 release_sock(sk);
437 }
438
439 return NULL;
440}
441
442static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100443 int flags)
Samuel Ortizd6469602011-12-14 16:43:12 +0100444{
445 DECLARE_WAITQUEUE(wait, current);
446 struct sock *sk = sock->sk, *new_sk;
447 long timeo;
448 int ret = 0;
449
450 pr_debug("parent %p\n", sk);
451
452 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
453
454 if (sk->sk_state != LLCP_LISTEN) {
455 ret = -EBADFD;
456 goto error;
457 }
458
459 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
460
461 /* Wait for an incoming connection. */
462 add_wait_queue_exclusive(sk_sleep(sk), &wait);
463 while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
464 set_current_state(TASK_INTERRUPTIBLE);
465
466 if (!timeo) {
467 ret = -EAGAIN;
468 break;
469 }
470
471 if (signal_pending(current)) {
472 ret = sock_intr_errno(timeo);
473 break;
474 }
475
476 release_sock(sk);
477 timeo = schedule_timeout(timeo);
478 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
479 }
480 __set_current_state(TASK_RUNNING);
481 remove_wait_queue(sk_sleep(sk), &wait);
482
483 if (ret)
484 goto error;
485
486 newsock->state = SS_CONNECTED;
487
488 pr_debug("new socket %p\n", new_sk);
489
490error:
491 release_sock(sk);
492
493 return ret;
494}
495
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200496static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
Samuel Ortizd6469602011-12-14 16:43:12 +0100497 int *len, int peer)
498{
Samuel Ortizd6469602011-12-14 16:43:12 +0100499 struct sock *sk = sock->sk;
500 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200501 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr);
Samuel Ortizd6469602011-12-14 16:43:12 +0100502
Samuel Ortizfe3c0942012-07-05 17:43:08 +0200503 if (llcp_sock == NULL || llcp_sock->dev == NULL)
504 return -EBADFD;
505
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200506 pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx,
507 llcp_sock->dsap, llcp_sock->ssap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100508
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000509 memset(llcp_addr, 0, sizeof(*llcp_addr));
Samuel Ortizd6469602011-12-14 16:43:12 +0100510 *len = sizeof(struct sockaddr_nfc_llcp);
511
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000512 llcp_addr->sa_family = AF_NFC;
Samuel Ortizd6469602011-12-14 16:43:12 +0100513 llcp_addr->dev_idx = llcp_sock->dev->idx;
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200514 llcp_addr->target_idx = llcp_sock->target_idx;
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000515 llcp_addr->nfc_protocol = llcp_sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100516 llcp_addr->dsap = llcp_sock->dsap;
517 llcp_addr->ssap = llcp_sock->ssap;
518 llcp_addr->service_name_len = llcp_sock->service_name_len;
519 memcpy(llcp_addr->service_name, llcp_sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100520 llcp_addr->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100521
522 return 0;
523}
524
525static inline unsigned int llcp_accept_poll(struct sock *parent)
526{
527 struct nfc_llcp_sock *llcp_sock, *n, *parent_sock;
528 struct sock *sk;
529
530 parent_sock = nfc_llcp_sock(parent);
531
532 list_for_each_entry_safe(llcp_sock, n, &parent_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100533 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100534 sk = &llcp_sock->sk;
535
536 if (sk->sk_state == LLCP_CONNECTED)
537 return POLLIN | POLLRDNORM;
538 }
539
540 return 0;
541}
542
543static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100544 poll_table *wait)
Samuel Ortizd6469602011-12-14 16:43:12 +0100545{
546 struct sock *sk = sock->sk;
547 unsigned int mask = 0;
548
549 pr_debug("%p\n", sk);
550
551 sock_poll_wait(file, sk_sleep(sk), wait);
552
553 if (sk->sk_state == LLCP_LISTEN)
554 return llcp_accept_poll(sk);
555
556 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +0000557 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -0700558 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Samuel Ortizd6469602011-12-14 16:43:12 +0100559
560 if (!skb_queue_empty(&sk->sk_receive_queue))
Samuel Ortiz4260c132012-05-07 12:31:20 +0200561 mask |= POLLIN | POLLRDNORM;
Samuel Ortizd6469602011-12-14 16:43:12 +0100562
563 if (sk->sk_state == LLCP_CLOSED)
564 mask |= POLLHUP;
565
Samuel Ortiz4260c132012-05-07 12:31:20 +0200566 if (sk->sk_shutdown & RCV_SHUTDOWN)
567 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
568
569 if (sk->sk_shutdown == SHUTDOWN_MASK)
570 mask |= POLLHUP;
571
Samuel Ortizb4011232013-05-03 18:29:30 +0200572 if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED)
Samuel Ortiz4260c132012-05-07 12:31:20 +0200573 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
574 else
575 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
576
577 pr_debug("mask 0x%x\n", mask);
578
Samuel Ortizd6469602011-12-14 16:43:12 +0100579 return mask;
580}
581
582static int llcp_sock_release(struct socket *sock)
583{
584 struct sock *sk = sock->sk;
585 struct nfc_llcp_local *local;
586 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100587 int err = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100588
589 if (!sk)
590 return 0;
591
592 pr_debug("%p\n", sk);
593
594 local = llcp_sock->local;
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100595 if (local == NULL) {
596 err = -ENODEV;
597 goto out;
598 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100599
Samuel Ortizd6469602011-12-14 16:43:12 +0100600 lock_sock(sk);
601
602 /* Send a DISC */
603 if (sk->sk_state == LLCP_CONNECTED)
Thierry Escande58e3dd12013-06-04 11:34:50 +0200604 nfc_llcp_send_disconnect(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100605
606 if (sk->sk_state == LLCP_LISTEN) {
607 struct nfc_llcp_sock *lsk, *n;
608 struct sock *accept_sk;
609
610 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100611 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100612 accept_sk = &lsk->sk;
613 lock_sock(accept_sk);
614
Thierry Escande58e3dd12013-06-04 11:34:50 +0200615 nfc_llcp_send_disconnect(lsk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100616 nfc_llcp_accept_unlink(accept_sk);
617
618 release_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100619 }
620 }
621
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200622 if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
623 nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100624
Samuel Ortizd6469602011-12-14 16:43:12 +0100625 release_sock(sk);
626
Thierry Escande17f7ae162013-06-04 11:34:51 +0200627 /* Keep this sock alive and therefore do not remove it from the sockets
628 * list until the DISC PDU has been actually sent. Otherwise we would
629 * reply with DM PDUs before sending the DISC one.
630 */
631 if (sk->sk_state == LLCP_DISCONNECTING)
632 return err;
633
Thierry Escande44635232012-09-26 18:16:44 +0200634 if (sock->type == SOCK_RAW)
635 nfc_llcp_sock_unlink(&local->raw_sockets, sk);
636 else
637 nfc_llcp_sock_unlink(&local->sockets, sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200638
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100639out:
Samuel Ortizd6469602011-12-14 16:43:12 +0100640 sock_orphan(sk);
641 sock_put(sk);
642
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100643 return err;
Samuel Ortizd6469602011-12-14 16:43:12 +0100644}
645
646static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100647 int len, int flags)
Samuel Ortizd6469602011-12-14 16:43:12 +0100648{
649 struct sock *sk = sock->sk;
650 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
651 struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
652 struct nfc_dev *dev;
653 struct nfc_llcp_local *local;
654 int ret = 0;
655
656 pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
657
658 if (!addr || len < sizeof(struct sockaddr_nfc) ||
Dave Jones32418cf2012-10-04 09:51:11 +0000659 addr->sa_family != AF_NFC)
Samuel Ortizd6469602011-12-14 16:43:12 +0100660 return -EINVAL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100661
Dave Jones32418cf2012-10-04 09:51:11 +0000662 if (addr->service_name_len == 0 && addr->dsap == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100663 return -EINVAL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100664
665 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100666 addr->target_idx, addr->nfc_protocol);
Samuel Ortizd6469602011-12-14 16:43:12 +0100667
668 lock_sock(sk);
669
670 if (sk->sk_state == LLCP_CONNECTED) {
671 ret = -EISCONN;
672 goto error;
673 }
674
675 dev = nfc_get_device(addr->dev_idx);
676 if (dev == NULL) {
677 ret = -ENODEV;
678 goto error;
679 }
680
681 local = nfc_llcp_find_local(dev);
682 if (local == NULL) {
683 ret = -ENODEV;
684 goto put_dev;
685 }
686
687 device_lock(&dev->dev);
688 if (dev->dep_link_up == false) {
689 ret = -ENOLINK;
690 device_unlock(&dev->dev);
691 goto put_dev;
692 }
693 device_unlock(&dev->dev);
694
695 if (local->rf_mode == NFC_RF_INITIATOR &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100696 addr->target_idx != local->target_idx) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100697 ret = -ENOLINK;
698 goto put_dev;
699 }
700
701 llcp_sock->dev = dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200702 llcp_sock->local = nfc_llcp_local_get(local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100703 llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
704 if (llcp_sock->ssap == LLCP_SAP_MAX) {
705 ret = -ENOMEM;
706 goto put_dev;
707 }
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200708
709 llcp_sock->reserved_ssap = llcp_sock->ssap;
710
Samuel Ortizd6469602011-12-14 16:43:12 +0100711 if (addr->service_name_len == 0)
712 llcp_sock->dsap = addr->dsap;
713 else
714 llcp_sock->dsap = LLCP_SAP_SDP;
715 llcp_sock->nfc_protocol = addr->nfc_protocol;
716 llcp_sock->service_name_len = min_t(unsigned int,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100717 addr->service_name_len,
718 NFC_LLCP_MAX_SERVICE_NAME);
Samuel Ortizd6469602011-12-14 16:43:12 +0100719 llcp_sock->service_name = kmemdup(addr->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100720 llcp_sock->service_name_len,
721 GFP_KERNEL);
Samuel Ortizd6469602011-12-14 16:43:12 +0100722
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200723 nfc_llcp_sock_link(&local->connecting_sockets, sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100724
725 ret = nfc_llcp_send_connect(llcp_sock);
726 if (ret)
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200727 goto sock_unlink;
Samuel Ortizd6469602011-12-14 16:43:12 +0100728
Samuel Ortizb4011232013-05-03 18:29:30 +0200729 sk->sk_state = LLCP_CONNECTING;
730
Samuel Ortizff353d82012-05-07 12:31:19 +0200731 ret = sock_wait_state(sk, LLCP_CONNECTED,
732 sock_sndtimeo(sk, flags & O_NONBLOCK));
Samuel Ortizb4011232013-05-03 18:29:30 +0200733 if (ret && ret != -EINPROGRESS)
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200734 goto sock_unlink;
Samuel Ortizd6469602011-12-14 16:43:12 +0100735
736 release_sock(sk);
Samuel Ortizff353d82012-05-07 12:31:19 +0200737
Samuel Ortizb4011232013-05-03 18:29:30 +0200738 return ret;
Samuel Ortizd6469602011-12-14 16:43:12 +0100739
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200740sock_unlink:
741 nfc_llcp_put_ssap(local, llcp_sock->ssap);
742
743 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
744
Samuel Ortizd6469602011-12-14 16:43:12 +0100745put_dev:
746 nfc_put_device(dev);
747
748error:
749 release_sock(sk);
750 return ret;
751}
752
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100753static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100754 struct msghdr *msg, size_t len)
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100755{
756 struct sock *sk = sock->sk;
757 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
758 int ret;
759
760 pr_debug("sock %p sk %p", sock, sk);
761
762 ret = sock_error(sk);
763 if (ret)
764 return ret;
765
766 if (msg->msg_flags & MSG_OOB)
767 return -EOPNOTSUPP;
768
769 lock_sock(sk);
770
Samuel Ortizb874dec2012-10-16 15:04:10 +0200771 if (sk->sk_type == SOCK_DGRAM) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100772 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr,
773 msg->msg_name);
Samuel Ortizb874dec2012-10-16 15:04:10 +0200774
775 if (msg->msg_namelen < sizeof(*addr)) {
776 release_sock(sk);
Samuel Ortizb874dec2012-10-16 15:04:10 +0200777 return -EINVAL;
778 }
779
780 release_sock(sk);
781
782 return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
783 msg, len);
784 }
785
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100786 if (sk->sk_state != LLCP_CONNECTED) {
787 release_sock(sk);
788 return -ENOTCONN;
789 }
790
791 release_sock(sk);
792
793 return nfc_llcp_send_i_frame(llcp_sock, msg, len);
794}
795
Samuel Ortizd6469602011-12-14 16:43:12 +0100796static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
797 struct msghdr *msg, size_t len, int flags)
798{
799 int noblock = flags & MSG_DONTWAIT;
800 struct sock *sk = sock->sk;
801 unsigned int copied, rlen;
802 struct sk_buff *skb, *cskb;
803 int err = 0;
804
805 pr_debug("%p %zu\n", sk, len);
806
807 lock_sock(sk);
808
809 if (sk->sk_state == LLCP_CLOSED &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100810 skb_queue_empty(&sk->sk_receive_queue)) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100811 release_sock(sk);
812 return 0;
813 }
814
815 release_sock(sk);
816
817 if (flags & (MSG_OOB))
818 return -EOPNOTSUPP;
819
820 skb = skb_recv_datagram(sk, flags, noblock, &err);
821 if (!skb) {
822 pr_err("Recv datagram failed state %d %d %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100823 sk->sk_state, err, sock_error(sk));
Samuel Ortizd6469602011-12-14 16:43:12 +0100824
825 if (sk->sk_shutdown & RCV_SHUTDOWN)
826 return 0;
827
828 return err;
829 }
830
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100831 rlen = skb->len; /* real length of skb */
Samuel Ortizd6469602011-12-14 16:43:12 +0100832 copied = min_t(unsigned int, rlen, len);
833
834 cskb = skb;
Samuel Ortizfb97c3e2013-01-11 14:54:22 +0100835 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100836 if (!(flags & MSG_PEEK))
837 skb_queue_head(&sk->sk_receive_queue, skb);
838 return -EFAULT;
839 }
840
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100841 sock_recv_timestamp(msg, sk, skb);
842
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200843 if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
844 struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100845 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, sockaddr,
846 msg->msg_name);
Samuel Ortizfad2e372013-01-11 14:48:48 +0100847
848 msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200849
850 pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
851
Mathias Kraused26d6502013-04-07 01:51:58 +0000852 memset(sockaddr, 0, sizeof(*sockaddr));
Samuel Ortizfad2e372013-01-11 14:48:48 +0100853 sockaddr->sa_family = AF_NFC;
854 sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP;
855 sockaddr->dsap = ui_cb->dsap;
856 sockaddr->ssap = ui_cb->ssap;
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200857 }
858
Samuel Ortizd6469602011-12-14 16:43:12 +0100859 /* Mark read part of skb as used */
860 if (!(flags & MSG_PEEK)) {
861
862 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200863 if (sk->sk_type == SOCK_STREAM ||
864 sk->sk_type == SOCK_DGRAM ||
865 sk->sk_type == SOCK_RAW) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100866 skb_pull(skb, copied);
867 if (skb->len) {
868 skb_queue_head(&sk->sk_receive_queue, skb);
869 goto done;
870 }
871 }
872
873 kfree_skb(skb);
874 }
875
876 /* XXX Queue backlogged skbs */
877
878done:
879 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
880 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
881 copied = rlen;
882
883 return copied;
884}
885
886static const struct proto_ops llcp_sock_ops = {
887 .family = PF_NFC,
888 .owner = THIS_MODULE,
889 .bind = llcp_sock_bind,
890 .connect = llcp_sock_connect,
891 .release = llcp_sock_release,
892 .socketpair = sock_no_socketpair,
893 .accept = llcp_sock_accept,
894 .getname = llcp_sock_getname,
895 .poll = llcp_sock_poll,
896 .ioctl = sock_no_ioctl,
897 .listen = llcp_sock_listen,
898 .shutdown = sock_no_shutdown,
Samuel Ortiz26fd76c2013-02-22 10:53:25 +0100899 .setsockopt = nfc_llcp_setsockopt,
900 .getsockopt = nfc_llcp_getsockopt,
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100901 .sendmsg = llcp_sock_sendmsg,
Samuel Ortizd6469602011-12-14 16:43:12 +0100902 .recvmsg = llcp_sock_recvmsg,
903 .mmap = sock_no_mmap,
904};
905
Thierry Escande44635232012-09-26 18:16:44 +0200906static const struct proto_ops llcp_rawsock_ops = {
907 .family = PF_NFC,
908 .owner = THIS_MODULE,
909 .bind = llcp_raw_sock_bind,
910 .connect = sock_no_connect,
911 .release = llcp_sock_release,
912 .socketpair = sock_no_socketpair,
913 .accept = sock_no_accept,
914 .getname = llcp_sock_getname,
915 .poll = llcp_sock_poll,
916 .ioctl = sock_no_ioctl,
917 .listen = sock_no_listen,
918 .shutdown = sock_no_shutdown,
919 .setsockopt = sock_no_setsockopt,
920 .getsockopt = sock_no_getsockopt,
921 .sendmsg = sock_no_sendmsg,
922 .recvmsg = llcp_sock_recvmsg,
923 .mmap = sock_no_mmap,
924};
925
Samuel Ortizd6469602011-12-14 16:43:12 +0100926static void llcp_sock_destruct(struct sock *sk)
927{
928 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
929
930 pr_debug("%p\n", sk);
931
932 if (sk->sk_state == LLCP_CONNECTED)
933 nfc_put_device(llcp_sock->dev);
934
935 skb_queue_purge(&sk->sk_receive_queue);
936
937 nfc_llcp_sock_free(llcp_sock);
938
939 if (!sock_flag(sk, SOCK_DEAD)) {
940 pr_err("Freeing alive NFC LLCP socket %p\n", sk);
941 return;
942 }
943}
944
945struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp)
946{
947 struct sock *sk;
948 struct nfc_llcp_sock *llcp_sock;
949
950 sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto);
951 if (!sk)
952 return NULL;
953
954 llcp_sock = nfc_llcp_sock(sk);
955
956 sock_init_data(sock, sk);
957 sk->sk_state = LLCP_CLOSED;
958 sk->sk_protocol = NFC_SOCKPROTO_LLCP;
959 sk->sk_type = type;
960 sk->sk_destruct = llcp_sock_destruct;
961
962 llcp_sock->ssap = 0;
963 llcp_sock->dsap = LLCP_SAP_SDP;
Samuel Ortiz06d44f82013-02-22 11:38:05 +0100964 llcp_sock->rw = LLCP_MAX_RW + 1;
Samuel Ortiz5eef6662013-03-20 16:06:12 +0100965 llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1);
Samuel Ortizd6469602011-12-14 16:43:12 +0100966 llcp_sock->send_n = llcp_sock->send_ack_n = 0;
967 llcp_sock->recv_n = llcp_sock->recv_ack_n = 0;
968 llcp_sock->remote_ready = 1;
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200969 llcp_sock->reserved_ssap = LLCP_SAP_MAX;
Thierry Escandeabd18d42013-04-02 10:25:15 +0200970 nfc_llcp_socket_remote_param_init(llcp_sock);
Samuel Ortizd6469602011-12-14 16:43:12 +0100971 skb_queue_head_init(&llcp_sock->tx_queue);
972 skb_queue_head_init(&llcp_sock->tx_pending_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100973 INIT_LIST_HEAD(&llcp_sock->accept_queue);
974
975 if (sock != NULL)
976 sock->state = SS_UNCONNECTED;
977
978 return sk;
979}
980
981void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
982{
983 kfree(sock->service_name);
984
985 skb_queue_purge(&sock->tx_queue);
986 skb_queue_purge(&sock->tx_pending_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100987
988 list_del_init(&sock->accept_queue);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100989
Samuel Ortizd6469602011-12-14 16:43:12 +0100990 sock->parent = NULL;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200991
992 nfc_llcp_local_put(sock->local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100993}
994
995static int llcp_sock_create(struct net *net, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100996 const struct nfc_protocol *nfc_proto)
Samuel Ortizd6469602011-12-14 16:43:12 +0100997{
998 struct sock *sk;
999
1000 pr_debug("%p\n", sock);
1001
Thierry Escande44635232012-09-26 18:16:44 +02001002 if (sock->type != SOCK_STREAM &&
1003 sock->type != SOCK_DGRAM &&
1004 sock->type != SOCK_RAW)
Samuel Ortizd6469602011-12-14 16:43:12 +01001005 return -ESOCKTNOSUPPORT;
1006
Thierry Escande44635232012-09-26 18:16:44 +02001007 if (sock->type == SOCK_RAW)
1008 sock->ops = &llcp_rawsock_ops;
1009 else
1010 sock->ops = &llcp_sock_ops;
Samuel Ortizd6469602011-12-14 16:43:12 +01001011
1012 sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC);
1013 if (sk == NULL)
1014 return -ENOMEM;
1015
1016 return 0;
1017}
1018
1019static const struct nfc_protocol llcp_nfc_proto = {
1020 .id = NFC_SOCKPROTO_LLCP,
1021 .proto = &llcp_sock_proto,
1022 .owner = THIS_MODULE,
1023 .create = llcp_sock_create
1024};
1025
1026int __init nfc_llcp_sock_init(void)
1027{
1028 return nfc_proto_register(&llcp_nfc_proto);
1029}
1030
1031void nfc_llcp_sock_exit(void)
1032{
1033 nfc_proto_unregister(&llcp_nfc_proto);
1034}