llcp_sock.c 23 KB

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