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