icmpv6_solicit.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_solicit.c
  3. *
  4. * Copyright (C) 2015, 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 <stdint.h>
  40. #include <string.h>
  41. #include <debug.h>
  42. #include <nuttx/net/net.h>
  43. #include <nuttx/net/netdev.h>
  44. #include <nuttx/net/netstats.h>
  45. #include "devif/devif.h"
  46. #include "netdev/netdev.h"
  47. #include "utils/utils.h"
  48. #include "icmpv6/icmpv6.h"
  49. #ifdef CONFIG_NET_ICMPv6
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. #define IPv6BUF ((struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  54. #define ICMPv6SOLICIT \
  55. ((struct icmpv6_neighbor_solicit_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
  56. /****************************************************************************
  57. * Private Data
  58. ****************************************************************************/
  59. /* First 6 hwords of the multi-cast address in network byte order */
  60. static const uint16_t g_icmpv_mcastaddr[6] =
  61. {
  62. HTONS(0xff02), HTONS(0x0000), HTONS(0x0000), HTONS(0x0000),
  63. HTONS(0x0000), HTONS(0x0001)
  64. };
  65. /****************************************************************************
  66. * Public Functions
  67. ****************************************************************************/
  68. /****************************************************************************
  69. * Name: icmpv6_solicit
  70. *
  71. * Description:
  72. * Set up to send an ICMPv6 Neighbor Solicitation message. This version
  73. * is for a standalone solicitation. If formats:
  74. *
  75. * - The IPv6 header
  76. * - The ICMPv6 Neighbor Solicitation Message
  77. *
  78. * Input Parameters:
  79. * dev - Reference to a device driver structure
  80. * ipaddr - IP address of Neighbor to be solicited
  81. *
  82. * Returned Value:
  83. * None
  84. *
  85. ****************************************************************************/
  86. void icmpv6_solicit(FAR struct net_driver_s *dev,
  87. FAR const net_ipv6addr_t ipaddr)
  88. {
  89. FAR struct ipv6_hdr_s *ipv6;
  90. FAR struct icmpv6_neighbor_solicit_s *sol;
  91. uint16_t lladdrsize;
  92. uint16_t l3size;
  93. /* Set up the IPv6 header (most is probably already in place) */
  94. ipv6 = IPv6BUF;
  95. ipv6->vtc = 0x60; /* Version/traffic class (MS) */
  96. ipv6->tcf = 0; /* Traffic class (LS)/Flow label (MS) */
  97. ipv6->flow = 0; /* Flow label (LS) */
  98. /* Length excludes the IPv6 header */
  99. lladdrsize = netdev_lladdrsize(dev);
  100. l3size = SIZEOF_ICMPV6_NEIGHBOR_SOLICIT_S(lladdrsize);
  101. ipv6->len[0] = (l3size >> 8);
  102. ipv6->len[1] = (l3size & 0xff);
  103. ipv6->proto = IP_PROTO_ICMP6; /* Next header */
  104. ipv6->ttl = 255; /* Hop limit */
  105. /* Set the multicast destination IP address */
  106. memcpy(ipv6->destipaddr, g_icmpv_mcastaddr, 6*sizeof(uint16_t));
  107. ipv6->destipaddr[6] = ipaddr[6] | HTONS(0xff00);
  108. ipv6->destipaddr[7] = ipaddr[7];
  109. /* Add out IPv6 address as the source address */
  110. net_ipv6addr_copy(ipv6->srcipaddr, dev->d_ipv6addr);
  111. /* Set up the ICMPv6 Neighbor Solicitation message */
  112. sol = ICMPv6SOLICIT;
  113. sol->type = ICMPv6_NEIGHBOR_SOLICIT; /* Message type */
  114. sol->code = 0; /* Message qualifier */
  115. sol->flags[0] = 0; /* flags */
  116. sol->flags[1] = 0;
  117. sol->flags[2] = 0;
  118. sol->flags[3] = 0;
  119. /* Copy the target address into the Neighbor Solicitation message */
  120. net_ipv6addr_copy(sol->tgtaddr, ipaddr);
  121. /* Set up the options */
  122. sol->opttype = ICMPv6_OPT_SRCLLADDR; /* Option type */
  123. sol->optlen = ICMPv6_OPT_OCTECTS(lladdrsize); /* Option length in octets */
  124. /* Copy our link layer address into the message */
  125. memcpy(sol->srclladdr, &dev->d_mac, lladdrsize);
  126. /* Calculate the checksum over both the ICMP header and payload */
  127. sol->chksum = 0;
  128. sol->chksum = ~icmpv6_chksum(dev, IPv6_HDRLEN);
  129. /* Set the size to the size of the IPv6 header and the payload size */
  130. dev->d_len = IPv6_HDRLEN + l3size;
  131. ninfo("Outgoing ICMPv6 Neighbor Solicitation length: %d (%d)\n",
  132. dev->d_len, (ipv6->len[0] << 8) | ipv6->len[1]);
  133. #ifdef CONFIG_NET_STATISTICS
  134. g_netstats.icmpv6.sent++;
  135. g_netstats.ipv6.sent++;
  136. #endif
  137. }
  138. #endif /* CONFIG_NET_ICMPv6 */