modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
casiotone401
Date:
Thu Oct 16 14:13:21 2014 +0000
Revision:
8:275256b5d807
Parent:
7:105191e07767
minor change

Who changed what in which revision?

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