Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
lwip_etharp.c
00001 /** 00002 * @file 00003 * Address Resolution Protocol module for IP over Ethernet 00004 * 00005 * Functionally, ARP is divided into two parts. The first maps an IP address 00006 * to a physical address when sending a packet, and the second part answers 00007 * requests from other machines for our physical address. 00008 * 00009 * This implementation complies with RFC 826 (Ethernet ARP). It supports 00010 * Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6 00011 * if an interface calls etharp_gratuitous(our_netif) upon address change. 00012 */ 00013 00014 /* 00015 * Copyright (c) 2001-2003 Swedish Institute of Computer Science. 00016 * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv> 00017 * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands. 00018 * All rights reserved. 00019 * 00020 * Redistribution and use in source and binary forms, with or without modification, 00021 * are permitted provided that the following conditions are met: 00022 * 00023 * 1. Redistributions of source code must retain the above copyright notice, 00024 * this list of conditions and the following disclaimer. 00025 * 2. Redistributions in binary form must reproduce the above copyright notice, 00026 * this list of conditions and the following disclaimer in the documentation 00027 * and/or other materials provided with the distribution. 00028 * 3. The name of the author may not be used to endorse or promote products 00029 * derived from this software without specific prior written permission. 00030 * 00031 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00032 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00033 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00034 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00035 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00036 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00037 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00038 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00039 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00040 * OF SUCH DAMAGE. 00041 * 00042 * This file is part of the lwIP TCP/IP stack. 00043 * 00044 */ 00045 00046 #include "lwip/opt.h" 00047 00048 #if LWIP_IPV4 && LWIP_ARP /* don't build if not configured for use in lwipopts.h */ 00049 00050 #include "lwip/etharp.h" 00051 #include "lwip/stats.h" 00052 #include "lwip/snmp.h" 00053 #include "lwip/dhcp.h" 00054 #include "lwip/autoip.h" 00055 #include "lwip/prot/iana.h" 00056 #include "netif/ethernet.h" 00057 00058 #include <string.h> 00059 00060 #ifdef LWIP_HOOK_FILENAME 00061 #include LWIP_HOOK_FILENAME 00062 #endif 00063 00064 /** Re-request a used ARP entry 1 minute before it would expire to prevent 00065 * breaking a steadily used connection because the ARP entry timed out. */ 00066 #define ARP_AGE_REREQUEST_USED_UNICAST (ARP_MAXAGE - 30) 00067 #define ARP_AGE_REREQUEST_USED_BROADCAST (ARP_MAXAGE - 15) 00068 00069 /** the time an ARP entry stays pending after first request, 00070 * for ARP_TMR_INTERVAL = 1000, this is 00071 * 10 seconds. 00072 * 00073 * @internal Keep this number at least 2, otherwise it might 00074 * run out instantly if the timeout occurs directly after a request. 00075 */ 00076 #define ARP_MAXPENDING 5 00077 00078 /** ARP states */ 00079 enum etharp_state { 00080 ETHARP_STATE_EMPTY = 0, 00081 ETHARP_STATE_PENDING, 00082 ETHARP_STATE_STABLE, 00083 ETHARP_STATE_STABLE_REREQUESTING_1, 00084 ETHARP_STATE_STABLE_REREQUESTING_2 00085 #if ETHARP_SUPPORT_STATIC_ENTRIES 00086 , ETHARP_STATE_STATIC 00087 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00088 }; 00089 00090 struct etharp_entry { 00091 #if ARP_QUEUEING 00092 /** Pointer to queue of pending outgoing packets on this ARP entry. */ 00093 struct etharp_q_entry *q; 00094 #else /* ARP_QUEUEING */ 00095 /** Pointer to a single pending outgoing packet on this ARP entry. */ 00096 struct pbuf *q; 00097 #endif /* ARP_QUEUEING */ 00098 ip4_addr_t ipaddr; 00099 struct netif *netif; 00100 struct eth_addr ethaddr; 00101 u16_t ctime; 00102 u8_t state; 00103 }; 00104 00105 static struct etharp_entry arp_table[ARP_TABLE_SIZE]; 00106 00107 #if !LWIP_NETIF_HWADDRHINT 00108 static netif_addr_idx_t etharp_cached_entry; 00109 #endif /* !LWIP_NETIF_HWADDRHINT */ 00110 00111 /** Try hard to create a new entry - we want the IP address to appear in 00112 the cache (even if this means removing an active entry or so). */ 00113 #define ETHARP_FLAG_TRY_HARD 1 00114 #define ETHARP_FLAG_FIND_ONLY 2 00115 #if ETHARP_SUPPORT_STATIC_ENTRIES 00116 #define ETHARP_FLAG_STATIC_ENTRY 4 00117 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00118 00119 #if LWIP_NETIF_HWADDRHINT 00120 #define ETHARP_SET_ADDRHINT(netif, addrhint) do { if (((netif) != NULL) && ((netif)->hints != NULL)) { \ 00121 (netif)->hints->addr_hint = (addrhint); }} while(0) 00122 #else /* LWIP_NETIF_HWADDRHINT */ 00123 #define ETHARP_SET_ADDRHINT(netif, addrhint) (etharp_cached_entry = (addrhint)) 00124 #endif /* LWIP_NETIF_HWADDRHINT */ 00125 00126 00127 /* Check for maximum ARP_TABLE_SIZE */ 00128 #if (ARP_TABLE_SIZE > NETIF_ADDR_IDX_MAX) 00129 #error "ARP_TABLE_SIZE must fit in an s16_t, you have to reduce it in your lwipopts.h" 00130 #endif 00131 00132 00133 static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr); 00134 static err_t etharp_raw(struct netif *netif, 00135 const struct eth_addr *ethsrc_addr, const struct eth_addr *ethdst_addr, 00136 const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr, 00137 const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr, 00138 const u16_t opcode); 00139 00140 #if ARP_QUEUEING 00141 /** 00142 * Free a complete queue of etharp entries 00143 * 00144 * @param q a qeueue of etharp_q_entry's to free 00145 */ 00146 static void 00147 free_etharp_q(struct etharp_q_entry *q) 00148 { 00149 struct etharp_q_entry *r; 00150 LWIP_ASSERT("q != NULL", q != NULL); 00151 while (q) { 00152 r = q; 00153 q = q->next; 00154 LWIP_ASSERT("r->p != NULL", (r->p != NULL)); 00155 pbuf_free(r->p); 00156 memp_free(MEMP_ARP_QUEUE, r); 00157 } 00158 } 00159 #else /* ARP_QUEUEING */ 00160 00161 /** Compatibility define: free the queued pbuf */ 00162 #define free_etharp_q(q) pbuf_free(q) 00163 00164 #endif /* ARP_QUEUEING */ 00165 00166 /** Clean up ARP table entries */ 00167 static void 00168 etharp_free_entry(int i) 00169 { 00170 /* remove from SNMP ARP index tree */ 00171 mib2_remove_arp_entry(arp_table[i].netif, &arp_table[i].ipaddr); 00172 /* and empty packet queue */ 00173 if (arp_table[i].q != NULL) { 00174 /* remove all queued packets */ 00175 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); 00176 free_etharp_q(arp_table[i].q); 00177 arp_table[i].q = NULL; 00178 } 00179 /* recycle entry for re-use */ 00180 arp_table[i].state = ETHARP_STATE_EMPTY; 00181 #ifdef LWIP_DEBUG 00182 /* for debugging, clean out the complete entry */ 00183 arp_table[i].ctime = 0; 00184 arp_table[i].netif = NULL; 00185 ip4_addr_set_zero(&arp_table[i].ipaddr); 00186 arp_table[i].ethaddr = ethzero; 00187 #endif /* LWIP_DEBUG */ 00188 } 00189 00190 /** 00191 * Clears expired entries in the ARP table. 00192 * 00193 * This function should be called every ARP_TMR_INTERVAL milliseconds (1 second), 00194 * in order to expire entries in the ARP table. 00195 */ 00196 void 00197 etharp_tmr(void) 00198 { 00199 int i; 00200 00201 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n")); 00202 /* remove expired entries from the ARP table */ 00203 for (i = 0; i < ARP_TABLE_SIZE; ++i) { 00204 u8_t state = arp_table[i].state; 00205 if (state != ETHARP_STATE_EMPTY 00206 #if ETHARP_SUPPORT_STATIC_ENTRIES 00207 && (state != ETHARP_STATE_STATIC) 00208 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00209 ) { 00210 arp_table[i].ctime++; 00211 if ((arp_table[i].ctime >= ARP_MAXAGE) || 00212 ((arp_table[i].state == ETHARP_STATE_PENDING) && 00213 (arp_table[i].ctime >= ARP_MAXPENDING))) { 00214 /* pending or stable entry has become old! */ 00215 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %d.\n", 00216 arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", i)); 00217 /* clean up entries that have just been expired */ 00218 etharp_free_entry(i); 00219 } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_1) { 00220 /* Don't send more than one request every 2 seconds. */ 00221 arp_table[i].state = ETHARP_STATE_STABLE_REREQUESTING_2; 00222 } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_2) { 00223 /* Reset state to stable, so that the next transmitted packet will 00224 re-send an ARP request. */ 00225 arp_table[i].state = ETHARP_STATE_STABLE; 00226 } else if (arp_table[i].state == ETHARP_STATE_PENDING) { 00227 /* still pending, resend an ARP query */ 00228 etharp_request(arp_table[i].netif, &arp_table[i].ipaddr); 00229 } 00230 } 00231 } 00232 } 00233 00234 /** 00235 * Search the ARP table for a matching or new entry. 00236 * 00237 * If an IP address is given, return a pending or stable ARP entry that matches 00238 * the address. If no match is found, create a new entry with this address set, 00239 * but in state ETHARP_EMPTY. The caller must check and possibly change the 00240 * state of the returned entry. 00241 * 00242 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY. 00243 * 00244 * In all cases, attempt to create new entries from an empty entry. If no 00245 * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle 00246 * old entries. Heuristic choose the least important entry for recycling. 00247 * 00248 * @param ipaddr IP address to find in ARP cache, or to add if not found. 00249 * @param flags See @ref etharp_state 00250 * @param netif netif related to this address (used for NETIF_HWADDRHINT) 00251 * 00252 * @return The ARP entry index that matched or is created, ERR_MEM if no 00253 * entry is found or could be recycled. 00254 */ 00255 static s16_t 00256 etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif) 00257 { 00258 s16_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE; 00259 s16_t empty = ARP_TABLE_SIZE; 00260 s16_t i = 0; 00261 /* oldest entry with packets on queue */ 00262 s16_t old_queue = ARP_TABLE_SIZE; 00263 /* its age */ 00264 u16_t age_queue = 0, age_pending = 0, age_stable = 0; 00265 00266 LWIP_UNUSED_ARG(netif); 00267 00268 /** 00269 * a) do a search through the cache, remember candidates 00270 * b) select candidate entry 00271 * c) create new entry 00272 */ 00273 00274 /* a) in a single search sweep, do all of this 00275 * 1) remember the first empty entry (if any) 00276 * 2) remember the oldest stable entry (if any) 00277 * 3) remember the oldest pending entry without queued packets (if any) 00278 * 4) remember the oldest pending entry with queued packets (if any) 00279 * 5) search for a matching IP entry, either pending or stable 00280 * until 5 matches, or all entries are searched for. 00281 */ 00282 00283 for (i = 0; i < ARP_TABLE_SIZE; ++i) { 00284 u8_t state = arp_table[i].state; 00285 /* no empty entry found yet and now we do find one? */ 00286 if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) { 00287 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_find_entry: found empty entry %d\n", (int)i)); 00288 /* remember first empty entry */ 00289 empty = i; 00290 } else if (state != ETHARP_STATE_EMPTY) { 00291 LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE", 00292 state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE); 00293 /* if given, does IP address match IP address in ARP entry? */ 00294 if (ipaddr && ip4_addr_cmp(ipaddr, &arp_table[i].ipaddr) 00295 #if ETHARP_TABLE_MATCH_NETIF 00296 && ((netif == NULL) || (netif == arp_table[i].netif)) 00297 #endif /* ETHARP_TABLE_MATCH_NETIF */ 00298 ) { 00299 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %d\n", (int)i)); 00300 /* found exact IP address match, simply bail out */ 00301 return i; 00302 } 00303 /* pending entry? */ 00304 if (state == ETHARP_STATE_PENDING) { 00305 /* pending with queued packets? */ 00306 if (arp_table[i].q != NULL) { 00307 if (arp_table[i].ctime >= age_queue) { 00308 old_queue = i; 00309 age_queue = arp_table[i].ctime; 00310 } 00311 } else 00312 /* pending without queued packets? */ 00313 { 00314 if (arp_table[i].ctime >= age_pending) { 00315 old_pending = i; 00316 age_pending = arp_table[i].ctime; 00317 } 00318 } 00319 /* stable entry? */ 00320 } else if (state >= ETHARP_STATE_STABLE) { 00321 #if ETHARP_SUPPORT_STATIC_ENTRIES 00322 /* don't record old_stable for static entries since they never expire */ 00323 if (state < ETHARP_STATE_STATIC) 00324 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00325 { 00326 /* remember entry with oldest stable entry in oldest, its age in maxtime */ 00327 if (arp_table[i].ctime >= age_stable) { 00328 old_stable = i; 00329 age_stable = arp_table[i].ctime; 00330 } 00331 } 00332 } 00333 } 00334 } 00335 /* { we have no match } => try to create a new entry */ 00336 00337 /* don't create new entry, only search? */ 00338 if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) || 00339 /* or no empty entry found and not allowed to recycle? */ 00340 ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) { 00341 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n")); 00342 return (s16_t)ERR_MEM; 00343 } 00344 00345 /* b) choose the least destructive entry to recycle: 00346 * 1) empty entry 00347 * 2) oldest stable entry 00348 * 3) oldest pending entry without queued packets 00349 * 4) oldest pending entry with queued packets 00350 * 00351 * { ETHARP_FLAG_TRY_HARD is set at this point } 00352 */ 00353 00354 /* 1) empty entry available? */ 00355 if (empty < ARP_TABLE_SIZE) { 00356 i = empty; 00357 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting empty entry %d\n", (int)i)); 00358 } else { 00359 /* 2) found recyclable stable entry? */ 00360 if (old_stable < ARP_TABLE_SIZE) { 00361 /* recycle oldest stable*/ 00362 i = old_stable; 00363 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest stable entry %d\n", (int)i)); 00364 /* no queued packets should exist on stable entries */ 00365 LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL); 00366 /* 3) found recyclable pending entry without queued packets? */ 00367 } else if (old_pending < ARP_TABLE_SIZE) { 00368 /* recycle oldest pending */ 00369 i = old_pending; 00370 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d (without queue)\n", (int)i)); 00371 /* 4) found recyclable pending entry with queued packets? */ 00372 } else if (old_queue < ARP_TABLE_SIZE) { 00373 /* recycle oldest pending (queued packets are free in etharp_free_entry) */ 00374 i = old_queue; 00375 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))); 00376 /* no empty or recyclable entries found */ 00377 } else { 00378 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n")); 00379 return (s16_t)ERR_MEM; 00380 } 00381 00382 /* { empty or recyclable entry found } */ 00383 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); 00384 etharp_free_entry(i); 00385 } 00386 00387 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); 00388 LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY", 00389 arp_table[i].state == ETHARP_STATE_EMPTY); 00390 00391 /* IP address given? */ 00392 if (ipaddr != NULL) { 00393 /* set IP address */ 00394 ip4_addr_copy(arp_table[i].ipaddr, *ipaddr); 00395 } 00396 arp_table[i].ctime = 0; 00397 #if ETHARP_TABLE_MATCH_NETIF 00398 arp_table[i].netif = netif; 00399 #endif /* ETHARP_TABLE_MATCH_NETIF */ 00400 return (s16_t)i; 00401 } 00402 00403 /** 00404 * Update (or insert) a IP/MAC address pair in the ARP cache. 00405 * 00406 * If a pending entry is resolved, any queued packets will be sent 00407 * at this point. 00408 * 00409 * @param netif netif related to this entry (used for NETIF_ADDRHINT) 00410 * @param ipaddr IP address of the inserted ARP entry. 00411 * @param ethaddr Ethernet address of the inserted ARP entry. 00412 * @param flags See @ref etharp_state 00413 * 00414 * @return 00415 * - ERR_OK Successfully updated ARP cache. 00416 * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set. 00417 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache. 00418 * 00419 * @see pbuf_free() 00420 */ 00421 static err_t 00422 etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags) 00423 { 00424 s16_t i; 00425 LWIP_ASSERT("netif->hwaddr_len == ETH_HWADDR_LEN", netif->hwaddr_len == ETH_HWADDR_LEN); 00426 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", 00427 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr), 00428 (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2], 00429 (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5])); 00430 /* non-unicast address? */ 00431 if (ip4_addr_isany(ipaddr) || 00432 ip4_addr_isbroadcast(ipaddr, netif) || 00433 ip4_addr_ismulticast(ipaddr)) { 00434 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n")); 00435 return ERR_ARG; 00436 } 00437 /* find or create ARP entry */ 00438 i = etharp_find_entry(ipaddr, flags, netif); 00439 /* bail out if no entry could be found */ 00440 if (i < 0) { 00441 return (err_t)i; 00442 } 00443 00444 #if ETHARP_SUPPORT_STATIC_ENTRIES 00445 if (flags & ETHARP_FLAG_STATIC_ENTRY) { 00446 /* record static type */ 00447 arp_table[i].state = ETHARP_STATE_STATIC; 00448 } else if (arp_table[i].state == ETHARP_STATE_STATIC) { 00449 /* found entry is a static type, don't overwrite it */ 00450 return ERR_VAL; 00451 } else 00452 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00453 { 00454 /* mark it stable */ 00455 arp_table[i].state = ETHARP_STATE_STABLE; 00456 } 00457 00458 /* record network interface */ 00459 arp_table[i].netif = netif; 00460 /* insert in SNMP ARP index tree */ 00461 mib2_add_arp_entry(netif, &arp_table[i].ipaddr); 00462 00463 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", i)); 00464 /* update address */ 00465 SMEMCPY(&arp_table[i].ethaddr, ethaddr, ETH_HWADDR_LEN); 00466 /* reset time stamp */ 00467 arp_table[i].ctime = 0; 00468 /* this is where we will send out queued packets! */ 00469 #if ARP_QUEUEING 00470 while (arp_table[i].q != NULL) { 00471 struct pbuf *p; 00472 /* remember remainder of queue */ 00473 struct etharp_q_entry *q = arp_table[i].q; 00474 /* pop first item off the queue */ 00475 arp_table[i].q = q->next; 00476 /* get the packet pointer */ 00477 p = q->p; 00478 /* now queue entry can be freed */ 00479 memp_free(MEMP_ARP_QUEUE, q); 00480 #else /* ARP_QUEUEING */ 00481 if (arp_table[i].q != NULL) { 00482 struct pbuf *p = arp_table[i].q; 00483 arp_table[i].q = NULL; 00484 #endif /* ARP_QUEUEING */ 00485 /* send the queued IP packet */ 00486 ethernet_output(netif, p, (struct eth_addr *)(netif->hwaddr), ethaddr, ETHTYPE_IP); 00487 /* free the queued IP packet */ 00488 pbuf_free(p); 00489 } 00490 return ERR_OK; 00491 } 00492 00493 #if ETHARP_SUPPORT_STATIC_ENTRIES 00494 /** Add a new static entry to the ARP table. If an entry exists for the 00495 * specified IP address, this entry is overwritten. 00496 * If packets are queued for the specified IP address, they are sent out. 00497 * 00498 * @param ipaddr IP address for the new static entry 00499 * @param ethaddr ethernet address for the new static entry 00500 * @return See return values of etharp_add_static_entry 00501 */ 00502 err_t 00503 etharp_add_static_entry(const ip4_addr_t *ipaddr, struct eth_addr *ethaddr) 00504 { 00505 struct netif *netif; 00506 LWIP_ASSERT_CORE_LOCKED(); 00507 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", 00508 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr), 00509 (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2], 00510 (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5])); 00511 00512 netif = ip4_route(ipaddr); 00513 if (netif == NULL) { 00514 return ERR_RTE; 00515 } 00516 00517 return etharp_update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY); 00518 } 00519 00520 /** Remove a static entry from the ARP table previously added with a call to 00521 * etharp_add_static_entry. 00522 * 00523 * @param ipaddr IP address of the static entry to remove 00524 * @return ERR_OK: entry removed 00525 * ERR_MEM: entry wasn't found 00526 * ERR_ARG: entry wasn't a static entry but a dynamic one 00527 */ 00528 err_t 00529 etharp_remove_static_entry(const ip4_addr_t *ipaddr) 00530 { 00531 s16_t i; 00532 LWIP_ASSERT_CORE_LOCKED(); 00533 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", 00534 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr))); 00535 00536 /* find or create ARP entry */ 00537 i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, NULL); 00538 /* bail out if no entry could be found */ 00539 if (i < 0) { 00540 return (err_t)i; 00541 } 00542 00543 if (arp_table[i].state != ETHARP_STATE_STATIC) { 00544 /* entry wasn't a static entry, cannot remove it */ 00545 return ERR_ARG; 00546 } 00547 /* entry found, free it */ 00548 etharp_free_entry(i); 00549 return ERR_OK; 00550 } 00551 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00552 00553 /** 00554 * Remove all ARP table entries of the specified netif. 00555 * 00556 * @param netif points to a network interface 00557 */ 00558 void 00559 etharp_cleanup_netif(struct netif *netif) 00560 { 00561 int i; 00562 00563 for (i = 0; i < ARP_TABLE_SIZE; ++i) { 00564 u8_t state = arp_table[i].state; 00565 if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) { 00566 etharp_free_entry(i); 00567 } 00568 } 00569 } 00570 00571 /** 00572 * Finds (stable) ethernet/IP address pair from ARP table 00573 * using interface and IP address index. 00574 * @note the addresses in the ARP table are in network order! 00575 * 00576 * @param netif points to interface index 00577 * @param ipaddr points to the (network order) IP address index 00578 * @param eth_ret points to return pointer 00579 * @param ip_ret points to return pointer 00580 * @return table index if found, -1 otherwise 00581 */ 00582 ssize_t 00583 etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr, 00584 struct eth_addr **eth_ret, const ip4_addr_t **ip_ret) 00585 { 00586 s16_t i; 00587 00588 LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL", 00589 eth_ret != NULL && ip_ret != NULL); 00590 00591 LWIP_UNUSED_ARG(netif); 00592 00593 i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, netif); 00594 if ((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) { 00595 *eth_ret = &arp_table[i].ethaddr; 00596 *ip_ret = &arp_table[i].ipaddr; 00597 return i; 00598 } 00599 return -1; 00600 } 00601 00602 /** 00603 * Possibility to iterate over stable ARP table entries 00604 * 00605 * @param i entry number, 0 to ARP_TABLE_SIZE 00606 * @param ipaddr return value: IP address 00607 * @param netif return value: points to interface 00608 * @param eth_ret return value: ETH address 00609 * @return 1 on valid index, 0 otherwise 00610 */ 00611 int 00612 etharp_get_entry(size_t i, ip4_addr_t **ipaddr, struct netif **netif, struct eth_addr **eth_ret) 00613 { 00614 LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL); 00615 LWIP_ASSERT("netif != NULL", netif != NULL); 00616 LWIP_ASSERT("eth_ret != NULL", eth_ret != NULL); 00617 00618 if ((i < ARP_TABLE_SIZE) && (arp_table[i].state >= ETHARP_STATE_STABLE)) { 00619 *ipaddr = &arp_table[i].ipaddr; 00620 *netif = arp_table[i].netif; 00621 *eth_ret = &arp_table[i].ethaddr; 00622 return 1; 00623 } else { 00624 return 0; 00625 } 00626 } 00627 00628 /** 00629 * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache 00630 * send out queued IP packets. Updates cache with snooped address pairs. 00631 * 00632 * Should be called for incoming ARP packets. The pbuf in the argument 00633 * is freed by this function. 00634 * 00635 * @param p The ARP packet that arrived on netif. Is freed by this function. 00636 * @param netif The lwIP network interface on which the ARP packet pbuf arrived. 00637 * 00638 * @see pbuf_free() 00639 */ 00640 void 00641 etharp_input(struct pbuf *p, struct netif *netif) 00642 { 00643 struct etharp_hdr *hdr; 00644 /* these are aligned properly, whereas the ARP header fields might not be */ 00645 ip4_addr_t sipaddr, dipaddr; 00646 u8_t for_us; 00647 00648 LWIP_ASSERT_CORE_LOCKED(); 00649 00650 LWIP_ERROR("netif != NULL", (netif != NULL), return;); 00651 00652 hdr = (struct etharp_hdr *)p->payload; 00653 00654 /* RFC 826 "Packet Reception": */ 00655 if ((hdr->hwtype != PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET)) || 00656 (hdr->hwlen != ETH_HWADDR_LEN) || 00657 (hdr->protolen != sizeof(ip4_addr_t)) || 00658 (hdr->proto != PP_HTONS(ETHTYPE_IP))) { 00659 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, 00660 ("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n", 00661 hdr->hwtype, (u16_t)hdr->hwlen, hdr->proto, (u16_t)hdr->protolen)); 00662 ETHARP_STATS_INC(etharp.proterr); 00663 ETHARP_STATS_INC(etharp.drop); 00664 pbuf_free(p); 00665 return; 00666 } 00667 ETHARP_STATS_INC(etharp.recv); 00668 00669 #if LWIP_AUTOIP 00670 /* We have to check if a host already has configured our random 00671 * created link local address and continuously check if there is 00672 * a host with this IP-address so we can detect collisions */ 00673 autoip_arp_reply(netif, hdr); 00674 #endif /* LWIP_AUTOIP */ 00675 00676 /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without 00677 * structure packing (not using structure copy which breaks strict-aliasing rules). */ 00678 IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&sipaddr, &hdr->sipaddr); 00679 IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&dipaddr, &hdr->dipaddr); 00680 00681 /* this interface is not configured? */ 00682 if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { 00683 for_us = 0; 00684 } else { 00685 /* ARP packet directed to us? */ 00686 for_us = (u8_t)ip4_addr_cmp(&dipaddr, netif_ip4_addr(netif)); 00687 } 00688 00689 /* ARP message directed to us? 00690 -> add IP address in ARP cache; assume requester wants to talk to us, 00691 can result in directly sending the queued packets for this host. 00692 ARP message not directed to us? 00693 -> update the source IP address in the cache, if present */ 00694 etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), 00695 for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY); 00696 00697 /* now act on the message itself */ 00698 switch (hdr->opcode) { 00699 /* ARP request? */ 00700 case PP_HTONS(ARP_REQUEST): 00701 /* ARP request. If it asked for our address, we send out a 00702 * reply. In any case, we time-stamp any existing ARP entry, 00703 * and possibly send out an IP packet that was queued on it. */ 00704 00705 LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP request\n")); 00706 /* ARP request for our address? */ 00707 if (for_us) { 00708 /* send ARP response */ 00709 etharp_raw(netif, 00710 (struct eth_addr *)netif->hwaddr, &hdr->shwaddr, 00711 (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), 00712 &hdr->shwaddr, &sipaddr, 00713 ARP_REPLY); 00714 /* we are not configured? */ 00715 } else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { 00716 /* { for_us == 0 and netif->ip_addr.addr == 0 } */ 00717 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: we are unconfigured, ARP request ignored.\n")); 00718 /* request was not directed to us */ 00719 } else { 00720 /* { for_us == 0 and netif->ip_addr.addr != 0 } */ 00721 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP request was not for us.\n")); 00722 } 00723 break; 00724 case PP_HTONS(ARP_REPLY): 00725 /* ARP reply. We already updated the ARP cache earlier. */ 00726 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP reply\n")); 00727 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK) 00728 /* DHCP wants to know about ARP replies from any host with an 00729 * IP address also offered to us by the DHCP server. We do not 00730 * want to take a duplicate IP address on a single network. 00731 * @todo How should we handle redundant (fail-over) interfaces? */ 00732 dhcp_arp_reply(netif, &sipaddr); 00733 #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */ 00734 break; 00735 default: 00736 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP unknown opcode type %"S16_F"\n", lwip_htons(hdr->opcode))); 00737 ETHARP_STATS_INC(etharp.err); 00738 break; 00739 } 00740 /* free ARP packet */ 00741 pbuf_free(p); 00742 } 00743 00744 /** Just a small helper function that sends a pbuf to an ethernet address 00745 * in the arp_table specified by the index 'arp_idx'. 00746 */ 00747 static err_t 00748 etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, netif_addr_idx_t arp_idx) 00749 { 00750 LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE", 00751 arp_table[arp_idx].state >= ETHARP_STATE_STABLE); 00752 /* if arp table entry is about to expire: re-request it, 00753 but only if its state is ETHARP_STATE_STABLE to prevent flooding the 00754 network with ARP requests if this address is used frequently. */ 00755 if (arp_table[arp_idx].state == ETHARP_STATE_STABLE) { 00756 if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_BROADCAST) { 00757 /* issue a standard request using broadcast */ 00758 if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) { 00759 arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1; 00760 } 00761 } else if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_UNICAST) { 00762 /* issue a unicast request (for 15 seconds) to prevent unnecessary broadcast */ 00763 if (etharp_request_dst(netif, &arp_table[arp_idx].ipaddr, &arp_table[arp_idx].ethaddr) == ERR_OK) { 00764 arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1; 00765 } 00766 } 00767 } 00768 00769 return ethernet_output(netif, q, (struct eth_addr *)(netif->hwaddr), &arp_table[arp_idx].ethaddr, ETHTYPE_IP); 00770 } 00771 00772 /** 00773 * Resolve and fill-in Ethernet address header for outgoing IP packet. 00774 * 00775 * For IP multicast and broadcast, corresponding Ethernet addresses 00776 * are selected and the packet is transmitted on the link. 00777 * 00778 * For unicast addresses, the packet is submitted to etharp_query(). In 00779 * case the IP address is outside the local network, the IP address of 00780 * the gateway is used. 00781 * 00782 * @param netif The lwIP network interface which the IP packet will be sent on. 00783 * @param q The pbuf(s) containing the IP packet to be sent. 00784 * @param ipaddr The IP address of the packet destination. 00785 * 00786 * @return 00787 * - ERR_RTE No route to destination (no gateway to external networks), 00788 * or the return type of either etharp_query() or ethernet_output(). 00789 */ 00790 err_t 00791 etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) 00792 { 00793 const struct eth_addr *dest; 00794 struct eth_addr mcastaddr; 00795 const ip4_addr_t *dst_addr = ipaddr; 00796 00797 LWIP_ASSERT_CORE_LOCKED(); 00798 LWIP_ASSERT("netif != NULL", netif != NULL); 00799 LWIP_ASSERT("q != NULL", q != NULL); 00800 LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL); 00801 00802 /* Determine on destination hardware address. Broadcasts and multicasts 00803 * are special, other IP addresses are looked up in the ARP table. */ 00804 00805 /* broadcast destination IP address? */ 00806 if (ip4_addr_isbroadcast(ipaddr, netif)) { 00807 /* broadcast on Ethernet also */ 00808 dest = (const struct eth_addr *)ðbroadcast; 00809 /* multicast destination IP address? */ 00810 } else if (ip4_addr_ismulticast(ipaddr)) { 00811 /* Hash IP multicast address to MAC address.*/ 00812 mcastaddr.addr[0] = LL_IP4_MULTICAST_ADDR_0; 00813 mcastaddr.addr[1] = LL_IP4_MULTICAST_ADDR_1; 00814 mcastaddr.addr[2] = LL_IP4_MULTICAST_ADDR_2; 00815 mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f; 00816 mcastaddr.addr[4] = ip4_addr3(ipaddr); 00817 mcastaddr.addr[5] = ip4_addr4(ipaddr); 00818 /* destination Ethernet address is multicast */ 00819 dest = &mcastaddr; 00820 /* unicast destination IP address? */ 00821 } else { 00822 netif_addr_idx_t i; 00823 /* outside local network? if so, this can neither be a global broadcast nor 00824 a subnet broadcast. */ 00825 if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) && 00826 !ip4_addr_islinklocal(ipaddr)) { 00827 #if LWIP_AUTOIP 00828 struct ip_hdr *iphdr = LWIP_ALIGNMENT_CAST(struct ip_hdr *, q->payload); 00829 /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with 00830 a link-local source address must always be "directly to its destination 00831 on the same physical link. The host MUST NOT send the packet to any 00832 router for forwarding". */ 00833 if (!ip4_addr_islinklocal(&iphdr->src)) 00834 #endif /* LWIP_AUTOIP */ 00835 { 00836 #ifdef LWIP_HOOK_ETHARP_GET_GW 00837 /* For advanced routing, a single default gateway might not be enough, so get 00838 the IP address of the gateway to handle the current destination address. */ 00839 dst_addr = LWIP_HOOK_ETHARP_GET_GW(netif, ipaddr); 00840 if (dst_addr == NULL) 00841 #endif /* LWIP_HOOK_ETHARP_GET_GW */ 00842 { 00843 /* interface has default gateway? */ 00844 if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) { 00845 /* send to hardware address of default gateway IP address */ 00846 dst_addr = netif_ip4_gw(netif); 00847 /* no default gateway available */ 00848 } else { 00849 /* no route to destination error (default gateway missing) */ 00850 return ERR_RTE; 00851 } 00852 } 00853 } 00854 } 00855 #if LWIP_NETIF_HWADDRHINT 00856 if (netif->hints != NULL) { 00857 /* per-pcb cached entry was given */ 00858 netif_addr_idx_t etharp_cached_entry = netif->hints->addr_hint; 00859 if (etharp_cached_entry < ARP_TABLE_SIZE) { 00860 #endif /* LWIP_NETIF_HWADDRHINT */ 00861 if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) && 00862 #if ETHARP_TABLE_MATCH_NETIF 00863 (arp_table[etharp_cached_entry].netif == netif) && 00864 #endif 00865 (ip4_addr_cmp(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) { 00866 /* the per-pcb-cached entry is stable and the right one! */ 00867 ETHARP_STATS_INC(etharp.cachehit); 00868 return etharp_output_to_arp_index(netif, q, etharp_cached_entry); 00869 } 00870 #if LWIP_NETIF_HWADDRHINT 00871 } 00872 } 00873 #endif /* LWIP_NETIF_HWADDRHINT */ 00874 00875 /* find stable entry: do this here since this is a critical path for 00876 throughput and etharp_find_entry() is kind of slow */ 00877 for (i = 0; i < ARP_TABLE_SIZE; i++) { 00878 if ((arp_table[i].state >= ETHARP_STATE_STABLE) && 00879 #if ETHARP_TABLE_MATCH_NETIF 00880 (arp_table[i].netif == netif) && 00881 #endif 00882 (ip4_addr_cmp(dst_addr, &arp_table[i].ipaddr))) { 00883 /* found an existing, stable entry */ 00884 ETHARP_SET_ADDRHINT(netif, i); 00885 return etharp_output_to_arp_index(netif, q, i); 00886 } 00887 } 00888 /* no stable entry found, use the (slower) query function: 00889 queue on destination Ethernet address belonging to ipaddr */ 00890 return etharp_query(netif, dst_addr, q); 00891 } 00892 00893 /* continuation for multicast/broadcast destinations */ 00894 /* obtain source Ethernet address of the given interface */ 00895 /* send packet directly on the link */ 00896 return ethernet_output(netif, q, (struct eth_addr *)(netif->hwaddr), dest, ETHTYPE_IP); 00897 } 00898 00899 /** 00900 * Send an ARP request for the given IP address and/or queue a packet. 00901 * 00902 * If the IP address was not yet in the cache, a pending ARP cache entry 00903 * is added and an ARP request is sent for the given address. The packet 00904 * is queued on this entry. 00905 * 00906 * If the IP address was already pending in the cache, a new ARP request 00907 * is sent for the given address. The packet is queued on this entry. 00908 * 00909 * If the IP address was already stable in the cache, and a packet is 00910 * given, it is directly sent and no ARP request is sent out. 00911 * 00912 * If the IP address was already stable in the cache, and no packet is 00913 * given, an ARP request is sent out. 00914 * 00915 * @param netif The lwIP network interface on which ipaddr 00916 * must be queried for. 00917 * @param ipaddr The IP address to be resolved. 00918 * @param q If non-NULL, a pbuf that must be delivered to the IP address. 00919 * q is not freed by this function. 00920 * 00921 * @note q must only be ONE packet, not a packet queue! 00922 * 00923 * @return 00924 * - ERR_BUF Could not make room for Ethernet header. 00925 * - ERR_MEM Hardware address unknown, and no more ARP entries available 00926 * to query for address or queue the packet. 00927 * - ERR_MEM Could not queue packet due to memory shortage. 00928 * - ERR_RTE No route to destination (no gateway to external networks). 00929 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache. 00930 * 00931 */ 00932 err_t 00933 etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q) 00934 { 00935 struct eth_addr *srcaddr = (struct eth_addr *)netif->hwaddr; 00936 err_t result = ERR_MEM; 00937 int is_new_entry = 0; 00938 s16_t i_err; 00939 netif_addr_idx_t i; 00940 00941 /* non-unicast address? */ 00942 if (ip4_addr_isbroadcast(ipaddr, netif) || 00943 ip4_addr_ismulticast(ipaddr) || 00944 ip4_addr_isany(ipaddr)) { 00945 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n")); 00946 return ERR_ARG; 00947 } 00948 00949 /* find entry in ARP cache, ask to create entry if queueing packet */ 00950 i_err = etharp_find_entry(ipaddr, ETHARP_FLAG_TRY_HARD, netif); 00951 00952 /* could not find or create entry? */ 00953 if (i_err < 0) { 00954 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n")); 00955 if (q) { 00956 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n")); 00957 ETHARP_STATS_INC(etharp.memerr); 00958 } 00959 return (err_t)i_err; 00960 } 00961 LWIP_ASSERT("type overflow", (size_t)i_err < NETIF_ADDR_IDX_MAX); 00962 i = (netif_addr_idx_t)i_err; 00963 00964 /* mark a fresh entry as pending (we just sent a request) */ 00965 if (arp_table[i].state == ETHARP_STATE_EMPTY) { 00966 is_new_entry = 1; 00967 arp_table[i].state = ETHARP_STATE_PENDING; 00968 /* record network interface for re-sending arp request in etharp_tmr */ 00969 arp_table[i].netif = netif; 00970 } 00971 00972 /* { i is either a STABLE or (new or existing) PENDING entry } */ 00973 LWIP_ASSERT("arp_table[i].state == PENDING or STABLE", 00974 ((arp_table[i].state == ETHARP_STATE_PENDING) || 00975 (arp_table[i].state >= ETHARP_STATE_STABLE))); 00976 00977 /* do we have a new entry? or an implicit query request? */ 00978 if (is_new_entry || (q == NULL)) { 00979 /* try to resolve it; send out ARP request */ 00980 result = etharp_request(netif, ipaddr); 00981 if (result != ERR_OK) { 00982 /* ARP request couldn't be sent */ 00983 /* We don't re-send arp request in etharp_tmr, but we still queue packets, 00984 since this failure could be temporary, and the next packet calling 00985 etharp_query again could lead to sending the queued packets. */ 00986 } 00987 if (q == NULL) { 00988 return result; 00989 } 00990 } 00991 00992 /* packet given? */ 00993 LWIP_ASSERT("q != NULL", q != NULL); 00994 /* stable entry? */ 00995 if (arp_table[i].state >= ETHARP_STATE_STABLE) { 00996 /* we have a valid IP->Ethernet address mapping */ 00997 ETHARP_SET_ADDRHINT(netif, i); 00998 /* send the packet */ 00999 result = ethernet_output(netif, q, srcaddr, &(arp_table[i].ethaddr), ETHTYPE_IP); 01000 /* pending entry? (either just created or already pending */ 01001 } else if (arp_table[i].state == ETHARP_STATE_PENDING) { 01002 /* entry is still pending, queue the given packet 'q' */ 01003 struct pbuf *p; 01004 int copy_needed = 0; 01005 /* IF q includes a pbuf that must be copied, copy the whole chain into a 01006 * new PBUF_RAM. See the definition of PBUF_NEEDS_COPY for details. */ 01007 p = q; 01008 while (p) { 01009 LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0)); 01010 if (PBUF_NEEDS_COPY(p)) { 01011 copy_needed = 1; 01012 break; 01013 } 01014 p = p->next; 01015 } 01016 if (copy_needed) { 01017 /* copy the whole packet into new pbufs */ 01018 p = pbuf_clone(PBUF_LINK, PBUF_RAM, q); 01019 } else { 01020 /* referencing the old pbuf is enough */ 01021 p = q; 01022 pbuf_ref(p); 01023 } 01024 /* packet could be taken over? */ 01025 if (p != NULL) { 01026 /* queue packet ... */ 01027 #if ARP_QUEUEING 01028 struct etharp_q_entry *new_entry; 01029 /* allocate a new arp queue entry */ 01030 new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE); 01031 if (new_entry != NULL) { 01032 unsigned int qlen = 0; 01033 new_entry->next = 0; 01034 new_entry->p = p; 01035 if (arp_table[i].q != NULL) { 01036 /* queue was already existent, append the new entry to the end */ 01037 struct etharp_q_entry *r; 01038 r = arp_table[i].q; 01039 qlen++; 01040 while (r->next != NULL) { 01041 r = r->next; 01042 qlen++; 01043 } 01044 r->next = new_entry; 01045 } else { 01046 /* queue did not exist, first item in queue */ 01047 arp_table[i].q = new_entry; 01048 } 01049 #if ARP_QUEUE_LEN 01050 if (qlen >= ARP_QUEUE_LEN) { 01051 struct etharp_q_entry *old; 01052 old = arp_table[i].q; 01053 arp_table[i].q = arp_table[i].q->next; 01054 pbuf_free(old->p); 01055 memp_free(MEMP_ARP_QUEUE, old); 01056 } 01057 #endif 01058 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, i)); 01059 result = ERR_OK; 01060 } else { 01061 /* the pool MEMP_ARP_QUEUE is empty */ 01062 pbuf_free(p); 01063 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)); 01064 result = ERR_MEM; 01065 } 01066 #else /* ARP_QUEUEING */ 01067 /* always queue one packet per ARP request only, freeing a previously queued packet */ 01068 if (arp_table[i].q != NULL) { 01069 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)); 01070 pbuf_free(arp_table[i].q); 01071 } 01072 arp_table[i].q = p; 01073 result = ERR_OK; 01074 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, (u16_t)i)); 01075 #endif /* ARP_QUEUEING */ 01076 } else { 01077 ETHARP_STATS_INC(etharp.memerr); 01078 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)); 01079 result = ERR_MEM; 01080 } 01081 } 01082 return result; 01083 } 01084 01085 /** 01086 * Send a raw ARP packet (opcode and all addresses can be modified) 01087 * 01088 * @param netif the lwip network interface on which to send the ARP packet 01089 * @param ethsrc_addr the source MAC address for the ethernet header 01090 * @param ethdst_addr the destination MAC address for the ethernet header 01091 * @param hwsrc_addr the source MAC address for the ARP protocol header 01092 * @param ipsrc_addr the source IP address for the ARP protocol header 01093 * @param hwdst_addr the destination MAC address for the ARP protocol header 01094 * @param ipdst_addr the destination IP address for the ARP protocol header 01095 * @param opcode the type of the ARP packet 01096 * @return ERR_OK if the ARP packet has been sent 01097 * ERR_MEM if the ARP packet couldn't be allocated 01098 * any other err_t on failure 01099 */ 01100 static err_t 01101 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr, 01102 const struct eth_addr *ethdst_addr, 01103 const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr, 01104 const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr, 01105 const u16_t opcode) 01106 { 01107 struct pbuf *p; 01108 err_t result = ERR_OK; 01109 struct etharp_hdr *hdr; 01110 01111 LWIP_ASSERT("netif != NULL", netif != NULL); 01112 01113 /* allocate a pbuf for the outgoing ARP request packet */ 01114 p = pbuf_alloc(PBUF_LINK, SIZEOF_ETHARP_HDR, PBUF_RAM); 01115 /* could allocate a pbuf for an ARP request? */ 01116 if (p == NULL) { 01117 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, 01118 ("etharp_raw: could not allocate pbuf for ARP request.\n")); 01119 ETHARP_STATS_INC(etharp.memerr); 01120 return ERR_MEM; 01121 } 01122 LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr", 01123 (p->len >= SIZEOF_ETHARP_HDR)); 01124 01125 hdr = (struct etharp_hdr *)p->payload; 01126 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n")); 01127 hdr->opcode = lwip_htons(opcode); 01128 01129 LWIP_ASSERT("netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!", 01130 (netif->hwaddr_len == ETH_HWADDR_LEN)); 01131 01132 /* Write the ARP MAC-Addresses */ 01133 SMEMCPY(&hdr->shwaddr, hwsrc_addr, ETH_HWADDR_LEN); 01134 SMEMCPY(&hdr->dhwaddr, hwdst_addr, ETH_HWADDR_LEN); 01135 /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without 01136 * structure packing. */ 01137 IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->sipaddr, ipsrc_addr); 01138 IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->dipaddr, ipdst_addr); 01139 01140 hdr->hwtype = PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET); 01141 hdr->proto = PP_HTONS(ETHTYPE_IP); 01142 /* set hwlen and protolen */ 01143 hdr->hwlen = ETH_HWADDR_LEN; 01144 hdr->protolen = sizeof(ip4_addr_t); 01145 01146 /* send ARP query */ 01147 #if LWIP_AUTOIP 01148 /* If we are using Link-Local, all ARP packets that contain a Link-Local 01149 * 'sender IP address' MUST be sent using link-layer broadcast instead of 01150 * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */ 01151 if (ip4_addr_islinklocal(ipsrc_addr)) { 01152 ethernet_output(netif, p, ethsrc_addr, ðbroadcast, ETHTYPE_ARP); 01153 } else 01154 #endif /* LWIP_AUTOIP */ 01155 { 01156 ethernet_output(netif, p, ethsrc_addr, ethdst_addr, ETHTYPE_ARP); 01157 } 01158 01159 ETHARP_STATS_INC(etharp.xmit); 01160 /* free ARP query packet */ 01161 pbuf_free(p); 01162 p = NULL; 01163 /* could not allocate pbuf for ARP request */ 01164 01165 return result; 01166 } 01167 01168 /** 01169 * Send an ARP request packet asking for ipaddr to a specific eth address. 01170 * Used to send unicast request to refresh the ARP table just before an entry 01171 * times out 01172 * 01173 * @param netif the lwip network interface on which to send the request 01174 * @param ipaddr the IP address for which to ask 01175 * @param hw_dst_addr the ethernet address to send this packet to 01176 * @return ERR_OK if the request has been sent 01177 * ERR_MEM if the ARP packet couldn't be allocated 01178 * any other err_t on failure 01179 */ 01180 static err_t 01181 etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr) 01182 { 01183 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, hw_dst_addr, 01184 (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), ðzero, 01185 ipaddr, ARP_REQUEST); 01186 } 01187 01188 /** 01189 * Send an ARP request packet asking for ipaddr. 01190 * 01191 * @param netif the lwip network interface on which to send the request 01192 * @param ipaddr the IP address for which to ask 01193 * @return ERR_OK if the request has been sent 01194 * ERR_MEM if the ARP packet couldn't be allocated 01195 * any other err_t on failure 01196 */ 01197 err_t 01198 etharp_request(struct netif *netif, const ip4_addr_t *ipaddr) 01199 { 01200 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n")); 01201 return etharp_request_dst(netif, ipaddr, ðbroadcast); 01202 } 01203 01204 #endif /* LWIP_IPV4 && LWIP_ARP */
Generated on Tue Jul 12 2022 13:54:28 by
