2010-07-08 05:04:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/uip/uip_igmpgroup.c
|
|
|
|
* IGMP group data structure management logic
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2010-07-14 03:57:00 +02:00
|
|
|
#include <nuttx/compiler.h>
|
2010-07-08 05:04:04 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-07-14 03:57:00 +02:00
|
|
|
#include <wdog.h>
|
2010-07-08 05:04:04 +02:00
|
|
|
#include <queue.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
#include <nuttx/arch.h>
|
2010-07-10 03:20:34 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2010-07-08 05:04:04 +02:00
|
|
|
|
|
|
|
#include <net/uip/uip.h>
|
|
|
|
#include <net/uip/uip-igmp.h>
|
|
|
|
|
|
|
|
#include "uip_internal.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IGMP
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-07-14 03:57:00 +02:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
2010-07-08 05:04:04 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
# error "IGMP for IPv6 not supported"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_PREALLOC_IGMPGROUPS
|
|
|
|
# define CONFIG_PREALLOC_IGMPGROUPS 4
|
|
|
|
#endif
|
|
|
|
|
2010-07-14 03:57:00 +02:00
|
|
|
/* Debug ********************************************************************/
|
|
|
|
|
|
|
|
#undef IGMP_GRPDEBUG /* Define to enable detailed IGMP group debug */
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET_IGMP
|
|
|
|
# undef IGMP_GRPDEBUG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_CPP_HAVE_VARARGS
|
|
|
|
# ifdef IGMP_GRPDEBUG
|
|
|
|
# define grpdbg(format, arg...) ndbg(format, ##arg)
|
|
|
|
# define grplldbg(format, arg...) nlldbg(format, ##arg)
|
|
|
|
# define grpvdbg(format, arg...) nvdbg(format, ##arg)
|
|
|
|
# define grpllvdbg(format, arg...) nllvdbg(format, ##arg)
|
|
|
|
# else
|
|
|
|
# define grpdbg(x...)
|
|
|
|
# define grplldbg(x...)
|
|
|
|
# define grpvdbg(x...)
|
|
|
|
# define grpllvdbg(x...)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# ifdef IGMP_GRPDEBUG
|
|
|
|
# define grpdbg ndbg
|
|
|
|
# define grplldbg nlldbg
|
|
|
|
# define grpvdbg nvdbg
|
|
|
|
# define grpllvdbg nllvdbg
|
|
|
|
# else
|
|
|
|
# define grpdbg (void)
|
|
|
|
# define grplldbg (void)
|
|
|
|
# define grpvdbg (void)
|
|
|
|
# define grpllvdbg (void)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2010-07-08 05:04:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* malloc() cannot be called from an interrupt handler. To work around this,
|
|
|
|
* a small number of IGMP groups are preallocated just for use in interrupt
|
|
|
|
* handling logic.
|
|
|
|
*/
|
|
|
|
|
2010-07-14 05:30:43 +02:00
|
|
|
#if CONFIG_PREALLOC_IGMPGROUPS > 0
|
2010-07-08 05:04:04 +02:00
|
|
|
static struct igmp_group_s g_preallocgrps[CONFIG_PREALLOC_IGMPGROUPS];
|
|
|
|
static FAR sq_queue_t g_freelist;
|
2010-07-14 05:30:43 +02:00
|
|
|
#endif
|
2010-07-08 05:04:04 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_grpalloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new group from heap memory.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Calls malloc and so cannot be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline FAR struct igmp_group_s *uip_grpheapalloc(void)
|
|
|
|
{
|
|
|
|
return (FAR struct igmp_group_s *)zalloc(sizeof(struct igmp_group_s));
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_grpprealloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new group from the pre-allocated groups.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function should only be called from an interrupt handler (or with
|
|
|
|
* interrupts disabled).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-07-14 05:30:43 +02:00
|
|
|
#if CONFIG_PREALLOC_IGMPGROUPS > 0
|
2010-07-08 05:04:04 +02:00
|
|
|
static inline FAR struct igmp_group_s *uip_grpprealloc(void)
|
|
|
|
{
|
|
|
|
FAR struct igmp_group_s *group = (FAR struct igmp_group_s *)sq_remfirst(&g_freelist);
|
|
|
|
if (group)
|
|
|
|
{
|
|
|
|
memset(group, 0, sizeof(struct igmp_group_s));
|
|
|
|
group->flags = IGMP_PREALLOCATED;
|
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
2010-07-14 05:30:43 +02:00
|
|
|
#endif
|
2010-07-08 05:04:04 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_grpinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* One-time initialization of group data.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called only during early boot phases (pre-multitasking).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void uip_grpinit(void)
|
|
|
|
{
|
|
|
|
FAR struct igmp_group_s *group;
|
|
|
|
int i;
|
|
|
|
|
2010-07-14 03:57:00 +02:00
|
|
|
grplldbg("Initializing\n");
|
|
|
|
|
2010-07-14 05:30:43 +02:00
|
|
|
#if CONFIG_PREALLOC_IGMPGROUPS > 0
|
2010-07-08 05:04:04 +02:00
|
|
|
for (i = 0; i < CONFIG_PREALLOC_IGMPGROUPS; i++)
|
|
|
|
{
|
|
|
|
group = &g_preallocgrps[i];
|
|
|
|
sq_addfirst((FAR sq_entry_t *)group, &g_freelist);
|
|
|
|
}
|
2010-07-14 05:30:43 +02:00
|
|
|
#endif
|
2010-07-08 05:04:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_grpalloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new group from heap memory.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May be called from either user or interrupt level processing.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-07-11 20:10:22 +02:00
|
|
|
FAR struct igmp_group_s *uip_grpalloc(FAR struct uip_driver_s *dev,
|
|
|
|
FAR const uip_ipaddr_t *addr)
|
2010-07-08 05:04:04 +02:00
|
|
|
{
|
|
|
|
FAR struct igmp_group_s *group;
|
2011-03-15 21:26:34 +01:00
|
|
|
uip_lock_t flags;
|
2010-07-08 05:04:04 +02:00
|
|
|
|
|
|
|
nllvdbg("addr: %08x dev: %p\n", *addr, dev);
|
|
|
|
if (up_interrupt_context())
|
|
|
|
{
|
2010-07-14 19:09:45 +02:00
|
|
|
#if CONFIG_PREALLOC_IGMPGROUPS > 0
|
2010-07-14 05:30:43 +02:00
|
|
|
grplldbg("Use a pre-allocated group entry\n");
|
|
|
|
group = uip_grpprealloc();
|
2010-07-14 19:09:45 +02:00
|
|
|
#else
|
|
|
|
grplldbg("Cannot allocate from interrupt handler\n");
|
|
|
|
group = NULL;
|
|
|
|
#endif
|
2010-07-08 05:04:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-14 05:30:43 +02:00
|
|
|
grplldbg("Allocate from the heap\n");
|
|
|
|
group = uip_grpheapalloc();
|
2010-07-08 05:04:04 +02:00
|
|
|
}
|
2010-07-14 03:57:00 +02:00
|
|
|
grplldbg("group: %p\n", group);
|
2010-07-08 05:04:04 +02:00
|
|
|
|
|
|
|
/* Check if we succesfully allocated a group structure */
|
|
|
|
|
|
|
|
if (group)
|
|
|
|
{
|
|
|
|
/* Initialize the non-zero elements of the group structure */
|
|
|
|
|
2010-07-13 05:43:06 +02:00
|
|
|
uip_ipaddr_copy(group->grpaddr, *addr);
|
2010-07-10 19:02:52 +02:00
|
|
|
sem_init(&group->sem, 0, 0);
|
2010-07-08 05:04:04 +02:00
|
|
|
|
2010-07-14 03:57:00 +02:00
|
|
|
/* Initialize the group timer (but don't start it yet) */
|
|
|
|
|
|
|
|
group->wdog = wd_create();
|
|
|
|
DEBUGASSERT(group->wdog);
|
|
|
|
|
2010-07-08 05:04:04 +02:00
|
|
|
/* Interrupts must be disabled in order to modify the group list */
|
|
|
|
|
2011-03-15 21:26:34 +01:00
|
|
|
flags = uip_lock();
|
2010-07-08 05:04:04 +02:00
|
|
|
|
|
|
|
/* Add the group structure to the list in the device structure */
|
|
|
|
|
|
|
|
sq_addfirst((FAR sq_entry_t*)group, &dev->grplist);
|
2011-03-15 21:26:34 +01:00
|
|
|
uip_unlock(flags);
|
2010-07-08 05:04:04 +02:00
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_grpfind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find an existing group.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May be called from either user or interrupt level processing.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-07-11 20:10:22 +02:00
|
|
|
FAR struct igmp_group_s *uip_grpfind(FAR struct uip_driver_s *dev,
|
|
|
|
FAR const uip_ipaddr_t *addr)
|
2010-07-08 05:04:04 +02:00
|
|
|
{
|
|
|
|
FAR struct igmp_group_s *group;
|
2011-03-15 21:26:34 +01:00
|
|
|
uip_lock_t flags;
|
2010-07-08 05:04:04 +02:00
|
|
|
|
2010-07-14 03:57:00 +02:00
|
|
|
grplldbg("Searching for addr %08x\n", (int)*addr);
|
|
|
|
|
|
|
|
/* We must disable interrupts because we don't which context we were
|
|
|
|
* called from.
|
2010-07-08 05:04:04 +02:00
|
|
|
*/
|
|
|
|
|
2011-03-15 21:26:34 +01:00
|
|
|
flags = uip_lock();
|
2010-07-14 03:57:00 +02:00
|
|
|
for (group = (FAR struct igmp_group_s *)dev->grplist.head;
|
|
|
|
group;
|
|
|
|
group = group->next)
|
2010-07-08 05:04:04 +02:00
|
|
|
{
|
2010-07-14 03:57:00 +02:00
|
|
|
grplldbg("Compare: %08x vs. %08x\n", group->grpaddr, *addr);
|
|
|
|
if (uip_ipaddr_cmp(group->grpaddr, *addr))
|
2010-07-08 05:04:04 +02:00
|
|
|
{
|
2010-07-14 03:57:00 +02:00
|
|
|
grplldbg("Match!\n");
|
2010-07-08 05:04:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-03-15 21:26:34 +01:00
|
|
|
uip_unlock(flags);
|
2010-07-08 05:04:04 +02:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_grpallocfind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find an existing group. If not found, create a new group for the
|
|
|
|
* address.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May be called from either user or interrupt level processing.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-07-11 20:10:22 +02:00
|
|
|
FAR struct igmp_group_s *uip_grpallocfind(FAR struct uip_driver_s *dev,
|
|
|
|
FAR const uip_ipaddr_t *addr)
|
2010-07-08 05:04:04 +02:00
|
|
|
{
|
|
|
|
FAR struct igmp_group_s *group = uip_grpfind(dev, addr);
|
2010-07-14 03:57:00 +02:00
|
|
|
|
|
|
|
grplldbg("group: %p addr: %08x\n", group, (int)*addr);
|
2010-07-08 05:04:04 +02:00
|
|
|
if (!group)
|
|
|
|
{
|
|
|
|
group = uip_grpalloc(dev, addr);
|
|
|
|
}
|
2010-07-14 03:57:00 +02:00
|
|
|
grplldbg("group: %p\n", group);
|
2010-07-08 05:04:04 +02:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_grpfree
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release a previously allocated group.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May be called from either user or interrupt level processing.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void uip_grpfree(FAR struct uip_driver_s *dev, FAR struct igmp_group_s *group)
|
|
|
|
{
|
2011-03-15 21:26:34 +01:00
|
|
|
uip_lock_t flags;
|
2010-07-14 03:57:00 +02:00
|
|
|
|
|
|
|
grplldbg("Free: %p flags: %02x\n", group, group->flags);
|
|
|
|
|
|
|
|
/* Cancel the wdog */
|
|
|
|
|
2011-03-15 21:26:34 +01:00
|
|
|
flags = uip_lock();
|
2010-07-14 03:57:00 +02:00
|
|
|
wd_cancel(group->wdog);
|
|
|
|
|
|
|
|
/* Remove the group structure from the group list in the device structure */
|
2010-07-08 05:04:04 +02:00
|
|
|
|
2010-07-10 21:47:37 +02:00
|
|
|
sq_rem((FAR sq_entry_t*)group, &dev->grplist);
|
2010-07-10 19:02:52 +02:00
|
|
|
|
|
|
|
/* Destroy the wait semapore */
|
|
|
|
|
|
|
|
(void)sem_destroy(&group->sem);
|
2010-07-14 03:57:00 +02:00
|
|
|
|
|
|
|
/* Destroy the wdog */
|
|
|
|
|
|
|
|
wd_delete(group->wdog);
|
2010-07-10 19:02:52 +02:00
|
|
|
|
|
|
|
/* Then release the group structure resources. Check first if this is one
|
|
|
|
* of the pre-allocated group structures that we will retain in a free list.
|
|
|
|
*/
|
|
|
|
|
2010-07-14 05:30:43 +02:00
|
|
|
#if CONFIG_PREALLOC_IGMPGROUPS > 0
|
2010-07-08 05:04:04 +02:00
|
|
|
if (IS_PREALLOCATED(group->flags))
|
|
|
|
{
|
2010-07-14 03:57:00 +02:00
|
|
|
grplldbg("Put back on free list\n");
|
2010-07-08 05:04:04 +02:00
|
|
|
sq_addlast((FAR sq_entry_t*)group, &g_freelist);
|
2011-03-15 21:26:34 +01:00
|
|
|
uip_unlock(flags);
|
2010-07-08 05:04:04 +02:00
|
|
|
}
|
|
|
|
else
|
2010-07-14 05:30:43 +02:00
|
|
|
#endif
|
2010-07-08 05:04:04 +02:00
|
|
|
{
|
2010-07-10 19:02:52 +02:00
|
|
|
/* No.. deallocate the group structure. Use sched_free() just in case
|
|
|
|
* this function is executing within an interrupt handler.
|
|
|
|
*/
|
|
|
|
|
2011-03-15 21:26:34 +01:00
|
|
|
uip_unlock(flags);
|
2010-07-14 03:57:00 +02:00
|
|
|
grplldbg("Call sched_free()\n");
|
2010-07-10 03:20:34 +02:00
|
|
|
sched_free(group);
|
2010-07-08 05:04:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_IGMP */
|