Donatien Garnier / LwIPNetworking

Dependencies:   DebugLib Socket lwip lwip-sys

Dependents:   EthernetInterface

Fork of NetworkingCoreLib by Donatien Garnier

Committer:
donatien
Date:
Thu May 24 14:49:40 2012 +0000
Revision:
0:013f5d54248c
Initial Commit

Who changed what in which revision?

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