NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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