ReactOS 0.4.16-dev-833-g4bc97ad
mld6.c
Go to the documentation of this file.
1
17/*
18 * Copyright (c) 2010 Inico Technologies Ltd.
19 * All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without modification,
22 * are permitted provided that the following conditions are met:
23 *
24 * 1. Redistributions of source code must retain the above copyright notice,
25 * this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright notice,
27 * this list of conditions and the following disclaimer in the documentation
28 * and/or other materials provided with the distribution.
29 * 3. The name of the author may not be used to endorse or promote products
30 * derived from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
35 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
41 * OF SUCH DAMAGE.
42 *
43 * This file is part of the lwIP TCP/IP stack.
44 *
45 * Author: Ivan Delamer <delamer@inicotech.com>
46 *
47 *
48 * Please coordinate changes and requests with Ivan Delamer
49 * <delamer@inicotech.com>
50 */
51
52/* Based on igmp.c implementation of igmp v2 protocol */
53
54#include "lwip/opt.h"
55
56#if LWIP_IPV6 && LWIP_IPV6_MLD /* don't build if not configured for use in lwipopts.h */
57
58#include "lwip/mld6.h"
59#include "lwip/prot/mld6.h"
60#include "lwip/icmp6.h"
61#include "lwip/ip6.h"
62#include "lwip/ip6_addr.h"
63#include "lwip/ip.h"
64#include "lwip/inet_chksum.h"
65#include "lwip/pbuf.h"
66#include "lwip/netif.h"
67#include "lwip/memp.h"
68#include "lwip/stats.h"
69
70#include <string.h>
71
72
73/*
74 * MLD constants
75 */
76#define MLD6_HL 1
77#define MLD6_JOIN_DELAYING_MEMBER_TMR_MS (500)
78
79#define MLD6_GROUP_NON_MEMBER 0
80#define MLD6_GROUP_DELAYING_MEMBER 1
81#define MLD6_GROUP_IDLE_MEMBER 2
82
83/* Forward declarations. */
84static struct mld_group *mld6_new_group(struct netif *ifp, const ip6_addr_t *addr);
85static err_t mld6_remove_group(struct netif *netif, struct mld_group *group);
86static void mld6_delayed_report(struct mld_group *group, u16_t maxresp);
87static void mld6_send(struct netif *netif, struct mld_group *group, u8_t type);
88
89
96mld6_stop(struct netif *netif)
97{
98 struct mld_group *group = netif_mld6_data(netif);
99
100 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_MLD6, NULL);
101
102 while (group != NULL) {
103 struct mld_group *next = group->next; /* avoid use-after-free below */
104
105 /* disable the group at the MAC level */
106 if (netif->mld_mac_filter != NULL) {
107 netif->mld_mac_filter(netif, &(group->group_address), NETIF_DEL_MAC_FILTER);
108 }
109
110 /* free group */
111 memp_free(MEMP_MLD6_GROUP, group);
112
113 /* move to "next" */
114 group = next;
115 }
116 return ERR_OK;
117}
118
124void
125mld6_report_groups(struct netif *netif)
126{
127 struct mld_group *group = netif_mld6_data(netif);
128
129 while (group != NULL) {
130 mld6_delayed_report(group, MLD6_JOIN_DELAYING_MEMBER_TMR_MS);
131 group = group->next;
132 }
133}
134
143struct mld_group *
144mld6_lookfor_group(struct netif *ifp, const ip6_addr_t *addr)
145{
146 struct mld_group *group = netif_mld6_data(ifp);
147
148 while (group != NULL) {
149 if (ip6_addr_eq(&(group->group_address), addr)) {
150 return group;
151 }
152 group = group->next;
153 }
154
155 return NULL;
156}
157
158
167static struct mld_group *
168mld6_new_group(struct netif *ifp, const ip6_addr_t *addr)
169{
170 struct mld_group *group;
171
172 group = (struct mld_group *)memp_malloc(MEMP_MLD6_GROUP);
173 if (group != NULL) {
174 ip6_addr_set(&(group->group_address), addr);
175 group->timer = 0; /* Not running */
176 group->group_state = MLD6_GROUP_IDLE_MEMBER;
177 group->last_reporter_flag = 0;
178 group->use = 0;
179 group->next = netif_mld6_data(ifp);
180
181 netif_set_client_data(ifp, LWIP_NETIF_CLIENT_DATA_INDEX_MLD6, group);
182 }
183
184 return group;
185}
186
193static err_t
194mld6_remove_group(struct netif *netif, struct mld_group *group)
195{
196 err_t err = ERR_OK;
197
198 /* Is it the first group? */
199 if (netif_mld6_data(netif) == group) {
200 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_MLD6, group->next);
201 } else {
202 /* look for group further down the list */
203 struct mld_group *tmpGroup;
204 for (tmpGroup = netif_mld6_data(netif); tmpGroup != NULL; tmpGroup = tmpGroup->next) {
205 if (tmpGroup->next == group) {
206 tmpGroup->next = group->next;
207 break;
208 }
209 }
210 /* Group not find group */
211 if (tmpGroup == NULL) {
212 err = ERR_ARG;
213 }
214 }
215
216 return err;
217}
218
219
226void
227mld6_input(struct pbuf *p, struct netif *inp)
228{
229 struct mld_header *mld_hdr;
230 struct mld_group *group;
231
232 MLD6_STATS_INC(mld6.recv);
233
234 /* Check that mld header fits in packet. */
235 if (p->len < sizeof(struct mld_header)) {
236 /* @todo debug message */
237 pbuf_free(p);
238 MLD6_STATS_INC(mld6.lenerr);
239 MLD6_STATS_INC(mld6.drop);
240 return;
241 }
242
243 mld_hdr = (struct mld_header *)p->payload;
244
245 switch (mld_hdr->type) {
246 case ICMP6_TYPE_MLQ: /* Multicast listener query. */
247 /* Is it a general query? */
248 if (ip6_addr_isallnodes_linklocal(ip6_current_dest_addr()) &&
249 ip6_addr_isany(&(mld_hdr->multicast_address))) {
250 MLD6_STATS_INC(mld6.rx_general);
251 /* Report all groups, except all nodes group, and if-local groups. */
252 group = netif_mld6_data(inp);
253 while (group != NULL) {
254 if ((!(ip6_addr_ismulticast_iflocal(&(group->group_address)))) &&
255 (!(ip6_addr_isallnodes_linklocal(&(group->group_address))))) {
256 mld6_delayed_report(group, lwip_ntohs(mld_hdr->max_resp_delay));
257 }
258 group = group->next;
259 }
260 } else {
261 /* Have we joined this group?
262 * We use IP6 destination address to have a memory aligned copy.
263 * mld_hdr->multicast_address should be the same. */
264 MLD6_STATS_INC(mld6.rx_group);
265 group = mld6_lookfor_group(inp, ip6_current_dest_addr());
266 if (group != NULL) {
267 /* Schedule a report. */
268 mld6_delayed_report(group, lwip_ntohs(mld_hdr->max_resp_delay));
269 }
270 }
271 break; /* ICMP6_TYPE_MLQ */
272 case ICMP6_TYPE_MLR: /* Multicast listener report. */
273 /* Have we joined this group?
274 * We use IP6 destination address to have a memory aligned copy.
275 * mld_hdr->multicast_address should be the same. */
276 MLD6_STATS_INC(mld6.rx_report);
277 group = mld6_lookfor_group(inp, ip6_current_dest_addr());
278 if (group != NULL) {
279 /* If we are waiting to report, cancel it. */
280 if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) {
281 group->timer = 0; /* stopped */
282 group->group_state = MLD6_GROUP_IDLE_MEMBER;
283 group->last_reporter_flag = 0;
284 }
285 }
286 break; /* ICMP6_TYPE_MLR */
287 case ICMP6_TYPE_MLD: /* Multicast listener done. */
288 /* Do nothing, router will query us. */
289 break; /* ICMP6_TYPE_MLD */
290 default:
291 MLD6_STATS_INC(mld6.proterr);
292 MLD6_STATS_INC(mld6.drop);
293 break;
294 }
295
296 pbuf_free(p);
297}
298
314err_t
315mld6_joingroup(const ip6_addr_t *srcaddr, const ip6_addr_t *groupaddr)
316{
317 err_t err = ERR_VAL; /* no matching interface */
318 struct netif *netif;
319
321
322 /* loop through netif's */
324 /* Should we join this interface ? */
325 if (ip6_addr_isany(srcaddr) ||
326 netif_get_ip6_addr_match(netif, srcaddr) >= 0) {
327 err = mld6_joingroup_netif(netif, groupaddr);
328 if (err != ERR_OK) {
329 return err;
330 }
331 }
332 }
333
334 return err;
335}
336
346err_t
347mld6_joingroup_netif(struct netif *netif, const ip6_addr_t *groupaddr)
348{
349 struct mld_group *group;
350#if LWIP_IPV6_SCOPES
351 ip6_addr_t ip6addr;
352
353 /* If the address has a particular scope but no zone set, use the netif to
354 * set one now. Within the mld6 module, all addresses are properly zoned. */
355 if (ip6_addr_lacks_zone(groupaddr, IP6_MULTICAST)) {
356 ip6_addr_set(&ip6addr, groupaddr);
357 ip6_addr_assign_zone(&ip6addr, IP6_MULTICAST, netif);
358 groupaddr = &ip6addr;
359 }
360 IP6_ADDR_ZONECHECK_NETIF(groupaddr, netif);
361#endif /* LWIP_IPV6_SCOPES */
362
364
365 /* find group or create a new one if not found */
366 group = mld6_lookfor_group(netif, groupaddr);
367
368 if (group == NULL) {
369 /* Joining a new group. Create a new group entry. */
370 group = mld6_new_group(netif, groupaddr);
371 if (group == NULL) {
372 return ERR_MEM;
373 }
374
375 /* Activate this address on the MAC layer. */
376 if (netif->mld_mac_filter != NULL) {
377 netif->mld_mac_filter(netif, groupaddr, NETIF_ADD_MAC_FILTER);
378 }
379
380 /* Report our membership. */
381 MLD6_STATS_INC(mld6.tx_report);
382 mld6_send(netif, group, ICMP6_TYPE_MLR);
383 mld6_delayed_report(group, MLD6_JOIN_DELAYING_MEMBER_TMR_MS);
384 }
385
386 /* Increment group use */
387 group->use++;
388 return ERR_OK;
389}
390
403err_t
404mld6_leavegroup(const ip6_addr_t *srcaddr, const ip6_addr_t *groupaddr)
405{
406 err_t err = ERR_VAL; /* no matching interface */
407 struct netif *netif;
408
410
411 /* loop through netif's */
413 /* Should we leave this interface ? */
414 if (ip6_addr_isany(srcaddr) ||
415 netif_get_ip6_addr_match(netif, srcaddr) >= 0) {
416 err_t res = mld6_leavegroup_netif(netif, groupaddr);
417 if (err != ERR_OK) {
418 /* Store this result if we have not yet gotten a success */
419 err = res;
420 }
421 }
422 }
423
424 return err;
425}
426
436err_t
437mld6_leavegroup_netif(struct netif *netif, const ip6_addr_t *groupaddr)
438{
439 struct mld_group *group;
440#if LWIP_IPV6_SCOPES
441 ip6_addr_t ip6addr;
442
443 if (ip6_addr_lacks_zone(groupaddr, IP6_MULTICAST)) {
444 ip6_addr_set(&ip6addr, groupaddr);
445 ip6_addr_assign_zone(&ip6addr, IP6_MULTICAST, netif);
446 groupaddr = &ip6addr;
447 }
448 IP6_ADDR_ZONECHECK_NETIF(groupaddr, netif);
449#endif /* LWIP_IPV6_SCOPES */
450
452
453 /* find group */
454 group = mld6_lookfor_group(netif, groupaddr);
455
456 if (group != NULL) {
457 /* Leave if there is no other use of the group */
458 if (group->use <= 1) {
459 /* Remove the group from the list */
460 mld6_remove_group(netif, group);
461
462 /* If we are the last reporter for this group */
463 if (group->last_reporter_flag) {
464 MLD6_STATS_INC(mld6.tx_leave);
465 mld6_send(netif, group, ICMP6_TYPE_MLD);
466 }
467
468 /* Disable the group at the MAC level */
469 if (netif->mld_mac_filter != NULL) {
470 netif->mld_mac_filter(netif, groupaddr, NETIF_DEL_MAC_FILTER);
471 }
472
473 /* free group struct */
474 memp_free(MEMP_MLD6_GROUP, group);
475 } else {
476 /* Decrement group use */
477 group->use--;
478 }
479
480 /* Left group */
481 return ERR_OK;
482 }
483
484 /* Group not found */
485 return ERR_VAL;
486}
487
488
495void
496mld6_tmr(void)
497{
498 struct netif *netif;
499
501 struct mld_group *group = netif_mld6_data(netif);
502
503 while (group != NULL) {
504 if (group->timer > 0) {
505 group->timer--;
506 if (group->timer == 0) {
507 /* If the state is MLD6_GROUP_DELAYING_MEMBER then we send a report for this group */
508 if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) {
509 MLD6_STATS_INC(mld6.tx_report);
510 mld6_send(netif, group, ICMP6_TYPE_MLR);
511 group->group_state = MLD6_GROUP_IDLE_MEMBER;
512 }
513 }
514 }
515 group = group->next;
516 }
517 }
518}
519
527static void
528mld6_delayed_report(struct mld_group *group, u16_t maxresp_in)
529{
530 /* Convert maxresp from milliseconds to tmr ticks */
531 u16_t maxresp = maxresp_in / MLD6_TMR_INTERVAL;
532 if (maxresp == 0) {
533 maxresp = 1;
534 }
535
536#ifdef LWIP_RAND
537 /* Randomize maxresp. (if LWIP_RAND is supported) */
538 maxresp = (u16_t)(LWIP_RAND() % maxresp);
539 if (maxresp == 0) {
540 maxresp = 1;
541 }
542#endif /* LWIP_RAND */
543
544 /* Apply timer value if no report has been scheduled already. */
545 if ((group->group_state == MLD6_GROUP_IDLE_MEMBER) ||
546 ((group->group_state == MLD6_GROUP_DELAYING_MEMBER) &&
547 ((group->timer == 0) || (maxresp < group->timer)))) {
548 group->timer = maxresp;
549 group->group_state = MLD6_GROUP_DELAYING_MEMBER;
550 }
551}
552
562static void
563mld6_send(struct netif *netif, struct mld_group *group, u8_t type)
564{
565 struct mld_header *mld_hdr;
566 struct pbuf *p;
567 const ip6_addr_t *src_addr;
568
569 /* Allocate a packet. Size is MLD header + IPv6 Hop-by-hop options header. */
570 p = pbuf_alloc(PBUF_IP, sizeof(struct mld_header) + MLD6_HBH_HLEN, PBUF_RAM);
571 if (p == NULL) {
572 MLD6_STATS_INC(mld6.memerr);
573 return;
574 }
575
576 /* Move to make room for Hop-by-hop options header. */
578 pbuf_free(p);
579 MLD6_STATS_INC(mld6.lenerr);
580 return;
581 }
582
583 /* Select our source address. */
584 if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) {
585 /* This is a special case, when we are performing duplicate address detection.
586 * We must join the multicast group, but we don't have a valid address yet. */
587 src_addr = IP6_ADDR_ANY6;
588 } else {
589 /* Use link-local address as source address. */
590 src_addr = netif_ip6_addr(netif, 0);
591 }
592
593 /* MLD message header pointer. */
594 mld_hdr = (struct mld_header *)p->payload;
595
596 /* Set fields. */
597 mld_hdr->type = type;
598 mld_hdr->code = 0;
599 mld_hdr->chksum = 0;
600 mld_hdr->max_resp_delay = 0;
601 mld_hdr->reserved = 0;
602 ip6_addr_copy_to_packed(mld_hdr->multicast_address, group->group_address);
603
604#if CHECKSUM_GEN_ICMP6
605 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
606 mld_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len,
607 src_addr, &(group->group_address));
608 }
609#endif /* CHECKSUM_GEN_ICMP6 */
610
611 /* Add hop-by-hop headers options: router alert with MLD value. */
612 ip6_options_add_hbh_ra(p, IP6_NEXTH_ICMP6, IP6_ROUTER_ALERT_VALUE_MLD);
613
614 if (type == ICMP6_TYPE_MLR) {
615 /* Remember we were the last to report */
616 group->last_reporter_flag = 1;
617 }
618
619 /* Send the packet out. */
620 MLD6_STATS_INC(mld6.xmit);
621 ip6_output_if(p, (ip6_addr_isany(src_addr)) ? NULL : src_addr, &(group->group_address),
622 MLD6_HL, 0, IP6_NEXTH_HOPBYHOP, netif);
623 pbuf_free(p);
624}
625
626#endif /* LWIP_IPV6 */
#define lwip_ntohs(x)
Definition: def.h:87
#define NULL
Definition: types.h:112
switch(r->id)
Definition: btrfs.c:3046
#define ERR_MEM
Definition: fontsub.h:52
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint res
Definition: glext.h:9613
GLboolean GLuint group
Definition: glext.h:11120
GLenum const GLvoid * addr
Definition: glext.h:9621
GLfloat GLfloat p
Definition: glext.h:8902
uint8_t u8_t
Definition: arch.h:125
uint16_t u16_t
Definition: arch.h:127
s8_t err_t
Definition: err.h:96
@ ERR_OK
Definition: err.h:55
@ ERR_VAL
Definition: err.h:67
@ ERR_ARG
Definition: err.h:88
#define LWIP_ASSERT_CORE_LOCKED()
Definition: opt.h:227
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:224
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:727
@ PBUF_RAM
Definition: pbuf.h:152
@ PBUF_IP
Definition: pbuf.h:97
void * memp_malloc(memp_t type)
Definition: memp.c:337
void memp_free(memp_t type, void *mem)
Definition: memp.c:420
#define NETIF_FOREACH(netif)
Definition: netif.h:424
@ NETIF_ADD_MAC_FILTER
Definition: netif.h:163
@ NETIF_DEL_MAC_FILTER
Definition: netif.h:161
#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
Definition: netif.h:416
u8_t pbuf_remove_header(struct pbuf *p, size_t header_size_decrement)
Definition: pbuf.c:585
@ ICMP6_TYPE_MLD
Definition: icmp6.h:72
@ ICMP6_TYPE_MLR
Definition: icmp6.h:70
@ ICMP6_TYPE_MLQ
Definition: icmp6.h:68
#define IP6_NEXTH_HOPBYHOP
Definition: ip6.h:66
#define IP6_NEXTH_ICMP6
Definition: ip6.h:72
#define IP6_ROUTER_ALERT_VALUE_MLD
Definition: ip6.h:118
#define MLD6_HBH_HLEN
Definition: mld6.h:47
static unsigned __int64 next
Definition: rand_nt.c:6
#define err(...)
#define MLD6_STATS_INC(x)
Definition: stats.h:452
struct define * next
Definition: compiler.c:65
Definition: netif.h:269
struct netif * next
Definition: netif.h:272
Definition: pbuf.h:186