Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

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