SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lorcansmith 0:2a53a4c3238c 1 /**
lorcansmith 0:2a53a4c3238c 2 * @file
lorcansmith 0:2a53a4c3238c 3 * IGMP - Internet Group Management Protocol
lorcansmith 0:2a53a4c3238c 4 *
lorcansmith 0:2a53a4c3238c 5 */
lorcansmith 0:2a53a4c3238c 6
lorcansmith 0:2a53a4c3238c 7 /*
lorcansmith 0:2a53a4c3238c 8 * Copyright (c) 2002 CITEL Technologies Ltd.
lorcansmith 0:2a53a4c3238c 9 * All rights reserved.
lorcansmith 0:2a53a4c3238c 10 *
lorcansmith 0:2a53a4c3238c 11 * Redistribution and use in source and binary forms, with or without
lorcansmith 0:2a53a4c3238c 12 * modification, are permitted provided that the following conditions
lorcansmith 0:2a53a4c3238c 13 * are met:
lorcansmith 0:2a53a4c3238c 14 * 1. Redistributions of source code must retain the above copyright
lorcansmith 0:2a53a4c3238c 15 * notice, this list of conditions and the following disclaimer.
lorcansmith 0:2a53a4c3238c 16 * 2. Redistributions in binary form must reproduce the above copyright
lorcansmith 0:2a53a4c3238c 17 * notice, this list of conditions and the following disclaimer in the
lorcansmith 0:2a53a4c3238c 18 * documentation and/or other materials provided with the distribution.
lorcansmith 0:2a53a4c3238c 19 * 3. Neither the name of CITEL Technologies Ltd nor the names of its contributors
lorcansmith 0:2a53a4c3238c 20 * may be used to endorse or promote products derived from this software
lorcansmith 0:2a53a4c3238c 21 * without specific prior written permission.
lorcansmith 0:2a53a4c3238c 22 *
lorcansmith 0:2a53a4c3238c 23 * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
lorcansmith 0:2a53a4c3238c 24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
lorcansmith 0:2a53a4c3238c 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
lorcansmith 0:2a53a4c3238c 26 * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE LIABLE
lorcansmith 0:2a53a4c3238c 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
lorcansmith 0:2a53a4c3238c 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
lorcansmith 0:2a53a4c3238c 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
lorcansmith 0:2a53a4c3238c 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
lorcansmith 0:2a53a4c3238c 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
lorcansmith 0:2a53a4c3238c 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
lorcansmith 0:2a53a4c3238c 33 * SUCH DAMAGE.
lorcansmith 0:2a53a4c3238c 34 *
lorcansmith 0:2a53a4c3238c 35 * This file is a contribution to the lwIP TCP/IP stack.
lorcansmith 0:2a53a4c3238c 36 * The Swedish Institute of Computer Science and Adam Dunkels
lorcansmith 0:2a53a4c3238c 37 * are specifically granted permission to redistribute this
lorcansmith 0:2a53a4c3238c 38 * source code.
lorcansmith 0:2a53a4c3238c 39 */
lorcansmith 0:2a53a4c3238c 40
lorcansmith 0:2a53a4c3238c 41 /*-------------------------------------------------------------
lorcansmith 0:2a53a4c3238c 42 Note 1)
lorcansmith 0:2a53a4c3238c 43 Although the rfc requires V1 AND V2 capability
lorcansmith 0:2a53a4c3238c 44 we will only support v2 since now V1 is very old (August 1989)
lorcansmith 0:2a53a4c3238c 45 V1 can be added if required
lorcansmith 0:2a53a4c3238c 46
lorcansmith 0:2a53a4c3238c 47 a debug print and statistic have been implemented to
lorcansmith 0:2a53a4c3238c 48 show this up.
lorcansmith 0:2a53a4c3238c 49 -------------------------------------------------------------
lorcansmith 0:2a53a4c3238c 50 -------------------------------------------------------------
lorcansmith 0:2a53a4c3238c 51 Note 2)
lorcansmith 0:2a53a4c3238c 52 A query for a specific group address (as opposed to ALLHOSTS)
lorcansmith 0:2a53a4c3238c 53 has now been implemented as I am unsure if it is required
lorcansmith 0:2a53a4c3238c 54
lorcansmith 0:2a53a4c3238c 55 a debug print and statistic have been implemented to
lorcansmith 0:2a53a4c3238c 56 show this up.
lorcansmith 0:2a53a4c3238c 57 -------------------------------------------------------------
lorcansmith 0:2a53a4c3238c 58 -------------------------------------------------------------
lorcansmith 0:2a53a4c3238c 59 Note 3)
lorcansmith 0:2a53a4c3238c 60 The router alert rfc 2113 is implemented in outgoing packets
lorcansmith 0:2a53a4c3238c 61 but not checked rigorously incoming
lorcansmith 0:2a53a4c3238c 62 -------------------------------------------------------------
lorcansmith 0:2a53a4c3238c 63 Steve Reynolds
lorcansmith 0:2a53a4c3238c 64 ------------------------------------------------------------*/
lorcansmith 0:2a53a4c3238c 65
lorcansmith 0:2a53a4c3238c 66 /*-----------------------------------------------------------------------------
lorcansmith 0:2a53a4c3238c 67 * RFC 988 - Host extensions for IP multicasting - V0
lorcansmith 0:2a53a4c3238c 68 * RFC 1054 - Host extensions for IP multicasting -
lorcansmith 0:2a53a4c3238c 69 * RFC 1112 - Host extensions for IP multicasting - V1
lorcansmith 0:2a53a4c3238c 70 * RFC 2236 - Internet Group Management Protocol, Version 2 - V2 <- this code is based on this RFC (it's the "de facto" standard)
lorcansmith 0:2a53a4c3238c 71 * RFC 3376 - Internet Group Management Protocol, Version 3 - V3
lorcansmith 0:2a53a4c3238c 72 * RFC 4604 - Using Internet Group Management Protocol Version 3... - V3+
lorcansmith 0:2a53a4c3238c 73 * RFC 2113 - IP Router Alert Option -
lorcansmith 0:2a53a4c3238c 74 *----------------------------------------------------------------------------*/
lorcansmith 0:2a53a4c3238c 75
lorcansmith 0:2a53a4c3238c 76 /*-----------------------------------------------------------------------------
lorcansmith 0:2a53a4c3238c 77 * Includes
lorcansmith 0:2a53a4c3238c 78 *----------------------------------------------------------------------------*/
lorcansmith 0:2a53a4c3238c 79
lorcansmith 0:2a53a4c3238c 80 #include "lwip/opt.h"
lorcansmith 0:2a53a4c3238c 81
lorcansmith 0:2a53a4c3238c 82 #if LWIP_IGMP /* don't build if not configured for use in lwipopts.h */
lorcansmith 0:2a53a4c3238c 83
lorcansmith 0:2a53a4c3238c 84 #include "lwip/igmp.h"
lorcansmith 0:2a53a4c3238c 85 #include "lwip/debug.h"
lorcansmith 0:2a53a4c3238c 86 #include "lwip/def.h"
lorcansmith 0:2a53a4c3238c 87 #include "lwip/mem.h"
lorcansmith 0:2a53a4c3238c 88 #include "lwip/ip.h"
lorcansmith 0:2a53a4c3238c 89 #include "lwip/inet_chksum.h"
lorcansmith 0:2a53a4c3238c 90 #include "lwip/netif.h"
lorcansmith 0:2a53a4c3238c 91 #include "lwip/icmp.h"
lorcansmith 0:2a53a4c3238c 92 #include "lwip/udp.h"
lorcansmith 0:2a53a4c3238c 93 #include "lwip/tcp.h"
lorcansmith 0:2a53a4c3238c 94 #include "lwip/stats.h"
lorcansmith 0:2a53a4c3238c 95
lorcansmith 0:2a53a4c3238c 96 #include "string.h"
lorcansmith 0:2a53a4c3238c 97
lorcansmith 0:2a53a4c3238c 98 /*
lorcansmith 0:2a53a4c3238c 99 * IGMP constants
lorcansmith 0:2a53a4c3238c 100 */
lorcansmith 0:2a53a4c3238c 101 #define IGMP_TTL 1
lorcansmith 0:2a53a4c3238c 102 #define IGMP_MINLEN 8
lorcansmith 0:2a53a4c3238c 103 #define ROUTER_ALERT 0x9404
lorcansmith 0:2a53a4c3238c 104 #define ROUTER_ALERTLEN 4
lorcansmith 0:2a53a4c3238c 105
lorcansmith 0:2a53a4c3238c 106 /*
lorcansmith 0:2a53a4c3238c 107 * IGMP message types, including version number.
lorcansmith 0:2a53a4c3238c 108 */
lorcansmith 0:2a53a4c3238c 109 #define IGMP_MEMB_QUERY 0x11 /* Membership query */
lorcansmith 0:2a53a4c3238c 110 #define IGMP_V1_MEMB_REPORT 0x12 /* Ver. 1 membership report */
lorcansmith 0:2a53a4c3238c 111 #define IGMP_V2_MEMB_REPORT 0x16 /* Ver. 2 membership report */
lorcansmith 0:2a53a4c3238c 112 #define IGMP_LEAVE_GROUP 0x17 /* Leave-group message */
lorcansmith 0:2a53a4c3238c 113
lorcansmith 0:2a53a4c3238c 114 /* Group membership states */
lorcansmith 0:2a53a4c3238c 115 #define IGMP_GROUP_NON_MEMBER 0
lorcansmith 0:2a53a4c3238c 116 #define IGMP_GROUP_DELAYING_MEMBER 1
lorcansmith 0:2a53a4c3238c 117 #define IGMP_GROUP_IDLE_MEMBER 2
lorcansmith 0:2a53a4c3238c 118
lorcansmith 0:2a53a4c3238c 119 /**
lorcansmith 0:2a53a4c3238c 120 * IGMP packet format.
lorcansmith 0:2a53a4c3238c 121 */
lorcansmith 0:2a53a4c3238c 122 #ifdef PACK_STRUCT_USE_INCLUDES
lorcansmith 0:2a53a4c3238c 123 # include "arch/bpstruct.h"
lorcansmith 0:2a53a4c3238c 124 #endif
lorcansmith 0:2a53a4c3238c 125 PACK_STRUCT_BEGIN
lorcansmith 0:2a53a4c3238c 126 struct igmp_msg {
lorcansmith 0:2a53a4c3238c 127 PACK_STRUCT_FIELD(u8_t igmp_msgtype);
lorcansmith 0:2a53a4c3238c 128 PACK_STRUCT_FIELD(u8_t igmp_maxresp);
lorcansmith 0:2a53a4c3238c 129 PACK_STRUCT_FIELD(u16_t igmp_checksum);
lorcansmith 0:2a53a4c3238c 130 PACK_STRUCT_FIELD(ip_addr_p_t igmp_group_address);
lorcansmith 0:2a53a4c3238c 131 } PACK_STRUCT_STRUCT;
lorcansmith 0:2a53a4c3238c 132 PACK_STRUCT_END
lorcansmith 0:2a53a4c3238c 133 #ifdef PACK_STRUCT_USE_INCLUDES
lorcansmith 0:2a53a4c3238c 134 # include "arch/epstruct.h"
lorcansmith 0:2a53a4c3238c 135 #endif
lorcansmith 0:2a53a4c3238c 136
lorcansmith 0:2a53a4c3238c 137
lorcansmith 0:2a53a4c3238c 138 static struct igmp_group *igmp_lookup_group(struct netif *ifp, ip_addr_t *addr);
lorcansmith 0:2a53a4c3238c 139 static err_t igmp_remove_group(struct igmp_group *group);
lorcansmith 0:2a53a4c3238c 140 static void igmp_timeout( struct igmp_group *group);
lorcansmith 0:2a53a4c3238c 141 static void igmp_start_timer(struct igmp_group *group, u8_t max_time);
lorcansmith 0:2a53a4c3238c 142 static void igmp_stop_timer(struct igmp_group *group);
lorcansmith 0:2a53a4c3238c 143 static void igmp_delaying_member(struct igmp_group *group, u8_t maxresp);
lorcansmith 0:2a53a4c3238c 144 static err_t igmp_ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, struct netif *netif);
lorcansmith 0:2a53a4c3238c 145 static void igmp_send(struct igmp_group *group, u8_t type);
lorcansmith 0:2a53a4c3238c 146
lorcansmith 0:2a53a4c3238c 147
lorcansmith 0:2a53a4c3238c 148 static struct igmp_group* igmp_group_list;
lorcansmith 0:2a53a4c3238c 149 static ip_addr_t allsystems;
lorcansmith 0:2a53a4c3238c 150 static ip_addr_t allrouters;
lorcansmith 0:2a53a4c3238c 151
lorcansmith 0:2a53a4c3238c 152
lorcansmith 0:2a53a4c3238c 153 /**
lorcansmith 0:2a53a4c3238c 154 * Initialize the IGMP module
lorcansmith 0:2a53a4c3238c 155 */
lorcansmith 0:2a53a4c3238c 156 void
lorcansmith 0:2a53a4c3238c 157 igmp_init(void)
lorcansmith 0:2a53a4c3238c 158 {
lorcansmith 0:2a53a4c3238c 159 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_init: initializing\n"));
lorcansmith 0:2a53a4c3238c 160
lorcansmith 0:2a53a4c3238c 161 IP4_ADDR(&allsystems, 224, 0, 0, 1);
lorcansmith 0:2a53a4c3238c 162 IP4_ADDR(&allrouters, 224, 0, 0, 2);
lorcansmith 0:2a53a4c3238c 163 }
lorcansmith 0:2a53a4c3238c 164
lorcansmith 0:2a53a4c3238c 165 #ifdef LWIP_DEBUG
lorcansmith 0:2a53a4c3238c 166 /**
lorcansmith 0:2a53a4c3238c 167 * Dump global IGMP groups list
lorcansmith 0:2a53a4c3238c 168 */
lorcansmith 0:2a53a4c3238c 169 void
lorcansmith 0:2a53a4c3238c 170 igmp_dump_group_list()
lorcansmith 0:2a53a4c3238c 171 {
lorcansmith 0:2a53a4c3238c 172 struct igmp_group *group = igmp_group_list;
lorcansmith 0:2a53a4c3238c 173
lorcansmith 0:2a53a4c3238c 174 while (group != NULL) {
lorcansmith 0:2a53a4c3238c 175 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_dump_group_list: [%"U32_F"] ", (u32_t)(group->group_state)));
lorcansmith 0:2a53a4c3238c 176 ip_addr_debug_print(IGMP_DEBUG, &group->group_address);
lorcansmith 0:2a53a4c3238c 177 LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", group->netif));
lorcansmith 0:2a53a4c3238c 178 group = group->next;
lorcansmith 0:2a53a4c3238c 179 }
lorcansmith 0:2a53a4c3238c 180 LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
lorcansmith 0:2a53a4c3238c 181 }
lorcansmith 0:2a53a4c3238c 182 #else
lorcansmith 0:2a53a4c3238c 183 #define igmp_dump_group_list()
lorcansmith 0:2a53a4c3238c 184 #endif /* LWIP_DEBUG */
lorcansmith 0:2a53a4c3238c 185
lorcansmith 0:2a53a4c3238c 186 /**
lorcansmith 0:2a53a4c3238c 187 * Start IGMP processing on interface
lorcansmith 0:2a53a4c3238c 188 *
lorcansmith 0:2a53a4c3238c 189 * @param netif network interface on which start IGMP processing
lorcansmith 0:2a53a4c3238c 190 */
lorcansmith 0:2a53a4c3238c 191 err_t
lorcansmith 0:2a53a4c3238c 192 igmp_start(struct netif *netif)
lorcansmith 0:2a53a4c3238c 193 {
lorcansmith 0:2a53a4c3238c 194 struct igmp_group* group;
lorcansmith 0:2a53a4c3238c 195
lorcansmith 0:2a53a4c3238c 196 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_start: starting IGMP processing on if %p\n", netif));
lorcansmith 0:2a53a4c3238c 197
lorcansmith 0:2a53a4c3238c 198 group = igmp_lookup_group(netif, &allsystems);
lorcansmith 0:2a53a4c3238c 199
lorcansmith 0:2a53a4c3238c 200 if (group != NULL) {
lorcansmith 0:2a53a4c3238c 201 group->group_state = IGMP_GROUP_IDLE_MEMBER;
lorcansmith 0:2a53a4c3238c 202 group->use++;
lorcansmith 0:2a53a4c3238c 203
lorcansmith 0:2a53a4c3238c 204 /* Allow the igmp messages at the MAC level */
lorcansmith 0:2a53a4c3238c 205 if (netif->igmp_mac_filter != NULL) {
lorcansmith 0:2a53a4c3238c 206 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_start: igmp_mac_filter(ADD "));
lorcansmith 0:2a53a4c3238c 207 ip_addr_debug_print(IGMP_DEBUG, &allsystems);
lorcansmith 0:2a53a4c3238c 208 LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
lorcansmith 0:2a53a4c3238c 209 netif->igmp_mac_filter(netif, &allsystems, IGMP_ADD_MAC_FILTER);
lorcansmith 0:2a53a4c3238c 210 }
lorcansmith 0:2a53a4c3238c 211
lorcansmith 0:2a53a4c3238c 212 return ERR_OK;
lorcansmith 0:2a53a4c3238c 213 }
lorcansmith 0:2a53a4c3238c 214
lorcansmith 0:2a53a4c3238c 215 return ERR_MEM;
lorcansmith 0:2a53a4c3238c 216 }
lorcansmith 0:2a53a4c3238c 217
lorcansmith 0:2a53a4c3238c 218 /**
lorcansmith 0:2a53a4c3238c 219 * Stop IGMP processing on interface
lorcansmith 0:2a53a4c3238c 220 *
lorcansmith 0:2a53a4c3238c 221 * @param netif network interface on which stop IGMP processing
lorcansmith 0:2a53a4c3238c 222 */
lorcansmith 0:2a53a4c3238c 223 err_t
lorcansmith 0:2a53a4c3238c 224 igmp_stop(struct netif *netif)
lorcansmith 0:2a53a4c3238c 225 {
lorcansmith 0:2a53a4c3238c 226 struct igmp_group *group = igmp_group_list;
lorcansmith 0:2a53a4c3238c 227 struct igmp_group *prev = NULL;
lorcansmith 0:2a53a4c3238c 228 struct igmp_group *next;
lorcansmith 0:2a53a4c3238c 229
lorcansmith 0:2a53a4c3238c 230 /* look for groups joined on this interface further down the list */
lorcansmith 0:2a53a4c3238c 231 while (group != NULL) {
lorcansmith 0:2a53a4c3238c 232 next = group->next;
lorcansmith 0:2a53a4c3238c 233 /* is it a group joined on this interface? */
lorcansmith 0:2a53a4c3238c 234 if (group->netif == netif) {
lorcansmith 0:2a53a4c3238c 235 /* is it the first group of the list? */
lorcansmith 0:2a53a4c3238c 236 if (group == igmp_group_list) {
lorcansmith 0:2a53a4c3238c 237 igmp_group_list = next;
lorcansmith 0:2a53a4c3238c 238 }
lorcansmith 0:2a53a4c3238c 239 /* is there a "previous" group defined? */
lorcansmith 0:2a53a4c3238c 240 if (prev != NULL) {
lorcansmith 0:2a53a4c3238c 241 prev->next = next;
lorcansmith 0:2a53a4c3238c 242 }
lorcansmith 0:2a53a4c3238c 243 /* disable the group at the MAC level */
lorcansmith 0:2a53a4c3238c 244 if (netif->igmp_mac_filter != NULL) {
lorcansmith 0:2a53a4c3238c 245 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_stop: igmp_mac_filter(DEL "));
lorcansmith 0:2a53a4c3238c 246 ip_addr_debug_print(IGMP_DEBUG, &group->group_address);
lorcansmith 0:2a53a4c3238c 247 LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
lorcansmith 0:2a53a4c3238c 248 netif->igmp_mac_filter(netif, &(group->group_address), IGMP_DEL_MAC_FILTER);
lorcansmith 0:2a53a4c3238c 249 }
lorcansmith 0:2a53a4c3238c 250 /* free group */
lorcansmith 0:2a53a4c3238c 251 memp_free(MEMP_IGMP_GROUP, group);
lorcansmith 0:2a53a4c3238c 252 } else {
lorcansmith 0:2a53a4c3238c 253 /* change the "previous" */
lorcansmith 0:2a53a4c3238c 254 prev = group;
lorcansmith 0:2a53a4c3238c 255 }
lorcansmith 0:2a53a4c3238c 256 /* move to "next" */
lorcansmith 0:2a53a4c3238c 257 group = next;
lorcansmith 0:2a53a4c3238c 258 }
lorcansmith 0:2a53a4c3238c 259 return ERR_OK;
lorcansmith 0:2a53a4c3238c 260 }
lorcansmith 0:2a53a4c3238c 261
lorcansmith 0:2a53a4c3238c 262 /**
lorcansmith 0:2a53a4c3238c 263 * Report IGMP memberships for this interface
lorcansmith 0:2a53a4c3238c 264 *
lorcansmith 0:2a53a4c3238c 265 * @param netif network interface on which report IGMP memberships
lorcansmith 0:2a53a4c3238c 266 */
lorcansmith 0:2a53a4c3238c 267 void
lorcansmith 0:2a53a4c3238c 268 igmp_report_groups(struct netif *netif)
lorcansmith 0:2a53a4c3238c 269 {
lorcansmith 0:2a53a4c3238c 270 struct igmp_group *group = igmp_group_list;
lorcansmith 0:2a53a4c3238c 271
lorcansmith 0:2a53a4c3238c 272 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_report_groups: sending IGMP reports on if %p\n", netif));
lorcansmith 0:2a53a4c3238c 273
lorcansmith 0:2a53a4c3238c 274 while (group != NULL) {
lorcansmith 0:2a53a4c3238c 275 if (group->netif == netif) {
lorcansmith 0:2a53a4c3238c 276 igmp_delaying_member(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
lorcansmith 0:2a53a4c3238c 277 }
lorcansmith 0:2a53a4c3238c 278 group = group->next;
lorcansmith 0:2a53a4c3238c 279 }
lorcansmith 0:2a53a4c3238c 280 }
lorcansmith 0:2a53a4c3238c 281
lorcansmith 0:2a53a4c3238c 282 /**
lorcansmith 0:2a53a4c3238c 283 * Search for a group in the global igmp_group_list
lorcansmith 0:2a53a4c3238c 284 *
lorcansmith 0:2a53a4c3238c 285 * @param ifp the network interface for which to look
lorcansmith 0:2a53a4c3238c 286 * @param addr the group ip address to search for
lorcansmith 0:2a53a4c3238c 287 * @return a struct igmp_group* if the group has been found,
lorcansmith 0:2a53a4c3238c 288 * NULL if the group wasn't found.
lorcansmith 0:2a53a4c3238c 289 */
lorcansmith 0:2a53a4c3238c 290 struct igmp_group *
lorcansmith 0:2a53a4c3238c 291 igmp_lookfor_group(struct netif *ifp, ip_addr_t *addr)
lorcansmith 0:2a53a4c3238c 292 {
lorcansmith 0:2a53a4c3238c 293 struct igmp_group *group = igmp_group_list;
lorcansmith 0:2a53a4c3238c 294
lorcansmith 0:2a53a4c3238c 295 while (group != NULL) {
lorcansmith 0:2a53a4c3238c 296 if ((group->netif == ifp) && (ip_addr_cmp(&(group->group_address), addr))) {
lorcansmith 0:2a53a4c3238c 297 return group;
lorcansmith 0:2a53a4c3238c 298 }
lorcansmith 0:2a53a4c3238c 299 group = group->next;
lorcansmith 0:2a53a4c3238c 300 }
lorcansmith 0:2a53a4c3238c 301
lorcansmith 0:2a53a4c3238c 302 /* to be clearer, we return NULL here instead of
lorcansmith 0:2a53a4c3238c 303 * 'group' (which is also NULL at this point).
lorcansmith 0:2a53a4c3238c 304 */
lorcansmith 0:2a53a4c3238c 305 return NULL;
lorcansmith 0:2a53a4c3238c 306 }
lorcansmith 0:2a53a4c3238c 307
lorcansmith 0:2a53a4c3238c 308 /**
lorcansmith 0:2a53a4c3238c 309 * Search for a specific igmp group and create a new one if not found-
lorcansmith 0:2a53a4c3238c 310 *
lorcansmith 0:2a53a4c3238c 311 * @param ifp the network interface for which to look
lorcansmith 0:2a53a4c3238c 312 * @param addr the group ip address to search
lorcansmith 0:2a53a4c3238c 313 * @return a struct igmp_group*,
lorcansmith 0:2a53a4c3238c 314 * NULL on memory error.
lorcansmith 0:2a53a4c3238c 315 */
lorcansmith 0:2a53a4c3238c 316 struct igmp_group *
lorcansmith 0:2a53a4c3238c 317 igmp_lookup_group(struct netif *ifp, ip_addr_t *addr)
lorcansmith 0:2a53a4c3238c 318 {
lorcansmith 0:2a53a4c3238c 319 struct igmp_group *group = igmp_group_list;
lorcansmith 0:2a53a4c3238c 320
lorcansmith 0:2a53a4c3238c 321 /* Search if the group already exists */
lorcansmith 0:2a53a4c3238c 322 group = igmp_lookfor_group(ifp, addr);
lorcansmith 0:2a53a4c3238c 323 if (group != NULL) {
lorcansmith 0:2a53a4c3238c 324 /* Group already exists. */
lorcansmith 0:2a53a4c3238c 325 return group;
lorcansmith 0:2a53a4c3238c 326 }
lorcansmith 0:2a53a4c3238c 327
lorcansmith 0:2a53a4c3238c 328 /* Group doesn't exist yet, create a new one */
lorcansmith 0:2a53a4c3238c 329 group = (struct igmp_group *)memp_malloc(MEMP_IGMP_GROUP);
lorcansmith 0:2a53a4c3238c 330 if (group != NULL) {
lorcansmith 0:2a53a4c3238c 331 group->netif = ifp;
lorcansmith 0:2a53a4c3238c 332 ip_addr_set(&(group->group_address), addr);
lorcansmith 0:2a53a4c3238c 333 group->timer = 0; /* Not running */
lorcansmith 0:2a53a4c3238c 334 group->group_state = IGMP_GROUP_NON_MEMBER;
lorcansmith 0:2a53a4c3238c 335 group->last_reporter_flag = 0;
lorcansmith 0:2a53a4c3238c 336 group->use = 0;
lorcansmith 0:2a53a4c3238c 337 group->next = igmp_group_list;
lorcansmith 0:2a53a4c3238c 338
lorcansmith 0:2a53a4c3238c 339 igmp_group_list = group;
lorcansmith 0:2a53a4c3238c 340 }
lorcansmith 0:2a53a4c3238c 341
lorcansmith 0:2a53a4c3238c 342 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_lookup_group: %sallocated a new group with address ", (group?"":"impossible to ")));
lorcansmith 0:2a53a4c3238c 343 ip_addr_debug_print(IGMP_DEBUG, addr);
lorcansmith 0:2a53a4c3238c 344 LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", ifp));
lorcansmith 0:2a53a4c3238c 345
lorcansmith 0:2a53a4c3238c 346 return group;
lorcansmith 0:2a53a4c3238c 347 }
lorcansmith 0:2a53a4c3238c 348
lorcansmith 0:2a53a4c3238c 349 /**
lorcansmith 0:2a53a4c3238c 350 * Remove a group in the global igmp_group_list
lorcansmith 0:2a53a4c3238c 351 *
lorcansmith 0:2a53a4c3238c 352 * @param group the group to remove from the global igmp_group_list
lorcansmith 0:2a53a4c3238c 353 * @return ERR_OK if group was removed from the list, an err_t otherwise
lorcansmith 0:2a53a4c3238c 354 */
lorcansmith 0:2a53a4c3238c 355 static err_t
lorcansmith 0:2a53a4c3238c 356 igmp_remove_group(struct igmp_group *group)
lorcansmith 0:2a53a4c3238c 357 {
lorcansmith 0:2a53a4c3238c 358 err_t err = ERR_OK;
lorcansmith 0:2a53a4c3238c 359
lorcansmith 0:2a53a4c3238c 360 /* Is it the first group? */
lorcansmith 0:2a53a4c3238c 361 if (igmp_group_list == group) {
lorcansmith 0:2a53a4c3238c 362 igmp_group_list = group->next;
lorcansmith 0:2a53a4c3238c 363 } else {
lorcansmith 0:2a53a4c3238c 364 /* look for group further down the list */
lorcansmith 0:2a53a4c3238c 365 struct igmp_group *tmpGroup;
lorcansmith 0:2a53a4c3238c 366 for (tmpGroup = igmp_group_list; tmpGroup != NULL; tmpGroup = tmpGroup->next) {
lorcansmith 0:2a53a4c3238c 367 if (tmpGroup->next == group) {
lorcansmith 0:2a53a4c3238c 368 tmpGroup->next = group->next;
lorcansmith 0:2a53a4c3238c 369 break;
lorcansmith 0:2a53a4c3238c 370 }
lorcansmith 0:2a53a4c3238c 371 }
lorcansmith 0:2a53a4c3238c 372 /* Group not found in the global igmp_group_list */
lorcansmith 0:2a53a4c3238c 373 if (tmpGroup == NULL)
lorcansmith 0:2a53a4c3238c 374 err = ERR_ARG;
lorcansmith 0:2a53a4c3238c 375 }
lorcansmith 0:2a53a4c3238c 376 /* free group */
lorcansmith 0:2a53a4c3238c 377 memp_free(MEMP_IGMP_GROUP, group);
lorcansmith 0:2a53a4c3238c 378
lorcansmith 0:2a53a4c3238c 379 return err;
lorcansmith 0:2a53a4c3238c 380 }
lorcansmith 0:2a53a4c3238c 381
lorcansmith 0:2a53a4c3238c 382 /**
lorcansmith 0:2a53a4c3238c 383 * Called from ip_input() if a new IGMP packet is received.
lorcansmith 0:2a53a4c3238c 384 *
lorcansmith 0:2a53a4c3238c 385 * @param p received igmp packet, p->payload pointing to the ip header
lorcansmith 0:2a53a4c3238c 386 * @param inp network interface on which the packet was received
lorcansmith 0:2a53a4c3238c 387 * @param dest destination ip address of the igmp packet
lorcansmith 0:2a53a4c3238c 388 */
lorcansmith 0:2a53a4c3238c 389 void
lorcansmith 0:2a53a4c3238c 390 igmp_input(struct pbuf *p, struct netif *inp, ip_addr_t *dest)
lorcansmith 0:2a53a4c3238c 391 {
lorcansmith 0:2a53a4c3238c 392 struct ip_hdr * iphdr;
lorcansmith 0:2a53a4c3238c 393 struct igmp_msg* igmp;
lorcansmith 0:2a53a4c3238c 394 struct igmp_group* group;
lorcansmith 0:2a53a4c3238c 395 struct igmp_group* groupref;
lorcansmith 0:2a53a4c3238c 396
lorcansmith 0:2a53a4c3238c 397 IGMP_STATS_INC(igmp.recv);
lorcansmith 0:2a53a4c3238c 398
lorcansmith 0:2a53a4c3238c 399 /* Note that the length CAN be greater than 8 but only 8 are used - All are included in the checksum */
lorcansmith 0:2a53a4c3238c 400 iphdr = (struct ip_hdr *)p->payload;
lorcansmith 0:2a53a4c3238c 401 if (pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4)) || (p->len < IGMP_MINLEN)) {
lorcansmith 0:2a53a4c3238c 402 pbuf_free(p);
lorcansmith 0:2a53a4c3238c 403 IGMP_STATS_INC(igmp.lenerr);
lorcansmith 0:2a53a4c3238c 404 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: length error\n"));
lorcansmith 0:2a53a4c3238c 405 return;
lorcansmith 0:2a53a4c3238c 406 }
lorcansmith 0:2a53a4c3238c 407
lorcansmith 0:2a53a4c3238c 408 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: message from "));
lorcansmith 0:2a53a4c3238c 409 ip_addr_debug_print(IGMP_DEBUG, &(iphdr->src));
lorcansmith 0:2a53a4c3238c 410 LWIP_DEBUGF(IGMP_DEBUG, (" to address "));
lorcansmith 0:2a53a4c3238c 411 ip_addr_debug_print(IGMP_DEBUG, &(iphdr->dest));
lorcansmith 0:2a53a4c3238c 412 LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", inp));
lorcansmith 0:2a53a4c3238c 413
lorcansmith 0:2a53a4c3238c 414 /* Now calculate and check the checksum */
lorcansmith 0:2a53a4c3238c 415 igmp = (struct igmp_msg *)p->payload;
lorcansmith 0:2a53a4c3238c 416 if (inet_chksum(igmp, p->len)) {
lorcansmith 0:2a53a4c3238c 417 pbuf_free(p);
lorcansmith 0:2a53a4c3238c 418 IGMP_STATS_INC(igmp.chkerr);
lorcansmith 0:2a53a4c3238c 419 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: checksum error\n"));
lorcansmith 0:2a53a4c3238c 420 return;
lorcansmith 0:2a53a4c3238c 421 }
lorcansmith 0:2a53a4c3238c 422
lorcansmith 0:2a53a4c3238c 423 /* Packet is ok so find an existing group */
lorcansmith 0:2a53a4c3238c 424 group = igmp_lookfor_group(inp, dest); /* use the destination IP address of incoming packet */
lorcansmith 0:2a53a4c3238c 425
lorcansmith 0:2a53a4c3238c 426 /* If group can be found or create... */
lorcansmith 0:2a53a4c3238c 427 if (!group) {
lorcansmith 0:2a53a4c3238c 428 pbuf_free(p);
lorcansmith 0:2a53a4c3238c 429 IGMP_STATS_INC(igmp.drop);
lorcansmith 0:2a53a4c3238c 430 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP frame not for us\n"));
lorcansmith 0:2a53a4c3238c 431 return;
lorcansmith 0:2a53a4c3238c 432 }
lorcansmith 0:2a53a4c3238c 433
lorcansmith 0:2a53a4c3238c 434 /* NOW ACT ON THE INCOMING MESSAGE TYPE... */
lorcansmith 0:2a53a4c3238c 435 switch (igmp->igmp_msgtype) {
lorcansmith 0:2a53a4c3238c 436 case IGMP_MEMB_QUERY: {
lorcansmith 0:2a53a4c3238c 437 /* IGMP_MEMB_QUERY to the "all systems" address ? */
lorcansmith 0:2a53a4c3238c 438 if ((ip_addr_cmp(dest, &allsystems)) && ip_addr_isany(&igmp->igmp_group_address)) {
lorcansmith 0:2a53a4c3238c 439 /* THIS IS THE GENERAL QUERY */
lorcansmith 0:2a53a4c3238c 440 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: General IGMP_MEMB_QUERY on \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
lorcansmith 0:2a53a4c3238c 441
lorcansmith 0:2a53a4c3238c 442 if (igmp->igmp_maxresp == 0) {
lorcansmith 0:2a53a4c3238c 443 IGMP_STATS_INC(igmp.rx_v1);
lorcansmith 0:2a53a4c3238c 444 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: got an all hosts query with time== 0 - this is V1 and not implemented - treat as v2\n"));
lorcansmith 0:2a53a4c3238c 445 igmp->igmp_maxresp = IGMP_V1_DELAYING_MEMBER_TMR;
lorcansmith 0:2a53a4c3238c 446 } else {
lorcansmith 0:2a53a4c3238c 447 IGMP_STATS_INC(igmp.rx_general);
lorcansmith 0:2a53a4c3238c 448 }
lorcansmith 0:2a53a4c3238c 449
lorcansmith 0:2a53a4c3238c 450 groupref = igmp_group_list;
lorcansmith 0:2a53a4c3238c 451 while (groupref) {
lorcansmith 0:2a53a4c3238c 452 /* Do not send messages on the all systems group address! */
lorcansmith 0:2a53a4c3238c 453 if ((groupref->netif == inp) && (!(ip_addr_cmp(&(groupref->group_address), &allsystems)))) {
lorcansmith 0:2a53a4c3238c 454 igmp_delaying_member(groupref, igmp->igmp_maxresp);
lorcansmith 0:2a53a4c3238c 455 }
lorcansmith 0:2a53a4c3238c 456 groupref = groupref->next;
lorcansmith 0:2a53a4c3238c 457 }
lorcansmith 0:2a53a4c3238c 458 } else {
lorcansmith 0:2a53a4c3238c 459 /* IGMP_MEMB_QUERY to a specific group ? */
lorcansmith 0:2a53a4c3238c 460 if (!ip_addr_isany(&igmp->igmp_group_address)) {
lorcansmith 0:2a53a4c3238c 461 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP_MEMB_QUERY to a specific group "));
lorcansmith 0:2a53a4c3238c 462 ip_addr_debug_print(IGMP_DEBUG, &igmp->igmp_group_address);
lorcansmith 0:2a53a4c3238c 463 if (ip_addr_cmp(dest, &allsystems)) {
lorcansmith 0:2a53a4c3238c 464 ip_addr_t groupaddr;
lorcansmith 0:2a53a4c3238c 465 LWIP_DEBUGF(IGMP_DEBUG, (" using \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
lorcansmith 0:2a53a4c3238c 466 /* we first need to re-look for the group since we used dest last time */
lorcansmith 0:2a53a4c3238c 467 ip_addr_copy(groupaddr, igmp->igmp_group_address);
lorcansmith 0:2a53a4c3238c 468 group = igmp_lookfor_group(inp, &groupaddr);
lorcansmith 0:2a53a4c3238c 469 } else {
lorcansmith 0:2a53a4c3238c 470 LWIP_DEBUGF(IGMP_DEBUG, (" with the group address as destination [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
lorcansmith 0:2a53a4c3238c 471 }
lorcansmith 0:2a53a4c3238c 472
lorcansmith 0:2a53a4c3238c 473 if (group != NULL) {
lorcansmith 0:2a53a4c3238c 474 IGMP_STATS_INC(igmp.rx_group);
lorcansmith 0:2a53a4c3238c 475 igmp_delaying_member(group, igmp->igmp_maxresp);
lorcansmith 0:2a53a4c3238c 476 } else {
lorcansmith 0:2a53a4c3238c 477 IGMP_STATS_INC(igmp.drop);
lorcansmith 0:2a53a4c3238c 478 }
lorcansmith 0:2a53a4c3238c 479 } else {
lorcansmith 0:2a53a4c3238c 480 IGMP_STATS_INC(igmp.proterr);
lorcansmith 0:2a53a4c3238c 481 }
lorcansmith 0:2a53a4c3238c 482 }
lorcansmith 0:2a53a4c3238c 483 break;
lorcansmith 0:2a53a4c3238c 484 }
lorcansmith 0:2a53a4c3238c 485 case IGMP_V2_MEMB_REPORT: {
lorcansmith 0:2a53a4c3238c 486 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP_V2_MEMB_REPORT\n"));
lorcansmith 0:2a53a4c3238c 487 IGMP_STATS_INC(igmp.rx_report);
lorcansmith 0:2a53a4c3238c 488 if (group->group_state == IGMP_GROUP_DELAYING_MEMBER) {
lorcansmith 0:2a53a4c3238c 489 /* This is on a specific group we have already looked up */
lorcansmith 0:2a53a4c3238c 490 group->timer = 0; /* stopped */
lorcansmith 0:2a53a4c3238c 491 group->group_state = IGMP_GROUP_IDLE_MEMBER;
lorcansmith 0:2a53a4c3238c 492 group->last_reporter_flag = 0;
lorcansmith 0:2a53a4c3238c 493 }
lorcansmith 0:2a53a4c3238c 494 break;
lorcansmith 0:2a53a4c3238c 495 }
lorcansmith 0:2a53a4c3238c 496 default: {
lorcansmith 0:2a53a4c3238c 497 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: unexpected msg %d in state %d on group %p on if %p\n",
lorcansmith 0:2a53a4c3238c 498 igmp->igmp_msgtype, group->group_state, &group, group->netif));
lorcansmith 0:2a53a4c3238c 499 IGMP_STATS_INC(igmp.proterr);
lorcansmith 0:2a53a4c3238c 500 break;
lorcansmith 0:2a53a4c3238c 501 }
lorcansmith 0:2a53a4c3238c 502 }
lorcansmith 0:2a53a4c3238c 503
lorcansmith 0:2a53a4c3238c 504 pbuf_free(p);
lorcansmith 0:2a53a4c3238c 505 return;
lorcansmith 0:2a53a4c3238c 506 }
lorcansmith 0:2a53a4c3238c 507
lorcansmith 0:2a53a4c3238c 508 /**
lorcansmith 0:2a53a4c3238c 509 * Join a group on one network interface.
lorcansmith 0:2a53a4c3238c 510 *
lorcansmith 0:2a53a4c3238c 511 * @param ifaddr ip address of the network interface which should join a new group
lorcansmith 0:2a53a4c3238c 512 * @param groupaddr the ip address of the group which to join
lorcansmith 0:2a53a4c3238c 513 * @return ERR_OK if group was joined on the netif(s), an err_t otherwise
lorcansmith 0:2a53a4c3238c 514 */
lorcansmith 0:2a53a4c3238c 515 err_t
lorcansmith 0:2a53a4c3238c 516 igmp_joingroup(ip_addr_t *ifaddr, ip_addr_t *groupaddr)
lorcansmith 0:2a53a4c3238c 517 {
lorcansmith 0:2a53a4c3238c 518 err_t err = ERR_VAL; /* no matching interface */
lorcansmith 0:2a53a4c3238c 519 struct igmp_group *group;
lorcansmith 0:2a53a4c3238c 520 struct netif *netif;
lorcansmith 0:2a53a4c3238c 521
lorcansmith 0:2a53a4c3238c 522 /* make sure it is multicast address */
lorcansmith 0:2a53a4c3238c 523 LWIP_ERROR("igmp_joingroup: attempt to join non-multicast address", ip_addr_ismulticast(groupaddr), return ERR_VAL;);
lorcansmith 0:2a53a4c3238c 524 LWIP_ERROR("igmp_joingroup: attempt to join allsystems address", (!ip_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
lorcansmith 0:2a53a4c3238c 525
lorcansmith 0:2a53a4c3238c 526 /* loop through netif's */
lorcansmith 0:2a53a4c3238c 527 netif = netif_list;
lorcansmith 0:2a53a4c3238c 528 while (netif != NULL) {
lorcansmith 0:2a53a4c3238c 529 /* Should we join this interface ? */
lorcansmith 0:2a53a4c3238c 530 if ((netif->flags & NETIF_FLAG_IGMP) && ((ip_addr_isany(ifaddr) || ip_addr_cmp(&(netif->ip_addr), ifaddr)))) {
lorcansmith 0:2a53a4c3238c 531 /* find group or create a new one if not found */
lorcansmith 0:2a53a4c3238c 532 group = igmp_lookup_group(netif, groupaddr);
lorcansmith 0:2a53a4c3238c 533
lorcansmith 0:2a53a4c3238c 534 if (group != NULL) {
lorcansmith 0:2a53a4c3238c 535 /* This should create a new group, check the state to make sure */
lorcansmith 0:2a53a4c3238c 536 if (group->group_state != IGMP_GROUP_NON_MEMBER) {
lorcansmith 0:2a53a4c3238c 537 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: join to group not in state IGMP_GROUP_NON_MEMBER\n"));
lorcansmith 0:2a53a4c3238c 538 } else {
lorcansmith 0:2a53a4c3238c 539 /* OK - it was new group */
lorcansmith 0:2a53a4c3238c 540 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: join to new group: "));
lorcansmith 0:2a53a4c3238c 541 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
lorcansmith 0:2a53a4c3238c 542 LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
lorcansmith 0:2a53a4c3238c 543
lorcansmith 0:2a53a4c3238c 544 /* If first use of the group, allow the group at the MAC level */
lorcansmith 0:2a53a4c3238c 545 if ((group->use==0) && (netif->igmp_mac_filter != NULL)) {
lorcansmith 0:2a53a4c3238c 546 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: igmp_mac_filter(ADD "));
lorcansmith 0:2a53a4c3238c 547 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
lorcansmith 0:2a53a4c3238c 548 LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
lorcansmith 0:2a53a4c3238c 549 netif->igmp_mac_filter(netif, groupaddr, IGMP_ADD_MAC_FILTER);
lorcansmith 0:2a53a4c3238c 550 }
lorcansmith 0:2a53a4c3238c 551
lorcansmith 0:2a53a4c3238c 552 IGMP_STATS_INC(igmp.tx_join);
lorcansmith 0:2a53a4c3238c 553 igmp_send(group, IGMP_V2_MEMB_REPORT);
lorcansmith 0:2a53a4c3238c 554
lorcansmith 0:2a53a4c3238c 555 igmp_start_timer(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
lorcansmith 0:2a53a4c3238c 556
lorcansmith 0:2a53a4c3238c 557 /* Need to work out where this timer comes from */
lorcansmith 0:2a53a4c3238c 558 group->group_state = IGMP_GROUP_DELAYING_MEMBER;
lorcansmith 0:2a53a4c3238c 559 }
lorcansmith 0:2a53a4c3238c 560 /* Increment group use */
lorcansmith 0:2a53a4c3238c 561 group->use++;
lorcansmith 0:2a53a4c3238c 562 /* Join on this interface */
lorcansmith 0:2a53a4c3238c 563 err = ERR_OK;
lorcansmith 0:2a53a4c3238c 564 } else {
lorcansmith 0:2a53a4c3238c 565 /* Return an error even if some network interfaces are joined */
lorcansmith 0:2a53a4c3238c 566 /** @todo undo any other netif already joined */
lorcansmith 0:2a53a4c3238c 567 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: Not enought memory to join to group\n"));
lorcansmith 0:2a53a4c3238c 568 return ERR_MEM;
lorcansmith 0:2a53a4c3238c 569 }
lorcansmith 0:2a53a4c3238c 570 }
lorcansmith 0:2a53a4c3238c 571 /* proceed to next network interface */
lorcansmith 0:2a53a4c3238c 572 netif = netif->next;
lorcansmith 0:2a53a4c3238c 573 }
lorcansmith 0:2a53a4c3238c 574
lorcansmith 0:2a53a4c3238c 575 return err;
lorcansmith 0:2a53a4c3238c 576 }
lorcansmith 0:2a53a4c3238c 577
lorcansmith 0:2a53a4c3238c 578 /**
lorcansmith 0:2a53a4c3238c 579 * Leave a group on one network interface.
lorcansmith 0:2a53a4c3238c 580 *
lorcansmith 0:2a53a4c3238c 581 * @param ifaddr ip address of the network interface which should leave a group
lorcansmith 0:2a53a4c3238c 582 * @param groupaddr the ip address of the group which to leave
lorcansmith 0:2a53a4c3238c 583 * @return ERR_OK if group was left on the netif(s), an err_t otherwise
lorcansmith 0:2a53a4c3238c 584 */
lorcansmith 0:2a53a4c3238c 585 err_t
lorcansmith 0:2a53a4c3238c 586 igmp_leavegroup(ip_addr_t *ifaddr, ip_addr_t *groupaddr)
lorcansmith 0:2a53a4c3238c 587 {
lorcansmith 0:2a53a4c3238c 588 err_t err = ERR_VAL; /* no matching interface */
lorcansmith 0:2a53a4c3238c 589 struct igmp_group *group;
lorcansmith 0:2a53a4c3238c 590 struct netif *netif;
lorcansmith 0:2a53a4c3238c 591
lorcansmith 0:2a53a4c3238c 592 /* make sure it is multicast address */
lorcansmith 0:2a53a4c3238c 593 LWIP_ERROR("igmp_leavegroup: attempt to leave non-multicast address", ip_addr_ismulticast(groupaddr), return ERR_VAL;);
lorcansmith 0:2a53a4c3238c 594 LWIP_ERROR("igmp_leavegroup: attempt to leave allsystems address", (!ip_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
lorcansmith 0:2a53a4c3238c 595
lorcansmith 0:2a53a4c3238c 596 /* loop through netif's */
lorcansmith 0:2a53a4c3238c 597 netif = netif_list;
lorcansmith 0:2a53a4c3238c 598 while (netif != NULL) {
lorcansmith 0:2a53a4c3238c 599 /* Should we leave this interface ? */
lorcansmith 0:2a53a4c3238c 600 if ((netif->flags & NETIF_FLAG_IGMP) && ((ip_addr_isany(ifaddr) || ip_addr_cmp(&(netif->ip_addr), ifaddr)))) {
lorcansmith 0:2a53a4c3238c 601 /* find group */
lorcansmith 0:2a53a4c3238c 602 group = igmp_lookfor_group(netif, groupaddr);
lorcansmith 0:2a53a4c3238c 603
lorcansmith 0:2a53a4c3238c 604 if (group != NULL) {
lorcansmith 0:2a53a4c3238c 605 /* Only send a leave if the flag is set according to the state diagram */
lorcansmith 0:2a53a4c3238c 606 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: Leaving group: "));
lorcansmith 0:2a53a4c3238c 607 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
lorcansmith 0:2a53a4c3238c 608 LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
lorcansmith 0:2a53a4c3238c 609
lorcansmith 0:2a53a4c3238c 610 /* If there is no other use of the group */
lorcansmith 0:2a53a4c3238c 611 if (group->use <= 1) {
lorcansmith 0:2a53a4c3238c 612 /* If we are the last reporter for this group */
lorcansmith 0:2a53a4c3238c 613 if (group->last_reporter_flag) {
lorcansmith 0:2a53a4c3238c 614 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: sending leaving group\n"));
lorcansmith 0:2a53a4c3238c 615 IGMP_STATS_INC(igmp.tx_leave);
lorcansmith 0:2a53a4c3238c 616 igmp_send(group, IGMP_LEAVE_GROUP);
lorcansmith 0:2a53a4c3238c 617 }
lorcansmith 0:2a53a4c3238c 618
lorcansmith 0:2a53a4c3238c 619 /* Disable the group at the MAC level */
lorcansmith 0:2a53a4c3238c 620 if (netif->igmp_mac_filter != NULL) {
lorcansmith 0:2a53a4c3238c 621 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: igmp_mac_filter(DEL "));
lorcansmith 0:2a53a4c3238c 622 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
lorcansmith 0:2a53a4c3238c 623 LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
lorcansmith 0:2a53a4c3238c 624 netif->igmp_mac_filter(netif, groupaddr, IGMP_DEL_MAC_FILTER);
lorcansmith 0:2a53a4c3238c 625 }
lorcansmith 0:2a53a4c3238c 626
lorcansmith 0:2a53a4c3238c 627 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: remove group: "));
lorcansmith 0:2a53a4c3238c 628 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
lorcansmith 0:2a53a4c3238c 629 LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
lorcansmith 0:2a53a4c3238c 630
lorcansmith 0:2a53a4c3238c 631 /* Free the group */
lorcansmith 0:2a53a4c3238c 632 igmp_remove_group(group);
lorcansmith 0:2a53a4c3238c 633 } else {
lorcansmith 0:2a53a4c3238c 634 /* Decrement group use */
lorcansmith 0:2a53a4c3238c 635 group->use--;
lorcansmith 0:2a53a4c3238c 636 }
lorcansmith 0:2a53a4c3238c 637 /* Leave on this interface */
lorcansmith 0:2a53a4c3238c 638 err = ERR_OK;
lorcansmith 0:2a53a4c3238c 639 } else {
lorcansmith 0:2a53a4c3238c 640 /* It's not a fatal error on "leavegroup" */
lorcansmith 0:2a53a4c3238c 641 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: not member of group\n"));
lorcansmith 0:2a53a4c3238c 642 }
lorcansmith 0:2a53a4c3238c 643 }
lorcansmith 0:2a53a4c3238c 644 /* proceed to next network interface */
lorcansmith 0:2a53a4c3238c 645 netif = netif->next;
lorcansmith 0:2a53a4c3238c 646 }
lorcansmith 0:2a53a4c3238c 647
lorcansmith 0:2a53a4c3238c 648 return err;
lorcansmith 0:2a53a4c3238c 649 }
lorcansmith 0:2a53a4c3238c 650
lorcansmith 0:2a53a4c3238c 651 /**
lorcansmith 0:2a53a4c3238c 652 * The igmp timer function (both for NO_SYS=1 and =0)
lorcansmith 0:2a53a4c3238c 653 * Should be called every IGMP_TMR_INTERVAL milliseconds (100 ms is default).
lorcansmith 0:2a53a4c3238c 654 */
lorcansmith 0:2a53a4c3238c 655 void
lorcansmith 0:2a53a4c3238c 656 igmp_tmr(void)
lorcansmith 0:2a53a4c3238c 657 {
lorcansmith 0:2a53a4c3238c 658 struct igmp_group *group = igmp_group_list;
lorcansmith 0:2a53a4c3238c 659
lorcansmith 0:2a53a4c3238c 660 while (group != NULL) {
lorcansmith 0:2a53a4c3238c 661 if (group->timer > 0) {
lorcansmith 0:2a53a4c3238c 662 group->timer--;
lorcansmith 0:2a53a4c3238c 663 if (group->timer == 0) {
lorcansmith 0:2a53a4c3238c 664 igmp_timeout(group);
lorcansmith 0:2a53a4c3238c 665 }
lorcansmith 0:2a53a4c3238c 666 }
lorcansmith 0:2a53a4c3238c 667 group = group->next;
lorcansmith 0:2a53a4c3238c 668 }
lorcansmith 0:2a53a4c3238c 669 }
lorcansmith 0:2a53a4c3238c 670
lorcansmith 0:2a53a4c3238c 671 /**
lorcansmith 0:2a53a4c3238c 672 * Called if a timeout for one group is reached.
lorcansmith 0:2a53a4c3238c 673 * Sends a report for this group.
lorcansmith 0:2a53a4c3238c 674 *
lorcansmith 0:2a53a4c3238c 675 * @param group an igmp_group for which a timeout is reached
lorcansmith 0:2a53a4c3238c 676 */
lorcansmith 0:2a53a4c3238c 677 static void
lorcansmith 0:2a53a4c3238c 678 igmp_timeout(struct igmp_group *group)
lorcansmith 0:2a53a4c3238c 679 {
lorcansmith 0:2a53a4c3238c 680 /* If the state is IGMP_GROUP_DELAYING_MEMBER then we send a report for this group */
lorcansmith 0:2a53a4c3238c 681 if (group->group_state == IGMP_GROUP_DELAYING_MEMBER) {
lorcansmith 0:2a53a4c3238c 682 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_timeout: report membership for group with address "));
lorcansmith 0:2a53a4c3238c 683 ip_addr_debug_print(IGMP_DEBUG, &(group->group_address));
lorcansmith 0:2a53a4c3238c 684 LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", group->netif));
lorcansmith 0:2a53a4c3238c 685
lorcansmith 0:2a53a4c3238c 686 IGMP_STATS_INC(igmp.tx_report);
lorcansmith 0:2a53a4c3238c 687 igmp_send(group, IGMP_V2_MEMB_REPORT);
lorcansmith 0:2a53a4c3238c 688 }
lorcansmith 0:2a53a4c3238c 689 }
lorcansmith 0:2a53a4c3238c 690
lorcansmith 0:2a53a4c3238c 691 /**
lorcansmith 0:2a53a4c3238c 692 * Start a timer for an igmp group
lorcansmith 0:2a53a4c3238c 693 *
lorcansmith 0:2a53a4c3238c 694 * @param group the igmp_group for which to start a timer
lorcansmith 0:2a53a4c3238c 695 * @param max_time the time in multiples of IGMP_TMR_INTERVAL (decrease with
lorcansmith 0:2a53a4c3238c 696 * every call to igmp_tmr())
lorcansmith 0:2a53a4c3238c 697 */
lorcansmith 0:2a53a4c3238c 698 static void
lorcansmith 0:2a53a4c3238c 699 igmp_start_timer(struct igmp_group *group, u8_t max_time)
lorcansmith 0:2a53a4c3238c 700 {
lorcansmith 0:2a53a4c3238c 701 /* ensure the input value is > 0 */
lorcansmith 0:2a53a4c3238c 702 if (max_time == 0) {
lorcansmith 0:2a53a4c3238c 703 max_time = 1;
lorcansmith 0:2a53a4c3238c 704 }
lorcansmith 0:2a53a4c3238c 705 /* ensure the random value is > 0 */
lorcansmith 0:2a53a4c3238c 706 group->timer = (LWIP_RAND() % (max_time - 1)) + 1;
lorcansmith 0:2a53a4c3238c 707 }
lorcansmith 0:2a53a4c3238c 708
lorcansmith 0:2a53a4c3238c 709 /**
lorcansmith 0:2a53a4c3238c 710 * Stop a timer for an igmp_group
lorcansmith 0:2a53a4c3238c 711 *
lorcansmith 0:2a53a4c3238c 712 * @param group the igmp_group for which to stop the timer
lorcansmith 0:2a53a4c3238c 713 */
lorcansmith 0:2a53a4c3238c 714 static void
lorcansmith 0:2a53a4c3238c 715 igmp_stop_timer(struct igmp_group *group)
lorcansmith 0:2a53a4c3238c 716 {
lorcansmith 0:2a53a4c3238c 717 group->timer = 0;
lorcansmith 0:2a53a4c3238c 718 }
lorcansmith 0:2a53a4c3238c 719
lorcansmith 0:2a53a4c3238c 720 /**
lorcansmith 0:2a53a4c3238c 721 * Delaying membership report for a group if necessary
lorcansmith 0:2a53a4c3238c 722 *
lorcansmith 0:2a53a4c3238c 723 * @param group the igmp_group for which "delaying" membership report
lorcansmith 0:2a53a4c3238c 724 * @param maxresp query delay
lorcansmith 0:2a53a4c3238c 725 */
lorcansmith 0:2a53a4c3238c 726 static void
lorcansmith 0:2a53a4c3238c 727 igmp_delaying_member(struct igmp_group *group, u8_t maxresp)
lorcansmith 0:2a53a4c3238c 728 {
lorcansmith 0:2a53a4c3238c 729 if ((group->group_state == IGMP_GROUP_IDLE_MEMBER) ||
lorcansmith 0:2a53a4c3238c 730 ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) &&
lorcansmith 0:2a53a4c3238c 731 ((group->timer == 0) || (maxresp < group->timer)))) {
lorcansmith 0:2a53a4c3238c 732 igmp_start_timer(group, maxresp);
lorcansmith 0:2a53a4c3238c 733 group->group_state = IGMP_GROUP_DELAYING_MEMBER;
lorcansmith 0:2a53a4c3238c 734 }
lorcansmith 0:2a53a4c3238c 735 }
lorcansmith 0:2a53a4c3238c 736
lorcansmith 0:2a53a4c3238c 737
lorcansmith 0:2a53a4c3238c 738 /**
lorcansmith 0:2a53a4c3238c 739 * Sends an IP packet on a network interface. This function constructs the IP header
lorcansmith 0:2a53a4c3238c 740 * and calculates the IP header checksum. If the source IP address is NULL,
lorcansmith 0:2a53a4c3238c 741 * the IP address of the outgoing network interface is filled in as source address.
lorcansmith 0:2a53a4c3238c 742 *
lorcansmith 0:2a53a4c3238c 743 * @param p the packet to send (p->payload points to the data, e.g. next
lorcansmith 0:2a53a4c3238c 744 protocol header; if dest == IP_HDRINCL, p already includes an IP
lorcansmith 0:2a53a4c3238c 745 header and p->payload points to that IP header)
lorcansmith 0:2a53a4c3238c 746 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
lorcansmith 0:2a53a4c3238c 747 * IP address of the netif used to send is used as source address)
lorcansmith 0:2a53a4c3238c 748 * @param dest the destination IP address to send the packet to
lorcansmith 0:2a53a4c3238c 749 * @param ttl the TTL value to be set in the IP header
lorcansmith 0:2a53a4c3238c 750 * @param proto the PROTOCOL to be set in the IP header
lorcansmith 0:2a53a4c3238c 751 * @param netif the netif on which to send this packet
lorcansmith 0:2a53a4c3238c 752 * @return ERR_OK if the packet was sent OK
lorcansmith 0:2a53a4c3238c 753 * ERR_BUF if p doesn't have enough space for IP/LINK headers
lorcansmith 0:2a53a4c3238c 754 * returns errors returned by netif->output
lorcansmith 0:2a53a4c3238c 755 */
lorcansmith 0:2a53a4c3238c 756 static err_t
lorcansmith 0:2a53a4c3238c 757 igmp_ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, struct netif *netif)
lorcansmith 0:2a53a4c3238c 758 {
lorcansmith 0:2a53a4c3238c 759 /* This is the "router alert" option */
lorcansmith 0:2a53a4c3238c 760 u16_t ra[2];
lorcansmith 0:2a53a4c3238c 761 ra[0] = PP_HTONS(ROUTER_ALERT);
lorcansmith 0:2a53a4c3238c 762 ra[1] = 0x0000; /* Router shall examine packet */
lorcansmith 0:2a53a4c3238c 763 IGMP_STATS_INC(igmp.xmit);
lorcansmith 0:2a53a4c3238c 764 return ip_output_if_opt(p, src, dest, IGMP_TTL, 0, IP_PROTO_IGMP, netif, ra, ROUTER_ALERTLEN);
lorcansmith 0:2a53a4c3238c 765 }
lorcansmith 0:2a53a4c3238c 766
lorcansmith 0:2a53a4c3238c 767 /**
lorcansmith 0:2a53a4c3238c 768 * Send an igmp packet to a specific group.
lorcansmith 0:2a53a4c3238c 769 *
lorcansmith 0:2a53a4c3238c 770 * @param group the group to which to send the packet
lorcansmith 0:2a53a4c3238c 771 * @param type the type of igmp packet to send
lorcansmith 0:2a53a4c3238c 772 */
lorcansmith 0:2a53a4c3238c 773 static void
lorcansmith 0:2a53a4c3238c 774 igmp_send(struct igmp_group *group, u8_t type)
lorcansmith 0:2a53a4c3238c 775 {
lorcansmith 0:2a53a4c3238c 776 struct pbuf* p = NULL;
lorcansmith 0:2a53a4c3238c 777 struct igmp_msg* igmp = NULL;
lorcansmith 0:2a53a4c3238c 778 ip_addr_t src = *IP_ADDR_ANY;
lorcansmith 0:2a53a4c3238c 779 ip_addr_t* dest = NULL;
lorcansmith 0:2a53a4c3238c 780
lorcansmith 0:2a53a4c3238c 781 /* IP header + "router alert" option + IGMP header */
lorcansmith 0:2a53a4c3238c 782 p = pbuf_alloc(PBUF_TRANSPORT, IGMP_MINLEN, PBUF_RAM);
lorcansmith 0:2a53a4c3238c 783
lorcansmith 0:2a53a4c3238c 784 if (p) {
lorcansmith 0:2a53a4c3238c 785 igmp = (struct igmp_msg *)p->payload;
lorcansmith 0:2a53a4c3238c 786 LWIP_ASSERT("igmp_send: check that first pbuf can hold struct igmp_msg",
lorcansmith 0:2a53a4c3238c 787 (p->len >= sizeof(struct igmp_msg)));
lorcansmith 0:2a53a4c3238c 788 ip_addr_copy(src, group->netif->ip_addr);
lorcansmith 0:2a53a4c3238c 789
lorcansmith 0:2a53a4c3238c 790 if (type == IGMP_V2_MEMB_REPORT) {
lorcansmith 0:2a53a4c3238c 791 dest = &(group->group_address);
lorcansmith 0:2a53a4c3238c 792 ip_addr_copy(igmp->igmp_group_address, group->group_address);
lorcansmith 0:2a53a4c3238c 793 group->last_reporter_flag = 1; /* Remember we were the last to report */
lorcansmith 0:2a53a4c3238c 794 } else {
lorcansmith 0:2a53a4c3238c 795 if (type == IGMP_LEAVE_GROUP) {
lorcansmith 0:2a53a4c3238c 796 dest = &allrouters;
lorcansmith 0:2a53a4c3238c 797 ip_addr_copy(igmp->igmp_group_address, group->group_address);
lorcansmith 0:2a53a4c3238c 798 }
lorcansmith 0:2a53a4c3238c 799 }
lorcansmith 0:2a53a4c3238c 800
lorcansmith 0:2a53a4c3238c 801 if ((type == IGMP_V2_MEMB_REPORT) || (type == IGMP_LEAVE_GROUP)) {
lorcansmith 0:2a53a4c3238c 802 igmp->igmp_msgtype = type;
lorcansmith 0:2a53a4c3238c 803 igmp->igmp_maxresp = 0;
lorcansmith 0:2a53a4c3238c 804 igmp->igmp_checksum = 0;
lorcansmith 0:2a53a4c3238c 805 igmp->igmp_checksum = inet_chksum(igmp, IGMP_MINLEN);
lorcansmith 0:2a53a4c3238c 806
lorcansmith 0:2a53a4c3238c 807 igmp_ip_output_if(p, &src, dest, group->netif);
lorcansmith 0:2a53a4c3238c 808 }
lorcansmith 0:2a53a4c3238c 809
lorcansmith 0:2a53a4c3238c 810 pbuf_free(p);
lorcansmith 0:2a53a4c3238c 811 } else {
lorcansmith 0:2a53a4c3238c 812 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_send: not enough memory for igmp_send\n"));
lorcansmith 0:2a53a4c3238c 813 IGMP_STATS_INC(igmp.memerr);
lorcansmith 0:2a53a4c3238c 814 }
lorcansmith 0:2a53a4c3238c 815 }
lorcansmith 0:2a53a4c3238c 816
lorcansmith 0:2a53a4c3238c 817 #endif /* LWIP_IGMP */