Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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