summaryrefslogtreecommitdiff
path: root/nuttx/net/igmp/igmp_input.c
blob: 7668f81c5a32ec6e5affd94c51abb6ed084490b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/****************************************************************************
 * net/igmp/igmp_input.c
 *
 *   Copyright (C) 2010 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * The NuttX implementation of IGMP was inspired by the IGMP add-on for the
 * lwIP TCP/IP stack by Steve Reynolds:
 *
 *   Copyright (c) 2002 CITEL Technologies Ltd.
 *   All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of CITEL Technologies Ltd nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <wdog.h>
#include <assert.h>
#include <debug.h>

#include <nuttx/net/netconfig.h>
#include <nuttx/net/uip.h>
#include <nuttx/net/igmp.h>
#include <nuttx/net/netstats.h>

#include "devif/devif.h"
#include "igmp/igmp.h"

#ifdef CONFIG_NET_IGMP

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define IGMPBUF ((struct igmp_iphdr_s *)&dev->d_buf[NET_LLH_LEN])

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name:  igmp_input
 *
 * Description:
 *   An IGMP packet has been received.
 *
 *                              ________________
 *                             |                |
 *                             |                |
 *                             |                |
 *                             |                |
 *                  +--------->|   Non-Member   |<---------+
 *                  |          |                |          |
 *                  |          |                |          |
 *                  |          |                |          |
 *                  |          |________________|          |
 *                  |                   |                  |
 *                  | leave group       | join group       | leave group
 *                  | (stop timer,      |(send report,     | (send leave
 *                  |  send leave if    | set flag,        |  if flag set)
 *                  |  flag set)        | start timer)     |
 *          ________|________           |          ________|________
 *         |                 |<---------+         |                 |
 *         |                 |                    |                 |
 *         |                 |<-------------------|                 |
 *         |                 |   query received   |                 |
 *         | Delaying Member |    (start timer)   |   Idle Member   |
 *   +---->|                 |------------------->|                 |
 *   |     |                 |   report received  |                 |
 *   |     |                 |    (stop timer,    |                 |
 *   |     |                 |     clear flag)    |                 |
 *   |     |_________________|------------------->|_________________|
 *   | query received    |        timer expired
 *   | (reset timer if   |        (send report,
 *   |  Max Resp Time    |         set flag)
 *   |  < current timer) |
 *   +-------------------+
 *
 * NOTE: This is most likely executing from an interrupt handler.
 *
 ****************************************************************************/

