Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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