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