kEIS

Dependents:   EthernetInterface_ccx

Fork of lwip by mbed official

Committer:
mbed_official
Date:
Fri Jun 22 09:25:39 2012 +0000
Revision:
0:51ac1d130fd4
Initial import from lwip-1.4.0: http://download.savannah.gnu.org/releases/lwip/lwip-1.4.0.zip

Who changed what in which revision?

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