The Pedigree Project 0.1
mld6.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
31/*
32 * Copyright (c) 2010 Inico Technologies Ltd.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without modification,
36 * are permitted provided that the following conditions are met:
37 *
38 * 1. Redistributions of source code must retain the above copyright notice,
39 * this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright notice,
41 * this list of conditions and the following disclaimer in the documentation
42 * and/or other materials provided with the distribution.
43 * 3. The name of the author may not be used to endorse or promote products
44 * derived from this software without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
47 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
49 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
51 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
54 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
55 * OF SUCH DAMAGE.
56 *
57 * This file is part of the lwIP TCP/IP stack.
58 *
59 * Author: Ivan Delamer <delamer@inicotech.com>
60 *
61 *
62 * Please coordinate changes and requests with Ivan Delamer
63 * <delamer@inicotech.com>
64 */
65
66/* Based on igmp.c implementation of igmp v2 protocol */
67
68#include "lwip/opt.h"
69
70#if LWIP_IPV6 && LWIP_IPV6_MLD /* don't build if not configured for use in lwipopts.h */
71
72#include "lwip/mld6.h"
73#include "lwip/prot/mld6.h"
74#include "lwip/icmp6.h"
75#include "lwip/ip6.h"
76#include "lwip/ip6_addr.h"
77#include "lwip/ip.h"
78#include "lwip/inet_chksum.h"
79#include "lwip/pbuf.h"
80#include "lwip/netif.h"
81#include "lwip/memp.h"
82#include "lwip/stats.h"
83
84#include <string.h>
85
86
87/*
88 * MLD constants
89 */
90#define MLD6_HL 1
91#define MLD6_JOIN_DELAYING_MEMBER_TMR_MS (500)
92
93#define MLD6_GROUP_NON_MEMBER 0
94#define MLD6_GROUP_DELAYING_MEMBER 1
95#define MLD6_GROUP_IDLE_MEMBER 2
96
97/* Forward declarations. */
98static struct mld_group *mld6_new_group(struct netif *ifp, const ip6_addr_t *addr);
99static err_t mld6_remove_group(struct netif *netif, struct mld_group *group);
100static void mld6_delayed_report(struct mld_group *group, u16_t maxresp);
101static void mld6_send(struct netif *netif, struct mld_group *group, u8_t type);
102
103
109err_t
110mld6_stop(struct netif *netif)
111{
112 struct mld_group *group = netif_mld6_data(netif);
113
114 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_MLD6, NULL);
115
116 while (group != NULL) {
117 struct mld_group *next = group->next; /* avoid use-after-free below */
118
119 /* disable the group at the MAC level */
120 if (netif->mld_mac_filter != NULL) {
121 netif->mld_mac_filter(netif, &(group->group_address), NETIF_DEL_MAC_FILTER);
122 }
123
124 /* free group */
125 memp_free(MEMP_MLD6_GROUP, group);
126
127 /* move to "next" */
128 group = next;
129 }
130 return ERR_OK;
131}
132
138void
139mld6_report_groups(struct netif *netif)
140{
141 struct mld_group *group = netif_mld6_data(netif);
142
143 while (group != NULL) {
144 mld6_delayed_report(group, MLD6_JOIN_DELAYING_MEMBER_TMR_MS);
145 group = group->next;
146 }
147}
148
157struct mld_group *
158mld6_lookfor_group(struct netif *ifp, const ip6_addr_t *addr)
159{
160 struct mld_group *group = netif_mld6_data(ifp);
161
162 while (group != NULL) {
163 if (ip6_addr_cmp(&(group->group_address), addr)) {
164 return group;
165 }
166 group = group->next;
167 }
168
169 return NULL;
170}
171
172
181static struct mld_group *
182mld6_new_group(struct netif *ifp, const ip6_addr_t *addr)
183{
184 struct mld_group *group;
185
186 group = (struct mld_group *)memp_malloc(MEMP_MLD6_GROUP);
187 if (group != NULL) {
188 ip6_addr_set(&(group->group_address), addr);
189 group->timer = 0; /* Not running */
190 group->group_state = MLD6_GROUP_IDLE_MEMBER;
191 group->last_reporter_flag = 0;
192 group->use = 0;
193 group->next = netif_mld6_data(ifp);
194
195 netif_set_client_data(ifp, LWIP_NETIF_CLIENT_DATA_INDEX_MLD6, group);
196 }
197
198 return group;
199}
200
207static err_t
208mld6_remove_group(struct netif *netif, struct mld_group *group)
209{
210 err_t err = ERR_OK;
211
212 /* Is it the first group? */
213 if (netif_mld6_data(netif) == group) {
214 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_MLD6, group->next);
215 } else {
216 /* look for group further down the list */
217 struct mld_group *tmpGroup;
218 for (tmpGroup = netif_mld6_data(netif); tmpGroup != NULL; tmpGroup = tmpGroup->next) {
219 if (tmpGroup->next == group) {
220 tmpGroup->next = group->next;
221 break;
222 }
223 }
224 /* Group not find group */
225 if (tmpGroup == NULL) {
226 err = ERR_ARG;
227 }
228 }
229
230 return err;
231}
232
233
240void
241mld6_input(struct pbuf *p, struct netif *inp)
242{
243 struct mld_header *mld_hdr;
244 struct mld_group *group;
245
246 MLD6_STATS_INC(mld6.recv);
247
248 /* Check that mld header fits in packet. */
249 if (p->len < sizeof(struct mld_header)) {
250 /* @todo debug message */
251 pbuf_free(p);
252 MLD6_STATS_INC(mld6.lenerr);
253 MLD6_STATS_INC(mld6.drop);
254 return;
255 }
256
257 mld_hdr = (struct mld_header *)p->payload;
258
259 switch (mld_hdr->type) {
260 case ICMP6_TYPE_MLQ: /* Multicast listener query. */
261 /* Is it a general query? */
262 if (ip6_addr_isallnodes_linklocal(ip6_current_dest_addr()) &&
263 ip6_addr_isany(&(mld_hdr->multicast_address))) {
264 MLD6_STATS_INC(mld6.rx_general);
265 /* Report all groups, except all nodes group, and if-local groups. */
266 group = netif_mld6_data(inp);
267 while (group != NULL) {
268 if ((!(ip6_addr_ismulticast_iflocal(&(group->group_address)))) &&
269 (!(ip6_addr_isallnodes_linklocal(&(group->group_address))))) {
270 mld6_delayed_report(group, mld_hdr->max_resp_delay);
271 }
272 group = group->next;
273 }
274 } else {
275 /* Have we joined this group?
276 * We use IP6 destination address to have a memory aligned copy.
277 * mld_hdr->multicast_address should be the same. */
278 MLD6_STATS_INC(mld6.rx_group);
279 group = mld6_lookfor_group(inp, ip6_current_dest_addr());
280 if (group != NULL) {
281 /* Schedule a report. */
282 mld6_delayed_report(group, mld_hdr->max_resp_delay);
283 }
284 }
285 break; /* ICMP6_TYPE_MLQ */
286 case ICMP6_TYPE_MLR: /* Multicast listener report. */
287 /* Have we joined this group?
288 * We use IP6 destination address to have a memory aligned copy.
289 * mld_hdr->multicast_address should be the same. */
290 MLD6_STATS_INC(mld6.rx_report);
291 group = mld6_lookfor_group(inp, ip6_current_dest_addr());
292 if (group != NULL) {
293 /* If we are waiting to report, cancel it. */
294 if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) {
295 group->timer = 0; /* stopped */
296 group->group_state = MLD6_GROUP_IDLE_MEMBER;
297 group->last_reporter_flag = 0;
298 }
299 }
300 break; /* ICMP6_TYPE_MLR */
301 case ICMP6_TYPE_MLD: /* Multicast listener done. */
302 /* Do nothing, router will query us. */
303 break; /* ICMP6_TYPE_MLD */
304 default:
305 MLD6_STATS_INC(mld6.proterr);
306 MLD6_STATS_INC(mld6.drop);
307 break;
308 }
309
310 pbuf_free(p);
311}
312
322err_t
323mld6_joingroup(const ip6_addr_t *srcaddr, const ip6_addr_t *groupaddr)
324{
325 err_t err = ERR_VAL; /* no matching interface */
326 struct netif *netif;
327
328 /* loop through netif's */
330 while (netif != NULL) {
331 /* Should we join this interface ? */
332 if (ip6_addr_isany(srcaddr) ||
333 netif_get_ip6_addr_match(netif, srcaddr) >= 0) {
334 err = mld6_joingroup_netif(netif, groupaddr);
335 if (err != ERR_OK) {
336 return err;
337 }
338 }
339
340 /* proceed to next network interface */
341 netif = netif->next;
342 }
343
344 return err;
345}
346
355err_t
356mld6_joingroup_netif(struct netif *netif, const ip6_addr_t *groupaddr)
357{
358 struct mld_group *group;
359
360 /* find group or create a new one if not found */
361 group = mld6_lookfor_group(netif, groupaddr);
362
363 if (group == NULL) {
364 /* Joining a new group. Create a new group entry. */
365 group = mld6_new_group(netif, groupaddr);
366 if (group == NULL) {
367 return ERR_MEM;
368 }
369
370 /* Activate this address on the MAC layer. */
371 if (netif->mld_mac_filter != NULL) {
372 netif->mld_mac_filter(netif, groupaddr, NETIF_ADD_MAC_FILTER);
373 }
374
375 /* Report our membership. */
376 MLD6_STATS_INC(mld6.tx_report);
377 mld6_send(netif, group, ICMP6_TYPE_MLR);
378 mld6_delayed_report(group, MLD6_JOIN_DELAYING_MEMBER_TMR_MS);
379 }
380
381 /* Increment group use */
382 group->use++;
383 return ERR_OK;
384}
385
395err_t
396mld6_leavegroup(const ip6_addr_t *srcaddr, const ip6_addr_t *groupaddr)
397{
398 err_t err = ERR_VAL; /* no matching interface */
399 struct netif *netif;
400
401 /* loop through netif's */
403 while (netif != NULL) {
404 /* Should we leave this interface ? */
405 if (ip6_addr_isany(srcaddr) ||
406 netif_get_ip6_addr_match(netif, srcaddr) >= 0) {
407 err_t res = mld6_leavegroup_netif(netif, groupaddr);
408 if (err != ERR_OK) {
409 /* Store this result if we have not yet gotten a success */
410 err = res;
411 }
412 }
413 /* proceed to next network interface */
414 netif = netif->next;
415 }
416
417 return err;
418}
419
428err_t
429mld6_leavegroup_netif(struct netif *netif, const ip6_addr_t *groupaddr)
430{
431 struct mld_group *group;
432
433 /* find group */
434 group = mld6_lookfor_group(netif, groupaddr);
435
436 if (group != NULL) {
437 /* Leave if there is no other use of the group */
438 if (group->use <= 1) {
439 /* Remove the group from the list */
440 mld6_remove_group(netif, group);
441
442 /* If we are the last reporter for this group */
443 if (group->last_reporter_flag) {
444 MLD6_STATS_INC(mld6.tx_leave);
445 mld6_send(netif, group, ICMP6_TYPE_MLD);
446 }
447
448 /* Disable the group at the MAC level */
449 if (netif->mld_mac_filter != NULL) {
450 netif->mld_mac_filter(netif, groupaddr, NETIF_DEL_MAC_FILTER);
451 }
452
453 /* free group struct */
454 memp_free(MEMP_MLD6_GROUP, group);
455 } else {
456 /* Decrement group use */
457 group->use--;
458 }
459
460 /* Left group */
461 return ERR_OK;
462 }
463
464 /* Group not found */
465 return ERR_VAL;
466}
467
468
475void
476mld6_tmr(void)
477{
478 struct netif *netif = netif_list;
479
480 while (netif != NULL) {
481 struct mld_group *group = netif_mld6_data(netif);
482
483 while (group != NULL) {
484 if (group->timer > 0) {
485 group->timer--;
486 if (group->timer == 0) {
487 /* If the state is MLD6_GROUP_DELAYING_MEMBER then we send a report for this group */
488 if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) {
489 MLD6_STATS_INC(mld6.tx_report);
490 mld6_send(netif, group, ICMP6_TYPE_MLR);
491 group->group_state = MLD6_GROUP_IDLE_MEMBER;
492 }
493 }
494 }
495 group = group->next;
496 }
497 netif = netif->next;
498 }
499}
500
508static void
509mld6_delayed_report(struct mld_group *group, u16_t maxresp)
510{
511 /* Convert maxresp from milliseconds to tmr ticks */
512 maxresp = maxresp / MLD6_TMR_INTERVAL;
513 if (maxresp == 0) {
514 maxresp = 1;
515 }
516
517#ifdef LWIP_RAND
518 /* Randomize maxresp. (if LWIP_RAND is supported) */
519 maxresp = LWIP_RAND() % maxresp;
520 if (maxresp == 0) {
521 maxresp = 1;
522 }
523#endif /* LWIP_RAND */
524
525 /* Apply timer value if no report has been scheduled already. */
526 if ((group->group_state == MLD6_GROUP_IDLE_MEMBER) ||
527 ((group->group_state == MLD6_GROUP_DELAYING_MEMBER) &&
528 ((group->timer == 0) || (maxresp < group->timer)))) {
529 group->timer = maxresp;
530 group->group_state = MLD6_GROUP_DELAYING_MEMBER;
531 }
532}
533
543static void
544mld6_send(struct netif *netif, struct mld_group *group, u8_t type)
545{
546 struct mld_header *mld_hdr;
547 struct pbuf *p;
548 const ip6_addr_t *src_addr;
549
550 /* Allocate a packet. Size is MLD header + IPv6 Hop-by-hop options header. */
551 p = pbuf_alloc(PBUF_IP, sizeof(struct mld_header) + sizeof(struct ip6_hbh_hdr), PBUF_RAM);
552 if (p == NULL) {
553 MLD6_STATS_INC(mld6.memerr);
554 return;
555 }
556
557 /* Move to make room for Hop-by-hop options header. */
558 if (pbuf_header(p, -IP6_HBH_HLEN)) {
559 pbuf_free(p);
560 MLD6_STATS_INC(mld6.lenerr);
561 return;
562 }
563
564 /* Select our source address. */
565 if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, 0))) {
566 /* This is a special case, when we are performing duplicate address detection.
567 * We must join the multicast group, but we don't have a valid address yet. */
568 src_addr = IP6_ADDR_ANY6;
569 } else {
570 /* Use link-local address as source address. */
571 src_addr = netif_ip6_addr(netif, 0);
572 }
573
574 /* MLD message header pointer. */
575 mld_hdr = (struct mld_header *)p->payload;
576
577 /* Set fields. */
578 mld_hdr->type = type;
579 mld_hdr->code = 0;
580 mld_hdr->chksum = 0;
581 mld_hdr->max_resp_delay = 0;
582 mld_hdr->reserved = 0;
583 ip6_addr_set(&(mld_hdr->multicast_address), &(group->group_address));
584
585#if CHECKSUM_GEN_ICMP6
586 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_ICMP6) {
587 mld_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len,
588 src_addr, &(group->group_address));
589 }
590#endif /* CHECKSUM_GEN_ICMP6 */
591
592 /* Add hop-by-hop headers options: router alert with MLD value. */
593 ip6_options_add_hbh_ra(p, IP6_NEXTH_ICMP6, IP6_ROUTER_ALERT_VALUE_MLD);
594
595 if (type == ICMP6_TYPE_MLR) {
596 /* Remember we were the last to report */
597 group->last_reporter_flag = 1;
598 }
599
600 /* Send the packet out. */
601 MLD6_STATS_INC(mld6.xmit);
602 ip6_output_if(p, (ip6_addr_isany(src_addr)) ? NULL : src_addr, &(group->group_address),
603 MLD6_HL, 0, IP6_NEXTH_HOPBYHOP, netif);
604 pbuf_free(p);
605}
606
607#endif /* LWIP_IPV6 */
s8_t err_t
Definition err.h:76
@ ERR_OK
Definition err.h:82
@ ERR_VAL
Definition err.h:94
@ ERR_MEM
Definition err.h:84
@ ERR_ARG
Definition err.h:115
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition pbuf.c:267
u8_t pbuf_free(struct pbuf *p)
Definition pbuf.c:734
@ PBUF_RAM
Definition pbuf.h:127
@ PBUF_IP
Definition pbuf.h:99
void * memp_malloc(memp_t type)
Definition memp.c:404
void memp_free(memp_t type, void *mem)
Definition memp.c:488
struct netif * netif_list
Definition netif.c:123
@ NETIF_ADD_MAC_FILTER
Definition netif.h:166
@ NETIF_DEL_MAC_FILTER
Definition netif.h:164
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition pbuf.c:684
@ ICMP6_TYPE_MLD
Definition prot/icmp6.h:91
@ ICMP6_TYPE_MLR
Definition prot/icmp6.h:89
@ ICMP6_TYPE_MLQ
Definition prot/icmp6.h:87
Definition netif.h:244
struct netif * next
Definition netif.h:246
Definition pbuf.h:161
u16_t len
Definition pbuf.h:178
void * payload
Definition pbuf.h:166