-
Notifications
You must be signed in to change notification settings - Fork 27
/
simulator.c
284 lines (235 loc) · 6.29 KB
/
simulator.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
/*
* Homeplug AV INT6x00 simulator device
*
* Copyright (C) 2012, Florian Fainelli <[email protected]>
*
* this device mimics the behavior of a Qualcomm/Atheros/Intellon INT6x00
* device and will answer to the frames sent to it
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <linux/if_ether.h>
#include <event2/event-config.h>
#include <event2/event.h>
#include <event2/util.h>
#include "homeplug_av.h"
struct context {
struct event_base *ev;
struct event *read_ev;
int sock_fd;
const char *iface;
int if_index;
};
static int sim_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 sim_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 sim_send_pkt(ctx, to, &hdr, sizeof(hdr), payload, payload_len);
}
static void sim_read_cb(evutil_socket_t fd, short flags, void *argv)
{
struct context *ctx = argv;
char frame[ETH_DATA_LEN];
ssize_t len;
socklen_t lllen;
struct sockaddr_ll ll;
struct hpav_frame_header *hdr;
const void *payload = NULL;
size_t payload_size = 0;
uint8_t qca_bcast[ETH_ALEN] = { 0x00, 0xB0, 0x52, 0x00, 0x00, 0x00 };
int ret;
lllen = sizeof(ll);
len = recvfrom(ctx->sock_fd, frame, sizeof(frame), 0,
(struct sockaddr *)&ll, &lllen);
if (len < 0 || len < HPAV_MIN_FRAMSIZ) {
if (errno != EAGAIN)
perror("recvfrom");
return;
}
hdr = (struct hpav_frame_header *)frame;
switch (hdr->mmtype) {
case 0xA000:
fprintf(stdout, "Get Software version\n");
struct get_device_sw_version_confirm sw_confirm;
memset(&sw_confirm, 0, sizeof(sw_confirm));
sw_confirm.mstatus = 0;
sw_confirm.device_id = INT6300_DEVICE_ID;
sw_confirm.version_length = sizeof(sw_confirm.version);
snprintf((char *)sw_confirm.version, sizeof(sw_confirm.version), "%s", "Faifa simulator");
sw_confirm.upgradeable = 1;
payload = &sw_confirm;
payload_size = sizeof(sw_confirm);
break;
}
if (!payload_size)
return;
ret = sim_send_vendor_pkt(ctx, qca_bcast, htole16(hdr->mmtype + 1),
payload, payload_size);
if (ret)
fprintf(stdout, "failed to reply: %d\n", ret);
}
static int sim_init_ctx(struct context *ctx)
{
int fd;
int ret;
struct sockaddr_ll ll;
ctx->ev = event_base_new();
if (!ctx->ev) {
fprintf(stderr, "failed to create new libevent context\n");
return -ENOMEM;
}
fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETHERTYPE_HOMEPLUG_AV));
if (fd < 0) {
perror("socket");
ret = fd;
goto out;
}
ctx->if_index = if_nametoindex(ctx->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(fd, (struct sockaddr *)&ll, sizeof(ll));
if (ret < 0) {
perror("bind");
goto out_close;
}
ctx->sock_fd = fd;
/* setup libevent for polling this socket */
ctx->read_ev = event_new(ctx->ev, fd, EV_READ | EV_PERSIST, sim_read_cb, ctx);
if (!ctx->read_ev) {
fprintf(stderr, "failed to create read event");
ret = -EINVAL;
goto out_close;
}
fprintf(stdout, "initialized RAW socket\n");
event_add(ctx->read_ev, NULL);
return 0;
out_close:
close(fd);
out:
event_base_free(ctx->ev);
return ret;
}
static void sim_deinit_ctx(struct context *ctx)
{
/* disable all events */
event_base_free(ctx->ev);
}
static int sim_event_loop(struct context *ctx)
{
event_base_dispatch(ctx->ev);
return 0;
}
static void usage(const char *name)
{
fprintf(stderr, "Usage %s [options] [interface]\n"
"-h: this help",
name);
exit(1);
}
int main(int argc, char **argv)
{
int opt;
struct context ctx;
int ret;
const char *appname = argv[0];
memset(&ctx, 0, sizeof(ctx));
while ((opt = getopt(argc, argv, "V:h")) > 0) {
switch (opt) {
case 'h':
default:
usage(appname);
break;
}
}
argv += optind;
argc -= optind;
ctx.iface = argv[0];
if (!ctx.iface)
usage(appname);
ret = sim_init_ctx(&ctx);
if (ret) {
fprintf(stderr, "failed to initialize context\n");
return ret;
}
ret = sim_event_loop(&ctx);
sim_deinit_ctx(&ctx);
return ret;
}