Committer:
mbed714
Date:
Sat Sep 18 23:05:49 2010 +0000
Revision:
0:d616ece2d859

        

Who changed what in which revision?

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