summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/plcd/src/hpav.c
blob: ffdebbf70345ebb00887b481b817442ef8b998ea (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
/* SPC300 project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    devkit/plcd/src/hpav.c
 * \brief   communication with HP AV stack
 * \ingroup plcd
 *
 * All communication functions with the HP AV stack
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <syslog.h>
#include <linux/if_ether.h>
#include <errno.h>
#include <assert.h>
#include <arpa/inet.h>          /* for htons() */
#include "libmme.h"
#include "spidlib.h"
#include "plcd.h"

#ifdef __UTESTS__
    #include "hpav_utests.h"
#endif

static int string_to_binary (const char* string, unsigned char *binary, unsigned int binary_length)
{
    int i, count = 0;
    char hexa[3];

    assert (string != NULL);
    assert (binary != NULL);

    for(i = 0; i < binary_length; i++)
    {
        if(strlen (string) < i * 2)
                break;
        memcpy (hexa, string + (i * 2), 2);
        hexa[2] = '\0';
        binary[i] = strtoul (hexa, NULL, 16);
        count++;
    }
    return count;
}

int hpav_compute_nmk (const char *npw, unsigned char *nmk)
{
    return 0;
}

int hpav_compute_dak (const char *dpw, unsigned char *dak)
{
    assert (dpw);
    assert (dak);
    spidlib_secu_pbkdf1 (dpw, strlen (dpw), SPIDLIB_SECU_SALT_TYPE_DAK, SPIDLIB_SECU_PBKDF1_ITERATION, dak, SPIDLIB_SECU_OUTPUT_KEY_SIZE);
    return 0;
}

static int hpav_send_mme (const plcd_ctx_t *plcd_ctx, mme_ctx_t *request_ctx, mme_ctx_t *confirm_ctx)
{
    struct sockaddr_nl kernel_addr;
    struct nlmsghdr *nlh;
    int msg_payload_len;
    struct iovec iov;
    struct msghdr msg;
    MME_t *mme_hdr;
    fd_set readfds;
    struct timeval timeout;
    int result;
    unsigned int len;

    assert (NULL != plcd_ctx);
    assert (NULL != request_ctx);
    assert (NULL != confirm_ctx);
    assert (MME_STATUS_INIT != request_ctx->status);
    assert (MME_STATUS_INIT != confirm_ctx->status);
    assert (MME_TYPE_REQ == (request_ctx->mmtype & MME_TYPE_MASK));

    msg_payload_len = request_ctx->tail - request_ctx->head + sizeof(MME_t);
    assert (ETH_FRAME_LEN >= msg_payload_len);
    mme_get_free_space (confirm_ctx, &len);
    assert (ETH_DATA_LEN <= len);

    /* fill the netlink address to send msg */
    memset(&kernel_addr, 0, sizeof(kernel_addr));
    kernel_addr.nl_family = AF_NETLINK;
    kernel_addr.nl_pid = 0;   /* For Linux Kernel */
    kernel_addr.nl_groups = 0; /* unicast */

    /* create the message */
    nlh=(struct nlmsghdr *)malloc(NLMSG_SPACE(ETH_FRAME_LEN));
    memset(nlh, 0, NLMSG_SPACE(ETH_FRAME_LEN));

    /* fill the netlink message header */
    nlh->nlmsg_len = NLMSG_SPACE(ETH_FRAME_LEN);
    nlh->nlmsg_pid = getpid();  /* self pid */
    nlh->nlmsg_flags = 0;

    /* fill the MME header */
    mme_hdr = (MME_t *)NLMSG_DATA (nlh);
    memcpy (mme_hdr->mme_dest, plcd_ctx->nvram->plc_address, ETH_ALEN);
    memcpy (mme_hdr->mme_src, plcd_ctx->nvram->plc_address, ETH_ALEN);
    mme_hdr->mtype = htons (MME_TYPE);
    mme_hdr->mmv = MME_VERSION;
    mme_hdr->mmtype = request_ctx->mmtype;

    /* fill MME payload */
    memcpy ((unsigned char *)mme_hdr + sizeof(MME_t), request_ctx->buffer + request_ctx->head, request_ctx->tail - request_ctx->head);

    /* fill message info */
    memset(&msg, 0, sizeof(msg));
    msg.msg_name = (void *)&kernel_addr;
    msg.msg_namelen = sizeof(kernel_addr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    iov.iov_base = (void *)nlh;
    iov.iov_len = NLMSG_SPACE(msg_payload_len);

    if(0 > sendmsg (plcd_ctx->plc_sock, &msg, 0))
    {
        syslog (LOG_WARNING, "sendmsg error (%d), type=%04x", errno, mme_hdr->mmtype);
        free (nlh);
        return -1;
    }

    /* prepare the recv message */
    memset(nlh, 0, NLMSG_SPACE(ETH_FRAME_LEN));
    iov.iov_len = NLMSG_SPACE(ETH_FRAME_LEN);

 /* wait for confirm message */
    timeout.tv_sec = MME_TOUT;
    timeout.tv_usec = 0;
    FD_ZERO(&readfds);
    FD_SET(plcd_ctx->plc_sock, &readfds);

    result = select (plcd_ctx->plc_sock + 1, &readfds, NULL, NULL, &timeout);
    if(0 > result)
    {
        /* recv error */
        syslog (LOG_WARNING, "select failed (%d)", errno);
        free (nlh);
        return -1;
    }
    else if(0 < result)
    {
        /* read received */
        if(0 >= recvmsg (plcd_ctx->plc_sock, &msg, 0))
        {
            syslog (LOG_WARNING, "mme confirm error (%d)", errno);
            free (nlh);
            return -1;
        }
        /* check MMTYPE */
        mme_hdr = (MME_t *)NLMSG_DATA (nlh);

        if(((request_ctx->mmtype & ~MME_TYPE_MASK) | MME_TYPE_CNF) != mme_hdr->mmtype)
        {
            syslog (LOG_WARNING, "bad msg received, type=%04x", mme_hdr->mmtype);
            free (nlh);
            return -1;
        }

        /* transfert to confirm ctx */
        mme_put (confirm_ctx, (unsigned char *)NLMSG_DATA (nlh) + sizeof(MME_t), ETH_DATA_LEN, &len);
        free (nlh);
        return 0;
    }

    /* timeout */
    syslog (LOG_WARNING, "read confirm timeout");
    free (nlh);
    return -1;
}

int hpav_send_single_value (const plcd_ctx_t *plcd_ctx, unsigned int mmtype, const void *value, unsigned int length)
{
    unsigned char buffer[ETH_DATA_LEN];
    mme_ctx_t request_ctx, confirm_ctx;
    unsigned char result;
    unsigned int result_len;

    assert (NULL != plcd_ctx);

    /* init mme */
    mme_init (&request_ctx, mmtype | MME_TYPE_REQ, buffer, ETH_DATA_LEN);
    mme_init (&confirm_ctx, mmtype | MME_TYPE_CNF, buffer, ETH_DATA_LEN);
    if(value != NULL)
    {
        /* put value */
        mme_put (&request_ctx, value, length, &result_len);
    }
    if((0 <= hpav_send_mme (plcd_ctx, &request_ctx, &confirm_ctx))
       && (MME_SUCCESS == mme_pull (&confirm_ctx, &result, sizeof(result), &result_len)))
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

int hpav_send_mac_address (const plcd_ctx_t *plcd_ctx, const unsigned char *mac_addr)
{
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_SET_MAC_ADDR, mac_addr, ETH_ALEN);
}

int hpav_send_cco_preferred (const plcd_ctx_t *plcd_ctx, const int is_cco_preferred)
{
    unsigned char value = is_cco_preferred;
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_SET_CCO_PREF, &value, 1);
}

int hpav_send_was_cco (const plcd_ctx_t *plcd_ctx, const int is_was_cco)
{
    unsigned char value = is_was_cco;
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_SET_WAS_CCO, &value, 1);
}

