Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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