rawsock.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  6. * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  24. #include <net/tcp_states.h>
  25. #include <linux/nfc.h>
  26. #include <linux/export.h>
  27. #include "nfc.h"
  28. static void rawsock_write_queue_purge(struct sock *sk)
  29. {
  30. pr_debug("sk=%p\n", sk);
  31. spin_lock_bh(&sk->sk_write_queue.lock);
  32. __skb_queue_purge(&sk->sk_write_queue);
  33. nfc_rawsock(sk)->tx_work_scheduled = false;
  34. spin_unlock_bh(&sk->sk_write_queue.lock);
  35. }
  36. static void rawsock_report_error(struct sock *sk, int err)
  37. {
  38. pr_debug("sk=%p err=%d\n", sk, err);
  39. sk->sk_shutdown = SHUTDOWN_MASK;
  40. sk->sk_err = -err;
  41. sk->sk_error_report(sk);
  42. rawsock_write_queue_purge(sk);
  43. }
  44. static int rawsock_release(struct socket *sock)
  45. {
  46. struct sock *sk = sock->sk;
  47. pr_debug("sock=%p sk=%p\n", sock, sk);
  48. if (!sk)
  49. return 0;
  50. sock_orphan(sk);
  51. sock_put(sk);
  52. return 0;
  53. }
  54. static int rawsock_connect(struct socket *sock, struct sockaddr *_addr,
  55. int len, int flags)
  56. {
  57. struct sock *sk = sock->sk;
  58. struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr;
  59. struct nfc_dev *dev;
  60. int rc = 0;
  61. pr_debug("sock=%p sk=%p flags=%d\n", sock, sk, flags);
  62. if (!addr || len < sizeof(struct sockaddr_nfc) ||
  63. addr->sa_family != AF_NFC)
  64. return -EINVAL;
  65. pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n",
  66. addr->dev_idx, addr->target_idx, addr->nfc_protocol);
  67. lock_sock(sk);
  68. if (sock->state == SS_CONNECTED) {
  69. rc = -EISCONN;
  70. goto error;
  71. }
  72. dev = nfc_get_device(addr->dev_idx);
  73. if (!dev) {
  74. rc = -ENODEV;
  75. goto error;
  76. }
  77. rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol);
  78. if (rc)
  79. goto put_dev;
  80. nfc_rawsock(sk)->dev = dev;
  81. nfc_rawsock(sk)->target_idx = addr->target_idx;
  82. sock->state = SS_CONNECTED;
  83. sk->sk_state = TCP_ESTABLISHED;
  84. sk->sk_state_change(sk);
  85. release_sock(sk);
  86. return 0;
  87. put_dev:
  88. nfc_put_device(dev);
  89. error:
  90. release_sock(sk);
  91. return rc;
  92. }
  93. static int rawsock_add_header(struct sk_buff *skb)
  94. {
  95. *skb_push(skb, NFC_HEADER_SIZE) = 0;
  96. return 0;
  97. }
  98. static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
  99. int err)
  100. {
  101. struct sock *sk = (struct sock *) context;
  102. BUG_ON(in_irq());
  103. pr_debug("sk=%p err=%d\n", sk, err);
  104. if (err)
  105. goto error;
  106. err = rawsock_add_header(skb);
  107. if (err)
  108. goto error;
  109. err = sock_queue_rcv_skb(sk, skb);
  110. if (err)
  111. goto error;
  112. spin_lock_bh(&sk->sk_write_queue.lock);
  113. if (!skb_queue_empty(&sk->sk_write_queue))
  114. schedule_work(&nfc_rawsock(sk)->tx_work);
  115. else
  116. nfc_rawsock(sk)->tx_work_scheduled = false;
  117. spin_unlock_bh(&sk->sk_write_queue.lock);
  118. sock_put(sk);
  119. return;
  120. error:
  121. rawsock_report_error(sk, err);
  122. sock_put(sk);
  123. }
  124. static void rawsock_tx_work(struct work_struct *work)
  125. {
  126. struct sock *sk = to_rawsock_sk(work);
  127. struct nfc_dev *dev = nfc_rawsock(sk)->dev;
  128. u32 target_idx = nfc_rawsock(sk)->target_idx;
  129. struct sk_buff *skb;
  130. int rc;
  131. pr_debug("sk=%p target_idx=%u\n", sk, target_idx);
  132. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  133. rawsock_write_queue_purge(sk);
  134. return;
  135. }
  136. skb = skb_dequeue(&sk->sk_write_queue);
  137. sock_hold(sk);
  138. rc = nfc_data_exchange(dev, target_idx, skb,
  139. rawsock_data_exchange_complete, sk);
  140. if (rc) {
  141. rawsock_report_error(sk, rc);
  142. sock_put(sk);
  143. }
  144. }
  145. static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
  146. struct msghdr *msg, size_t len)
  147. {
  148. struct sock *sk = sock->sk;
  149. struct nfc_dev *dev = nfc_rawsock(sk)->dev;
  150. struct sk_buff *skb;
  151. int rc;
  152. pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len);
  153. if (msg->msg_namelen)
  154. return -EOPNOTSUPP;
  155. if (sock->state != SS_CONNECTED)
  156. return -ENOTCONN;
  157. skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
  158. if (skb == NULL)
  159. return rc;
  160. rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  161. if (rc < 0) {
  162. kfree_skb(skb);
  163. return rc;
  164. }
  165. spin_lock_bh(&sk->sk_write_queue.lock);
  166. __skb_queue_tail(&sk->sk_write_queue, skb);
  167. if (!nfc_rawsock(sk)->tx_work_scheduled) {
  168. schedule_work(&nfc_rawsock(sk)->tx_work);
  169. nfc_rawsock(sk)->tx_work_scheduled = true;
  170. }
  171. spin_unlock_bh(&sk->sk_write_queue.lock);
  172. return len;
  173. }
  174. static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock,
  175. struct msghdr *msg, size_t len, int flags)
  176. {
  177. int noblock = flags & MSG_DONTWAIT;
  178. struct sock *sk = sock->sk;
  179. struct sk_buff *skb;
  180. int copied;
  181. int rc;
  182. pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock, sk, len, flags);
  183. skb = skb_recv_datagram(sk, flags, noblock, &rc);
  184. if (!skb)
  185. return rc;
  186. copied = skb->len;
  187. if (len < copied) {
  188. msg->msg_flags |= MSG_TRUNC;
  189. copied = len;
  190. }
  191. rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  192. skb_free_datagram(sk, skb);
  193. return rc ? : copied;
  194. }
  195. static const struct proto_ops rawsock_ops = {
  196. .family = PF_NFC,
  197. .owner = THIS_MODULE,
  198. .release = rawsock_release,
  199. .bind = sock_no_bind,
  200. .connect = rawsock_connect,
  201. .socketpair = sock_no_socketpair,
  202. .accept = sock_no_accept,
  203. .getname = sock_no_getname,
  204. .poll = datagram_poll,
  205. .ioctl = sock_no_ioctl,
  206. .listen = sock_no_listen,
  207. .shutdown = sock_no_shutdown,
  208. .setsockopt = sock_no_setsockopt,
  209. .getsockopt = sock_no_getsockopt,
  210. .sendmsg = rawsock_sendmsg,
  211. .recvmsg = rawsock_recvmsg,
  212. .mmap = sock_no_mmap,
  213. };
  214. static void rawsock_destruct(struct sock *sk)
  215. {
  216. pr_debug("sk=%p\n", sk);
  217. if (sk->sk_state == TCP_ESTABLISHED) {
  218. nfc_deactivate_target(nfc_rawsock(sk)->dev,
  219. nfc_rawsock(sk)->target_idx);
  220. nfc_put_device(nfc_rawsock(sk)->dev);
  221. }
  222. skb_queue_purge(&sk->sk_receive_queue);
  223. if (!sock_flag(sk, SOCK_DEAD)) {
  224. pr_err("Freeing alive NFC raw socket %p\n", sk);
  225. return;
  226. }
  227. }
  228. static int rawsock_create(struct net *net, struct socket *sock,
  229. const struct nfc_protocol *nfc_proto)
  230. {
  231. struct sock *sk;
  232. pr_debug("sock=%p\n", sock);
  233. if (sock->type != SOCK_SEQPACKET)
  234. return -ESOCKTNOSUPPORT;
  235. sock->ops = &rawsock_ops;
  236. sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto);
  237. if (!sk)
  238. return -ENOMEM;
  239. sock_init_data(sock, sk);
  240. sk->sk_protocol = nfc_proto->id;
  241. sk->sk_destruct = rawsock_destruct;
  242. sock->state = SS_UNCONNECTED;
  243. INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
  244. nfc_rawsock(sk)->tx_work_scheduled = false;
  245. return 0;
  246. }
  247. static struct proto rawsock_proto = {
  248. .name = "NFC_RAW",
  249. .owner = THIS_MODULE,
  250. .obj_size = sizeof(struct nfc_rawsock),
  251. };
  252. static const struct nfc_protocol rawsock_nfc_proto = {
  253. .id = NFC_SOCKPROTO_RAW,
  254. .proto = &rawsock_proto,
  255. .owner = THIS_MODULE,
  256. .create = rawsock_create
  257. };
  258. int __init rawsock_init(void)
  259. {
  260. int rc;
  261. rc = nfc_proto_register(&rawsock_nfc_proto);
  262. return rc;
  263. }
  264. void rawsock_exit(void)
  265. {
  266. nfc_proto_unregister(&rawsock_nfc_proto);
  267. }