summaryrefslogtreecommitdiff
path: root/cesar/cp/msg/src/msg_drv.c
blob: f88aa626946cccdac6d2132f2009a5880170c930 (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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/msg/src/msg_drv.c
 * \brief   MME Driver.
 * \ingroup cp_msg
 *
 */
#include "common/std.h"
#include "common/defs/ethernet.h"
#include "cp/cp.h"
#include "cp/mme.h"
#include "cp/msg/msg.h"

#include "cp/msg/inc/msg_drv.h"
#include "cp/msg/inc/msg.h"

/**
 * Receive a DRV_STA_SET_MAC_ADDR.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  mac  received mac address
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_mac_addr_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                         mac_t *mac)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (mac);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        bitstream_access (&mme->bitstream, mac,
                          BYTES_SIZE_TO_BITS(ETH_MAC_ADDRESS_SIZE));

        if (MAC_IS_VALID(*mac))
            return true;
    }
    return false;
}

/**
 * Receive a DRV_STA_SET_CCO_PREF.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  cco_pref  received CCo preferred flag
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_cco_pref_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                         bool *cco_pref)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (cco_pref);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        bitstream_access (&mme->bitstream, cco_pref, 1);
        return true;
    }
    return false;
}

/**
 * Receive a DRV_STA_SET_WAS_CCO.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  was_cco  received was CCo flag
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_was_cco_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                        bool *was_cco)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (was_cco);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        bitstream_access (&mme->bitstream, was_cco, 1);
        return true;
    }
    return false;
}

/**
 * Process a string.
 * \param  ctx  the Control plane context.
 * \param  mme  the MME context.
 * \param  buffer  the buffer to store the data in.
 * \param  min_length  the payload min length mandatory.
 * \param  max_length  the payload max length mandatory.
 * \retun  true if the payload is correct, false if corrupted.
 */
bool
cp_msg_drv_sta_string_recv (cp_t *ctx, cp_mme_rx_t *mme, char *buffer,
                            uint min_length, uint max_length)
{
    uint i;
    bool ok;

    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (buffer);
    dbg_assert (max_length);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        bitstream_access_buf (&mme->bitstream, (u8 *)buffer, max_length);
        buffer[max_length] = 0;

        for (i = 0; i < max_length; i++)
        {
            if ((((u8) buffer[i]) < 32) || (((u8) buffer[i]) > 127))
            {
                ok = (i < min_length ? false : true);
                break;
            }
        }

        if ((ok == false)
            || (!cp_msg_mme_read_end_check (ctx, mme)))
            return false;
        return true;
    }
    return false;
}

/**
 * Receive a DRV_STA_SET_NPW.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  npw  received NPW
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_npw_req_receive (cp_t *ctx, cp_mme_rx_t *mme, char *npw)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (npw);

    return cp_msg_drv_sta_string_recv (ctx, mme, npw, CP_NPW_MIN_SIZE,
                                       CP_NPW_MAX_SIZE);
}

/**
 * Receive a DRV_STA_SET_DPW.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  dpw  received device password
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_dpw_req_receive (cp_t *ctx, cp_mme_rx_t *mme, char *dpw)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (dpw);

    return cp_msg_drv_sta_string_recv (ctx, mme, dpw, CP_DPW_MIN_SIZE,
                                       CP_DPW_MAX_SIZE);
}

/**
 * Receive a DRV_STA_SET_SL.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  sl  received SL
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_sl_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                   cp_security_level_t *sl)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (sl);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        bitstream_access (&mme->bitstream, sl, 8);

        if (*sl >= CP_SECURITY_LEVEL_NB)
            return false;
        return true;
    }
    return false;
}

/**
 * Receive a DRV_STA_SET_NID.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  nid  received NID
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_nid_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                    cp_nid_t *nid)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (nid);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        bitstream_access (&mme->bitstream, nid, 56);

        if (*nid >> CP_NID_SIZE_BITS)
            return false;
        return true;
    }
    return false;
}

/**
 * Receive a DRV_STA_SET_M_STA_HFID.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  m_sta_hfid  received manufacturer STA HFID
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_m_sta_hfid_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                           char *m_sta_hfid)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (m_sta_hfid);

    return cp_msg_drv_sta_string_recv (ctx, mme, m_sta_hfid, 0,
                                       CP_HFID_SIZE);
}

/**
 * Receive a DRV_STA_SET_U_STA_HFID.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  u_sta_hfid  received user STA HFID
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_u_sta_hfid_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                           char *u_sta_hfid)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (u_sta_hfid);

    return cp_msg_drv_sta_string_recv (ctx, mme, u_sta_hfid, 0,
                                       CP_HFID_SIZE);
}

/**
 * Receive a DRV_STA_SET_AVLN_HFID.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  avln_hfid  received AVLN HFID
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_avln_hfid_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                          char *avln_hfid)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (avln_hfid);

    return cp_msg_drv_sta_string_recv (ctx, mme, avln_hfid, 0,
                                       CP_HFID_SIZE);
}

/**
 * Receive a DRV_STA_SET_TONEMASK.REQ.
 * FIXME
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  tonemask  received tonemask
 * \return  true on success
 */
