Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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