ethip6.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @file
  3. *
  4. * Ethernet output for IPv6. Uses ND tables for link-layer addressing.
  5. */
  6. /*
  7. * Copyright (c) 2010 Inico Technologies Ltd.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Ivan Delamer <delamer@inicotech.com>
  35. *
  36. *
  37. * Please coordinate changes and requests with Ivan Delamer
  38. * <delamer@inicotech.com>
  39. */
  40. #include "lwip/opt.h"
  41. #if LWIP_IPV6 && LWIP_ETHERNET
  42. #include "lwip/ethip6.h"
  43. #include "lwip/nd6.h"
  44. #include "lwip/pbuf.h"
  45. #include "lwip/ip6.h"
  46. #include "lwip/ip6_addr.h"
  47. #include "lwip/inet_chksum.h"
  48. #include "lwip/netif.h"
  49. #include "lwip/icmp6.h"
  50. #include "lwip/prot/ethernet.h"
  51. #include "netif/ethernet.h"
  52. #include <string.h>
  53. /**
  54. * Resolve and fill-in Ethernet address header for outgoing IPv6 packet.
  55. *
  56. * For IPv6 multicast, corresponding Ethernet addresses
  57. * are selected and the packet is transmitted on the link.
  58. *
  59. * For unicast addresses, ask the ND6 module what to do. It will either let us
  60. * send the the packet right away, or queue the packet for later itself, unless
  61. * an error occurs.
  62. *
  63. * @todo anycast addresses
  64. *
  65. * @param netif The lwIP network interface which the IP packet will be sent on.
  66. * @param q The pbuf(s) containing the IP packet to be sent.
  67. * @param ip6addr The IP address of the packet destination.
  68. *
  69. * @return
  70. * - ERR_OK or the return value of @ref nd6_get_next_hop_addr_or_queue.
  71. */
  72. err_t
  73. ethip6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
  74. {
  75. struct eth_addr dest;
  76. const u8_t *hwaddr;
  77. err_t result;
  78. LWIP_ASSERT_CORE_LOCKED();
  79. /* The destination IP address must be properly zoned from here on down. */
  80. IP6_ADDR_ZONECHECK_NETIF(ip6addr, netif);
  81. /* multicast destination IP address? */
  82. if (ip6_addr_ismulticast(ip6addr)) {
  83. /* Hash IP multicast address to MAC address.*/
  84. dest.addr[0] = 0x33;
  85. dest.addr[1] = 0x33;
  86. dest.addr[2] = ((const u8_t *)(&(ip6addr->addr[3])))[0];
  87. dest.addr[3] = ((const u8_t *)(&(ip6addr->addr[3])))[1];
  88. dest.addr[4] = ((const u8_t *)(&(ip6addr->addr[3])))[2];
  89. dest.addr[5] = ((const u8_t *)(&(ip6addr->addr[3])))[3];
  90. /* Send out. */
  91. return ethernet_output(netif, q, (const struct eth_addr*)(netif->hwaddr), &dest, ETHTYPE_IPV6);
  92. }
  93. /* We have a unicast destination IP address */
  94. /* @todo anycast? */
  95. /* Ask ND6 what to do with the packet. */
  96. result = nd6_get_next_hop_addr_or_queue(netif, q, ip6addr, &hwaddr);
  97. if (result != ERR_OK) {
  98. return result;
  99. }
  100. /* If no hardware address is returned, nd6 has queued the packet for later. */
  101. if (hwaddr == NULL) {
  102. return ERR_OK;
  103. }
  104. /* Send out the packet using the returned hardware address. */
  105. SMEMCPY(dest.addr, hwaddr, 6);
  106. return ethernet_output(netif, q, (const struct eth_addr*)(netif->hwaddr), &dest, ETHTYPE_IPV6);
  107. }
  108. #endif /* LWIP_IPV6 && LWIP_ETHERNET */