Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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