summaryrefslogtreecommitdiff
path: root/cesar/cp/sta/action/src/drv.c
blob: 36453f8ea48513a1060ddd3cf89d70ea6312e8df (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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/sta/action/src/drv.c
 * \brief   STA action, driver related definitions.
 * \ingroup cp_sta_action
 */
#include "common/std.h"

#include "action.h"

#include "cp/msg/msg.h"
#include "cp/inc/context.h"
#include "cp/fsm/fsm.h"
#include "lib/stats.h"
#include "common/defs/igmp.h"
#include "cl/cl_mactotei.h"
#include "cl/mcast.h"

#if CONFIG_CP_EOC
    #include "cp/eoc/sta/mgr/sta_own_data.h"
#endif

void
cp_sta_action_drv__stopped__drv_sta_set_mac_addr_req (cp_t *ctx,
                                                      cp_mme_rx_t *mme)
{
    mac_t mac;
    bool ok = cp_msg_drv_sta_set_mac_addr_req_receive (ctx, mme, &mac);
    if (ok)
        cp_sta_own_data_set_mac_address (ctx, mac);
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_MAC_ADDR_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_cco_pref_req (cp_t *ctx,
                                                      cp_mme_rx_t *mme)
{
    bool cco_pref;
    bool ok = cp_msg_drv_sta_set_cco_pref_req_receive (ctx, mme, &cco_pref);
    if (ok)
    {
        cp_sta_own_data_t *own = cp_sta_mgr_get_sta_own_data (ctx);
        own->cco_prefered = cco_pref;
    }
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_CCO_PREF_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_was_cco_req (cp_t *ctx,
                                                     cp_mme_rx_t *mme)
{
    bool was_cco;
    bool ok = cp_msg_drv_sta_set_was_cco_req_receive (ctx, mme, &was_cco);
    if (ok)
        cp_sta_own_data_set_was_cco (ctx, was_cco);
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_WAS_CCO_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_npw_req (cp_t *ctx,
                                                 cp_mme_rx_t *mme)
{
    char npw[CP_NPW_MAX_SIZE + 1];
    bool ok = cp_msg_drv_sta_set_npw_req_receive (ctx, mme, npw);
    if (ok)
    {
        /* Compute NID from received information. */
        cp_security_level_t sl;
        sl = cp_sta_own_data_get_security_level (ctx);
        cp_sta_own_data_set_npw (ctx, npw);
        cp_compute_nmk_and_nid_from_npw (ctx, npw, sl);
    }
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_NPW_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_dpw_req (cp_t *ctx,
                                                 cp_mme_rx_t *mme)
{
    char dpw[CP_DPW_MAX_SIZE + 1];
    bool ok = cp_msg_drv_sta_set_dpw_req_receive (ctx, mme, dpw);
    if (ok)
        cp_sta_own_data_set_dpw (ctx, dpw);
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_DPW_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_sl_req (cp_t *ctx,
                                                cp_mme_rx_t *mme)
{
    cp_security_level_t sl;
    bool ok = cp_msg_drv_sta_set_sl_req_receive (ctx, mme, &sl);
    if (ok)
    {
        /* Compute NID from received information. */
        const char *npw;
        npw = cp_sta_own_data_get_npw (ctx);

        cp_sta_own_data_set_security_level (ctx, sl);
        cp_compute_nmk_and_nid_from_npw (ctx, npw, sl);
    }
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_SL_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_nid_req (cp_t *ctx,
                                                 cp_mme_rx_t *mme)
{
    cp_nid_t nid;
    bool ok = cp_msg_drv_sta_set_nid_req_receive (ctx, mme, &nid);
    if (ok)
        cp_sta_own_data_set_nid (ctx, nid);
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_NID_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_m_sta_hfid_req (cp_t *ctx,
                                                        cp_mme_rx_t *mme)
{
    char m_sta_hfid[CP_HFID_SIZE + 1];
    bool ok = cp_msg_drv_sta_set_m_sta_hfid_req_receive (ctx, mme,
                                                         m_sta_hfid);
    if (ok)
        cp_sta_own_data_set_hfid_manufacturer (ctx, m_sta_hfid);
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_M_STA_HFID_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_u_sta_hfid_req (cp_t *ctx,
                                                        cp_mme_rx_t *mme)
{
    char u_sta_hfid[CP_HFID_SIZE + 1];
    bool ok = cp_msg_drv_sta_set_u_sta_hfid_req_receive (ctx, mme,
                                                         u_sta_hfid);
    if (ok)
        cp_sta_own_data_set_hfid_user (ctx, u_sta_hfid);
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_U_STA_HFID_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_set_avln_hfid_req (cp_t *ctx,
                                                       cp_mme_rx_t *mme)
{
    char avln_hfid[CP_HFID_SIZE + 1];
    bool ok = cp_msg_drv_sta_set_avln_hfid_req_receive (ctx, mme, avln_hfid);
    if (ok)
        cp_sta_own_data_set_hfid_avln (ctx, avln_hfid);
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_AVLN_HFID_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);
}

void
cp_sta_action_drv__stopped__drv_sta_mac_start_req (cp_t *ctx,
                                                   cp_mme_rx_t *mme)
{
    bool ok = cp_msg_drv_sta_mac_start_req_receive (ctx, mme);
    if (ok)
    {
        /* Start power-on procedure. */
        cp_sta_action_poweron_start (ctx);
    }
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_MAC_START_CNF,
                             ok ? CP_MSG_DRV_RESULT_SUCCESS
                             : CP_MSG_DRV_RESULT_FAILURE);

}

void
cp_sta_action_drv__started__drv_sta_mac_stop_req (cp_t *ctx,
                                                  cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    bool ok = cp_msg_drv_sta_mac_stop_req_receive (ctx, mme);
    /* This come from the driver, it should be OK. */
    dbg_assert (ok);
    /* Save driver address. */
    ctx->sta_action.drv_peer = mme->peer;
    /* Stop station. */
    cp_sta_action_poweron_stop (ctx);
}

void
cp_sta_action_drv__stopping__stopped (cp_t *ctx)
{
    dbg_assert (ctx);
    /* Signal the station is stopped. */
    cp_msg_drv_any_cnf_send (ctx, &ctx->sta_action.drv_peer,
                             DRV_STA_MAC_STOP_CNF, CP_MSG_DRV_RESULT_SUCCESS);
}

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

    /* Check DRV MME. */
    bool ok = cp_msg_drv_sta_sc_req_receive (ctx, mme, sc_join);
    /* This come from the driver, it should be OK. */
    dbg_assert (ok);

    /* Send the driver MME reply. */
    /* Failure if (nok or (sc_add and not allowed sc add)). */
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SC_CNF,
                             (!ok || (*sc_join && !allow_sc_join)) ?
                             CP_MSG_DRV_RESULT_FAILURE :
                             CP_MSG_DRV_RESULT_SUCCESS);
}

void
cp_sta_action_drv__stopped__drv_sta_set_dak_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_key_t dak;
    dbg_assert (ctx);
    dbg_assert (mme);

    if (cp_msg_drv_sta_set_dak_req_receive (ctx, mme, &dak))
    {
        cp_sta_own_data_set_dak (ctx, dak);
#if CONFIG_CP_EOC
        cp_eoc_sta_own_data_set_original_dak (ctx, dak);
#endif
        cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_DAK_CNF,
                                 CP_MSG_DRV_RESULT_SUCCESS);
    }
    else
    {
        cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_DAK_CNF,
                                 CP_MSG_DRV_RESULT_FAILURE);
    }
}

void
cp_sta_action_drv__drv_sta_get_key_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_msg_drv_sta_get_key_t data;
    dbg_assert (ctx);
    dbg_assert (mme);

    memset (&data, 0, sizeof (cp_msg_drv_sta_get_key_t));
    if (cp_msg_drv_sta_get_key_req_receive (ctx, mme))
    {
        data.result = CP_MSG_DRV_RESULT_SUCCESS;
        data.nmk = cp_sta_own_data_get_nmk (ctx);
        data.nid = cp_sta_own_data_get_nid (ctx);
        data.sl = cp_sta_own_data_get_security_level (ctx);

        cp_msg_drv_sta_get_key_cnf_send (ctx, &mme->peer, &data);
    }
    else
    {
        data.result = CP_MSG_DRV_RESULT_FAILURE;
        cp_msg_drv_sta_get_key_cnf_send (ctx, &mme->peer, &data);
    }
}

void
cp_sta_action_drv__stopped__drv_sta_force_role_req (
    cp_t *ctx, cp_mme_rx_t *mme)
{
    char force_role;
    dbg_assert (ctx);
    dbg_assert (mme);

    if (cp_msg_drv_sta_force_role_req_receive (
            ctx, mme, &force_role))
    {
        cp_sta_own_data_t *own = cp_sta_mgr_get_sta_own_data (ctx);

        /* 'force_role' values in MME are the same as 'mac_force_role_t'. */
        own->force_role = force_role;

        cp_msg_drv_any_cnf_send (
            ctx, &mme->peer, DRV_STA_FORCE_ROLE_CNF,
            CP_MSG_DRV_RESULT_SUCCESS);
    }
    else
        cp_msg_drv_any_cnf_send (
            ctx, &mme->peer, DRV_STA_FORCE_ROLE_CNF,
            CP_MSG_DRV_RESULT_FAILURE);
}

/**
 * Fill the data structure corresponding the station's status.
 * \param  ctx  the control place context.
 * \param  data  the data structure corresponding to DRV_STA_STATUS.CNF fields
 * without the result.
 */
static void
cp_sta_action_drv__drv_sta_status_common (cp_t *ctx,
                                          cp_msg_drv_sta_status_t *data)
{
    dbg_assert (ctx);
    dbg_assert (data);

    if (cp_sta_own_data_get_cco_status (ctx))
    {
        cp_net_t *net = cp_sta_mgr_get_our_avln (ctx);
        data->cco = CP_MSG_DRV_STA_STATUS_CCO_CCO;
        if (net->num_associated_stas)
            data->status = CP_MSG_DRV_STA_STATUS_STATUS_AUTH;
        else
            data->status = CP_MSG_DRV_STA_STATUS_STATUS_UNASSOC;
    }
    else if (cp_sta_own_data_get_tei (ctx) == MAC_TEI_UNASSOCIATED)
    {
        data->status = CP_MSG_DRV_STA_STATUS_STATUS_UNASSOC;
        data->cco = CP_MSG_DRV_STA_STATUS_CCO_STA;
    }
    else if (MAC_TEI_IS_STA(cp_sta_own_data_get_tei (ctx))
             && !cp_sta_own_data_get_authenticated_status (ctx))
    {
        data->status = CP_MSG_DRV_STA_STATUS_STATUS_ASSOC;
        data->cco = CP_MSG_DRV_STA_STATUS_CCO_STA;
    }
    else
    {
        data->status = CP_MSG_DRV_STA_STATUS_STATUS_AUTH;

        if (cp_sta_own_data_get_pco_status (ctx))
            data->cco = CP_MSG_DRV_STA_STATUS_CCO_PCO;
        else if (cp_sta_own_data_get_cco_status (ctx))
            data->cco = CP_MSG_DRV_STA_STATUS_CCO_CCO;
        else
            data->cco = CP_MSG_DRV_STA_STATUS_CCO_STA;
    }

    data->preferred_cco = false; /* TODO. */
    data->backup_cco = false; /* TODO. */
    data->simple_connect = cp_sta_own_data_get_sc (ctx);
    data->pwl_sync_frequency = ctx->bsu_aclf->frequency;
}

void
cp_sta_action_drv__drv_sta_status_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_msg_drv_sta_status_cnf_t data;
    dbg_assert (ctx);
    dbg_assert (mme);

    memset (&data, 0, sizeof (cp_msg_drv_sta_status_cnf_t));
    if (cp_msg_drv_sta_status_req_receive (ctx, mme))
    {
        data.result = CP_MSG_DRV_RESULT_SUCCESS;
        cp_sta_action_drv__drv_sta_status_common (ctx, &data.status);
        cp_msg_drv_sta_status_cnf_send (ctx, &mme->peer, &data);
    }
    else
    {
        data.result = CP_MSG_DRV_RESULT_FAILURE;
        cp_msg_drv_sta_status_cnf_send (ctx, &mme->peer, &data);
    }
}

void
cp_sta_action_drv__drv_sta_status_ind_send (cp_t *ctx)
{
    cp_msg_drv_sta_status_t data;
    cp_mme_peer_t peer;
    dbg_assert (ctx);

    peer = CP_MME_PEER (cp_sta_own_data_get_mac_address (ctx),
                        MAC_TEI_FOREIGN);
    if (MAC_IS_VALID(peer.mac))
    {
        cp_sta_action_drv__drv_sta_status_common (ctx, &data);
        cp_msg_drv_sta_status_ind_send (ctx, &peer, &data);
    }
}

void
cp_sta_action_drv__drv_sta_set_config_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    /* Check parameter. */
    dbg_assert (mme);

    char config[HPAV_MME_PAYLOAD_MAX_SIZE];
    cp_msg_drv_result_t result = CP_MSG_DRV_RESULT_FAILURE;

    if (cp_msg_drv_sta_set_config_req_receive (ctx, mme, config))
    {
        /* Correctly decoded, let's try to set it up. */
        if (lib_stats_write_stats (config))
            /* Success. */
            result = CP_MSG_DRV_RESULT_SUCCESS;
    }
    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_STA_SET_CONFIG_CNF, result);
}

