udp_callback.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /****************************************************************************
  2. * net/udp/udp_callback.c
  3. *
  4. * Copyright (C) 2007-2009, 2015 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. #if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
  40. #include <stdint.h>
  41. #include <string.h>
  42. #include <debug.h>
  43. #include <nuttx/net/netconfig.h>
  44. #include <nuttx/net/netdev.h>
  45. #include <nuttx/net/netstats.h>
  46. #include <nuttx/net/udp.h>
  47. #include "devif/devif.h"
  48. #include "udp/udp.h"
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. #define IPv4BUF ((FAR struct ipv4_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  53. #define IPv6BUF ((FAR struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  54. #define UDPIPv4BUF ((FAR struct udp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv4_HDRLEN])
  55. #define UDPIPv6BUF ((FAR struct udp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: udp_datahandler
  61. *
  62. * Description:
  63. * Handle the receipt of UDP data by adding the newly received packet to
  64. * the UDP read-ahead buffer.
  65. *
  66. ****************************************************************************/
  67. #ifdef CONFIG_NET_UDP_READAHEAD
  68. static uint16_t udp_datahandler(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn,
  69. FAR uint8_t *buffer, uint16_t buflen)
  70. {
  71. FAR struct iob_s *iob;
  72. int ret;
  73. #ifdef CONFIG_NET_IPv6
  74. FAR struct sockaddr_in6 src_addr6;
  75. #endif
  76. #ifdef CONFIG_NET_IPv4
  77. FAR struct sockaddr_in src_addr4;
  78. #endif
  79. FAR void *src_addr;
  80. uint8_t src_addr_size;
  81. /* Allocate on I/O buffer to start the chain (throttling as necessary).
  82. * We will not wait for an I/O buffer to become available in this context.
  83. */
  84. iob = iob_tryalloc(true);
  85. if (iob == NULL)
  86. {
  87. nerr("ERROR: Failed to create new I/O buffer chain\n");
  88. return 0;
  89. }
  90. #ifdef CONFIG_NET_IPv6
  91. #ifdef CONFIG_NET_IPv4
  92. if (IFF_IS_IPv6(dev->d_flags))
  93. #endif
  94. {
  95. FAR struct udp_hdr_s *udp = UDPIPv6BUF;
  96. FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
  97. src_addr6.sin6_family = AF_INET6;
  98. src_addr6.sin6_port = udp->srcport;
  99. net_ipv6addr_copy(src_addr6.sin6_addr.s6_addr, ipv6->srcipaddr);
  100. src_addr_size = sizeof(src_addr6);
  101. src_addr = &src_addr6;
  102. }
  103. #endif /* CONFIG_NET_IPv6 */
  104. #ifdef CONFIG_NET_IPv4
  105. #ifdef CONFIG_NET_IPv6
  106. else
  107. #endif
  108. {
  109. #ifdef CONFIG_NET_IPv6
  110. /* Hybrid dual-stack IPv6/IPv4 implementations recognize a special
  111. * class of addresses, the IPv4-mapped IPv6 addresses.
  112. */
  113. if (conn->domain == PF_INET6)
  114. {
  115. FAR struct udp_hdr_s *udp = UDPIPv6BUF;
  116. FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
  117. in_addr_t ipv4addr;
  118. /* Encode the IPv4 address as an IPv-mapped IPv6 address */
  119. src_addr6.sin6_family = AF_INET6;
  120. src_addr6.sin6_port = udp->srcport;
  121. ipv4addr = net_ip4addr_conv32(ipv6->srcipaddr);
  122. ip6_map_ipv4addr(ipv4addr, src_addr6.sin6_addr.s6_addr16);
  123. src_addr_size = sizeof(src_addr6);
  124. src_addr = &src_addr6;
  125. }
  126. else
  127. #endif
  128. {
  129. FAR struct udp_hdr_s *udp = UDPIPv4BUF;
  130. FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
  131. src_addr4.sin_family = AF_INET;
  132. src_addr4.sin_port = udp->srcport;
  133. net_ipv4addr_copy(src_addr4.sin_addr.s_addr,
  134. net_ip4addr_conv32(ipv4->srcipaddr));
  135. src_addr_size = sizeof(src_addr4);
  136. src_addr = &src_addr4;
  137. }
  138. }
  139. #endif /* CONFIG_NET_IPv4 */
  140. /* Copy the src address info into the I/O buffer chain. We will not wait
  141. * for an I/O buffer to become available in this context. It there is
  142. * any failure to allocated, the entire I/O buffer chain will be discarded.
  143. */
  144. ret = iob_trycopyin(iob, (FAR const uint8_t *)&src_addr_size,
  145. sizeof(uint8_t), 0, true);
  146. if (ret < 0)
  147. {
  148. /* On a failure, iob_trycopyin return a negated error value but does
  149. * not free any I/O buffers.
  150. */
  151. nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
  152. (void)iob_free_chain(iob);
  153. return 0;
  154. }
  155. ret = iob_trycopyin(iob, (FAR const uint8_t *)src_addr, src_addr_size,
  156. sizeof(uint8_t), true);
  157. if (ret < 0)
  158. {
  159. /* On a failure, iob_trycopyin return a negated error value but does
  160. * not free any I/O buffers.
  161. */
  162. nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
  163. (void)iob_free_chain(iob);
  164. return 0;
  165. }
  166. if (buflen > 0)
  167. {
  168. /* Copy the new appdata into the I/O buffer chain */
  169. ret = iob_trycopyin(iob, buffer, buflen,
  170. src_addr_size + sizeof(uint8_t), true);
  171. if (ret < 0)
  172. {
  173. /* On a failure, iob_trycopyin return a negated error value but
  174. * does not free any I/O buffers.
  175. */
  176. nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n",
  177. ret);
  178. (void)iob_free_chain(iob);
  179. return 0;
  180. }
  181. }
  182. /* Add the new I/O buffer chain to the tail of the read-ahead queue */
  183. ret = iob_tryadd_queue(iob, &conn->readahead);
  184. if (ret < 0)
  185. {
  186. nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
  187. (void)iob_free_chain(iob);
  188. return 0;
  189. }
  190. #ifdef CONFIG_UDP_READAHEAD_NOTIFIER
  191. /* Provided notification(s) that additional UDP read-ahead data is
  192. * available.
  193. */
  194. udp_notifier_signal(conn);
  195. #endif
  196. ninfo("Buffered %d bytes\n", buflen);
  197. return buflen;
  198. }
  199. #endif /* CONFIG_NET_UDP_READAHEAD */
  200. /****************************************************************************
  201. * Name: net_dataevent
  202. *
  203. * Description:
  204. * Handling the network UDP_NEWDATA event.
  205. *
  206. ****************************************************************************/
  207. static inline uint16_t
  208. net_dataevent(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn,
  209. uint16_t flags)
  210. {
  211. uint16_t ret;
  212. #ifdef CONFIG_NET_UDP_READAHEAD
  213. uint8_t *buffer = dev->d_appdata;
  214. int buflen = dev->d_len;
  215. uint16_t recvlen;
  216. #endif
  217. ret = (flags & ~UDP_NEWDATA);
  218. /* Is there new data? With non-zero length? (Certain connection events
  219. * can have zero-length with UDP_NEWDATA set just to cause an ACK).
  220. */
  221. ninfo("No receive on connection\n");
  222. #ifdef CONFIG_NET_UDP_READAHEAD
  223. /* Save as the packet data as in the read-ahead buffer. NOTE that
  224. * partial packets will not be buffered.
  225. */
  226. recvlen = udp_datahandler(dev, conn, buffer, buflen);
  227. if (recvlen < buflen)
  228. #endif
  229. {
  230. /* There is no handler to receive new data and there are no free
  231. * read-ahead buffers to retain the data -- drop the packet.
  232. */
  233. ninfo("Dropped %d bytes\n", dev->d_len);
  234. #ifdef CONFIG_NET_STATISTICS
  235. g_netstats.udp.drop++;
  236. #endif
  237. }
  238. /* In any event, the new data has now been handled */
  239. dev->d_len = 0;
  240. return ret;
  241. }
  242. /****************************************************************************
  243. * Public Functions
  244. ****************************************************************************/
  245. /****************************************************************************
  246. * Name: udp_callback
  247. *
  248. * Description:
  249. * Inform the application holding the UDP socket of a change in state.
  250. *
  251. * Returned Value:
  252. * OK if packet has been processed, otherwise ERROR.
  253. *
  254. * Assumptions:
  255. * This function must be called with the network locked.
  256. *
  257. ****************************************************************************/
  258. uint16_t udp_callback(FAR struct net_driver_s *dev,
  259. FAR struct udp_conn_s *conn, uint16_t flags)
  260. {
  261. ninfo("flags: %04x\n", flags);
  262. /* Some sanity checking */
  263. if (conn)
  264. {
  265. /* Perform the callback */
  266. flags = devif_conn_event(dev, conn, flags, conn->list);
  267. if ((flags & UDP_NEWDATA) != 0)
  268. {
  269. /* Data was not handled.. dispose of it appropriately */
  270. flags = net_dataevent(dev, conn, flags);
  271. }
  272. }
  273. return flags;
  274. }
  275. #endif /* CONFIG_NET && CONFIG_NET_UDP */