4 * Ethernet output for IPv6. Uses ND tables for link-layer addressing.
8 * Copyright (c) 2010 Inico Technologies Ltd.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Ivan Delamer <delamer@inicotech.com>
38 * Please coordinate changes and requests with Ivan Delamer
39 * <delamer@inicotech.com>
44 #if LWIP_IPV6 && LWIP_ETHERNET
46 #include "lwip/ethip6.h"
48 #include "lwip/pbuf.h"
50 #include "lwip/ip6_addr.h"
51 #include "lwip/inet_chksum.h"
52 #include "lwip/netif.h"
53 #include "lwip/icmp6.h"
57 #define ETHTYPE_IPV6 0x86DD
59 /** The ethernet address */
60 #ifdef PACK_STRUCT_USE_INCLUDES
61 # include "arch/bpstruct.h"
65 PACK_STRUCT_FIELD(u8_t addr[6]);
68 #ifdef PACK_STRUCT_USE_INCLUDES
69 # include "arch/epstruct.h"
72 /** Ethernet header */
73 #ifdef PACK_STRUCT_USE_INCLUDES
74 # include "arch/bpstruct.h"
79 PACK_STRUCT_FIELD(u8_t padding[ETH_PAD_SIZE]);
81 PACK_STRUCT_FIELD(struct eth_addr dest);
82 PACK_STRUCT_FIELD(struct eth_addr src);
83 PACK_STRUCT_FIELD(u16_t type);
86 #ifdef PACK_STRUCT_USE_INCLUDES
87 # include "arch/epstruct.h"
90 #define SIZEOF_ETH_HDR (14 + ETH_PAD_SIZE)
93 * Send an IPv6 packet on the network using netif->linkoutput
94 * The ethernet header is filled in before sending.
96 * @params netif the lwIP network interface on which to send the packet
97 * @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
98 * @params src the source MAC address to be copied into the ethernet header
99 * @params dst the destination MAC address to be copied into the ethernet header
100 * @return ERR_OK if the packet was sent, any other err_t on failure
103 ethip6_send(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
105 struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
107 LWIP_ASSERT("netif->hwaddr_len must be 6 for ethip6!",
108 (netif->hwaddr_len == 6));
109 SMEMCPY(ðhdr->dest, dst, 6);
110 SMEMCPY(ðhdr->src, src, 6);
111 ethhdr->type = PP_HTONS(ETHTYPE_IPV6);
112 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("ethip6_send: sending packet %p\n", (void *)p));
113 /* send the packet */
114 return netif->linkoutput(netif, p);
118 * Resolve and fill-in Ethernet address header for outgoing IPv6 packet.
120 * For IPv6 multicast, corresponding Ethernet addresses
121 * are selected and the packet is transmitted on the link.
123 * For unicast addresses, ...
125 * @TODO anycast addresses
127 * @param netif The lwIP network interface which the IP packet will be sent on.
128 * @param q The pbuf(s) containing the IP packet to be sent.
129 * @param ip6addr The IP address of the packet destination.
132 * - ERR_RTE No route to destination (no gateway to external networks),
133 * or the return type of either etharp_query() or etharp_send_ip().
136 ethip6_output(struct netif *netif, struct pbuf *q, ip6_addr_t *ip6addr)
138 struct eth_addr dest;
141 /* make room for Ethernet header - should not fail */
142 if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
144 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
145 ("etharp_output: could not allocate room for header.\n"));
149 /* multicast destination IP address? */
150 if (ip6_addr_ismulticast(ip6addr)) {
151 /* Hash IP multicast address to MAC address.*/
154 dest.addr[2] = ((u8_t *)(&(ip6addr->addr[3])))[0];
155 dest.addr[3] = ((u8_t *)(&(ip6addr->addr[3])))[1];
156 dest.addr[4] = ((u8_t *)(&(ip6addr->addr[3])))[2];
157 dest.addr[5] = ((u8_t *)(&(ip6addr->addr[3])))[3];
160 return ethip6_send(netif, q, (struct eth_addr*)(netif->hwaddr), &dest);
163 /* We have a unicast destination IP address */
165 /* Get next hop record. */
166 i = nd6_get_next_hop_entry(ip6addr, netif);
168 /* failed to get a next hop neighbor record. */
172 /* Now that we have a destination record, send or queue the packet. */
173 if (neighbor_cache[i].state == ND6_STALE) {
174 /* Switch to delay state. */
175 neighbor_cache[i].state = ND6_DELAY;
176 neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME;
178 /* TODO should we send or queue if PROBE? send for now, to let unicast NS pass. */
179 if ((neighbor_cache[i].state == ND6_REACHABLE) ||
180 (neighbor_cache[i].state == ND6_DELAY) ||
181 (neighbor_cache[i].state == ND6_PROBE)) {
184 SMEMCPY(dest.addr, neighbor_cache[i].lladdr, 6);
185 return ethip6_send(netif, q, (struct eth_addr*)(netif->hwaddr), &dest);
188 #if LWIP_ND6_QUEUEING
189 /* We should queue packet on this interface. */
190 pbuf_header(q, -(s16_t)SIZEOF_ETH_HDR);
191 return nd6_queue_packet(i, q);
192 #else /* LWIP_ND6_QUEUEING */
194 #endif /* LWIP_ND6_QUEUEING */
197 #endif /* LWIP_IPV6 && LWIP_ETHERNET */