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