Michael Fischler / Mbed 2 deprecated Contest_Winner

Dependencies:   mbed

Committer:
mafischl
Date:
Thu Oct 13 17:02:29 2011 +0000
Revision:
0:d9266031f832

        

Who changed what in which revision?

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