![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Embedded C project:18/12/2014
Dependencies: DS1307 TextLCD mbed
NetServices/lwip/core/ipv4/autoip.c@0:8d87bc453349, 2014-12-18 (annotated)
- Committer:
- ninoderkinderen
- Date:
- Thu Dec 18 09:35:49 2014 +0000
- Revision:
- 0:8d87bc453349
Programma embedded C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ninoderkinderen | 0:8d87bc453349 | 1 | /** |
ninoderkinderen | 0:8d87bc453349 | 2 | * @file |
ninoderkinderen | 0:8d87bc453349 | 3 | * AutoIP Automatic LinkLocal IP Configuration |
ninoderkinderen | 0:8d87bc453349 | 4 | * |
ninoderkinderen | 0:8d87bc453349 | 5 | */ |
ninoderkinderen | 0:8d87bc453349 | 6 | |
ninoderkinderen | 0:8d87bc453349 | 7 | /* |
ninoderkinderen | 0:8d87bc453349 | 8 | * |
ninoderkinderen | 0:8d87bc453349 | 9 | * Copyright (c) 2007 Dominik Spies <kontakt@dspies.de> |
ninoderkinderen | 0:8d87bc453349 | 10 | * All rights reserved. |
ninoderkinderen | 0:8d87bc453349 | 11 | * |
ninoderkinderen | 0:8d87bc453349 | 12 | * Redistribution and use in source and binary forms, with or without modification, |
ninoderkinderen | 0:8d87bc453349 | 13 | * are permitted provided that the following conditions are met: |
ninoderkinderen | 0:8d87bc453349 | 14 | * |
ninoderkinderen | 0:8d87bc453349 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
ninoderkinderen | 0:8d87bc453349 | 16 | * this list of conditions and the following disclaimer. |
ninoderkinderen | 0:8d87bc453349 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
ninoderkinderen | 0:8d87bc453349 | 18 | * this list of conditions and the following disclaimer in the documentation |
ninoderkinderen | 0:8d87bc453349 | 19 | * and/or other materials provided with the distribution. |
ninoderkinderen | 0:8d87bc453349 | 20 | * 3. The name of the author may not be used to endorse or promote products |
ninoderkinderen | 0:8d87bc453349 | 21 | * derived from this software without specific prior written permission. |
ninoderkinderen | 0:8d87bc453349 | 22 | * |
ninoderkinderen | 0:8d87bc453349 | 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
ninoderkinderen | 0:8d87bc453349 | 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
ninoderkinderen | 0:8d87bc453349 | 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
ninoderkinderen | 0:8d87bc453349 | 26 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
ninoderkinderen | 0:8d87bc453349 | 27 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
ninoderkinderen | 0:8d87bc453349 | 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
ninoderkinderen | 0:8d87bc453349 | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
ninoderkinderen | 0:8d87bc453349 | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
ninoderkinderen | 0:8d87bc453349 | 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
ninoderkinderen | 0:8d87bc453349 | 32 | * OF SUCH DAMAGE. |
ninoderkinderen | 0:8d87bc453349 | 33 | * |
ninoderkinderen | 0:8d87bc453349 | 34 | * Author: Dominik Spies <kontakt@dspies.de> |
ninoderkinderen | 0:8d87bc453349 | 35 | * |
ninoderkinderen | 0:8d87bc453349 | 36 | * This is a AutoIP implementation for the lwIP TCP/IP stack. It aims to conform |
ninoderkinderen | 0:8d87bc453349 | 37 | * with RFC 3927. |
ninoderkinderen | 0:8d87bc453349 | 38 | * |
ninoderkinderen | 0:8d87bc453349 | 39 | * |
ninoderkinderen | 0:8d87bc453349 | 40 | * Please coordinate changes and requests with Dominik Spies |
ninoderkinderen | 0:8d87bc453349 | 41 | * <kontakt@dspies.de> |
ninoderkinderen | 0:8d87bc453349 | 42 | */ |
ninoderkinderen | 0:8d87bc453349 | 43 | |
ninoderkinderen | 0:8d87bc453349 | 44 | /******************************************************************************* |
ninoderkinderen | 0:8d87bc453349 | 45 | * USAGE: |
ninoderkinderen | 0:8d87bc453349 | 46 | * |
ninoderkinderen | 0:8d87bc453349 | 47 | * define LWIP_AUTOIP 1 in your lwipopts.h |
ninoderkinderen | 0:8d87bc453349 | 48 | * |
ninoderkinderen | 0:8d87bc453349 | 49 | * If you don't use tcpip.c (so, don't call, you don't call tcpip_init): |
ninoderkinderen | 0:8d87bc453349 | 50 | * - First, call autoip_init(). |
ninoderkinderen | 0:8d87bc453349 | 51 | * - call autoip_tmr() all AUTOIP_TMR_INTERVAL msces, |
ninoderkinderen | 0:8d87bc453349 | 52 | * that should be defined in autoip.h. |
ninoderkinderen | 0:8d87bc453349 | 53 | * I recommend a value of 100. The value must divide 1000 with a remainder almost 0. |
ninoderkinderen | 0:8d87bc453349 | 54 | * Possible values are 1000, 500, 333, 250, 200, 166, 142, 125, 111, 100 .... |
ninoderkinderen | 0:8d87bc453349 | 55 | * |
ninoderkinderen | 0:8d87bc453349 | 56 | * Without DHCP: |
ninoderkinderen | 0:8d87bc453349 | 57 | * - Call autoip_start() after netif_add(). |
ninoderkinderen | 0:8d87bc453349 | 58 | * |
ninoderkinderen | 0:8d87bc453349 | 59 | * With DHCP: |
ninoderkinderen | 0:8d87bc453349 | 60 | * - define LWIP_DHCP_AUTOIP_COOP 1 in your lwipopts.h. |
ninoderkinderen | 0:8d87bc453349 | 61 | * - Configure your DHCP Client. |
ninoderkinderen | 0:8d87bc453349 | 62 | * |
ninoderkinderen | 0:8d87bc453349 | 63 | */ |
ninoderkinderen | 0:8d87bc453349 | 64 | |
ninoderkinderen | 0:8d87bc453349 | 65 | #include "lwip/opt.h" |
ninoderkinderen | 0:8d87bc453349 | 66 | |
ninoderkinderen | 0:8d87bc453349 | 67 | #if LWIP_AUTOIP /* don't build if not configured for use in lwipopts.h */ |
ninoderkinderen | 0:8d87bc453349 | 68 | |
ninoderkinderen | 0:8d87bc453349 | 69 | #include "lwip/mem.h" |
ninoderkinderen | 0:8d87bc453349 | 70 | #include "lwip/udp.h" |
ninoderkinderen | 0:8d87bc453349 | 71 | #include "lwip/ip_addr.h" |
ninoderkinderen | 0:8d87bc453349 | 72 | #include "lwip/netif.h" |
ninoderkinderen | 0:8d87bc453349 | 73 | #include "lwip/autoip.h" |
ninoderkinderen | 0:8d87bc453349 | 74 | #include "netif/etharp.h" |
ninoderkinderen | 0:8d87bc453349 | 75 | |
ninoderkinderen | 0:8d87bc453349 | 76 | #include <stdlib.h> |
ninoderkinderen | 0:8d87bc453349 | 77 | #include <string.h> |
ninoderkinderen | 0:8d87bc453349 | 78 | |
ninoderkinderen | 0:8d87bc453349 | 79 | /* 169.254.0.0 */ |
ninoderkinderen | 0:8d87bc453349 | 80 | #define AUTOIP_NET 0xA9FE0000 |
ninoderkinderen | 0:8d87bc453349 | 81 | /* 169.254.1.0 */ |
ninoderkinderen | 0:8d87bc453349 | 82 | #define AUTOIP_RANGE_START (AUTOIP_NET | 0x0100) |
ninoderkinderen | 0:8d87bc453349 | 83 | /* 169.254.254.255 */ |
ninoderkinderen | 0:8d87bc453349 | 84 | #define AUTOIP_RANGE_END (AUTOIP_NET | 0xFEFF) |
ninoderkinderen | 0:8d87bc453349 | 85 | |
ninoderkinderen | 0:8d87bc453349 | 86 | |
ninoderkinderen | 0:8d87bc453349 | 87 | /** Pseudo random macro based on netif informations. |
ninoderkinderen | 0:8d87bc453349 | 88 | * You could use "rand()" from the C Library if you define LWIP_AUTOIP_RAND in lwipopts.h */ |
ninoderkinderen | 0:8d87bc453349 | 89 | #ifndef LWIP_AUTOIP_RAND |
ninoderkinderen | 0:8d87bc453349 | 90 | #define LWIP_AUTOIP_RAND(netif) ( (((u32_t)((netif->hwaddr[5]) & 0xff) << 24) | \ |
ninoderkinderen | 0:8d87bc453349 | 91 | ((u32_t)((netif->hwaddr[3]) & 0xff) << 16) | \ |
ninoderkinderen | 0:8d87bc453349 | 92 | ((u32_t)((netif->hwaddr[2]) & 0xff) << 8) | \ |
ninoderkinderen | 0:8d87bc453349 | 93 | ((u32_t)((netif->hwaddr[4]) & 0xff))) + \ |
ninoderkinderen | 0:8d87bc453349 | 94 | (netif->autoip?netif->autoip->tried_llipaddr:0)) |
ninoderkinderen | 0:8d87bc453349 | 95 | #endif /* LWIP_AUTOIP_RAND */ |
ninoderkinderen | 0:8d87bc453349 | 96 | |
ninoderkinderen | 0:8d87bc453349 | 97 | /** |
ninoderkinderen | 0:8d87bc453349 | 98 | * Macro that generates the initial IP address to be tried by AUTOIP. |
ninoderkinderen | 0:8d87bc453349 | 99 | * If you want to override this, define it to something else in lwipopts.h. |
ninoderkinderen | 0:8d87bc453349 | 100 | */ |
ninoderkinderen | 0:8d87bc453349 | 101 | #ifndef LWIP_AUTOIP_CREATE_SEED_ADDR |
ninoderkinderen | 0:8d87bc453349 | 102 | #define LWIP_AUTOIP_CREATE_SEED_ADDR(netif) \ |
ninoderkinderen | 0:8d87bc453349 | 103 | htonl(AUTOIP_RANGE_START + ((u32_t)(((u8_t)(netif->hwaddr[4])) | \ |
ninoderkinderen | 0:8d87bc453349 | 104 | ((u32_t)((u8_t)(netif->hwaddr[5]))) << 8))) |
ninoderkinderen | 0:8d87bc453349 | 105 | #endif /* LWIP_AUTOIP_CREATE_SEED_ADDR */ |
ninoderkinderen | 0:8d87bc453349 | 106 | |
ninoderkinderen | 0:8d87bc453349 | 107 | /* static functions */ |
ninoderkinderen | 0:8d87bc453349 | 108 | static void autoip_handle_arp_conflict(struct netif *netif); |
ninoderkinderen | 0:8d87bc453349 | 109 | |
ninoderkinderen | 0:8d87bc453349 | 110 | /* creates a pseudo random LL IP-Address for a network interface */ |
ninoderkinderen | 0:8d87bc453349 | 111 | static void autoip_create_addr(struct netif *netif, ip_addr_t *ipaddr); |
ninoderkinderen | 0:8d87bc453349 | 112 | |
ninoderkinderen | 0:8d87bc453349 | 113 | /* sends an ARP probe */ |
ninoderkinderen | 0:8d87bc453349 | 114 | static err_t autoip_arp_probe(struct netif *netif); |
ninoderkinderen | 0:8d87bc453349 | 115 | |
ninoderkinderen | 0:8d87bc453349 | 116 | /* sends an ARP announce */ |
ninoderkinderen | 0:8d87bc453349 | 117 | static err_t autoip_arp_announce(struct netif *netif); |
ninoderkinderen | 0:8d87bc453349 | 118 | |
ninoderkinderen | 0:8d87bc453349 | 119 | /* configure interface for use with current LL IP-Address */ |
ninoderkinderen | 0:8d87bc453349 | 120 | static err_t autoip_bind(struct netif *netif); |
ninoderkinderen | 0:8d87bc453349 | 121 | |
ninoderkinderen | 0:8d87bc453349 | 122 | /* start sending probes for llipaddr */ |
ninoderkinderen | 0:8d87bc453349 | 123 | static void autoip_start_probing(struct netif *netif); |
ninoderkinderen | 0:8d87bc453349 | 124 | |
ninoderkinderen | 0:8d87bc453349 | 125 | /** |
ninoderkinderen | 0:8d87bc453349 | 126 | * Initialize this module |
ninoderkinderen | 0:8d87bc453349 | 127 | */ |
ninoderkinderen | 0:8d87bc453349 | 128 | void |
ninoderkinderen | 0:8d87bc453349 | 129 | autoip_init(void) |
ninoderkinderen | 0:8d87bc453349 | 130 | { |
ninoderkinderen | 0:8d87bc453349 | 131 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_init()\n")); |
ninoderkinderen | 0:8d87bc453349 | 132 | } |
ninoderkinderen | 0:8d87bc453349 | 133 | |
ninoderkinderen | 0:8d87bc453349 | 134 | /** Set a statically allocated struct autoip to work with. |
ninoderkinderen | 0:8d87bc453349 | 135 | * Using this prevents autoip_start to allocate it using mem_malloc. |
ninoderkinderen | 0:8d87bc453349 | 136 | * |
ninoderkinderen | 0:8d87bc453349 | 137 | * @param netif the netif for which to set the struct autoip |
ninoderkinderen | 0:8d87bc453349 | 138 | * @param dhcp (uninitialised) dhcp struct allocated by the application |
ninoderkinderen | 0:8d87bc453349 | 139 | */ |
ninoderkinderen | 0:8d87bc453349 | 140 | void |
ninoderkinderen | 0:8d87bc453349 | 141 | autoip_set_struct(struct netif *netif, struct autoip *autoip) |
ninoderkinderen | 0:8d87bc453349 | 142 | { |
ninoderkinderen | 0:8d87bc453349 | 143 | LWIP_ASSERT("netif != NULL", netif != NULL); |
ninoderkinderen | 0:8d87bc453349 | 144 | LWIP_ASSERT("autoip != NULL", autoip != NULL); |
ninoderkinderen | 0:8d87bc453349 | 145 | LWIP_ASSERT("netif already has a struct autoip set", netif->autoip == NULL); |
ninoderkinderen | 0:8d87bc453349 | 146 | |
ninoderkinderen | 0:8d87bc453349 | 147 | /* clear data structure */ |
ninoderkinderen | 0:8d87bc453349 | 148 | memset(autoip, 0, sizeof(struct autoip)); |
ninoderkinderen | 0:8d87bc453349 | 149 | /* autoip->state = AUTOIP_STATE_OFF; */ |
ninoderkinderen | 0:8d87bc453349 | 150 | netif->autoip = autoip; |
ninoderkinderen | 0:8d87bc453349 | 151 | } |
ninoderkinderen | 0:8d87bc453349 | 152 | |
ninoderkinderen | 0:8d87bc453349 | 153 | /** Restart AutoIP client and check the next address (conflict detected) |
ninoderkinderen | 0:8d87bc453349 | 154 | * |
ninoderkinderen | 0:8d87bc453349 | 155 | * @param netif The netif under AutoIP control |
ninoderkinderen | 0:8d87bc453349 | 156 | */ |
ninoderkinderen | 0:8d87bc453349 | 157 | static void |
ninoderkinderen | 0:8d87bc453349 | 158 | autoip_restart(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 159 | { |
ninoderkinderen | 0:8d87bc453349 | 160 | netif->autoip->tried_llipaddr++; |
ninoderkinderen | 0:8d87bc453349 | 161 | autoip_start(netif); |
ninoderkinderen | 0:8d87bc453349 | 162 | } |
ninoderkinderen | 0:8d87bc453349 | 163 | |
ninoderkinderen | 0:8d87bc453349 | 164 | /** |
ninoderkinderen | 0:8d87bc453349 | 165 | * Handle a IP address conflict after an ARP conflict detection |
ninoderkinderen | 0:8d87bc453349 | 166 | */ |
ninoderkinderen | 0:8d87bc453349 | 167 | static void |
ninoderkinderen | 0:8d87bc453349 | 168 | autoip_handle_arp_conflict(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 169 | { |
ninoderkinderen | 0:8d87bc453349 | 170 | /* Somehow detect if we are defending or retreating */ |
ninoderkinderen | 0:8d87bc453349 | 171 | unsigned char defend = 1; /* tbd */ |
ninoderkinderen | 0:8d87bc453349 | 172 | |
ninoderkinderen | 0:8d87bc453349 | 173 | if(defend) { |
ninoderkinderen | 0:8d87bc453349 | 174 | if(netif->autoip->lastconflict > 0) { |
ninoderkinderen | 0:8d87bc453349 | 175 | /* retreat, there was a conflicting ARP in the last |
ninoderkinderen | 0:8d87bc453349 | 176 | * DEFEND_INTERVAL seconds |
ninoderkinderen | 0:8d87bc453349 | 177 | */ |
ninoderkinderen | 0:8d87bc453349 | 178 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
ninoderkinderen | 0:8d87bc453349 | 179 | ("autoip_handle_arp_conflict(): we are defending, but in DEFEND_INTERVAL, retreating\n")); |
ninoderkinderen | 0:8d87bc453349 | 180 | |
ninoderkinderen | 0:8d87bc453349 | 181 | /* TODO: close all TCP sessions */ |
ninoderkinderen | 0:8d87bc453349 | 182 | autoip_restart(netif); |
ninoderkinderen | 0:8d87bc453349 | 183 | } else { |
ninoderkinderen | 0:8d87bc453349 | 184 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
ninoderkinderen | 0:8d87bc453349 | 185 | ("autoip_handle_arp_conflict(): we are defend, send ARP Announce\n")); |
ninoderkinderen | 0:8d87bc453349 | 186 | autoip_arp_announce(netif); |
ninoderkinderen | 0:8d87bc453349 | 187 | netif->autoip->lastconflict = DEFEND_INTERVAL * AUTOIP_TICKS_PER_SECOND; |
ninoderkinderen | 0:8d87bc453349 | 188 | } |
ninoderkinderen | 0:8d87bc453349 | 189 | } else { |
ninoderkinderen | 0:8d87bc453349 | 190 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
ninoderkinderen | 0:8d87bc453349 | 191 | ("autoip_handle_arp_conflict(): we do not defend, retreating\n")); |
ninoderkinderen | 0:8d87bc453349 | 192 | /* TODO: close all TCP sessions */ |
ninoderkinderen | 0:8d87bc453349 | 193 | autoip_restart(netif); |
ninoderkinderen | 0:8d87bc453349 | 194 | } |
ninoderkinderen | 0:8d87bc453349 | 195 | } |
ninoderkinderen | 0:8d87bc453349 | 196 | |
ninoderkinderen | 0:8d87bc453349 | 197 | /** |
ninoderkinderen | 0:8d87bc453349 | 198 | * Create an IP-Address out of range 169.254.1.0 to 169.254.254.255 |
ninoderkinderen | 0:8d87bc453349 | 199 | * |
ninoderkinderen | 0:8d87bc453349 | 200 | * @param netif network interface on which create the IP-Address |
ninoderkinderen | 0:8d87bc453349 | 201 | * @param ipaddr ip address to initialize |
ninoderkinderen | 0:8d87bc453349 | 202 | */ |
ninoderkinderen | 0:8d87bc453349 | 203 | static void |
ninoderkinderen | 0:8d87bc453349 | 204 | autoip_create_addr(struct netif *netif, ip_addr_t *ipaddr) |
ninoderkinderen | 0:8d87bc453349 | 205 | { |
ninoderkinderen | 0:8d87bc453349 | 206 | /* Here we create an IP-Address out of range 169.254.1.0 to 169.254.254.255 |
ninoderkinderen | 0:8d87bc453349 | 207 | * compliant to RFC 3927 Section 2.1 |
ninoderkinderen | 0:8d87bc453349 | 208 | * We have 254 * 256 possibilities */ |
ninoderkinderen | 0:8d87bc453349 | 209 | |
ninoderkinderen | 0:8d87bc453349 | 210 | u32_t addr = ntohl(LWIP_AUTOIP_CREATE_SEED_ADDR(netif)); |
ninoderkinderen | 0:8d87bc453349 | 211 | addr += netif->autoip->tried_llipaddr; |
ninoderkinderen | 0:8d87bc453349 | 212 | addr = AUTOIP_NET | (addr & 0xffff); |
ninoderkinderen | 0:8d87bc453349 | 213 | /* Now, 169.254.0.0 <= addr <= 169.254.255.255 */ |
ninoderkinderen | 0:8d87bc453349 | 214 | |
ninoderkinderen | 0:8d87bc453349 | 215 | if (addr < AUTOIP_RANGE_START) { |
ninoderkinderen | 0:8d87bc453349 | 216 | addr += AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1; |
ninoderkinderen | 0:8d87bc453349 | 217 | } |
ninoderkinderen | 0:8d87bc453349 | 218 | if (addr > AUTOIP_RANGE_END) { |
ninoderkinderen | 0:8d87bc453349 | 219 | addr -= AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1; |
ninoderkinderen | 0:8d87bc453349 | 220 | } |
ninoderkinderen | 0:8d87bc453349 | 221 | LWIP_ASSERT("AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) && |
ninoderkinderen | 0:8d87bc453349 | 222 | (addr <= AUTOIP_RANGE_END)); |
ninoderkinderen | 0:8d87bc453349 | 223 | ip4_addr_set_u32(ipaddr, htonl(addr)); |
ninoderkinderen | 0:8d87bc453349 | 224 | |
ninoderkinderen | 0:8d87bc453349 | 225 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
ninoderkinderen | 0:8d87bc453349 | 226 | ("autoip_create_addr(): tried_llipaddr=%"U16_F", %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
ninoderkinderen | 0:8d87bc453349 | 227 | (u16_t)(netif->autoip->tried_llipaddr), ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), |
ninoderkinderen | 0:8d87bc453349 | 228 | ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr))); |
ninoderkinderen | 0:8d87bc453349 | 229 | } |
ninoderkinderen | 0:8d87bc453349 | 230 | |
ninoderkinderen | 0:8d87bc453349 | 231 | /** |
ninoderkinderen | 0:8d87bc453349 | 232 | * Sends an ARP probe from a network interface |
ninoderkinderen | 0:8d87bc453349 | 233 | * |
ninoderkinderen | 0:8d87bc453349 | 234 | * @param netif network interface used to send the probe |
ninoderkinderen | 0:8d87bc453349 | 235 | */ |
ninoderkinderen | 0:8d87bc453349 | 236 | static err_t |
ninoderkinderen | 0:8d87bc453349 | 237 | autoip_arp_probe(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 238 | { |
ninoderkinderen | 0:8d87bc453349 | 239 | return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, ðbroadcast, |
ninoderkinderen | 0:8d87bc453349 | 240 | (struct eth_addr *)netif->hwaddr, IP_ADDR_ANY, ðzero, |
ninoderkinderen | 0:8d87bc453349 | 241 | &netif->autoip->llipaddr, ARP_REQUEST); |
ninoderkinderen | 0:8d87bc453349 | 242 | } |
ninoderkinderen | 0:8d87bc453349 | 243 | |
ninoderkinderen | 0:8d87bc453349 | 244 | /** |
ninoderkinderen | 0:8d87bc453349 | 245 | * Sends an ARP announce from a network interface |
ninoderkinderen | 0:8d87bc453349 | 246 | * |
ninoderkinderen | 0:8d87bc453349 | 247 | * @param netif network interface used to send the announce |
ninoderkinderen | 0:8d87bc453349 | 248 | */ |
ninoderkinderen | 0:8d87bc453349 | 249 | static err_t |
ninoderkinderen | 0:8d87bc453349 | 250 | autoip_arp_announce(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 251 | { |
ninoderkinderen | 0:8d87bc453349 | 252 | return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, ðbroadcast, |
ninoderkinderen | 0:8d87bc453349 | 253 | (struct eth_addr *)netif->hwaddr, &netif->autoip->llipaddr, ðzero, |
ninoderkinderen | 0:8d87bc453349 | 254 | &netif->autoip->llipaddr, ARP_REQUEST); |
ninoderkinderen | 0:8d87bc453349 | 255 | } |
ninoderkinderen | 0:8d87bc453349 | 256 | |
ninoderkinderen | 0:8d87bc453349 | 257 | /** |
ninoderkinderen | 0:8d87bc453349 | 258 | * Configure interface for use with current LL IP-Address |
ninoderkinderen | 0:8d87bc453349 | 259 | * |
ninoderkinderen | 0:8d87bc453349 | 260 | * @param netif network interface to configure with current LL IP-Address |
ninoderkinderen | 0:8d87bc453349 | 261 | */ |
ninoderkinderen | 0:8d87bc453349 | 262 | static err_t |
ninoderkinderen | 0:8d87bc453349 | 263 | autoip_bind(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 264 | { |
ninoderkinderen | 0:8d87bc453349 | 265 | struct autoip *autoip = netif->autoip; |
ninoderkinderen | 0:8d87bc453349 | 266 | ip_addr_t sn_mask, gw_addr; |
ninoderkinderen | 0:8d87bc453349 | 267 | |
ninoderkinderen | 0:8d87bc453349 | 268 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
ninoderkinderen | 0:8d87bc453349 | 269 | ("autoip_bind(netif=%p) %c%c%"U16_F" %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
ninoderkinderen | 0:8d87bc453349 | 270 | (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num, |
ninoderkinderen | 0:8d87bc453349 | 271 | ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr), |
ninoderkinderen | 0:8d87bc453349 | 272 | ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr))); |
ninoderkinderen | 0:8d87bc453349 | 273 | |
ninoderkinderen | 0:8d87bc453349 | 274 | IP4_ADDR(&sn_mask, 255, 255, 0, 0); |
ninoderkinderen | 0:8d87bc453349 | 275 | IP4_ADDR(&gw_addr, 0, 0, 0, 0); |
ninoderkinderen | 0:8d87bc453349 | 276 | |
ninoderkinderen | 0:8d87bc453349 | 277 | netif_set_ipaddr(netif, &autoip->llipaddr); |
ninoderkinderen | 0:8d87bc453349 | 278 | netif_set_netmask(netif, &sn_mask); |
ninoderkinderen | 0:8d87bc453349 | 279 | netif_set_gw(netif, &gw_addr); |
ninoderkinderen | 0:8d87bc453349 | 280 | |
ninoderkinderen | 0:8d87bc453349 | 281 | /* bring the interface up */ |
ninoderkinderen | 0:8d87bc453349 | 282 | netif_set_up(netif); |
ninoderkinderen | 0:8d87bc453349 | 283 | |
ninoderkinderen | 0:8d87bc453349 | 284 | return ERR_OK; |
ninoderkinderen | 0:8d87bc453349 | 285 | } |
ninoderkinderen | 0:8d87bc453349 | 286 | |
ninoderkinderen | 0:8d87bc453349 | 287 | /** |
ninoderkinderen | 0:8d87bc453349 | 288 | * Start AutoIP client |
ninoderkinderen | 0:8d87bc453349 | 289 | * |
ninoderkinderen | 0:8d87bc453349 | 290 | * @param netif network interface on which start the AutoIP client |
ninoderkinderen | 0:8d87bc453349 | 291 | */ |
ninoderkinderen | 0:8d87bc453349 | 292 | err_t |
ninoderkinderen | 0:8d87bc453349 | 293 | autoip_start(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 294 | { |
ninoderkinderen | 0:8d87bc453349 | 295 | struct autoip *autoip = netif->autoip; |
ninoderkinderen | 0:8d87bc453349 | 296 | err_t result = ERR_OK; |
ninoderkinderen | 0:8d87bc453349 | 297 | |
ninoderkinderen | 0:8d87bc453349 | 298 | if(netif_is_up(netif)) { |
ninoderkinderen | 0:8d87bc453349 | 299 | netif_set_down(netif); |
ninoderkinderen | 0:8d87bc453349 | 300 | } |
ninoderkinderen | 0:8d87bc453349 | 301 | |
ninoderkinderen | 0:8d87bc453349 | 302 | /* Set IP-Address, Netmask and Gateway to 0 to make sure that |
ninoderkinderen | 0:8d87bc453349 | 303 | * ARP Packets are formed correctly |
ninoderkinderen | 0:8d87bc453349 | 304 | */ |
ninoderkinderen | 0:8d87bc453349 | 305 | ip_addr_set_zero(&netif->ip_addr); |
ninoderkinderen | 0:8d87bc453349 | 306 | ip_addr_set_zero(&netif->netmask); |
ninoderkinderen | 0:8d87bc453349 | 307 | ip_addr_set_zero(&netif->gw); |
ninoderkinderen | 0:8d87bc453349 | 308 | |
ninoderkinderen | 0:8d87bc453349 | 309 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
ninoderkinderen | 0:8d87bc453349 | 310 | ("autoip_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], |
ninoderkinderen | 0:8d87bc453349 | 311 | netif->name[1], (u16_t)netif->num)); |
ninoderkinderen | 0:8d87bc453349 | 312 | if(autoip == NULL) { |
ninoderkinderen | 0:8d87bc453349 | 313 | /* no AutoIP client attached yet? */ |
ninoderkinderen | 0:8d87bc453349 | 314 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
ninoderkinderen | 0:8d87bc453349 | 315 | ("autoip_start(): starting new AUTOIP client\n")); |
ninoderkinderen | 0:8d87bc453349 | 316 | autoip = (struct autoip *)mem_malloc(sizeof(struct autoip)); |
ninoderkinderen | 0:8d87bc453349 | 317 | if(autoip == NULL) { |
ninoderkinderen | 0:8d87bc453349 | 318 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
ninoderkinderen | 0:8d87bc453349 | 319 | ("autoip_start(): could not allocate autoip\n")); |
ninoderkinderen | 0:8d87bc453349 | 320 | return ERR_MEM; |
ninoderkinderen | 0:8d87bc453349 | 321 | } |
ninoderkinderen | 0:8d87bc453349 | 322 | memset(autoip, 0, sizeof(struct autoip)); |
ninoderkinderen | 0:8d87bc453349 | 323 | /* store this AutoIP client in the netif */ |
ninoderkinderen | 0:8d87bc453349 | 324 | netif->autoip = autoip; |
ninoderkinderen | 0:8d87bc453349 | 325 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip")); |
ninoderkinderen | 0:8d87bc453349 | 326 | } else { |
ninoderkinderen | 0:8d87bc453349 | 327 | autoip->state = AUTOIP_STATE_OFF; |
ninoderkinderen | 0:8d87bc453349 | 328 | autoip->ttw = 0; |
ninoderkinderen | 0:8d87bc453349 | 329 | autoip->sent_num = 0; |
ninoderkinderen | 0:8d87bc453349 | 330 | ip_addr_set_zero(&autoip->llipaddr); |
ninoderkinderen | 0:8d87bc453349 | 331 | autoip->lastconflict = 0; |
ninoderkinderen | 0:8d87bc453349 | 332 | } |
ninoderkinderen | 0:8d87bc453349 | 333 | |
ninoderkinderen | 0:8d87bc453349 | 334 | autoip_create_addr(netif, &(autoip->llipaddr)); |
ninoderkinderen | 0:8d87bc453349 | 335 | autoip_start_probing(netif); |
ninoderkinderen | 0:8d87bc453349 | 336 | |
ninoderkinderen | 0:8d87bc453349 | 337 | return result; |
ninoderkinderen | 0:8d87bc453349 | 338 | } |
ninoderkinderen | 0:8d87bc453349 | 339 | |
ninoderkinderen | 0:8d87bc453349 | 340 | static void |
ninoderkinderen | 0:8d87bc453349 | 341 | autoip_start_probing(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 342 | { |
ninoderkinderen | 0:8d87bc453349 | 343 | struct autoip *autoip = netif->autoip; |
ninoderkinderen | 0:8d87bc453349 | 344 | |
ninoderkinderen | 0:8d87bc453349 | 345 | autoip->state = AUTOIP_STATE_PROBING; |
ninoderkinderen | 0:8d87bc453349 | 346 | autoip->sent_num = 0; |
ninoderkinderen | 0:8d87bc453349 | 347 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
ninoderkinderen | 0:8d87bc453349 | 348 | ("autoip_start_probing(): changing state to PROBING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
ninoderkinderen | 0:8d87bc453349 | 349 | ip4_addr1_16(&netif->autoip->llipaddr), ip4_addr2_16(&netif->autoip->llipaddr), |
ninoderkinderen | 0:8d87bc453349 | 350 | ip4_addr3_16(&netif->autoip->llipaddr), ip4_addr4_16(&netif->autoip->llipaddr))); |
ninoderkinderen | 0:8d87bc453349 | 351 | |
ninoderkinderen | 0:8d87bc453349 | 352 | /* time to wait to first probe, this is randomly |
ninoderkinderen | 0:8d87bc453349 | 353 | * choosen out of 0 to PROBE_WAIT seconds. |
ninoderkinderen | 0:8d87bc453349 | 354 | * compliant to RFC 3927 Section 2.2.1 |
ninoderkinderen | 0:8d87bc453349 | 355 | */ |
ninoderkinderen | 0:8d87bc453349 | 356 | autoip->ttw = (u16_t)(LWIP_AUTOIP_RAND(netif) % (PROBE_WAIT * AUTOIP_TICKS_PER_SECOND)); |
ninoderkinderen | 0:8d87bc453349 | 357 | |
ninoderkinderen | 0:8d87bc453349 | 358 | /* |
ninoderkinderen | 0:8d87bc453349 | 359 | * if we tried more then MAX_CONFLICTS we must limit our rate for |
ninoderkinderen | 0:8d87bc453349 | 360 | * accquiring and probing address |
ninoderkinderen | 0:8d87bc453349 | 361 | * compliant to RFC 3927 Section 2.2.1 |
ninoderkinderen | 0:8d87bc453349 | 362 | */ |
ninoderkinderen | 0:8d87bc453349 | 363 | if(autoip->tried_llipaddr > MAX_CONFLICTS) { |
ninoderkinderen | 0:8d87bc453349 | 364 | autoip->ttw = RATE_LIMIT_INTERVAL * AUTOIP_TICKS_PER_SECOND; |
ninoderkinderen | 0:8d87bc453349 | 365 | } |
ninoderkinderen | 0:8d87bc453349 | 366 | } |
ninoderkinderen | 0:8d87bc453349 | 367 | |
ninoderkinderen | 0:8d87bc453349 | 368 | /** |
ninoderkinderen | 0:8d87bc453349 | 369 | * Handle a possible change in the network configuration. |
ninoderkinderen | 0:8d87bc453349 | 370 | * |
ninoderkinderen | 0:8d87bc453349 | 371 | * If there is an AutoIP address configured, take the interface down |
ninoderkinderen | 0:8d87bc453349 | 372 | * and begin probing with the same address. |
ninoderkinderen | 0:8d87bc453349 | 373 | */ |
ninoderkinderen | 0:8d87bc453349 | 374 | void |
ninoderkinderen | 0:8d87bc453349 | 375 | autoip_network_changed(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 376 | { |
ninoderkinderen | 0:8d87bc453349 | 377 | if (netif->autoip && netif->autoip->state != AUTOIP_STATE_OFF) { |
ninoderkinderen | 0:8d87bc453349 | 378 | netif_set_down(netif); |
ninoderkinderen | 0:8d87bc453349 | 379 | autoip_start_probing(netif); |
ninoderkinderen | 0:8d87bc453349 | 380 | } |
ninoderkinderen | 0:8d87bc453349 | 381 | } |
ninoderkinderen | 0:8d87bc453349 | 382 | |
ninoderkinderen | 0:8d87bc453349 | 383 | /** |
ninoderkinderen | 0:8d87bc453349 | 384 | * Stop AutoIP client |
ninoderkinderen | 0:8d87bc453349 | 385 | * |
ninoderkinderen | 0:8d87bc453349 | 386 | * @param netif network interface on which stop the AutoIP client |
ninoderkinderen | 0:8d87bc453349 | 387 | */ |
ninoderkinderen | 0:8d87bc453349 | 388 | err_t |
ninoderkinderen | 0:8d87bc453349 | 389 | autoip_stop(struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 390 | { |
ninoderkinderen | 0:8d87bc453349 | 391 | netif->autoip->state = AUTOIP_STATE_OFF; |
ninoderkinderen | 0:8d87bc453349 | 392 | netif_set_down(netif); |
ninoderkinderen | 0:8d87bc453349 | 393 | return ERR_OK; |
ninoderkinderen | 0:8d87bc453349 | 394 | } |
ninoderkinderen | 0:8d87bc453349 | 395 | |
ninoderkinderen | 0:8d87bc453349 | 396 | /** |
ninoderkinderen | 0:8d87bc453349 | 397 | * Has to be called in loop every AUTOIP_TMR_INTERVAL milliseconds |
ninoderkinderen | 0:8d87bc453349 | 398 | */ |
ninoderkinderen | 0:8d87bc453349 | 399 | void |
ninoderkinderen | 0:8d87bc453349 | 400 | autoip_tmr() |
ninoderkinderen | 0:8d87bc453349 | 401 | { |
ninoderkinderen | 0:8d87bc453349 | 402 | struct netif *netif = netif_list; |
ninoderkinderen | 0:8d87bc453349 | 403 | /* loop through netif's */ |
ninoderkinderen | 0:8d87bc453349 | 404 | while (netif != NULL) { |
ninoderkinderen | 0:8d87bc453349 | 405 | /* only act on AutoIP configured interfaces */ |
ninoderkinderen | 0:8d87bc453349 | 406 | if (netif->autoip != NULL) { |
ninoderkinderen | 0:8d87bc453349 | 407 | if(netif->autoip->lastconflict > 0) { |
ninoderkinderen | 0:8d87bc453349 | 408 | netif->autoip->lastconflict--; |
ninoderkinderen | 0:8d87bc453349 | 409 | } |
ninoderkinderen | 0:8d87bc453349 | 410 | |
ninoderkinderen | 0:8d87bc453349 | 411 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
ninoderkinderen | 0:8d87bc453349 | 412 | ("autoip_tmr() AutoIP-State: %"U16_F", ttw=%"U16_F"\n", |
ninoderkinderen | 0:8d87bc453349 | 413 | (u16_t)(netif->autoip->state), netif->autoip->ttw)); |
ninoderkinderen | 0:8d87bc453349 | 414 | |
ninoderkinderen | 0:8d87bc453349 | 415 | switch(netif->autoip->state) { |
ninoderkinderen | 0:8d87bc453349 | 416 | case AUTOIP_STATE_PROBING: |
ninoderkinderen | 0:8d87bc453349 | 417 | if(netif->autoip->ttw > 0) { |
ninoderkinderen | 0:8d87bc453349 | 418 | netif->autoip->ttw--; |
ninoderkinderen | 0:8d87bc453349 | 419 | } else { |
ninoderkinderen | 0:8d87bc453349 | 420 | if(netif->autoip->sent_num >= PROBE_NUM) { |
ninoderkinderen | 0:8d87bc453349 | 421 | netif->autoip->state = AUTOIP_STATE_ANNOUNCING; |
ninoderkinderen | 0:8d87bc453349 | 422 | netif->autoip->sent_num = 0; |
ninoderkinderen | 0:8d87bc453349 | 423 | netif->autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND; |
ninoderkinderen | 0:8d87bc453349 | 424 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
ninoderkinderen | 0:8d87bc453349 | 425 | ("autoip_tmr(): changing state to ANNOUNCING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
ninoderkinderen | 0:8d87bc453349 | 426 | ip4_addr1_16(&netif->autoip->llipaddr), ip4_addr2_16(&netif->autoip->llipaddr), |
ninoderkinderen | 0:8d87bc453349 | 427 | ip4_addr3_16(&netif->autoip->llipaddr), ip4_addr4_16(&netif->autoip->llipaddr))); |
ninoderkinderen | 0:8d87bc453349 | 428 | } else { |
ninoderkinderen | 0:8d87bc453349 | 429 | autoip_arp_probe(netif); |
ninoderkinderen | 0:8d87bc453349 | 430 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
ninoderkinderen | 0:8d87bc453349 | 431 | ("autoip_tmr() PROBING Sent Probe\n")); |
ninoderkinderen | 0:8d87bc453349 | 432 | netif->autoip->sent_num++; |
ninoderkinderen | 0:8d87bc453349 | 433 | /* calculate time to wait to next probe */ |
ninoderkinderen | 0:8d87bc453349 | 434 | netif->autoip->ttw = (u16_t)((LWIP_AUTOIP_RAND(netif) % |
ninoderkinderen | 0:8d87bc453349 | 435 | ((PROBE_MAX - PROBE_MIN) * AUTOIP_TICKS_PER_SECOND) ) + |
ninoderkinderen | 0:8d87bc453349 | 436 | PROBE_MIN * AUTOIP_TICKS_PER_SECOND); |
ninoderkinderen | 0:8d87bc453349 | 437 | } |
ninoderkinderen | 0:8d87bc453349 | 438 | } |
ninoderkinderen | 0:8d87bc453349 | 439 | break; |
ninoderkinderen | 0:8d87bc453349 | 440 | |
ninoderkinderen | 0:8d87bc453349 | 441 | case AUTOIP_STATE_ANNOUNCING: |
ninoderkinderen | 0:8d87bc453349 | 442 | if(netif->autoip->ttw > 0) { |
ninoderkinderen | 0:8d87bc453349 | 443 | netif->autoip->ttw--; |
ninoderkinderen | 0:8d87bc453349 | 444 | } else { |
ninoderkinderen | 0:8d87bc453349 | 445 | if(netif->autoip->sent_num == 0) { |
ninoderkinderen | 0:8d87bc453349 | 446 | /* We are here the first time, so we waited ANNOUNCE_WAIT seconds |
ninoderkinderen | 0:8d87bc453349 | 447 | * Now we can bind to an IP address and use it. |
ninoderkinderen | 0:8d87bc453349 | 448 | * |
ninoderkinderen | 0:8d87bc453349 | 449 | * autoip_bind calls netif_set_up. This triggers a gratuitous ARP |
ninoderkinderen | 0:8d87bc453349 | 450 | * which counts as an announcement. |
ninoderkinderen | 0:8d87bc453349 | 451 | */ |
ninoderkinderen | 0:8d87bc453349 | 452 | autoip_bind(netif); |
ninoderkinderen | 0:8d87bc453349 | 453 | } else { |
ninoderkinderen | 0:8d87bc453349 | 454 | autoip_arp_announce(netif); |
ninoderkinderen | 0:8d87bc453349 | 455 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
ninoderkinderen | 0:8d87bc453349 | 456 | ("autoip_tmr() ANNOUNCING Sent Announce\n")); |
ninoderkinderen | 0:8d87bc453349 | 457 | } |
ninoderkinderen | 0:8d87bc453349 | 458 | netif->autoip->ttw = ANNOUNCE_INTERVAL * AUTOIP_TICKS_PER_SECOND; |
ninoderkinderen | 0:8d87bc453349 | 459 | netif->autoip->sent_num++; |
ninoderkinderen | 0:8d87bc453349 | 460 | |
ninoderkinderen | 0:8d87bc453349 | 461 | if(netif->autoip->sent_num >= ANNOUNCE_NUM) { |
ninoderkinderen | 0:8d87bc453349 | 462 | netif->autoip->state = AUTOIP_STATE_BOUND; |
ninoderkinderen | 0:8d87bc453349 | 463 | netif->autoip->sent_num = 0; |
ninoderkinderen | 0:8d87bc453349 | 464 | netif->autoip->ttw = 0; |
ninoderkinderen | 0:8d87bc453349 | 465 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
ninoderkinderen | 0:8d87bc453349 | 466 | ("autoip_tmr(): changing state to BOUND: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
ninoderkinderen | 0:8d87bc453349 | 467 | ip4_addr1_16(&netif->autoip->llipaddr), ip4_addr2_16(&netif->autoip->llipaddr), |
ninoderkinderen | 0:8d87bc453349 | 468 | ip4_addr3_16(&netif->autoip->llipaddr), ip4_addr4_16(&netif->autoip->llipaddr))); |
ninoderkinderen | 0:8d87bc453349 | 469 | } |
ninoderkinderen | 0:8d87bc453349 | 470 | } |
ninoderkinderen | 0:8d87bc453349 | 471 | break; |
ninoderkinderen | 0:8d87bc453349 | 472 | } |
ninoderkinderen | 0:8d87bc453349 | 473 | } |
ninoderkinderen | 0:8d87bc453349 | 474 | /* proceed to next network interface */ |
ninoderkinderen | 0:8d87bc453349 | 475 | netif = netif->next; |
ninoderkinderen | 0:8d87bc453349 | 476 | } |
ninoderkinderen | 0:8d87bc453349 | 477 | } |
ninoderkinderen | 0:8d87bc453349 | 478 | |
ninoderkinderen | 0:8d87bc453349 | 479 | /** |
ninoderkinderen | 0:8d87bc453349 | 480 | * Handles every incoming ARP Packet, called by etharp_arp_input. |
ninoderkinderen | 0:8d87bc453349 | 481 | * |
ninoderkinderen | 0:8d87bc453349 | 482 | * @param netif network interface to use for autoip processing |
ninoderkinderen | 0:8d87bc453349 | 483 | * @param hdr Incoming ARP packet |
ninoderkinderen | 0:8d87bc453349 | 484 | */ |
ninoderkinderen | 0:8d87bc453349 | 485 | void |
ninoderkinderen | 0:8d87bc453349 | 486 | autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr) |
ninoderkinderen | 0:8d87bc453349 | 487 | { |
ninoderkinderen | 0:8d87bc453349 | 488 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_arp_reply()\n")); |
ninoderkinderen | 0:8d87bc453349 | 489 | if ((netif->autoip != NULL) && (netif->autoip->state != AUTOIP_STATE_OFF)) { |
ninoderkinderen | 0:8d87bc453349 | 490 | /* when ip.src == llipaddr && hw.src != netif->hwaddr |
ninoderkinderen | 0:8d87bc453349 | 491 | * |
ninoderkinderen | 0:8d87bc453349 | 492 | * when probing ip.dst == llipaddr && hw.src != netif->hwaddr |
ninoderkinderen | 0:8d87bc453349 | 493 | * we have a conflict and must solve it |
ninoderkinderen | 0:8d87bc453349 | 494 | */ |
ninoderkinderen | 0:8d87bc453349 | 495 | ip_addr_t sipaddr, dipaddr; |
ninoderkinderen | 0:8d87bc453349 | 496 | struct eth_addr netifaddr; |
ninoderkinderen | 0:8d87bc453349 | 497 | netifaddr.addr[0] = netif->hwaddr[0]; |
ninoderkinderen | 0:8d87bc453349 | 498 | netifaddr.addr[1] = netif->hwaddr[1]; |
ninoderkinderen | 0:8d87bc453349 | 499 | netifaddr.addr[2] = netif->hwaddr[2]; |
ninoderkinderen | 0:8d87bc453349 | 500 | netifaddr.addr[3] = netif->hwaddr[3]; |
ninoderkinderen | 0:8d87bc453349 | 501 | netifaddr.addr[4] = netif->hwaddr[4]; |
ninoderkinderen | 0:8d87bc453349 | 502 | netifaddr.addr[5] = netif->hwaddr[5]; |
ninoderkinderen | 0:8d87bc453349 | 503 | |
ninoderkinderen | 0:8d87bc453349 | 504 | /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without |
ninoderkinderen | 0:8d87bc453349 | 505 | * structure packing (not using structure copy which breaks strict-aliasing rules). |
ninoderkinderen | 0:8d87bc453349 | 506 | */ |
ninoderkinderen | 0:8d87bc453349 | 507 | IPADDR2_COPY(&sipaddr, &hdr->sipaddr); |
ninoderkinderen | 0:8d87bc453349 | 508 | IPADDR2_COPY(&dipaddr, &hdr->dipaddr); |
ninoderkinderen | 0:8d87bc453349 | 509 | |
ninoderkinderen | 0:8d87bc453349 | 510 | if ((netif->autoip->state == AUTOIP_STATE_PROBING) || |
ninoderkinderen | 0:8d87bc453349 | 511 | ((netif->autoip->state == AUTOIP_STATE_ANNOUNCING) && |
ninoderkinderen | 0:8d87bc453349 | 512 | (netif->autoip->sent_num == 0))) { |
ninoderkinderen | 0:8d87bc453349 | 513 | /* RFC 3927 Section 2.2.1: |
ninoderkinderen | 0:8d87bc453349 | 514 | * from beginning to after ANNOUNCE_WAIT |
ninoderkinderen | 0:8d87bc453349 | 515 | * seconds we have a conflict if |
ninoderkinderen | 0:8d87bc453349 | 516 | * ip.src == llipaddr OR |
ninoderkinderen | 0:8d87bc453349 | 517 | * ip.dst == llipaddr && hw.src != own hwaddr |
ninoderkinderen | 0:8d87bc453349 | 518 | */ |
ninoderkinderen | 0:8d87bc453349 | 519 | if ((ip_addr_cmp(&sipaddr, &netif->autoip->llipaddr)) || |
ninoderkinderen | 0:8d87bc453349 | 520 | (ip_addr_cmp(&dipaddr, &netif->autoip->llipaddr) && |
ninoderkinderen | 0:8d87bc453349 | 521 | !eth_addr_cmp(&netifaddr, &hdr->shwaddr))) { |
ninoderkinderen | 0:8d87bc453349 | 522 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING, |
ninoderkinderen | 0:8d87bc453349 | 523 | ("autoip_arp_reply(): Probe Conflict detected\n")); |
ninoderkinderen | 0:8d87bc453349 | 524 | autoip_restart(netif); |
ninoderkinderen | 0:8d87bc453349 | 525 | } |
ninoderkinderen | 0:8d87bc453349 | 526 | } else { |
ninoderkinderen | 0:8d87bc453349 | 527 | /* RFC 3927 Section 2.5: |
ninoderkinderen | 0:8d87bc453349 | 528 | * in any state we have a conflict if |
ninoderkinderen | 0:8d87bc453349 | 529 | * ip.src == llipaddr && hw.src != own hwaddr |
ninoderkinderen | 0:8d87bc453349 | 530 | */ |
ninoderkinderen | 0:8d87bc453349 | 531 | if (ip_addr_cmp(&sipaddr, &netif->autoip->llipaddr) && |
ninoderkinderen | 0:8d87bc453349 | 532 | !eth_addr_cmp(&netifaddr, &hdr->shwaddr)) { |
ninoderkinderen | 0:8d87bc453349 | 533 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING, |
ninoderkinderen | 0:8d87bc453349 | 534 | ("autoip_arp_reply(): Conflicting ARP-Packet detected\n")); |
ninoderkinderen | 0:8d87bc453349 | 535 | autoip_handle_arp_conflict(netif); |
ninoderkinderen | 0:8d87bc453349 | 536 | } |
ninoderkinderen | 0:8d87bc453349 | 537 | } |
ninoderkinderen | 0:8d87bc453349 | 538 | } |
ninoderkinderen | 0:8d87bc453349 | 539 | } |
ninoderkinderen | 0:8d87bc453349 | 540 | |
ninoderkinderen | 0:8d87bc453349 | 541 | #endif /* LWIP_AUTOIP */ |