UbixOS V2  2.0
autoip.c
Go to the documentation of this file.
1 
29 /*
30  *
31  * Copyright (c) 2007 Dominik Spies <kontakt@dspies.de>
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without modification,
35  * are permitted provided that the following conditions are met:
36  *
37  * 1. Redistributions of source code must retain the above copyright notice,
38  * this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright notice,
40  * this list of conditions and the following disclaimer in the documentation
41  * and/or other materials provided with the distribution.
42  * 3. The name of the author may not be used to endorse or promote products
43  * derived from this software without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
46  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
47  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
48  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
49  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
50  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
53  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
54  * OF SUCH DAMAGE.
55  *
56  * Author: Dominik Spies <kontakt@dspies.de>
57  */
58 
59 #include "net/opt.h"
60 
61 #if LWIP_IPV4 && LWIP_AUTOIP /* don't build if not configured for use in lwipopts.h */
62 
63 #include "net/mem.h"
64 /* #include "net/udp.h" */
65 #include "net/ip_addr.h"
66 #include "net/netif.h"
67 #include "net/autoip.h"
68 #include "net/etharp.h"
69 #include "net/prot/autoip.h"
70 
71 #include <string.h>
72 
75 #ifndef LWIP_AUTOIP_RAND
76 #define LWIP_AUTOIP_RAND(netif) ( (((u32_t)((netif->hwaddr[5]) & 0xff) << 24) | \
77  ((u32_t)((netif->hwaddr[3]) & 0xff) << 16) | \
78  ((u32_t)((netif->hwaddr[2]) & 0xff) << 8) | \
79  ((u32_t)((netif->hwaddr[4]) & 0xff))) + \
80  (netif_autoip_data(netif)? netif_autoip_data(netif)->tried_llipaddr : 0))
81 #endif /* LWIP_AUTOIP_RAND */
82 
87 #ifndef LWIP_AUTOIP_CREATE_SEED_ADDR
88 #define LWIP_AUTOIP_CREATE_SEED_ADDR(netif) \
89  lwip_htonl(AUTOIP_RANGE_START + ((u32_t)(((u8_t)(netif->hwaddr[4])) | \
90  ((u32_t)((u8_t)(netif->hwaddr[5]))) << 8)))
91 #endif /* LWIP_AUTOIP_CREATE_SEED_ADDR */
92 
93 /* static functions */
94 static err_t autoip_arp_announce(struct netif *netif);
95 static void autoip_start_probing(struct netif *netif);
96 
105 void
106 autoip_set_struct(struct netif *netif, struct autoip *autoip)
107 {
108  LWIP_ASSERT("netif != NULL", netif != NULL);
109  LWIP_ASSERT("autoip != NULL", autoip != NULL);
110  LWIP_ASSERT("netif already has a struct autoip set",
111  netif_autoip_data(netif) == NULL);
112 
113  /* clear data structure */
114  memset(autoip, 0, sizeof(struct autoip));
115  /* autoip->state = AUTOIP_STATE_OFF; */
116  netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip);
117 }
118 
123 static void
124 autoip_restart(struct netif *netif)
125 {
126  struct autoip* autoip = netif_autoip_data(netif);
127  autoip->tried_llipaddr++;
128  autoip_start(netif);
129 }
130 
134 static void
135 autoip_handle_arp_conflict(struct netif *netif)
136 {
137  struct autoip* autoip = netif_autoip_data(netif);
138 
139  /* RFC3927, 2.5 "Conflict Detection and Defense" allows two options where
140  a) means retreat on the first conflict and
141  b) allows to keep an already configured address when having only one
142  conflict in 10 seconds
143  We use option b) since it helps to improve the chance that one of the two
144  conflicting hosts may be able to retain its address. */
145 
146  if (autoip->lastconflict > 0) {
147  /* retreat, there was a conflicting ARP in the last DEFEND_INTERVAL seconds */
149  ("autoip_handle_arp_conflict(): we are defending, but in DEFEND_INTERVAL, retreating\n"));
150 
151  /* Active TCP sessions are aborted when removing the ip addresss */
152  autoip_restart(netif);
153  } else {
155  ("autoip_handle_arp_conflict(): we are defend, send ARP Announce\n"));
156  autoip_arp_announce(netif);
157  autoip->lastconflict = DEFEND_INTERVAL * AUTOIP_TICKS_PER_SECOND;
158  }
159 }
160 
167 static void
168 autoip_create_addr(struct netif *netif, ip4_addr_t *ipaddr)
169 {
170  struct autoip* autoip = netif_autoip_data(netif);
171 
172  /* Here we create an IP-Address out of range 169.254.1.0 to 169.254.254.255
173  * compliant to RFC 3927 Section 2.1
174  * We have 254 * 256 possibilities */
175 
176  u32_t addr = lwip_ntohl(LWIP_AUTOIP_CREATE_SEED_ADDR(netif));
177  addr += autoip->tried_llipaddr;
178  addr = AUTOIP_NET | (addr & 0xffff);
179  /* Now, 169.254.0.0 <= addr <= 169.254.255.255 */
180 
181  if (addr < AUTOIP_RANGE_START) {
182  addr += AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
183  }
184  if (addr > AUTOIP_RANGE_END) {
185  addr -= AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
186  }
187  LWIP_ASSERT("AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) &&
188  (addr <= AUTOIP_RANGE_END));
189  ip4_addr_set_u32(ipaddr, lwip_htonl(addr));
190 
192  ("autoip_create_addr(): tried_llipaddr=%"U16_F", %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
193  (u16_t)(autoip->tried_llipaddr), ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr),
194  ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
195 }
196 
202 static err_t
203 autoip_arp_probe(struct netif *netif)
204 {
205  struct autoip* autoip = netif_autoip_data(netif);
206  /* this works because netif->ip_addr is ANY */
207  return etharp_request(netif, &autoip->llipaddr);
208 }
209 
215 static err_t
216 autoip_arp_announce(struct netif *netif)
217 {
218  return etharp_gratuitous(netif);
219 }
220 
226 static err_t
227 autoip_bind(struct netif *netif)
228 {
229  struct autoip* autoip = netif_autoip_data(netif);
230  ip4_addr_t sn_mask, gw_addr;
231 
233  ("autoip_bind(netif=%p) %c%c%"U16_F" %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
234  (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num,
235  ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
236  ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
237 
238  IP4_ADDR(&sn_mask, 255, 255, 0, 0);
239  IP4_ADDR(&gw_addr, 0, 0, 0, 0);
240 
241  netif_set_addr(netif, &autoip->llipaddr, &sn_mask, &gw_addr);
242  /* interface is used by routing now that an address is set */
243 
244  return ERR_OK;
245 }
246 
253 err_t
254 autoip_start(struct netif *netif)
255 {
256  struct autoip* autoip = netif_autoip_data(netif);
257  err_t result = ERR_OK;
258 
259  LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;);
260 
261  /* Set IP-Address, Netmask and Gateway to 0 to make sure that
262  * ARP Packets are formed correctly
263  */
264  netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
265 
267  ("autoip_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0],
268  netif->name[1], (u16_t)netif->num));
269  if (autoip == NULL) {
270  /* no AutoIP client attached yet? */
272  ("autoip_start(): starting new AUTOIP client\n"));
273  autoip = (struct autoip *)mem_malloc(sizeof(struct autoip));
274  if (autoip == NULL) {
276  ("autoip_start(): could not allocate autoip\n"));
277  return ERR_MEM;
278  }
279  memset(autoip, 0, sizeof(struct autoip));
280  /* store this AutoIP client in the netif */
281  netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip);
282  LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip"));
283  } else {
284  autoip->state = AUTOIP_STATE_OFF;
285  autoip->ttw = 0;
286  autoip->sent_num = 0;
287  ip4_addr_set_zero(&autoip->llipaddr);
288  autoip->lastconflict = 0;
289  }
290 
291  autoip_create_addr(netif, &(autoip->llipaddr));
292  autoip_start_probing(netif);
293 
294  return result;
295 }
296 
297 static void
298 autoip_start_probing(struct netif *netif)
299 {
300  struct autoip* autoip = netif_autoip_data(netif);
301 
302  autoip->state = AUTOIP_STATE_PROBING;
303  autoip->sent_num = 0;
305  ("autoip_start_probing(): changing state to PROBING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
306  ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
307  ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
308 
309  /* time to wait to first probe, this is randomly
310  * chosen out of 0 to PROBE_WAIT seconds.
311  * compliant to RFC 3927 Section 2.2.1
312  */
313  autoip->ttw = (u16_t)(LWIP_AUTOIP_RAND(netif) % (PROBE_WAIT * AUTOIP_TICKS_PER_SECOND));
314 
315  /*
316  * if we tried more then MAX_CONFLICTS we must limit our rate for
317  * acquiring and probing address
318  * compliant to RFC 3927 Section 2.2.1
319  */
320  if (autoip->tried_llipaddr > MAX_CONFLICTS) {
321  autoip->ttw = RATE_LIMIT_INTERVAL * AUTOIP_TICKS_PER_SECOND;
322  }
323 }
324 
331 void
332 autoip_network_changed(struct netif *netif)
333 {
334  struct autoip* autoip = netif_autoip_data(netif);
335 
336  if (autoip && (autoip->state != AUTOIP_STATE_OFF)) {
337  autoip_start_probing(netif);
338  }
339 }
340 
347 err_t
348 autoip_stop(struct netif *netif)
349 {
350  struct autoip* autoip = netif_autoip_data(netif);
351 
352  if (autoip != NULL) {
353  autoip->state = AUTOIP_STATE_OFF;
354  if (ip4_addr_islinklocal(netif_ip4_addr(netif))) {
355  netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
356  }
357  }
358  return ERR_OK;
359 }
360 
364 void
365 autoip_tmr(void)
366 {
367  struct netif *netif = netif_list;
368  /* loop through netif's */
369  while (netif != NULL) {
370  struct autoip* autoip = netif_autoip_data(netif);
371  /* only act on AutoIP configured interfaces */
372  if (autoip != NULL) {
373  if (autoip->lastconflict > 0) {
374  autoip->lastconflict--;
375  }
376 
378  ("autoip_tmr() AutoIP-State: %"U16_F", ttw=%"U16_F"\n",
379  (u16_t)(autoip->state), autoip->ttw));
380 
381  if (autoip->ttw > 0) {
382  autoip->ttw--;
383  }
384 
385  switch(autoip->state) {
387  if (autoip->ttw == 0) {
388  if (autoip->sent_num >= PROBE_NUM) {
389  /* Switch to ANNOUNCING: now we can bind to an IP address and use it */
390  autoip->state = AUTOIP_STATE_ANNOUNCING;
391  autoip_bind(netif);
392  /* autoip_bind() calls netif_set_addr(): this triggers a gratuitous ARP
393  which counts as an announcement */
394  autoip->sent_num = 1;
395  autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND;
397  ("autoip_tmr(): changing state to ANNOUNCING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
398  ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
399  ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
400  } else {
401  autoip_arp_probe(netif);
402  LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_tmr() PROBING Sent Probe\n"));
403  autoip->sent_num++;
404  if (autoip->sent_num == PROBE_NUM) {
405  /* calculate time to wait to for announce */
406  autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND;
407  } else {
408  /* calculate time to wait to next probe */
409  autoip->ttw = (u16_t)((LWIP_AUTOIP_RAND(netif) %
410  ((PROBE_MAX - PROBE_MIN) * AUTOIP_TICKS_PER_SECOND) ) +
411  PROBE_MIN * AUTOIP_TICKS_PER_SECOND);
412  }
413  }
414  }
415  break;
416 
418  if (autoip->ttw == 0) {
419  autoip_arp_announce(netif);
420  LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_tmr() ANNOUNCING Sent Announce\n"));
421  autoip->ttw = ANNOUNCE_INTERVAL * AUTOIP_TICKS_PER_SECOND;
422  autoip->sent_num++;
423 
424  if (autoip->sent_num >= ANNOUNCE_NUM) {
425  autoip->state = AUTOIP_STATE_BOUND;
426  autoip->sent_num = 0;
427  autoip->ttw = 0;
429  ("autoip_tmr(): changing state to BOUND: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
430  ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
431  ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
432  }
433  }
434  break;
435 
436  default:
437  /* nothing to do in other states */
438  break;
439  }
440  }
441  /* proceed to next network interface */
442  netif = netif->next;
443  }
444 }
445 
452 void
453 autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr)
454 {
455  struct autoip* autoip = netif_autoip_data(netif);
456 
457  LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_arp_reply()\n"));
458  if ((autoip != NULL) && (autoip->state != AUTOIP_STATE_OFF)) {
459  /* when ip.src == llipaddr && hw.src != netif->hwaddr
460  *
461  * when probing ip.dst == llipaddr && hw.src != netif->hwaddr
462  * we have a conflict and must solve it
463  */
464  ip4_addr_t sipaddr, dipaddr;
465  struct eth_addr netifaddr;
466  ETHADDR16_COPY(netifaddr.addr, netif->hwaddr);
467 
468  /* Copy struct ip4_addr2 to aligned ip4_addr, to support compilers without
469  * structure packing (not using structure copy which breaks strict-aliasing rules).
470  */
471  IPADDR2_COPY(&sipaddr, &hdr->sipaddr);
472  IPADDR2_COPY(&dipaddr, &hdr->dipaddr);
473 
474  if (autoip->state == AUTOIP_STATE_PROBING) {
475  /* RFC 3927 Section 2.2.1:
476  * from beginning to after ANNOUNCE_WAIT
477  * seconds we have a conflict if
478  * ip.src == llipaddr OR
479  * ip.dst == llipaddr && hw.src != own hwaddr
480  */
481  if ((ip4_addr_cmp(&sipaddr, &autoip->llipaddr)) ||
482  (ip4_addr_isany_val(sipaddr) &&
483  ip4_addr_cmp(&dipaddr, &autoip->llipaddr) &&
484  !eth_addr_cmp(&netifaddr, &hdr->shwaddr))) {
486  ("autoip_arp_reply(): Probe Conflict detected\n"));
487  autoip_restart(netif);
488  }
489  } else {
490  /* RFC 3927 Section 2.5:
491  * in any state we have a conflict if
492  * ip.src == llipaddr && hw.src != own hwaddr
493  */
494  if (ip4_addr_cmp(&sipaddr, &autoip->llipaddr) &&
495  !eth_addr_cmp(&netifaddr, &hdr->shwaddr)) {
497  ("autoip_arp_reply(): Conflicting ARP-Packet detected\n"));
498  autoip_handle_arp_conflict(netif);
499  }
500  }
501  }
502 }
503 
510 u8_t
511 autoip_supplied_address(const struct netif *netif)
512 {
513  if ((netif != NULL) && (netif_autoip_data(netif) != NULL)) {
514  struct autoip* autoip = netif_autoip_data(netif);
515  return (autoip->state == AUTOIP_STATE_BOUND) || (autoip->state == AUTOIP_STATE_ANNOUNCING);
516  }
517  return 0;
518 }
519 
520 u8_t
521 autoip_accept_packet(struct netif *netif, const ip4_addr_t *addr)
522 {
523  struct autoip* autoip = netif_autoip_data(netif);
524  return (autoip != NULL) && ip4_addr_cmp(addr, &(autoip->llipaddr));
525 }
526 
527 #endif /* LWIP_IPV4 && LWIP_AUTOIP */
opt.h
PROBE_WAIT
#define PROBE_WAIT
Definition: autoip.h:55
LWIP_ASSERT
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
PROBE_NUM
#define PROBE_NUM
Definition: autoip.h:58
DEFEND_INTERVAL
#define DEFEND_INTERVAL
Definition: autoip.h:64
AUTOIP_STATE_PROBING
Definition: autoip.h:69
u16_t
uint16_t u16_t
Definition: arch.h:124
string.h
ANNOUNCE_INTERVAL
#define ANNOUNCE_INTERVAL
Definition: autoip.h:60
etharp_hdr
Definition: etharp.h:57
autoip.h
u32_t
uint32_t u32_t
Definition: arch.h:126
LWIP_DBG_TRACE
#define LWIP_DBG_TRACE
Definition: debug.h:83
PROBE_MAX
#define PROBE_MAX
Definition: autoip.h:57
ERR_MEM
Definition: err.h:65
netif::hwaddr
u8_t hwaddr[6U]
Definition: netif.h:311
netif.h
LWIP_ERROR
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:135
netif::num
u8_t num
Definition: netif.h:317
lwip_ntohl
#define lwip_ntohl(x)
Definition: def.h:78
PROBE_MIN
#define PROBE_MIN
Definition: autoip.h:56
AUTOIP_RANGE_START
#define AUTOIP_RANGE_START
Definition: autoip.h:50
u8_t
uint8_t u8_t
Definition: arch.h:122
AUTOIP_RANGE_END
#define AUTOIP_RANGE_END
Definition: autoip.h:52
netif
Definition: netif.h:233
ANNOUNCE_NUM
#define ANNOUNCE_NUM
Definition: autoip.h:59
ERR_ARG
Definition: err.h:96
eth_addr_cmp
#define eth_addr_cmp(addr1, addr2)
Definition: ethernet.h:164
lwip_htonl
u32_t lwip_htonl(u32_t n)
Definition: def.c:88
netif::name
char name[2]
Definition: netif.h:315
netif::next
struct netif * next
Definition: netif.h:235
ETHADDR16_COPY
#define ETHADDR16_COPY(dst, src)
Definition: ethernet.h:161
netif_list
struct netif * netif_list
Definition: netif.c:104
U16_F
#define U16_F
Definition: arch.h:148
ERR_OK
Definition: err.h:63
err_t
s8_t err_t
Definition: err.h:57
eth_addr
Definition: ethernet.h:58
AUTOIP_STATE_OFF
Definition: autoip.h:68
mem_malloc
void * mem_malloc(mem_size_t size)
Definition: mem.c:603
etharp.h
autoip.h
LWIP_DBG_LEVEL_WARNING
#define LWIP_DBG_LEVEL_WARNING
Definition: debug.h:55
memset
void * memset(void *dst, int c, size_t length)
RATE_LIMIT_INTERVAL
#define RATE_LIMIT_INTERVAL
Definition: autoip.h:63
AUTOIP_STATE_BOUND
Definition: autoip.h:71
MAX_CONFLICTS
#define MAX_CONFLICTS
Definition: autoip.h:62
ANNOUNCE_WAIT
#define ANNOUNCE_WAIT
Definition: autoip.h:61
mem.h
AUTOIP_DEBUG
#define AUTOIP_DEBUG
Definition: lwipopts.h:485
LWIP_DEBUGF
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:164
LWIP_DBG_STATE
#define LWIP_DBG_STATE
Definition: debug.h:85
AUTOIP_STATE_ANNOUNCING
Definition: autoip.h:70
ip_addr.h
netif_is_up
#define netif_is_up(netif)
Definition: netif.h:409
AUTOIP_NET
#define AUTOIP_NET
Definition: autoip.h:48
NULL
#define NULL
Definition: fat_string.h:17