Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

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