I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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