I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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