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