summaryrefslogtreecommitdiff
path: root/cleopatre/application/igmp_snoopd/src/igmp_common.c
blob: 43e218d36eec9b00a9be7d8925fa58147131144a (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * igmp_snoopd: an IGMP Snooping Daemon.
 * Copyright (C) 2011 SPiDCOM Technologies
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "internal.h"

#include <errno.h>
#include <linux/igmp.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>

#include "utils.h"

#include "data_struct.h"

/* Some renaming to make things more clear */
#define IGMPV1_HOST_MEMBERSHIP_REPORT       IGMP_HOST_MEMBERSHIP_REPORT
#define IGMPV2_HOST_LEAVE_MESSAGE       IGMP_HOST_LEAVE_MESSAGE

#define ONE_SEC_IN_USEC     1000000     /* One second in microseconds */
#define ONE_USEC_IN_NSEC    1000    /* One microsecond in nanoseconds */

void
trim (struct group *grp, struct group_member *member)
{
    /* If the member state is Include{}, remove the member.
     * Then if the group becomes empty, remove the group */
    if (LIST_EMPTY (&member->sources_head)
        && member->filter_mode == FILTER_MODE_INCLUDE)
    {
        del_group_member(member);

        if (LIST_EMPTY (&grp->members_head))
        {
            del_group(grp);
        }
    }
}

/**
 * Compute the value of [Other Querier Present Interval] according to the RFC.
 * \param  metrics  The IGMP metrics to use for the computation.
 * \return  The value of Other Querier Present Interval.
 */
static inline duration_sec_t
compute_other_querier_present_interval (const struct igmp_metrics *metrics)
{
    /* RFC 2236 Section 8.5. and RFC 3376 Section 8.5.
     * Other Querier Present Interval =
     * ((the Robustness Variable) times (the Query Interval)) plus
     (one half of one Query Response Interval) */
    return ((metrics->robustness_variable * metrics->query_interval)
            + ceil ((double)metrics->query_response_interval / 2.0));
}

/**
 * Compute the value of [Group Membership Interval] according to the RFC.
 * \param  metrics  The IGMP metrics to use for the computation.
 * \return  The value of Group Membership Interval.
 */
static inline duration_sec_t
compute_group_membership_interval (const struct igmp_metrics *metrics)
{
    /* Group Membership Interval =
     * ((the Robustness Variable) times (the Query Interval))
     * plus (one Query Response Interval). */
    return (duration_sec_t) ((metrics->robustness_variable * metrics->query_interval)
                             + metrics->query_response_interval);
}

/**
 * Compute the value of the [Older Host Present Interval] using the provided
 * metrics.
 * \param  metrics  The metrics.
 * \return  The [Older Host Present Interval]
 */
static inline duration_sec_t
compute_older_host_present_interval (const struct igmp_metrics *metrics)
{
    return (duration_sec_t) ((metrics->robustness_variable * metrics->query_interval)
                             + metrics->query_response_interval);
}

/**
 * Update the computed metrics.
 *
 * Some of the metrics are computed from the values of other metrics.
 * This function updates these computed metrics.
 *
 * \param  metrics  The metrics.
 */
void
update_computed_metrics (struct igmp_metrics *metrics)
{
    metrics->other_querier_present_interval =
            compute_other_querier_present_interval (metrics);

    metrics->group_membership_interval =
            compute_group_membership_interval (metrics);

    metrics->older_host_present_interval =
            compute_older_host_present_interval (metrics);
}

void
reset_metrics (struct igmp_metrics *metrics)
{
    metrics->robustness_variable =  DEFAULT_ROBUSTNESS_VARIABLE;
    metrics->query_interval = DEFAULT_QUERY_INTERVAL;
    metrics->query_response_interval = DEFAULT_QUERY_RESPONSE_INTERVAL;
    update_computed_metrics (metrics);
}

void
init_metrics (struct igmp_metrics *metrics)
{
    reset_metrics (metrics);
}

void
update_timers (const struct context *ctx, struct group *grp,
               struct group_member *member)
{
    member->group_membership_timer_end =
        ctx->ref_time + ctx->metrics.group_membership_interval;

    if (member->group_membership_timer_end > grp->max_group_membership_timer_end)
    {
        grp->max_group_membership_timer_end = member->group_membership_timer_end;
    }

    grp->last_report_time = ctx->ref_time;
    member->last_report_time = ctx->ref_time;
}

void
update_timer_query_tx (struct context *ctx)
{
    /* The query should be sent when the [Other Querier Present] timer expires.
     * But when this happens on each plug/board/whatever running igmp_snoopd,
     * each one of them may send a query. Which means, there may be many queries
     * sent on each timer expiration.
     * A random delay is added to the [Other Querier Present Interval] used to
     * set the timer. The purpose is to make that timer expire at slightly
     * different moments in each igmp_snoopd.
     * So the igmp_snoopd where the first expiration happens will send a query
     * and the other ones will see it and will not send theirs. */

    /* The random delay is chosen in microseconds, to have at least a
     * 1 microsecond gap between the delays on each igmp_snoopd. */
    long int delay_usec = random () % ONE_SEC_IN_USEC;

    set_timer_delay (&ctx->timer_query_tx, 0, delay_usec * ONE_USEC_IN_NSEC);
}

void
update_timer_querier_presence (struct context *ctx)
{
    time_t timer_end = ctx->now + ctx->metrics.other_querier_present_interval;

    set_timer_end (&ctx->timer_querier_presence, timer_end, 0);
}

void
update_timer_membership_timeout (struct context *ctx)
{
    time_t delay_sec;

    if (ctx->is_querier_present)
    {
        delay_sec = ctx->metrics.query_response_interval;
    }
    else
    {
        delay_sec = NO_QUERIER_MEMBERSHIP_TIMEOUT_CHECK_INTERVAL;
    }

    time_t timer_end = ctx->now + delay_sec;

    set_timer_end (&ctx->timer_membership_timeout, timer_end, 0);
}

/**
 * Determine the Group Compatibility Mode of the group.
 * \param  ctx  The context.
 * \param  grp  The group.
 * \return  GROUP_COMPAT_MODE_IGMP_V2, if the group is in v2 compatibility mode.
 *          GROUP_COMPAT_MODE_IGMP_V3, if the group is in v3 compatibility mode.
 */
static group_compatibility_mode_t
group_compat_mode (const struct context *ctx, const struct group *grp)
{
    if (ctx->is_querier_present)
    {
        if ((grp->v2_host_present_timer_end == 0)
            || (ctx->now > grp->v2_host_present_timer_end))
        {
            return GROUP_COMPAT_MODE_IGMP_V3;
        }
        else
        {
            return GROUP_COMPAT_MODE_IGMP_V2;
        }
    }
    else
    {
        if ((grp->last_v2_report_time == 0)
            || (ctx->now > (grp->last_v2_report_time +
                            NO_QUERIER_OLDER_HOST_PRESENT_INTERVAL)))

        {
            return GROUP_COMPAT_MODE_IGMP_V3;
        }
        else
        {
            return GROUP_COMPAT_MODE_IGMP_V2;
        }
    }
}

void
check_membership_timeout (struct context *ctx)
{
    struct group *grp = LIST_FIRST (&ctx->groups_head);
    while (grp)
    {
        struct group *next_grp = LIST_NEXT (grp, groups);

        bool is_inactive_group;

        if (ctx->is_querier_present)
        {
            is_inactive_group =
                (ctx->ref_time > grp->max_group_membership_timer_end);
        }
        else
        {
            is_inactive_group =
                (ctx->ref_time > (grp->last_report_time +
                             NO_QUERIER_GROUP_MEMBERSHIP_INTERVAL));
        }

        if (is_inactive_group)
        {
            del_group (grp);
        }
        else if (group_compat_mode (ctx, grp) == GROUP_COMPAT_MODE_IGMP_V3)
        {
            /* We only look for inactive members if the group is in V3 compat
             * mode.
             * If the group is in V2 compatibility mode, it means it has some
             * IGMPv2 members. Because v2 members may suppress their answer to
             * a query if they see an answer report sent by another host for
             * the same group, we can not determine that they are inactive in
             * the same way we do for IGMPv3 hosts. */

            struct group_member *member = LIST_FIRST (&grp->members_head);
            while (member)
            {
                struct group_member *next_member = LIST_NEXT (member, members);

                bool is_inactive_member;

                if (ctx->is_querier_present)
                {
                    is_inactive_member =
                        (ctx->ref_time > member->group_membership_timer_end);
                }
                else
                {
                    is_inactive_member =
                        (ctx->ref_time > (member->last_report_time +
                                        NO_QUERIER_GROUP_MEMBERSHIP_INTERVAL));
                }

                if (is_inactive_member)
                {
                    del_group_member (member);
                }

                member = next_member;
            }
            /* No need to check if the group is now empty after the deletion
             * of all its members, because we have: grp->max_group_membership_timer_end is
             * the furthest member->group_membership_timer_end and grp->last_report_time is
             * the most recent member->last_report_time, then if all members
             * were inactive, we should have found is_inactive_group == true */
        }

        grp = next_grp;
    }
}

void
dump_info (const struct igmp_metrics *metrics)
{
    log_debug ("Robustness Variable: %u", metrics->robustness_variable);
    log_debug ("Query Interval: %lu", (unsigned long)metrics->query_interval);
    log_debug ("Query Response Interval: %lu",
               (unsigned long)metrics->query_response_interval);
    log_debug ("Group Membership Interval: %lu",
               (unsigned long)metrics->group_membership_interval);
    log_debug ("Other Querier Present Interval: %lu",
               (unsigned long)metrics->other_querier_present_interval);
    log_debug ("Older Host Present Interval: %lu",
               (unsigned long)metrics->older_host_present_interval);
}

void
process_query (struct context *ctx, struct igmphdr *igmp_header, unsigned char *msg_end)
{
    log_debug ("Query");

    /* RFC 3376 Section 7.1. Query Version Distinctions */

    unsigned char *start = (unsigned char *)igmp_header;

    size_t query_len = msg_end - start;

    if (query_len == 8)
    {
        if (igmp_header->code == 0)
        {
            process_v1_query ();
        }
        else
        {
            process_v2_query (ctx, igmp_header);
        }
    }
    else if (query_len >= 12)
    {
        process_v3_query (ctx, start, msg_end);
    }
    else
    {
        /* RFC: "silently ignored" */
    }
}

bool
process_igmp_packet (struct context *ctx,
                     const unsigned char eth_source_addr[ETH_ALEN],
                     unsigned char *igmp_start,
                     unsigned char *msg_end)
{
    struct igmphdr *igmp_header = (struct igmphdr *) (igmp_start);

    if (MSG_TOO_SMALL (igmp_header, sizeof (struct igmphdr), msg_end))
    {
        log_error ("Message too small to contain IGMP header");
        return false;
    }

    if (inet_checksum ((const unsigned char *)igmp_header, msg_end - (unsigned char *)igmp_header) != 0)
    {
        log_error ("Bad IGMP checksum");
        return false;
    }

    log_debug ("IGMP ");

    switch (igmp_header->type)
    {
    case IGMP_HOST_MEMBERSHIP_QUERY:
        process_query (ctx, igmp_header, msg_end);
        break;

    case IGMPV1_HOST_MEMBERSHIP_REPORT:
        process_v1_report ();
        break;

    case IGMPV2_HOST_MEMBERSHIP_REPORT:
        process_v2_report (ctx, eth_source_addr, igmp_header);
        break;

    case IGMPV2_HOST_LEAVE_MESSAGE:
        process_v2_leave (ctx, eth_source_addr, igmp_header);
        break;

    case IGMPV3_HOST_MEMBERSHIP_REPORT:
        /* TODO: ignore this v3 report, if current version on the network is v2 or v1 ?  */
        process_v3_report (ctx, eth_source_addr, (unsigned char *)igmp_header, msg_end);
        break;
    }

    log_debug (" ");

    return true;
}