1 /**
2  * @file
3  *
4  * Ethernet output for IPv6. Uses ND tables for link-layer addressing.
5  */
6 
7 /*
8  * Copyright (c) 2010 Inico Technologies Ltd.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
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.
21  *
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
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Ivan Delamer <delamer@inicotech.com>
36  *
37  *
38  * Please coordinate changes and requests with Ivan Delamer
39  * <delamer@inicotech.com>
40  */
41 
42 #include "lwip/opt.h"
43 
44 #if LWIP_IPV6 && LWIP_ETHERNET
45 
46 #include "lwip/ethip6.h"
47 #include "lwip/nd6.h"
48 #include "lwip/pbuf.h"
49 #include "lwip/ip6.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 
57 #include <string.h>
58 
59 /**
60  * Resolve and fill-in Ethernet address header for outgoing IPv6 packet.
61  *
62  * For IPv6 multicast, corresponding Ethernet addresses
63  * are selected and the packet is transmitted on the link.
64  *
65  * For unicast addresses, ...
66  *
67  * @todo anycast addresses
68  *
69  * @param netif The lwIP network interface which the IP packet will be sent on.
70  * @param q The pbuf(s) containing the IP packet to be sent.
71  * @param ip6addr The IP address of the packet destination.
72  *
73  * @return
74  * - ERR_RTE No route to destination (no gateway to external networks),
75  * or the return type of either nd6_queue_packet() or ethernet_output().
76  */
77 err_t
ethip6_output(struct netif * netif,struct pbuf * q,const ip6_addr_t * ip6addr)78 ethip6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
79 {
80   struct eth_addr dest;
81   s8_t i;
82 
83   /* multicast destination IP address? */
84   if (ip6_addr_ismulticast(ip6addr)) {
85     /* Hash IP multicast address to MAC address.*/
86     dest.addr[0] = 0x33;
87     dest.addr[1] = 0x33;
88     dest.addr[2] = ((const u8_t *)(&(ip6addr->addr[3])))[0];
89     dest.addr[3] = ((const u8_t *)(&(ip6addr->addr[3])))[1];
90     dest.addr[4] = ((const u8_t *)(&(ip6addr->addr[3])))[2];
91     dest.addr[5] = ((const u8_t *)(&(ip6addr->addr[3])))[3];
92 
93     /* Send out. */
94     return ethernet_output(netif, q, (struct eth_addr*)(netif->hwaddr), &dest, ETHTYPE_IPV6);
95   }
96 
97   /* We have a unicast destination IP address */
98   /* @todo anycast? */
99   /* Get next hop record. */
100   i = nd6_get_next_hop_entry(ip6addr, netif);
101   if (i < 0) {
102     /* failed to get a next hop neighbor record. */
103     return ERR_MEM;
104   }
105 
106   /* Now that we have a destination record, send or queue the packet. */
107   if (neighbor_cache[i].state == ND6_STALE) {
108     /* Switch to delay state. */
109     neighbor_cache[i].state = ND6_DELAY;
110     neighbor_cache[i].counter.delay_time = LWIP_ND6_DELAY_FIRST_PROBE_TIME / ND6_TMR_INTERVAL;
111   }
112   /* @todo should we send or queue if PROBE? send for now, to let unicast NS pass. */
113   if ((neighbor_cache[i].state == ND6_REACHABLE) ||
114       (neighbor_cache[i].state == ND6_DELAY) ||
115       (neighbor_cache[i].state == ND6_PROBE)) {
116 
117     /* Send out. */
118     SMEMCPY(dest.addr, neighbor_cache[i].lladdr, 6);
119     return ethernet_output(netif, q, (struct eth_addr*)(netif->hwaddr), &dest, ETHTYPE_IPV6);
120   }
121 
122   /* We should queue packet on this interface. */
123   return nd6_queue_packet(i, q);
124 }
125 
126 #endif /* LWIP_IPV6 && LWIP_ETHERNET */
127