summaryrefslogtreecommitdiff
path: root/cesar/cp2/msg/src/msg.c
blob: 265a4c17f90a83a5b613b8d88edf7396b1a13718 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp2/msg/src/msg.c
 * \brief   MSG functions.
 * \ingroup cp2_msg
 *
 */
#include "common/std.h"
#include "lib/read_word.h"
#include "lib/swap.h"

#include "common/defs/homeplugAV.h"
#include "common/defs/ethernet.h"

#include "cp2/cp.h"
#include "cp2/msg/msg.h"
#include "cp2/cl_interf/cl_interf.h"
#include "cp2/sta/mgr/sta_mgr.h"
#include "cp2/sta/mgr/sta_own_data.h"

#include "cp2/msg/inc/msg.h"
#include "cp2/inc/context.h"

/**
 * Initialise the MSG module.
 * \param  ctx  control plane context
 */
void
cp_msg_init (cp_t *ctx)
{
    dbg_assert (ctx);

    slab_cache_init (&ctx->msg.mme_tx_slab_cache, "MME TX",
                     sizeof (cp_mme_tx_t),
                     (slab_object_destructor_t) cp_msg_mme_tx_destructor);

    slab_cache_init (&ctx->msg.mme_rx_slab_cache, "MME RX",
                     sizeof (cp_mme_rx_t),
                     (slab_object_destructor_t) cp_msg_mme_rx_destructor);
}

/**
 * Uninitialise the MSG module.
 * \param  ctx  control plane context
 */
void
cp_msg_uninit (cp_t *ctx)
{
    dbg_assert (ctx);

    slab_cache_uninit (&ctx->msg.mme_tx_slab_cache);
    slab_cache_uninit (&ctx->msg.mme_rx_slab_cache);
}

/**
 * Examine message type and post an event to the FSM.
 * \param  ctx  control plane context
 * \param  mme  received MME
 *
 * This function looks up the message type and translates it to a FSM event,
 * while checking the encryption is compliant with the message type.
 *
 * It also extracts and checks payload from encrypted messages.
 */
void
cp_msg_dispatch (cp_t *ctx, cp_mme_rx_t *mme);

/**
 * Initialise a MME handle for a new message to be transmitted.
 * \param  ctx  control plane context
 * \param  peer  peer information
 * \param  mmtype  the MME MMTYPE.
 * \return  the newly created message
 *
 * This function:
 *
 *  - gets a buffer for MME transmission,
 *  - encapsulates the MME in a CC_RELAY.REQ if necessary,
 *  - writes the MME header.
 */
cp_mme_tx_t *
cp_msg_mme_init (cp_t *ctx, cp_mme_peer_t *peer, uint mmtype)
{
    cp_mme_tx_t *msg;
    cp_sta_own_data_t *own_data;
    u64 data;

    dbg_assert (ctx);
    dbg_assert (peer);

    // Allocate the new message.
    msg = cp_msg_mme_tx_init (ctx);
    dbg_assert (msg);

    // Get a buffer.
    msg->p_mme = cp_cl_interf_get_buffer_tx (ctx);
    msg->peer = *peer;

    // Get the station own data.
    own_data = cp_sta_mgr_get_sta_own_data (ctx);

    // Initialise the bitstream context and fill the MME header.
    bitstream_init (&msg->bitstream, msg->p_mme,
                    peer->vlan_tag ? HPAV_MME_HEADER_LEN_WITH_VLAN :
                    HPAV_MME_HEADER,
                    BITSTREAM_WRITE);

    // Store the Destination Mac address.
    bitstream_access (&msg->bitstream, &peer->mac,
                      BYTES_SIZE_TO_BITS (ETH_MAC_ADDRESS_SIZE));

    // Store the source mac address.
    data = cp_sta_own_data_get_mac_address (own_data);
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (ETH_MAC_ADDRESS_SIZE));

    // Vlan tag.
    if (peer->vlan_tag)
        bitstream_access (&msg->bitstream, &peer->vlan_tag,
                          BYTES_SIZE_TO_BITS(ETH_VLAN_TAG_SIZE));

    // MTYPE.
    data = swap16(HPAV_MTYPE_MME);
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (HPAV_MTYPE_SIZE));

    // MMV
    data = HPAV_MMV;
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (HPAV_MMV_SIZE));

    //MMTYPE.
    bitstream_access (&msg->bitstream, &mmtype,
                      BYTES_SIZE_TO_BITS (HPAV_MMTYPE_SIZE));

    return msg;
}

/**
 * Initialise a MME handle for a new encrypted message to be transmitted.
 * \param  ctx  control plane context
 * \param  peer  peer information
 * \param  mmtype  the MME MMType.
 * \param  pid  protocol identifier
 * \param  peks  payload encryption key select
 * \param  prun  protocol run information
 * \return  the newly created message
 *
 * This does the same as cp_msg_mme_init(), but also encapsulate the MME in a
 * CM_ENCRYPTED_PAYLOAD.IND.
 */
