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