2012-03-04 00:18:34 +01:00
|
|
|
/****************************************************************************
|
2014-05-30 21:05:19 +02:00
|
|
|
* net/igmp/igmp_group.c
|
2012-03-04 00:18:34 +01:00
|
|
|
* IGMP group data structure management logic
|
|
|
|
*
|
2020-04-09 14:46:24 +02:00
|
|
|
* Copyright (C) 2010, 2013-2014, 2016, 2018 Gregory Nutt.
|
|
|
|
* All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2012-03-04 00:18:34 +01:00
|
|
|
*
|
|
|
|
* The NuttX implementation of IGMP was inspired by the IGMP add-on for the
|
|
|
|
* lwIP TCP/IP stack by Steve Reynolds:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002 CITEL Technologies Ltd.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-04-13 22:32:20 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2012-03-04 00:18:34 +01:00
|
|
|
*
|
2014-04-13 22:32:20 +02:00
|
|
|
* 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.
|
2020-04-09 14:46:24 +02:00
|
|
|
* 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.
|
2012-03-04 00:18:34 +01:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
|
2014-04-13 22:32:20 +02:00
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2020-04-09 14:46:24 +02:00
|
|
|
* 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.
|
2012-03-04 00:18:34 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <queue.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
|
2012-03-04 00:18:34 +01:00
|
|
|
#include <nuttx/arch.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/wdog.h>
|
2012-03-04 00:18:34 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2016-11-03 19:17:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2014-06-24 18:14:15 +02:00
|
|
|
#include <nuttx/net/igmp.h>
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 17:52:36 +02:00
|
|
|
#include "igmp/igmp.h"
|
2012-03-04 00:18:34 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IGMP
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Debug ********************************************************************/
|
|
|
|
|
|
|
|
#undef IGMP_GRPDEBUG /* Define to enable detailed IGMP group debug */
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET_IGMP
|
|
|
|
# undef IGMP_GRPDEBUG
|
|
|
|
#endif
|
|
|
|
|
2020-05-04 12:55:11 +02:00
|
|
|
#ifdef IGMP_GRPDEBUG
|
|
|
|
# define grperr nerr
|
|
|
|
# define grpinfo ninfo
|
2012-03-04 00:18:34 +01:00
|
|
|
#else
|
2020-05-04 12:55:11 +02:00
|
|
|
# define grperr _none
|
|
|
|
# define grpinfo _none
|
2012-03-04 00:18:34 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 17:52:36 +02:00
|
|
|
* Name: igmp_grpalloc
|
2012-03-04 00:18:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new group from heap memory.
|
|
|
|
*
|
2018-11-04 14:43:51 +01:00
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
2012-03-04 00:18:34 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
FAR struct igmp_group_s *igmp_grpalloc(FAR struct net_driver_s *dev,
|
2015-01-16 19:30:18 +01:00
|
|
|
FAR const in_addr_t *addr)
|
2012-03-04 00:18:34 +01:00
|
|
|
{
|
|
|
|
FAR struct igmp_group_s *group;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("addr: %08x dev: %p\n", *addr, dev);
|
2017-09-02 18:27:03 +02:00
|
|
|
group = (FAR struct igmp_group_s *)kmm_zalloc(sizeof(struct igmp_group_s));
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
grpinfo("group: %p\n", group);
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
/* Check if we successfully allocated a group structure */
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2017-09-02 18:27:03 +02:00
|
|
|
if (group != NULL)
|
2012-03-04 00:18:34 +01:00
|
|
|
{
|
|
|
|
/* Initialize the non-zero elements of the group structure */
|
|
|
|
|
2015-01-16 20:01:08 +01:00
|
|
|
net_ipv4addr_copy(group->grpaddr, *addr);
|
2016-11-03 19:17:02 +01:00
|
|
|
|
|
|
|
/* This semaphore is used for signaling and, hence, should not have
|
|
|
|
* priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&group->sem, 0, 0);
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&group->sem, SEM_PRIO_NONE);
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2018-11-04 14:43:51 +01:00
|
|
|
/* Save the interface index */
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2018-11-04 14:43:51 +01:00
|
|
|
group->ifindex = dev->d_ifindex;
|
2012-03-04 00:18:34 +01:00
|
|
|
|
|
|
|
/* Add the group structure to the list in the device structure */
|
|
|
|
|
2018-11-03 14:06:30 +01:00
|
|
|
sq_addfirst((FAR sq_entry_t *)group, &dev->d_igmp_grplist);
|
2012-03-04 00:18:34 +01:00
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2012-03-04 00:18:34 +01:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 17:52:36 +02:00
|
|
|
* Name: igmp_grpfind
|
2012-03-04 00:18:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find an existing group.
|
|
|
|
*
|
2018-11-04 14:43:51 +01:00
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
2012-03-04 00:18:34 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
FAR struct igmp_group_s *igmp_grpfind(FAR struct net_driver_s *dev,
|
2015-01-16 19:30:18 +01:00
|
|
|
FAR const in_addr_t *addr)
|
2012-03-04 00:18:34 +01:00
|
|
|
{
|
|
|
|
FAR struct igmp_group_s *group;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
grpinfo("Searching for addr %08x\n", (int)*addr);
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2018-11-03 14:06:30 +01:00
|
|
|
for (group = (FAR struct igmp_group_s *)dev->d_igmp_grplist.head;
|
2012-03-04 00:18:34 +01:00
|
|
|
group;
|
|
|
|
group = group->next)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
grpinfo("Compare: %08x vs. %08x\n", group->grpaddr, *addr);
|
2015-01-16 20:01:08 +01:00
|
|
|
if (net_ipv4addr_cmp(group->grpaddr, *addr))
|
2012-03-04 00:18:34 +01:00
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
grpinfo("Match!\n");
|
2018-11-04 14:43:51 +01:00
|
|
|
DEBUGASSERT(group->ifindex == dev->d_ifindex);
|
2012-03-04 00:18:34 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2012-03-04 00:18:34 +01:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 17:52:36 +02:00
|
|
|
* Name: igmp_grpallocfind
|
2012-03-04 00:18:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find an existing group. If not found, create a new group for the
|
|
|
|
* address.
|
|
|
|
*
|
2018-11-04 14:43:51 +01:00
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
2012-03-04 00:18:34 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
FAR struct igmp_group_s *igmp_grpallocfind(FAR struct net_driver_s *dev,
|
2015-01-16 19:30:18 +01:00
|
|
|
FAR const in_addr_t *addr)
|
2012-03-04 00:18:34 +01:00
|
|
|
{
|
2014-06-25 17:52:36 +02:00
|
|
|
FAR struct igmp_group_s *group = igmp_grpfind(dev, addr);
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
grpinfo("group: %p addr: %08x\n", group, (int)*addr);
|
2012-03-04 00:18:34 +01:00
|
|
|
if (!group)
|
|
|
|
{
|
2014-06-25 17:52:36 +02:00
|
|
|
group = igmp_grpalloc(dev, addr);
|
2012-03-04 00:18:34 +01:00
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
grpinfo("group: %p\n", group);
|
2012-03-04 00:18:34 +01:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 17:52:36 +02:00
|
|
|
* Name: igmp_grpfree
|
2012-03-04 00:18:34 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release a previously allocated group.
|
|
|
|
*
|
2018-11-04 14:43:51 +01:00
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
2012-03-04 00:18:34 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
void igmp_grpfree(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct igmp_group_s *group)
|
2012-03-04 00:18:34 +01:00
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
grpinfo("Free: %p flags: %02x\n", group, group->flags);
|
2012-03-04 00:18:34 +01:00
|
|
|
|
|
|
|
/* Cancel the wdog */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&group->wdog);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-03-04 00:18:34 +01:00
|
|
|
/* Remove the group structure from the group list in the device structure */
|
|
|
|
|
2018-11-03 14:06:30 +01:00
|
|
|
sq_rem((FAR sq_entry_t *)group, &dev->d_igmp_grplist);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2014-04-12 20:13:01 +02:00
|
|
|
/* Destroy the wait semaphore */
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_destroy(&group->sem);
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
/* Cancel the watchdog timer */
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&group->wdog);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2017-09-02 18:27:03 +02:00
|
|
|
/* Then release the group structure resources. */
|
2012-03-04 00:18:34 +01:00
|
|
|
|
2020-04-07 16:31:47 +02:00
|
|
|
grpinfo("Call kmm_free()\n");
|
|
|
|
kmm_free(group);
|
2012-03-04 00:18:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_IGMP */
|