mld_common.c
Go to the documentation of this file.
1 /**
2  * @file mld_common.c
3  * @brief Definitions common to MLD node, router and snooping switch
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  * MLD is used by an IPv6 router to discover the presence of multicast
30  * listeners on its directly attached links, and to discover specifically
31  * which multicast addresses are of interest to those neighboring nodes.
32  * Refer to the following RFCs for complete details:
33  * - RFC 2710: Multicast Listener Discovery (MLD) for IPv6
34  * - RFC 3590: Source Address Selection for MLD Protocol
35  * - RFC 3810: Multicast Listener Discovery Version 2 (MLDv2) for IPv6
36  * - RFC 9777: Multicast Listener Discovery Version 2 (MLDv2) for IPv6
37  *
38  * @author Oryx Embedded SARL (www.oryx-embedded.com)
39  * @version 2.6.0
40  **/
41 
42 //Switch to the appropriate trace level
43 #define TRACE_LEVEL MLD_TRACE_LEVEL
44 
45 //Dependencies
46 #include "core/net.h"
47 #include "ipv6/ipv6_multicast.h"
48 #include "ipv6/icmpv6.h"
49 #include "mld/mld_node.h"
50 #include "mld/mld_node_misc.h"
51 #include "mld/mld_common.h"
52 #include "mld/mld_debug.h"
53 #include "debug.h"
54 
55 //Check TCP/IP stack configuration
56 #if (IPV6_SUPPORT == ENABLED && MLD_NODE_SUPPORT == ENABLED)
57 
58 //Link-local All-Routers IPv6 address
60  IPV6_ADDR(0xFF02, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0016);
61 
62 
63 /**
64  * @brief MLD initialization
65  * @param[in] interface Underlying network interface
66  * @return Error code
67  **/
68 
70 {
71  error_t error;
72 
73  //Join the All-Nodes multicast address
75  //Any error to report?
76  if(error)
77  return error;
78 
79 #if (MLD_NODE_SUPPORT == ENABLED)
80  //MLD node initialization
81  error = mldNodeInit(interface);
82  //Any error to report?
83  if(error)
84  return error;
85 #endif
86 
87  //Successful initialization
88  return NO_ERROR;
89 }
90 
91 
92 /**
93  * @brief MLD node timer handler
94  *
95  * This routine must be periodically called by the TCP/IP stack to
96  * handle MLD related timers
97  *
98  * @param[in] interface Underlying network interface
99  **/
100 
101 void mldTick(NetInterface *interface)
102 {
103 #if (MLD_NODE_SUPPORT == ENABLED)
104  //Manage MLD node timers
105  mldNodeTick(&interface->mldNodeContext);
106 #endif
107 }
108 
109 
110 /**
111  * @brief Callback function for link change event
112  * @param[in] interface Underlying network interface
113  **/
114 
116 {
117 #if (MLD_NODE_SUPPORT == ENABLED)
118  //Notify the MLD node of link state changes
119  mldNodeLinkChangeEvent(&interface->mldNodeContext);
120 #endif
121 }
122 
123 
124 /**
125  * @brief Send MLD message
126  * @param[in] interface Underlying network interface
127  * @param[in] destAddr Destination IP address
128  * @param[in] buffer Multi-part buffer containing the payload
129  * @param[in] offset Offset to the first byte of the payload
130  * @return Error code
131  **/
132 
134  NetBuffer *buffer, size_t offset)
135 {
136  size_t length;
138  Ipv6PseudoHeader pseudoHeader;
139  NetTxAncillary ancillary;
140 
141  //Retrieve the length of payload
142  length = netBufferGetLength(buffer) - offset;
143 
144  //Point to the beginning of the MLD message
145  message = netBufferAt(buffer, offset, length);
146  //Sanity check
147  if(message == NULL)
148  return ERROR_FAILURE;
149 
150  //Check if a valid link-local address is available on the interface
152  {
153  //The message is sent with a link-local address as the IPv6 source address
154  pseudoHeader.srcAddr = interface->ipv6Context.addrList[0].addr;
155  }
156  else
157  {
158  //Check MLD message type
160  {
161  //MLD Query messages must be sent with a valid link-local address as
162  //the IPv6 source address (refer to RFC 3590, section 4)
163  return ERROR_NO_ADDRESS;
164  }
165  else
166  {
167  //MLD Report and Done messages are sent with a link-local address as
168  //the IPv6 source address, if a valid address is available on the
169  //interface. If a valid link-local address is not available, the
170  //message is sent with the unspecified address as the IPv6 source
171  //address
172  pseudoHeader.srcAddr = IPV6_UNSPECIFIED_ADDR;
173  }
174  }
175 
176  //Format IPv6 pseudo header
177  pseudoHeader.destAddr = *destAddr;
178  pseudoHeader.length = htons(length);
179  pseudoHeader.reserved[0] = 0;
180  pseudoHeader.reserved[1] = 0;
181  pseudoHeader.reserved[2] = 0;
182  pseudoHeader.nextHeader = IPV6_ICMPV6_HEADER;
183 
184  //Message checksum calculation
185  message->checksum = ipCalcUpperLayerChecksumEx(&pseudoHeader,
186  sizeof(Ipv6PseudoHeader), buffer, offset, length);
187 
188  //Total number of ICMP messages which this entity attempted to send
189  ICMPV6_STATS_INC_COUNTER32(outMsgs, 1);
190  //Increment per-message type ICMP counter
191  ICMPV6_STATS_INC_COUNTER32(outPkts[message->type], 1);
192 
193  //Debug message
194  TRACE_INFO("Sending MLD message (%" PRIuSIZE " bytes)...\r\n", length);
195  //Dump message contents for debugging purpose
197 
198  //Additional options can be passed to the stack along with the packet
199  ancillary = NET_DEFAULT_TX_ANCILLARY;
200 
201  //All MLD messages must be sent with an IPv6 Hop Limit of 1, and an IPv6
202  //Router Alert option in a Hop-by-Hop Options header (refer to RFC 2710,
203  //section 3)
204  ancillary.ttl = MLD_HOP_LIMIT;
205  ancillary.routerAlert = TRUE;
206 
207  //Send the MLD message
208  return ipv6SendDatagram(interface, &pseudoHeader, buffer, offset,
209  &ancillary);
210 }
211 
212 
213 /**
214  * @brief Process incoming MLD message
215  * @param[in] interface Underlying network interface
216  * @param[in] pseudoHeader IPv6 pseudo header
217  * @param[in] buffer Multi-part buffer containing the incoming MLD message
218  * @param[in] offset Offset to the first byte of the MLD message
219  * @param[in] ancillary Additional options passed to the stack along with
220  * the packet
221  **/
222 
224  const Ipv6PseudoHeader *pseudoHeader, const NetBuffer *buffer,
225  size_t offset, const NetRxAncillary *ancillary)
226 {
227  size_t length;
228  const MldMessage *message;
229 
230  //Retrieve the length of the MLD message
231  length = netBufferGetLength(buffer) - offset;
232 
233  //To be valid, an MLD message must be at least 24 octets long
234  if(length < sizeof(MldMessage))
235  return;
236 
237  //Point to the beginning of the MLD message
238  message = netBufferAt(buffer, offset, length);
239  //Sanity check
240  if(message == NULL)
241  return;
242 
243  //Debug message
244  TRACE_INFO("MLD message received (%" PRIuSIZE " bytes)...\r\n", length);
245  //Dump message contents for debugging purpose
247 
248  //All MLD messages are sent with an IPv6 Hop Limit of 1
249  if(ancillary->ttl != MLD_HOP_LIMIT)
250  return;
251 
252 #if (MLD_NODE_SUPPORT == ENABLED)
253  //Pass the message to the MLD node
254  mldNodeProcessMessage(&interface->mldNodeContext, pseudoHeader, message,
255  length);
256 #endif
257 }
258 
259 
260 /**
261  * @brief Generate a random delay
262  * @param[in] context Pointer to the TCP/IP stack context
263  * @param[in] maxDelay maximum delay
264  * @return Random amount of time
265  **/
266 
268 {
269  systime_t delay;
270 
271  //Generate a random delay in the specified range
272  if(maxDelay > MLD_TICK_INTERVAL)
273  {
274  delay = netGenerateRandRange(context, 0, maxDelay - MLD_TICK_INTERVAL);
275  }
276  else
277  {
278  delay = 0;
279  }
280 
281  //Return the random value
282  return delay;
283 }
284 
285 
286 /**
287  * @brief Decode a floating-point value (8-bit code)
288  * @param[in] code Floating-point representation
289  * @return Decoded value
290  **/
291 
293 {
294  uint8_t exp;
295  uint8_t mant;
296 
297  //Retrieve the value of the exponent
298  exp = (code >> 4) & 0x07;
299  //Retrieve the value of the mantissa
300  mant = code & 0x0F;
301 
302  //The code represents a floating-point value
303  return (mant | 0x10) << (exp + 3);
304 }
305 
306 
307 /**
308  * @brief Decode a floating-point value (16-bit code)
309  * @param[in] code Floating-point representation
310  * @return Decoded value
311  **/
312 
314 {
315  uint16_t exp;
316  uint16_t mant;
317 
318  //Retrieve the value of the exponent
319  exp = (code >> 12) & 0x07;
320  //Retrieve the value of the mantissa
321  mant = code & 0x0FFF;
322 
323  //The code represents a floating-point value
324  return (mant | 0x1000) << (exp + 3);
325 }
326 
327 #endif
systime_t mldGetRandomDelay(NetContext *context, systime_t maxDelay)
Generate a random delay.
Definition: mld_common.c:267
#define htons(value)
Definition: cpu_endian.h:413
#define NetContext
Definition: net.h:36
@ ERROR_NO_ADDRESS
Definition: error.h:200
uint8_t code
Definition: coap_common.h:179
Ipv4Addr destAddr
Definition: ipv4.h:354
const NetTxAncillary NET_DEFAULT_TX_ANCILLARY
Definition: net_misc.c:70
const Ipv6Addr MLD_V2_ALL_ROUTERS_ADDR
Definition: mld_common.c:59
#define IPV6_ADDR(a, b, c, d, e, f, g, h)
Definition: ipv6.h:125
uint32_t mldDecodeFloatingPointValue8(uint8_t code)
Decode a floating-point value (8-bit code)
Definition: mld_common.c:292
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t message[]
Definition: chap.h:154
#define TRUE
Definition: os_port.h:50
MldMessage
Definition: mld_common.h:134
error_t mldSendMessage(NetInterface *interface, const Ipv6Addr *destAddr, NetBuffer *buffer, size_t offset)
Send MLD message.
Definition: mld_common.c:133
Ipv6Addr
Definition: ipv6.h:280
@ IPV6_ICMPV6_HEADER
Definition: ipv6.h:213
error_t mldNodeInit(NetInterface *interface)
MLD node initialization.
Definition: mld_node.c:64
void mldTick(NetInterface *interface)
MLD node timer handler.
Definition: mld_common.c:101
@ ICMPV6_TYPE_MCAST_LISTENER_QUERY
Definition: icmpv6.h:77
void mldNodeTick(MldNodeContext *context)
MLD node timer handler.
Definition: mld_node.c:105
void mldLinkChangeEvent(NetInterface *interface)
Callback function for link change event.
Definition: mld_common.c:115
IPv6 multicast filtering.
ICMPv6 (Internet Control Message Protocol Version 6)
Data logging functions for debugging purpose (MLD)
error_t
Error codes.
Definition: error.h:43
error_t ipv6JoinMulticastGroup(NetInterface *interface, const Ipv6Addr *groupAddr)
Join an IPv6 multicast group.
#define Ipv6PseudoHeader
Definition: ipv6.h:42
Definitions common to MLD node, router and snooping switch.
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
const Ipv6Addr IPV6_LINK_LOCAL_ALL_NODES_ADDR
Definition: ipv6.c:73
#define NetRxAncillary
Definition: net_misc.h:40
#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
#define NetTxAncillary
Definition: net_misc.h:36
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:65
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
#define MLD_HOP_LIMIT
Definition: mld_common.h:80
#define ICMPV6_STATS_INC_COUNTER32(name, value)
Definition: icmpv6.h:50
uint32_t systime_t
System time.
void mldNodeLinkChangeEvent(MldNodeContext *context)
Callback function for link change event.
Definition: mld_node.c:468
MLD node (Multicast Listener Discovery for IPv6)
uint16_t ipCalcUpperLayerChecksumEx(const void *pseudoHeader, size_t pseudoHeaderLen, const NetBuffer *buffer, size_t offset, size_t length)
Calculate IP upper-layer checksum over a multi-part buffer.
Definition: ip.c:691
void mldNodeProcessMessage(MldNodeContext *context, const Ipv6PseudoHeader *pseudoHeader, const MldMessage *message, size_t length)
Process incoming MLD message.
error_t ipv6SendDatagram(NetInterface *interface, const Ipv6PseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send an IPv6 datagram.
Definition: ipv6.c:1716
void mldProcessMessage(NetInterface *interface, const Ipv6PseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Process incoming MLD message.
Definition: mld_common.c:223
Ipv6AddrState ipv6GetLinkLocalAddrState(NetInterface *interface)
Get the state of the link-local address.
Definition: ipv6.c:330
uint32_t mldDecodeFloatingPointValue16(uint16_t code)
Decode a floating-point value (16-bit code)
Definition: mld_common.c:313
#define MLD_TICK_INTERVAL
Definition: mld_common.h:39
void * netBufferAt(const NetBuffer *buffer, size_t offset, size_t length)
Returns a pointer to a data segment.
Definition: net_mem.c:418
@ IPV6_ADDR_STATE_PREFERRED
An address assigned to an interface whose use is unrestricted.
Definition: ipv6.h:195
error_t mldInit(NetInterface *interface)
MLD initialization.
Definition: mld_common.c:69
Helper functions for MLD node.
#define PRIuSIZE
TCP/IP stack core.
void mldDumpMessage(const MldMessage *message, size_t length)
Dump MLD message for debugging purpose.
Definition: mld_debug.c:70
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.