1 | /**
|
---|
2 | * @file
|
---|
3 | * AutoIP Automatic LinkLocal IP Configuration
|
---|
4 | *
|
---|
5 | * This is a AutoIP implementation for the lwIP TCP/IP stack. It aims to conform
|
---|
6 | * with RFC 3927.
|
---|
7 | *
|
---|
8 | * @defgroup autoip AUTOIP
|
---|
9 | * @ingroup ip4
|
---|
10 | * AUTOIP related functions
|
---|
11 | * USAGE:
|
---|
12 | *
|
---|
13 | * define @ref LWIP_AUTOIP 1 in your lwipopts.h
|
---|
14 | * Options:
|
---|
15 | * AUTOIP_TMR_INTERVAL msecs,
|
---|
16 | * I recommend a value of 100. The value must divide 1000 with a remainder almost 0.
|
---|
17 | * Possible values are 1000, 500, 333, 250, 200, 166, 142, 125, 111, 100 ....
|
---|
18 | *
|
---|
19 | * Without DHCP:
|
---|
20 | * - Call autoip_start() after netif_add().
|
---|
21 | *
|
---|
22 | * With DHCP:
|
---|
23 | * - define @ref LWIP_DHCP_AUTOIP_COOP 1 in your lwipopts.h.
|
---|
24 | * - Configure your DHCP Client.
|
---|
25 | *
|
---|
26 | * @see netifapi_autoip
|
---|
27 | */
|
---|
28 |
|
---|
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 "lwip/opt.h"
|
---|
60 |
|
---|
61 | #if LWIP_IPV4 && LWIP_AUTOIP /* don't build if not configured for use in lwipopts.h */
|
---|
62 |
|
---|
63 | #include "lwip/mem.h"
|
---|
64 | /* #include "lwip/udp.h" */
|
---|
65 | #include "lwip/ip_addr.h"
|
---|
66 | #include "lwip/netif.h"
|
---|
67 | #include "lwip/autoip.h"
|
---|
68 | #include "lwip/etharp.h"
|
---|
69 | #include "lwip/prot/autoip.h"
|
---|
70 |
|
---|
71 | #include <string.h>
|
---|
72 |
|
---|
73 | /** Pseudo random macro based on netif informations.
|
---|
74 | * You could use "rand()" from the C Library if you define LWIP_AUTOIP_RAND in lwipopts.h */
|
---|
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 |
|
---|
83 | /**
|
---|
84 | * Macro that generates the initial IP address to be tried by AUTOIP.
|
---|
85 | * If you want to override this, define it to something else in lwipopts.h.
|
---|
86 | */
|
---|
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 |
|
---|
97 | /**
|
---|
98 | * @ingroup autoip
|
---|
99 | * Set a statically allocated struct autoip to work with.
|
---|
100 | * Using this prevents autoip_start to allocate it using mem_malloc.
|
---|
101 | *
|
---|
102 | * @param netif the netif for which to set the struct autoip
|
---|
103 | * @param autoip (uninitialised) autoip struct allocated by the application
|
---|
104 | */
|
---|
105 | void
|
---|
106 | autoip_set_struct(struct netif *netif, struct autoip *autoip)
|
---|
107 | {
|
---|
108 | LWIP_ASSERT_CORE_LOCKED();
|
---|
109 | LWIP_ASSERT("netif != NULL", netif != NULL);
|
---|
110 | LWIP_ASSERT("autoip != NULL", autoip != NULL);
|
---|
111 | LWIP_ASSERT("netif already has a struct autoip set",
|
---|
112 | netif_autoip_data(netif) == NULL);
|
---|
113 |
|
---|
114 | /* clear data structure */
|
---|
115 | memset(autoip, 0, sizeof(struct autoip));
|
---|
116 | /* autoip->state = AUTOIP_STATE_OFF; */
|
---|
117 | netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Restart AutoIP client and check the next address (conflict detected)
|
---|
121 | *
|
---|
122 | * @param netif The netif under AutoIP control
|
---|
123 | */
|
---|
124 | static void
|
---|
125 | autoip_restart(struct netif *netif)
|
---|
126 | {
|
---|
127 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
128 | autoip->tried_llipaddr++;
|
---|
129 | autoip_start(netif);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Handle a IP address conflict after an ARP conflict detection
|
---|
134 | */
|
---|
135 | static void
|
---|
136 | autoip_handle_arp_conflict(struct netif *netif)
|
---|
137 | {
|
---|
138 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
139 |
|
---|
140 | /* RFC3927, 2.5 "Conflict Detection and Defense" allows two options where
|
---|
141 | a) means retreat on the first conflict and
|
---|
142 | b) allows to keep an already configured address when having only one
|
---|
143 | conflict in 10 seconds
|
---|
144 | We use option b) since it helps to improve the chance that one of the two
|
---|
145 | conflicting hosts may be able to retain its address. */
|
---|
146 |
|
---|
147 | if (autoip->lastconflict > 0) {
|
---|
148 | /* retreat, there was a conflicting ARP in the last DEFEND_INTERVAL seconds */
|
---|
149 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
|
---|
150 | ("autoip_handle_arp_conflict(): we are defending, but in DEFEND_INTERVAL, retreating\n"));
|
---|
151 |
|
---|
152 | /* Active TCP sessions are aborted when removing the ip addresss */
|
---|
153 | autoip_restart(netif);
|
---|
154 | } else {
|
---|
155 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
|
---|
156 | ("autoip_handle_arp_conflict(): we are defend, send ARP Announce\n"));
|
---|
157 | autoip_arp_announce(netif);
|
---|
158 | autoip->lastconflict = DEFEND_INTERVAL * AUTOIP_TICKS_PER_SECOND;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Create an IP-Address out of range 169.254.1.0 to 169.254.254.255
|
---|
164 | *
|
---|
165 | * @param netif network interface on which create the IP-Address
|
---|
166 | * @param ipaddr ip address to initialize
|
---|
167 | */
|
---|
168 | static void
|
---|
169 | autoip_create_addr(struct netif *netif, ip4_addr_t *ipaddr)
|
---|
170 | {
|
---|
171 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
172 |
|
---|
173 | /* Here we create an IP-Address out of range 169.254.1.0 to 169.254.254.255
|
---|
174 | * compliant to RFC 3927 Section 2.1
|
---|
175 | * We have 254 * 256 possibilities */
|
---|
176 |
|
---|
177 | u32_t addr = lwip_ntohl(LWIP_AUTOIP_CREATE_SEED_ADDR(netif));
|
---|
178 | addr += autoip->tried_llipaddr;
|
---|
179 | addr = AUTOIP_NET | (addr & 0xffff);
|
---|
180 | /* Now, 169.254.0.0 <= addr <= 169.254.255.255 */
|
---|
181 |
|
---|
182 | if (addr < AUTOIP_RANGE_START) {
|
---|
183 | addr += AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
|
---|
184 | }
|
---|
185 | if (addr > AUTOIP_RANGE_END) {
|
---|
186 | addr -= AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
|
---|
187 | }
|
---|
188 | LWIP_ASSERT("AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) &&
|
---|
189 | (addr <= AUTOIP_RANGE_END));
|
---|
190 | ip4_addr_set_u32(ipaddr, lwip_htonl(addr));
|
---|
191 |
|
---|
192 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
|
---|
193 | ("autoip_create_addr(): tried_llipaddr=%"U16_F", %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
---|
194 | (u16_t)(autoip->tried_llipaddr), ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr),
|
---|
195 | ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Sends an ARP probe from a network interface
|
---|
200 | *
|
---|
201 | * @param netif network interface used to send the probe
|
---|
202 | */
|
---|
203 | static err_t
|
---|
204 | autoip_arp_probe(struct netif *netif)
|
---|
205 | {
|
---|
206 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
207 | /* this works because netif->ip_addr is ANY */
|
---|
208 | return etharp_request(netif, &autoip->llipaddr);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Sends an ARP announce from a network interface
|
---|
213 | *
|
---|
214 | * @param netif network interface used to send the announce
|
---|
215 | */
|
---|
216 | static err_t
|
---|
217 | autoip_arp_announce(struct netif *netif)
|
---|
218 | {
|
---|
219 | return etharp_gratuitous(netif);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Configure interface for use with current LL IP-Address
|
---|
224 | *
|
---|
225 | * @param netif network interface to configure with current LL IP-Address
|
---|
226 | */
|
---|
227 | static err_t
|
---|
228 | autoip_bind(struct netif *netif)
|
---|
229 | {
|
---|
230 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
231 | ip4_addr_t sn_mask, gw_addr;
|
---|
232 |
|
---|
233 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
|
---|
234 | ("autoip_bind(netif=%p) %c%c%"U16_F" %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
---|
235 | (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num,
|
---|
236 | ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
|
---|
237 | ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
|
---|
238 |
|
---|
239 | IP4_ADDR(&sn_mask, 255, 255, 0, 0);
|
---|
240 | IP4_ADDR(&gw_addr, 0, 0, 0, 0);
|
---|
241 |
|
---|
242 | netif_set_addr(netif, &autoip->llipaddr, &sn_mask, &gw_addr);
|
---|
243 | /* interface is used by routing now that an address is set */
|
---|
244 |
|
---|
245 | return ERR_OK;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * @ingroup autoip
|
---|
250 | * Start AutoIP client
|
---|
251 | *
|
---|
252 | * @param netif network interface on which start the AutoIP client
|
---|
253 | */
|
---|
254 | err_t
|
---|
255 | autoip_start(struct netif *netif)
|
---|
256 | {
|
---|
257 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
258 | err_t result = ERR_OK;
|
---|
259 |
|
---|
260 | LWIP_ASSERT_CORE_LOCKED();
|
---|
261 | LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;);
|
---|
262 |
|
---|
263 | /* Set IP-Address, Netmask and Gateway to 0 to make sure that
|
---|
264 | * ARP Packets are formed correctly
|
---|
265 | */
|
---|
266 | netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
|
---|
267 |
|
---|
268 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
|
---|
269 | ("autoip_start(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0],
|
---|
270 | netif->name[1], (u16_t)netif->num));
|
---|
271 | if (autoip == NULL) {
|
---|
272 | /* no AutoIP client attached yet? */
|
---|
273 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
|
---|
274 | ("autoip_start(): starting new AUTOIP client\n"));
|
---|
275 | autoip = (struct autoip *)mem_calloc(1, sizeof(struct autoip));
|
---|
276 | if (autoip == NULL) {
|
---|
277 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
|
---|
278 | ("autoip_start(): could not allocate autoip\n"));
|
---|
279 | return ERR_MEM;
|
---|
280 | }
|
---|
281 | /* store this AutoIP client in the netif */
|
---|
282 | netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip);
|
---|
283 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip"));
|
---|
284 | } else {
|
---|
285 | autoip->state = AUTOIP_STATE_OFF;
|
---|
286 | autoip->ttw = 0;
|
---|
287 | autoip->sent_num = 0;
|
---|
288 | ip4_addr_set_zero(&autoip->llipaddr);
|
---|
289 | autoip->lastconflict = 0;
|
---|
290 | }
|
---|
291 |
|
---|
292 | autoip_create_addr(netif, &(autoip->llipaddr));
|
---|
293 | autoip_start_probing(netif);
|
---|
294 |
|
---|
295 | return result;
|
---|
296 | }
|
---|
297 |
|
---|
298 | static void
|
---|
299 | autoip_start_probing(struct netif *netif)
|
---|
300 | {
|
---|
301 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
302 |
|
---|
303 | autoip->state = AUTOIP_STATE_PROBING;
|
---|
304 | autoip->sent_num = 0;
|
---|
305 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
|
---|
306 | ("autoip_start_probing(): changing state to PROBING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
---|
307 | ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
|
---|
308 | ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
|
---|
309 |
|
---|
310 | /* time to wait to first probe, this is randomly
|
---|
311 | * chosen out of 0 to PROBE_WAIT seconds.
|
---|
312 | * compliant to RFC 3927 Section 2.2.1
|
---|
313 | */
|
---|
314 | autoip->ttw = (u16_t)(LWIP_AUTOIP_RAND(netif) % (PROBE_WAIT * AUTOIP_TICKS_PER_SECOND));
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * if we tried more then MAX_CONFLICTS we must limit our rate for
|
---|
318 | * acquiring and probing address
|
---|
319 | * compliant to RFC 3927 Section 2.2.1
|
---|
320 | */
|
---|
321 | if (autoip->tried_llipaddr > MAX_CONFLICTS) {
|
---|
322 | autoip->ttw = RATE_LIMIT_INTERVAL * AUTOIP_TICKS_PER_SECOND;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Handle a possible change in the network configuration.
|
---|
328 | *
|
---|
329 | * If there is an AutoIP address configured, take the interface down
|
---|
330 | * and begin probing with the same address.
|
---|
331 | */
|
---|
332 | void
|
---|
333 | autoip_network_changed(struct netif *netif)
|
---|
334 | {
|
---|
335 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
336 |
|
---|
337 | if (autoip && (autoip->state != AUTOIP_STATE_OFF)) {
|
---|
338 | autoip_start_probing(netif);
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * @ingroup autoip
|
---|
344 | * Stop AutoIP client
|
---|
345 | *
|
---|
346 | * @param netif network interface on which stop the AutoIP client
|
---|
347 | */
|
---|
348 | err_t
|
---|
349 | autoip_stop(struct netif *netif)
|
---|
350 | {
|
---|
351 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
352 |
|
---|
353 | LWIP_ASSERT_CORE_LOCKED();
|
---|
354 | if (autoip != NULL) {
|
---|
355 | autoip->state = AUTOIP_STATE_OFF;
|
---|
356 | if (ip4_addr_islinklocal(netif_ip4_addr(netif))) {
|
---|
357 | netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
|
---|
358 | }
|
---|
359 | }
|
---|
360 | return ERR_OK;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Has to be called in loop every AUTOIP_TMR_INTERVAL milliseconds
|
---|
365 | */
|
---|
366 | void
|
---|
367 | autoip_tmr(void)
|
---|
368 | {
|
---|
369 | struct netif *netif;
|
---|
370 | /* loop through netif's */
|
---|
371 | NETIF_FOREACH(netif) {
|
---|
372 | struct autoip *autoip = netif_autoip_data(netif);
|
---|
373 | /* only act on AutoIP configured interfaces */
|
---|
374 | if (autoip != NULL) {
|
---|
375 | if (autoip->lastconflict > 0) {
|
---|
376 | autoip->lastconflict--;
|
---|
377 | }
|
---|
378 |
|
---|
379 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
|
---|
380 | ("autoip_tmr() AutoIP-State: %"U16_F", ttw=%"U16_F"\n",
|
---|
381 | (u16_t)(autoip->state), autoip->ttw));
|
---|
382 |
|
---|
383 | if (autoip->ttw > 0) {
|
---|
384 | autoip->ttw--;
|
---|
385 | }
|
---|
386 |
|
---|
387 | switch (autoip->state) {
|
---|
388 | case AUTOIP_STATE_PROBING:
|
---|
389 | if (autoip->ttw == 0) {
|
---|
390 | if (autoip->sent_num >= PROBE_NUM) {
|
---|
391 | /* Switch to ANNOUNCING: now we can bind to an IP address and use it */
|
---|
392 | autoip->state = AUTOIP_STATE_ANNOUNCING;
|
---|
393 | autoip_bind(netif);
|
---|
394 | /* autoip_bind() calls netif_set_addr(): this triggers a gratuitous ARP
|
---|
395 | which counts as an announcement */
|
---|
396 | autoip->sent_num = 1;
|
---|
397 | autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND;
|
---|
398 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
|
---|
399 | ("autoip_tmr(): changing state to ANNOUNCING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
---|
400 | ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
|
---|
401 | ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
|
---|
402 | } else {
|
---|
403 | autoip_arp_probe(netif);
|
---|
404 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_tmr() PROBING Sent Probe\n"));
|
---|
405 | autoip->sent_num++;
|
---|
406 | if (autoip->sent_num == PROBE_NUM) {
|
---|
407 | /* calculate time to wait to for announce */
|
---|
408 | autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND;
|
---|
409 | } else {
|
---|
410 | /* calculate time to wait to next probe */
|
---|
411 | autoip->ttw = (u16_t)((LWIP_AUTOIP_RAND(netif) %
|
---|
412 | ((PROBE_MAX - PROBE_MIN) * AUTOIP_TICKS_PER_SECOND) ) +
|
---|
413 | PROBE_MIN * AUTOIP_TICKS_PER_SECOND);
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|
417 | break;
|
---|
418 |
|
---|
419 | case AUTOIP_STATE_ANNOUNCING:
|
---|
420 | if (autoip->ttw == 0) {
|
---|
421 | autoip_arp_announce(netif);
|
---|
422 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_tmr() ANNOUNCING Sent Announce\n"));
|
---|
423 | autoip->ttw = ANNOUNCE_INTERVAL * AUTOIP_TICKS_PER_SECOND;
|
---|
424 | autoip->sent_num++;
|
---|
425 |
|
---|
426 | if (autoip->sent_num >= ANNOUNCE_NUM) {
|
---|
427 | autoip->state = AUTOIP_STATE_BOUND;
|
---|
428 | autoip->sent_num = 0;
|
---|
429 | autoip->ttw = 0;
|
---|
430 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
|
---|
431 | ("autoip_tmr(): changing state to BOUND: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
---|
432 | ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
|
---|
433 | ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
|
---|
434 | }
|
---|
435 | }
|
---|
436 | break;
|
---|
437 |
|
---|
438 | default:
|
---|
439 | /* nothing to do in other states */
|
---|
440 | break;
|
---|
441 | }
|
---|
442 | }
|
---|
443 | }
|
---|
444 | }
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Handles every incoming ARP Packet, called by etharp_input().
|
---|
448 | *
|
---|
449 | * @param netif network interface to use for autoip processing
|
---|
450 | * @param hdr Incoming ARP packet
|
---|
451 | */
|
---|
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 | SMEMCPY(netifaddr.addr, netif->hwaddr, ETH_HWADDR_LEN);
|
---|
467 |
|
---|
468 | /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without
|
---|
469 | * structure packing (not using structure copy which breaks strict-aliasing rules).
|
---|
470 | */
|
---|
471 | IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&sipaddr, &hdr->sipaddr);
|
---|
472 | IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&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))) {
|
---|
485 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING,
|
---|
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)) {
|
---|
496 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING,
|
---|
497 | ("autoip_arp_reply(): Conflicting ARP-Packet detected\n"));
|
---|
498 | autoip_handle_arp_conflict(netif);
|
---|
499 | }
|
---|
500 | }
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | /** check if AutoIP supplied netif->ip_addr
|
---|
505 | *
|
---|
506 | * @param netif the netif to check
|
---|
507 | * @return 1 if AutoIP supplied netif->ip_addr (state BOUND or ANNOUNCING),
|
---|
508 | * 0 otherwise
|
---|
509 | */
|
---|
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 */
|
---|