forked from ffainelli/faifa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpav_cfg.c
376 lines (313 loc) · 7.58 KB
/
hpav_cfg.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
/*
* Lightweight Homeplug AV configuration utility
*
* Generates a NMK/DAK given their NPW and DPW
*
* Copyright (C) 2012, Florian Fainelli <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include "homeplug_av.h"
#include "crypto.h"
struct context {
int sock_fd;
int if_index;
};
static int send_pkt(struct context *ctx, const uint8_t *to,
const void *hdr, size_t hdrlen,
const void *payload, size_t payload_len)
{
struct sockaddr_ll ll;
struct msghdr msg;
struct iovec iov[3];
size_t total_len;
uint8_t padding[64];
int ret;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = iov;
total_len = 0;
iov[msg.msg_iovlen].iov_base = (void *)hdr;
iov[msg.msg_iovlen].iov_len = hdrlen;
total_len += hdrlen;
msg.msg_iovlen = 1;
if (payload_len) {
iov[msg.msg_iovlen].iov_base = (void *)payload;
iov[msg.msg_iovlen].iov_len = payload_len;
total_len += payload_len;
msg.msg_iovlen++;
}
if (total_len < 64) {
memset(padding, 0, sizeof(padding));
iov[msg.msg_iovlen].iov_base = (void *)padding;
iov[msg.msg_iovlen].iov_len = 64 - total_len;
msg.msg_iovlen++;
}
memset(&ll, 0, sizeof(ll));
ll.sll_family = AF_PACKET;
ll.sll_ifindex = ctx->if_index;
ll.sll_protocol = htons(ETHERTYPE_HOMEPLUG_AV);
memcpy(ll.sll_addr, to, ETH_ALEN);
msg.msg_name = ≪
msg.msg_namelen = sizeof(ll);
ret = sendmsg(ctx->sock_fd, &msg, 0);
if (ret < 0) {
if (errno != EAGAIN)
perror("sendmsg");
return ret;
}
return 0;
}
struct homeplug_av_vendor_hdr {
uint8_t mmver;
uint16_t mmtype;
uint8_t oui[3];
} __attribute__((__packed__));
static int send_vendor_pkt(struct context *ctx, const uint8_t *to,
uint16_t mmtype, const void *payload, size_t payload_len)
{
struct homeplug_av_vendor_hdr hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.mmver = 0;
hdr.mmtype = mmtype;
hdr.oui[0] = 0x00;
hdr.oui[1] = 0xB0;
hdr.oui[2] = 0x52;
return send_pkt(ctx, to, &hdr, sizeof(hdr), payload, payload_len);
}
static int init_socket(struct context *ctx, const char *iface)
{
int ret;
struct sockaddr_ll ll;
ctx->sock_fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETHERTYPE_HOMEPLUG_AV));
if (ctx->sock_fd < 0) {
perror("socket");
return -1;
}
ctx->if_index = if_nametoindex(iface);
memset(&ll, 0, sizeof(ll));
ll.sll_family = AF_PACKET;
ll.sll_ifindex = ctx->if_index;
ll.sll_protocol = htons(ETHERTYPE_HOMEPLUG_AV);
ret = bind(ctx->sock_fd, (struct sockaddr *)&ll, sizeof(ll));
if (ret < 0) {
perror("bind");
goto out_close;
}
return 0;
out_close:
close(ctx->sock_fd);
return ret;
}
static uint8_t bcast_hpav_mac[ETH_ALEN] = { 0x00, 0xB0, 0x52, 0x00, 0x00, 0x01 };
static int send_key(struct context *ctx, const char *npw,
const char *dpw, const uint8_t mac[ETH_ALEN])
{
struct set_encryption_key_request key_req;
uint8_t key[16];
memset(&key_req, 0, sizeof(key_req));
key_req.peks = 0x01;
gen_passphrase(npw, key, nmk_salt);
memcpy(key_req.nmk, key, AES_KEY_SIZE);
key_req.peks_payload = NO_KEY;
if (dpw) {
gen_passphrase(dpw, key, dak_salt);
memcpy(key_req.dak, key, AES_KEY_SIZE);
key_req.peks_payload = DST_STA_DAK;
}
memcpy(key_req.rdra, mac, ETH_ALEN);
return send_vendor_pkt(ctx, mac, HPAV_MMTYPE_SET_KEY_REQ,
&key_req, sizeof(key_req));
}
static int read_key_confirm(struct context *ctx, const uint8_t mac[ETH_ALEN])
{
uint8_t frame[ETH_DATA_LEN];
ssize_t len;
socklen_t sk_len;
struct sockaddr_ll ll;
struct hpav_frame *hpav_frame;
uint8_t status;
uint8_t *from;
sk_len = sizeof(ll);
len = recvfrom(ctx->sock_fd, frame, sizeof(frame), 0,
(struct sockaddr *)&ll, &sk_len);
if (len < 0 || len < HPAV_MIN_FRAMSIZ) {
if (errno != EAGAIN)
perror("recvfrom");
return len;
}
from = ll.sll_addr;
/* destination MAC is different from broadcast HomePlug AV MAC
* and source MAC is different form destination MAC
*/
if (memcmp(mac, bcast_hpav_mac, ETH_ALEN) &&
memcmp(mac, from, ETH_ALEN)) {
fprintf(stderr, "spurious reply from another station\n");
return 1;
}
hpav_frame = (struct hpav_frame *)frame;
if (le16toh(hpav_frame->header.mmtype) != HPAV_MMTYPE_SET_KEY_CNF)
return 1;
status = hpav_frame->payload.vendor.data[0];
switch (status) {
case KEY_SUCCESS:
fprintf(stdout, "Success!!\n");
return 0;
case KEY_INV_EKS:
fprintf(stderr, "Invalid EKS\n");
return 1;
case KEY_INV_PKS:
fprintf(stderr, "Invalid PKS\n");
return 1;
default:
fprintf(stderr, "unknown answer: 0x%02x\n", status);
}
return 0;
}
static int send_reset(struct context *ctx, uint8_t *mac)
{
return send_vendor_pkt(ctx, mac, HPAV_MMTYPE_RS_DEV_REQ,
NULL, 0);
}
static int generate_passphrase(struct context *ctx,
const char *npw, const char *dpw)
{
uint8_t key[16];
int i;
if (!npw && !dpw) {
fprintf(stderr, "missing NPW and DPW\n");
return 1;
}
if (npw)
gen_passphrase(npw, key, nmk_salt);
else
gen_passphrase(dpw, key, dak_salt);
for (i = 0; i < sizeof(key); i++)
fprintf(stdout, "%02x", key[i]);
return 0;
}
static void sighandler(int signo)
{
if (signo == SIGALRM) {
fprintf(stdout, "timeout reading answer from sta, exiting\n");
exit(1);
}
}
static void usage(void)
{
fprintf(stderr, "Usage: hpav_cfg [options] interface\n"
"-n: NPW passphrase\n"
"-d: DPW passphrase\n"
"-p: same as -n (deprecated)\n"
"-a: device MAC address\n"
"-r: send a device reset\n"
"-k: hash only\n");
}
int main(int argc, char **argv)
{
int opt;
int ret;
const char *mac_address = NULL;
const char *npw = NULL;
const char *dpw = NULL;
const char *iface = NULL;
struct context ctx;
unsigned int hash_only = 0;
unsigned int reset_device = 0;
uint8_t mac[ETH_ALEN] = { 0 };
memset(&ctx, 0, sizeof(ctx));
while ((opt = getopt(argc, argv, "n:d:p:a:i:krh")) > 0) {
switch (opt) {
case 'n':
case 'p':
npw = optarg;
break;
case 'd':
dpw = optarg;
break;
case 'a':
mac_address = optarg;
break;
case 'i':
iface = optarg;
break;
case 'k':
hash_only = 1;
break;
case 'r':
reset_device = 1;
break;
case 'h':
default:
usage();
return 1;
}
}
if (argc < 2) {
usage();
return 1;
}
argc -= optind;
argv += optind;
if (hash_only)
return generate_passphrase(&ctx, npw, dpw);
iface = argv[0];
if (!iface) {
fprintf(stderr, "missing interface argument\n");
return 1;
}
fprintf(stdout, "Interface: %s\n", iface);
if (mac_address) {
ret = sscanf(mac_address,
"%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8"",
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
if (ret != ETH_ALEN) {
fprintf(stdout, "invalid MAC address\n");
return ret;
}
fprintf(stdout, "MAC: %s\n", mac_address);
} else {
memcpy(mac, bcast_hpav_mac, sizeof(bcast_hpav_mac));
fprintf(stdout, "MAC: using broadcast HPAV\n");
}
ret = init_socket(&ctx, iface);
if (ret) {
fprintf(stdout, "failed to initialize raw socket\n");
return ret;
}
if (reset_device) {
ret = send_reset(&ctx, mac);
if (ret)
fprintf(stdout, "failed to send reset\n");
return ret;
}
fprintf(stdout, "NPW: %s\n", npw);
ret = send_key(&ctx, npw, dpw, mac);
if (ret) {
fprintf(stdout, "failed to send key\n");
return ret;
}
if (signal(SIGALRM, sighandler) == SIG_ERR) {
fprintf(stdout, "failed to setup signal handler\n");
return ret;
}
/* catch answer or timeout */
alarm(3);
ret = read_key_confirm(&ctx, mac);
return ret;
}