icmpv6_linkipaddr.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /****************************************************************************
  2. * net/icmpv6/icmpv6_linkipaddr.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 "netdev/netdev.h"
  25. #include "icmpv6/icmpv6.h"
  26. #ifdef CONFIG_NET_ICMPv6
  27. /****************************************************************************
  28. * Private Functions
  29. ****************************************************************************/
  30. static inline void
  31. icmpv6_linkipaddr_0(FAR struct net_driver_s *dev, net_ipv6addr_t ipaddr)
  32. {
  33. ipaddr[0] = HTONS(0xfe80);
  34. ipaddr[1] = 0;
  35. ipaddr[2] = 0;
  36. ipaddr[3] = 0;
  37. ipaddr[4] = 0;
  38. ipaddr[5] = 0;
  39. ipaddr[6] = 0;
  40. #ifdef CONFIG_NETDEV_IFINDEX
  41. ipaddr[7] = HTONS(dev->d_ifindex);
  42. #else
  43. ipaddr[7] = 0;
  44. #endif
  45. }
  46. static inline void
  47. icmpv6_linkipaddr_1(FAR const void *mac_, net_ipv6addr_t ipaddr)
  48. {
  49. FAR const uint8_t *mac = mac_;
  50. ipaddr[0] = HTONS(0xfe80);
  51. ipaddr[1] = 0;
  52. ipaddr[2] = 0;
  53. ipaddr[3] = 0;
  54. ipaddr[4] = 0;
  55. ipaddr[5] = HTONS(0x00ff);
  56. ipaddr[6] = HTONS(0xfe00);
  57. ipaddr[7] = HTONS(mac[0] << 8);
  58. }
  59. static inline void
  60. icmpv6_linkipaddr_2(FAR const void *mac_, net_ipv6addr_t ipaddr)
  61. {
  62. FAR const uint8_t *mac = mac_;
  63. ipaddr[0] = HTONS(0xfe80);
  64. ipaddr[1] = 0;
  65. ipaddr[2] = 0;
  66. ipaddr[3] = 0;
  67. ipaddr[4] = 0;
  68. ipaddr[5] = HTONS(0x00ff);
  69. ipaddr[6] = HTONS(0xfe00);
  70. ipaddr[7] = HTONS(mac[0] << 8 | mac[1]);
  71. }
  72. static inline void
  73. icmpv6_linkipaddr_6(FAR const void *mac_, net_ipv6addr_t ipaddr)
  74. {
  75. FAR const uint8_t *mac = mac_;
  76. ipaddr[0] = HTONS(0xfe80);
  77. ipaddr[1] = 0;
  78. ipaddr[2] = 0;
  79. ipaddr[3] = 0;
  80. ipaddr[4] = HTONS(mac[0] << 8 | mac[1]);
  81. ipaddr[5] = HTONS(mac[2] << 8 | 0x00ff);
  82. ipaddr[6] = HTONS(0x00fe << 8 | mac[3]);
  83. ipaddr[7] = HTONS(mac[4] << 8 | mac[5]);
  84. ipaddr[4] ^= HTONS(0x0200);
  85. }
  86. static inline void
  87. icmpv6_linkipaddr_8(FAR const void *mac_, net_ipv6addr_t ipaddr)
  88. {
  89. FAR const uint8_t *mac = mac_;
  90. ipaddr[0] = HTONS(0xfe80);
  91. ipaddr[1] = 0;
  92. ipaddr[2] = 0;
  93. ipaddr[3] = 0;
  94. ipaddr[4] = HTONS(mac[0] << 8 | mac[1]);
  95. ipaddr[5] = HTONS(mac[2] << 8 | mac[3]);
  96. ipaddr[6] = HTONS(mac[4] << 8 | mac[5]);
  97. ipaddr[7] = HTONS(mac[6] << 8 | mac[7]);
  98. ipaddr[4] ^= HTONS(0x0200);
  99. }
  100. /****************************************************************************
  101. * Public Functions
  102. ****************************************************************************/
  103. /****************************************************************************
  104. * Name: icmpv6_linkipaddr
  105. *
  106. * Description:
  107. * Generate the device link scope ipv6 address as below:
  108. * 128 112 96 80 64 48 32 16
  109. * ---- ---- ---- ---- ---- ---- ---- ----
  110. * fe80 0000 0000 0000 0000 00ff fe00 xx00 1B short address IEEE 48b MAC
  111. * fe80 0000 0000 0000 0000 00ff fe00 xxxx 2B short address IEEE 48b MAC
  112. * fe80 0000 0000 0000 xxxx xxff fexx xxxx 6B normal address IEEE 48b MAC
  113. * fe80 0000 0000 0000 xxxx xxxx xxxx xxxx 8B extended address IEEE EUI-64
  114. *
  115. * Input Parameters:
  116. * dev - The device driver structure containing the link layer address
  117. * ipaddr - Receive the device link scope ipv6 address
  118. *
  119. * Returned Value:
  120. * None
  121. *
  122. ****************************************************************************/
  123. void icmpv6_linkipaddr(FAR struct net_driver_s *dev, net_ipv6addr_t ipaddr)
  124. {
  125. switch (netdev_lladdrsize(dev))
  126. {
  127. case 1:
  128. icmpv6_linkipaddr_1(&dev->d_mac, ipaddr);
  129. break;
  130. case 2:
  131. icmpv6_linkipaddr_2(&dev->d_mac, ipaddr);
  132. break;
  133. case 6:
  134. icmpv6_linkipaddr_6(&dev->d_mac, ipaddr);
  135. break;
  136. case 8:
  137. icmpv6_linkipaddr_8(&dev->d_mac, ipaddr);
  138. break;
  139. default:
  140. icmpv6_linkipaddr_0(dev, ipaddr);
  141. break;
  142. }
  143. }
  144. #endif /* CONFIG_NET_ICMPv6 */