HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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