summaryrefslogtreecommitdiff
path: root/cesar/interface/sniffer/src/sniffer.c
blob: faae0fc84f12e8d73d8729ee433db5f573087ab8 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    interface/sniffer/src/sniffer.c
 * \brief   Sniffer source functions.
 * \ingroup interface_sniffer
 *
 * « long description »
 */
#include "common/std.h"
#include "common/defs/spidcom.h"
#include "lib/swap.h"

#include "hal/hle/defs.h"

#include "interface/sniffer/sniffer.h"
#include "interface/interface_module.h"
#include "interface/sniffer/inc/context.h"
#include "interface/def.h"

/* The TX and Interface sniffer type data. */
#define INTERFACE_SNIFFER_IDENTIFY_LENGTH 2

/* Static declaration. */
static interface_sniffer_t sniffer_global;

/** Configure the sniffer.
 * \param  ctx the sniffer context
 * \param  data the data to configure the sniffer.
 */
static inline void
interface_sniffer_configure (interface_sniffer_t *ctx, uint data)
{
    dbg_assert (ctx);

    ctx->sniff_mme_tx = BF_GET(SNIFFER_REG__SNIFF_MME_TX, data);
    ctx->sniff_mme_rx = BF_GET(SNIFFER_REG__SNIFF_MME_RX, data);
    ctx->sniff_beacon_tx = BF_GET(SNIFFER_REG__SNIFF_BEACON_TX, data);
    ctx->sniff_beacon_rx = BF_GET(SNIFFER_REG__SNIFF_BEACON_RX, data);
}

interface_sniffer_t*
interface_sniffer_init (interface_sniffer_send_message_cb_t cb, void *user_data,
                        mac_config_t *mac_config)
{
    dbg_assert (cb);

    sniffer_global.sniff_beacon_tx = false;
    sniffer_global.sniff_beacon_rx = false;
    sniffer_global.sniff_mme_tx = false;
    sniffer_global.sniff_mme_rx = false;

    sniffer_global.send_func = cb;
    sniffer_global.send_user_data = user_data;
    sniffer_global.mac_config = mac_config;

    return &sniffer_global;
}

/** Uninitalise the sniffer.
 * \param  ctx the sniffer context.
 */
void
interface_sniffer_uninit (interface_sniffer_t *ctx)
{
    dbg_assert (ctx);
}

/** Copy a MME to the buffer and request the interface to send the MME.
 * \param  ctx the sniffer context.
 * \param  mme the MME buffer
 * \param  length the MME length
 * \param  buffer the destination buffer.
 * \param  tx the MME way (TX/RX)
 */
void
interface_sniffer_copy_mme (interface_sniffer_t *ctx, u8 *mme, uint length,
                            u8 *buffer, bool tx, bool encrypted)
{
    bitstream_t bitstream;
    uint header_length;
    uint embedded_length;
    uint offset;
    uint word[2];

    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));
    dbg_assert (buffer);
    dbg_assert (ctx->send_func);

    /* Compute the header size. */
    if (ctx->vlan_tag == 0)
        header_length = HPAV_MME_HEADER + OUI_SIZE +
            INTERFACE_SNIFFER_IDENTIFY_LENGTH;
    else
        header_length = HPAV_MME_HEADER_LEN_WITH_VLAN + OUI_SIZE
            + INTERFACE_SNIFFER_IDENTIFY_LENGTH;

    if (length + header_length > ETH_PACKET_MAX_SIZE)
        embedded_length = ETH_PACKET_MAX_SIZE - header_length;
    else
        embedded_length = length;

    /* Create the header of the MME. */
    bitstream_write_init (&bitstream, buffer, header_length);
    bitstream_write_large (&bitstream, ctx->da, 48);
    bitstream_write_large (&bitstream, ctx->mac_config->sta_mac_address, 48);
    if (ctx->vlan_tag)
        bitstream_write (&bitstream, ctx->vlan_tag, 32);
    bitstream_write (&bitstream, swap16(HPAV_MTYPE_MME), 16);
    bitstream_write (&bitstream, HPAV_MMV, 8);
    bitstream_write (&bitstream, VS_SNIFFER_IND, 16);
    bitstream_write (&bitstream, 0, 16);
    bitstream_write (&bitstream, SPC_OUI, OUI_SIZE_BITS);

    bitstream_write (&bitstream, INTERFACE_SNIFFER_TYPE_MME, 8);
    /* 0 for TX, 1 for RX. */
    bitstream_write (&bitstream, !tx, 8);
    offset = bitstream_finalise (&bitstream) / 8;
    bitstream_memcpy (buffer + offset, mme, embedded_length);

    word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE, HLE_MSG_TYPE_INTERFACE),
	             (MSG_LENGTH, 1),
                     (PARAM_INTERFACE_TYPE, INTERFACE_MODULE_SNIFFER),
                     (PARAM_INTERFACE_LENGTH, length + offset));

    word[1] = (uint)buffer;


    /** Request the interface to send the message to the linux driver. */
    (*ctx->send_func) (ctx->send_user_data, word, 2);
}

