icmpv6_solicit.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_solicit.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <debug.h>
  27. #include <nuttx/net/net.h>
  28. #include <nuttx/net/netdev.h>
  29. #include <nuttx/net/netstats.h>
  30. #include "devif/devif.h"
  31. #include "netdev/netdev.h"
  32. #include "utils/utils.h"
  33. #include "icmpv6/icmpv6.h"
  34. #ifdef CONFIG_NET_ICMPv6
  35. /****************************************************************************
  36. * Pre-processor Definitions
  37. ****************************************************************************/
  38. #define IPv6BUF ((struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
  39. #define ICMPv6SOLICIT \
  40. ((struct icmpv6_neighbor_solicit_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
  41. /****************************************************************************
  42. * Private Data
  43. ****************************************************************************/
  44. /* First 6 hwords of the multi-cast address in network byte order */
  45. static const uint16_t g_icmpv_mcastaddr[6] =
  46. {
  47. HTONS(0xff02), HTONS(0x0000), HTONS(0x0000), HTONS(0x0000),
  48. HTONS(0x0000), HTONS(0x0001)
  49. };
  50. /****************************************************************************
  51. * Public Functions
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Name: icmpv6_solicit
  55. *
  56. * Description:
  57. * Set up to send an ICMPv6 Neighbor Solicitation message. This version
  58. * is for a standalone solicitation. If formats:
  59. *
  60. * - The IPv6 header
  61. * - The ICMPv6 Neighbor Solicitation Message
  62. *
  63. * Input Parameters:
  64. * dev - Reference to a device driver structure
  65. * ipaddr - IP address of Neighbor to be solicited
  66. *
  67. * Returned Value:
  68. * None
  69. *
  70. ****************************************************************************/
  71. void icmpv6_solicit(FAR struct net_driver_s *dev,
  72. FAR const net_ipv6addr_t ipaddr)
  73. {
  74. FAR struct ipv6_hdr_s *ipv6;
  75. FAR struct icmpv6_neighbor_solicit_s *sol;
  76. uint16_t lladdrsize;
  77. uint16_t l3size;
  78. /* Set up the IPv6 header (most is probably already in place) */
  79. ipv6 = IPv6BUF;
  80. ipv6->vtc = 0x60; /* Version/traffic class (MS) */
  81. ipv6->tcf = 0; /* Traffic class (LS)/Flow label (MS) */
  82. ipv6->flow = 0; /* Flow label (LS) */
  83. /* Length excludes the IPv6 header */
  84. lladdrsize = netdev_lladdrsize(dev);
  85. l3size = SIZEOF_ICMPV6_NEIGHBOR_SOLICIT_S(lladdrsize);
  86. ipv6->len[0] = (l3size >> 8);
  87. ipv6->len[1] = (l3size & 0xff);
  88. ipv6->proto = IP_PROTO_ICMP6; /* Next header */
  89. ipv6->ttl = 255; /* Hop limit */
  90. /* Set the multicast destination IP address */
  91. memcpy(ipv6->destipaddr, g_icmpv_mcastaddr, 6*sizeof(uint16_t));
  92. ipv6->destipaddr[6] = ipaddr[6] | HTONS(0xff00);
  93. ipv6->destipaddr[7] = ipaddr[7];
  94. /* Add out IPv6 address as the source address */
  95. net_ipv6addr_copy(ipv6->srcipaddr, dev->d_ipv6addr);
  96. /* Set up the ICMPv6 Neighbor Solicitation message */
  97. sol = ICMPv6SOLICIT;
  98. sol->type = ICMPv6_NEIGHBOR_SOLICIT; /* Message type */
  99. sol->code = 0; /* Message qualifier */
  100. sol->flags[0] = 0; /* flags */
  101. sol->flags[1] = 0;
  102. sol->flags[2] = 0;
  103. sol->flags[3] = 0;
  104. /* Copy the target address into the Neighbor Solicitation message */
  105. net_ipv6addr_copy(sol->tgtaddr, ipaddr);
  106. /* Set up the options */
  107. sol->opttype = ICMPv6_OPT_SRCLLADDR; /* Option type */
  108. sol->optlen = ICMPv6_OPT_OCTECTS(lladdrsize); /* Option length in octets */
  109. /* Copy our link layer address into the message */
  110. memcpy(sol->srclladdr, &dev->d_mac, lladdrsize);
  111. /* Calculate the checksum over both the ICMP header and payload */
  112. sol->chksum = 0;
  113. sol->chksum = ~icmpv6_chksum(dev, IPv6_HDRLEN);
  114. /* Set the size to the size of the IPv6 header and the payload size */
  115. dev->d_len = IPv6_HDRLEN + l3size;
  116. ninfo("Outgoing ICMPv6 Neighbor Solicitation length: %d (%d)\n",
  117. dev->d_len, (ipv6->len[0] << 8) | ipv6->len[1]);
  118. #ifdef CONFIG_NET_STATISTICS
  119. g_netstats.icmpv6.sent++;
  120. g_netstats.ipv6.sent++;
  121. #endif
  122. }
  123. #endif /* CONFIG_NET_ICMPv6 */