Library for Bert van Dam's book "ARM MICROCONTROLLERS" For all chapters with internet.

Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

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