Rik Van Dyck / Mbed 2 deprecated Project_Embedded_C

Dependencies:   mbed DS1307 Servo TextLCD

Committer:
rikvandyck
Date:
Thu Dec 04 10:44:07 2014 +0000
Revision:
0:e1edd52b1ee2
test

Who changed what in which revision?

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