]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/net/lwip/lwip/netif/etharp.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / net / lwip / lwip / 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 #else /* ARP_QUEUEING */
97 /** Pointer to a single pending outgoing packet on this ARP entry. */
98 struct pbuf *q;
99 #endif /* ARP_QUEUEING */
100 ip_addr_t ipaddr;
101 struct eth_addr ethaddr;
102 #if LWIP_SNMP
103 struct netif *netif;
104 #endif /* LWIP_SNMP */
105 u8_t state;
106 u8_t ctime;
107 #if ETHARP_SUPPORT_STATIC_ENTRIES
108 u8_t static_entry;
109 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
110 };
111
112 static struct etharp_entry arp_table[ARP_TABLE_SIZE];
113
114 #if !LWIP_NETIF_HWADDRHINT
115 static u8_t etharp_cached_entry;
116 #endif /* !LWIP_NETIF_HWADDRHINT */
117
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
123
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 */
130
131 static err_t update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags);
132
133
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"
137 #endif
138
139
140 #if ARP_QUEUEING
141 /**
142 * Free a complete queue of etharp entries
143 *
144 * @param q a qeueue of etharp_q_entry's to free
145 */
146 static void
147 free_etharp_q(struct etharp_q_entry *q)
148 {
149 struct etharp_q_entry *r;
150 LWIP_ASSERT("q != NULL", q != NULL);
151 LWIP_ASSERT("q->p != NULL", q->p != NULL);
152 while (q) {
153 r = q;
154 q = q->next;
155 LWIP_ASSERT("r->p != NULL", (r->p != NULL));
156 pbuf_free(r->p);
157 memp_free(MEMP_ARP_QUEUE, r);
158 }
159 }
160 #else /* ARP_QUEUEING */
161
162 /** Compatibility define: free the queued pbuf */
163 #define free_etharp_q(q) pbuf_free(q)
164
165 #endif /* ARP_QUEUEING */
166
167 /** Clean up ARP table entries */
168 static void
169 free_entry(int i)
170 {
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;
179 }
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 */
185 #ifdef LWIP_DEBUG
186 /* for debugging, clean out the complete entry */
187 arp_table[i].ctime = 0;
188 #if LWIP_SNMP
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 */
194 }
195
196 /**
197 * Clears expired entries in the ARP table.
198 *
199 * This function should be called every ETHARP_TMR_INTERVAL milliseconds (5 seconds),
200 * in order to expire entries in the ARP table.
201 */
202 void
203 etharp_tmr(void)
204 {
205 u8_t i;
206
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 */
215 ) {
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 */
224 free_entry(i);
225 }
226 #if ARP_QUEUEING
227 /* still pending entry? (not expired) */
228 if (arp_table[i].state == ETHARP_STATE_PENDING) {
229 /* resend an ARP query here? */
230 }
231 #endif /* ARP_QUEUEING */
232 }
233 }
234 }
235
236 /**
237 * Search the ARP table for a matching or new entry.
238 *
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.
243 *
244 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
245 *
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.
249 *
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)
253 *
254 * @return The ARP entry index that matched or is created, ERR_MEM if no
255 * entry is found or could be recycled.
256 */
257 static s8_t
258 find_entry(ip_addr_t *ipaddr, u8_t flags)
259 {
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;
265 /* its age */
266 u8_t age_queue = 0;
267
268 /**
269 * a) do a search through the cache, remember candidates
270 * b) select candidate entry
271 * c) create new entry
272 */
273
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.
281 */
282
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 */
289 empty = i;
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 */
297 return i;
298 }
299 /* pending entry? */
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) {
304 old_queue = i;
305 age_queue = arp_table[i].ctime;
306 }
307 } else
308 /* pending without queued packets? */
309 {
310 if (arp_table[i].ctime >= age_pending) {
311 old_pending = i;
312 age_pending = arp_table[i].ctime;
313 }
314 }
315 /* stable entry? */
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 */
321 {
322 /* remember entry with oldest stable entry in oldest, its age in maxtime */
323 if (arp_table[i].ctime >= age_stable) {
324 old_stable = i;
325 age_stable = arp_table[i].ctime;
326 }
327 }
328 }
329 }
330 }
331 /* { we have no match } => try to create a new entry */
332
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;
339 }
340
341 /* b) choose the least destructive entry to recycle:
342 * 1) empty entry
343 * 2) oldest stable entry
344 * 3) oldest pending entry without queued packets
345 * 4) oldest pending entry with queued packets
346 *
347 * { ETHARP_FLAG_TRY_HARD is set at this point }
348 */
349
350 /* 1) empty entry available? */
351 if (empty < ARP_TABLE_SIZE) {
352 i = empty;
353 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
354 } else {
355 /* 2) found recyclable stable entry? */
356 if (old_stable < ARP_TABLE_SIZE) {
357 /* recycle oldest stable*/
358 i = old_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 */
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 /* 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) */
370 i = old_queue;
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 */
373 } else {
374 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty or recyclable entries found\n"));
375 return (s8_t)ERR_MEM;
376 }
377
378 /* { empty or recyclable entry found } */
379 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
380 free_entry(i);
381 }
382
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);
386
387 /* IP address given? */
388 if (ipaddr != NULL) {
389 /* set IP address */
390 ip_addr_copy(arp_table[i].ipaddr, *ipaddr);
391 }
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 */
396 return (err_t)i;
397 }
398
399 /**
400 * Send an IP packet on the network using netif->linkoutput
401 * The ethernet header is filled in before sending.
402 *
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
408 */
409 static err_t
410 etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
411 {
412 struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
413
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(&ethhdr->dest, dst);
417 ETHADDR16_COPY(&ethhdr->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);
422 }
423
424 /**
425 * Update (or insert) a IP/MAC address pair in the ARP cache.
426 *
427 * If a pending entry is resolved, any queued packets will be sent
428 * at this point.
429 *
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_*
434 *
435 * @return
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.
439 *
440 * @see pbuf_free()
441 */
442 static err_t
443 update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
444 {
445 s8_t i;
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"));
456 return ERR_ARG;
457 }
458 /* find or create ARP entry */
459 i = find_entry(ipaddr, flags);
460 /* bail out if no entry could be found */
461 if (i < 0) {
462 return (err_t)i;
463 }
464
465 #if ETHARP_SUPPORT_STATIC_ENTRIES
466 if (flags & ETHARP_FLAG_STATIC_ENTRY) {
467 /* record static type */
468 arp_table[i].static_entry = 1;
469 }
470 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
471
472 /* mark it stable */
473 arp_table[i].state = ETHARP_STATE_STABLE;
474
475 #if LWIP_SNMP
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);
481
482 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
483 /* update address */
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! */
488 #if ARP_QUEUEING
489 while (arp_table[i].q != NULL) {
490 struct pbuf *p;
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 */
496 p = q->p;
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 */
507 pbuf_free(p);
508 }
509 return ERR_OK;
510 }
511
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.
516 *
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
520 */
521 err_t
522 etharp_add_static_entry(ip_addr_t *ipaddr, struct eth_addr *ethaddr)
523 {
524 struct netif *netif;
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]));
529
530 netif = ip_route(ipaddr);
531 if (netif == NULL) {
532 return ERR_RTE;
533 }
534
535 return update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
536 }
537
538 /** Remove a static entry from the ARP table previously added with a call to
539 * etharp_add_static_entry.
540 *
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
545 */
546 err_t
547 etharp_remove_static_entry(ip_addr_t *ipaddr)
548 {
549 s8_t i;
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)));
552
553 /* find or create ARP entry */
554 i = find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
555 /* bail out if no entry could be found */
556 if (i < 0) {
557 return (err_t)i;
558 }
559
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 */
563 return ERR_ARG;
564 }
565 /* entry found, free it */
566 free_entry(i);
567 return ERR_OK;
568 }
569 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
570
571 /**
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!
575 *
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
581 */
582 s8_t
583 etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr,
584 struct eth_addr **eth_ret, ip_addr_t **ip_ret)
585 {
586 s8_t i;
587
588 LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
589 eth_ret != NULL && ip_ret != NULL);
590
591 LWIP_UNUSED_ARG(netif);
592
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;
597 return i;
598 }
599 return -1;
600 }
601
602 #if ETHARP_TRUST_IP_MAC
603 /**
604 * Updates the ARP table using the given IP packet.
605 *
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.
610 *
611 * @param netif The lwIP network interface on which the IP packet pbuf arrived.
612 * @param p The IP packet that arrived on netif.
613 *
614 * @return NULL
615 *
616 * @see pbuf_free()
617 */
618 static void
619 etharp_ip_input(struct netif *netif, struct pbuf *p)
620 {
621 struct eth_hdr *ethhdr;
622 struct ip_hdr *iphdr;
623 ip_addr_t iphdr_src;
624 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
625
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);
633 }
634 #endif /* ETHARP_SUPPORT_VLAN */
635
636 ip_addr_copy(iphdr_src, iphdr->src);
637
638 /* source is not on the local network? */
639 if (!ip_addr_netcmp(&iphdr_src, &(netif->ip_addr), &(netif->netmask))) {
640 /* do nothing */
641 return;
642 }
643
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);
649 }
650 #endif /* ETHARP_TRUST_IP_MAC */
651
652 /**
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.
655 *
656 * Should be called for incoming ARP packets. The pbuf in the argument
657 * is freed by this function.
658 *
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.
662 *
663 * @return NULL
664 *
665 * @see pbuf_free()
666 */
667 static void
668 etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
669 {
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;
674 u8_t for_us;
675 #if LWIP_AUTOIP
676 const u8_t * ethdst_hwaddr;
677 #endif /* LWIP_AUTOIP */
678
679 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
680
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);
689 pbuf_free(p);
690 return;
691 }
692
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);
698 }
699 #endif /* ETHARP_SUPPORT_VLAN */
700
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);
711 pbuf_free(p);
712 return;
713 }
714 ETHARP_STATS_INC(etharp.recv);
715
716 #if LWIP_AUTOIP
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 */
722
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);
727
728 /* this interface is not configured? */
729 if (ip_addr_isany(&netif->ip_addr)) {
730 for_us = 0;
731 } else {
732 /* ARP packet directed to us? */
733 for_us = (u8_t)ip_addr_cmp(&dipaddr, &(netif->ip_addr));
734 }
735
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);
743
744 /* now act on the message itself */
745 switch (hdr->opcode) {
746 /* ARP request? */
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. */
751
752 LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
753 /* ARP request for our address? */
754 if (for_us) {
755
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);
761
762 IPADDR2_COPY(&hdr->dipaddr, &hdr->sipaddr);
763 IPADDR2_COPY(&hdr->sipaddr, &netif->ip_addr);
764
765 LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
766 (netif->hwaddr_len == ETHARP_HWADDR_LEN));
767 #if LWIP_AUTOIP
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 */
773
774 ETHADDR16_COPY(&hdr->dhwaddr, &hdr->shwaddr);
775 #if LWIP_AUTOIP
776 ETHADDR16_COPY(&ethhdr->dest, ethdst_hwaddr);
777 #else /* LWIP_AUTOIP */
778 ETHADDR16_COPY(&ethhdr->dest, &hdr->shwaddr);
779 #endif /* LWIP_AUTOIP */
780 ETHADDR16_COPY(&hdr->shwaddr, ethaddr);
781 ETHADDR16_COPY(&ethhdr->src, ethaddr);
782
783 /* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header
784 are already correct, we tested that before */
785
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 */
793 } else {
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"));
796 }
797 break;
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) */
808 break;
809 default:
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);
812 break;
813 }
814 /* free ARP packet */
815 pbuf_free(p);
816 }
817
818 /**
819 * Resolve and fill-in Ethernet address header for outgoing IP packet.
820 *
821 * For IP multicast and broadcast, corresponding Ethernet addresses
822 * are selected and the packet is transmitted on the link.
823 *
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.
827 *
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.
831 *
832 * @return
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().
835 */
836 err_t
837 etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)
838 {
839 struct eth_addr *dest, mcastaddr;
840
841 /* make room for Ethernet header - should not fail */
842 if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
843 /* bail out */
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);
847 return ERR_BUF;
848 }
849
850 /* assume unresolved Ethernet address */
851 dest = NULL;
852 /* Determine on destination hardware address. Broadcasts and multicasts
853 * are special, other IP addresses are looked up in the ARP table. */
854
855 /* broadcast destination IP address? */
856 if (ip_addr_isbroadcast(ipaddr, netif)) {
857 /* broadcast on Ethernet also */
858 dest = (struct eth_addr *)&ethbroadcast;
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 */
869 dest = &mcastaddr;
870 /* unicast destination IP address? */
871 } else {
872 /* outside local network? */
873 if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask)) &&
874 !ip_addr_islinklocal(ipaddr)) {
875 #if LWIP_AUTOIP
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 */
884 {
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 */
890 } else {
891 /* no route to destination error (default gateway missing) */
892 return ERR_RTE;
893 }
894 }
895 }
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);
908 }
909 #if LWIP_NETIF_HWADDRHINT
910 }
911 }
912 #endif /* LWIP_NETIF_HWADDRHINT */
913 /* queue on destination Ethernet address belonging to ipaddr */
914 return etharp_query(netif, ipaddr, q);
915 }
916
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);
921 }
922
923 /**
924 * Send an ARP request for the given IP address and/or queue a packet.
925 *
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.
929 *
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.
932 *
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.
935 *
936 * If the IP address was already stable in the cache, and no packet is
937 * given, an ARP request is sent out.
938 *
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.
944 *
945 * @note q must only be ONE packet, not a packet queue!
946 *
947 * @return
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.
954 *
955 */
956 err_t
957 etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q)
958 {
959 struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
960 err_t result = ERR_MEM;
961 s8_t i; /* ARP entry index */
962
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"));
968 return ERR_ARG;
969 }
970
971 /* find entry in ARP cache, ask to create entry if queueing packet */
972 i = find_entry(ipaddr, ETHARP_FLAG_TRY_HARD);
973
974 /* could not find or create entry? */
975 if (i < 0) {
976 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
977 if (q) {
978 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
979 ETHARP_STATS_INC(etharp.memerr);
980 }
981 return (err_t)i;
982 }
983
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;
987 }
988
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)));
993
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. */
1003 }
1004 if (q == NULL) {
1005 return result;
1006 }
1007 }
1008
1009 /* packet given? */
1010 LWIP_ASSERT("q != NULL", q != NULL);
1011 /* stable entry? */
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' */
1020 struct pbuf *p;
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. */
1025 p = q;
1026 while (p) {
1027 LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
1028 if(p->type != PBUF_ROM) {
1029 copy_needed = 1;
1030 break;
1031 }
1032 p = p->next;
1033 }
1034 if(copy_needed) {
1035 /* copy the whole packet into new pbufs */
1036 p = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
1037 if(p != NULL) {
1038 if (pbuf_copy(p, q) != ERR_OK) {
1039 pbuf_free(p);
1040 p = NULL;
1041 }
1042 }
1043 } else {
1044 /* referencing the old pbuf is enough */
1045 p = q;
1046 pbuf_ref(p);
1047 }
1048 /* packet could be taken over? */
1049 if (p != NULL) {
1050 /* queue packet ... */
1051 #if ARP_QUEUEING
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;
1057 new_entry->p = p;
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;
1061 r = arp_table[i].q;
1062 while (r->next != NULL) {
1063 r = r->next;
1064 }
1065 r->next = new_entry;
1066 } else {
1067 /* queue did not exist, first item in queue */
1068 arp_table[i].q = new_entry;
1069 }
1070 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1071 result = ERR_OK;
1072 } else {
1073 /* the pool MEMP_ARP_QUEUE is empty */
1074 pbuf_free(p);
1075 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
1076 result = ERR_MEM;
1077 }
1078 #else /* ARP_QUEUEING */
1079 /* always queue one packet per ARP request only, freeing a previously queued packet */
1080 if (arp_table[i].q != NULL) {
1081 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1082 pbuf_free(arp_table[i].q);
1083 }
1084 arp_table[i].q = p;
1085 result = ERR_OK;
1086 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1087 #endif /* ARP_QUEUEING */
1088 } else {
1089 ETHARP_STATS_INC(etharp.memerr);
1090 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
1091 result = ERR_MEM;
1092 }
1093 }
1094 return result;
1095 }
1096
1097 /**
1098 * Send a raw ARP packet (opcode and all addresses can be modified)
1099 *
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
1111 */
1112 #if !LWIP_AUTOIP
1113 static
1114 #endif /* LWIP_AUTOIP */
1115 err_t
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,
1120 const u16_t opcode)
1121 {
1122 struct pbuf *p;
1123 err_t result = ERR_OK;
1124 struct eth_hdr *ethhdr;
1125 struct etharp_hdr *hdr;
1126 #if LWIP_AUTOIP
1127 const u8_t * ethdst_hwaddr;
1128 #endif /* LWIP_AUTOIP */
1129
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? */
1133 if (p == NULL) {
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);
1137 return ERR_MEM;
1138 }
1139 LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
1140 (p->len >= SIZEOF_ETHARP_PACKET));
1141
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);
1146
1147 LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
1148 (netif->hwaddr_len == ETHARP_HWADDR_LEN));
1149 #if LWIP_AUTOIP
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 */
1159 #if LWIP_AUTOIP
1160 ETHADDR16_COPY(&ethhdr->dest, ethdst_hwaddr);
1161 #else /* LWIP_AUTOIP */
1162 ETHADDR16_COPY(&ethhdr->dest, ethdst_addr);
1163 #endif /* LWIP_AUTOIP */
1164 ETHADDR16_COPY(&ethhdr->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);
1169
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);
1175
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 */
1181 pbuf_free(p);
1182 p = NULL;
1183 /* could not allocate pbuf for ARP request */
1184
1185 return result;
1186 }
1187
1188 /**
1189 * Send an ARP request packet asking for ipaddr.
1190 *
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
1196 */
1197 err_t
1198 etharp_request(struct netif *netif, ip_addr_t *ipaddr)
1199 {
1200 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
1201 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
1202 (struct eth_addr *)netif->hwaddr, &netif->ip_addr, &ethzero,
1203 ipaddr, ARP_REQUEST);
1204 }
1205 #endif /* LWIP_ARP */
1206
1207 /**
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.
1211 *
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
1214 */
1215 err_t
1216 ethernet_input(struct pbuf *p, struct netif *netif)
1217 {
1218 struct eth_hdr* ethhdr;
1219 u16_t type;
1220 s16_t ip_hdr_offset = SIZEOF_ETH_HDR;
1221
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;
1227 }
1228
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)));
1238
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;
1248 }
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 */
1252 pbuf_free(p);
1253 return ERR_OK;
1254 }
1255 #endif /* ETHARP_VLAN_CHECK */
1256 type = vlan->tpid;
1257 ip_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR;
1258 }
1259 #endif /* ETHARP_SUPPORT_VLAN */
1260
1261 #if LWIP_ARP_FILTER_NETIF
1262 netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, htons(type));
1263 #endif /* LWIP_ARP_FILTER_NETIF*/
1264
1265 switch (type) {
1266 #if LWIP_ARP
1267 /* IP packet? */
1268 case PP_HTONS(ETHTYPE_IP):
1269 if (!(netif->flags & NETIF_FLAG_ETHARP)) {
1270 goto free_and_return;
1271 }
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;
1280 } else {
1281 /* pass to IP layer */
1282 ip_input(p, netif);
1283 }
1284 break;
1285
1286 case PP_HTONS(ETHTYPE_ARP):
1287 if (!(netif->flags & NETIF_FLAG_ETHARP)) {
1288 goto free_and_return;
1289 }
1290 /* pass p to ARP module */
1291 etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
1292 break;
1293 #endif /* LWIP_ARP */
1294 #if PPPOE_SUPPORT
1295 case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */
1296 pppoe_disc_input(netif, p);
1297 break;
1298
1299 case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */
1300 pppoe_data_input(netif, p);
1301 break;
1302 #endif /* PPPOE_SUPPORT */
1303
1304 default:
1305 ETHARP_STATS_INC(etharp.proterr);
1306 ETHARP_STATS_INC(etharp.drop);
1307 goto free_and_return;
1308 }
1309
1310 /* This means the pbuf is freed or consumed,
1311 so the caller doesn't have to free it again */
1312 return ERR_OK;
1313
1314 free_and_return:
1315 pbuf_free(p);
1316 return ERR_OK;
1317 }
1318 #endif /* LWIP_ARP || LWIP_ETHERNET */
Imprint / Impressum