source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/SERVICES/LWIP/lwip-1.3.2/src/core/ipv4/autoip.c @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

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