Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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