void igmp_input(struct net_driver_s *dev)
{
  FAR struct igmp_group_s *group;
  net_ipaddr_t destipaddr;
  net_ipaddr_t grpaddr;
  unsigned int ticks;

  nllvdbg("IGMP message: %04x%04x\n", IGMPBUF->destipaddr[1], IGMPBUF->destipaddr[0]);

  /* Verify the message length */

  if (dev->d_len < NET_LLH_LEN+UIP_IPIGMPH_LEN)
    {
      IGMP_STATINCR(g_netstats.igmp.length_errors);
      nlldbg("Length error\n");
      return;
    }

  /* Calculate and check the IGMP checksum */

  if (net_chksum((uint16_t*)&IGMPBUF->type, UIP_IGMPH_LEN) != 0)
    {
      IGMP_STATINCR(g_netstats.igmp.chksum_errors);
      nlldbg("Checksum error\n");
      return;
    }

  /* Find the group (or create a new one) using the incoming IP address*/

  destipaddr = net_ip4addr_conv32(IGMPBUF->destipaddr);
  group = igmp_grpallocfind(dev, &destipaddr);
  if (!group)
    {
      nlldbg("Failed to allocate/find group: %08x\n", destipaddr);
      return;
    }

  /* Now handle the message based on the IGMP message type */

  switch (IGMPBUF->type)
    {
      case IGMP_MEMBERSHIP_QUERY:
        /* RFC 2236, 2.2.  ax Response Time
         *  "The Max Response Time field is meaningful only in Membership Query
         *   messages, and specifies the maximum allowed time before sending a
         *   responding report in units of 1/10 second.  In all other messages,
         *   it is set to zero by the sender and ignored by receivers.
         */

        /* Check if the query was sent to all systems */

        if (net_ipaddr_cmp(destipaddr, g_allsystems))
          {
            /* Yes... Now check the if this this is a general or a group
             * specific query.
             *
             * RFC 2236, 2.1.  Type
             * There are two sub-types of Membership Query messages:
             * - General Query, used to learn which groups have members on an
             *   attached network.
             * - Group-Specific Query, used to learn if a particular group
             *   has any members on an attached network.
             *
             * RFC 2236, 2.4. Group Address
             *   "In a Membership Query message, the group address field is
             *    set to zero when sending a General Query, and set to the
             *    group address being queried when sending a Group-Specific
             *    Query."
             */

            if (IGMPBUF->grpaddr == 0)
              {
                FAR struct igmp_group_s *member;

                /* This is the general query */

                nllvdbg("General multicast query\n");
                if (IGMPBUF->maxresp == 0)
                  {
                    IGMP_STATINCR(g_netstats.igmp.v1_received);
                    IGMPBUF->maxresp = 10;

                    nlldbg("V1 not implemented\n");
                  }

                IGMP_STATINCR(g_netstats.igmp.query_received);
                for (member = (FAR struct igmp_group_s *)dev->grplist.head;
                     member;
                     member = member->next)
                  {
                    /* Skip over the all systems group entry */

                    if (!net_ipaddr_cmp(member->grpaddr, g_allsystems))
                      {
                        ticks = igmp_decisec2tick((int)IGMPBUF->maxresp);
                        if (IS_IDLEMEMBER(member->flags) ||
                            igmp_cmptimer(member, ticks))
                          {
                            igmp_startticks(member, ticks);
                            CLR_IDLEMEMBER(member->flags);
                          }
                      }
                  }
              }
            else /* if (IGMPBUF->grpaddr != 0) */
              {
                nllvdbg("Group-specific multicast queury\n");

                /* We first need to re-lookup the group since we used dest last time.
                 * Use the incoming IPaddress!
                 */

                IGMP_STATINCR(g_netstats.igmp.ucast_query);
                grpaddr = net_ip4addr_conv32(IGMPBUF->grpaddr);
                group   = igmp_grpallocfind(dev, &grpaddr);
                ticks   = igmp_decisec2tick((int)IGMPBUF->maxresp);
                if (IS_IDLEMEMBER(group->flags) || igmp_cmptimer(group, ticks))
                  {
                    igmp_startticks(group, ticks);
                    CLR_IDLEMEMBER(group->flags);
                  }
              }
          }

        /* Not sent to all systems -- Unicast query */

        else if (group->grpaddr != 0)
          {
            nllvdbg("Unicast query\n");
            IGMP_STATINCR(g_netstats.igmp.ucast_query);

            nlldbg("Query to a specific group with the group address as destination\n");

            ticks = igmp_decisec2tick((int)IGMPBUF->maxresp);
            if (IS_IDLEMEMBER(group->flags) || igmp_cmptimer(group, ticks))
              {
                igmp_startticks(group, ticks);
                CLR_IDLEMEMBER(group->flags);
              }
          }
        break;

      case IGMPv2_MEMBERSHIP_REPORT:
        {
          nllvdbg("Membership report\n");

          IGMP_STATINCR(g_netstats.igmp.report_received);
          if (!IS_IDLEMEMBER(group->flags))
            {
              /* This is on a specific group we have already looked up */

              wd_cancel(group->wdog);
              SET_IDLEMEMBER(group->flags);
              CLR_LASTREPORT(group->flags);
            }
        }
      break;

      default:
        {
          nlldbg("Unexpected msg %02x\n", IGMPBUF->type);
        }
      break;
    }
}

#endif /* CONFIG_NET_IGMP */