STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
ethip6.c
Go to the documentation of this file.
1 
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 
55 #include <string.h>
56 
57 #define ETHTYPE_IPV6 0x86DD
58 
60 #ifdef PACK_STRUCT_USE_INCLUDES
61 # include "arch/bpstruct.h"
62 #endif
64 struct eth_addr {
65  PACK_STRUCT_FLD_8(u8_t addr[6]);
68 #ifdef PACK_STRUCT_USE_INCLUDES
69 # include "arch/epstruct.h"
70 #endif
71 
73 #ifdef PACK_STRUCT_USE_INCLUDES
74 # include "arch/bpstruct.h"
75 #endif
77 struct eth_hdr {
78 #if ETH_PAD_SIZE
80 #endif
81  PACK_STRUCT_FLD_S(struct eth_addr dest);
82  PACK_STRUCT_FLD_S(struct eth_addr src);
86 #ifdef PACK_STRUCT_USE_INCLUDES
87 # include "arch/epstruct.h"
88 #endif
89 
90 #define SIZEOF_ETH_HDR (14 + ETH_PAD_SIZE)
91 
102 static err_t
103 ethip6_send(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
104 {
105  struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
106 
107  LWIP_ASSERT("netif->hwaddr_len must be 6 for ethip6!",
108  (netif->hwaddr_len == 6));
109  SMEMCPY(&ethhdr->dest, dst, 6);
110  SMEMCPY(&ethhdr->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);
115 }
116 
135 err_t
136 ethip6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
137 {
138  struct eth_addr dest;
139  s8_t i;
140 
141  /* make room for Ethernet header - should not fail */
142  if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
143  /* bail out */
145  ("etharp_output: could not allocate room for header.\n"));
146  return ERR_BUF;
147  }
148 
149  /* multicast destination IP address? */
150  if (ip6_addr_ismulticast(ip6addr)) {
151  /* Hash IP multicast address to MAC address.*/
152  dest.addr[0] = 0x33;
153  dest.addr[1] = 0x33;
154  dest.addr[2] = ((const u8_t *)(&(ip6addr->addr[3])))[0];
155  dest.addr[3] = ((const u8_t *)(&(ip6addr->addr[3])))[1];
156  dest.addr[4] = ((const u8_t *)(&(ip6addr->addr[3])))[2];
157  dest.addr[5] = ((const u8_t *)(&(ip6addr->addr[3])))[3];
158 
159  /* Send out. */
160  return ethip6_send(netif, q, (struct eth_addr*)(netif->hwaddr), &dest);
161  }
162 
163  /* We have a unicast destination IP address */
164  /* TODO anycast? */
165  /* Get next hop record. */
166  i = nd6_get_next_hop_entry(ip6addr, netif);
167  if (i < 0) {
168  /* failed to get a next hop neighbor record. */
169  return ERR_MEM;
170  }
171 
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;
177  }
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)) {
182 
183  /* Send out. */
184  SMEMCPY(dest.addr, neighbor_cache[i].lladdr, 6);
185  return ethip6_send(netif, q, (struct eth_addr*)(netif->hwaddr), &dest);
186  }
187 
188  /* We should queue packet on this interface. */
189  pbuf_header(q, -(s16_t)SIZEOF_ETH_HDR);
190  return nd6_queue_packet(i, q);
191 }
192 
193 #endif /* LWIP_IPV6 && LWIP_ETHERNET */
194 
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:47
#define PACK_STRUCT_STRUCT
Definition: arch.h:68
signed char s8_t
Definition: cc.h:39
signed short s16_t
Definition: cc.h:41
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: netif.h:267
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
#define PACK_STRUCT_FLD_8(x)
Definition: arch.h:78
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
#define PACK_STRUCT_FIELD(x)
Definition: arch.h:72
netif_linkoutput_fn linkoutput
Definition: netif.h:211
#define PP_HTONS(x)
Definition: def.h:97
#define LWIP_DBG_TRACE
Definition: debug.h:57
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
Definition: netif.h:182
u8_t hwaddr_len
Definition: netif.h:265
#define PACK_STRUCT_FLD_S(x)
Definition: arch.h:84
#define PACK_STRUCT_BEGIN
Definition: arch.h:60
#define ETH_PAD_SIZE
Definition: opt.h:522
unsigned char u8_t
Definition: cc.h:38
#define LWIP_ND6_DELAY_FIRST_PROBE_TIME
Definition: opt.h:2604
#define PACK_STRUCT_END
Definition: arch.h:64
#define ETHARP_DEBUG
Definition: opt.h:2774
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define ERR_MEM
Definition: err.h:53
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113
#define ERR_BUF
Definition: err.h:54