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_query(our_netif, its_ip_addr, NULL) upon
16 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
17 * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
18 * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without modification,
22 * are permitted provided that the following conditions are met:
24 * 1. Redistributions of source code must retain the above copyright notice,
25 * this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright notice,
27 * this list of conditions and the following disclaimer in the documentation
28 * and/or other materials provided with the distribution.
29 * 3. The name of the author may not be used to endorse or promote products
30 * derived from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
35 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
43 * This file is part of the lwIP TCP/IP stack.
48 #include "lwip/inet.h"
49 #include "netif/etharp.h"
51 #include "lwip/stats.h"
53 /* ARP needs to inform DHCP of any ARP replies? */
54 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
55 # include "lwip/dhcp.h"
58 /** the time an ARP entry stays valid after its last update,
59 * (240 * 5) seconds = 20 minutes.
61 #define ARP_MAXAGE 240
62 /** the time an ARP entry stays pending after first request,
63 * (2 * 5) seconds = 10 seconds.
65 * @internal Keep this number at least 2, otherwise it might
66 * run out instantly if the timeout occurs directly after a request.
68 #define ARP_MAXPENDING 2
70 #define HWTYPE_ETHERNET 1
72 /** ARP message types */
76 #define ARPH_HWLEN(hdr) (ntohs((hdr)->_hwlen_protolen) >> 8)
77 #define ARPH_PROTOLEN(hdr) (ntohs((hdr)->_hwlen_protolen) & 0xff)
79 #define ARPH_HWLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons(ARPH_PROTOLEN(hdr) | ((len) << 8))
80 #define ARPH_PROTOLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons((len) | (ARPH_HWLEN(hdr) << 8))
86 /** @internal transitional state used in etharp_tmr() for convenience*/
93 * Pointer to queue of pending outgoing packets on this ARP entry.
97 struct ip_addr ipaddr
;
98 struct eth_addr ethaddr
;
99 enum etharp_state state
;
103 static const struct eth_addr ethbroadcast
= {{0xff,0xff,0xff,0xff,0xff,0xff}};
104 static struct etharp_entry arp_table
[ARP_TABLE_SIZE
];
107 * Try hard to create a new entry - we want the IP address to appear in
108 * the cache (even if this means removing an active entry or so). */
109 #define ETHARP_TRY_HARD 1
111 static s8_t
find_entry(struct ip_addr
*ipaddr
, u8_t flags
);
112 static err_t
update_arp_entry(struct netif
*netif
, struct ip_addr
*ipaddr
, struct eth_addr
*ethaddr
, u8_t flags
);
114 * Initializes ARP module.
120 /* clear ARP entries */
121 for(i
= 0; i
< ARP_TABLE_SIZE
; ++i
) {
122 arp_table
[i
].state
= ETHARP_STATE_EMPTY
;
124 arp_table
[i
].p
= NULL
;
126 arp_table
[i
].ctime
= 0;
131 * Clears expired entries in the ARP table.
133 * This function should be called every ETHARP_TMR_INTERVAL microseconds (5 seconds),
134 * in order to expire entries in the ARP table.
141 LWIP_DEBUGF(ETHARP_DEBUG
, ("etharp_timer\n"));
142 /* remove expired entries from the ARP table */
143 for (i
= 0; i
< ARP_TABLE_SIZE
; ++i
) {
144 arp_table
[i
].ctime
++;
146 if ((arp_table
[i
].state
== ETHARP_STATE_STABLE
) &&
147 /* entry has become old? */
148 (arp_table
[i
].ctime
>= ARP_MAXAGE
)) {
149 LWIP_DEBUGF(ETHARP_DEBUG
, ("etharp_timer: expired stable entry %"U16_F
".\n", (u16_t
)i
));
150 arp_table
[i
].state
= ETHARP_STATE_EXPIRED
;
152 } else if (arp_table
[i
].state
== ETHARP_STATE_PENDING
) {
153 /* entry unresolved/pending for too long? */
154 if (arp_table
[i
].ctime
>= ARP_MAXPENDING
) {
155 LWIP_DEBUGF(ETHARP_DEBUG
, ("etharp_timer: expired pending entry %"U16_F
".\n", (u16_t
)i
));
156 arp_table
[i
].state
= ETHARP_STATE_EXPIRED
;
158 } else if (arp_table
[i
].p
!= NULL
) {
159 /* resend an ARP query here */
163 /* clean up entries that have just been expired */
164 if (arp_table
[i
].state
== ETHARP_STATE_EXPIRED
) {
166 /* and empty packet queue */
167 if (arp_table
[i
].p
!= 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
].p
)));
170 pbuf_free(arp_table
[i
].p
);
171 arp_table
[i
].p
= NULL
;
174 /* recycle entry for re-use */
175 arp_table
[i
].state
= ETHARP_STATE_EMPTY
;
181 * Search the ARP table for a matching or new entry.
183 * If an IP address is given, return a pending or stable ARP entry that matches
184 * the address. If no match is found, create a new entry with this address set,
185 * but in state ETHARP_EMPTY. The caller must check and possibly change the
186 * state of the returned entry.
188 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
190 * In all cases, attempt to create new entries from an empty entry. If no
191 * empty entries are available and ETHARP_TRY_HARD flag is set, recycle
192 * old entries. Heuristic choose the least important entry for recycling.
194 * @param ipaddr IP address to find in ARP cache, or to add if not found.
196 * - ETHARP_TRY_HARD: Try hard to create a entry by allowing recycling of
197 * active (stable or pending) entries.
199 * @return The ARP entry index that matched or is created, ERR_MEM if no
200 * entry is found or could be recycled.
202 static s8_t
find_entry(struct ip_addr
*ipaddr
, u8_t flags
)
204 s8_t old_pending
= ARP_TABLE_SIZE
, old_stable
= ARP_TABLE_SIZE
;
205 s8_t empty
= ARP_TABLE_SIZE
;
206 u8_t i
= 0, age_pending
= 0, age_stable
= 0;
208 /* oldest entry with packets on queue */
209 s8_t old_queue
= ARP_TABLE_SIZE
;
215 * a) do a search through the cache, remember candidates
216 * b) select candidate entry
217 * c) create new entry
220 /* a) in a single search sweep, do all of this
221 * 1) remember the first empty entry (if any)
222 * 2) remember the oldest stable entry (if any)
223 * 3) remember the oldest pending entry without queued packets (if any)
224 * 4) remember the oldest pending entry with queued packets (if any)
225 * 5) search for a matching IP entry, either pending or stable
226 * until 5 matches, or all entries are searched for.
229 for (i
= 0; i
< ARP_TABLE_SIZE
; ++i
) {
230 /* no empty entry found yet and now we do find one? */
231 if ((empty
== ARP_TABLE_SIZE
) && (arp_table
[i
].state
== ETHARP_STATE_EMPTY
)) {
232 LWIP_DEBUGF(ETHARP_DEBUG
, ("find_entry: found empty entry %"U16_F
"\n", (u16_t
)i
));
233 /* remember first empty entry */
237 else if (arp_table
[i
].state
== ETHARP_STATE_PENDING
) {
238 /* if given, does IP address match IP address in ARP entry? */
239 if (ipaddr
&& ip_addr_cmp(ipaddr
, &arp_table
[i
].ipaddr
)) {
240 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("find_entry: found matching pending entry %"U16_F
"\n", (u16_t
)i
));
241 /* found exact IP address match, simply bail out */
244 /* pending with queued packets? */
245 } else if (arp_table
[i
].p
!= NULL
) {
246 if (arp_table
[i
].ctime
>= age_queue
) {
248 age_queue
= arp_table
[i
].ctime
;
251 /* pending without queued packets? */
253 if (arp_table
[i
].ctime
>= age_pending
) {
255 age_pending
= arp_table
[i
].ctime
;
260 else if (arp_table
[i
].state
== ETHARP_STATE_STABLE
) {
261 /* if given, does IP address match IP address in ARP entry? */
262 if (ipaddr
&& ip_addr_cmp(ipaddr
, &arp_table
[i
].ipaddr
)) {
263 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("find_entry: found matching stable entry %"U16_F
"\n", (u16_t
)i
));
264 /* found exact IP address match, simply bail out */
266 /* remember entry with oldest stable entry in oldest, its age in maxtime */
267 } else if (arp_table
[i
].ctime
>= age_stable
) {
269 age_stable
= arp_table
[i
].ctime
;
273 /* { we have no match } => try to create a new entry */
275 /* no empty entry found and not allowed to recycle? */
276 if ((empty
== ARP_TABLE_SIZE
) && ((flags
& ETHARP_TRY_HARD
) == 0))
278 return (s8_t
)ERR_MEM
;
281 /* b) choose the least destructive entry to recycle:
283 * 2) oldest stable entry
284 * 3) oldest pending entry without queued packets
285 * 4) oldest pending entry without queued packets
287 * { ETHARP_TRY_HARD is set at this point }
290 /* 1) empty entry available? */
291 if (empty
< ARP_TABLE_SIZE
) {
293 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("find_entry: selecting empty entry %"U16_F
"\n", (u16_t
)i
));
295 /* 2) found recyclable stable entry? */
296 else if (old_stable
< ARP_TABLE_SIZE
) {
297 /* recycle oldest stable*/
299 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("find_entry: selecting oldest stable entry %"U16_F
"\n", (u16_t
)i
));
301 /* no queued packets should exist on stable entries */
302 LWIP_ASSERT("arp_table[i].p == NULL", arp_table
[i
].p
== NULL
);
304 /* 3) found recyclable pending entry without queued packets? */
305 } else if (old_pending
< ARP_TABLE_SIZE
) {
306 /* recycle oldest pending */
308 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("find_entry: selecting oldest pending entry %"U16_F
" (without queue)\n", (u16_t
)i
));
310 /* 4) found recyclable pending entry with queued packets? */
311 } else if (old_queue
< ARP_TABLE_SIZE
) {
312 /* recycle oldest pending */
314 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("find_entry: selecting oldest pending entry %"U16_F
", freeing packet queue %p\n", (u16_t
)i
, (void *)(arp_table
[i
].p
)));
315 pbuf_free(arp_table
[i
].p
);
316 arp_table
[i
].p
= NULL
;
318 /* no empty or recyclable entries found */
320 return (s8_t
)ERR_MEM
;
323 /* { empty or recyclable entry found } */
324 LWIP_ASSERT("i < ARP_TABLE_SIZE", i
< ARP_TABLE_SIZE
);
326 /* recycle entry (no-op for an already empty entry) */
327 arp_table
[i
].state
= ETHARP_STATE_EMPTY
;
329 /* IP address given? */
330 if (ipaddr
!= NULL
) {
332 ip_addr_set(&arp_table
[i
].ipaddr
, ipaddr
);
334 arp_table
[i
].ctime
= 0;
339 * Update (or insert) a IP/MAC address pair in the ARP cache.
341 * If a pending entry is resolved, any queued packets will be sent
344 * @param ipaddr IP address of the inserted ARP entry.
345 * @param ethaddr Ethernet address of the inserted ARP entry.
346 * @param flags Defines behaviour:
347 * - ETHARP_TRY_HARD Allows ARP to insert this as a new item. If not specified,
348 * only existing ARP entries will be updated.
351 * - ERR_OK Succesfully updated ARP cache.
352 * - ERR_MEM If we could not add a new ARP entry when ETHARP_TRY_HARD was set.
353 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
358 update_arp_entry(struct netif
*netif
, struct ip_addr
*ipaddr
, struct eth_addr
*ethaddr
, u8_t flags
)
361 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
| 3, ("update_arp_entry()\n"));
362 LWIP_ASSERT("netif->hwaddr_len != 0", netif
->hwaddr_len
!= 0);
363 LWIP_DEBUGF(ETHARP_DEBUG
| 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",
364 ip4_addr1(ipaddr
), ip4_addr2(ipaddr
), ip4_addr3(ipaddr
), ip4_addr4(ipaddr
),
365 ethaddr
->addr
[0], ethaddr
->addr
[1], ethaddr
->addr
[2],
366 ethaddr
->addr
[3], ethaddr
->addr
[4], ethaddr
->addr
[5]));
367 /* non-unicast address? */
368 if (ip_addr_isany(ipaddr
) ||
369 ip_addr_isbroadcast(ipaddr
, netif
) ||
370 ip_addr_ismulticast(ipaddr
)) {
371 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
374 /* find or create ARP entry */
375 i
= find_entry(ipaddr
, flags
);
376 /* bail out if no entry could be found */
377 if (i
< 0) return (err_t
)i
;
380 arp_table
[i
].state
= ETHARP_STATE_STABLE
;
382 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("update_arp_entry: updating stable entry %"S16_F
"\n", (s16_t
)i
));
384 for (k
= 0; k
< netif
->hwaddr_len
; ++k
) {
385 arp_table
[i
].ethaddr
.addr
[k
] = ethaddr
->addr
[k
];
387 /* reset time stamp */
388 arp_table
[i
].ctime
= 0;
389 /* this is where we will send out queued packets! */
391 while (arp_table
[i
].p
!= NULL
) {
392 /* get the first packet on the queue */
393 struct pbuf
*p
= arp_table
[i
].p
;
394 /* Ethernet header */
395 struct eth_hdr
*ethhdr
= p
->payload
;
396 /* remember (and reference) remainder of queue */
397 /* note: this will also terminate the p pbuf chain */
398 arp_table
[i
].p
= pbuf_dequeue(p
);
399 /* fill-in Ethernet header */
400 for (k
= 0; k
< netif
->hwaddr_len
; ++k
) {
401 ethhdr
->dest
.addr
[k
] = ethaddr
->addr
[k
];
402 ethhdr
->src
.addr
[k
] = netif
->hwaddr
[k
];
404 ethhdr
->type
= htons(ETHTYPE_IP
);
405 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("update_arp_entry: sending queued IP packet %p.\n", (void *)p
));
406 /* send the queued IP packet */
407 netif
->linkoutput(netif
, p
);
408 /* free the queued IP packet */
416 * Updates the ARP table using the given IP packet.
418 * Uses the incoming IP packet's source address to update the
419 * ARP cache for the local network. The function does not alter
420 * or free the packet. This function must be called before the
421 * packet p is passed to the IP layer.
423 * @param netif The lwIP network interface on which the IP packet pbuf arrived.
424 * @param pbuf The IP packet that arrived on netif.
431 etharp_ip_input(struct netif
*netif
, struct pbuf
*p
)
433 struct ethip_hdr
*hdr
;
434 LWIP_ASSERT("netif != NULL", netif
!= NULL
);
435 /* Only insert an entry if the source IP address of the
436 incoming IP packet comes from a host on the local network. */
438 /* source is not on the local network? */
439 if (!ip_addr_netcmp(&(hdr
->ip
.src
), &(netif
->ip_addr
), &(netif
->netmask
))) {
444 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_ip_input: updating ETHARP table.\n"));
445 /* update ARP table */
446 /* @todo We could use ETHARP_TRY_HARD if we think we are going to talk
447 * back soon (for example, if the destination IP address is ours. */
448 update_arp_entry(netif
, &(hdr
->ip
.src
), &(hdr
->eth
.src
), 0);
453 * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
454 * send out queued IP packets. Updates cache with snooped address pairs.
456 * Should be called for incoming ARP packets. The pbuf in the argument
457 * is freed by this function.
459 * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
460 * @param pbuf The ARP packet that arrived on netif. Is freed by this function.
461 * @param ethaddr Ethernet address of netif.
468 etharp_arp_input(struct netif
*netif
, struct eth_addr
*ethaddr
, struct pbuf
*p
)
470 struct etharp_hdr
*hdr
;
471 /* these are aligned properly, whereas the ARP header fields might not be */
472 struct ip_addr sipaddr
, dipaddr
;
476 LWIP_ASSERT("netif != NULL", netif
!= NULL
);
478 /* drop short ARP packets */
479 if (p
->tot_len
< sizeof(struct etharp_hdr
)) {
480 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
| 1, ("etharp_arp_input: packet dropped, too short (%"S16_F
"/%"S16_F
")\n", p
->tot_len
, sizeof(struct etharp_hdr
)));
487 /* get aligned copies of addresses */
488 *(struct ip_addr2
*)((void*)&sipaddr
) = hdr
->sipaddr
;
489 *(struct ip_addr2
*)((void*)&dipaddr
) = hdr
->dipaddr
;
491 /* this interface is not configured? */
492 if (netif
->ip_addr
.addr
== 0) {
495 /* ARP packet directed to us? */
496 for_us
= ip_addr_cmp(&dipaddr
, &(netif
->ip_addr
));
499 /* ARP message directed to us? */
501 /* add IP address in ARP cache; assume requester wants to talk to us.
502 * can result in directly sending the queued packets for this host. */
503 update_arp_entry(netif
, &sipaddr
, &(hdr
->shwaddr
), ETHARP_TRY_HARD
);
504 /* ARP message not directed to us? */
506 /* update the source IP address in the cache, if present */
507 update_arp_entry(netif
, &sipaddr
, &(hdr
->shwaddr
), 0);
510 /* now act on the message itself */
511 switch (htons(hdr
->opcode
)) {
514 /* ARP request. If it asked for our address, we send out a
515 * reply. In any case, we time-stamp any existing ARP entry,
516 * and possiby send out an IP packet that was queued on it. */
518 LWIP_DEBUGF (ETHARP_DEBUG
| DBG_TRACE
, ("etharp_arp_input: incoming ARP request\n"));
519 /* ARP request for our address? */
522 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_arp_input: replying to ARP request for our IP address\n"));
523 /* re-use pbuf to send ARP reply */
524 hdr
->opcode
= htons(ARP_REPLY
);
526 hdr
->dipaddr
= hdr
->sipaddr
;
527 hdr
->sipaddr
= *(struct ip_addr2
*)((void*)&netif
->ip_addr
);
529 for(i
= 0; i
< netif
->hwaddr_len
; ++i
) {
530 hdr
->dhwaddr
.addr
[i
] = hdr
->shwaddr
.addr
[i
];
531 hdr
->shwaddr
.addr
[i
] = ethaddr
->addr
[i
];
532 hdr
->ethhdr
.dest
.addr
[i
] = hdr
->dhwaddr
.addr
[i
];
533 hdr
->ethhdr
.src
.addr
[i
] = ethaddr
->addr
[i
];
536 hdr
->hwtype
= htons(HWTYPE_ETHERNET
);
537 ARPH_HWLEN_SET(hdr
, netif
->hwaddr_len
);
539 hdr
->proto
= htons(ETHTYPE_IP
);
540 ARPH_PROTOLEN_SET(hdr
, sizeof(struct ip_addr
));
542 hdr
->ethhdr
.type
= htons(ETHTYPE_ARP
);
543 /* return ARP reply */
544 netif
->linkoutput(netif
, p
);
545 /* we are not configured? */
546 } else if (netif
->ip_addr
.addr
== 0) {
547 /* { for_us == 0 and netif->ip_addr.addr == 0 } */
548 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
549 /* request was not directed to us */
551 /* { for_us == 0 and netif->ip_addr.addr != 0 } */
552 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_arp_input: ARP request was not for us.\n"));
556 /* ARP reply. We already updated the ARP cache earlier. */
557 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_arp_input: incoming ARP reply\n"));
558 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
559 /* DHCP wants to know about ARP replies from any host with an
560 * IP address also offered to us by the DHCP server. We do not
561 * want to take a duplicate IP address on a single network.
562 * @todo How should we handle redundant (fail-over) interfaces?
564 dhcp_arp_reply(netif
, &sipaddr
);
568 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_arp_input: ARP unknown opcode type %"S16_F
"\n", htons(hdr
->opcode
)));
571 /* free ARP packet */
576 * Resolve and fill-in Ethernet address header for outgoing packet.
578 * For IP multicast and broadcast, corresponding Ethernet addresses
579 * are selected and the packet is transmitted on the link.
581 * For unicast addresses, the packet is submitted to etharp_query(). In
582 * case the IP address is outside the local network, the IP address of
583 * the gateway is used.
585 * @param netif The lwIP network interface which the IP packet will be sent on.
586 * @param ipaddr The IP address of the packet destination.
587 * @param pbuf The pbuf(s) containing the IP packet to be sent.
590 * - ERR_RTE No route to destination (no gateway to external networks),
591 * or the return type of either etharp_query() or netif->linkoutput().
594 etharp_output(struct netif
*netif
, struct ip_addr
*ipaddr
, struct pbuf
*q
)
596 struct eth_addr
*dest
, *srcaddr
, mcastaddr
;
597 struct eth_hdr
*ethhdr
;
600 /* make room for Ethernet header - should not fail */
601 if (pbuf_header(q
, sizeof(struct eth_hdr
)) != 0) {
603 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
| 2, ("etharp_output: could not allocate room for header.\n"));
604 LINK_STATS_INC(link
.lenerr
);
608 /* assume unresolved Ethernet address */
610 /* Determine on destination hardware address. Broadcasts and multicasts
611 * are special, other IP addresses are looked up in the ARP table. */
613 /* broadcast destination IP address? */
614 if (ip_addr_isbroadcast(ipaddr
, netif
)) {
615 /* broadcast on Ethernet also */
616 dest
= (struct eth_addr
*)ðbroadcast
;
617 /* multicast destination IP address? */
618 } else if (ip_addr_ismulticast(ipaddr
)) {
619 /* Hash IP multicast address to MAC address.*/
620 mcastaddr
.addr
[0] = 0x01;
621 mcastaddr
.addr
[1] = 0x00;
622 mcastaddr
.addr
[2] = 0x5e;
623 mcastaddr
.addr
[3] = ip4_addr2(ipaddr
) & 0x7f;
624 mcastaddr
.addr
[4] = ip4_addr3(ipaddr
);
625 mcastaddr
.addr
[5] = ip4_addr4(ipaddr
);
626 /* destination Ethernet address is multicast */
628 /* unicast destination IP address? */
630 /* outside local network? */
631 if (!ip_addr_netcmp(ipaddr
, &(netif
->ip_addr
), &(netif
->netmask
))) {
632 /* interface has default gateway? */
633 if (netif
->gw
.addr
!= 0) {
634 /* send to hardware address of default gateway IP address */
635 ipaddr
= &(netif
->gw
);
636 /* no default gateway available */
638 /* no route to destination error (default gateway missing) */
642 /* queue on destination Ethernet address belonging to ipaddr */
643 return etharp_query(netif
, ipaddr
, q
);
646 /* continuation for multicast/broadcast destinations */
647 /* obtain source Ethernet address of the given interface */
648 srcaddr
= (struct eth_addr
*)netif
->hwaddr
;
650 for (i
= 0; i
< netif
->hwaddr_len
; i
++) {
651 ethhdr
->dest
.addr
[i
] = dest
->addr
[i
];
652 ethhdr
->src
.addr
[i
] = srcaddr
->addr
[i
];
654 ethhdr
->type
= htons(ETHTYPE_IP
);
655 /* send packet directly on the link */
656 return netif
->linkoutput(netif
, q
);
660 * Send an ARP request for the given IP address and/or queue a packet.
662 * If the IP address was not yet in the cache, a pending ARP cache entry
663 * is added and an ARP request is sent for the given address. The packet
664 * is queued on this entry.
666 * If the IP address was already pending in the cache, a new ARP request
667 * is sent for the given address. The packet is queued on this entry.
669 * If the IP address was already stable in the cache, and a packet is
670 * given, it is directly sent and no ARP request is sent out.
672 * If the IP address was already stable in the cache, and no packet is
673 * given, an ARP request is sent out.
675 * @param netif The lwIP network interface on which ipaddr
676 * must be queried for.
677 * @param ipaddr The IP address to be resolved.
678 * @param q If non-NULL, a pbuf that must be delivered to the IP address.
679 * q is not freed by this function.
682 * - ERR_BUF Could not make room for Ethernet header.
683 * - ERR_MEM Hardware address unknown, and no more ARP entries available
684 * to query for address or queue the packet.
685 * - ERR_MEM Could not queue packet due to memory shortage.
686 * - ERR_RTE No route to destination (no gateway to external networks).
687 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
690 err_t
etharp_query(struct netif
*netif
, struct ip_addr
*ipaddr
, struct pbuf
*q
)
692 struct eth_addr
* srcaddr
= (struct eth_addr
*)netif
->hwaddr
;
693 err_t result
= ERR_MEM
;
694 s8_t i
; /* ARP entry index */
695 u8_t k
; /* Ethernet address octet index */
697 /* non-unicast address? */
698 if (ip_addr_isbroadcast(ipaddr
, netif
) ||
699 ip_addr_ismulticast(ipaddr
) ||
700 ip_addr_isany(ipaddr
)) {
701 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
705 /* find entry in ARP cache, ask to create entry if queueing packet */
706 i
= find_entry(ipaddr
, ETHARP_TRY_HARD
);
708 /* could not find or create entry? */
711 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_query: could not create ARP entry\n"));
712 if (q
) LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_query: packet dropped\n"));
716 /* mark a fresh entry as pending (we just sent a request) */
717 if (arp_table
[i
].state
== ETHARP_STATE_EMPTY
) {
718 arp_table
[i
].state
= ETHARP_STATE_PENDING
;
721 /* { i is either a STABLE or (new or existing) PENDING entry } */
722 LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
723 ((arp_table
[i
].state
== ETHARP_STATE_PENDING
) ||
724 (arp_table
[i
].state
== ETHARP_STATE_STABLE
)));
726 /* do we have a pending entry? or an implicit query request? */
727 if ((arp_table
[i
].state
== ETHARP_STATE_PENDING
) || (q
== NULL
)) {
728 /* try to resolve it; send out ARP request */
729 result
= etharp_request(netif
, ipaddr
);
735 if (arp_table
[i
].state
== ETHARP_STATE_STABLE
) {
736 /* we have a valid IP->Ethernet address mapping,
737 * fill in the Ethernet header for the outgoing packet */
738 struct eth_hdr
*ethhdr
= q
->payload
;
739 for(k
= 0; k
< netif
->hwaddr_len
; k
++) {
740 ethhdr
->dest
.addr
[k
] = arp_table
[i
].ethaddr
.addr
[k
];
741 ethhdr
->src
.addr
[k
] = srcaddr
->addr
[k
];
743 ethhdr
->type
= htons(ETHTYPE_IP
);
744 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_query: sending packet %p\n", (void *)q
));
745 /* send the packet */
746 result
= netif
->linkoutput(netif
, q
);
747 /* pending entry? (either just created or already pending */
748 } else if (arp_table
[i
].state
== ETHARP_STATE_PENDING
) {
749 #if ARP_QUEUEING /* queue the given q packet */
751 /* copy any PBUF_REF referenced payloads into PBUF_RAM */
752 /* (the caller of lwIP assumes the referenced payload can be
753 * freed after it returns from the lwIP call that brought us here) */
755 /* packet could be taken over? */
757 /* queue packet ... */
758 if (arp_table
[i
].p
== NULL
) {
759 /* ... in the empty queue */
762 #if 0 /* multi-packet-queueing disabled, see bug #11400 */
764 /* ... at tail of non-empty queue */
765 pbuf_queue(arp_table
[i
].p
, p
);
768 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_query: queued packet %p on ARP entry %"S16_F
"\n", (void *)q
, (s16_t
)i
));
771 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q
));
772 /* { result == ERR_MEM } through initialization */
774 #else /* ARP_QUEUEING == 0 */
775 /* q && state == PENDING && ARP_QUEUEING == 0 => result = ERR_MEM */
776 /* { result == ERR_MEM } through initialization */
777 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_query: Ethernet destination address unknown, queueing disabled, packet %p dropped\n", (void *)q
));
784 err_t
etharp_request(struct netif
*netif
, struct ip_addr
*ipaddr
)
787 struct eth_addr
* srcaddr
= (struct eth_addr
*)netif
->hwaddr
;
788 err_t result
= ERR_OK
;
789 u8_t k
; /* ARP entry index */
791 /* allocate a pbuf for the outgoing ARP request packet */
792 p
= pbuf_alloc(PBUF_LINK
, sizeof(struct etharp_hdr
), PBUF_RAM
);
793 /* could allocate a pbuf for an ARP request? */
795 struct etharp_hdr
*hdr
= p
->payload
;
796 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
, ("etharp_request: sending ARP request.\n"));
797 hdr
->opcode
= htons(ARP_REQUEST
);
798 for (k
= 0; k
< netif
->hwaddr_len
; k
++)
800 hdr
->shwaddr
.addr
[k
] = srcaddr
->addr
[k
];
801 /* the hardware address is what we ask for, in
802 * a request it is a don't-care value, we use zeroes */
803 hdr
->dhwaddr
.addr
[k
] = 0x00;
805 hdr
->dipaddr
= *(struct ip_addr2
*)ipaddr
;
806 hdr
->sipaddr
= *(struct ip_addr2
*)((void*)&netif
->ip_addr
);
808 hdr
->hwtype
= htons(HWTYPE_ETHERNET
);
809 ARPH_HWLEN_SET(hdr
, netif
->hwaddr_len
);
811 hdr
->proto
= htons(ETHTYPE_IP
);
812 ARPH_PROTOLEN_SET(hdr
, sizeof(struct ip_addr
));
813 for (k
= 0; k
< netif
->hwaddr_len
; ++k
)
815 /* broadcast to all network interfaces on the local network */
816 hdr
->ethhdr
.dest
.addr
[k
] = 0xff;
817 hdr
->ethhdr
.src
.addr
[k
] = srcaddr
->addr
[k
];
819 hdr
->ethhdr
.type
= htons(ETHTYPE_ARP
);
821 result
= netif
->linkoutput(netif
, p
);
822 /* free ARP query packet */
825 /* could not allocate pbuf for ARP request */
828 LWIP_DEBUGF(ETHARP_DEBUG
| DBG_TRACE
| 2, ("etharp_request: could not allocate pbuf for ARP request.\n"));