forked from ffainelli/faifa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpibtool.c
450 lines (369 loc) · 9.54 KB
/
pibtool.c
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
/*
* PIB retrieval
*
* Copyright (C) 2007-2008 Xavier Carcelle <[email protected]>
* Florian Fainelli <[email protected]>
* Nicolas Thill <[email protected]>
* Saul St. John <[email protected]>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include "crc32.h"
#include "faifa.h"
#include "faifa_compat.h"
#include "faifa_priv.h"
#include "homeplug_av.h"
/* Command line arguments storing */
int opt_help = 0;
int opt_key = 0;
const char *opt_fname = NULL;
const char *prog_name = NULL;
/**
* error - display error message
*/
static void error(char *message)
{
fprintf(stderr, "%s: %s\n", prog_name, message);
}
/**
* usage - show the program usage
*/
static void usage(void)
{
fprintf(stderr, "-i : interface\n"
"-a : station MAC address\n"
"-k : network key\n"
"-v : be verbose (default: no)\n"
"-h : this help\n");
}
struct read_request_frame {
union {
struct {
struct ether_header eth_header;
struct hpav_frame_header hpav_header;
struct hpav_frame_vendor_payload payload;
struct read_mod_data_request request;
};
short data[60];
};
} __attribute__((__packed__));
struct read_confirmation_frame {
struct ether_header eth_header;
struct hpav_frame_header hpav_header;
struct hpav_frame_vendor_payload payload;
struct read_mod_data_confirm response;
} __attribute__((__packed__));
int send_read_request(faifa_t *faifa, int len, int offset)
{
int res;
struct read_request_frame f;
memcpy(f.eth_header.ether_dhost, ((struct faifa *)faifa)->dst_addr, ETHER_ADDR_LEN);
memset(f.eth_header.ether_shost, 0, ETHER_ADDR_LEN);
f.eth_header.ether_type = htons(ETHERTYPE_HOMEPLUG_AV);
f.hpav_header.mmtype = HPAV_MMTYPE_RD_MOD_REQ;
f.payload.oui[0] = 0;
f.payload.oui[1] = 0xb0;
f.payload.oui[2] = 0x52;
f.request.module_id = 2;
f.request.length = len;
f.request.offset = offset;
res = faifa_send(faifa, &f, sizeof(f));
if(-1 == res) {
error(faifa_error(faifa));
}
return res;
}
uint32_t checksum(const char *data, size_t len)
{
uint32_t sum = 0xffffffff;
int i = 0;
for ( ; i < len; i++)
{
sum ^= (uint32_t)((unsigned char)data[i]);
sum = (sum >> 8) | (sum << 24);
}
return sum;
}
int recv_read_confirmation(faifa_t *faifa, char *pib, int *offset)
{
struct read_confirmation_frame *f = malloc(ETHER_MAX_LEN);
int res;
uint32_t cksum;
res = faifa_recv(faifa, (char *)f, ETHER_MAX_LEN);
cksum = checksum((char *)f->response.data, f->response.length);
if (cksum != f->response.checksum)
printf("checksum mismatch %08x != %08x !\n", cksum, f->response.checksum);
memcpy(&pib[*offset], &f->response.data, f->response.length);
*offset += f->response.length;
free(f);
return res;
}
void pib_write_file(char* pib)
{
const char *fname;
FILE *file;
int i;
if (NULL != opt_fname)
fname = opt_fname;
else
fname = "./pibtool.out";
file = fopen(fname, "w");
for (i = 0; i < 16352; i++)
fprintf(file, "%c", pib[i]);
fclose(file);
}
int pib_dump(faifa_t *faifa)
{
int offset = 0;
char *pib = malloc(16352);
int res = 0;
/* this is super insecure */
while (offset < 16352) {
int len = (16352 - offset < 0x400) ?
16352 - offset :
0x400;
if (-1 == ((res = send_read_request(faifa, len, offset)))) {
break;
}
if (-1 == ((res = recv_read_confirmation(faifa, pib, &offset)))) {
break;
}
}
pib_write_file(pib);
free(pib);
return res;
}
struct write_request_frame {
struct ether_header eth_header;
struct hpav_frame_header hpav_header;
struct hpav_frame_vendor_payload payload;
struct write_mod_data_request request;
} __attribute__((__packed__));
struct write_confirmation_frame {
union {
struct {
struct ether_header eth_header;
struct hpav_frame_header hpav_header;
struct hpav_frame_vendor_payload payload;
struct write_mod_data_confirm response;
};
char data[60];
};
} __attribute__((__packed__));
struct nvm_write_request_frame {
union {
struct {
struct ether_header eth_header;
struct hpav_frame_header hpav_header;
struct hpav_frame_vendor_payload payload;
struct write_module_data_to_nvm_request request;
};
char data[60];
};
} __attribute__((__packed__));
int send_commit_write_to_nvm(faifa_t *faifa, int module)
{
struct nvm_write_request_frame f;
int res = 0;
memcpy(f.eth_header.ether_dhost, faifa->dst_addr, ETHER_ADDR_LEN);
f.eth_header.ether_type = htons(ETHERTYPE_HOMEPLUG_AV);
f.hpav_header.mmtype = HPAV_MMTYPE_NVM_MOD_REQ;
f.payload.oui[1] = 0xb0;
f.payload.oui[2] = 0x52;
f.request.module_id = module;
res = faifa_send(faifa, &f, sizeof(f));
if(-1 == res) {
error(faifa_error(faifa));
}
return res;
}
int send_write_request(faifa_t *faifa, const char *pib, int offset, int len)
{
struct write_request_frame *f = malloc(ETHER_MAX_LEN);
int res = 0;
memset(f, 0, ETHER_MAX_LEN);
memcpy(f->eth_header.ether_dhost, faifa->dst_addr, ETHER_ADDR_LEN);
f->eth_header.ether_type = htons(ETHERTYPE_HOMEPLUG_AV);
f->hpav_header.mmtype = HPAV_MMTYPE_WR_MOD_REQ;
f->payload.oui[0] = 0;
f->payload.oui[1] = 0xb0;
f->payload.oui[2] = 0x52;
f->request.module_id = 2;
f->request.length = len;
f->request.offset = offset;
f->request.checksum = checksum(&pib[offset], len);
memcpy(&f->request.data, &pib[offset], len);
res = faifa_send(faifa, f, sizeof(struct write_request_frame) + len);
if(-1 == res) {
error(faifa_error(faifa));
}
return res;
}
void pib_read_file(char *pib)
{
const char *fname;
FILE *file;
int offset = 0;
if (NULL != opt_fname)
fname = opt_fname;
else
fname = "./pibtool.in";
file = fopen(fname, "r");
while (!feof(file)) {
int read = fread(&pib[offset], 1, 0x400, file);
offset += read;
}
fclose(file);
}
void recv_write_confirmation(faifa_t *faifa)
{
struct write_confirmation_frame f;
int res;
res = faifa_recv(faifa, (char *)&f, sizeof(struct write_confirmation_frame));
}
int pib_write(faifa_t *faifa)
{
char *pib = malloc(16352);
int offset = 0;
pib_read_file(pib);
for (offset = 0; offset < 16352; offset += 0x400) {
int len = (offset + 0x400 > 16352) ?
16352 - offset :
0x400;
send_write_request(faifa, pib, offset, len);
recv_write_confirmation(faifa);
}
send_commit_write_to_nvm(faifa, 2);
}
void checksum_test()
{
uint32_t cksum;
char *buf = malloc(1024);
memset(buf, 0, 1024);
cksum = checksum(buf, 1024);
printf("checksum_test: 0x%08x\n", cksum);
free(buf);
}
/**
* main - main function of faifa
* @argc: number of arguments
* @argv: array of arguments
*/
int main(int argc, char **argv)
{
faifa_t *faifa;
char *opt_ifname = NULL;
char *opt_macaddr = NULL;
int opt_verbose = 0;
int opt_write = 0;
int opt_dump = 0;
int c;
int ret = 0;
u_int8_t addr[ETHER_ADDR_LEN] = { 0 };
prog_name = strdup(basename(argv[0]));
fprintf(stdout, "%s for HomePlug AV (SVN revision %d)\n\n", prog_name, SVN_REV);
if (argc < 2) {
usage();
return -1;
}
while ((c = getopt(argc, argv, "di:a:k:f:wvh")) != -1) {
switch (c) {
case 'i':
opt_ifname = optarg;
break;
case 'a':
opt_macaddr = optarg;
break;
case 'f':
opt_fname = optarg;
break;
case 'd':
opt_dump = 1;
break;
case 'w':
opt_write = 1;
break;
case 'k':
opt_key = 1;
break;
case 'v':
opt_verbose = 1;
break;
case 'h':
default:
opt_help = 1;
break;
}
}
if (opt_help) {
usage();
return -1;
}
if (opt_ifname == NULL)
opt_ifname = "eth0";
faifa = faifa_init();
if (faifa == NULL) {
error("can't initialize Faifa library");
return -1;
}
if (faifa_open(faifa, opt_ifname) == -1) {
error(faifa_error(faifa));
faifa_free(faifa);
return -1;
}
faifa_set_verbose(faifa, opt_verbose);
if (opt_macaddr) {
ret = faifa_parse_mac_addr(faifa, opt_macaddr, addr);
if (ret < 0) {
error(faifa_error(faifa));
goto out_error;
}
faifa_set_dst_addr(faifa, addr);
}
checksum_test();
if (opt_dump) {
pib_dump(faifa);
} else if (opt_write) {
pib_write(faifa);
} else {
error("nothing to do");
ret = -1;
}
out_error:
faifa_close(faifa);
faifa_free(faifa);
return ret;
}