net_ipchksum.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /****************************************************************************
  2. * net/utils/net_ipchksum.c
  3. *
  4. * Copyright (C) 2007-2010, 2012, 2014-2015, 2017-2018 Gregory Nutt. All
  5. * rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdint.h>
  41. #include <assert.h>
  42. #include <nuttx/net/netconfig.h>
  43. #include <nuttx/net/netdev.h>
  44. #include <nuttx/net/ip.h>
  45. #include "utils/utils.h"
  46. #ifdef CONFIG_NET
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. #define IPv4BUF ((struct ipv4_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  51. #define IPv6BUF ((struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  52. /****************************************************************************
  53. * Public Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: ipv4_upperlayer_chksum
  57. *
  58. * Description:
  59. * Perform the checksum calculation over the IPv4, protocol headers, and
  60. * data payload as necessary.
  61. *
  62. * Input Parameters:
  63. * dev - The network driver instance. The packet data is in the d_buf
  64. * of the device.
  65. * proto - The protocol being supported
  66. *
  67. * Returned Value:
  68. * The calculated checksum
  69. *
  70. ****************************************************************************/
  71. #if !defined(CONFIG_NET_ARCH_CHKSUM) && defined(CONFIG_NET_IPv4)
  72. uint16_t ipv4_upperlayer_chksum(FAR struct net_driver_s *dev, uint8_t proto)
  73. {
  74. FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
  75. uint16_t upperlen;
  76. uint16_t sum;
  77. /* The length reported in the IPv4 header is the length of both the IPv4
  78. * header and the payload that follows the header. We need to subtract
  79. * the size of the IPv4 header to get the size of the payload.
  80. */
  81. upperlen = (((uint16_t)(ipv4->len[0]) << 8) + ipv4->len[1]) - IPv4_HDRLEN;
  82. /* Verify some minimal assumptions */
  83. if (upperlen > NETDEV_PKTSIZE(dev))
  84. {
  85. return 0;
  86. }
  87. /* First sum pseudo-header. */
  88. /* IP protocol and length fields. This addition cannot carry. */
  89. sum = upperlen + proto;
  90. /* Sum IP source and destination addresses. */
  91. sum = chksum(sum, (FAR uint8_t *)&ipv4->srcipaddr, 2 * sizeof(in_addr_t));
  92. /* Sum IP payload data. */
  93. sum = chksum(sum, &dev->d_buf[IPv4_HDRLEN + NET_LL_HDRLEN(dev)], upperlen);
  94. return (sum == 0) ? 0xffff : htons(sum);
  95. }
  96. #endif /* CONFIG_NET_ARCH_CHKSUM */
  97. /****************************************************************************
  98. * Name: ipv6_upperlayer_chksum
  99. *
  100. * Description:
  101. * Perform the checksum calculation over the IPv6, protocol headers, and
  102. * data payload as necessary.
  103. *
  104. * Input Parameters:
  105. * dev - The network driver instance. The packet data is in the d_buf
  106. * of the device.
  107. * proto - The protocol being supported
  108. * iplen - The size of the IPv6 header. This may be larger than
  109. * IPv6_HDRLEN the IPv6 header if IPv6 extension headers are
  110. * present.
  111. *
  112. * Returned Value:
  113. * The calculated checksum
  114. *
  115. ****************************************************************************/
  116. #if !defined(CONFIG_NET_ARCH_CHKSUM) && defined(CONFIG_NET_IPv6)
  117. uint16_t ipv6_upperlayer_chksum(FAR struct net_driver_s *dev,
  118. uint8_t proto, unsigned int iplen)
  119. {
  120. FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
  121. uint16_t upperlen;
  122. uint16_t sum;
  123. DEBUGASSERT(dev != NULL && iplen >= IPv6_HDRLEN);
  124. /* The length reported in the IPv6 header is the length of the payload
  125. * that follows the header. If extension heders are present, then this
  126. * size includes the size of the IPv6 extension headers.
  127. */
  128. upperlen = ((uint16_t)ipv6->len[0] << 8) + ipv6->len[1];
  129. /* Adjust for the presence of any extension headers */
  130. upperlen -= (iplen - IPv6_HDRLEN);
  131. /* Verify some minimal assumptions */
  132. if (upperlen > NETDEV_PKTSIZE(dev))
  133. {
  134. return 0;
  135. }
  136. /* The checksum is calculated starting with a pseudo-header of IPv6 header
  137. * fields according to the IPv6 standard, which consists of the source
  138. * and destination addresses, the packet length and the next header field.
  139. */
  140. sum = upperlen + proto;
  141. /* Sum IP source and destination addresses. */
  142. sum = chksum(sum, (FAR uint8_t *)&ipv6->srcipaddr, 2 * sizeof(net_ipv6addr_t));
  143. /* Sum IP payload data. */
  144. sum = chksum(sum, &dev->d_buf[NET_LL_HDRLEN(dev) + iplen], upperlen);
  145. return (sum == 0) ? 0xffff : htons(sum);
  146. }
  147. #endif /* CONFIG_NET_ARCH_CHKSUM */
  148. /****************************************************************************
  149. * Name: ipv4_chksum
  150. *
  151. * Description:
  152. * Calculate the IPv4 header checksum of the packet header in d_buf.
  153. *
  154. * The IPv4 header checksum is the Internet checksum of the 20 bytes of
  155. * the IPv4 header.
  156. *
  157. * If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
  158. * provided by architecture-specific logic.
  159. *
  160. * Returned Value:
  161. * The IPv4 header checksum of the IPv4 header in the d_buf buffer.
  162. *
  163. ****************************************************************************/
  164. #if defined(CONFIG_NET_IPv4) && !defined(CONFIG_NET_ARCH_CHKSUM)
  165. uint16_t ipv4_chksum(FAR struct net_driver_s *dev)
  166. {
  167. uint16_t sum;
  168. sum = chksum(0, &dev->d_buf[NET_LL_HDRLEN(dev)], IPv4_HDRLEN);
  169. return (sum == 0) ? 0xffff : htons(sum);
  170. }
  171. #endif /* CONFIG_NET_ARCH_CHKSUM */
  172. #endif /* CONFIG_NET */