udp_input.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /****************************************************************************
  2. * net/udp/udp_input.c
  3. * Handling incoming UDP input
  4. *
  5. * Copyright (C) 2007-2009, 2011, 2018 Gregory Nutt. All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Adapted for NuttX from logic in uIP which also has a BSD-like license:
  9. *
  10. * Original author Adam Dunkels <adam@dunkels.com>
  11. * Copyright () 2001-2003, Adam Dunkels.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. The name of the author may not be used to endorse or promote
  24. * products derived from this software without specific prior
  25. * written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  28. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  35. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ****************************************************************************/
  40. /****************************************************************************
  41. * Included Files
  42. ****************************************************************************/
  43. #include <nuttx/config.h>
  44. #if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
  45. #include <debug.h>
  46. #include <nuttx/net/netconfig.h>
  47. #include <nuttx/net/netdev.h>
  48. #include <nuttx/net/udp.h>
  49. #include <nuttx/net/netstats.h>
  50. #include "devif/devif.h"
  51. #include "utils/utils.h"
  52. #include "udp/udp.h"
  53. /****************************************************************************
  54. * Private Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: udp_input
  58. *
  59. * Description:
  60. * Handle incoming UDP input
  61. *
  62. * Input Parameters:
  63. * dev - The device driver structure containing the received UDP packet
  64. * udp - A pointer to the UDP header in the packet
  65. * iplen - Length of the IP and UDP headers
  66. *
  67. * Returned Value:
  68. * OK - The packet has been processed and can be deleted
  69. * ERROR - Hold the packet and try again later. There is a listening
  70. * socket but no receive in place to catch the packet yet. The
  71. * device's d_len will be set to zero in this case as there is
  72. * no outgoing data.
  73. *
  74. * Assumptions:
  75. * The network is locked.
  76. *
  77. ****************************************************************************/
  78. static int udp_input(FAR struct net_driver_s *dev, unsigned int iplen)
  79. {
  80. FAR struct udp_hdr_s *udp;
  81. FAR struct udp_conn_s *conn;
  82. unsigned int udpiplen;
  83. unsigned int hdrlen;
  84. #ifdef CONFIG_NET_UDP_CHECKSUMS
  85. uint16_t chksum;
  86. #endif
  87. int ret = OK;
  88. /* Update the count of UDP packets received */
  89. #ifdef CONFIG_NET_STATISTICS
  90. g_netstats.udp.recv++;
  91. #endif
  92. /* Get a pointer to the UDP header. The UDP header lies just after the
  93. * the link layer header and the IP header.
  94. */
  95. udp = (FAR struct udp_hdr_s *)&dev->d_buf[iplen + NET_LL_HDRLEN(dev)];
  96. /* Get the size of the IP header and the UDP header */
  97. udpiplen = iplen + UDP_HDRLEN;
  98. /* Get the size of the link layer header, the IP header, and the UDP header */
  99. hdrlen = udpiplen + NET_LL_HDRLEN(dev);
  100. /* UDP processing is really just a hack. We don't do anything to the UDP/IP
  101. * headers, but let the UDP application do all the hard work. If the
  102. * application sets d_sndlen, it has a packet to send.
  103. */
  104. dev->d_len -= udpiplen;
  105. dev->d_appdata = &dev->d_buf[hdrlen];
  106. #ifdef CONFIG_NET_UDP_CHECKSUMS
  107. chksum = udp->udpchksum;
  108. if (chksum != 0)
  109. {
  110. #ifdef CONFIG_NET_IPv6
  111. #ifdef CONFIG_NET_IPv4
  112. if (IFF_IS_IPv6(dev->d_flags))
  113. #endif
  114. {
  115. chksum = ~udp_ipv6_chksum(dev);
  116. }
  117. #endif /* CONFIG_NET_IPv6 */
  118. #ifdef CONFIG_NET_IPv4
  119. #ifdef CONFIG_NET_IPv6
  120. else
  121. #endif
  122. {
  123. chksum = ~udp_ipv4_chksum(dev);
  124. }
  125. #endif /* CONFIG_NET_IPv6 */
  126. }
  127. if (chksum != 0)
  128. {
  129. #ifdef CONFIG_NET_STATISTICS
  130. g_netstats.udp.drop++;
  131. g_netstats.udp.chkerr++;
  132. #endif
  133. nwarn("WARNING: Bad UDP checksum\n");
  134. dev->d_len = 0;
  135. }
  136. else
  137. #endif
  138. {
  139. /* Demultiplex this UDP packet between the UDP "connections".
  140. *
  141. * REVISIT: The logic here expects either a single receive socket or
  142. * none at all. However, multiple sockets should be capable of
  143. * receiving a UDP datagram (multicast reception). This could be
  144. * handled easily by something like:
  145. *
  146. * for (conn = NULL; conn = udp_active(dev, udp); )
  147. *
  148. * If the callback logic that receives a packet responds with an
  149. * outgoing packet, then it will over-write the received buffer,
  150. * however. recvfrom() will not do that, however. We would have to
  151. * make that the rule: Recipients of a UDP packet must treat the
  152. * packet as read-only.
  153. */
  154. conn = udp_active(dev, udp);
  155. if (conn)
  156. {
  157. uint16_t flags;
  158. /* Set-up for the application callback */
  159. dev->d_appdata = &dev->d_buf[hdrlen];
  160. dev->d_sndlen = 0;
  161. /* Perform the application callback */
  162. flags = udp_callback(dev, conn, UDP_NEWDATA);
  163. /* If the operation was successful and the UDP data was "consumed,"
  164. * then the UDP_NEWDATA flag will be cleared by logic in
  165. * udp_callback(). The packet memory can then be freed by the
  166. * network driver. OK will be returned to the network driver to
  167. * indicate this case.
  168. *
  169. * "Consumed" here means that either the received data was (1)
  170. * accepted by a socket waiting for data on the port or was (2)
  171. * buffered in the UDP socket's read-ahead buffer.
  172. */
  173. if ((flags & UDP_NEWDATA) != 0)
  174. {
  175. /* No.. the packet was not processed now. Return ERROR so
  176. * that the driver may retry again later. We still need to
  177. * set d_len to zero so that the driver is aware that there
  178. * is nothing to be sent.
  179. */
  180. nwarn("WARNING: Packet not processed\n");
  181. dev->d_len = 0;
  182. ret = ERROR;
  183. }
  184. /* If the application has data to send, setup the UDP/IP header */
  185. if (dev->d_sndlen > 0)
  186. {
  187. udp_send(dev, conn);
  188. }
  189. }
  190. else
  191. {
  192. nwarn("WARNING: No listener on UDP port\n");
  193. dev->d_len = 0;
  194. }
  195. }
  196. return ret;
  197. }
  198. /****************************************************************************
  199. * Public Functions
  200. ****************************************************************************/
  201. /****************************************************************************
  202. * Name: udp_ipv4_input
  203. *
  204. * Description:
  205. * Handle incoming UDP input in an IPv4 packet
  206. *
  207. * Input Parameters:
  208. * dev - The device driver structure containing the received UDP packet
  209. *
  210. * Returned Value:
  211. * OK The packet has been processed and can be deleted
  212. * ERROR Hold the packet and try again later. There is a listening socket
  213. * but no receive in place to catch the packet yet.
  214. *
  215. * Assumptions:
  216. * Called from network stack logic with the network stack locked
  217. *
  218. ****************************************************************************/
  219. #ifdef CONFIG_NET_IPv4
  220. int udp_ipv4_input(FAR struct net_driver_s *dev)
  221. {
  222. /* Configure to receive an UDP IPv4 packet */
  223. udp_ipv4_select(dev);
  224. /* Then process in the UDP IPv4 input */
  225. return udp_input(dev, IPv4_HDRLEN);
  226. }
  227. #endif
  228. /****************************************************************************
  229. * Name: udp_ipv6_input
  230. *
  231. * Description:
  232. * Handle incoming UDP input in an IPv6 packet
  233. *
  234. * Input Parameters:
  235. * dev - The device driver structure containing the received UDP packet
  236. * iplen - The size of the IPv6 header. This may be larger than
  237. * IPv6_HDRLEN the IPv6 header if IPv6 extension headers are
  238. * present.
  239. *
  240. * Returned Value:
  241. * OK The packet has been processed and can be deleted
  242. * ERROR Hold the packet and try again later. There is a listening socket
  243. * but no receive in place to catch the packet yet.
  244. *
  245. * Assumptions:
  246. * Called from network stack logic with the network stack locked
  247. *
  248. ****************************************************************************/
  249. #ifdef CONFIG_NET_IPv6
  250. int udp_ipv6_input(FAR struct net_driver_s *dev, unsigned int iplen)
  251. {
  252. /* Configure to receive an UDP IPv6 packet */
  253. udp_ipv6_select(dev);
  254. /* Then process in the UDP IPv6 input */
  255. return udp_input(dev, iplen);
  256. }
  257. #endif
  258. #endif /* CONFIG_NET && CONFIG_NET_UDP */