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