llcp_sock.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/nfc.h>
  22. #include "nfc.h"
  23. #include "llcp.h"
  24. static int sock_wait_state(struct sock *sk, int state, unsigned long timeo)
  25. {
  26. DECLARE_WAITQUEUE(wait, current);
  27. int err = 0;
  28. pr_debug("sk %p", sk);
  29. add_wait_queue(sk_sleep(sk), &wait);
  30. set_current_state(TASK_INTERRUPTIBLE);
  31. while (sk->sk_state != state) {
  32. if (!timeo) {
  33. err = -EINPROGRESS;
  34. break;
  35. }
  36. if (signal_pending(current)) {
  37. err = sock_intr_errno(timeo);
  38. break;
  39. }
  40. release_sock(sk);
  41. timeo = schedule_timeout(timeo);
  42. lock_sock(sk);
  43. set_current_state(TASK_INTERRUPTIBLE);
  44. err = sock_error(sk);
  45. if (err)
  46. break;
  47. }
  48. __set_current_state(TASK_RUNNING);
  49. remove_wait_queue(sk_sleep(sk), &wait);
  50. return err;
  51. }
  52. static struct proto llcp_sock_proto = {
  53. .name = "NFC_LLCP",
  54. .owner = THIS_MODULE,
  55. .obj_size = sizeof(struct nfc_llcp_sock),
  56. };
  57. static int llcp_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
  58. {
  59. struct sock *sk = sock->sk;
  60. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  61. struct nfc_llcp_local *local;
  62. struct nfc_dev *dev;
  63. struct sockaddr_nfc_llcp llcp_addr;
  64. int len, ret = 0;
  65. if (!addr || alen < offsetofend(struct sockaddr, sa_family) ||
  66. addr->sa_family != AF_NFC)
  67. return -EINVAL;
  68. pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
  69. memset(&llcp_addr, 0, sizeof(llcp_addr));
  70. len = min_t(unsigned int, sizeof(llcp_addr), alen);
  71. memcpy(&llcp_addr, addr, len);
  72. /* This is going to be a listening socket, dsap must be 0 */
  73. if (llcp_addr.dsap != 0)
  74. return -EINVAL;
  75. lock_sock(sk);
  76. if (sk->sk_state != LLCP_CLOSED) {
  77. ret = -EBADFD;
  78. goto error;
  79. }
  80. dev = nfc_get_device(llcp_addr.dev_idx);
  81. if (dev == NULL) {
  82. ret = -ENODEV;
  83. goto error;
  84. }
  85. local = nfc_llcp_find_local(dev);
  86. if (local == NULL) {
  87. ret = -ENODEV;
  88. goto put_dev;
  89. }
  90. llcp_sock->dev = dev;
  91. llcp_sock->local = nfc_llcp_local_get(local);
  92. llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
  93. llcp_sock->service_name_len = min_t(unsigned int,
  94. llcp_addr.service_name_len,
  95. NFC_LLCP_MAX_SERVICE_NAME);
  96. llcp_sock->service_name = kmemdup(llcp_addr.service_name,
  97. llcp_sock->service_name_len,
  98. GFP_KERNEL);
  99. llcp_sock->ssap = nfc_llcp_get_sdp_ssap(local, llcp_sock);
  100. if (llcp_sock->ssap == LLCP_SAP_MAX) {
  101. ret = -EADDRINUSE;
  102. goto put_dev;
  103. }
  104. llcp_sock->reserved_ssap = llcp_sock->ssap;
  105. nfc_llcp_sock_link(&local->sockets, sk);
  106. pr_debug("Socket bound to SAP %d\n", llcp_sock->ssap);
  107. sk->sk_state = LLCP_BOUND;
  108. put_dev:
  109. nfc_put_device(dev);
  110. error:
  111. release_sock(sk);
  112. return ret;
  113. }
  114. static int llcp_raw_sock_bind(struct socket *sock, struct sockaddr *addr,
  115. int alen)
  116. {
  117. struct sock *sk = sock->sk;
  118. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  119. struct nfc_llcp_local *local;
  120. struct nfc_dev *dev;
  121. struct sockaddr_nfc_llcp llcp_addr;
  122. int len, ret = 0;
  123. if (!addr || alen < offsetofend(struct sockaddr, sa_family) ||
  124. addr->sa_family != AF_NFC)
  125. return -EINVAL;
  126. pr_debug("sk %p addr %p family %d\n", sk, addr, addr->sa_family);
  127. memset(&llcp_addr, 0, sizeof(llcp_addr));
  128. len = min_t(unsigned int, sizeof(llcp_addr), alen);
  129. memcpy(&llcp_addr, addr, len);
  130. lock_sock(sk);
  131. if (sk->sk_state != LLCP_CLOSED) {
  132. ret = -EBADFD;
  133. goto error;
  134. }
  135. dev = nfc_get_device(llcp_addr.dev_idx);
  136. if (dev == NULL) {
  137. ret = -ENODEV;
  138. goto error;
  139. }
  140. local = nfc_llcp_find_local(dev);
  141. if (local == NULL) {
  142. ret = -ENODEV;
  143. goto put_dev;
  144. }
  145. llcp_sock->dev = dev;
  146. llcp_sock->local = nfc_llcp_local_get(local);
  147. llcp_sock->nfc_protocol = llcp_addr.nfc_protocol;
  148. nfc_llcp_sock_link(&local->raw_sockets, sk);
  149. sk->sk_state = LLCP_BOUND;
  150. put_dev:
  151. nfc_put_device(dev);
  152. error:
  153. release_sock(sk);
  154. return ret;
  155. }
  156. static int llcp_sock_listen(struct socket *sock, int backlog)
  157. {
  158. struct sock *sk = sock->sk;
  159. int ret = 0;
  160. pr_debug("sk %p backlog %d\n", sk, backlog);
  161. lock_sock(sk);
  162. if ((sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) ||
  163. sk->sk_state != LLCP_BOUND) {
  164. ret = -EBADFD;
  165. goto error;
  166. }
  167. sk->sk_max_ack_backlog = backlog;
  168. sk->sk_ack_backlog = 0;
  169. pr_debug("Socket listening\n");
  170. sk->sk_state = LLCP_LISTEN;
  171. error:
  172. release_sock(sk);
  173. return ret;
  174. }
  175. static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname,
  176. char __user *optval, unsigned int optlen)
  177. {
  178. struct sock *sk = sock->sk;
  179. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  180. u32 opt;
  181. int err = 0;
  182. pr_debug("%p optname %d\n", sk, optname);
  183. if (level != SOL_NFC)
  184. return -ENOPROTOOPT;
  185. lock_sock(sk);
  186. switch (optname) {
  187. case NFC_LLCP_RW:
  188. if (sk->sk_state == LLCP_CONNECTED ||
  189. sk->sk_state == LLCP_BOUND ||
  190. sk->sk_state == LLCP_LISTEN) {
  191. err = -EINVAL;
  192. break;
  193. }
  194. if (get_user(opt, (u32 __user *) optval)) {
  195. err = -EFAULT;
  196. break;
  197. }
  198. if (opt > LLCP_MAX_RW) {
  199. err = -EINVAL;
  200. break;
  201. }
  202. llcp_sock->rw = (u8) opt;
  203. break;
  204. case NFC_LLCP_MIUX:
  205. if (sk->sk_state == LLCP_CONNECTED ||
  206. sk->sk_state == LLCP_BOUND ||
  207. sk->sk_state == LLCP_LISTEN) {
  208. err = -EINVAL;
  209. break;
  210. }
  211. if (get_user(opt, (u32 __user *) optval)) {
  212. err = -EFAULT;
  213. break;
  214. }
  215. if (opt > LLCP_MAX_MIUX) {
  216. err = -EINVAL;
  217. break;
  218. }
  219. llcp_sock->miux = cpu_to_be16((u16) opt);
  220. break;
  221. default:
  222. err = -ENOPROTOOPT;
  223. break;
  224. }
  225. release_sock(sk);
  226. pr_debug("%p rw %d miux %d\n", llcp_sock,
  227. llcp_sock->rw, llcp_sock->miux);
  228. return err;
  229. }
  230. static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
  231. char __user *optval, int __user *optlen)
  232. {
  233. struct nfc_llcp_local *local;
  234. struct sock *sk = sock->sk;
  235. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  236. int len, err = 0;
  237. u16 miux, remote_miu;
  238. u8 rw;
  239. pr_debug("%p optname %d\n", sk, optname);
  240. if (level != SOL_NFC)
  241. return -ENOPROTOOPT;
  242. if (get_user(len, optlen))
  243. return -EFAULT;
  244. local = llcp_sock->local;
  245. if (!local)
  246. return -ENODEV;
  247. len = min_t(u32, len, sizeof(u32));
  248. lock_sock(sk);
  249. switch (optname) {
  250. case NFC_LLCP_RW:
  251. rw = llcp_sock->rw > LLCP_MAX_RW ? local->rw : llcp_sock->rw;
  252. if (put_user(rw, (u32 __user *) optval))
  253. err = -EFAULT;
  254. break;
  255. case NFC_LLCP_MIUX:
  256. miux = be16_to_cpu(llcp_sock->miux) > LLCP_MAX_MIUX ?
  257. be16_to_cpu(local->miux) : be16_to_cpu(llcp_sock->miux);
  258. if (put_user(miux, (u32 __user *) optval))
  259. err = -EFAULT;
  260. break;
  261. case NFC_LLCP_REMOTE_MIU:
  262. remote_miu = llcp_sock->remote_miu > LLCP_MAX_MIU ?
  263. local->remote_miu : llcp_sock->remote_miu;
  264. if (put_user(remote_miu, (u32 __user *) optval))
  265. err = -EFAULT;
  266. break;
  267. case NFC_LLCP_REMOTE_LTO:
  268. if (put_user(local->remote_lto / 10, (u32 __user *) optval))
  269. err = -EFAULT;
  270. break;
  271. case NFC_LLCP_REMOTE_RW:
  272. if (put_user(llcp_sock->remote_rw, (u32 __user *) optval))
  273. err = -EFAULT;
  274. break;
  275. default:
  276. err = -ENOPROTOOPT;
  277. break;
  278. }
  279. release_sock(sk);
  280. if (put_user(len, optlen))
  281. return -EFAULT;
  282. return err;
  283. }
  284. void nfc_llcp_accept_unlink(struct sock *sk)
  285. {
  286. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  287. pr_debug("state %d\n", sk->sk_state);
  288. list_del_init(&llcp_sock->accept_queue);
  289. sk_acceptq_removed(llcp_sock->parent);
  290. llcp_sock->parent = NULL;
  291. sock_put(sk);
  292. }
  293. void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk)
  294. {
  295. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  296. struct nfc_llcp_sock *llcp_sock_parent = nfc_llcp_sock(parent);
  297. /* Lock will be free from unlink */
  298. sock_hold(sk);
  299. list_add_tail(&llcp_sock->accept_queue,
  300. &llcp_sock_parent->accept_queue);
  301. llcp_sock->parent = parent;
  302. sk_acceptq_added(parent);
  303. }
  304. struct sock *nfc_llcp_accept_dequeue(struct sock *parent,
  305. struct socket *newsock)
  306. {
  307. struct nfc_llcp_sock *lsk, *n, *llcp_parent;
  308. struct sock *sk;
  309. llcp_parent = nfc_llcp_sock(parent);
  310. list_for_each_entry_safe(lsk, n, &llcp_parent->accept_queue,
  311. accept_queue) {
  312. sk = &lsk->sk;
  313. lock_sock(sk);
  314. if (sk->sk_state == LLCP_CLOSED) {
  315. release_sock(sk);
  316. nfc_llcp_accept_unlink(sk);
  317. continue;
  318. }
  319. if (sk->sk_state == LLCP_CONNECTED || !newsock) {
  320. list_del_init(&lsk->accept_queue);
  321. sock_put(sk);
  322. if (newsock)
  323. sock_graft(sk, newsock);
  324. release_sock(sk);
  325. pr_debug("Returning sk state %d\n", sk->sk_state);
  326. sk_acceptq_removed(parent);
  327. return sk;
  328. }
  329. release_sock(sk);
  330. }
  331. return NULL;
  332. }
  333. static int llcp_sock_accept(struct socket *sock, struct socket *newsock,
  334. int flags)
  335. {
  336. DECLARE_WAITQUEUE(wait, current);
  337. struct sock *sk = sock->sk, *new_sk;
  338. long timeo;
  339. int ret = 0;
  340. pr_debug("parent %p\n", sk);
  341. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  342. if (sk->sk_state != LLCP_LISTEN) {
  343. ret = -EBADFD;
  344. goto error;
  345. }
  346. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  347. /* Wait for an incoming connection. */
  348. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  349. while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
  350. set_current_state(TASK_INTERRUPTIBLE);
  351. if (!timeo) {
  352. ret = -EAGAIN;
  353. break;
  354. }
  355. if (signal_pending(current)) {
  356. ret = sock_intr_errno(timeo);
  357. break;
  358. }
  359. release_sock(sk);
  360. timeo = schedule_timeout(timeo);
  361. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  362. }
  363. __set_current_state(TASK_RUNNING);
  364. remove_wait_queue(sk_sleep(sk), &wait);
  365. if (ret)
  366. goto error;
  367. newsock->state = SS_CONNECTED;
  368. pr_debug("new socket %p\n", new_sk);
  369. error:
  370. release_sock(sk);
  371. return ret;
  372. }
  373. static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
  374. int *len, int peer)
  375. {
  376. struct sock *sk = sock->sk;
  377. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  378. DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, llcp_addr, uaddr);
  379. if (llcp_sock == NULL || llcp_sock->dev == NULL)
  380. return -EBADFD;
  381. pr_debug("%p %d %d %d\n", sk, llcp_sock->target_idx,
  382. llcp_sock->dsap, llcp_sock->ssap);
  383. memset(llcp_addr, 0, sizeof(*llcp_addr));
  384. *len = sizeof(struct sockaddr_nfc_llcp);
  385. lock_sock(sk);
  386. if (!llcp_sock->dev) {
  387. release_sock(sk);
  388. return -EBADFD;
  389. }
  390. llcp_addr->sa_family = AF_NFC;
  391. llcp_addr->dev_idx = llcp_sock->dev->idx;
  392. llcp_addr->target_idx = llcp_sock->target_idx;
  393. llcp_addr->nfc_protocol = llcp_sock->nfc_protocol;
  394. llcp_addr->dsap = llcp_sock->dsap;
  395. llcp_addr->ssap = llcp_sock->ssap;
  396. llcp_addr->service_name_len = llcp_sock->service_name_len;
  397. memcpy(llcp_addr->service_name, llcp_sock->service_name,
  398. llcp_addr->service_name_len);
  399. release_sock(sk);
  400. return 0;
  401. }
  402. static inline unsigned int llcp_accept_poll(struct sock *parent)
  403. {
  404. struct nfc_llcp_sock *llcp_sock, *parent_sock;
  405. struct sock *sk;
  406. parent_sock = nfc_llcp_sock(parent);
  407. list_for_each_entry(llcp_sock, &parent_sock->accept_queue,
  408. accept_queue) {
  409. sk = &llcp_sock->sk;
  410. if (sk->sk_state == LLCP_CONNECTED)
  411. return POLLIN | POLLRDNORM;
  412. }
  413. return 0;
  414. }
  415. static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
  416. poll_table *wait)
  417. {
  418. struct sock *sk = sock->sk;
  419. unsigned int mask = 0;
  420. pr_debug("%p\n", sk);
  421. sock_poll_wait(file, sk_sleep(sk), wait);
  422. if (sk->sk_state == LLCP_LISTEN)
  423. return llcp_accept_poll(sk);
  424. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  425. mask |= POLLERR |
  426. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
  427. if (!skb_queue_empty(&sk->sk_receive_queue))
  428. mask |= POLLIN | POLLRDNORM;
  429. if (sk->sk_state == LLCP_CLOSED)
  430. mask |= POLLHUP;
  431. if (sk->sk_shutdown & RCV_SHUTDOWN)
  432. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  433. if (sk->sk_shutdown == SHUTDOWN_MASK)
  434. mask |= POLLHUP;
  435. if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED)
  436. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  437. else
  438. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  439. pr_debug("mask 0x%x\n", mask);
  440. return mask;
  441. }
  442. static int llcp_sock_release(struct socket *sock)
  443. {
  444. struct sock *sk = sock->sk;
  445. struct nfc_llcp_local *local;
  446. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  447. int err = 0;
  448. if (!sk)
  449. return 0;
  450. pr_debug("%p\n", sk);
  451. local = llcp_sock->local;
  452. if (local == NULL) {
  453. err = -ENODEV;
  454. goto out;
  455. }
  456. lock_sock(sk);
  457. /* Send a DISC */
  458. if (sk->sk_state == LLCP_CONNECTED)
  459. nfc_llcp_send_disconnect(llcp_sock);
  460. if (sk->sk_state == LLCP_LISTEN) {
  461. struct nfc_llcp_sock *lsk, *n;
  462. struct sock *accept_sk;
  463. list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
  464. accept_queue) {
  465. accept_sk = &lsk->sk;
  466. lock_sock(accept_sk);
  467. nfc_llcp_send_disconnect(lsk);
  468. nfc_llcp_accept_unlink(accept_sk);
  469. release_sock(accept_sk);
  470. }
  471. }
  472. if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
  473. nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
  474. release_sock(sk);
  475. /* Keep this sock alive and therefore do not remove it from the sockets
  476. * list until the DISC PDU has been actually sent. Otherwise we would
  477. * reply with DM PDUs before sending the DISC one.
  478. */
  479. if (sk->sk_state == LLCP_DISCONNECTING)
  480. return err;
  481. if (sock->type == SOCK_RAW)
  482. nfc_llcp_sock_unlink(&local->raw_sockets, sk);
  483. else
  484. nfc_llcp_sock_unlink(&local->sockets, sk);
  485. out:
  486. sock_orphan(sk);
  487. sock_put(sk);
  488. return err;
  489. }
  490. static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
  491. int len, int flags)
  492. {
  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. pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
  500. if (!addr || len < sizeof(*addr) || addr->sa_family != AF_NFC)
  501. return -EINVAL;
  502. if (addr->service_name_len == 0 && addr->dsap == 0)
  503. return -EINVAL;
  504. pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
  505. addr->target_idx, addr->nfc_protocol);
  506. lock_sock(sk);
  507. if (sk->sk_state == LLCP_CONNECTED) {
  508. ret = -EISCONN;
  509. goto error;
  510. }
  511. dev = nfc_get_device(addr->dev_idx);
  512. if (dev == NULL) {
  513. ret = -ENODEV;
  514. goto error;
  515. }
  516. local = nfc_llcp_find_local(dev);
  517. if (local == NULL) {
  518. ret = -ENODEV;
  519. goto put_dev;
  520. }
  521. device_lock(&dev->dev);
  522. if (dev->dep_link_up == false) {
  523. ret = -ENOLINK;
  524. device_unlock(&dev->dev);
  525. goto put_dev;
  526. }
  527. device_unlock(&dev->dev);
  528. if (local->rf_mode == NFC_RF_INITIATOR &&
  529. addr->target_idx != local->target_idx) {
  530. ret = -ENOLINK;
  531. goto put_dev;
  532. }
  533. llcp_sock->dev = dev;
  534. llcp_sock->local = nfc_llcp_local_get(local);
  535. llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
  536. if (llcp_sock->ssap == LLCP_SAP_MAX) {
  537. ret = -ENOMEM;
  538. goto put_dev;
  539. }
  540. llcp_sock->reserved_ssap = llcp_sock->ssap;
  541. if (addr->service_name_len == 0)
  542. llcp_sock->dsap = addr->dsap;
  543. else
  544. llcp_sock->dsap = LLCP_SAP_SDP;
  545. llcp_sock->nfc_protocol = addr->nfc_protocol;
  546. llcp_sock->service_name_len = min_t(unsigned int,
  547. addr->service_name_len,
  548. NFC_LLCP_MAX_SERVICE_NAME);
  549. llcp_sock->service_name = kmemdup(addr->service_name,
  550. llcp_sock->service_name_len,
  551. GFP_KERNEL);
  552. nfc_llcp_sock_link(&local->connecting_sockets, sk);
  553. ret = nfc_llcp_send_connect(llcp_sock);
  554. if (ret)
  555. goto sock_unlink;
  556. sk->sk_state = LLCP_CONNECTING;
  557. ret = sock_wait_state(sk, LLCP_CONNECTED,
  558. sock_sndtimeo(sk, flags & O_NONBLOCK));
  559. if (ret && ret != -EINPROGRESS)
  560. goto sock_unlink;
  561. release_sock(sk);
  562. return ret;
  563. sock_unlink:
  564. nfc_llcp_put_ssap(local, llcp_sock->ssap);
  565. nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
  566. put_dev:
  567. nfc_put_device(dev);
  568. error:
  569. release_sock(sk);
  570. return ret;
  571. }
  572. static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg,
  573. size_t len)
  574. {
  575. struct sock *sk = sock->sk;
  576. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  577. int ret;
  578. pr_debug("sock %p sk %p", sock, sk);
  579. ret = sock_error(sk);
  580. if (ret)
  581. return ret;
  582. if (msg->msg_flags & MSG_OOB)
  583. return -EOPNOTSUPP;
  584. lock_sock(sk);
  585. if (sk->sk_type == SOCK_DGRAM) {
  586. DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr,
  587. msg->msg_name);
  588. if (msg->msg_namelen < sizeof(*addr)) {
  589. release_sock(sk);
  590. return -EINVAL;
  591. }
  592. release_sock(sk);
  593. return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
  594. msg, len);
  595. }
  596. if (sk->sk_state != LLCP_CONNECTED) {
  597. release_sock(sk);
  598. return -ENOTCONN;
  599. }
  600. release_sock(sk);
  601. return nfc_llcp_send_i_frame(llcp_sock, msg, len);
  602. }
  603. static int llcp_sock_recvmsg(struct socket *sock, struct msghdr *msg,
  604. size_t len, int flags)
  605. {
  606. int noblock = flags & MSG_DONTWAIT;
  607. struct sock *sk = sock->sk;
  608. unsigned int copied, rlen;
  609. struct sk_buff *skb, *cskb;
  610. int err = 0;
  611. pr_debug("%p %zu\n", sk, len);
  612. lock_sock(sk);
  613. if (sk->sk_state == LLCP_CLOSED &&
  614. skb_queue_empty(&sk->sk_receive_queue)) {
  615. release_sock(sk);
  616. return 0;
  617. }
  618. release_sock(sk);
  619. if (flags & (MSG_OOB))
  620. return -EOPNOTSUPP;
  621. skb = skb_recv_datagram(sk, flags, noblock, &err);
  622. if (!skb) {
  623. pr_err("Recv datagram failed state %d %d %d",
  624. sk->sk_state, err, sock_error(sk));
  625. if (sk->sk_shutdown & RCV_SHUTDOWN)
  626. return 0;
  627. return err;
  628. }
  629. rlen = skb->len; /* real length of skb */
  630. copied = min_t(unsigned int, rlen, len);
  631. cskb = skb;
  632. if (skb_copy_datagram_msg(cskb, 0, msg, copied)) {
  633. if (!(flags & MSG_PEEK))
  634. skb_queue_head(&sk->sk_receive_queue, skb);
  635. return -EFAULT;
  636. }
  637. sock_recv_timestamp(msg, sk, skb);
  638. if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
  639. struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
  640. DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, sockaddr,
  641. msg->msg_name);
  642. msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
  643. pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
  644. memset(sockaddr, 0, sizeof(*sockaddr));
  645. sockaddr->sa_family = AF_NFC;
  646. sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP;
  647. sockaddr->dsap = ui_cb->dsap;
  648. sockaddr->ssap = ui_cb->ssap;
  649. }
  650. /* Mark read part of skb as used */
  651. if (!(flags & MSG_PEEK)) {
  652. /* SOCK_STREAM: re-queue skb if it contains unreceived data */
  653. if (sk->sk_type == SOCK_STREAM ||
  654. sk->sk_type == SOCK_DGRAM ||
  655. sk->sk_type == SOCK_RAW) {
  656. skb_pull(skb, copied);
  657. if (skb->len) {
  658. skb_queue_head(&sk->sk_receive_queue, skb);
  659. goto done;
  660. }
  661. }
  662. kfree_skb(skb);
  663. }
  664. /* XXX Queue backlogged skbs */
  665. done:
  666. /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
  667. if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
  668. copied = rlen;
  669. return copied;
  670. }
  671. static const struct proto_ops llcp_sock_ops = {
  672. .family = PF_NFC,
  673. .owner = THIS_MODULE,
  674. .bind = llcp_sock_bind,
  675. .connect = llcp_sock_connect,
  676. .release = llcp_sock_release,
  677. .socketpair = sock_no_socketpair,
  678. .accept = llcp_sock_accept,
  679. .getname = llcp_sock_getname,
  680. .poll = llcp_sock_poll,
  681. .ioctl = sock_no_ioctl,
  682. .listen = llcp_sock_listen,
  683. .shutdown = sock_no_shutdown,
  684. .setsockopt = nfc_llcp_setsockopt,
  685. .getsockopt = nfc_llcp_getsockopt,
  686. .sendmsg = llcp_sock_sendmsg,
  687. .recvmsg = llcp_sock_recvmsg,
  688. .mmap = sock_no_mmap,
  689. };
  690. static const struct proto_ops llcp_rawsock_ops = {
  691. .family = PF_NFC,
  692. .owner = THIS_MODULE,
  693. .bind = llcp_raw_sock_bind,
  694. .connect = sock_no_connect,
  695. .release = llcp_sock_release,
  696. .socketpair = sock_no_socketpair,
  697. .accept = sock_no_accept,
  698. .getname = llcp_sock_getname,
  699. .poll = llcp_sock_poll,
  700. .ioctl = sock_no_ioctl,
  701. .listen = sock_no_listen,
  702. .shutdown = sock_no_shutdown,
  703. .setsockopt = sock_no_setsockopt,
  704. .getsockopt = sock_no_getsockopt,
  705. .sendmsg = sock_no_sendmsg,
  706. .recvmsg = llcp_sock_recvmsg,
  707. .mmap = sock_no_mmap,
  708. };
  709. static void llcp_sock_destruct(struct sock *sk)
  710. {
  711. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  712. pr_debug("%p\n", sk);
  713. if (sk->sk_state == LLCP_CONNECTED)
  714. nfc_put_device(llcp_sock->dev);
  715. skb_queue_purge(&sk->sk_receive_queue);
  716. nfc_llcp_sock_free(llcp_sock);
  717. if (!sock_flag(sk, SOCK_DEAD)) {
  718. pr_err("Freeing alive NFC LLCP socket %p\n", sk);
  719. return;
  720. }
  721. }
  722. struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp, int kern)
  723. {
  724. struct sock *sk;
  725. struct nfc_llcp_sock *llcp_sock;
  726. sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto, kern);
  727. if (!sk)
  728. return NULL;
  729. llcp_sock = nfc_llcp_sock(sk);
  730. sock_init_data(sock, sk);
  731. sk->sk_state = LLCP_CLOSED;
  732. sk->sk_protocol = NFC_SOCKPROTO_LLCP;
  733. sk->sk_type = type;
  734. sk->sk_destruct = llcp_sock_destruct;
  735. llcp_sock->ssap = 0;
  736. llcp_sock->dsap = LLCP_SAP_SDP;
  737. llcp_sock->rw = LLCP_MAX_RW + 1;
  738. llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1);
  739. llcp_sock->send_n = llcp_sock->send_ack_n = 0;
  740. llcp_sock->recv_n = llcp_sock->recv_ack_n = 0;
  741. llcp_sock->remote_ready = 1;
  742. llcp_sock->reserved_ssap = LLCP_SAP_MAX;
  743. nfc_llcp_socket_remote_param_init(llcp_sock);
  744. skb_queue_head_init(&llcp_sock->tx_queue);
  745. skb_queue_head_init(&llcp_sock->tx_pending_queue);
  746. INIT_LIST_HEAD(&llcp_sock->accept_queue);
  747. if (sock != NULL)
  748. sock->state = SS_UNCONNECTED;
  749. return sk;
  750. }
  751. void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
  752. {
  753. kfree(sock->service_name);
  754. skb_queue_purge(&sock->tx_queue);
  755. skb_queue_purge(&sock->tx_pending_queue);
  756. list_del_init(&sock->accept_queue);
  757. sock->parent = NULL;
  758. nfc_llcp_local_put(sock->local);
  759. }
  760. static int llcp_sock_create(struct net *net, struct socket *sock,
  761. const struct nfc_protocol *nfc_proto, int kern)
  762. {
  763. struct sock *sk;
  764. pr_debug("%p\n", sock);
  765. if (sock->type != SOCK_STREAM &&
  766. sock->type != SOCK_DGRAM &&
  767. sock->type != SOCK_RAW)
  768. return -ESOCKTNOSUPPORT;
  769. if (sock->type == SOCK_RAW)
  770. sock->ops = &llcp_rawsock_ops;
  771. else
  772. sock->ops = &llcp_sock_ops;
  773. sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC, kern);
  774. if (sk == NULL)
  775. return -ENOMEM;
  776. return 0;
  777. }
  778. static const struct nfc_protocol llcp_nfc_proto = {
  779. .id = NFC_SOCKPROTO_LLCP,
  780. .proto = &llcp_sock_proto,
  781. .owner = THIS_MODULE,
  782. .create = llcp_sock_create
  783. };
  784. int __init nfc_llcp_sock_init(void)
  785. {
  786. return nfc_proto_register(&llcp_nfc_proto);
  787. }
  788. void nfc_llcp_sock_exit(void)
  789. {
  790. nfc_proto_unregister(&llcp_nfc_proto);
  791. }