auto_ip.c
Go to the documentation of this file.
1 /**
2  * @file auto_ip.c
3  * @brief Auto-IP (Dynamic Configuration of IPv4 Link-Local Addresses)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @section Description
28  *
29  * Auto-IP describes a method by which a host may automatically configure an
30  * interface with an IPv4 address in the 169.254/16 prefix that is valid for
31  * Link-Local communication on that interface. This is especially valuable in
32  * environments where no other configuration mechanism is available. Refer to
33  * the following RFCs for complete details:
34  * - RFC 3927: Dynamic Configuration of IPv4 Link-Local Addresses
35  * - RFC 5227: IPv4 Address Conflict Detection
36  *
37  * @author Oryx Embedded SARL (www.oryx-embedded.com)
38  * @version 2.6.0
39  **/
40 
41 //Switch to the appropriate trace level
42 #define TRACE_LEVEL AUTO_IP_TRACE_LEVEL
43 
44 //Dependencies
45 #include "core/net.h"
46 #include "ipv4/auto_ip.h"
47 #include "ipv4/auto_ip_misc.h"
48 #include "debug.h"
49 
50 //Check TCP/IP stack configuration
51 #if (IPV4_SUPPORT == ENABLED && AUTO_IP_SUPPORT == ENABLED)
52 
53 
54 /**
55  * @brief Initialize settings with default values
56  * @param[out] settings Structure that contains Auto-IP settings
57  **/
58 
60 {
61  //Network interface to configure
62  settings->interface = NULL;
63  //Index of the IP address to be configured
64  settings->ipAddrIndex = 0;
65 
66  //Initial link-local address to be used
68  //Link state change event
69  settings->linkChangeEvent = NULL;
70  //FSM state change event
71  settings->stateChangeEvent = NULL;
72 }
73 
74 
75 /**
76  * @brief Auto-IP initialization
77  * @param[in] context Pointer to the Auto-IP context
78  * @param[in] settings Auto-IP specific settings
79  * @return Error code
80  **/
81 
82 error_t autoIpInit(AutoIpContext *context, const AutoIpSettings *settings)
83 {
84  NetInterface *interface;
85 
86  //Debug message
87  TRACE_INFO("Initializing Auto-IP...\r\n");
88 
89  //Ensure the parameters are valid
90  if(context == NULL || settings == NULL)
92 
93  //The Auto-IP service must be bound to a valid interface
94  if(settings->interface == NULL)
96 
97  //Point to the underlying network interface
98  interface = settings->interface;
99 
100  //Clear the Auto-IP context
101  osMemset(context, 0, sizeof(AutoIpContext));
102 
103  //Attach TCP/IP stack context
104  context->netContext = settings->interface->netContext;
105 
106  //Save user settings
107  context->interface = settings->interface;
108  context->ipAddrIndex = settings->ipAddrIndex;
109  context->linkChangeEvent = settings->linkChangeEvent;
110  context->stateChangeEvent = settings->stateChangeEvent;
111 
112  //Use default link-local address
113  context->linkLocalAddr = settings->linkLocalAddr;
114  //Reset conflict counter
115  context->conflictCount = 0;
116 
117  //Auto-IP operation is currently suspended
118  context->running = FALSE;
119  //Initialize state machine
120  context->state = AUTO_IP_STATE_INIT;
121 
122  //Get exclusive access
123  netLock(context->netContext);
124  //Attach the Auto-IP context to the network interface
125  interface->autoIpContext = context;
126  //Release exclusive access
127  netUnlock(context->netContext);
128 
129  //Successful initialization
130  return NO_ERROR;
131 }
132 
133 
134 /**
135  * @brief Start Auto-IP process
136  * @param[in] context Pointer to the Auto-IP context
137  * @return Error code
138  **/
139 
141 {
142  //Make sure the Auto-IP context is valid
143  if(context == NULL)
145 
146  //Debug message
147  TRACE_INFO("Starting Auto-IP...\r\n");
148 
149  //Get exclusive access
150  netLock(context->netContext);
151 
152  //Reset Auto-IP configuration
153  autoIpResetConfig(context);
154 
155  //Initialize state machine
156  context->state = AUTO_IP_STATE_INIT;
157  //Reset conflict counter
158  context->conflictCount = 0;
159  //Start Auto-IP operation
160  context->running = TRUE;
161 
162  //Release exclusive access
163  netUnlock(context->netContext);
164 
165  //Successful processing
166  return NO_ERROR;
167 }
168 
169 
170 /**
171  * @brief Stop Auto-IP process
172  * @param[in] context Pointer to the Auto-IP context
173  * @return Error code
174  **/
175 
177 {
178  //Make sure the Auto-IP context is valid
179  if(context == NULL)
181 
182  //Debug message
183  TRACE_INFO("Stopping Auto-IP...\r\n");
184 
185  //Get exclusive access
186  netLock(context->netContext);
187 
188  //Suspend Auto-IP operation
189  context->running = FALSE;
190  //Reinitialize state machine
191  context->state = AUTO_IP_STATE_INIT;
192 
193  //Release exclusive access
194  netUnlock(context->netContext);
195 
196  //Successful processing
197  return NO_ERROR;
198 }
199 
200 
201 /**
202  * @brief Retrieve current state
203  * @param[in] context Pointer to the Auto-IP context
204  * @return Current Auto-IP state
205  **/
206 
208 {
209  AutoIpState state;
210 
211  //Get exclusive access
212  netLock(context->netContext);
213  //Get current state
214  state = context->state;
215  //Release exclusive access
216  netUnlock(context->netContext);
217 
218  //Return current state
219  return state;
220 }
221 
222 
223 /**
224  * @brief Release Auto-IP context
225  * @param[in] context Pointer to the Auto-IP context
226  **/
227 
229 {
230  NetInterface *interface;
231 
232  //Make sure the Auto-IP context is valid
233  if(context != NULL)
234  {
235  //Get exclusive access
236  netLock(context->netContext);
237 
238  //Point to the underlying network interface
239  interface = context->interface;
240  //Detach the Auto-IP context from the network interface
241  interface->autoIpContext = NULL;
242 
243  //Release exclusive access
244  netUnlock(context->netContext);
245 
246  //Clear Auto-IP context
247  osMemset(context, 0, sizeof(AutoIpContext));
248  }
249 }
250 
251 #endif
AutoIpState autoIpGetState(AutoIpContext *context)
Retrieve current state.
Definition: auto_ip.c:207
error_t autoIpInit(AutoIpContext *context, const AutoIpSettings *settings)
Auto-IP initialization.
Definition: auto_ip.c:82
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Definition: net.c:319
AutoIpStateChangeCallback stateChangeEvent
FSM state change event.
Definition: auto_ip.h:187
#define TRUE
Definition: os_port.h:50
#define AutoIpContext
Definition: auto_ip.h:139
Auto-IP settings.
Definition: auto_ip.h:182
error_t autoIpStop(AutoIpContext *context)
Stop Auto-IP process.
Definition: auto_ip.c:176
AutoIpLinkChangeCallback linkChangeEvent
Link state change event.
Definition: auto_ip.h:186
#define FALSE
Definition: os_port.h:46
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
#define NetInterface
Definition: net.h:40
uint_t ipAddrIndex
Index of the IP address to be configured.
Definition: auto_ip.h:184
void autoIpResetConfig(AutoIpContext *context)
Reset Auto-IP configuration.
Definition: auto_ip_misc.c:364
#define TRACE_INFO(...)
Definition: debug.h:105
void autoIpGetDefaultSettings(AutoIpSettings *settings)
Initialize settings with default values.
Definition: auto_ip.c:59
Ipv4Addr linkLocalAddr
Initial link-local address to be used.
Definition: auto_ip.h:185
void autoIpDeinit(AutoIpContext *context)
Release Auto-IP context.
Definition: auto_ip.c:228
@ AUTO_IP_STATE_INIT
Definition: auto_ip.h:153
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:307
Helper functions for Auto-IP.
NetInterface * interface
Network interface to configure.
Definition: auto_ip.h:183
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
AutoIpState
Auto-IP FSM states.
Definition: auto_ip.h:152
error_t autoIpStart(AutoIpContext *context)
Start Auto-IP process.
Definition: auto_ip.c:140
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:128
Auto-IP (Dynamic Configuration of IPv4 Link-Local Addresses)