summaryrefslogtreecommitdiff
path: root/cleopatre/application/igmp_snoopd/src/main.c
blob: ce12a5634160fa8fad5be76f6896e04400bcf113 (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
#include <assert.h>
#include <errno.h>
#include <linux/filter.h>
#include <linux/igmp.h>
#include <netinet/ip.h>
#include <netpacket/packet.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "libspid.h"

#include "data_struct.h"
#include "igmp_common.h"
#include "internal.h"
#include "utils.h"
#include "write_file.h"

#define INTERFACE_NAME      "br0"

#define MSG_LEN  ETH_FRAME_LEN  /* TODO: Check that this is the right size */

/* 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

/**
 * Process an IGMP Query.
 * \param  ctx  The context.
 * \param  igmp_header  The IGMP Header in the message.
 * \param  msg_end  The end of the message.
 */
static 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 (start);
        }
        else
        {
            process_v2_query (start);
        }
    }
    else if (query_len >= 12)
    {
        process_v3_query (ctx, start, msg_end);
    }
    else
    {
        /* RFC: "silently ignored" */
    }
}

/**
 * Process a packet.
 * \param  ctx  The context.
 * \param  msg  The received message.
 * \param  len  The length of the received message.
 * \return  true, if the message was of interest.
 *          false, otherwise (eg: packet discarded).
 */
static bool
process_packet (struct context *ctx, unsigned char *msg, size_t len)
{
    log_debug ("New packet");

    unsigned char * msg_end = msg + len;

    struct ethhdr *eth_header = (struct ethhdr *) msg;

    /* It's an "assert" instead of an "if", because thanks to the Linux Socket Filter,
     * we should only receive IGMP packets */
    assert ( ! MSG_TOO_SMALL (eth_header, sizeof (struct ethhdr), msg_end));

    struct iphdr *ip_header = (struct iphdr *) (msg + sizeof (struct ethhdr));

    assert ( ! MSG_TOO_SMALL (ip_header, sizeof (struct iphdr), msg_end));

    if (inet_checksum ((const unsigned char *)ip_header, ip_header->ihl*4) != 0)
    {
        log_error ("Bad IP checksum");
        return false;
    }

    assert (ip_header->protocol == IPPROTO_IGMP);

    struct igmphdr *igmp_header = (struct igmphdr *) ((unsigned char *)ip_header + (ip_header->ihl * 4));
    /* multiplied by 4 because ihl is the length of the internet header in 32 bit words */

    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;
    }

    char mac_str[LIBSPID_MAC_STR_LEN];
    (void)libspid_mac_bin_to_str (eth_header->h_source, mac_str);
    log_debug ("From %s", mac_str);
    (void)libspid_mac_bin_to_str (eth_header->h_dest, mac_str);
    log_debug ("To   %s", mac_str);

    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 ();
        break;

    case IGMPV2_HOST_LEAVE_MESSAGE:
        process_v2_leave ();
        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_header->h_source, (unsigned char *)igmp_header, msg_end);
        break;
    }

    log_debug (" ");

    return true;
}


/**
 * Create and bind the socket used for snooping.
 * \return  The socket file descriptor, on success.
 *          -1, on error.
 */
