Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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