int hpav_send_key (const plcd_ctx_t *plcd_ctx, const unsigned char *nmk, const unsigned char *nid, const char *sl)
{
    unsigned char value[16 + 9];  /* total message size =  size of nmk + size of NID/SL */
    unsigned char *nid_sl = &value[16]; /* pointer to NID/SL part of message */

    assert (NULL != plcd_ctx);
    assert (NULL != nmk);

    /* put NMK */
    memcpy(value, nmk, 16);

    /* select value for NID or SL */
    memset (nid_sl, '\0', 9);
    if(sl != NULL)
    {
        /* put choose sl */
        nid_sl[0] = 0x01;
        if(!strcmp (sl, SPIDLIB_HPAV_CONF_VALUE_SL_HS))
            nid_sl[8] = 0x01;
    }
    else
    {
        memcpy (nid_sl + 1, nid, 7);
    }

    return hpav_send_single_value( plcd_ctx, MME_TYPE_DRV_STA_SET_KEY, value, sizeof(value) );
}

int hpav_send_dak (const plcd_ctx_t *plcd_ctx, const unsigned char *dak)
{
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_SET_DAK, dak, SPIDLIB_SECU_OUTPUT_KEY_SIZE);
}

int hpav_send_mfg_hfid (const plcd_ctx_t *plcd_ctx, const char *mfg_hfid)
{
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_SET_M_STA_HFID, mfg_hfid, 64);
}

int hpav_send_user_hfid (const plcd_ctx_t *plcd_ctx, const char *user_hfid)
{
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_SET_U_STA_HFID, user_hfid, 64);
}

