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