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 <kontakt@dspies.de>
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 <kontakt@dspies.de>
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 /**
113  * @ingroup autoip
114  * Remove a struct autoip previously set to the netif using autoip_set_struct()
115  *
116  * @param netif the netif for which to set the struct autoip
117  */
118 void
autoip_remove_struct(struct netif * netif)119 autoip_remove_struct(struct netif *netif)
120 {
121   LWIP_ASSERT_CORE_LOCKED();
122   LWIP_ASSERT("netif != NULL", netif != NULL);
123   LWIP_ASSERT("netif has no struct autoip set",
124               netif_autoip_data(netif) != NULL);
125 
126   netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, NULL);
127 }
128 
129 /** Restart AutoIP client and check the next address (conflict detected)
130  *
131  * @param netif The netif under AutoIP control
132  */
133 static void
autoip_restart(struct netif * netif)134 autoip_restart(struct netif *netif)
135 {
136   struct autoip *autoip = netif_autoip_data(netif);
137   autoip->tried_llipaddr++;
138   autoip_start(netif);
139 }
140 
141 
142 /**
143  * Create an IP-Address out of range 169.254.1.0 to 169.254.254.255
144  *
145  * @param netif network interface on which create the IP-Address
146  * @param ipaddr ip address to initialize
147  */
148 static void
autoip_create_addr(struct netif * netif,ip4_addr_t * ipaddr)149 autoip_create_addr(struct netif *netif, ip4_addr_t *ipaddr)
150 {
151   struct autoip *autoip = netif_autoip_data(netif);
152 
153   /* Here we create an IP-Address out of range 169.254.1.0 to 169.254.254.255
154    * compliant to RFC 3927 Section 2.1
155    * We have 254 * 256 possibilities */
156 
157   u32_t addr = lwip_ntohl(LWIP_AUTOIP_CREATE_SEED_ADDR(netif));
158   addr += autoip->tried_llipaddr;
159   addr = AUTOIP_NET | (addr & 0xffff);
160   /* Now, 169.254.0.0 <= addr <= 169.254.255.255 */
161 
162   if (addr < AUTOIP_RANGE_START) {
163     addr += AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
164   }
165   if (addr > AUTOIP_RANGE_END) {
166     addr -= AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
167   }
168   LWIP_ASSERT("AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) &&
169               (addr <= AUTOIP_RANGE_END));
170   ip4_addr_set_u32(ipaddr, lwip_htonl(addr));
171 
172   LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
173               ("autoip_create_addr(): tried_llipaddr=%"U16_F", %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
174                (u16_t)(autoip->tried_llipaddr), ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr),
175                ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
176 }
177 
178 
179 /**
180  * Configure interface for use with current LL IP-Address
181  *
182  * @param netif network interface to configure with current LL IP-Address
183  */
184 static err_t
autoip_bind(struct netif * netif)185 autoip_bind(struct netif *netif)
186 {
187   struct autoip *autoip = netif_autoip_data(netif);
188   ip4_addr_t sn_mask, gw_addr;
189 
190   autoip->state = AUTOIP_STATE_BOUND;
191 
192   LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
193               ("autoip_bind(netif=%p) %c%c%"U16_F" %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
194                (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num,
195                ip4_addr1_16(&autoip->llipaddr), ip4_addr2_16(&autoip->llipaddr),
196                ip4_addr3_16(&autoip->llipaddr), ip4_addr4_16(&autoip->llipaddr)));
197 
198   IP4_ADDR(&sn_mask, 255, 255, 0, 0);
199   IP4_ADDR(&gw_addr, 0, 0, 0, 0);
200 
201   netif_set_addr(netif, &autoip->llipaddr, &sn_mask, &gw_addr);
202   /* interface is used by routing now that an address is set */
203 
204   return ERR_OK;
205 }
206 
207 /**
208 * Handle conflict information from ACD module
209 *
210 * @param netif   network interface to handle conflict information on
211 * @param state   acd_callback_enum_t
212  */
213 static void
autoip_conflict_callback(struct netif * netif,acd_callback_enum_t state)214 autoip_conflict_callback(struct netif *netif, acd_callback_enum_t state)
215 {
216   struct autoip *autoip = netif_autoip_data(netif);
217 
218   switch (state) {
219     case ACD_IP_OK:
220       autoip_bind(netif);
221       break;
222     case ACD_RESTART_CLIENT:
223       autoip_restart(netif);
224       break;
225     case ACD_DECLINE:
226       /* "delete" conflicting address so a new one will be selected in
227        * autoip_start() */
228       ip4_addr_set_any(&autoip->llipaddr);
229       autoip_stop(netif);
230       break;
231       default:
232       break;
233   }
234 }
235 
236 /**
237  * @ingroup autoip
238  * Start AutoIP client
239  *
240  * @param netif network interface on which start the AutoIP client
241  */
242 err_t
autoip_start(struct netif * netif)243 autoip_start(struct netif *netif)
244 {
245   struct autoip *autoip = netif_autoip_data(netif);
246   err_t result = ERR_OK;
247 
248   LWIP_ASSERT_CORE_LOCKED();
249   LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;);
250 
251   if (autoip == NULL) {
252     /* no AutoIP client attached yet? */
253     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
254                 ("autoip_start(): starting new AUTOIP client\n"));
255     autoip = (struct autoip *)mem_calloc(1, sizeof(struct autoip));
256     if (autoip == NULL) {
257       LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
258                   ("autoip_start(): could not allocate autoip\n"));
259       return ERR_MEM;
260     }
261     /* store this AutoIP client in the netif */
262     netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip);
263     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip\n"));
264   }
265 
266   if (autoip->state == AUTOIP_STATE_OFF) {
267     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
268                 ("autoip_start(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0],
269                  netif->name[1], (u16_t)netif->num));
270 
271     /* add acd struct to list*/
272     acd_add(netif, &autoip->acd, autoip_conflict_callback);
273 
274     /* In accordance to RFC3927 section 2.1:
275      * Keep using the same link local address as much as possible.
276      * Only when there is none or when there was a conflict, select a new one.
277      */
278     if (!ip4_addr_islinklocal(&autoip->llipaddr)) {
279       autoip_create_addr(netif, &(autoip->llipaddr));
280     }
281     autoip->state = AUTOIP_STATE_CHECKING;
282     acd_start(netif, &autoip->acd, autoip->llipaddr);
283   } else {
284     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
285                 ("autoip_start(): already started on netif=%p %c%c%"U16_F"\n",
286                 (void *)netif, netif->name[0],
287                  netif->name[1], (u16_t)netif->num));
288   }
289 
290   return result;
291 }
292 
293 
294 /**
295  * Handle a possible change in the network configuration: link up
296  *
297  * If there is an AutoIP address configured and AutoIP is not in cooperation
298  * with DHCP, start probing for previous address.
299  */
300 void
autoip_network_changed_link_up(struct netif * netif)301 autoip_network_changed_link_up(struct netif *netif)
302 {
303   struct autoip *autoip = netif_autoip_data(netif);
304 
305   if (autoip && (autoip->state != AUTOIP_STATE_OFF) && !LWIP_DHCP_AUTOIP_COOP) {
306     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
307                 ("autoip_network_changed_link_up(): start acd\n"));
308     autoip->state = AUTOIP_STATE_CHECKING;
309     /* Start acd check again for the last used address */
310     acd_start(netif, &autoip->acd, autoip->llipaddr);
311   }
312 }
313 
314 /**
315  * Handle a possible change in the network configuration: link down
316  *
317  * If there is an AutoIP address configured and AutoIP is in cooperation
318  * with DHCP, then stop the autoip module. When the link goes up, we do not want
319  * the autoip module to start again. DHCP will initiate autoip when needed.
320  */
321 void
autoip_network_changed_link_down(struct netif * netif)322 autoip_network_changed_link_down(struct netif *netif)
323 {
324   struct autoip *autoip = netif_autoip_data(netif);
325 
326   if (autoip && (autoip->state != AUTOIP_STATE_OFF) && LWIP_DHCP_AUTOIP_COOP) {
327     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
328                 ("autoip_network_changed_link_down(): stop autoip\n"));
329     autoip_stop(netif);
330   }
331 }
332 
333 /**
334  * @ingroup autoip
335  * Stop AutoIP client
336  *
337  * @param netif network interface on which stop the AutoIP client
338  */
339 err_t
autoip_stop(struct netif * netif)340 autoip_stop(struct netif *netif)
341 {
342   struct autoip *autoip = netif_autoip_data(netif);
343 
344   LWIP_ASSERT_CORE_LOCKED();
345   if (autoip != NULL) {
346     autoip->state = AUTOIP_STATE_OFF;
347     if (ip4_addr_islinklocal(netif_ip4_addr(netif))) {
348       netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
349     }
350     LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,("autoip_stop()\n"));
351   }
352   return ERR_OK;
353 }
354 
355 /** check if AutoIP supplied netif->ip_addr
356  *
357  * @param netif the netif to check
358  * @return 1 if AutoIP supplied netif->ip_addr (state BOUND),
359  *         0 otherwise
360  */
361 u8_t
autoip_supplied_address(struct netif * netif)362 autoip_supplied_address(struct netif *netif)
363 {
364   struct autoip *autoip = netif_autoip_data(netif);
365   return     (autoip != NULL)
366           && (ip4_addr_eq(netif_ip4_addr(netif), &(autoip->llipaddr)))
367           && (autoip->state == AUTOIP_STATE_BOUND);
368 }
369 
370 u8_t
autoip_accept_packet(struct netif * netif,const ip4_addr_t * addr)371 autoip_accept_packet(struct netif *netif, const ip4_addr_t *addr)
372 {
373   struct autoip *autoip = netif_autoip_data(netif);
374   return     (autoip != NULL)
375           && (ip4_addr_eq(addr, &(autoip->llipaddr)))
376           && (autoip->state == AUTOIP_STATE_BOUND);
377 }
378 
379 #endif /* LWIP_IPV4 && LWIP_AUTOIP */
380