bool
cp_msg_drv_sta_set_tonemask_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                         u32 *tonemask)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (tonemask);

    return true;
}

/**
 * Receive a DRV_STA_MAC_START.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \return  true on success
 */
bool
cp_msg_drv_sta_mac_start_req_receive (cp_t *ctx, cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    return cp_msg_mme_read_end_check (ctx, mme);
}

bool
cp_msg_drv_sta_sc_req_receive (cp_t *ctx, cp_mme_rx_t *mme, bool *sc_join)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (sc_join);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        /* Get SC_JOIN field. */
        bitstream_access (&mme->bitstream, sc_join, 8);

        /* Reseved value, between 0x02 - 0xFF. */
        if (*sc_join > 1)
            return false;
        return true;
    }
    return false;
}

/**
 * Receive a DRV_STA_MAC_STOP.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \return  true on success
 */
bool
cp_msg_drv_sta_mac_stop_req_receive (cp_t *ctx, cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    return true;
}

bool
cp_msg_drv_sta_set_key_req_receive (cp_t *ctx, cp_mme_rx_t *mme, u8 *nmk,
                                enum cp_msg_drv_sta_set_key_type_t *type,
                                cp_nid_t *nid,
                                cp_security_level_t *sl)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (nmk);
    dbg_assert (type);
    dbg_assert (nid);
    dbg_assert (sl);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        bitstream_read_buf (&mme->bitstream, nmk, 16);
        *type = bitstream_read (&mme->bitstream, 8);
        *nid = bitstream_read_large (&mme->bitstream, 56);
        *sl = bitstream_read (&mme->bitstream, 8);

        if ((*type >= CP_MSG_DRV_STA_SET_KEY_TYPE_NB)
            || (*sl >= CP_SECURITY_LEVEL_NB)
            || (*nid >> CP_NID_SIZE_BITS))
            return false;
        return true;
    }
    return false;
}

void
cp_msg_drv_sta_set_key_ind_send (cp_t *ctx, cp_mme_peer_t *peer, u8 *nmk,
                                enum cp_msg_drv_sta_set_key_type_t type,
                                cp_nid_t nid,
                                cp_security_level_t sl)
{
    cp_mme_tx_t *tx;

    dbg_assert (ctx);
    dbg_assert (peer);
    dbg_assert (nmk);

    tx = cp_msg_mme_init (ctx, peer, DRV_STA_SET_KEY_IND);
    dbg_assert (tx);

    bitstream_write_buf (&tx->bitstream, nmk, CP_NMK_SIZE);
    bitstream_write (&tx->bitstream, type, 8);
    bitstream_write_large (&tx->bitstream, nid, 56);
    bitstream_write (&tx->bitstream, sl, 8);

    cp_msg_mme_send (ctx, tx);
}

/**
 * Send a DRV_....CNF, works for any DRV messages.
 * \param  ctx  control plane context
 * \param  peer  peer information
 * \param  mmtype  MMTYPE to respond to
 * \param  result  result code
 */
void
cp_msg_drv_any_cnf_send (cp_t *ctx, cp_mme_peer_t *peer, cp_mmtype_t mmtype,
                         cp_msg_drv_result_t result)
{
    cp_mme_tx_t *msg;

    dbg_assert (peer);
    dbg_assert (mmtype);

    msg = cp_msg_mme_init (ctx, peer, mmtype);
    dbg_check (msg);

    bitstream_access (&msg->bitstream, &result, 8);

    cp_msg_mme_send (ctx, msg);
}