Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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