cp_mme_tx_t *
cp_msg_mme_init_encrypted (cp_t *ctx, cp_mme_peer_t *peer,
                           uint mmtype,
                           cp_mme_peks_t peks,
                           const cp_secu_protocol_run_t *prun)
{
    cp_mme_tx_t *msg;
    cp_sta_own_data_t *own_data;
    u64 data;

    dbg_assert (ctx);
    dbg_assert (peer);
    dbg_assert (prun);

    // Initialise the message context.
    msg = cp_msg_mme_tx_init (ctx);
    dbg_assert (msg);

    // Get a buffer
    msg->p_mme = cp_cl_interf_get_buffer_tx (ctx);
    msg->peer = *peer;
    msg->peks = peks;
    msg->prun = *prun;

    own_data = cp_sta_mgr_get_sta_own_data (ctx);
    // Initialise the bitstream context.
    bitstream_init (&msg->bitstream, msg->p_mme,
                    peer->vlan_tag ? 2 * HPAV_MME_HEADER_LEN_WITH_VLAN :
                    2 * HPAV_MME_HEADER, BITSTREAM_WRITE);

    // Store the ODA.
    bitstream_access (&msg->bitstream, &msg->peer.mac,
                      BYTES_SIZE_TO_BITS (ETH_MAC_ADDRESS_SIZE));

    // Store the OSA.
    data = cp_sta_own_data_get_mac_address (own_data);
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (ETH_MAC_ADDRESS_SIZE));

    // Vlan tag.
    if (peer->vlan_tag)
        bitstream_access (&msg->bitstream, &peer->vlan_tag,
                          BYTES_SIZE_TO_BITS(ETH_VLAN_TAG_SIZE));

    // MTYPE.
    data = swap16(HPAV_MTYPE_MME);
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (HPAV_MTYPE_SIZE));

    // MMV
    data = HPAV_MMV;
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (HPAV_MMV_SIZE));

    //MMTYPE.
    data = CM_ENCRYPTED_PAYLOAD_IND;
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (HPAV_MMTYPE_SIZE));

    // FMI = 0.
    data = 0;
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (HPAV_FMI_SIZE));

    // The payload
    // Store the ODA.
    bitstream_access (&msg->bitstream, &msg->peer.mac,
                      BYTES_SIZE_TO_BITS (ETH_MAC_ADDRESS_SIZE));

    // Store the OSA.
    data = cp_sta_own_data_get_mac_address (own_data);
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (ETH_MAC_ADDRESS_SIZE));

    // Vlan tag.
    if (peer->vlan_tag)
        bitstream_access (&msg->bitstream, &peer->vlan_tag,
                          BYTES_SIZE_TO_BITS(ETH_VLAN_TAG_SIZE));

    // MTYPE.
    data = swap16(HPAV_MTYPE_MME);
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (HPAV_MTYPE_SIZE));

    // MMV
    data = HPAV_MMV;
    bitstream_access (&msg->bitstream, &data,
                      BYTES_SIZE_TO_BITS (HPAV_MMV_SIZE));

    //MMTYPE.
    bitstream_access (&msg->bitstream, &mmtype,
                      BYTES_SIZE_TO_BITS (HPAV_MMTYPE_SIZE));

    return msg;
}

/**
 * Finalise and send a MME.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 *
 * If the MME is encapsulated, write any pending footer, then send the
 * message.
 */
void
cp_msg_mme_send (cp_t *ctx, cp_mme_tx_t *mme);


/**
 * Read the header of a received MME and return the MME context.
 * \param  ctx  the module context.
 * \param  mme  the MME received.
 * \param  length  the MME length.
 * \param  tei  the source TEI (0xFF if coming from the HLE).
 * \param  fmi  the FMI data.
 * \return  cp_mme_rx_t object associated.
 */
cp_mme_rx_t *
cp_msg_mme_read_header (cp_t *ctx, u8 *mme, uint length, cp_tei_t tei,
                        uint *fmi)
{
    cp_mme_rx_t *mme_rx;
    bool vlantag_present;
    uint mtype;
    uint mmv;

    dbg_assert (ctx);
    dbg_assert (mme);

    if (read_u16_from_word (mme + 12) == swap16 (HPAV_MTYPE_MME))
        vlantag_present = false;
    else
        vlantag_present = true;

    mme_rx = cp_msg_mme_rx_init (ctx, mme, length, tei);

    bitstream_init (&mme_rx->bitstream, mme + ETH_MAC_ADDRESS_SIZE,
                    length, BITSTREAM_READ);
    bitstream_access (&mme_rx->bitstream, &mme_rx->peer.mac,
                      BYTES_SIZE_TO_BITS(ETH_MAC_ADDRESS_SIZE));
    if (vlantag_present)
        bitstream_access (&mme_rx->bitstream, &mme_rx->peer.vlan_tag, 32);
    else
        mme_rx->peer.vlan_tag = 0;

    bitstream_access (&mme_rx->bitstream, &mtype, 16);
    bitstream_access (&mme_rx->bitstream, &mmv, 8);
    bitstream_access (&mme_rx->bitstream, &mme_rx->mmtype, 16);
    bitstream_access (&mme_rx->bitstream, fmi, 16);

    // Verify some data.
    if ((mmv != HPAV_MMV1) || (swap16(mtype) != HPAV_MTYPE_MME))
    {
        slab_release (mme_rx);
        mme_rx = NULL;
    }

    return mme_rx;
}