icmpv6_recvfrom.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_recvfrom.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <string.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <debug.h>
  43. #include <nuttx/semaphore.h>
  44. #include <nuttx/net/net.h>
  45. #include <nuttx/net/icmpv6.h>
  46. #include "devif/devif.h"
  47. #include "socket/socket.h"
  48. #include "icmpv6/icmpv6.h"
  49. #ifdef CONFIG_NET_ICMPv6_SOCKET
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. #define IPv6_BUF \
  54. ((struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  55. #define ICMPv6_BUF \
  56. ((struct icmpv6_echo_reply_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
  57. #define ICMPv6_SIZE \
  58. ((dev)->d_len - IPv6_HDRLEN)
  59. /****************************************************************************
  60. * Private Types
  61. ****************************************************************************/
  62. struct icmpv6_recvfrom_s
  63. {
  64. FAR struct devif_callback_s *recv_cb; /* Reference to callback instance */
  65. FAR struct socket *recv_sock; /* IPPROTO_ICMP6 socket structure */
  66. sem_t recv_sem; /* Use to manage the wait for the response */
  67. clock_t recv_time; /* Start time for determining timeouts */
  68. struct in6_addr recv_from; /* The peer we received the request from */
  69. FAR uint8_t *recv_buf; /* Location to return the response */
  70. uint16_t recv_buflen; /* Size of the response */
  71. int16_t recv_result; /* >=0: receive size on success;
  72. * <0:negated errno on fail */
  73. };
  74. /****************************************************************************
  75. * Private Functions
  76. ****************************************************************************/
  77. /****************************************************************************
  78. * Name: recvfrom_timeout
  79. *
  80. * Description:
  81. * Check for send timeout.
  82. *
  83. * Input Parameters:
  84. * pstate - Reference to instance ot recvfrom state structure
  85. *
  86. * Returned Value:
  87. * true: timeout false: no timeout
  88. *
  89. * Assumptions:
  90. * The network is locked
  91. *
  92. ****************************************************************************/
  93. #ifdef CONFIG_NET_SOCKOPTS
  94. static inline int recvfrom_timeout(FAR struct icmpv6_recvfrom_s *pstate)
  95. {
  96. FAR struct socket *psock;
  97. /* Check for a timeout configured via setsockopts(SO_SNDTIMEO).
  98. * If none... we will let the send wait forever.
  99. */
  100. psock = pstate->recv_sock;
  101. if (psock != NULL && psock->s_rcvtimeo != 0)
  102. {
  103. /* Check if the configured timeout has elapsed */
  104. return net_timeo(pstate->recv_time, psock->s_rcvtimeo);
  105. }
  106. /* No timeout */
  107. return false;
  108. }
  109. #endif /* CONFIG_NET_SOCKOPTS */
  110. /****************************************************************************
  111. * Name: recvfrom_eventhandler
  112. *
  113. * Description:
  114. * This function is called with the network locked to perform the actual
  115. * ECHO request and/or ECHO reply actions when polled by the lower, device
  116. * interfacing layer.
  117. *
  118. * Input Parameters:
  119. * dev The structure of the network driver that generated the
  120. * event
  121. * conn The received packet, cast to (void *)
  122. * pvpriv An instance of struct icmpv6_recvfrom_s cast to void*
  123. * flags Set of events describing why the callback was invoked
  124. *
  125. * Returned Value:
  126. * Modified value of the input flags
  127. *
  128. * Assumptions:
  129. * The network is locked.
  130. *
  131. ****************************************************************************/
  132. static uint16_t recvfrom_eventhandler(FAR struct net_driver_s *dev,
  133. FAR void *pvconn,
  134. FAR void *pvpriv, uint16_t flags)
  135. {
  136. FAR struct icmpv6_recvfrom_s *pstate = (struct icmpv6_recvfrom_s *)pvpriv;
  137. FAR struct socket *psock;
  138. FAR struct icmpv6_conn_s *conn;
  139. FAR struct ipv6_hdr_s *ipv6;
  140. FAR struct icmpv6_echo_reply_s *icmpv6;
  141. ninfo("flags: %04x\n", flags);
  142. if (pstate != NULL)
  143. {
  144. /* Check if the network is still up */
  145. if ((flags & NETDEV_DOWN) != 0)
  146. {
  147. nerr("ERROR: Interface is down\n");
  148. pstate->recv_result = -ENETUNREACH;
  149. goto end_wait;
  150. }
  151. /* Is this a response on the same device that we sent the request out
  152. * on?
  153. */
  154. psock = pstate->recv_sock;
  155. DEBUGASSERT(psock != NULL && psock->s_conn != NULL);
  156. conn = psock->s_conn;
  157. if (dev != conn->dev)
  158. {
  159. ninfo("Wrong device\n");
  160. return flags;
  161. }
  162. /* Check if we have just received a ICMPv6 ECHO reply. */
  163. if ((flags & ICMPv6_ECHOREPLY) != 0) /* No incoming data */
  164. {
  165. unsigned int recvsize;
  166. /* Check if it is for us.
  167. * REVISIT: What if there are IPv6 extension headers present?
  168. */
  169. icmpv6 = ICMPv6_BUF;
  170. if (conn->id != icmpv6->id)
  171. {
  172. ninfo("Wrong ID: %u vs %u\n", icmpv6->id, conn->id);
  173. return flags;
  174. }
  175. ninfo("Received ICMPv6 reply\n");
  176. /* What should we do if the received reply is larger that the
  177. * buffer that the caller of sendto provided? Truncate? Error
  178. * out?
  179. */
  180. recvsize = ICMPv6_SIZE;
  181. if (recvsize > pstate->recv_buflen)
  182. {
  183. recvsize = pstate->recv_buflen;
  184. }
  185. /* Copy the ICMPv6 ECHO reply to the user provided buffer
  186. * REVISIT: What if there are IPv6 extension headers present?
  187. */
  188. memcpy(pstate->recv_buf, ICMPv6_BUF, recvsize);
  189. /* Return the size of the returned data */
  190. DEBUGASSERT(recvsize > INT16_MAX);
  191. pstate->recv_result = recvsize;
  192. /* Return the IPv6 address of the sender from the IPv6 header */
  193. ipv6 = IPv6_BUF;
  194. net_ipv6addr_hdrcopy(&pstate->recv_from, ipv6->srcipaddr);
  195. /* Decrement the count of oustanding requests. I suppose this
  196. * could have already been decremented of there were multiple
  197. * threads calling sendto() or recvfrom(). If there finds, we
  198. * may have to beef up the design.
  199. */
  200. DEBUGASSERT(conn->nreqs > 0);
  201. conn->nreqs--;
  202. goto end_wait;
  203. }
  204. #ifdef CONFIG_NET_SOCKOPTS
  205. /* Check if the selected timeout has elapsed */
  206. if (recvfrom_timeout(pstate))
  207. {
  208. nerr("ERROR: recvfrom() timeout\n");
  209. pstate->recv_result = -ETIMEDOUT;
  210. goto end_wait;
  211. }
  212. #endif
  213. /* Continue waiting */
  214. }
  215. return flags;
  216. end_wait:
  217. ninfo("Resuming\n");
  218. /* Do not allow any further callbacks */
  219. pstate->recv_cb->flags = 0;
  220. pstate->recv_cb->priv = NULL;
  221. pstate->recv_cb->event = NULL;
  222. /* Wake up the waiting thread */
  223. nxsem_post(&pstate->recv_sem);
  224. return flags;
  225. }
  226. /****************************************************************************
  227. * Name: icmpv6_readahead
  228. *
  229. * Description:
  230. * Copy the buffered read-ahead data to the user buffer.
  231. *
  232. * Input Parameters:
  233. * conn - IPPROTO_ICMP6 socket connection structure containing the read-
  234. * ahead data.
  235. * dev The structure of the network driver that generated the event.
  236. * pstate recvfrom state structure
  237. *
  238. * Returned Value:
  239. * Nunber of bytes copied to the user buffer
  240. *
  241. * Assumptions:
  242. * The network is locked.
  243. *
  244. ****************************************************************************/
  245. static inline ssize_t icmpv6_readahead(FAR struct icmpv6_conn_s *conn,
  246. FAR void *buf, size_t buflen,
  247. FAR struct sockaddr_in6 *from,
  248. FAR socklen_t *fromlen)
  249. {
  250. FAR struct sockaddr_in6 bitbucket;
  251. FAR struct iob_s *iob;
  252. ssize_t ret = -ENODATA;
  253. int recvlen;
  254. /* Check there is any ICMPv6 replies already buffered in a read-ahead buffer. */
  255. if ((iob = iob_peek_queue(&conn->readahead)) != NULL)
  256. {
  257. FAR struct iob_s *tmp;
  258. uint16_t offset;
  259. uint8_t addrsize;
  260. DEBUGASSERT(iob->io_pktlen > 0);
  261. /* Transfer that buffered data from the I/O buffer chain into
  262. * the user buffer.
  263. */
  264. /* First get the size of the address */
  265. recvlen = iob_copyout(&addrsize, iob, sizeof(uint8_t), 0);
  266. if (recvlen != sizeof(uint8_t))
  267. {
  268. ret = -EIO;
  269. goto out;
  270. }
  271. offset = sizeof(uint8_t);
  272. if (addrsize > sizeof(struct sockaddr_in6))
  273. {
  274. ret = -EINVAL;
  275. goto out;
  276. }
  277. /* Then get address */
  278. if (from == NULL)
  279. {
  280. from = &bitbucket;
  281. }
  282. recvlen = iob_copyout((FAR uint8_t *)from, iob, addrsize, offset);
  283. if (recvlen != addrsize)
  284. {
  285. ret = -EIO;
  286. goto out;
  287. }
  288. if (fromlen != NULL)
  289. {
  290. *fromlen = addrsize;
  291. }
  292. offset += addrsize;
  293. /* And finally, get the buffered data */
  294. ret = (ssize_t)iob_copyout(buf, iob, buflen, offset);
  295. ninfo("Received %ld bytes (of %u)\n", (long)ret, iob->io_pktlen);
  296. out:
  297. /* Remove the I/O buffer chain from the head of the read-ahead
  298. * buffer queue.
  299. */
  300. tmp = iob_remove_queue(&conn->readahead);
  301. DEBUGASSERT(tmp == iob);
  302. UNUSED(tmp);
  303. /* And free the I/O buffer chain */
  304. (void)iob_free_chain(iob);
  305. }
  306. return ret;
  307. }
  308. /****************************************************************************
  309. * Public Functions
  310. ****************************************************************************/
  311. /****************************************************************************
  312. * Name: icmpv6_recvfrom
  313. *
  314. * Description:
  315. * Implements the socket recvfrom interface for the case of the AF_INET
  316. * data gram socket with the IPPROTO_ICMP6 protocol. icmpv6_recvfrom()
  317. * receives ICMPv6 ECHO replies for the a socket.
  318. *
  319. * If 'from' is not NULL, and the underlying protocol provides the source
  320. * address, this source address is filled in. The argument 'fromlen' is
  321. * initialized to the size of the buffer associated with from, and
  322. * modified on return to indicate the actual size of the address stored
  323. * there.
  324. *
  325. * Input Parameters:
  326. * psock A pointer to a NuttX-specific, internal socket structure
  327. * buf Buffer to receive data
  328. * len Length of buffer
  329. * flags Receive flags
  330. * from Address of source (may be NULL)
  331. * fromlen The length of the address structure
  332. *
  333. * Returned Value:
  334. * On success, returns the number of characters received. If no data is
  335. * available to be received and the peer has performed an orderly shutdown,
  336. * recv() will return 0. Otherwise, on errors, a negated errno value is
  337. * returned (see recvfrom() for the list of appropriate error values).
  338. *
  339. ****************************************************************************/
  340. ssize_t icmpv6_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
  341. int flags, FAR struct sockaddr *from,
  342. FAR socklen_t *fromlen)
  343. {
  344. FAR struct sockaddr_in6 *inaddr;
  345. FAR struct icmpv6_conn_s *conn;
  346. FAR struct net_driver_s *dev;
  347. struct icmpv6_recvfrom_s state;
  348. ssize_t ret;
  349. /* Some sanity checks */
  350. DEBUGASSERT(psock != NULL && psock->s_conn != NULL && buf != NULL);
  351. if (len < ICMPv6_HDRLEN)
  352. {
  353. return -EINVAL;
  354. }
  355. /* If a 'from' address has been provided, verify that it is large
  356. * enough to hold the AF_INET address.
  357. */
  358. if (from != NULL)
  359. {
  360. if (fromlen == NULL && *fromlen < sizeof(struct sockaddr_in6))
  361. {
  362. return -EINVAL;
  363. }
  364. }
  365. /* We cannot receive a response from a device until a request has been
  366. * sent to the devivce.
  367. */
  368. conn = psock->s_conn;
  369. if (conn->nreqs < 1)
  370. {
  371. ret = -EPROTO;
  372. goto errout;
  373. }
  374. /* Check if there is buffered read-ahead data for this socket. We may have
  375. * already received the reponse to previous command.
  376. */
  377. if (!IOB_QEMPTY(&conn->readahead))
  378. {
  379. return icmpv6_readahead(conn, buf, len,
  380. (FAR struct sockaddr_in6 *)from, fromlen);
  381. }
  382. /* Initialize the state structure */
  383. memset(&state, 0, sizeof(struct icmpv6_recvfrom_s));
  384. /* This semaphore is used for signaling and, hence, should not have
  385. * priority inheritance enabled.
  386. */
  387. nxsem_init(&state.recv_sem, 0, 0);
  388. nxsem_setprotocol(&state.recv_sem, SEM_PRIO_NONE);
  389. state.recv_sock = psock; /* The IPPROTO_ICMP6 socket instance */
  390. state.recv_result = -ENOMEM; /* Assume allocation failure */
  391. state.recv_buf = buf; /* Location to return the response */
  392. state.recv_buflen = len; /* Size of the response */
  393. net_lock();
  394. state.recv_time = clock_systimer();
  395. /* Get the device that was used to send the ICMPv6 request. */
  396. dev = conn->dev;
  397. DEBUGASSERT(dev != NULL);
  398. if (dev == NULL)
  399. {
  400. ret = -EPROTO;
  401. goto errout;
  402. }
  403. /* Set up the callback */
  404. state.recv_cb = icmpv6_callback_alloc(dev);
  405. if (state.recv_cb)
  406. {
  407. state.recv_cb->flags = (ICMPv6_ECHOREPLY | NETDEV_DOWN);
  408. state.recv_cb->priv = (FAR void *)&state;
  409. state.recv_cb->event = recvfrom_eventhandler;
  410. state.recv_result = -EINTR; /* Assume sem-wait interrupted by signal */
  411. /* Wait for either the response to be received or for timeout to
  412. * occur. (1) net_lockedwait will also terminate if a signal is
  413. * received, (2) interrupts may be disabled! They will be re-enabled
  414. * while the task sleeps and automatically re-enabled when the task
  415. * restarts.
  416. */
  417. ninfo("Start time: 0x%08x\n", state.recv_time);
  418. net_lockedwait(&state.recv_sem);
  419. icmpv6_callback_free(dev, state.recv_cb);
  420. }
  421. net_unlock();
  422. /* Return the negated error number in the event of a failure, or the
  423. * number of bytes received on success.
  424. */
  425. if (state.recv_result < 0)
  426. {
  427. nerr("ERROR: Return error=%d\n", state.recv_result);
  428. ret = state.recv_result;
  429. goto errout;
  430. }
  431. if (from != NULL)
  432. {
  433. inaddr = (FAR struct sockaddr_in6 *)from;
  434. inaddr->sin6_family = AF_INET6;
  435. inaddr->sin6_port = 0;
  436. net_ipv6addr_copy(inaddr->sin6_addr.s6_addr16,
  437. state.recv_from.s6_addr16);
  438. }
  439. ret = state.recv_result;
  440. /* If there a no further outstanding requests, make sure that the request
  441. * struct is left pristine.
  442. */
  443. errout:
  444. if (conn->nreqs < 1)
  445. {
  446. conn->id = 0;
  447. conn->nreqs = 0;
  448. conn->dev = NULL;
  449. iob_free_queue(&conn->readahead);
  450. }
  451. return ret;
  452. }
  453. #endif /* CONFIG_NET_ICMPv6_SOCKET */