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