blob: e16315719eafe731366a6dfd3e6b6f2aee7a22ed [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
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 Escande44635232012-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
226void 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
239void 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 Ortiz427a2eb2012-03-05 01:03:52 +0100248 &llcp_sock_parent->accept_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100249 llcp_sock->parent = parent;
250 sk_acceptq_added(parent);
251}
252
253struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100254 struct socket *newsock)
Samuel Ortizd6469602011-12-14 16:43:12 +0100255{
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 Ortiz427a2eb2012-03-05 01:03:52 +0100262 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100263 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) {
Samuel Ortiz39a352a2013-03-26 14:35:57 +0100273 list_del_init(&lsk->accept_queue);
274 sock_put(sk);
275
Samuel Ortizd6469602011-12-14 16:43:12 +0100276 if (newsock)
277 sock_graft(sk, newsock);
278
279 release_sock(sk);
280
281 pr_debug("Returning sk state %d\n", sk->sk_state);
282
Samuel Ortizb141e812013-02-21 11:04:45 +0100283 sk_acceptq_removed(parent);
284
Samuel Ortizd6469602011-12-14 16:43:12 +0100285 return sk;
286 }
287
288 release_sock(sk);
289 }
290
291 return NULL;
292}
293
294static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100295 int flags)
Samuel Ortizd6469602011-12-14 16:43:12 +0100296{
297 DECLARE_WAITQUEUE(wait, current);
298 struct sock *sk = sock->sk, *new_sk;
299 long timeo;
300 int ret = 0;
301
302 pr_debug("parent %p\n", sk);
303
304 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
305
306 if (sk->sk_state != LLCP_LISTEN) {
307 ret = -EBADFD;
308 goto error;
309 }
310
311 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
312
313 /* Wait for an incoming connection. */
314 add_wait_queue_exclusive(sk_sleep(sk), &wait);
315 while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
316 set_current_state(TASK_INTERRUPTIBLE);
317
318 if (!timeo) {
319 ret = -EAGAIN;
320 break;
321 }
322
323 if (signal_pending(current)) {
324 ret = sock_intr_errno(timeo);
325 break;
326 }
327
328 release_sock(sk);
329 timeo = schedule_timeout(timeo);
330 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
331 }
332 __set_current_state(TASK_RUNNING);
333 remove_wait_queue(sk_sleep(sk), &wait);
334
335 if (ret)
336 goto error;
337
338 newsock->state = SS_CONNECTED;
339
340 pr_debug("new socket %p\n", new_sk);
341
342error:
343 release_sock(sk);
344
345 return ret;
346}
347
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200348static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
Samuel Ortizd6469602011-12-14 16:43:12 +0100349 int *len, int peer)
350{
Samuel Ortizd6469602011-12-14 16:43:12 +0100351 struct sock *sk = sock->sk;
352 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200353 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr);
Samuel Ortizd6469602011-12-14 16:43:12 +0100354
Samuel Ortizfe3c0942012-07-05 17:43:08 +0200355 if (llcp_sock == NULL || llcp_sock->dev == NULL)
356 return -EBADFD;
357
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200358 pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx,
359 llcp_sock->dsap, llcp_sock->ssap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100360
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000361 memset(llcp_addr, 0, sizeof(*llcp_addr));
Samuel Ortizd6469602011-12-14 16:43:12 +0100362 *len = sizeof(struct sockaddr_nfc_llcp);
363
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000364 llcp_addr->sa_family = AF_NFC;
Samuel Ortizd6469602011-12-14 16:43:12 +0100365 llcp_addr->dev_idx = llcp_sock->dev->idx;
Samuel Ortiz12e5bdf2012-06-21 17:41:42 +0200366 llcp_addr->target_idx = llcp_sock->target_idx;
Dan Carpenter5ffedc62013-04-22 20:24:52 +0000367 llcp_addr->nfc_protocol = llcp_sock->nfc_protocol;
Samuel Ortizd6469602011-12-14 16:43:12 +0100368 llcp_addr->dsap = llcp_sock->dsap;
369 llcp_addr->ssap = llcp_sock->ssap;
370 llcp_addr->service_name_len = llcp_sock->service_name_len;
371 memcpy(llcp_addr->service_name, llcp_sock->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100372 llcp_addr->service_name_len);
Samuel Ortizd6469602011-12-14 16:43:12 +0100373
374 return 0;
375}
376
377static inline unsigned int llcp_accept_poll(struct sock *parent)
378{
379 struct nfc_llcp_sock *llcp_sock, *n, *parent_sock;
380 struct sock *sk;
381
382 parent_sock = nfc_llcp_sock(parent);
383
384 list_for_each_entry_safe(llcp_sock, n, &parent_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100385 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100386 sk = &llcp_sock->sk;
387
388 if (sk->sk_state == LLCP_CONNECTED)
389 return POLLIN | POLLRDNORM;
390 }
391
392 return 0;
393}
394
395static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100396 poll_table *wait)
Samuel Ortizd6469602011-12-14 16:43:12 +0100397{
398 struct sock *sk = sock->sk;
399 unsigned int mask = 0;
400
401 pr_debug("%p\n", sk);
402
403 sock_poll_wait(file, sk_sleep(sk), wait);
404
405 if (sk->sk_state == LLCP_LISTEN)
406 return llcp_accept_poll(sk);
407
408 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
409 mask |= POLLERR;
410
411 if (!skb_queue_empty(&sk->sk_receive_queue))
Samuel Ortiz4260c132012-05-07 12:31:20 +0200412 mask |= POLLIN | POLLRDNORM;
Samuel Ortizd6469602011-12-14 16:43:12 +0100413
414 if (sk->sk_state == LLCP_CLOSED)
415 mask |= POLLHUP;
416
Samuel Ortiz4260c132012-05-07 12:31:20 +0200417 if (sk->sk_shutdown & RCV_SHUTDOWN)
418 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
419
420 if (sk->sk_shutdown == SHUTDOWN_MASK)
421 mask |= POLLHUP;
422
423 if (sock_writeable(sk))
424 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
425 else
426 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
427
428 pr_debug("mask 0x%x\n", mask);
429
Samuel Ortizd6469602011-12-14 16:43:12 +0100430 return mask;
431}
432
433static int llcp_sock_release(struct socket *sock)
434{
435 struct sock *sk = sock->sk;
436 struct nfc_llcp_local *local;
437 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100438 int err = 0;
Samuel Ortizd6469602011-12-14 16:43:12 +0100439
440 if (!sk)
441 return 0;
442
443 pr_debug("%p\n", sk);
444
445 local = llcp_sock->local;
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100446 if (local == NULL) {
447 err = -ENODEV;
448 goto out;
449 }
Samuel Ortizd6469602011-12-14 16:43:12 +0100450
Samuel Ortizd6469602011-12-14 16:43:12 +0100451 lock_sock(sk);
452
453 /* Send a DISC */
454 if (sk->sk_state == LLCP_CONNECTED)
455 nfc_llcp_disconnect(llcp_sock);
456
457 if (sk->sk_state == LLCP_LISTEN) {
458 struct nfc_llcp_sock *lsk, *n;
459 struct sock *accept_sk;
460
461 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100462 accept_queue) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100463 accept_sk = &lsk->sk;
464 lock_sock(accept_sk);
465
466 nfc_llcp_disconnect(lsk);
467 nfc_llcp_accept_unlink(accept_sk);
468
469 release_sock(accept_sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100470 }
471 }
472
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200473 if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
474 nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
Samuel Ortizd6469602011-12-14 16:43:12 +0100475
Samuel Ortizd6469602011-12-14 16:43:12 +0100476 release_sock(sk);
477
Thierry Escande44635232012-09-26 18:16:44 +0200478 if (sock->type == SOCK_RAW)
479 nfc_llcp_sock_unlink(&local->raw_sockets, sk);
480 else
481 nfc_llcp_sock_unlink(&local->sockets, sk);
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200482
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100483out:
Samuel Ortizd6469602011-12-14 16:43:12 +0100484 sock_orphan(sk);
485 sock_put(sk);
486
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100487 return err;
Samuel Ortizd6469602011-12-14 16:43:12 +0100488}
489
490static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100491 int len, int flags)
Samuel Ortizd6469602011-12-14 16:43:12 +0100492{
493 struct sock *sk = sock->sk;
494 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
495 struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
496 struct nfc_dev *dev;
497 struct nfc_llcp_local *local;
498 int ret = 0;
499
500 pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
501
502 if (!addr || len < sizeof(struct sockaddr_nfc) ||
Dave Jones32418cf2012-10-04 09:51:11 +0000503 addr->sa_family != AF_NFC)
Samuel Ortizd6469602011-12-14 16:43:12 +0100504 return -EINVAL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100505
Dave Jones32418cf2012-10-04 09:51:11 +0000506 if (addr->service_name_len == 0 && addr->dsap == 0)
Samuel Ortizd6469602011-12-14 16:43:12 +0100507 return -EINVAL;
Samuel Ortizd6469602011-12-14 16:43:12 +0100508
509 pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100510 addr->target_idx, addr->nfc_protocol);
Samuel Ortizd6469602011-12-14 16:43:12 +0100511
512 lock_sock(sk);
513
514 if (sk->sk_state == LLCP_CONNECTED) {
515 ret = -EISCONN;
516 goto error;
517 }
518
519 dev = nfc_get_device(addr->dev_idx);
520 if (dev == NULL) {
521 ret = -ENODEV;
522 goto error;
523 }
524
525 local = nfc_llcp_find_local(dev);
526 if (local == NULL) {
527 ret = -ENODEV;
528 goto put_dev;
529 }
530
531 device_lock(&dev->dev);
532 if (dev->dep_link_up == false) {
533 ret = -ENOLINK;
534 device_unlock(&dev->dev);
535 goto put_dev;
536 }
537 device_unlock(&dev->dev);
538
539 if (local->rf_mode == NFC_RF_INITIATOR &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100540 addr->target_idx != local->target_idx) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100541 ret = -ENOLINK;
542 goto put_dev;
543 }
544
545 llcp_sock->dev = dev;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200546 llcp_sock->local = nfc_llcp_local_get(local);
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200547 llcp_sock->miu = llcp_sock->local->remote_miu;
Samuel Ortizd6469602011-12-14 16:43:12 +0100548 llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
549 if (llcp_sock->ssap == LLCP_SAP_MAX) {
550 ret = -ENOMEM;
551 goto put_dev;
552 }
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200553
554 llcp_sock->reserved_ssap = llcp_sock->ssap;
555
Samuel Ortizd6469602011-12-14 16:43:12 +0100556 if (addr->service_name_len == 0)
557 llcp_sock->dsap = addr->dsap;
558 else
559 llcp_sock->dsap = LLCP_SAP_SDP;
560 llcp_sock->nfc_protocol = addr->nfc_protocol;
561 llcp_sock->service_name_len = min_t(unsigned int,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100562 addr->service_name_len,
563 NFC_LLCP_MAX_SERVICE_NAME);
Samuel Ortizd6469602011-12-14 16:43:12 +0100564 llcp_sock->service_name = kmemdup(addr->service_name,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100565 llcp_sock->service_name_len,
566 GFP_KERNEL);
Samuel Ortizd6469602011-12-14 16:43:12 +0100567
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200568 nfc_llcp_sock_link(&local->connecting_sockets, sk);
Samuel Ortizd6469602011-12-14 16:43:12 +0100569
570 ret = nfc_llcp_send_connect(llcp_sock);
571 if (ret)
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200572 goto sock_unlink;
Samuel Ortizd6469602011-12-14 16:43:12 +0100573
Samuel Ortizff353d82012-05-07 12:31:19 +0200574 ret = sock_wait_state(sk, LLCP_CONNECTED,
575 sock_sndtimeo(sk, flags & O_NONBLOCK));
576 if (ret)
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200577 goto sock_unlink;
Samuel Ortizd6469602011-12-14 16:43:12 +0100578
579 release_sock(sk);
Samuel Ortizff353d82012-05-07 12:31:19 +0200580
Samuel Ortizd6469602011-12-14 16:43:12 +0100581 return 0;
582
Samuel Ortiza69f32a2012-05-04 17:04:19 +0200583sock_unlink:
584 nfc_llcp_put_ssap(local, llcp_sock->ssap);
585
586 nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
587
Samuel Ortizd6469602011-12-14 16:43:12 +0100588put_dev:
589 nfc_put_device(dev);
590
591error:
592 release_sock(sk);
593 return ret;
594}
595
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100596static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100597 struct msghdr *msg, size_t len)
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100598{
599 struct sock *sk = sock->sk;
600 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
601 int ret;
602
603 pr_debug("sock %p sk %p", sock, sk);
604
605 ret = sock_error(sk);
606 if (ret)
607 return ret;
608
609 if (msg->msg_flags & MSG_OOB)
610 return -EOPNOTSUPP;
611
612 lock_sock(sk);
613
Samuel Ortizb874dec2012-10-16 15:04:10 +0200614 if (sk->sk_type == SOCK_DGRAM) {
615 struct sockaddr_nfc_llcp *addr =
616 (struct sockaddr_nfc_llcp *)msg->msg_name;
617
618 if (msg->msg_namelen < sizeof(*addr)) {
619 release_sock(sk);
Samuel Ortizb874dec2012-10-16 15:04:10 +0200620 return -EINVAL;
621 }
622
623 release_sock(sk);
624
625 return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
626 msg, len);
627 }
628
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100629 if (sk->sk_state != LLCP_CONNECTED) {
630 release_sock(sk);
631 return -ENOTCONN;
632 }
633
634 release_sock(sk);
635
636 return nfc_llcp_send_i_frame(llcp_sock, msg, len);
637}
638
Samuel Ortizd6469602011-12-14 16:43:12 +0100639static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
640 struct msghdr *msg, size_t len, int flags)
641{
642 int noblock = flags & MSG_DONTWAIT;
643 struct sock *sk = sock->sk;
644 unsigned int copied, rlen;
645 struct sk_buff *skb, *cskb;
646 int err = 0;
647
648 pr_debug("%p %zu\n", sk, len);
649
Mathias Kraused26d6502013-04-07 01:51:58 +0000650 msg->msg_namelen = 0;
651
Samuel Ortizd6469602011-12-14 16:43:12 +0100652 lock_sock(sk);
653
654 if (sk->sk_state == LLCP_CLOSED &&
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100655 skb_queue_empty(&sk->sk_receive_queue)) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100656 release_sock(sk);
657 return 0;
658 }
659
660 release_sock(sk);
661
662 if (flags & (MSG_OOB))
663 return -EOPNOTSUPP;
664
665 skb = skb_recv_datagram(sk, flags, noblock, &err);
666 if (!skb) {
667 pr_err("Recv datagram failed state %d %d %d",
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100668 sk->sk_state, err, sock_error(sk));
Samuel Ortizd6469602011-12-14 16:43:12 +0100669
670 if (sk->sk_shutdown & RCV_SHUTDOWN)
671 return 0;
672
673 return err;
674 }
675
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100676 rlen = skb->len; /* real length of skb */
Samuel Ortizd6469602011-12-14 16:43:12 +0100677 copied = min_t(unsigned int, rlen, len);
678
679 cskb = skb;
Samuel Ortizfb97c3e2013-01-11 14:54:22 +0100680 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100681 if (!(flags & MSG_PEEK))
682 skb_queue_head(&sk->sk_receive_queue, skb);
683 return -EFAULT;
684 }
685
Thierry Escande2c2d45b2012-11-27 15:44:24 +0100686 sock_recv_timestamp(msg, sk, skb);
687
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200688 if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
689 struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
Samuel Ortizfad2e372013-01-11 14:48:48 +0100690 struct sockaddr_nfc_llcp *sockaddr =
691 (struct sockaddr_nfc_llcp *) msg->msg_name;
692
693 msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200694
695 pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
696
Mathias Kraused26d6502013-04-07 01:51:58 +0000697 memset(sockaddr, 0, sizeof(*sockaddr));
Samuel Ortizfad2e372013-01-11 14:48:48 +0100698 sockaddr->sa_family = AF_NFC;
699 sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP;
700 sockaddr->dsap = ui_cb->dsap;
701 sockaddr->ssap = ui_cb->ssap;
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200702 }
703
Samuel Ortizd6469602011-12-14 16:43:12 +0100704 /* Mark read part of skb as used */
705 if (!(flags & MSG_PEEK)) {
706
707 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
Samuel Ortiz31ca61a2012-10-15 17:44:44 +0200708 if (sk->sk_type == SOCK_STREAM ||
709 sk->sk_type == SOCK_DGRAM ||
710 sk->sk_type == SOCK_RAW) {
Samuel Ortizd6469602011-12-14 16:43:12 +0100711 skb_pull(skb, copied);
712 if (skb->len) {
713 skb_queue_head(&sk->sk_receive_queue, skb);
714 goto done;
715 }
716 }
717
718 kfree_skb(skb);
719 }
720
721 /* XXX Queue backlogged skbs */
722
723done:
724 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
725 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
726 copied = rlen;
727
728 return copied;
729}
730
731static const struct proto_ops llcp_sock_ops = {
732 .family = PF_NFC,
733 .owner = THIS_MODULE,
734 .bind = llcp_sock_bind,
735 .connect = llcp_sock_connect,
736 .release = llcp_sock_release,
737 .socketpair = sock_no_socketpair,
738 .accept = llcp_sock_accept,
739 .getname = llcp_sock_getname,
740 .poll = llcp_sock_poll,
741 .ioctl = sock_no_ioctl,
742 .listen = llcp_sock_listen,
743 .shutdown = sock_no_shutdown,
744 .setsockopt = sock_no_setsockopt,
745 .getsockopt = sock_no_getsockopt,
Samuel Ortiz53a0ac22012-03-05 01:03:37 +0100746 .sendmsg = llcp_sock_sendmsg,
Samuel Ortizd6469602011-12-14 16:43:12 +0100747 .recvmsg = llcp_sock_recvmsg,
748 .mmap = sock_no_mmap,
749};
750
Thierry Escande44635232012-09-26 18:16:44 +0200751static const struct proto_ops llcp_rawsock_ops = {
752 .family = PF_NFC,
753 .owner = THIS_MODULE,
754 .bind = llcp_raw_sock_bind,
755 .connect = sock_no_connect,
756 .release = llcp_sock_release,
757 .socketpair = sock_no_socketpair,
758 .accept = sock_no_accept,
759 .getname = llcp_sock_getname,
760 .poll = llcp_sock_poll,
761 .ioctl = sock_no_ioctl,
762 .listen = sock_no_listen,
763 .shutdown = sock_no_shutdown,
764 .setsockopt = sock_no_setsockopt,
765 .getsockopt = sock_no_getsockopt,
766 .sendmsg = sock_no_sendmsg,
767 .recvmsg = llcp_sock_recvmsg,
768 .mmap = sock_no_mmap,
769};
770
Samuel Ortizd6469602011-12-14 16:43:12 +0100771static void llcp_sock_destruct(struct sock *sk)
772{
773 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
774
775 pr_debug("%p\n", sk);
776
777 if (sk->sk_state == LLCP_CONNECTED)
778 nfc_put_device(llcp_sock->dev);
779
780 skb_queue_purge(&sk->sk_receive_queue);
781
782 nfc_llcp_sock_free(llcp_sock);
783
784 if (!sock_flag(sk, SOCK_DEAD)) {
785 pr_err("Freeing alive NFC LLCP socket %p\n", sk);
786 return;
787 }
788}
789
790struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp)
791{
792 struct sock *sk;
793 struct nfc_llcp_sock *llcp_sock;
794
795 sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto);
796 if (!sk)
797 return NULL;
798
799 llcp_sock = nfc_llcp_sock(sk);
800
801 sock_init_data(sock, sk);
802 sk->sk_state = LLCP_CLOSED;
803 sk->sk_protocol = NFC_SOCKPROTO_LLCP;
804 sk->sk_type = type;
805 sk->sk_destruct = llcp_sock_destruct;
806
807 llcp_sock->ssap = 0;
808 llcp_sock->dsap = LLCP_SAP_SDP;
Samuel Ortiz7a06e582012-05-07 22:03:34 +0200809 llcp_sock->rw = LLCP_DEFAULT_RW;
Samuel Ortiz93d7e492012-05-14 17:37:32 +0200810 llcp_sock->miu = LLCP_DEFAULT_MIU;
Samuel Ortizd6469602011-12-14 16:43:12 +0100811 llcp_sock->send_n = llcp_sock->send_ack_n = 0;
812 llcp_sock->recv_n = llcp_sock->recv_ack_n = 0;
813 llcp_sock->remote_ready = 1;
Samuel Ortizcbbf4722012-06-22 14:48:11 +0200814 llcp_sock->reserved_ssap = LLCP_SAP_MAX;
Samuel Ortizd6469602011-12-14 16:43:12 +0100815 skb_queue_head_init(&llcp_sock->tx_queue);
816 skb_queue_head_init(&llcp_sock->tx_pending_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100817 INIT_LIST_HEAD(&llcp_sock->accept_queue);
818
819 if (sock != NULL)
820 sock->state = SS_UNCONNECTED;
821
822 return sk;
823}
824
825void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
826{
827 kfree(sock->service_name);
828
829 skb_queue_purge(&sock->tx_queue);
830 skb_queue_purge(&sock->tx_pending_queue);
Samuel Ortizd6469602011-12-14 16:43:12 +0100831
832 list_del_init(&sock->accept_queue);
Samuel Ortiz40c75f82012-03-05 01:03:51 +0100833
Samuel Ortizd6469602011-12-14 16:43:12 +0100834 sock->parent = NULL;
Samuel Ortizc7aa1222012-05-04 11:24:16 +0200835
836 nfc_llcp_local_put(sock->local);
Samuel Ortizd6469602011-12-14 16:43:12 +0100837}
838
839static int llcp_sock_create(struct net *net, struct socket *sock,
Samuel Ortiz427a2eb2012-03-05 01:03:52 +0100840 const struct nfc_protocol *nfc_proto)
Samuel Ortizd6469602011-12-14 16:43:12 +0100841{
842 struct sock *sk;
843
844 pr_debug("%p\n", sock);
845
Thierry Escande44635232012-09-26 18:16:44 +0200846 if (sock->type != SOCK_STREAM &&
847 sock->type != SOCK_DGRAM &&
848 sock->type != SOCK_RAW)
Samuel Ortizd6469602011-12-14 16:43:12 +0100849 return -ESOCKTNOSUPPORT;
850
Thierry Escande44635232012-09-26 18:16:44 +0200851 if (sock->type == SOCK_RAW)
852 sock->ops = &llcp_rawsock_ops;
853 else
854 sock->ops = &llcp_sock_ops;
Samuel Ortizd6469602011-12-14 16:43:12 +0100855
856 sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC);
857 if (sk == NULL)
858 return -ENOMEM;
859
860 return 0;
861}
862
863static const struct nfc_protocol llcp_nfc_proto = {
864 .id = NFC_SOCKPROTO_LLCP,
865 .proto = &llcp_sock_proto,
866 .owner = THIS_MODULE,
867 .create = llcp_sock_create
868};
869
870int __init nfc_llcp_sock_init(void)
871{
872 return nfc_proto_register(&llcp_nfc_proto);
873}
874
875void nfc_llcp_sock_exit(void)
876{
877 nfc_proto_unregister(&llcp_nfc_proto);
878}