rawsock.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  22. #include <net/tcp_states.h>
  23. #include <linux/nfc.h>
  24. #include <linux/export.h>
  25. #include "nfc.h"
  26. static struct nfc_sock_list raw_sk_list = {
  27. .lock = __RW_LOCK_UNLOCKED(raw_sk_list.lock)
  28. };
  29. static void nfc_sock_link(struct nfc_sock_list *l, struct sock *sk)
  30. {
  31. write_lock(&l->lock);
  32. sk_add_node(sk, &l->head);
  33. write_unlock(&l->lock);
  34. }
  35. static void nfc_sock_unlink(struct nfc_sock_list *l, struct sock *sk)
  36. {
  37. write_lock(&l->lock);
  38. sk_del_node_init(sk);
  39. write_unlock(&l->lock);
  40. }
  41. static void rawsock_write_queue_purge(struct sock *sk)
  42. {
  43. pr_debug("sk=%p\n", sk);
  44. spin_lock_bh(&sk->sk_write_queue.lock);
  45. __skb_queue_purge(&sk->sk_write_queue);
  46. nfc_rawsock(sk)->tx_work_scheduled = false;
  47. spin_unlock_bh(&sk->sk_write_queue.lock);
  48. }
  49. static void rawsock_report_error(struct sock *sk, int err)
  50. {
  51. pr_debug("sk=%p err=%d\n", sk, err);
  52. sk->sk_shutdown = SHUTDOWN_MASK;
  53. sk->sk_err = -err;
  54. sk->sk_error_report(sk);
  55. rawsock_write_queue_purge(sk);
  56. }
  57. static int rawsock_release(struct socket *sock)
  58. {
  59. struct sock *sk = sock->sk;
  60. pr_debug("sock=%p sk=%p\n", sock, sk);
  61. if (!sk)
  62. return 0;
  63. if (sock->type == SOCK_RAW)
  64. nfc_sock_unlink(&raw_sk_list, sk);
  65. sock_orphan(sk);
  66. sock_put(sk);
  67. return 0;
  68. }
  69. static int rawsock_connect(struct socket *sock, struct sockaddr *_addr,
  70. int len, int flags)
  71. {
  72. struct sock *sk = sock->sk;
  73. struct sockaddr_nfc *addr = (struct sockaddr_nfc *)_addr;
  74. struct nfc_dev *dev;
  75. int rc = 0;
  76. pr_debug("sock=%p sk=%p flags=%d\n", sock, sk, flags);
  77. if (!addr || len < sizeof(struct sockaddr_nfc) ||
  78. addr->sa_family != AF_NFC)
  79. return -EINVAL;
  80. pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n",
  81. addr->dev_idx, addr->target_idx, addr->nfc_protocol);
  82. lock_sock(sk);
  83. if (sock->state == SS_CONNECTED) {
  84. rc = -EISCONN;
  85. goto error;
  86. }
  87. dev = nfc_get_device(addr->dev_idx);
  88. if (!dev) {
  89. rc = -ENODEV;
  90. goto error;
  91. }
  92. if (addr->target_idx > dev->target_next_idx - 1 ||
  93. addr->target_idx < dev->target_next_idx - dev->n_targets) {
  94. rc = -EINVAL;
  95. goto error;
  96. }
  97. rc = nfc_activate_target(dev, addr->target_idx, addr->nfc_protocol);
  98. if (rc)
  99. goto put_dev;
  100. nfc_rawsock(sk)->dev = dev;
  101. nfc_rawsock(sk)->target_idx = addr->target_idx;
  102. sock->state = SS_CONNECTED;
  103. sk->sk_state = TCP_ESTABLISHED;
  104. sk->sk_state_change(sk);
  105. release_sock(sk);
  106. return 0;
  107. put_dev:
  108. nfc_put_device(dev);
  109. error:
  110. release_sock(sk);
  111. return rc;
  112. }
  113. static int rawsock_add_header(struct sk_buff *skb)
  114. {
  115. *skb_push(skb, NFC_HEADER_SIZE) = 0;
  116. return 0;
  117. }
  118. static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
  119. int err)
  120. {
  121. struct sock *sk = (struct sock *) context;
  122. BUG_ON(in_irq());
  123. pr_debug("sk=%p err=%d\n", sk, err);
  124. if (err)
  125. goto error;
  126. err = rawsock_add_header(skb);
  127. if (err)
  128. goto error_skb;
  129. err = sock_queue_rcv_skb(sk, skb);
  130. if (err)
  131. goto error_skb;
  132. spin_lock_bh(&sk->sk_write_queue.lock);
  133. if (!skb_queue_empty(&sk->sk_write_queue))
  134. schedule_work(&nfc_rawsock(sk)->tx_work);
  135. else
  136. nfc_rawsock(sk)->tx_work_scheduled = false;
  137. spin_unlock_bh(&sk->sk_write_queue.lock);
  138. sock_put(sk);
  139. return;
  140. error_skb:
  141. kfree_skb(skb);
  142. error:
  143. rawsock_report_error(sk, err);
  144. sock_put(sk);
  145. }
  146. static void rawsock_tx_work(struct work_struct *work)
  147. {
  148. struct sock *sk = to_rawsock_sk(work);
  149. struct nfc_dev *dev = nfc_rawsock(sk)->dev;
  150. u32 target_idx = nfc_rawsock(sk)->target_idx;
  151. struct sk_buff *skb;
  152. int rc;
  153. pr_debug("sk=%p target_idx=%u\n", sk, target_idx);
  154. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  155. rawsock_write_queue_purge(sk);
  156. return;
  157. }
  158. skb = skb_dequeue(&sk->sk_write_queue);
  159. sock_hold(sk);
  160. rc = nfc_data_exchange(dev, target_idx, skb,
  161. rawsock_data_exchange_complete, sk);
  162. if (rc) {
  163. rawsock_report_error(sk, rc);
  164. sock_put(sk);
  165. }
  166. }
  167. static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  168. {
  169. struct sock *sk = sock->sk;
  170. struct nfc_dev *dev = nfc_rawsock(sk)->dev;
  171. struct sk_buff *skb;
  172. int rc;
  173. pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len);
  174. if (msg->msg_namelen)
  175. return -EOPNOTSUPP;
  176. if (sock->state != SS_CONNECTED)
  177. return -ENOTCONN;
  178. skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
  179. if (skb == NULL)
  180. return rc;
  181. rc = memcpy_from_msg(skb_put(skb, len), msg, len);
  182. if (rc < 0) {
  183. kfree_skb(skb);
  184. return rc;
  185. }
  186. spin_lock_bh(&sk->sk_write_queue.lock);
  187. __skb_queue_tail(&sk->sk_write_queue, skb);
  188. if (!nfc_rawsock(sk)->tx_work_scheduled) {
  189. schedule_work(&nfc_rawsock(sk)->tx_work);
  190. nfc_rawsock(sk)->tx_work_scheduled = true;
  191. }
  192. spin_unlock_bh(&sk->sk_write_queue.lock);
  193. return len;
  194. }
  195. static int rawsock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  196. int flags)
  197. {
  198. int noblock = flags & MSG_DONTWAIT;
  199. struct sock *sk = sock->sk;
  200. struct sk_buff *skb;
  201. int copied;
  202. int rc;
  203. pr_debug("sock=%p sk=%p len=%zu flags=%d\n", sock, sk, len, flags);
  204. skb = skb_recv_datagram(sk, flags, noblock, &rc);
  205. if (!skb)
  206. return rc;
  207. copied = skb->len;
  208. if (len < copied) {
  209. msg->msg_flags |= MSG_TRUNC;
  210. copied = len;
  211. }
  212. rc = skb_copy_datagram_msg(skb, 0, msg, copied);
  213. skb_free_datagram(sk, skb);
  214. return rc ? : copied;
  215. }
  216. static const struct proto_ops rawsock_ops = {
  217. .family = PF_NFC,
  218. .owner = THIS_MODULE,
  219. .release = rawsock_release,
  220. .bind = sock_no_bind,
  221. .connect = rawsock_connect,
  222. .socketpair = sock_no_socketpair,
  223. .accept = sock_no_accept,
  224. .getname = sock_no_getname,
  225. .poll = datagram_poll,
  226. .ioctl = sock_no_ioctl,
  227. .listen = sock_no_listen,
  228. .shutdown = sock_no_shutdown,
  229. .setsockopt = sock_no_setsockopt,
  230. .getsockopt = sock_no_getsockopt,
  231. .sendmsg = rawsock_sendmsg,
  232. .recvmsg = rawsock_recvmsg,
  233. .mmap = sock_no_mmap,
  234. };
  235. static const struct proto_ops rawsock_raw_ops = {
  236. .family = PF_NFC,
  237. .owner = THIS_MODULE,
  238. .release = rawsock_release,
  239. .bind = sock_no_bind,
  240. .connect = sock_no_connect,
  241. .socketpair = sock_no_socketpair,
  242. .accept = sock_no_accept,
  243. .getname = sock_no_getname,
  244. .poll = datagram_poll,
  245. .ioctl = sock_no_ioctl,
  246. .listen = sock_no_listen,
  247. .shutdown = sock_no_shutdown,
  248. .setsockopt = sock_no_setsockopt,
  249. .getsockopt = sock_no_getsockopt,
  250. .sendmsg = sock_no_sendmsg,
  251. .recvmsg = rawsock_recvmsg,
  252. .mmap = sock_no_mmap,
  253. };
  254. static void rawsock_destruct(struct sock *sk)
  255. {
  256. pr_debug("sk=%p\n", sk);
  257. if (sk->sk_state == TCP_ESTABLISHED) {
  258. nfc_deactivate_target(nfc_rawsock(sk)->dev,
  259. nfc_rawsock(sk)->target_idx,
  260. NFC_TARGET_MODE_IDLE);
  261. nfc_put_device(nfc_rawsock(sk)->dev);
  262. }
  263. skb_queue_purge(&sk->sk_receive_queue);
  264. if (!sock_flag(sk, SOCK_DEAD)) {
  265. pr_err("Freeing alive NFC raw socket %p\n", sk);
  266. return;
  267. }
  268. }
  269. static int rawsock_create(struct net *net, struct socket *sock,
  270. const struct nfc_protocol *nfc_proto, int kern)
  271. {
  272. struct sock *sk;
  273. pr_debug("sock=%p\n", sock);
  274. if ((sock->type != SOCK_SEQPACKET) && (sock->type != SOCK_RAW))
  275. return -ESOCKTNOSUPPORT;
  276. if (sock->type == SOCK_RAW)
  277. sock->ops = &rawsock_raw_ops;
  278. else
  279. sock->ops = &rawsock_ops;
  280. sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto, kern);
  281. if (!sk)
  282. return -ENOMEM;
  283. sock_init_data(sock, sk);
  284. sk->sk_protocol = nfc_proto->id;
  285. sk->sk_destruct = rawsock_destruct;
  286. sock->state = SS_UNCONNECTED;
  287. if (sock->type == SOCK_RAW)
  288. nfc_sock_link(&raw_sk_list, sk);
  289. else {
  290. INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
  291. nfc_rawsock(sk)->tx_work_scheduled = false;
  292. }
  293. return 0;
  294. }
  295. void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
  296. u8 payload_type, u8 direction)
  297. {
  298. struct sk_buff *skb_copy = NULL, *nskb;
  299. struct sock *sk;
  300. u8 *data;
  301. read_lock(&raw_sk_list.lock);
  302. sk_for_each(sk, &raw_sk_list.head) {
  303. if (!skb_copy) {
  304. skb_copy = __pskb_copy_fclone(skb, NFC_RAW_HEADER_SIZE,
  305. GFP_ATOMIC, true);
  306. if (!skb_copy)
  307. continue;
  308. data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
  309. data[0] = dev ? dev->idx : 0xFF;
  310. data[1] = direction & 0x01;
  311. data[1] |= (payload_type << 1);
  312. }
  313. nskb = skb_clone(skb_copy, GFP_ATOMIC);
  314. if (!nskb)
  315. continue;
  316. if (sock_queue_rcv_skb(sk, nskb))
  317. kfree_skb(nskb);
  318. }
  319. read_unlock(&raw_sk_list.lock);
  320. kfree_skb(skb_copy);
  321. }
  322. EXPORT_SYMBOL(nfc_send_to_raw_sock);
  323. static struct proto rawsock_proto = {
  324. .name = "NFC_RAW",
  325. .owner = THIS_MODULE,
  326. .obj_size = sizeof(struct nfc_rawsock),
  327. };
  328. static const struct nfc_protocol rawsock_nfc_proto = {
  329. .id = NFC_SOCKPROTO_RAW,
  330. .proto = &rawsock_proto,
  331. .owner = THIS_MODULE,
  332. .create = rawsock_create
  333. };
  334. int __init rawsock_init(void)
  335. {
  336. int rc;
  337. rc = nfc_proto_register(&rawsock_nfc_proto);
  338. return rc;
  339. }
  340. void rawsock_exit(void)
  341. {
  342. nfc_proto_unregister(&rawsock_nfc_proto);
  343. }