llcp_sock.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. llcp_addr->sa_family = AF_NFC;
  386. llcp_addr->dev_idx = llcp_sock->dev->idx;
  387. llcp_addr->target_idx = llcp_sock->target_idx;
  388. llcp_addr->nfc_protocol = llcp_sock->nfc_protocol;
  389. llcp_addr->dsap = llcp_sock->dsap;
  390. llcp_addr->ssap = llcp_sock->ssap;
  391. llcp_addr->service_name_len = llcp_sock->service_name_len;
  392. memcpy(llcp_addr->service_name, llcp_sock->service_name,
  393. llcp_addr->service_name_len);
  394. return 0;
  395. }
  396. static inline unsigned int llcp_accept_poll(struct sock *parent)
  397. {
  398. struct nfc_llcp_sock *llcp_sock, *parent_sock;
  399. struct sock *sk;
  400. parent_sock = nfc_llcp_sock(parent);
  401. list_for_each_entry(llcp_sock, &parent_sock->accept_queue,
  402. accept_queue) {
  403. sk = &llcp_sock->sk;
  404. if (sk->sk_state == LLCP_CONNECTED)
  405. return POLLIN | POLLRDNORM;
  406. }
  407. return 0;
  408. }
  409. static unsigned int llcp_sock_poll(struct file *file, struct socket *sock,
  410. poll_table *wait)
  411. {
  412. struct sock *sk = sock->sk;
  413. unsigned int mask = 0;
  414. pr_debug("%p\n", sk);
  415. sock_poll_wait(file, sk_sleep(sk), wait);
  416. if (sk->sk_state == LLCP_LISTEN)
  417. return llcp_accept_poll(sk);
  418. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  419. mask |= POLLERR |
  420. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
  421. if (!skb_queue_empty(&sk->sk_receive_queue))
  422. mask |= POLLIN | POLLRDNORM;
  423. if (sk->sk_state == LLCP_CLOSED)
  424. mask |= POLLHUP;
  425. if (sk->sk_shutdown & RCV_SHUTDOWN)
  426. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  427. if (sk->sk_shutdown == SHUTDOWN_MASK)
  428. mask |= POLLHUP;
  429. if (sock_writeable(sk) && sk->sk_state == LLCP_CONNECTED)
  430. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  431. else
  432. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  433. pr_debug("mask 0x%x\n", mask);
  434. return mask;
  435. }
  436. static int llcp_sock_release(struct socket *sock)
  437. {
  438. struct sock *sk = sock->sk;
  439. struct nfc_llcp_local *local;
  440. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  441. int err = 0;
  442. if (!sk)
  443. return 0;
  444. pr_debug("%p\n", sk);
  445. local = llcp_sock->local;
  446. if (local == NULL) {
  447. err = -ENODEV;
  448. goto out;
  449. }
  450. lock_sock(sk);
  451. /* Send a DISC */
  452. if (sk->sk_state == LLCP_CONNECTED)
  453. nfc_llcp_send_disconnect(llcp_sock);
  454. if (sk->sk_state == LLCP_LISTEN) {
  455. struct nfc_llcp_sock *lsk, *n;
  456. struct sock *accept_sk;
  457. list_for_each_entry_safe(lsk, n, &llcp_sock->accept_queue,
  458. accept_queue) {
  459. accept_sk = &lsk->sk;
  460. lock_sock(accept_sk);
  461. nfc_llcp_send_disconnect(lsk);
  462. nfc_llcp_accept_unlink(accept_sk);
  463. release_sock(accept_sk);
  464. }
  465. }
  466. if (llcp_sock->reserved_ssap < LLCP_SAP_MAX)
  467. nfc_llcp_put_ssap(llcp_sock->local, llcp_sock->ssap);
  468. release_sock(sk);
  469. /* Keep this sock alive and therefore do not remove it from the sockets
  470. * list until the DISC PDU has been actually sent. Otherwise we would
  471. * reply with DM PDUs before sending the DISC one.
  472. */
  473. if (sk->sk_state == LLCP_DISCONNECTING)
  474. return err;
  475. if (sock->type == SOCK_RAW)
  476. nfc_llcp_sock_unlink(&local->raw_sockets, sk);
  477. else
  478. nfc_llcp_sock_unlink(&local->sockets, sk);
  479. out:
  480. sock_orphan(sk);
  481. sock_put(sk);
  482. return err;
  483. }
  484. static int llcp_sock_connect(struct socket *sock, struct sockaddr *_addr,
  485. int len, int flags)
  486. {
  487. struct sock *sk = sock->sk;
  488. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  489. struct sockaddr_nfc_llcp *addr = (struct sockaddr_nfc_llcp *)_addr;
  490. struct nfc_dev *dev;
  491. struct nfc_llcp_local *local;
  492. int ret = 0;
  493. pr_debug("sock %p sk %p flags 0x%x\n", sock, sk, flags);
  494. if (!addr || len < sizeof(*addr) || addr->sa_family != AF_NFC)
  495. return -EINVAL;
  496. if (addr->service_name_len == 0 && addr->dsap == 0)
  497. return -EINVAL;
  498. pr_debug("addr dev_idx=%u target_idx=%u protocol=%u\n", addr->dev_idx,
  499. addr->target_idx, addr->nfc_protocol);
  500. lock_sock(sk);
  501. if (sk->sk_state == LLCP_CONNECTED) {
  502. ret = -EISCONN;
  503. goto error;
  504. }
  505. dev = nfc_get_device(addr->dev_idx);
  506. if (dev == NULL) {
  507. ret = -ENODEV;
  508. goto error;
  509. }
  510. local = nfc_llcp_find_local(dev);
  511. if (local == NULL) {
  512. ret = -ENODEV;
  513. goto put_dev;
  514. }
  515. device_lock(&dev->dev);
  516. if (dev->dep_link_up == false) {
  517. ret = -ENOLINK;
  518. device_unlock(&dev->dev);
  519. goto put_dev;
  520. }
  521. device_unlock(&dev->dev);
  522. if (local->rf_mode == NFC_RF_INITIATOR &&
  523. addr->target_idx != local->target_idx) {
  524. ret = -ENOLINK;
  525. goto put_dev;
  526. }
  527. llcp_sock->dev = dev;
  528. llcp_sock->local = nfc_llcp_local_get(local);
  529. llcp_sock->ssap = nfc_llcp_get_local_ssap(local);
  530. if (llcp_sock->ssap == LLCP_SAP_MAX) {
  531. ret = -ENOMEM;
  532. goto put_dev;
  533. }
  534. llcp_sock->reserved_ssap = llcp_sock->ssap;
  535. if (addr->service_name_len == 0)
  536. llcp_sock->dsap = addr->dsap;
  537. else
  538. llcp_sock->dsap = LLCP_SAP_SDP;
  539. llcp_sock->nfc_protocol = addr->nfc_protocol;
  540. llcp_sock->service_name_len = min_t(unsigned int,
  541. addr->service_name_len,
  542. NFC_LLCP_MAX_SERVICE_NAME);
  543. llcp_sock->service_name = kmemdup(addr->service_name,
  544. llcp_sock->service_name_len,
  545. GFP_KERNEL);
  546. nfc_llcp_sock_link(&local->connecting_sockets, sk);
  547. ret = nfc_llcp_send_connect(llcp_sock);
  548. if (ret)
  549. goto sock_unlink;
  550. sk->sk_state = LLCP_CONNECTING;
  551. ret = sock_wait_state(sk, LLCP_CONNECTED,
  552. sock_sndtimeo(sk, flags & O_NONBLOCK));
  553. if (ret && ret != -EINPROGRESS)
  554. goto sock_unlink;
  555. release_sock(sk);
  556. return ret;
  557. sock_unlink:
  558. nfc_llcp_put_ssap(local, llcp_sock->ssap);
  559. nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
  560. put_dev:
  561. nfc_put_device(dev);
  562. error:
  563. release_sock(sk);
  564. return ret;
  565. }
  566. static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg,
  567. size_t len)
  568. {
  569. struct sock *sk = sock->sk;
  570. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  571. int ret;
  572. pr_debug("sock %p sk %p", sock, sk);
  573. ret = sock_error(sk);
  574. if (ret)
  575. return ret;
  576. if (msg->msg_flags & MSG_OOB)
  577. return -EOPNOTSUPP;
  578. lock_sock(sk);
  579. if (sk->sk_type == SOCK_DGRAM) {
  580. DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr,
  581. msg->msg_name);
  582. if (msg->msg_namelen < sizeof(*addr)) {
  583. release_sock(sk);
  584. return -EINVAL;
  585. }
  586. release_sock(sk);
  587. return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap,
  588. msg, len);
  589. }
  590. if (sk->sk_state != LLCP_CONNECTED) {
  591. release_sock(sk);
  592. return -ENOTCONN;
  593. }
  594. release_sock(sk);
  595. return nfc_llcp_send_i_frame(llcp_sock, msg, len);
  596. }
  597. static int llcp_sock_recvmsg(struct socket *sock, struct msghdr *msg,
  598. size_t len, int flags)
  599. {
  600. int noblock = flags & MSG_DONTWAIT;
  601. struct sock *sk = sock->sk;
  602. unsigned int copied, rlen;
  603. struct sk_buff *skb, *cskb;
  604. int err = 0;
  605. pr_debug("%p %zu\n", sk, len);
  606. lock_sock(sk);
  607. if (sk->sk_state == LLCP_CLOSED &&
  608. skb_queue_empty(&sk->sk_receive_queue)) {
  609. release_sock(sk);
  610. return 0;
  611. }
  612. release_sock(sk);
  613. if (flags & (MSG_OOB))
  614. return -EOPNOTSUPP;
  615. skb = skb_recv_datagram(sk, flags, noblock, &err);
  616. if (!skb) {
  617. pr_err("Recv datagram failed state %d %d %d",
  618. sk->sk_state, err, sock_error(sk));
  619. if (sk->sk_shutdown & RCV_SHUTDOWN)
  620. return 0;
  621. return err;
  622. }
  623. rlen = skb->len; /* real length of skb */
  624. copied = min_t(unsigned int, rlen, len);
  625. cskb = skb;
  626. if (skb_copy_datagram_msg(cskb, 0, msg, copied)) {
  627. if (!(flags & MSG_PEEK))
  628. skb_queue_head(&sk->sk_receive_queue, skb);
  629. return -EFAULT;
  630. }
  631. sock_recv_timestamp(msg, sk, skb);
  632. if (sk->sk_type == SOCK_DGRAM && msg->msg_name) {
  633. struct nfc_llcp_ui_cb *ui_cb = nfc_llcp_ui_skb_cb(skb);
  634. DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, sockaddr,
  635. msg->msg_name);
  636. msg->msg_namelen = sizeof(struct sockaddr_nfc_llcp);
  637. pr_debug("Datagram socket %d %d\n", ui_cb->dsap, ui_cb->ssap);
  638. memset(sockaddr, 0, sizeof(*sockaddr));
  639. sockaddr->sa_family = AF_NFC;
  640. sockaddr->nfc_protocol = NFC_PROTO_NFC_DEP;
  641. sockaddr->dsap = ui_cb->dsap;
  642. sockaddr->ssap = ui_cb->ssap;
  643. }
  644. /* Mark read part of skb as used */
  645. if (!(flags & MSG_PEEK)) {
  646. /* SOCK_STREAM: re-queue skb if it contains unreceived data */
  647. if (sk->sk_type == SOCK_STREAM ||
  648. sk->sk_type == SOCK_DGRAM ||
  649. sk->sk_type == SOCK_RAW) {
  650. skb_pull(skb, copied);
  651. if (skb->len) {
  652. skb_queue_head(&sk->sk_receive_queue, skb);
  653. goto done;
  654. }
  655. }
  656. kfree_skb(skb);
  657. }
  658. /* XXX Queue backlogged skbs */
  659. done:
  660. /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
  661. if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
  662. copied = rlen;
  663. return copied;
  664. }
  665. static const struct proto_ops llcp_sock_ops = {
  666. .family = PF_NFC,
  667. .owner = THIS_MODULE,
  668. .bind = llcp_sock_bind,
  669. .connect = llcp_sock_connect,
  670. .release = llcp_sock_release,
  671. .socketpair = sock_no_socketpair,
  672. .accept = llcp_sock_accept,
  673. .getname = llcp_sock_getname,
  674. .poll = llcp_sock_poll,
  675. .ioctl = sock_no_ioctl,
  676. .listen = llcp_sock_listen,
  677. .shutdown = sock_no_shutdown,
  678. .setsockopt = nfc_llcp_setsockopt,
  679. .getsockopt = nfc_llcp_getsockopt,
  680. .sendmsg = llcp_sock_sendmsg,
  681. .recvmsg = llcp_sock_recvmsg,
  682. .mmap = sock_no_mmap,
  683. };
  684. static const struct proto_ops llcp_rawsock_ops = {
  685. .family = PF_NFC,
  686. .owner = THIS_MODULE,
  687. .bind = llcp_raw_sock_bind,
  688. .connect = sock_no_connect,
  689. .release = llcp_sock_release,
  690. .socketpair = sock_no_socketpair,
  691. .accept = sock_no_accept,
  692. .getname = llcp_sock_getname,
  693. .poll = llcp_sock_poll,
  694. .ioctl = sock_no_ioctl,
  695. .listen = sock_no_listen,
  696. .shutdown = sock_no_shutdown,
  697. .setsockopt = sock_no_setsockopt,
  698. .getsockopt = sock_no_getsockopt,
  699. .sendmsg = sock_no_sendmsg,
  700. .recvmsg = llcp_sock_recvmsg,
  701. .mmap = sock_no_mmap,
  702. };
  703. static void llcp_sock_destruct(struct sock *sk)
  704. {
  705. struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
  706. pr_debug("%p\n", sk);
  707. if (sk->sk_state == LLCP_CONNECTED)
  708. nfc_put_device(llcp_sock->dev);
  709. skb_queue_purge(&sk->sk_receive_queue);
  710. nfc_llcp_sock_free(llcp_sock);
  711. if (!sock_flag(sk, SOCK_DEAD)) {
  712. pr_err("Freeing alive NFC LLCP socket %p\n", sk);
  713. return;
  714. }
  715. }
  716. struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp, int kern)
  717. {
  718. struct sock *sk;
  719. struct nfc_llcp_sock *llcp_sock;
  720. sk = sk_alloc(&init_net, PF_NFC, gfp, &llcp_sock_proto, kern);
  721. if (!sk)
  722. return NULL;
  723. llcp_sock = nfc_llcp_sock(sk);
  724. sock_init_data(sock, sk);
  725. sk->sk_state = LLCP_CLOSED;
  726. sk->sk_protocol = NFC_SOCKPROTO_LLCP;
  727. sk->sk_type = type;
  728. sk->sk_destruct = llcp_sock_destruct;
  729. llcp_sock->ssap = 0;
  730. llcp_sock->dsap = LLCP_SAP_SDP;
  731. llcp_sock->rw = LLCP_MAX_RW + 1;
  732. llcp_sock->miux = cpu_to_be16(LLCP_MAX_MIUX + 1);
  733. llcp_sock->send_n = llcp_sock->send_ack_n = 0;
  734. llcp_sock->recv_n = llcp_sock->recv_ack_n = 0;
  735. llcp_sock->remote_ready = 1;
  736. llcp_sock->reserved_ssap = LLCP_SAP_MAX;
  737. nfc_llcp_socket_remote_param_init(llcp_sock);
  738. skb_queue_head_init(&llcp_sock->tx_queue);
  739. skb_queue_head_init(&llcp_sock->tx_pending_queue);
  740. INIT_LIST_HEAD(&llcp_sock->accept_queue);
  741. if (sock != NULL)
  742. sock->state = SS_UNCONNECTED;
  743. return sk;
  744. }
  745. void nfc_llcp_sock_free(struct nfc_llcp_sock *sock)
  746. {
  747. kfree(sock->service_name);
  748. skb_queue_purge(&sock->tx_queue);
  749. skb_queue_purge(&sock->tx_pending_queue);
  750. list_del_init(&sock->accept_queue);
  751. sock->parent = NULL;
  752. nfc_llcp_local_put(sock->local);
  753. }
  754. static int llcp_sock_create(struct net *net, struct socket *sock,
  755. const struct nfc_protocol *nfc_proto, int kern)
  756. {
  757. struct sock *sk;
  758. pr_debug("%p\n", sock);
  759. if (sock->type != SOCK_STREAM &&
  760. sock->type != SOCK_DGRAM &&
  761. sock->type != SOCK_RAW)
  762. return -ESOCKTNOSUPPORT;
  763. if (sock->type == SOCK_RAW)
  764. sock->ops = &llcp_rawsock_ops;
  765. else
  766. sock->ops = &llcp_sock_ops;
  767. sk = nfc_llcp_sock_alloc(sock, sock->type, GFP_ATOMIC, kern);
  768. if (sk == NULL)
  769. return -ENOMEM;
  770. return 0;
  771. }
  772. static const struct nfc_protocol llcp_nfc_proto = {
  773. .id = NFC_SOCKPROTO_LLCP,
  774. .proto = &llcp_sock_proto,
  775. .owner = THIS_MODULE,
  776. .create = llcp_sock_create
  777. };
  778. int __init nfc_llcp_sock_init(void)
  779. {
  780. return nfc_proto_register(&llcp_nfc_proto);
  781. }
  782. void nfc_llcp_sock_exit(void)
  783. {
  784. nfc_proto_unregister(&llcp_nfc_proto);
  785. }