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-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  * 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  *
37  * @author Oryx Embedded SARL (www.oryx-embedded.com)
38  * @version 2.4.4
39  **/
40 
41 //Switch to the appropriate trace level
42 #define TRACE_LEVEL MLD_TRACE_LEVEL
43 
44 //Dependencies
45 #include "core/net.h"
46 #include "ipv6/ipv6_multicast.h"
47 #include "ipv6/icmpv6.h"
48 #include "mld/mld_node.h"
49 #include "mld/mld_node_misc.h"
50 #include "mld/mld_common.h"
51 #include "mld/mld_debug.h"
52 #include "mibs/ip_mib_module.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 //Tick counter to handle periodic operations
64 
65 
66 /**
67  * @brief MLD initialization
68  * @param[in] interface Underlying network interface
69  * @return Error code
70  **/
71 
73 {
74  error_t error;
75 
76  //Join the All-Nodes multicast address
78  //Any error to report?
79  if(error)
80  return error;
81 
82 #if (MLD_NODE_SUPPORT == ENABLED)
83  //MLD node initialization
84  error = mldNodeInit(interface);
85  //Any error to report?
86  if(error)
87  return error;
88 #endif
89 
90  //Successful initialization
91  return NO_ERROR;
92 }
93 
94 
95 /**
96  * @brief MLD node timer handler
97  *
98  * This routine must be periodically called by the TCP/IP stack to
99  * handle MLD related timers
100  *
101  * @param[in] interface Underlying network interface
102  **/
103 
104 void mldTick(NetInterface *interface)
105 {
106 #if (MLD_NODE_SUPPORT == ENABLED)
107  //Manage MLD node timers
108  mldNodeTick(&interface->mldNodeContext);
109 #endif
110 }
111 
112 
113 /**
114  * @brief Callback function for link change event
115  * @param[in] interface Underlying network interface
116  **/
117 
119 {
120 #if (MLD_NODE_SUPPORT == ENABLED)
121  //Notify the MLD node of link state changes
122  mldNodeLinkChangeEvent(&interface->mldNodeContext);
123 #endif
124 }
125 
126 
127 /**
128  * @brief Send MLD message
129  * @param[in] interface Underlying network interface
130  * @param[in] destAddr Destination IP address
131  * @param[in] buffer Multi-part buffer containing the payload
132  * @param[in] offset Offset to the first byte of the payload
133  * @return Error code
134  **/
135 
137  NetBuffer *buffer, size_t offset)
138 {
139  size_t length;
141  Ipv6PseudoHeader pseudoHeader;
142  NetTxAncillary ancillary;
143 
144  //Retrieve the length of payload
145  length = netBufferGetLength(buffer) - offset;
146 
147  //Point to the beginning of the MLD message
148  message = netBufferAt(buffer, offset, length);
149  //Sanity check
150  if(message == NULL)
151  return ERROR_FAILURE;
152 
153  //Check if a valid link-local address is available on the interface
155  {
156  //The message is sent with a link-local address as the IPv6 source address
157  pseudoHeader.srcAddr = interface->ipv6Context.addrList[0].addr;
158  }
159  else
160  {
161  //Check MLD message type
163  {
164  //MLD Query messages must be sent with a valid link-local address as
165  //the IPv6 source address (refer to RFC 3590, section 4)
166  return ERROR_NO_ADDRESS;
167  }
168  else
169  {
170  //MLD Report and Done messages are sent with a link-local address as
171  //the IPv6 source address, if a valid address is available on the
172  //interface. If a valid link-local address is not available, the
173  //message is sent with the unspecified address as the IPv6 source
174  //address
175  pseudoHeader.srcAddr = IPV6_UNSPECIFIED_ADDR;
176  }
177  }
178 
179  //Format IPv6 pseudo header
180  pseudoHeader.destAddr = *destAddr;
181  pseudoHeader.length = htons(length);
182  pseudoHeader.reserved[0] = 0;
183  pseudoHeader.reserved[1] = 0;
184  pseudoHeader.reserved[2] = 0;
185  pseudoHeader.nextHeader = IPV6_ICMPV6_HEADER;
186 
187  //Message checksum calculation
188  message->checksum = ipCalcUpperLayerChecksumEx(&pseudoHeader,
189  sizeof(Ipv6PseudoHeader), buffer, offset, length);
190 
191  //Total number of ICMP messages which this entity attempted to send
192  IP_MIB_INC_COUNTER32(icmpv6Stats.icmpStatsOutMsgs, 1);
193  //Increment per-message type ICMP counter
194  IP_MIB_INC_COUNTER32(icmpv6MsgStatsTable.icmpMsgStatsOutPkts[message->type], 1);
195 
196  //Debug message
197  TRACE_INFO("Sending MLD message (%" PRIuSIZE " bytes)...\r\n", length);
198  //Dump message contents for debugging purpose
200 
201  //Additional options can be passed to the stack along with the packet
202  ancillary = NET_DEFAULT_TX_ANCILLARY;
203 
204  //All MLD messages must be sent with an IPv6 Hop Limit of 1, and an IPv6
205  //Router Alert option in a Hop-by-Hop Options header (refer to RFC 2710,
206  //section 3)
207  ancillary.ttl = MLD_HOP_LIMIT;
208  ancillary.routerAlert = TRUE;
209 
210  //Send the MLD message
211  return ipv6SendDatagram(interface, &pseudoHeader, buffer, offset,
212  &ancillary);
213 }
214 
215 
216 /**
217  * @brief Process incoming MLD message
218  * @param[in] interface Underlying network interface
219  * @param[in] pseudoHeader IPv6 pseudo header
220  * @param[in] buffer Multi-part buffer containing the incoming MLD message
221  * @param[in] offset Offset to the first byte of the MLD message
222  * @param[in] ancillary Additional options passed to the stack along with
223  * the packet
224  **/
225 
227  const Ipv6PseudoHeader *pseudoHeader, const NetBuffer *buffer,
228  size_t offset, const NetRxAncillary *ancillary)
229 {
230  size_t length;
231  const MldMessage *message;
232 
233  //Retrieve the length of the MLD message
234  length = netBufferGetLength(buffer) - offset;
235 
236  //To be valid, an MLD message must be at least 24 octets long
237  if(length < sizeof(MldMessage))
238  return;
239 
240  //Point to the beginning of the MLD message
241  message = netBufferAt(buffer, offset, length);
242  //Sanity check
243  if(message == NULL)
244  return;
245 
246  //Debug message
247  TRACE_INFO("MLD message received (%" PRIuSIZE " bytes)...\r\n", length);
248  //Dump message contents for debugging purpose
250 
251  //All MLD messages are sent with an IPv6 Hop Limit of 1
252  if(ancillary->ttl != MLD_HOP_LIMIT)
253  return;
254 
255 #if (MLD_NODE_SUPPORT == ENABLED)
256  //Pass the message to the MLD node
257  mldNodeProcessMessage(&interface->mldNodeContext, pseudoHeader, message,
258  length);
259 #endif
260 }
261 
262 
263 /**
264  * @brief Generate a random delay
265  * @param[in] maxDelay maximum delay
266  * @return Random amount of time
267  **/
268 
270 {
271  systime_t delay;
272 
273  //Generate a random delay in the specified range
274  if(maxDelay > MLD_TICK_INTERVAL)
275  {
276  delay = netGenerateRandRange(0, maxDelay - MLD_TICK_INTERVAL);
277  }
278  else
279  {
280  delay = 0;
281  }
282 
283  //Return the random value
284  return delay;
285 }
286 
287 
288 /**
289  * @brief Decode a floating-point value (8-bit code)
290  * @param[in] code Floating-point representation
291  * @return Decoded value
292  **/
293 
295 {
296  uint8_t exp;
297  uint8_t mant;
298 
299  //Retrieve the value of the exponent
300  exp = (code >> 4) & 0x07;
301  //Retrieve the value of the mantissa
302  mant = code & 0x0F;
303 
304  //The code represents a floating-point value
305  return (mant | 0x10) << (exp + 3);
306 }
307 
308 
309 /**
310  * @brief Decode a floating-point value (16-bit code)
311  * @param[in] code Floating-point representation
312  * @return Decoded value
313  **/
314 
316 {
317  uint16_t exp;
318  uint16_t mant;
319 
320  //Retrieve the value of the exponent
321  exp = (code >> 12) & 0x07;
322  //Retrieve the value of the mantissa
323  mant = code & 0x0FFF;
324 
325  //The code represents a floating-point value
326  return (mant | 0x1000) << (exp + 3);
327 }
328 
329 #endif
#define htons(value)
Definition: cpu_endian.h:413
@ ERROR_NO_ADDRESS
Definition: error.h:199
uint8_t code
Definition: coap_common.h:179
Ipv4Addr destAddr
Definition: ipv4.h:329
const NetTxAncillary NET_DEFAULT_TX_ANCILLARY
Definition: net_misc.c:71
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:118
uint32_t mldDecodeFloatingPointValue8(uint8_t code)
Decode a floating-point value (8-bit code)
Definition: mld_common.c:294
#define IP_MIB_INC_COUNTER32(name, value)
Definition: ip_mib_module.h:46
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:136
Ipv6Addr
Definition: ipv6.h:260
systime_t mldTickCounter
Definition: mld_common.c:63
@ IPV6_ICMPV6_HEADER
Definition: ipv6.h:193
error_t mldNodeInit(NetInterface *interface)
MLD node initialization.
Definition: mld_node.c:53
void mldTick(NetInterface *interface)
MLD node timer handler.
Definition: mld_common.c:104
@ ICMPV6_TYPE_MCAST_LISTENER_QUERY
Definition: icmpv6.h:59
void mldNodeTick(MldNodeContext *context)
MLD node timer handler.
Definition: mld_node.c:92
void mldLinkChangeEvent(NetInterface *interface)
Callback function for link change event.
Definition: mld_common.c:118
systime_t mldGetRandomDelay(systime_t maxDelay)
Generate a random delay.
Definition: mld_common.c:269
IPv6 multicast filtering.
ICMPv6 (Internet Control Message Protocol Version 6)
uint32_t netGenerateRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:948
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:74
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
#define NetTxAncillary
Definition: net_misc.h:36
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:66
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t length
Definition: tcp.h:368
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
uint32_t systime_t
System time.
void mldNodeLinkChangeEvent(MldNodeContext *context)
Callback function for link change event.
Definition: mld_node.c:451
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:686
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:1703
void mldProcessMessage(NetInterface *interface, const Ipv6PseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Process incoming MLD message.
Definition: mld_common.c:226
IP MIB module.
Ipv6AddrState ipv6GetLinkLocalAddrState(NetInterface *interface)
Get the state of the link-local address.
Definition: ipv6.c:327
uint32_t mldDecodeFloatingPointValue16(uint16_t code)
Decode a floating-point value (16-bit code)
Definition: mld_common.c:315
#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:175
error_t mldInit(NetInterface *interface)
MLD initialization.
Definition: mld_common.c:72
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.