-
Notifications
You must be signed in to change notification settings - Fork 0
/
drv_mac80211.c
336 lines (259 loc) · 8.31 KB
/
drv_mac80211.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
/*
This file is part of lorcon
lorcon 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.
lorcon 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 lorcon; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Copyright (c) 2005 dragorn and Joshua Wright
*/
#include "config.h"
#include "drv_mac80211.h"
#if defined(SYS_LINUX) && defined(HAVE_LINUX_WIRELESS) && defined(HAVE_LIBNL)
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <net/if_arp.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <linux/types.h>
#include <linux/if.h>
#include <linux/wireless.h>
#include <net/ethernet.h>
#include <netpacket/packet.h>
#define ETH_P_80211_RAW (ETH_P_ECONET + 1)
#include "ifcontrol_linux.h"
#include "nl80211_control.h"
#include "lorcon_int.h"
#ifndef IEEE80211_RADIOTAP_F_FRAG
#define IEEE80211_RADIOTAP_F_FRAG 0x08
#endif
struct mac80211_lorcon {
void *nlhandle, *nlcache, *nlfamily;
};
/* Monitor, inject, and injmon are all the same method, open a new vap */
int mac80211_openmon_cb(lorcon_t *context) {
char *parent;
char pcaperr[PCAP_ERRBUF_SIZE];
struct mac80211_lorcon *extras = (struct mac80211_lorcon *) context->auxptr;
short flags;
struct ifreq if_req;
struct sockaddr_ll sa_ll;
int optval;
socklen_t optlen;
if (strlen(context->vapname) == 0) {
snprintf(context->vapname, MAX_IFNAME_LEN, "%smon", context->ifname);
}
if ((parent = nl80211_find_parent(context->vapname)) == NULL) {
if (nl80211_createvap(context->ifname, context->vapname, context->errstr) < 0) {
free(parent);
return -1;
}
}
free(parent);
if (ifconfig_delta_flags(context->vapname, context->errstr,
(IFF_UP | IFF_RUNNING | IFF_PROMISC)) < 0) {
return -1;
}
if (nl80211_connect(context->vapname, &(extras->nlhandle), &(extras->nlcache),
&(extras->nlfamily), context->errstr) < 0) {
return -1;
}
pcaperr[0] = '\0';
if ((context->pcap = pcap_open_live(context->vapname, LORCON_MAX_PACKET_LEN,
1, context->timeout_ms, pcaperr)) == NULL) {
snprintf(context->errstr, LORCON_STATUS_MAX, "%s", pcaperr);
return -1;
}
context->capture_fd = pcap_get_selectable_fd(context->pcap);
context->dlt = pcap_datalink(context->pcap);
context->inject_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (context->inject_fd < 0) {
snprintf(context->errstr, LORCON_STATUS_MAX, "failed to create injection "
"socket: %s", strerror(errno));
nl80211_disconnect(extras->nlhandle);
pcap_close(context->pcap);
return -1;
}
memset(&if_req, 0, sizeof(if_req));
memcpy(if_req.ifr_name, context->vapname, IFNAMSIZ);
if_req.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(context->inject_fd, SIOCGIFINDEX, &if_req) < 0) {
snprintf(context->errstr, LORCON_STATUS_MAX, "failed to get interface idex: %s",
strerror(errno));
close(context->inject_fd);
pcap_close(context->pcap);
nl80211_disconnect(extras->nlhandle);
return -1;
}
memset(&sa_ll, 0, sizeof(sa_ll));
sa_ll.sll_family = AF_PACKET;
sa_ll.sll_protocol = htons(ETH_P_ALL);
sa_ll.sll_ifindex = if_req.ifr_ifindex;
if (bind(context->inject_fd, (struct sockaddr *) &sa_ll, sizeof(sa_ll)) != 0) {
snprintf(context->errstr, LORCON_STATUS_MAX, "failed to bind injection "
"socket: %s", strerror(errno));
close(context->inject_fd);
pcap_close(context->pcap);
nl80211_disconnect(extras->nlhandle);
return -1;
}
optlen = sizeof(optval);
optval = 20;
if (setsockopt(context->inject_fd, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
snprintf(context->errstr, LORCON_STATUS_MAX, "failed to set priority on "
"injection socket: %s", strerror(errno));
close(context->inject_fd);
pcap_close(context->pcap);
nl80211_disconnect(extras->nlhandle);
return -1;
}
return 1;
}
int mac80211_setchan_cb(lorcon_t *context, int channel) {
struct mac80211_lorcon *extras = (struct mac80211_lorcon *) context->auxptr;
if (nl80211_setchannel_cache(context->vapname, extras->nlhandle, extras->nlfamily,
channel, 0, context->errstr) < 0) {
return -1;
}
return 0;
}
int mac80211_getchan_cb(lorcon_t *context) {
int ch;
if ((ch = iwconfig_get_channel(context->vapname, context->errstr)) < 0) {
// Fall back to parent if vap doesn't act right (mac80211 seems to do this)
if ((ch = iwconfig_get_channel(context->ifname, context->errstr)) < 0)
return -1;
}
return ch;
}
int mac80211_getmac_cb(lorcon_t *context, uint8_t **mac) {
/* 802.11 MACs are always 6 */
uint8_t int_mac[6];
if (ifconfig_get_hwaddr(context->vapname, context->errstr, int_mac) < 0) {
return -1;
}
(*mac) = malloc(sizeof(uint8_t) * 6);
memcpy(*mac, int_mac, 6);
return 6;
}
int mac80211_setmac_cb(lorcon_t *context, int mac_len, uint8_t *mac) {
short flags;
/* 802.11 MACs are always 6 */
if (mac_len != 6) {
snprintf(context->errstr, LORCON_STATUS_MAX,
"MAC passed to mac80211 driver on %s not 6 bytes, all "
"802.11 MACs must be 6 bytes", context->vapname);
return -1;
}
if (ifconfig_ifupdown(context->vapname, context->errstr, 0) < 0)
return -1;
if (ifconfig_set_hwaddr(context->vapname, context->errstr, mac) < 0)
return -1;
if (ifconfig_ifupdown(context->vapname, context->errstr, 1) < 0)
return -1;
return 0;
}
int mac80211_sendpacket(lorcon_t *context, lorcon_packet_t *packet) {
int ret;
u_char rtap_hdr[] = {
/* rt version */
0x00, 0x00,
/* rt len */
0x0e, 0x00,
/* rt bitmap, flags, tx, rx */
0x02, 0xc0, 0x00, 0x00,
/* Allow frgmentation */
IEEE80211_RADIOTAP_F_FRAG,
/* pad */
0x00,
/* rx and tx set to inject */
0x00, 0x00,
0x00, 0x00,
};
u_char *bytes;
int len, freebytes;
struct iovec iov[2];
struct msghdr msg = {
.msg_name = NULL,
.msg_namelen = 0,
.msg_iov = iov,
.msg_iovlen = 2,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0,
};
if (packet->lcpa != NULL) {
len = lcpa_size(packet->lcpa);
freebytes = 1;
bytes = (u_char *) malloc(sizeof(u_char) * len);
lcpa_freeze(packet->lcpa, bytes);
} else if (packet->packet_header != NULL) {
freebytes = 0;
len = packet->length_header;
bytes = (u_char *) packet->packet_header;
} else {
freebytes = 0;
len = packet->length;
bytes = (u_char *) packet->packet_raw;
}
iov[0].iov_base = &rtap_hdr;
iov[0].iov_len = sizeof(rtap_hdr);
iov[1].iov_base = bytes;
iov[1].iov_len = len;
/*
if (encrypt)
rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
*/
ret = sendmsg(context->inject_fd, &msg, 0);
snprintf(context->errstr, LORCON_STATUS_MAX, "drv_mac80211 failed "
"to send packet: %s", strerror(errno));
if (freebytes)
free(bytes);
return ret;
}
int drv_mac80211_init(lorcon_t *context) {
struct mac80211_lorcon *extras =
(struct mac80211_lorcon *) malloc(sizeof(struct mac80211_lorcon));
memset(extras, 0, sizeof(struct mac80211_lorcon));
context->openinject_cb = mac80211_openmon_cb;
context->openmon_cb = mac80211_openmon_cb;
context->openinjmon_cb = mac80211_openmon_cb;
context->sendpacket_cb = mac80211_sendpacket;
context->setchan_cb = mac80211_setchan_cb;
context->getchan_cb = mac80211_getchan_cb;
context->getmac_cb = mac80211_getmac_cb;
context->setmac_cb = mac80211_setmac_cb;
context->auxptr = extras;
return 1;
}
int drv_mac80211_probe(const char *interface) {
/* key driver detection entirely off the phy80211 /sys attribute */
if (ifconfig_get_sysattr(interface, "phy80211"))
return 1;
return 0;
}
lorcon_driver_t *drv_mac80211_listdriver(lorcon_driver_t *head) {
lorcon_driver_t *d = (lorcon_driver_t *) malloc(sizeof(lorcon_driver_t));
d->name = strdup("mac80211");
d->details = strdup("Linux mac80211 kernel drivers, includes all in-kernel "
"drivers on modern systems");
d->init_func = drv_mac80211_init;
d->probe_func = drv_mac80211_probe;
d->next = head;
return d;
}
#endif