auto_ip_misc.c
Go to the documentation of this file.
1 /**
2  * @file auto_ip_misc.c
3  * @brief Helper functions for Auto-IP
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL AUTO_IP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/ethernet.h"
37 #include "ipv4/arp.h"
38 #include "ipv4/auto_ip.h"
39 #include "ipv4/auto_ip_misc.h"
40 #include "mdns/mdns_responder.h"
41 #include "debug.h"
42 
43 //Check TCP/IP stack configuration
44 #if (IPV4_SUPPORT == ENABLED && AUTO_IP_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Auto-IP timer handler
49  *
50  * This routine must be periodically called by the TCP/IP stack to
51  * manage Auto-IP operation
52  *
53  * @param[in] context Pointer to the Auto-IP context
54  **/
55 
56 void autoIpTick(AutoIpContext *context)
57 {
58  uint_t i;
60  systime_t delay;
61  NetInterface *interface;
62 
63  //Make sure Auto-IP has been properly instantiated
64  if(context == NULL)
65  return;
66 
67  //Point to the underlying network interface
68  interface = context->interface;
69  //Index of the IP address in the list of addresses assigned to the interface
70  i = context->ipAddrIndex;
71 
72  //Get current time
74 
75  //Check current state
76  if(context->state == AUTO_IP_STATE_INIT)
77  {
78  //Wait for the link to be up before starting Auto-IP
79  if(context->running && interface->linkState)
80  {
81  //Configure subnet mask
82  interface->ipv4Context.addrList[i].subnetMask = AUTO_IP_MASK;
83 
84  //The address must be in the range from 169.254.1.0 to 169.254.254.255
85  if(ntohl(context->linkLocalAddr) < NTOHL(AUTO_IP_ADDR_MIN) ||
86  ntohl(context->linkLocalAddr) > NTOHL(AUTO_IP_ADDR_MAX))
87  {
88  //Generate a random link-local address
89  autoIpGenerateAddr(context);
90  }
91 
92  //Use the link-local address as a tentative address
93  interface->ipv4Context.addrList[i].addr = context->linkLocalAddr;
94  interface->ipv4Context.addrList[i].state = IPV4_ADDR_STATE_TENTATIVE;
95 
96  //Clear conflict flag
97  interface->ipv4Context.addrList[i].conflict = FALSE;
98 
99  //Initial random delay
100  delay = netGenerateRandRange(context->netContext, 0,
102 
103  //Check whether the number of conflicts exceeds the maximum acceptable
104  //value
105  if(context->conflictCount >= AUTO_IP_MAX_CONFLICTS)
106  {
107  //The host must limit the rate at which it probes for new addresses
109  }
110 
111  //Verify the uniqueness of the link-local address
112  autoIpChangeState(context, AUTO_IP_STATE_PROBING, delay);
113  }
114  }
115  else if(context->state == AUTO_IP_STATE_PROBING)
116  {
117  //Any conflict detected?
118  if(interface->ipv4Context.addrList[i].conflict)
119  {
120  //The address is already in use by some other host and must not be
121  //assigned to the interface
122  autoIpResetConfig(context);
123 
124  //The host should maintain a counter of the number of address conflicts
125  //it has experienced
126  context->conflictCount++;
127 
128  //The host must pick a new random address and repeat the process
129  autoIpGenerateAddr(context);
130  //Update state machine
132  }
133  else
134  {
135  //Check current time
136  if(timeCompare(time, context->timestamp + context->timeout) >= 0)
137  {
138  //Address Conflict Detection is on-going?
139  if(context->retransmitCount < AUTO_IP_PROBE_NUM)
140  {
141  //Conflict detection is done using ARP probes
142  arpSendProbe(interface, context->linkLocalAddr);
143 
144  //Save the time at which the packet was sent
145  context->timestamp = time;
146  //Increment retransmission counter
147  context->retransmitCount++;
148 
149  //Last probe packet sent?
150  if(context->retransmitCount == AUTO_IP_PROBE_NUM)
151  {
152  //Delay before announcing
153  context->timeout = AUTO_IP_ANNOUNCE_WAIT;
154  }
155  else
156  {
157  //Maximum delay till repeated probe
158  context->timeout = netGenerateRandRange(context->netContext,
160  }
161  }
162  else
163  {
164  //The use of the IPv4 address is now unrestricted
165  interface->ipv4Context.addrList[i].state = IPV4_ADDR_STATE_VALID;
166 
167 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
168  //Restart mDNS probing process
169  mdnsResponderStartProbing(interface->mdnsResponderContext);
170 #endif
171  //The host must then announce its claimed address
173  }
174  }
175  }
176  }
177  else if(context->state == AUTO_IP_STATE_ANNOUNCING)
178  {
179  //Check current time
180  if(timeCompare(time, context->timestamp + context->timeout) >= 0)
181  {
182  //An ARP announcement is identical to an ARP probe, except that now
183  //the sender and target IP addresses are both set to the host's newly
184  //selected IPv4 address
185  arpSendRequest(interface, context->linkLocalAddr, &MAC_BROADCAST_ADDR);
186 
187  //Save the time at which the packet was sent
188  context->timestamp = time;
189  //Time interval between announcement packets
190  context->timeout = AUTO_IP_ANNOUNCE_INTERVAL;
191  //Increment retransmission counter
192  context->retransmitCount++;
193 
194  //Announcing is complete?
195  if(context->retransmitCount >= AUTO_IP_ANNOUNCE_NUM)
196  {
197  //Successful address autoconfiguration
199  //Reset conflict counter
200  context->conflictCount = 0;
201 
202  //Dump current IPv4 configuration for debugging purpose
203  autoIpDumpConfig(context);
204  }
205  }
206  }
207  else if(context->state == AUTO_IP_STATE_CONFIGURED)
208  {
209  //Address Conflict Detection is an on-going process that is in effect for
210  //as long as a host is using an IPv4 link-local address
211  if(interface->ipv4Context.addrList[i].conflict)
212  {
213  //The host may elect to attempt to defend its address by recording
214  //the time that the conflicting ARP packet was received, and then
215  //broadcasting one single ARP announcement, giving its own IP and
216  //hardware addresses as the sender addresses of the ARP
217 #if (AUTO_IP_BCT_SUPPORT == ENABLED)
218  arpSendProbe(interface, context->linkLocalAddr);
219 #else
220  arpSendRequest(interface, context->linkLocalAddr, &MAC_BROADCAST_ADDR);
221 #endif
222  //Clear conflict flag
223  interface->ipv4Context.addrList[i].conflict = FALSE;
224 
225  //The host can then continue to use the address normally without
226  //any further special action
228  }
229  }
230  else if(context->state == AUTO_IP_STATE_DEFENDING)
231  {
232  //if this is not the first conflicting ARP packet the host has seen, and
233  //the time recorded for the previous conflicting ARP packet is recent,
234  //within DEFEND_INTERVAL seconds, then the host must immediately cease
235  //using this address
236  if(interface->ipv4Context.addrList[i].conflict)
237  {
238  //The link-local address cannot be used anymore
239  autoIpResetConfig(context);
240 
241 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
242  //Restart mDNS probing process
243  mdnsResponderStartProbing(interface->mdnsResponderContext);
244 #endif
245  //The host must pick a new random address and probes/announces again
246  autoIpGenerateAddr(context);
247  //Update state machine
249  }
250  else
251  {
252  //Check whether the DEFEND_INTERVAL has elapsed
253  if(timeCompare(time, context->timestamp + AUTO_IP_DEFEND_INTERVAL) >= 0)
254  {
255  //The host can continue to use its link-local address
257  }
258  }
259  }
260 }
261 
262 
263 /**
264  * @brief Callback function for link change event
265  * @param[in] context Pointer to the Auto-IP context
266  **/
267 
269 {
270  NetInterface *interface;
271 
272  //Make sure Auto-IP has been properly instantiated
273  if(context == NULL)
274  return;
275 
276  //Point to the underlying network interface
277  interface = context->interface;
278 
279  //Check whether Auto-IP is enabled
280  if(context->running)
281  {
282  //The host address is not longer valid
283  autoIpResetConfig(context);
284 
285 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
286  //Restart mDNS probing process
287  mdnsResponderStartProbing(interface->mdnsResponderContext);
288 #endif
289  }
290 
291  //Reinitialize state machine
292  context->state = AUTO_IP_STATE_INIT;
293  //Reset conflict counter
294  context->conflictCount = 0;
295 
296  //Any registered callback?
297  if(context->linkChangeEvent != NULL)
298  {
299  //Release exclusive access
300  netUnlock(context->netContext);
301  //Invoke user callback function
302  context->linkChangeEvent(context, interface, interface->linkState);
303  //Get exclusive access
304  netLock(context->netContext);
305  }
306 }
307 
308 
309 /**
310  * @brief Update Auto-IP FSM state
311  * @param[in] context Pointer to the Auto-IP context
312  * @param[in] newState New Auto-IP state to switch to
313  * @param[in] delay Initial delay
314  **/
315 
317  systime_t delay)
318 {
319  //Set time stamp
320  context->timestamp = osGetSystemTime();
321  //Set initial delay
322  context->timeout = delay;
323  //Reset retransmission counter
324  context->retransmitCount = 0;
325  //Switch to the new state
326  context->state = newState;
327 
328  //Any registered callback?
329  if(context->stateChangeEvent != NULL)
330  {
331  //Release exclusive access
332  netUnlock(context->netContext);
333  //Invoke user callback function
334  context->stateChangeEvent(context, context->interface, newState);
335  //Get exclusive access
336  netLock(context->netContext);
337  }
338 }
339 
340 
341 /**
342  * @brief Generate a random link-local address
343  * @param[in] context Pointer to the Auto-IP context
344  **/
345 
347 {
348  uint32_t n;
349 
350  //Generate a random address in the range from 169.254.1.0 to 169.254.254.255
351  n = netGenerateRand(context->netContext) % (NTOHL(AUTO_IP_ADDR_MAX - AUTO_IP_ADDR_MIN) + 1);
353 
354  //Convert the resulting address to network byte order
355  context->linkLocalAddr = htonl(n);
356 }
357 
358 
359 /**
360  * @brief Reset Auto-IP configuration
361  * @param[in] context Pointer to the Auto-IP context
362  **/
363 
365 {
366  uint_t i;
367  NetInterface *interface;
368 
369  //Point to the underlying network interface
370  interface = context->interface;
371  //Index of the IP address in the list of addresses assigned to the interface
372  i = context->ipAddrIndex;
373 
374  //The host address is not longer valid
375  interface->ipv4Context.addrList[i].addr = IPV4_UNSPECIFIED_ADDR;
376  interface->ipv4Context.addrList[i].state = IPV4_ADDR_STATE_INVALID;
377 
378  //Clear subnet mask
379  interface->ipv4Context.addrList[i].subnetMask = IPV4_UNSPECIFIED_ADDR;
380 
381  //The host must not send packets to any router for forwarding (refer to
382  //RFC 3927, section 2.6.2)
383  interface->ipv4Context.addrList[i].defaultGateway = IPV4_UNSPECIFIED_ADDR;
384 }
385 
386 
387 /**
388  * @brief Dump Auto-IP configuration for debugging purpose
389  * @param[in] context Pointer to the Auto-IP context
390  **/
391 
393 {
394 #if (AUTO_IP_TRACE_LEVEL >= TRACE_LEVEL_INFO)
395  uint_t i;
396  Ipv4Context *ipv4Context;
397 
398  //Point to the IPv4 context
399  ipv4Context = &context->interface->ipv4Context;
400  //Index of the IP address in the list of addresses assigned to the interface
401  i = context->ipAddrIndex;
402 
403  //Debug message
404  TRACE_INFO("\r\n");
405  TRACE_INFO("Auto-IP configuration:\r\n");
406 
407  //Link-local address
408  TRACE_INFO(" Link-local Address = %s\r\n",
409  ipv4AddrToString(ipv4Context->addrList[i].addr, NULL));
410 
411  //Subnet mask
412  TRACE_INFO(" Subnet Mask = %s\r\n",
413  ipv4AddrToString(ipv4Context->addrList[i].subnetMask, NULL));
414 
415  //Debug message
416  TRACE_INFO("\r\n");
417 #endif
418 }
419 
420 #endif
@ AUTO_IP_STATE_PROBING
Definition: auto_ip.h:154
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Definition: net.c:319
Ipv4Addr addr
IPv4 address.
Definition: ipv4.h:411
@ AUTO_IP_STATE_DEFENDING
Definition: auto_ip.h:157
void autoIpDumpConfig(AutoIpContext *context)
Dump Auto-IP configuration for debugging purpose.
Definition: auto_ip_misc.c:392
#define AUTO_IP_MAX_CONFLICTS
Definition: auto_ip.h:109
uint32_t netGenerateRand(NetContext *context)
Generate a random 32-bit value.
Definition: net_misc.c:956
#define AutoIpContext
Definition: auto_ip.h:139
#define AUTO_IP_DEFEND_INTERVAL
Definition: auto_ip.h:123
#define AUTO_IP_PROBE_NUM
Definition: auto_ip.h:67
void autoIpLinkChangeEvent(AutoIpContext *context)
Callback function for link change event.
Definition: auto_ip_misc.c:268
#define timeCompare(t1, t2)
Definition: os_port.h:40
#define AUTO_IP_PROBE_MIN
Definition: auto_ip.h:74
IPv4 context.
Definition: ipv4.h:451
Ethernet.
#define AUTO_IP_ADDR_MIN
Definition: auto_ip.h:134
#define FALSE
Definition: os_port.h:46
@ AUTO_IP_STATE_CONFIGURED
Definition: auto_ip.h:156
#define htonl(value)
Definition: cpu_endian.h:414
@ IPV4_ADDR_STATE_TENTATIVE
An address whose uniqueness on a link is being verified.
Definition: ipv4.h:227
#define AUTO_IP_ANNOUNCE_NUM
Definition: auto_ip.h:95
#define AUTO_IP_ANNOUNCE_INTERVAL
Definition: auto_ip.h:102
void autoIpChangeState(AutoIpContext *context, AutoIpState newState, systime_t delay)
Update Auto-IP FSM state.
Definition: auto_ip_misc.c:316
#define AUTO_IP_MASK
Definition: auto_ip.h:131
#define NetInterface
Definition: net.h:40
uint32_t netGenerateRandRange(NetContext *context, uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:983
void autoIpResetConfig(AutoIpContext *context)
Reset Auto-IP configuration.
Definition: auto_ip_misc.c:364
#define TRACE_INFO(...)
Definition: debug.h:105
error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr, const MacAddr *destMacAddr)
Send ARP request.
Definition: arp.c:979
uint32_t systime_t
System time.
#define AUTO_IP_ADDR_MAX
Definition: auto_ip.h:135
Ipv4Addr subnetMask
Subnet mask.
Definition: ipv4.h:414
uint32_t time
uint8_t n
#define AUTO_IP_ANNOUNCE_WAIT
Definition: auto_ip.h:88
Ipv4AddrEntry addrList[IPV4_ADDR_LIST_SIZE]
IPv4 address list.
Definition: ipv4.h:458
void autoIpGenerateAddr(AutoIpContext *context)
Generate a random link-local address.
Definition: auto_ip_misc.c:346
@ IPV4_ADDR_STATE_INVALID
An address that is not assigned to any interface.
Definition: ipv4.h:226
@ AUTO_IP_STATE_INIT
Definition: auto_ip.h:153
void autoIpTick(AutoIpContext *context)
Auto-IP timer handler.
Definition: auto_ip_misc.c:56
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:307
Helper functions for Auto-IP.
#define AUTO_IP_RATE_LIMIT_INTERVAL
Definition: auto_ip.h:116
#define AUTO_IP_PROBE_WAIT
Definition: auto_ip.h:60
@ IPV4_ADDR_STATE_VALID
An address assigned to an interface whose use is unrestricted.
Definition: ipv4.h:228
#define NTOHL(value)
Definition: cpu_endian.h:419
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
char_t * ipv4AddrToString(Ipv4Addr ipAddr, char_t *str)
Convert a binary IPv4 address to dot-decimal notation.
Definition: ipv4.c:1468
AutoIpState
Auto-IP FSM states.
Definition: auto_ip.h:152
ARP (Address Resolution Protocol)
#define ntohl(value)
Definition: cpu_endian.h:422
error_t mdnsResponderStartProbing(MdnsResponderContext *context)
Restart probing process.
@ AUTO_IP_STATE_ANNOUNCING
Definition: auto_ip.h:155
#define AUTO_IP_PROBE_MAX
Definition: auto_ip.h:81
Debugging facilities.
error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr)
Send ARP probe.
Definition: arp.c:919
const MacAddr MAC_BROADCAST_ADDR
Definition: ethernet.c:53
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:128
mDNS responder (Multicast DNS)
Auto-IP (Dynamic Configuration of IPv4 Link-Local Addresses)
systime_t osGetSystemTime(void)
Retrieve system time.