2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Multicast listener discovery for IPv6. Aims to be compliant with RFC 2710.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * No support for MLDv2.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Copyright (c) 2010 Inico Technologies Ltd.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * All rights reserved.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Redistribution and use in source and binary forms, with or without modification,
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * are permitted provided that the following conditions are met:
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * 1. Redistributions of source code must retain the above copyright notice,
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * this list of conditions and the following disclaimer.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * 2. Redistributions in binary form must reproduce the above copyright notice,
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * this list of conditions and the following disclaimer in the documentation
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * and/or other materials provided with the distribution.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * 3. The name of the author may not be used to endorse or promote products
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * derived from this software without specific prior written permission.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * OF SUCH DAMAGE.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * This file is part of the lwIP TCP/IP stack.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Author: Ivan Delamer <delamer@inicotech.com>
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Please coordinate changes and requests with Ivan Delamer
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * <delamer@inicotech.com>
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync/* Based on igmp.c implementation of igmp v2 protocol */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync#if LWIP_IPV6 && LWIP_IPV6_MLD /* don't build if not configured for use in lwipopts.h */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * MLD constants
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync/* The list of joined groups. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync/* Forward declarations. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncstatic struct mld_group * mld6_new_group(struct netif *ifp, ip6_addr_t *addr);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncstatic err_t mld6_free_group(struct mld_group *group);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncstatic void mld6_delayed_report(struct mld_group *group, u16_t maxresp);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncstatic void mld6_send(struct mld_group *group, u8_t type);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Stop MLD processing on interface
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param netif network interface on which stop MLD processing
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* look for groups joined on this interface further down the list */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* is it a group joined on this interface? */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* is it the first group of the list? */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* is there a "previous" group defined? */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* disable the group at the MAC level */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync netif->mld_mac_filter(netif, &(group->group_address), MLD6_DEL_MAC_FILTER);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* free group */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* change the "previous" */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* move to "next" */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Report MLD memberships for this interface
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param netif network interface on which report MLD memberships
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync mld6_delayed_report(group, MLD6_JOIN_DELAYING_MEMBER_TMR_MS);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Search for a group that is joined on a netif
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param ifp the network interface for which to look
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param addr the group ipv6 address to search for
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @return a struct mld_group* if the group has been found,
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * NULL if the group wasn't found.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncmld6_lookfor_group(struct netif *ifp, ip6_addr_t *addr)
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync if ((group->netif == ifp) && (ip6_addr_cmp(&(group->group_address), addr))) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * create a new group
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param ifp the network interface for which to create
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param addr the new group ipv6
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @return a struct mld_group*,
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * NULL on memory error.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncstatic struct mld_group *
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync group = (struct mld_group *)memp_malloc(MEMP_MLD6_GROUP);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Remove a group in the mld_group_list and free
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param group the group to remove
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @return ERR_OK if group was removed from the list, an err_t otherwise
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Is it the first group? */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* look for group further down the list */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync for (tmpGroup = mld_group_list; tmpGroup != NULL; tmpGroup = tmpGroup->next) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Group not find group */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* free group */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Process an input MLD message. Called by icmp6_input.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param p the mld packet, p->payload pointing to the icmpv6 header
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param inp the netif on which this packet was received
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Check that mld header fits in packet. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* TODO debug message */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync case ICMP6_TYPE_MLQ: /* Multicast listener query. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Is it a general query? */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync if (ip6_addr_isallnodes_linklocal(ip6_current_dest_addr()) &&
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Report all groups, except all nodes group, and if-local groups. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync (!(ip6_addr_ismulticast_iflocal(&(group->group_address)))) &&
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync (!(ip6_addr_isallnodes_linklocal(&(group->group_address))))) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync mld6_delayed_report(group, mld_hdr->max_resp_delay);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Have we joined this group?
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * We use IP6 destination address to have a memory aligned copy.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * mld_hdr->multicast_address should be the same. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync group = mld6_lookfor_group(inp, ip6_current_dest_addr());
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Schedule a report. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync mld6_delayed_report(group, mld_hdr->max_resp_delay);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync break; /* ICMP6_TYPE_MLQ */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync case ICMP6_TYPE_MLR: /* Multicast listener report. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Have we joined this group?
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * We use IP6 destination address to have a memory aligned copy.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * mld_hdr->multicast_address should be the same. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync group = mld6_lookfor_group(inp, ip6_current_dest_addr());
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* If we are waiting to report, cancel it. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync break; /* ICMP6_TYPE_MLR */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync case ICMP6_TYPE_MLD: /* Multicast listener done. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Do nothing, router will query us. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync break; /* ICMP6_TYPE_MLD */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Join a group on a network interface.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param srcaddr ipv6 address of the network interface which should
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * join a new group. If IP6_ADDR_ANY, join on all netifs
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param groupaddr the ipv6 address of the group to join
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @return ERR_OK if group was joined on the netif(s), an err_t otherwise
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncmld6_joingroup(ip6_addr_t *srcaddr, ip6_addr_t *groupaddr)
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync for (netif = netif_list; netif != NULL; netif = netif->next) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync for (netif = netif_list; netif != NULL; netif = netif->next) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Join a group on the specified network interface.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param netif the network interface which should join a new group.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param groupaddr the ipv6 address of the group to join
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @return ERR_OK if group was joined, an err_t otherwise
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncmld6_netif_joingroup(struct netif *netif, ip6_addr_t *groupaddr)
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* find group or create a new one if not found */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Joining a new group. Create a new group entry. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Activate this address on the MAC layer. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync netif->mld_mac_filter(netif, groupaddr, MLD6_ADD_MAC_FILTER);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Report our membership. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync mld6_delayed_report(group, MLD6_JOIN_DELAYING_MEMBER_TMR_MS);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Increment group use */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Leave a group on a network interface.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param srcaddr ipv6 address of the network interface which should
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * leave the group. If IP6_ISANY, leave on all netifs
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param groupaddr the ipv6 address of the group to leave
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @return ERR_OK if group was left on the netif(s), an err_t otherwise
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncmld6_leavegroup(ip6_addr_t *srcaddr, ip6_addr_t *groupaddr)
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync for (netif = netif_list; netif != NULL; netif = netif->next) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync for (netif = netif_list; netif != NULL; netif = netif->next) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Leave a group on the specified network interface.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param netif the network interface which should leave the group.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param groupaddr the ipv6 address of the group to leave
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @return ERR_OK if group was left, an err_t otherwise
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncmld6_netif_leavegroup(struct netif *netif, ip6_addr_t *groupaddr)
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* find group */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Leave if there is no other use of the group */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* If we are the last reporter for this group */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Disable the group at the MAC level */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync netif->mld_mac_filter(netif, groupaddr, MLD6_DEL_MAC_FILTER);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Free the group */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Decrement group use */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Periodic timer for mld processing. Must be called every
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * MLD6_TMR_INTERVAL milliseconds (100).
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * When a delaying member expires, a membership report is sent.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* If the state is MLD6_GROUP_DELAYING_MEMBER then we send a report for this group */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Schedule a delayed membership report for a group
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param group the mld_group for which "delaying" membership report
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * should be sent
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param maxresp the max resp delay provided in the query
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsyncmld6_delayed_report(struct mld_group *group, u16_t maxresp)
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Convert maxresp from milliseconds to tmr ticks */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Randomize maxresp. (if LWIP_RAND is supported) */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync#endif /* LWIP_RAND */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Apply timer value if no report has been scheduled already. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync if ((group->group_state == MLD6_GROUP_IDLE_MEMBER) ||
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync ((group->group_state == MLD6_GROUP_DELAYING_MEMBER) &&
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync ((group->timer == 0) || (maxresp < group->timer)))) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * Send a MLD message (report or done).
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * An IPv6 hop-by-hop options header with a router alert option
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * is prepended.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param group the group to report or quit
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * @param type ICMP6_TYPE_MLR (report) or ICMP6_TYPE_MLD (done)
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync struct pbuf * p;
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Allocate a packet. Size is MLD header + IPv6 Hop-by-hop options header. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync p = pbuf_alloc(PBUF_IP, sizeof(struct mld_header) + sizeof(struct ip6_hbh_hdr), PBUF_RAM);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync if ((p == NULL) || (p->len < (sizeof(struct mld_header) + sizeof(struct ip6_hbh_hdr)))) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* We couldn't allocate a suitable pbuf. drop it. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync if (p != NULL) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Move to make room for Hop-by-hop options header. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Select our source address. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync if (!ip6_addr_isvalid(netif_ip6_addr_state(group->netif, 0))) {
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* This is a special case, when we are performing duplicate address detection.
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync * We must join the multicast group, but we don't have a valid address yet. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Use link-local address as source address. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* MLD message header pointer. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Set fields. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync ip6_addr_set(&(mld_hdr->multicast_address), &(group->group_address));
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync mld_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len,
5395d415168ae1b063240e1ee881bd028f37a60dvboxsync#endif /* CHECKSUM_GEN_ICMP6 */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Add hop-by-hop headers options: router alert with MLD value. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync ip6_options_add_hbh_ra(p, IP6_NEXTH_ICMP6, IP6_ROUTER_ALERT_VALUE_MLD);
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync /* Send the packet out. */
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync ip6_output_if(p, (ip6_addr_isany(src_addr)) ? NULL : src_addr, &(group->group_address),
2e848e79ccf2e4285250a0af98ddb9eb28864878vboxsync#endif /* LWIP_IPV6 */