3 * AutoIP Automatic LinkLocal IP Configuration
9 * Copyright (c) 2007 Dominik Spies <kontakt@dspies.de>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without modification,
13 * are permitted provided that the following conditions are met:
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.
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
34 * Author: Dominik Spies <kontakt@dspies.de>
36 * This is a AutoIP implementation for the lwIP TCP/IP stack. It aims to conform
40 * Please coordinate changes and requests with Dominik Spies
44 /*******************************************************************************
47 * define LWIP_AUTOIP 1 in your lwipopts.h
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 ....
57 * - Call autoip_start() after netif_add().
60 * - define LWIP_DHCP_AUTOIP_COOP 1 in your lwipopts.h.
61 * - Configure your DHCP Client.
67 #if LWIP_AUTOIP /* don't build if not configured for use in lwipopts.h */
71 #include "lwip/ip_addr.h"
72 #include "lwip/netif.h"
73 #include "lwip/autoip.h"
74 #include "netif/etharp.h"
80 #define AUTOIP_NET 0xA9FE0000
82 #define AUTOIP_RANGE_START (AUTOIP_NET | 0x0100)
84 #define AUTOIP_RANGE_END (AUTOIP_NET | 0xFEFF)
87 /** Pseudo random macro based on netif informations.
88 * You could use "rand()" from the C Library if you define LWIP_AUTOIP_RAND in lwipopts.h */
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 */
98 * Macro that generates the initial IP address to be tried by AUTOIP.
99 * If you want to override this, define it to something else in lwipopts.h.
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 */
107 /* static functions */
108 static void autoip_handle_arp_conflict(struct netif
*netif
);
110 /* creates a pseudo random LL IP-Address for a network interface */
111 static void autoip_create_addr(struct netif
*netif
, struct ip_addr
*ipaddr
);
113 /* sends an ARP probe */
114 static err_t
autoip_arp_probe(struct netif
*netif
);
116 /* sends an ARP announce */
117 static err_t
autoip_arp_announce(struct netif
*netif
);
119 /* configure interface for use with current LL IP-Address */
120 static err_t
autoip_bind(struct netif
*netif
);
122 /* start sending probes for llipaddr */
123 static void autoip_start_probing(struct netif
*netif
);
126 * Initialize this module
131 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
, ("autoip_init()\n"));
135 * Handle a IP address conflict after an ARP conflict detection
138 autoip_handle_arp_conflict(struct netif
*netif
)
140 /* Somehow detect if we are defending or retreating */
141 unsigned char defend
= 1; /* tbd */
144 if(netif
->autoip
->lastconflict
> 0) {
145 /* retreat, there was a conflicting ARP in the last
146 * DEFEND_INTERVAL seconds
148 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_STATE
,
149 ("autoip_handle_arp_conflict(): we are defending, but in DEFEND_INTERVAL, retreating\n"));
151 /* TODO: close all TCP sessions */
154 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_STATE
,
155 ("autoip_handle_arp_conflict(): we are defend, send ARP Announce\n"));
156 autoip_arp_announce(netif
);
157 netif
->autoip
->lastconflict
= DEFEND_INTERVAL
* AUTOIP_TICKS_PER_SECOND
;
160 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_STATE
,
161 ("autoip_handle_arp_conflict(): we do not defend, retreating\n"));
162 /* TODO: close all TCP sessions */
168 * Create an IP-Address out of range 169.254.1.0 to 169.254.254.255
170 * @param netif network interface on which create the IP-Address
171 * @param ipaddr ip address to initialize
174 autoip_create_addr(struct netif
*netif
, struct ip_addr
*ipaddr
)
176 /* Here we create an IP-Address out of range 169.254.1.0 to 169.254.254.255
177 * compliant to RFC 3927 Section 2.1
178 * We have 254 * 256 possibilities */
180 u32_t addr
= ntohl(LWIP_AUTOIP_CREATE_SEED_ADDR(netif
));
181 addr
+= netif
->autoip
->tried_llipaddr
;
182 addr
= AUTOIP_NET
| (addr
& 0xffff);
183 /* Now, 169.254.0.0 <= addr <= 169.254.255.255 */
185 if (addr
< AUTOIP_RANGE_START
) {
186 addr
+= AUTOIP_RANGE_END
- AUTOIP_RANGE_START
+ 1;
188 if (addr
> AUTOIP_RANGE_END
) {
189 addr
-= AUTOIP_RANGE_END
- AUTOIP_RANGE_START
+ 1;
191 LWIP_ASSERT("AUTOIP address not in range", (addr
>= AUTOIP_RANGE_START
) &&
192 (addr
<= AUTOIP_RANGE_END
));
193 ipaddr
->addr
= htonl(addr
);
195 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_STATE
,
196 ("autoip_create_addr(): tried_llipaddr=%"U16_F
", 0x%08"X32_F
"\n",
197 (u16_t
)(netif
->autoip
->tried_llipaddr
), (u32_t
)(ipaddr
->addr
)));
201 * Sends an ARP probe from a network interface
203 * @param netif network interface used to send the probe
206 autoip_arp_probe(struct netif
*netif
)
208 return etharp_raw(netif
, (struct eth_addr
*)netif
->hwaddr
, ðbroadcast
,
209 (struct eth_addr
*)netif
->hwaddr
, IP_ADDR_ANY
, ðzero
,
210 &netif
->autoip
->llipaddr
, ARP_REQUEST
);
214 * Sends an ARP announce from a network interface
216 * @param netif network interface used to send the announce
219 autoip_arp_announce(struct netif
*netif
)
221 return etharp_raw(netif
, (struct eth_addr
*)netif
->hwaddr
, ðbroadcast
,
222 (struct eth_addr
*)netif
->hwaddr
, &netif
->autoip
->llipaddr
, ðzero
,
223 &netif
->autoip
->llipaddr
, ARP_REQUEST
);
227 * Configure interface for use with current LL IP-Address
229 * @param netif network interface to configure with current LL IP-Address
232 autoip_bind(struct netif
*netif
)
234 struct autoip
*autoip
= netif
->autoip
;
235 struct ip_addr sn_mask
, gw_addr
;
237 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
,
238 ("autoip_bind(netif=%p) %c%c%"U16_F
" 0x%08"X32_F
"\n",
239 (void*)netif
, netif
->name
[0], netif
->name
[1], (u16_t
)netif
->num
, autoip
->llipaddr
.addr
));
241 IP4_ADDR(&sn_mask
, 255, 255, 0, 0);
242 IP4_ADDR(&gw_addr
, 0, 0, 0, 0);
244 netif_set_ipaddr(netif
, &autoip
->llipaddr
);
245 netif_set_netmask(netif
, &sn_mask
);
246 netif_set_gw(netif
, &gw_addr
);
248 /* bring the interface up */
255 * Start AutoIP client
257 * @param netif network interface on which start the AutoIP client
260 autoip_start(struct netif
*netif
)
262 struct autoip
*autoip
= netif
->autoip
;
263 err_t result
= ERR_OK
;
265 if(netif_is_up(netif
)) {
266 netif_set_down(netif
);
269 /* Set IP-Address, Netmask and Gateway to 0 to make sure that
270 * ARP Packets are formed correctly
272 netif
->ip_addr
.addr
= 0;
273 netif
->netmask
.addr
= 0;
276 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_STATE
,
277 ("autoip_start(netif=%p) %c%c%"U16_F
"\n", (void*)netif
, netif
->name
[0],
278 netif
->name
[1], (u16_t
)netif
->num
));
280 /* no AutoIP client attached yet? */
281 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
,
282 ("autoip_start(): starting new AUTOIP client\n"));
283 autoip
= mem_malloc(sizeof(struct autoip
));
285 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
,
286 ("autoip_start(): could not allocate autoip\n"));
289 memset( autoip
, 0, sizeof(struct autoip
));
290 /* store this AutoIP client in the netif */
291 netif
->autoip
= autoip
;
292 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
, ("autoip_start(): allocated autoip"));
294 autoip
->state
= AUTOIP_STATE_OFF
;
296 autoip
->sent_num
= 0;
297 memset(&autoip
->llipaddr
, 0, sizeof(struct ip_addr
));
298 autoip
->lastconflict
= 0;
301 autoip_create_addr(netif
, &(autoip
->llipaddr
));
302 autoip
->tried_llipaddr
++;
303 autoip_start_probing(netif
);
309 autoip_start_probing(struct netif
*netif
)
311 struct autoip
*autoip
= netif
->autoip
;
313 autoip
->state
= AUTOIP_STATE_PROBING
;
314 autoip
->sent_num
= 0;
316 /* time to wait to first probe, this is randomly
317 * choosen out of 0 to PROBE_WAIT seconds.
318 * compliant to RFC 3927 Section 2.2.1
320 autoip
->ttw
= (u16_t
)(LWIP_AUTOIP_RAND(netif
) % (PROBE_WAIT
* AUTOIP_TICKS_PER_SECOND
));
323 * if we tried more then MAX_CONFLICTS we must limit our rate for
324 * accquiring and probing address
325 * compliant to RFC 3927 Section 2.2.1
327 if(autoip
->tried_llipaddr
> MAX_CONFLICTS
) {
328 autoip
->ttw
= RATE_LIMIT_INTERVAL
* AUTOIP_TICKS_PER_SECOND
;
333 * Handle a possible change in the network configuration.
335 * If there is an AutoIP address configured, take the interface down
336 * and begin probing with the same address.
339 autoip_network_changed(struct netif
*netif
)
341 if (netif
->autoip
&& netif
->autoip
->state
!= AUTOIP_STATE_OFF
) {
342 netif_set_down(netif
);
343 autoip_start_probing(netif
);
350 * @param netif network interface on which stop the AutoIP client
353 autoip_stop(struct netif
*netif
)
355 netif
->autoip
->state
= AUTOIP_STATE_OFF
;
356 netif_set_down(netif
);
361 * Has to be called in loop every AUTOIP_TMR_INTERVAL milliseconds
366 struct netif
*netif
= netif_list
;
367 /* loop through netif's */
368 while (netif
!= NULL
) {
369 /* only act on AutoIP configured interfaces */
370 if (netif
->autoip
!= NULL
) {
371 if(netif
->autoip
->lastconflict
> 0) {
372 netif
->autoip
->lastconflict
--;
375 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
,
376 ("autoip_tmr() AutoIP-State: %"U16_F
", ttw=%"U16_F
"\n",
377 (u16_t
)(netif
->autoip
->state
), netif
->autoip
->ttw
));
379 switch(netif
->autoip
->state
) {
380 case AUTOIP_STATE_PROBING
:
381 if(netif
->autoip
->ttw
> 0) {
382 netif
->autoip
->ttw
--;
384 if(netif
->autoip
->sent_num
>= PROBE_NUM
) {
385 netif
->autoip
->state
= AUTOIP_STATE_ANNOUNCING
;
386 netif
->autoip
->sent_num
= 0;
387 netif
->autoip
->ttw
= ANNOUNCE_WAIT
* AUTOIP_TICKS_PER_SECOND
;
389 autoip_arp_probe(netif
);
390 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
,
391 ("autoip_tmr() PROBING Sent Probe\n"));
392 netif
->autoip
->sent_num
++;
393 /* calculate time to wait to next probe */
394 netif
->autoip
->ttw
= (u16_t
)((LWIP_AUTOIP_RAND(netif
) %
395 ((PROBE_MAX
- PROBE_MIN
) * AUTOIP_TICKS_PER_SECOND
) ) +
396 PROBE_MIN
* AUTOIP_TICKS_PER_SECOND
);
401 case AUTOIP_STATE_ANNOUNCING
:
402 if(netif
->autoip
->ttw
> 0) {
403 netif
->autoip
->ttw
--;
405 if(netif
->autoip
->sent_num
== 0) {
406 /* We are here the first time, so we waited ANNOUNCE_WAIT seconds
407 * Now we can bind to an IP address and use it.
409 * autoip_bind calls netif_set_up. This triggers a gratuitous ARP
410 * which counts as an announcement.
414 autoip_arp_announce(netif
);
415 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
,
416 ("autoip_tmr() ANNOUNCING Sent Announce\n"));
418 netif
->autoip
->ttw
= ANNOUNCE_INTERVAL
* AUTOIP_TICKS_PER_SECOND
;
419 netif
->autoip
->sent_num
++;
421 if(netif
->autoip
->sent_num
>= ANNOUNCE_NUM
) {
422 netif
->autoip
->state
= AUTOIP_STATE_BOUND
;
423 netif
->autoip
->sent_num
= 0;
424 netif
->autoip
->ttw
= 0;
430 /* proceed to next network interface */
436 * Handles every incoming ARP Packet, called by etharp_arp_input.
438 * @param netif network interface to use for autoip processing
439 * @param hdr Incoming ARP packet
442 autoip_arp_reply(struct netif
*netif
, struct etharp_hdr
*hdr
)
444 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
, ("autoip_arp_reply()\n"));
445 if ((netif
->autoip
!= NULL
) && (netif
->autoip
->state
!= AUTOIP_STATE_OFF
)) {
446 /* when ip.src == llipaddr && hw.src != netif->hwaddr
448 * when probing ip.dst == llipaddr && hw.src != netif->hwaddr
449 * we have a conflict and must solve it
451 struct ip_addr sipaddr
, dipaddr
;
452 struct eth_addr netifaddr
;
453 netifaddr
.addr
[0] = netif
->hwaddr
[0];
454 netifaddr
.addr
[1] = netif
->hwaddr
[1];
455 netifaddr
.addr
[2] = netif
->hwaddr
[2];
456 netifaddr
.addr
[3] = netif
->hwaddr
[3];
457 netifaddr
.addr
[4] = netif
->hwaddr
[4];
458 netifaddr
.addr
[5] = netif
->hwaddr
[5];
460 /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
461 * structure packing (not using structure copy which breaks strict-aliasing rules).
463 SMEMCPY(&sipaddr
, &hdr
->sipaddr
, sizeof(sipaddr
));
464 SMEMCPY(&dipaddr
, &hdr
->dipaddr
, sizeof(dipaddr
));
466 if ((netif
->autoip
->state
== AUTOIP_STATE_PROBING
) ||
467 ((netif
->autoip
->state
== AUTOIP_STATE_ANNOUNCING
) &&
468 (netif
->autoip
->sent_num
== 0))) {
469 /* RFC 3927 Section 2.2.1:
470 * from beginning to after ANNOUNCE_WAIT
471 * seconds we have a conflict if
472 * ip.src == llipaddr OR
473 * ip.dst == llipaddr && hw.src != own hwaddr
475 if ((ip_addr_cmp(&sipaddr
, &netif
->autoip
->llipaddr
)) ||
476 (ip_addr_cmp(&dipaddr
, &netif
->autoip
->llipaddr
) &&
477 !eth_addr_cmp(&netifaddr
, &hdr
->shwaddr
))) {
478 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_STATE
| LWIP_DBG_LEVEL_WARNING
,
479 ("autoip_arp_reply(): Probe Conflict detected\n"));
483 /* RFC 3927 Section 2.5:
484 * in any state we have a conflict if
485 * ip.src == llipaddr && hw.src != own hwaddr
487 if (ip_addr_cmp(&sipaddr
, &netif
->autoip
->llipaddr
) &&
488 !eth_addr_cmp(&netifaddr
, &hdr
->shwaddr
)) {
489 LWIP_DEBUGF(AUTOIP_DEBUG
| LWIP_DBG_TRACE
| LWIP_DBG_STATE
| LWIP_DBG_LEVEL_WARNING
,
490 ("autoip_arp_reply(): Conflicting ARP-Packet detected\n"));
491 autoip_handle_arp_conflict(netif
);
497 #endif /* LWIP_AUTOIP */