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