This repository was archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwireless.c
307 lines (240 loc) · 6.92 KB
/
wireless.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
/*
* Copyright (c) 2015, 2016, 2017, 2018 Gregor Best <gbe@unobtanium.de>
* Parts Copyright (c) 2013, 2014, OpenBSD project
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <err.h>
#include <libgen.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/queue.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
#include "config.h"
#define ARRAY_SIZE(x) ((sizeof(x)) / sizeof(*x))
#define SCANSZ 512
/* Selects the first network in `cnf->networks` that is in `nr` by ESSID and
* annotates it with the bssid of the scanned AP. The selected network is then
* returned
*/
struct network *
select_network(struct config *cnf, struct ieee80211_nodereq *nr, int numr) {
int i;
for (i = 0; i < numr; i++) {
struct network *n;
TAILQ_FOREACH(n, &cnf->networks, networks) {
if (!strncmp(n->nwid, (char*) nr[i].nr_nwid, IEEE80211_NWID_LEN)) {
memcpy(n->bssid, nr[i].nr_bssid, IEEE80211_ADDR_LEN);
return n;
}
}
}
return NULL;
}
/* This function comes from OpenBSD's /usr/src/sbin/ifconfig/ifconfig.c */
int
rssicmp(const void *a, const void *b) {
const struct ieee80211_nodereq *n1 = a, *n2 = b;
int r1 = n1->nr_rssi;
int r2 = n2->nr_rssi;
return r2 < r1 ? -1 : r2 > r1;
}
#define DPRINTF(...) do { \
if (cnf->debug) { \
fprintf(stderr, __VA_ARGS__); \
} \
} while (0)
int
scan(struct config *cnf, struct ieee80211_nodereq *nr, int nrlen) {
struct ieee80211_nodereq_all na;
struct ifreq ifr;
int s;
assert(nrlen > 0);
DPRINTF("device %s up\n", cnf->device);
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
err(1, "socket");
memset(&ifr, 0x00, sizeof(ifr));
(void) strlcpy(ifr.ifr_name, cnf->device, sizeof(ifr.ifr_name));
DPRINTF("running SIOCS80211SCAN ioctl\n");
if (ioctl(s, SIOCS80211SCAN, &ifr) != 0)
err(1, "ioctl");
DPRINTF("SIOCS80211SCAN done\n");
memset(&na, 0x00, sizeof(na));
memset(nr, 0x00, sizeof(nr) * nrlen);
na.na_node = nr;
na.na_size = nrlen * sizeof(*nr);
(void) strlcpy(na.na_ifname, cnf->device, sizeof(na.na_ifname));
DPRINTF("running SIOCG80211ALLNODES ioctl\n");
if (ioctl(s, SIOCG80211ALLNODES, &na) != 0)
err(1, "ioctl");
DPRINTF("SIOCG80211ALLNODES done\n");
if (pledge("stdio proc exec rpath wpath cpath", NULL) == -1) {
perror("pledge");
exit(2);
}
if (close(s) != 0)
err(1, "close");
qsort(nr, na.na_nodes, sizeof(*nr), rssicmp);
return na.na_nodes;
}
void
configure_network(struct config *cnf, struct network *nw) {
pid_t child;
struct ether_addr ea;
char *params[13]; /* Maximum number of ifconfig/wpa_cli parameters */
if (!nw) {
return;
}
memcpy(&ea.ether_addr_octet, nw->bssid, sizeof(ea.ether_addr_octet));
/* Common parameters for all configuration options */
params[0] = "ifconfig";
params[1] = cnf->device;
params[2] = "nwid";
params[3] = nw->nwid;
params[4] = "bssid";
params[5] = ether_ntoa(&ea);
params[6] = "-chan"; /* Autoselect channel */
/* three options: open wifi, wpa/wpa2 or 802.1X */
switch (nw->type) {
case NW_OPEN:
params[7] = "-wpa";
params[8] = "-wpakey";
params[9] = NULL;
break;
case NW_WPA2:
case NW_8021X:
params[7] = "wpa";
params[8] = "wpaakms";
if (nw->type == NW_WPA2) {
params[9] = "psk";
params[10] = "wpakey";
params[11] = nw->wpakey;
params[12] = NULL;
} else {
params[9] = "802.1x";
params[10] = NULL;
}
break;
default:
errx(1, "BUG: Unknown network type %d!\n", nw->type);
}
if (cnf->verbose) {
fprintf(stderr, "configuring wireless network %s\n", nw->nwid);
}
if (posix_spawn(&child, "/sbin/ifconfig", NULL, NULL, params, NULL) != 0) {
err(1, "posix_spawn: ifconfig");
}
waitpid(child, NULL, 0);
if (nw->type == NW_8021X) {
params[0] = "wpa_cli";
params[1] = "reassoc";
params[2] = NULL;
if (posix_spawn(&child, "/usr/local/sbin/wpa_cli", NULL, NULL, params, NULL) != 0) {
err(1, "posix_spawn: wpa_cli");
}
waitpid(child, NULL, 0);
}
}
void
write_nwlist(struct config *cnf, struct ieee80211_nodereq *nr, int numnodes) {
int i, fd, ret;
char *tmpname;
if (!cnf->dump) {
DPRINTF("Won't dump network list, dump not configured\n");
return;
}
DPRINTF("Dumping scanned networks to %s now\n", cnf->dump);
ret = asprintf(&tmpname, "%s/%s.XXXXXXXXXX", dirname(cnf->dump), basename(cnf->dump));
if ((!tmpname) || (ret == -1)) {
err(1, NULL);
}
DPRINTF("tmpname=%s\n", tmpname);
/* Write out /tmp/nw-aps */
fd = mkstemp(tmpname);
if (fd < 0) {
err(1, "mkstemp");
}
dprintf(fd, "BSSID% 12c\tRSSI\tENC\tKNOWN\tNWID\n", ' ');
for (i = 0; i < numnodes; i++) {
char nwid[IEEE80211_NWID_LEN + 1];
struct ether_addr ea;
int len, enc, known;
struct network *n;
nwid[IEEE80211_NWID_LEN] = 0x00;
len = nr[i].nr_nwid_len;
if (len > IEEE80211_NWID_LEN)
len = IEEE80211_NWID_LEN;
memcpy(nwid, nr[i].nr_nwid, len);
nwid[len] = 0x00;
memcpy(&ea.ether_addr_octet, nr[i].nr_bssid, sizeof(ea.ether_addr_octet));
enc = nr[i].nr_capinfo & IEEE80211_CAPINFO_PRIVACY;
known = 0;
TAILQ_FOREACH(n, &cnf->networks, networks) {
if (!strncmp(n->nwid, (char*) nr[i].nr_nwid, IEEE80211_NWID_LEN)) {
known = 1;
break;
}
}
/* bssid signal strength enc? known? nwid */
dprintf(fd, "%s\t%d\t%s\t%sknown\t%s\n", ether_ntoa(&ea), nr[i].nr_rssi,
enc? "enc": "open", known? "": "un", nwid);
}
close(fd);
if (rename(tmpname, cnf->dump) == -1)
err(1, "rename");
free(tmpname);
}
void
free_config(struct config *cnf) {
struct network *n, *tmp;
TAILQ_FOREACH_SAFE(n, &cnf->networks, networks, tmp) {
free(n->nwid);
free(n->wpakey);
free(n);
}
free(cnf->device);
free(cnf);
}
int
main(int argc, char **argv) {
struct config *cnf;
struct ieee80211_nodereq nr[SCANSZ];
int numnodes;
if (argc == 2)
cnf = parse_config(argv[1]);
else
cnf = parse_config("/etc/wireless.conf");
if (!cnf) {
return 1;
}
if (!cnf->device) {
free_config(cnf);
errx(1, "No device specified");
}
memset(nr, 0x00, ARRAY_SIZE(nr));
numnodes = scan(cnf, nr, ARRAY_SIZE(nr));
configure_network(cnf, select_network(cnf, nr, numnodes));
write_nwlist(cnf, nr, numnodes);
free_config(cnf);
return 0;
}