forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmld_leave.c
219 lines (180 loc) · 7.41 KB
/
mld_leave.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/****************************************************************************
* net/mld/mld_leave.c
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of CITEL Technologies Ltd nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <debug.h>
#include <netinet/in.h>
#include <nuttx/wdog.h>
#include <nuttx/net/netconfig.h>
#include <nuttx/net/net.h>
#include <nuttx/net/netstats.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/mld.h>
#include "netdev/netdev.h"
#include "mld/mld.h"
#ifdef CONFIG_NET_MLD
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mld_leavegroup
*
* Description:
* Remove the specified group address to the group. This function
* implements the logic for the IPV6_LEAVE_GROUP socket option.
*
* The IPV6_JOIN_GROUP socket option is used to join a multicast group.
* This is accomplished by using the setsockopt() API and specifying the
* address of the ipv6_mreq structure containing the IPv6 multicast address
* and the local IPv6 multicast interface index. The stack chooses a
* default multicast interface if an interface index of 0 is passed. The
* values specified in the IPV6_MREQ structure used by IPV6_JOIN_GROUP
* and IPV6_LEAVE_GROUP must be symmetrical. The format of the ipv6_mreq
* structure can be found in include/netinet/in.h
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
int mld_leavegroup(FAR const struct ipv6_mreq *mrec)
{
FAR struct net_driver_s *dev;
struct mld_group_s *group;
int ret;
DEBUGASSERT(mrec != NULL);
/* Get the device from the interface index. Use the default network device
* if an interface index of 0 is provided.
*/
if (mrec->ipv6mr_interface == 0)
{
dev = netdev_default();
}
else
{
dev = netdev_findbyindex(mrec->ipv6mr_interface);
}
if (dev == NULL)
{
return -ENODEV;
}
mldinfo("Leave group: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
mrec->ipv6mr_multiaddr.s6_addr16[0],
mrec->ipv6mr_multiaddr.s6_addr16[1],
mrec->ipv6mr_multiaddr.s6_addr16[2],
mrec->ipv6mr_multiaddr.s6_addr16[3],
mrec->ipv6mr_multiaddr.s6_addr16[4],
mrec->ipv6mr_multiaddr.s6_addr16[5],
mrec->ipv6mr_multiaddr.s6_addr16[5],
mrec->ipv6mr_multiaddr.s6_addr16[7]);
/* Find the entry corresponding to the address leaving the group */
group = mld_grpfind(dev, mrec->ipv6mr_multiaddr.s6_addr16);
if (group != NULL)
{
mldinfo("Leaving group: %p\n", group);
/* Indicate one fewer members of the group from this host */
DEBUGASSERT(group->njoins > 0);
group->njoins--;
MLD_STATINCR(g_netstats.mld.nleaves);
/* Take no further actions if there are other members of this group
* on this host.
*/
if (group->njoins > 0)
{
return OK;
}
/* Perform actions that would be performed only when the number of
* njoins from this host goes to zero.
*/
if (group->njoins == 0)
{
/* Send a leave if the LASTREPORT flag is set for the group. If
* there are other members of the group, then their reports will
* clear the LAST REPORT flag. In this case we know that there are
* other members of the group and we do not have to send the Done
* message.
*
* The MLDv1 router responds to the Done message with a multicast-
* address- specific (MAS) Query. If any other node responds to
* the Query with a Report message the there are still listeners
* present.
*/
if (IS_MLD_LASTREPORT(group->flags))
{
mldinfo("Schedule Done message\n");
MLD_STATINCR(g_netstats.mld.done_sched);
/* REVIST: This will interfere is the are any other tasks
* waiting for a message to be sent. Can that happen?
*/
ret = mld_waitmsg(group, MLD_SEND_DONE);
if (ret < 0)
{
mlderr("ERROR: Failed to schedule message: %d\n", ret);
}
}
/* Remove the group address from the Ethernet drivers MAC filter
* set
*/
mld_removemcastmac(dev, mrec->ipv6mr_multiaddr.s6_addr16);
/* Perform additional the number of members not on this host is
* also zero.
*/
#ifdef CONFIG_NET_MLD_ROUTER
if (group->members == 0 && group->lstmbrs == 0)
#endif
{
/* Cancel the timers and discard any queued Reports. Canceling
* the timer will prevent any new Reports from being sent;
* clearing the flags will discard any pending Reports that
* could interfere with freeing the group.
*/
wd_cancel(&group->polldog);
CLR_MLD_SCHEDMSG(group->flags);
CLR_MLD_WAITMSG(group->flags);
/* Free the group structure */
mld_grpfree(dev, group);
/* REVISIT: It is expected that higher level logic will remove
* the routing table entry for the old multicast address. That
* is not done here.
*/
}
}
return OK;
}
/* Return ENOENT if the address is not a member of the group */
mldinfo("Return -ENOENT\n");
return -ENOENT;
}
#endif /* CONFIG_NET_MLD */