• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * Multicast listener discovery
4  *
5  * @defgroup mld6 MLD6
6  * @ingroup ip6
7  * Multicast listener discovery for IPv6. Aims to be compliant with RFC 2710.
8  * No support for MLDv2.\n
9  * Note: The allnodes (ff01::1, ff02::1) group is assumed be received by your
10  * netif since it must always be received for correct IPv6 operation (e.g. SLAAC).
11  * Ensure the netif filters are configured accordingly!\n
12  * The netif flags also need NETIF_FLAG_MLD6 flag set to enable MLD6 on a
13  * netif ("netif->flags |= NETIF_FLAG_MLD6;").\n
14  * To be called from TCPIP thread.
15  */
16 
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. */
84 static struct mld_group *mld6_new_group(struct netif *ifp, const ip6_addr_t *addr);
85 static err_t mld6_remove_group(struct netif *netif, struct mld_group *group);
86 static void mld6_delayed_report(struct mld_group *group, u16_t maxresp);
87 static void mld6_send(struct netif *netif, struct mld_group *group, u8_t type);
88 
89 
90 /**
91  * Stop MLD processing on interface
92  *
93  * @param netif network interface on which stop MLD processing
94  */
95 err_t
mld6_stop(struct netif * netif)96 mld6_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 
119 /**
120  * Report MLD memberships for this interface
121  *
122  * @param netif network interface on which report MLD memberships
123  */
124 void
mld6_report_groups(struct netif * netif)125 mld6_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 
135 /**
136  * Search for a group that is joined on a netif
137  *
138  * @param ifp the network interface for which to look
139  * @param addr the group ipv6 address to search for
140  * @return a struct mld_group* if the group has been found,
141  *         NULL if the group wasn't found.
142  */
143 struct mld_group *
mld6_lookfor_group(struct netif * ifp,const ip6_addr_t * addr)144 mld6_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_cmp(&(group->group_address), addr)) {
150       return group;
151     }
152     group = group->next;
153   }
154 
155   return NULL;
156 }
157 
158 
159 /**
160  * create a new group
161  *
162  * @param ifp the network interface for which to create
163  * @param addr the new group ipv6
164  * @return a struct mld_group*,
165  *         NULL on memory error.
166  */
167 static struct mld_group *
mld6_new_group(struct netif * ifp,const ip6_addr_t * addr)168 mld6_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 
187 /**
188  * Remove a group from the mld_group_list, but do not free it yet
189  *
190  * @param group the group to remove
191  * @return ERR_OK if group was removed from the list, an err_t otherwise
192  */
193 static err_t
mld6_remove_group(struct netif * netif,struct mld_group * group)194 mld6_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 
220 /**
221  * Process an input MLD message. Called by icmp6_input.
222  *
223  * @param p the mld packet, p->payload pointing to the icmpv6 header
224  * @param inp the netif on which this packet was received
225  */
226 void
mld6_input(struct pbuf * p,struct netif * inp)227 mld6_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 
299 /**
300  * @ingroup mld6
301  * Join a group on one or all network interfaces.
302  *
303  * If the group is to be joined on all interfaces, the given group address must
304  * not have a zone set (i.e., it must have its zone index set to IP6_NO_ZONE).
305  * If the group is to be joined on one particular interface, the given group
306  * address may or may not have a zone set.
307  *
308  * @param srcaddr ipv6 address (zoned) of the network interface which should
309  *                join a new group. If IP6_ADDR_ANY6, join on all netifs
310  * @param groupaddr the ipv6 address of the group to join (possibly but not
311  *                  necessarily zoned)
312  * @return ERR_OK if group was joined on the netif(s), an err_t otherwise
313  */
314 err_t
mld6_joingroup(const ip6_addr_t * srcaddr,const ip6_addr_t * groupaddr)315 mld6_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 
320   LWIP_ASSERT_CORE_LOCKED();
321 
322   /* loop through netif's */
323   NETIF_FOREACH(netif) {
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 
337 /**
338  * @ingroup mld6
339  * Join a group on a network interface.
340  *
341  * @param netif the network interface which should join a new group.
342  * @param groupaddr the ipv6 address of the group to join (possibly but not
343  *                  necessarily zoned)
344  * @return ERR_OK if group was joined on the netif, an err_t otherwise
345  */
346 err_t
mld6_joingroup_netif(struct netif * netif,const ip6_addr_t * groupaddr)347 mld6_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 
363   LWIP_ASSERT_CORE_LOCKED();
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 
391 /**
392  * @ingroup mld6
393  * Leave a group on a network interface.
394  *
395  * Zoning of address follows the same rules as @ref mld6_joingroup.
396  *
397  * @param srcaddr ipv6 address (zoned) of the network interface which should
398  *                leave the group. If IP6_ADDR_ANY6, leave on all netifs
399  * @param groupaddr the ipv6 address of the group to leave (possibly, but not
400  *                  necessarily zoned)
401  * @return ERR_OK if group was left on the netif(s), an err_t otherwise
402  */
403 err_t
mld6_leavegroup(const ip6_addr_t * srcaddr,const ip6_addr_t * groupaddr)404 mld6_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 
409   LWIP_ASSERT_CORE_LOCKED();
410 
411   /* loop through netif's */
412   NETIF_FOREACH(netif) {
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 
427 /**
428  * @ingroup mld6
429  * Leave a group on a network interface.
430  *
431  * @param netif the network interface which should leave the group.
432  * @param groupaddr the ipv6 address of the group to leave (possibly, but not
433  *                  necessarily zoned)
434  * @return ERR_OK if group was left on the netif, an err_t otherwise
435  */
436 err_t
mld6_leavegroup_netif(struct netif * netif,const ip6_addr_t * groupaddr)437 mld6_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 
451   LWIP_ASSERT_CORE_LOCKED();
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 
489 /**
490  * Periodic timer for mld processing. Must be called every
491  * MLD6_TMR_INTERVAL milliseconds (100).
492  *
493  * When a delaying member expires, a membership report is sent.
494  */
495 void
mld6_tmr(void)496 mld6_tmr(void)
497 {
498   struct netif *netif;
499 
500   NETIF_FOREACH(netif) {
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 
520 /**
521  * Schedule a delayed membership report for a group
522  *
523  * @param group the mld_group for which "delaying" membership report
524  *              should be sent
525  * @param maxresp_in the max resp delay provided in the query
526  */
527 static void
mld6_delayed_report(struct mld_group * group,u16_t maxresp_in)528 mld6_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 
553 /**
554  * Send a MLD message (report or done).
555  *
556  * An IPv6 hop-by-hop options header with a router alert option
557  * is prepended.
558  *
559  * @param group the group to report or quit
560  * @param type ICMP6_TYPE_MLR (report) or ICMP6_TYPE_MLD (done)
561  */
562 static void
mld6_send(struct netif * netif,struct mld_group * group,u8_t type)563 mld6_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. */
577   if (pbuf_remove_header(p, MLD6_HBH_HLEN)) {
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 */
627