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