void
cp_sta_action_drv__drv_mcast_set_list_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (mme);

    cp_msg_drv_result_t result = CP_MSG_DRV_RESULT_SUCCESS;
    igmp_groups_t *igmp_groups = NULL;

    igmp_groups = cl_get_igmp_groups (ctx->cl);

    if (cp_msg_drv_mcast_set_list_req_receive (ctx, mme, &igmp_groups->nb,
                                               igmp_groups->group_mac,
                                               igmp_groups->nb_total_members,
                                               igmp_groups->member_mac))
    {
        /* Create a new table. */
        cl_mactotei_blk_t *new_table = cl_mactotei_new ();
        /* Copy entries except the groups entries. */
        cl_mactotei_copy_except_tei (ctx->cl, new_table, MAC_TEI_BCAST);
        /* Add new entries. */
        uint g;
        for (g = 0; g < igmp_groups->nb; g++)
        {
            cl_mactotei_addr_add (new_table, igmp_groups->group_mac[g],
                                  MAC_TEI_BCAST, g);
        }
        /* Use new MAC to TEI table. */
        cl_mactotei_use_table (ctx->cl, new_table);
        cl_update_igmp_groups (ctx->cl);
    }
    else
        result = CP_MSG_DRV_RESULT_FAILURE;

    cp_msg_drv_any_cnf_send (ctx, &mme->peer, DRV_MCAST_SET_LIST_CNF, result);
}