]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/net/lwip/lwip/netif/etharp.c
3 * Address Resolution Protocol module for IP over Ethernet
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without modification,
21 * are permitted provided that the following conditions are met:
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.
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
42 * This file is part of the lwIP TCP/IP stack.
48 #if LWIP_ARP || LWIP_ETHERNET
50 #include "lwip/ip_addr.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"
60 #include "netif/ppp_oe.h"
61 #endif /* PPPOE_SUPPORT */
65 const struct eth_addr ethbroadcast
= {{0xff,0xff,0xff,0xff,0xff,0xff}};
66 const struct eth_addr ethzero
= {{0,0,0,0,0,0}};
68 #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */
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.
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.
79 * @internal Keep this number at least 2, otherwise it might
80 * run out instantly if the timeout occurs directly after a request.
82 #define ARP_MAXPENDING 2
84 #define HWTYPE_ETHERNET 1
87 ETHARP_STATE_EMPTY
= 0,
94 /** Pointer to queue of pending outgoing packets on this ARP entry. */
95 struct etharp_q_entry
*q
;
96 #else /* ARP_QUEUEING */
97 /** Pointer to a single pending outgoing packet on this ARP entry. */
99 #endif /* ARP_QUEUEING */
101 struct eth_addr ethaddr
;
104 #endif /* LWIP_SNMP */
107 #if ETHARP_SUPPORT_STATIC_ENTRIES
109 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
112 static struct etharp_entry arp_table
[ARP_TABLE_SIZE
];
114 #if !LWIP_NETIF_HWADDRHINT
115 static u8_t etharp_cached_entry
;
116 #endif /* !LWIP_NETIF_HWADDRHINT */
118 /** Try hard to create a new entry - we want the IP address to appear in
119 the cache (even if this means removing an active entry or so). */
120 #define ETHARP_FLAG_TRY_HARD 1
121 #define ETHARP_FLAG_FIND_ONLY 2
122 #define ETHARP_FLAG_STATIC_ENTRY 4
124 #if LWIP_NETIF_HWADDRHINT
125 #define ETHARP_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \
126 *((netif)->addr_hint) = (hint);
127 #else /* LWIP_NETIF_HWADDRHINT */
128 #define ETHARP_SET_HINT(netif, hint) (etharp_cached_entry = (hint))
129 #endif /* LWIP_NETIF_HWADDRHINT */
131 static err_t
update_arp_entry(struct netif
*netif
, ip_addr_t
*ipaddr
, struct eth_addr
*ethaddr
, u8_t flags
);
134 /* Some checks, instead of etharp_init(): */
135 #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f))
136 #error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h"
142 * Free a complete queue of etharp entries
144 * @param q a qeueue of etharp_q_entry's to free
147 free_etharp_q(struct etharp_q_entry
*q
)
149 struct etharp_q_entry
*r
;
150 LWIP_ASSERT("q != NULL", q
!= NULL
);
151 LWIP_ASSERT("q->p != NULL", q
->p
!= NULL
);
155 LWIP_ASSERT("r->p != NULL", (r
->p
!= NULL
));
157 memp_free(MEMP_ARP_QUEUE
, r
);
160 #else /* ARP_QUEUEING */
162 /** Compatibility define: free the queued pbuf */
163 #define free_etharp_q(q) pbuf_free(q)
165 #endif /* ARP_QUEUEING */
167 /** Clean up ARP table entries */
171 /* remove from SNMP ARP index tree */
172 snmp_delete_arpidx_tree(arp_table
[i
].netif
, &arp_table
[i
].ipaddr
);
173 /* and empty packet queue */
174 if (arp_table
[i
].q
!= NULL
) {
175 /* remove all queued packets */
176 LWIP_DEBUGF(ETHARP_DEBUG
, ("etharp_timer: freeing entry %"U16_F
", packet queue %p.\n", (u16_t
)i
, (void *)(arp_table
[i
].q
)));
177 free_etharp_q(arp_table
[i
].q
);
178 arp_table
[i
].q
= NULL
;
180 /* recycle entry for re-use */
181 arp_table
[i
].state
= ETHARP_STATE_EMPTY
;
182 #if ETHARP_SUPPORT_STATIC_ENTRIES
183 arp_table
[i
].static_entry
= 0;
184 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
186 /* for debugging, clean out the complete entry */
187 arp_table
[i
].ctime
= 0;
189 arp_table
[i
].netif
= NULL
;
190 #endif /* LWIP_SNMP */
191 ip_addr_set_zero(&arp_table
[i
].ipaddr
);
192 arp_table
[i
].ethaddr
= ethzero
;
193 #endif /* LWIP_DEBUG */
197 * Clears expired entries in the ARP table.
199 * This function should be called every ETHARP_TMR_INTERVAL milliseconds (5 seconds),
200 * in order to expire entries in the ARP table.
207 LWIP_DEBUGF(ETHARP_DEBUG
, ("etharp_timer\n"));
208 /* remove expired entries from the ARP table */
209 for (i
= 0; i
< ARP_TABLE_SIZE
; ++i
) {
210 u8_t state
= arp_table
[i
].state
;
211 if (state
!= ETHARP_STATE_EMPTY
212 #if ETHARP_SUPPORT_STATIC_ENTRIES
213 && (arp_table
[i
].static_entry
== 0)
214 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
216 arp_table
[i
].ctime
++;
217 if ((arp_table
[i
].ctime
>= ARP_MAXAGE
) ||
218 ((arp_table
[i
].state
== ETHARP_STATE_PENDING
) &&
219 (arp_table
[i
].ctime
>= ARP_MAXPENDING
))) {
220 /* pending or stable entry has become old! */
221 LWIP_DEBUGF(ETHARP_DEBUG
, ("etharp_timer: expired %s entry %"U16_F
".\n",
222 arp_table
[i
].state
== ETHARP_STATE_STABLE
? "stable" : "pending", (u16_t
)i
));
223 /* clean up entries that have just been expired */
227 /* still pending entry? (not expired) */
228 if (arp_table
[i
].state
== ETHARP_STATE_PENDING
) {
229 /* resend an ARP query here? */
231 #endif /* ARP_QUEUEING */
237 * Search the ARP table for a matching or new entry.
239 * If an IP address is given, return a pending or stable ARP entry that matches
240 * the address. If no match is found, create a new entry with this address set,
241 * but in state ETHARP_EMPTY. The caller must check and possibly change the
242 * state of the returned entry.
244 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
246 * In all cases, attempt to create new entries from an empty entry. If no
247 * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle
248 * old entries. Heuristic choose the least important entry for recycling.
250 * @param ipaddr IP address to find in ARP cache, or to add if not found.
251 * @param flags @see definition of ETHARP_FLAG_*
252 * @param netif netif related to this address (used for NETIF_HWADDRHINT)
254 * @return The ARP entry index that matched or is created, ERR_MEM if no
255 * entry is found or could be recycled.
258 find_entry(ip_addr_t
*ipaddr
, u8_t flags
)
260 s8_t old_pending
= ARP_TABLE_SIZE
, old_stable
= ARP_TABLE_SIZE
;
261 s8_t empty
= ARP_TABLE_SIZE
;
262 u8_t i
= 0, age_pending
= 0, age_stable
= 0;
263 /* oldest entry with packets on queue */
264 s8_t old_queue
= ARP_TABLE_SIZE
;
269 * a) do a search through the cache, remember candidates
270 * b) select candidate entry
271 * c) create new entry
274 /* a) in a single search sweep, do all of this
275 * 1) remember the first empty entry (if any)
276 * 2) remember the oldest stable entry (if any)
277 * 3) remember the oldest pending entry without queued packets (if any)
278 * 4) remember the oldest pending entry with queued packets (if any)
279 * 5) search for a matching IP entry, either pending or stable
280 * until 5 matches, or all entries are searched for.
283 for (i
= 0; i
< ARP_TABLE_SIZE
; ++i
) {
284 u8_t state
= arp_table
[i
].state
;
285 /* no empty entry found yet and now we do find one? */
286 if ((empty
== ARP_TABLE_SIZE
) && (state
== ETHARP_STATE_EMPTY
)) {
287 LWIP_DEBUGF(ETHARP_DEBUG
, ("find_entry: found empty entry %"U16_F
"\n", (u16_t
)i
));
288 /* remember first empty entry */
290 } else if (state
!= ETHARP_STATE_EMPTY
) {
291 LWIP_ASSERT("state == ETHARP_STATE_PENDING || state == ETHARP_STATE_STABLE",
292 state
== ETHARP_STATE_PENDING
|| state
== ETHARP_STATE_STABLE
);
293 /* if given, does IP address match IP address in ARP entry? */
294 if (ipaddr
&& ip_addr_cmp(ipaddr
, &arp_table
[i
].ipaddr
)) {
295 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("find_entry: found matching entry %"U16_F
"\n", (u16_t
)i
));
296 /* found exact IP address match, simply bail out */
300 if (state
== ETHARP_STATE_PENDING
) {
301 /* pending with queued packets? */
302 if (arp_table
[i
].q
!= NULL
) {
303 if (arp_table
[i
].ctime
>= age_queue
) {
305 age_queue
= arp_table
[i
].ctime
;
308 /* pending without queued packets? */
310 if (arp_table
[i
].ctime
>= age_pending
) {
312 age_pending
= arp_table
[i
].ctime
;
316 } else if (state
== ETHARP_STATE_STABLE
) {
317 #if ETHARP_SUPPORT_STATIC_ENTRIES
318 /* don't record old_stable for static entries since they never expire */
319 if (arp_table
[i
].static_entry
== 0)
320 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
322 /* remember entry with oldest stable entry in oldest, its age in maxtime */
323 if (arp_table
[i
].ctime
>= age_stable
) {
325 age_stable
= arp_table
[i
].ctime
;
331 /* { we have no match } => try to create a new entry */
333 /* don't create new entry, only search? */
334 if (((flags
& ETHARP_FLAG_FIND_ONLY
) != 0) ||
335 /* or no empty entry found and not allowed to recycle? */
336 ((empty
== ARP_TABLE_SIZE
) && ((flags
& ETHARP_FLAG_TRY_HARD
) == 0))) {
337 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("find_entry: no empty entry found and not allowed to recycle\n"));
338 return (s8_t
)ERR_MEM
;
341 /* b) choose the least destructive entry to recycle:
343 * 2) oldest stable entry
344 * 3) oldest pending entry without queued packets
345 * 4) oldest pending entry with queued packets
347 * { ETHARP_FLAG_TRY_HARD is set at this point }
350 /* 1) empty entry available? */
351 if (empty
< ARP_TABLE_SIZE
) {
353 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("find_entry: selecting empty entry %"U16_F
"\n", (u16_t
)i
));
355 /* 2) found recyclable stable entry? */
356 if (old_stable
< ARP_TABLE_SIZE
) {
357 /* recycle oldest stable*/
359 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("find_entry: selecting oldest stable entry %"U16_F
"\n", (u16_t
)i
));
360 /* no queued packets should exist on stable entries */
361 LWIP_ASSERT("arp_table[i].q == NULL", arp_table
[i
].q
== NULL
);
362 /* 3) found recyclable pending entry without queued packets? */
363 } else if (old_pending
< ARP_TABLE_SIZE
) {
364 /* recycle oldest pending */
366 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("find_entry: selecting oldest pending entry %"U16_F
" (without queue)\n", (u16_t
)i
));
367 /* 4) found recyclable pending entry with queued packets? */
368 } else if (old_queue
< ARP_TABLE_SIZE
) {
369 /* recycle oldest pending (queued packets are free in free_entry) */
371 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
)));
372 /* no empty or recyclable entries found */
374 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("find_entry: no empty or recyclable entries found\n"));
375 return (s8_t
)ERR_MEM
;
378 /* { empty or recyclable entry found } */
379 LWIP_ASSERT("i < ARP_TABLE_SIZE", i
< ARP_TABLE_SIZE
);
383 LWIP_ASSERT("i < ARP_TABLE_SIZE", i
< ARP_TABLE_SIZE
);
384 LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
385 arp_table
[i
].state
== ETHARP_STATE_EMPTY
);
387 /* IP address given? */
388 if (ipaddr
!= NULL
) {
390 ip_addr_copy(arp_table
[i
].ipaddr
, *ipaddr
);
392 arp_table
[i
].ctime
= 0;
393 #if ETHARP_SUPPORT_STATIC_ENTRIES
394 arp_table
[i
].static_entry
= 0;
395 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
400 * Send an IP packet on the network using netif->linkoutput
401 * The ethernet header is filled in before sending.
403 * @params netif the lwIP network interface on which to send the packet
404 * @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
405 * @params src the source MAC address to be copied into the ethernet header
406 * @params dst the destination MAC address to be copied into the ethernet header
407 * @return ERR_OK if the packet was sent, any other err_t on failure
410 etharp_send_ip(struct netif
*netif
, struct pbuf
*p
, struct eth_addr
*src
, struct eth_addr
*dst
)
412 struct eth_hdr
*ethhdr
= (struct eth_hdr
*)p
->payload
;
414 LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
415 (netif
->hwaddr_len
== ETHARP_HWADDR_LEN
));
416 ETHADDR32_COPY(ðhdr
->dest
, dst
);
417 ETHADDR16_COPY(ðhdr
->src
, src
);
418 ethhdr
->type
= PP_HTONS(ETHTYPE_IP
);
419 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_send_ip: sending packet %p\n", (void *)p
));
420 /* send the packet */
421 return netif
->linkoutput(netif
, p
);
425 * Update (or insert) a IP/MAC address pair in the ARP cache.
427 * If a pending entry is resolved, any queued packets will be sent
430 * @param netif netif related to this entry (used for NETIF_ADDRHINT)
431 * @param ipaddr IP address of the inserted ARP entry.
432 * @param ethaddr Ethernet address of the inserted ARP entry.
433 * @param flags @see definition of ETHARP_FLAG_*
436 * - ERR_OK Succesfully updated ARP cache.
437 * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.
438 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
443 update_arp_entry(struct netif
*netif
, ip_addr_t
*ipaddr
, struct eth_addr
*ethaddr
, u8_t flags
)
446 LWIP_ASSERT("netif->hwaddr_len == ETHARP_HWADDR_LEN", netif
->hwaddr_len
== ETHARP_HWADDR_LEN
);
447 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",
448 ip4_addr1_16(ipaddr
), ip4_addr2_16(ipaddr
), ip4_addr3_16(ipaddr
), ip4_addr4_16(ipaddr
),
449 ethaddr
->addr
[0], ethaddr
->addr
[1], ethaddr
->addr
[2],
450 ethaddr
->addr
[3], ethaddr
->addr
[4], ethaddr
->addr
[5]));
451 /* non-unicast address? */
452 if (ip_addr_isany(ipaddr
) ||
453 ip_addr_isbroadcast(ipaddr
, netif
) ||
454 ip_addr_ismulticast(ipaddr
)) {
455 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
458 /* find or create ARP entry */
459 i
= find_entry(ipaddr
, flags
);
460 /* bail out if no entry could be found */
465 #if ETHARP_SUPPORT_STATIC_ENTRIES
466 if (flags
& ETHARP_FLAG_STATIC_ENTRY
) {
467 /* record static type */
468 arp_table
[i
].static_entry
= 1;
470 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
473 arp_table
[i
].state
= ETHARP_STATE_STABLE
;
476 /* record network interface */
477 arp_table
[i
].netif
= netif
;
478 #endif /* LWIP_SNMP */
479 /* insert in SNMP ARP index tree */
480 snmp_insert_arpidx_tree(netif
, &arp_table
[i
].ipaddr
);
482 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("update_arp_entry: updating stable entry %"S16_F
"\n", (s16_t
)i
));
484 ETHADDR32_COPY(&arp_table
[i
].ethaddr
, ethaddr
);
485 /* reset time stamp */
486 arp_table
[i
].ctime
= 0;
487 /* this is where we will send out queued packets! */
489 while (arp_table
[i
].q
!= NULL
) {
491 /* remember remainder of queue */
492 struct etharp_q_entry
*q
= arp_table
[i
].q
;
493 /* pop first item off the queue */
494 arp_table
[i
].q
= q
->next
;
495 /* get the packet pointer */
497 /* now queue entry can be freed */
498 memp_free(MEMP_ARP_QUEUE
, q
);
499 #else /* ARP_QUEUEING */
500 if (arp_table
[i
].q
!= NULL
) {
501 struct pbuf
*p
= arp_table
[i
].q
;
502 arp_table
[i
].q
= NULL
;
503 #endif /* ARP_QUEUEING */
504 /* send the queued IP packet */
505 etharp_send_ip(netif
, p
, (struct eth_addr
*)(netif
->hwaddr
), ethaddr
);
506 /* free the queued IP packet */
512 #if ETHARP_SUPPORT_STATIC_ENTRIES
513 /** Add a new static entry to the ARP table. If an entry exists for the
514 * specified IP address, this entry is overwritten.
515 * If packets are queued for the specified IP address, they are sent out.
517 * @param ipaddr IP address for the new static entry
518 * @param ethaddr ethernet address for the new static entry
519 * @return @see return values of etharp_add_static_entry
522 etharp_add_static_entry(ip_addr_t
*ipaddr
, struct eth_addr
*ethaddr
)
525 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",
526 ip4_addr1_16(ipaddr
), ip4_addr2_16(ipaddr
), ip4_addr3_16(ipaddr
), ip4_addr4_16(ipaddr
),
527 ethaddr
->addr
[0], ethaddr
->addr
[1], ethaddr
->addr
[2],
528 ethaddr
->addr
[3], ethaddr
->addr
[4], ethaddr
->addr
[5]));
530 netif
= ip_route(ipaddr
);
535 return update_arp_entry(netif
, ipaddr
, ethaddr
, ETHARP_FLAG_TRY_HARD
| ETHARP_FLAG_STATIC_ENTRY
);
538 /** Remove a static entry from the ARP table previously added with a call to
539 * etharp_add_static_entry.
541 * @param ipaddr IP address of the static entry to remove
542 * @return ERR_OK: entry removed
543 * ERR_MEM: entry wasn't found
544 * ERR_ARG: entry wasn't a static entry but a dynamic one
547 etharp_remove_static_entry(ip_addr_t
*ipaddr
)
550 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_remove_static_entry: %"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
"\n",
551 ip4_addr1_16(ipaddr
), ip4_addr2_16(ipaddr
), ip4_addr3_16(ipaddr
), ip4_addr4_16(ipaddr
)));
553 /* find or create ARP entry */
554 i
= find_entry(ipaddr
, ETHARP_FLAG_FIND_ONLY
);
555 /* bail out if no entry could be found */
560 if ((arp_table
[i
].state
!= ETHARP_STATE_STABLE
) ||
561 (arp_table
[i
].static_entry
== 0)) {
562 /* entry wasn't a static entry, cannot remove it */
565 /* entry found, free it */
569 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
572 * Finds (stable) ethernet/IP address pair from ARP table
573 * using interface and IP address index.
574 * @note the addresses in the ARP table are in network order!
576 * @param netif points to interface index
577 * @param ipaddr points to the (network order) IP address index
578 * @param eth_ret points to return pointer
579 * @param ip_ret points to return pointer
580 * @return table index if found, -1 otherwise
583 etharp_find_addr(struct netif
*netif
, ip_addr_t
*ipaddr
,
584 struct eth_addr
**eth_ret
, ip_addr_t
**ip_ret
)
588 LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
589 eth_ret
!= NULL
&& ip_ret
!= NULL
);
591 LWIP_UNUSED_ARG(netif
);
593 i
= find_entry(ipaddr
, ETHARP_FLAG_FIND_ONLY
);
594 if((i
>= 0) && arp_table
[i
].state
== ETHARP_STATE_STABLE
) {
595 *eth_ret
= &arp_table
[i
].ethaddr
;
596 *ip_ret
= &arp_table
[i
].ipaddr
;
602 #if ETHARP_TRUST_IP_MAC
604 * Updates the ARP table using the given IP packet.
606 * Uses the incoming IP packet's source address to update the
607 * ARP cache for the local network. The function does not alter
608 * or free the packet. This function must be called before the
609 * packet p is passed to the IP layer.
611 * @param netif The lwIP network interface on which the IP packet pbuf arrived.
612 * @param p The IP packet that arrived on netif.
619 etharp_ip_input(struct netif
*netif
, struct pbuf
*p
)
621 struct eth_hdr
*ethhdr
;
622 struct ip_hdr
*iphdr
;
624 LWIP_ERROR("netif != NULL", (netif
!= NULL
), return;);
626 /* Only insert an entry if the source IP address of the
627 incoming IP packet comes from a host on the local network. */
628 ethhdr
= (struct eth_hdr
*)p
->payload
;
629 iphdr
= (struct ip_hdr
*)((u8_t
*)ethhdr
+ SIZEOF_ETH_HDR
);
630 #if ETHARP_SUPPORT_VLAN
631 if (ethhdr
->type
== PP_HTONS(ETHTYPE_VLAN
)) {
632 iphdr
= (struct ip_hdr
*)((u8_t
*)ethhdr
+ SIZEOF_ETH_HDR
+ SIZEOF_VLAN_HDR
);
634 #endif /* ETHARP_SUPPORT_VLAN */
636 ip_addr_copy(iphdr_src
, iphdr
->src
);
638 /* source is not on the local network? */
639 if (!ip_addr_netcmp(&iphdr_src
, &(netif
->ip_addr
), &(netif
->netmask
))) {
644 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_ip_input: updating ETHARP table.\n"));
645 /* update the source IP address in the cache, if present */
646 /* @todo We could use ETHARP_FLAG_TRY_HARD if we think we are going to talk
647 * back soon (for example, if the destination IP address is ours. */
648 update_arp_entry(netif
, &iphdr_src
, &(ethhdr
->src
), ETHARP_FLAG_FIND_ONLY
);
650 #endif /* ETHARP_TRUST_IP_MAC */
653 * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
654 * send out queued IP packets. Updates cache with snooped address pairs.
656 * Should be called for incoming ARP packets. The pbuf in the argument
657 * is freed by this function.
659 * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
660 * @param ethaddr Ethernet address of netif.
661 * @param p The ARP packet that arrived on netif. Is freed by this function.
668 etharp_arp_input(struct netif
*netif
, struct eth_addr
*ethaddr
, struct pbuf
*p
)
670 struct etharp_hdr
*hdr
;
671 struct eth_hdr
*ethhdr
;
672 /* these are aligned properly, whereas the ARP header fields might not be */
673 ip_addr_t sipaddr
, dipaddr
;
676 const u8_t
* ethdst_hwaddr
;
677 #endif /* LWIP_AUTOIP */
679 LWIP_ERROR("netif != NULL", (netif
!= NULL
), return;);
681 /* drop short ARP packets: we have to check for p->len instead of p->tot_len here
682 since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */
683 if (p
->len
< SIZEOF_ETHARP_PACKET
) {
684 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_LEVEL_WARNING
,
685 ("etharp_arp_input: packet dropped, too short (%"S16_F
"/%"S16_F
")\n", p
->tot_len
,
686 (s16_t
)SIZEOF_ETHARP_PACKET
));
687 ETHARP_STATS_INC(etharp
.lenerr
);
688 ETHARP_STATS_INC(etharp
.drop
);
693 ethhdr
= (struct eth_hdr
*)p
->payload
;
694 hdr
= (struct etharp_hdr
*)((u8_t
*)ethhdr
+ SIZEOF_ETH_HDR
);
695 #if ETHARP_SUPPORT_VLAN
696 if (ethhdr
->type
== PP_HTONS(ETHTYPE_VLAN
)) {
697 hdr
= (struct etharp_hdr
*)(((u8_t
*)ethhdr
) + SIZEOF_ETH_HDR
+ SIZEOF_VLAN_HDR
);
699 #endif /* ETHARP_SUPPORT_VLAN */
701 /* RFC 826 "Packet Reception": */
702 if ((hdr
->hwtype
!= PP_HTONS(HWTYPE_ETHERNET
)) ||
703 (hdr
->hwlen
!= ETHARP_HWADDR_LEN
) ||
704 (hdr
->protolen
!= sizeof(ip_addr_t
)) ||
705 (hdr
->proto
!= PP_HTONS(ETHTYPE_IP
))) {
706 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_LEVEL_WARNING
,
707 ("etharp_arp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F
"/%"U16_F
"/%"U16_F
"/%"U16_F
")\n",
708 hdr
->hwtype
, hdr
->hwlen
, hdr
->proto
, hdr
->protolen
));
709 ETHARP_STATS_INC(etharp
.proterr
);
710 ETHARP_STATS_INC(etharp
.drop
);
714 ETHARP_STATS_INC(etharp
.recv
);
717 /* We have to check if a host already has configured our random
718 * created link local address and continously check if there is
719 * a host with this IP-address so we can detect collisions */
720 autoip_arp_reply(netif
, hdr
);
721 #endif /* LWIP_AUTOIP */
723 /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
724 * structure packing (not using structure copy which breaks strict-aliasing rules). */
725 IPADDR2_COPY(&sipaddr
, &hdr
->sipaddr
);
726 IPADDR2_COPY(&dipaddr
, &hdr
->dipaddr
);
728 /* this interface is not configured? */
729 if (ip_addr_isany(&netif
->ip_addr
)) {
732 /* ARP packet directed to us? */
733 for_us
= (u8_t
)ip_addr_cmp(&dipaddr
, &(netif
->ip_addr
));
736 /* ARP message directed to us?
737 -> add IP address in ARP cache; assume requester wants to talk to us,
738 can result in directly sending the queued packets for this host.
739 ARP message not directed to us?
740 -> update the source IP address in the cache, if present */
741 update_arp_entry(netif
, &sipaddr
, &(hdr
->shwaddr
),
742 for_us
? ETHARP_FLAG_TRY_HARD
: ETHARP_FLAG_FIND_ONLY
);
744 /* now act on the message itself */
745 switch (hdr
->opcode
) {
747 case PP_HTONS(ARP_REQUEST
):
748 /* ARP request. If it asked for our address, we send out a
749 * reply. In any case, we time-stamp any existing ARP entry,
750 * and possiby send out an IP packet that was queued on it. */
752 LWIP_DEBUGF (ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_arp_input: incoming ARP request\n"));
753 /* ARP request for our address? */
756 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_arp_input: replying to ARP request for our IP address\n"));
757 /* Re-use pbuf to send ARP reply.
758 Since we are re-using an existing pbuf, we can't call etharp_raw since
759 that would allocate a new pbuf. */
760 hdr
->opcode
= htons(ARP_REPLY
);
762 IPADDR2_COPY(&hdr
->dipaddr
, &hdr
->sipaddr
);
763 IPADDR2_COPY(&hdr
->sipaddr
, &netif
->ip_addr
);
765 LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
766 (netif
->hwaddr_len
== ETHARP_HWADDR_LEN
));
768 /* If we are using Link-Local, all ARP packets that contain a Link-Local
769 * 'sender IP address' MUST be sent using link-layer broadcast instead of
770 * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
771 ethdst_hwaddr
= ip_addr_islinklocal(&netif
->ip_addr
) ? (u8_t
*)(ethbroadcast
.addr
) : hdr
->shwaddr
.addr
;
772 #endif /* LWIP_AUTOIP */
774 ETHADDR16_COPY(&hdr
->dhwaddr
, &hdr
->shwaddr
);
776 ETHADDR16_COPY(ðhdr
->dest
, ethdst_hwaddr
);
777 #else /* LWIP_AUTOIP */
778 ETHADDR16_COPY(ðhdr
->dest
, &hdr
->shwaddr
);
779 #endif /* LWIP_AUTOIP */
780 ETHADDR16_COPY(&hdr
->shwaddr
, ethaddr
);
781 ETHADDR16_COPY(ðhdr
->src
, ethaddr
);
783 /* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header
784 are already correct, we tested that before */
786 /* return ARP reply */
787 netif
->linkoutput(netif
, p
);
788 /* we are not configured? */
789 } else if (ip_addr_isany(&netif
->ip_addr
)) {
790 /* { for_us == 0 and netif->ip_addr.addr == 0 } */
791 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
792 /* request was not directed to us */
794 /* { for_us == 0 and netif->ip_addr.addr != 0 } */
795 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_arp_input: ARP request was not for us.\n"));
798 case PP_HTONS(ARP_REPLY
):
799 /* ARP reply. We already updated the ARP cache earlier. */
800 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_arp_input: incoming ARP reply\n"));
801 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
802 /* DHCP wants to know about ARP replies from any host with an
803 * IP address also offered to us by the DHCP server. We do not
804 * want to take a duplicate IP address on a single network.
805 * @todo How should we handle redundant (fail-over) interfaces? */
806 dhcp_arp_reply(netif
, &sipaddr
);
807 #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */
810 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_arp_input: ARP unknown opcode type %"S16_F
"\n", htons(hdr
->opcode
)));
811 ETHARP_STATS_INC(etharp
.err
);
814 /* free ARP packet */
819 * Resolve and fill-in Ethernet address header for outgoing IP packet.
821 * For IP multicast and broadcast, corresponding Ethernet addresses
822 * are selected and the packet is transmitted on the link.
824 * For unicast addresses, the packet is submitted to etharp_query(). In
825 * case the IP address is outside the local network, the IP address of
826 * the gateway is used.
828 * @param netif The lwIP network interface which the IP packet will be sent on.
829 * @param q The pbuf(s) containing the IP packet to be sent.
830 * @param ipaddr The IP address of the packet destination.
833 * - ERR_RTE No route to destination (no gateway to external networks),
834 * or the return type of either etharp_query() or etharp_send_ip().
837 etharp_output(struct netif
*netif
, struct pbuf
*q
, ip_addr_t
*ipaddr
)
839 struct eth_addr
*dest
, mcastaddr
;
841 /* make room for Ethernet header - should not fail */
842 if (pbuf_header(q
, sizeof(struct eth_hdr
)) != 0) {
844 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_LEVEL_SERIOUS
,
845 ("etharp_output: could not allocate room for header.\n"));
846 LINK_STATS_INC(link
.lenerr
);
850 /* assume unresolved Ethernet address */
852 /* Determine on destination hardware address. Broadcasts and multicasts
853 * are special, other IP addresses are looked up in the ARP table. */
855 /* broadcast destination IP address? */
856 if (ip_addr_isbroadcast(ipaddr
, netif
)) {
857 /* broadcast on Ethernet also */
858 dest
= (struct eth_addr
*)ðbroadcast
;
859 /* multicast destination IP address? */
860 } else if (ip_addr_ismulticast(ipaddr
)) {
861 /* Hash IP multicast address to MAC address.*/
862 mcastaddr
.addr
[0] = 0x01;
863 mcastaddr
.addr
[1] = 0x00;
864 mcastaddr
.addr
[2] = 0x5e;
865 mcastaddr
.addr
[3] = ip4_addr2(ipaddr
) & 0x7f;
866 mcastaddr
.addr
[4] = ip4_addr3(ipaddr
);
867 mcastaddr
.addr
[5] = ip4_addr4(ipaddr
);
868 /* destination Ethernet address is multicast */
870 /* unicast destination IP address? */
872 /* outside local network? */
873 if (!ip_addr_netcmp(ipaddr
, &(netif
->ip_addr
), &(netif
->netmask
)) &&
874 !ip_addr_islinklocal(ipaddr
)) {
876 struct ip_hdr
*iphdr
= (struct ip_hdr
*)((u8_t
*)q
->payload
+
877 sizeof(struct eth_hdr
));
878 /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with
879 a link-local source address must always be "directly to its destination
880 on the same physical link. The host MUST NOT send the packet to any
881 router for forwarding". */
882 if (!ip_addr_islinklocal(&iphdr
->src
))
883 #endif /* LWIP_AUTOIP */
885 /* interface has default gateway? */
886 if (!ip_addr_isany(&netif
->gw
)) {
887 /* send to hardware address of default gateway IP address */
888 ipaddr
= &(netif
->gw
);
889 /* no default gateway available */
891 /* no route to destination error (default gateway missing) */
896 #if LWIP_NETIF_HWADDRHINT
897 if (netif
->addr_hint
!= NULL
) {
898 /* per-pcb cached entry was given */
899 u8_t etharp_cached_entry
= *(netif
->addr_hint
);
900 if (etharp_cached_entry
< ARP_TABLE_SIZE
) {
901 #endif /* LWIP_NETIF_HWADDRHINT */
902 if ((arp_table
[etharp_cached_entry
].state
== ETHARP_STATE_STABLE
) &&
903 (ip_addr_cmp(ipaddr
, &arp_table
[etharp_cached_entry
].ipaddr
))) {
904 /* the per-pcb-cached entry is stable and the right one! */
905 ETHARP_STATS_INC(etharp
.cachehit
);
906 return etharp_send_ip(netif
, q
, (struct eth_addr
*)(netif
->hwaddr
),
907 &arp_table
[etharp_cached_entry
].ethaddr
);
909 #if LWIP_NETIF_HWADDRHINT
912 #endif /* LWIP_NETIF_HWADDRHINT */
913 /* queue on destination Ethernet address belonging to ipaddr */
914 return etharp_query(netif
, ipaddr
, q
);
917 /* continuation for multicast/broadcast destinations */
918 /* obtain source Ethernet address of the given interface */
919 /* send packet directly on the link */
920 return etharp_send_ip(netif
, q
, (struct eth_addr
*)(netif
->hwaddr
), dest
);
924 * Send an ARP request for the given IP address and/or queue a packet.
926 * If the IP address was not yet in the cache, a pending ARP cache entry
927 * is added and an ARP request is sent for the given address. The packet
928 * is queued on this entry.
930 * If the IP address was already pending in the cache, a new ARP request
931 * is sent for the given address. The packet is queued on this entry.
933 * If the IP address was already stable in the cache, and a packet is
934 * given, it is directly sent and no ARP request is sent out.
936 * If the IP address was already stable in the cache, and no packet is
937 * given, an ARP request is sent out.
939 * @param netif The lwIP network interface on which ipaddr
940 * must be queried for.
941 * @param ipaddr The IP address to be resolved.
942 * @param q If non-NULL, a pbuf that must be delivered to the IP address.
943 * q is not freed by this function.
945 * @note q must only be ONE packet, not a packet queue!
948 * - ERR_BUF Could not make room for Ethernet header.
949 * - ERR_MEM Hardware address unknown, and no more ARP entries available
950 * to query for address or queue the packet.
951 * - ERR_MEM Could not queue packet due to memory shortage.
952 * - ERR_RTE No route to destination (no gateway to external networks).
953 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
957 etharp_query(struct netif
*netif
, ip_addr_t
*ipaddr
, struct pbuf
*q
)
959 struct eth_addr
* srcaddr
= (struct eth_addr
*)netif
->hwaddr
;
960 err_t result
= ERR_MEM
;
961 s8_t i
; /* ARP entry index */
963 /* non-unicast address? */
964 if (ip_addr_isbroadcast(ipaddr
, netif
) ||
965 ip_addr_ismulticast(ipaddr
) ||
966 ip_addr_isany(ipaddr
)) {
967 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
971 /* find entry in ARP cache, ask to create entry if queueing packet */
972 i
= find_entry(ipaddr
, ETHARP_FLAG_TRY_HARD
);
974 /* could not find or create entry? */
976 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_query: could not create ARP entry\n"));
978 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_query: packet dropped\n"));
979 ETHARP_STATS_INC(etharp
.memerr
);
984 /* mark a fresh entry as pending (we just sent a request) */
985 if (arp_table
[i
].state
== ETHARP_STATE_EMPTY
) {
986 arp_table
[i
].state
= ETHARP_STATE_PENDING
;
989 /* { i is either a STABLE or (new or existing) PENDING entry } */
990 LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
991 ((arp_table
[i
].state
== ETHARP_STATE_PENDING
) ||
992 (arp_table
[i
].state
== ETHARP_STATE_STABLE
)));
994 /* do we have a pending entry? or an implicit query request? */
995 if ((arp_table
[i
].state
== ETHARP_STATE_PENDING
) || (q
== NULL
)) {
996 /* try to resolve it; send out ARP request */
997 result
= etharp_request(netif
, ipaddr
);
998 if (result
!= ERR_OK
) {
999 /* ARP request couldn't be sent */
1000 /* We don't re-send arp request in etharp_tmr, but we still queue packets,
1001 since this failure could be temporary, and the next packet calling
1002 etharp_query again could lead to sending the queued packets. */
1010 LWIP_ASSERT("q != NULL", q
!= NULL
);
1012 if (arp_table
[i
].state
== ETHARP_STATE_STABLE
) {
1013 /* we have a valid IP->Ethernet address mapping */
1014 ETHARP_SET_HINT(netif
, i
);
1015 /* send the packet */
1016 result
= etharp_send_ip(netif
, q
, srcaddr
, &(arp_table
[i
].ethaddr
));
1017 /* pending entry? (either just created or already pending */
1018 } else if (arp_table
[i
].state
== ETHARP_STATE_PENDING
) {
1019 /* entry is still pending, queue the given packet 'q' */
1021 int copy_needed
= 0;
1022 /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
1023 * to copy the whole queue into a new PBUF_RAM (see bug #11400)
1024 * PBUF_ROMs can be left as they are, since ROM must not get changed. */
1027 LWIP_ASSERT("no packet queues allowed!", (p
->len
!= p
->tot_len
) || (p
->next
== 0));
1028 if(p
->type
!= PBUF_ROM
) {
1035 /* copy the whole packet into new pbufs */
1036 p
= pbuf_alloc(PBUF_RAW
, p
->tot_len
, PBUF_RAM
);
1038 if (pbuf_copy(p
, q
) != ERR_OK
) {
1044 /* referencing the old pbuf is enough */
1048 /* packet could be taken over? */
1050 /* queue packet ... */
1052 struct etharp_q_entry
*new_entry
;
1053 /* allocate a new arp queue entry */
1054 new_entry
= (struct etharp_q_entry
*)memp_malloc(MEMP_ARP_QUEUE
);
1055 if (new_entry
!= NULL
) {
1056 new_entry
->next
= 0;
1058 if(arp_table
[i
].q
!= NULL
) {
1059 /* queue was already existent, append the new entry to the end */
1060 struct etharp_q_entry
*r
;
1062 while (r
->next
!= NULL
) {
1065 r
->next
= new_entry
;
1067 /* queue did not exist, first item in queue */
1068 arp_table
[i
].q
= new_entry
;
1070 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_query: queued packet %p on ARP entry %"S16_F
"\n", (void *)q
, (s16_t
)i
));
1073 /* the pool MEMP_ARP_QUEUE is empty */
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
));
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
);
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 */
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
));
1098 * Send a raw ARP packet (opcode and all addresses can be modified)
1100 * @param netif the lwip network interface on which to send the ARP packet
1101 * @param ethsrc_addr the source MAC address for the ethernet header
1102 * @param ethdst_addr the destination MAC address for the ethernet header
1103 * @param hwsrc_addr the source MAC address for the ARP protocol header
1104 * @param ipsrc_addr the source IP address for the ARP protocol header
1105 * @param hwdst_addr the destination MAC address for the ARP protocol header
1106 * @param ipdst_addr the destination IP address for the ARP protocol header
1107 * @param opcode the type of the ARP packet
1108 * @return ERR_OK if the ARP packet has been sent
1109 * ERR_MEM if the ARP packet couldn't be allocated
1110 * any other err_t on failure
1114 #endif /* LWIP_AUTOIP */
1116 etharp_raw(struct netif
*netif
, const struct eth_addr
*ethsrc_addr
,
1117 const struct eth_addr
*ethdst_addr
,
1118 const struct eth_addr
*hwsrc_addr
, const ip_addr_t
*ipsrc_addr
,
1119 const struct eth_addr
*hwdst_addr
, const ip_addr_t
*ipdst_addr
,
1123 err_t result
= ERR_OK
;
1124 struct eth_hdr
*ethhdr
;
1125 struct etharp_hdr
*hdr
;
1127 const u8_t
* ethdst_hwaddr
;
1128 #endif /* LWIP_AUTOIP */
1130 /* allocate a pbuf for the outgoing ARP request packet */
1131 p
= pbuf_alloc(PBUF_RAW
, SIZEOF_ETHARP_PACKET
, PBUF_RAM
);
1132 /* could allocate a pbuf for an ARP request? */
1134 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_LEVEL_SERIOUS
,
1135 ("etharp_raw: could not allocate pbuf for ARP request.\n"));
1136 ETHARP_STATS_INC(etharp
.memerr
);
1139 LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
1140 (p
->len
>= SIZEOF_ETHARP_PACKET
));
1142 ethhdr
= (struct eth_hdr
*)p
->payload
;
1143 hdr
= (struct etharp_hdr
*)((u8_t
*)ethhdr
+ SIZEOF_ETH_HDR
);
1144 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_raw: sending raw ARP packet.\n"));
1145 hdr
->opcode
= htons(opcode
);
1147 LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
1148 (netif
->hwaddr_len
== ETHARP_HWADDR_LEN
));
1150 /* If we are using Link-Local, all ARP packets that contain a Link-Local
1151 * 'sender IP address' MUST be sent using link-layer broadcast instead of
1152 * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
1153 ethdst_hwaddr
= ip_addr_islinklocal(ipsrc_addr
) ? (u8_t
*)(ethbroadcast
.addr
) : ethdst_addr
->addr
;
1154 #endif /* LWIP_AUTOIP */
1155 /* Write the ARP MAC-Addresses */
1156 ETHADDR16_COPY(&hdr
->shwaddr
, hwsrc_addr
);
1157 ETHADDR16_COPY(&hdr
->dhwaddr
, hwdst_addr
);
1158 /* Write the Ethernet MAC-Addresses */
1160 ETHADDR16_COPY(ðhdr
->dest
, ethdst_hwaddr
);
1161 #else /* LWIP_AUTOIP */
1162 ETHADDR16_COPY(ðhdr
->dest
, ethdst_addr
);
1163 #endif /* LWIP_AUTOIP */
1164 ETHADDR16_COPY(ðhdr
->src
, ethsrc_addr
);
1165 /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
1166 * structure packing. */
1167 IPADDR2_COPY(&hdr
->sipaddr
, ipsrc_addr
);
1168 IPADDR2_COPY(&hdr
->dipaddr
, ipdst_addr
);
1170 hdr
->hwtype
= PP_HTONS(HWTYPE_ETHERNET
);
1171 hdr
->proto
= PP_HTONS(ETHTYPE_IP
);
1172 /* set hwlen and protolen */
1173 hdr
->hwlen
= ETHARP_HWADDR_LEN
;
1174 hdr
->protolen
= sizeof(ip_addr_t
);
1176 ethhdr
->type
= PP_HTONS(ETHTYPE_ARP
);
1177 /* send ARP query */
1178 result
= netif
->linkoutput(netif
, p
);
1179 ETHARP_STATS_INC(etharp
.xmit
);
1180 /* free ARP query packet */
1183 /* could not allocate pbuf for ARP request */
1189 * Send an ARP request packet asking for ipaddr.
1191 * @param netif the lwip network interface on which to send the request
1192 * @param ipaddr the IP address for which to ask
1193 * @return ERR_OK if the request has been sent
1194 * ERR_MEM if the ARP packet couldn't be allocated
1195 * any other err_t on failure
1198 etharp_request(struct netif
*netif
, ip_addr_t
*ipaddr
)
1200 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
, ("etharp_request: sending ARP request.\n"));
1201 return etharp_raw(netif
, (struct eth_addr
*)netif
->hwaddr
, ðbroadcast
,
1202 (struct eth_addr
*)netif
->hwaddr
, &netif
->ip_addr
, ðzero
,
1203 ipaddr
, ARP_REQUEST
);
1205 #endif /* LWIP_ARP */
1208 * Process received ethernet frames. Using this function instead of directly
1209 * calling ip_input and passing ARP frames through etharp in ethernetif_input,
1210 * the ARP cache is protected from concurrent access.
1212 * @param p the recevied packet, p->payload pointing to the ethernet header
1213 * @param netif the network interface on which the packet was received
1216 ethernet_input(struct pbuf
*p
, struct netif
*netif
)
1218 struct eth_hdr
* ethhdr
;
1220 s16_t ip_hdr_offset
= SIZEOF_ETH_HDR
;
1222 if (p
->len
<= SIZEOF_ETH_HDR
) {
1223 /* a packet with only an ethernet header (or less) is not valid for us */
1224 ETHARP_STATS_INC(etharp
.proterr
);
1225 ETHARP_STATS_INC(etharp
.drop
);
1226 goto free_and_return
;
1229 /* points to packet payload, which starts with an Ethernet header */
1230 ethhdr
= (struct eth_hdr
*)p
->payload
;
1231 LWIP_DEBUGF(ETHARP_DEBUG
| LWIP_DBG_TRACE
,
1232 ("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",
1233 (unsigned)ethhdr
->dest
.addr
[0], (unsigned)ethhdr
->dest
.addr
[1], (unsigned)ethhdr
->dest
.addr
[2],
1234 (unsigned)ethhdr
->dest
.addr
[3], (unsigned)ethhdr
->dest
.addr
[4], (unsigned)ethhdr
->dest
.addr
[5],
1235 (unsigned)ethhdr
->src
.addr
[0], (unsigned)ethhdr
->src
.addr
[1], (unsigned)ethhdr
->src
.addr
[2],
1236 (unsigned)ethhdr
->src
.addr
[3], (unsigned)ethhdr
->src
.addr
[4], (unsigned)ethhdr
->src
.addr
[5],
1237 (unsigned)htons(ethhdr
->type
)));
1239 type
= ethhdr
->type
;
1240 #if ETHARP_SUPPORT_VLAN
1241 if (type
== PP_HTONS(ETHTYPE_VLAN
)) {
1242 struct eth_vlan_hdr
*vlan
= (struct eth_vlan_hdr
*)(((char*)ethhdr
) + SIZEOF_ETH_HDR
);
1243 if (p
->len
<= SIZEOF_ETH_HDR
+ SIZEOF_VLAN_HDR
) {
1244 /* a packet with only an ethernet/vlan header (or less) is not valid for us */
1245 ETHARP_STATS_INC(etharp
.proterr
);
1246 ETHARP_STATS_INC(etharp
.drop
);
1247 goto free_and_return
;
1249 #ifdef ETHARP_VLAN_CHECK /* if not, allow all VLANs */
1250 if (VLAN_ID(vlan
) != ETHARP_VLAN_CHECK
) {
1251 /* silently ignore this packet: not for our VLAN */
1255 #endif /* ETHARP_VLAN_CHECK */
1257 ip_hdr_offset
= SIZEOF_ETH_HDR
+ SIZEOF_VLAN_HDR
;
1259 #endif /* ETHARP_SUPPORT_VLAN */
1261 #if LWIP_ARP_FILTER_NETIF
1262 netif
= LWIP_ARP_FILTER_NETIF_FN(p
, netif
, htons(type
));
1263 #endif /* LWIP_ARP_FILTER_NETIF*/
1268 case PP_HTONS(ETHTYPE_IP
):
1269 if (!(netif
->flags
& NETIF_FLAG_ETHARP
)) {
1270 goto free_and_return
;
1272 #if ETHARP_TRUST_IP_MAC
1273 /* update ARP table */
1274 etharp_ip_input(netif
, p
);
1275 #endif /* ETHARP_TRUST_IP_MAC */
1276 /* skip Ethernet header */
1277 if(pbuf_header(p
, -ip_hdr_offset
)) {
1278 LWIP_ASSERT("Can't move over header in packet", 0);
1279 goto free_and_return
;
1281 /* pass to IP layer */
1286 case PP_HTONS(ETHTYPE_ARP
):
1287 if (!(netif
->flags
& NETIF_FLAG_ETHARP
)) {
1288 goto free_and_return
;
1290 /* pass p to ARP module */
1291 etharp_arp_input(netif
, (struct eth_addr
*)(netif
->hwaddr
), p
);
1293 #endif /* LWIP_ARP */
1295 case PP_HTONS(ETHTYPE_PPPOEDISC
): /* PPP Over Ethernet Discovery Stage */
1296 pppoe_disc_input(netif
, p
);
1299 case PP_HTONS(ETHTYPE_PPPOE
): /* PPP Over Ethernet Session Stage */
1300 pppoe_data_input(netif
, p
);
1302 #endif /* PPPOE_SUPPORT */
1305 ETHARP_STATS_INC(etharp
.proterr
);
1306 ETHARP_STATS_INC(etharp
.drop
);
1307 goto free_and_return
;
1310 /* This means the pbuf is freed or consumed,
1311 so the caller doesn't have to free it again */
1318 #endif /* LWIP_ARP || LWIP_ETHERNET */