xref: /btstack/3rd-party/lwip/core/src/core/ipv4/autoip.c (revision 97dc5e692c7d94a280158af58036a0efee5b0e56)
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. It uses IPv4 address conflict detection to evaluate the chosen
7  * address. The ACD module aims to be conform to RFC 5227.
8  * RFC 5227 is extracted out of RFC 3927 so the acd module fits nicely in autoip.
9  *
10  * @defgroup autoip AUTOIP
11  * @ingroup ip4
12  * AUTOIP related functions
13  * USAGE:
14  *
15  * define @ref LWIP_AUTOIP 1 in your lwipopts.h
16  *
17  * Without DHCP:
18  * - Call autoip_start() after netif_add().
19  *
20  * With DHCP:
21  * - define @ref LWIP_DHCP_AUTOIP_COOP 1 in your lwipopts.h.
22  * - Configure your DHCP Client.
23  *
24  * @see netifapi_autoip
25  */
26 
27 /*
28  *
29  * Copyright (c) 2007 Dominik Spies <[email protected]>
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without modification,
33  * are permitted provided that the following conditions are met:
34  *
35  * 1. Redistributions of source code must retain the above copyright notice,
36  *    this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright notice,
38  *    this list of conditions and the following disclaimer in the documentation
39  *    and/or other materials provided with the distribution.
40  * 3. The name of the author may not be used to endorse or promote products
41  *    derived from this software without specific prior written permission.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
44  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
45  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
46  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
48  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
51  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
52  * OF SUCH DAMAGE.
53  *
54  * Author: Dominik Spies <[email protected]>
55  */
56 
57 #include "lwip/opt.h"
58 
59 #if LWIP_IPV4 && LWIP_AUTOIP /* don't build if not configured for use in lwipopts.h */
60 
61 #include "lwip/mem.h"
62 /* #include "lwip/udp.h" */
63 #include "lwip/ip_addr.h"
64 #include "lwip/netif.h"
65 #include "lwip/autoip.h"
66 #include "lwip/acd.h"
67 #include "lwip/etharp.h"
68 #include "lwip/prot/autoip.h"
69 
70 #include <string.h>
71 
72 /**
73  * Macro that generates the initial IP address to be tried by AUTOIP.
74  * If you want to override this, define it to something else in lwipopts.h.
75  */
76 #ifndef LWIP_AUTOIP_CREATE_SEED_ADDR
77 #define LWIP_AUTOIP_CREATE_SEED_ADDR(netif) \
78   lwip_htonl(AUTOIP_RANGE_START + ((u32_t)(((u8_t)(netif->hwaddr[4])) | \
79                  ((u32_t)((u8_t)(netif->hwaddr[5]))) << 8)))
80 #endif /* LWIP_AUTOIP_CREATE_SEED_ADDR */
81 
82 /* Function definitions */
83 static void autoip_restart(struct netif *netif);
84 static void autoip_create_addr(struct netif *netif, ip4_addr_t *ipaddr);
85 static err_t autoip_bind(struct netif *netif);
86 static void autoip_conflict_callback(struct netif *netif,
87                                      acd_callback_enum_t state);
88 
89 /**
90  * @ingroup autoip
91  * Set a statically allocated struct autoip to work with.
92  * Using this prevents autoip_start to allocate it using mem_malloc.
93  *
94  * @param netif the netif for which to set the struct autoip
95  * @param autoip (uninitialised) autoip struct allocated by the application
96  */
97 void
autoip_set_struct(struct netif * netif,struct autoip * autoip)98 autoip_set_struct(struct netif *netif, struct autoip *autoip)
99 {
100   LWIP_ASSERT_CORE_LOCKED();
101   LWIP_ASSERT("netif != NULL", netif != NULL);
102   LWIP_ASSERT("autoip != NULL", autoip != NULL);
103   LWIP_ASSERT("netif already has a struct autoip set",
104               netif_autoip_data(netif) == NULL);
105 
106   /* clear data structure */
107   memset(autoip, 0, sizeof(struct autoip));
108   /* autoip->state = AUTOIP_STATE_OFF; */
109   netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip);
110 }
111 
112 /** Restart AutoIP client and check the next address (conflict detected)
113  *
114  * @param netif The netif under AutoIP control
115  */
116 static void
autoip_restart(struct netif * netif)117 autoip_restart(struct netif *netif)
118 {
119   struct autoip *autoip = netif_autoip_data(netif);
120   autoip->tried_llipaddr++;
121   autoip_start(netif);
122 }
123 
124 
125 /**
126  * Create an IP-Address out of range 169.254.1.0 to 169.254.254.255
127  *
128  * @param netif network interface on which create the IP-Address
129  * @param ipaddr ip address to initialize
130  */
131 static void
autoip_create_addr(struct netif * netif,ip4_addr_t * ipaddr)132 autoip_create_addr(struct netif *netif, ip4_addr_t *ipaddr)
133 {
134   struct autoip *autoip = netif_autoip_data(netif);
135 
136   /* Here we create an IP-Address out of range 169.254.1.0 to 169.254.254.255
137    * compliant to RFC 3927 Section 2.1
138    * We have 254 * 256 possibilities */
139 
140   u32_t addr = lwip_ntohl(LWIP_AUTOIP_CREATE_SEED_ADDR(netif));
141   addr += autoip->tried_llipaddr;
142   addr = AUTOIP_NET | (addr & 0xffff);
143   /* Now, 169.254.0.0 <= addr <= 169.254.255.255 */
144 
145   if (addr < AUTOIP_RANGE_START) {
146     addr += AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
147   }
148   if (addr > AUTOIP_RANGE_END) {
149     addr -= AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
150   }
151   LWIP_ASSERT("AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) &&
152               (addr <= AUTOIP_RANGE_END));
153   ip4_addr_set_u32(ipaddr, lwip_htonl(addr));
154 
155   LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
156               ("autoip_create_addr(): tried_llipaddr=%"U16_F", %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
157                (u16_t)(autoip->tried_llipaddr), ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr),
158                ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
159 }
160 
161 
162 /**
163  * Configure interface for use with current LL IP-Address
164  *
165  * @param netif network interface to configure with current LL IP-Address
166  */
167 static err_t
autoip_bind(struct netif * netif)168 autoip_bind(struct netif *netif)
169 {
170   struct autoip *autoip = netif_autoip_data(netif);
171   ip4_addr_t sn_mask, gw_addr;
172 
173   autoip->state = AUTOIP_STATE_BOUND;
174 
175   LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
176               ("autoip_bind(netif=%p) %c%c%"U16_F" %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
177                (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num,
178                ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
179                ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
180 
181   IP4_ADDR(&sn_mask, 255, 255, 0, 0);
182   IP4_ADDR(&gw_addr, 0, 0, 0, 0);
183 
184   netif_set_addr(netif, &autoip->llipaddr, &sn_mask, &gw_addr);
185   /* interface is used by routing now that an address is set */
186 
187   return ERR_OK;
188 }
189 
190 /**
191 * Handle conflict information from ACD module
192 *
193 * @param netif   network interface to handle conflict information on
194 * @param state   acd_callback_enum_t
195  */
196 static void
autoip_conflict_callback(struct netif * netif,acd_callback_enum_t state)197 autoip_conflict_callback(struct netif *netif, acd_callback_enum_t state)
198 {
199   struct autoip *autoip = netif_autoip_data(netif);
200 
201   switch (state) {
202     case ACD_IP_OK:
203       autoip_bind(netif);
204       break;
205     case ACD_RESTART_CLIENT:
206       autoip_restart(netif);
207       break;
208     case ACD_DECLINE:
209       /* "delete" conflicting address so a new one will be selected in
210        * autoip_start() */
211       ip4_addr_set_any(&autoip->llipaddr);
212       autoip_stop(netif);
213       break;
214       default:
215       break;
216   }
217 }
218 
219 /**
220  * @ingroup autoip
221  * Start AutoIP client
222  *
223  * @param netif network interface on which start the AutoIP client
224  */
225 err_t
autoip_start(struct netif * netif)226 autoip_start(struct netif *netif)
227 {
228   struct autoip *autoip = netif_autoip_data(netif);
229   err_t result = ERR_OK;
230 
231   LWIP_ASSERT_CORE_LOCKED();
232   LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;);
233 
234   if (autoip->state == AUTOIP_STATE_OFF) {
235     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
236                 ("autoip_start(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0],
237                  netif->name[1], (u16_t)netif->num));
238     if (autoip == NULL) {
239       /* no AutoIP client attached yet? */
240       LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
241                   ("autoip_start(): starting new AUTOIP client\n"));
242       autoip = (struct autoip *)mem_calloc(1, sizeof(struct autoip));
243       if (autoip == NULL) {
244         LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
245                     ("autoip_start(): could not allocate autoip\n"));
246         return ERR_MEM;
247       }
248       /* store this AutoIP client in the netif */
249       netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip);
250       LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip"));
251     }
252 
253     /* add acd struct to list*/
254     acd_add(netif, &autoip->acd, autoip_conflict_callback);
255 
256     /* In accordance to RFC3927 section 2.1:
257      * Keep using the same link local address as much as possible.
258      * Only when their is none or when their was a conflict, select a new one.
259      */
260     if (!ip4_addr_islinklocal(&autoip->llipaddr)) {
261       autoip_create_addr(netif, &(autoip->llipaddr));
262     }
263     autoip->state = AUTOIP_STATE_CHECKING;
264     acd_start(netif, &autoip->acd, autoip->llipaddr);
265   }
266   else {
267     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
268                 ("autoip_start(): already started on netif=%p %c%c%"U16_F"\n",
269                 (void *)netif, netif->name[0],
270                  netif->name[1], (u16_t)netif->num));
271   }
272 
273   return result;
274 }
275 
276 
277 /**
278  * Handle a possible change in the network configuration: link up
279  *
280  * If there is an AutoIP address configured and AutoIP is not in cooperation
281  * with DHCP, start probing for previous address.
282  */
283 void
autoip_network_changed_link_up(struct netif * netif)284 autoip_network_changed_link_up(struct netif *netif)
285 {
286   struct autoip *autoip = netif_autoip_data(netif);
287 
288   if (autoip && (autoip->state != AUTOIP_STATE_OFF) && !LWIP_DHCP_AUTOIP_COOP) {
289     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
290                 ("autoip_network_changed_link_up(): start acd"));
291     autoip->state = AUTOIP_STATE_CHECKING;
292     /* Start acd check again for the last used address */
293     acd_start(netif, &autoip->acd, autoip->llipaddr);
294   }
295 }
296 
297 /**
298  * Handle a possible change in the network configuration: link down
299  *
300  * If there is an AutoIP address configured and AutoIP is in cooperation
301  * with DHCP, then stop the autoip module. When the link goes up, we do not want
302  * the autoip module to start again. DHCP will initiate autoip when needed.
303  */
304 void
autoip_network_changed_link_down(struct netif * netif)305 autoip_network_changed_link_down(struct netif *netif)
306 {
307   struct autoip *autoip = netif_autoip_data(netif);
308 
309   if (autoip && (autoip->state != AUTOIP_STATE_OFF) && LWIP_DHCP_AUTOIP_COOP) {
310     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
311                 ("autoip_network_changed_link_down(): stop autoip"));
312     autoip_stop(netif);
313   }
314 }
315 
316 /**
317  * @ingroup autoip
318  * Stop AutoIP client
319  *
320  * @param netif network interface on which stop the AutoIP client
321  */
322 err_t
autoip_stop(struct netif * netif)323 autoip_stop(struct netif *netif)
324 {
325   struct autoip *autoip = netif_autoip_data(netif);
326 
327   LWIP_ASSERT_CORE_LOCKED();
328   if (autoip != NULL) {
329     autoip->state = AUTOIP_STATE_OFF;
330     if (ip4_addr_islinklocal(netif_ip4_addr(netif))) {
331       netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
332     }
333     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,("autoip_stop()"));
334   }
335   return ERR_OK;
336 }
337 
338 /** check if AutoIP supplied netif->ip_addr
339  *
340  * @param netif the netif to check
341  * @return 1 if AutoIP supplied netif->ip_addr (state BOUND),
342  *         0 otherwise
343  */
344 u8_t
autoip_supplied_address(struct netif * netif)345 autoip_supplied_address(struct netif *netif)
346 {
347   struct autoip *autoip = netif_autoip_data(netif);
348   return     (autoip != NULL)
349           && (ip4_addr_cmp(netif_ip4_addr(netif), &(autoip->llipaddr)))
350           && (autoip->state == AUTOIP_STATE_BOUND);
351 }
352 
353 u8_t
autoip_accept_packet(struct netif * netif,const ip4_addr_t * addr)354 autoip_accept_packet(struct netif *netif, const ip4_addr_t *addr)
355 {
356   struct autoip *autoip = netif_autoip_data(netif);
357   return     (autoip != NULL)
358           && (ip4_addr_cmp(addr, &(autoip->llipaddr)))
359           && (autoip->state == AUTOIP_STATE_BOUND);
360 }
361 
362 #endif /* LWIP_IPV4 && LWIP_AUTOIP */
363