NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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