icmpv6_advertise.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_advertise.c
  3. * Send an ICMPv6 Neighbor Advertisement
  4. *
  5. * Copyright (C) 2015, 2017 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. #include <stdint.h>
  45. #include <string.h>
  46. #include <debug.h>
  47. #include <nuttx/net/netconfig.h>
  48. #include <nuttx/net/netstats.h>
  49. #include <nuttx/net/netdev.h>
  50. #include <nuttx/net/net.h>
  51. #include <nuttx/net/icmpv6.h>
  52. #include "netdev/netdev.h"
  53. #include "utils/utils.h"
  54. #include "icmpv6/icmpv6.h"
  55. #ifdef CONFIG_NET_ICMPv6
  56. /****************************************************************************
  57. * Pre-processor Definitions
  58. ****************************************************************************/
  59. #define IPv6BUF ((struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  60. #define ICMPv6ADVERTISE \
  61. ((struct icmpv6_neighbor_advertise_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
  62. /****************************************************************************
  63. * Public Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Name: icmpv6_advertise
  67. *
  68. * Description:
  69. * Send an ICMPv6 Neighbor Advertisement
  70. *
  71. * Input Parameters:
  72. * dev - The device driver structure containing the outgoing ICMPv6 packet
  73. * buffer
  74. *
  75. * Returned Value:
  76. * None
  77. *
  78. * Assumptions:
  79. * The network is locked
  80. *
  81. ****************************************************************************/
  82. void icmpv6_advertise(FAR struct net_driver_s *dev,
  83. const net_ipv6addr_t destipaddr)
  84. {
  85. FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
  86. FAR struct icmpv6_neighbor_advertise_s *adv;
  87. uint16_t lladdrsize;
  88. uint16_t l3size;
  89. /* Set up the IPv6 header */
  90. ipv6->vtc = 0x60; /* Version/traffic class (MS) */
  91. ipv6->tcf = 0; /* Traffic class (LS)/Flow label (MS) */
  92. ipv6->flow = 0; /* Flow label (LS) */
  93. /* Length excludes the IPv6 header */
  94. lladdrsize = netdev_lladdrsize(dev);
  95. l3size = SIZEOF_ICMPV6_NEIGHBOR_ADVERTISE_S(lladdrsize);
  96. ipv6->len[0] = (l3size >> 8);
  97. ipv6->len[1] = (l3size & 0xff);
  98. ipv6->proto = IP_PROTO_ICMP6; /* Next header */
  99. ipv6->ttl = 255; /* Hop limit */
  100. /* Swap source for destination IP address, add our source IP address */
  101. net_ipv6addr_copy(ipv6->destipaddr, destipaddr);
  102. net_ipv6addr_copy(ipv6->srcipaddr, dev->d_ipv6addr);
  103. /* Set up the ICMPv6 Neighbor Advertise response */
  104. adv = ICMPv6ADVERTISE;
  105. adv->type = ICMPv6_NEIGHBOR_ADVERTISE; /* Message type */
  106. adv->code = 0; /* Message qualifier */
  107. adv->flags[0] = ICMPv6_NADV_FLAG_S | ICMPv6_NADV_FLAG_O; /* Solicited+Override flags. */
  108. adv->flags[1] = 0;
  109. adv->flags[2] = 0;
  110. adv->flags[3] = 0;
  111. /* Copy the target address into the Neighbor Advertisement message */
  112. net_ipv6addr_copy(adv->tgtaddr, dev->d_ipv6addr);
  113. /* Set up the options */
  114. adv->opttype = ICMPv6_OPT_TGTLLADDR; /* Option type */
  115. adv->optlen = ICMPv6_OPT_OCTECTS(lladdrsize); /* Option length in octets */
  116. /* Copy our link layer address into the message */
  117. memcpy(adv->tgtlladdr, &dev->d_mac, lladdrsize);
  118. /* Calculate the checksum over both the ICMP header and payload */
  119. adv->chksum = 0;
  120. adv->chksum = ~icmpv6_chksum(dev, IPv6_HDRLEN);
  121. /* Set the size to the size of the IPv6 header and the payload size */
  122. dev->d_len = IPv6_HDRLEN + l3size;
  123. ninfo("Outgoing ICMPv6 Neighbor Advertise length: %d (%d)\n",
  124. dev->d_len, (ipv6->len[0] << 8) | ipv6->len[1]);
  125. #ifdef CONFIG_NET_STATISTICS
  126. g_netstats.icmpv6.sent++;
  127. g_netstats.ipv6.sent++;
  128. #endif
  129. }
  130. #endif /* CONFIG_NET_ICMPv6 */