icmpv6_rsolicit.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_rsolicit.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/netdev.h>
  43. #include <nuttx/net/netstats.h>
  44. #include "devif/devif.h"
  45. #include "netdev/netdev.h"
  46. #include "inet/inet.h"
  47. #include "utils/utils.h"
  48. #include "icmpv6/icmpv6.h"
  49. #ifdef CONFIG_NET_ICMPv6_AUTOCONF
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. #define IPv6BUF ((struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  54. #define ICMPv6RSOLICIT \
  55. ((struct icmpv6_router_solicit_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
  56. /****************************************************************************
  57. * Public Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: icmpv6_rsolicit
  61. *
  62. * Description:
  63. * Set up to send an ICMPv6 Router Solicitation message. This version
  64. * is for a standalone solicitation. If formats:
  65. *
  66. * - The IPv6 header
  67. * - The ICMPv6 Router Solicitation Message
  68. *
  69. * The device IP address should have been set to the link local address
  70. * prior to calling this function.
  71. *
  72. * Input Parameters:
  73. * dev - Reference to a device driver structure
  74. *
  75. * Returned Value:
  76. * None
  77. *
  78. ****************************************************************************/
  79. void icmpv6_rsolicit(FAR struct net_driver_s *dev)
  80. {
  81. FAR struct ipv6_hdr_s *ipv6;
  82. FAR struct icmpv6_router_solicit_s *sol;
  83. uint16_t lladdrsize;
  84. uint16_t l3size;
  85. /* Set up the IPv6 header (most is probably already in place) */
  86. ipv6 = IPv6BUF;
  87. ipv6->vtc = 0x60; /* Version/traffic class (MS) */
  88. ipv6->tcf = 0; /* Traffic class (LS)/Flow label (MS) */
  89. ipv6->flow = 0; /* Flow label (LS) */
  90. /* Length excludes the IPv6 header */
  91. lladdrsize = netdev_lladdrsize(dev);
  92. l3size = SIZEOF_ICMPV6_ROUTER_SOLICIT_S(lladdrsize);
  93. ipv6->len[0] = (l3size >> 8);
  94. ipv6->len[1] = (l3size & 0xff);
  95. ipv6->proto = IP_PROTO_ICMP6; /* Next header */
  96. ipv6->ttl = 255; /* Hop limit */
  97. /* Set the multicast destination IP address to the IPv6 all link-
  98. * local routers address: ff02::2
  99. */
  100. net_ipv6addr_copy(ipv6->destipaddr, g_ipv6_allrouters);
  101. /* Add our link local IPv6 address as the source address */
  102. net_ipv6addr_copy(ipv6->srcipaddr, dev->d_ipv6addr);
  103. /* Set up the ICMPv6 Router Solicitation message */
  104. sol = ICMPv6RSOLICIT;
  105. sol->type = ICMPV6_ROUTER_SOLICIT; /* Message type */
  106. sol->code = 0; /* Message qualifier */
  107. sol->flags[0] = 0; /* flags */
  108. sol->flags[1] = 0;
  109. sol->flags[2] = 0;
  110. sol->flags[3] = 0;
  111. /* Set up the options */
  112. sol->opttype = ICMPv6_OPT_SRCLLADDR; /* Option type */
  113. sol->optlen = ICMPv6_OPT_OCTECTS(lladdrsize); /* Option length in octets */
  114. /* Copy our link layer address into the message */
  115. memcpy(sol->srclladdr, &dev->d_mac, lladdrsize);
  116. /* Calculate the checksum over both the ICMP header and payload */
  117. sol->chksum = 0;
  118. sol->chksum = ~icmpv6_chksum(dev, IPv6_HDRLEN);
  119. /* Set the size to the size of the IPv6 header and the payload size */
  120. dev->d_len = IPv6_HDRLEN + l3size;
  121. ninfo("Outgoing ICMPv6 Router Solicitation length: %d (%d)\n",
  122. dev->d_len, (ipv6->len[0] << 8) | ipv6->len[1]);
  123. #ifdef CONFIG_NET_STATISTICS
  124. g_netstats.icmpv6.sent++;
  125. g_netstats.ipv6.sent++;
  126. #endif
  127. }
  128. #endif /* CONFIG_NET_ICMPv6_AUTOCONF */