icmpv6_conn.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_conn.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <debug.h>
  28. #include <arch/irq.h>
  29. #include <nuttx/semaphore.h>
  30. #include <nuttx/net/netconfig.h>
  31. #include <nuttx/net/net.h>
  32. #include <nuttx/net/netdev.h>
  33. #include "devif/devif.h"
  34. #include "icmpv6/icmpv6.h"
  35. #ifdef CONFIG_NET_ICMPv6_SOCKET
  36. /****************************************************************************
  37. * Private Data
  38. ****************************************************************************/
  39. /* The array containing all IPPROTO_ICMP socket connections */
  40. static struct icmpv6_conn_s g_icmpv6_connections[CONFIG_NET_ICMPv6_NCONNS];
  41. /* A list of all free IPPROTO_ICMP socket connections */
  42. static dq_queue_t g_free_icmpv6_connections;
  43. static sem_t g_free_sem;
  44. /* A list of all allocated IPPROTO_ICMP socket connections */
  45. static dq_queue_t g_active_icmpv6_connections;
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: icmpv6_sock_initialize
  51. *
  52. * Description:
  53. * Initialize the IPPROTO_ICMP socket connection structures. Called once
  54. * and only from the network initialization layer.
  55. *
  56. ****************************************************************************/
  57. void icmpv6_sock_initialize(void)
  58. {
  59. int i;
  60. /* Initialize the queues */
  61. dq_init(&g_free_icmpv6_connections);
  62. dq_init(&g_active_icmpv6_connections);
  63. nxsem_init(&g_free_sem, 0, 1);
  64. for (i = 0; i < CONFIG_NET_ICMPv6_NCONNS; i++)
  65. {
  66. /* Move the connection structure to the free list */
  67. dq_addlast(&g_icmpv6_connections[i].node, &g_free_icmpv6_connections);
  68. }
  69. }
  70. /****************************************************************************
  71. * Name: icmpv6_alloc
  72. *
  73. * Description:
  74. * Allocate a new, uninitialized IPPROTO_ICMP socket connection structure.
  75. * This is normally something done by the implementation of the socket()
  76. * interface.
  77. *
  78. ****************************************************************************/
  79. FAR struct icmpv6_conn_s *icmpv6_alloc(void)
  80. {
  81. FAR struct icmpv6_conn_s *conn = NULL;
  82. int ret;
  83. /* The free list is protected by a semaphore (that behaves like a mutex). */
  84. ret = net_lockedwait(&g_free_sem);
  85. if (ret >= 0)
  86. {
  87. conn = (FAR struct icmpv6_conn_s *)
  88. dq_remfirst(&g_free_icmpv6_connections);
  89. if (conn != NULL)
  90. {
  91. /* Clear the connection structure */
  92. memset(conn, 0, sizeof(struct icmpv6_conn_s));
  93. /* Enqueue the connection into the active list */
  94. dq_addlast(&conn->node, &g_active_icmpv6_connections);
  95. }
  96. nxsem_post(&g_free_sem);
  97. }
  98. return conn;
  99. }
  100. /****************************************************************************
  101. * Name: icmpv6_free
  102. *
  103. * Description:
  104. * Free a IPPROTO_ICMP socket connection structure that is no longer in
  105. * use. This should be done by the implementation of close().
  106. *
  107. ****************************************************************************/
  108. void icmpv6_free(FAR struct icmpv6_conn_s *conn)
  109. {
  110. /* The free list is protected by a semaphore (that behaves like a mutex). */
  111. DEBUGASSERT(conn->crefs == 0);
  112. /* Take the semaphore (perhaps waiting) */
  113. net_lockedwait_uninterruptible(&g_free_sem);
  114. /* Remove the connection from the active list */
  115. dq_rem(&conn->node, &g_active_icmpv6_connections);
  116. /* Free the connection */
  117. dq_addlast(&conn->node, &g_free_icmpv6_connections);
  118. nxsem_post(&g_free_sem);
  119. }
  120. /****************************************************************************
  121. * Name: icmpv6_active()
  122. *
  123. * Description:
  124. * Find a connection structure that is the appropriate connection to be
  125. * used with the provided ECHO request ID.
  126. *
  127. * Assumptions:
  128. * This function is called from network logic at with the network locked.
  129. *
  130. ****************************************************************************/
  131. FAR struct icmpv6_conn_s *icmpv6_active(uint16_t id)
  132. {
  133. FAR struct icmpv6_conn_s *conn =
  134. (FAR struct icmpv6_conn_s *)g_active_icmpv6_connections.head;
  135. while (conn != NULL)
  136. {
  137. /* FIXME lmac in conn should have been set by icmpv6_bind() */
  138. if (id == conn->id)
  139. {
  140. /* Matching connection found.. return a reference to it */
  141. break;
  142. }
  143. /* Look at the next active connection */
  144. conn = (FAR struct icmpv6_conn_s *)conn->node.flink;
  145. }
  146. return conn;
  147. }
  148. /****************************************************************************
  149. * Name: icmpv6_nextconn
  150. *
  151. * Description:
  152. * Traverse the list of allocated packet connections
  153. *
  154. * Assumptions:
  155. * This function is called from network logic at with the network locked.
  156. *
  157. ****************************************************************************/
  158. FAR struct icmpv6_conn_s *icmpv6_nextconn(FAR struct icmpv6_conn_s *conn)
  159. {
  160. if (conn == NULL)
  161. {
  162. return (FAR struct icmpv6_conn_s *)g_active_icmpv6_connections.head;
  163. }
  164. else
  165. {
  166. return (FAR struct icmpv6_conn_s *)conn->node.flink;
  167. }
  168. }
  169. /****************************************************************************
  170. * Name: icmpv6_findconn
  171. *
  172. * Description:
  173. * Find an ICMPv6 connection structure that is expecting a ICMPv6 ECHO
  174. * response with this ID from this device
  175. *
  176. * Assumptions:
  177. * This function is called from network logic at with the network locked.
  178. *
  179. ****************************************************************************/
  180. FAR struct icmpv6_conn_s *icmpv6_findconn(FAR struct net_driver_s *dev,
  181. uint16_t id)
  182. {
  183. FAR struct icmpv6_conn_s *conn;
  184. for (conn = icmpv6_nextconn(NULL); conn != NULL;
  185. conn = icmpv6_nextconn(conn))
  186. {
  187. if (conn->id == id && conn->dev == dev && conn->nreqs > 0)
  188. {
  189. return conn;
  190. }
  191. }
  192. return conn;
  193. }
  194. #endif /* CONFIG_NET_ICMP */