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"
54 #include "lwip/prot/ethernet.h"
55 #include "netif/ethernet.h"
56 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
57 #include "lwip/ip6_route_table.h"
63 * Resolve and fill-in Ethernet address header for outgoing IPv6 packet.
65 * For IPv6 multicast, corresponding Ethernet addresses
66 * are selected and the packet is transmitted on the link.
68 * For unicast addresses, ask the ND6 module what to do. It will either let us
69 * send the the packet right away, or queue the packet for later itself, unless
72 * @todo anycast addresses
74 * @param netif The lwIP network interface which the IP packet will be sent on.
75 * @param q The pbuf(s) containing the IP packet to be sent.
76 * @param ip6addr The IP address of the packet destination.
79 * - ERR_OK or the return value of @ref nd6_get_next_hop_addr_or_queue.
82 ethip6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
87 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
88 ip6_addr_t *gateway = NULL;
91 /* multicast destination IP address? */
92 if (ip6_addr_ismulticast(ip6addr)) {
93 /* Hash IP multicast address to MAC address.*/
96 dest.addr[2] = ((const u8_t *)(&(ip6addr->addr[3])))[0];
97 dest.addr[3] = ((const u8_t *)(&(ip6addr->addr[3])))[1];
98 dest.addr[4] = ((const u8_t *)(&(ip6addr->addr[3])))[2];
99 dest.addr[5] = ((const u8_t *)(&(ip6addr->addr[3])))[3];
102 return ethernet_output(netif, q, (const struct eth_addr*)(netif->hwaddr), &dest, ETHTYPE_IPV6);
105 /* We have a unicast destination IP address */
108 #if LWIP_IPV6_ROUTE_TABLE_SUPPORT
109 /* See if a gateway is present for the destination address in the static route table */
110 #ifdef LWIP_HOOK_ETHIP6_GET_GW
111 gateway = LWIP_HOOK_ETHIP6_GET_GW(netif, ip6addr);
113 if (gateway != NULL) {
114 /* Replace the destination with the gateway. The gateway is
115 * assumed to be on-link.
121 /* Ask ND6 what to do with the packet. */
122 result = nd6_get_next_hop_addr_or_queue(netif, q, ip6addr, &hwaddr);
123 if (result != ERR_OK) {
127 /* If no hardware address is returned, nd6 has queued the packet for later. */
128 if (hwaddr == NULL) {
132 /* Send out the packet using the returned hardware address. */
133 SMEMCPY(dest.addr, hwaddr, 6);
134 return ethernet_output(netif, q, (const struct eth_addr*)(netif->hwaddr), &dest, ETHTYPE_IPV6);
137 #endif /* LWIP_IPV6 && LWIP_ETHERNET */