Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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