Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Mon Jun 16 07:40:08 2014 +0000
Revision:
1:809b59c7a800
Parent:
0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".

Who changed what in which revision?

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