ethip6.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <string.h>
  51. #define ETHTYPE_IPV6 0x86DD
  52. /** The ethernet address */
  53. #ifdef PACK_STRUCT_USE_INCLUDES
  54. # include "arch/bpstruct.h"
  55. #endif
  56. PACK_STRUCT_BEGIN
  57. struct eth_addr {
  58. PACK_STRUCT_FIELD(u8_t addr[6]);
  59. } PACK_STRUCT_STRUCT;
  60. PACK_STRUCT_END
  61. #ifdef PACK_STRUCT_USE_INCLUDES
  62. # include "arch/epstruct.h"
  63. #endif
  64. /** Ethernet header */
  65. #ifdef PACK_STRUCT_USE_INCLUDES
  66. # include "arch/bpstruct.h"
  67. #endif
  68. PACK_STRUCT_BEGIN
  69. struct eth_hdr {
  70. #if ETH_PAD_SIZE
  71. PACK_STRUCT_FIELD(u8_t padding[ETH_PAD_SIZE]);
  72. #endif
  73. PACK_STRUCT_FIELD(struct eth_addr dest);
  74. PACK_STRUCT_FIELD(struct eth_addr src);
  75. PACK_STRUCT_FIELD(u16_t type);
  76. } PACK_STRUCT_STRUCT;
  77. PACK_STRUCT_END
  78. #ifdef PACK_STRUCT_USE_INCLUDES
  79. # include "arch/epstruct.h"
  80. #endif
  81. #define SIZEOF_ETH_HDR (14 + ETH_PAD_SIZE)
  82. /**
  83. * Send an IPv6 packet on the network using netif->linkoutput
  84. * The ethernet header is filled in before sending.
  85. *
  86. * @params netif the lwIP network interface on which to send the packet
  87. * @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
  88. * @params src the source MAC address to be copied into the ethernet header
  89. * @params dst the destination MAC address to be copied into the ethernet header
  90. * @return ERR_OK if the packet was sent, any other err_t on failure
  91. */
  92. static err_t
  93. ethip6_send(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
  94. {
  95. struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
  96. LWIP_ASSERT("netif->hwaddr_len must be 6 for ethip6!",
  97. (netif->hwaddr_len == 6));
  98. SMEMCPY(&ethhdr->dest, dst, 6);
  99. SMEMCPY(&ethhdr->src, src, 6);
  100. ethhdr->type = PP_HTONS(ETHTYPE_IPV6);
  101. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("ethip6_send: sending packet %p\n", (void *)p));
  102. /* send the packet */
  103. return netif->linkoutput(netif, p);
  104. }
  105. /**
  106. * Resolve and fill-in Ethernet address header for outgoing IPv6 packet.
  107. *
  108. * For IPv6 multicast, corresponding Ethernet addresses
  109. * are selected and the packet is transmitted on the link.
  110. *
  111. * For unicast addresses, ...
  112. *
  113. * @TODO anycast addresses
  114. *
  115. * @param netif The lwIP network interface which the IP packet will be sent on.
  116. * @param q The pbuf(s) containing the IP packet to be sent.
  117. * @param ip6addr The IP address of the packet destination.
  118. *
  119. * @return
  120. * - ERR_RTE No route to destination (no gateway to external networks),
  121. * or the return type of either etharp_query() or etharp_send_ip().
  122. */
  123. err_t
  124. ethip6_output(struct netif *netif, struct pbuf *q, ip6_addr_t *ip6addr)
  125. {
  126. struct eth_addr dest;
  127. s8_t i;
  128. /* make room for Ethernet header - should not fail */
  129. if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
  130. /* bail out */
  131. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
  132. ("etharp_output: could not allocate room for header.\n"));
  133. return ERR_BUF;
  134. }
  135. /* multicast destination IP address? */
  136. if (ip6_addr_ismulticast(ip6addr)) {
  137. /* Hash IP multicast address to MAC address.*/
  138. dest.addr[0] = 0x33;
  139. dest.addr[1] = 0x33;
  140. dest.addr[2] = ((u8_t *)(&(ip6addr->addr[3])))[0];
  141. dest.addr[3] = ((u8_t *)(&(ip6addr->addr[3])))[1];
  142. dest.addr[4] = ((u8_t *)(&(ip6addr->addr[3])))[2];
  143. dest.addr[5] = ((u8_t *)(&(ip6addr->addr[3])))[3];
  144. /* Send out. */
  145. return ethip6_send(netif, q, (struct eth_addr*)(netif->hwaddr), &dest);
  146. }
  147. /* We have a unicast destination IP address */
  148. /* TODO anycast? */
  149. /* Get next hop record. */
  150. i = nd6_get_next_hop_entry(ip6addr, netif);
  151. if (i < 0) {
  152. /* failed to get a next hop neighbor record. */
  153. return ERR_MEM;
  154. }
  155. /* Now that we have a destination record, send or queue the packet. */
  156. if (neighbor_cache[i].state == ND6_STALE) {
  157. /* Switch to delay state. */
  158. neighbor_cache[i].state = ND6_DELAY;
  159. neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME;
  160. }
  161. /* TODO should we send or queue if PROBE? send for now, to let unicast NS pass. */
  162. if ((neighbor_cache[i].state == ND6_REACHABLE) ||
  163. (neighbor_cache[i].state == ND6_DELAY) ||
  164. (neighbor_cache[i].state == ND6_PROBE)) {
  165. /* Send out. */
  166. SMEMCPY(dest.addr, neighbor_cache[i].lladdr, 6);
  167. return ethip6_send(netif, q, (struct eth_addr*)(netif->hwaddr), &dest);
  168. }
  169. /* We should queue packet on this interface. */
  170. pbuf_header(q, -(s16_t)SIZEOF_ETH_HDR);
  171. return nd6_queue_packet(i, q);
  172. }
  173. #endif /* LWIP_IPV6 && LWIP_ETHERNET */