etharp.c 44 KB

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