icmpv6_conn.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /****************************************************************************
  2. * net/icmp/icmpv6_conn.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 the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote
  17. * products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  21. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ****************************************************************************/
  33. /****************************************************************************
  34. * Included Files
  35. ****************************************************************************/
  36. #include <nuttx/config.h>
  37. #include <string.h>
  38. #include <assert.h>
  39. #include <errno.h>
  40. #include <debug.h>
  41. #include <arch/irq.h>
  42. #include <nuttx/semaphore.h>
  43. #include <nuttx/net/netconfig.h>
  44. #include <nuttx/net/net.h>
  45. #include <nuttx/net/netdev.h>
  46. #include "devif/devif.h"
  47. #include "icmpv6/icmpv6.h"
  48. #ifdef CONFIG_NET_ICMPv6_SOCKET
  49. /****************************************************************************
  50. * Private Data
  51. ****************************************************************************/
  52. /* The array containing all IPPROTO_ICMP socket connections */
  53. static struct icmpv6_conn_s g_icmpv6_connections[CONFIG_NET_ICMPv6_NCONNS];
  54. /* A list of all free IPPROTO_ICMP socket connections */
  55. static dq_queue_t g_free_icmpv6_connections;
  56. static sem_t g_free_sem;
  57. /* A list of all allocated IPPROTO_ICMP socket connections */
  58. static dq_queue_t g_active_icmpv6_connections;
  59. /****************************************************************************
  60. * Public Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Name: icmpv6_sock_initialize
  64. *
  65. * Description:
  66. * Initialize the IPPROTO_ICMP socket connection structures. Called once
  67. * and only from the network initialization layer.
  68. *
  69. ****************************************************************************/
  70. void icmpv6_sock_initialize(void)
  71. {
  72. int i;
  73. /* Initialize the queues */
  74. dq_init(&g_free_icmpv6_connections);
  75. dq_init(&g_active_icmpv6_connections);
  76. nxsem_init(&g_free_sem, 0, 1);
  77. for (i = 0; i < CONFIG_NET_ICMPv6_NCONNS; i++)
  78. {
  79. /* Move the connection structure to the free list */
  80. dq_addlast(&g_icmpv6_connections[i].node, &g_free_icmpv6_connections);
  81. }
  82. }
  83. /****************************************************************************
  84. * Name: icmpv6_alloc
  85. *
  86. * Description:
  87. * Allocate a new, uninitialized IPPROTO_ICMP socket connection structure.
  88. * This is normally something done by the implementation of the socket()
  89. * interface.
  90. *
  91. ****************************************************************************/
  92. FAR struct icmpv6_conn_s *icmpv6_alloc(void)
  93. {
  94. FAR struct icmpv6_conn_s *conn = NULL;
  95. int ret;
  96. /* The free list is protected by a semaphore (that behaves like a mutex). */
  97. ret = net_lockedwait(&g_free_sem);
  98. if (ret >= 0)
  99. {
  100. conn = (FAR struct icmpv6_conn_s *)dq_remfirst(&g_free_icmpv6_connections);
  101. if (conn != NULL)
  102. {
  103. /* Clear the connection structure */
  104. memset(conn, 0, sizeof(struct icmpv6_conn_s));
  105. /* Enqueue the connection into the active list */
  106. dq_addlast(&conn->node, &g_active_icmpv6_connections);
  107. }
  108. nxsem_post(&g_free_sem);
  109. }
  110. return conn;
  111. }
  112. /****************************************************************************
  113. * Name: icmpv6_free
  114. *
  115. * Description:
  116. * Free a IPPROTO_ICMP socket connection structure that is no longer in
  117. * use. This should be done by the implementation of close().
  118. *
  119. ****************************************************************************/
  120. void icmpv6_free(FAR struct icmpv6_conn_s *conn)
  121. {
  122. int ret;
  123. /* The free list is protected by a semaphore (that behaves like a mutex). */
  124. DEBUGASSERT(conn->crefs == 0);
  125. /* Take the semaphore (perhaps waiting) */
  126. while ((ret = net_lockedwait(&g_free_sem)) < 0)
  127. {
  128. /* The only case that an error should occur here is if
  129. * the wait was awakened by a signal.
  130. */
  131. DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
  132. }
  133. UNUSED(ret);
  134. /* Remove the connection from the active list */
  135. dq_rem(&conn->node, &g_active_icmpv6_connections);
  136. /* Free the connection */
  137. dq_addlast(&conn->node, &g_free_icmpv6_connections);
  138. nxsem_post(&g_free_sem);
  139. }
  140. /****************************************************************************
  141. * Name: icmpv6_active()
  142. *
  143. * Description:
  144. * Find a connection structure that is the appropriate connection to be
  145. * used with the provided ECHO request ID.
  146. *
  147. * Assumptions:
  148. * This function is called from network logic at with the network locked.
  149. *
  150. ****************************************************************************/
  151. FAR struct icmpv6_conn_s *icmpv6_active(uint16_t id)
  152. {
  153. FAR struct icmpv6_conn_s *conn =
  154. (FAR struct icmpv6_conn_s *)g_active_icmpv6_connections.head;
  155. while (conn != NULL)
  156. {
  157. /* FIXME lmac in conn should have been set by icmpv6_bind() */
  158. if (id == conn->id)
  159. {
  160. /* Matching connection found.. return a reference to it */
  161. break;
  162. }
  163. /* Look at the next active connection */
  164. conn = (FAR struct icmpv6_conn_s *)conn->node.flink;
  165. }
  166. return conn;
  167. }
  168. /****************************************************************************
  169. * Name: icmpv6_nextconn
  170. *
  171. * Description:
  172. * Traverse the list of allocated packet connections
  173. *
  174. * Assumptions:
  175. * This function is called from network logic at with the network locked.
  176. *
  177. ****************************************************************************/
  178. FAR struct icmpv6_conn_s *icmpv6_nextconn(FAR struct icmpv6_conn_s *conn)
  179. {
  180. if (conn == NULL)
  181. {
  182. return (FAR struct icmpv6_conn_s *)g_active_icmpv6_connections.head;
  183. }
  184. else
  185. {
  186. return (FAR struct icmpv6_conn_s *)conn->node.flink;
  187. }
  188. }
  189. /****************************************************************************
  190. * Name: icmpv6_findconn
  191. *
  192. * Description:
  193. * Find an ICMPv6 connection structure that is expecting a ICMPv6 ECHO response
  194. * with this ID from this device
  195. *
  196. * Assumptions:
  197. * This function is called from network logic at with the network locked.
  198. *
  199. ****************************************************************************/
  200. FAR struct icmpv6_conn_s *icmpv6_findconn(FAR struct net_driver_s *dev,
  201. uint16_t id)
  202. {
  203. FAR struct icmpv6_conn_s *conn;
  204. for (conn = icmpv6_nextconn(NULL); conn != NULL; conn = icmpv6_nextconn(conn))
  205. {
  206. if (conn->id == id && conn->dev == dev && conn->nreqs > 0)
  207. {
  208. return conn;
  209. }
  210. }
  211. return conn;
  212. }
  213. #endif /* CONFIG_NET_ICMP */