BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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("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 | |||
119 | /** Restart AutoIP client and check the next address (conflict detected) |
||
120 | * |
||
121 | * @param netif The netif under AutoIP control |
||
122 | */ |
||
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 | |||
131 | /** |
||
132 | * Handle a IP address conflict after an ARP conflict detection |
||
133 | */ |
||
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 */ |
||
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")); |
||
150 | |||
151 | /* Active TCP sessions are aborted when removing the ip addresss */ |
||
152 | autoip_restart(netif); |
||
153 | } else { |
||
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 | autoip->lastconflict = DEFEND_INTERVAL * AUTOIP_TICKS_PER_SECOND; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Create an IP-Address out of range 169.254.1.0 to 169.254.254.255 |
||
163 | * |
||
164 | * @param netif network interface on which create the IP-Address |
||
165 | * @param ipaddr ip address to initialize |
||
166 | */ |
||
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 | |||
191 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
||
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 | |||
197 | /** |
||
198 | * Sends an ARP probe from a network interface |
||
199 | * |
||
200 | * @param netif network interface used to send the probe |
||
201 | */ |
||
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 | |||
210 | /** |
||
211 | * Sends an ARP announce from a network interface |
||
212 | * |
||
213 | * @param netif network interface used to send the announce |
||
214 | */ |
||
215 | static err_t |
||
216 | autoip_arp_announce(struct netif *netif) |
||
217 | { |
||
218 | return etharp_gratuitous(netif); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Configure interface for use with current LL IP-Address |
||
223 | * |
||
224 | * @param netif network interface to configure with current LL IP-Address |
||
225 | */ |
||
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 | |||
232 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
||
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 | |||
247 | /** |
||
248 | * @ingroup autoip |
||
249 | * Start AutoIP client |
||
250 | * |
||
251 | * @param netif network interface on which start the AutoIP client |
||
252 | */ |
||
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 | |||
266 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
||
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? */ |
||
271 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
||
272 | ("autoip_start(): starting new AUTOIP client\n")); |
||
273 | autoip = (struct autoip *)mem_calloc(1, sizeof(struct autoip)); |
||
274 | if (autoip == NULL) { |
||
275 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
||
276 | ("autoip_start(): could not allocate autoip\n")); |
||
277 | return ERR_MEM; |
||
278 | } |
||
279 | /* store this AutoIP client in the netif */ |
||
280 | netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip); |
||
281 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip")); |
||
282 | } else { |
||
283 | autoip->state = AUTOIP_STATE_OFF; |
||
284 | autoip->ttw = 0; |
||
285 | autoip->sent_num = 0; |
||
286 | ip4_addr_set_zero(&autoip->llipaddr); |
||
287 | autoip->lastconflict = 0; |
||
288 | } |
||
289 | |||
290 | autoip_create_addr(netif, &(autoip->llipaddr)); |
||
291 | autoip_start_probing(netif); |
||
292 | |||
293 | return result; |
||
294 | } |
||
295 | |||
296 | static void |
||
297 | autoip_start_probing(struct netif *netif) |
||
298 | { |
||
299 | struct autoip *autoip = netif_autoip_data(netif); |
||
300 | |||
301 | autoip->state = AUTOIP_STATE_PROBING; |
||
302 | autoip->sent_num = 0; |
||
303 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
||
304 | ("autoip_start_probing(): changing state to PROBING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
||
305 | ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr), |
||
306 | ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr))); |
||
307 | |||
308 | /* time to wait to first probe, this is randomly |
||
309 | * chosen out of 0 to PROBE_WAIT seconds. |
||
310 | * compliant to RFC 3927 Section 2.2.1 |
||
311 | */ |
||
312 | autoip->ttw = (u16_t)(LWIP_AUTOIP_RAND(netif) % (PROBE_WAIT * AUTOIP_TICKS_PER_SECOND)); |
||
313 | |||
314 | /* |
||
315 | * if we tried more then MAX_CONFLICTS we must limit our rate for |
||
316 | * acquiring and probing address |
||
317 | * compliant to RFC 3927 Section 2.2.1 |
||
318 | */ |
||
319 | if (autoip->tried_llipaddr > MAX_CONFLICTS) { |
||
320 | autoip->ttw = RATE_LIMIT_INTERVAL * AUTOIP_TICKS_PER_SECOND; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Handle a possible change in the network configuration. |
||
326 | * |
||
327 | * If there is an AutoIP address configured, take the interface down |
||
328 | * and begin probing with the same address. |
||
329 | */ |
||
330 | void |
||
331 | autoip_network_changed(struct netif *netif) |
||
332 | { |
||
333 | struct autoip *autoip = netif_autoip_data(netif); |
||
334 | |||
335 | if (autoip && (autoip->state != AUTOIP_STATE_OFF)) { |
||
336 | autoip_start_probing(netif); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @ingroup autoip |
||
342 | * Stop AutoIP client |
||
343 | * |
||
344 | * @param netif network interface on which stop the AutoIP client |
||
345 | */ |
||
346 | err_t |
||
347 | autoip_stop(struct netif *netif) |
||
348 | { |
||
349 | struct autoip *autoip = netif_autoip_data(netif); |
||
350 | |||
351 | if (autoip != NULL) { |
||
352 | autoip->state = AUTOIP_STATE_OFF; |
||
353 | if (ip4_addr_islinklocal(netif_ip4_addr(netif))) { |
||
354 | netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4); |
||
355 | } |
||
356 | } |
||
357 | return ERR_OK; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Has to be called in loop every AUTOIP_TMR_INTERVAL milliseconds |
||
362 | */ |
||
363 | void |
||
364 | autoip_tmr(void) |
||
365 | { |
||
366 | struct netif *netif; |
||
367 | /* loop through netif's */ |
||
368 | NETIF_FOREACH(netif) { |
||
369 | struct autoip *autoip = netif_autoip_data(netif); |
||
370 | /* only act on AutoIP configured interfaces */ |
||
371 | if (autoip != NULL) { |
||
372 | if (autoip->lastconflict > 0) { |
||
373 | autoip->lastconflict--; |
||
374 | } |
||
375 | |||
376 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, |
||
377 | ("autoip_tmr() AutoIP-State: %"U16_F", ttw=%"U16_F"\n", |
||
378 | (u16_t)(autoip->state), autoip->ttw)); |
||
379 | |||
380 | if (autoip->ttw > 0) { |
||
381 | autoip->ttw--; |
||
382 | } |
||
383 | |||
384 | switch (autoip->state) { |
||
385 | case AUTOIP_STATE_PROBING: |
||
386 | if (autoip->ttw == 0) { |
||
387 | if (autoip->sent_num >= PROBE_NUM) { |
||
388 | /* Switch to ANNOUNCING: now we can bind to an IP address and use it */ |
||
389 | autoip->state = AUTOIP_STATE_ANNOUNCING; |
||
390 | autoip_bind(netif); |
||
391 | /* autoip_bind() calls netif_set_addr(): this triggers a gratuitous ARP |
||
392 | which counts as an announcement */ |
||
393 | autoip->sent_num = 1; |
||
394 | autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND; |
||
395 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
||
396 | ("autoip_tmr(): changing state to ANNOUNCING: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
||
397 | ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr), |
||
398 | ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr))); |
||
399 | } else { |
||
400 | autoip_arp_probe(netif); |
||
401 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_tmr() PROBING Sent Probe\n")); |
||
402 | autoip->sent_num++; |
||
403 | if (autoip->sent_num == PROBE_NUM) { |
||
404 | /* calculate time to wait to for announce */ |
||
405 | autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND; |
||
406 | } else { |
||
407 | /* calculate time to wait to next probe */ |
||
408 | autoip->ttw = (u16_t)((LWIP_AUTOIP_RAND(netif) % |
||
409 | ((PROBE_MAX - PROBE_MIN) * AUTOIP_TICKS_PER_SECOND) ) + |
||
410 | PROBE_MIN * AUTOIP_TICKS_PER_SECOND); |
||
411 | } |
||
412 | } |
||
413 | } |
||
414 | break; |
||
415 | |||
416 | case AUTOIP_STATE_ANNOUNCING: |
||
417 | if (autoip->ttw == 0) { |
||
418 | autoip_arp_announce(netif); |
||
419 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_tmr() ANNOUNCING Sent Announce\n")); |
||
420 | autoip->ttw = ANNOUNCE_INTERVAL * AUTOIP_TICKS_PER_SECOND; |
||
421 | autoip->sent_num++; |
||
422 | |||
423 | if (autoip->sent_num >= ANNOUNCE_NUM) { |
||
424 | autoip->state = AUTOIP_STATE_BOUND; |
||
425 | autoip->sent_num = 0; |
||
426 | autoip->ttw = 0; |
||
427 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, |
||
428 | ("autoip_tmr(): changing state to BOUND: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
||
429 | ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr), |
||
430 | ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr))); |
||
431 | } |
||
432 | } |
||
433 | break; |
||
434 | |||
435 | default: |
||
436 | /* nothing to do in other states */ |
||
437 | break; |
||
438 | } |
||
439 | } |
||
440 | } |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Handles every incoming ARP Packet, called by etharp_input(). |
||
445 | * |
||
446 | * @param netif network interface to use for autoip processing |
||
447 | * @param hdr Incoming ARP packet |
||
448 | */ |
||
449 | void |
||
450 | autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr) |
||
451 | { |
||
452 | struct autoip *autoip = netif_autoip_data(netif); |
||
453 | |||
454 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_arp_reply()\n")); |
||
455 | if ((autoip != NULL) && (autoip->state != AUTOIP_STATE_OFF)) { |
||
456 | /* when ip.src == llipaddr && hw.src != netif->hwaddr |
||
457 | * |
||
458 | * when probing ip.dst == llipaddr && hw.src != netif->hwaddr |
||
459 | * we have a conflict and must solve it |
||
460 | */ |
||
461 | ip4_addr_t sipaddr, dipaddr; |
||
462 | struct eth_addr netifaddr; |
||
463 | SMEMCPY(netifaddr.addr, netif->hwaddr, ETH_HWADDR_LEN); |
||
464 | |||
465 | /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without |
||
466 | * structure packing (not using structure copy which breaks strict-aliasing rules). |
||
467 | */ |
||
468 | IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&sipaddr, &hdr->sipaddr); |
||
469 | IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&dipaddr, &hdr->dipaddr); |
||
470 | |||
471 | if (autoip->state == AUTOIP_STATE_PROBING) { |
||
472 | /* RFC 3927 Section 2.2.1: |
||
473 | * from beginning to after ANNOUNCE_WAIT |
||
474 | * seconds we have a conflict if |
||
475 | * ip.src == llipaddr OR |
||
476 | * ip.dst == llipaddr && hw.src != own hwaddr |
||
477 | */ |
||
478 | if ((ip4_addr_cmp(&sipaddr, &autoip->llipaddr)) || |
||
479 | (ip4_addr_isany_val(sipaddr) && |
||
480 | ip4_addr_cmp(&dipaddr, &autoip->llipaddr) && |
||
481 | !eth_addr_cmp(&netifaddr, &hdr->shwaddr))) { |
||
482 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING, |
||
483 | ("autoip_arp_reply(): Probe Conflict detected\n")); |
||
484 | autoip_restart(netif); |
||
485 | } |
||
486 | } else { |
||
487 | /* RFC 3927 Section 2.5: |
||
488 | * in any state we have a conflict if |
||
489 | * ip.src == llipaddr && hw.src != own hwaddr |
||
490 | */ |
||
491 | if (ip4_addr_cmp(&sipaddr, &autoip->llipaddr) && |
||
492 | !eth_addr_cmp(&netifaddr, &hdr->shwaddr)) { |
||
493 | LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING, |
||
494 | ("autoip_arp_reply(): Conflicting ARP-Packet detected\n")); |
||
495 | autoip_handle_arp_conflict(netif); |
||
496 | } |
||
497 | } |
||
498 | } |
||
499 | } |
||
500 | |||
501 | /** check if AutoIP supplied netif->ip_addr |
||
502 | * |
||
503 | * @param netif the netif to check |
||
504 | * @return 1 if AutoIP supplied netif->ip_addr (state BOUND or ANNOUNCING), |
||
505 | * 0 otherwise |
||
506 | */ |
||
507 | u8_t |
||
508 | autoip_supplied_address(const struct netif *netif) |
||
509 | { |
||
510 | if ((netif != NULL) && (netif_autoip_data(netif) != NULL)) { |
||
511 | struct autoip *autoip = netif_autoip_data(netif); |
||
512 | return (autoip->state == AUTOIP_STATE_BOUND) || (autoip->state == AUTOIP_STATE_ANNOUNCING); |
||
513 | } |
||
514 | return 0; |
||
515 | } |
||
516 | |||
517 | u8_t |
||
518 | autoip_accept_packet(struct netif *netif, const ip4_addr_t *addr) |
||
519 | { |
||
520 | struct autoip *autoip = netif_autoip_data(netif); |
||
521 | return (autoip != NULL) && ip4_addr_cmp(addr, &(autoip->llipaddr)); |
||
522 | } |
||
523 | |||
524 | #endif /* LWIP_IPV4 && LWIP_AUTOIP */ |