~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/nfc/llcp_sock.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 2011  Intel Corporation. All rights reserved.
  3  *
  4  * This program is free software; you can redistribute it and/or modify
  5  * it under the terms of the GNU General Public License as published by
  6  * the Free Software Foundation; either version 2 of the License, or
  7  * (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
 16  */
 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 
 25 #include "nfc.h"
 26 #include "llcp.h"
 27 
 28 static 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 
 64 static struct proto llcp_sock_proto = {
 65         .name     = "NFC_LLCP",
 66         .owner    = THIS_MODULE,
 67         .obj_size = sizeof(struct nfc_llcp_sock),
 68 };
 69 
 70 static 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 
 79         if (!addr || addr->sa_family != AF_NFC)
 80                 return -EINVAL;
 81 
 82         pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
 83 
 84         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;
112         llcp_sock->local = nfc_llcp_local_get(local);
113         llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
114         llcp_sock->service_name_len = min_t(unsigned int,
115                                             llcp_addr.service_name_len,
116                                             NFC_LLCP_MAX_SERVICE_NAME);
117         llcp_sock->service_name = kmemdup(llcp_addr.service_name,
118                                           llcp_sock->service_name_len,
119                                           GFP_KERNEL);
120         if (!llcp_sock->service_name) {
121                 ret = -ENOMEM;
122                 goto put_dev;
123         }
124         llcp_sock->ssap = nfc_llcp_get_sdp_ssap(local, llcp_sock);
125         if (llcp_sock->ssap == LLCP_SAP_MAX) {
126                 kfree(llcp_sock->service_name);
127                 llcp_sock->service_name = NULL;
128                 ret = -EADDRINUSE;
129                 goto put_dev;
130         }
131 
132         llcp_sock->reserved_ssap = llcp_sock->ssap;
133 
134         nfc_llcp_sock_link(&local->sockets, sk);
135 
136         pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap);
137 
138         sk->sk_state = LLCP_BOUND;
139 
140 put_dev:
141         nfc_put_device(dev);
142 
143 error:
144         release_sock(sk);
145         return ret;
146 }
147 
148 static int llcp_raw_sock_bind(struct socket *sock, struct sockaddr *addr,
149                               int alen)
150 {
151         struct sock *sk = sock->sk;
152         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
153         struct nfc_llcp_local *local;
154         struct nfc_dev *dev;
155         struct sockaddr_nfc_llcp llcp_addr;
156         int len, ret = 0;
157 
158         if (!addr || addr->sa_family != AF_NFC)
159                 return -EINVAL;
160 
161         pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
162 
163         memset(&llcp_addr, 0, sizeof(llcp_addr));
164         len = min_t(unsigned int, sizeof(llcp_addr), alen);
165         memcpy(&llcp_addr, addr, len);
166 
167         lock_sock(sk);
168 
169         if (sk->sk_state != LLCP_CLOSED) {
170                 ret = -EBADFD;
171                 goto error;
172         }
173 
174         dev = nfc_get_device(llcp_addr.dev_idx);
175         if (dev == NULL) {
176                 ret = -ENODEV;
177                 goto error;
178         }
179 
180         local = nfc_llcp_find_local(dev);
181         if (local == NULL) {
182                 ret = -ENODEV;
183                 goto put_dev;
184         }
185 
186         llcp_sock->dev = dev;
187         llcp_sock->local = nfc_llcp_local_get(local);
188         llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
189 
190         nfc_llcp_sock_link(&local->raw_sockets, sk);
191 
192         sk->sk_state = LLCP_BOUND;
193 
194 put_dev:
195         nfc_put_device(dev);
196 
197 error:
198         release_sock(sk);
199         return ret;
200 }
201 
202 static int llcp_sock_listen(struct socket *sock, int backlog)
203 {
204         struct sock *sk = sock->sk;
205         int ret = 0;
206 
207         pr_debug("sk %p backlog %d\n", sk, backlog);
208 
209         lock_sock(sk);
210 
211         if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) ||
212             sk->sk_state != LLCP_BOUND) {
213                 ret = -EBADFD;
214                 goto error;
215         }
216 
217         sk->sk_max_ack_backlog = backlog;
218         sk->sk_ack_backlog = 0;
219 
220         pr_debug("Socket listening\n");
221         sk->sk_state = LLCP_LISTEN;
222 
223 error:
224         release_sock(sk);
225 
226         return ret;
227 }
228 
229 static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname,
230                                char __user *optval, unsigned int optlen)
231 {
232         struct sock *sk = sock->sk;
233         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
234         u32 opt;
235         int err = 0;
236 
237         pr_debug("%p optname %d\n", sk, optname);
238 
239         if (level != SOL_NFC)
240                 return -ENOPROTOOPT;
241 
242         lock_sock(sk);
243 
244         switch (optname) {
245         case NFC_LLCP_RW:
246                 if (sk->sk_state == LLCP_CONNECTED ||
247                     sk->sk_state == LLCP_BOUND ||
248                     sk->sk_state == LLCP_LISTEN) {
249                         err = -EINVAL;
250                         break;
251                 }
252 
253                 if (get_user(opt, (u32 __user *) optval)) {
254                         err = -EFAULT;
255                         break;
256                 }
257 
258                 if (opt > LLCP_MAX_RW) {
259                         err = -EINVAL;
260                         break;
261                 }
262 
263                 llcp_sock->rw = (u8) opt;
264 
265                 break;
266 
267         case NFC_LLCP_MIUX:
268                 if (sk->sk_state == LLCP_CONNECTED ||
269                     sk->sk_state == LLCP_BOUND ||
270                     sk->sk_state == LLCP_LISTEN) {
271                         err = -EINVAL;
272                         break;
273                 }
274 
275                 if (get_user(opt, (u32 __user *) optval)) {
276                         err = -EFAULT;
277                         break;
278                 }
279 
280                 if (opt > LLCP_MAX_MIUX) {
281                         err = -EINVAL;
282                         break;
283                 }
284 
285                 llcp_sock->miux = cpu_to_be16((u16) opt);
286 
287                 break;
288 
289         default:
290                 err = -ENOPROTOOPT;
291                 break;
292         }
293 
294         release_sock(sk);
295 
296         pr_debug("%p rw %d miux %d\n", llcp_sock,
297                  llcp_sock->rw, llcp_sock->miux);
298 
299         return err;
300 }
301 
302 static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
303                                char __user *optval, int __user *optlen)
304 {
305         struct nfc_llcp_local *local;
306         struct sock *sk = sock->sk;
307         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
308         int len, err = 0;
309         u16 miux, remote_miu;
310         u8 rw;
311 
312         pr_debug("%p optname %d\n", sk, optname);
313 
314         if (level != SOL_NFC)
315                 return -ENOPROTOOPT;
316 
317         if (get_user(len, optlen))
318                 return -EFAULT;
319 
320         local = llcp_sock->local;
321         if (!local)
322                 return -ENODEV;
323 
324         len = min_t(u32, len, sizeof(u32));
325 
326         lock_sock(sk);
327 
328         switch (optname) {
329         case NFC_LLCP_RW:
330                 rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw;
331                 if (put_user(rw, (u32 __user *) optval))
332                         err = -EFAULT;
333 
334                 break;
335 
336         case NFC_LLCP_MIUX:
337                 miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ?
338                         be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux);
339 
340                 if (put_user(miux, (u32 __user *) optval))
341                         err = -EFAULT;
342 
343                 break;
344 
345         case NFC_LLCP_REMOTE_MIU:
346                 remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ?
347                                 local->remote_miu : llcp_sock->remote_miu;
348 
349                 if (put_user(remote_miu, (u32 __user *) optval))
350                         err = -EFAULT;
351 
352                 break;
353 
354         case NFC_LLCP_REMOTE_LTO:
355                 if (put_user(local->remote_lto / 10, (u32 __user *) optval))
356                         err = -EFAULT;
357 
358                 break;
359 
360         case NFC_LLCP_REMOTE_RW:
361                 if (put_user(llcp_sock->remote_rw, (u32 __user *) optval))
362                         err = -EFAULT;
363 
364                 break;
365 
366         default:
367                 err = -ENOPROTOOPT;
368                 break;
369         }
370 
371         release_sock(sk);
372 
373         if (put_user(len, optlen))
374                 return -EFAULT;
375 
376         return err;
377 }
378 
379 void nfc_llcp_accept_unlink(struct sock *sk)
380 {
381         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
382 
383         pr_debug("state %d\n", sk->sk_state);
384 
385         list_del_init(&llcp_sock->accept_queue);
386         sk_acceptq_removed(llcp_sock->parent);
387         llcp_sock->parent = NULL;
388 
389         sock_put(sk);
390 }
391 
392 void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk)
393 {
394         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
395         struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent);
396 
397         /* Lock will be free from unlink */
398         sock_hold(sk);
399 
400         list_add_tail(&llcp_sock->accept_queue,
401                       &llcp_sock_parent->accept_queue);
402         llcp_sock->parent = parent;
403         sk_acceptq_added(parent);
404 }
405 
406 struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
407                                      struct socket *newsock)
408 {
409         struct nfc_llcp_sock *lsk, *n, *llcp_parent;
410         struct sock *sk;
411 
412         llcp_parent = nfc_llcp_sock(parent);
413 
414         list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue,
415                                  accept_queue) {
416                 sk = &lsk->sk;
417                 lock_sock(sk);
418 
419                 if (sk->sk_state == LLCP_CLOSED) {
420                         release_sock(sk);
421                         nfc_llcp_accept_unlink(sk);
422                         continue;
423                 }
424 
425                 if (sk->sk_state == LLCP_CONNECTED || !newsock) {
426                         list_del_init(&lsk->accept_queue);
427                         sock_put(sk);
428 
429                         if (newsock)
430                                 sock_graft(sk, newsock);
431 
432                         release_sock(sk);
433 
434                         pr_debug("Returning sk state %d\n", sk->sk_state);
435 
436                         sk_acceptq_removed(parent);
437 
438                         return sk;
439                 }
440 
441                 release_sock(sk);
442         }
443 
444         return NULL;
445 }
446 
447 static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
448                             int flags)
449 {
450         DECLARE_WAITQUEUE(wait, current);
451         struct sock *sk = sock->sk, *new_sk;
452         long timeo;
453         int ret = 0;
454 
455         pr_debug("parent %p\n", sk);
456 
457         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
458 
459         if (sk->sk_state != LLCP_LISTEN) {
460                 ret = -EBADFD;
461                 goto error;
462         }
463 
464         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
465 
466         /* Wait for an incoming connection. */
467         add_wait_queue_exclusive(sk_sleep(sk), &wait);
468         while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
469                 set_current_state(TASK_INTERRUPTIBLE);
470 
471                 if (!timeo) {
472                         ret = -EAGAIN;
473                         break;
474                 }
475 
476                 if (signal_pending(current)) {
477                         ret = sock_intr_errno(timeo);
478                         break;
479                 }
480 
481                 release_sock(sk);
482                 timeo = schedule_timeout(timeo);
483                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
484         }
485         __set_current_state(TASK_RUNNING);
486         remove_wait_queue(sk_sleep(sk), &wait);
487 
488         if (ret)
489                 goto error;
490 
491         newsock->state = SS_CONNECTED;
492 
493         pr_debug("new socket %p\n", new_sk);
494 
495 error:
496         release_sock(sk);
497 
498         return ret;
499 }
500 
501 static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
502                              int *len, int peer)
503 {
504         struct sock *sk = sock->sk;
505         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
506         DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr);
507 
508         if (llcp_sock == NULL || llcp_sock->dev == NULL)
509                 return -EBADFD;
510 
511         pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx,
512                  llcp_sock->dsap, llcp_sock->ssap);
513 
514         memset(llcp_addr, 0, sizeof(*llcp_addr));
515         *len = sizeof(struct sockaddr_nfc_llcp);
516 
517         llcp_addr->sa_family = AF_NFC;
518         llcp_addr->dev_idx = llcp_sock->dev->idx;
519         llcp_addr->target_idx = llcp_sock->target_idx;
520         llcp_addr->nfc_protocol = llcp_sock->nfc_protocol;
521         llcp_addr->dsap = llcp_sock->dsap;
522         llcp_addr->ssap = llcp_sock->ssap;
523         llcp_addr->service_name_len = llcp_sock->service_name_len;
524         memcpy(llcp_addr->service_name, llcp_sock->service_name,
525                llcp_addr->service_name_len);
526 
527         return 0;
528 }
529 
530 static inline unsigned int llcp_accept_poll(struct sock *parent)
531 {
532         struct nfc_llcp_sock *llcp_sock, *n, *parent_sock;
533         struct sock *sk;
534 
535         parent_sock = nfc_llcp_sock(parent);
536 
537         list_for_each_entry_safe(llcp_sock, n, &parent_sock->accept_queue,
538                                  accept_queue) {
539                 sk = &llcp_sock->sk;
540 
541                 if (sk->sk_state == LLCP_CONNECTED)
542                         return POLLIN | POLLRDNORM;
543         }
544 
545         return 0;
546 }
547 
548 static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
549                                    poll_table *wait)
550 {
551         struct sock *sk = sock->sk;
552         unsigned int mask = 0;
553 
554         pr_debug("%p\n", sk);
555 
556         sock_poll_wait(file, sk_sleep(sk), wait);
557 
558         if (sk->sk_state == LLCP_LISTEN)
559                 return llcp_accept_poll(sk);
560 
561         if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
562                 mask |= POLLERR |
563                         (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
564 
565         if (!skb_queue_empty(&sk->sk_receive_queue))
566                 mask |= POLLIN | POLLRDNORM;
567 
568         if (sk->sk_state == LLCP_CLOSED)
569                 mask |= POLLHUP;
570 
571         if (sk->sk_shutdown & RCV_SHUTDOWN)
572                 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
573 
574         if (sk->sk_shutdown == SHUTDOWN_MASK)
575                 mask |= POLLHUP;
576 
577         if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED)
578                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
579         else
580                 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
581 
582         pr_debug("mask 0x%x\n", mask);
583 
584         return mask;
585 }
586 
587 static int llcp_sock_release(struct socket *sock)
588 {
589         struct sock *sk = sock->sk;
590         struct nfc_llcp_local *local;
591         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
592         int err = 0;
593 
594         if (!sk)
595                 return 0;
596 
597         pr_debug("%p\n", sk);
598 
599         local = llcp_sock->local;
600         if (local == NULL) {
601                 err = -ENODEV;
602                 goto out;
603         }
604 
605         lock_sock(sk);
606 
607         /* Send a DISC */
608         if (sk->sk_state == LLCP_CONNECTED)
609                 nfc_llcp_send_disconnect(llcp_sock);
610 
611         if (sk->sk_state == LLCP_LISTEN) {
612                 struct nfc_llcp_sock *lsk, *n;
613                 struct sock *accept_sk;
614 
615                 list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
616                                          accept_queue) {
617                         accept_sk = &lsk->sk;
618                         lock_sock(accept_sk);
619 
620                         nfc_llcp_send_disconnect(lsk);
621                         nfc_llcp_accept_unlink(accept_sk);
622 
623                         release_sock(accept_sk);
624                 }
625         }
626 
627         if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
628                 nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
629 
630         release_sock(sk);
631 
632         /* Keep this sock alive and therefore do not remove it from the sockets
633          * list until the DISC PDU has been actually sent. Otherwise we would
634          * reply with DM PDUs before sending the DISC one.
635          */
636         if (sk->sk_state == LLCP_DISCONNECTING)
637                 return err;
638 
639         if (sock->type == SOCK_RAW)
640                 nfc_llcp_sock_unlink(&local->raw_sockets, sk);
641         else
642                 nfc_llcp_sock_unlink(&local->sockets, sk);
643 
644 out:
645         sock_orphan(sk);
646         sock_put(sk);
647 
648         return err;
649 }
650 
651 static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
652                              int len, int flags)
653 {
654         struct sock *sk = sock->sk;
655         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
656         struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
657         struct nfc_dev *dev;
658         struct nfc_llcp_local *local;
659         int ret = 0;
660 
661         pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
662 
663         if (!addr || len < sizeof(struct sockaddr_nfc) ||
664             addr->sa_family != AF_NFC)
665                 return -EINVAL;
666 
667         if (addr->service_name_len == 0 && addr->dsap == 0)
668                 return -EINVAL;
669 
670         pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
671                  addr->target_idx, addr->nfc_protocol);
672 
673         lock_sock(sk);
674 
675         if (sk->sk_state == LLCP_CONNECTED) {
676                 ret = -EISCONN;
677                 goto error;
678         }
679 
680         dev = nfc_get_device(addr->dev_idx);
681         if (dev == NULL) {
682                 ret = -ENODEV;
683                 goto error;
684         }
685 
686         local = nfc_llcp_find_local(dev);
687         if (local == NULL) {
688                 ret = -ENODEV;
689                 goto put_dev;
690         }
691 
692         device_lock(&dev->dev);
693         if (dev->dep_link_up == false) {
694                 ret = -ENOLINK;
695                 device_unlock(&dev->dev);
696                 goto put_dev;
697         }
698         device_unlock(&dev->dev);
699 
700         if (local->rf_mode == NFC_RF_INITIATOR &&
701             addr->target_idx != local->target_idx) {
702                 ret = -ENOLINK;
703                 goto put_dev;
704         }
705 
706         llcp_sock->dev = dev;
707         llcp_sock->local = nfc_llcp_local_get(local);
708         llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
709         if (llcp_sock->ssap == LLCP_SAP_MAX) {
710                 ret = -ENOMEM;
711                 goto put_dev;
712         }
713 
714         llcp_sock->reserved_ssap = llcp_sock->ssap;
715 
716         if (addr->service_name_len == 0)
717                 llcp_sock->dsap = addr->dsap;
718         else
719                 llcp_sock->dsap = LLCP_SAP_SDP;
720         llcp_sock->nfc_protocol = addr->nfc_protocol;
721         llcp_sock->service_name_len = min_t(unsigned int,
722                                             addr->service_name_len,
723                                             NFC_LLCP_MAX_SERVICE_NAME);
724         llcp_sock->service_name = kmemdup(addr->service_name,
725                                           llcp_sock->service_name_len,
726                                           GFP_KERNEL);
727 
728         nfc_llcp_sock_link(&local->connecting_sockets, sk);
729 
730         ret = nfc_llcp_send_connect(llcp_sock);
731         if (ret)
732                 goto sock_unlink;
733 
734         sk->sk_state = LLCP_CONNECTING;
735 
736         ret = sock_wait_state(sk, LLCP_CONNECTED,
737                               sock_sndtimeo(sk, flags & O_NONBLOCK));
738         if (ret && ret != -EINPROGRESS)
739                 goto sock_unlink;
740 
741         release_sock(sk);
742 
743         return ret;
744 
745 sock_unlink:
746         nfc_llcp_put_ssap(local, llcp_sock->ssap);
747 
748         nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
749 
750 put_dev:
751         nfc_put_device(dev);
752 
753 error:
754         release_sock(sk);
755         return ret;
756 }
757 
758 static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
759                              struct msghdr *msg, size_t len)
760 {
761         struct sock *sk = sock->sk;
762         struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
763         int ret;
764 
765         pr_debug("sock %p sk %p", sock, sk);
766 
767         ret = sock_error(sk);
768         if (ret)
769                 return ret;
770 
771         if (msg->msg_flags & MSG_OOB)
772                 return -EOPNOTSUPP;
773 
774         lock_sock(sk);
775 
776         if (sk->sk_type == SOCK_DGRAM) {
777                 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr,
778                                  msg->msg_name);
779 
780                 if (msg->msg_namelen < sizeof(*addr)) {
781                         release_sock(sk);
782                         return -EINVAL;
783                 }
784 
785                 release_sock(sk);
786 
787                 return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
788                                               msg, len);
789         }
790 
791         if (sk->sk_state != LLCP_CONNECTED) {
792                 release_sock(sk);
793                 return -ENOTCONN;
794         }
795 
796         release_sock(sk);
797 
798         return nfc_llcp_send_i_frame(llcp_sock, msg, len);
799 }
800 
801 static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
802                              struct msghdr *msg, size_t len, int flags)
803 {
804         int noblock = flags & MSG_DONTWAIT;
805         struct sock *sk = sock->sk;
806         unsigned int copied, rlen;
807         struct sk_buff *skb, *cskb;
808         int err = 0;
809 
810         pr_debug("%p %zu\n", sk, len);
811 
812         lock_sock(sk);
813 
814         if (sk->sk_state == LLCP_CLOSED &&
815             skb_queue_empty(&sk->sk_receive_queue)) {
816                 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",
828                        sk->sk_state, err, sock_error(sk));
829 
830                 if (sk->sk_shutdown & RCV_SHUTDOWN)
831                         return 0;
832 
833                 return err;
834         }
835 
836         rlen = skb->len;                /* real length of skb */
837         copied = min_t(unsigned int, rlen, len);
838 
839         cskb = skb;
840         if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
841                 if (!(flags & MSG_PEEK))
842                         skb_queue_head(&sk->sk_receive_queue, skb);
843                 return -EFAULT;
844         }
845 
846         sock_recv_timestamp(msg, sk, skb);
847 
848         if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
849                 struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
850                 DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, sockaddr,
851                                  msg->msg_name);
852 
853                 msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
854 
855                 pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
856 
857                 memset(sockaddr, 0, sizeof(*sockaddr));
858                 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;
862         }
863 
864         /* Mark read part of skb as used */
865         if (!(flags & MSG_PEEK)) {
866 
867                 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
868                 if (sk->sk_type == SOCK_STREAM ||
869                     sk->sk_type == SOCK_DGRAM ||
870                     sk->sk_type == SOCK_RAW) {
871                         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 
883 done:
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 
891 static 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,
904         .setsockopt     = nfc_llcp_setsockopt,
905         .getsockopt     = nfc_llcp_getsockopt,
906         .sendmsg        = llcp_sock_sendmsg,
907         .recvmsg        = llcp_sock_recvmsg,
908         .mmap           = sock_no_mmap,
909 };
910 
911 static 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 
931 static 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 
950 struct 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;
969         llcp_sock->rw = LLCP_MAX_RW + 1;
970         llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1);
971         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;
974         llcp_sock->reserved_ssap = LLCP_SAP_MAX;
975         nfc_llcp_socket_remote_param_init(llcp_sock);
976         skb_queue_head_init(&llcp_sock->tx_queue);
977         skb_queue_head_init(&llcp_sock->tx_pending_queue);
978         INIT_LIST_HEAD(&llcp_sock->accept_queue);
979 
980         if (sock != NULL)
981                 sock->state = SS_UNCONNECTED;
982 
983         return sk;
984 }
985 
986 void 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);
992 
993         list_del_init(&sock->accept_queue);
994 
995         sock->parent = NULL;
996 
997         nfc_llcp_local_put(sock->local);
998 }
999 
1000 static int llcp_sock_create(struct net *net, struct socket *sock,
1001                             const struct nfc_protocol *nfc_proto)
1002 {
1003         struct sock *sk;
1004 
1005         pr_debug("%p\n", sock);
1006 
1007         if (sock->type != SOCK_STREAM &&
1008             sock->type != SOCK_DGRAM &&
1009             sock->type != SOCK_RAW)
1010                 return -ESOCKTNOSUPPORT;
1011 
1012         if (sock->type == SOCK_RAW) {
1013                 if (!capable(CAP_NET_RAW))
1014                         return -EPERM;
1015                 sock->ops = &llcp_rawsock_ops;
1016         } else {
1017                 sock->ops = &llcp_sock_ops;
1018         }
1019 
1020         sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC);
1021         if (sk == NULL)
1022                 return -ENOMEM;
1023 
1024         return 0;
1025 }
1026 
1027 static const struct nfc_protocol llcp_nfc_proto = {
1028         .id       = NFC_SOCKPROTO_LLCP,
1029         .proto    = &llcp_sock_proto,
1030         .owner    = THIS_MODULE,
1031         .create   = llcp_sock_create
1032 };
1033 
1034 int __init nfc_llcp_sock_init(void)
1035 {
1036         return nfc_proto_register(&llcp_nfc_proto);
1037 }
1038 
1039 void nfc_llcp_sock_exit(void)
1040 {
1041         nfc_proto_unregister(&llcp_nfc_proto);
1042 }
1043 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp