-
Notifications
You must be signed in to change notification settings - Fork 102
/
DeviceProxy_dot11.cpp
418 lines (378 loc) · 13.3 KB
/
DeviceProxy_dot11.cpp
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
/*
* This file is part of USBProxy.
*/
#include "DeviceProxy_dot11.h"
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include "HexString.h"
#include "Packet.h"
#include "TRACE.h"
#include "USBString.h"
// Find the right place to pull this in from
#define cpu_to_le16(x) (x)
int DeviceProxy_dot11::debugLevel = 1;
#define STRING_MANUFACTURER 1
#define STRING_PRODUCT 2
#define STRING_SERIAL 3
#define STRING_DOT11 4
extern "C" {
#include <lorcon2/lorcon.h>
static lorcon_driver_t *drvlist, *driver; // Needed to set up interface/context
static lorcon_t *context; // LORCON context
static USBString** dot11_strings;
static int dot11_stringMaxIndex;
DeviceProxy_dot11::DeviceProxy_dot11(ConfigParser *cfg) {
/* FIXME pull these values from the config object */
int vendorId = 0xffff;
int productId = 0x0005;
interface = cfg->get("802.11_interface");
p_is_connected = false;
dot11_device_descriptor.bLength = USB_DT_DEVICE_SIZE;
dot11_device_descriptor.bDescriptorType = USB_DT_DEVICE;
dot11_device_descriptor.bcdUSB = cpu_to_le16(0x0100);
dot11_device_descriptor.bDeviceClass = USB_CLASS_HID;
dot11_device_descriptor.bDeviceSubClass = 0;
dot11_device_descriptor.bDeviceProtocol = 0;
dot11_device_descriptor.bMaxPacketSize0=64;
dot11_device_descriptor.idVendor = cpu_to_le16(vendorId & 0xffff);
dot11_device_descriptor.idProduct = cpu_to_le16(productId & 0xffff);
fprintf(stderr,"V: %04x P: %04x\n",dot11_device_descriptor.idVendor,dot11_device_descriptor.idProduct);
dot11_device_descriptor.bcdDevice = 0;
dot11_device_descriptor.iManufacturer = STRING_MANUFACTURER;
dot11_device_descriptor.iProduct = STRING_PRODUCT;
dot11_device_descriptor.iSerialNumber = STRING_SERIAL;
dot11_device_descriptor.bNumConfigurations = 1;
dot11_config_descriptor.bLength = USB_DT_CONFIG_SIZE;
dot11_config_descriptor.bDescriptorType = USB_DT_CONFIG;
dot11_config_descriptor.bNumInterfaces = 1;
dot11_config_descriptor.bConfigurationValue = 1;
dot11_config_descriptor.iConfiguration = STRING_DOT11;
dot11_config_descriptor.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
dot11_config_descriptor.bMaxPower = 1; /* self-powered */
dot11_interface_descriptor.bLength = USB_DT_INTERFACE_SIZE;
dot11_interface_descriptor.bDescriptorType = USB_DT_INTERFACE;
dot11_interface_descriptor.bInterfaceNumber=0;
dot11_interface_descriptor.bAlternateSetting=0;
dot11_interface_descriptor.bNumEndpoints = 2;
dot11_interface_descriptor.bInterfaceClass = USB_CLASS_HID;
dot11_interface_descriptor.bInterfaceSubClass=0;
dot11_interface_descriptor.bInterfaceProtocol=0;
dot11_interface_descriptor.iInterface = STRING_DOT11;
struct usb_endpoint_descriptor *ep;
ep = &dot11_eps[0];
ep->bLength = USB_DT_ENDPOINT_SIZE;
ep->bDescriptorType = USB_DT_ENDPOINT;
ep->bEndpointAddress = USB_ENDPOINT_DIR_MASK | 1;
ep->bmAttributes = USB_ENDPOINT_XFER_BULK;
ep->wMaxPacketSize = 64;
ep->bInterval = 10;
ep = &dot11_eps[1];
ep->bLength = USB_DT_ENDPOINT_SIZE;
ep->bDescriptorType = USB_DT_ENDPOINT;
ep->bEndpointAddress = 2;
ep->bmAttributes = USB_ENDPOINT_XFER_BULK;
ep->wMaxPacketSize = 64;
ep->bInterval = 10;
dot11_config_descriptor.wTotalLength=dot11_config_descriptor.bLength+dot11_interface_descriptor.bLength+dot11_eps[0].bLength+dot11_eps[1].bLength;
__u16 string0[2]={0x0409,0x0000};
dot11_strings=(USBString**)calloc(5,sizeof(USBString*));
dot11_strings[0]=new USBString(string0,0,0);
dot11_strings[STRING_MANUFACTURER]=new USBString("USBProxy",STRING_MANUFACTURER,0x409);
dot11_strings[STRING_PRODUCT]=new USBString("802.11 HID Device",STRING_PRODUCT,0x409);
dot11_strings[STRING_SERIAL]=new USBString("0001",STRING_SERIAL,0x409);
dot11_strings[STRING_DOT11]=new USBString("802.11",STRING_DOT11,0x409);
dot11_stringMaxIndex=STRING_DOT11;
monitor = false;
}
DeviceProxy_dot11::~DeviceProxy_dot11() {
disconnect();
delete[] dot11_strings;
free(dot11_strings);
}
int DeviceProxy_dot11::connect(int timeout) {
if ( interface == "" ) {
fprintf(stderr, "ERROR: 802.11 interface not set\n");
return 1;
}
fprintf(stderr, "802.11: Using interface %s\n", interface.c_str());
if ( (driver = lorcon_auto_driver(interface.c_str())) == NULL) {
fprintf(stderr, "ERROR: 802.11: Could not determine the driver for %s\n",interface.c_str());
return 1;
} else {
fprintf(stderr, "802.11: Driver: %s\n",driver->name);
}
// Create LORCON context
if ((context = lorcon_create(interface.c_str(), driver)) == nullptr) {
fprintf(stderr, "Error: 802.11: Failed to create context");
return 1;
}
int rv = lorcon_open_monitor(context);
if (rv < 0) {
fprintf(stderr, "Error: 802.11: Could not create Monitor Mode interface!\n");
return 1;
} else {
fprintf(stderr, "802.11: Monitor Mode VAP: %s, (%d)\n",
lorcon_get_vap(context), rv);
monitor = true;
}
lorcon_free_driver_list(driver);
p_is_connected = true;
return 0;
}
void DeviceProxy_dot11::disconnect() {
// Free the LORCON Context
lorcon_free(context);
p_is_connected = false;
}
void DeviceProxy_dot11::reset() {
}
bool DeviceProxy_dot11::is_connected() {
return p_is_connected;
}
bool DeviceProxy_dot11::is_highspeed() {
return false;
}
//return -1 to stall
int DeviceProxy_dot11::control_request(const usb_ctrlrequest* setup_packet, int* nbytes, __u8* dataptr, int timeout) {
int rv = 0;
if (debugLevel>1) {
char* hex=hex_string((void*)setup_packet,sizeof(*setup_packet));
fprintf(stderr, "802.11< %s\n",hex);
free(hex);
}
if((setup_packet->bRequestType & USB_DIR_IN) && setup_packet->bRequest == USB_REQ_GET_DESCRIPTOR) {
__u8* buf;
__u8* p;
__u8 idx;
const usb_string_descriptor* string_desc;
switch ((setup_packet->wValue)>>8) {
case USB_DT_DEVICE:
memcpy(dataptr, &dot11_device_descriptor, dot11_device_descriptor.bLength);
*nbytes = dot11_device_descriptor.bLength;
break;
case USB_DT_CONFIG:
idx=setup_packet->wValue & 0xff;
if (idx>=dot11_device_descriptor.bNumConfigurations) return -1;
buf=(__u8*)malloc(dot11_config_descriptor.wTotalLength);
p=buf;
memcpy(p, &dot11_config_descriptor, dot11_config_descriptor.bLength);
p+=dot11_config_descriptor.bLength;
memcpy(p, &dot11_interface_descriptor, dot11_interface_descriptor.bLength);
p+=dot11_interface_descriptor.bLength;
memcpy(p, &dot11_eps[0], dot11_eps[0].bLength);
p+=dot11_eps[0].bLength;
memcpy(p, &dot11_eps[1], dot11_eps[1].bLength);
*nbytes = dot11_config_descriptor.wTotalLength>setup_packet->wLength?setup_packet->wLength:dot11_config_descriptor.wTotalLength;
memcpy(dataptr, buf, *nbytes);
free(buf);
break;
case USB_DT_STRING:
idx=setup_packet->wValue & 0xff;
if (idx>0 && setup_packet->wIndex!=0x409) return -1;
if (idx>dot11_stringMaxIndex) return -1;
string_desc=dot11_strings[idx]->get_descriptor();
*nbytes=string_desc->bLength>setup_packet->wLength?setup_packet->wLength:string_desc->bLength;
memcpy(dataptr,string_desc,*nbytes);
break;
case USB_DT_DEVICE_QUALIFIER:
return -1;
break;
case USB_DT_OTHER_SPEED_CONFIG:
return -1;
break;
}
} else if (setup_packet->bRequest==USB_REQ_GET_CONFIGURATION){
dataptr[0]=1;
*nbytes=1;
} else if (setup_packet->bRequest==USB_REQ_SET_CONFIGURATION){
fprintf(stderr, "Setting config %d (As if that does anything)\n",
setup_packet->wValue);
} else if (setup_packet->bRequest==USB_REQ_GET_INTERFACE){
dataptr[0]=1;
*nbytes=1;
} else if (setup_packet->bRequestType & USB_TYPE_VENDOR) {
/* These are our custom commands */
rv = vendor_request(setup_packet, nbytes, dataptr, timeout);
} else {
fprintf(stderr,"Unhandled control request: 0x%02x, 0x%02x, %d, %d\n",
setup_packet->bRequestType, setup_packet->bRequest,
setup_packet->wValue, setup_packet->wIndex);
}
if (debugLevel>1 && nbytes) {
char* hex=hex_string(dataptr, *nbytes);
fprintf(stderr, "802.11> %s\n",hex);
free(hex);
}
return 0;
}
int DeviceProxy_dot11::vendor_request(const usb_ctrlrequest* setup_packet, int* nbytes, __u8* dataptr, int timeout) {
int rv, value;
char *chr_res;
uint8_t **mac;
fprintf(stderr, "802.11> Handling vendor request (%d)\n", setup_packet->bRequest);
switch (setup_packet->bRequest) {
case DOT11_OPEN_INJECT:
rv = lorcon_open_inject(context);
if (rv < 0) {
fprintf(stderr, "Error: 802.11: Could not create Injector interface!\n");
return 1;
} else {
fprintf(stderr, "802.11: Injector VAP: %s\n",
lorcon_get_vap(context));
lorcon_free_driver_list(driver);
}
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_OPEN_MONITOR:
rv = lorcon_open_monitor(context);
if (rv < 0) {
fprintf(stderr, "Error: 802.11: Could not create Monitor Mode interface!\n");
return 1;
} else {
fprintf(stderr, "802.11: Monitor Mode VAP: %s, (%d)\n",
lorcon_get_vap(context), rv);
lorcon_free_driver_list(driver);
monitor = true;
}
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_OPEN_INJMON:
rv = lorcon_open_injmon(context);
if (rv < 0) {
fprintf(stderr, "Error: 802.11: Could not create Injector / Monitor Mode interface!\n");
return 1;
} else {
fprintf(stderr, "802.11: Injector / Monitor Mode VAP: %s\n",
lorcon_get_vap(context));
lorcon_free_driver_list(driver);
monitor = true;
}
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_SET_TIMEOUT:
value = *(int *)(dataptr);
lorcon_set_timeout(context, value);
*nbytes = 0;
break;
case DOT11_GET_TIMEOUT:
/* I have no idea if this is right... */
rv = lorcon_get_timeout(context);
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_GET_CAPIFACE:
chr_res = (char *) lorcon_get_capiface(context);
*nbytes = strlen(chr_res) + 1;
fprintf(stderr, "CapIface: %s\n", chr_res);
memcpy(dataptr, chr_res, *nbytes);
break;
case DOT11_GET_DRIVER_NAME:
chr_res = (char *) lorcon_get_driver_name(context);
*nbytes = strlen(chr_res);
memcpy(dataptr, chr_res, *nbytes);
break;
case DOT11_CLOSE_INTERFACE:
lorcon_close(context);
*nbytes = 0;
break;
case DOT11_GET_DATALINK:
rv = lorcon_get_datalink(context);
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_SET_DATALINK:
value = *(int *)(dataptr);
rv = lorcon_set_datalink(context, value);
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_GET_CHANNEL:
rv = lorcon_get_channel(context);
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_SET_CHANNEL:
value = *(int *)(dataptr);
rv = lorcon_set_channel(context, value);
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_GET_HWMAC:
*nbytes = lorcon_get_hwmac(context, (uint8_t **) &chr_res);
memcpy(dataptr, chr_res, *nbytes);
break;
case DOT11_SET_HWMAC:
rv = lorcon_set_hwmac(context, *nbytes, dataptr);
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
case DOT11_ADD_WEPKEY:
rv = lorcon_add_wepkey(context, dataptr, dataptr+6, *nbytes-6);
*(int *)(dataptr) = rv;
*nbytes = 4;
break;
}
}
/* Send data to the device */
void DeviceProxy_dot11::send_data(__u8 endpoint,__u8 attributes, __u16 maxPacketSize, __u8* dataptr, int length) {
//fprintf(stderr, "send_data\n");
}
/* Receive data from the device */
void DeviceProxy_dot11::receive_data(__u8 endpoint,__u8 attributes, __u16 maxPacketSize, __u8** dataptr, int* length, int timeout) {
//fprintf(stderr, "receive_data\n");
int rv;
lorcon_packet_t *packet;
if(monitor) {
rv = lorcon_next_ex(context, &packet);
if(rv > 0) {
dot11_packet_header_t *pkthdr;
// pkthdr = (dot11_packet_header_t *) malloc(sizeof(dot11_packet_header_t));
*dataptr = (__u8 *) malloc(sizeof(dot11_packet_header_t) + packet->length_header + packet->length_data);
if(*dataptr==NULL)
fprintf(stderr, "Uh oh, malloc failed\n");
pkthdr = (dot11_packet_header_t *) dataptr;
pkthdr->tv_sec = packet->ts.tv_sec;
pkthdr->tv_usec = packet->ts.tv_usec;
pkthdr->dlt = packet->dlt;
pkthdr->channel = packet->channel;
pkthdr->length_capheader = packet->length_header;
pkthdr->length_data = packet->length_data;
fprintf(stderr, "Allocating %d + %d bytes (length=%d)\n",
packet->length_header,
packet->length_data,
packet->length);
//memcpy(*dataptr+sizeof(dot11_packet_header_t), packet->packet_header, packet->length_header);
//memcpy(*dataptr+sizeof(dot11_packet_header_t)+packet->length_header,
// packet->packet_data,
// packet->length_data);
*length = sizeof(dot11_packet_header_t) + packet->length_header + packet->length_data;
free(packet);
}
}
}
void DeviceProxy_dot11::setConfig(Configuration* fs_cfg, Configuration* hs_cfg, bool hs) {
;
}
void DeviceProxy_dot11::claim_interface(__u8 interface) {
;
}
void DeviceProxy_dot11::release_interface(__u8 interface) {
;
}
__u8 DeviceProxy_dot11::get_address() {
return 1;
}
static DeviceProxy_dot11 *proxy;
DeviceProxy * get_deviceproxy_plugin(ConfigParser *cfg) {
proxy = new DeviceProxy_dot11(cfg);
return (DeviceProxy *) proxy;
}
void destroy_plugin() {
delete proxy;
}
}