A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

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