Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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