Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested
Fork of F7_Ethernet by
lwip/netif/etharp.c@0:d26c1b55cfca, 2016-06-19 (annotated)
- Committer:
- DieterGraef
- Date:
- Sun Jun 19 16:23:40 2016 +0000
- Revision:
- 0:d26c1b55cfca
- Child:
- 2:11660e6c9d7a
Ethernet Library for Nucleo stm32f746ZG and Disco stm32f746NG works under arm and gcc environment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DieterGraef | 0:d26c1b55cfca | 1 | /** |
DieterGraef | 0:d26c1b55cfca | 2 | * @file |
DieterGraef | 0:d26c1b55cfca | 3 | * Address Resolution Protocol module for IP over Ethernet |
DieterGraef | 0:d26c1b55cfca | 4 | * |
DieterGraef | 0:d26c1b55cfca | 5 | * Functionally, ARP is divided into two parts. The first maps an IP address |
DieterGraef | 0:d26c1b55cfca | 6 | * to a physical address when sending a packet, and the second part answers |
DieterGraef | 0:d26c1b55cfca | 7 | * requests from other machines for our physical address. |
DieterGraef | 0:d26c1b55cfca | 8 | * |
DieterGraef | 0:d26c1b55cfca | 9 | * This implementation complies with RFC 826 (Ethernet ARP). It supports |
DieterGraef | 0:d26c1b55cfca | 10 | * Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6 |
DieterGraef | 0:d26c1b55cfca | 11 | * if an interface calls etharp_gratuitous(our_netif) upon address change. |
DieterGraef | 0:d26c1b55cfca | 12 | */ |
DieterGraef | 0:d26c1b55cfca | 13 | |
DieterGraef | 0:d26c1b55cfca | 14 | /* |
DieterGraef | 0:d26c1b55cfca | 15 | * Copyright (c) 2001-2003 Swedish Institute of Computer Science. |
DieterGraef | 0:d26c1b55cfca | 16 | * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv> |
DieterGraef | 0:d26c1b55cfca | 17 | * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands. |
DieterGraef | 0:d26c1b55cfca | 18 | * All rights reserved. |
DieterGraef | 0:d26c1b55cfca | 19 | * |
DieterGraef | 0:d26c1b55cfca | 20 | * Redistribution and use in source and binary forms, with or without modification, |
DieterGraef | 0:d26c1b55cfca | 21 | * are permitted provided that the following conditions are met: |
DieterGraef | 0:d26c1b55cfca | 22 | * |
DieterGraef | 0:d26c1b55cfca | 23 | * 1. Redistributions of source code must retain the above copyright notice, |
DieterGraef | 0:d26c1b55cfca | 24 | * this list of conditions and the following disclaimer. |
DieterGraef | 0:d26c1b55cfca | 25 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
DieterGraef | 0:d26c1b55cfca | 26 | * this list of conditions and the following disclaimer in the documentation |
DieterGraef | 0:d26c1b55cfca | 27 | * and/or other materials provided with the distribution. |
DieterGraef | 0:d26c1b55cfca | 28 | * 3. The name of the author may not be used to endorse or promote products |
DieterGraef | 0:d26c1b55cfca | 29 | * derived from this software without specific prior written permission. |
DieterGraef | 0:d26c1b55cfca | 30 | * |
DieterGraef | 0:d26c1b55cfca | 31 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
DieterGraef | 0:d26c1b55cfca | 32 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
DieterGraef | 0:d26c1b55cfca | 33 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
DieterGraef | 0:d26c1b55cfca | 34 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
DieterGraef | 0:d26c1b55cfca | 35 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
DieterGraef | 0:d26c1b55cfca | 36 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
DieterGraef | 0:d26c1b55cfca | 37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
DieterGraef | 0:d26c1b55cfca | 38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
DieterGraef | 0:d26c1b55cfca | 39 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
DieterGraef | 0:d26c1b55cfca | 40 | * OF SUCH DAMAGE. |
DieterGraef | 0:d26c1b55cfca | 41 | * |
DieterGraef | 0:d26c1b55cfca | 42 | * This file is part of the lwIP TCP/IP stack. |
DieterGraef | 0:d26c1b55cfca | 43 | * |
DieterGraef | 0:d26c1b55cfca | 44 | */ |
DieterGraef | 0:d26c1b55cfca | 45 | |
DieterGraef | 0:d26c1b55cfca | 46 | #include "lwip/opt.h" |
DieterGraef | 0:d26c1b55cfca | 47 | |
DieterGraef | 0:d26c1b55cfca | 48 | #if LWIP_ARP || LWIP_ETHERNET |
DieterGraef | 0:d26c1b55cfca | 49 | |
DieterGraef | 0:d26c1b55cfca | 50 | #include "lwip/ip_addr.h" |
DieterGraef | 0:d26c1b55cfca | 51 | #include "lwip/def.h" |
DieterGraef | 0:d26c1b55cfca | 52 | #include "lwip/ip.h" |
DieterGraef | 0:d26c1b55cfca | 53 | #include "lwip/stats.h" |
DieterGraef | 0:d26c1b55cfca | 54 | #include "lwip/snmp.h" |
DieterGraef | 0:d26c1b55cfca | 55 | #include "lwip/dhcp.h" |
DieterGraef | 0:d26c1b55cfca | 56 | #include "lwip/autoip.h" |
DieterGraef | 0:d26c1b55cfca | 57 | #include "netif/etharp.h" |
DieterGraef | 0:d26c1b55cfca | 58 | |
DieterGraef | 0:d26c1b55cfca | 59 | #if PPPOE_SUPPORT |
DieterGraef | 0:d26c1b55cfca | 60 | #include "netif/ppp_oe.h" |
DieterGraef | 0:d26c1b55cfca | 61 | #endif /* PPPOE_SUPPORT */ |
DieterGraef | 0:d26c1b55cfca | 62 | |
DieterGraef | 0:d26c1b55cfca | 63 | #include <string.h> |
DieterGraef | 0:d26c1b55cfca | 64 | |
DieterGraef | 0:d26c1b55cfca | 65 | const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}}; |
DieterGraef | 0:d26c1b55cfca | 66 | const struct eth_addr ethzero = {{0,0,0,0,0,0}}; |
DieterGraef | 0:d26c1b55cfca | 67 | |
DieterGraef | 0:d26c1b55cfca | 68 | /** The 24-bit IANA multicast OUI is 01-00-5e: */ |
DieterGraef | 0:d26c1b55cfca | 69 | #define LL_MULTICAST_ADDR_0 0x01 |
DieterGraef | 0:d26c1b55cfca | 70 | #define LL_MULTICAST_ADDR_1 0x00 |
DieterGraef | 0:d26c1b55cfca | 71 | #define LL_MULTICAST_ADDR_2 0x5e |
DieterGraef | 0:d26c1b55cfca | 72 | |
DieterGraef | 0:d26c1b55cfca | 73 | #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */ |
DieterGraef | 0:d26c1b55cfca | 74 | |
DieterGraef | 0:d26c1b55cfca | 75 | /** the time an ARP entry stays valid after its last update, |
DieterGraef | 0:d26c1b55cfca | 76 | * for ARP_TMR_INTERVAL = 5000, this is |
DieterGraef | 0:d26c1b55cfca | 77 | * (240 * 5) seconds = 20 minutes. |
DieterGraef | 0:d26c1b55cfca | 78 | */ |
DieterGraef | 0:d26c1b55cfca | 79 | #define ARP_MAXAGE 240 |
DieterGraef | 0:d26c1b55cfca | 80 | /** Re-request a used ARP entry 1 minute before it would expire to prevent |
DieterGraef | 0:d26c1b55cfca | 81 | * breaking a steadily used connection because the ARP entry timed out. */ |
DieterGraef | 0:d26c1b55cfca | 82 | #define ARP_AGE_REREQUEST_USED (ARP_MAXAGE - 12) |
DieterGraef | 0:d26c1b55cfca | 83 | |
DieterGraef | 0:d26c1b55cfca | 84 | /** the time an ARP entry stays pending after first request, |
DieterGraef | 0:d26c1b55cfca | 85 | * for ARP_TMR_INTERVAL = 5000, this is |
DieterGraef | 0:d26c1b55cfca | 86 | * (2 * 5) seconds = 10 seconds. |
DieterGraef | 0:d26c1b55cfca | 87 | * |
DieterGraef | 0:d26c1b55cfca | 88 | * @internal Keep this number at least 2, otherwise it might |
DieterGraef | 0:d26c1b55cfca | 89 | * run out instantly if the timeout occurs directly after a request. |
DieterGraef | 0:d26c1b55cfca | 90 | */ |
DieterGraef | 0:d26c1b55cfca | 91 | #define ARP_MAXPENDING 2 |
DieterGraef | 0:d26c1b55cfca | 92 | |
DieterGraef | 0:d26c1b55cfca | 93 | #define HWTYPE_ETHERNET 1 |
DieterGraef | 0:d26c1b55cfca | 94 | |
DieterGraef | 0:d26c1b55cfca | 95 | enum etharp_state { |
DieterGraef | 0:d26c1b55cfca | 96 | ETHARP_STATE_EMPTY = 0, |
DieterGraef | 0:d26c1b55cfca | 97 | ETHARP_STATE_PENDING, |
DieterGraef | 0:d26c1b55cfca | 98 | ETHARP_STATE_STABLE, |
DieterGraef | 0:d26c1b55cfca | 99 | ETHARP_STATE_STABLE_REREQUESTING |
DieterGraef | 0:d26c1b55cfca | 100 | #if ETHARP_SUPPORT_STATIC_ENTRIES |
DieterGraef | 0:d26c1b55cfca | 101 | ,ETHARP_STATE_STATIC |
DieterGraef | 0:d26c1b55cfca | 102 | #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ |
DieterGraef | 0:d26c1b55cfca | 103 | }; |
DieterGraef | 0:d26c1b55cfca | 104 | |
DieterGraef | 0:d26c1b55cfca | 105 | struct etharp_entry { |
DieterGraef | 0:d26c1b55cfca | 106 | #if ARP_QUEUEING |
DieterGraef | 0:d26c1b55cfca | 107 | /** Pointer to queue of pending outgoing packets on this ARP entry. */ |
DieterGraef | 0:d26c1b55cfca | 108 | struct etharp_q_entry *q; |
DieterGraef | 0:d26c1b55cfca | 109 | #else /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 110 | /** Pointer to a single pending outgoing packet on this ARP entry. */ |
DieterGraef | 0:d26c1b55cfca | 111 | struct pbuf *q; |
DieterGraef | 0:d26c1b55cfca | 112 | #endif /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 113 | ip_addr_t ipaddr; |
DieterGraef | 0:d26c1b55cfca | 114 | struct netif *netif; |
DieterGraef | 0:d26c1b55cfca | 115 | struct eth_addr ethaddr; |
DieterGraef | 0:d26c1b55cfca | 116 | u8_t state; |
DieterGraef | 0:d26c1b55cfca | 117 | u8_t ctime; |
DieterGraef | 0:d26c1b55cfca | 118 | }; |
DieterGraef | 0:d26c1b55cfca | 119 | |
DieterGraef | 0:d26c1b55cfca | 120 | static struct etharp_entry arp_table[ARP_TABLE_SIZE]; |
DieterGraef | 0:d26c1b55cfca | 121 | |
DieterGraef | 0:d26c1b55cfca | 122 | #if !LWIP_NETIF_HWADDRHINT |
DieterGraef | 0:d26c1b55cfca | 123 | static u8_t etharp_cached_entry; |
DieterGraef | 0:d26c1b55cfca | 124 | #endif /* !LWIP_NETIF_HWADDRHINT */ |
DieterGraef | 0:d26c1b55cfca | 125 | |
DieterGraef | 0:d26c1b55cfca | 126 | /** Try hard to create a new entry - we want the IP address to appear in |
DieterGraef | 0:d26c1b55cfca | 127 | the cache (even if this means removing an active entry or so). */ |
DieterGraef | 0:d26c1b55cfca | 128 | #define ETHARP_FLAG_TRY_HARD 1 |
DieterGraef | 0:d26c1b55cfca | 129 | #define ETHARP_FLAG_FIND_ONLY 2 |
DieterGraef | 0:d26c1b55cfca | 130 | #if ETHARP_SUPPORT_STATIC_ENTRIES |
DieterGraef | 0:d26c1b55cfca | 131 | #define ETHARP_FLAG_STATIC_ENTRY 4 |
DieterGraef | 0:d26c1b55cfca | 132 | #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ |
DieterGraef | 0:d26c1b55cfca | 133 | |
DieterGraef | 0:d26c1b55cfca | 134 | #if LWIP_NETIF_HWADDRHINT |
DieterGraef | 0:d26c1b55cfca | 135 | #define ETHARP_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \ |
DieterGraef | 0:d26c1b55cfca | 136 | *((netif)->addr_hint) = (hint); |
DieterGraef | 0:d26c1b55cfca | 137 | #else /* LWIP_NETIF_HWADDRHINT */ |
DieterGraef | 0:d26c1b55cfca | 138 | #define ETHARP_SET_HINT(netif, hint) (etharp_cached_entry = (hint)) |
DieterGraef | 0:d26c1b55cfca | 139 | #endif /* LWIP_NETIF_HWADDRHINT */ |
DieterGraef | 0:d26c1b55cfca | 140 | |
DieterGraef | 0:d26c1b55cfca | 141 | |
DieterGraef | 0:d26c1b55cfca | 142 | /* Some checks, instead of etharp_init(): */ |
DieterGraef | 0:d26c1b55cfca | 143 | #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f)) |
DieterGraef | 0:d26c1b55cfca | 144 | #error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h" |
DieterGraef | 0:d26c1b55cfca | 145 | #endif |
DieterGraef | 0:d26c1b55cfca | 146 | |
DieterGraef | 0:d26c1b55cfca | 147 | |
DieterGraef | 0:d26c1b55cfca | 148 | #if ARP_QUEUEING |
DieterGraef | 0:d26c1b55cfca | 149 | /** |
DieterGraef | 0:d26c1b55cfca | 150 | * Free a complete queue of etharp entries |
DieterGraef | 0:d26c1b55cfca | 151 | * |
DieterGraef | 0:d26c1b55cfca | 152 | * @param q a qeueue of etharp_q_entry's to free |
DieterGraef | 0:d26c1b55cfca | 153 | */ |
DieterGraef | 0:d26c1b55cfca | 154 | static void |
DieterGraef | 0:d26c1b55cfca | 155 | free_etharp_q(struct etharp_q_entry *q) |
DieterGraef | 0:d26c1b55cfca | 156 | { |
DieterGraef | 0:d26c1b55cfca | 157 | struct etharp_q_entry *r; |
DieterGraef | 0:d26c1b55cfca | 158 | LWIP_ASSERT("q != NULL", q != NULL); |
DieterGraef | 0:d26c1b55cfca | 159 | LWIP_ASSERT("q->p != NULL", q->p != NULL); |
DieterGraef | 0:d26c1b55cfca | 160 | while (q) { |
DieterGraef | 0:d26c1b55cfca | 161 | r = q; |
DieterGraef | 0:d26c1b55cfca | 162 | q = q->next; |
DieterGraef | 0:d26c1b55cfca | 163 | LWIP_ASSERT("r->p != NULL", (r->p != NULL)); |
DieterGraef | 0:d26c1b55cfca | 164 | pbuf_free(r->p); |
DieterGraef | 0:d26c1b55cfca | 165 | memp_free(MEMP_ARP_QUEUE, r); |
DieterGraef | 0:d26c1b55cfca | 166 | } |
DieterGraef | 0:d26c1b55cfca | 167 | } |
DieterGraef | 0:d26c1b55cfca | 168 | #else /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 169 | |
DieterGraef | 0:d26c1b55cfca | 170 | /** Compatibility define: free the queued pbuf */ |
DieterGraef | 0:d26c1b55cfca | 171 | #define free_etharp_q(q) pbuf_free(q) |
DieterGraef | 0:d26c1b55cfca | 172 | |
DieterGraef | 0:d26c1b55cfca | 173 | #endif /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 174 | |
DieterGraef | 0:d26c1b55cfca | 175 | /** Clean up ARP table entries */ |
DieterGraef | 0:d26c1b55cfca | 176 | static void |
DieterGraef | 0:d26c1b55cfca | 177 | etharp_free_entry(int i) |
DieterGraef | 0:d26c1b55cfca | 178 | { |
DieterGraef | 0:d26c1b55cfca | 179 | /* remove from SNMP ARP index tree */ |
DieterGraef | 0:d26c1b55cfca | 180 | snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr); |
DieterGraef | 0:d26c1b55cfca | 181 | /* and empty packet queue */ |
DieterGraef | 0:d26c1b55cfca | 182 | if (arp_table[i].q != NULL) { |
DieterGraef | 0:d26c1b55cfca | 183 | /* remove all queued packets */ |
DieterGraef | 0:d26c1b55cfca | 184 | LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); |
DieterGraef | 0:d26c1b55cfca | 185 | free_etharp_q(arp_table[i].q); |
DieterGraef | 0:d26c1b55cfca | 186 | arp_table[i].q = NULL; |
DieterGraef | 0:d26c1b55cfca | 187 | } |
DieterGraef | 0:d26c1b55cfca | 188 | /* recycle entry for re-use */ |
DieterGraef | 0:d26c1b55cfca | 189 | arp_table[i].state = ETHARP_STATE_EMPTY; |
DieterGraef | 0:d26c1b55cfca | 190 | #ifdef LWIP_DEBUG |
DieterGraef | 0:d26c1b55cfca | 191 | /* for debugging, clean out the complete entry */ |
DieterGraef | 0:d26c1b55cfca | 192 | arp_table[i].ctime = 0; |
DieterGraef | 0:d26c1b55cfca | 193 | arp_table[i].netif = NULL; |
DieterGraef | 0:d26c1b55cfca | 194 | ip_addr_set_zero(&arp_table[i].ipaddr); |
DieterGraef | 0:d26c1b55cfca | 195 | arp_table[i].ethaddr = ethzero; |
DieterGraef | 0:d26c1b55cfca | 196 | #endif /* LWIP_DEBUG */ |
DieterGraef | 0:d26c1b55cfca | 197 | } |
DieterGraef | 0:d26c1b55cfca | 198 | |
DieterGraef | 0:d26c1b55cfca | 199 | /** |
DieterGraef | 0:d26c1b55cfca | 200 | * Clears expired entries in the ARP table. |
DieterGraef | 0:d26c1b55cfca | 201 | * |
DieterGraef | 0:d26c1b55cfca | 202 | * This function should be called every ETHARP_TMR_INTERVAL milliseconds (5 seconds), |
DieterGraef | 0:d26c1b55cfca | 203 | * in order to expire entries in the ARP table. |
DieterGraef | 0:d26c1b55cfca | 204 | */ |
DieterGraef | 0:d26c1b55cfca | 205 | void |
DieterGraef | 0:d26c1b55cfca | 206 | etharp_tmr(void) |
DieterGraef | 0:d26c1b55cfca | 207 | { |
DieterGraef | 0:d26c1b55cfca | 208 | u8_t i; |
DieterGraef | 0:d26c1b55cfca | 209 | |
DieterGraef | 0:d26c1b55cfca | 210 | LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n")); |
DieterGraef | 0:d26c1b55cfca | 211 | /* remove expired entries from the ARP table */ |
DieterGraef | 0:d26c1b55cfca | 212 | for (i = 0; i < ARP_TABLE_SIZE; ++i) { |
DieterGraef | 0:d26c1b55cfca | 213 | u8_t state = arp_table[i].state; |
DieterGraef | 0:d26c1b55cfca | 214 | if (state != ETHARP_STATE_EMPTY |
DieterGraef | 0:d26c1b55cfca | 215 | #if ETHARP_SUPPORT_STATIC_ENTRIES |
DieterGraef | 0:d26c1b55cfca | 216 | && (state != ETHARP_STATE_STATIC) |
DieterGraef | 0:d26c1b55cfca | 217 | #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ |
DieterGraef | 0:d26c1b55cfca | 218 | ) { |
DieterGraef | 0:d26c1b55cfca | 219 | arp_table[i].ctime++; |
DieterGraef | 0:d26c1b55cfca | 220 | if ((arp_table[i].ctime >= ARP_MAXAGE) || |
DieterGraef | 0:d26c1b55cfca | 221 | ((arp_table[i].state == ETHARP_STATE_PENDING) && |
DieterGraef | 0:d26c1b55cfca | 222 | (arp_table[i].ctime >= ARP_MAXPENDING))) { |
DieterGraef | 0:d26c1b55cfca | 223 | /* pending or stable entry has become old! */ |
DieterGraef | 0:d26c1b55cfca | 224 | LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n", |
DieterGraef | 0:d26c1b55cfca | 225 | arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 226 | /* clean up entries that have just been expired */ |
DieterGraef | 0:d26c1b55cfca | 227 | etharp_free_entry(i); |
DieterGraef | 0:d26c1b55cfca | 228 | } |
DieterGraef | 0:d26c1b55cfca | 229 | else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING) { |
DieterGraef | 0:d26c1b55cfca | 230 | /* Reset state to stable, so that the next transmitted packet will |
DieterGraef | 0:d26c1b55cfca | 231 | re-send an ARP request. */ |
DieterGraef | 0:d26c1b55cfca | 232 | arp_table[i].state = ETHARP_STATE_STABLE; |
DieterGraef | 0:d26c1b55cfca | 233 | } |
DieterGraef | 0:d26c1b55cfca | 234 | #if ARP_QUEUEING |
DieterGraef | 0:d26c1b55cfca | 235 | /* still pending entry? (not expired) */ |
DieterGraef | 0:d26c1b55cfca | 236 | if (arp_table[i].state == ETHARP_STATE_PENDING) { |
DieterGraef | 0:d26c1b55cfca | 237 | /* resend an ARP query here? */ |
DieterGraef | 0:d26c1b55cfca | 238 | } |
DieterGraef | 0:d26c1b55cfca | 239 | #endif /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 240 | } |
DieterGraef | 0:d26c1b55cfca | 241 | } |
DieterGraef | 0:d26c1b55cfca | 242 | } |
DieterGraef | 0:d26c1b55cfca | 243 | |
DieterGraef | 0:d26c1b55cfca | 244 | /** |
DieterGraef | 0:d26c1b55cfca | 245 | * Search the ARP table for a matching or new entry. |
DieterGraef | 0:d26c1b55cfca | 246 | * |
DieterGraef | 0:d26c1b55cfca | 247 | * If an IP address is given, return a pending or stable ARP entry that matches |
DieterGraef | 0:d26c1b55cfca | 248 | * the address. If no match is found, create a new entry with this address set, |
DieterGraef | 0:d26c1b55cfca | 249 | * but in state ETHARP_EMPTY. The caller must check and possibly change the |
DieterGraef | 0:d26c1b55cfca | 250 | * state of the returned entry. |
DieterGraef | 0:d26c1b55cfca | 251 | * |
DieterGraef | 0:d26c1b55cfca | 252 | * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY. |
DieterGraef | 0:d26c1b55cfca | 253 | * |
DieterGraef | 0:d26c1b55cfca | 254 | * In all cases, attempt to create new entries from an empty entry. If no |
DieterGraef | 0:d26c1b55cfca | 255 | * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle |
DieterGraef | 0:d26c1b55cfca | 256 | * old entries. Heuristic choose the least important entry for recycling. |
DieterGraef | 0:d26c1b55cfca | 257 | * |
DieterGraef | 0:d26c1b55cfca | 258 | * @param ipaddr IP address to find in ARP cache, or to add if not found. |
DieterGraef | 0:d26c1b55cfca | 259 | * @param flags @see definition of ETHARP_FLAG_* |
DieterGraef | 0:d26c1b55cfca | 260 | * @param netif netif related to this address (used for NETIF_HWADDRHINT) |
DieterGraef | 0:d26c1b55cfca | 261 | * |
DieterGraef | 0:d26c1b55cfca | 262 | * @return The ARP entry index that matched or is created, ERR_MEM if no |
DieterGraef | 0:d26c1b55cfca | 263 | * entry is found or could be recycled. |
DieterGraef | 0:d26c1b55cfca | 264 | */ |
DieterGraef | 0:d26c1b55cfca | 265 | static s8_t |
DieterGraef | 0:d26c1b55cfca | 266 | etharp_find_entry(ip_addr_t *ipaddr, u8_t flags) |
DieterGraef | 0:d26c1b55cfca | 267 | { |
DieterGraef | 0:d26c1b55cfca | 268 | s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE; |
DieterGraef | 0:d26c1b55cfca | 269 | s8_t empty = ARP_TABLE_SIZE; |
DieterGraef | 0:d26c1b55cfca | 270 | u8_t i = 0, age_pending = 0, age_stable = 0; |
DieterGraef | 0:d26c1b55cfca | 271 | /* oldest entry with packets on queue */ |
DieterGraef | 0:d26c1b55cfca | 272 | s8_t old_queue = ARP_TABLE_SIZE; |
DieterGraef | 0:d26c1b55cfca | 273 | /* its age */ |
DieterGraef | 0:d26c1b55cfca | 274 | u8_t age_queue = 0; |
DieterGraef | 0:d26c1b55cfca | 275 | |
DieterGraef | 0:d26c1b55cfca | 276 | /** |
DieterGraef | 0:d26c1b55cfca | 277 | * a) do a search through the cache, remember candidates |
DieterGraef | 0:d26c1b55cfca | 278 | * b) select candidate entry |
DieterGraef | 0:d26c1b55cfca | 279 | * c) create new entry |
DieterGraef | 0:d26c1b55cfca | 280 | */ |
DieterGraef | 0:d26c1b55cfca | 281 | |
DieterGraef | 0:d26c1b55cfca | 282 | /* a) in a single search sweep, do all of this |
DieterGraef | 0:d26c1b55cfca | 283 | * 1) remember the first empty entry (if any) |
DieterGraef | 0:d26c1b55cfca | 284 | * 2) remember the oldest stable entry (if any) |
DieterGraef | 0:d26c1b55cfca | 285 | * 3) remember the oldest pending entry without queued packets (if any) |
DieterGraef | 0:d26c1b55cfca | 286 | * 4) remember the oldest pending entry with queued packets (if any) |
DieterGraef | 0:d26c1b55cfca | 287 | * 5) search for a matching IP entry, either pending or stable |
DieterGraef | 0:d26c1b55cfca | 288 | * until 5 matches, or all entries are searched for. |
DieterGraef | 0:d26c1b55cfca | 289 | */ |
DieterGraef | 0:d26c1b55cfca | 290 | |
DieterGraef | 0:d26c1b55cfca | 291 | for (i = 0; i < ARP_TABLE_SIZE; ++i) { |
DieterGraef | 0:d26c1b55cfca | 292 | u8_t state = arp_table[i].state; |
DieterGraef | 0:d26c1b55cfca | 293 | /* no empty entry found yet and now we do find one? */ |
DieterGraef | 0:d26c1b55cfca | 294 | if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) { |
DieterGraef | 0:d26c1b55cfca | 295 | LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_find_entry: found empty entry %"U16_F"\n", (u16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 296 | /* remember first empty entry */ |
DieterGraef | 0:d26c1b55cfca | 297 | empty = i; |
DieterGraef | 0:d26c1b55cfca | 298 | } else if (state != ETHARP_STATE_EMPTY) { |
DieterGraef | 0:d26c1b55cfca | 299 | LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE", |
DieterGraef | 0:d26c1b55cfca | 300 | state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE); |
DieterGraef | 0:d26c1b55cfca | 301 | /* if given, does IP address match IP address in ARP entry? */ |
DieterGraef | 0:d26c1b55cfca | 302 | if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) { |
DieterGraef | 0:d26c1b55cfca | 303 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %"U16_F"\n", (u16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 304 | /* found exact IP address match, simply bail out */ |
DieterGraef | 0:d26c1b55cfca | 305 | return i; |
DieterGraef | 0:d26c1b55cfca | 306 | } |
DieterGraef | 0:d26c1b55cfca | 307 | /* pending entry? */ |
DieterGraef | 0:d26c1b55cfca | 308 | if (state == ETHARP_STATE_PENDING) { |
DieterGraef | 0:d26c1b55cfca | 309 | /* pending with queued packets? */ |
DieterGraef | 0:d26c1b55cfca | 310 | if (arp_table[i].q != NULL) { |
DieterGraef | 0:d26c1b55cfca | 311 | if (arp_table[i].ctime >= age_queue) { |
DieterGraef | 0:d26c1b55cfca | 312 | old_queue = i; |
DieterGraef | 0:d26c1b55cfca | 313 | age_queue = arp_table[i].ctime; |
DieterGraef | 0:d26c1b55cfca | 314 | } |
DieterGraef | 0:d26c1b55cfca | 315 | } else |
DieterGraef | 0:d26c1b55cfca | 316 | /* pending without queued packets? */ |
DieterGraef | 0:d26c1b55cfca | 317 | { |
DieterGraef | 0:d26c1b55cfca | 318 | if (arp_table[i].ctime >= age_pending) { |
DieterGraef | 0:d26c1b55cfca | 319 | old_pending = i; |
DieterGraef | 0:d26c1b55cfca | 320 | age_pending = arp_table[i].ctime; |
DieterGraef | 0:d26c1b55cfca | 321 | } |
DieterGraef | 0:d26c1b55cfca | 322 | } |
DieterGraef | 0:d26c1b55cfca | 323 | /* stable entry? */ |
DieterGraef | 0:d26c1b55cfca | 324 | } else if (state >= ETHARP_STATE_STABLE) { |
DieterGraef | 0:d26c1b55cfca | 325 | #if ETHARP_SUPPORT_STATIC_ENTRIES |
DieterGraef | 0:d26c1b55cfca | 326 | /* don't record old_stable for static entries since they never expire */ |
DieterGraef | 0:d26c1b55cfca | 327 | if (state < ETHARP_STATE_STATIC) |
DieterGraef | 0:d26c1b55cfca | 328 | #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ |
DieterGraef | 0:d26c1b55cfca | 329 | { |
DieterGraef | 0:d26c1b55cfca | 330 | /* remember entry with oldest stable entry in oldest, its age in maxtime */ |
DieterGraef | 0:d26c1b55cfca | 331 | if (arp_table[i].ctime >= age_stable) { |
DieterGraef | 0:d26c1b55cfca | 332 | old_stable = i; |
DieterGraef | 0:d26c1b55cfca | 333 | age_stable = arp_table[i].ctime; |
DieterGraef | 0:d26c1b55cfca | 334 | } |
DieterGraef | 0:d26c1b55cfca | 335 | } |
DieterGraef | 0:d26c1b55cfca | 336 | } |
DieterGraef | 0:d26c1b55cfca | 337 | } |
DieterGraef | 0:d26c1b55cfca | 338 | } |
DieterGraef | 0:d26c1b55cfca | 339 | /* { we have no match } => try to create a new entry */ |
DieterGraef | 0:d26c1b55cfca | 340 | |
DieterGraef | 0:d26c1b55cfca | 341 | /* don't create new entry, only search? */ |
DieterGraef | 0:d26c1b55cfca | 342 | if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) || |
DieterGraef | 0:d26c1b55cfca | 343 | /* or no empty entry found and not allowed to recycle? */ |
DieterGraef | 0:d26c1b55cfca | 344 | ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) { |
DieterGraef | 0:d26c1b55cfca | 345 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n")); |
DieterGraef | 0:d26c1b55cfca | 346 | return (s8_t)ERR_MEM; |
DieterGraef | 0:d26c1b55cfca | 347 | } |
DieterGraef | 0:d26c1b55cfca | 348 | |
DieterGraef | 0:d26c1b55cfca | 349 | /* b) choose the least destructive entry to recycle: |
DieterGraef | 0:d26c1b55cfca | 350 | * 1) empty entry |
DieterGraef | 0:d26c1b55cfca | 351 | * 2) oldest stable entry |
DieterGraef | 0:d26c1b55cfca | 352 | * 3) oldest pending entry without queued packets |
DieterGraef | 0:d26c1b55cfca | 353 | * 4) oldest pending entry with queued packets |
DieterGraef | 0:d26c1b55cfca | 354 | * |
DieterGraef | 0:d26c1b55cfca | 355 | * { ETHARP_FLAG_TRY_HARD is set at this point } |
DieterGraef | 0:d26c1b55cfca | 356 | */ |
DieterGraef | 0:d26c1b55cfca | 357 | |
DieterGraef | 0:d26c1b55cfca | 358 | /* 1) empty entry available? */ |
DieterGraef | 0:d26c1b55cfca | 359 | if (empty < ARP_TABLE_SIZE) { |
DieterGraef | 0:d26c1b55cfca | 360 | i = empty; |
DieterGraef | 0:d26c1b55cfca | 361 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting empty entry %"U16_F"\n", (u16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 362 | } else { |
DieterGraef | 0:d26c1b55cfca | 363 | /* 2) found recyclable stable entry? */ |
DieterGraef | 0:d26c1b55cfca | 364 | if (old_stable < ARP_TABLE_SIZE) { |
DieterGraef | 0:d26c1b55cfca | 365 | /* recycle oldest stable*/ |
DieterGraef | 0:d26c1b55cfca | 366 | i = old_stable; |
DieterGraef | 0:d26c1b55cfca | 367 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 368 | /* no queued packets should exist on stable entries */ |
DieterGraef | 0:d26c1b55cfca | 369 | LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL); |
DieterGraef | 0:d26c1b55cfca | 370 | /* 3) found recyclable pending entry without queued packets? */ |
DieterGraef | 0:d26c1b55cfca | 371 | } else if (old_pending < ARP_TABLE_SIZE) { |
DieterGraef | 0:d26c1b55cfca | 372 | /* recycle oldest pending */ |
DieterGraef | 0:d26c1b55cfca | 373 | i = old_pending; |
DieterGraef | 0:d26c1b55cfca | 374 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 375 | /* 4) found recyclable pending entry with queued packets? */ |
DieterGraef | 0:d26c1b55cfca | 376 | } else if (old_queue < ARP_TABLE_SIZE) { |
DieterGraef | 0:d26c1b55cfca | 377 | /* recycle oldest pending (queued packets are free in etharp_free_entry) */ |
DieterGraef | 0:d26c1b55cfca | 378 | i = old_queue; |
DieterGraef | 0:d26c1b55cfca | 379 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %"U16_F", freeing packet queue %p\n", (u16_t)i, (void *)(arp_table[i].q))); |
DieterGraef | 0:d26c1b55cfca | 380 | /* no empty or recyclable entries found */ |
DieterGraef | 0:d26c1b55cfca | 381 | } else { |
DieterGraef | 0:d26c1b55cfca | 382 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n")); |
DieterGraef | 0:d26c1b55cfca | 383 | return (s8_t)ERR_MEM; |
DieterGraef | 0:d26c1b55cfca | 384 | } |
DieterGraef | 0:d26c1b55cfca | 385 | |
DieterGraef | 0:d26c1b55cfca | 386 | /* { empty or recyclable entry found } */ |
DieterGraef | 0:d26c1b55cfca | 387 | LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); |
DieterGraef | 0:d26c1b55cfca | 388 | etharp_free_entry(i); |
DieterGraef | 0:d26c1b55cfca | 389 | } |
DieterGraef | 0:d26c1b55cfca | 390 | |
DieterGraef | 0:d26c1b55cfca | 391 | LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); |
DieterGraef | 0:d26c1b55cfca | 392 | LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY", |
DieterGraef | 0:d26c1b55cfca | 393 | arp_table[i].state == ETHARP_STATE_EMPTY); |
DieterGraef | 0:d26c1b55cfca | 394 | |
DieterGraef | 0:d26c1b55cfca | 395 | /* IP address given? */ |
DieterGraef | 0:d26c1b55cfca | 396 | if (ipaddr != NULL) { |
DieterGraef | 0:d26c1b55cfca | 397 | /* set IP address */ |
DieterGraef | 0:d26c1b55cfca | 398 | ip_addr_copy(arp_table[i].ipaddr, *ipaddr); |
DieterGraef | 0:d26c1b55cfca | 399 | } |
DieterGraef | 0:d26c1b55cfca | 400 | arp_table[i].ctime = 0; |
DieterGraef | 0:d26c1b55cfca | 401 | return (err_t)i; |
DieterGraef | 0:d26c1b55cfca | 402 | } |
DieterGraef | 0:d26c1b55cfca | 403 | |
DieterGraef | 0:d26c1b55cfca | 404 | /** |
DieterGraef | 0:d26c1b55cfca | 405 | * Send an IP packet on the network using netif->linkoutput |
DieterGraef | 0:d26c1b55cfca | 406 | * The ethernet header is filled in before sending. |
DieterGraef | 0:d26c1b55cfca | 407 | * |
DieterGraef | 0:d26c1b55cfca | 408 | * @params netif the lwIP network interface on which to send the packet |
DieterGraef | 0:d26c1b55cfca | 409 | * @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header |
DieterGraef | 0:d26c1b55cfca | 410 | * @params src the source MAC address to be copied into the ethernet header |
DieterGraef | 0:d26c1b55cfca | 411 | * @params dst the destination MAC address to be copied into the ethernet header |
DieterGraef | 0:d26c1b55cfca | 412 | * @return ERR_OK if the packet was sent, any other err_t on failure |
DieterGraef | 0:d26c1b55cfca | 413 | */ |
DieterGraef | 0:d26c1b55cfca | 414 | static err_t |
DieterGraef | 0:d26c1b55cfca | 415 | etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst) |
DieterGraef | 0:d26c1b55cfca | 416 | { |
DieterGraef | 0:d26c1b55cfca | 417 | struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload; |
DieterGraef | 0:d26c1b55cfca | 418 | |
DieterGraef | 0:d26c1b55cfca | 419 | LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!", |
DieterGraef | 0:d26c1b55cfca | 420 | (netif->hwaddr_len == ETHARP_HWADDR_LEN)); |
DieterGraef | 0:d26c1b55cfca | 421 | ETHADDR32_COPY(ðhdr->dest, dst); |
DieterGraef | 0:d26c1b55cfca | 422 | ETHADDR16_COPY(ðhdr->src, src); |
DieterGraef | 0:d26c1b55cfca | 423 | ethhdr->type = PP_HTONS(ETHTYPE_IP); |
DieterGraef | 0:d26c1b55cfca | 424 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_send_ip: sending packet %p\n", (void *)p)); |
DieterGraef | 0:d26c1b55cfca | 425 | /* send the packet */ |
DieterGraef | 0:d26c1b55cfca | 426 | return netif->linkoutput(netif, p); |
DieterGraef | 0:d26c1b55cfca | 427 | } |
DieterGraef | 0:d26c1b55cfca | 428 | |
DieterGraef | 0:d26c1b55cfca | 429 | /** |
DieterGraef | 0:d26c1b55cfca | 430 | * Update (or insert) a IP/MAC address pair in the ARP cache. |
DieterGraef | 0:d26c1b55cfca | 431 | * |
DieterGraef | 0:d26c1b55cfca | 432 | * If a pending entry is resolved, any queued packets will be sent |
DieterGraef | 0:d26c1b55cfca | 433 | * at this point. |
DieterGraef | 0:d26c1b55cfca | 434 | * |
DieterGraef | 0:d26c1b55cfca | 435 | * @param netif netif related to this entry (used for NETIF_ADDRHINT) |
DieterGraef | 0:d26c1b55cfca | 436 | * @param ipaddr IP address of the inserted ARP entry. |
DieterGraef | 0:d26c1b55cfca | 437 | * @param ethaddr Ethernet address of the inserted ARP entry. |
DieterGraef | 0:d26c1b55cfca | 438 | * @param flags @see definition of ETHARP_FLAG_* |
DieterGraef | 0:d26c1b55cfca | 439 | * |
DieterGraef | 0:d26c1b55cfca | 440 | * @return |
DieterGraef | 0:d26c1b55cfca | 441 | * - ERR_OK Succesfully updated ARP cache. |
DieterGraef | 0:d26c1b55cfca | 442 | * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set. |
DieterGraef | 0:d26c1b55cfca | 443 | * - ERR_ARG Non-unicast address given, those will not appear in ARP cache. |
DieterGraef | 0:d26c1b55cfca | 444 | * |
DieterGraef | 0:d26c1b55cfca | 445 | * @see pbuf_free() |
DieterGraef | 0:d26c1b55cfca | 446 | */ |
DieterGraef | 0:d26c1b55cfca | 447 | static err_t |
DieterGraef | 0:d26c1b55cfca | 448 | etharp_update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags) |
DieterGraef | 0:d26c1b55cfca | 449 | { |
DieterGraef | 0:d26c1b55cfca | 450 | s8_t i; |
DieterGraef | 0:d26c1b55cfca | 451 | LWIP_ASSERT("netif->hwaddr_len == ETHARP_HWADDR_LEN", netif->hwaddr_len == ETHARP_HWADDR_LEN); |
DieterGraef | 0:d26c1b55cfca | 452 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n", |
DieterGraef | 0:d26c1b55cfca | 453 | ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr), |
DieterGraef | 0:d26c1b55cfca | 454 | ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2], |
DieterGraef | 0:d26c1b55cfca | 455 | ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5])); |
DieterGraef | 0:d26c1b55cfca | 456 | /* non-unicast address? */ |
DieterGraef | 0:d26c1b55cfca | 457 | if (ip_addr_isany(ipaddr) || |
DieterGraef | 0:d26c1b55cfca | 458 | ip_addr_isbroadcast(ipaddr, netif) || |
DieterGraef | 0:d26c1b55cfca | 459 | ip_addr_ismulticast(ipaddr)) { |
DieterGraef | 0:d26c1b55cfca | 460 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n")); |
DieterGraef | 0:d26c1b55cfca | 461 | return ERR_ARG; |
DieterGraef | 0:d26c1b55cfca | 462 | } |
DieterGraef | 0:d26c1b55cfca | 463 | /* find or create ARP entry */ |
DieterGraef | 0:d26c1b55cfca | 464 | i = etharp_find_entry(ipaddr, flags); |
DieterGraef | 0:d26c1b55cfca | 465 | /* bail out if no entry could be found */ |
DieterGraef | 0:d26c1b55cfca | 466 | if (i < 0) { |
DieterGraef | 0:d26c1b55cfca | 467 | return (err_t)i; |
DieterGraef | 0:d26c1b55cfca | 468 | } |
DieterGraef | 0:d26c1b55cfca | 469 | |
DieterGraef | 0:d26c1b55cfca | 470 | #if ETHARP_SUPPORT_STATIC_ENTRIES |
DieterGraef | 0:d26c1b55cfca | 471 | if (flags & ETHARP_FLAG_STATIC_ENTRY) { |
DieterGraef | 0:d26c1b55cfca | 472 | /* record static type */ |
DieterGraef | 0:d26c1b55cfca | 473 | arp_table[i].state = ETHARP_STATE_STATIC; |
DieterGraef | 0:d26c1b55cfca | 474 | } else |
DieterGraef | 0:d26c1b55cfca | 475 | #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ |
DieterGraef | 0:d26c1b55cfca | 476 | { |
DieterGraef | 0:d26c1b55cfca | 477 | /* mark it stable */ |
DieterGraef | 0:d26c1b55cfca | 478 | arp_table[i].state = ETHARP_STATE_STABLE; |
DieterGraef | 0:d26c1b55cfca | 479 | } |
DieterGraef | 0:d26c1b55cfca | 480 | |
DieterGraef | 0:d26c1b55cfca | 481 | /* record network interface */ |
DieterGraef | 0:d26c1b55cfca | 482 | arp_table[i].netif = netif; |
DieterGraef | 0:d26c1b55cfca | 483 | /* insert in SNMP ARP index tree */ |
DieterGraef | 0:d26c1b55cfca | 484 | snmp_insert_arpidx_tree(netif, &arp_table[i].ipaddr); |
DieterGraef | 0:d26c1b55cfca | 485 | |
DieterGraef | 0:d26c1b55cfca | 486 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 487 | /* update address */ |
DieterGraef | 0:d26c1b55cfca | 488 | ETHADDR32_COPY(&arp_table[i].ethaddr, ethaddr); |
DieterGraef | 0:d26c1b55cfca | 489 | /* reset time stamp */ |
DieterGraef | 0:d26c1b55cfca | 490 | arp_table[i].ctime = 0; |
DieterGraef | 0:d26c1b55cfca | 491 | /* this is where we will send out queued packets! */ |
DieterGraef | 0:d26c1b55cfca | 492 | #if ARP_QUEUEING |
DieterGraef | 0:d26c1b55cfca | 493 | while (arp_table[i].q != NULL) { |
DieterGraef | 0:d26c1b55cfca | 494 | struct pbuf *p; |
DieterGraef | 0:d26c1b55cfca | 495 | /* remember remainder of queue */ |
DieterGraef | 0:d26c1b55cfca | 496 | struct etharp_q_entry *q = arp_table[i].q; |
DieterGraef | 0:d26c1b55cfca | 497 | /* pop first item off the queue */ |
DieterGraef | 0:d26c1b55cfca | 498 | arp_table[i].q = q->next; |
DieterGraef | 0:d26c1b55cfca | 499 | /* get the packet pointer */ |
DieterGraef | 0:d26c1b55cfca | 500 | p = q->p; |
DieterGraef | 0:d26c1b55cfca | 501 | /* now queue entry can be freed */ |
DieterGraef | 0:d26c1b55cfca | 502 | memp_free(MEMP_ARP_QUEUE, q); |
DieterGraef | 0:d26c1b55cfca | 503 | #else /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 504 | if (arp_table[i].q != NULL) { |
DieterGraef | 0:d26c1b55cfca | 505 | struct pbuf *p = arp_table[i].q; |
DieterGraef | 0:d26c1b55cfca | 506 | arp_table[i].q = NULL; |
DieterGraef | 0:d26c1b55cfca | 507 | #endif /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 508 | /* send the queued IP packet */ |
DieterGraef | 0:d26c1b55cfca | 509 | etharp_send_ip(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr); |
DieterGraef | 0:d26c1b55cfca | 510 | /* free the queued IP packet */ |
DieterGraef | 0:d26c1b55cfca | 511 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 512 | } |
DieterGraef | 0:d26c1b55cfca | 513 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 514 | } |
DieterGraef | 0:d26c1b55cfca | 515 | |
DieterGraef | 0:d26c1b55cfca | 516 | #if ETHARP_SUPPORT_STATIC_ENTRIES |
DieterGraef | 0:d26c1b55cfca | 517 | /** Add a new static entry to the ARP table. If an entry exists for the |
DieterGraef | 0:d26c1b55cfca | 518 | * specified IP address, this entry is overwritten. |
DieterGraef | 0:d26c1b55cfca | 519 | * If packets are queued for the specified IP address, they are sent out. |
DieterGraef | 0:d26c1b55cfca | 520 | * |
DieterGraef | 0:d26c1b55cfca | 521 | * @param ipaddr IP address for the new static entry |
DieterGraef | 0:d26c1b55cfca | 522 | * @param ethaddr ethernet address for the new static entry |
DieterGraef | 0:d26c1b55cfca | 523 | * @return @see return values of etharp_add_static_entry |
DieterGraef | 0:d26c1b55cfca | 524 | */ |
DieterGraef | 0:d26c1b55cfca | 525 | err_t |
DieterGraef | 0:d26c1b55cfca | 526 | etharp_add_static_entry(ip_addr_t *ipaddr, struct eth_addr *ethaddr) |
DieterGraef | 0:d26c1b55cfca | 527 | { |
DieterGraef | 0:d26c1b55cfca | 528 | struct netif *netif; |
DieterGraef | 0:d26c1b55cfca | 529 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_add_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n", |
DieterGraef | 0:d26c1b55cfca | 530 | ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr), |
DieterGraef | 0:d26c1b55cfca | 531 | ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2], |
DieterGraef | 0:d26c1b55cfca | 532 | ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5])); |
DieterGraef | 0:d26c1b55cfca | 533 | |
DieterGraef | 0:d26c1b55cfca | 534 | netif = ip_route(ipaddr); |
DieterGraef | 0:d26c1b55cfca | 535 | if (netif == NULL) { |
DieterGraef | 0:d26c1b55cfca | 536 | return ERR_RTE; |
DieterGraef | 0:d26c1b55cfca | 537 | } |
DieterGraef | 0:d26c1b55cfca | 538 | |
DieterGraef | 0:d26c1b55cfca | 539 | return etharp_update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY); |
DieterGraef | 0:d26c1b55cfca | 540 | } |
DieterGraef | 0:d26c1b55cfca | 541 | |
DieterGraef | 0:d26c1b55cfca | 542 | /** Remove a static entry from the ARP table previously added with a call to |
DieterGraef | 0:d26c1b55cfca | 543 | * etharp_add_static_entry. |
DieterGraef | 0:d26c1b55cfca | 544 | * |
DieterGraef | 0:d26c1b55cfca | 545 | * @param ipaddr IP address of the static entry to remove |
DieterGraef | 0:d26c1b55cfca | 546 | * @return ERR_OK: entry removed |
DieterGraef | 0:d26c1b55cfca | 547 | * ERR_MEM: entry wasn't found |
DieterGraef | 0:d26c1b55cfca | 548 | * ERR_ARG: entry wasn't a static entry but a dynamic one |
DieterGraef | 0:d26c1b55cfca | 549 | */ |
DieterGraef | 0:d26c1b55cfca | 550 | err_t |
DieterGraef | 0:d26c1b55cfca | 551 | etharp_remove_static_entry(ip_addr_t *ipaddr) |
DieterGraef | 0:d26c1b55cfca | 552 | { |
DieterGraef | 0:d26c1b55cfca | 553 | s8_t i; |
DieterGraef | 0:d26c1b55cfca | 554 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
DieterGraef | 0:d26c1b55cfca | 555 | ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr))); |
DieterGraef | 0:d26c1b55cfca | 556 | |
DieterGraef | 0:d26c1b55cfca | 557 | /* find or create ARP entry */ |
DieterGraef | 0:d26c1b55cfca | 558 | i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY); |
DieterGraef | 0:d26c1b55cfca | 559 | /* bail out if no entry could be found */ |
DieterGraef | 0:d26c1b55cfca | 560 | if (i < 0) { |
DieterGraef | 0:d26c1b55cfca | 561 | return (err_t)i; |
DieterGraef | 0:d26c1b55cfca | 562 | } |
DieterGraef | 0:d26c1b55cfca | 563 | |
DieterGraef | 0:d26c1b55cfca | 564 | if (arp_table[i].state != ETHARP_STATE_STATIC) { |
DieterGraef | 0:d26c1b55cfca | 565 | /* entry wasn't a static entry, cannot remove it */ |
DieterGraef | 0:d26c1b55cfca | 566 | return ERR_ARG; |
DieterGraef | 0:d26c1b55cfca | 567 | } |
DieterGraef | 0:d26c1b55cfca | 568 | /* entry found, free it */ |
DieterGraef | 0:d26c1b55cfca | 569 | etharp_free_entry(i); |
DieterGraef | 0:d26c1b55cfca | 570 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 571 | } |
DieterGraef | 0:d26c1b55cfca | 572 | #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ |
DieterGraef | 0:d26c1b55cfca | 573 | |
DieterGraef | 0:d26c1b55cfca | 574 | /** |
DieterGraef | 0:d26c1b55cfca | 575 | * Remove all ARP table entries of the specified netif. |
DieterGraef | 0:d26c1b55cfca | 576 | * |
DieterGraef | 0:d26c1b55cfca | 577 | * @param netif points to a network interface |
DieterGraef | 0:d26c1b55cfca | 578 | */ |
DieterGraef | 0:d26c1b55cfca | 579 | void etharp_cleanup_netif(struct netif *netif) |
DieterGraef | 0:d26c1b55cfca | 580 | { |
DieterGraef | 0:d26c1b55cfca | 581 | u8_t i; |
DieterGraef | 0:d26c1b55cfca | 582 | |
DieterGraef | 0:d26c1b55cfca | 583 | for (i = 0; i < ARP_TABLE_SIZE; ++i) { |
DieterGraef | 0:d26c1b55cfca | 584 | u8_t state = arp_table[i].state; |
DieterGraef | 0:d26c1b55cfca | 585 | if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) { |
DieterGraef | 0:d26c1b55cfca | 586 | etharp_free_entry(i); |
DieterGraef | 0:d26c1b55cfca | 587 | } |
DieterGraef | 0:d26c1b55cfca | 588 | } |
DieterGraef | 0:d26c1b55cfca | 589 | } |
DieterGraef | 0:d26c1b55cfca | 590 | |
DieterGraef | 0:d26c1b55cfca | 591 | /** |
DieterGraef | 0:d26c1b55cfca | 592 | * Finds (stable) ethernet/IP address pair from ARP table |
DieterGraef | 0:d26c1b55cfca | 593 | * using interface and IP address index. |
DieterGraef | 0:d26c1b55cfca | 594 | * @note the addresses in the ARP table are in network order! |
DieterGraef | 0:d26c1b55cfca | 595 | * |
DieterGraef | 0:d26c1b55cfca | 596 | * @param netif points to interface index |
DieterGraef | 0:d26c1b55cfca | 597 | * @param ipaddr points to the (network order) IP address index |
DieterGraef | 0:d26c1b55cfca | 598 | * @param eth_ret points to return pointer |
DieterGraef | 0:d26c1b55cfca | 599 | * @param ip_ret points to return pointer |
DieterGraef | 0:d26c1b55cfca | 600 | * @return table index if found, -1 otherwise |
DieterGraef | 0:d26c1b55cfca | 601 | */ |
DieterGraef | 0:d26c1b55cfca | 602 | s8_t |
DieterGraef | 0:d26c1b55cfca | 603 | etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr, |
DieterGraef | 0:d26c1b55cfca | 604 | struct eth_addr **eth_ret, ip_addr_t **ip_ret) |
DieterGraef | 0:d26c1b55cfca | 605 | { |
DieterGraef | 0:d26c1b55cfca | 606 | s8_t i; |
DieterGraef | 0:d26c1b55cfca | 607 | |
DieterGraef | 0:d26c1b55cfca | 608 | LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL", |
DieterGraef | 0:d26c1b55cfca | 609 | eth_ret != NULL && ip_ret != NULL); |
DieterGraef | 0:d26c1b55cfca | 610 | |
DieterGraef | 0:d26c1b55cfca | 611 | LWIP_UNUSED_ARG(netif); |
DieterGraef | 0:d26c1b55cfca | 612 | |
DieterGraef | 0:d26c1b55cfca | 613 | i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY); |
DieterGraef | 0:d26c1b55cfca | 614 | if((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) { |
DieterGraef | 0:d26c1b55cfca | 615 | *eth_ret = &arp_table[i].ethaddr; |
DieterGraef | 0:d26c1b55cfca | 616 | *ip_ret = &arp_table[i].ipaddr; |
DieterGraef | 0:d26c1b55cfca | 617 | return i; |
DieterGraef | 0:d26c1b55cfca | 618 | } |
DieterGraef | 0:d26c1b55cfca | 619 | return -1; |
DieterGraef | 0:d26c1b55cfca | 620 | } |
DieterGraef | 0:d26c1b55cfca | 621 | |
DieterGraef | 0:d26c1b55cfca | 622 | #if ETHARP_TRUST_IP_MAC |
DieterGraef | 0:d26c1b55cfca | 623 | /** |
DieterGraef | 0:d26c1b55cfca | 624 | * Updates the ARP table using the given IP packet. |
DieterGraef | 0:d26c1b55cfca | 625 | * |
DieterGraef | 0:d26c1b55cfca | 626 | * Uses the incoming IP packet's source address to update the |
DieterGraef | 0:d26c1b55cfca | 627 | * ARP cache for the local network. The function does not alter |
DieterGraef | 0:d26c1b55cfca | 628 | * or free the packet. This function must be called before the |
DieterGraef | 0:d26c1b55cfca | 629 | * packet p is passed to the IP layer. |
DieterGraef | 0:d26c1b55cfca | 630 | * |
DieterGraef | 0:d26c1b55cfca | 631 | * @param netif The lwIP network interface on which the IP packet pbuf arrived. |
DieterGraef | 0:d26c1b55cfca | 632 | * @param p The IP packet that arrived on netif. |
DieterGraef | 0:d26c1b55cfca | 633 | * |
DieterGraef | 0:d26c1b55cfca | 634 | * @return NULL |
DieterGraef | 0:d26c1b55cfca | 635 | * |
DieterGraef | 0:d26c1b55cfca | 636 | * @see pbuf_free() |
DieterGraef | 0:d26c1b55cfca | 637 | */ |
DieterGraef | 0:d26c1b55cfca | 638 | static void |
DieterGraef | 0:d26c1b55cfca | 639 | etharp_ip_input(struct netif *netif, struct pbuf *p) |
DieterGraef | 0:d26c1b55cfca | 640 | { |
DieterGraef | 0:d26c1b55cfca | 641 | struct eth_hdr *ethhdr; |
DieterGraef | 0:d26c1b55cfca | 642 | struct ip_hdr *iphdr; |
DieterGraef | 0:d26c1b55cfca | 643 | ip_addr_t iphdr_src; |
DieterGraef | 0:d26c1b55cfca | 644 | LWIP_ERROR("netif != NULL", (netif != NULL), return;); |
DieterGraef | 0:d26c1b55cfca | 645 | |
DieterGraef | 0:d26c1b55cfca | 646 | /* Only insert an entry if the source IP address of the |
DieterGraef | 0:d26c1b55cfca | 647 | incoming IP packet comes from a host on the local network. */ |
DieterGraef | 0:d26c1b55cfca | 648 | ethhdr = (struct eth_hdr *)p->payload; |
DieterGraef | 0:d26c1b55cfca | 649 | iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR); |
DieterGraef | 0:d26c1b55cfca | 650 | #if ETHARP_SUPPORT_VLAN |
DieterGraef | 0:d26c1b55cfca | 651 | if (ethhdr->type == PP_HTONS(ETHTYPE_VLAN)) { |
DieterGraef | 0:d26c1b55cfca | 652 | iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR); |
DieterGraef | 0:d26c1b55cfca | 653 | } |
DieterGraef | 0:d26c1b55cfca | 654 | #endif /* ETHARP_SUPPORT_VLAN */ |
DieterGraef | 0:d26c1b55cfca | 655 | |
DieterGraef | 0:d26c1b55cfca | 656 | ip_addr_copy(iphdr_src, iphdr->src); |
DieterGraef | 0:d26c1b55cfca | 657 | |
DieterGraef | 0:d26c1b55cfca | 658 | /* source is not on the local network? */ |
DieterGraef | 0:d26c1b55cfca | 659 | if (!ip_addr_netcmp(&iphdr_src, &(netif->ip_addr), &(netif->netmask))) { |
DieterGraef | 0:d26c1b55cfca | 660 | /* do nothing */ |
DieterGraef | 0:d26c1b55cfca | 661 | return; |
DieterGraef | 0:d26c1b55cfca | 662 | } |
DieterGraef | 0:d26c1b55cfca | 663 | |
DieterGraef | 0:d26c1b55cfca | 664 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n")); |
DieterGraef | 0:d26c1b55cfca | 665 | /* update the source IP address in the cache, if present */ |
DieterGraef | 0:d26c1b55cfca | 666 | /* @todo We could use ETHARP_FLAG_TRY_HARD if we think we are going to talk |
DieterGraef | 0:d26c1b55cfca | 667 | * back soon (for example, if the destination IP address is ours. */ |
DieterGraef | 0:d26c1b55cfca | 668 | etharp_update_arp_entry(netif, &iphdr_src, &(ethhdr->src), ETHARP_FLAG_FIND_ONLY); |
DieterGraef | 0:d26c1b55cfca | 669 | } |
DieterGraef | 0:d26c1b55cfca | 670 | #endif /* ETHARP_TRUST_IP_MAC */ |
DieterGraef | 0:d26c1b55cfca | 671 | |
DieterGraef | 0:d26c1b55cfca | 672 | /** |
DieterGraef | 0:d26c1b55cfca | 673 | * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache |
DieterGraef | 0:d26c1b55cfca | 674 | * send out queued IP packets. Updates cache with snooped address pairs. |
DieterGraef | 0:d26c1b55cfca | 675 | * |
DieterGraef | 0:d26c1b55cfca | 676 | * Should be called for incoming ARP packets. The pbuf in the argument |
DieterGraef | 0:d26c1b55cfca | 677 | * is freed by this function. |
DieterGraef | 0:d26c1b55cfca | 678 | * |
DieterGraef | 0:d26c1b55cfca | 679 | * @param netif The lwIP network interface on which the ARP packet pbuf arrived. |
DieterGraef | 0:d26c1b55cfca | 680 | * @param ethaddr Ethernet address of netif. |
DieterGraef | 0:d26c1b55cfca | 681 | * @param p The ARP packet that arrived on netif. Is freed by this function. |
DieterGraef | 0:d26c1b55cfca | 682 | * |
DieterGraef | 0:d26c1b55cfca | 683 | * @return NULL |
DieterGraef | 0:d26c1b55cfca | 684 | * |
DieterGraef | 0:d26c1b55cfca | 685 | * @see pbuf_free() |
DieterGraef | 0:d26c1b55cfca | 686 | */ |
DieterGraef | 0:d26c1b55cfca | 687 | static void |
DieterGraef | 0:d26c1b55cfca | 688 | etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) |
DieterGraef | 0:d26c1b55cfca | 689 | { |
DieterGraef | 0:d26c1b55cfca | 690 | struct etharp_hdr *hdr; |
DieterGraef | 0:d26c1b55cfca | 691 | struct eth_hdr *ethhdr; |
DieterGraef | 0:d26c1b55cfca | 692 | /* these are aligned properly, whereas the ARP header fields might not be */ |
DieterGraef | 0:d26c1b55cfca | 693 | ip_addr_t sipaddr, dipaddr; |
DieterGraef | 0:d26c1b55cfca | 694 | u8_t for_us; |
DieterGraef | 0:d26c1b55cfca | 695 | #if LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 696 | const u8_t * ethdst_hwaddr; |
DieterGraef | 0:d26c1b55cfca | 697 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 698 | |
DieterGraef | 0:d26c1b55cfca | 699 | LWIP_ERROR("netif != NULL", (netif != NULL), return;); |
DieterGraef | 0:d26c1b55cfca | 700 | |
DieterGraef | 0:d26c1b55cfca | 701 | /* drop short ARP packets: we have to check for p->len instead of p->tot_len here |
DieterGraef | 0:d26c1b55cfca | 702 | since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */ |
DieterGraef | 0:d26c1b55cfca | 703 | if (p->len < SIZEOF_ETHARP_PACKET) { |
DieterGraef | 0:d26c1b55cfca | 704 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, |
DieterGraef | 0:d26c1b55cfca | 705 | ("etharp_arp_input: packet dropped, too short (%"S16_F"/%"S16_F")\n", p->tot_len, |
DieterGraef | 0:d26c1b55cfca | 706 | (s16_t)SIZEOF_ETHARP_PACKET)); |
DieterGraef | 0:d26c1b55cfca | 707 | ETHARP_STATS_INC(etharp.lenerr); |
DieterGraef | 0:d26c1b55cfca | 708 | ETHARP_STATS_INC(etharp.drop); |
DieterGraef | 0:d26c1b55cfca | 709 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 710 | return; |
DieterGraef | 0:d26c1b55cfca | 711 | } |
DieterGraef | 0:d26c1b55cfca | 712 | |
DieterGraef | 0:d26c1b55cfca | 713 | ethhdr = (struct eth_hdr *)p->payload; |
DieterGraef | 0:d26c1b55cfca | 714 | hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR); |
DieterGraef | 0:d26c1b55cfca | 715 | #if ETHARP_SUPPORT_VLAN |
DieterGraef | 0:d26c1b55cfca | 716 | if (ethhdr->type == PP_HTONS(ETHTYPE_VLAN)) { |
DieterGraef | 0:d26c1b55cfca | 717 | hdr = (struct etharp_hdr *)(((u8_t*)ethhdr) + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR); |
DieterGraef | 0:d26c1b55cfca | 718 | } |
DieterGraef | 0:d26c1b55cfca | 719 | #endif /* ETHARP_SUPPORT_VLAN */ |
DieterGraef | 0:d26c1b55cfca | 720 | |
DieterGraef | 0:d26c1b55cfca | 721 | /* RFC 826 "Packet Reception": */ |
DieterGraef | 0:d26c1b55cfca | 722 | if ((hdr->hwtype != PP_HTONS(HWTYPE_ETHERNET)) || |
DieterGraef | 0:d26c1b55cfca | 723 | (hdr->hwlen != ETHARP_HWADDR_LEN) || |
DieterGraef | 0:d26c1b55cfca | 724 | (hdr->protolen != sizeof(ip_addr_t)) || |
DieterGraef | 0:d26c1b55cfca | 725 | (hdr->proto != PP_HTONS(ETHTYPE_IP))) { |
DieterGraef | 0:d26c1b55cfca | 726 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, |
DieterGraef | 0:d26c1b55cfca | 727 | ("etharp_arp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n", |
DieterGraef | 0:d26c1b55cfca | 728 | hdr->hwtype, hdr->hwlen, hdr->proto, hdr->protolen)); |
DieterGraef | 0:d26c1b55cfca | 729 | ETHARP_STATS_INC(etharp.proterr); |
DieterGraef | 0:d26c1b55cfca | 730 | ETHARP_STATS_INC(etharp.drop); |
DieterGraef | 0:d26c1b55cfca | 731 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 732 | return; |
DieterGraef | 0:d26c1b55cfca | 733 | } |
DieterGraef | 0:d26c1b55cfca | 734 | ETHARP_STATS_INC(etharp.recv); |
DieterGraef | 0:d26c1b55cfca | 735 | |
DieterGraef | 0:d26c1b55cfca | 736 | #if LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 737 | /* We have to check if a host already has configured our random |
DieterGraef | 0:d26c1b55cfca | 738 | * created link local address and continously check if there is |
DieterGraef | 0:d26c1b55cfca | 739 | * a host with this IP-address so we can detect collisions */ |
DieterGraef | 0:d26c1b55cfca | 740 | autoip_arp_reply(netif, hdr); |
DieterGraef | 0:d26c1b55cfca | 741 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 742 | |
DieterGraef | 0:d26c1b55cfca | 743 | /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without |
DieterGraef | 0:d26c1b55cfca | 744 | * structure packing (not using structure copy which breaks strict-aliasing rules). */ |
DieterGraef | 0:d26c1b55cfca | 745 | IPADDR2_COPY(&sipaddr, &hdr->sipaddr); |
DieterGraef | 0:d26c1b55cfca | 746 | IPADDR2_COPY(&dipaddr, &hdr->dipaddr); |
DieterGraef | 0:d26c1b55cfca | 747 | |
DieterGraef | 0:d26c1b55cfca | 748 | /* this interface is not configured? */ |
DieterGraef | 0:d26c1b55cfca | 749 | if (ip_addr_isany(&netif->ip_addr)) { |
DieterGraef | 0:d26c1b55cfca | 750 | for_us = 0; |
DieterGraef | 0:d26c1b55cfca | 751 | } else { |
DieterGraef | 0:d26c1b55cfca | 752 | /* ARP packet directed to us? */ |
DieterGraef | 0:d26c1b55cfca | 753 | for_us = (u8_t)ip_addr_cmp(&dipaddr, &(netif->ip_addr)); |
DieterGraef | 0:d26c1b55cfca | 754 | } |
DieterGraef | 0:d26c1b55cfca | 755 | |
DieterGraef | 0:d26c1b55cfca | 756 | /* ARP message directed to us? |
DieterGraef | 0:d26c1b55cfca | 757 | -> add IP address in ARP cache; assume requester wants to talk to us, |
DieterGraef | 0:d26c1b55cfca | 758 | can result in directly sending the queued packets for this host. |
DieterGraef | 0:d26c1b55cfca | 759 | ARP message not directed to us? |
DieterGraef | 0:d26c1b55cfca | 760 | -> update the source IP address in the cache, if present */ |
DieterGraef | 0:d26c1b55cfca | 761 | etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), |
DieterGraef | 0:d26c1b55cfca | 762 | for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY); |
DieterGraef | 0:d26c1b55cfca | 763 | |
DieterGraef | 0:d26c1b55cfca | 764 | /* now act on the message itself */ |
DieterGraef | 0:d26c1b55cfca | 765 | switch (hdr->opcode) { |
DieterGraef | 0:d26c1b55cfca | 766 | /* ARP request? */ |
DieterGraef | 0:d26c1b55cfca | 767 | case PP_HTONS(ARP_REQUEST): |
DieterGraef | 0:d26c1b55cfca | 768 | /* ARP request. If it asked for our address, we send out a |
DieterGraef | 0:d26c1b55cfca | 769 | * reply. In any case, we time-stamp any existing ARP entry, |
DieterGraef | 0:d26c1b55cfca | 770 | * and possiby send out an IP packet that was queued on it. */ |
DieterGraef | 0:d26c1b55cfca | 771 | |
DieterGraef | 0:d26c1b55cfca | 772 | LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP request\n")); |
DieterGraef | 0:d26c1b55cfca | 773 | /* ARP request for our address? */ |
DieterGraef | 0:d26c1b55cfca | 774 | if (for_us) { |
DieterGraef | 0:d26c1b55cfca | 775 | |
DieterGraef | 0:d26c1b55cfca | 776 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n")); |
DieterGraef | 0:d26c1b55cfca | 777 | /* Re-use pbuf to send ARP reply. |
DieterGraef | 0:d26c1b55cfca | 778 | Since we are re-using an existing pbuf, we can't call etharp_raw since |
DieterGraef | 0:d26c1b55cfca | 779 | that would allocate a new pbuf. */ |
DieterGraef | 0:d26c1b55cfca | 780 | hdr->opcode = htons(ARP_REPLY); |
DieterGraef | 0:d26c1b55cfca | 781 | |
DieterGraef | 0:d26c1b55cfca | 782 | IPADDR2_COPY(&hdr->dipaddr, &hdr->sipaddr); |
DieterGraef | 0:d26c1b55cfca | 783 | IPADDR2_COPY(&hdr->sipaddr, &netif->ip_addr); |
DieterGraef | 0:d26c1b55cfca | 784 | |
DieterGraef | 0:d26c1b55cfca | 785 | LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!", |
DieterGraef | 0:d26c1b55cfca | 786 | (netif->hwaddr_len == ETHARP_HWADDR_LEN)); |
DieterGraef | 0:d26c1b55cfca | 787 | #if LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 788 | /* If we are using Link-Local, all ARP packets that contain a Link-Local |
DieterGraef | 0:d26c1b55cfca | 789 | * 'sender IP address' MUST be sent using link-layer broadcast instead of |
DieterGraef | 0:d26c1b55cfca | 790 | * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */ |
DieterGraef | 0:d26c1b55cfca | 791 | ethdst_hwaddr = ip_addr_islinklocal(&netif->ip_addr) ? (u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr; |
DieterGraef | 0:d26c1b55cfca | 792 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 793 | |
DieterGraef | 0:d26c1b55cfca | 794 | ETHADDR16_COPY(&hdr->dhwaddr, &hdr->shwaddr); |
DieterGraef | 0:d26c1b55cfca | 795 | #if LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 796 | ETHADDR16_COPY(ðhdr->dest, ethdst_hwaddr); |
DieterGraef | 0:d26c1b55cfca | 797 | #else /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 798 | ETHADDR16_COPY(ðhdr->dest, &hdr->shwaddr); |
DieterGraef | 0:d26c1b55cfca | 799 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 800 | ETHADDR16_COPY(&hdr->shwaddr, ethaddr); |
DieterGraef | 0:d26c1b55cfca | 801 | ETHADDR16_COPY(ðhdr->src, ethaddr); |
DieterGraef | 0:d26c1b55cfca | 802 | |
DieterGraef | 0:d26c1b55cfca | 803 | /* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header |
DieterGraef | 0:d26c1b55cfca | 804 | are already correct, we tested that before */ |
DieterGraef | 0:d26c1b55cfca | 805 | |
DieterGraef | 0:d26c1b55cfca | 806 | /* return ARP reply */ |
DieterGraef | 0:d26c1b55cfca | 807 | netif->linkoutput(netif, p); |
DieterGraef | 0:d26c1b55cfca | 808 | /* we are not configured? */ |
DieterGraef | 0:d26c1b55cfca | 809 | } else if (ip_addr_isany(&netif->ip_addr)) { |
DieterGraef | 0:d26c1b55cfca | 810 | /* { for_us == 0 and netif->ip_addr.addr == 0 } */ |
DieterGraef | 0:d26c1b55cfca | 811 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n")); |
DieterGraef | 0:d26c1b55cfca | 812 | /* request was not directed to us */ |
DieterGraef | 0:d26c1b55cfca | 813 | } else { |
DieterGraef | 0:d26c1b55cfca | 814 | /* { for_us == 0 and netif->ip_addr.addr != 0 } */ |
DieterGraef | 0:d26c1b55cfca | 815 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n")); |
DieterGraef | 0:d26c1b55cfca | 816 | } |
DieterGraef | 0:d26c1b55cfca | 817 | break; |
DieterGraef | 0:d26c1b55cfca | 818 | case PP_HTONS(ARP_REPLY): |
DieterGraef | 0:d26c1b55cfca | 819 | /* ARP reply. We already updated the ARP cache earlier. */ |
DieterGraef | 0:d26c1b55cfca | 820 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n")); |
DieterGraef | 0:d26c1b55cfca | 821 | #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK) |
DieterGraef | 0:d26c1b55cfca | 822 | /* DHCP wants to know about ARP replies from any host with an |
DieterGraef | 0:d26c1b55cfca | 823 | * IP address also offered to us by the DHCP server. We do not |
DieterGraef | 0:d26c1b55cfca | 824 | * want to take a duplicate IP address on a single network. |
DieterGraef | 0:d26c1b55cfca | 825 | * @todo How should we handle redundant (fail-over) interfaces? */ |
DieterGraef | 0:d26c1b55cfca | 826 | dhcp_arp_reply(netif, &sipaddr); |
DieterGraef | 0:d26c1b55cfca | 827 | #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */ |
DieterGraef | 0:d26c1b55cfca | 828 | break; |
DieterGraef | 0:d26c1b55cfca | 829 | default: |
DieterGraef | 0:d26c1b55cfca | 830 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %"S16_F"\n", htons(hdr->opcode))); |
DieterGraef | 0:d26c1b55cfca | 831 | ETHARP_STATS_INC(etharp.err); |
DieterGraef | 0:d26c1b55cfca | 832 | break; |
DieterGraef | 0:d26c1b55cfca | 833 | } |
DieterGraef | 0:d26c1b55cfca | 834 | /* free ARP packet */ |
DieterGraef | 0:d26c1b55cfca | 835 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 836 | } |
DieterGraef | 0:d26c1b55cfca | 837 | |
DieterGraef | 0:d26c1b55cfca | 838 | /** Just a small helper function that sends a pbuf to an ethernet address |
DieterGraef | 0:d26c1b55cfca | 839 | * in the arp_table specified by the index 'arp_idx'. |
DieterGraef | 0:d26c1b55cfca | 840 | */ |
DieterGraef | 0:d26c1b55cfca | 841 | static err_t |
DieterGraef | 0:d26c1b55cfca | 842 | etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, u8_t arp_idx) |
DieterGraef | 0:d26c1b55cfca | 843 | { |
DieterGraef | 0:d26c1b55cfca | 844 | LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE", |
DieterGraef | 0:d26c1b55cfca | 845 | arp_table[arp_idx].state >= ETHARP_STATE_STABLE); |
DieterGraef | 0:d26c1b55cfca | 846 | /* if arp table entry is about to expire: re-request it, |
DieterGraef | 0:d26c1b55cfca | 847 | but only if its state is ETHARP_STATE_STABLE to prevent flooding the |
DieterGraef | 0:d26c1b55cfca | 848 | network with ARP requests if this address is used frequently. */ |
DieterGraef | 0:d26c1b55cfca | 849 | if ((arp_table[arp_idx].state == ETHARP_STATE_STABLE) && |
DieterGraef | 0:d26c1b55cfca | 850 | (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED)) { |
DieterGraef | 0:d26c1b55cfca | 851 | if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) { |
DieterGraef | 0:d26c1b55cfca | 852 | arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING; |
DieterGraef | 0:d26c1b55cfca | 853 | } |
DieterGraef | 0:d26c1b55cfca | 854 | } |
DieterGraef | 0:d26c1b55cfca | 855 | |
DieterGraef | 0:d26c1b55cfca | 856 | return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr), |
DieterGraef | 0:d26c1b55cfca | 857 | &arp_table[arp_idx].ethaddr); |
DieterGraef | 0:d26c1b55cfca | 858 | } |
DieterGraef | 0:d26c1b55cfca | 859 | |
DieterGraef | 0:d26c1b55cfca | 860 | /** |
DieterGraef | 0:d26c1b55cfca | 861 | * Resolve and fill-in Ethernet address header for outgoing IP packet. |
DieterGraef | 0:d26c1b55cfca | 862 | * |
DieterGraef | 0:d26c1b55cfca | 863 | * For IP multicast and broadcast, corresponding Ethernet addresses |
DieterGraef | 0:d26c1b55cfca | 864 | * are selected and the packet is transmitted on the link. |
DieterGraef | 0:d26c1b55cfca | 865 | * |
DieterGraef | 0:d26c1b55cfca | 866 | * For unicast addresses, the packet is submitted to etharp_query(). In |
DieterGraef | 0:d26c1b55cfca | 867 | * case the IP address is outside the local network, the IP address of |
DieterGraef | 0:d26c1b55cfca | 868 | * the gateway is used. |
DieterGraef | 0:d26c1b55cfca | 869 | * |
DieterGraef | 0:d26c1b55cfca | 870 | * @param netif The lwIP network interface which the IP packet will be sent on. |
DieterGraef | 0:d26c1b55cfca | 871 | * @param q The pbuf(s) containing the IP packet to be sent. |
DieterGraef | 0:d26c1b55cfca | 872 | * @param ipaddr The IP address of the packet destination. |
DieterGraef | 0:d26c1b55cfca | 873 | * |
DieterGraef | 0:d26c1b55cfca | 874 | * @return |
DieterGraef | 0:d26c1b55cfca | 875 | * - ERR_RTE No route to destination (no gateway to external networks), |
DieterGraef | 0:d26c1b55cfca | 876 | * or the return type of either etharp_query() or etharp_send_ip(). |
DieterGraef | 0:d26c1b55cfca | 877 | */ |
DieterGraef | 0:d26c1b55cfca | 878 | err_t |
DieterGraef | 0:d26c1b55cfca | 879 | etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr) |
DieterGraef | 0:d26c1b55cfca | 880 | { |
DieterGraef | 0:d26c1b55cfca | 881 | struct eth_addr *dest; |
DieterGraef | 0:d26c1b55cfca | 882 | struct eth_addr mcastaddr; |
DieterGraef | 0:d26c1b55cfca | 883 | ip_addr_t *dst_addr = ipaddr; |
DieterGraef | 0:d26c1b55cfca | 884 | |
DieterGraef | 0:d26c1b55cfca | 885 | LWIP_ASSERT("netif != NULL", netif != NULL); |
DieterGraef | 0:d26c1b55cfca | 886 | LWIP_ASSERT("q != NULL", q != NULL); |
DieterGraef | 0:d26c1b55cfca | 887 | LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL); |
DieterGraef | 0:d26c1b55cfca | 888 | |
DieterGraef | 0:d26c1b55cfca | 889 | /* make room for Ethernet header - should not fail */ |
DieterGraef | 0:d26c1b55cfca | 890 | if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) { |
DieterGraef | 0:d26c1b55cfca | 891 | /* bail out */ |
DieterGraef | 0:d26c1b55cfca | 892 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, |
DieterGraef | 0:d26c1b55cfca | 893 | ("etharp_output: could not allocate room for header.\n")); |
DieterGraef | 0:d26c1b55cfca | 894 | LINK_STATS_INC(link.lenerr); |
DieterGraef | 0:d26c1b55cfca | 895 | return ERR_BUF; |
DieterGraef | 0:d26c1b55cfca | 896 | } |
DieterGraef | 0:d26c1b55cfca | 897 | |
DieterGraef | 0:d26c1b55cfca | 898 | /* Determine on destination hardware address. Broadcasts and multicasts |
DieterGraef | 0:d26c1b55cfca | 899 | * are special, other IP addresses are looked up in the ARP table. */ |
DieterGraef | 0:d26c1b55cfca | 900 | |
DieterGraef | 0:d26c1b55cfca | 901 | /* broadcast destination IP address? */ |
DieterGraef | 0:d26c1b55cfca | 902 | if (ip_addr_isbroadcast(ipaddr, netif)) { |
DieterGraef | 0:d26c1b55cfca | 903 | /* broadcast on Ethernet also */ |
DieterGraef | 0:d26c1b55cfca | 904 | dest = (struct eth_addr *)ðbroadcast; |
DieterGraef | 0:d26c1b55cfca | 905 | /* multicast destination IP address? */ |
DieterGraef | 0:d26c1b55cfca | 906 | } else if (ip_addr_ismulticast(ipaddr)) { |
DieterGraef | 0:d26c1b55cfca | 907 | /* Hash IP multicast address to MAC address.*/ |
DieterGraef | 0:d26c1b55cfca | 908 | mcastaddr.addr[0] = LL_MULTICAST_ADDR_0; |
DieterGraef | 0:d26c1b55cfca | 909 | mcastaddr.addr[1] = LL_MULTICAST_ADDR_1; |
DieterGraef | 0:d26c1b55cfca | 910 | mcastaddr.addr[2] = LL_MULTICAST_ADDR_2; |
DieterGraef | 0:d26c1b55cfca | 911 | mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f; |
DieterGraef | 0:d26c1b55cfca | 912 | mcastaddr.addr[4] = ip4_addr3(ipaddr); |
DieterGraef | 0:d26c1b55cfca | 913 | mcastaddr.addr[5] = ip4_addr4(ipaddr); |
DieterGraef | 0:d26c1b55cfca | 914 | /* destination Ethernet address is multicast */ |
DieterGraef | 0:d26c1b55cfca | 915 | dest = &mcastaddr; |
DieterGraef | 0:d26c1b55cfca | 916 | /* unicast destination IP address? */ |
DieterGraef | 0:d26c1b55cfca | 917 | } else { |
DieterGraef | 0:d26c1b55cfca | 918 | s8_t i; |
DieterGraef | 0:d26c1b55cfca | 919 | /* outside local network? if so, this can neither be a global broadcast nor |
DieterGraef | 0:d26c1b55cfca | 920 | a subnet broadcast. */ |
DieterGraef | 0:d26c1b55cfca | 921 | if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask)) && |
DieterGraef | 0:d26c1b55cfca | 922 | !ip_addr_islinklocal(ipaddr)) { |
DieterGraef | 0:d26c1b55cfca | 923 | #if LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 924 | struct ip_hdr *iphdr = (struct ip_hdr*)((u8_t*)q->payload + |
DieterGraef | 0:d26c1b55cfca | 925 | sizeof(struct eth_hdr)); |
DieterGraef | 0:d26c1b55cfca | 926 | /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with |
DieterGraef | 0:d26c1b55cfca | 927 | a link-local source address must always be "directly to its destination |
DieterGraef | 0:d26c1b55cfca | 928 | on the same physical link. The host MUST NOT send the packet to any |
DieterGraef | 0:d26c1b55cfca | 929 | router for forwarding". */ |
DieterGraef | 0:d26c1b55cfca | 930 | if (!ip_addr_islinklocal(&iphdr->src)) |
DieterGraef | 0:d26c1b55cfca | 931 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 932 | { |
DieterGraef | 0:d26c1b55cfca | 933 | /* interface has default gateway? */ |
DieterGraef | 0:d26c1b55cfca | 934 | if (!ip_addr_isany(&netif->gw)) { |
DieterGraef | 0:d26c1b55cfca | 935 | /* send to hardware address of default gateway IP address */ |
DieterGraef | 0:d26c1b55cfca | 936 | dst_addr = &(netif->gw); |
DieterGraef | 0:d26c1b55cfca | 937 | /* no default gateway available */ |
DieterGraef | 0:d26c1b55cfca | 938 | } else { |
DieterGraef | 0:d26c1b55cfca | 939 | /* no route to destination error (default gateway missing) */ |
DieterGraef | 0:d26c1b55cfca | 940 | return ERR_RTE; |
DieterGraef | 0:d26c1b55cfca | 941 | } |
DieterGraef | 0:d26c1b55cfca | 942 | } |
DieterGraef | 0:d26c1b55cfca | 943 | } |
DieterGraef | 0:d26c1b55cfca | 944 | #if LWIP_NETIF_HWADDRHINT |
DieterGraef | 0:d26c1b55cfca | 945 | if (netif->addr_hint != NULL) { |
DieterGraef | 0:d26c1b55cfca | 946 | /* per-pcb cached entry was given */ |
DieterGraef | 0:d26c1b55cfca | 947 | u8_t etharp_cached_entry = *(netif->addr_hint); |
DieterGraef | 0:d26c1b55cfca | 948 | if (etharp_cached_entry < ARP_TABLE_SIZE) { |
DieterGraef | 0:d26c1b55cfca | 949 | #endif /* LWIP_NETIF_HWADDRHINT */ |
DieterGraef | 0:d26c1b55cfca | 950 | if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) && |
DieterGraef | 0:d26c1b55cfca | 951 | (ip_addr_cmp(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) { |
DieterGraef | 0:d26c1b55cfca | 952 | /* the per-pcb-cached entry is stable and the right one! */ |
DieterGraef | 0:d26c1b55cfca | 953 | ETHARP_STATS_INC(etharp.cachehit); |
DieterGraef | 0:d26c1b55cfca | 954 | return etharp_output_to_arp_index(netif, q, etharp_cached_entry); |
DieterGraef | 0:d26c1b55cfca | 955 | } |
DieterGraef | 0:d26c1b55cfca | 956 | #if LWIP_NETIF_HWADDRHINT |
DieterGraef | 0:d26c1b55cfca | 957 | } |
DieterGraef | 0:d26c1b55cfca | 958 | } |
DieterGraef | 0:d26c1b55cfca | 959 | #endif /* LWIP_NETIF_HWADDRHINT */ |
DieterGraef | 0:d26c1b55cfca | 960 | |
DieterGraef | 0:d26c1b55cfca | 961 | /* find stable entry: do this here since this is a critical path for |
DieterGraef | 0:d26c1b55cfca | 962 | throughput and etharp_find_entry() is kind of slow */ |
DieterGraef | 0:d26c1b55cfca | 963 | for (i = 0; i < ARP_TABLE_SIZE; i++) { |
DieterGraef | 0:d26c1b55cfca | 964 | if ((arp_table[i].state >= ETHARP_STATE_STABLE) && |
DieterGraef | 0:d26c1b55cfca | 965 | (ip_addr_cmp(dst_addr, &arp_table[i].ipaddr))) { |
DieterGraef | 0:d26c1b55cfca | 966 | /* found an existing, stable entry */ |
DieterGraef | 0:d26c1b55cfca | 967 | ETHARP_SET_HINT(netif, i); |
DieterGraef | 0:d26c1b55cfca | 968 | return etharp_output_to_arp_index(netif, q, i); |
DieterGraef | 0:d26c1b55cfca | 969 | } |
DieterGraef | 0:d26c1b55cfca | 970 | } |
DieterGraef | 0:d26c1b55cfca | 971 | /* no stable entry found, use the (slower) query function: |
DieterGraef | 0:d26c1b55cfca | 972 | queue on destination Ethernet address belonging to ipaddr */ |
DieterGraef | 0:d26c1b55cfca | 973 | return etharp_query(netif, dst_addr, q); |
DieterGraef | 0:d26c1b55cfca | 974 | } |
DieterGraef | 0:d26c1b55cfca | 975 | |
DieterGraef | 0:d26c1b55cfca | 976 | /* continuation for multicast/broadcast destinations */ |
DieterGraef | 0:d26c1b55cfca | 977 | /* obtain source Ethernet address of the given interface */ |
DieterGraef | 0:d26c1b55cfca | 978 | /* send packet directly on the link */ |
DieterGraef | 0:d26c1b55cfca | 979 | return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr), dest); |
DieterGraef | 0:d26c1b55cfca | 980 | } |
DieterGraef | 0:d26c1b55cfca | 981 | |
DieterGraef | 0:d26c1b55cfca | 982 | /** |
DieterGraef | 0:d26c1b55cfca | 983 | * Send an ARP request for the given IP address and/or queue a packet. |
DieterGraef | 0:d26c1b55cfca | 984 | * |
DieterGraef | 0:d26c1b55cfca | 985 | * If the IP address was not yet in the cache, a pending ARP cache entry |
DieterGraef | 0:d26c1b55cfca | 986 | * is added and an ARP request is sent for the given address. The packet |
DieterGraef | 0:d26c1b55cfca | 987 | * is queued on this entry. |
DieterGraef | 0:d26c1b55cfca | 988 | * |
DieterGraef | 0:d26c1b55cfca | 989 | * If the IP address was already pending in the cache, a new ARP request |
DieterGraef | 0:d26c1b55cfca | 990 | * is sent for the given address. The packet is queued on this entry. |
DieterGraef | 0:d26c1b55cfca | 991 | * |
DieterGraef | 0:d26c1b55cfca | 992 | * If the IP address was already stable in the cache, and a packet is |
DieterGraef | 0:d26c1b55cfca | 993 | * given, it is directly sent and no ARP request is sent out. |
DieterGraef | 0:d26c1b55cfca | 994 | * |
DieterGraef | 0:d26c1b55cfca | 995 | * If the IP address was already stable in the cache, and no packet is |
DieterGraef | 0:d26c1b55cfca | 996 | * given, an ARP request is sent out. |
DieterGraef | 0:d26c1b55cfca | 997 | * |
DieterGraef | 0:d26c1b55cfca | 998 | * @param netif The lwIP network interface on which ipaddr |
DieterGraef | 0:d26c1b55cfca | 999 | * must be queried for. |
DieterGraef | 0:d26c1b55cfca | 1000 | * @param ipaddr The IP address to be resolved. |
DieterGraef | 0:d26c1b55cfca | 1001 | * @param q If non-NULL, a pbuf that must be delivered to the IP address. |
DieterGraef | 0:d26c1b55cfca | 1002 | * q is not freed by this function. |
DieterGraef | 0:d26c1b55cfca | 1003 | * |
DieterGraef | 0:d26c1b55cfca | 1004 | * @note q must only be ONE packet, not a packet queue! |
DieterGraef | 0:d26c1b55cfca | 1005 | * |
DieterGraef | 0:d26c1b55cfca | 1006 | * @return |
DieterGraef | 0:d26c1b55cfca | 1007 | * - ERR_BUF Could not make room for Ethernet header. |
DieterGraef | 0:d26c1b55cfca | 1008 | * - ERR_MEM Hardware address unknown, and no more ARP entries available |
DieterGraef | 0:d26c1b55cfca | 1009 | * to query for address or queue the packet. |
DieterGraef | 0:d26c1b55cfca | 1010 | * - ERR_MEM Could not queue packet due to memory shortage. |
DieterGraef | 0:d26c1b55cfca | 1011 | * - ERR_RTE No route to destination (no gateway to external networks). |
DieterGraef | 0:d26c1b55cfca | 1012 | * - ERR_ARG Non-unicast address given, those will not appear in ARP cache. |
DieterGraef | 0:d26c1b55cfca | 1013 | * |
DieterGraef | 0:d26c1b55cfca | 1014 | */ |
DieterGraef | 0:d26c1b55cfca | 1015 | err_t |
DieterGraef | 0:d26c1b55cfca | 1016 | etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q) |
DieterGraef | 0:d26c1b55cfca | 1017 | { |
DieterGraef | 0:d26c1b55cfca | 1018 | struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr; |
DieterGraef | 0:d26c1b55cfca | 1019 | err_t result = ERR_MEM; |
DieterGraef | 0:d26c1b55cfca | 1020 | s8_t i; /* ARP entry index */ |
DieterGraef | 0:d26c1b55cfca | 1021 | |
DieterGraef | 0:d26c1b55cfca | 1022 | /* non-unicast address? */ |
DieterGraef | 0:d26c1b55cfca | 1023 | if (ip_addr_isbroadcast(ipaddr, netif) || |
DieterGraef | 0:d26c1b55cfca | 1024 | ip_addr_ismulticast(ipaddr) || |
DieterGraef | 0:d26c1b55cfca | 1025 | ip_addr_isany(ipaddr)) { |
DieterGraef | 0:d26c1b55cfca | 1026 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n")); |
DieterGraef | 0:d26c1b55cfca | 1027 | return ERR_ARG; |
DieterGraef | 0:d26c1b55cfca | 1028 | } |
DieterGraef | 0:d26c1b55cfca | 1029 | |
DieterGraef | 0:d26c1b55cfca | 1030 | /* find entry in ARP cache, ask to create entry if queueing packet */ |
DieterGraef | 0:d26c1b55cfca | 1031 | i = etharp_find_entry(ipaddr, ETHARP_FLAG_TRY_HARD); |
DieterGraef | 0:d26c1b55cfca | 1032 | |
DieterGraef | 0:d26c1b55cfca | 1033 | /* could not find or create entry? */ |
DieterGraef | 0:d26c1b55cfca | 1034 | if (i < 0) { |
DieterGraef | 0:d26c1b55cfca | 1035 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n")); |
DieterGraef | 0:d26c1b55cfca | 1036 | if (q) { |
DieterGraef | 0:d26c1b55cfca | 1037 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n")); |
DieterGraef | 0:d26c1b55cfca | 1038 | ETHARP_STATS_INC(etharp.memerr); |
DieterGraef | 0:d26c1b55cfca | 1039 | } |
DieterGraef | 0:d26c1b55cfca | 1040 | return (err_t)i; |
DieterGraef | 0:d26c1b55cfca | 1041 | } |
DieterGraef | 0:d26c1b55cfca | 1042 | |
DieterGraef | 0:d26c1b55cfca | 1043 | /* mark a fresh entry as pending (we just sent a request) */ |
DieterGraef | 0:d26c1b55cfca | 1044 | if (arp_table[i].state == ETHARP_STATE_EMPTY) { |
DieterGraef | 0:d26c1b55cfca | 1045 | arp_table[i].state = ETHARP_STATE_PENDING; |
DieterGraef | 0:d26c1b55cfca | 1046 | } |
DieterGraef | 0:d26c1b55cfca | 1047 | |
DieterGraef | 0:d26c1b55cfca | 1048 | /* { i is either a STABLE or (new or existing) PENDING entry } */ |
DieterGraef | 0:d26c1b55cfca | 1049 | LWIP_ASSERT("arp_table[i].state == PENDING or STABLE", |
DieterGraef | 0:d26c1b55cfca | 1050 | ((arp_table[i].state == ETHARP_STATE_PENDING) || |
DieterGraef | 0:d26c1b55cfca | 1051 | (arp_table[i].state >= ETHARP_STATE_STABLE))); |
DieterGraef | 0:d26c1b55cfca | 1052 | |
DieterGraef | 0:d26c1b55cfca | 1053 | /* do we have a pending entry? or an implicit query request? */ |
DieterGraef | 0:d26c1b55cfca | 1054 | if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) { |
DieterGraef | 0:d26c1b55cfca | 1055 | /* try to resolve it; send out ARP request */ |
DieterGraef | 0:d26c1b55cfca | 1056 | result = etharp_request(netif, ipaddr); |
DieterGraef | 0:d26c1b55cfca | 1057 | if (result != ERR_OK) { |
DieterGraef | 0:d26c1b55cfca | 1058 | /* ARP request couldn't be sent */ |
DieterGraef | 0:d26c1b55cfca | 1059 | /* We don't re-send arp request in etharp_tmr, but we still queue packets, |
DieterGraef | 0:d26c1b55cfca | 1060 | since this failure could be temporary, and the next packet calling |
DieterGraef | 0:d26c1b55cfca | 1061 | etharp_query again could lead to sending the queued packets. */ |
DieterGraef | 0:d26c1b55cfca | 1062 | } |
DieterGraef | 0:d26c1b55cfca | 1063 | if (q == NULL) { |
DieterGraef | 0:d26c1b55cfca | 1064 | return result; |
DieterGraef | 0:d26c1b55cfca | 1065 | } |
DieterGraef | 0:d26c1b55cfca | 1066 | } |
DieterGraef | 0:d26c1b55cfca | 1067 | |
DieterGraef | 0:d26c1b55cfca | 1068 | /* packet given? */ |
DieterGraef | 0:d26c1b55cfca | 1069 | LWIP_ASSERT("q != NULL", q != NULL); |
DieterGraef | 0:d26c1b55cfca | 1070 | /* stable entry? */ |
DieterGraef | 0:d26c1b55cfca | 1071 | if (arp_table[i].state >= ETHARP_STATE_STABLE) { |
DieterGraef | 0:d26c1b55cfca | 1072 | /* we have a valid IP->Ethernet address mapping */ |
DieterGraef | 0:d26c1b55cfca | 1073 | ETHARP_SET_HINT(netif, i); |
DieterGraef | 0:d26c1b55cfca | 1074 | /* send the packet */ |
DieterGraef | 0:d26c1b55cfca | 1075 | result = etharp_send_ip(netif, q, srcaddr, &(arp_table[i].ethaddr)); |
DieterGraef | 0:d26c1b55cfca | 1076 | /* pending entry? (either just created or already pending */ |
DieterGraef | 0:d26c1b55cfca | 1077 | } else if (arp_table[i].state == ETHARP_STATE_PENDING) { |
DieterGraef | 0:d26c1b55cfca | 1078 | /* entry is still pending, queue the given packet 'q' */ |
DieterGraef | 0:d26c1b55cfca | 1079 | struct pbuf *p; |
DieterGraef | 0:d26c1b55cfca | 1080 | int copy_needed = 0; |
DieterGraef | 0:d26c1b55cfca | 1081 | /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but |
DieterGraef | 0:d26c1b55cfca | 1082 | * to copy the whole queue into a new PBUF_RAM (see bug #11400) |
DieterGraef | 0:d26c1b55cfca | 1083 | * PBUF_ROMs can be left as they are, since ROM must not get changed. */ |
DieterGraef | 0:d26c1b55cfca | 1084 | p = q; |
DieterGraef | 0:d26c1b55cfca | 1085 | while (p) { |
DieterGraef | 0:d26c1b55cfca | 1086 | LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0)); |
DieterGraef | 0:d26c1b55cfca | 1087 | if(p->type != PBUF_ROM) { |
DieterGraef | 0:d26c1b55cfca | 1088 | copy_needed = 1; |
DieterGraef | 0:d26c1b55cfca | 1089 | break; |
DieterGraef | 0:d26c1b55cfca | 1090 | } |
DieterGraef | 0:d26c1b55cfca | 1091 | p = p->next; |
DieterGraef | 0:d26c1b55cfca | 1092 | } |
DieterGraef | 0:d26c1b55cfca | 1093 | if(copy_needed) { |
DieterGraef | 0:d26c1b55cfca | 1094 | /* copy the whole packet into new pbufs */ |
DieterGraef | 0:d26c1b55cfca | 1095 | p = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM); |
DieterGraef | 0:d26c1b55cfca | 1096 | if(p != NULL) { |
DieterGraef | 0:d26c1b55cfca | 1097 | if (pbuf_copy(p, q) != ERR_OK) { |
DieterGraef | 0:d26c1b55cfca | 1098 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 1099 | p = NULL; |
DieterGraef | 0:d26c1b55cfca | 1100 | } |
DieterGraef | 0:d26c1b55cfca | 1101 | } |
DieterGraef | 0:d26c1b55cfca | 1102 | } else { |
DieterGraef | 0:d26c1b55cfca | 1103 | /* referencing the old pbuf is enough */ |
DieterGraef | 0:d26c1b55cfca | 1104 | p = q; |
DieterGraef | 0:d26c1b55cfca | 1105 | pbuf_ref(p); |
DieterGraef | 0:d26c1b55cfca | 1106 | } |
DieterGraef | 0:d26c1b55cfca | 1107 | /* packet could be taken over? */ |
DieterGraef | 0:d26c1b55cfca | 1108 | if (p != NULL) { |
DieterGraef | 0:d26c1b55cfca | 1109 | /* queue packet ... */ |
DieterGraef | 0:d26c1b55cfca | 1110 | #if ARP_QUEUEING |
DieterGraef | 0:d26c1b55cfca | 1111 | struct etharp_q_entry *new_entry; |
DieterGraef | 0:d26c1b55cfca | 1112 | /* allocate a new arp queue entry */ |
DieterGraef | 0:d26c1b55cfca | 1113 | new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE); |
DieterGraef | 0:d26c1b55cfca | 1114 | if (new_entry != NULL) { |
DieterGraef | 0:d26c1b55cfca | 1115 | new_entry->next = 0; |
DieterGraef | 0:d26c1b55cfca | 1116 | new_entry->p = p; |
DieterGraef | 0:d26c1b55cfca | 1117 | if(arp_table[i].q != NULL) { |
DieterGraef | 0:d26c1b55cfca | 1118 | /* queue was already existent, append the new entry to the end */ |
DieterGraef | 0:d26c1b55cfca | 1119 | struct etharp_q_entry *r; |
DieterGraef | 0:d26c1b55cfca | 1120 | r = arp_table[i].q; |
DieterGraef | 0:d26c1b55cfca | 1121 | while (r->next != NULL) { |
DieterGraef | 0:d26c1b55cfca | 1122 | r = r->next; |
DieterGraef | 0:d26c1b55cfca | 1123 | } |
DieterGraef | 0:d26c1b55cfca | 1124 | r->next = new_entry; |
DieterGraef | 0:d26c1b55cfca | 1125 | } else { |
DieterGraef | 0:d26c1b55cfca | 1126 | /* queue did not exist, first item in queue */ |
DieterGraef | 0:d26c1b55cfca | 1127 | arp_table[i].q = new_entry; |
DieterGraef | 0:d26c1b55cfca | 1128 | } |
DieterGraef | 0:d26c1b55cfca | 1129 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 1130 | result = ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 1131 | } else { |
DieterGraef | 0:d26c1b55cfca | 1132 | /* the pool MEMP_ARP_QUEUE is empty */ |
DieterGraef | 0:d26c1b55cfca | 1133 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 1134 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q)); |
DieterGraef | 0:d26c1b55cfca | 1135 | result = ERR_MEM; |
DieterGraef | 0:d26c1b55cfca | 1136 | } |
DieterGraef | 0:d26c1b55cfca | 1137 | #else /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 1138 | /* always queue one packet per ARP request only, freeing a previously queued packet */ |
DieterGraef | 0:d26c1b55cfca | 1139 | if (arp_table[i].q != NULL) { |
DieterGraef | 0:d26c1b55cfca | 1140 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"S16_F"\n", (void *)q, (s16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 1141 | pbuf_free(arp_table[i].q); |
DieterGraef | 0:d26c1b55cfca | 1142 | } |
DieterGraef | 0:d26c1b55cfca | 1143 | arp_table[i].q = p; |
DieterGraef | 0:d26c1b55cfca | 1144 | result = ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 1145 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i)); |
DieterGraef | 0:d26c1b55cfca | 1146 | #endif /* ARP_QUEUEING */ |
DieterGraef | 0:d26c1b55cfca | 1147 | } else { |
DieterGraef | 0:d26c1b55cfca | 1148 | ETHARP_STATS_INC(etharp.memerr); |
DieterGraef | 0:d26c1b55cfca | 1149 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q)); |
DieterGraef | 0:d26c1b55cfca | 1150 | result = ERR_MEM; |
DieterGraef | 0:d26c1b55cfca | 1151 | } |
DieterGraef | 0:d26c1b55cfca | 1152 | } |
DieterGraef | 0:d26c1b55cfca | 1153 | return result; |
DieterGraef | 0:d26c1b55cfca | 1154 | } |
DieterGraef | 0:d26c1b55cfca | 1155 | |
DieterGraef | 0:d26c1b55cfca | 1156 | /** |
DieterGraef | 0:d26c1b55cfca | 1157 | * Send a raw ARP packet (opcode and all addresses can be modified) |
DieterGraef | 0:d26c1b55cfca | 1158 | * |
DieterGraef | 0:d26c1b55cfca | 1159 | * @param netif the lwip network interface on which to send the ARP packet |
DieterGraef | 0:d26c1b55cfca | 1160 | * @param ethsrc_addr the source MAC address for the ethernet header |
DieterGraef | 0:d26c1b55cfca | 1161 | * @param ethdst_addr the destination MAC address for the ethernet header |
DieterGraef | 0:d26c1b55cfca | 1162 | * @param hwsrc_addr the source MAC address for the ARP protocol header |
DieterGraef | 0:d26c1b55cfca | 1163 | * @param ipsrc_addr the source IP address for the ARP protocol header |
DieterGraef | 0:d26c1b55cfca | 1164 | * @param hwdst_addr the destination MAC address for the ARP protocol header |
DieterGraef | 0:d26c1b55cfca | 1165 | * @param ipdst_addr the destination IP address for the ARP protocol header |
DieterGraef | 0:d26c1b55cfca | 1166 | * @param opcode the type of the ARP packet |
DieterGraef | 0:d26c1b55cfca | 1167 | * @return ERR_OK if the ARP packet has been sent |
DieterGraef | 0:d26c1b55cfca | 1168 | * ERR_MEM if the ARP packet couldn't be allocated |
DieterGraef | 0:d26c1b55cfca | 1169 | * any other err_t on failure |
DieterGraef | 0:d26c1b55cfca | 1170 | */ |
DieterGraef | 0:d26c1b55cfca | 1171 | #if !LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 1172 | static |
DieterGraef | 0:d26c1b55cfca | 1173 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 1174 | err_t |
DieterGraef | 0:d26c1b55cfca | 1175 | etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr, |
DieterGraef | 0:d26c1b55cfca | 1176 | const struct eth_addr *ethdst_addr, |
DieterGraef | 0:d26c1b55cfca | 1177 | const struct eth_addr *hwsrc_addr, const ip_addr_t *ipsrc_addr, |
DieterGraef | 0:d26c1b55cfca | 1178 | const struct eth_addr *hwdst_addr, const ip_addr_t *ipdst_addr, |
DieterGraef | 0:d26c1b55cfca | 1179 | const u16_t opcode) |
DieterGraef | 0:d26c1b55cfca | 1180 | { |
DieterGraef | 0:d26c1b55cfca | 1181 | struct pbuf *p; |
DieterGraef | 0:d26c1b55cfca | 1182 | err_t result = ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 1183 | struct eth_hdr *ethhdr; |
DieterGraef | 0:d26c1b55cfca | 1184 | struct etharp_hdr *hdr; |
DieterGraef | 0:d26c1b55cfca | 1185 | #if LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 1186 | const u8_t * ethdst_hwaddr; |
DieterGraef | 0:d26c1b55cfca | 1187 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 1188 | |
DieterGraef | 0:d26c1b55cfca | 1189 | LWIP_ASSERT("netif != NULL", netif != NULL); |
DieterGraef | 0:d26c1b55cfca | 1190 | |
DieterGraef | 0:d26c1b55cfca | 1191 | /* allocate a pbuf for the outgoing ARP request packet */ |
DieterGraef | 0:d26c1b55cfca | 1192 | p = pbuf_alloc(PBUF_RAW, SIZEOF_ETHARP_PACKET, PBUF_RAM); |
DieterGraef | 0:d26c1b55cfca | 1193 | /* could allocate a pbuf for an ARP request? */ |
DieterGraef | 0:d26c1b55cfca | 1194 | if (p == NULL) { |
DieterGraef | 0:d26c1b55cfca | 1195 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, |
DieterGraef | 0:d26c1b55cfca | 1196 | ("etharp_raw: could not allocate pbuf for ARP request.\n")); |
DieterGraef | 0:d26c1b55cfca | 1197 | ETHARP_STATS_INC(etharp.memerr); |
DieterGraef | 0:d26c1b55cfca | 1198 | return ERR_MEM; |
DieterGraef | 0:d26c1b55cfca | 1199 | } |
DieterGraef | 0:d26c1b55cfca | 1200 | LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr", |
DieterGraef | 0:d26c1b55cfca | 1201 | (p->len >= SIZEOF_ETHARP_PACKET)); |
DieterGraef | 0:d26c1b55cfca | 1202 | |
DieterGraef | 0:d26c1b55cfca | 1203 | ethhdr = (struct eth_hdr *)p->payload; |
DieterGraef | 0:d26c1b55cfca | 1204 | hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR); |
DieterGraef | 0:d26c1b55cfca | 1205 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n")); |
DieterGraef | 0:d26c1b55cfca | 1206 | hdr->opcode = htons(opcode); |
DieterGraef | 0:d26c1b55cfca | 1207 | |
DieterGraef | 0:d26c1b55cfca | 1208 | LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!", |
DieterGraef | 0:d26c1b55cfca | 1209 | (netif->hwaddr_len == ETHARP_HWADDR_LEN)); |
DieterGraef | 0:d26c1b55cfca | 1210 | #if LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 1211 | /* If we are using Link-Local, all ARP packets that contain a Link-Local |
DieterGraef | 0:d26c1b55cfca | 1212 | * 'sender IP address' MUST be sent using link-layer broadcast instead of |
DieterGraef | 0:d26c1b55cfca | 1213 | * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */ |
DieterGraef | 0:d26c1b55cfca | 1214 | ethdst_hwaddr = ip_addr_islinklocal(ipsrc_addr) ? (u8_t*)(ethbroadcast.addr) : ethdst_addr->addr; |
DieterGraef | 0:d26c1b55cfca | 1215 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 1216 | /* Write the ARP MAC-Addresses */ |
DieterGraef | 0:d26c1b55cfca | 1217 | ETHADDR16_COPY(&hdr->shwaddr, hwsrc_addr); |
DieterGraef | 0:d26c1b55cfca | 1218 | ETHADDR16_COPY(&hdr->dhwaddr, hwdst_addr); |
DieterGraef | 0:d26c1b55cfca | 1219 | /* Write the Ethernet MAC-Addresses */ |
DieterGraef | 0:d26c1b55cfca | 1220 | #if LWIP_AUTOIP |
DieterGraef | 0:d26c1b55cfca | 1221 | ETHADDR16_COPY(ðhdr->dest, ethdst_hwaddr); |
DieterGraef | 0:d26c1b55cfca | 1222 | #else /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 1223 | ETHADDR16_COPY(ðhdr->dest, ethdst_addr); |
DieterGraef | 0:d26c1b55cfca | 1224 | #endif /* LWIP_AUTOIP */ |
DieterGraef | 0:d26c1b55cfca | 1225 | ETHADDR16_COPY(ðhdr->src, ethsrc_addr); |
DieterGraef | 0:d26c1b55cfca | 1226 | /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without |
DieterGraef | 0:d26c1b55cfca | 1227 | * structure packing. */ |
DieterGraef | 0:d26c1b55cfca | 1228 | IPADDR2_COPY(&hdr->sipaddr, ipsrc_addr); |
DieterGraef | 0:d26c1b55cfca | 1229 | IPADDR2_COPY(&hdr->dipaddr, ipdst_addr); |
DieterGraef | 0:d26c1b55cfca | 1230 | |
DieterGraef | 0:d26c1b55cfca | 1231 | hdr->hwtype = PP_HTONS(HWTYPE_ETHERNET); |
DieterGraef | 0:d26c1b55cfca | 1232 | hdr->proto = PP_HTONS(ETHTYPE_IP); |
DieterGraef | 0:d26c1b55cfca | 1233 | /* set hwlen and protolen */ |
DieterGraef | 0:d26c1b55cfca | 1234 | hdr->hwlen = ETHARP_HWADDR_LEN; |
DieterGraef | 0:d26c1b55cfca | 1235 | hdr->protolen = sizeof(ip_addr_t); |
DieterGraef | 0:d26c1b55cfca | 1236 | |
DieterGraef | 0:d26c1b55cfca | 1237 | ethhdr->type = PP_HTONS(ETHTYPE_ARP); |
DieterGraef | 0:d26c1b55cfca | 1238 | /* send ARP query */ |
DieterGraef | 0:d26c1b55cfca | 1239 | result = netif->linkoutput(netif, p); |
DieterGraef | 0:d26c1b55cfca | 1240 | ETHARP_STATS_INC(etharp.xmit); |
DieterGraef | 0:d26c1b55cfca | 1241 | /* free ARP query packet */ |
DieterGraef | 0:d26c1b55cfca | 1242 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 1243 | p = NULL; |
DieterGraef | 0:d26c1b55cfca | 1244 | /* could not allocate pbuf for ARP request */ |
DieterGraef | 0:d26c1b55cfca | 1245 | |
DieterGraef | 0:d26c1b55cfca | 1246 | return result; |
DieterGraef | 0:d26c1b55cfca | 1247 | } |
DieterGraef | 0:d26c1b55cfca | 1248 | |
DieterGraef | 0:d26c1b55cfca | 1249 | /** |
DieterGraef | 0:d26c1b55cfca | 1250 | * Send an ARP request packet asking for ipaddr. |
DieterGraef | 0:d26c1b55cfca | 1251 | * |
DieterGraef | 0:d26c1b55cfca | 1252 | * @param netif the lwip network interface on which to send the request |
DieterGraef | 0:d26c1b55cfca | 1253 | * @param ipaddr the IP address for which to ask |
DieterGraef | 0:d26c1b55cfca | 1254 | * @return ERR_OK if the request has been sent |
DieterGraef | 0:d26c1b55cfca | 1255 | * ERR_MEM if the ARP packet couldn't be allocated |
DieterGraef | 0:d26c1b55cfca | 1256 | * any other err_t on failure |
DieterGraef | 0:d26c1b55cfca | 1257 | */ |
DieterGraef | 0:d26c1b55cfca | 1258 | err_t |
DieterGraef | 0:d26c1b55cfca | 1259 | etharp_request(struct netif *netif, ip_addr_t *ipaddr) |
DieterGraef | 0:d26c1b55cfca | 1260 | { |
DieterGraef | 0:d26c1b55cfca | 1261 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n")); |
DieterGraef | 0:d26c1b55cfca | 1262 | return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, ðbroadcast, |
DieterGraef | 0:d26c1b55cfca | 1263 | (struct eth_addr *)netif->hwaddr, &netif->ip_addr, ðzero, |
DieterGraef | 0:d26c1b55cfca | 1264 | ipaddr, ARP_REQUEST); |
DieterGraef | 0:d26c1b55cfca | 1265 | } |
DieterGraef | 0:d26c1b55cfca | 1266 | #endif /* LWIP_ARP */ |
DieterGraef | 0:d26c1b55cfca | 1267 | |
DieterGraef | 0:d26c1b55cfca | 1268 | /** |
DieterGraef | 0:d26c1b55cfca | 1269 | * Process received ethernet frames. Using this function instead of directly |
DieterGraef | 0:d26c1b55cfca | 1270 | * calling ip_input and passing ARP frames through etharp in ethernetif_input, |
DieterGraef | 0:d26c1b55cfca | 1271 | * the ARP cache is protected from concurrent access. |
DieterGraef | 0:d26c1b55cfca | 1272 | * |
DieterGraef | 0:d26c1b55cfca | 1273 | * @param p the recevied packet, p->payload pointing to the ethernet header |
DieterGraef | 0:d26c1b55cfca | 1274 | * @param netif the network interface on which the packet was received |
DieterGraef | 0:d26c1b55cfca | 1275 | */ |
DieterGraef | 0:d26c1b55cfca | 1276 | err_t |
DieterGraef | 0:d26c1b55cfca | 1277 | ethernet_input(struct pbuf *p, struct netif *netif) |
DieterGraef | 0:d26c1b55cfca | 1278 | { |
DieterGraef | 0:d26c1b55cfca | 1279 | struct eth_hdr* ethhdr; |
DieterGraef | 0:d26c1b55cfca | 1280 | u16_t type; |
DieterGraef | 0:d26c1b55cfca | 1281 | #if LWIP_ARP || ETHARP_SUPPORT_VLAN |
DieterGraef | 0:d26c1b55cfca | 1282 | s16_t ip_hdr_offset = SIZEOF_ETH_HDR; |
DieterGraef | 0:d26c1b55cfca | 1283 | #endif /* LWIP_ARP || ETHARP_SUPPORT_VLAN */ |
DieterGraef | 0:d26c1b55cfca | 1284 | |
DieterGraef | 0:d26c1b55cfca | 1285 | if (p->len <= SIZEOF_ETH_HDR) { |
DieterGraef | 0:d26c1b55cfca | 1286 | /* a packet with only an ethernet header (or less) is not valid for us */ |
DieterGraef | 0:d26c1b55cfca | 1287 | ETHARP_STATS_INC(etharp.proterr); |
DieterGraef | 0:d26c1b55cfca | 1288 | ETHARP_STATS_INC(etharp.drop); |
DieterGraef | 0:d26c1b55cfca | 1289 | goto free_and_return; |
DieterGraef | 0:d26c1b55cfca | 1290 | } |
DieterGraef | 0:d26c1b55cfca | 1291 | |
DieterGraef | 0:d26c1b55cfca | 1292 | /* points to packet payload, which starts with an Ethernet header */ |
DieterGraef | 0:d26c1b55cfca | 1293 | ethhdr = (struct eth_hdr *)p->payload; |
DieterGraef | 0:d26c1b55cfca | 1294 | LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, |
DieterGraef | 0:d26c1b55cfca | 1295 | ("ethernet_input: dest:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", src:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", type:%"X16_F"\n", |
DieterGraef | 0:d26c1b55cfca | 1296 | (unsigned)ethhdr->dest.addr[0], (unsigned)ethhdr->dest.addr[1], (unsigned)ethhdr->dest.addr[2], |
DieterGraef | 0:d26c1b55cfca | 1297 | (unsigned)ethhdr->dest.addr[3], (unsigned)ethhdr->dest.addr[4], (unsigned)ethhdr->dest.addr[5], |
DieterGraef | 0:d26c1b55cfca | 1298 | (unsigned)ethhdr->src.addr[0], (unsigned)ethhdr->src.addr[1], (unsigned)ethhdr->src.addr[2], |
DieterGraef | 0:d26c1b55cfca | 1299 | (unsigned)ethhdr->src.addr[3], (unsigned)ethhdr->src.addr[4], (unsigned)ethhdr->src.addr[5], |
DieterGraef | 0:d26c1b55cfca | 1300 | (unsigned)htons(ethhdr->type))); |
DieterGraef | 0:d26c1b55cfca | 1301 | |
DieterGraef | 0:d26c1b55cfca | 1302 | type = ethhdr->type; |
DieterGraef | 0:d26c1b55cfca | 1303 | #if ETHARP_SUPPORT_VLAN |
DieterGraef | 0:d26c1b55cfca | 1304 | if (type == PP_HTONS(ETHTYPE_VLAN)) { |
DieterGraef | 0:d26c1b55cfca | 1305 | struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR); |
DieterGraef | 0:d26c1b55cfca | 1306 | if (p->len <= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) { |
DieterGraef | 0:d26c1b55cfca | 1307 | /* a packet with only an ethernet/vlan header (or less) is not valid for us */ |
DieterGraef | 0:d26c1b55cfca | 1308 | ETHARP_STATS_INC(etharp.proterr); |
DieterGraef | 0:d26c1b55cfca | 1309 | ETHARP_STATS_INC(etharp.drop); |
DieterGraef | 0:d26c1b55cfca | 1310 | goto free_and_return; |
DieterGraef | 0:d26c1b55cfca | 1311 | } |
DieterGraef | 0:d26c1b55cfca | 1312 | #if defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) /* if not, allow all VLANs */ |
DieterGraef | 0:d26c1b55cfca | 1313 | #ifdef ETHARP_VLAN_CHECK_FN |
DieterGraef | 0:d26c1b55cfca | 1314 | if (!ETHARP_VLAN_CHECK_FN(ethhdr, vlan)) { |
DieterGraef | 0:d26c1b55cfca | 1315 | #elif defined(ETHARP_VLAN_CHECK) |
DieterGraef | 0:d26c1b55cfca | 1316 | if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) { |
DieterGraef | 0:d26c1b55cfca | 1317 | #endif |
DieterGraef | 0:d26c1b55cfca | 1318 | /* silently ignore this packet: not for our VLAN */ |
DieterGraef | 0:d26c1b55cfca | 1319 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 1320 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 1321 | } |
DieterGraef | 0:d26c1b55cfca | 1322 | #endif /* defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) */ |
DieterGraef | 0:d26c1b55cfca | 1323 | type = vlan->tpid; |
DieterGraef | 0:d26c1b55cfca | 1324 | ip_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR; |
DieterGraef | 0:d26c1b55cfca | 1325 | } |
DieterGraef | 0:d26c1b55cfca | 1326 | #endif /* ETHARP_SUPPORT_VLAN */ |
DieterGraef | 0:d26c1b55cfca | 1327 | |
DieterGraef | 0:d26c1b55cfca | 1328 | #if LWIP_ARP_FILTER_NETIF |
DieterGraef | 0:d26c1b55cfca | 1329 | netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, htons(type)); |
DieterGraef | 0:d26c1b55cfca | 1330 | #endif /* LWIP_ARP_FILTER_NETIF*/ |
DieterGraef | 0:d26c1b55cfca | 1331 | |
DieterGraef | 0:d26c1b55cfca | 1332 | if (ethhdr->dest.addr[0] & 1) { |
DieterGraef | 0:d26c1b55cfca | 1333 | /* this might be a multicast or broadcast packet */ |
DieterGraef | 0:d26c1b55cfca | 1334 | if (ethhdr->dest.addr[0] == LL_MULTICAST_ADDR_0) { |
DieterGraef | 0:d26c1b55cfca | 1335 | if ((ethhdr->dest.addr[1] == LL_MULTICAST_ADDR_1) && |
DieterGraef | 0:d26c1b55cfca | 1336 | (ethhdr->dest.addr[2] == LL_MULTICAST_ADDR_2)) { |
DieterGraef | 0:d26c1b55cfca | 1337 | /* mark the pbuf as link-layer multicast */ |
DieterGraef | 0:d26c1b55cfca | 1338 | p->flags |= PBUF_FLAG_LLMCAST; |
DieterGraef | 0:d26c1b55cfca | 1339 | } |
DieterGraef | 0:d26c1b55cfca | 1340 | } else if (eth_addr_cmp(ðhdr->dest, ðbroadcast)) { |
DieterGraef | 0:d26c1b55cfca | 1341 | /* mark the pbuf as link-layer broadcast */ |
DieterGraef | 0:d26c1b55cfca | 1342 | p->flags |= PBUF_FLAG_LLBCAST; |
DieterGraef | 0:d26c1b55cfca | 1343 | } |
DieterGraef | 0:d26c1b55cfca | 1344 | } |
DieterGraef | 0:d26c1b55cfca | 1345 | |
DieterGraef | 0:d26c1b55cfca | 1346 | switch (type) { |
DieterGraef | 0:d26c1b55cfca | 1347 | #if LWIP_ARP |
DieterGraef | 0:d26c1b55cfca | 1348 | /* IP packet? */ |
DieterGraef | 0:d26c1b55cfca | 1349 | case PP_HTONS(ETHTYPE_IP): |
DieterGraef | 0:d26c1b55cfca | 1350 | if (!(netif->flags & NETIF_FLAG_ETHARP)) { |
DieterGraef | 0:d26c1b55cfca | 1351 | goto free_and_return; |
DieterGraef | 0:d26c1b55cfca | 1352 | } |
DieterGraef | 0:d26c1b55cfca | 1353 | #if ETHARP_TRUST_IP_MAC |
DieterGraef | 0:d26c1b55cfca | 1354 | /* update ARP table */ |
DieterGraef | 0:d26c1b55cfca | 1355 | etharp_ip_input(netif, p); |
DieterGraef | 0:d26c1b55cfca | 1356 | #endif /* ETHARP_TRUST_IP_MAC */ |
DieterGraef | 0:d26c1b55cfca | 1357 | /* skip Ethernet header */ |
DieterGraef | 0:d26c1b55cfca | 1358 | if(pbuf_header(p, -ip_hdr_offset)) { |
DieterGraef | 0:d26c1b55cfca | 1359 | LWIP_ASSERT("Can't move over header in packet", 0); |
DieterGraef | 0:d26c1b55cfca | 1360 | goto free_and_return; |
DieterGraef | 0:d26c1b55cfca | 1361 | } else { |
DieterGraef | 0:d26c1b55cfca | 1362 | /* pass to IP layer */ |
DieterGraef | 0:d26c1b55cfca | 1363 | ip_input(p, netif); |
DieterGraef | 0:d26c1b55cfca | 1364 | } |
DieterGraef | 0:d26c1b55cfca | 1365 | break; |
DieterGraef | 0:d26c1b55cfca | 1366 | |
DieterGraef | 0:d26c1b55cfca | 1367 | case PP_HTONS(ETHTYPE_ARP): |
DieterGraef | 0:d26c1b55cfca | 1368 | if (!(netif->flags & NETIF_FLAG_ETHARP)) { |
DieterGraef | 0:d26c1b55cfca | 1369 | goto free_and_return; |
DieterGraef | 0:d26c1b55cfca | 1370 | } |
DieterGraef | 0:d26c1b55cfca | 1371 | /* pass p to ARP module */ |
DieterGraef | 0:d26c1b55cfca | 1372 | etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p); |
DieterGraef | 0:d26c1b55cfca | 1373 | break; |
DieterGraef | 0:d26c1b55cfca | 1374 | #endif /* LWIP_ARP */ |
DieterGraef | 0:d26c1b55cfca | 1375 | #if PPPOE_SUPPORT |
DieterGraef | 0:d26c1b55cfca | 1376 | case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */ |
DieterGraef | 0:d26c1b55cfca | 1377 | pppoe_disc_input(netif, p); |
DieterGraef | 0:d26c1b55cfca | 1378 | break; |
DieterGraef | 0:d26c1b55cfca | 1379 | |
DieterGraef | 0:d26c1b55cfca | 1380 | case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */ |
DieterGraef | 0:d26c1b55cfca | 1381 | pppoe_data_input(netif, p); |
DieterGraef | 0:d26c1b55cfca | 1382 | break; |
DieterGraef | 0:d26c1b55cfca | 1383 | #endif /* PPPOE_SUPPORT */ |
DieterGraef | 0:d26c1b55cfca | 1384 | |
DieterGraef | 0:d26c1b55cfca | 1385 | default: |
DieterGraef | 0:d26c1b55cfca | 1386 | ETHARP_STATS_INC(etharp.proterr); |
DieterGraef | 0:d26c1b55cfca | 1387 | ETHARP_STATS_INC(etharp.drop); |
DieterGraef | 0:d26c1b55cfca | 1388 | goto free_and_return; |
DieterGraef | 0:d26c1b55cfca | 1389 | } |
DieterGraef | 0:d26c1b55cfca | 1390 | |
DieterGraef | 0:d26c1b55cfca | 1391 | /* This means the pbuf is freed or consumed, |
DieterGraef | 0:d26c1b55cfca | 1392 | so the caller doesn't have to free it again */ |
DieterGraef | 0:d26c1b55cfca | 1393 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 1394 | |
DieterGraef | 0:d26c1b55cfca | 1395 | free_and_return: |
DieterGraef | 0:d26c1b55cfca | 1396 | pbuf_free(p); |
DieterGraef | 0:d26c1b55cfca | 1397 | return ERR_OK; |
DieterGraef | 0:d26c1b55cfca | 1398 | } |
DieterGraef | 0:d26c1b55cfca | 1399 | #endif /* LWIP_ARP || LWIP_ETHERNET */ |