-
Notifications
You must be signed in to change notification settings - Fork 26
/
sniffer.c
391 lines (325 loc) · 10.6 KB
/
sniffer.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
/*
* Sigma Control API DUT (sniffer)
* Copyright (c) 2013-2014, Qualcomm Atheros, Inc.
* Copyright (c) 2019, The Linux Foundation
* All Rights Reserved.
* Licensed under the Clear BSD license. See README for more details.
*/
#include "sigma_dut.h"
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
static void capture_process(const char *ifname, const char *filename)
{
char *env[] = { NULL };
char *argv[] = { "sigma_dut[capture]", "-i", strdup(ifname),
"-w", strdup(filename), NULL };
execve("/usr/bin/dumpcap", argv, env);
perror("execve");
exit(EXIT_FAILURE);
}
static enum sigma_cmd_result cmd_sniffer_control_start(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *filename = get_param(cmd, "filename");
enum sigma_cmd_result res;
pid_t pid;
if (dut->sniffer_pid) {
sigma_dut_print(dut, DUT_MSG_INFO, "Sniffer was already capturing - restart based on new parameters");
sniffer_close(dut);
}
if (filename == NULL) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Missing filename argument");
return STATUS_SENT;
}
if (strchr(filename, '/')) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,Invalid filename");
return STATUS_SENT;
}
res = cmd_wlantest_set_channel(dut, conn, cmd);
if (res != SUCCESS_SEND_STATUS)
return res;
mkdir("Captures", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
sigma_dut_print(dut, DUT_MSG_INFO, "Starting sniffer process");
snprintf(dut->sniffer_filename, sizeof(dut->sniffer_filename),
"Captures/%s", filename);
pid = fork();
if (pid < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to fork sniffer process");
return STATUS_SENT;
}
if (pid == 0) {
capture_process(dut->sniffer_ifname, dut->sniffer_filename);
return SUCCESS_SEND_STATUS;
}
dut->sniffer_pid = pid;
return SUCCESS_SEND_STATUS;
}
void sniffer_close(struct sigma_dut *dut)
{
if (!dut->sniffer_pid)
return;
if (kill(dut->sniffer_pid, SIGTERM) < 0) {
printf("Failed to kill sniffer process: %s\n", strerror(errno));
dut->sniffer_pid = 0;
return;
}
waitpid(dut->sniffer_pid, NULL, 0);
if (dut->sniffer_filename[0]) {
chmod(dut->sniffer_filename,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
dut->sniffer_filename[0] = '\0';
}
dut->sniffer_pid = 0;
}
static enum sigma_cmd_result cmd_sniffer_control_stop(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
if (!dut->sniffer_pid) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Sniffer was not capturing");
return STATUS_SENT;
}
sniffer_close(dut);
return SUCCESS_SEND_STATUS;
}
static enum sigma_cmd_result
cmd_sniffer_control_field_check(struct sigma_dut *dut, struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *filename = get_param(cmd, "filename");
const char *framename = get_param(cmd, "framename");
const char *srcmac = get_param(cmd, "srcmac");
const char *wsc_state = get_param(cmd, "WSC_State");
const char *pvb_bit = get_param(cmd, "pvb_bit");
const char *moredata_bit = get_param(cmd, "MoreData_bit");
const char *eosp_bit = get_param(cmd, "EOSP_bit");
char buf[2000], *pos;
FILE *f;
if (filename == NULL || srcmac == NULL)
return INVALID_SEND_STATUS;
if (strchr(filename, '/')) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,Invalid filename");
return STATUS_SENT;
}
if (!file_exists("sniffer-control-field-check.py")) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,sniffer-control-field-check.py not found");
return STATUS_SENT;
}
snprintf(buf, sizeof(buf),
"./sniffer-control-field-check.py FileName=Captures/%s SrcMac=%s%s%s%s%s%s%s%s%s%s%s",
filename, srcmac,
framename ? " FrameName=" : "", framename ? framename : "",
wsc_state ? " WSC_State=" : "", wsc_state ? wsc_state : "",
moredata_bit ? " MoreData_bit=" : "",
moredata_bit ? moredata_bit : "",
eosp_bit ? " EOSP_bit=" : "", eosp_bit ? eosp_bit : "",
pvb_bit ? " pvb_bit=" : "", pvb_bit ? pvb_bit : "");
sigma_dut_print(dut, DUT_MSG_INFO, "Run: %s", buf);
f = popen(buf, "r");
if (f == NULL) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to run sniffer helper");
return STATUS_SENT;
}
if (!fgets(buf, sizeof(buf), f)) {
pclose(f);
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed extract response from sniffer helper");
return STATUS_SENT;
}
pos = strchr(buf, '\n');
if (pos)
*pos = '\0';
pclose(f);
send_resp(dut, conn, SIGMA_COMPLETE, buf);
return STATUS_SENT;
}
static enum sigma_cmd_result cmd_sniffer_get_info(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
char buf[200];
snprintf(buf, sizeof(buf), "WfaSnifferVersion,SigmaSniffer-foo,SnifferSTA,foo,DeviceSwInfo,foo,WiresharkVersion,foo");
send_resp(dut, conn, SIGMA_COMPLETE, buf);
return STATUS_SENT;
}
static enum sigma_cmd_result
cmd_sniffer_control_filter_capture(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *infile = get_param(cmd, "InFile");
const char *outfile = get_param(cmd, "OutFile");
const char *srcmac = get_param(cmd, "SrcMac");
const char *framename = get_param(cmd, "FrameName");
const char *nframes = get_param(cmd, "Nframes");
const char *hasfield = get_param(cmd, "HasField");
const char *datalen = get_param(cmd, "Datalen");
char buf[500], *pos;
FILE *f;
if (infile == NULL || outfile == NULL || srcmac == NULL ||
nframes == NULL)
return INVALID_SEND_STATUS;
if (strchr(infile, '/') || strchr(outfile, '/'))
return INVALID_SEND_STATUS;
if (!file_exists("sniffer-control-filter-capture.py")) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,sniffer-control-filter-capture.py not found");
return STATUS_SENT;
}
snprintf(buf, sizeof(buf),
"./sniffer-control-filter-capture.py InFile=Captures/%s OutFile=Captures/%s SrcMac=%s%s%s Nframes=%s%s%s%s%s",
infile, outfile, srcmac,
framename ? " FrameName=" : "", framename ? framename : "",
nframes,
hasfield ? " HasField=" : "", hasfield ? hasfield : "",
datalen ? " Datalen=" : "", datalen ? datalen : "");
sigma_dut_print(dut, DUT_MSG_INFO, "Run: %s", buf);
f = popen(buf, "r");
if (f == NULL) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to run sniffer helper");
return STATUS_SENT;
}
if (!fgets(buf, sizeof(buf), f)) {
pclose(f);
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed extract response from sniffer helper");
return STATUS_SENT;
}
pos = strchr(buf, '\n');
if (pos)
*pos = '\0';
pclose(f);
send_resp(dut, conn, SIGMA_COMPLETE, buf);
return STATUS_SENT;
}
static enum sigma_cmd_result
cmd_sniffer_get_field_value(struct sigma_dut *dut, struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *infile = get_param(cmd, "FileName");
const char *srcmac = get_param(cmd, "SrcMac");
const char *framename = get_param(cmd, "FrameName");
const char *fieldname = get_param(cmd, "FieldName");
char buf[500], *pos;
FILE *f;
if (infile == NULL || srcmac == NULL || framename == NULL ||
fieldname == NULL)
return INVALID_SEND_STATUS;
if (!file_exists("sniffer-get-field-value.py")) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,sniffer-get-field-value.py not found");
return STATUS_SENT;
}
snprintf(buf, sizeof(buf),
"./sniffer-get-field-value.py FileName=Captures/%s SrcMac=%s FrameName=%s FieldName=%s",
infile, srcmac, framename, fieldname);
sigma_dut_print(dut, DUT_MSG_INFO, "Run: %s", buf);
f = popen(buf, "r");
if (f == NULL) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to run sniffer helper");
return STATUS_SENT;
}
if (!fgets(buf, sizeof(buf), f)) {
pclose(f);
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed extract response from sniffer helper");
return STATUS_SENT;
}
pos = strchr(buf, '\n');
if (pos)
*pos = '\0';
pclose(f);
send_resp(dut, conn, SIGMA_COMPLETE, buf);
return STATUS_SENT;
}
static enum sigma_cmd_result
cmd_sniffer_check_p2p_noa_duration(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
FILE *f;
char buf[200], *pos;
const char *infile = get_param(cmd, "FileName");
const char *bssid = get_param(cmd, "bssid");
const char *srcmac = get_param(cmd, "srcmac");
const char *destmac = get_param(cmd, "destmac");
if (infile == NULL || bssid == NULL || srcmac == NULL ||
destmac == NULL)
return INVALID_SEND_STATUS;
if (!file_exists("sniffer-check-p2p-noa-duration.py")) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,sniffer-check-p2p-noa-duration.py not found");
return STATUS_SENT;
}
snprintf(buf, sizeof(buf),
"./sniffer-check-p2p-noa-duration.py Captures/%s %s %s %s",
infile, bssid, srcmac, destmac);
sigma_dut_print(dut, DUT_MSG_INFO, "Run: %s", buf);
f = popen(buf, "r");
if (f == NULL) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to run sniffer check");
return STATUS_SENT;
}
if (!fgets(buf, sizeof(buf), f)) {
pclose(f);
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed extract response from sniffer check");
return STATUS_SENT;
}
pos = strchr(buf, '\n');
if (pos)
*pos = '\0';
pclose(f);
send_resp(dut, conn, SIGMA_COMPLETE, buf);
return STATUS_SENT;
}
static enum sigma_cmd_result
cmd_sniffer_check_p2p_opps_client(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
char buf[200];
/* TODO */
snprintf(buf, sizeof(buf), "FilterStatus,SUCCESS");
send_resp(dut, conn, SIGMA_COMPLETE, buf);
return STATUS_SENT;
}
static enum sigma_cmd_result
cmd_sniffer_check_frame_field(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
char buf[200];
/* TODO */
snprintf(buf, sizeof(buf), "FilterStatus,SUCCESS");
send_resp(dut, conn, SIGMA_COMPLETE, buf);
return STATUS_SENT;
}
void sniffer_register_cmds(void)
{
sigma_dut_reg_cmd("sniffer_control_start", NULL,
cmd_sniffer_control_start);
sigma_dut_reg_cmd("sniffer_control_stop", NULL,
cmd_sniffer_control_stop);
sigma_dut_reg_cmd("sniffer_control_field_check", NULL,
cmd_sniffer_control_field_check);
sigma_dut_reg_cmd("sniffer_get_info", NULL, cmd_sniffer_get_info);
sigma_dut_reg_cmd("sniffer_control_filter_capture", NULL,
cmd_sniffer_control_filter_capture);
sigma_dut_reg_cmd("wfa_sniffer_control_filter_capture", NULL,
cmd_sniffer_control_filter_capture);
sigma_dut_reg_cmd("sniffer_get_field_value", NULL,
cmd_sniffer_get_field_value);
sigma_dut_reg_cmd("sniffer_check_p2p_NoA_duration", NULL,
cmd_sniffer_check_p2p_noa_duration);
sigma_dut_reg_cmd("sniffer_check_p2p_opps_client", NULL,
cmd_sniffer_check_p2p_opps_client);
sigma_dut_reg_cmd("sniffer_check_frame_field", NULL,
cmd_sniffer_check_frame_field);
}