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