Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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