Deprecated fork of old network stack source from github. Please use official library instead: https://mbed.org/users/mbed_official/code/EthernetInterface/

Committer:
AdamGreen
Date:
Sat Sep 07 21:38:42 2013 +0000
Revision:
0:3b00827bb0b7
Sync to latest network stack source from github.

Who changed what in which revision?

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