etharp.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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 || LWIP_ETHERNET
  46. #include "lwip/ip_addr.h"
  47. #include "lwip/def.h"
  48. #include "lwip/ip.h"
  49. #include "lwip/stats.h"
  50. #include "lwip/snmp.h"
  51. #include "lwip/dhcp.h"
  52. #include "lwip/autoip.h"
  53. #include "netif/etharp.h"
  54. #if PPPOE_SUPPORT
  55. #include "netif/ppp_oe.h"
  56. #endif /* PPPOE_SUPPORT */
  57. #include <string.h>
  58. #ifdef MEMLEAK_DEBUG
  59. static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
  60. #endif
  61. const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
  62. const struct eth_addr ethzero = {{0,0,0,0,0,0}};
  63. #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */
  64. /** the time an ARP entry stays valid after its last update,
  65. * for ARP_TMR_INTERVAL = 5000, this is
  66. * (240 * 5) seconds = 20 minutes.
  67. */
  68. #define ARP_MAXAGE 240
  69. /** Re-request a used ARP entry 1 minute before it would expire to prevent
  70. * breaking a steadily used connection because the ARP entry timed out. */
  71. #define ARP_AGE_REREQUEST_USED (ARP_MAXAGE - 12)
  72. /** the time an ARP entry stays pending after first request,
  73. * for ARP_TMR_INTERVAL = 5000, this is
  74. * (2 * 5) seconds = 10 seconds.
  75. *
  76. * @internal Keep this number at least 2, otherwise it might
  77. * run out instantly if the timeout occurs directly after a request.
  78. */
  79. #define ARP_MAXPENDING 2
  80. #define HWTYPE_ETHERNET 1
  81. enum etharp_state {
  82. ETHARP_STATE_EMPTY = 0,
  83. ETHARP_STATE_PENDING,
  84. ETHARP_STATE_STABLE,
  85. ETHARP_STATE_STABLE_REREQUESTING
  86. };
  87. struct etharp_entry {
  88. #if ARP_QUEUEING
  89. /** Pointer to queue of pending outgoing packets on this ARP entry. */
  90. struct etharp_q_entry *q;
  91. #else /* ARP_QUEUEING */
  92. /** Pointer to a single pending outgoing packet on this ARP entry. */
  93. struct pbuf *q;
  94. #endif /* ARP_QUEUEING */
  95. ip_addr_t ipaddr;
  96. struct eth_addr ethaddr;
  97. #if LWIP_SNMP || LWIP_ARP
  98. struct netif *netif;
  99. #endif /* LWIP_SNMP */
  100. u8_t state;
  101. u8_t ctime;
  102. #if ETHARP_SUPPORT_STATIC_ENTRIES
  103. u8_t static_entry;
  104. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  105. };
  106. static struct etharp_entry arp_table[ARP_TABLE_SIZE];
  107. #if !LWIP_NETIF_HWADDRHINT
  108. static u8_t etharp_cached_entry;
  109. #endif /* !LWIP_NETIF_HWADDRHINT */
  110. /** Try hard to create a new entry - we want the IP address to appear in
  111. the cache (even if this means removing an active entry or so). */
  112. #define ETHARP_FLAG_TRY_HARD 1
  113. #define ETHARP_FLAG_FIND_ONLY 2
  114. #define ETHARP_FLAG_STATIC_ENTRY 4
  115. #if LWIP_NETIF_HWADDRHINT
  116. #define ETHARP_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \
  117. *((netif)->addr_hint) = (hint);
  118. #else /* LWIP_NETIF_HWADDRHINT */
  119. #define ETHARP_SET_HINT(netif, hint) (etharp_cached_entry = (hint))
  120. #endif /* LWIP_NETIF_HWADDRHINT */
  121. static err_t update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags);
  122. /* Some checks, instead of etharp_init(): */
  123. #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f))
  124. #error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h"
  125. #endif
  126. #if ARP_QUEUEING
  127. /**
  128. * Free a complete queue of etharp entries
  129. *
  130. * @param q a qeueue of etharp_q_entry's to free
  131. */
  132. static void
  133. free_etharp_q(struct etharp_q_entry *q)
  134. {
  135. struct etharp_q_entry *r;
  136. LWIP_ASSERT("q != NULL", q != NULL);
  137. LWIP_ASSERT("q->p != NULL", q->p != NULL);
  138. while (q) {
  139. r = q;
  140. q = q->next;
  141. LWIP_ASSERT("r->p != NULL", (r->p != NULL));
  142. pbuf_free(r->p);
  143. memp_free(MEMP_ARP_QUEUE, r);
  144. }
  145. }
  146. #else /* ARP_QUEUEING */
  147. /** Compatibility define: free the queued pbuf */
  148. #define free_etharp_q(q) pbuf_free(q)
  149. #endif /* ARP_QUEUEING */
  150. /** Clean up ARP table entries */
  151. static void ICACHE_FLASH_ATTR
  152. free_entry(int i)
  153. {
  154. /* remove from SNMP ARP index tree */
  155. snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr);
  156. /* and empty packet queue */
  157. if (arp_table[i].q != NULL) {
  158. /* remove all queued packets */
  159. LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
  160. free_etharp_q(arp_table[i].q);
  161. arp_table[i].q = NULL;
  162. }
  163. /* recycle entry for re-use */
  164. arp_table[i].state = ETHARP_STATE_EMPTY;
  165. #if ETHARP_SUPPORT_STATIC_ENTRIES
  166. arp_table[i].static_entry = 0;
  167. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  168. #ifdef LWIP_DEBUG
  169. /* for debugging, clean out the complete entry */
  170. arp_table[i].ctime = 0;
  171. #if LWIP_SNMP
  172. arp_table[i].netif = NULL;
  173. #endif /* LWIP_SNMP */
  174. ip_addr_set_zero(&arp_table[i].ipaddr);
  175. arp_table[i].ethaddr = ethzero;
  176. #endif /* LWIP_DEBUG */
  177. }
  178. /**
  179. * Clears expired entries in the ARP table.
  180. *
  181. * This function should be called every ETHARP_TMR_INTERVAL milliseconds (5 seconds),
  182. * in order to expire entries in the ARP table.
  183. */
  184. void
  185. etharp_tmr(void)
  186. {
  187. u8_t i;
  188. LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
  189. /* remove expired entries from the ARP table */
  190. for (i = 0; i < ARP_TABLE_SIZE; ++i) {
  191. u8_t state = arp_table[i].state;
  192. if (state != ETHARP_STATE_EMPTY
  193. #if ETHARP_SUPPORT_STATIC_ENTRIES
  194. && (arp_table[i].static_entry == 0)
  195. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  196. ) {
  197. arp_table[i].ctime++;
  198. if ((arp_table[i].ctime >= ARP_MAXAGE) ||
  199. ((arp_table[i].state == ETHARP_STATE_PENDING) &&
  200. (arp_table[i].ctime >= ARP_MAXPENDING))) {
  201. /* pending or stable entry has become old! */
  202. LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n",
  203. arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i));
  204. /* clean up entries that have just been expired */
  205. free_entry(i);
  206. }
  207. else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING) {
  208. /* Reset state to stable, so that the next transmitted packet will
  209. re-send an ARP request. */
  210. arp_table[i].state = ETHARP_STATE_STABLE;
  211. }
  212. #if ARP_QUEUEING
  213. /* still pending entry? (not expired) */
  214. if (arp_table[i].state == ETHARP_STATE_PENDING) {
  215. /* resend an ARP query here? */
  216. }
  217. #endif /* ARP_QUEUEING */
  218. }
  219. }
  220. }
  221. /**
  222. * Search the ARP table for a matching or new entry.
  223. *
  224. * If an IP address is given, return a pending or stable ARP entry that matches
  225. * the address. If no match is found, create a new entry with this address set,
  226. * but in state ETHARP_EMPTY. The caller must check and possibly change the
  227. * state of the returned entry.
  228. *
  229. * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
  230. *
  231. * In all cases, attempt to create new entries from an empty entry. If no
  232. * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle
  233. * old entries. Heuristic choose the least important entry for recycling.
  234. *
  235. * @param ipaddr IP address to find in ARP cache, or to add if not found.
  236. * @param flags @see definition of ETHARP_FLAG_*
  237. * @param netif netif related to this address (used for NETIF_HWADDRHINT)
  238. *
  239. * @return The ARP entry index that matched or is created, ERR_MEM if no
  240. * entry is found or could be recycled.
  241. */
  242. static s8_t ICACHE_FLASH_ATTR
  243. find_entry(ip_addr_t *ipaddr, u8_t flags)
  244. {
  245. s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
  246. s8_t empty = ARP_TABLE_SIZE;
  247. u8_t i = 0, age_pending = 0, age_stable = 0;
  248. /* oldest entry with packets on queue */
  249. s8_t old_queue = ARP_TABLE_SIZE;
  250. /* its age */
  251. u8_t age_queue = 0;
  252. /**
  253. * a) do a search through the cache, remember candidates
  254. * b) select candidate entry
  255. * c) create new entry
  256. */
  257. /* a) in a single search sweep, do all of this
  258. * 1) remember the first empty entry (if any)
  259. * 2) remember the oldest stable entry (if any)
  260. * 3) remember the oldest pending entry without queued packets (if any)
  261. * 4) remember the oldest pending entry with queued packets (if any)
  262. * 5) search for a matching IP entry, either pending or stable
  263. * until 5 matches, or all entries are searched for.
  264. */
  265. for (i = 0; i < ARP_TABLE_SIZE; ++i) {
  266. u8_t state = arp_table[i].state;
  267. /* no empty entry found yet and now we do find one? */
  268. if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) {
  269. LWIP_DEBUGF(ETHARP_DEBUG, ("find_entry: found empty entry %"U16_F"\n", (u16_t)i));
  270. /* remember first empty entry */
  271. empty = i;
  272. } else if (state != ETHARP_STATE_EMPTY) {
  273. LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE",
  274. state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE);
  275. /* if given, does IP address match IP address in ARP entry? */
  276. if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
  277. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching entry %"U16_F"\n", (u16_t)i));
  278. /* found exact IP address match, simply bail out */
  279. return i;
  280. }
  281. /* pending entry? */
  282. if (state == ETHARP_STATE_PENDING) {
  283. /* pending with queued packets? */
  284. if (arp_table[i].q != NULL) {
  285. if (arp_table[i].ctime >= age_queue) {
  286. old_queue = i;
  287. age_queue = arp_table[i].ctime;
  288. }
  289. } else
  290. /* pending without queued packets? */
  291. {
  292. if (arp_table[i].ctime >= age_pending) {
  293. old_pending = i;
  294. age_pending = arp_table[i].ctime;
  295. }
  296. }
  297. /* stable entry? */
  298. } else if (state >= ETHARP_STATE_STABLE) {
  299. #if ETHARP_SUPPORT_STATIC_ENTRIES
  300. /* don't record old_stable for static entries since they never expire */
  301. if (arp_table[i].static_entry == 0)
  302. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  303. {
  304. /* remember entry with oldest stable entry in oldest, its age in maxtime */
  305. if (arp_table[i].ctime >= age_stable) {
  306. old_stable = i;
  307. age_stable = arp_table[i].ctime;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. /* { we have no match } => try to create a new entry */
  314. /* don't create new entry, only search? */
  315. if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) ||
  316. /* or no empty entry found and not allowed to recycle? */
  317. ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) {
  318. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty entry found and not allowed to recycle\n"));
  319. return (s8_t)ERR_MEM;
  320. }
  321. /* b) choose the least destructive entry to recycle:
  322. * 1) empty entry
  323. * 2) oldest stable entry
  324. * 3) oldest pending entry without queued packets
  325. * 4) oldest pending entry with queued packets
  326. *
  327. * { ETHARP_FLAG_TRY_HARD is set at this point }
  328. */
  329. /* 1) empty entry available? */
  330. if (empty < ARP_TABLE_SIZE) {
  331. i = empty;
  332. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
  333. } else {
  334. /* 2) found recyclable stable entry? */
  335. if (old_stable < ARP_TABLE_SIZE) {
  336. /* recycle oldest stable*/
  337. i = old_stable;
  338. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i));
  339. /* no queued packets should exist on stable entries */
  340. LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
  341. /* 3) found recyclable pending entry without queued packets? */
  342. } else if (old_pending < ARP_TABLE_SIZE) {
  343. /* recycle oldest pending */
  344. i = old_pending;
  345. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i));
  346. /* 4) found recyclable pending entry with queued packets? */
  347. } else if (old_queue < ARP_TABLE_SIZE) {
  348. /* recycle oldest pending (queued packets are free in free_entry) */
  349. i = old_queue;
  350. 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)));
  351. /* no empty or recyclable entries found */
  352. } else {
  353. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty or recyclable entries found\n"));
  354. return (s8_t)ERR_MEM;
  355. }
  356. /* { empty or recyclable entry found } */
  357. LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
  358. free_entry(i);
  359. }
  360. LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
  361. LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
  362. arp_table[i].state == ETHARP_STATE_EMPTY);
  363. /* IP address given? */
  364. if (ipaddr != NULL) {
  365. /* set IP address */
  366. ip_addr_copy(arp_table[i].ipaddr, *ipaddr);
  367. }
  368. arp_table[i].ctime = 0;
  369. #if ETHARP_SUPPORT_STATIC_ENTRIES
  370. arp_table[i].static_entry = 0;
  371. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  372. return (err_t)i;
  373. }
  374. /**
  375. * Send an IP packet on the network using netif->linkoutput
  376. * The ethernet header is filled in before sending.
  377. *
  378. * @params netif the lwIP network interface on which to send the packet
  379. * @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
  380. * @params src the source MAC address to be copied into the ethernet header
  381. * @params dst the destination MAC address to be copied into the ethernet header
  382. * @return ERR_OK if the packet was sent, any other err_t on failure
  383. */
  384. static err_t ICACHE_FLASH_ATTR
  385. etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
  386. {
  387. struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
  388. LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
  389. (netif->hwaddr_len == ETHARP_HWADDR_LEN));
  390. ETHADDR32_COPY(&ethhdr->dest, dst);
  391. ETHADDR16_COPY(&ethhdr->src, src);
  392. ethhdr->type = PP_HTONS(ETHTYPE_IP);
  393. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_send_ip: sending packet %p\n", (void *)p));
  394. /* send the packet */
  395. return netif->linkoutput(netif, p);
  396. }
  397. /**
  398. * Update (or insert) a IP/MAC address pair in the ARP cache.
  399. *
  400. * If a pending entry is resolved, any queued packets will be sent
  401. * at this point.
  402. *
  403. * @param netif netif related to this entry (used for NETIF_ADDRHINT)
  404. * @param ipaddr IP address of the inserted ARP entry.
  405. * @param ethaddr Ethernet address of the inserted ARP entry.
  406. * @param flags @see definition of ETHARP_FLAG_*
  407. *
  408. * @return
  409. * - ERR_OK Succesfully updated ARP cache.
  410. * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.
  411. * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
  412. *
  413. * @see pbuf_free()
  414. */
  415. static err_t ICACHE_FLASH_ATTR
  416. update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
  417. {
  418. s8_t i;
  419. LWIP_ASSERT("netif->hwaddr_len == ETHARP_HWADDR_LEN", netif->hwaddr_len == ETHARP_HWADDR_LEN);
  420. 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",
  421. ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
  422. ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
  423. ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
  424. /* non-unicast address? */
  425. if (ip_addr_isany(ipaddr) ||
  426. ip_addr_isbroadcast(ipaddr, netif) ||
  427. ip_addr_ismulticast(ipaddr)) {
  428. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
  429. return ERR_ARG;
  430. }
  431. /* find or create ARP entry */
  432. i = find_entry(ipaddr, flags);
  433. /* bail out if no entry could be found */
  434. if (i < 0) {
  435. return (err_t)i;
  436. }
  437. #if ETHARP_SUPPORT_STATIC_ENTRIES
  438. if (flags & ETHARP_FLAG_STATIC_ENTRY) {
  439. /* record static type */
  440. arp_table[i].static_entry = 1;
  441. }
  442. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  443. /* mark it stable */
  444. arp_table[i].state = ETHARP_STATE_STABLE;
  445. #if LWIP_SNMP
  446. /* record network interface */
  447. arp_table[i].netif = netif;
  448. #endif /* LWIP_SNMP */
  449. /* insert in SNMP ARP index tree */
  450. snmp_insert_arpidx_tree(netif, &arp_table[i].ipaddr);
  451. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
  452. /* update address */
  453. ETHADDR32_COPY(&arp_table[i].ethaddr, ethaddr);
  454. /* reset time stamp */
  455. arp_table[i].ctime = 0;
  456. /* this is where we will send out queued packets! */
  457. #if ARP_QUEUEING
  458. while (arp_table[i].q != NULL) {
  459. struct pbuf *p;
  460. /* remember remainder of queue */
  461. struct etharp_q_entry *q = arp_table[i].q;
  462. /* pop first item off the queue */
  463. arp_table[i].q = q->next;
  464. /* get the packet pointer */
  465. p = q->p;
  466. /* now queue entry can be freed */
  467. memp_free(MEMP_ARP_QUEUE, q);
  468. #else /* ARP_QUEUEING */
  469. if (arp_table[i].q != NULL) {
  470. struct pbuf *p = arp_table[i].q;
  471. arp_table[i].q = NULL;
  472. #endif /* ARP_QUEUEING */
  473. /* send the queued IP packet */
  474. etharp_send_ip(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr);
  475. /* free the queued IP packet */
  476. pbuf_free(p);
  477. }
  478. return ERR_OK;
  479. }
  480. #if ETHARP_SUPPORT_STATIC_ENTRIES
  481. /** Add a new static entry to the ARP table. If an entry exists for the
  482. * specified IP address, this entry is overwritten.
  483. * If packets are queued for the specified IP address, they are sent out.
  484. *
  485. * @param ipaddr IP address for the new static entry
  486. * @param ethaddr ethernet address for the new static entry
  487. * @return @see return values of etharp_add_static_entry
  488. */
  489. err_t
  490. etharp_add_static_entry(ip_addr_t *ipaddr, struct eth_addr *ethaddr)
  491. {
  492. struct netif *netif;
  493. 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",
  494. ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
  495. ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
  496. ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
  497. netif = ip_route(ipaddr);
  498. if (netif == NULL) {
  499. return ERR_RTE;
  500. }
  501. return update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
  502. }
  503. /** Remove a static entry from the ARP table previously added with a call to
  504. * etharp_add_static_entry.
  505. *
  506. * @param ipaddr IP address of the static entry to remove
  507. * @return ERR_OK: entry removed
  508. * ERR_MEM: entry wasn't found
  509. * ERR_ARG: entry wasn't a static entry but a dynamic one
  510. */
  511. err_t
  512. etharp_remove_static_entry(ip_addr_t *ipaddr)
  513. {
  514. s8_t i;
  515. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  516. ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
  517. /* find or create ARP entry */
  518. i = find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
  519. /* bail out if no entry could be found */
  520. if (i < 0) {
  521. return (err_t)i;
  522. }
  523. if ((arp_table[i].state != ETHARP_STATE_STABLE) ||
  524. (arp_table[i].static_entry == 0)) {
  525. /* entry wasn't a static entry, cannot remove it */
  526. return ERR_ARG;
  527. }
  528. /* entry found, free it */
  529. free_entry(i);
  530. return ERR_OK;
  531. }
  532. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  533. /**
  534. * Remove all ARP table entries of the specified netif.
  535. *
  536. * @param netif points to a network interface
  537. */
  538. void ICACHE_FLASH_ATTR etharp_cleanup_netif(struct netif *netif)
  539. {
  540. u8_t i;
  541. for (i = 0; i < ARP_TABLE_SIZE; ++i) {
  542. u8_t state = arp_table[i].state;
  543. if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) {
  544. free_entry(i);
  545. }
  546. }
  547. }
  548. /**
  549. * Finds (stable) ethernet/IP address pair from ARP table
  550. * using interface and IP address index.
  551. * @note the addresses in the ARP table are in network order!
  552. *
  553. * @param netif points to interface index
  554. * @param ipaddr points to the (network order) IP address index
  555. * @param eth_ret points to return pointer
  556. * @param ip_ret points to return pointer
  557. * @return table index if found, -1 otherwise
  558. */
  559. s8_t
  560. etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr,
  561. struct eth_addr **eth_ret, ip_addr_t **ip_ret)
  562. {
  563. s8_t i;
  564. LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
  565. eth_ret != NULL && ip_ret != NULL);
  566. LWIP_UNUSED_ARG(netif);
  567. i = find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
  568. if((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
  569. *eth_ret = &arp_table[i].ethaddr;
  570. *ip_ret = &arp_table[i].ipaddr;
  571. return i;
  572. }
  573. return -1;
  574. }
  575. #if ETHARP_TRUST_IP_MAC
  576. /**
  577. * Updates the ARP table using the given IP packet.
  578. *
  579. * Uses the incoming IP packet's source address to update the
  580. * ARP cache for the local network. The function does not alter
  581. * or free the packet. This function must be called before the
  582. * packet p is passed to the IP layer.
  583. *
  584. * @param netif The lwIP network interface on which the IP packet pbuf arrived.
  585. * @param p The IP packet that arrived on netif.
  586. *
  587. * @return NULL
  588. *
  589. * @see pbuf_free()
  590. */
  591. static void ICACHE_FLASH_ATTR
  592. etharp_ip_input(struct netif *netif, struct pbuf *p)
  593. {
  594. struct eth_hdr *ethhdr;
  595. struct ip_hdr *iphdr;
  596. ip_addr_t iphdr_src;
  597. LWIP_ERROR("netif != NULL", (netif != NULL), return;);
  598. /* Only insert an entry if the source IP address of the
  599. incoming IP packet comes from a host on the local network. */
  600. ethhdr = (struct eth_hdr *)p->payload;
  601. iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
  602. #if ETHARP_SUPPORT_VLAN
  603. if (ethhdr->type == ETHTYPE_VLAN) {
  604. iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
  605. }
  606. #endif /* ETHARP_SUPPORT_VLAN */
  607. ip_addr_copy(iphdr_src, iphdr->src);
  608. /* source is not on the local network? */
  609. if (!ip_addr_netcmp(&iphdr_src, &(netif->ip_addr), &(netif->netmask))) {
  610. /* do nothing */
  611. return;
  612. }
  613. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
  614. /* update the source IP address in the cache, if present */
  615. /* @todo We could use ETHARP_FLAG_TRY_HARD if we think we are going to talk
  616. * back soon (for example, if the destination IP address is ours. */
  617. update_arp_entry(netif, &iphdr_src, &(ethhdr->src), ETHARP_FLAG_FIND_ONLY);
  618. }
  619. #endif /* ETHARP_TRUST_IP_MAC */
  620. /**
  621. * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
  622. * send out queued IP packets. Updates cache with snooped address pairs.
  623. *
  624. * Should be called for incoming ARP packets. The pbuf in the argument
  625. * is freed by this function.
  626. *
  627. * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
  628. * @param ethaddr Ethernet address of netif.
  629. * @param p The ARP packet that arrived on netif. Is freed by this function.
  630. *
  631. * @return NULL
  632. *
  633. * @see pbuf_free()
  634. */
  635. static void ICACHE_FLASH_ATTR
  636. etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
  637. {
  638. struct etharp_hdr *hdr;
  639. struct eth_hdr *ethhdr;
  640. /* these are aligned properly, whereas the ARP header fields might not be */
  641. ip_addr_t sipaddr, dipaddr;
  642. u8_t for_us;
  643. #if LWIP_AUTOIP
  644. const u8_t * ethdst_hwaddr;
  645. #endif /* LWIP_AUTOIP */
  646. #ifdef EBUF_LWIP
  647. struct pbuf *q;
  648. #endif /* EBUF_LWIP */
  649. LWIP_ERROR("netif != NULL", (netif != NULL), return;);
  650. /* drop short ARP packets: we have to check for p->len instead of p->tot_len here
  651. since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */
  652. if (p->len < SIZEOF_ETHARP_PACKET) {
  653. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
  654. ("etharp_arp_input: packet dropped, too short (%"S16_F"/%"S16_F")\n", p->tot_len,
  655. (s16_t)SIZEOF_ETHARP_PACKET));
  656. ETHARP_STATS_INC(etharp.lenerr);
  657. ETHARP_STATS_INC(etharp.drop);
  658. pbuf_free(p);
  659. return;
  660. }
  661. ethhdr = (struct eth_hdr *)p->payload;
  662. hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
  663. #if ETHARP_SUPPORT_VLAN
  664. if (ethhdr->type == ETHTYPE_VLAN) {
  665. hdr = (struct etharp_hdr *)(((u8_t*)ethhdr) + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
  666. }
  667. #endif /* ETHARP_SUPPORT_VLAN */
  668. /* RFC 826 "Packet Reception": */
  669. if ((hdr->hwtype != PP_HTONS(HWTYPE_ETHERNET)) ||
  670. (hdr->hwlen != ETHARP_HWADDR_LEN) ||
  671. (hdr->protolen != sizeof(ip_addr_t)) ||
  672. (hdr->proto != PP_HTONS(ETHTYPE_IP)) ||
  673. (ethhdr->type != PP_HTONS(ETHTYPE_ARP))) {
  674. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
  675. ("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",
  676. hdr->hwtype, hdr->hwlen, hdr->proto, hdr->protolen, ethhdr->type));
  677. ETHARP_STATS_INC(etharp.proterr);
  678. ETHARP_STATS_INC(etharp.drop);
  679. pbuf_free(p);
  680. return;
  681. }
  682. ETHARP_STATS_INC(etharp.recv);
  683. #if LWIP_AUTOIP
  684. /* We have to check if a host already has configured our random
  685. * created link local address and continously check if there is
  686. * a host with this IP-address so we can detect collisions */
  687. autoip_arp_reply(netif, hdr);
  688. #endif /* LWIP_AUTOIP */
  689. /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
  690. * structure packing (not using structure copy which breaks strict-aliasing rules). */
  691. IPADDR2_COPY(&sipaddr, &hdr->sipaddr);
  692. IPADDR2_COPY(&dipaddr, &hdr->dipaddr);
  693. /* this interface is not configured? */
  694. if (ip_addr_isany(&netif->ip_addr)) {
  695. for_us = 0;
  696. } else {
  697. /* ARP packet directed to us? */
  698. for_us = (u8_t)ip_addr_cmp(&dipaddr, &(netif->ip_addr));
  699. }
  700. /* ARP message directed to us?
  701. -> add IP address in ARP cache; assume requester wants to talk to us,
  702. can result in directly sending the queued packets for this host.
  703. ARP message not directed to us?
  704. -> update the source IP address in the cache, if present */
  705. update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
  706. for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
  707. /* now act on the message itself */
  708. switch (hdr->opcode) {
  709. /* ARP request? */
  710. case PP_HTONS(ARP_REQUEST):
  711. /* ARP request. If it asked for our address, we send out a
  712. * reply. In any case, we time-stamp any existing ARP entry,
  713. * and possiby send out an IP packet that was queued on it. */
  714. LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
  715. /* ARP request for our address? */
  716. if (for_us) {
  717. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n"));
  718. /* Re-use pbuf to send ARP reply.
  719. Since we are re-using an existing pbuf, we can't call etharp_raw since
  720. that would allocate a new pbuf. */
  721. hdr->opcode = htons(ARP_REPLY);
  722. IPADDR2_COPY(&hdr->dipaddr, &hdr->sipaddr);
  723. IPADDR2_COPY(&hdr->sipaddr, &netif->ip_addr);
  724. LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
  725. (netif->hwaddr_len == ETHARP_HWADDR_LEN));
  726. #if LWIP_AUTOIP
  727. /* If we are using Link-Local, all ARP packets that contain a Link-Local
  728. * 'sender IP address' MUST be sent using link-layer broadcast instead of
  729. * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
  730. ethdst_hwaddr = ip_addr_islinklocal(&netif->ip_addr) ? (u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr;
  731. #endif /* LWIP_AUTOIP */
  732. ETHADDR16_COPY(&hdr->dhwaddr, &hdr->shwaddr);
  733. #if LWIP_AUTOIP
  734. ETHADDR16_COPY(&ethhdr->dest, ethdst_hwaddr);
  735. #else /* LWIP_AUTOIP */
  736. ETHADDR16_COPY(&ethhdr->dest, &hdr->shwaddr);
  737. #endif /* LWIP_AUTOIP */
  738. ETHADDR16_COPY(&hdr->shwaddr, ethaddr);
  739. ETHADDR16_COPY(&ethhdr->src, ethaddr);
  740. /* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header
  741. are already correct, we tested that before */
  742. #ifdef EBUF_LWIP
  743. /*
  744. * don't do flip-flop here... do a copy here.
  745. * otherwise, we need to handle existing pbuf->eb in ieee80211_output.c
  746. */
  747. q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  748. if (q != NULL) {
  749. pbuf_copy(q, p);
  750. //pbuf_free(p);
  751. } else {
  752. LWIP_ASSERT("q != NULL", q != NULL);
  753. }
  754. netif->linkoutput(netif, q);
  755. pbuf_free(q);
  756. #else
  757. /* return ARP reply */
  758. netif->linkoutput(netif, p);
  759. #endif /* ESF_LWIP */
  760. /* we are not configured? */
  761. } else if (ip_addr_isany(&netif->ip_addr)) {
  762. /* { for_us == 0 and netif->ip_addr.addr == 0 } */
  763. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
  764. /* request was not directed to us */
  765. } else {
  766. /* { for_us == 0 and netif->ip_addr.addr != 0 } */
  767. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n"));
  768. }
  769. break;
  770. case PP_HTONS(ARP_REPLY):
  771. /* ARP reply. We already updated the ARP cache earlier. */
  772. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n"));
  773. #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
  774. /* DHCP wants to know about ARP replies from any host with an
  775. * IP address also offered to us by the DHCP server. We do not
  776. * want to take a duplicate IP address on a single network.
  777. * @todo How should we handle redundant (fail-over) interfaces? */
  778. dhcp_arp_reply(netif, &sipaddr);
  779. #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */
  780. break;
  781. default:
  782. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %"S16_F"\n", htons(hdr->opcode)));
  783. ETHARP_STATS_INC(etharp.err);
  784. break;
  785. }
  786. /* free ARP packet */
  787. pbuf_free(p);
  788. }
  789. /** Just a small helper function that sends a pbuf to an ethernet address
  790. * in the arp_table specified by the index 'arp_idx'.
  791. */
  792. static err_t ICACHE_FLASH_ATTR
  793. etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, u8_t arp_idx)
  794. {
  795. LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE",
  796. arp_table[arp_idx].state >= ETHARP_STATE_STABLE);
  797. /* if arp table entry is about to expire: re-request it,
  798. but only if its state is ETHARP_STATE_STABLE to prevent flooding the
  799. network with ARP requests if this address is used frequently. */
  800. if ((arp_table[arp_idx].state == ETHARP_STATE_STABLE) &&
  801. (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED)) {
  802. if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) {
  803. arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING;
  804. }
  805. }
  806. return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr),
  807. &arp_table[arp_idx].ethaddr);
  808. }
  809. /**
  810. * Resolve and fill-in Ethernet address header for outgoing IP packet.
  811. *
  812. * For IP multicast and broadcast, corresponding Ethernet addresses
  813. * are selected and the packet is transmitted on the link.
  814. *
  815. * For unicast addresses, the packet is submitted to etharp_query(). In
  816. * case the IP address is outside the local network, the IP address of
  817. * the gateway is used.
  818. *
  819. * @param netif The lwIP network interface which the IP packet will be sent on.
  820. * @param q The pbuf(s) containing the IP packet to be sent.
  821. * @param ipaddr The IP address of the packet destination.
  822. *
  823. * @return
  824. * - ERR_RTE No route to destination (no gateway to external networks),
  825. * or the return type of either etharp_query() or etharp_send_ip().
  826. */
  827. err_t
  828. etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)
  829. {
  830. struct eth_addr *dest, mcastaddr;
  831. /* make room for Ethernet header - should not fail */
  832. if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
  833. /* bail out */
  834. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
  835. ("etharp_output: could not allocate room for header.\n"));
  836. LINK_STATS_INC(link.lenerr);
  837. return ERR_BUF;
  838. }
  839. /* assume unresolved Ethernet address */
  840. dest = NULL;
  841. /* Determine on destination hardware address. Broadcasts and multicasts
  842. * are special, other IP addresses are looked up in the ARP table. */
  843. /* broadcast destination IP address? */
  844. if (ip_addr_isbroadcast(ipaddr, netif)) {
  845. /* broadcast on Ethernet also */
  846. dest = (struct eth_addr *)&ethbroadcast;
  847. /* multicast destination IP address? */
  848. } else if (ip_addr_ismulticast(ipaddr)) {
  849. /* Hash IP multicast address to MAC address.*/
  850. mcastaddr.addr[0] = 0x01;
  851. mcastaddr.addr[1] = 0x00;
  852. mcastaddr.addr[2] = 0x5e;
  853. mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
  854. mcastaddr.addr[4] = ip4_addr3(ipaddr);
  855. mcastaddr.addr[5] = ip4_addr4(ipaddr);
  856. /* destination Ethernet address is multicast */
  857. dest = &mcastaddr;
  858. /* unicast destination IP address? */
  859. } else {
  860. s8_t i;
  861. /* outside local network? if so, this can neither be a global broadcast nor
  862. a subnet broadcast. */
  863. if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask)) &&
  864. !ip_addr_islinklocal(ipaddr)) {
  865. #if LWIP_AUTOIP
  866. struct ip_hdr *iphdr = (struct ip_hdr*)((u8_t*)q->payload +
  867. sizeof(struct eth_hdr));
  868. /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with
  869. a link-local source address must always be "directly to its destination
  870. on the same physical link. The host MUST NOT send the packet to any
  871. router for forwarding". */
  872. if (!ip_addr_islinklocal(&iphdr->src))
  873. #endif /* LWIP_AUTOIP */
  874. {
  875. /* interface has default gateway? */
  876. if (!ip_addr_isany(&netif->gw)) {
  877. /* send to hardware address of default gateway IP address */
  878. ipaddr = &(netif->gw);
  879. /* no default gateway available */
  880. } else {
  881. /* no route to destination error (default gateway missing) */
  882. return ERR_RTE;
  883. }
  884. }
  885. }
  886. #if LWIP_NETIF_HWADDRHINT
  887. if (netif->addr_hint != NULL) {
  888. /* per-pcb cached entry was given */
  889. u8_t etharp_cached_entry = *(netif->addr_hint);
  890. if (etharp_cached_entry < ARP_TABLE_SIZE) {
  891. #endif /* LWIP_NETIF_HWADDRHINT */
  892. if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) &&
  893. (ip_addr_cmp(ipaddr, &arp_table[etharp_cached_entry].ipaddr))) {
  894. /* the per-pcb-cached entry is stable and the right one! */
  895. ETHARP_STATS_INC(etharp.cachehit);
  896. return etharp_output_to_arp_index(netif, q, etharp_cached_entry);
  897. }
  898. #if LWIP_NETIF_HWADDRHINT
  899. }
  900. }
  901. #endif /* LWIP_NETIF_HWADDRHINT */
  902. /* find stable entry: do this here since this is a critical path for
  903. throughput and etharp_find_entry() is kind of slow */
  904. for (i = 0; i < ARP_TABLE_SIZE; i++) {
  905. if ((arp_table[i].state >= ETHARP_STATE_STABLE) &&
  906. (ip_addr_cmp(ipaddr, &arp_table[i].ipaddr))) {
  907. /* found an existing, stable entry */
  908. ETHARP_SET_HINT(netif, i);
  909. return etharp_output_to_arp_index(netif, q, i);
  910. }
  911. }
  912. /* queue on destination Ethernet address belonging to ipaddr */
  913. return etharp_query(netif, ipaddr, q);
  914. }
  915. /* continuation for multicast/broadcast destinations */
  916. /* obtain source Ethernet address of the given interface */
  917. /* send packet directly on the link */
  918. return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr), dest);
  919. }
  920. /**
  921. * Send an ARP request for the given IP address and/or queue a packet.
  922. *
  923. * If the IP address was not yet in the cache, a pending ARP cache entry
  924. * is added and an ARP request is sent for the given address. The packet
  925. * is queued on this entry.
  926. *
  927. * If the IP address was already pending in the cache, a new ARP request
  928. * is sent for the given address. The packet is queued on this entry.
  929. *
  930. * If the IP address was already stable in the cache, and a packet is
  931. * given, it is directly sent and no ARP request is sent out.
  932. *
  933. * If the IP address was already stable in the cache, and no packet is
  934. * given, an ARP request is sent out.
  935. *
  936. * @param netif The lwIP network interface on which ipaddr
  937. * must be queried for.
  938. * @param ipaddr The IP address to be resolved.
  939. * @param q If non-NULL, a pbuf that must be delivered to the IP address.
  940. * q is not freed by this function.
  941. *
  942. * @note q must only be ONE packet, not a packet queue!
  943. *
  944. * @return
  945. * - ERR_BUF Could not make room for Ethernet header.
  946. * - ERR_MEM Hardware address unknown, and no more ARP entries available
  947. * to query for address or queue the packet.
  948. * - ERR_MEM Could not queue packet due to memory shortage.
  949. * - ERR_RTE No route to destination (no gateway to external networks).
  950. * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
  951. *
  952. */
  953. err_t
  954. etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q)
  955. {
  956. struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
  957. err_t result = ERR_MEM;
  958. s8_t i; /* ARP entry index */
  959. /* non-unicast address? */
  960. if (ip_addr_isbroadcast(ipaddr, netif) ||
  961. ip_addr_ismulticast(ipaddr) ||
  962. ip_addr_isany(ipaddr)) {
  963. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
  964. return ERR_ARG;
  965. }
  966. /* find entry in ARP cache, ask to create entry if queueing packet */
  967. i = find_entry(ipaddr, ETHARP_FLAG_TRY_HARD);
  968. /* could not find or create entry? */
  969. if (i < 0) {
  970. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
  971. if (q) {
  972. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
  973. ETHARP_STATS_INC(etharp.memerr);
  974. }
  975. return (err_t)i;
  976. }
  977. /* mark a fresh entry as pending (we just sent a request) */
  978. if (arp_table[i].state == ETHARP_STATE_EMPTY) {
  979. arp_table[i].state = ETHARP_STATE_PENDING;
  980. }
  981. /* { i is either a STABLE or (new or existing) PENDING entry } */
  982. LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
  983. ((arp_table[i].state == ETHARP_STATE_PENDING) ||
  984. (arp_table[i].state >= ETHARP_STATE_STABLE)));
  985. /* do we have a pending entry? or an implicit query request? */
  986. if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) {
  987. /* try to resolve it; send out ARP request */
  988. result = etharp_request(netif, ipaddr);
  989. if (result != ERR_OK) {
  990. /* ARP request couldn't be sent */
  991. /* We don't re-send arp request in etharp_tmr, but we still queue packets,
  992. since this failure could be temporary, and the next packet calling
  993. etharp_query again could lead to sending the queued packets. */
  994. }
  995. if (q == NULL) {
  996. return result;
  997. }
  998. }
  999. /* packet given? */
  1000. LWIP_ASSERT("q != NULL", q != NULL);
  1001. /* stable entry? */
  1002. if (arp_table[i].state >= ETHARP_STATE_STABLE) {
  1003. /* we have a valid IP->Ethernet address mapping */
  1004. ETHARP_SET_HINT(netif, i);
  1005. /* send the packet */
  1006. result = etharp_send_ip(netif, q, srcaddr, &(arp_table[i].ethaddr));
  1007. /* pending entry? (either just created or already pending */
  1008. } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
  1009. /* entry is still pending, queue the given packet 'q' */
  1010. struct pbuf *p;
  1011. int copy_needed = 0;
  1012. /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
  1013. * to copy the whole queue into a new PBUF_RAM (see bug #11400)
  1014. * PBUF_ROMs can be left as they are, since ROM must not get changed. */
  1015. p = q;
  1016. while (p) {
  1017. LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
  1018. if(p->type != PBUF_ROM) {
  1019. copy_needed = 1;
  1020. break;
  1021. }
  1022. p = p->next;
  1023. }
  1024. if(copy_needed) {
  1025. /* copy the whole packet into new pbufs */
  1026. p = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  1027. if(p != NULL) {
  1028. if (pbuf_copy(p, q) != ERR_OK) {
  1029. pbuf_free(p);
  1030. p = NULL;
  1031. }
  1032. }
  1033. } else {
  1034. /* referencing the old pbuf is enough */
  1035. p = q;
  1036. pbuf_ref(p);
  1037. }
  1038. /* packet could be taken over? */
  1039. if (p != NULL) {
  1040. /* queue packet ... */
  1041. #if ARP_QUEUEING
  1042. struct etharp_q_entry *new_entry;
  1043. /* allocate a new arp queue entry */
  1044. new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE);
  1045. if (new_entry != NULL) {
  1046. unsigned int qlen = 0;
  1047. new_entry->next = 0;
  1048. new_entry->p = p;
  1049. if(arp_table[i].q != NULL) {
  1050. /* queue was already existent, append the new entry to the end */
  1051. struct etharp_q_entry *r;
  1052. r = arp_table[i].q;
  1053. qlen++;
  1054. while (r->next != NULL) {
  1055. r = r->next;
  1056. qlen++;
  1057. }
  1058. r->next = new_entry;
  1059. } else {
  1060. /* queue did not exist, first item in queue */
  1061. arp_table[i].q = new_entry;
  1062. }
  1063. if(qlen >= 3) {
  1064. struct etharp_q_entry *old;
  1065. old = arp_table[i].q;
  1066. arp_table[i].q = arp_table[i].q->next;
  1067. pbuf_free(old->p);
  1068. memp_free(MEM_ARP_QUEUE, old);
  1069. }
  1070. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
  1071. result = ERR_OK;
  1072. } else {
  1073. /* the pool MEMP_ARP_QUEUE is empty */
  1074. pbuf_free(p);
  1075. 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));
  1076. result = ERR_MEM;
  1077. }
  1078. #else /* ARP_QUEUEING */
  1079. /* always queue one packet per ARP request only, freeing a previously queued packet */
  1080. if (arp_table[i].q != NULL) {
  1081. 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));
  1082. pbuf_free(arp_table[i].q);
  1083. }
  1084. arp_table[i].q = p;
  1085. result = ERR_OK;
  1086. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
  1087. #endif /* ARP_QUEUEING */
  1088. } else {
  1089. ETHARP_STATS_INC(etharp.memerr);
  1090. 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));
  1091. result = ERR_MEM;
  1092. }
  1093. }
  1094. return result;
  1095. }
  1096. /**
  1097. * Send a raw ARP packet (opcode and all addresses can be modified)
  1098. *
  1099. * @param netif the lwip network interface on which to send the ARP packet
  1100. * @param ethsrc_addr the source MAC address for the ethernet header
  1101. * @param ethdst_addr the destination MAC address for the ethernet header
  1102. * @param hwsrc_addr the source MAC address for the ARP protocol header
  1103. * @param ipsrc_addr the source IP address for the ARP protocol header
  1104. * @param hwdst_addr the destination MAC address for the ARP protocol header
  1105. * @param ipdst_addr the destination IP address for the ARP protocol header
  1106. * @param opcode the type of the ARP packet
  1107. * @return ERR_OK if the ARP packet has been sent
  1108. * ERR_MEM if the ARP packet couldn't be allocated
  1109. * any other err_t on failure
  1110. */
  1111. #if !LWIP_AUTOIP
  1112. static
  1113. #endif /* LWIP_AUTOIP */
  1114. err_t ICACHE_FLASH_ATTR
  1115. etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
  1116. const struct eth_addr *ethdst_addr,
  1117. const struct eth_addr *hwsrc_addr, const ip_addr_t *ipsrc_addr,
  1118. const struct eth_addr *hwdst_addr, const ip_addr_t *ipdst_addr,
  1119. const u16_t opcode)
  1120. {
  1121. struct pbuf *p;
  1122. err_t result = ERR_OK;
  1123. struct eth_hdr *ethhdr;
  1124. struct etharp_hdr *hdr;
  1125. #if LWIP_AUTOIP
  1126. const u8_t * ethdst_hwaddr;
  1127. #endif /* LWIP_AUTOIP */
  1128. /* allocate a pbuf for the outgoing ARP request packet */
  1129. p = pbuf_alloc(PBUF_RAW, SIZEOF_ETHARP_PACKET, PBUF_RAM);
  1130. /* could allocate a pbuf for an ARP request? */
  1131. if (p == NULL) {
  1132. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
  1133. ("etharp_raw: could not allocate pbuf for ARP request.\n"));
  1134. ETHARP_STATS_INC(etharp.memerr);
  1135. return ERR_MEM;
  1136. }
  1137. LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
  1138. (p->len >= SIZEOF_ETHARP_PACKET));
  1139. ethhdr = (struct eth_hdr *)p->payload;
  1140. hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
  1141. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
  1142. hdr->opcode = htons(opcode);
  1143. LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
  1144. (netif->hwaddr_len == ETHARP_HWADDR_LEN));
  1145. #if LWIP_AUTOIP
  1146. /* If we are using Link-Local, all ARP packets that contain a Link-Local
  1147. * 'sender IP address' MUST be sent using link-layer broadcast instead of
  1148. * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
  1149. ethdst_hwaddr = ip_addr_islinklocal(ipsrc_addr) ? (u8_t*)(ethbroadcast.addr) : ethdst_addr->addr;
  1150. #endif /* LWIP_AUTOIP */
  1151. /* Write the ARP MAC-Addresses */
  1152. ETHADDR16_COPY(&hdr->shwaddr, hwsrc_addr);
  1153. ETHADDR16_COPY(&hdr->dhwaddr, hwdst_addr);
  1154. /* Write the Ethernet MAC-Addresses */
  1155. #if LWIP_AUTOIP
  1156. ETHADDR16_COPY(&ethhdr->dest, ethdst_hwaddr);
  1157. #else /* LWIP_AUTOIP */
  1158. ETHADDR16_COPY(&ethhdr->dest, ethdst_addr);
  1159. #endif /* LWIP_AUTOIP */
  1160. ETHADDR16_COPY(&ethhdr->src, ethsrc_addr);
  1161. /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
  1162. * structure packing. */
  1163. IPADDR2_COPY(&hdr->sipaddr, ipsrc_addr);
  1164. IPADDR2_COPY(&hdr->dipaddr, ipdst_addr);
  1165. hdr->hwtype = PP_HTONS(HWTYPE_ETHERNET);
  1166. hdr->proto = PP_HTONS(ETHTYPE_IP);
  1167. /* set hwlen and protolen */
  1168. hdr->hwlen = ETHARP_HWADDR_LEN;
  1169. hdr->protolen = sizeof(ip_addr_t);
  1170. ethhdr->type = PP_HTONS(ETHTYPE_ARP);
  1171. /* send ARP query */
  1172. result = netif->linkoutput(netif, p);
  1173. ETHARP_STATS_INC(etharp.xmit);
  1174. /* free ARP query packet */
  1175. pbuf_free(p);
  1176. p = NULL;
  1177. /* could not allocate pbuf for ARP request */
  1178. return result;
  1179. }
  1180. /**
  1181. * Send an ARP request packet asking for ipaddr.
  1182. *
  1183. * @param netif the lwip network interface on which to send the request
  1184. * @param ipaddr the IP address for which to ask
  1185. * @return ERR_OK if the request has been sent
  1186. * ERR_MEM if the ARP packet couldn't be allocated
  1187. * any other err_t on failure
  1188. */
  1189. err_t
  1190. etharp_request(struct netif *netif, ip_addr_t *ipaddr)
  1191. {
  1192. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
  1193. return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
  1194. (struct eth_addr *)netif->hwaddr, &netif->ip_addr, &ethzero,
  1195. ipaddr, ARP_REQUEST);
  1196. }
  1197. #endif /* LWIP_ARP */
  1198. /**
  1199. * Process received ethernet frames. Using this function instead of directly
  1200. * calling ip_input and passing ARP frames through etharp in ethernetif_input,
  1201. * the ARP cache is protected from concurrent access.
  1202. *
  1203. * @param p the recevied packet, p->payload pointing to the ethernet header
  1204. * @param netif the network interface on which the packet was received
  1205. */
  1206. err_t
  1207. ethernet_input(struct pbuf *p, struct netif *netif)
  1208. {
  1209. struct eth_hdr* ethhdr;
  1210. u16_t type;
  1211. s16_t ip_hdr_offset = SIZEOF_ETH_HDR;
  1212. if (p->len <= SIZEOF_ETH_HDR) {
  1213. /* a packet with only an ethernet header (or less) is not valid for us modify by ives at 2014.4.24*/
  1214. ETHARP_STATS_INC(etharp.proterr);
  1215. ETHARP_STATS_INC(etharp.drop);
  1216. goto free_and_return;
  1217. }
  1218. /* points to packet payload, which starts with an Ethernet header */
  1219. ethhdr = (struct eth_hdr *)p->payload;
  1220. LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
  1221. ("ethernet_input: dest:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", src:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", type:%"X16_F"\n",
  1222. (unsigned)ethhdr->dest.addr[0], (unsigned)ethhdr->dest.addr[1], (unsigned)ethhdr->dest.addr[2],
  1223. (unsigned)ethhdr->dest.addr[3], (unsigned)ethhdr->dest.addr[4], (unsigned)ethhdr->dest.addr[5],
  1224. (unsigned)ethhdr->src.addr[0], (unsigned)ethhdr->src.addr[1], (unsigned)ethhdr->src.addr[2],
  1225. (unsigned)ethhdr->src.addr[3], (unsigned)ethhdr->src.addr[4], (unsigned)ethhdr->src.addr[5],
  1226. (unsigned)htons(ethhdr->type)));
  1227. type = ethhdr->type;
  1228. #if ETHARP_SUPPORT_VLAN
  1229. if (type == PP_HTONS(ETHTYPE_VLAN)) {
  1230. struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR);
  1231. if (p->len <= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) {
  1232. /* a packet with only an ethernet/vlan header (or less) is not valid for us modify by ives at 2014.4.24*/
  1233. ETHARP_STATS_INC(etharp.proterr);
  1234. ETHARP_STATS_INC(etharp.drop);
  1235. goto free_and_return;
  1236. }
  1237. #ifdef ETHARP_VLAN_CHECK /* if not, allow all VLANs */
  1238. if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {
  1239. /* silently ignore this packet: not for our VLAN */
  1240. pbuf_free(p);
  1241. return ERR_OK;
  1242. }
  1243. #endif /* ETHARP_VLAN_CHECK */
  1244. type = vlan->tpid;
  1245. ip_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR;
  1246. }
  1247. #endif /* ETHARP_SUPPORT_VLAN */
  1248. #if LWIP_ARP_FILTER_NETIF
  1249. netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, htons(type));
  1250. #endif /* LWIP_ARP_FILTER_NETIF*/
  1251. switch (type) {
  1252. #if LWIP_ARP
  1253. /* IP packet? */
  1254. case PP_HTONS(ETHTYPE_IP):
  1255. if (!(netif->flags & NETIF_FLAG_ETHARP)) {
  1256. goto free_and_return;
  1257. }
  1258. #if ETHARP_TRUST_IP_MAC
  1259. /* update ARP table */
  1260. etharp_ip_input(netif, p);
  1261. #endif /* ETHARP_TRUST_IP_MAC */
  1262. /* skip Ethernet header */
  1263. if(pbuf_header(p, -ip_hdr_offset)) {
  1264. LWIP_ASSERT("Can't move over header in packet", 0);
  1265. goto free_and_return;
  1266. } else {
  1267. /* pass to IP layer */
  1268. ip_input(p, netif);
  1269. }
  1270. break;
  1271. case PP_HTONS(ETHTYPE_ARP):
  1272. if (!(netif->flags & NETIF_FLAG_ETHARP)) {
  1273. goto free_and_return;
  1274. }
  1275. /* pass p to ARP module */
  1276. etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
  1277. break;
  1278. #endif /* LWIP_ARP */
  1279. #if PPPOE_SUPPORT
  1280. case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */
  1281. pppoe_disc_input(netif, p);
  1282. break;
  1283. case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */
  1284. pppoe_data_input(netif, p);
  1285. break;
  1286. #endif /* PPPOE_SUPPORT */
  1287. default:
  1288. ETHARP_STATS_INC(etharp.proterr);
  1289. ETHARP_STATS_INC(etharp.drop);
  1290. goto free_and_return;
  1291. }
  1292. /* This means the pbuf is freed or consumed,
  1293. so the caller doesn't have to free it again */
  1294. return ERR_OK;
  1295. free_and_return:
  1296. pbuf_free(p);
  1297. return ERR_OK;
  1298. }
  1299. #endif /* LWIP_ARP || LWIP_ETHERNET */