Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
lwip_etharp.c
Go to the documentation of this file.
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_ARP || LWIP_ETHERNET 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 "netif/lwip_ethernet.h" 00056 00057 #include <string.h> 00058 00059 #ifdef LWIP_HOOK_FILENAME 00060 #include LWIP_HOOK_FILENAME 00061 #endif 00062 00063 #if LWIP_IPV4 && LWIP_ARP /* don't build if not configured for use in lwipopts.h */ 00064 00065 /** Re-request a used ARP entry 1 minute before it would expire to prevent 00066 * breaking a steadily used connection because the ARP entry timed out. */ 00067 #define ARP_AGE_REREQUEST_USED_UNICAST (ARP_MAXAGE - 30) 00068 #define ARP_AGE_REREQUEST_USED_BROADCAST (ARP_MAXAGE - 15) 00069 00070 /** the time an ARP entry stays pending after first request, 00071 * for ARP_TMR_INTERVAL = 1000, this is 00072 * 10 seconds. 00073 * 00074 * @internal Keep this number at least 2, otherwise it might 00075 * run out instantly if the timeout occurs directly after a request. 00076 */ 00077 #define ARP_MAXPENDING 5 00078 00079 /** ARP states */ 00080 enum etharp_state { 00081 ETHARP_STATE_EMPTY = 0, 00082 ETHARP_STATE_PENDING, 00083 ETHARP_STATE_STABLE, 00084 ETHARP_STATE_STABLE_REREQUESTING_1, 00085 ETHARP_STATE_STABLE_REREQUESTING_2 00086 #if ETHARP_SUPPORT_STATIC_ENTRIES 00087 ,ETHARP_STATE_STATIC 00088 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00089 }; 00090 00091 struct etharp_entry { 00092 #if ARP_QUEUEING 00093 /** Pointer to queue of pending outgoing packets on this ARP entry. */ 00094 struct etharp_q_entry *q; 00095 #else /* ARP_QUEUEING */ 00096 /** Pointer to a single pending outgoing packet on this ARP entry. */ 00097 struct pbuf *q; 00098 #endif /* ARP_QUEUEING */ 00099 ip4_addr_t ipaddr; 00100 struct netif *netif; 00101 struct eth_addr ethaddr; 00102 u16_t ctime; 00103 u8_t state; 00104 }; 00105 00106 static struct etharp_entry arp_table[ARP_TABLE_SIZE]; 00107 00108 #if !LWIP_NETIF_HWADDRHINT 00109 static u8_t etharp_cached_entry; 00110 #endif /* !LWIP_NETIF_HWADDRHINT */ 00111 00112 /** Try hard to create a new entry - we want the IP address to appear in 00113 the cache (even if this means removing an active entry or so). */ 00114 #define ETHARP_FLAG_TRY_HARD 1 00115 #define ETHARP_FLAG_FIND_ONLY 2 00116 #if ETHARP_SUPPORT_STATIC_ENTRIES 00117 #define ETHARP_FLAG_STATIC_ENTRY 4 00118 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00119 00120 #if LWIP_NETIF_HWADDRHINT 00121 #define ETHARP_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \ 00122 *((netif)->addr_hint) = (hint); 00123 #else /* LWIP_NETIF_HWADDRHINT */ 00124 #define ETHARP_SET_HINT(netif, hint) (etharp_cached_entry = (hint)) 00125 #endif /* LWIP_NETIF_HWADDRHINT */ 00126 00127 00128 /* Some checks, instead of etharp_init(): */ 00129 #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f)) 00130 #error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h" 00131 #endif 00132 00133 00134 static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr* hw_dst_addr); 00135 static err_t etharp_raw(struct netif *netif, 00136 const struct eth_addr *ethsrc_addr, const struct eth_addr *ethdst_addr, 00137 const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr, 00138 const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr, 00139 const u16_t opcode); 00140 00141 #if ARP_QUEUEING 00142 /** 00143 * Free a complete queue of etharp entries 00144 * 00145 * @param q a qeueue of etharp_q_entry's to free 00146 */ 00147 static void 00148 free_etharp_q(struct etharp_q_entry *q) 00149 { 00150 struct etharp_q_entry *r; 00151 LWIP_ASSERT("q != NULL", q != NULL); 00152 LWIP_ASSERT("q->p != NULL", q->p != NULL); 00153 while (q) { 00154 r = q; 00155 q = q->next; 00156 LWIP_ASSERT("r->p != NULL", (r->p != NULL)); 00157 pbuf_free(r->p); 00158 memp_free(MEMP_ARP_QUEUE, r); 00159 } 00160 } 00161 #else /* ARP_QUEUEING */ 00162 00163 /** Compatibility define: free the queued pbuf */ 00164 #define free_etharp_q(q) pbuf_free(q) 00165 00166 #endif /* ARP_QUEUEING */ 00167 00168 /** Clean up ARP table entries */ 00169 static void 00170 etharp_free_entry(int i) 00171 { 00172 /* remove from SNMP ARP index tree */ 00173 mib2_remove_arp_entry(arp_table[i].netif, &arp_table[i].ipaddr); 00174 /* and empty packet queue */ 00175 if (arp_table[i].q != NULL) { 00176 /* remove all queued packets */ 00177 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); 00178 free_etharp_q(arp_table[i].q); 00179 arp_table[i].q = NULL; 00180 } 00181 /* recycle entry for re-use */ 00182 arp_table[i].state = ETHARP_STATE_EMPTY; 00183 #ifdef LWIP_DEBUG 00184 /* for debugging, clean out the complete entry */ 00185 arp_table[i].ctime = 0; 00186 arp_table[i].netif = NULL; 00187 ip4_addr_set_zero(&arp_table[i].ipaddr); 00188 arp_table[i].ethaddr = ethzero; 00189 #endif /* LWIP_DEBUG */ 00190 } 00191 00192 /** 00193 * Clears expired entries in the ARP table. 00194 * 00195 * This function should be called every ARP_TMR_INTERVAL milliseconds (1 second), 00196 * in order to expire entries in the ARP table. 00197 */ 00198 void 00199 etharp_tmr(void) 00200 { 00201 u8_t i; 00202 00203 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n")); 00204 /* remove expired entries from the ARP table */ 00205 for (i = 0; i < ARP_TABLE_SIZE; ++i) { 00206 u8_t state = arp_table[i].state; 00207 if (state != ETHARP_STATE_EMPTY 00208 #if ETHARP_SUPPORT_STATIC_ENTRIES 00209 && (state != ETHARP_STATE_STATIC) 00210 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00211 ) { 00212 arp_table[i].ctime++; 00213 if ((arp_table[i].ctime >= ARP_MAXAGE) || 00214 ((arp_table[i].state == ETHARP_STATE_PENDING) && 00215 (arp_table[i].ctime >= ARP_MAXPENDING))) { 00216 /* pending or stable entry has become old! */ 00217 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n", 00218 arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i)); 00219 /* clean up entries that have just been expired */ 00220 etharp_free_entry(i); 00221 } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_1) { 00222 /* Don't send more than one request every 2 seconds. */ 00223 arp_table[i].state = ETHARP_STATE_STABLE_REREQUESTING_2; 00224 } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_2) { 00225 /* Reset state to stable, so that the next transmitted packet will 00226 re-send an ARP request. */ 00227 arp_table[i].state = ETHARP_STATE_STABLE; 00228 } else if (arp_table[i].state == ETHARP_STATE_PENDING) { 00229 /* still pending, resend an ARP query */ 00230 etharp_request(arp_table[i].netif, &arp_table[i].ipaddr); 00231 } 00232 } 00233 } 00234 } 00235 00236 /** 00237 * Search the ARP table for a matching or new entry. 00238 * 00239 * If an IP address is given, return a pending or stable ARP entry that matches 00240 * the address. If no match is found, create a new entry with this address set, 00241 * but in state ETHARP_EMPTY. The caller must check and possibly change the 00242 * state of the returned entry. 00243 * 00244 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY. 00245 * 00246 * In all cases, attempt to create new entries from an empty entry. If no 00247 * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle 00248 * old entries. Heuristic choose the least important entry for recycling. 00249 * 00250 * @param ipaddr IP address to find in ARP cache, or to add if not found. 00251 * @param flags See @ref etharp_state 00252 * @param netif netif related to this address (used for NETIF_HWADDRHINT) 00253 * 00254 * @return The ARP entry index that matched or is created, ERR_MEM if no 00255 * entry is found or could be recycled. 00256 */ 00257 static s8_t 00258 etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif* netif) 00259 { 00260 s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE; 00261 s8_t empty = ARP_TABLE_SIZE; 00262 u8_t i = 0; 00263 /* oldest entry with packets on queue */ 00264 s8_t old_queue = ARP_TABLE_SIZE; 00265 /* its age */ 00266 u16_t age_queue = 0, age_pending = 0, age_stable = 0; 00267 00268 LWIP_UNUSED_ARG(netif); 00269 00270 /** 00271 * a) do a search through the cache, remember candidates 00272 * b) select candidate entry 00273 * c) create new entry 00274 */ 00275 00276 /* a) in a single search sweep, do all of this 00277 * 1) remember the first empty entry (if any) 00278 * 2) remember the oldest stable entry (if any) 00279 * 3) remember the oldest pending entry without queued packets (if any) 00280 * 4) remember the oldest pending entry with queued packets (if any) 00281 * 5) search for a matching IP entry, either pending or stable 00282 * until 5 matches, or all entries are searched for. 00283 */ 00284 00285 for (i = 0; i < ARP_TABLE_SIZE; ++i) { 00286 u8_t state = arp_table[i].state; 00287 /* no empty entry found yet and now we do find one? */ 00288 if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) { 00289 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_find_entry: found empty entry %"U16_F"\n", (u16_t)i)); 00290 /* remember first empty entry */ 00291 empty = i; 00292 } else if (state != ETHARP_STATE_EMPTY) { 00293 LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE", 00294 state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE); 00295 /* if given, does IP address match IP address in ARP entry? */ 00296 if (ipaddr && ip4_addr_cmp(ipaddr, &arp_table[i].ipaddr) 00297 #if ETHARP_TABLE_MATCH_NETIF 00298 && ((netif == NULL) || (netif == arp_table[i].netif)) 00299 #endif /* ETHARP_TABLE_MATCH_NETIF */ 00300 ) { 00301 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %"U16_F"\n", (u16_t)i)); 00302 /* found exact IP address match, simply bail out */ 00303 return i; 00304 } 00305 /* pending entry? */ 00306 if (state == ETHARP_STATE_PENDING) { 00307 /* pending with queued packets? */ 00308 if (arp_table[i].q != NULL) { 00309 if (arp_table[i].ctime >= age_queue) { 00310 old_queue = i; 00311 age_queue = arp_table[i].ctime; 00312 } 00313 } else 00314 /* pending without queued packets? */ 00315 { 00316 if (arp_table[i].ctime >= age_pending) { 00317 old_pending = i; 00318 age_pending = arp_table[i].ctime; 00319 } 00320 } 00321 /* stable entry? */ 00322 } else if (state >= ETHARP_STATE_STABLE) { 00323 #if ETHARP_SUPPORT_STATIC_ENTRIES 00324 /* don't record old_stable for static entries since they never expire */ 00325 if (state < ETHARP_STATE_STATIC) 00326 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00327 { 00328 /* remember entry with oldest stable entry in oldest, its age in maxtime */ 00329 if (arp_table[i].ctime >= age_stable) { 00330 old_stable = i; 00331 age_stable = arp_table[i].ctime; 00332 } 00333 } 00334 } 00335 } 00336 } 00337 /* { we have no match } => try to create a new entry */ 00338 00339 /* don't create new entry, only search? */ 00340 if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) || 00341 /* or no empty entry found and not allowed to recycle? */ 00342 ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) { 00343 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n")); 00344 return (s8_t)ERR_MEM; 00345 } 00346 00347 /* b) choose the least destructive entry to recycle: 00348 * 1) empty entry 00349 * 2) oldest stable entry 00350 * 3) oldest pending entry without queued packets 00351 * 4) oldest pending entry with queued packets 00352 * 00353 * { ETHARP_FLAG_TRY_HARD is set at this point } 00354 */ 00355 00356 /* 1) empty entry available? */ 00357 if (empty < ARP_TABLE_SIZE) { 00358 i = empty; 00359 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting empty entry %"U16_F"\n", (u16_t)i)); 00360 } else { 00361 /* 2) found recyclable stable entry? */ 00362 if (old_stable < ARP_TABLE_SIZE) { 00363 /* recycle oldest stable*/ 00364 i = old_stable; 00365 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i)); 00366 /* no queued packets should exist on stable entries */ 00367 LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL); 00368 /* 3) found recyclable pending entry without queued packets? */ 00369 } else if (old_pending < ARP_TABLE_SIZE) { 00370 /* recycle oldest pending */ 00371 i = old_pending; 00372 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i)); 00373 /* 4) found recyclable pending entry with queued packets? */ 00374 } else if (old_queue < ARP_TABLE_SIZE) { 00375 /* recycle oldest pending (queued packets are free in etharp_free_entry) */ 00376 i = old_queue; 00377 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))); 00378 /* no empty or recyclable entries found */ 00379 } else { 00380 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n")); 00381 return (s8_t)ERR_MEM; 00382 } 00383 00384 /* { empty or recyclable entry found } */ 00385 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); 00386 etharp_free_entry(i); 00387 } 00388 00389 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); 00390 LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY", 00391 arp_table[i].state == ETHARP_STATE_EMPTY); 00392 00393 /* IP address given? */ 00394 if (ipaddr != NULL) { 00395 /* set IP address */ 00396 ip4_addr_copy(arp_table[i].ipaddr, *ipaddr); 00397 } 00398 arp_table[i].ctime = 0; 00399 #if ETHARP_TABLE_MATCH_NETIF 00400 arp_table[i].netif = netif; 00401 #endif /* ETHARP_TABLE_MATCH_NETIF*/ 00402 return (err_t)i; 00403 } 00404 00405 /** 00406 * Update (or insert) a IP/MAC address pair in the ARP cache. 00407 * 00408 * If a pending entry is resolved, any queued packets will be sent 00409 * at this point. 00410 * 00411 * @param netif netif related to this entry (used for NETIF_ADDRHINT) 00412 * @param ipaddr IP address of the inserted ARP entry. 00413 * @param ethaddr Ethernet address of the inserted ARP entry. 00414 * @param flags See @ref etharp_state 00415 * 00416 * @return 00417 * - ERR_OK Successfully updated ARP cache. 00418 * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set. 00419 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache. 00420 * 00421 * @see pbuf_free() 00422 */ 00423 static err_t 00424 etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags) 00425 { 00426 s8_t i; 00427 LWIP_ASSERT("netif->hwaddr_len == ETH_HWADDR_LEN", netif->hwaddr_len == ETH_HWADDR_LEN); 00428 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", 00429 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr), 00430 (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2], 00431 (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5])); 00432 /* non-unicast address? */ 00433 if (ip4_addr_isany(ipaddr) || 00434 ip4_addr_isbroadcast(ipaddr, netif) || 00435 ip4_addr_ismulticast(ipaddr)) { 00436 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n")); 00437 return ERR_ARG; 00438 } 00439 /* find or create ARP entry */ 00440 i = etharp_find_entry(ipaddr, flags, netif); 00441 /* bail out if no entry could be found */ 00442 if (i < 0) { 00443 return (err_t)i; 00444 } 00445 00446 #if ETHARP_SUPPORT_STATIC_ENTRIES 00447 if (flags & ETHARP_FLAG_STATIC_ENTRY) { 00448 /* record static type */ 00449 arp_table[i].state = ETHARP_STATE_STATIC; 00450 } else if (arp_table[i].state == ETHARP_STATE_STATIC) { 00451 /* found entry is a static type, don't overwrite it */ 00452 return ERR_VAL; 00453 } else 00454 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ 00455 { 00456 /* mark it stable */ 00457 arp_table[i].state = ETHARP_STATE_STABLE; 00458 } 00459 00460 /* record network interface */ 00461 arp_table[i].netif = netif; 00462 /* insert in SNMP ARP index tree */ 00463 mib2_add_arp_entry(netif, &arp_table[i].ipaddr); 00464 00465 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i)); 00466 /* update address */ 00467 ETHADDR32_COPY(&arp_table[i].ethaddr, ethaddr); 00468 /* reset time stamp */ 00469 arp_table[i].ctime = 0; 00470 /* this is where we will send out queued packets! */ 00471 #if ARP_QUEUEING 00472 while (arp_table[i].q != NULL) { 00473 struct pbuf *p; 00474 /* remember remainder of queue */ 00475 struct etharp_q_entry *q = arp_table[i].q; 00476 /* pop first item off the queue */ 00477 arp_table[i].q = q->next; 00478 /* get the packet pointer */ 00479 p = q->p; 00480 /* now queue entry can be freed */ 00481 memp_free(MEMP_ARP_QUEUE, q); 00482 #else /* ARP_QUEUEING */ 00483 if (arp_table[i].q != NULL) { 00484 struct pbuf *p = arp_table[i].q; 00485 arp_table[i].q = NULL; 00486 #endif /* ARP_QUEUEING */ 00487 /* send the queued IP packet */ 00488 ethernet_output(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr, ETHTYPE_IP); 00489 /* free the queued IP packet */ 00490 pbuf_free(p); 00491 } 00492 return ERR_OK; 00493 } 00494 00495 #if ETHARP_SUPPORT_STATIC_ENTRIES 00496 /** Add a new static entry to the ARP table. If an entry exists for the 00497 * specified IP address, this entry is overwritten. 00498 * If packets are queued for the specified IP address, they are sent out. 00499 * 00500 * @param ipaddr IP address for the new static entry 00501 * @param ethaddr ethernet address for the new static entry 00502 * @return See return values of etharp_add_static_entry 00503 */ 00504 err_t 00505 etharp_add_static_entry(const ip4_addr_t *ipaddr, struct eth_addr *ethaddr) 00506 { 00507 struct netif *netif; 00508 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", 00509 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr), 00510 (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2], 00511 (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5])); 00512 00513 netif = ip4_route(ipaddr); 00514 if (netif == NULL) { 00515 return ERR_RTE; 00516 } 00517 00518 return etharp_update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY); 00519 } 00520 00521 /** Remove a static entry from the ARP table previously added with a call to 00522 * etharp_add_static_entry. 00523 * 00524 * @param ipaddr IP address of the static entry to remove 00525 * @return ERR_OK: entry removed 00526 * ERR_MEM: entry wasn't found 00527 * ERR_ARG: entry wasn't a static entry but a dynamic one 00528 */ 00529 err_t 00530 etharp_remove_static_entry(const ip4_addr_t *ipaddr) 00531 { 00532 s8_t i; 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 u8_t 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 s8_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 s8_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 u8_t 00612 etharp_get_entry(u8_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_ERROR("netif != NULL", (netif != NULL), return;); 00649 00650 hdr = (struct etharp_hdr *)p->payload; 00651 00652 /* RFC 826 "Packet Reception": */ 00653 if ((hdr->hwtype != PP_HTONS(HWTYPE_ETHERNET)) || 00654 (hdr->hwlen != ETH_HWADDR_LEN) || 00655 (hdr->protolen != sizeof(ip4_addr_t)) || 00656 (hdr->proto != PP_HTONS(ETHTYPE_IP))) { 00657 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, 00658 ("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n", 00659 hdr->hwtype, (u16_t)hdr->hwlen, hdr->proto, (u16_t)hdr->protolen)); 00660 ETHARP_STATS_INC(etharp.proterr); 00661 ETHARP_STATS_INC(etharp.drop); 00662 pbuf_free(p); 00663 return; 00664 } 00665 ETHARP_STATS_INC(etharp.recv); 00666 00667 #if LWIP_AUTOIP 00668 /* We have to check if a host already has configured our random 00669 * created link local address and continuously check if there is 00670 * a host with this IP-address so we can detect collisions */ 00671 autoip_arp_reply(netif, hdr); 00672 #endif /* LWIP_AUTOIP */ 00673 00674 /* Copy struct ip4_addr2 to aligned ip4_addr, to support compilers without 00675 * structure packing (not using structure copy which breaks strict-aliasing rules). */ 00676 IPADDR2_COPY(&sipaddr, &hdr->sipaddr); 00677 IPADDR2_COPY(&dipaddr, &hdr->dipaddr); 00678 00679 /* this interface is not configured? */ 00680 if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { 00681 for_us = 0; 00682 } else { 00683 /* ARP packet directed to us? */ 00684 for_us = (u8_t)ip4_addr_cmp(&dipaddr, netif_ip4_addr(netif)); 00685 } 00686 00687 /* ARP message directed to us? 00688 -> add IP address in ARP cache; assume requester wants to talk to us, 00689 can result in directly sending the queued packets for this host. 00690 ARP message not directed to us? 00691 -> update the source IP address in the cache, if present */ 00692 etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), 00693 for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY); 00694 00695 /* now act on the message itself */ 00696 switch (hdr->opcode) { 00697 /* ARP request? */ 00698 case PP_HTONS(ARP_REQUEST): 00699 /* ARP request. If it asked for our address, we send out a 00700 * reply. In any case, we time-stamp any existing ARP entry, 00701 * and possibly send out an IP packet that was queued on it. */ 00702 00703 LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP request\n")); 00704 /* ARP request for our address? */ 00705 if (for_us) { 00706 /* send ARP response */ 00707 etharp_raw(netif, 00708 (struct eth_addr *)netif->hwaddr, &hdr->shwaddr, 00709 (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), 00710 &hdr->shwaddr, &sipaddr, 00711 ARP_REPLY); 00712 /* we are not configured? */ 00713 } else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { 00714 /* { for_us == 0 and netif->ip_addr.addr == 0 } */ 00715 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: we are unconfigured, ARP request ignored.\n")); 00716 /* request was not directed to us */ 00717 } else { 00718 /* { for_us == 0 and netif->ip_addr.addr != 0 } */ 00719 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP request was not for us.\n")); 00720 } 00721 break; 00722 case PP_HTONS(ARP_REPLY): 00723 /* ARP reply. We already updated the ARP cache earlier. */ 00724 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP reply\n")); 00725 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK) 00726 /* DHCP wants to know about ARP replies from any host with an 00727 * IP address also offered to us by the DHCP server. We do not 00728 * want to take a duplicate IP address on a single network. 00729 * @todo How should we handle redundant (fail-over) interfaces? */ 00730 dhcp_arp_reply(netif, &sipaddr); 00731 #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */ 00732 break; 00733 default: 00734 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP unknown opcode type %"S16_F"\n", lwip_htons(hdr->opcode))); 00735 ETHARP_STATS_INC(etharp.err); 00736 break; 00737 } 00738 /* free ARP packet */ 00739 pbuf_free(p); 00740 } 00741 00742 /** Just a small helper function that sends a pbuf to an ethernet address 00743 * in the arp_table specified by the index 'arp_idx'. 00744 */ 00745 static err_t 00746 etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, u8_t arp_idx) 00747 { 00748 LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE", 00749 arp_table[arp_idx].state >= ETHARP_STATE_STABLE); 00750 /* if arp table entry is about to expire: re-request it, 00751 but only if its state is ETHARP_STATE_STABLE to prevent flooding the 00752 network with ARP requests if this address is used frequently. */ 00753 if (arp_table[arp_idx].state == ETHARP_STATE_STABLE) { 00754 if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_BROADCAST) { 00755 /* issue a standard request using broadcast */ 00756 if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) { 00757 arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1; 00758 } 00759 } else if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_UNICAST) { 00760 /* issue a unicast request (for 15 seconds) to prevent unnecessary broadcast */ 00761 if (etharp_request_dst(netif, &arp_table[arp_idx].ipaddr, &arp_table[arp_idx].ethaddr) == ERR_OK) { 00762 arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1; 00763 } 00764 } 00765 } 00766 00767 return ethernet_output(netif, q, (struct eth_addr*)(netif->hwaddr), &arp_table[arp_idx].ethaddr, ETHTYPE_IP); 00768 } 00769 00770 /** 00771 * Resolve and fill-in Ethernet address header for outgoing IP packet. 00772 * 00773 * For IP multicast and broadcast, corresponding Ethernet addresses 00774 * are selected and the packet is transmitted on the link. 00775 * 00776 * For unicast addresses, the packet is submitted to etharp_query(). In 00777 * case the IP address is outside the local network, the IP address of 00778 * the gateway is used. 00779 * 00780 * @param netif The lwIP network interface which the IP packet will be sent on. 00781 * @param q The pbuf(s) containing the IP packet to be sent. 00782 * @param ipaddr The IP address of the packet destination. 00783 * 00784 * @return 00785 * - ERR_RTE No route to destination (no gateway to external networks), 00786 * or the return type of either etharp_query() or ethernet_output(). 00787 */ 00788 err_t 00789 etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) 00790 { 00791 const struct eth_addr *dest; 00792 struct eth_addr mcastaddr; 00793 const ip4_addr_t *dst_addr = ipaddr; 00794 00795 LWIP_ASSERT("netif != NULL", netif != NULL); 00796 LWIP_ASSERT("q != NULL", q != NULL); 00797 LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL); 00798 00799 /* Determine on destination hardware address. Broadcasts and multicasts 00800 * are special, other IP addresses are looked up in the ARP table. */ 00801 00802 /* broadcast destination IP address? */ 00803 if (ip4_addr_isbroadcast(ipaddr, netif)) { 00804 /* broadcast on Ethernet also */ 00805 dest = (const struct eth_addr *)ðbroadcast; 00806 /* multicast destination IP address? */ 00807 } else if (ip4_addr_ismulticast(ipaddr)) { 00808 /* Hash IP multicast address to MAC address.*/ 00809 mcastaddr.addr[0] = LL_IP4_MULTICAST_ADDR_0; 00810 mcastaddr.addr[1] = LL_IP4_MULTICAST_ADDR_1; 00811 mcastaddr.addr[2] = LL_IP4_MULTICAST_ADDR_2; 00812 mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f; 00813 mcastaddr.addr[4] = ip4_addr3(ipaddr); 00814 mcastaddr.addr[5] = ip4_addr4(ipaddr); 00815 /* destination Ethernet address is multicast */ 00816 dest = &mcastaddr; 00817 /* unicast destination IP address? */ 00818 } else { 00819 s8_t i; 00820 /* outside local network? if so, this can neither be a global broadcast nor 00821 a subnet broadcast. */ 00822 if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) && 00823 !ip4_addr_islinklocal(ipaddr)) { 00824 #if LWIP_AUTOIP 00825 struct ip_hdr *iphdr = LWIP_ALIGNMENT_CAST(struct ip_hdr*, q->payload); 00826 /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with 00827 a link-local source address must always be "directly to its destination 00828 on the same physical link. The host MUST NOT send the packet to any 00829 router for forwarding". */ 00830 if (!ip4_addr_islinklocal(&iphdr->src)) 00831 #endif /* LWIP_AUTOIP */ 00832 { 00833 #ifdef LWIP_HOOK_ETHARP_GET_GW 00834 /* For advanced routing, a single default gateway might not be enough, so get 00835 the IP address of the gateway to handle the current destination address. */ 00836 dst_addr = LWIP_HOOK_ETHARP_GET_GW(netif, ipaddr); 00837 if (dst_addr == NULL) 00838 #endif /* LWIP_HOOK_ETHARP_GET_GW */ 00839 { 00840 /* interface has default gateway? */ 00841 if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) { 00842 /* send to hardware address of default gateway IP address */ 00843 dst_addr = netif_ip4_gw(netif); 00844 /* no default gateway available */ 00845 } else { 00846 /* no route to destination error (default gateway missing) */ 00847 return ERR_RTE; 00848 } 00849 } 00850 } 00851 } 00852 #if LWIP_NETIF_HWADDRHINT 00853 if (netif->addr_hint != NULL) { 00854 /* per-pcb cached entry was given */ 00855 u8_t etharp_cached_entry = *(netif->addr_hint); 00856 if (etharp_cached_entry < ARP_TABLE_SIZE) { 00857 #endif /* LWIP_NETIF_HWADDRHINT */ 00858 if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) && 00859 #if ETHARP_TABLE_MATCH_NETIF 00860 (arp_table[etharp_cached_entry].netif == netif) && 00861 #endif 00862 (ip4_addr_cmp(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) { 00863 /* the per-pcb-cached entry is stable and the right one! */ 00864 ETHARP_STATS_INC(etharp.cachehit); 00865 return etharp_output_to_arp_index(netif, q, etharp_cached_entry); 00866 } 00867 #if LWIP_NETIF_HWADDRHINT 00868 } 00869 } 00870 #endif /* LWIP_NETIF_HWADDRHINT */ 00871 00872 /* find stable entry: do this here since this is a critical path for 00873 throughput and etharp_find_entry() is kind of slow */ 00874 for (i = 0; i < ARP_TABLE_SIZE; i++) { 00875 if ((arp_table[i].state >= ETHARP_STATE_STABLE) && 00876 #if ETHARP_TABLE_MATCH_NETIF 00877 (arp_table[i].netif == netif) && 00878 #endif 00879 (ip4_addr_cmp(dst_addr, &arp_table[i].ipaddr))) { 00880 /* found an existing, stable entry */ 00881 ETHARP_SET_HINT(netif, i); 00882 return etharp_output_to_arp_index(netif, q, i); 00883 } 00884 } 00885 /* no stable entry found, use the (slower) query function: 00886 queue on destination Ethernet address belonging to ipaddr */ 00887 return etharp_query(netif, dst_addr, q); 00888 } 00889 00890 /* continuation for multicast/broadcast destinations */ 00891 /* obtain source Ethernet address of the given interface */ 00892 /* send packet directly on the link */ 00893 return ethernet_output(netif, q, (struct eth_addr*)(netif->hwaddr), dest, ETHTYPE_IP); 00894 } 00895 00896 /** 00897 * Send an ARP request for the given IP address and/or queue a packet. 00898 * 00899 * If the IP address was not yet in the cache, a pending ARP cache entry 00900 * is added and an ARP request is sent for the given address. The packet 00901 * is queued on this entry. 00902 * 00903 * If the IP address was already pending in the cache, a new ARP request 00904 * is sent for the given address. The packet is queued on this entry. 00905 * 00906 * If the IP address was already stable in the cache, and a packet is 00907 * given, it is directly sent and no ARP request is sent out. 00908 * 00909 * If the IP address was already stable in the cache, and no packet is 00910 * given, an ARP request is sent out. 00911 * 00912 * @param netif The lwIP network interface on which ipaddr 00913 * must be queried for. 00914 * @param ipaddr The IP address to be resolved. 00915 * @param q If non-NULL, a pbuf that must be delivered to the IP address. 00916 * q is not freed by this function. 00917 * 00918 * @note q must only be ONE packet, not a packet queue! 00919 * 00920 * @return 00921 * - ERR_BUF Could not make room for Ethernet header. 00922 * - ERR_MEM Hardware address unknown, and no more ARP entries available 00923 * to query for address or queue the packet. 00924 * - ERR_MEM Could not queue packet due to memory shortage. 00925 * - ERR_RTE No route to destination (no gateway to external networks). 00926 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache. 00927 * 00928 */ 00929 err_t 00930 etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q) 00931 { 00932 struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr; 00933 err_t result = ERR_MEM; 00934 int is_new_entry = 0; 00935 s8_t i; /* ARP entry index */ 00936 00937 /* non-unicast address? */ 00938 if (ip4_addr_isbroadcast(ipaddr, netif) || 00939 ip4_addr_ismulticast(ipaddr) || 00940 ip4_addr_isany(ipaddr)) { 00941 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n")); 00942 return ERR_ARG; 00943 } 00944 00945 /* find entry in ARP cache, ask to create entry if queueing packet */ 00946 i = etharp_find_entry(ipaddr, ETHARP_FLAG_TRY_HARD, netif); 00947 00948 /* could not find or create entry? */ 00949 if (i < 0) { 00950 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n")); 00951 if (q) { 00952 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n")); 00953 ETHARP_STATS_INC(etharp.memerr); 00954 } 00955 return (err_t)i; 00956 } 00957 00958 /* mark a fresh entry as pending (we just sent a request) */ 00959 if (arp_table[i].state == ETHARP_STATE_EMPTY) { 00960 is_new_entry = 1; 00961 arp_table[i].state = ETHARP_STATE_PENDING; 00962 /* record network interface for re-sending arp request in etharp_tmr */ 00963 arp_table[i].netif = netif; 00964 } 00965 00966 /* { i is either a STABLE or (new or existing) PENDING entry } */ 00967 LWIP_ASSERT("arp_table[i].state == PENDING or STABLE", 00968 ((arp_table[i].state == ETHARP_STATE_PENDING) || 00969 (arp_table[i].state >= ETHARP_STATE_STABLE))); 00970 00971 /* do we have a new entry? or an implicit query request? */ 00972 if (is_new_entry || (q == NULL)) { 00973 /* try to resolve it; send out ARP request */ 00974 result = etharp_request(netif, ipaddr); 00975 if (result != ERR_OK) { 00976 /* ARP request couldn't be sent */ 00977 /* We don't re-send arp request in etharp_tmr, but we still queue packets, 00978 since this failure could be temporary, and the next packet calling 00979 etharp_query again could lead to sending the queued packets. */ 00980 } 00981 if (q == NULL) { 00982 return result; 00983 } 00984 } 00985 00986 /* packet given? */ 00987 LWIP_ASSERT("q != NULL", q != NULL); 00988 /* stable entry? */ 00989 if (arp_table[i].state >= ETHARP_STATE_STABLE) { 00990 /* we have a valid IP->Ethernet address mapping */ 00991 ETHARP_SET_HINT(netif, i); 00992 /* send the packet */ 00993 result = ethernet_output(netif, q, srcaddr, &(arp_table[i].ethaddr), ETHTYPE_IP); 00994 /* pending entry? (either just created or already pending */ 00995 } else if (arp_table[i].state == ETHARP_STATE_PENDING) { 00996 /* entry is still pending, queue the given packet 'q' */ 00997 struct pbuf *p; 00998 int copy_needed = 0; 00999 /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but 01000 * to copy the whole queue into a new PBUF_RAM (see bug #11400) 01001 * PBUF_ROMs can be left as they are, since ROM must not get changed. */ 01002 p = q; 01003 while (p) { 01004 LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0)); 01005 if (p->type != PBUF_ROM) { 01006 copy_needed = 1; 01007 break; 01008 } 01009 p = p->next; 01010 } 01011 if (copy_needed) { 01012 /* copy the whole packet into new pbufs */ 01013 p = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM); 01014 if (p != NULL) { 01015 if (pbuf_copy(p, q) != ERR_OK) { 01016 pbuf_free(p); 01017 p = NULL; 01018 } 01019 } 01020 } else { 01021 /* referencing the old pbuf is enough */ 01022 p = q; 01023 pbuf_ref(p); 01024 } 01025 /* packet could be taken over? */ 01026 if (p != NULL) { 01027 /* queue packet ... */ 01028 #if ARP_QUEUEING 01029 struct etharp_q_entry *new_entry; 01030 /* allocate a new arp queue entry */ 01031 new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE); 01032 if (new_entry != NULL) { 01033 unsigned int qlen = 0; 01034 new_entry->next = 0; 01035 new_entry->p = p; 01036 if (arp_table[i].q != NULL) { 01037 /* queue was already existent, append the new entry to the end */ 01038 struct etharp_q_entry *r; 01039 r = arp_table[i].q; 01040 qlen++; 01041 while (r->next != NULL) { 01042 r = r->next; 01043 qlen++; 01044 } 01045 r->next = new_entry; 01046 } else { 01047 /* queue did not exist, first item in queue */ 01048 arp_table[i].q = new_entry; 01049 } 01050 #if ARP_QUEUE_LEN 01051 if (qlen >= ARP_QUEUE_LEN) { 01052 struct etharp_q_entry *old; 01053 old = arp_table[i].q; 01054 arp_table[i].q = arp_table[i].q->next; 01055 pbuf_free(old->p); 01056 memp_free(MEMP_ARP_QUEUE, old); 01057 } 01058 #endif 01059 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i)); 01060 result = ERR_OK; 01061 } else { 01062 /* the pool MEMP_ARP_QUEUE is empty */ 01063 pbuf_free(p); 01064 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)); 01065 result = ERR_MEM; 01066 } 01067 #else /* ARP_QUEUEING */ 01068 /* always queue one packet per ARP request only, freeing a previously queued packet */ 01069 if (arp_table[i].q != NULL) { 01070 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)); 01071 pbuf_free(arp_table[i].q); 01072 } 01073 arp_table[i].q = p; 01074 result = ERR_OK; 01075 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i)); 01076 #endif /* ARP_QUEUEING */ 01077 } else { 01078 ETHARP_STATS_INC(etharp.memerr); 01079 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)); 01080 result = ERR_MEM; 01081 } 01082 } 01083 return result; 01084 } 01085 01086 /** 01087 * Send a raw ARP packet (opcode and all addresses can be modified) 01088 * 01089 * @param netif the lwip network interface on which to send the ARP packet 01090 * @param ethsrc_addr the source MAC address for the ethernet header 01091 * @param ethdst_addr the destination MAC address for the ethernet header 01092 * @param hwsrc_addr the source MAC address for the ARP protocol header 01093 * @param ipsrc_addr the source IP address for the ARP protocol header 01094 * @param hwdst_addr the destination MAC address for the ARP protocol header 01095 * @param ipdst_addr the destination IP address for the ARP protocol header 01096 * @param opcode the type of the ARP packet 01097 * @return ERR_OK if the ARP packet has been sent 01098 * ERR_MEM if the ARP packet couldn't be allocated 01099 * any other err_t on failure 01100 */ 01101 static err_t 01102 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr, 01103 const struct eth_addr *ethdst_addr, 01104 const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr, 01105 const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr, 01106 const u16_t opcode) 01107 { 01108 struct pbuf *p; 01109 err_t result = ERR_OK; 01110 struct etharp_hdr *hdr; 01111 01112 LWIP_ASSERT("netif != NULL", netif != NULL); 01113 01114 /* allocate a pbuf for the outgoing ARP request packet */ 01115 p = pbuf_alloc(PBUF_LINK, SIZEOF_ETHARP_HDR, PBUF_RAM); 01116 /* could allocate a pbuf for an ARP request? */ 01117 if (p == NULL) { 01118 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, 01119 ("etharp_raw: could not allocate pbuf for ARP request.\n")); 01120 ETHARP_STATS_INC(etharp.memerr); 01121 return ERR_MEM; 01122 } 01123 LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr", 01124 (p->len >= SIZEOF_ETHARP_HDR)); 01125 01126 hdr = (struct etharp_hdr *)p->payload; 01127 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n")); 01128 hdr->opcode = lwip_htons(opcode); 01129 01130 LWIP_ASSERT("netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!", 01131 (netif->hwaddr_len == ETH_HWADDR_LEN)); 01132 01133 /* Write the ARP MAC-Addresses */ 01134 ETHADDR16_COPY(&hdr->shwaddr, hwsrc_addr); 01135 ETHADDR16_COPY(&hdr->dhwaddr, hwdst_addr); 01136 /* Copy struct ip4_addr2 to aligned ip4_addr, to support compilers without 01137 * structure packing. */ 01138 IPADDR2_COPY(&hdr->sipaddr, ipsrc_addr); 01139 IPADDR2_COPY(&hdr->dipaddr, ipdst_addr); 01140 01141 hdr->hwtype = PP_HTONS(HWTYPE_ETHERNET); 01142 hdr->proto = PP_HTONS(ETHTYPE_IP); 01143 /* set hwlen and protolen */ 01144 hdr->hwlen = ETH_HWADDR_LEN; 01145 hdr->protolen = sizeof(ip4_addr_t); 01146 01147 /* send ARP query */ 01148 #if LWIP_AUTOIP 01149 /* If we are using Link-Local, all ARP packets that contain a Link-Local 01150 * 'sender IP address' MUST be sent using link-layer broadcast instead of 01151 * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */ 01152 if(ip4_addr_islinklocal(ipsrc_addr)) { 01153 ethernet_output(netif, p, ethsrc_addr, ðbroadcast, ETHTYPE_ARP); 01154 } else 01155 #endif /* LWIP_AUTOIP */ 01156 { 01157 ethernet_output(netif, p, ethsrc_addr, ethdst_addr, ETHTYPE_ARP); 01158 } 01159 01160 ETHARP_STATS_INC(etharp.xmit); 01161 /* free ARP query packet */ 01162 pbuf_free(p); 01163 p = NULL; 01164 /* could not allocate pbuf for ARP request */ 01165 01166 return result; 01167 } 01168 01169 /** 01170 * Send an ARP request packet asking for ipaddr to a specific eth address. 01171 * Used to send unicast request to refresh the ARP table just before an entry 01172 * times out 01173 * 01174 * @param netif the lwip network interface on which to send the request 01175 * @param ipaddr the IP address for which to ask 01176 * @param hw_dst_addr the ethernet address to send this packet to 01177 * @return ERR_OK if the request has been sent 01178 * ERR_MEM if the ARP packet couldn't be allocated 01179 * any other err_t on failure 01180 */ 01181 static err_t 01182 etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr* hw_dst_addr) 01183 { 01184 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, hw_dst_addr, 01185 (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), ðzero, 01186 ipaddr, ARP_REQUEST); 01187 } 01188 01189 /** 01190 * Send an ARP request packet asking for ipaddr. 01191 * 01192 * @param netif the lwip network interface on which to send the request 01193 * @param ipaddr the IP address for which to ask 01194 * @return ERR_OK if the request has been sent 01195 * ERR_MEM if the ARP packet couldn't be allocated 01196 * any other err_t on failure 01197 */ 01198 err_t 01199 etharp_request(struct netif *netif, const ip4_addr_t *ipaddr) 01200 { 01201 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n")); 01202 return etharp_request_dst(netif, ipaddr, ðbroadcast); 01203 } 01204 #endif /* LWIP_IPV4 && LWIP_ARP */ 01205 01206 #endif /* LWIP_ARP || LWIP_ETHERNET */
Generated on Sun Jul 17 2022 08:25:24 by 1.7.2