summaryrefslogtreecommitdiff
path: root/cleopatre/application/spidapp/src/spidapp.c
blob: 1a6961715ab90f319c6c0c5b5034207f71d688e8 (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
/*
 * application/spidapp/src/spidapp.c
 *
 * (C) Copyright 2009 SPiDCOM Technologies
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <errno.h>
#include "libspid.h"
#include "libmme.h"
#include "spidapp.h"

#define USAGE_NMK                          \
    "nmk: create a new NMK from a human friendly network password\n"    \
    "     --npw  <network password>\n"     \
    "     --file <config file>\n"

#define USAGE_SNIFFER                      \
    "sniffer: put the AV stack in sniffer mode\n"\
    "     --mac <dst mac addr>\n"          \
    "     --mode <mode bitfield>\n"        \
    "         0: disable all\n"            \
    "         1: MME Tx\n"                 \
    "         2: MME Rx\n"                 \
    "         4: Beacon Tx\n"              \
    "         8: Beacon Rx\n"              \
    "        32: FC Rx\n"                  \
    "       128: MPDU Rx\n"

struct cmd_entry {
    char *name;
    unsigned int arg_nb;
    char *usage;
    int (*cmd)(struct cmd_entry *desc, int argc, char **argv);
};

static void print_usage (struct cmd_entry *cmd_entry);

static void binary_to_string (const unsigned char *binary, int binary_len, char *string)
{
    int i;
    for(i = 0; i < binary_len; i++)
    {
        sprintf (string + i * 2, "%02x", binary[i]);
    }
}

/******************************************************
 * NMK command: create a NMK from a NPW
 * (don't forget to save the modified hpav.conf file)
 ****************************************************/
int cmd_nmk (struct cmd_entry *cmd_entry, int argc, char **argv)
{
    int c = 0, i;
    int opt_index = 0;
    char config_file[256];
    char npw[64];
    char nmk[LIBSPID_SECU_OUTPUT_KEY_SIZE], nmk_str[64];
    struct option long_opts[] = {
        { "npw",     required_argument, NULL, 'n' },
        { "file",    required_argument, NULL, 'f' },
    };

    memset (npw, '\0', sizeof(npw));
    memset (config_file, '\0', sizeof(config_file));
    strcpy (config_file, LIBSPID_HPAV_CONF_PATH);
    strcpy (npw, NPW_DEFAULT);
    while( ( c = getopt_long_only(argc, argv, "", long_opts, &opt_index) ) != -1 )
    {
        switch(c)
        {
        case 'n':
            if ((strlen(optarg) >= NPW_MIN_LEN) && (strlen(optarg) <= NPW_MAX_LEN))
            {
                if (strlen(optarg) <= NPW_ADVISED_MIN_LEN)
                {
                    printf ("The password may not be secure\n");
                }
                for (i = 0; i<strlen(optarg); i++)
                {
                    if ((optarg[i] < 32) || (optarg[i] > 127))
                    {
                        printf ("Network Password must use characters from ASCII[32] to ASCII[127]\n");
                        return (-1);
                    }
                }
                strcpy (npw, optarg);
            }
            else
            {
                printf ("Network Password cannot be created using less than 8 or more than 64 characters\n");
                return (-1);
            }
            break;
        case 'f':
            strncpy (config_file, optarg, sizeof(config_file) - 1);
            break;
        default:
            print_usage (cmd_entry);
            return (-1);
        }

    }

    libspid_secu_pbkdf1 (npw, strlen (npw), LIBSPID_SECU_SALT_TYPE_NMK, LIBSPID_SECU_PBKDF1_ITERATION, nmk, LIBSPID_SECU_OUTPUT_KEY_SIZE);
    binary_to_string (nmk, LIBSPID_SECU_OUTPUT_KEY_SIZE, nmk_str);
    libspid_config_write_item (config_file, LIBSPID_HPAV_CONF_LABEL_NMK, nmk_str);
    printf ("NMK(%s): '%s'\n", npw, nmk_str);

    /* hpav.conf save is needed */
    libspid_system_save_file (LIBSPID_HPAV_CONF_PATH);

  /* plc daemon needs to be informed */
  if (LIBSPID_SUCCESS != libspid_system_file_update_warn (getpid(),
                                                          LIBSPID_HPAV_CONF_PATH))
  {
    return -1;
  }

  return 0;
}

/****************************************************************
 * sniffer command: set the HPAV sniffer in sniff mode.
 * The final host target mac-address of sniff packet must be provided
 * else packets will be transmitted in broadcast mode
 ***************************************************************/
int cmd_sniffer (struct cmd_entry *cmd_entry, int argc, char **argv)
{
    int sock;
    struct sockaddr_ll sll;
    struct ifreq ifr;
    int c = 0;
    int opt_index = 0;
    unsigned char mode = 0;
    unsigned char mac_src[ETH_ALEN];
    char pkt_buffer[1024];
    MME_t *mme_hdr;
    int pkt_len, len;

    struct option long_opts[] = {
        { "mac",    required_argument, NULL, 'm' },
        { "mode",   required_argument, NULL, 'n' },
    };

    /* set default src mac to broadcast */
    memset (mac_src, '\xff', ETH_ALEN);
    while( ( c = getopt_long_only(argc, argv, "", long_opts, &opt_index) ) != -1 )
    {
        switch(c)
        {
        case 'm':
            if(libspid_mac_str_to_bin (optarg, mac_src) != LIBSPID_SUCCESS)
            {
                print_usage (cmd_entry);
                return (-1);
            }
            break;
        case 'n':
            mode = atoi (optarg);
            if(mode & ~0xaf)
            {
                print_usage (cmd_entry);
                return (-1);
            }
            break;
        default:
            print_usage (cmd_entry);
            return (-1);
        }
    }

    /* dirty, but we need to do it by hand because libmme does not
     * allow to change the src mac address of MME packet */
    if (( sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
    {
        perror ("Error creating raw socket");
        return 0;
    }
    memset(&sll, 0x0, sizeof(sll));
    memset(&ifr, 0x0, sizeof(ifr));

    /* First Get the Interface Index  */
    strcpy(ifr.ifr_name, IFNAME_LO);
    if ((ioctl(sock, SIOCGIFINDEX, &ifr)) < 0)
    {
        perror("Error getting plc0 interface index");
        close(sock);
        return 0;
    }

    /* Bind our raw socket to this interface */
    sll.sll_family = AF_PACKET;
    sll.sll_ifindex = ifr.ifr_ifindex;
    sll.sll_protocol = htons(ETH_P_ALL);
    if((bind(sock, (struct sockaddr *)&sll, sizeof(sll))) < 0)
    {
        perror("Error binding raw socket to plc0 interface");
        close(sock);
        return 0;
    }
    /* get plc0 mac-address */
    ioctl(sock, SIOCGIFHWADDR, &ifr);

    /* fill MME VS_SNIFFER.REQ packet */
    mme_hdr = (MME_t *)pkt_buffer;
    memcpy (mme_hdr->mme_dest, (unsigned char *)ifr.ifr_hwaddr.sa_data, ETH_ALEN);
    //mme_hdr->mme_dest[4] = 0x02; // no need for this hack
    memcpy (mme_hdr->mme_src, mac_src, ETH_ALEN);
    mme_hdr->mtype = htons (MME_TYPE);
    mme_hdr->mmv = 1;
    mme_hdr->mmtype = MME_TYPE_VS_SNIFFER | MME_TYPE_REQ;
    mme_hdr->fmi = 0;
    memcpy ((unsigned char *)mme_hdr + sizeof(MME_t), OUI_SPIDCOM, sizeof(OUI_SPIDCOM) - 1);
    memcpy ((unsigned char *)mme_hdr + sizeof(MME_t) + sizeof(OUI_SPIDCOM) - 1, &mode, sizeof(mode));
    pkt_len = sizeof(MME_t) + sizeof(OUI_SPIDCOM) - 1 + sizeof(mode);

    /* send MME */
    if((len = write (sock, pkt_buffer, pkt_len)) != pkt_len)
    {
        perror ("Failed to transmit MME: ");
    }

    close (sock);
    return 0;
}

struct cmd_entry cmd_table[] = {
    { "nmk", 1, USAGE_NMK, cmd_nmk },
    { "sniffer", 1, USAGE_SNIFFER, cmd_sniffer },
    { NULL, 0, NULL, NULL },
};

static void print_usage (struct cmd_entry *cmd_entry)
{
    printf ("Usage: spidapp <command> [options]\n");
    printf ("Utility tool for spc300\n");
    if(cmd_entry == NULL)
    {
        struct cmd_entry *tmp_entry = cmd_table;
        int i = 0;
        printf ("Command list:\n");
        while(cmd_table[i].name != NULL)
        {
            printf ("%s", cmd_table[i].usage);
            i++;
        }
    }
    else
    {
        printf ("%s", cmd_entry->usage);
    }
}

int main (int argc, char **argv)
{
    int cmd_index = 0;
    int argv_index = 0;
    struct cmd_entry *cmd_entry;
    if (argc < 2)
    {
        print_usage (NULL);
        exit (1);
    }
    argc--;
    argv_index++;

    while(cmd_table[cmd_index].name != NULL)
    {
        if(!strcmp (cmd_table[cmd_index].name, argv[argv_index]))
            break;
        cmd_index++;
    }
    if(NULL == cmd_table[cmd_index].name)
    {
        print_usage (NULL);
        exit (1);
    }

    if((cmd_table[cmd_index].cmd) (cmd_table + cmd_index, argc, argv + argv_index) < 0)
    {
        exit (1);
    }

    exit (0);
}