static int
create_and_bind_socket (const char *ifname)
{
    /* We want the MAC addresses, so AF_PACKET and SOCK_RAW
     * Although we only want the IP Packets, we set ETH_P_ALL here
     * and let the Linux Socket Filter do the filtering on IP and IGMP */
    int domain = AF_PACKET;
    int protocol = htons (ETH_P_ALL);
    int sockfd = socket (domain, SOCK_RAW, protocol);
    if (sockfd == -1)
    {
        log_error ("socket: %s", strerror (errno));
        return -1;
    }

    /* TODO: Set interface to multicast promiscuous mode ? ALLMULTI ? */

    /* A Linux Socket Filter to let the Kernel do the filtering on IGMP.
     * So our code will only receive IGMP packets. */
    struct sock_filter filter[] =
    {
        /* We first check that the packet is an IP packet,
         * then that it's an IGMP packet.
         * (A is the accumulator) */

        /* A = eth_header->hproto */
        BPF_STMT (BPF_LD | BPF_H | BPF_ABS, offsetof (struct ethhdr, h_proto)),

        /* TODO: Handle VLAN tagging (IEEE 802.1Q) ? */

        /* (A == ETH_P_IP) ? goto Accept : goto Ignore */
        BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, ETH_P_IP, 0, 3),

        /*  A = ip_header->protocol */
        BPF_STMT (BPF_LD | BPF_B | BPF_ABS,  sizeof (struct ethhdr) + offsetof (struct iphdr, protocol)),

        /* (A == IPPROTO_IGMP) ? goto Accept : goto Ignore */
        BPF_JUMP (BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_IGMP, 0, 1),

        /* Accept: */
        /* Pass this packet to our code */
        BPF_STMT (BPF_RET | BPF_K, MSG_LEN),

        /* Ignore: */
        /* Don't pass this packet to our code */
        BPF_STMT (BPF_RET | BPF_K, 0)
    };

    struct sock_fprog fprog =
    {
        .len = sizeof (filter)/sizeof (struct sock_filter),
        .filter = filter
    };

    if (setsockopt (sockfd, SOL_SOCKET, SO_ATTACH_FILTER,
                   (const void *)&fprog, (socklen_t)sizeof (struct sock_fprog)) == -1)
    {
        log_error ("setsockopt: %s", strerror (errno));
        return -1;
    }

    /* TODO: bind really needed ? */
    struct sockaddr_ll sock_addr;
    sock_addr.sll_family = domain;
    sock_addr.sll_protocol = protocol;
    sock_addr.sll_ifindex = get_ifindex (sockfd, ifname);

    if (bind (sockfd, (struct sockaddr *) &sock_addr, sizeof (sock_addr)) == -1)
    {
        log_error ("bind: %s", strerror (errno));
        return -1;
    }

    return sockfd;
}

/**
 * Initialize.
 * \param  ctx  The context.
 */
static void
init (struct context *ctx)
{
    LIST_INIT (&ctx->groups_head);

    init_v3 (&ctx->metrics);

    ctx->multicast_info = NULL;
}

/**
 * Dump info about current state.
 * \param  ctx  The context.
 */
static void
dump (const struct context *ctx)
{
    dump_info_v3 (&ctx->metrics);
    dump_groups (&ctx->groups_head);
}

/**
 * Start IGMP Snooping.
 * \param  ctx  The context.
 */
static void
igmp_snooping (const char *ifname, const char *mcast_info_file, struct context *ctx)
{
    int sockfd = create_and_bind_socket (ifname);
    if (sockfd == -1)
    {
        exit (EXIT_FAILURE);
    }

    while (true)
    {
        unsigned char msg[MSG_LEN];
        ssize_t len = recvfrom (sockfd, msg, sizeof (msg), 0, NULL, NULL);
        if (len == -1)
        {
            log_error ("recvfrom: %s", strerror (errno));
        }

        bool was_of_interest = process_packet (ctx, msg, (size_t)len);

        if (was_of_interest)
        {
            del_inactive (ctx);
            dump (ctx);

            bool should_update_mcast_info_file
                    = update_multicast_info (&ctx->multicast_info, &ctx->groups_head);

            if (should_update_mcast_info_file)
            {
                write_to_file (mcast_info_file, ctx->multicast_info);
                libspid_error_t status = libspid_system_file_update_warn (getpid (), mcast_info_file);
                if (status != LIBSPID_SUCCESS)
                {
                    log_error ("libspid_system_file_update_warn: %d", status);
                }
            }
            else
            {
                log_debug ("no need to update multicast file");
            }
        }
        else
        {
            log_debug ("packet was not of interest");
        }
    }
}

int
main (void)
{
    struct context ctx;

    init (&ctx);

    init_log ("igmp_snoopd");

    igmp_snooping (INTERFACE_NAME, LIBSPID_MCAST_INFO_PATH, &ctx); /* never returns */

    return 0;
}