-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmadwifing_control.c
216 lines (169 loc) · 5.1 KB
/
madwifing_control.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
/*
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"
#ifdef SYS_LINUX
#include "madwifing_control.h"
#include <sys/types.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <getopt.h>
#include <err.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include "tx80211.h"
#include "ifcontrol_linux.h"
struct madwifi_vaps *madwifing_list_vaps(const char *ifname, char *errstr) {
DIR *devdir;
struct dirent *devfile;
char dirpath[1024], owner[512];
struct madwifi_vaps *vlist;
int numv = 0;
snprintf(dirpath, 1024, "/sys/class/net/%s/device/", ifname);
if ((devdir = opendir(dirpath)) == NULL) {
snprintf(errstr, TX80211_STATUS_MAX, "madwifing sys directory open failed: %s %s",
dirpath, strerror(errno));
return NULL;
}
while ((devfile = readdir(devdir)) != NULL) {
snprintf(owner, 512, "net:%s", ifname);
if (strncmp("net:", devfile->d_name, 4) == 0 &&
strcmp(devfile->d_name, owner))
numv++;
}
rewinddir(devdir);
vlist = (struct madwifi_vaps *) malloc(sizeof(struct madwifi_vaps));
vlist->vaplist = (char **) malloc(sizeof(char *) * numv);
vlist->vaplen = numv;
numv = 0;
while ((devfile = readdir(devdir)) != NULL) {
snprintf(owner, 512, "net:%s", ifname);
if (strncmp("net:", devfile->d_name, 4) == 0 &&
strcmp(devfile->d_name, owner))
vlist->vaplist[numv++] = strdup(devfile->d_name + 4);
}
return vlist;
}
void madwifing_free_vaps(struct madwifi_vaps *in_vaplist) {
int n = 0;
for (n = 0; n < in_vaplist->vaplen; n++) {
free(in_vaplist->vaplist[n]);
}
free(in_vaplist->vaplist);
free(in_vaplist);
}
int madwifing_destroy_vap(const char *ifname, char *errstr) {
struct ifreq ifr;
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
snprintf(errstr, TX80211_STATUS_MAX, "Failed to create socket to madwifi: %s",
strerror(errno));
return -1;
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(sock, SIOC80211IFDESTROY, &ifr) < 0) {
snprintf(errstr, TX80211_STATUS_MAX, "Failed to destroy VAP: %s", strerror(errno));
close(sock);
return -1;
}
close(sock);
return 1;
}
int madwifing_build_vap(const char *ifname, char *errstr, const char *vapname,
char *retvapname, int vapmode, int vapflags) {
struct ieee80211_clone_params {
char icp_name[IFNAMSIZ];
uint16_t icp_opmode;
uint16_t icp_flags;
};
struct ieee80211_clone_params cp;
struct ifreq ifr;
int sock;
char tnam[IFNAMSIZ];
int n;
// Find a numbered vapname which is useable
for (n = 0; n < 10; n++) {
short fl;
snprintf(tnam, IFNAMSIZ, "%s%d", vapname, n);
if (ifconfig_get_flags(tnam, errstr, &fl) < 0)
break;
// Default to no temp name as error
tnam[0] = '\0';
}
if (tnam[0] == '\0') {
snprintf(errstr, 1024, "Unable to find free slot for VAP %s", vapname);
return -1;
}
memset(&ifr, 0, sizeof(ifr));
memset(&cp, 0, sizeof(cp));
strncpy(cp.icp_name, tnam, IFNAMSIZ);
cp.icp_opmode = vapmode;
cp.icp_flags = vapflags;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_data = (caddr_t) &cp;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
snprintf(errstr, 1024, "Unable to create socket to madwifi-ng: %s",
strerror(errno));
return -1;
}
if (ioctl(sock, SIOC80211IFCREATE, &ifr) < 0) {
snprintf(errstr, 1024, "Unable to create VAP: %s", strerror(errno));
close(sock);
return -1;
}
if (madwifing_setdevtype(ifr.ifr_name, ARPHDR_RADIOTAP, errstr) < 0) {
return -1;
}
strncpy(retvapname, ifr.ifr_name, IFNAMSIZ);
close(sock);
return 1;
}
/*
* Set the device link type for the named interface by changing the dev_type
* file in the sys filesystem.
*/
int madwifing_setdevtype(const char *ifname, char *devtype, char *errstr)
{
FILE *fp;
char athdevpath[64];
int ret;
snprintf(athdevpath, 64, "/proc/sys/net/%s/dev_type", ifname);
fp = fopen(athdevpath, "w");
if (!fp) {
snprintf(errstr, TX80211_STATUS_MAX, "Error setting madwifi-ng "
"capture header type, unable to open proc device \"%s\"",
athdevpath);
return -1;
}
ret = fprintf(fp, "%s\n", devtype);
if (ret < 0) {
snprintf(errstr, TX80211_STATUS_MAX, "Error setting madwifi-ng "
"capture header type, unable to write to proc device \"%s\"",
athdevpath);
return ret;
}
fclose(fp);
return 0;
}
#endif /* linux */