void
interface_sniffer_copy_beacon (
    interface_sniffer_t *ctx, bsu_beacon_t *beacon, u8 *buffer, bool tx)
{
    bitstream_t bitstream;
    uint length;
    uint word[2];
    length = INTERFACE_BEACON_SIZE + sizeof (pbproc_rx_beacon_params_t);
    // Fill the buffer header.
    bitstream_init (&bitstream, buffer,
                    length + HPAV_MME_HEADER + INTERFACE_MME_HEADER_SIZE +
                    OUI_SIZE,
                    BITSTREAM_WRITE);
    bitstream_write_large (&bitstream, ctx->da, 48);
    bitstream_write_large (&bitstream, ctx->mac_config->sta_mac_address, 48);
    if (ctx->vlan_tag)
        bitstream_write (&bitstream, ctx->vlan_tag, 32);
    bitstream_write (&bitstream, swap16(HPAV_MTYPE_MME), 16);
    bitstream_write (&bitstream, HPAV_MMV, 8);
    bitstream_write (&bitstream, VS_SNIFFER_IND, 16);
    bitstream_write (&bitstream, 0, 16);
    bitstream_write (&bitstream, SPC_OUI, OUI_SIZE_BITS);
    bitstream_write (&bitstream, INTERFACE_SNIFFER_TYPE_BEACON, 8);
    /* 0 for TX, 1 for RX. */
    bitstream_write (&bitstream, !tx, 8);
    u8 *data = buffer + bitstream_written_bits (&bitstream) / 8;
    bitstream_write_large (&bitstream, beacon->vf.nid, 54);
    bsu_beacon_write_bitstream_initialised_no_nid (beacon, &bitstream, data);
    bitstream_finalise (&bitstream);

    word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE, HLE_MSG_TYPE_INTERFACE),
	             (MSG_LENGTH, 1),
                     (PARAM_INTERFACE_TYPE, INTERFACE_MODULE_SNIFFER),
                     (PARAM_INTERFACE_LENGTH, length));

    word[1] = (uint)buffer;


    /** Request the interface to send the message to the linux driver. */
    (*ctx->send_func) (ctx->send_user_data, word, 2);
}


/** Provides the MME sniff status.
 * \param  ctx the sniffer context.
 * \param  tx the way.
 * \return  the MME sniff status.
 */
bool
interface_sniffer_mme_status (interface_sniffer_t *ctx, bool tx)
{
    dbg_assert (ctx);

    if (tx)
        return ctx->sniff_mme_tx;
    else
        return ctx->sniff_mme_rx;
}

/** Provides the beacon sniff status.
 * \param  ctx the sniffer context.
 * \param  tx  the way.
 * \return  the beacon sniff status.
 */
bool
interface_sniffer_beacon_status (interface_sniffer_t *ctx, bool tx)
{
    dbg_assert (ctx);

    if (tx)
        return ctx->sniff_beacon_tx;
    else
        return ctx->sniff_beacon_rx;
}

/** Return the status as the same format as received in the configuration
 * status.
 * \param  ctx  the interface sniffer context.
 * \return  the status.
 */
uint
interface_sniffer_status (interface_sniffer_t *ctx)
{
    uint res;
    dbg_assert (ctx);

    res = BF_FILL (SNIFFER_REG, (SNIFF_MME_TX, ctx->sniff_mme_tx),
                    (SNIFF_MME_RX, ctx->sniff_mme_rx),
                    (SNIFF_BEACON_TX, ctx->sniff_beacon_tx),
                    (SNIFF_BEACON_RX, ctx->sniff_beacon_rx));

    return res;
}

void
interface_sniffer_configure_and_respond (interface_sniffer_t *ctx, u8 *buffer,
                                         u8 *resp)
{
    uint vlan_value = 0;
    mac_t osa = 0;
    uint data = 0;
    bool ok = false;
    uint oui;
    bitstream_t stream;

    dbg_assert (ctx);
    dbg_assert (ctx->mac_config);
    dbg_assert (resp);
    dbg_assert (buffer);
    dbg_assert (buffer != resp);

    osa = bitstream_direct_read_large (buffer, 48, 48);
    if (bitstream_direct_read (buffer, 96, 16) == swap16(HPAV_MTYPE_MME))
    {
        oui = bitstream_direct_read (buffer, HPAV_MME_HEADER * 8,
                                     OUI_SIZE_BITS);
        data = bitstream_direct_read (buffer, HPAV_MME_HEADER * 8 +
                                      OUI_SIZE_BITS, 8);
    }
    else
    {
        vlan_value = bitstream_direct_read (buffer, 96, 32);
        oui = bitstream_direct_read (buffer, HPAV_MME_HEADER_LEN_WITH_VLAN * 8,
                                     OUI_SIZE_BITS);
        data = bitstream_direct_read (buffer,
                                      HPAV_MME_HEADER_LEN_WITH_VLAN * 8 +
                                      OUI_SIZE_BITS,
                                      8);
    }

    /* If the sniffer is activated, only the computer which had activated it
     * can change the sniffer mode, the others should only stop the sniffer. */
    if (((ctx->da == 0)
         || (ctx->da == osa)
         || ((ctx->da != osa) && (data == 0x0)))
        && (oui == SPC_OUI))
    {
        ctx->da = osa;
        ctx->vlan_tag = vlan_value;
        interface_sniffer_configure (ctx, data);
        ok = true;
    }

    /* Prepare the answer. */
    bitstream_write_init (&stream, resp, ETH_PACKET_MAX_SIZE);
    bitstream_write_large (&stream, osa, 48);
    bitstream_write_large (&stream, ctx->mac_config->sta_mac_address, 48);

    if (vlan_value)
        bitstream_write (&stream, vlan_value, 32);

    bitstream_write (&stream, swap16(HPAV_MTYPE_MME), 16);
    bitstream_write (&stream, HPAV_MMV, 8);
    bitstream_write (&stream, VS_SNIFFER_CNF, 16);
    bitstream_write (&stream, 0, 16);
    bitstream_write (&stream, SPC_OUI, OUI_SIZE_BITS);
    bitstream_write (&stream, !(ok && (data == interface_sniffer_status (ctx))),
                     8);
    bitstream_write (&stream, interface_sniffer_status (ctx), 8);
    bitstream_write_large (&stream, ctx->da, 48);
    bitstream_finalise (&stream);
}