udp_callback.c 10 KB

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