Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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