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-2024 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.4.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  //Use default interface
62  settings->interface = netGetDefaultInterface();
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  //Save user settings
103  context->settings = *settings;
104 
105  //Use default link-local address
106  context->linkLocalAddr = settings->linkLocalAddr;
107  //Reset conflict counter
108  context->conflictCount = 0;
109 
110  //Auto-IP operation is currently suspended
111  context->running = FALSE;
112  //Initialize state machine
113  context->state = AUTO_IP_STATE_INIT;
114 
115  //Attach the Auto-IP context to the network interface
116  interface->autoIpContext = context;
117 
118  //Successful initialization
119  return NO_ERROR;
120 }
121 
122 
123 /**
124  * @brief Start Auto-IP process
125  * @param[in] context Pointer to the Auto-IP context
126  * @return Error code
127  **/
128 
130 {
131  //Make sure the Auto-IP context is valid
132  if(context == NULL)
134 
135  //Debug message
136  TRACE_INFO("Starting Auto-IP...\r\n");
137 
138  //Get exclusive access
140 
141  //Reset Auto-IP configuration
142  autoIpResetConfig(context);
143 
144  //Initialize state machine
145  context->state = AUTO_IP_STATE_INIT;
146  //Reset conflict counter
147  context->conflictCount = 0;
148  //Start Auto-IP operation
149  context->running = TRUE;
150 
151  //Release exclusive access
153 
154  //Successful processing
155  return NO_ERROR;
156 }
157 
158 
159 /**
160  * @brief Stop Auto-IP process
161  * @param[in] context Pointer to the Auto-IP context
162  * @return Error code
163  **/
164 
166 {
167  //Make sure the Auto-IP context is valid
168  if(context == NULL)
170 
171  //Debug message
172  TRACE_INFO("Stopping Auto-IP...\r\n");
173 
174  //Get exclusive access
176 
177  //Suspend Auto-IP operation
178  context->running = FALSE;
179  //Reinitialize state machine
180  context->state = AUTO_IP_STATE_INIT;
181 
182  //Release exclusive access
184 
185  //Successful processing
186  return NO_ERROR;
187 }
188 
189 
190 /**
191  * @brief Retrieve current state
192  * @param[in] context Pointer to the Auto-IP context
193  * @return Current Auto-IP state
194  **/
195 
197 {
198  AutoIpState state;
199 
200  //Get exclusive access
202  //Get current state
203  state = context->state;
204  //Release exclusive access
206 
207  //Return current state
208  return state;
209 }
210 
211 #endif
void autoIpGetDefaultSettings(AutoIpSettings *settings)
Initialize settings with default values.
Definition: auto_ip.c:59
error_t autoIpStop(AutoIpContext *context)
Stop Auto-IP process.
Definition: auto_ip.c:165
error_t autoIpStart(AutoIpContext *context)
Start Auto-IP process.
Definition: auto_ip.c:129
error_t autoIpInit(AutoIpContext *context, const AutoIpSettings *settings)
Auto-IP initialization.
Definition: auto_ip.c:82
AutoIpState autoIpGetState(AutoIpContext *context)
Retrieve current state.
Definition: auto_ip.c:196
Auto-IP (Dynamic Configuration of IPv4 Link-Local Addresses)
AutoIpState
Auto-IP FSM states.
Definition: auto_ip.h:152
@ AUTO_IP_STATE_INIT
Definition: auto_ip.h:153
#define AutoIpContext
Definition: auto_ip.h:139
void autoIpResetConfig(AutoIpContext *context)
Reset Auto-IP configuration.
Definition: auto_ip_misc.c:371
Helper functions for Auto-IP.
Debugging facilities.
#define TRACE_INFO(...)
Definition: debug.h:95
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:110
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:470
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netMutex
Definition: net_legacy.h:195
#define osMemset(p, value, length)
Definition: os_port.h:135
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Auto-IP settings.
Definition: auto_ip.h:182
AutoIpLinkChangeCallback linkChangeEvent
Link state change event.
Definition: auto_ip.h:186
AutoIpStateChangeCallback stateChangeEvent
FSM state change event.
Definition: auto_ip.h:187
uint_t ipAddrIndex
Index of the IP address to be configured.
Definition: auto_ip.h:184
Ipv4Addr linkLocalAddr
Initial link-local address to be used.
Definition: auto_ip.h:185
NetInterface * interface
Network interface to configure.
Definition: auto_ip.h:183