int hpav_send_avln_hfid (const plcd_ctx_t *plcd_ctx, const char *avln_hfid)
{
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_SET_AVLN_HFID, avln_hfid, 64);
}

int hpav_send_tonemask (const plcd_ctx_t *plcd_ctx, const unsigned char *tonemask)
{
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_SET_TONEMASK, tonemask, 192);
}

int hpav_send_start (const plcd_ctx_t *plcd_ctx)
{
    return hpav_send_single_value (plcd_ctx, MME_TYPE_DRV_STA_MAC_START, NULL, 0);
}

int hpav_init (plcd_ctx_t *plcd_ctx)
{
    char buffer[1024];
    unsigned char nmk[SPIDLIB_SECU_OUTPUT_KEY_SIZE];
    unsigned char nid[7];
    unsigned char dak[SPIDLIB_SECU_OUTPUT_KEY_SIZE];

    assert (NULL != plcd_ctx);
    assert (NULL != plcd_ctx->nvram);

    /* send the MAC address to the AV stack */
    if(0 > hpav_send_mac_address (plcd_ctx, plcd_ctx->nvram->plc_address))
        return -1;

    /* get the CCO_PREFERRED and send it to AV stack */
    spidlib_config_read_item (plcd_ctx->hpav_conf_path, SPIDLIB_HPAV_CONF_LABEL_CCO_PREFERRED, buffer, sizeof(buffer));
    if( 0 > hpav_send_cco_preferred (plcd_ctx, SPIDLIB_GET_BOOLEAN (buffer)))
        return -1;

    /* get the WAS_CCO and send it to AV stack */
    spidlib_config_read_item (plcd_ctx->hpav_conf_path, SPIDLIB_HPAV_CONF_LABEL_WAS_CCO, buffer , sizeof(buffer));
    if( 0 > hpav_send_was_cco (plcd_ctx, SPIDLIB_GET_BOOLEAN (buffer)))
        return -1;

    /* get the NMK, and compute it if necessary */
    spidlib_config_read_item (plcd_ctx->hpav_conf_path, SPIDLIB_HPAV_CONF_LABEL_NMK, buffer , sizeof(buffer));
    if(!strcmp (buffer, SPIDLIB_HPAV_CONF_VALUE_NONE))
    {
        /* compute the NMK from NPW */
        spidlib_config_read_item (plcd_ctx->hpav_conf_path, SPIDLIB_HPAV_CONF_LABEL_NPW, buffer , sizeof(buffer));
        hpav_compute_nmk (buffer, nmk);
    }
    else
    {
        /* get NMK in binary format from string */
        string_to_binary (buffer, nmk, 16);
    }

    /* get the NID from hpav.conf */
    spidlib_config_read_item (plcd_ctx->hpav_conf_path, SPIDLIB_HPAV_CONF_LABEL_NID, buffer , sizeof(buffer));
    if(!strcmp (buffer, SPIDLIB_HPAV_CONF_VALUE_NONE))
    {
        /* get SL */
        spidlib_config_read_item (plcd_ctx->hpav_conf_path, SPIDLIB_HPAV_CONF_LABEL_SL, buffer , sizeof(buffer));
        /* send NMK and SL */
        if(0 > hpav_send_key (plcd_ctx, nmk, NULL, buffer))
            return -1;
    }
    else
    {
        /* get NID in binary format */
        string_to_binary (buffer, nid, 7);
        /* send NMK and NID */
        if(0 > hpav_send_key (plcd_ctx, nmk, nid, NULL))
            return -1;
    }

    /* compute DAK from DPW */
    hpav_compute_dak (plcd_ctx->nvram->device_password, dak);
    /* send DAK */
    if(0 > hpav_send_dak (plcd_ctx, dak))
        return -1;

    /* send manufacturer HFID */
    if(0 > hpav_send_mfg_hfid (plcd_ctx, plcd_ctx->nvram->product_name))
        return -1;

    /* get user HFID from hpav config file end send it */
    spidlib_config_read_item (plcd_ctx->hpav_conf_path, SPIDLIB_HPAV_CONF_LABEL_USER_HFID, buffer , sizeof(buffer));
    if(0 > hpav_send_user_hfid (plcd_ctx, buffer))
        return -1;

    /* get avln HFID from hpav config file end send it */
    spidlib_config_read_item (plcd_ctx->hpav_conf_path, SPIDLIB_HPAV_CONF_LABEL_AVLN_HFID, buffer , sizeof(buffer));
    if(0 > hpav_send_avln_hfid (plcd_ctx, buffer))
        return -1;

    /* send tonemask */
    if(0 > hpav_send_tonemask (plcd_ctx, plcd_ctx->nvram->tonemask))
        return -1;

    /* start the AV stack */
    if(0 > hpav_send_start (plcd_ctx))
        return -1;

    return 0;
}