icmpv6_radvertise.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_radvertise.c
  3. * Send an ICMPv6 Router 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/icmpv6.h>
  51. #include "netdev/netdev.h"
  52. #include "inet/inet.h"
  53. #include "utils/utils.h"
  54. #include "icmpv6/icmpv6.h"
  55. #ifdef CONFIG_NET_ICMPv6_ROUTER
  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_router_advertise_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
  62. /****************************************************************************
  63. * Private Data
  64. ****************************************************************************/
  65. #ifdef CONFIG_NET_ICMPv6_ROUTER_MANUAL
  66. static const net_ipv6addr_t g_ipv6_prefix =
  67. {
  68. HTONS(CONFIG_NET_ICMPv6_PREFIX_1),
  69. HTONS(CONFIG_NET_ICMPv6_PREFIX_2),
  70. HTONS(CONFIG_NET_ICMPv6_PREFIX_3),
  71. HTONS(CONFIG_NET_ICMPv6_PREFIX_4),
  72. HTONS(CONFIG_NET_ICMPv6_PREFIX_5),
  73. HTONS(CONFIG_NET_ICMPv6_PREFIX_6),
  74. HTONS(CONFIG_NET_ICMPv6_PREFIX_7),
  75. HTONS(CONFIG_NET_ICMPv6_PREFIX_8)
  76. };
  77. #endif /* CONFIG_NET_ICMPv6_ROUTER_MANUAL */
  78. /****************************************************************************
  79. * Private Functions
  80. ****************************************************************************/
  81. /****************************************************************************
  82. * Name: ipv6addr_mask
  83. *
  84. * Description:
  85. * Copy an IPv6 address under a mask
  86. *
  87. * Input Parameters:
  88. * dest - Location to return the masked address
  89. * src - The IPv6 address to mask
  90. * maksk - The address mask
  91. *
  92. * Returned Value:
  93. * None
  94. *
  95. ****************************************************************************/
  96. #ifndef CONFIG_NET_ICMPv6_ROUTER_MANUAL
  97. static inline void ipv6addr_mask(FAR uint16_t *dest, FAR const uint16_t *src,
  98. FAR const uint16_t *mask)
  99. {
  100. int i;
  101. for (i = 0; i < 8; ++i)
  102. {
  103. dest[i] = src[i] & mask[i];
  104. }
  105. }
  106. #endif /* !CONFIG_NET_ICMPv6_ROUTER_MANUAL */
  107. /****************************************************************************
  108. * Public Functions
  109. ****************************************************************************/
  110. /****************************************************************************
  111. * Name: icmpv6_radvertise
  112. *
  113. * Description:
  114. * Send an ICMPv6 Router Advertisement
  115. *
  116. * Input Parameters:
  117. * dev - The device driver structure containing the outgoing ICMPv6 packet
  118. * buffer
  119. *
  120. * Returned Value:
  121. * None
  122. *
  123. * Assumptions:
  124. * The network is locked
  125. *
  126. ****************************************************************************/
  127. void icmpv6_radvertise(FAR struct net_driver_s *dev)
  128. {
  129. FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
  130. FAR struct icmpv6_router_advertise_s *adv;
  131. FAR struct icmpv6_srclladdr_s *srcaddr;
  132. FAR struct icmpv6_mtu_s *mtu;
  133. FAR struct icmpv6_prefixinfo_s *prefix;
  134. uint16_t lladdrsize;
  135. uint16_t l3size;
  136. /* Set up the IPv6 header */
  137. ipv6->vtc = 0x60; /* Version/traffic class (MS) */
  138. ipv6->tcf = 0; /* Traffic class (LS)/Flow label (MS) */
  139. ipv6->flow = 0; /* Flow label (LS) */
  140. /* Length excludes the IPv6 header */
  141. lladdrsize = netdev_lladdrsize(dev);
  142. l3size = sizeof(struct icmpv6_router_advertise_s) +
  143. SIZEOF_ICMPV6_SRCLLADDR_S(lladdrsize) +
  144. sizeof(struct icmpv6_mtu_s) +
  145. sizeof(struct icmpv6_prefixinfo_s);
  146. ipv6->len[0] = (l3size >> 8);
  147. ipv6->len[1] = (l3size & 0xff);
  148. ipv6->proto = IP_PROTO_ICMP6; /* Next header */
  149. ipv6->ttl = 255; /* Hop limit */
  150. /* Swap source for destination IP address, add our source IP address */
  151. net_ipv6addr_copy(ipv6->destipaddr, g_ipv6_allnodes);
  152. /* Source IP address must be set to link-local IP */
  153. icmpv6_linkipaddr(dev, ipv6->srcipaddr);
  154. /* Set up the ICMPv6 Router Advertise response */
  155. adv = ICMPv6ADVERTISE;
  156. adv->type = ICMPV6_ROUTER_ADVERTISE; /* Message type */
  157. adv->code = 0; /* Message qualifier */
  158. adv->hoplimit = 64; /* Current hop limit */
  159. adv->flags = ICMPv6_RADV_FLAG_M; /* Managed address flag. */
  160. adv->lifetime = HTONS(1800); /* Router lifetime */
  161. adv->reachable = 0; /* Reachable time */
  162. adv->retrans = 0; /* Retransmission timer */
  163. /* Set up the source address option */
  164. srcaddr = (FAR struct icmpv6_srclladdr_s *)
  165. ((FAR uint8_t *)adv + sizeof(struct icmpv6_router_advertise_s));
  166. srcaddr->opttype = ICMPv6_OPT_SRCLLADDR;
  167. srcaddr->optlen = ICMPv6_OPT_OCTECTS(lladdrsize);
  168. memcpy(srcaddr->srclladdr, &dev->d_mac, lladdrsize);
  169. /* Set up the MTU option */
  170. mtu = (FAR struct icmpv6_mtu_s *)
  171. ((FAR uint8_t *)srcaddr + SIZEOF_ICMPV6_SRCLLADDR_S(lladdrsize));
  172. mtu->opttype = ICMPv6_OPT_MTU;
  173. mtu->optlen = 1;
  174. mtu->reserved = 0;
  175. mtu->mtu = HTONL(dev->d_pktsize - dev->d_llhdrlen);
  176. /* Set up the prefix option */
  177. prefix = (FAR struct icmpv6_prefixinfo_s *)
  178. ((FAR uint8_t *)mtu + sizeof(struct icmpv6_mtu_s));
  179. prefix->opttype = ICMPv6_OPT_PREFIX;
  180. prefix->optlen = 4;
  181. prefix->flags = ICMPv6_PRFX_FLAG_L | ICMPv6_PRFX_FLAG_A;
  182. prefix->vlifetime = HTONL(2592000);
  183. prefix->plifetime = HTONL(604800);
  184. prefix->reserved[0] = 0;
  185. prefix->reserved[1] = 0;
  186. #ifdef CONFIG_NET_ICMPv6_ROUTER_MANUAL
  187. /* Copy the configured prefex */
  188. prefix->preflen = CONFIG_NET_ICMPv6_PREFLEN;
  189. net_ipv6addr_copy(prefix->prefix, g_ipv6_prefix);
  190. #else
  191. /* Set the prefix and prefix length based on net driver IP and netmask */
  192. prefix->preflen = net_ipv6_mask2pref(dev->d_ipv6netmask);
  193. ipv6addr_mask(prefix->prefix, dev->d_ipv6addr, dev->d_ipv6netmask);
  194. #endif /* CONFIG_NET_ICMPv6_ROUTER_MANUAL */
  195. /* Calculate the checksum over both the ICMP header and payload */
  196. adv->chksum = 0;
  197. adv->chksum = ~icmpv6_chksum(dev, IPv6_HDRLEN);
  198. /* Set the size to the size of the IPv6 header and the payload size */
  199. dev->d_len = IPv6_HDRLEN + l3size;
  200. ninfo("Outgoing ICMPv6 Router Advertise length: %d (%d)\n",
  201. dev->d_len, (ipv6->len[0] << 8) | ipv6->len[1]);
  202. #ifdef CONFIG_NET_STATISTICS
  203. g_netstats.icmpv6.sent++;
  204. g_netstats.ipv6.sent++;
  205. #endif
  206. }
  207. #endif /* CONFIG_NET_ICMPv6_ROUTER */