sixlowpan_icmpv6send.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. * net/sixlowpan/sixlowpan_icmpv6send.c
  3. *
  4. * Copyright (C) 2017 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. #include <assert.h>
  40. #include <debug.h>
  41. #include <nuttx/net/netdev.h>
  42. #include <nuttx/net/radiodev.h>
  43. #include <nuttx/net/ip.h>
  44. #include "icmpv6/icmpv6.h"
  45. #include "sixlowpan/sixlowpan_internal.h"
  46. #include "sixlowpan/sixlowpan.h"
  47. #if defined(CONFIG_NET_6LOWPAN) && defined(CONFIG_NET_ICMPv6)
  48. /****************************************************************************
  49. * Public Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Name: sixlowpan_icmpv6_send
  53. *
  54. * Description:
  55. * Handles forwarding a ICMPv6 packet via 6LoWPAN. This is currently only
  56. * used by the IPv6 forwarding logic.
  57. *
  58. * Input Parameters:
  59. * dev - An instance of nework device state structure
  60. * fwddev - The network device used to send the data. This will be the
  61. * same device except for the IP forwarding case where packets
  62. * are sent across devices.
  63. * ipv6 - A pointer to the IPv6 header in dev->d_buf which lies AFTER
  64. * the L1 header. NOTE: dev->d_len must have been decremented
  65. * by the size of any preceding MAC header.
  66. *
  67. * Returned Value:
  68. * None
  69. *
  70. * Assumptions:
  71. * Called with the network locked.
  72. *
  73. ****************************************************************************/
  74. void sixlowpan_icmpv6_send(FAR struct net_driver_s *dev,
  75. FAR struct net_driver_s *fwddev,
  76. FAR struct ipv6_hdr_s *ipv6)
  77. {
  78. FAR struct ipv6icmp_hdr_s *ipv6icmpv6 = (FAR struct ipv6icmp_hdr_s *)ipv6;
  79. /* Double check */
  80. DEBUGASSERT(dev != NULL && dev->d_len > 0 && fwddev != NULL);
  81. ninfo("d_len %u\n", dev->d_len);
  82. if (dev != NULL && dev->d_len > 0)
  83. {
  84. sixlowpan_dumpbuffer("Outgoing ICMPv6 packet",
  85. (FAR const uint8_t *)ipv6icmpv6, dev->d_len);
  86. /* The ICMPv6 data payload should follow the IPv6 header plus the
  87. * protocol header.
  88. */
  89. if (ipv6icmpv6->ipv6.proto != IP_PROTO_ICMP6)
  90. {
  91. nwarn("WARNING: Expected ICMPv6 protoype: %u vs %s\n",
  92. ipv6icmpv6->ipv6.proto, IP_PROTO_ICMP6);
  93. }
  94. else
  95. {
  96. struct netdev_varaddr_s destmac;
  97. FAR uint8_t *buf;
  98. uint16_t hdrlen;
  99. uint16_t buflen;
  100. int ret;
  101. /* Get the IEEE 802.15.4 MAC address of the next hop. */
  102. ret = sixlowpan_nexthopaddr((FAR struct radio_driver_s *)fwddev,
  103. ipv6icmpv6->ipv6.destipaddr, &destmac);
  104. if (ret < 0)
  105. {
  106. nerr("ERROR: Failed to get dest MAC address: %d\n", ret);
  107. goto drop;
  108. }
  109. /* Get the IPv6 + ICMPv6 combined header length. NOTE: This header
  110. * size includes only the common 32-bit header at the beginning of
  111. * each ICMPv6 message.
  112. */
  113. hdrlen = IPv6_HDRLEN + ICMPv6_HDRLEN;
  114. /* Drop the packet if the buffer length is less than this. */
  115. if (hdrlen > dev->d_len)
  116. {
  117. nwarn("WARNING: Dropping small ICMPv6 packet: %u < %u\n",
  118. buflen, hdrlen);
  119. }
  120. else
  121. {
  122. /* Convert the outgoing packet into a frame list. */
  123. buf = (FAR uint8_t *)ipv6 + hdrlen;
  124. buflen = dev->d_len - hdrlen;
  125. (void)sixlowpan_queue_frames(
  126. (FAR struct radio_driver_s *)fwddev,
  127. &ipv6icmpv6->ipv6, buf, buflen, &destmac);
  128. }
  129. }
  130. }
  131. drop:
  132. dev->d_len = 0;
  133. }
  134. #endif /* CONFIG_NET_6LOWPAN && CONFIG_NET_ICMPv6 */