summaryrefslogtreecommitdiff
path: root/cesar/interface/sniffer/src/sniffer.c
blob: e0722496c54cc9fc2fac87b8e322eb6f4a55a863 (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
/* 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 "lib/swap.h"

#include "hal/hle/defs.h"

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

/* Static declaration. */
static interface_sniffer_t sniffer_global;

/** Initialise the sniffer and all the callback of the sniffer.
 * \param  cb the function to call when the sniffer needs to send a message.
 * \param  user_data the data to provide on each function callback provided.
 */
interface_sniffer_t*
interface_sniffer_init (interface_sniffer_send_message_cb_t cb, void *user_data)
{
    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;

    return &sniffer_global;
}


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

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

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


/** 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)
{
    uint word[2];

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

    bitstream_memcpy (buffer, mme, 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));

    word[1] = (uint)buffer;


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

/** Copy a beacon to the buffer and request the interface to send the copied beacon.
 * Encapsulate the beacon in a MME.
 * \param  ctx the sniffer context.
 * \param  beacon the beacon
 * \param  buffer the destination buffer.
 * \param  tx the beacon way (TX/RX)
 * \param  mac_address  the station mac address to fill the OSA ODA in the
 * MME.
 */
void
interface_sniffer_copy_beacon (interface_sniffer_t *ctx, pb_beacon_t *beacon,
                               u8 *buffer, bool tx, mac_t mac_address)
{
    bitstream_t bitstream;
    uint data;
    uint length;
    uint word[2];

    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (beacon);

    length = 136 +  sizeof (pbproc_rx_beacon_params_t);

    // Fill the buffer header.
    bitstream_init (&bitstream, buffer, 27, BITSTREAM_WRITE);
    bitstream_access (&bitstream, &mac_address, 48);
    bitstream_access (&bitstream, &mac_address, 48);

    /* Inserting the MTYPE. */
    data = htons(HPAV_MTYPE_MME);
    bitstream_access (&bitstream, &data, 16);

    /* Inserting the MMV. */
    data = HPAV_MMV;
    bitstream_access (&bitstream, &data, 8);

    /* Inserting the MMTYPE. */
    data = 0xA036;
    bitstream_access (&bitstream, &data, 16);

    /* Inserting the FMI. */
    data = 0;
    bitstream_access (&bitstream, &data, 16);

    /* Inserting the module type. */
    data = INTERFACE_MODULE_SNIFFER;
    bitstream_access (&bitstream, &data, 8);

    /* Inserting data type i.e. beacon == 0 */
    data = 0;
    bitstream_access (&bitstream, &data, 8);

    /* Inserting length. */
    bitstream_access (&bitstream, &length, 16);

    /* inserting the first data word of the beacon. */
    bitstream_access (&bitstream, &beacon->first_data_word, 32);
    bitstream_finalise (&bitstream);

    /* Make a copy of the full beacon. */
    bitstream_memcpy (buffer + bitstream_bytes_processed(&bitstream),
                      beacon->data, length - 4);


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